Update ChangeLog and version files for release
[official-gcc.git] / gcc / ada / sem_ch3.adb
blob7c3f7e601c0ac7a1a11d80e4adafc3034bedfe72
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 Ignore_Freezing : Boolean;
2494 -- Flag set when deciding to freeze an expression function in the
2495 -- current scope.
2497 -- Start of processing for Analyze_Declarations
2499 begin
2500 if Restriction_Check_Required (SPARK_05) then
2501 Check_Later_Vs_Basic_Declarations (L, During_Parsing => False);
2502 end if;
2504 Decl := First (L);
2505 while Present (Decl) loop
2507 -- Package spec cannot contain a package declaration in SPARK
2509 if Nkind (Decl) = N_Package_Declaration
2510 and then Nkind (Parent (L)) = N_Package_Specification
2511 then
2512 Check_SPARK_05_Restriction
2513 ("package specification cannot contain a package declaration",
2514 Decl);
2515 end if;
2517 -- Complete analysis of declaration
2519 Analyze (Decl);
2520 Next_Decl := Next (Decl);
2522 if No (Freeze_From) then
2523 Freeze_From := First_Entity (Current_Scope);
2524 end if;
2526 -- At the end of a declarative part, freeze remaining entities
2527 -- declared in it. The end of the visible declarations of package
2528 -- specification is not the end of a declarative part if private
2529 -- declarations are present. The end of a package declaration is a
2530 -- freezing point only if it a library package. A task definition or
2531 -- protected type definition is not a freeze point either. Finally,
2532 -- we do not freeze entities in generic scopes, because there is no
2533 -- code generated for them and freeze nodes will be generated for
2534 -- the instance.
2536 -- The end of a package instantiation is not a freeze point, but
2537 -- for now we make it one, because the generic body is inserted
2538 -- (currently) immediately after. Generic instantiations will not
2539 -- be a freeze point once delayed freezing of bodies is implemented.
2540 -- (This is needed in any case for early instantiations ???).
2542 if No (Next_Decl) then
2543 if Nkind (Parent (L)) = N_Component_List then
2544 null;
2546 elsif Nkind_In (Parent (L), N_Protected_Definition,
2547 N_Task_Definition)
2548 then
2549 Check_Entry_Contracts;
2551 elsif Nkind (Parent (L)) /= N_Package_Specification then
2552 if Nkind (Parent (L)) = N_Package_Body then
2553 Freeze_From := First_Entity (Current_Scope);
2554 end if;
2556 -- There may have been several freezing points previously,
2557 -- for example object declarations or subprogram bodies, but
2558 -- at the end of a declarative part we check freezing from
2559 -- the beginning, even though entities may already be frozen,
2560 -- in order to perform visibility checks on delayed aspects.
2562 Adjust_Decl;
2563 Freeze_All (First_Entity (Current_Scope), Decl);
2564 Freeze_From := Last_Entity (Current_Scope);
2566 -- Current scope is a package specification
2568 elsif Scope (Current_Scope) /= Standard_Standard
2569 and then not Is_Child_Unit (Current_Scope)
2570 and then No (Generic_Parent (Parent (L)))
2571 then
2572 -- This is needed in all cases to catch visibility errors in
2573 -- aspect expressions, but several large user tests are now
2574 -- rejected. Pending notification we restrict this call to
2575 -- ASIS mode.
2577 if False and then ASIS_Mode then -- ????
2578 Resolve_Aspects;
2579 end if;
2581 elsif L /= Visible_Declarations (Parent (L))
2582 or else No (Private_Declarations (Parent (L)))
2583 or else Is_Empty_List (Private_Declarations (Parent (L)))
2584 then
2585 Adjust_Decl;
2587 -- End of a package declaration
2589 -- In compilation mode the expansion of freeze node takes care
2590 -- of resolving expressions of all aspects in the list. In ASIS
2591 -- mode this must be done explicitly.
2593 if ASIS_Mode
2594 and then Scope (Current_Scope) = Standard_Standard
2595 then
2596 Resolve_Aspects;
2597 end if;
2599 -- This is a freeze point because it is the end of a
2600 -- compilation unit.
2602 Freeze_All (First_Entity (Current_Scope), Decl);
2603 Freeze_From := Last_Entity (Current_Scope);
2605 -- At the end of the visible declarations the expressions in
2606 -- aspects of all entities declared so far must be resolved.
2607 -- The entities themselves might be frozen later, and the
2608 -- generated pragmas and attribute definition clauses analyzed
2609 -- in full at that point, but name resolution must take place
2610 -- now.
2611 -- In addition to being the proper semantics, this is mandatory
2612 -- within generic units, because global name capture requires
2613 -- those expressions to be analyzed, given that the generated
2614 -- pragmas do not appear in the original generic tree.
2616 elsif Serious_Errors_Detected = 0 then
2617 Resolve_Aspects;
2618 end if;
2620 -- If next node is a body then freeze all types before the body.
2621 -- An exception occurs for some expander-generated bodies. If these
2622 -- are generated at places where in general language rules would not
2623 -- allow a freeze point, then we assume that the expander has
2624 -- explicitly checked that all required types are properly frozen,
2625 -- and we do not cause general freezing here. This special circuit
2626 -- is used when the encountered body is marked as having already
2627 -- been analyzed.
2629 -- In all other cases (bodies that come from source, and expander
2630 -- generated bodies that have not been analyzed yet), freeze all
2631 -- types now. Note that in the latter case, the expander must take
2632 -- care to attach the bodies at a proper place in the tree so as to
2633 -- not cause unwanted freezing at that point.
2635 elsif not Analyzed (Next_Decl) and then Is_Body (Next_Decl) then
2637 -- Check for an edge case that may cause premature freezing of
2638 -- a private type. If there is a type which depends on another
2639 -- private type from an enclosing package that is in the same
2640 -- scope as a non-completing expression function then we cannot
2641 -- freeze here.
2643 Ignore_Freezing := False;
2645 if Nkind (Next_Decl) = N_Subprogram_Body
2646 and then Was_Expression_Function (Next_Decl)
2647 and then not Is_Compilation_Unit (Current_Scope)
2648 and then not Is_Generic_Instance (Current_Scope)
2649 then
2650 -- Loop through all entities in the current scope to identify
2651 -- an instance of the edge case outlined above and ignore
2652 -- freezing if it is detected.
2654 declare
2655 Curr : Entity_Id := First_Entity (Current_Scope);
2656 begin
2657 loop
2658 if Nkind (Curr) in N_Entity
2659 and then Depends_On_Private (Curr)
2660 then
2661 Ignore_Freezing := True;
2662 exit;
2663 end if;
2665 exit when Last_Entity (Current_Scope) = Curr;
2666 Curr := Next_Entity (Curr);
2667 end loop;
2668 end;
2669 end if;
2671 if not Ignore_Freezing then
2673 -- When a controlled type is frozen, the expander generates
2674 -- stream and controlled-type support routines. If the freeze
2675 -- is caused by the stand-alone body of Initialize, Adjust, or
2676 -- Finalize, the expander will end up using the wrong version
2677 -- of these routines, as the body has not been processed yet.
2678 -- To remedy this, detect a late controlled primitive and
2679 -- create a proper spec for it. This ensures that the primitive
2680 -- will override its inherited counterpart before the freeze
2681 -- takes place.
2683 -- If the declaration we just processed is a body, do not
2684 -- attempt to examine Next_Decl as the late primitive idiom can
2685 -- only apply to the first encountered body.
2687 -- The spec of the late primitive is not generated in ASIS mode
2688 -- to ensure a consistent list of primitives that indicates the
2689 -- true semantic structure of the program (which is not
2690 -- relevant when generating executable code).
2692 -- ??? A cleaner approach may be possible and/or this solution
2693 -- could be extended to general-purpose late primitives, TBD.
2695 if not ASIS_Mode
2696 and then not Body_Seen
2697 and then not Is_Body (Decl)
2698 then
2699 Body_Seen := True;
2701 if Nkind (Next_Decl) = N_Subprogram_Body then
2702 Handle_Late_Controlled_Primitive (Next_Decl);
2703 end if;
2704 end if;
2706 Adjust_Decl;
2708 -- The generated body of an expression function does not
2709 -- freeze, unless it is a completion, in which case only the
2710 -- expression itself freezes. This is handled when the body
2711 -- itself is analyzed (see Freeze_Expr_Types, sem_ch6.adb).
2713 Freeze_All (Freeze_From, Decl);
2714 Freeze_From := Last_Entity (Current_Scope);
2715 end if;
2716 end if;
2718 Decl := Next_Decl;
2719 end loop;
2721 -- Post-freezing actions
2723 if Present (L) then
2724 Context := Parent (L);
2726 -- Analyze the contracts of packages and their bodies
2728 if Nkind (Context) = N_Package_Specification then
2730 -- When a package has private declarations, its contract must be
2731 -- analyzed at the end of the said declarations. This way both the
2732 -- analysis and freeze actions are properly synchronized in case
2733 -- of private type use within the contract.
2735 if L = Private_Declarations (Context) then
2736 Analyze_Package_Contract (Defining_Entity (Context));
2738 -- Otherwise the contract is analyzed at the end of the visible
2739 -- declarations.
2741 elsif L = Visible_Declarations (Context)
2742 and then No (Private_Declarations (Context))
2743 then
2744 Analyze_Package_Contract (Defining_Entity (Context));
2745 end if;
2747 elsif Nkind (Context) = N_Package_Body then
2748 Analyze_Package_Body_Contract (Defining_Entity (Context));
2749 end if;
2751 -- Analyze the contracts of various constructs now due to the delayed
2752 -- visibility needs of their aspects and pragmas.
2754 Analyze_Contracts (L);
2756 if Nkind (Context) = N_Package_Body then
2758 -- Ensure that all abstract states and objects declared in the
2759 -- state space of a package body are utilized as constituents.
2761 Check_Unused_Body_States (Defining_Entity (Context));
2763 -- State refinements are visible up to the end of the package body
2764 -- declarations. Hide the state refinements from visibility to
2765 -- restore the original state conditions.
2767 Remove_Visible_Refinements (Corresponding_Spec (Context));
2768 Remove_Partial_Visible_Refinements (Corresponding_Spec (Context));
2770 elsif Nkind (Context) = N_Package_Declaration then
2772 -- Partial state refinements are visible up to the end of the
2773 -- package spec declarations. Hide the partial state refinements
2774 -- from visibility to restore the original state conditions.
2776 Remove_Partial_Visible_Refinements (Corresponding_Spec (Context));
2777 end if;
2779 -- Verify that all abstract states found in any package declared in
2780 -- the input declarative list have proper refinements. The check is
2781 -- performed only when the context denotes a block, entry, package,
2782 -- protected, subprogram, or task body (SPARK RM 7.2.2(3)).
2784 Check_State_Refinements (Context);
2786 -- Create the subprogram bodies which verify the run-time semantics
2787 -- of pragmas Default_Initial_Condition and [Type_]Invariant for all
2788 -- types within the current declarative list. This ensures that all
2789 -- assertion expressions are preanalyzed and resolved at the end of
2790 -- the declarative part. Note that the resolution happens even when
2791 -- freezing does not take place.
2793 Build_Assertion_Bodies (L, Context);
2794 end if;
2795 end Analyze_Declarations;
2797 -----------------------------------
2798 -- Analyze_Full_Type_Declaration --
2799 -----------------------------------
2801 procedure Analyze_Full_Type_Declaration (N : Node_Id) is
2802 Def : constant Node_Id := Type_Definition (N);
2803 Def_Id : constant Entity_Id := Defining_Identifier (N);
2804 T : Entity_Id;
2805 Prev : Entity_Id;
2807 Is_Remote : constant Boolean :=
2808 (Is_Remote_Types (Current_Scope)
2809 or else Is_Remote_Call_Interface (Current_Scope))
2810 and then not (In_Private_Part (Current_Scope)
2811 or else In_Package_Body (Current_Scope));
2813 procedure Check_Nonoverridable_Aspects;
2814 -- Apply the rule in RM 13.1.1(18.4/4) on iterator aspects that cannot
2815 -- be overridden, and can only be confirmed on derivation.
2817 procedure Check_Ops_From_Incomplete_Type;
2818 -- If there is a tagged incomplete partial view of the type, traverse
2819 -- the primitives of the incomplete view and change the type of any
2820 -- controlling formals and result to indicate the full view. The
2821 -- primitives will be added to the full type's primitive operations
2822 -- list later in Sem_Disp.Check_Operation_From_Incomplete_Type (which
2823 -- is called from Process_Incomplete_Dependents).
2825 ----------------------------------
2826 -- Check_Nonoverridable_Aspects --
2827 ----------------------------------
2829 procedure Check_Nonoverridable_Aspects is
2830 function Get_Aspect_Spec
2831 (Specs : List_Id;
2832 Aspect_Name : Name_Id) return Node_Id;
2833 -- Check whether a list of aspect specifications includes an entry
2834 -- for a specific aspect. The list is either that of a partial or
2835 -- a full view.
2837 ---------------------
2838 -- Get_Aspect_Spec --
2839 ---------------------
2841 function Get_Aspect_Spec
2842 (Specs : List_Id;
2843 Aspect_Name : Name_Id) return Node_Id
2845 Spec : Node_Id;
2847 begin
2848 Spec := First (Specs);
2849 while Present (Spec) loop
2850 if Chars (Identifier (Spec)) = Aspect_Name then
2851 return Spec;
2852 end if;
2853 Next (Spec);
2854 end loop;
2856 return Empty;
2857 end Get_Aspect_Spec;
2859 -- Local variables
2861 Prev_Aspects : constant List_Id :=
2862 Aspect_Specifications (Parent (Def_Id));
2863 Par_Type : Entity_Id;
2864 Prev_Aspect : Node_Id;
2866 -- Start of processing for Check_Nonoverridable_Aspects
2868 begin
2869 -- Get parent type of derived type. Note that Prev is the entity in
2870 -- the partial declaration, but its contents are now those of full
2871 -- view, while Def_Id reflects the partial view.
2873 if Is_Private_Type (Def_Id) then
2874 Par_Type := Etype (Full_View (Def_Id));
2875 else
2876 Par_Type := Etype (Def_Id);
2877 end if;
2879 -- If there is an inherited Implicit_Dereference, verify that it is
2880 -- made explicit in the partial view.
2882 if Has_Discriminants (Base_Type (Par_Type))
2883 and then Nkind (Parent (Prev)) = N_Full_Type_Declaration
2884 and then Present (Discriminant_Specifications (Parent (Prev)))
2885 and then Present (Get_Reference_Discriminant (Par_Type))
2886 then
2887 Prev_Aspect :=
2888 Get_Aspect_Spec (Prev_Aspects, Name_Implicit_Dereference);
2890 if No (Prev_Aspect)
2891 and then Present
2892 (Discriminant_Specifications
2893 (Original_Node (Parent (Prev))))
2894 then
2895 Error_Msg_N
2896 ("type does not inherit implicit dereference", Prev);
2898 else
2899 -- If one of the views has the aspect specified, verify that it
2900 -- is consistent with that of the parent.
2902 declare
2903 Par_Discr : constant Entity_Id :=
2904 Get_Reference_Discriminant (Par_Type);
2905 Cur_Discr : constant Entity_Id :=
2906 Get_Reference_Discriminant (Prev);
2908 begin
2909 if Corresponding_Discriminant (Cur_Discr) /= Par_Discr then
2910 Error_Msg_N ("aspect incosistent with that of parent", N);
2911 end if;
2913 -- Check that specification in partial view matches the
2914 -- inherited aspect. Compare names directly because aspect
2915 -- expression may not be analyzed.
2917 if Present (Prev_Aspect)
2918 and then Nkind (Expression (Prev_Aspect)) = N_Identifier
2919 and then Chars (Expression (Prev_Aspect)) /=
2920 Chars (Cur_Discr)
2921 then
2922 Error_Msg_N
2923 ("aspect incosistent with that of parent", N);
2924 end if;
2925 end;
2926 end if;
2927 end if;
2929 -- TBD : other nonoverridable aspects.
2930 end Check_Nonoverridable_Aspects;
2932 ------------------------------------
2933 -- Check_Ops_From_Incomplete_Type --
2934 ------------------------------------
2936 procedure Check_Ops_From_Incomplete_Type is
2937 Elmt : Elmt_Id;
2938 Formal : Entity_Id;
2939 Op : Entity_Id;
2941 begin
2942 if Prev /= T
2943 and then Ekind (Prev) = E_Incomplete_Type
2944 and then Is_Tagged_Type (Prev)
2945 and then Is_Tagged_Type (T)
2946 then
2947 Elmt := First_Elmt (Primitive_Operations (Prev));
2948 while Present (Elmt) loop
2949 Op := Node (Elmt);
2951 Formal := First_Formal (Op);
2952 while Present (Formal) loop
2953 if Etype (Formal) = Prev then
2954 Set_Etype (Formal, T);
2955 end if;
2957 Next_Formal (Formal);
2958 end loop;
2960 if Etype (Op) = Prev then
2961 Set_Etype (Op, T);
2962 end if;
2964 Next_Elmt (Elmt);
2965 end loop;
2966 end if;
2967 end Check_Ops_From_Incomplete_Type;
2969 -- Start of processing for Analyze_Full_Type_Declaration
2971 begin
2972 Prev := Find_Type_Name (N);
2974 -- The full view, if present, now points to the current type. If there
2975 -- is an incomplete partial view, set a link to it, to simplify the
2976 -- retrieval of primitive operations of the type.
2978 -- Ada 2005 (AI-50217): If the type was previously decorated when
2979 -- imported through a LIMITED WITH clause, it appears as incomplete
2980 -- but has no full view.
2982 if Ekind (Prev) = E_Incomplete_Type
2983 and then Present (Full_View (Prev))
2984 then
2985 T := Full_View (Prev);
2986 Set_Incomplete_View (N, Parent (Prev));
2987 else
2988 T := Prev;
2989 end if;
2991 Set_Is_Pure (T, Is_Pure (Current_Scope));
2993 -- We set the flag Is_First_Subtype here. It is needed to set the
2994 -- corresponding flag for the Implicit class-wide-type created
2995 -- during tagged types processing.
2997 Set_Is_First_Subtype (T, True);
2999 -- Only composite types other than array types are allowed to have
3000 -- discriminants.
3002 case Nkind (Def) is
3004 -- For derived types, the rule will be checked once we've figured
3005 -- out the parent type.
3007 when N_Derived_Type_Definition =>
3008 null;
3010 -- For record types, discriminants are allowed, unless we are in
3011 -- SPARK.
3013 when N_Record_Definition =>
3014 if Present (Discriminant_Specifications (N)) then
3015 Check_SPARK_05_Restriction
3016 ("discriminant type is not allowed",
3017 Defining_Identifier
3018 (First (Discriminant_Specifications (N))));
3019 end if;
3021 when others =>
3022 if Present (Discriminant_Specifications (N)) then
3023 Error_Msg_N
3024 ("elementary or array type cannot have discriminants",
3025 Defining_Identifier
3026 (First (Discriminant_Specifications (N))));
3027 end if;
3028 end case;
3030 -- Elaborate the type definition according to kind, and generate
3031 -- subsidiary (implicit) subtypes where needed. We skip this if it was
3032 -- already done (this happens during the reanalysis that follows a call
3033 -- to the high level optimizer).
3035 if not Analyzed (T) then
3036 Set_Analyzed (T);
3038 case Nkind (Def) is
3039 when N_Access_To_Subprogram_Definition =>
3040 Access_Subprogram_Declaration (T, Def);
3042 -- If this is a remote access to subprogram, we must create the
3043 -- equivalent fat pointer type, and related subprograms.
3045 if Is_Remote then
3046 Process_Remote_AST_Declaration (N);
3047 end if;
3049 -- Validate categorization rule against access type declaration
3050 -- usually a violation in Pure unit, Shared_Passive unit.
3052 Validate_Access_Type_Declaration (T, N);
3054 when N_Access_To_Object_Definition =>
3055 Access_Type_Declaration (T, Def);
3057 -- Validate categorization rule against access type declaration
3058 -- usually a violation in Pure unit, Shared_Passive unit.
3060 Validate_Access_Type_Declaration (T, N);
3062 -- If we are in a Remote_Call_Interface package and define a
3063 -- RACW, then calling stubs and specific stream attributes
3064 -- must be added.
3066 if Is_Remote
3067 and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
3068 then
3069 Add_RACW_Features (Def_Id);
3070 end if;
3072 when N_Array_Type_Definition =>
3073 Array_Type_Declaration (T, Def);
3075 when N_Derived_Type_Definition =>
3076 Derived_Type_Declaration (T, N, T /= Def_Id);
3078 when N_Enumeration_Type_Definition =>
3079 Enumeration_Type_Declaration (T, Def);
3081 when N_Floating_Point_Definition =>
3082 Floating_Point_Type_Declaration (T, Def);
3084 when N_Decimal_Fixed_Point_Definition =>
3085 Decimal_Fixed_Point_Type_Declaration (T, Def);
3087 when N_Ordinary_Fixed_Point_Definition =>
3088 Ordinary_Fixed_Point_Type_Declaration (T, Def);
3090 when N_Signed_Integer_Type_Definition =>
3091 Signed_Integer_Type_Declaration (T, Def);
3093 when N_Modular_Type_Definition =>
3094 Modular_Type_Declaration (T, Def);
3096 when N_Record_Definition =>
3097 Record_Type_Declaration (T, N, Prev);
3099 -- If declaration has a parse error, nothing to elaborate.
3101 when N_Error =>
3102 null;
3104 when others =>
3105 raise Program_Error;
3106 end case;
3107 end if;
3109 if Etype (T) = Any_Type then
3110 return;
3111 end if;
3113 -- Controlled type is not allowed in SPARK
3115 if Is_Visibly_Controlled (T) then
3116 Check_SPARK_05_Restriction ("controlled type is not allowed", N);
3117 end if;
3119 -- Some common processing for all types
3121 Set_Depends_On_Private (T, Has_Private_Component (T));
3122 Check_Ops_From_Incomplete_Type;
3124 -- Both the declared entity, and its anonymous base type if one was
3125 -- created, need freeze nodes allocated.
3127 declare
3128 B : constant Entity_Id := Base_Type (T);
3130 begin
3131 -- In the case where the base type differs from the first subtype, we
3132 -- pre-allocate a freeze node, and set the proper link to the first
3133 -- subtype. Freeze_Entity will use this preallocated freeze node when
3134 -- it freezes the entity.
3136 -- This does not apply if the base type is a generic type, whose
3137 -- declaration is independent of the current derived definition.
3139 if B /= T and then not Is_Generic_Type (B) then
3140 Ensure_Freeze_Node (B);
3141 Set_First_Subtype_Link (Freeze_Node (B), T);
3142 end if;
3144 -- A type that is imported through a limited_with clause cannot
3145 -- generate any code, and thus need not be frozen. However, an access
3146 -- type with an imported designated type needs a finalization list,
3147 -- which may be referenced in some other package that has non-limited
3148 -- visibility on the designated type. Thus we must create the
3149 -- finalization list at the point the access type is frozen, to
3150 -- prevent unsatisfied references at link time.
3152 if not From_Limited_With (T) or else Is_Access_Type (T) then
3153 Set_Has_Delayed_Freeze (T);
3154 end if;
3155 end;
3157 -- Case where T is the full declaration of some private type which has
3158 -- been swapped in Defining_Identifier (N).
3160 if T /= Def_Id and then Is_Private_Type (Def_Id) then
3161 Process_Full_View (N, T, Def_Id);
3163 -- Record the reference. The form of this is a little strange, since
3164 -- the full declaration has been swapped in. So the first parameter
3165 -- here represents the entity to which a reference is made which is
3166 -- the "real" entity, i.e. the one swapped in, and the second
3167 -- parameter provides the reference location.
3169 -- Also, we want to kill Has_Pragma_Unreferenced temporarily here
3170 -- since we don't want a complaint about the full type being an
3171 -- unwanted reference to the private type
3173 declare
3174 B : constant Boolean := Has_Pragma_Unreferenced (T);
3175 begin
3176 Set_Has_Pragma_Unreferenced (T, False);
3177 Generate_Reference (T, T, 'c');
3178 Set_Has_Pragma_Unreferenced (T, B);
3179 end;
3181 Set_Completion_Referenced (Def_Id);
3183 -- For completion of incomplete type, process incomplete dependents
3184 -- and always mark the full type as referenced (it is the incomplete
3185 -- type that we get for any real reference).
3187 elsif Ekind (Prev) = E_Incomplete_Type then
3188 Process_Incomplete_Dependents (N, T, Prev);
3189 Generate_Reference (Prev, Def_Id, 'c');
3190 Set_Completion_Referenced (Def_Id);
3192 -- If not private type or incomplete type completion, this is a real
3193 -- definition of a new entity, so record it.
3195 else
3196 Generate_Definition (Def_Id);
3197 end if;
3199 -- Propagate any pending access types whose finalization masters need to
3200 -- be fully initialized from the partial to the full view. Guard against
3201 -- an illegal full view that remains unanalyzed.
3203 if Is_Type (Def_Id) and then Is_Incomplete_Or_Private_Type (Prev) then
3204 Set_Pending_Access_Types (Def_Id, Pending_Access_Types (Prev));
3205 end if;
3207 if Chars (Scope (Def_Id)) = Name_System
3208 and then Chars (Def_Id) = Name_Address
3209 and then Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (N)))
3210 then
3211 Set_Is_Descendant_Of_Address (Def_Id);
3212 Set_Is_Descendant_Of_Address (Base_Type (Def_Id));
3213 Set_Is_Descendant_Of_Address (Prev);
3214 end if;
3216 Set_Optimize_Alignment_Flags (Def_Id);
3217 Check_Eliminated (Def_Id);
3219 -- If the declaration is a completion and aspects are present, apply
3220 -- them to the entity for the type which is currently the partial
3221 -- view, but which is the one that will be frozen.
3223 if Has_Aspects (N) then
3225 -- In most cases the partial view is a private type, and both views
3226 -- appear in different declarative parts. In the unusual case where
3227 -- the partial view is incomplete, perform the analysis on the
3228 -- full view, to prevent freezing anomalies with the corresponding
3229 -- class-wide type, which otherwise might be frozen before the
3230 -- dispatch table is built.
3232 if Prev /= Def_Id
3233 and then Ekind (Prev) /= E_Incomplete_Type
3234 then
3235 Analyze_Aspect_Specifications (N, Prev);
3237 -- Normal case
3239 else
3240 Analyze_Aspect_Specifications (N, Def_Id);
3241 end if;
3242 end if;
3244 if Is_Derived_Type (Prev)
3245 and then Def_Id /= Prev
3246 then
3247 Check_Nonoverridable_Aspects;
3248 end if;
3249 end Analyze_Full_Type_Declaration;
3251 ----------------------------------
3252 -- Analyze_Incomplete_Type_Decl --
3253 ----------------------------------
3255 procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
3256 F : constant Boolean := Is_Pure (Current_Scope);
3257 T : Entity_Id;
3259 begin
3260 Check_SPARK_05_Restriction ("incomplete type is not allowed", N);
3262 Generate_Definition (Defining_Identifier (N));
3264 -- Process an incomplete declaration. The identifier must not have been
3265 -- declared already in the scope. However, an incomplete declaration may
3266 -- appear in the private part of a package, for a private type that has
3267 -- already been declared.
3269 -- In this case, the discriminants (if any) must match
3271 T := Find_Type_Name (N);
3273 Set_Ekind (T, E_Incomplete_Type);
3274 Init_Size_Align (T);
3275 Set_Is_First_Subtype (T, True);
3276 Set_Etype (T, T);
3278 -- Ada 2005 (AI-326): Minimum decoration to give support to tagged
3279 -- incomplete types.
3281 if Tagged_Present (N) then
3282 Set_Is_Tagged_Type (T, True);
3283 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
3284 Make_Class_Wide_Type (T);
3285 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3286 end if;
3288 Set_Stored_Constraint (T, No_Elist);
3290 if Present (Discriminant_Specifications (N)) then
3291 Push_Scope (T);
3292 Process_Discriminants (N);
3293 End_Scope;
3294 end if;
3296 -- If the type has discriminants, nontrivial subtypes may be declared
3297 -- before the full view of the type. The full views of those subtypes
3298 -- will be built after the full view of the type.
3300 Set_Private_Dependents (T, New_Elmt_List);
3301 Set_Is_Pure (T, F);
3302 end Analyze_Incomplete_Type_Decl;
3304 -----------------------------------
3305 -- Analyze_Interface_Declaration --
3306 -----------------------------------
3308 procedure Analyze_Interface_Declaration (T : Entity_Id; Def : Node_Id) is
3309 CW : constant Entity_Id := Class_Wide_Type (T);
3311 begin
3312 Set_Is_Tagged_Type (T);
3313 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
3315 Set_Is_Limited_Record (T, Limited_Present (Def)
3316 or else Task_Present (Def)
3317 or else Protected_Present (Def)
3318 or else Synchronized_Present (Def));
3320 -- Type is abstract if full declaration carries keyword, or if previous
3321 -- partial view did.
3323 Set_Is_Abstract_Type (T);
3324 Set_Is_Interface (T);
3326 -- Type is a limited interface if it includes the keyword limited, task,
3327 -- protected, or synchronized.
3329 Set_Is_Limited_Interface
3330 (T, Limited_Present (Def)
3331 or else Protected_Present (Def)
3332 or else Synchronized_Present (Def)
3333 or else Task_Present (Def));
3335 Set_Interfaces (T, New_Elmt_List);
3336 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3338 -- Complete the decoration of the class-wide entity if it was already
3339 -- built (i.e. during the creation of the limited view)
3341 if Present (CW) then
3342 Set_Is_Interface (CW);
3343 Set_Is_Limited_Interface (CW, Is_Limited_Interface (T));
3344 end if;
3346 -- Check runtime support for synchronized interfaces
3348 if (Is_Task_Interface (T)
3349 or else Is_Protected_Interface (T)
3350 or else Is_Synchronized_Interface (T))
3351 and then not RTE_Available (RE_Select_Specific_Data)
3352 then
3353 Error_Msg_CRT ("synchronized interfaces", T);
3354 end if;
3355 end Analyze_Interface_Declaration;
3357 -----------------------------
3358 -- Analyze_Itype_Reference --
3359 -----------------------------
3361 -- Nothing to do. This node is placed in the tree only for the benefit of
3362 -- back end processing, and has no effect on the semantic processing.
3364 procedure Analyze_Itype_Reference (N : Node_Id) is
3365 begin
3366 pragma Assert (Is_Itype (Itype (N)));
3367 null;
3368 end Analyze_Itype_Reference;
3370 --------------------------------
3371 -- Analyze_Number_Declaration --
3372 --------------------------------
3374 procedure Analyze_Number_Declaration (N : Node_Id) is
3375 E : constant Node_Id := Expression (N);
3376 Id : constant Entity_Id := Defining_Identifier (N);
3377 Index : Interp_Index;
3378 It : Interp;
3379 T : Entity_Id;
3381 begin
3382 Generate_Definition (Id);
3383 Enter_Name (Id);
3385 -- This is an optimization of a common case of an integer literal
3387 if Nkind (E) = N_Integer_Literal then
3388 Set_Is_Static_Expression (E, True);
3389 Set_Etype (E, Universal_Integer);
3391 Set_Etype (Id, Universal_Integer);
3392 Set_Ekind (Id, E_Named_Integer);
3393 Set_Is_Frozen (Id, True);
3394 return;
3395 end if;
3397 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3399 -- Process expression, replacing error by integer zero, to avoid
3400 -- cascaded errors or aborts further along in the processing
3402 -- Replace Error by integer zero, which seems least likely to cause
3403 -- cascaded errors.
3405 if E = Error then
3406 Rewrite (E, Make_Integer_Literal (Sloc (E), Uint_0));
3407 Set_Error_Posted (E);
3408 end if;
3410 Analyze (E);
3412 -- Verify that the expression is static and numeric. If
3413 -- the expression is overloaded, we apply the preference
3414 -- rule that favors root numeric types.
3416 if not Is_Overloaded (E) then
3417 T := Etype (E);
3418 if Has_Dynamic_Predicate_Aspect (T) then
3419 Error_Msg_N
3420 ("subtype has dynamic predicate, "
3421 & "not allowed in number declaration", N);
3422 end if;
3424 else
3425 T := Any_Type;
3427 Get_First_Interp (E, Index, It);
3428 while Present (It.Typ) loop
3429 if (Is_Integer_Type (It.Typ) or else Is_Real_Type (It.Typ))
3430 and then (Scope (Base_Type (It.Typ))) = Standard_Standard
3431 then
3432 if T = Any_Type then
3433 T := It.Typ;
3435 elsif It.Typ = Universal_Real
3436 or else
3437 It.Typ = Universal_Integer
3438 then
3439 -- Choose universal interpretation over any other
3441 T := It.Typ;
3442 exit;
3443 end if;
3444 end if;
3446 Get_Next_Interp (Index, It);
3447 end loop;
3448 end if;
3450 if Is_Integer_Type (T) then
3451 Resolve (E, T);
3452 Set_Etype (Id, Universal_Integer);
3453 Set_Ekind (Id, E_Named_Integer);
3455 elsif Is_Real_Type (T) then
3457 -- Because the real value is converted to universal_real, this is a
3458 -- legal context for a universal fixed expression.
3460 if T = Universal_Fixed then
3461 declare
3462 Loc : constant Source_Ptr := Sloc (N);
3463 Conv : constant Node_Id := Make_Type_Conversion (Loc,
3464 Subtype_Mark =>
3465 New_Occurrence_Of (Universal_Real, Loc),
3466 Expression => Relocate_Node (E));
3468 begin
3469 Rewrite (E, Conv);
3470 Analyze (E);
3471 end;
3473 elsif T = Any_Fixed then
3474 Error_Msg_N ("illegal context for mixed mode operation", E);
3476 -- Expression is of the form : universal_fixed * integer. Try to
3477 -- resolve as universal_real.
3479 T := Universal_Real;
3480 Set_Etype (E, T);
3481 end if;
3483 Resolve (E, T);
3484 Set_Etype (Id, Universal_Real);
3485 Set_Ekind (Id, E_Named_Real);
3487 else
3488 Wrong_Type (E, Any_Numeric);
3489 Resolve (E, T);
3491 Set_Etype (Id, T);
3492 Set_Ekind (Id, E_Constant);
3493 Set_Never_Set_In_Source (Id, True);
3494 Set_Is_True_Constant (Id, True);
3495 return;
3496 end if;
3498 if Nkind_In (E, N_Integer_Literal, N_Real_Literal) then
3499 Set_Etype (E, Etype (Id));
3500 end if;
3502 if not Is_OK_Static_Expression (E) then
3503 Flag_Non_Static_Expr
3504 ("non-static expression used in number declaration!", E);
3505 Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
3506 Set_Etype (E, Any_Type);
3507 end if;
3509 Analyze_Dimension (N);
3510 end Analyze_Number_Declaration;
3512 --------------------------------
3513 -- Analyze_Object_Declaration --
3514 --------------------------------
3516 -- WARNING: This routine manages Ghost regions. Return statements must be
3517 -- replaced by gotos which jump to the end of the routine and restore the
3518 -- Ghost mode.
3520 procedure Analyze_Object_Declaration (N : Node_Id) is
3521 Loc : constant Source_Ptr := Sloc (N);
3522 Id : constant Entity_Id := Defining_Identifier (N);
3523 Act_T : Entity_Id;
3524 T : Entity_Id;
3526 E : Node_Id := Expression (N);
3527 -- E is set to Expression (N) throughout this routine. When
3528 -- Expression (N) is modified, E is changed accordingly.
3530 Prev_Entity : Entity_Id := Empty;
3532 function Count_Tasks (T : Entity_Id) return Uint;
3533 -- This function is called when a non-generic library level object of a
3534 -- task type is declared. Its function is to count the static number of
3535 -- tasks declared within the type (it is only called if Has_Task is set
3536 -- for T). As a side effect, if an array of tasks with non-static bounds
3537 -- or a variant record type is encountered, Check_Restriction is called
3538 -- indicating the count is unknown.
3540 function Delayed_Aspect_Present return Boolean;
3541 -- If the declaration has an expression that is an aggregate, and it
3542 -- has aspects that require delayed analysis, the resolution of the
3543 -- aggregate must be deferred to the freeze point of the objet. This
3544 -- special processing was created for address clauses, but it must
3545 -- also apply to Alignment. This must be done before the aspect
3546 -- specifications are analyzed because we must handle the aggregate
3547 -- before the analysis of the object declaration is complete.
3549 -- Any other relevant delayed aspects on object declarations ???
3551 -----------------
3552 -- Count_Tasks --
3553 -----------------
3555 function Count_Tasks (T : Entity_Id) return Uint is
3556 C : Entity_Id;
3557 X : Node_Id;
3558 V : Uint;
3560 begin
3561 if Is_Task_Type (T) then
3562 return Uint_1;
3564 elsif Is_Record_Type (T) then
3565 if Has_Discriminants (T) then
3566 Check_Restriction (Max_Tasks, N);
3567 return Uint_0;
3569 else
3570 V := Uint_0;
3571 C := First_Component (T);
3572 while Present (C) loop
3573 V := V + Count_Tasks (Etype (C));
3574 Next_Component (C);
3575 end loop;
3577 return V;
3578 end if;
3580 elsif Is_Array_Type (T) then
3581 X := First_Index (T);
3582 V := Count_Tasks (Component_Type (T));
3583 while Present (X) loop
3584 C := Etype (X);
3586 if not Is_OK_Static_Subtype (C) then
3587 Check_Restriction (Max_Tasks, N);
3588 return Uint_0;
3589 else
3590 V := V * (UI_Max (Uint_0,
3591 Expr_Value (Type_High_Bound (C)) -
3592 Expr_Value (Type_Low_Bound (C)) + Uint_1));
3593 end if;
3595 Next_Index (X);
3596 end loop;
3598 return V;
3600 else
3601 return Uint_0;
3602 end if;
3603 end Count_Tasks;
3605 ----------------------------
3606 -- Delayed_Aspect_Present --
3607 ----------------------------
3609 function Delayed_Aspect_Present return Boolean is
3610 A : Node_Id;
3611 A_Id : Aspect_Id;
3613 begin
3614 if Present (Aspect_Specifications (N)) then
3615 A := First (Aspect_Specifications (N));
3616 A_Id := Get_Aspect_Id (Chars (Identifier (A)));
3617 while Present (A) loop
3618 if A_Id = Aspect_Alignment or else A_Id = Aspect_Address then
3619 return True;
3620 end if;
3622 Next (A);
3623 end loop;
3624 end if;
3626 return False;
3627 end Delayed_Aspect_Present;
3629 -- Local variables
3631 Mode : Ghost_Mode_Type;
3632 Mode_Set : Boolean := False;
3633 Related_Id : Entity_Id;
3635 -- Start of processing for Analyze_Object_Declaration
3637 begin
3638 -- There are three kinds of implicit types generated by an
3639 -- object declaration:
3641 -- 1. Those generated by the original Object Definition
3643 -- 2. Those generated by the Expression
3645 -- 3. Those used to constrain the Object Definition with the
3646 -- expression constraints when the definition is unconstrained.
3648 -- They must be generated in this order to avoid order of elaboration
3649 -- issues. Thus the first step (after entering the name) is to analyze
3650 -- the object definition.
3652 if Constant_Present (N) then
3653 Prev_Entity := Current_Entity_In_Scope (Id);
3655 if Present (Prev_Entity)
3656 and then
3657 -- If the homograph is an implicit subprogram, it is overridden
3658 -- by the current declaration.
3660 ((Is_Overloadable (Prev_Entity)
3661 and then Is_Inherited_Operation (Prev_Entity))
3663 -- The current object is a discriminal generated for an entry
3664 -- family index. Even though the index is a constant, in this
3665 -- particular context there is no true constant redeclaration.
3666 -- Enter_Name will handle the visibility.
3668 or else
3669 (Is_Discriminal (Id)
3670 and then Ekind (Discriminal_Link (Id)) =
3671 E_Entry_Index_Parameter)
3673 -- The current object is the renaming for a generic declared
3674 -- within the instance.
3676 or else
3677 (Ekind (Prev_Entity) = E_Package
3678 and then Nkind (Parent (Prev_Entity)) =
3679 N_Package_Renaming_Declaration
3680 and then not Comes_From_Source (Prev_Entity)
3681 and then
3682 Is_Generic_Instance (Renamed_Entity (Prev_Entity)))
3684 -- The entity may be a homonym of a private component of the
3685 -- enclosing protected object, for which we create a local
3686 -- renaming declaration. The declaration is legal, even if
3687 -- useless when it just captures that component.
3689 or else
3690 (Ekind (Scope (Current_Scope)) = E_Protected_Type
3691 and then Nkind (Parent (Prev_Entity)) =
3692 N_Object_Renaming_Declaration))
3693 then
3694 Prev_Entity := Empty;
3695 end if;
3696 end if;
3698 if Present (Prev_Entity) then
3700 -- The object declaration is Ghost when it completes a deferred Ghost
3701 -- constant.
3703 Mark_And_Set_Ghost_Completion (N, Prev_Entity, Mode);
3704 Mode_Set := True;
3706 Constant_Redeclaration (Id, N, T);
3708 Generate_Reference (Prev_Entity, Id, 'c');
3709 Set_Completion_Referenced (Id);
3711 if Error_Posted (N) then
3713 -- Type mismatch or illegal redeclaration; do not analyze
3714 -- expression to avoid cascaded errors.
3716 T := Find_Type_Of_Object (Object_Definition (N), N);
3717 Set_Etype (Id, T);
3718 Set_Ekind (Id, E_Variable);
3719 goto Leave;
3720 end if;
3722 -- In the normal case, enter identifier at the start to catch premature
3723 -- usage in the initialization expression.
3725 else
3726 Generate_Definition (Id);
3727 Enter_Name (Id);
3729 Mark_Coextensions (N, Object_Definition (N));
3731 T := Find_Type_Of_Object (Object_Definition (N), N);
3733 if Nkind (Object_Definition (N)) = N_Access_Definition
3734 and then Present
3735 (Access_To_Subprogram_Definition (Object_Definition (N)))
3736 and then Protected_Present
3737 (Access_To_Subprogram_Definition (Object_Definition (N)))
3738 then
3739 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
3740 end if;
3742 if Error_Posted (Id) then
3743 Set_Etype (Id, T);
3744 Set_Ekind (Id, E_Variable);
3745 goto Leave;
3746 end if;
3747 end if;
3749 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
3750 -- out some static checks.
3752 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (T) then
3754 -- In case of aggregates we must also take care of the correct
3755 -- initialization of nested aggregates bug this is done at the
3756 -- point of the analysis of the aggregate (see sem_aggr.adb) ???
3758 if Present (Expression (N))
3759 and then Nkind (Expression (N)) = N_Aggregate
3760 then
3761 null;
3763 else
3764 declare
3765 Save_Typ : constant Entity_Id := Etype (Id);
3766 begin
3767 Set_Etype (Id, T); -- Temp. decoration for static checks
3768 Null_Exclusion_Static_Checks (N);
3769 Set_Etype (Id, Save_Typ);
3770 end;
3771 end if;
3772 end if;
3774 -- Object is marked pure if it is in a pure scope
3776 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3778 -- If deferred constant, make sure context is appropriate. We detect
3779 -- a deferred constant as a constant declaration with no expression.
3780 -- A deferred constant can appear in a package body if its completion
3781 -- is by means of an interface pragma.
3783 if Constant_Present (N) and then No (E) then
3785 -- A deferred constant may appear in the declarative part of the
3786 -- following constructs:
3788 -- blocks
3789 -- entry bodies
3790 -- extended return statements
3791 -- package specs
3792 -- package bodies
3793 -- subprogram bodies
3794 -- task bodies
3796 -- When declared inside a package spec, a deferred constant must be
3797 -- completed by a full constant declaration or pragma Import. In all
3798 -- other cases, the only proper completion is pragma Import. Extended
3799 -- return statements are flagged as invalid contexts because they do
3800 -- not have a declarative part and so cannot accommodate the pragma.
3802 if Ekind (Current_Scope) = E_Return_Statement then
3803 Error_Msg_N
3804 ("invalid context for deferred constant declaration (RM 7.4)",
3806 Error_Msg_N
3807 ("\declaration requires an initialization expression",
3809 Set_Constant_Present (N, False);
3811 -- In Ada 83, deferred constant must be of private type
3813 elsif not Is_Private_Type (T) then
3814 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
3815 Error_Msg_N
3816 ("(Ada 83) deferred constant must be private type", N);
3817 end if;
3818 end if;
3820 -- If not a deferred constant, then the object declaration freezes
3821 -- its type, unless the object is of an anonymous type and has delayed
3822 -- aspects. In that case the type is frozen when the object itself is.
3824 else
3825 Check_Fully_Declared (T, N);
3827 if Has_Delayed_Aspects (Id)
3828 and then Is_Array_Type (T)
3829 and then Is_Itype (T)
3830 then
3831 Set_Has_Delayed_Freeze (T);
3832 else
3833 Freeze_Before (N, T);
3834 end if;
3835 end if;
3837 -- If the object was created by a constrained array definition, then
3838 -- set the link in both the anonymous base type and anonymous subtype
3839 -- that are built to represent the array type to point to the object.
3841 if Nkind (Object_Definition (Declaration_Node (Id))) =
3842 N_Constrained_Array_Definition
3843 then
3844 Set_Related_Array_Object (T, Id);
3845 Set_Related_Array_Object (Base_Type (T), Id);
3846 end if;
3848 -- Special checks for protected objects not at library level
3850 if Has_Protected (T) and then not Is_Library_Level_Entity (Id) then
3851 Check_Restriction (No_Local_Protected_Objects, Id);
3853 -- Protected objects with interrupt handlers must be at library level
3855 -- Ada 2005: This test is not needed (and the corresponding clause
3856 -- in the RM is removed) because accessibility checks are sufficient
3857 -- to make handlers not at the library level illegal.
3859 -- AI05-0303: The AI is in fact a binding interpretation, and thus
3860 -- applies to the '95 version of the language as well.
3862 if Is_Protected_Type (T)
3863 and then Has_Interrupt_Handler (T)
3864 and then Ada_Version < Ada_95
3865 then
3866 Error_Msg_N
3867 ("interrupt object can only be declared at library level", Id);
3868 end if;
3869 end if;
3871 -- Check for violation of No_Local_Timing_Events
3873 if Has_Timing_Event (T) and then not Is_Library_Level_Entity (Id) then
3874 Check_Restriction (No_Local_Timing_Events, Id);
3875 end if;
3877 -- The actual subtype of the object is the nominal subtype, unless
3878 -- the nominal one is unconstrained and obtained from the expression.
3880 Act_T := T;
3882 -- These checks should be performed before the initialization expression
3883 -- is considered, so that the Object_Definition node is still the same
3884 -- as in source code.
3886 -- In SPARK, the nominal subtype is always given by a subtype mark
3887 -- and must not be unconstrained. (The only exception to this is the
3888 -- acceptance of declarations of constants of type String.)
3890 if not Nkind_In (Object_Definition (N), N_Expanded_Name, N_Identifier)
3891 then
3892 Check_SPARK_05_Restriction
3893 ("subtype mark required", Object_Definition (N));
3895 elsif Is_Array_Type (T)
3896 and then not Is_Constrained (T)
3897 and then T /= Standard_String
3898 then
3899 Check_SPARK_05_Restriction
3900 ("subtype mark of constrained type expected",
3901 Object_Definition (N));
3902 end if;
3904 -- There are no aliased objects in SPARK
3906 if Aliased_Present (N) then
3907 Check_SPARK_05_Restriction ("aliased object is not allowed", N);
3908 end if;
3910 -- Process initialization expression if present and not in error
3912 if Present (E) and then E /= Error then
3914 -- Generate an error in case of CPP class-wide object initialization.
3915 -- Required because otherwise the expansion of the class-wide
3916 -- assignment would try to use 'size to initialize the object
3917 -- (primitive that is not available in CPP tagged types).
3919 if Is_Class_Wide_Type (Act_T)
3920 and then
3921 (Is_CPP_Class (Root_Type (Etype (Act_T)))
3922 or else
3923 (Present (Full_View (Root_Type (Etype (Act_T))))
3924 and then
3925 Is_CPP_Class (Full_View (Root_Type (Etype (Act_T))))))
3926 then
3927 Error_Msg_N
3928 ("predefined assignment not available for 'C'P'P tagged types",
3930 end if;
3932 Mark_Coextensions (N, E);
3933 Analyze (E);
3935 -- In case of errors detected in the analysis of the expression,
3936 -- decorate it with the expected type to avoid cascaded errors
3938 if No (Etype (E)) then
3939 Set_Etype (E, T);
3940 end if;
3942 -- If an initialization expression is present, then we set the
3943 -- Is_True_Constant flag. It will be reset if this is a variable
3944 -- and it is indeed modified.
3946 Set_Is_True_Constant (Id, True);
3948 -- If we are analyzing a constant declaration, set its completion
3949 -- flag after analyzing and resolving the expression.
3951 if Constant_Present (N) then
3952 Set_Has_Completion (Id);
3953 end if;
3955 -- Set type and resolve (type may be overridden later on). Note:
3956 -- Ekind (Id) must still be E_Void at this point so that incorrect
3957 -- early usage within E is properly diagnosed.
3959 Set_Etype (Id, T);
3961 -- If the expression is an aggregate we must look ahead to detect
3962 -- the possible presence of an address clause, and defer resolution
3963 -- and expansion of the aggregate to the freeze point of the entity.
3965 -- This is not always legal because the aggregate may contain other
3966 -- references that need freezing, e.g. references to other entities
3967 -- with address clauses. In any case, when compiling with -gnatI the
3968 -- presence of the address clause must be ignored.
3970 if Comes_From_Source (N)
3971 and then Expander_Active
3972 and then Nkind (E) = N_Aggregate
3973 and then
3974 ((Present (Following_Address_Clause (N))
3975 and then not Ignore_Rep_Clauses)
3976 or else Delayed_Aspect_Present)
3977 then
3978 Set_Etype (E, T);
3980 else
3981 Resolve (E, T);
3982 end if;
3984 -- No further action needed if E is a call to an inlined function
3985 -- which returns an unconstrained type and it has been expanded into
3986 -- a procedure call. In that case N has been replaced by an object
3987 -- declaration without initializing expression and it has been
3988 -- analyzed (see Expand_Inlined_Call).
3990 if Back_End_Inlining
3991 and then Expander_Active
3992 and then Nkind (E) = N_Function_Call
3993 and then Nkind (Name (E)) in N_Has_Entity
3994 and then Is_Inlined (Entity (Name (E)))
3995 and then not Is_Constrained (Etype (E))
3996 and then Analyzed (N)
3997 and then No (Expression (N))
3998 then
3999 goto Leave;
4000 end if;
4002 -- If E is null and has been replaced by an N_Raise_Constraint_Error
4003 -- node (which was marked already-analyzed), we need to set the type
4004 -- to something other than Any_Access in order to keep gigi happy.
4006 if Etype (E) = Any_Access then
4007 Set_Etype (E, T);
4008 end if;
4010 -- If the object is an access to variable, the initialization
4011 -- expression cannot be an access to constant.
4013 if Is_Access_Type (T)
4014 and then not Is_Access_Constant (T)
4015 and then Is_Access_Type (Etype (E))
4016 and then Is_Access_Constant (Etype (E))
4017 then
4018 Error_Msg_N
4019 ("access to variable cannot be initialized with an "
4020 & "access-to-constant expression", E);
4021 end if;
4023 if not Assignment_OK (N) then
4024 Check_Initialization (T, E);
4025 end if;
4027 Check_Unset_Reference (E);
4029 -- If this is a variable, then set current value. If this is a
4030 -- declared constant of a scalar type with a static expression,
4031 -- indicate that it is always valid.
4033 if not Constant_Present (N) then
4034 if Compile_Time_Known_Value (E) then
4035 Set_Current_Value (Id, E);
4036 end if;
4038 elsif Is_Scalar_Type (T) and then Is_OK_Static_Expression (E) then
4039 Set_Is_Known_Valid (Id);
4040 end if;
4042 -- Deal with setting of null flags
4044 if Is_Access_Type (T) then
4045 if Known_Non_Null (E) then
4046 Set_Is_Known_Non_Null (Id, True);
4047 elsif Known_Null (E) and then not Can_Never_Be_Null (Id) then
4048 Set_Is_Known_Null (Id, True);
4049 end if;
4050 end if;
4052 -- Check incorrect use of dynamically tagged expressions
4054 if Is_Tagged_Type (T) then
4055 Check_Dynamically_Tagged_Expression
4056 (Expr => E,
4057 Typ => T,
4058 Related_Nod => N);
4059 end if;
4061 Apply_Scalar_Range_Check (E, T);
4062 Apply_Static_Length_Check (E, T);
4064 if Nkind (Original_Node (N)) = N_Object_Declaration
4065 and then Comes_From_Source (Original_Node (N))
4067 -- Only call test if needed
4069 and then Restriction_Check_Required (SPARK_05)
4070 and then not Is_SPARK_05_Initialization_Expr (Original_Node (E))
4071 then
4072 Check_SPARK_05_Restriction
4073 ("initialization expression is not appropriate", E);
4074 end if;
4076 -- A formal parameter of a specific tagged type whose related
4077 -- subprogram is subject to pragma Extensions_Visible with value
4078 -- "False" cannot be implicitly converted to a class-wide type by
4079 -- means of an initialization expression (SPARK RM 6.1.7(3)). Do
4080 -- not consider internally generated expressions.
4082 if Is_Class_Wide_Type (T)
4083 and then Comes_From_Source (E)
4084 and then Is_EVF_Expression (E)
4085 then
4086 Error_Msg_N
4087 ("formal parameter cannot be implicitly converted to "
4088 & "class-wide type when Extensions_Visible is False", E);
4089 end if;
4090 end if;
4092 -- If the No_Streams restriction is set, check that the type of the
4093 -- object is not, and does not contain, any subtype derived from
4094 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
4095 -- Has_Stream just for efficiency reasons. There is no point in
4096 -- spending time on a Has_Stream check if the restriction is not set.
4098 if Restriction_Check_Required (No_Streams) then
4099 if Has_Stream (T) then
4100 Check_Restriction (No_Streams, N);
4101 end if;
4102 end if;
4104 -- Deal with predicate check before we start to do major rewriting. It
4105 -- is OK to initialize and then check the initialized value, since the
4106 -- object goes out of scope if we get a predicate failure. Note that we
4107 -- do this in the analyzer and not the expander because the analyzer
4108 -- does some substantial rewriting in some cases.
4110 -- We need a predicate check if the type has predicates that are not
4111 -- ignored, and if either there is an initializing expression, or for
4112 -- default initialization when we have at least one case of an explicit
4113 -- default initial value and then this is not an internal declaration
4114 -- whose initialization comes later (as for an aggregate expansion).
4116 if not Suppress_Assignment_Checks (N)
4117 and then Present (Predicate_Function (T))
4118 and then not Predicates_Ignored (T)
4119 and then not No_Initialization (N)
4120 and then
4121 (Present (E)
4122 or else
4123 Is_Partially_Initialized_Type (T, Include_Implicit => False))
4124 then
4125 -- If the type has a static predicate and the expression is known at
4126 -- compile time, see if the expression satisfies the predicate.
4128 if Present (E) then
4129 Check_Expression_Against_Static_Predicate (E, T);
4130 end if;
4132 -- If the type is a null record and there is no explicit initial
4133 -- expression, no predicate check applies.
4135 if No (E) and then Is_Null_Record_Type (T) then
4136 null;
4138 else
4139 Insert_After (N,
4140 Make_Predicate_Check (T, New_Occurrence_Of (Id, Loc)));
4141 end if;
4142 end if;
4144 -- Case of unconstrained type
4146 if not Is_Definite_Subtype (T) then
4148 -- In SPARK, a declaration of unconstrained type is allowed
4149 -- only for constants of type string.
4151 if Is_String_Type (T) and then not Constant_Present (N) then
4152 Check_SPARK_05_Restriction
4153 ("declaration of object of unconstrained type not allowed", N);
4154 end if;
4156 -- Nothing to do in deferred constant case
4158 if Constant_Present (N) and then No (E) then
4159 null;
4161 -- Case of no initialization present
4163 elsif No (E) then
4164 if No_Initialization (N) then
4165 null;
4167 elsif Is_Class_Wide_Type (T) then
4168 Error_Msg_N
4169 ("initialization required in class-wide declaration ", N);
4171 else
4172 Error_Msg_N
4173 ("unconstrained subtype not allowed (need initialization)",
4174 Object_Definition (N));
4176 if Is_Record_Type (T) and then Has_Discriminants (T) then
4177 Error_Msg_N
4178 ("\provide initial value or explicit discriminant values",
4179 Object_Definition (N));
4181 Error_Msg_NE
4182 ("\or give default discriminant values for type&",
4183 Object_Definition (N), T);
4185 elsif Is_Array_Type (T) then
4186 Error_Msg_N
4187 ("\provide initial value or explicit array bounds",
4188 Object_Definition (N));
4189 end if;
4190 end if;
4192 -- Case of initialization present but in error. Set initial
4193 -- expression as absent (but do not make above complaints)
4195 elsif E = Error then
4196 Set_Expression (N, Empty);
4197 E := Empty;
4199 -- Case of initialization present
4201 else
4202 -- Check restrictions in Ada 83
4204 if not Constant_Present (N) then
4206 -- Unconstrained variables not allowed in Ada 83 mode
4208 if Ada_Version = Ada_83
4209 and then Comes_From_Source (Object_Definition (N))
4210 then
4211 Error_Msg_N
4212 ("(Ada 83) unconstrained variable not allowed",
4213 Object_Definition (N));
4214 end if;
4215 end if;
4217 -- Now we constrain the variable from the initializing expression
4219 -- If the expression is an aggregate, it has been expanded into
4220 -- individual assignments. Retrieve the actual type from the
4221 -- expanded construct.
4223 if Is_Array_Type (T)
4224 and then No_Initialization (N)
4225 and then Nkind (Original_Node (E)) = N_Aggregate
4226 then
4227 Act_T := Etype (E);
4229 -- In case of class-wide interface object declarations we delay
4230 -- the generation of the equivalent record type declarations until
4231 -- its expansion because there are cases in they are not required.
4233 elsif Is_Interface (T) then
4234 null;
4236 -- In GNATprove mode, Expand_Subtype_From_Expr does nothing. Thus,
4237 -- we should prevent the generation of another Itype with the
4238 -- same name as the one already generated, or we end up with
4239 -- two identical types in GNATprove.
4241 elsif GNATprove_Mode then
4242 null;
4244 -- If the type is an unchecked union, no subtype can be built from
4245 -- the expression. Rewrite declaration as a renaming, which the
4246 -- back-end can handle properly. This is a rather unusual case,
4247 -- because most unchecked_union declarations have default values
4248 -- for discriminants and are thus not indefinite.
4250 elsif Is_Unchecked_Union (T) then
4251 if Constant_Present (N) or else Nkind (E) = N_Function_Call then
4252 Set_Ekind (Id, E_Constant);
4253 else
4254 Set_Ekind (Id, E_Variable);
4255 end if;
4257 Rewrite (N,
4258 Make_Object_Renaming_Declaration (Loc,
4259 Defining_Identifier => Id,
4260 Subtype_Mark => New_Occurrence_Of (T, Loc),
4261 Name => E));
4263 Set_Renamed_Object (Id, E);
4264 Freeze_Before (N, T);
4265 Set_Is_Frozen (Id);
4266 goto Leave;
4268 else
4269 -- Ensure that the generated subtype has a unique external name
4270 -- when the related object is public. This guarantees that the
4271 -- subtype and its bounds will not be affected by switches or
4272 -- pragmas that may offset the internal counter due to extra
4273 -- generated code.
4275 if Is_Public (Id) then
4276 Related_Id := Id;
4277 else
4278 Related_Id := Empty;
4279 end if;
4281 Expand_Subtype_From_Expr
4282 (N => N,
4283 Unc_Type => T,
4284 Subtype_Indic => Object_Definition (N),
4285 Exp => E,
4286 Related_Id => Related_Id);
4288 Act_T := Find_Type_Of_Object (Object_Definition (N), N);
4289 end if;
4291 Set_Is_Constr_Subt_For_U_Nominal (Act_T);
4293 if Aliased_Present (N) then
4294 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4295 end if;
4297 Freeze_Before (N, Act_T);
4298 Freeze_Before (N, T);
4299 end if;
4301 elsif Is_Array_Type (T)
4302 and then No_Initialization (N)
4303 and then (Nkind (Original_Node (E)) = N_Aggregate
4304 or else (Nkind (Original_Node (E)) = N_Qualified_Expression
4305 and then Nkind (Original_Node (Expression
4306 (Original_Node (E)))) = N_Aggregate))
4307 then
4308 if not Is_Entity_Name (Object_Definition (N)) then
4309 Act_T := Etype (E);
4310 Check_Compile_Time_Size (Act_T);
4312 if Aliased_Present (N) then
4313 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4314 end if;
4315 end if;
4317 -- When the given object definition and the aggregate are specified
4318 -- independently, and their lengths might differ do a length check.
4319 -- This cannot happen if the aggregate is of the form (others =>...)
4321 if not Is_Constrained (T) then
4322 null;
4324 elsif Nkind (E) = N_Raise_Constraint_Error then
4326 -- Aggregate is statically illegal. Place back in declaration
4328 Set_Expression (N, E);
4329 Set_No_Initialization (N, False);
4331 elsif T = Etype (E) then
4332 null;
4334 elsif Nkind (E) = N_Aggregate
4335 and then Present (Component_Associations (E))
4336 and then Present (Choice_List (First (Component_Associations (E))))
4337 and then
4338 Nkind (First (Choice_List (First (Component_Associations (E))))) =
4339 N_Others_Choice
4340 then
4341 null;
4343 else
4344 Apply_Length_Check (E, T);
4345 end if;
4347 -- If the type is limited unconstrained with defaulted discriminants and
4348 -- there is no expression, then the object is constrained by the
4349 -- defaults, so it is worthwhile building the corresponding subtype.
4351 elsif (Is_Limited_Record (T) or else Is_Concurrent_Type (T))
4352 and then not Is_Constrained (T)
4353 and then Has_Discriminants (T)
4354 then
4355 if No (E) then
4356 Act_T := Build_Default_Subtype (T, N);
4357 else
4358 -- Ada 2005: A limited object may be initialized by means of an
4359 -- aggregate. If the type has default discriminants it has an
4360 -- unconstrained nominal type, Its actual subtype will be obtained
4361 -- from the aggregate, and not from the default discriminants.
4363 Act_T := Etype (E);
4364 end if;
4366 Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
4368 elsif Nkind (E) = N_Function_Call
4369 and then Constant_Present (N)
4370 and then Has_Unconstrained_Elements (Etype (E))
4371 then
4372 -- The back-end has problems with constants of a discriminated type
4373 -- with defaults, if the initial value is a function call. We
4374 -- generate an intermediate temporary that will receive a reference
4375 -- to the result of the call. The initialization expression then
4376 -- becomes a dereference of that temporary.
4378 Remove_Side_Effects (E);
4380 -- If this is a constant declaration of an unconstrained type and
4381 -- the initialization is an aggregate, we can use the subtype of the
4382 -- aggregate for the declared entity because it is immutable.
4384 elsif not Is_Constrained (T)
4385 and then Has_Discriminants (T)
4386 and then Constant_Present (N)
4387 and then not Has_Unchecked_Union (T)
4388 and then Nkind (E) = N_Aggregate
4389 then
4390 Act_T := Etype (E);
4391 end if;
4393 -- Check No_Wide_Characters restriction
4395 Check_Wide_Character_Restriction (T, Object_Definition (N));
4397 -- Indicate this is not set in source. Certainly true for constants, and
4398 -- true for variables so far (will be reset for a variable if and when
4399 -- we encounter a modification in the source).
4401 Set_Never_Set_In_Source (Id);
4403 -- Now establish the proper kind and type of the object
4405 if Constant_Present (N) then
4406 Set_Ekind (Id, E_Constant);
4407 Set_Is_True_Constant (Id);
4409 else
4410 Set_Ekind (Id, E_Variable);
4412 -- A variable is set as shared passive if it appears in a shared
4413 -- passive package, and is at the outer level. This is not done for
4414 -- entities generated during expansion, because those are always
4415 -- manipulated locally.
4417 if Is_Shared_Passive (Current_Scope)
4418 and then Is_Library_Level_Entity (Id)
4419 and then Comes_From_Source (Id)
4420 then
4421 Set_Is_Shared_Passive (Id);
4422 Check_Shared_Var (Id, T, N);
4423 end if;
4425 -- Set Has_Initial_Value if initializing expression present. Note
4426 -- that if there is no initializing expression, we leave the state
4427 -- of this flag unchanged (usually it will be False, but notably in
4428 -- the case of exception choice variables, it will already be true).
4430 if Present (E) then
4431 Set_Has_Initial_Value (Id);
4432 end if;
4433 end if;
4435 -- Initialize alignment and size and capture alignment setting
4437 Init_Alignment (Id);
4438 Init_Esize (Id);
4439 Set_Optimize_Alignment_Flags (Id);
4441 -- Deal with aliased case
4443 if Aliased_Present (N) then
4444 Set_Is_Aliased (Id);
4446 -- If the object is aliased and the type is unconstrained with
4447 -- defaulted discriminants and there is no expression, then the
4448 -- object is constrained by the defaults, so it is worthwhile
4449 -- building the corresponding subtype.
4451 -- Ada 2005 (AI-363): If the aliased object is discriminated and
4452 -- unconstrained, then only establish an actual subtype if the
4453 -- nominal subtype is indefinite. In definite cases the object is
4454 -- unconstrained in Ada 2005.
4456 if No (E)
4457 and then Is_Record_Type (T)
4458 and then not Is_Constrained (T)
4459 and then Has_Discriminants (T)
4460 and then (Ada_Version < Ada_2005
4461 or else not Is_Definite_Subtype (T))
4462 then
4463 Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
4464 end if;
4465 end if;
4467 -- Now we can set the type of the object
4469 Set_Etype (Id, Act_T);
4471 -- Non-constant object is marked to be treated as volatile if type is
4472 -- volatile and we clear the Current_Value setting that may have been
4473 -- set above. Doing so for constants isn't required and might interfere
4474 -- with possible uses of the object as a static expression in contexts
4475 -- incompatible with volatility (e.g. as a case-statement alternative).
4477 if Ekind (Id) /= E_Constant and then Treat_As_Volatile (Etype (Id)) then
4478 Set_Treat_As_Volatile (Id);
4479 Set_Current_Value (Id, Empty);
4480 end if;
4482 -- Deal with controlled types
4484 if Has_Controlled_Component (Etype (Id))
4485 or else Is_Controlled (Etype (Id))
4486 then
4487 if not Is_Library_Level_Entity (Id) then
4488 Check_Restriction (No_Nested_Finalization, N);
4489 else
4490 Validate_Controlled_Object (Id);
4491 end if;
4492 end if;
4494 if Has_Task (Etype (Id)) then
4495 Check_Restriction (No_Tasking, N);
4497 -- Deal with counting max tasks
4499 -- Nothing to do if inside a generic
4501 if Inside_A_Generic then
4502 null;
4504 -- If library level entity, then count tasks
4506 elsif Is_Library_Level_Entity (Id) then
4507 Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
4509 -- If not library level entity, then indicate we don't know max
4510 -- tasks and also check task hierarchy restriction and blocking
4511 -- operation (since starting a task is definitely blocking).
4513 else
4514 Check_Restriction (Max_Tasks, N);
4515 Check_Restriction (No_Task_Hierarchy, N);
4516 Check_Potentially_Blocking_Operation (N);
4517 end if;
4519 -- A rather specialized test. If we see two tasks being declared
4520 -- of the same type in the same object declaration, and the task
4521 -- has an entry with an address clause, we know that program error
4522 -- will be raised at run time since we can't have two tasks with
4523 -- entries at the same address.
4525 if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
4526 declare
4527 E : Entity_Id;
4529 begin
4530 E := First_Entity (Etype (Id));
4531 while Present (E) loop
4532 if Ekind (E) = E_Entry
4533 and then Present (Get_Attribute_Definition_Clause
4534 (E, Attribute_Address))
4535 then
4536 Error_Msg_Warn := SPARK_Mode /= On;
4537 Error_Msg_N
4538 ("more than one task with same entry address<<", N);
4539 Error_Msg_N ("\Program_Error [<<", N);
4540 Insert_Action (N,
4541 Make_Raise_Program_Error (Loc,
4542 Reason => PE_Duplicated_Entry_Address));
4543 exit;
4544 end if;
4546 Next_Entity (E);
4547 end loop;
4548 end;
4549 end if;
4550 end if;
4552 -- Some simple constant-propagation: if the expression is a constant
4553 -- string initialized with a literal, share the literal. This avoids
4554 -- a run-time copy.
4556 if Present (E)
4557 and then Is_Entity_Name (E)
4558 and then Ekind (Entity (E)) = E_Constant
4559 and then Base_Type (Etype (E)) = Standard_String
4560 then
4561 declare
4562 Val : constant Node_Id := Constant_Value (Entity (E));
4563 begin
4564 if Present (Val) and then Nkind (Val) = N_String_Literal then
4565 Rewrite (E, New_Copy (Val));
4566 end if;
4567 end;
4568 end if;
4570 -- Another optimization: if the nominal subtype is unconstrained and
4571 -- the expression is a function call that returns an unconstrained
4572 -- type, rewrite the declaration as a renaming of the result of the
4573 -- call. The exceptions below are cases where the copy is expected,
4574 -- either by the back end (Aliased case) or by the semantics, as for
4575 -- initializing controlled types or copying tags for class-wide types.
4577 if Present (E)
4578 and then Nkind (E) = N_Explicit_Dereference
4579 and then Nkind (Original_Node (E)) = N_Function_Call
4580 and then not Is_Library_Level_Entity (Id)
4581 and then not Is_Constrained (Underlying_Type (T))
4582 and then not Is_Aliased (Id)
4583 and then not Is_Class_Wide_Type (T)
4584 and then not Is_Controlled_Active (T)
4585 and then not Has_Controlled_Component (Base_Type (T))
4586 and then Expander_Active
4587 then
4588 Rewrite (N,
4589 Make_Object_Renaming_Declaration (Loc,
4590 Defining_Identifier => Id,
4591 Access_Definition => Empty,
4592 Subtype_Mark => New_Occurrence_Of
4593 (Base_Type (Etype (Id)), Loc),
4594 Name => E));
4596 Set_Renamed_Object (Id, E);
4598 -- Force generation of debugging information for the constant and for
4599 -- the renamed function call.
4601 Set_Debug_Info_Needed (Id);
4602 Set_Debug_Info_Needed (Entity (Prefix (E)));
4603 end if;
4605 if Present (Prev_Entity)
4606 and then Is_Frozen (Prev_Entity)
4607 and then not Error_Posted (Id)
4608 then
4609 Error_Msg_N ("full constant declaration appears too late", N);
4610 end if;
4612 Check_Eliminated (Id);
4614 -- Deal with setting In_Private_Part flag if in private part
4616 if Ekind (Scope (Id)) = E_Package
4617 and then In_Private_Part (Scope (Id))
4618 then
4619 Set_In_Private_Part (Id);
4620 end if;
4622 <<Leave>>
4623 -- Initialize the refined state of a variable here because this is a
4624 -- common destination for legal and illegal object declarations.
4626 if Ekind (Id) = E_Variable then
4627 Set_Encapsulating_State (Id, Empty);
4628 end if;
4630 if Has_Aspects (N) then
4631 Analyze_Aspect_Specifications (N, Id);
4632 end if;
4634 Analyze_Dimension (N);
4636 -- Verify whether the object declaration introduces an illegal hidden
4637 -- state within a package subject to a null abstract state.
4639 if Ekind (Id) = E_Variable then
4640 Check_No_Hidden_State (Id);
4641 end if;
4643 if Mode_Set then
4644 Restore_Ghost_Mode (Mode);
4645 end if;
4646 end Analyze_Object_Declaration;
4648 ---------------------------
4649 -- Analyze_Others_Choice --
4650 ---------------------------
4652 -- Nothing to do for the others choice node itself, the semantic analysis
4653 -- of the others choice will occur as part of the processing of the parent
4655 procedure Analyze_Others_Choice (N : Node_Id) is
4656 pragma Warnings (Off, N);
4657 begin
4658 null;
4659 end Analyze_Others_Choice;
4661 -------------------------------------------
4662 -- Analyze_Private_Extension_Declaration --
4663 -------------------------------------------
4665 procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
4666 Indic : constant Node_Id := Subtype_Indication (N);
4667 T : constant Entity_Id := Defining_Identifier (N);
4668 Iface : Entity_Id;
4669 Iface_Elmt : Elmt_Id;
4670 Parent_Base : Entity_Id;
4671 Parent_Type : Entity_Id;
4673 begin
4674 -- Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
4676 if Is_Non_Empty_List (Interface_List (N)) then
4677 declare
4678 Intf : Node_Id;
4679 T : Entity_Id;
4681 begin
4682 Intf := First (Interface_List (N));
4683 while Present (Intf) loop
4684 T := Find_Type_Of_Subtype_Indic (Intf);
4686 Diagnose_Interface (Intf, T);
4687 Next (Intf);
4688 end loop;
4689 end;
4690 end if;
4692 Generate_Definition (T);
4694 -- For other than Ada 2012, just enter the name in the current scope
4696 if Ada_Version < Ada_2012 then
4697 Enter_Name (T);
4699 -- Ada 2012 (AI05-0162): Enter the name in the current scope handling
4700 -- case of private type that completes an incomplete type.
4702 else
4703 declare
4704 Prev : Entity_Id;
4706 begin
4707 Prev := Find_Type_Name (N);
4709 pragma Assert (Prev = T
4710 or else (Ekind (Prev) = E_Incomplete_Type
4711 and then Present (Full_View (Prev))
4712 and then Full_View (Prev) = T));
4713 end;
4714 end if;
4716 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
4717 Parent_Base := Base_Type (Parent_Type);
4719 if Parent_Type = Any_Type or else Etype (Parent_Type) = Any_Type then
4720 Set_Ekind (T, Ekind (Parent_Type));
4721 Set_Etype (T, Any_Type);
4722 goto Leave;
4724 elsif not Is_Tagged_Type (Parent_Type) then
4725 Error_Msg_N
4726 ("parent of type extension must be a tagged type ", Indic);
4727 goto Leave;
4729 elsif Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
4730 Error_Msg_N ("premature derivation of incomplete type", Indic);
4731 goto Leave;
4733 elsif Is_Concurrent_Type (Parent_Type) then
4734 Error_Msg_N
4735 ("parent type of a private extension cannot be a synchronized "
4736 & "tagged type (RM 3.9.1 (3/1))", N);
4738 Set_Etype (T, Any_Type);
4739 Set_Ekind (T, E_Limited_Private_Type);
4740 Set_Private_Dependents (T, New_Elmt_List);
4741 Set_Error_Posted (T);
4742 goto Leave;
4743 end if;
4745 -- Perhaps the parent type should be changed to the class-wide type's
4746 -- specific type in this case to prevent cascading errors ???
4748 if Is_Class_Wide_Type (Parent_Type) then
4749 Error_Msg_N
4750 ("parent of type extension must not be a class-wide type", Indic);
4751 goto Leave;
4752 end if;
4754 if (not Is_Package_Or_Generic_Package (Current_Scope)
4755 and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
4756 or else In_Private_Part (Current_Scope)
4757 then
4758 Error_Msg_N ("invalid context for private extension", N);
4759 end if;
4761 -- Set common attributes
4763 Set_Is_Pure (T, Is_Pure (Current_Scope));
4764 Set_Scope (T, Current_Scope);
4765 Set_Ekind (T, E_Record_Type_With_Private);
4766 Init_Size_Align (T);
4767 Set_Default_SSO (T);
4769 Set_Etype (T, Parent_Base);
4770 Propagate_Concurrent_Flags (T, Parent_Base);
4772 Set_Convention (T, Convention (Parent_Type));
4773 Set_First_Rep_Item (T, First_Rep_Item (Parent_Type));
4774 Set_Is_First_Subtype (T);
4775 Make_Class_Wide_Type (T);
4777 if Unknown_Discriminants_Present (N) then
4778 Set_Discriminant_Constraint (T, No_Elist);
4779 end if;
4781 Build_Derived_Record_Type (N, Parent_Type, T);
4783 -- A private extension inherits the Default_Initial_Condition pragma
4784 -- coming from any parent type within the derivation chain.
4786 if Has_DIC (Parent_Type) then
4787 Set_Has_Inherited_DIC (T);
4788 end if;
4790 -- A private extension inherits any class-wide invariants coming from a
4791 -- parent type or an interface. Note that the invariant procedure of the
4792 -- parent type should not be inherited because the private extension may
4793 -- define invariants of its own.
4795 if Has_Inherited_Invariants (Parent_Type)
4796 or else Has_Inheritable_Invariants (Parent_Type)
4797 then
4798 Set_Has_Inherited_Invariants (T);
4800 elsif Present (Interfaces (T)) then
4801 Iface_Elmt := First_Elmt (Interfaces (T));
4802 while Present (Iface_Elmt) loop
4803 Iface := Node (Iface_Elmt);
4805 if Has_Inheritable_Invariants (Iface) then
4806 Set_Has_Inherited_Invariants (T);
4807 exit;
4808 end if;
4810 Next_Elmt (Iface_Elmt);
4811 end loop;
4812 end if;
4814 -- Ada 2005 (AI-443): Synchronized private extension or a rewritten
4815 -- synchronized formal derived type.
4817 if Ada_Version >= Ada_2005 and then Synchronized_Present (N) then
4818 Set_Is_Limited_Record (T);
4820 -- Formal derived type case
4822 if Is_Generic_Type (T) then
4824 -- The parent must be a tagged limited type or a synchronized
4825 -- interface.
4827 if (not Is_Tagged_Type (Parent_Type)
4828 or else not Is_Limited_Type (Parent_Type))
4829 and then
4830 (not Is_Interface (Parent_Type)
4831 or else not Is_Synchronized_Interface (Parent_Type))
4832 then
4833 Error_Msg_NE
4834 ("parent type of & must be tagged limited or synchronized",
4835 N, T);
4836 end if;
4838 -- The progenitors (if any) must be limited or synchronized
4839 -- interfaces.
4841 if Present (Interfaces (T)) then
4842 Iface_Elmt := First_Elmt (Interfaces (T));
4843 while Present (Iface_Elmt) loop
4844 Iface := Node (Iface_Elmt);
4846 if not Is_Limited_Interface (Iface)
4847 and then not Is_Synchronized_Interface (Iface)
4848 then
4849 Error_Msg_NE
4850 ("progenitor & must be limited or synchronized",
4851 N, Iface);
4852 end if;
4854 Next_Elmt (Iface_Elmt);
4855 end loop;
4856 end if;
4858 -- Regular derived extension, the parent must be a limited or
4859 -- synchronized interface.
4861 else
4862 if not Is_Interface (Parent_Type)
4863 or else (not Is_Limited_Interface (Parent_Type)
4864 and then not Is_Synchronized_Interface (Parent_Type))
4865 then
4866 Error_Msg_NE
4867 ("parent type of & must be limited interface", N, T);
4868 end if;
4869 end if;
4871 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
4872 -- extension with a synchronized parent must be explicitly declared
4873 -- synchronized, because the full view will be a synchronized type.
4874 -- This must be checked before the check for limited types below,
4875 -- to ensure that types declared limited are not allowed to extend
4876 -- synchronized interfaces.
4878 elsif Is_Interface (Parent_Type)
4879 and then Is_Synchronized_Interface (Parent_Type)
4880 and then not Synchronized_Present (N)
4881 then
4882 Error_Msg_NE
4883 ("private extension of& must be explicitly synchronized",
4884 N, Parent_Type);
4886 elsif Limited_Present (N) then
4887 Set_Is_Limited_Record (T);
4889 if not Is_Limited_Type (Parent_Type)
4890 and then
4891 (not Is_Interface (Parent_Type)
4892 or else not Is_Limited_Interface (Parent_Type))
4893 then
4894 Error_Msg_NE ("parent type& of limited extension must be limited",
4895 N, Parent_Type);
4896 end if;
4897 end if;
4899 <<Leave>>
4900 if Has_Aspects (N) then
4901 Analyze_Aspect_Specifications (N, T);
4902 end if;
4903 end Analyze_Private_Extension_Declaration;
4905 ---------------------------------
4906 -- Analyze_Subtype_Declaration --
4907 ---------------------------------
4909 procedure Analyze_Subtype_Declaration
4910 (N : Node_Id;
4911 Skip : Boolean := False)
4913 Id : constant Entity_Id := Defining_Identifier (N);
4914 R_Checks : Check_Result;
4915 T : Entity_Id;
4917 begin
4918 Generate_Definition (Id);
4919 Set_Is_Pure (Id, Is_Pure (Current_Scope));
4920 Init_Size_Align (Id);
4922 -- The following guard condition on Enter_Name is to handle cases where
4923 -- the defining identifier has already been entered into the scope but
4924 -- the declaration as a whole needs to be analyzed.
4926 -- This case in particular happens for derived enumeration types. The
4927 -- derived enumeration type is processed as an inserted enumeration type
4928 -- declaration followed by a rewritten subtype declaration. The defining
4929 -- identifier, however, is entered into the name scope very early in the
4930 -- processing of the original type declaration and therefore needs to be
4931 -- avoided here, when the created subtype declaration is analyzed. (See
4932 -- Build_Derived_Types)
4934 -- This also happens when the full view of a private type is derived
4935 -- type with constraints. In this case the entity has been introduced
4936 -- in the private declaration.
4938 -- Finally this happens in some complex cases when validity checks are
4939 -- enabled, where the same subtype declaration may be analyzed twice.
4940 -- This can happen if the subtype is created by the pre-analysis of
4941 -- an attribute tht gives the range of a loop statement, and the loop
4942 -- itself appears within an if_statement that will be rewritten during
4943 -- expansion.
4945 if Skip
4946 or else (Present (Etype (Id))
4947 and then (Is_Private_Type (Etype (Id))
4948 or else Is_Task_Type (Etype (Id))
4949 or else Is_Rewrite_Substitution (N)))
4950 then
4951 null;
4953 elsif Current_Entity (Id) = Id then
4954 null;
4956 else
4957 Enter_Name (Id);
4958 end if;
4960 T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
4962 -- Class-wide equivalent types of records with unknown discriminants
4963 -- involve the generation of an itype which serves as the private view
4964 -- of a constrained record subtype. In such cases the base type of the
4965 -- current subtype we are processing is the private itype. Use the full
4966 -- of the private itype when decorating various attributes.
4968 if Is_Itype (T)
4969 and then Is_Private_Type (T)
4970 and then Present (Full_View (T))
4971 then
4972 T := Full_View (T);
4973 end if;
4975 -- Inherit common attributes
4977 Set_Is_Volatile (Id, Is_Volatile (T));
4978 Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
4979 Set_Is_Generic_Type (Id, Is_Generic_Type (Base_Type (T)));
4980 Set_Convention (Id, Convention (T));
4982 -- If ancestor has predicates then so does the subtype, and in addition
4983 -- we must delay the freeze to properly arrange predicate inheritance.
4985 -- The Ancestor_Type test is really unpleasant, there seem to be cases
4986 -- in which T = ID, so the above tests and assignments do nothing???
4988 if Has_Predicates (T)
4989 or else (Present (Ancestor_Subtype (T))
4990 and then Has_Predicates (Ancestor_Subtype (T)))
4991 then
4992 Set_Has_Predicates (Id);
4993 Set_Has_Delayed_Freeze (Id);
4995 -- Generated subtypes inherit the predicate function from the parent
4996 -- (no aspects to examine on the generated declaration).
4998 if not Comes_From_Source (N) then
4999 Set_Ekind (Id, Ekind (T));
5001 if Present (Predicate_Function (T)) then
5002 Set_Predicate_Function (Id, Predicate_Function (T));
5004 elsif Present (Ancestor_Subtype (T))
5005 and then Has_Predicates (Ancestor_Subtype (T))
5006 and then Present (Predicate_Function (Ancestor_Subtype (T)))
5007 then
5008 Set_Predicate_Function (Id,
5009 Predicate_Function (Ancestor_Subtype (T)));
5010 end if;
5011 end if;
5012 end if;
5014 -- Subtype of Boolean cannot have a constraint in SPARK
5016 if Is_Boolean_Type (T)
5017 and then Nkind (Subtype_Indication (N)) = N_Subtype_Indication
5018 then
5019 Check_SPARK_05_Restriction
5020 ("subtype of Boolean cannot have constraint", N);
5021 end if;
5023 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5024 declare
5025 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
5026 One_Cstr : Node_Id;
5027 Low : Node_Id;
5028 High : Node_Id;
5030 begin
5031 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint then
5032 One_Cstr := First (Constraints (Cstr));
5033 while Present (One_Cstr) loop
5035 -- Index or discriminant constraint in SPARK must be a
5036 -- subtype mark.
5038 if not
5039 Nkind_In (One_Cstr, N_Identifier, N_Expanded_Name)
5040 then
5041 Check_SPARK_05_Restriction
5042 ("subtype mark required", One_Cstr);
5044 -- String subtype must have a lower bound of 1 in SPARK.
5045 -- Note that we do not need to test for the non-static case
5046 -- here, since that was already taken care of in
5047 -- Process_Range_Expr_In_Decl.
5049 elsif Base_Type (T) = Standard_String then
5050 Get_Index_Bounds (One_Cstr, Low, High);
5052 if Is_OK_Static_Expression (Low)
5053 and then Expr_Value (Low) /= 1
5054 then
5055 Check_SPARK_05_Restriction
5056 ("String subtype must have lower bound of 1", N);
5057 end if;
5058 end if;
5060 Next (One_Cstr);
5061 end loop;
5062 end if;
5063 end;
5064 end if;
5066 -- In the case where there is no constraint given in the subtype
5067 -- indication, Process_Subtype just returns the Subtype_Mark, so its
5068 -- semantic attributes must be established here.
5070 if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
5071 Set_Etype (Id, Base_Type (T));
5073 -- Subtype of unconstrained array without constraint is not allowed
5074 -- in SPARK.
5076 if Is_Array_Type (T) and then not Is_Constrained (T) then
5077 Check_SPARK_05_Restriction
5078 ("subtype of unconstrained array must have constraint", N);
5079 end if;
5081 case Ekind (T) is
5082 when Array_Kind =>
5083 Set_Ekind (Id, E_Array_Subtype);
5084 Copy_Array_Subtype_Attributes (Id, T);
5086 when Decimal_Fixed_Point_Kind =>
5087 Set_Ekind (Id, E_Decimal_Fixed_Point_Subtype);
5088 Set_Digits_Value (Id, Digits_Value (T));
5089 Set_Delta_Value (Id, Delta_Value (T));
5090 Set_Scale_Value (Id, Scale_Value (T));
5091 Set_Small_Value (Id, Small_Value (T));
5092 Set_Scalar_Range (Id, Scalar_Range (T));
5093 Set_Machine_Radix_10 (Id, Machine_Radix_10 (T));
5094 Set_Is_Constrained (Id, Is_Constrained (T));
5095 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5096 Set_RM_Size (Id, RM_Size (T));
5098 when Enumeration_Kind =>
5099 Set_Ekind (Id, E_Enumeration_Subtype);
5100 Set_First_Literal (Id, First_Literal (Base_Type (T)));
5101 Set_Scalar_Range (Id, Scalar_Range (T));
5102 Set_Is_Character_Type (Id, Is_Character_Type (T));
5103 Set_Is_Constrained (Id, Is_Constrained (T));
5104 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5105 Set_RM_Size (Id, RM_Size (T));
5106 Inherit_Predicate_Flags (Id, T);
5108 when Ordinary_Fixed_Point_Kind =>
5109 Set_Ekind (Id, E_Ordinary_Fixed_Point_Subtype);
5110 Set_Scalar_Range (Id, Scalar_Range (T));
5111 Set_Small_Value (Id, Small_Value (T));
5112 Set_Delta_Value (Id, Delta_Value (T));
5113 Set_Is_Constrained (Id, Is_Constrained (T));
5114 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5115 Set_RM_Size (Id, RM_Size (T));
5117 when Float_Kind =>
5118 Set_Ekind (Id, E_Floating_Point_Subtype);
5119 Set_Scalar_Range (Id, Scalar_Range (T));
5120 Set_Digits_Value (Id, Digits_Value (T));
5121 Set_Is_Constrained (Id, Is_Constrained (T));
5123 -- If the floating point type has dimensions, these will be
5124 -- inherited subsequently when Analyze_Dimensions is called.
5126 when Signed_Integer_Kind =>
5127 Set_Ekind (Id, E_Signed_Integer_Subtype);
5128 Set_Scalar_Range (Id, Scalar_Range (T));
5129 Set_Is_Constrained (Id, Is_Constrained (T));
5130 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5131 Set_RM_Size (Id, RM_Size (T));
5132 Inherit_Predicate_Flags (Id, T);
5134 when Modular_Integer_Kind =>
5135 Set_Ekind (Id, E_Modular_Integer_Subtype);
5136 Set_Scalar_Range (Id, Scalar_Range (T));
5137 Set_Is_Constrained (Id, Is_Constrained (T));
5138 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5139 Set_RM_Size (Id, RM_Size (T));
5140 Inherit_Predicate_Flags (Id, T);
5142 when Class_Wide_Kind =>
5143 Set_Ekind (Id, E_Class_Wide_Subtype);
5144 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5145 Set_Cloned_Subtype (Id, T);
5146 Set_Is_Tagged_Type (Id, True);
5147 Set_Has_Unknown_Discriminants
5148 (Id, True);
5149 Set_No_Tagged_Streams_Pragma
5150 (Id, No_Tagged_Streams_Pragma (T));
5152 if Ekind (T) = E_Class_Wide_Subtype then
5153 Set_Equivalent_Type (Id, Equivalent_Type (T));
5154 end if;
5156 when E_Record_Subtype
5157 | E_Record_Type
5159 Set_Ekind (Id, E_Record_Subtype);
5161 if Ekind (T) = E_Record_Subtype
5162 and then Present (Cloned_Subtype (T))
5163 then
5164 Set_Cloned_Subtype (Id, Cloned_Subtype (T));
5165 else
5166 Set_Cloned_Subtype (Id, T);
5167 end if;
5169 Set_First_Entity (Id, First_Entity (T));
5170 Set_Last_Entity (Id, Last_Entity (T));
5171 Set_Has_Discriminants (Id, Has_Discriminants (T));
5172 Set_Is_Constrained (Id, Is_Constrained (T));
5173 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5174 Set_Has_Implicit_Dereference
5175 (Id, Has_Implicit_Dereference (T));
5176 Set_Has_Unknown_Discriminants
5177 (Id, Has_Unknown_Discriminants (T));
5179 if Has_Discriminants (T) then
5180 Set_Discriminant_Constraint
5181 (Id, Discriminant_Constraint (T));
5182 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5184 elsif Has_Unknown_Discriminants (Id) then
5185 Set_Discriminant_Constraint (Id, No_Elist);
5186 end if;
5188 if Is_Tagged_Type (T) then
5189 Set_Is_Tagged_Type (Id, True);
5190 Set_No_Tagged_Streams_Pragma
5191 (Id, No_Tagged_Streams_Pragma (T));
5192 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5193 Set_Direct_Primitive_Operations
5194 (Id, Direct_Primitive_Operations (T));
5195 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5197 if Is_Interface (T) then
5198 Set_Is_Interface (Id);
5199 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
5200 end if;
5201 end if;
5203 when Private_Kind =>
5204 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
5205 Set_Has_Discriminants (Id, Has_Discriminants (T));
5206 Set_Is_Constrained (Id, Is_Constrained (T));
5207 Set_First_Entity (Id, First_Entity (T));
5208 Set_Last_Entity (Id, Last_Entity (T));
5209 Set_Private_Dependents (Id, New_Elmt_List);
5210 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5211 Set_Has_Implicit_Dereference
5212 (Id, Has_Implicit_Dereference (T));
5213 Set_Has_Unknown_Discriminants
5214 (Id, Has_Unknown_Discriminants (T));
5215 Set_Known_To_Have_Preelab_Init
5216 (Id, Known_To_Have_Preelab_Init (T));
5218 if Is_Tagged_Type (T) then
5219 Set_Is_Tagged_Type (Id);
5220 Set_No_Tagged_Streams_Pragma (Id,
5221 No_Tagged_Streams_Pragma (T));
5222 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5223 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5224 Set_Direct_Primitive_Operations (Id,
5225 Direct_Primitive_Operations (T));
5226 end if;
5228 -- In general the attributes of the subtype of a private type
5229 -- are the attributes of the partial view of parent. However,
5230 -- the full view may be a discriminated type, and the subtype
5231 -- must share the discriminant constraint to generate correct
5232 -- calls to initialization procedures.
5234 if Has_Discriminants (T) then
5235 Set_Discriminant_Constraint
5236 (Id, Discriminant_Constraint (T));
5237 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5239 elsif Present (Full_View (T))
5240 and then Has_Discriminants (Full_View (T))
5241 then
5242 Set_Discriminant_Constraint
5243 (Id, Discriminant_Constraint (Full_View (T)));
5244 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5246 -- This would seem semantically correct, but apparently
5247 -- generates spurious errors about missing components ???
5249 -- Set_Has_Discriminants (Id);
5250 end if;
5252 Prepare_Private_Subtype_Completion (Id, N);
5254 -- If this is the subtype of a constrained private type with
5255 -- discriminants that has got a full view and we also have
5256 -- built a completion just above, show that the completion
5257 -- is a clone of the full view to the back-end.
5259 if Has_Discriminants (T)
5260 and then not Has_Unknown_Discriminants (T)
5261 and then not Is_Empty_Elmt_List (Discriminant_Constraint (T))
5262 and then Present (Full_View (T))
5263 and then Present (Full_View (Id))
5264 then
5265 Set_Cloned_Subtype (Full_View (Id), Full_View (T));
5266 end if;
5268 when Access_Kind =>
5269 Set_Ekind (Id, E_Access_Subtype);
5270 Set_Is_Constrained (Id, Is_Constrained (T));
5271 Set_Is_Access_Constant
5272 (Id, Is_Access_Constant (T));
5273 Set_Directly_Designated_Type
5274 (Id, Designated_Type (T));
5275 Set_Can_Never_Be_Null (Id, Can_Never_Be_Null (T));
5277 -- A Pure library_item must not contain the declaration of a
5278 -- named access type, except within a subprogram, generic
5279 -- subprogram, task unit, or protected unit, or if it has
5280 -- a specified Storage_Size of zero (RM05-10.2.1(15.4-15.5)).
5282 if Comes_From_Source (Id)
5283 and then In_Pure_Unit
5284 and then not In_Subprogram_Task_Protected_Unit
5285 and then not No_Pool_Assigned (Id)
5286 then
5287 Error_Msg_N
5288 ("named access types not allowed in pure unit", N);
5289 end if;
5291 when Concurrent_Kind =>
5292 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
5293 Set_Corresponding_Record_Type (Id,
5294 Corresponding_Record_Type (T));
5295 Set_First_Entity (Id, First_Entity (T));
5296 Set_First_Private_Entity (Id, First_Private_Entity (T));
5297 Set_Has_Discriminants (Id, Has_Discriminants (T));
5298 Set_Is_Constrained (Id, Is_Constrained (T));
5299 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5300 Set_Last_Entity (Id, Last_Entity (T));
5302 if Is_Tagged_Type (T) then
5303 Set_No_Tagged_Streams_Pragma
5304 (Id, No_Tagged_Streams_Pragma (T));
5305 end if;
5307 if Has_Discriminants (T) then
5308 Set_Discriminant_Constraint
5309 (Id, Discriminant_Constraint (T));
5310 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5311 end if;
5313 when Incomplete_Kind =>
5314 if Ada_Version >= Ada_2005 then
5316 -- In Ada 2005 an incomplete type can be explicitly tagged:
5317 -- propagate indication. Note that we also have to include
5318 -- subtypes for Ada 2012 extended use of incomplete types.
5320 Set_Ekind (Id, E_Incomplete_Subtype);
5321 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5322 Set_Private_Dependents (Id, New_Elmt_List);
5324 if Is_Tagged_Type (Id) then
5325 Set_No_Tagged_Streams_Pragma
5326 (Id, No_Tagged_Streams_Pragma (T));
5327 Set_Direct_Primitive_Operations (Id, New_Elmt_List);
5328 end if;
5330 -- Ada 2005 (AI-412): Decorate an incomplete subtype of an
5331 -- incomplete type visible through a limited with clause.
5333 if From_Limited_With (T)
5334 and then Present (Non_Limited_View (T))
5335 then
5336 Set_From_Limited_With (Id);
5337 Set_Non_Limited_View (Id, Non_Limited_View (T));
5339 -- Ada 2005 (AI-412): Add the regular incomplete subtype
5340 -- to the private dependents of the original incomplete
5341 -- type for future transformation.
5343 else
5344 Append_Elmt (Id, Private_Dependents (T));
5345 end if;
5347 -- If the subtype name denotes an incomplete type an error
5348 -- was already reported by Process_Subtype.
5350 else
5351 Set_Etype (Id, Any_Type);
5352 end if;
5354 when others =>
5355 raise Program_Error;
5356 end case;
5357 end if;
5359 if Etype (Id) = Any_Type then
5360 goto Leave;
5361 end if;
5363 -- Some common processing on all types
5365 Set_Size_Info (Id, T);
5366 Set_First_Rep_Item (Id, First_Rep_Item (T));
5368 -- If the parent type is a generic actual, so is the subtype. This may
5369 -- happen in a nested instance. Why Comes_From_Source test???
5371 if not Comes_From_Source (N) then
5372 Set_Is_Generic_Actual_Type (Id, Is_Generic_Actual_Type (T));
5373 end if;
5375 -- If this is a subtype declaration for an actual in an instance,
5376 -- inherit static and dynamic predicates if any.
5378 -- If declaration has no aspect specifications, inherit predicate
5379 -- info as well. Unclear how to handle the case of both specified
5380 -- and inherited predicates ??? Other inherited aspects, such as
5381 -- invariants, should be OK, but the combination with later pragmas
5382 -- may also require special merging.
5384 if Has_Predicates (T)
5385 and then Present (Predicate_Function (T))
5386 and then
5387 ((In_Instance and then not Comes_From_Source (N))
5388 or else No (Aspect_Specifications (N)))
5389 then
5390 Set_Subprograms_For_Type (Id, Subprograms_For_Type (T));
5392 if Has_Static_Predicate (T) then
5393 Set_Has_Static_Predicate (Id);
5394 Set_Static_Discrete_Predicate (Id, Static_Discrete_Predicate (T));
5395 end if;
5396 end if;
5398 -- Remaining processing depends on characteristics of base type
5400 T := Etype (Id);
5402 Set_Is_Immediately_Visible (Id, True);
5403 Set_Depends_On_Private (Id, Has_Private_Component (T));
5404 Set_Is_Descendant_Of_Address (Id, Is_Descendant_Of_Address (T));
5406 if Is_Interface (T) then
5407 Set_Is_Interface (Id);
5408 end if;
5410 if Present (Generic_Parent_Type (N))
5411 and then
5412 (Nkind (Parent (Generic_Parent_Type (N))) /=
5413 N_Formal_Type_Declaration
5414 or else Nkind (Formal_Type_Definition
5415 (Parent (Generic_Parent_Type (N)))) /=
5416 N_Formal_Private_Type_Definition)
5417 then
5418 if Is_Tagged_Type (Id) then
5420 -- If this is a generic actual subtype for a synchronized type,
5421 -- the primitive operations are those of the corresponding record
5422 -- for which there is a separate subtype declaration.
5424 if Is_Concurrent_Type (Id) then
5425 null;
5426 elsif Is_Class_Wide_Type (Id) then
5427 Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
5428 else
5429 Derive_Subprograms (Generic_Parent_Type (N), Id, T);
5430 end if;
5432 elsif Scope (Etype (Id)) /= Standard_Standard then
5433 Derive_Subprograms (Generic_Parent_Type (N), Id);
5434 end if;
5435 end if;
5437 if Is_Private_Type (T) and then Present (Full_View (T)) then
5438 Conditional_Delay (Id, Full_View (T));
5440 -- The subtypes of components or subcomponents of protected types
5441 -- do not need freeze nodes, which would otherwise appear in the
5442 -- wrong scope (before the freeze node for the protected type). The
5443 -- proper subtypes are those of the subcomponents of the corresponding
5444 -- record.
5446 elsif Ekind (Scope (Id)) /= E_Protected_Type
5447 and then Present (Scope (Scope (Id))) -- error defense
5448 and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
5449 then
5450 Conditional_Delay (Id, T);
5451 end if;
5453 -- Check that Constraint_Error is raised for a scalar subtype indication
5454 -- when the lower or upper bound of a non-null range lies outside the
5455 -- range of the type mark.
5457 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5458 if Is_Scalar_Type (Etype (Id))
5459 and then Scalar_Range (Id) /=
5460 Scalar_Range
5461 (Etype (Subtype_Mark (Subtype_Indication (N))))
5462 then
5463 Apply_Range_Check
5464 (Scalar_Range (Id),
5465 Etype (Subtype_Mark (Subtype_Indication (N))));
5467 -- In the array case, check compatibility for each index
5469 elsif Is_Array_Type (Etype (Id)) and then Present (First_Index (Id))
5470 then
5471 -- This really should be a subprogram that finds the indications
5472 -- to check???
5474 declare
5475 Subt_Index : Node_Id := First_Index (Id);
5476 Target_Index : Node_Id :=
5477 First_Index (Etype
5478 (Subtype_Mark (Subtype_Indication (N))));
5479 Has_Dyn_Chk : Boolean := Has_Dynamic_Range_Check (N);
5481 begin
5482 while Present (Subt_Index) loop
5483 if ((Nkind (Subt_Index) = N_Identifier
5484 and then Ekind (Entity (Subt_Index)) in Scalar_Kind)
5485 or else Nkind (Subt_Index) = N_Subtype_Indication)
5486 and then
5487 Nkind (Scalar_Range (Etype (Subt_Index))) = N_Range
5488 then
5489 declare
5490 Target_Typ : constant Entity_Id :=
5491 Etype (Target_Index);
5492 begin
5493 R_Checks :=
5494 Get_Range_Checks
5495 (Scalar_Range (Etype (Subt_Index)),
5496 Target_Typ,
5497 Etype (Subt_Index),
5498 Defining_Identifier (N));
5500 -- Reset Has_Dynamic_Range_Check on the subtype to
5501 -- prevent elision of the index check due to a dynamic
5502 -- check generated for a preceding index (needed since
5503 -- Insert_Range_Checks tries to avoid generating
5504 -- redundant checks on a given declaration).
5506 Set_Has_Dynamic_Range_Check (N, False);
5508 Insert_Range_Checks
5509 (R_Checks,
5511 Target_Typ,
5512 Sloc (Defining_Identifier (N)));
5514 -- Record whether this index involved a dynamic check
5516 Has_Dyn_Chk :=
5517 Has_Dyn_Chk or else Has_Dynamic_Range_Check (N);
5518 end;
5519 end if;
5521 Next_Index (Subt_Index);
5522 Next_Index (Target_Index);
5523 end loop;
5525 -- Finally, mark whether the subtype involves dynamic checks
5527 Set_Has_Dynamic_Range_Check (N, Has_Dyn_Chk);
5528 end;
5529 end if;
5530 end if;
5532 Set_Optimize_Alignment_Flags (Id);
5533 Check_Eliminated (Id);
5535 <<Leave>>
5536 if Has_Aspects (N) then
5537 Analyze_Aspect_Specifications (N, Id);
5538 end if;
5540 Analyze_Dimension (N);
5542 -- Check No_Dynamic_Sized_Objects restriction, which disallows subtype
5543 -- indications on composite types where the constraints are dynamic.
5544 -- Note that object declarations and aggregates generate implicit
5545 -- subtype declarations, which this covers. One special case is that the
5546 -- implicitly generated "=" for discriminated types includes an
5547 -- offending subtype declaration, which is harmless, so we ignore it
5548 -- here.
5550 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5551 declare
5552 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
5553 begin
5554 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint
5555 and then not (Is_Internal (Id)
5556 and then Is_TSS (Scope (Id),
5557 TSS_Composite_Equality))
5558 and then not Within_Init_Proc
5559 and then not All_Composite_Constraints_Static (Cstr)
5560 then
5561 Check_Restriction (No_Dynamic_Sized_Objects, Cstr);
5562 end if;
5563 end;
5564 end if;
5565 end Analyze_Subtype_Declaration;
5567 --------------------------------
5568 -- Analyze_Subtype_Indication --
5569 --------------------------------
5571 procedure Analyze_Subtype_Indication (N : Node_Id) is
5572 T : constant Entity_Id := Subtype_Mark (N);
5573 R : constant Node_Id := Range_Expression (Constraint (N));
5575 begin
5576 Analyze (T);
5578 if R /= Error then
5579 Analyze (R);
5580 Set_Etype (N, Etype (R));
5581 Resolve (R, Entity (T));
5582 else
5583 Set_Error_Posted (R);
5584 Set_Error_Posted (T);
5585 end if;
5586 end Analyze_Subtype_Indication;
5588 --------------------------
5589 -- Analyze_Variant_Part --
5590 --------------------------
5592 procedure Analyze_Variant_Part (N : Node_Id) is
5593 Discr_Name : Node_Id;
5594 Discr_Type : Entity_Id;
5596 procedure Process_Variant (A : Node_Id);
5597 -- Analyze declarations for a single variant
5599 package Analyze_Variant_Choices is
5600 new Generic_Analyze_Choices (Process_Variant);
5601 use Analyze_Variant_Choices;
5603 ---------------------
5604 -- Process_Variant --
5605 ---------------------
5607 procedure Process_Variant (A : Node_Id) is
5608 CL : constant Node_Id := Component_List (A);
5609 begin
5610 if not Null_Present (CL) then
5611 Analyze_Declarations (Component_Items (CL));
5613 if Present (Variant_Part (CL)) then
5614 Analyze (Variant_Part (CL));
5615 end if;
5616 end if;
5617 end Process_Variant;
5619 -- Start of processing for Analyze_Variant_Part
5621 begin
5622 Discr_Name := Name (N);
5623 Analyze (Discr_Name);
5625 -- If Discr_Name bad, get out (prevent cascaded errors)
5627 if Etype (Discr_Name) = Any_Type then
5628 return;
5629 end if;
5631 -- Check invalid discriminant in variant part
5633 if Ekind (Entity (Discr_Name)) /= E_Discriminant then
5634 Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
5635 end if;
5637 Discr_Type := Etype (Entity (Discr_Name));
5639 if not Is_Discrete_Type (Discr_Type) then
5640 Error_Msg_N
5641 ("discriminant in a variant part must be of a discrete type",
5642 Name (N));
5643 return;
5644 end if;
5646 -- Now analyze the choices, which also analyzes the declarations that
5647 -- are associated with each choice.
5649 Analyze_Choices (Variants (N), Discr_Type);
5651 -- Note: we used to instantiate and call Check_Choices here to check
5652 -- that the choices covered the discriminant, but it's too early to do
5653 -- that because of statically predicated subtypes, whose analysis may
5654 -- be deferred to their freeze point which may be as late as the freeze
5655 -- point of the containing record. So this call is now to be found in
5656 -- Freeze_Record_Declaration.
5658 end Analyze_Variant_Part;
5660 ----------------------------
5661 -- Array_Type_Declaration --
5662 ----------------------------
5664 procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
5665 Component_Def : constant Node_Id := Component_Definition (Def);
5666 Component_Typ : constant Node_Id := Subtype_Indication (Component_Def);
5667 P : constant Node_Id := Parent (Def);
5668 Element_Type : Entity_Id;
5669 Implicit_Base : Entity_Id;
5670 Index : Node_Id;
5671 Nb_Index : Nat;
5672 Priv : Entity_Id;
5673 Related_Id : Entity_Id := Empty;
5675 begin
5676 if Nkind (Def) = N_Constrained_Array_Definition then
5677 Index := First (Discrete_Subtype_Definitions (Def));
5678 else
5679 Index := First (Subtype_Marks (Def));
5680 end if;
5682 -- Find proper names for the implicit types which may be public. In case
5683 -- of anonymous arrays we use the name of the first object of that type
5684 -- as prefix.
5686 if No (T) then
5687 Related_Id := Defining_Identifier (P);
5688 else
5689 Related_Id := T;
5690 end if;
5692 Nb_Index := 1;
5693 while Present (Index) loop
5694 Analyze (Index);
5696 -- Test for odd case of trying to index a type by the type itself
5698 if Is_Entity_Name (Index) and then Entity (Index) = T then
5699 Error_Msg_N ("type& cannot be indexed by itself", Index);
5700 Set_Entity (Index, Standard_Boolean);
5701 Set_Etype (Index, Standard_Boolean);
5702 end if;
5704 -- Check SPARK restriction requiring a subtype mark
5706 if not Nkind_In (Index, N_Identifier, N_Expanded_Name) then
5707 Check_SPARK_05_Restriction ("subtype mark required", Index);
5708 end if;
5710 -- Add a subtype declaration for each index of private array type
5711 -- declaration whose etype is also private. For example:
5713 -- package Pkg is
5714 -- type Index is private;
5715 -- private
5716 -- type Table is array (Index) of ...
5717 -- end;
5719 -- This is currently required by the expander for the internally
5720 -- generated equality subprogram of records with variant parts in
5721 -- which the etype of some component is such private type.
5723 if Ekind (Current_Scope) = E_Package
5724 and then In_Private_Part (Current_Scope)
5725 and then Has_Private_Declaration (Etype (Index))
5726 then
5727 declare
5728 Loc : constant Source_Ptr := Sloc (Def);
5729 Decl : Entity_Id;
5730 New_E : Entity_Id;
5732 begin
5733 New_E := Make_Temporary (Loc, 'T');
5734 Set_Is_Internal (New_E);
5736 Decl :=
5737 Make_Subtype_Declaration (Loc,
5738 Defining_Identifier => New_E,
5739 Subtype_Indication =>
5740 New_Occurrence_Of (Etype (Index), Loc));
5742 Insert_Before (Parent (Def), Decl);
5743 Analyze (Decl);
5744 Set_Etype (Index, New_E);
5746 -- If the index is a range the Entity attribute is not
5747 -- available. Example:
5749 -- package Pkg is
5750 -- type T is private;
5751 -- private
5752 -- type T is new Natural;
5753 -- Table : array (T(1) .. T(10)) of Boolean;
5754 -- end Pkg;
5756 if Nkind (Index) /= N_Range then
5757 Set_Entity (Index, New_E);
5758 end if;
5759 end;
5760 end if;
5762 Make_Index (Index, P, Related_Id, Nb_Index);
5764 -- Check error of subtype with predicate for index type
5766 Bad_Predicated_Subtype_Use
5767 ("subtype& has predicate, not allowed as index subtype",
5768 Index, Etype (Index));
5770 -- Move to next index
5772 Next_Index (Index);
5773 Nb_Index := Nb_Index + 1;
5774 end loop;
5776 -- Process subtype indication if one is present
5778 if Present (Component_Typ) then
5779 Element_Type := Process_Subtype (Component_Typ, P, Related_Id, 'C');
5781 Set_Etype (Component_Typ, Element_Type);
5783 if not Nkind_In (Component_Typ, N_Identifier, N_Expanded_Name) then
5784 Check_SPARK_05_Restriction
5785 ("subtype mark required", Component_Typ);
5786 end if;
5788 -- Ada 2005 (AI-230): Access Definition case
5790 else pragma Assert (Present (Access_Definition (Component_Def)));
5792 -- Indicate that the anonymous access type is created by the
5793 -- array type declaration.
5795 Element_Type := Access_Definition
5796 (Related_Nod => P,
5797 N => Access_Definition (Component_Def));
5798 Set_Is_Local_Anonymous_Access (Element_Type);
5800 -- Propagate the parent. This field is needed if we have to generate
5801 -- the master_id associated with an anonymous access to task type
5802 -- component (see Expand_N_Full_Type_Declaration.Build_Master)
5804 Set_Parent (Element_Type, Parent (T));
5806 -- Ada 2005 (AI-230): In case of components that are anonymous access
5807 -- types the level of accessibility depends on the enclosing type
5808 -- declaration
5810 Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
5812 -- Ada 2005 (AI-254)
5814 declare
5815 CD : constant Node_Id :=
5816 Access_To_Subprogram_Definition
5817 (Access_Definition (Component_Def));
5818 begin
5819 if Present (CD) and then Protected_Present (CD) then
5820 Element_Type :=
5821 Replace_Anonymous_Access_To_Protected_Subprogram (Def);
5822 end if;
5823 end;
5824 end if;
5826 -- Constrained array case
5828 if No (T) then
5829 T := Create_Itype (E_Void, P, Related_Id, 'T');
5830 end if;
5832 if Nkind (Def) = N_Constrained_Array_Definition then
5834 -- Establish Implicit_Base as unconstrained base type
5836 Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
5838 Set_Etype (Implicit_Base, Implicit_Base);
5839 Set_Scope (Implicit_Base, Current_Scope);
5840 Set_Has_Delayed_Freeze (Implicit_Base);
5841 Set_Default_SSO (Implicit_Base);
5843 -- The constrained array type is a subtype of the unconstrained one
5845 Set_Ekind (T, E_Array_Subtype);
5846 Init_Size_Align (T);
5847 Set_Etype (T, Implicit_Base);
5848 Set_Scope (T, Current_Scope);
5849 Set_Is_Constrained (T);
5850 Set_First_Index (T,
5851 First (Discrete_Subtype_Definitions (Def)));
5852 Set_Has_Delayed_Freeze (T);
5854 -- Complete setup of implicit base type
5856 Set_Component_Size (Implicit_Base, Uint_0);
5857 Set_Component_Type (Implicit_Base, Element_Type);
5858 Set_Finalize_Storage_Only
5859 (Implicit_Base,
5860 Finalize_Storage_Only (Element_Type));
5861 Set_First_Index (Implicit_Base, First_Index (T));
5862 Set_Has_Controlled_Component
5863 (Implicit_Base,
5864 Has_Controlled_Component (Element_Type)
5865 or else Is_Controlled_Active (Element_Type));
5866 Set_Packed_Array_Impl_Type
5867 (Implicit_Base, Empty);
5869 Propagate_Concurrent_Flags (Implicit_Base, Element_Type);
5871 -- Unconstrained array case
5873 else
5874 Set_Ekind (T, E_Array_Type);
5875 Init_Size_Align (T);
5876 Set_Etype (T, T);
5877 Set_Scope (T, Current_Scope);
5878 Set_Component_Size (T, Uint_0);
5879 Set_Is_Constrained (T, False);
5880 Set_First_Index (T, First (Subtype_Marks (Def)));
5881 Set_Has_Delayed_Freeze (T, True);
5882 Propagate_Concurrent_Flags (T, Element_Type);
5883 Set_Has_Controlled_Component (T, Has_Controlled_Component
5884 (Element_Type)
5885 or else
5886 Is_Controlled_Active (Element_Type));
5887 Set_Finalize_Storage_Only (T, Finalize_Storage_Only
5888 (Element_Type));
5889 Set_Default_SSO (T);
5890 end if;
5892 -- Common attributes for both cases
5894 Set_Component_Type (Base_Type (T), Element_Type);
5895 Set_Packed_Array_Impl_Type (T, Empty);
5897 if Aliased_Present (Component_Definition (Def)) then
5898 Check_SPARK_05_Restriction
5899 ("aliased is not allowed", Component_Definition (Def));
5900 Set_Has_Aliased_Components (Etype (T));
5901 end if;
5903 -- Ada 2005 (AI-231): Propagate the null-excluding attribute to the
5904 -- array type to ensure that objects of this type are initialized.
5906 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (Element_Type) then
5907 Set_Can_Never_Be_Null (T);
5909 if Null_Exclusion_Present (Component_Definition (Def))
5911 -- No need to check itypes because in their case this check was
5912 -- done at their point of creation
5914 and then not Is_Itype (Element_Type)
5915 then
5916 Error_Msg_N
5917 ("`NOT NULL` not allowed (null already excluded)",
5918 Subtype_Indication (Component_Definition (Def)));
5919 end if;
5920 end if;
5922 Priv := Private_Component (Element_Type);
5924 if Present (Priv) then
5926 -- Check for circular definitions
5928 if Priv = Any_Type then
5929 Set_Component_Type (Etype (T), Any_Type);
5931 -- There is a gap in the visibility of operations on the composite
5932 -- type only if the component type is defined in a different scope.
5934 elsif Scope (Priv) = Current_Scope then
5935 null;
5937 elsif Is_Limited_Type (Priv) then
5938 Set_Is_Limited_Composite (Etype (T));
5939 Set_Is_Limited_Composite (T);
5940 else
5941 Set_Is_Private_Composite (Etype (T));
5942 Set_Is_Private_Composite (T);
5943 end if;
5944 end if;
5946 -- A syntax error in the declaration itself may lead to an empty index
5947 -- list, in which case do a minimal patch.
5949 if No (First_Index (T)) then
5950 Error_Msg_N ("missing index definition in array type declaration", T);
5952 declare
5953 Indexes : constant List_Id :=
5954 New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
5955 begin
5956 Set_Discrete_Subtype_Definitions (Def, Indexes);
5957 Set_First_Index (T, First (Indexes));
5958 return;
5959 end;
5960 end if;
5962 -- Create a concatenation operator for the new type. Internal array
5963 -- types created for packed entities do not need such, they are
5964 -- compatible with the user-defined type.
5966 if Number_Dimensions (T) = 1
5967 and then not Is_Packed_Array_Impl_Type (T)
5968 then
5969 New_Concatenation_Op (T);
5970 end if;
5972 -- In the case of an unconstrained array the parser has already verified
5973 -- that all the indexes are unconstrained but we still need to make sure
5974 -- that the element type is constrained.
5976 if not Is_Definite_Subtype (Element_Type) then
5977 Error_Msg_N
5978 ("unconstrained element type in array declaration",
5979 Subtype_Indication (Component_Def));
5981 elsif Is_Abstract_Type (Element_Type) then
5982 Error_Msg_N
5983 ("the type of a component cannot be abstract",
5984 Subtype_Indication (Component_Def));
5985 end if;
5987 -- There may be an invariant declared for the component type, but
5988 -- the construction of the component invariant checking procedure
5989 -- takes place during expansion.
5990 end Array_Type_Declaration;
5992 ------------------------------------------------------
5993 -- Replace_Anonymous_Access_To_Protected_Subprogram --
5994 ------------------------------------------------------
5996 function Replace_Anonymous_Access_To_Protected_Subprogram
5997 (N : Node_Id) return Entity_Id
5999 Loc : constant Source_Ptr := Sloc (N);
6001 Curr_Scope : constant Scope_Stack_Entry :=
6002 Scope_Stack.Table (Scope_Stack.Last);
6004 Anon : constant Entity_Id := Make_Temporary (Loc, 'S');
6006 Acc : Node_Id;
6007 -- Access definition in declaration
6009 Comp : Node_Id;
6010 -- Object definition or formal definition with an access definition
6012 Decl : Node_Id;
6013 -- Declaration of anonymous access to subprogram type
6015 Spec : Node_Id;
6016 -- Original specification in access to subprogram
6018 P : Node_Id;
6020 begin
6021 Set_Is_Internal (Anon);
6023 case Nkind (N) is
6024 when N_Constrained_Array_Definition
6025 | N_Component_Declaration
6026 | N_Unconstrained_Array_Definition
6028 Comp := Component_Definition (N);
6029 Acc := Access_Definition (Comp);
6031 when N_Discriminant_Specification =>
6032 Comp := Discriminant_Type (N);
6033 Acc := Comp;
6035 when N_Parameter_Specification =>
6036 Comp := Parameter_Type (N);
6037 Acc := Comp;
6039 when N_Access_Function_Definition =>
6040 Comp := Result_Definition (N);
6041 Acc := Comp;
6043 when N_Object_Declaration =>
6044 Comp := Object_Definition (N);
6045 Acc := Comp;
6047 when N_Function_Specification =>
6048 Comp := Result_Definition (N);
6049 Acc := Comp;
6051 when others =>
6052 raise Program_Error;
6053 end case;
6055 Spec := Access_To_Subprogram_Definition (Acc);
6057 Decl :=
6058 Make_Full_Type_Declaration (Loc,
6059 Defining_Identifier => Anon,
6060 Type_Definition => Copy_Separate_Tree (Spec));
6062 Mark_Rewrite_Insertion (Decl);
6064 -- In ASIS mode, analyze the profile on the original node, because
6065 -- the separate copy does not provide enough links to recover the
6066 -- original tree. Analysis is limited to type annotations, within
6067 -- a temporary scope that serves as an anonymous subprogram to collect
6068 -- otherwise useless temporaries and itypes.
6070 if ASIS_Mode then
6071 declare
6072 Typ : constant Entity_Id := Make_Temporary (Loc, 'S');
6074 begin
6075 if Nkind (Spec) = N_Access_Function_Definition then
6076 Set_Ekind (Typ, E_Function);
6077 else
6078 Set_Ekind (Typ, E_Procedure);
6079 end if;
6081 Set_Parent (Typ, N);
6082 Set_Scope (Typ, Current_Scope);
6083 Push_Scope (Typ);
6085 -- Nothing to do if procedure is parameterless
6087 if Present (Parameter_Specifications (Spec)) then
6088 Process_Formals (Parameter_Specifications (Spec), Spec);
6089 end if;
6091 if Nkind (Spec) = N_Access_Function_Definition then
6092 declare
6093 Def : constant Node_Id := Result_Definition (Spec);
6095 begin
6096 -- The result might itself be an anonymous access type, so
6097 -- have to recurse.
6099 if Nkind (Def) = N_Access_Definition then
6100 if Present (Access_To_Subprogram_Definition (Def)) then
6101 Set_Etype
6102 (Def,
6103 Replace_Anonymous_Access_To_Protected_Subprogram
6104 (Spec));
6105 else
6106 Find_Type (Subtype_Mark (Def));
6107 end if;
6109 else
6110 Find_Type (Def);
6111 end if;
6112 end;
6113 end if;
6115 End_Scope;
6116 end;
6117 end if;
6119 -- Insert the new declaration in the nearest enclosing scope. If the
6120 -- parent is a body and N is its return type, the declaration belongs
6121 -- in the enclosing scope. Likewise if N is the type of a parameter.
6123 P := Parent (N);
6125 if Nkind (N) = N_Function_Specification
6126 and then Nkind (P) = N_Subprogram_Body
6127 then
6128 P := Parent (P);
6129 elsif Nkind (N) = N_Parameter_Specification
6130 and then Nkind (P) in N_Subprogram_Specification
6131 and then Nkind (Parent (P)) = N_Subprogram_Body
6132 then
6133 P := Parent (Parent (P));
6134 end if;
6136 while Present (P) and then not Has_Declarations (P) loop
6137 P := Parent (P);
6138 end loop;
6140 pragma Assert (Present (P));
6142 if Nkind (P) = N_Package_Specification then
6143 Prepend (Decl, Visible_Declarations (P));
6144 else
6145 Prepend (Decl, Declarations (P));
6146 end if;
6148 -- Replace the anonymous type with an occurrence of the new declaration.
6149 -- In all cases the rewritten node does not have the null-exclusion
6150 -- attribute because (if present) it was already inherited by the
6151 -- anonymous entity (Anon). Thus, in case of components we do not
6152 -- inherit this attribute.
6154 if Nkind (N) = N_Parameter_Specification then
6155 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6156 Set_Etype (Defining_Identifier (N), Anon);
6157 Set_Null_Exclusion_Present (N, False);
6159 elsif Nkind (N) = N_Object_Declaration then
6160 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6161 Set_Etype (Defining_Identifier (N), Anon);
6163 elsif Nkind (N) = N_Access_Function_Definition then
6164 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6166 elsif Nkind (N) = N_Function_Specification then
6167 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6168 Set_Etype (Defining_Unit_Name (N), Anon);
6170 else
6171 Rewrite (Comp,
6172 Make_Component_Definition (Loc,
6173 Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
6174 end if;
6176 Mark_Rewrite_Insertion (Comp);
6178 if Nkind_In (N, N_Object_Declaration, N_Access_Function_Definition)
6179 or else (Nkind (Parent (N)) = N_Full_Type_Declaration
6180 and then not Is_Type (Current_Scope))
6181 then
6183 -- Declaration can be analyzed in the current scope.
6185 Analyze (Decl);
6187 else
6188 -- Temporarily remove the current scope (record or subprogram) from
6189 -- the stack to add the new declarations to the enclosing scope.
6190 -- The anonymous entity is an Itype with the proper attributes.
6192 Scope_Stack.Decrement_Last;
6193 Analyze (Decl);
6194 Set_Is_Itype (Anon);
6195 Set_Associated_Node_For_Itype (Anon, N);
6196 Scope_Stack.Append (Curr_Scope);
6197 end if;
6199 Set_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
6200 Set_Can_Use_Internal_Rep (Anon, not Always_Compatible_Rep_On_Target);
6201 return Anon;
6202 end Replace_Anonymous_Access_To_Protected_Subprogram;
6204 -------------------------------
6205 -- Build_Derived_Access_Type --
6206 -------------------------------
6208 procedure Build_Derived_Access_Type
6209 (N : Node_Id;
6210 Parent_Type : Entity_Id;
6211 Derived_Type : Entity_Id)
6213 S : constant Node_Id := Subtype_Indication (Type_Definition (N));
6215 Desig_Type : Entity_Id;
6216 Discr : Entity_Id;
6217 Discr_Con_Elist : Elist_Id;
6218 Discr_Con_El : Elmt_Id;
6219 Subt : Entity_Id;
6221 begin
6222 -- Set the designated type so it is available in case this is an access
6223 -- to a self-referential type, e.g. a standard list type with a next
6224 -- pointer. Will be reset after subtype is built.
6226 Set_Directly_Designated_Type
6227 (Derived_Type, Designated_Type (Parent_Type));
6229 Subt := Process_Subtype (S, N);
6231 if Nkind (S) /= N_Subtype_Indication
6232 and then Subt /= Base_Type (Subt)
6233 then
6234 Set_Ekind (Derived_Type, E_Access_Subtype);
6235 end if;
6237 if Ekind (Derived_Type) = E_Access_Subtype then
6238 declare
6239 Pbase : constant Entity_Id := Base_Type (Parent_Type);
6240 Ibase : constant Entity_Id :=
6241 Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
6242 Svg_Chars : constant Name_Id := Chars (Ibase);
6243 Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
6245 begin
6246 Copy_Node (Pbase, Ibase);
6248 -- Restore Itype status after Copy_Node
6250 Set_Is_Itype (Ibase);
6251 Set_Associated_Node_For_Itype (Ibase, N);
6253 Set_Chars (Ibase, Svg_Chars);
6254 Set_Next_Entity (Ibase, Svg_Next_E);
6255 Set_Sloc (Ibase, Sloc (Derived_Type));
6256 Set_Scope (Ibase, Scope (Derived_Type));
6257 Set_Freeze_Node (Ibase, Empty);
6258 Set_Is_Frozen (Ibase, False);
6259 Set_Comes_From_Source (Ibase, False);
6260 Set_Is_First_Subtype (Ibase, False);
6262 Set_Etype (Ibase, Pbase);
6263 Set_Etype (Derived_Type, Ibase);
6264 end;
6265 end if;
6267 Set_Directly_Designated_Type
6268 (Derived_Type, Designated_Type (Subt));
6270 Set_Is_Constrained (Derived_Type, Is_Constrained (Subt));
6271 Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
6272 Set_Size_Info (Derived_Type, Parent_Type);
6273 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
6274 Set_Depends_On_Private (Derived_Type,
6275 Has_Private_Component (Derived_Type));
6276 Conditional_Delay (Derived_Type, Subt);
6278 -- Ada 2005 (AI-231): Set the null-exclusion attribute, and verify
6279 -- that it is not redundant.
6281 if Null_Exclusion_Present (Type_Definition (N)) then
6282 Set_Can_Never_Be_Null (Derived_Type);
6284 elsif Can_Never_Be_Null (Parent_Type) then
6285 Set_Can_Never_Be_Null (Derived_Type);
6286 end if;
6288 -- Note: we do not copy the Storage_Size_Variable, since we always go to
6289 -- the root type for this information.
6291 -- Apply range checks to discriminants for derived record case
6292 -- ??? THIS CODE SHOULD NOT BE HERE REALLY.
6294 Desig_Type := Designated_Type (Derived_Type);
6296 if Is_Composite_Type (Desig_Type)
6297 and then (not Is_Array_Type (Desig_Type))
6298 and then Has_Discriminants (Desig_Type)
6299 and then Base_Type (Desig_Type) /= Desig_Type
6300 then
6301 Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
6302 Discr_Con_El := First_Elmt (Discr_Con_Elist);
6304 Discr := First_Discriminant (Base_Type (Desig_Type));
6305 while Present (Discr_Con_El) loop
6306 Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
6307 Next_Elmt (Discr_Con_El);
6308 Next_Discriminant (Discr);
6309 end loop;
6310 end if;
6311 end Build_Derived_Access_Type;
6313 ------------------------------
6314 -- Build_Derived_Array_Type --
6315 ------------------------------
6317 procedure Build_Derived_Array_Type
6318 (N : Node_Id;
6319 Parent_Type : Entity_Id;
6320 Derived_Type : Entity_Id)
6322 Loc : constant Source_Ptr := Sloc (N);
6323 Tdef : constant Node_Id := Type_Definition (N);
6324 Indic : constant Node_Id := Subtype_Indication (Tdef);
6325 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
6326 Implicit_Base : Entity_Id;
6327 New_Indic : Node_Id;
6329 procedure Make_Implicit_Base;
6330 -- If the parent subtype is constrained, the derived type is a subtype
6331 -- of an implicit base type derived from the parent base.
6333 ------------------------
6334 -- Make_Implicit_Base --
6335 ------------------------
6337 procedure Make_Implicit_Base is
6338 begin
6339 Implicit_Base :=
6340 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
6342 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
6343 Set_Etype (Implicit_Base, Parent_Base);
6345 Copy_Array_Subtype_Attributes (Implicit_Base, Parent_Base);
6346 Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
6348 Set_Has_Delayed_Freeze (Implicit_Base, True);
6349 end Make_Implicit_Base;
6351 -- Start of processing for Build_Derived_Array_Type
6353 begin
6354 if not Is_Constrained (Parent_Type) then
6355 if Nkind (Indic) /= N_Subtype_Indication then
6356 Set_Ekind (Derived_Type, E_Array_Type);
6358 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6359 Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
6361 Set_Has_Delayed_Freeze (Derived_Type, True);
6363 else
6364 Make_Implicit_Base;
6365 Set_Etype (Derived_Type, Implicit_Base);
6367 New_Indic :=
6368 Make_Subtype_Declaration (Loc,
6369 Defining_Identifier => Derived_Type,
6370 Subtype_Indication =>
6371 Make_Subtype_Indication (Loc,
6372 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
6373 Constraint => Constraint (Indic)));
6375 Rewrite (N, New_Indic);
6376 Analyze (N);
6377 end if;
6379 else
6380 if Nkind (Indic) /= N_Subtype_Indication then
6381 Make_Implicit_Base;
6383 Set_Ekind (Derived_Type, Ekind (Parent_Type));
6384 Set_Etype (Derived_Type, Implicit_Base);
6385 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6387 else
6388 Error_Msg_N ("illegal constraint on constrained type", Indic);
6389 end if;
6390 end if;
6392 -- If parent type is not a derived type itself, and is declared in
6393 -- closed scope (e.g. a subprogram), then we must explicitly introduce
6394 -- the new type's concatenation operator since Derive_Subprograms
6395 -- will not inherit the parent's operator. If the parent type is
6396 -- unconstrained, the operator is of the unconstrained base type.
6398 if Number_Dimensions (Parent_Type) = 1
6399 and then not Is_Limited_Type (Parent_Type)
6400 and then not Is_Derived_Type (Parent_Type)
6401 and then not Is_Package_Or_Generic_Package
6402 (Scope (Base_Type (Parent_Type)))
6403 then
6404 if not Is_Constrained (Parent_Type)
6405 and then Is_Constrained (Derived_Type)
6406 then
6407 New_Concatenation_Op (Implicit_Base);
6408 else
6409 New_Concatenation_Op (Derived_Type);
6410 end if;
6411 end if;
6412 end Build_Derived_Array_Type;
6414 -----------------------------------
6415 -- Build_Derived_Concurrent_Type --
6416 -----------------------------------
6418 procedure Build_Derived_Concurrent_Type
6419 (N : Node_Id;
6420 Parent_Type : Entity_Id;
6421 Derived_Type : Entity_Id)
6423 Loc : constant Source_Ptr := Sloc (N);
6425 Corr_Record : constant Entity_Id := Make_Temporary (Loc, 'C');
6426 Corr_Decl : Node_Id;
6427 Corr_Decl_Needed : Boolean;
6428 -- If the derived type has fewer discriminants than its parent, the
6429 -- corresponding record is also a derived type, in order to account for
6430 -- the bound discriminants. We create a full type declaration for it in
6431 -- this case.
6433 Constraint_Present : constant Boolean :=
6434 Nkind (Subtype_Indication (Type_Definition (N))) =
6435 N_Subtype_Indication;
6437 D_Constraint : Node_Id;
6438 New_Constraint : Elist_Id;
6439 Old_Disc : Entity_Id;
6440 New_Disc : Entity_Id;
6441 New_N : Node_Id;
6443 begin
6444 Set_Stored_Constraint (Derived_Type, No_Elist);
6445 Corr_Decl_Needed := False;
6446 Old_Disc := Empty;
6448 if Present (Discriminant_Specifications (N))
6449 and then Constraint_Present
6450 then
6451 Old_Disc := First_Discriminant (Parent_Type);
6452 New_Disc := First (Discriminant_Specifications (N));
6453 while Present (New_Disc) and then Present (Old_Disc) loop
6454 Next_Discriminant (Old_Disc);
6455 Next (New_Disc);
6456 end loop;
6457 end if;
6459 if Present (Old_Disc) and then Expander_Active then
6461 -- The new type has fewer discriminants, so we need to create a new
6462 -- corresponding record, which is derived from the corresponding
6463 -- record of the parent, and has a stored constraint that captures
6464 -- the values of the discriminant constraints. The corresponding
6465 -- record is needed only if expander is active and code generation is
6466 -- enabled.
6468 -- The type declaration for the derived corresponding record has the
6469 -- same discriminant part and constraints as the current declaration.
6470 -- Copy the unanalyzed tree to build declaration.
6472 Corr_Decl_Needed := True;
6473 New_N := Copy_Separate_Tree (N);
6475 Corr_Decl :=
6476 Make_Full_Type_Declaration (Loc,
6477 Defining_Identifier => Corr_Record,
6478 Discriminant_Specifications =>
6479 Discriminant_Specifications (New_N),
6480 Type_Definition =>
6481 Make_Derived_Type_Definition (Loc,
6482 Subtype_Indication =>
6483 Make_Subtype_Indication (Loc,
6484 Subtype_Mark =>
6485 New_Occurrence_Of
6486 (Corresponding_Record_Type (Parent_Type), Loc),
6487 Constraint =>
6488 Constraint
6489 (Subtype_Indication (Type_Definition (New_N))))));
6490 end if;
6492 -- Copy Storage_Size and Relative_Deadline variables if task case
6494 if Is_Task_Type (Parent_Type) then
6495 Set_Storage_Size_Variable (Derived_Type,
6496 Storage_Size_Variable (Parent_Type));
6497 Set_Relative_Deadline_Variable (Derived_Type,
6498 Relative_Deadline_Variable (Parent_Type));
6499 end if;
6501 if Present (Discriminant_Specifications (N)) then
6502 Push_Scope (Derived_Type);
6503 Check_Or_Process_Discriminants (N, Derived_Type);
6505 if Constraint_Present then
6506 New_Constraint :=
6507 Expand_To_Stored_Constraint
6508 (Parent_Type,
6509 Build_Discriminant_Constraints
6510 (Parent_Type,
6511 Subtype_Indication (Type_Definition (N)), True));
6512 end if;
6514 End_Scope;
6516 elsif Constraint_Present then
6518 -- Build constrained subtype, copying the constraint, and derive
6519 -- from it to create a derived constrained type.
6521 declare
6522 Loc : constant Source_Ptr := Sloc (N);
6523 Anon : constant Entity_Id :=
6524 Make_Defining_Identifier (Loc,
6525 Chars => New_External_Name (Chars (Derived_Type), 'T'));
6526 Decl : Node_Id;
6528 begin
6529 Decl :=
6530 Make_Subtype_Declaration (Loc,
6531 Defining_Identifier => Anon,
6532 Subtype_Indication =>
6533 New_Copy_Tree (Subtype_Indication (Type_Definition (N))));
6534 Insert_Before (N, Decl);
6535 Analyze (Decl);
6537 Rewrite (Subtype_Indication (Type_Definition (N)),
6538 New_Occurrence_Of (Anon, Loc));
6539 Set_Analyzed (Derived_Type, False);
6540 Analyze (N);
6541 return;
6542 end;
6543 end if;
6545 -- By default, operations and private data are inherited from parent.
6546 -- However, in the presence of bound discriminants, a new corresponding
6547 -- record will be created, see below.
6549 Set_Has_Discriminants
6550 (Derived_Type, Has_Discriminants (Parent_Type));
6551 Set_Corresponding_Record_Type
6552 (Derived_Type, Corresponding_Record_Type (Parent_Type));
6554 -- Is_Constrained is set according the parent subtype, but is set to
6555 -- False if the derived type is declared with new discriminants.
6557 Set_Is_Constrained
6558 (Derived_Type,
6559 (Is_Constrained (Parent_Type) or else Constraint_Present)
6560 and then not Present (Discriminant_Specifications (N)));
6562 if Constraint_Present then
6563 if not Has_Discriminants (Parent_Type) then
6564 Error_Msg_N ("untagged parent must have discriminants", N);
6566 elsif Present (Discriminant_Specifications (N)) then
6568 -- Verify that new discriminants are used to constrain old ones
6570 D_Constraint :=
6571 First
6572 (Constraints
6573 (Constraint (Subtype_Indication (Type_Definition (N)))));
6575 Old_Disc := First_Discriminant (Parent_Type);
6577 while Present (D_Constraint) loop
6578 if Nkind (D_Constraint) /= N_Discriminant_Association then
6580 -- Positional constraint. If it is a reference to a new
6581 -- discriminant, it constrains the corresponding old one.
6583 if Nkind (D_Constraint) = N_Identifier then
6584 New_Disc := First_Discriminant (Derived_Type);
6585 while Present (New_Disc) loop
6586 exit when Chars (New_Disc) = Chars (D_Constraint);
6587 Next_Discriminant (New_Disc);
6588 end loop;
6590 if Present (New_Disc) then
6591 Set_Corresponding_Discriminant (New_Disc, Old_Disc);
6592 end if;
6593 end if;
6595 Next_Discriminant (Old_Disc);
6597 -- if this is a named constraint, search by name for the old
6598 -- discriminants constrained by the new one.
6600 elsif Nkind (Expression (D_Constraint)) = N_Identifier then
6602 -- Find new discriminant with that name
6604 New_Disc := First_Discriminant (Derived_Type);
6605 while Present (New_Disc) loop
6606 exit when
6607 Chars (New_Disc) = Chars (Expression (D_Constraint));
6608 Next_Discriminant (New_Disc);
6609 end loop;
6611 if Present (New_Disc) then
6613 -- Verify that new discriminant renames some discriminant
6614 -- of the parent type, and associate the new discriminant
6615 -- with one or more old ones that it renames.
6617 declare
6618 Selector : Node_Id;
6620 begin
6621 Selector := First (Selector_Names (D_Constraint));
6622 while Present (Selector) loop
6623 Old_Disc := First_Discriminant (Parent_Type);
6624 while Present (Old_Disc) loop
6625 exit when Chars (Old_Disc) = Chars (Selector);
6626 Next_Discriminant (Old_Disc);
6627 end loop;
6629 if Present (Old_Disc) then
6630 Set_Corresponding_Discriminant
6631 (New_Disc, Old_Disc);
6632 end if;
6634 Next (Selector);
6635 end loop;
6636 end;
6637 end if;
6638 end if;
6640 Next (D_Constraint);
6641 end loop;
6643 New_Disc := First_Discriminant (Derived_Type);
6644 while Present (New_Disc) loop
6645 if No (Corresponding_Discriminant (New_Disc)) then
6646 Error_Msg_NE
6647 ("new discriminant& must constrain old one", N, New_Disc);
6649 elsif not
6650 Subtypes_Statically_Compatible
6651 (Etype (New_Disc),
6652 Etype (Corresponding_Discriminant (New_Disc)))
6653 then
6654 Error_Msg_NE
6655 ("& not statically compatible with parent discriminant",
6656 N, New_Disc);
6657 end if;
6659 Next_Discriminant (New_Disc);
6660 end loop;
6661 end if;
6663 elsif Present (Discriminant_Specifications (N)) then
6664 Error_Msg_N
6665 ("missing discriminant constraint in untagged derivation", N);
6666 end if;
6668 -- The entity chain of the derived type includes the new discriminants
6669 -- but shares operations with the parent.
6671 if Present (Discriminant_Specifications (N)) then
6672 Old_Disc := First_Discriminant (Parent_Type);
6673 while Present (Old_Disc) loop
6674 if No (Next_Entity (Old_Disc))
6675 or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
6676 then
6677 Set_Next_Entity
6678 (Last_Entity (Derived_Type), Next_Entity (Old_Disc));
6679 exit;
6680 end if;
6682 Next_Discriminant (Old_Disc);
6683 end loop;
6685 else
6686 Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
6687 if Has_Discriminants (Parent_Type) then
6688 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
6689 Set_Discriminant_Constraint (
6690 Derived_Type, Discriminant_Constraint (Parent_Type));
6691 end if;
6692 end if;
6694 Set_Last_Entity (Derived_Type, Last_Entity (Parent_Type));
6696 Set_Has_Completion (Derived_Type);
6698 if Corr_Decl_Needed then
6699 Set_Stored_Constraint (Derived_Type, New_Constraint);
6700 Insert_After (N, Corr_Decl);
6701 Analyze (Corr_Decl);
6702 Set_Corresponding_Record_Type (Derived_Type, Corr_Record);
6703 end if;
6704 end Build_Derived_Concurrent_Type;
6706 ------------------------------------
6707 -- Build_Derived_Enumeration_Type --
6708 ------------------------------------
6710 procedure Build_Derived_Enumeration_Type
6711 (N : Node_Id;
6712 Parent_Type : Entity_Id;
6713 Derived_Type : Entity_Id)
6715 Loc : constant Source_Ptr := Sloc (N);
6716 Def : constant Node_Id := Type_Definition (N);
6717 Indic : constant Node_Id := Subtype_Indication (Def);
6718 Implicit_Base : Entity_Id;
6719 Literal : Entity_Id;
6720 New_Lit : Entity_Id;
6721 Literals_List : List_Id;
6722 Type_Decl : Node_Id;
6723 Hi, Lo : Node_Id;
6724 Rang_Expr : Node_Id;
6726 begin
6727 -- Since types Standard.Character and Standard.[Wide_]Wide_Character do
6728 -- not have explicit literals lists we need to process types derived
6729 -- from them specially. This is handled by Derived_Standard_Character.
6730 -- If the parent type is a generic type, there are no literals either,
6731 -- and we construct the same skeletal representation as for the generic
6732 -- parent type.
6734 if Is_Standard_Character_Type (Parent_Type) then
6735 Derived_Standard_Character (N, Parent_Type, Derived_Type);
6737 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
6738 declare
6739 Lo : Node_Id;
6740 Hi : Node_Id;
6742 begin
6743 if Nkind (Indic) /= N_Subtype_Indication then
6744 Lo :=
6745 Make_Attribute_Reference (Loc,
6746 Attribute_Name => Name_First,
6747 Prefix => New_Occurrence_Of (Derived_Type, Loc));
6748 Set_Etype (Lo, Derived_Type);
6750 Hi :=
6751 Make_Attribute_Reference (Loc,
6752 Attribute_Name => Name_Last,
6753 Prefix => New_Occurrence_Of (Derived_Type, Loc));
6754 Set_Etype (Hi, Derived_Type);
6756 Set_Scalar_Range (Derived_Type,
6757 Make_Range (Loc,
6758 Low_Bound => Lo,
6759 High_Bound => Hi));
6760 else
6762 -- Analyze subtype indication and verify compatibility
6763 -- with parent type.
6765 if Base_Type (Process_Subtype (Indic, N)) /=
6766 Base_Type (Parent_Type)
6767 then
6768 Error_Msg_N
6769 ("illegal constraint for formal discrete type", N);
6770 end if;
6771 end if;
6772 end;
6774 else
6775 -- If a constraint is present, analyze the bounds to catch
6776 -- premature usage of the derived literals.
6778 if Nkind (Indic) = N_Subtype_Indication
6779 and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
6780 then
6781 Analyze (Low_Bound (Range_Expression (Constraint (Indic))));
6782 Analyze (High_Bound (Range_Expression (Constraint (Indic))));
6783 end if;
6785 -- Introduce an implicit base type for the derived type even if there
6786 -- is no constraint attached to it, since this seems closer to the
6787 -- Ada semantics. Build a full type declaration tree for the derived
6788 -- type using the implicit base type as the defining identifier. The
6789 -- build a subtype declaration tree which applies the constraint (if
6790 -- any) have it replace the derived type declaration.
6792 Literal := First_Literal (Parent_Type);
6793 Literals_List := New_List;
6794 while Present (Literal)
6795 and then Ekind (Literal) = E_Enumeration_Literal
6796 loop
6797 -- Literals of the derived type have the same representation as
6798 -- those of the parent type, but this representation can be
6799 -- overridden by an explicit representation clause. Indicate
6800 -- that there is no explicit representation given yet. These
6801 -- derived literals are implicit operations of the new type,
6802 -- and can be overridden by explicit ones.
6804 if Nkind (Literal) = N_Defining_Character_Literal then
6805 New_Lit :=
6806 Make_Defining_Character_Literal (Loc, Chars (Literal));
6807 else
6808 New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
6809 end if;
6811 Set_Ekind (New_Lit, E_Enumeration_Literal);
6812 Set_Enumeration_Pos (New_Lit, Enumeration_Pos (Literal));
6813 Set_Enumeration_Rep (New_Lit, Enumeration_Rep (Literal));
6814 Set_Enumeration_Rep_Expr (New_Lit, Empty);
6815 Set_Alias (New_Lit, Literal);
6816 Set_Is_Known_Valid (New_Lit, True);
6818 Append (New_Lit, Literals_List);
6819 Next_Literal (Literal);
6820 end loop;
6822 Implicit_Base :=
6823 Make_Defining_Identifier (Sloc (Derived_Type),
6824 Chars => New_External_Name (Chars (Derived_Type), 'B'));
6826 -- Indicate the proper nature of the derived type. This must be done
6827 -- before analysis of the literals, to recognize cases when a literal
6828 -- may be hidden by a previous explicit function definition (cf.
6829 -- c83031a).
6831 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
6832 Set_Etype (Derived_Type, Implicit_Base);
6834 Type_Decl :=
6835 Make_Full_Type_Declaration (Loc,
6836 Defining_Identifier => Implicit_Base,
6837 Discriminant_Specifications => No_List,
6838 Type_Definition =>
6839 Make_Enumeration_Type_Definition (Loc, Literals_List));
6841 Mark_Rewrite_Insertion (Type_Decl);
6842 Insert_Before (N, Type_Decl);
6843 Analyze (Type_Decl);
6845 -- The anonymous base now has a full declaration, but this base
6846 -- is not a first subtype.
6848 Set_Is_First_Subtype (Implicit_Base, False);
6850 -- After the implicit base is analyzed its Etype needs to be changed
6851 -- to reflect the fact that it is derived from the parent type which
6852 -- was ignored during analysis. We also set the size at this point.
6854 Set_Etype (Implicit_Base, Parent_Type);
6856 Set_Size_Info (Implicit_Base, Parent_Type);
6857 Set_RM_Size (Implicit_Base, RM_Size (Parent_Type));
6858 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
6860 -- Copy other flags from parent type
6862 Set_Has_Non_Standard_Rep
6863 (Implicit_Base, Has_Non_Standard_Rep
6864 (Parent_Type));
6865 Set_Has_Pragma_Ordered
6866 (Implicit_Base, Has_Pragma_Ordered
6867 (Parent_Type));
6868 Set_Has_Delayed_Freeze (Implicit_Base);
6870 -- Process the subtype indication including a validation check on the
6871 -- constraint, if any. If a constraint is given, its bounds must be
6872 -- implicitly converted to the new type.
6874 if Nkind (Indic) = N_Subtype_Indication then
6875 declare
6876 R : constant Node_Id :=
6877 Range_Expression (Constraint (Indic));
6879 begin
6880 if Nkind (R) = N_Range then
6881 Hi := Build_Scalar_Bound
6882 (High_Bound (R), Parent_Type, Implicit_Base);
6883 Lo := Build_Scalar_Bound
6884 (Low_Bound (R), Parent_Type, Implicit_Base);
6886 else
6887 -- Constraint is a Range attribute. Replace with explicit
6888 -- mention of the bounds of the prefix, which must be a
6889 -- subtype.
6891 Analyze (Prefix (R));
6892 Hi :=
6893 Convert_To (Implicit_Base,
6894 Make_Attribute_Reference (Loc,
6895 Attribute_Name => Name_Last,
6896 Prefix =>
6897 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
6899 Lo :=
6900 Convert_To (Implicit_Base,
6901 Make_Attribute_Reference (Loc,
6902 Attribute_Name => Name_First,
6903 Prefix =>
6904 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
6905 end if;
6906 end;
6908 else
6909 Hi :=
6910 Build_Scalar_Bound
6911 (Type_High_Bound (Parent_Type),
6912 Parent_Type, Implicit_Base);
6913 Lo :=
6914 Build_Scalar_Bound
6915 (Type_Low_Bound (Parent_Type),
6916 Parent_Type, Implicit_Base);
6917 end if;
6919 Rang_Expr :=
6920 Make_Range (Loc,
6921 Low_Bound => Lo,
6922 High_Bound => Hi);
6924 -- If we constructed a default range for the case where no range
6925 -- was given, then the expressions in the range must not freeze
6926 -- since they do not correspond to expressions in the source.
6927 -- However, if the type inherits predicates the expressions will
6928 -- be elaborated earlier and must freeze.
6930 if Nkind (Indic) /= N_Subtype_Indication
6931 and then not Has_Predicates (Derived_Type)
6932 then
6933 Set_Must_Not_Freeze (Lo);
6934 Set_Must_Not_Freeze (Hi);
6935 Set_Must_Not_Freeze (Rang_Expr);
6936 end if;
6938 Rewrite (N,
6939 Make_Subtype_Declaration (Loc,
6940 Defining_Identifier => Derived_Type,
6941 Subtype_Indication =>
6942 Make_Subtype_Indication (Loc,
6943 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
6944 Constraint =>
6945 Make_Range_Constraint (Loc,
6946 Range_Expression => Rang_Expr))));
6948 Analyze (N);
6950 -- Propagate the aspects from the original type declaration to the
6951 -- declaration of the implicit base.
6953 Move_Aspects (From => Original_Node (N), To => Type_Decl);
6955 -- Apply a range check. Since this range expression doesn't have an
6956 -- Etype, we have to specifically pass the Source_Typ parameter. Is
6957 -- this right???
6959 if Nkind (Indic) = N_Subtype_Indication then
6960 Apply_Range_Check
6961 (Range_Expression (Constraint (Indic)), Parent_Type,
6962 Source_Typ => Entity (Subtype_Mark (Indic)));
6963 end if;
6964 end if;
6965 end Build_Derived_Enumeration_Type;
6967 --------------------------------
6968 -- Build_Derived_Numeric_Type --
6969 --------------------------------
6971 procedure Build_Derived_Numeric_Type
6972 (N : Node_Id;
6973 Parent_Type : Entity_Id;
6974 Derived_Type : Entity_Id)
6976 Loc : constant Source_Ptr := Sloc (N);
6977 Tdef : constant Node_Id := Type_Definition (N);
6978 Indic : constant Node_Id := Subtype_Indication (Tdef);
6979 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
6980 No_Constraint : constant Boolean := Nkind (Indic) /=
6981 N_Subtype_Indication;
6982 Implicit_Base : Entity_Id;
6984 Lo : Node_Id;
6985 Hi : Node_Id;
6987 begin
6988 -- Process the subtype indication including a validation check on
6989 -- the constraint if any.
6991 Discard_Node (Process_Subtype (Indic, N));
6993 -- Introduce an implicit base type for the derived type even if there
6994 -- is no constraint attached to it, since this seems closer to the Ada
6995 -- semantics.
6997 Implicit_Base :=
6998 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
7000 Set_Etype (Implicit_Base, Parent_Base);
7001 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
7002 Set_Size_Info (Implicit_Base, Parent_Base);
7003 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
7004 Set_Parent (Implicit_Base, Parent (Derived_Type));
7005 Set_Is_Known_Valid (Implicit_Base, Is_Known_Valid (Parent_Base));
7007 -- Set RM Size for discrete type or decimal fixed-point type
7008 -- Ordinary fixed-point is excluded, why???
7010 if Is_Discrete_Type (Parent_Base)
7011 or else Is_Decimal_Fixed_Point_Type (Parent_Base)
7012 then
7013 Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
7014 end if;
7016 Set_Has_Delayed_Freeze (Implicit_Base);
7018 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
7019 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
7021 Set_Scalar_Range (Implicit_Base,
7022 Make_Range (Loc,
7023 Low_Bound => Lo,
7024 High_Bound => Hi));
7026 if Has_Infinities (Parent_Base) then
7027 Set_Includes_Infinities (Scalar_Range (Implicit_Base));
7028 end if;
7030 -- The Derived_Type, which is the entity of the declaration, is a
7031 -- subtype of the implicit base. Its Ekind is a subtype, even in the
7032 -- absence of an explicit constraint.
7034 Set_Etype (Derived_Type, Implicit_Base);
7036 -- If we did not have a constraint, then the Ekind is set from the
7037 -- parent type (otherwise Process_Subtype has set the bounds)
7039 if No_Constraint then
7040 Set_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
7041 end if;
7043 -- If we did not have a range constraint, then set the range from the
7044 -- parent type. Otherwise, the Process_Subtype call has set the bounds.
7046 if No_Constraint or else not Has_Range_Constraint (Indic) then
7047 Set_Scalar_Range (Derived_Type,
7048 Make_Range (Loc,
7049 Low_Bound => New_Copy_Tree (Type_Low_Bound (Parent_Type)),
7050 High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
7051 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7053 if Has_Infinities (Parent_Type) then
7054 Set_Includes_Infinities (Scalar_Range (Derived_Type));
7055 end if;
7057 Set_Is_Known_Valid (Derived_Type, Is_Known_Valid (Parent_Type));
7058 end if;
7060 Set_Is_Descendant_Of_Address (Derived_Type,
7061 Is_Descendant_Of_Address (Parent_Type));
7062 Set_Is_Descendant_Of_Address (Implicit_Base,
7063 Is_Descendant_Of_Address (Parent_Type));
7065 -- Set remaining type-specific fields, depending on numeric type
7067 if Is_Modular_Integer_Type (Parent_Type) then
7068 Set_Modulus (Implicit_Base, Modulus (Parent_Base));
7070 Set_Non_Binary_Modulus
7071 (Implicit_Base, Non_Binary_Modulus (Parent_Base));
7073 Set_Is_Known_Valid
7074 (Implicit_Base, Is_Known_Valid (Parent_Base));
7076 elsif Is_Floating_Point_Type (Parent_Type) then
7078 -- Digits of base type is always copied from the digits value of
7079 -- the parent base type, but the digits of the derived type will
7080 -- already have been set if there was a constraint present.
7082 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
7083 Set_Float_Rep (Implicit_Base, Float_Rep (Parent_Base));
7085 if No_Constraint then
7086 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
7087 end if;
7089 elsif Is_Fixed_Point_Type (Parent_Type) then
7091 -- Small of base type and derived type are always copied from the
7092 -- parent base type, since smalls never change. The delta of the
7093 -- base type is also copied from the parent base type. However the
7094 -- delta of the derived type will have been set already if a
7095 -- constraint was present.
7097 Set_Small_Value (Derived_Type, Small_Value (Parent_Base));
7098 Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
7099 Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
7101 if No_Constraint then
7102 Set_Delta_Value (Derived_Type, Delta_Value (Parent_Type));
7103 end if;
7105 -- The scale and machine radix in the decimal case are always
7106 -- copied from the parent base type.
7108 if Is_Decimal_Fixed_Point_Type (Parent_Type) then
7109 Set_Scale_Value (Derived_Type, Scale_Value (Parent_Base));
7110 Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
7112 Set_Machine_Radix_10
7113 (Derived_Type, Machine_Radix_10 (Parent_Base));
7114 Set_Machine_Radix_10
7115 (Implicit_Base, Machine_Radix_10 (Parent_Base));
7117 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
7119 if No_Constraint then
7120 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
7122 else
7123 -- the analysis of the subtype_indication sets the
7124 -- digits value of the derived type.
7126 null;
7127 end if;
7128 end if;
7129 end if;
7131 if Is_Integer_Type (Parent_Type) then
7132 Set_Has_Shift_Operator
7133 (Implicit_Base, Has_Shift_Operator (Parent_Type));
7134 end if;
7136 -- The type of the bounds is that of the parent type, and they
7137 -- must be converted to the derived type.
7139 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
7141 -- The implicit_base should be frozen when the derived type is frozen,
7142 -- but note that it is used in the conversions of the bounds. For fixed
7143 -- types we delay the determination of the bounds until the proper
7144 -- freezing point. For other numeric types this is rejected by GCC, for
7145 -- reasons that are currently unclear (???), so we choose to freeze the
7146 -- implicit base now. In the case of integers and floating point types
7147 -- this is harmless because subsequent representation clauses cannot
7148 -- affect anything, but it is still baffling that we cannot use the
7149 -- same mechanism for all derived numeric types.
7151 -- There is a further complication: actually some representation
7152 -- clauses can affect the implicit base type. For example, attribute
7153 -- definition clauses for stream-oriented attributes need to set the
7154 -- corresponding TSS entries on the base type, and this normally
7155 -- cannot be done after the base type is frozen, so the circuitry in
7156 -- Sem_Ch13.New_Stream_Subprogram must account for this possibility
7157 -- and not use Set_TSS in this case.
7159 -- There are also consequences for the case of delayed representation
7160 -- aspects for some cases. For example, a Size aspect is delayed and
7161 -- should not be evaluated to the freeze point. This early freezing
7162 -- means that the size attribute evaluation happens too early???
7164 if Is_Fixed_Point_Type (Parent_Type) then
7165 Conditional_Delay (Implicit_Base, Parent_Type);
7166 else
7167 Freeze_Before (N, Implicit_Base);
7168 end if;
7169 end Build_Derived_Numeric_Type;
7171 --------------------------------
7172 -- Build_Derived_Private_Type --
7173 --------------------------------
7175 procedure Build_Derived_Private_Type
7176 (N : Node_Id;
7177 Parent_Type : Entity_Id;
7178 Derived_Type : Entity_Id;
7179 Is_Completion : Boolean;
7180 Derive_Subps : Boolean := True)
7182 Loc : constant Source_Ptr := Sloc (N);
7183 Par_Base : constant Entity_Id := Base_Type (Parent_Type);
7184 Par_Scope : constant Entity_Id := Scope (Par_Base);
7185 Full_N : constant Node_Id := New_Copy_Tree (N);
7186 Full_Der : Entity_Id := New_Copy (Derived_Type);
7187 Full_P : Entity_Id;
7189 procedure Build_Full_Derivation;
7190 -- Build full derivation, i.e. derive from the full view
7192 procedure Copy_And_Build;
7193 -- Copy derived type declaration, replace parent with its full view,
7194 -- and build derivation
7196 ---------------------------
7197 -- Build_Full_Derivation --
7198 ---------------------------
7200 procedure Build_Full_Derivation is
7201 begin
7202 -- If parent scope is not open, install the declarations
7204 if not In_Open_Scopes (Par_Scope) then
7205 Install_Private_Declarations (Par_Scope);
7206 Install_Visible_Declarations (Par_Scope);
7207 Copy_And_Build;
7208 Uninstall_Declarations (Par_Scope);
7210 -- If parent scope is open and in another unit, and parent has a
7211 -- completion, then the derivation is taking place in the visible
7212 -- part of a child unit. In that case retrieve the full view of
7213 -- the parent momentarily.
7215 elsif not In_Same_Source_Unit (N, Parent_Type) then
7216 Full_P := Full_View (Parent_Type);
7217 Exchange_Declarations (Parent_Type);
7218 Copy_And_Build;
7219 Exchange_Declarations (Full_P);
7221 -- Otherwise it is a local derivation
7223 else
7224 Copy_And_Build;
7225 end if;
7226 end Build_Full_Derivation;
7228 --------------------
7229 -- Copy_And_Build --
7230 --------------------
7232 procedure Copy_And_Build is
7233 Full_Parent : Entity_Id := Parent_Type;
7235 begin
7236 -- If the parent is itself derived from another private type,
7237 -- installing the private declarations has not affected its
7238 -- privacy status, so use its own full view explicitly.
7240 if Is_Private_Type (Full_Parent)
7241 and then Present (Full_View (Full_Parent))
7242 then
7243 Full_Parent := Full_View (Full_Parent);
7244 end if;
7246 -- And its underlying full view if necessary
7248 if Is_Private_Type (Full_Parent)
7249 and then Present (Underlying_Full_View (Full_Parent))
7250 then
7251 Full_Parent := Underlying_Full_View (Full_Parent);
7252 end if;
7254 -- For record, access and most enumeration types, derivation from
7255 -- the full view requires a fully-fledged declaration. In the other
7256 -- cases, just use an itype.
7258 if Ekind (Full_Parent) in Record_Kind
7259 or else Ekind (Full_Parent) in Access_Kind
7260 or else
7261 (Ekind (Full_Parent) in Enumeration_Kind
7262 and then not Is_Standard_Character_Type (Full_Parent)
7263 and then not Is_Generic_Type (Root_Type (Full_Parent)))
7264 then
7265 -- Copy and adjust declaration to provide a completion for what
7266 -- is originally a private declaration. Indicate that full view
7267 -- is internally generated.
7269 Set_Comes_From_Source (Full_N, False);
7270 Set_Comes_From_Source (Full_Der, False);
7271 Set_Parent (Full_Der, Full_N);
7272 Set_Defining_Identifier (Full_N, Full_Der);
7274 -- If there are no constraints, adjust the subtype mark
7276 if Nkind (Subtype_Indication (Type_Definition (Full_N))) /=
7277 N_Subtype_Indication
7278 then
7279 Set_Subtype_Indication
7280 (Type_Definition (Full_N),
7281 New_Occurrence_Of (Full_Parent, Sloc (Full_N)));
7282 end if;
7284 Insert_After (N, Full_N);
7286 -- Build full view of derived type from full view of parent which
7287 -- is now installed. Subprograms have been derived on the partial
7288 -- view, the completion does not derive them anew.
7290 if Ekind (Full_Parent) in Record_Kind then
7292 -- If parent type is tagged, the completion inherits the proper
7293 -- primitive operations.
7295 if Is_Tagged_Type (Parent_Type) then
7296 Build_Derived_Record_Type
7297 (Full_N, Full_Parent, Full_Der, Derive_Subps);
7298 else
7299 Build_Derived_Record_Type
7300 (Full_N, Full_Parent, Full_Der, Derive_Subps => False);
7301 end if;
7303 else
7304 Build_Derived_Type
7305 (Full_N, Full_Parent, Full_Der,
7306 Is_Completion => False, Derive_Subps => False);
7307 end if;
7309 -- The full declaration has been introduced into the tree and
7310 -- processed in the step above. It should not be analyzed again
7311 -- (when encountered later in the current list of declarations)
7312 -- to prevent spurious name conflicts. The full entity remains
7313 -- invisible.
7315 Set_Analyzed (Full_N);
7317 else
7318 Full_Der :=
7319 Make_Defining_Identifier (Sloc (Derived_Type),
7320 Chars => Chars (Derived_Type));
7321 Set_Is_Itype (Full_Der);
7322 Set_Associated_Node_For_Itype (Full_Der, N);
7323 Set_Parent (Full_Der, N);
7324 Build_Derived_Type
7325 (N, Full_Parent, Full_Der,
7326 Is_Completion => False, Derive_Subps => False);
7327 end if;
7329 Set_Has_Private_Declaration (Full_Der);
7330 Set_Has_Private_Declaration (Derived_Type);
7332 Set_Scope (Full_Der, Scope (Derived_Type));
7333 Set_Is_First_Subtype (Full_Der, Is_First_Subtype (Derived_Type));
7334 Set_Has_Size_Clause (Full_Der, False);
7335 Set_Has_Alignment_Clause (Full_Der, False);
7336 Set_Has_Delayed_Freeze (Full_Der);
7337 Set_Is_Frozen (Full_Der, False);
7338 Set_Freeze_Node (Full_Der, Empty);
7339 Set_Depends_On_Private (Full_Der, Has_Private_Component (Full_Der));
7340 Set_Is_Public (Full_Der, Is_Public (Derived_Type));
7342 -- The convention on the base type may be set in the private part
7343 -- and not propagated to the subtype until later, so we obtain the
7344 -- convention from the base type of the parent.
7346 Set_Convention (Full_Der, Convention (Base_Type (Full_Parent)));
7347 end Copy_And_Build;
7349 -- Start of processing for Build_Derived_Private_Type
7351 begin
7352 if Is_Tagged_Type (Parent_Type) then
7353 Full_P := Full_View (Parent_Type);
7355 -- A type extension of a type with unknown discriminants is an
7356 -- indefinite type that the back-end cannot handle directly.
7357 -- We treat it as a private type, and build a completion that is
7358 -- derived from the full view of the parent, and hopefully has
7359 -- known discriminants.
7361 -- If the full view of the parent type has an underlying record view,
7362 -- use it to generate the underlying record view of this derived type
7363 -- (required for chains of derivations with unknown discriminants).
7365 -- Minor optimization: we avoid the generation of useless underlying
7366 -- record view entities if the private type declaration has unknown
7367 -- discriminants but its corresponding full view has no
7368 -- discriminants.
7370 if Has_Unknown_Discriminants (Parent_Type)
7371 and then Present (Full_P)
7372 and then (Has_Discriminants (Full_P)
7373 or else Present (Underlying_Record_View (Full_P)))
7374 and then not In_Open_Scopes (Par_Scope)
7375 and then Expander_Active
7376 then
7377 declare
7378 Full_Der : constant Entity_Id := Make_Temporary (Loc, 'T');
7379 New_Ext : constant Node_Id :=
7380 Copy_Separate_Tree
7381 (Record_Extension_Part (Type_Definition (N)));
7382 Decl : Node_Id;
7384 begin
7385 Build_Derived_Record_Type
7386 (N, Parent_Type, Derived_Type, Derive_Subps);
7388 -- Build anonymous completion, as a derivation from the full
7389 -- view of the parent. This is not a completion in the usual
7390 -- sense, because the current type is not private.
7392 Decl :=
7393 Make_Full_Type_Declaration (Loc,
7394 Defining_Identifier => Full_Der,
7395 Type_Definition =>
7396 Make_Derived_Type_Definition (Loc,
7397 Subtype_Indication =>
7398 New_Copy_Tree
7399 (Subtype_Indication (Type_Definition (N))),
7400 Record_Extension_Part => New_Ext));
7402 -- If the parent type has an underlying record view, use it
7403 -- here to build the new underlying record view.
7405 if Present (Underlying_Record_View (Full_P)) then
7406 pragma Assert
7407 (Nkind (Subtype_Indication (Type_Definition (Decl)))
7408 = N_Identifier);
7409 Set_Entity (Subtype_Indication (Type_Definition (Decl)),
7410 Underlying_Record_View (Full_P));
7411 end if;
7413 Install_Private_Declarations (Par_Scope);
7414 Install_Visible_Declarations (Par_Scope);
7415 Insert_Before (N, Decl);
7417 -- Mark entity as an underlying record view before analysis,
7418 -- to avoid generating the list of its primitive operations
7419 -- (which is not really required for this entity) and thus
7420 -- prevent spurious errors associated with missing overriding
7421 -- of abstract primitives (overridden only for Derived_Type).
7423 Set_Ekind (Full_Der, E_Record_Type);
7424 Set_Is_Underlying_Record_View (Full_Der);
7425 Set_Default_SSO (Full_Der);
7427 Analyze (Decl);
7429 pragma Assert (Has_Discriminants (Full_Der)
7430 and then not Has_Unknown_Discriminants (Full_Der));
7432 Uninstall_Declarations (Par_Scope);
7434 -- Freeze the underlying record view, to prevent generation of
7435 -- useless dispatching information, which is simply shared with
7436 -- the real derived type.
7438 Set_Is_Frozen (Full_Der);
7440 -- If the derived type has access discriminants, create
7441 -- references to their anonymous types now, to prevent
7442 -- back-end problems when their first use is in generated
7443 -- bodies of primitives.
7445 declare
7446 E : Entity_Id;
7448 begin
7449 E := First_Entity (Full_Der);
7451 while Present (E) loop
7452 if Ekind (E) = E_Discriminant
7453 and then Ekind (Etype (E)) = E_Anonymous_Access_Type
7454 then
7455 Build_Itype_Reference (Etype (E), Decl);
7456 end if;
7458 Next_Entity (E);
7459 end loop;
7460 end;
7462 -- Set up links between real entity and underlying record view
7464 Set_Underlying_Record_View (Derived_Type, Base_Type (Full_Der));
7465 Set_Underlying_Record_View (Base_Type (Full_Der), Derived_Type);
7466 end;
7468 -- If discriminants are known, build derived record
7470 else
7471 Build_Derived_Record_Type
7472 (N, Parent_Type, Derived_Type, Derive_Subps);
7473 end if;
7475 return;
7477 elsif Has_Discriminants (Parent_Type) then
7479 -- Build partial view of derived type from partial view of parent.
7480 -- This must be done before building the full derivation because the
7481 -- second derivation will modify the discriminants of the first and
7482 -- the discriminants are chained with the rest of the components in
7483 -- the full derivation.
7485 Build_Derived_Record_Type
7486 (N, Parent_Type, Derived_Type, Derive_Subps);
7488 -- Build the full derivation if this is not the anonymous derived
7489 -- base type created by Build_Derived_Record_Type in the constrained
7490 -- case (see point 5. of its head comment) since we build it for the
7491 -- derived subtype. And skip it for protected types altogether, as
7492 -- gigi does not use these types directly.
7494 if Present (Full_View (Parent_Type))
7495 and then not Is_Itype (Derived_Type)
7496 and then not (Ekind (Full_View (Parent_Type)) in Protected_Kind)
7497 then
7498 declare
7499 Der_Base : constant Entity_Id := Base_Type (Derived_Type);
7500 Discr : Entity_Id;
7501 Last_Discr : Entity_Id;
7503 begin
7504 -- If this is not a completion, construct the implicit full
7505 -- view by deriving from the full view of the parent type.
7506 -- But if this is a completion, the derived private type
7507 -- being built is a full view and the full derivation can
7508 -- only be its underlying full view.
7510 Build_Full_Derivation;
7512 if not Is_Completion then
7513 Set_Full_View (Derived_Type, Full_Der);
7514 else
7515 Set_Underlying_Full_View (Derived_Type, Full_Der);
7516 Set_Is_Underlying_Full_View (Full_Der);
7517 end if;
7519 if not Is_Base_Type (Derived_Type) then
7520 Set_Full_View (Der_Base, Base_Type (Full_Der));
7521 end if;
7523 -- Copy the discriminant list from full view to the partial
7524 -- view (base type and its subtype). Gigi requires that the
7525 -- partial and full views have the same discriminants.
7527 -- Note that since the partial view points to discriminants
7528 -- in the full view, their scope will be that of the full
7529 -- view. This might cause some front end problems and need
7530 -- adjustment???
7532 Discr := First_Discriminant (Base_Type (Full_Der));
7533 Set_First_Entity (Der_Base, Discr);
7535 loop
7536 Last_Discr := Discr;
7537 Next_Discriminant (Discr);
7538 exit when No (Discr);
7539 end loop;
7541 Set_Last_Entity (Der_Base, Last_Discr);
7542 Set_First_Entity (Derived_Type, First_Entity (Der_Base));
7543 Set_Last_Entity (Derived_Type, Last_Entity (Der_Base));
7545 Set_Stored_Constraint
7546 (Full_Der, Stored_Constraint (Derived_Type));
7547 end;
7548 end if;
7550 elsif Present (Full_View (Parent_Type))
7551 and then Has_Discriminants (Full_View (Parent_Type))
7552 then
7553 if Has_Unknown_Discriminants (Parent_Type)
7554 and then Nkind (Subtype_Indication (Type_Definition (N))) =
7555 N_Subtype_Indication
7556 then
7557 Error_Msg_N
7558 ("cannot constrain type with unknown discriminants",
7559 Subtype_Indication (Type_Definition (N)));
7560 return;
7561 end if;
7563 -- If this is not a completion, construct the implicit full view by
7564 -- deriving from the full view of the parent type. But if this is a
7565 -- completion, the derived private type being built is a full view
7566 -- and the full derivation can only be its underlying full view.
7568 Build_Full_Derivation;
7570 if not Is_Completion then
7571 Set_Full_View (Derived_Type, Full_Der);
7572 else
7573 Set_Underlying_Full_View (Derived_Type, Full_Der);
7574 Set_Is_Underlying_Full_View (Full_Der);
7575 end if;
7577 -- In any case, the primitive operations are inherited from the
7578 -- parent type, not from the internal full view.
7580 Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
7582 if Derive_Subps then
7583 Derive_Subprograms (Parent_Type, Derived_Type);
7584 end if;
7586 Set_Stored_Constraint (Derived_Type, No_Elist);
7587 Set_Is_Constrained
7588 (Derived_Type, Is_Constrained (Full_View (Parent_Type)));
7590 else
7591 -- Untagged type, No discriminants on either view
7593 if Nkind (Subtype_Indication (Type_Definition (N))) =
7594 N_Subtype_Indication
7595 then
7596 Error_Msg_N
7597 ("illegal constraint on type without discriminants", N);
7598 end if;
7600 if Present (Discriminant_Specifications (N))
7601 and then Present (Full_View (Parent_Type))
7602 and then not Is_Tagged_Type (Full_View (Parent_Type))
7603 then
7604 Error_Msg_N ("cannot add discriminants to untagged type", N);
7605 end if;
7607 Set_Stored_Constraint (Derived_Type, No_Elist);
7608 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7609 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
7610 Set_Disable_Controlled (Derived_Type, Disable_Controlled
7611 (Parent_Type));
7612 Set_Has_Controlled_Component
7613 (Derived_Type, Has_Controlled_Component
7614 (Parent_Type));
7616 -- Direct controlled types do not inherit Finalize_Storage_Only flag
7618 if not Is_Controlled_Active (Parent_Type) then
7619 Set_Finalize_Storage_Only
7620 (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
7621 end if;
7623 -- If this is not a completion, construct the implicit full view by
7624 -- deriving from the full view of the parent type.
7626 -- ??? If the parent is untagged private and its completion is
7627 -- tagged, this mechanism will not work because we cannot derive from
7628 -- the tagged full view unless we have an extension.
7630 if Present (Full_View (Parent_Type))
7631 and then not Is_Tagged_Type (Full_View (Parent_Type))
7632 and then not Is_Completion
7633 then
7634 Build_Full_Derivation;
7635 Set_Full_View (Derived_Type, Full_Der);
7636 end if;
7637 end if;
7639 Set_Has_Unknown_Discriminants (Derived_Type,
7640 Has_Unknown_Discriminants (Parent_Type));
7642 if Is_Private_Type (Derived_Type) then
7643 Set_Private_Dependents (Derived_Type, New_Elmt_List);
7644 end if;
7646 -- If the parent base type is in scope, add the derived type to its
7647 -- list of private dependents, because its full view may become
7648 -- visible subsequently (in a nested private part, a body, or in a
7649 -- further child unit).
7651 if Is_Private_Type (Par_Base) and then In_Open_Scopes (Par_Scope) then
7652 Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
7654 -- Check for unusual case where a type completed by a private
7655 -- derivation occurs within a package nested in a child unit, and
7656 -- the parent is declared in an ancestor.
7658 if Is_Child_Unit (Scope (Current_Scope))
7659 and then Is_Completion
7660 and then In_Private_Part (Current_Scope)
7661 and then Scope (Parent_Type) /= Current_Scope
7663 -- Note that if the parent has a completion in the private part,
7664 -- (which is itself a derivation from some other private type)
7665 -- it is that completion that is visible, there is no full view
7666 -- available, and no special processing is needed.
7668 and then Present (Full_View (Parent_Type))
7669 then
7670 -- In this case, the full view of the parent type will become
7671 -- visible in the body of the enclosing child, and only then will
7672 -- the current type be possibly non-private. Build an underlying
7673 -- full view that will be installed when the enclosing child body
7674 -- is compiled.
7676 if Present (Underlying_Full_View (Derived_Type)) then
7677 Full_Der := Underlying_Full_View (Derived_Type);
7678 else
7679 Build_Full_Derivation;
7680 Set_Underlying_Full_View (Derived_Type, Full_Der);
7681 Set_Is_Underlying_Full_View (Full_Der);
7682 end if;
7684 -- The full view will be used to swap entities on entry/exit to
7685 -- the body, and must appear in the entity list for the package.
7687 Append_Entity (Full_Der, Scope (Derived_Type));
7688 end if;
7689 end if;
7690 end Build_Derived_Private_Type;
7692 -------------------------------
7693 -- Build_Derived_Record_Type --
7694 -------------------------------
7696 -- 1. INTRODUCTION
7698 -- Ideally we would like to use the same model of type derivation for
7699 -- tagged and untagged record types. Unfortunately this is not quite
7700 -- possible because the semantics of representation clauses is different
7701 -- for tagged and untagged records under inheritance. Consider the
7702 -- following:
7704 -- type R (...) is [tagged] record ... end record;
7705 -- type T (...) is new R (...) [with ...];
7707 -- The representation clauses for T can specify a completely different
7708 -- record layout from R's. Hence the same component can be placed in two
7709 -- very different positions in objects of type T and R. If R and T are
7710 -- tagged types, representation clauses for T can only specify the layout
7711 -- of non inherited components, thus components that are common in R and T
7712 -- have the same position in objects of type R and T.
7714 -- This has two implications. The first is that the entire tree for R's
7715 -- declaration needs to be copied for T in the untagged case, so that T
7716 -- can be viewed as a record type of its own with its own representation
7717 -- clauses. The second implication is the way we handle discriminants.
7718 -- Specifically, in the untagged case we need a way to communicate to Gigi
7719 -- what are the real discriminants in the record, while for the semantics
7720 -- we need to consider those introduced by the user to rename the
7721 -- discriminants in the parent type. This is handled by introducing the
7722 -- notion of stored discriminants. See below for more.
7724 -- Fortunately the way regular components are inherited can be handled in
7725 -- the same way in tagged and untagged types.
7727 -- To complicate things a bit more the private view of a private extension
7728 -- cannot be handled in the same way as the full view (for one thing the
7729 -- semantic rules are somewhat different). We will explain what differs
7730 -- below.
7732 -- 2. DISCRIMINANTS UNDER INHERITANCE
7734 -- The semantic rules governing the discriminants of derived types are
7735 -- quite subtle.
7737 -- type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
7738 -- [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
7740 -- If parent type has discriminants, then the discriminants that are
7741 -- declared in the derived type are [3.4 (11)]:
7743 -- o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
7744 -- there is one;
7746 -- o Otherwise, each discriminant of the parent type (implicitly declared
7747 -- in the same order with the same specifications). In this case, the
7748 -- discriminants are said to be "inherited", or if unknown in the parent
7749 -- are also unknown in the derived type.
7751 -- Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
7753 -- o The parent subtype must be constrained;
7755 -- o If the parent type is not a tagged type, then each discriminant of
7756 -- the derived type must be used in the constraint defining a parent
7757 -- subtype. [Implementation note: This ensures that the new discriminant
7758 -- can share storage with an existing discriminant.]
7760 -- For the derived type each discriminant of the parent type is either
7761 -- inherited, constrained to equal some new discriminant of the derived
7762 -- type, or constrained to the value of an expression.
7764 -- When inherited or constrained to equal some new discriminant, the
7765 -- parent discriminant and the discriminant of the derived type are said
7766 -- to "correspond".
7768 -- If a discriminant of the parent type is constrained to a specific value
7769 -- in the derived type definition, then the discriminant is said to be
7770 -- "specified" by that derived type definition.
7772 -- 3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
7774 -- We have spoken about stored discriminants in point 1 (introduction)
7775 -- above. There are two sort of stored discriminants: implicit and
7776 -- explicit. As long as the derived type inherits the same discriminants as
7777 -- the root record type, stored discriminants are the same as regular
7778 -- discriminants, and are said to be implicit. However, if any discriminant
7779 -- in the root type was renamed in the derived type, then the derived
7780 -- type will contain explicit stored discriminants. Explicit stored
7781 -- discriminants are discriminants in addition to the semantically visible
7782 -- discriminants defined for the derived type. Stored discriminants are
7783 -- used by Gigi to figure out what are the physical discriminants in
7784 -- objects of the derived type (see precise definition in einfo.ads).
7785 -- As an example, consider the following:
7787 -- type R (D1, D2, D3 : Int) is record ... end record;
7788 -- type T1 is new R;
7789 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
7790 -- type T3 is new T2;
7791 -- type T4 (Y : Int) is new T3 (Y, 99);
7793 -- The following table summarizes the discriminants and stored
7794 -- discriminants in R and T1 through T4.
7796 -- Type Discrim Stored Discrim Comment
7797 -- R (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in R
7798 -- T1 (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in T1
7799 -- T2 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T2
7800 -- T3 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T3
7801 -- T4 (Y) (D1, D2, D3) Girder discrims EXPLICIT in T4
7803 -- Field Corresponding_Discriminant (abbreviated CD below) allows us to
7804 -- find the corresponding discriminant in the parent type, while
7805 -- Original_Record_Component (abbreviated ORC below), the actual physical
7806 -- component that is renamed. Finally the field Is_Completely_Hidden
7807 -- (abbreviated ICH below) is set for all explicit stored discriminants
7808 -- (see einfo.ads for more info). For the above example this gives:
7810 -- Discrim CD ORC ICH
7811 -- ^^^^^^^ ^^ ^^^ ^^^
7812 -- D1 in R empty itself no
7813 -- D2 in R empty itself no
7814 -- D3 in R empty itself no
7816 -- D1 in T1 D1 in R itself no
7817 -- D2 in T1 D2 in R itself no
7818 -- D3 in T1 D3 in R itself no
7820 -- X1 in T2 D3 in T1 D3 in T2 no
7821 -- X2 in T2 D1 in T1 D1 in T2 no
7822 -- D1 in T2 empty itself yes
7823 -- D2 in T2 empty itself yes
7824 -- D3 in T2 empty itself yes
7826 -- X1 in T3 X1 in T2 D3 in T3 no
7827 -- X2 in T3 X2 in T2 D1 in T3 no
7828 -- D1 in T3 empty itself yes
7829 -- D2 in T3 empty itself yes
7830 -- D3 in T3 empty itself yes
7832 -- Y in T4 X1 in T3 D3 in T3 no
7833 -- D1 in T3 empty itself yes
7834 -- D2 in T3 empty itself yes
7835 -- D3 in T3 empty itself yes
7837 -- 4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
7839 -- Type derivation for tagged types is fairly straightforward. If no
7840 -- discriminants are specified by the derived type, these are inherited
7841 -- from the parent. No explicit stored discriminants are ever necessary.
7842 -- The only manipulation that is done to the tree is that of adding a
7843 -- _parent field with parent type and constrained to the same constraint
7844 -- specified for the parent in the derived type definition. For instance:
7846 -- type R (D1, D2, D3 : Int) is tagged record ... end record;
7847 -- type T1 is new R with null record;
7848 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
7850 -- are changed into:
7852 -- type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
7853 -- _parent : R (D1, D2, D3);
7854 -- end record;
7856 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
7857 -- _parent : T1 (X2, 88, X1);
7858 -- end record;
7860 -- The discriminants actually present in R, T1 and T2 as well as their CD,
7861 -- ORC and ICH fields are:
7863 -- Discrim CD ORC ICH
7864 -- ^^^^^^^ ^^ ^^^ ^^^
7865 -- D1 in R empty itself no
7866 -- D2 in R empty itself no
7867 -- D3 in R empty itself no
7869 -- D1 in T1 D1 in R D1 in R no
7870 -- D2 in T1 D2 in R D2 in R no
7871 -- D3 in T1 D3 in R D3 in R no
7873 -- X1 in T2 D3 in T1 D3 in R no
7874 -- X2 in T2 D1 in T1 D1 in R no
7876 -- 5. FIRST TRANSFORMATION FOR DERIVED RECORDS
7878 -- Regardless of whether we dealing with a tagged or untagged type
7879 -- we will transform all derived type declarations of the form
7881 -- type T is new R (...) [with ...];
7882 -- or
7883 -- subtype S is R (...);
7884 -- type T is new S [with ...];
7885 -- into
7886 -- type BT is new R [with ...];
7887 -- subtype T is BT (...);
7889 -- That is, the base derived type is constrained only if it has no
7890 -- discriminants. The reason for doing this is that GNAT's semantic model
7891 -- assumes that a base type with discriminants is unconstrained.
7893 -- Note that, strictly speaking, the above transformation is not always
7894 -- correct. Consider for instance the following excerpt from ACVC b34011a:
7896 -- procedure B34011A is
7897 -- type REC (D : integer := 0) is record
7898 -- I : Integer;
7899 -- end record;
7901 -- package P is
7902 -- type T6 is new Rec;
7903 -- function F return T6;
7904 -- end P;
7906 -- use P;
7907 -- package Q6 is
7908 -- type U is new T6 (Q6.F.I); -- ERROR: Q6.F.
7909 -- end Q6;
7911 -- The definition of Q6.U is illegal. However transforming Q6.U into
7913 -- type BaseU is new T6;
7914 -- subtype U is BaseU (Q6.F.I)
7916 -- turns U into a legal subtype, which is incorrect. To avoid this problem
7917 -- we always analyze the constraint (in this case (Q6.F.I)) before applying
7918 -- the transformation described above.
7920 -- There is another instance where the above transformation is incorrect.
7921 -- Consider:
7923 -- package Pack is
7924 -- type Base (D : Integer) is tagged null record;
7925 -- procedure P (X : Base);
7927 -- type Der is new Base (2) with null record;
7928 -- procedure P (X : Der);
7929 -- end Pack;
7931 -- Then the above transformation turns this into
7933 -- type Der_Base is new Base with null record;
7934 -- -- procedure P (X : Base) is implicitly inherited here
7935 -- -- as procedure P (X : Der_Base).
7937 -- subtype Der is Der_Base (2);
7938 -- procedure P (X : Der);
7939 -- -- The overriding of P (X : Der_Base) is illegal since we
7940 -- -- have a parameter conformance problem.
7942 -- To get around this problem, after having semantically processed Der_Base
7943 -- and the rewritten subtype declaration for Der, we copy Der_Base field
7944 -- Discriminant_Constraint from Der so that when parameter conformance is
7945 -- checked when P is overridden, no semantic errors are flagged.
7947 -- 6. SECOND TRANSFORMATION FOR DERIVED RECORDS
7949 -- Regardless of whether we are dealing with a tagged or untagged type
7950 -- we will transform all derived type declarations of the form
7952 -- type R (D1, .., Dn : ...) is [tagged] record ...;
7953 -- type T is new R [with ...];
7954 -- into
7955 -- type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
7957 -- The reason for such transformation is that it allows us to implement a
7958 -- very clean form of component inheritance as explained below.
7960 -- Note that this transformation is not achieved by direct tree rewriting
7961 -- and manipulation, but rather by redoing the semantic actions that the
7962 -- above transformation will entail. This is done directly in routine
7963 -- Inherit_Components.
7965 -- 7. TYPE DERIVATION AND COMPONENT INHERITANCE
7967 -- In both tagged and untagged derived types, regular non discriminant
7968 -- components are inherited in the derived type from the parent type. In
7969 -- the absence of discriminants component, inheritance is straightforward
7970 -- as components can simply be copied from the parent.
7972 -- If the parent has discriminants, inheriting components constrained with
7973 -- these discriminants requires caution. Consider the following example:
7975 -- type R (D1, D2 : Positive) is [tagged] record
7976 -- S : String (D1 .. D2);
7977 -- end record;
7979 -- type T1 is new R [with null record];
7980 -- type T2 (X : positive) is new R (1, X) [with null record];
7982 -- As explained in 6. above, T1 is rewritten as
7983 -- type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
7984 -- which makes the treatment for T1 and T2 identical.
7986 -- What we want when inheriting S, is that references to D1 and D2 in R are
7987 -- replaced with references to their correct constraints, i.e. D1 and D2 in
7988 -- T1 and 1 and X in T2. So all R's discriminant references are replaced
7989 -- with either discriminant references in the derived type or expressions.
7990 -- This replacement is achieved as follows: before inheriting R's
7991 -- components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
7992 -- created in the scope of T1 (resp. scope of T2) so that discriminants D1
7993 -- and D2 of T1 are visible (resp. discriminant X of T2 is visible).
7994 -- For T2, for instance, this has the effect of replacing String (D1 .. D2)
7995 -- by String (1 .. X).
7997 -- 8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
7999 -- We explain here the rules governing private type extensions relevant to
8000 -- type derivation. These rules are explained on the following example:
8002 -- type D [(...)] is new A [(...)] with private; <-- partial view
8003 -- type D [(...)] is new P [(...)] with null record; <-- full view
8005 -- Type A is called the ancestor subtype of the private extension.
8006 -- Type P is the parent type of the full view of the private extension. It
8007 -- must be A or a type derived from A.
8009 -- The rules concerning the discriminants of private type extensions are
8010 -- [7.3(10-13)]:
8012 -- o If a private extension inherits known discriminants from the ancestor
8013 -- subtype, then the full view must also inherit its discriminants from
8014 -- the ancestor subtype and the parent subtype of the full view must be
8015 -- constrained if and only if the ancestor subtype is constrained.
8017 -- o If a partial view has unknown discriminants, then the full view may
8018 -- define a definite or an indefinite subtype, with or without
8019 -- discriminants.
8021 -- o If a partial view has neither known nor unknown discriminants, then
8022 -- the full view must define a definite subtype.
8024 -- o If the ancestor subtype of a private extension has constrained
8025 -- discriminants, then the parent subtype of the full view must impose a
8026 -- statically matching constraint on those discriminants.
8028 -- This means that only the following forms of private extensions are
8029 -- allowed:
8031 -- type D is new A with private; <-- partial view
8032 -- type D is new P with null record; <-- full view
8034 -- If A has no discriminants than P has no discriminants, otherwise P must
8035 -- inherit A's discriminants.
8037 -- type D is new A (...) with private; <-- partial view
8038 -- type D is new P (:::) with null record; <-- full view
8040 -- P must inherit A's discriminants and (...) and (:::) must statically
8041 -- match.
8043 -- subtype A is R (...);
8044 -- type D is new A with private; <-- partial view
8045 -- type D is new P with null record; <-- full view
8047 -- P must have inherited R's discriminants and must be derived from A or
8048 -- any of its subtypes.
8050 -- type D (..) is new A with private; <-- partial view
8051 -- type D (..) is new P [(:::)] with null record; <-- full view
8053 -- No specific constraints on P's discriminants or constraint (:::).
8054 -- Note that A can be unconstrained, but the parent subtype P must either
8055 -- be constrained or (:::) must be present.
8057 -- type D (..) is new A [(...)] with private; <-- partial view
8058 -- type D (..) is new P [(:::)] with null record; <-- full view
8060 -- P's constraints on A's discriminants must statically match those
8061 -- imposed by (...).
8063 -- 9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
8065 -- The full view of a private extension is handled exactly as described
8066 -- above. The model chose for the private view of a private extension is
8067 -- the same for what concerns discriminants (i.e. they receive the same
8068 -- treatment as in the tagged case). However, the private view of the
8069 -- private extension always inherits the components of the parent base,
8070 -- without replacing any discriminant reference. Strictly speaking this is
8071 -- incorrect. However, Gigi never uses this view to generate code so this
8072 -- is a purely semantic issue. In theory, a set of transformations similar
8073 -- to those given in 5. and 6. above could be applied to private views of
8074 -- private extensions to have the same model of component inheritance as
8075 -- for non private extensions. However, this is not done because it would
8076 -- further complicate private type processing. Semantically speaking, this
8077 -- leaves us in an uncomfortable situation. As an example consider:
8079 -- package Pack is
8080 -- type R (D : integer) is tagged record
8081 -- S : String (1 .. D);
8082 -- end record;
8083 -- procedure P (X : R);
8084 -- type T is new R (1) with private;
8085 -- private
8086 -- type T is new R (1) with null record;
8087 -- end;
8089 -- This is transformed into:
8091 -- package Pack is
8092 -- type R (D : integer) is tagged record
8093 -- S : String (1 .. D);
8094 -- end record;
8095 -- procedure P (X : R);
8096 -- type T is new R (1) with private;
8097 -- private
8098 -- type BaseT is new R with null record;
8099 -- subtype T is BaseT (1);
8100 -- end;
8102 -- (strictly speaking the above is incorrect Ada)
8104 -- From the semantic standpoint the private view of private extension T
8105 -- should be flagged as constrained since one can clearly have
8107 -- Obj : T;
8109 -- in a unit withing Pack. However, when deriving subprograms for the
8110 -- private view of private extension T, T must be seen as unconstrained
8111 -- since T has discriminants (this is a constraint of the current
8112 -- subprogram derivation model). Thus, when processing the private view of
8113 -- a private extension such as T, we first mark T as unconstrained, we
8114 -- process it, we perform program derivation and just before returning from
8115 -- Build_Derived_Record_Type we mark T as constrained.
8117 -- ??? Are there are other uncomfortable cases that we will have to
8118 -- deal with.
8120 -- 10. RECORD_TYPE_WITH_PRIVATE complications
8122 -- Types that are derived from a visible record type and have a private
8123 -- extension present other peculiarities. They behave mostly like private
8124 -- types, but if they have primitive operations defined, these will not
8125 -- have the proper signatures for further inheritance, because other
8126 -- primitive operations will use the implicit base that we define for
8127 -- private derivations below. This affect subprogram inheritance (see
8128 -- Derive_Subprograms for details). We also derive the implicit base from
8129 -- the base type of the full view, so that the implicit base is a record
8130 -- type and not another private type, This avoids infinite loops.
8132 procedure Build_Derived_Record_Type
8133 (N : Node_Id;
8134 Parent_Type : Entity_Id;
8135 Derived_Type : Entity_Id;
8136 Derive_Subps : Boolean := True)
8138 Discriminant_Specs : constant Boolean :=
8139 Present (Discriminant_Specifications (N));
8140 Is_Tagged : constant Boolean := Is_Tagged_Type (Parent_Type);
8141 Loc : constant Source_Ptr := Sloc (N);
8142 Private_Extension : constant Boolean :=
8143 Nkind (N) = N_Private_Extension_Declaration;
8144 Assoc_List : Elist_Id;
8145 Constraint_Present : Boolean;
8146 Constrs : Elist_Id;
8147 Discrim : Entity_Id;
8148 Indic : Node_Id;
8149 Inherit_Discrims : Boolean := False;
8150 Last_Discrim : Entity_Id;
8151 New_Base : Entity_Id;
8152 New_Decl : Node_Id;
8153 New_Discrs : Elist_Id;
8154 New_Indic : Node_Id;
8155 Parent_Base : Entity_Id;
8156 Save_Etype : Entity_Id;
8157 Save_Discr_Constr : Elist_Id;
8158 Save_Next_Entity : Entity_Id;
8159 Type_Def : Node_Id;
8161 Discs : Elist_Id := New_Elmt_List;
8162 -- An empty Discs list means that there were no constraints in the
8163 -- subtype indication or that there was an error processing it.
8165 begin
8166 if Ekind (Parent_Type) = E_Record_Type_With_Private
8167 and then Present (Full_View (Parent_Type))
8168 and then Has_Discriminants (Parent_Type)
8169 then
8170 Parent_Base := Base_Type (Full_View (Parent_Type));
8171 else
8172 Parent_Base := Base_Type (Parent_Type);
8173 end if;
8175 -- AI05-0115 : if this is a derivation from a private type in some
8176 -- other scope that may lead to invisible components for the derived
8177 -- type, mark it accordingly.
8179 if Is_Private_Type (Parent_Type) then
8180 if Scope (Parent_Type) = Scope (Derived_Type) then
8181 null;
8183 elsif In_Open_Scopes (Scope (Parent_Type))
8184 and then In_Private_Part (Scope (Parent_Type))
8185 then
8186 null;
8188 else
8189 Set_Has_Private_Ancestor (Derived_Type);
8190 end if;
8192 else
8193 Set_Has_Private_Ancestor
8194 (Derived_Type, Has_Private_Ancestor (Parent_Type));
8195 end if;
8197 -- Before we start the previously documented transformations, here is
8198 -- little fix for size and alignment of tagged types. Normally when we
8199 -- derive type D from type P, we copy the size and alignment of P as the
8200 -- default for D, and in the absence of explicit representation clauses
8201 -- for D, the size and alignment are indeed the same as the parent.
8203 -- But this is wrong for tagged types, since fields may be added, and
8204 -- the default size may need to be larger, and the default alignment may
8205 -- need to be larger.
8207 -- We therefore reset the size and alignment fields in the tagged case.
8208 -- Note that the size and alignment will in any case be at least as
8209 -- large as the parent type (since the derived type has a copy of the
8210 -- parent type in the _parent field)
8212 -- The type is also marked as being tagged here, which is needed when
8213 -- processing components with a self-referential anonymous access type
8214 -- in the call to Check_Anonymous_Access_Components below. Note that
8215 -- this flag is also set later on for completeness.
8217 if Is_Tagged then
8218 Set_Is_Tagged_Type (Derived_Type);
8219 Init_Size_Align (Derived_Type);
8220 end if;
8222 -- STEP 0a: figure out what kind of derived type declaration we have
8224 if Private_Extension then
8225 Type_Def := N;
8226 Set_Ekind (Derived_Type, E_Record_Type_With_Private);
8227 Set_Default_SSO (Derived_Type);
8229 else
8230 Type_Def := Type_Definition (N);
8232 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
8233 -- Parent_Base can be a private type or private extension. However,
8234 -- for tagged types with an extension the newly added fields are
8235 -- visible and hence the Derived_Type is always an E_Record_Type.
8236 -- (except that the parent may have its own private fields).
8237 -- For untagged types we preserve the Ekind of the Parent_Base.
8239 if Present (Record_Extension_Part (Type_Def)) then
8240 Set_Ekind (Derived_Type, E_Record_Type);
8241 Set_Default_SSO (Derived_Type);
8243 -- Create internal access types for components with anonymous
8244 -- access types.
8246 if Ada_Version >= Ada_2005 then
8247 Check_Anonymous_Access_Components
8248 (N, Derived_Type, Derived_Type,
8249 Component_List (Record_Extension_Part (Type_Def)));
8250 end if;
8252 else
8253 Set_Ekind (Derived_Type, Ekind (Parent_Base));
8254 end if;
8255 end if;
8257 -- Indic can either be an N_Identifier if the subtype indication
8258 -- contains no constraint or an N_Subtype_Indication if the subtype
8259 -- indication has a constraint.
8261 Indic := Subtype_Indication (Type_Def);
8262 Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
8264 -- Check that the type has visible discriminants. The type may be
8265 -- a private type with unknown discriminants whose full view has
8266 -- discriminants which are invisible.
8268 if Constraint_Present then
8269 if not Has_Discriminants (Parent_Base)
8270 or else
8271 (Has_Unknown_Discriminants (Parent_Base)
8272 and then Is_Private_Type (Parent_Base))
8273 then
8274 Error_Msg_N
8275 ("invalid constraint: type has no discriminant",
8276 Constraint (Indic));
8278 Constraint_Present := False;
8279 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
8281 elsif Is_Constrained (Parent_Type) then
8282 Error_Msg_N
8283 ("invalid constraint: parent type is already constrained",
8284 Constraint (Indic));
8286 Constraint_Present := False;
8287 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
8288 end if;
8289 end if;
8291 -- STEP 0b: If needed, apply transformation given in point 5. above
8293 if not Private_Extension
8294 and then Has_Discriminants (Parent_Type)
8295 and then not Discriminant_Specs
8296 and then (Is_Constrained (Parent_Type) or else Constraint_Present)
8297 then
8298 -- First, we must analyze the constraint (see comment in point 5.)
8299 -- The constraint may come from the subtype indication of the full
8300 -- declaration.
8302 if Constraint_Present then
8303 New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
8305 -- If there is no explicit constraint, there might be one that is
8306 -- inherited from a constrained parent type. In that case verify that
8307 -- it conforms to the constraint in the partial view. In perverse
8308 -- cases the parent subtypes of the partial and full view can have
8309 -- different constraints.
8311 elsif Present (Stored_Constraint (Parent_Type)) then
8312 New_Discrs := Stored_Constraint (Parent_Type);
8314 else
8315 New_Discrs := No_Elist;
8316 end if;
8318 if Has_Discriminants (Derived_Type)
8319 and then Has_Private_Declaration (Derived_Type)
8320 and then Present (Discriminant_Constraint (Derived_Type))
8321 and then Present (New_Discrs)
8322 then
8323 -- Verify that constraints of the full view statically match
8324 -- those given in the partial view.
8326 declare
8327 C1, C2 : Elmt_Id;
8329 begin
8330 C1 := First_Elmt (New_Discrs);
8331 C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
8332 while Present (C1) and then Present (C2) loop
8333 if Fully_Conformant_Expressions (Node (C1), Node (C2))
8334 or else
8335 (Is_OK_Static_Expression (Node (C1))
8336 and then Is_OK_Static_Expression (Node (C2))
8337 and then
8338 Expr_Value (Node (C1)) = Expr_Value (Node (C2)))
8339 then
8340 null;
8342 else
8343 if Constraint_Present then
8344 Error_Msg_N
8345 ("constraint not conformant to previous declaration",
8346 Node (C1));
8347 else
8348 Error_Msg_N
8349 ("constraint of full view is incompatible "
8350 & "with partial view", N);
8351 end if;
8352 end if;
8354 Next_Elmt (C1);
8355 Next_Elmt (C2);
8356 end loop;
8357 end;
8358 end if;
8360 -- Insert and analyze the declaration for the unconstrained base type
8362 New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
8364 New_Decl :=
8365 Make_Full_Type_Declaration (Loc,
8366 Defining_Identifier => New_Base,
8367 Type_Definition =>
8368 Make_Derived_Type_Definition (Loc,
8369 Abstract_Present => Abstract_Present (Type_Def),
8370 Limited_Present => Limited_Present (Type_Def),
8371 Subtype_Indication =>
8372 New_Occurrence_Of (Parent_Base, Loc),
8373 Record_Extension_Part =>
8374 Relocate_Node (Record_Extension_Part (Type_Def)),
8375 Interface_List => Interface_List (Type_Def)));
8377 Set_Parent (New_Decl, Parent (N));
8378 Mark_Rewrite_Insertion (New_Decl);
8379 Insert_Before (N, New_Decl);
8381 -- In the extension case, make sure ancestor is frozen appropriately
8382 -- (see also non-discriminated case below).
8384 if Present (Record_Extension_Part (Type_Def))
8385 or else Is_Interface (Parent_Base)
8386 then
8387 Freeze_Before (New_Decl, Parent_Type);
8388 end if;
8390 -- Note that this call passes False for the Derive_Subps parameter
8391 -- because subprogram derivation is deferred until after creating
8392 -- the subtype (see below).
8394 Build_Derived_Type
8395 (New_Decl, Parent_Base, New_Base,
8396 Is_Completion => False, Derive_Subps => False);
8398 -- ??? This needs re-examination to determine whether the
8399 -- above call can simply be replaced by a call to Analyze.
8401 Set_Analyzed (New_Decl);
8403 -- Insert and analyze the declaration for the constrained subtype
8405 if Constraint_Present then
8406 New_Indic :=
8407 Make_Subtype_Indication (Loc,
8408 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
8409 Constraint => Relocate_Node (Constraint (Indic)));
8411 else
8412 declare
8413 Constr_List : constant List_Id := New_List;
8414 C : Elmt_Id;
8415 Expr : Node_Id;
8417 begin
8418 C := First_Elmt (Discriminant_Constraint (Parent_Type));
8419 while Present (C) loop
8420 Expr := Node (C);
8422 -- It is safe here to call New_Copy_Tree since we called
8423 -- Force_Evaluation on each constraint previously
8424 -- in Build_Discriminant_Constraints.
8426 Append (New_Copy_Tree (Expr), To => Constr_List);
8428 Next_Elmt (C);
8429 end loop;
8431 New_Indic :=
8432 Make_Subtype_Indication (Loc,
8433 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
8434 Constraint =>
8435 Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
8436 end;
8437 end if;
8439 Rewrite (N,
8440 Make_Subtype_Declaration (Loc,
8441 Defining_Identifier => Derived_Type,
8442 Subtype_Indication => New_Indic));
8444 Analyze (N);
8446 -- Derivation of subprograms must be delayed until the full subtype
8447 -- has been established, to ensure proper overriding of subprograms
8448 -- inherited by full types. If the derivations occurred as part of
8449 -- the call to Build_Derived_Type above, then the check for type
8450 -- conformance would fail because earlier primitive subprograms
8451 -- could still refer to the full type prior the change to the new
8452 -- subtype and hence would not match the new base type created here.
8453 -- Subprograms are not derived, however, when Derive_Subps is False
8454 -- (since otherwise there could be redundant derivations).
8456 if Derive_Subps then
8457 Derive_Subprograms (Parent_Type, Derived_Type);
8458 end if;
8460 -- For tagged types the Discriminant_Constraint of the new base itype
8461 -- is inherited from the first subtype so that no subtype conformance
8462 -- problem arise when the first subtype overrides primitive
8463 -- operations inherited by the implicit base type.
8465 if Is_Tagged then
8466 Set_Discriminant_Constraint
8467 (New_Base, Discriminant_Constraint (Derived_Type));
8468 end if;
8470 return;
8471 end if;
8473 -- If we get here Derived_Type will have no discriminants or it will be
8474 -- a discriminated unconstrained base type.
8476 -- STEP 1a: perform preliminary actions/checks for derived tagged types
8478 if Is_Tagged then
8480 -- The parent type is frozen for non-private extensions (RM 13.14(7))
8481 -- The declaration of a specific descendant of an interface type
8482 -- freezes the interface type (RM 13.14).
8484 if not Private_Extension or else Is_Interface (Parent_Base) then
8485 Freeze_Before (N, Parent_Type);
8486 end if;
8488 -- In Ada 2005 (AI-344), the restriction that a derived tagged type
8489 -- cannot be declared at a deeper level than its parent type is
8490 -- removed. The check on derivation within a generic body is also
8491 -- relaxed, but there's a restriction that a derived tagged type
8492 -- cannot be declared in a generic body if it's derived directly
8493 -- or indirectly from a formal type of that generic.
8495 if Ada_Version >= Ada_2005 then
8496 if Present (Enclosing_Generic_Body (Derived_Type)) then
8497 declare
8498 Ancestor_Type : Entity_Id;
8500 begin
8501 -- Check to see if any ancestor of the derived type is a
8502 -- formal type.
8504 Ancestor_Type := Parent_Type;
8505 while not Is_Generic_Type (Ancestor_Type)
8506 and then Etype (Ancestor_Type) /= Ancestor_Type
8507 loop
8508 Ancestor_Type := Etype (Ancestor_Type);
8509 end loop;
8511 -- If the derived type does have a formal type as an
8512 -- ancestor, then it's an error if the derived type is
8513 -- declared within the body of the generic unit that
8514 -- declares the formal type in its generic formal part. It's
8515 -- sufficient to check whether the ancestor type is declared
8516 -- inside the same generic body as the derived type (such as
8517 -- within a nested generic spec), in which case the
8518 -- derivation is legal. If the formal type is declared
8519 -- outside of that generic body, then it's guaranteed that
8520 -- the derived type is declared within the generic body of
8521 -- the generic unit declaring the formal type.
8523 if Is_Generic_Type (Ancestor_Type)
8524 and then Enclosing_Generic_Body (Ancestor_Type) /=
8525 Enclosing_Generic_Body (Derived_Type)
8526 then
8527 Error_Msg_NE
8528 ("parent type of& must not be descendant of formal type"
8529 & " of an enclosing generic body",
8530 Indic, Derived_Type);
8531 end if;
8532 end;
8533 end if;
8535 elsif Type_Access_Level (Derived_Type) /=
8536 Type_Access_Level (Parent_Type)
8537 and then not Is_Generic_Type (Derived_Type)
8538 then
8539 if Is_Controlled (Parent_Type) then
8540 Error_Msg_N
8541 ("controlled type must be declared at the library level",
8542 Indic);
8543 else
8544 Error_Msg_N
8545 ("type extension at deeper accessibility level than parent",
8546 Indic);
8547 end if;
8549 else
8550 declare
8551 GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
8552 begin
8553 if Present (GB)
8554 and then GB /= Enclosing_Generic_Body (Parent_Base)
8555 then
8556 Error_Msg_NE
8557 ("parent type of& must not be outside generic body"
8558 & " (RM 3.9.1(4))",
8559 Indic, Derived_Type);
8560 end if;
8561 end;
8562 end if;
8563 end if;
8565 -- Ada 2005 (AI-251)
8567 if Ada_Version >= Ada_2005 and then Is_Tagged then
8569 -- "The declaration of a specific descendant of an interface type
8570 -- freezes the interface type" (RM 13.14).
8572 declare
8573 Iface : Node_Id;
8574 begin
8575 if Is_Non_Empty_List (Interface_List (Type_Def)) then
8576 Iface := First (Interface_List (Type_Def));
8577 while Present (Iface) loop
8578 Freeze_Before (N, Etype (Iface));
8579 Next (Iface);
8580 end loop;
8581 end if;
8582 end;
8583 end if;
8585 -- STEP 1b : preliminary cleanup of the full view of private types
8587 -- If the type is already marked as having discriminants, then it's the
8588 -- completion of a private type or private extension and we need to
8589 -- retain the discriminants from the partial view if the current
8590 -- declaration has Discriminant_Specifications so that we can verify
8591 -- conformance. However, we must remove any existing components that
8592 -- were inherited from the parent (and attached in Copy_And_Swap)
8593 -- because the full type inherits all appropriate components anyway, and
8594 -- we do not want the partial view's components interfering.
8596 if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
8597 Discrim := First_Discriminant (Derived_Type);
8598 loop
8599 Last_Discrim := Discrim;
8600 Next_Discriminant (Discrim);
8601 exit when No (Discrim);
8602 end loop;
8604 Set_Last_Entity (Derived_Type, Last_Discrim);
8606 -- In all other cases wipe out the list of inherited components (even
8607 -- inherited discriminants), it will be properly rebuilt here.
8609 else
8610 Set_First_Entity (Derived_Type, Empty);
8611 Set_Last_Entity (Derived_Type, Empty);
8612 end if;
8614 -- STEP 1c: Initialize some flags for the Derived_Type
8616 -- The following flags must be initialized here so that
8617 -- Process_Discriminants can check that discriminants of tagged types do
8618 -- not have a default initial value and that access discriminants are
8619 -- only specified for limited records. For completeness, these flags are
8620 -- also initialized along with all the other flags below.
8622 -- AI-419: Limitedness is not inherited from an interface parent, so to
8623 -- be limited in that case the type must be explicitly declared as
8624 -- limited. However, task and protected interfaces are always limited.
8626 if Limited_Present (Type_Def) then
8627 Set_Is_Limited_Record (Derived_Type);
8629 elsif Is_Limited_Record (Parent_Type)
8630 or else (Present (Full_View (Parent_Type))
8631 and then Is_Limited_Record (Full_View (Parent_Type)))
8632 then
8633 if not Is_Interface (Parent_Type)
8634 or else Is_Synchronized_Interface (Parent_Type)
8635 or else Is_Protected_Interface (Parent_Type)
8636 or else Is_Task_Interface (Parent_Type)
8637 then
8638 Set_Is_Limited_Record (Derived_Type);
8639 end if;
8640 end if;
8642 -- STEP 2a: process discriminants of derived type if any
8644 Push_Scope (Derived_Type);
8646 if Discriminant_Specs then
8647 Set_Has_Unknown_Discriminants (Derived_Type, False);
8649 -- The following call initializes fields Has_Discriminants and
8650 -- Discriminant_Constraint, unless we are processing the completion
8651 -- of a private type declaration.
8653 Check_Or_Process_Discriminants (N, Derived_Type);
8655 -- For untagged types, the constraint on the Parent_Type must be
8656 -- present and is used to rename the discriminants.
8658 if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
8659 Error_Msg_N ("untagged parent must have discriminants", Indic);
8661 elsif not Is_Tagged and then not Constraint_Present then
8662 Error_Msg_N
8663 ("discriminant constraint needed for derived untagged records",
8664 Indic);
8666 -- Otherwise the parent subtype must be constrained unless we have a
8667 -- private extension.
8669 elsif not Constraint_Present
8670 and then not Private_Extension
8671 and then not Is_Constrained (Parent_Type)
8672 then
8673 Error_Msg_N
8674 ("unconstrained type not allowed in this context", Indic);
8676 elsif Constraint_Present then
8677 -- The following call sets the field Corresponding_Discriminant
8678 -- for the discriminants in the Derived_Type.
8680 Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
8682 -- For untagged types all new discriminants must rename
8683 -- discriminants in the parent. For private extensions new
8684 -- discriminants cannot rename old ones (implied by [7.3(13)]).
8686 Discrim := First_Discriminant (Derived_Type);
8687 while Present (Discrim) loop
8688 if not Is_Tagged
8689 and then No (Corresponding_Discriminant (Discrim))
8690 then
8691 Error_Msg_N
8692 ("new discriminants must constrain old ones", Discrim);
8694 elsif Private_Extension
8695 and then Present (Corresponding_Discriminant (Discrim))
8696 then
8697 Error_Msg_N
8698 ("only static constraints allowed for parent"
8699 & " discriminants in the partial view", Indic);
8700 exit;
8701 end if;
8703 -- If a new discriminant is used in the constraint, then its
8704 -- subtype must be statically compatible with the parent
8705 -- discriminant's subtype (3.7(15)).
8707 -- However, if the record contains an array constrained by
8708 -- the discriminant but with some different bound, the compiler
8709 -- tries to create a smaller range for the discriminant type.
8710 -- (See exp_ch3.Adjust_Discriminants). In this case, where
8711 -- the discriminant type is a scalar type, the check must use
8712 -- the original discriminant type in the parent declaration.
8714 declare
8715 Corr_Disc : constant Entity_Id :=
8716 Corresponding_Discriminant (Discrim);
8717 Disc_Type : constant Entity_Id := Etype (Discrim);
8718 Corr_Type : Entity_Id;
8720 begin
8721 if Present (Corr_Disc) then
8722 if Is_Scalar_Type (Disc_Type) then
8723 Corr_Type :=
8724 Entity (Discriminant_Type (Parent (Corr_Disc)));
8725 else
8726 Corr_Type := Etype (Corr_Disc);
8727 end if;
8729 if not
8730 Subtypes_Statically_Compatible (Disc_Type, Corr_Type)
8731 then
8732 Error_Msg_N
8733 ("subtype must be compatible "
8734 & "with parent discriminant",
8735 Discrim);
8736 end if;
8737 end if;
8738 end;
8740 Next_Discriminant (Discrim);
8741 end loop;
8743 -- Check whether the constraints of the full view statically
8744 -- match those imposed by the parent subtype [7.3(13)].
8746 if Present (Stored_Constraint (Derived_Type)) then
8747 declare
8748 C1, C2 : Elmt_Id;
8750 begin
8751 C1 := First_Elmt (Discs);
8752 C2 := First_Elmt (Stored_Constraint (Derived_Type));
8753 while Present (C1) and then Present (C2) loop
8754 if not
8755 Fully_Conformant_Expressions (Node (C1), Node (C2))
8756 then
8757 Error_Msg_N
8758 ("not conformant with previous declaration",
8759 Node (C1));
8760 end if;
8762 Next_Elmt (C1);
8763 Next_Elmt (C2);
8764 end loop;
8765 end;
8766 end if;
8767 end if;
8769 -- STEP 2b: No new discriminants, inherit discriminants if any
8771 else
8772 if Private_Extension then
8773 Set_Has_Unknown_Discriminants
8774 (Derived_Type,
8775 Has_Unknown_Discriminants (Parent_Type)
8776 or else Unknown_Discriminants_Present (N));
8778 -- The partial view of the parent may have unknown discriminants,
8779 -- but if the full view has discriminants and the parent type is
8780 -- in scope they must be inherited.
8782 elsif Has_Unknown_Discriminants (Parent_Type)
8783 and then
8784 (not Has_Discriminants (Parent_Type)
8785 or else not In_Open_Scopes (Scope (Parent_Type)))
8786 then
8787 Set_Has_Unknown_Discriminants (Derived_Type);
8788 end if;
8790 if not Has_Unknown_Discriminants (Derived_Type)
8791 and then not Has_Unknown_Discriminants (Parent_Base)
8792 and then Has_Discriminants (Parent_Type)
8793 then
8794 Inherit_Discrims := True;
8795 Set_Has_Discriminants
8796 (Derived_Type, True);
8797 Set_Discriminant_Constraint
8798 (Derived_Type, Discriminant_Constraint (Parent_Base));
8799 end if;
8801 -- The following test is true for private types (remember
8802 -- transformation 5. is not applied to those) and in an error
8803 -- situation.
8805 if Constraint_Present then
8806 Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
8807 end if;
8809 -- For now mark a new derived type as constrained only if it has no
8810 -- discriminants. At the end of Build_Derived_Record_Type we properly
8811 -- set this flag in the case of private extensions. See comments in
8812 -- point 9. just before body of Build_Derived_Record_Type.
8814 Set_Is_Constrained
8815 (Derived_Type,
8816 not (Inherit_Discrims
8817 or else Has_Unknown_Discriminants (Derived_Type)));
8818 end if;
8820 -- STEP 3: initialize fields of derived type
8822 Set_Is_Tagged_Type (Derived_Type, Is_Tagged);
8823 Set_Stored_Constraint (Derived_Type, No_Elist);
8825 -- Ada 2005 (AI-251): Private type-declarations can implement interfaces
8826 -- but cannot be interfaces
8828 if not Private_Extension
8829 and then Ekind (Derived_Type) /= E_Private_Type
8830 and then Ekind (Derived_Type) /= E_Limited_Private_Type
8831 then
8832 if Interface_Present (Type_Def) then
8833 Analyze_Interface_Declaration (Derived_Type, Type_Def);
8834 end if;
8836 Set_Interfaces (Derived_Type, No_Elist);
8837 end if;
8839 -- Fields inherited from the Parent_Type
8841 Set_Has_Specified_Layout
8842 (Derived_Type, Has_Specified_Layout (Parent_Type));
8843 Set_Is_Limited_Composite
8844 (Derived_Type, Is_Limited_Composite (Parent_Type));
8845 Set_Is_Private_Composite
8846 (Derived_Type, Is_Private_Composite (Parent_Type));
8848 if Is_Tagged_Type (Parent_Type) then
8849 Set_No_Tagged_Streams_Pragma
8850 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
8851 end if;
8853 -- Fields inherited from the Parent_Base
8855 Set_Has_Controlled_Component
8856 (Derived_Type, Has_Controlled_Component (Parent_Base));
8857 Set_Has_Non_Standard_Rep
8858 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
8859 Set_Has_Primitive_Operations
8860 (Derived_Type, Has_Primitive_Operations (Parent_Base));
8862 -- Fields inherited from the Parent_Base in the non-private case
8864 if Ekind (Derived_Type) = E_Record_Type then
8865 Set_Has_Complex_Representation
8866 (Derived_Type, Has_Complex_Representation (Parent_Base));
8867 end if;
8869 -- Fields inherited from the Parent_Base for record types
8871 if Is_Record_Type (Derived_Type) then
8872 declare
8873 Parent_Full : Entity_Id;
8875 begin
8876 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
8877 -- Parent_Base can be a private type or private extension. Go
8878 -- to the full view here to get the E_Record_Type specific flags.
8880 if Present (Full_View (Parent_Base)) then
8881 Parent_Full := Full_View (Parent_Base);
8882 else
8883 Parent_Full := Parent_Base;
8884 end if;
8886 Set_OK_To_Reorder_Components
8887 (Derived_Type, OK_To_Reorder_Components (Parent_Full));
8888 end;
8889 end if;
8891 -- Set fields for private derived types
8893 if Is_Private_Type (Derived_Type) then
8894 Set_Depends_On_Private (Derived_Type, True);
8895 Set_Private_Dependents (Derived_Type, New_Elmt_List);
8897 -- Inherit fields from non private record types. If this is the
8898 -- completion of a derivation from a private type, the parent itself
8899 -- is private, and the attributes come from its full view, which must
8900 -- be present.
8902 else
8903 if Is_Private_Type (Parent_Base)
8904 and then not Is_Record_Type (Parent_Base)
8905 then
8906 Set_Component_Alignment
8907 (Derived_Type, Component_Alignment (Full_View (Parent_Base)));
8908 Set_C_Pass_By_Copy
8909 (Derived_Type, C_Pass_By_Copy (Full_View (Parent_Base)));
8910 else
8911 Set_Component_Alignment
8912 (Derived_Type, Component_Alignment (Parent_Base));
8913 Set_C_Pass_By_Copy
8914 (Derived_Type, C_Pass_By_Copy (Parent_Base));
8915 end if;
8916 end if;
8918 -- Set fields for tagged types
8920 if Is_Tagged then
8921 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
8923 -- All tagged types defined in Ada.Finalization are controlled
8925 if Chars (Scope (Derived_Type)) = Name_Finalization
8926 and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
8927 and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
8928 then
8929 Set_Is_Controlled (Derived_Type);
8930 else
8931 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Base));
8932 end if;
8934 -- Minor optimization: there is no need to generate the class-wide
8935 -- entity associated with an underlying record view.
8937 if not Is_Underlying_Record_View (Derived_Type) then
8938 Make_Class_Wide_Type (Derived_Type);
8939 end if;
8941 Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
8943 if Has_Discriminants (Derived_Type)
8944 and then Constraint_Present
8945 then
8946 Set_Stored_Constraint
8947 (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
8948 end if;
8950 if Ada_Version >= Ada_2005 then
8951 declare
8952 Ifaces_List : Elist_Id;
8954 begin
8955 -- Checks rules 3.9.4 (13/2 and 14/2)
8957 if Comes_From_Source (Derived_Type)
8958 and then not Is_Private_Type (Derived_Type)
8959 and then Is_Interface (Parent_Type)
8960 and then not Is_Interface (Derived_Type)
8961 then
8962 if Is_Task_Interface (Parent_Type) then
8963 Error_Msg_N
8964 ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
8965 Derived_Type);
8967 elsif Is_Protected_Interface (Parent_Type) then
8968 Error_Msg_N
8969 ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
8970 Derived_Type);
8971 end if;
8972 end if;
8974 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
8976 Check_Interfaces (N, Type_Def);
8978 -- Ada 2005 (AI-251): Collect the list of progenitors that are
8979 -- not already in the parents.
8981 Collect_Interfaces
8982 (T => Derived_Type,
8983 Ifaces_List => Ifaces_List,
8984 Exclude_Parents => True);
8986 Set_Interfaces (Derived_Type, Ifaces_List);
8988 -- If the derived type is the anonymous type created for
8989 -- a declaration whose parent has a constraint, propagate
8990 -- the interface list to the source type. This must be done
8991 -- prior to the completion of the analysis of the source type
8992 -- because the components in the extension may contain current
8993 -- instances whose legality depends on some ancestor.
8995 if Is_Itype (Derived_Type) then
8996 declare
8997 Def : constant Node_Id :=
8998 Associated_Node_For_Itype (Derived_Type);
8999 begin
9000 if Present (Def)
9001 and then Nkind (Def) = N_Full_Type_Declaration
9002 then
9003 Set_Interfaces
9004 (Defining_Identifier (Def), Ifaces_List);
9005 end if;
9006 end;
9007 end if;
9009 -- A type extension is automatically Ghost when one of its
9010 -- progenitors is Ghost (SPARK RM 6.9(9)). This property is
9011 -- also inherited when the parent type is Ghost, but this is
9012 -- done in Build_Derived_Type as the mechanism also handles
9013 -- untagged derivations.
9015 if Implements_Ghost_Interface (Derived_Type) then
9016 Set_Is_Ghost_Entity (Derived_Type);
9017 end if;
9018 end;
9019 end if;
9021 else
9022 Set_Is_Packed (Derived_Type, Is_Packed (Parent_Base));
9023 Set_Has_Non_Standard_Rep
9024 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
9025 end if;
9027 -- STEP 4: Inherit components from the parent base and constrain them.
9028 -- Apply the second transformation described in point 6. above.
9030 if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
9031 or else not Has_Discriminants (Parent_Type)
9032 or else not Is_Constrained (Parent_Type)
9033 then
9034 Constrs := Discs;
9035 else
9036 Constrs := Discriminant_Constraint (Parent_Type);
9037 end if;
9039 Assoc_List :=
9040 Inherit_Components
9041 (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
9043 -- STEP 5a: Copy the parent record declaration for untagged types
9045 Set_Has_Implicit_Dereference
9046 (Derived_Type, Has_Implicit_Dereference (Parent_Type));
9048 if not Is_Tagged then
9050 -- Discriminant_Constraint (Derived_Type) has been properly
9051 -- constructed. Save it and temporarily set it to Empty because we
9052 -- do not want the call to New_Copy_Tree below to mess this list.
9054 if Has_Discriminants (Derived_Type) then
9055 Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
9056 Set_Discriminant_Constraint (Derived_Type, No_Elist);
9057 else
9058 Save_Discr_Constr := No_Elist;
9059 end if;
9061 -- Save the Etype field of Derived_Type. It is correctly set now,
9062 -- but the call to New_Copy tree may remap it to point to itself,
9063 -- which is not what we want. Ditto for the Next_Entity field.
9065 Save_Etype := Etype (Derived_Type);
9066 Save_Next_Entity := Next_Entity (Derived_Type);
9068 -- Assoc_List maps all stored discriminants in the Parent_Base to
9069 -- stored discriminants in the Derived_Type. It is fundamental that
9070 -- no types or itypes with discriminants other than the stored
9071 -- discriminants appear in the entities declared inside
9072 -- Derived_Type, since the back end cannot deal with it.
9074 New_Decl :=
9075 New_Copy_Tree
9076 (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
9078 -- Restore the fields saved prior to the New_Copy_Tree call
9079 -- and compute the stored constraint.
9081 Set_Etype (Derived_Type, Save_Etype);
9082 Set_Next_Entity (Derived_Type, Save_Next_Entity);
9084 if Has_Discriminants (Derived_Type) then
9085 Set_Discriminant_Constraint
9086 (Derived_Type, Save_Discr_Constr);
9087 Set_Stored_Constraint
9088 (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
9089 Replace_Components (Derived_Type, New_Decl);
9090 end if;
9092 -- Insert the new derived type declaration
9094 Rewrite (N, New_Decl);
9096 -- STEP 5b: Complete the processing for record extensions in generics
9098 -- There is no completion for record extensions declared in the
9099 -- parameter part of a generic, so we need to complete processing for
9100 -- these generic record extensions here. The Record_Type_Definition call
9101 -- will change the Ekind of the components from E_Void to E_Component.
9103 elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
9104 Record_Type_Definition (Empty, Derived_Type);
9106 -- STEP 5c: Process the record extension for non private tagged types
9108 elsif not Private_Extension then
9109 Expand_Record_Extension (Derived_Type, Type_Def);
9111 -- Note : previously in ASIS mode we set the Parent_Subtype of the
9112 -- derived type to propagate some semantic information. This led
9113 -- to other ASIS failures and has been removed.
9115 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
9116 -- implemented interfaces if we are in expansion mode
9118 if Expander_Active
9119 and then Has_Interfaces (Derived_Type)
9120 then
9121 Add_Interface_Tag_Components (N, Derived_Type);
9122 end if;
9124 -- Analyze the record extension
9126 Record_Type_Definition
9127 (Record_Extension_Part (Type_Def), Derived_Type);
9128 end if;
9130 End_Scope;
9132 -- Nothing else to do if there is an error in the derivation.
9133 -- An unusual case: the full view may be derived from a type in an
9134 -- instance, when the partial view was used illegally as an actual
9135 -- in that instance, leading to a circular definition.
9137 if Etype (Derived_Type) = Any_Type
9138 or else Etype (Parent_Type) = Derived_Type
9139 then
9140 return;
9141 end if;
9143 -- Set delayed freeze and then derive subprograms, we need to do
9144 -- this in this order so that derived subprograms inherit the
9145 -- derived freeze if necessary.
9147 Set_Has_Delayed_Freeze (Derived_Type);
9149 if Derive_Subps then
9150 Derive_Subprograms (Parent_Type, Derived_Type);
9151 end if;
9153 -- If we have a private extension which defines a constrained derived
9154 -- type mark as constrained here after we have derived subprograms. See
9155 -- comment on point 9. just above the body of Build_Derived_Record_Type.
9157 if Private_Extension and then Inherit_Discrims then
9158 if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
9159 Set_Is_Constrained (Derived_Type, True);
9160 Set_Discriminant_Constraint (Derived_Type, Discs);
9162 elsif Is_Constrained (Parent_Type) then
9163 Set_Is_Constrained
9164 (Derived_Type, True);
9165 Set_Discriminant_Constraint
9166 (Derived_Type, Discriminant_Constraint (Parent_Type));
9167 end if;
9168 end if;
9170 -- Update the class-wide type, which shares the now-completed entity
9171 -- list with its specific type. In case of underlying record views,
9172 -- we do not generate the corresponding class wide entity.
9174 if Is_Tagged
9175 and then not Is_Underlying_Record_View (Derived_Type)
9176 then
9177 Set_First_Entity
9178 (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
9179 Set_Last_Entity
9180 (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
9181 end if;
9183 Check_Function_Writable_Actuals (N);
9184 end Build_Derived_Record_Type;
9186 ------------------------
9187 -- Build_Derived_Type --
9188 ------------------------
9190 procedure Build_Derived_Type
9191 (N : Node_Id;
9192 Parent_Type : Entity_Id;
9193 Derived_Type : Entity_Id;
9194 Is_Completion : Boolean;
9195 Derive_Subps : Boolean := True)
9197 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
9199 begin
9200 -- Set common attributes
9202 Set_Scope (Derived_Type, Current_Scope);
9204 Set_Etype (Derived_Type, Parent_Base);
9205 Set_Ekind (Derived_Type, Ekind (Parent_Base));
9206 Propagate_Concurrent_Flags (Derived_Type, Parent_Base);
9208 Set_Size_Info (Derived_Type, Parent_Type);
9209 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
9210 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
9211 Set_Disable_Controlled (Derived_Type, Disable_Controlled (Parent_Type));
9213 Set_Is_Tagged_Type (Derived_Type, Is_Tagged_Type (Parent_Type));
9214 Set_Is_Volatile (Derived_Type, Is_Volatile (Parent_Type));
9216 if Is_Tagged_Type (Derived_Type) then
9217 Set_No_Tagged_Streams_Pragma
9218 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
9219 end if;
9221 -- If the parent has primitive routines, set the derived type link
9223 if Has_Primitive_Operations (Parent_Type) then
9224 Set_Derived_Type_Link (Parent_Base, Derived_Type);
9225 end if;
9227 -- If the parent type is a private subtype, the convention on the base
9228 -- type may be set in the private part, and not propagated to the
9229 -- subtype until later, so we obtain the convention from the base type.
9231 Set_Convention (Derived_Type, Convention (Parent_Base));
9233 -- Set SSO default for record or array type
9235 if (Is_Array_Type (Derived_Type) or else Is_Record_Type (Derived_Type))
9236 and then Is_Base_Type (Derived_Type)
9237 then
9238 Set_Default_SSO (Derived_Type);
9239 end if;
9241 -- A derived type inherits the Default_Initial_Condition pragma coming
9242 -- from any parent type within the derivation chain.
9244 if Has_DIC (Parent_Type) then
9245 Set_Has_Inherited_DIC (Derived_Type);
9246 end if;
9248 -- A derived type inherits any class-wide invariants coming from a
9249 -- parent type or an interface. Note that the invariant procedure of
9250 -- the parent type should not be inherited because the derived type may
9251 -- define invariants of its own.
9253 if not Is_Interface (Derived_Type) then
9254 if Has_Inherited_Invariants (Parent_Type)
9255 or else Has_Inheritable_Invariants (Parent_Type)
9256 then
9257 Set_Has_Inherited_Invariants (Derived_Type);
9259 elsif Is_Concurrent_Type (Derived_Type)
9260 or else Is_Tagged_Type (Derived_Type)
9261 then
9262 declare
9263 Iface : Entity_Id;
9264 Ifaces : Elist_Id;
9265 Iface_Elmt : Elmt_Id;
9267 begin
9268 Collect_Interfaces
9269 (T => Derived_Type,
9270 Ifaces_List => Ifaces,
9271 Exclude_Parents => True);
9273 if Present (Ifaces) then
9274 Iface_Elmt := First_Elmt (Ifaces);
9275 while Present (Iface_Elmt) loop
9276 Iface := Node (Iface_Elmt);
9278 if Has_Inheritable_Invariants (Iface) then
9279 Set_Has_Inherited_Invariants (Derived_Type);
9280 exit;
9281 end if;
9283 Next_Elmt (Iface_Elmt);
9284 end loop;
9285 end if;
9286 end;
9287 end if;
9288 end if;
9290 -- We similarly inherit predicates. Note that for scalar derived types
9291 -- the predicate is inherited from the first subtype, and not from its
9292 -- (anonymous) base type.
9294 if Has_Predicates (Parent_Type)
9295 or else Has_Predicates (First_Subtype (Parent_Type))
9296 then
9297 Set_Has_Predicates (Derived_Type);
9298 end if;
9300 -- The derived type inherits the representation clauses of the parent
9302 Inherit_Rep_Item_Chain (Derived_Type, Parent_Type);
9304 -- If the parent type has delayed rep aspects, then mark the derived
9305 -- type as possibly inheriting a delayed rep aspect.
9307 if Has_Delayed_Rep_Aspects (Parent_Type) then
9308 Set_May_Inherit_Delayed_Rep_Aspects (Derived_Type);
9309 end if;
9311 -- A derived type becomes Ghost when its parent type is also Ghost
9312 -- (SPARK RM 6.9(9)). Note that the Ghost-related attributes are not
9313 -- directly inherited because the Ghost policy in effect may differ.
9315 if Is_Ghost_Entity (Parent_Type) then
9316 Set_Is_Ghost_Entity (Derived_Type);
9317 end if;
9319 -- Type dependent processing
9321 case Ekind (Parent_Type) is
9322 when Numeric_Kind =>
9323 Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
9325 when Array_Kind =>
9326 Build_Derived_Array_Type (N, Parent_Type, Derived_Type);
9328 when Class_Wide_Kind
9329 | E_Record_Subtype
9330 | E_Record_Type
9332 Build_Derived_Record_Type
9333 (N, Parent_Type, Derived_Type, Derive_Subps);
9334 return;
9336 when Enumeration_Kind =>
9337 Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
9339 when Access_Kind =>
9340 Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
9342 when Incomplete_Or_Private_Kind =>
9343 Build_Derived_Private_Type
9344 (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
9346 -- For discriminated types, the derivation includes deriving
9347 -- primitive operations. For others it is done below.
9349 if Is_Tagged_Type (Parent_Type)
9350 or else Has_Discriminants (Parent_Type)
9351 or else (Present (Full_View (Parent_Type))
9352 and then Has_Discriminants (Full_View (Parent_Type)))
9353 then
9354 return;
9355 end if;
9357 when Concurrent_Kind =>
9358 Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
9360 when others =>
9361 raise Program_Error;
9362 end case;
9364 -- Nothing more to do if some error occurred
9366 if Etype (Derived_Type) = Any_Type then
9367 return;
9368 end if;
9370 -- Set delayed freeze and then derive subprograms, we need to do this
9371 -- in this order so that derived subprograms inherit the derived freeze
9372 -- if necessary.
9374 Set_Has_Delayed_Freeze (Derived_Type);
9376 if Derive_Subps then
9377 Derive_Subprograms (Parent_Type, Derived_Type);
9378 end if;
9380 Set_Has_Primitive_Operations
9381 (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
9382 end Build_Derived_Type;
9384 -----------------------
9385 -- Build_Discriminal --
9386 -----------------------
9388 procedure Build_Discriminal (Discrim : Entity_Id) is
9389 D_Minal : Entity_Id;
9390 CR_Disc : Entity_Id;
9392 begin
9393 -- A discriminal has the same name as the discriminant
9395 D_Minal := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9397 Set_Ekind (D_Minal, E_In_Parameter);
9398 Set_Mechanism (D_Minal, Default_Mechanism);
9399 Set_Etype (D_Minal, Etype (Discrim));
9400 Set_Scope (D_Minal, Current_Scope);
9401 Set_Parent (D_Minal, Parent (Discrim));
9403 Set_Discriminal (Discrim, D_Minal);
9404 Set_Discriminal_Link (D_Minal, Discrim);
9406 -- For task types, build at once the discriminants of the corresponding
9407 -- record, which are needed if discriminants are used in entry defaults
9408 -- and in family bounds.
9410 if Is_Concurrent_Type (Current_Scope)
9411 or else
9412 Is_Limited_Type (Current_Scope)
9413 then
9414 CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9416 Set_Ekind (CR_Disc, E_In_Parameter);
9417 Set_Mechanism (CR_Disc, Default_Mechanism);
9418 Set_Etype (CR_Disc, Etype (Discrim));
9419 Set_Scope (CR_Disc, Current_Scope);
9420 Set_Discriminal_Link (CR_Disc, Discrim);
9421 Set_CR_Discriminant (Discrim, CR_Disc);
9422 end if;
9423 end Build_Discriminal;
9425 ------------------------------------
9426 -- Build_Discriminant_Constraints --
9427 ------------------------------------
9429 function Build_Discriminant_Constraints
9430 (T : Entity_Id;
9431 Def : Node_Id;
9432 Derived_Def : Boolean := False) return Elist_Id
9434 C : constant Node_Id := Constraint (Def);
9435 Nb_Discr : constant Nat := Number_Discriminants (T);
9437 Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
9438 -- Saves the expression corresponding to a given discriminant in T
9440 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
9441 -- Return the Position number within array Discr_Expr of a discriminant
9442 -- D within the discriminant list of the discriminated type T.
9444 procedure Process_Discriminant_Expression
9445 (Expr : Node_Id;
9446 D : Entity_Id);
9447 -- If this is a discriminant constraint on a partial view, do not
9448 -- generate an overflow check on the discriminant expression. The check
9449 -- will be generated when constraining the full view. Otherwise the
9450 -- backend creates duplicate symbols for the temporaries corresponding
9451 -- to the expressions to be checked, causing spurious assembler errors.
9453 ------------------
9454 -- Pos_Of_Discr --
9455 ------------------
9457 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
9458 Disc : Entity_Id;
9460 begin
9461 Disc := First_Discriminant (T);
9462 for J in Discr_Expr'Range loop
9463 if Disc = D then
9464 return J;
9465 end if;
9467 Next_Discriminant (Disc);
9468 end loop;
9470 -- Note: Since this function is called on discriminants that are
9471 -- known to belong to the discriminated type, falling through the
9472 -- loop with no match signals an internal compiler error.
9474 raise Program_Error;
9475 end Pos_Of_Discr;
9477 -------------------------------------
9478 -- Process_Discriminant_Expression --
9479 -------------------------------------
9481 procedure Process_Discriminant_Expression
9482 (Expr : Node_Id;
9483 D : Entity_Id)
9485 BDT : constant Entity_Id := Base_Type (Etype (D));
9487 begin
9488 -- If this is a discriminant constraint on a partial view, do
9489 -- not generate an overflow on the discriminant expression. The
9490 -- check will be generated when constraining the full view.
9492 if Is_Private_Type (T)
9493 and then Present (Full_View (T))
9494 then
9495 Analyze_And_Resolve (Expr, BDT, Suppress => Overflow_Check);
9496 else
9497 Analyze_And_Resolve (Expr, BDT);
9498 end if;
9499 end Process_Discriminant_Expression;
9501 -- Declarations local to Build_Discriminant_Constraints
9503 Discr : Entity_Id;
9504 E : Entity_Id;
9505 Elist : constant Elist_Id := New_Elmt_List;
9507 Constr : Node_Id;
9508 Expr : Node_Id;
9509 Id : Node_Id;
9510 Position : Nat;
9511 Found : Boolean;
9513 Discrim_Present : Boolean := False;
9515 -- Start of processing for Build_Discriminant_Constraints
9517 begin
9518 -- The following loop will process positional associations only.
9519 -- For a positional association, the (single) discriminant is
9520 -- implicitly specified by position, in textual order (RM 3.7.2).
9522 Discr := First_Discriminant (T);
9523 Constr := First (Constraints (C));
9524 for D in Discr_Expr'Range loop
9525 exit when Nkind (Constr) = N_Discriminant_Association;
9527 if No (Constr) then
9528 Error_Msg_N ("too few discriminants given in constraint", C);
9529 return New_Elmt_List;
9531 elsif Nkind (Constr) = N_Range
9532 or else (Nkind (Constr) = N_Attribute_Reference
9533 and then Attribute_Name (Constr) = Name_Range)
9534 then
9535 Error_Msg_N
9536 ("a range is not a valid discriminant constraint", Constr);
9537 Discr_Expr (D) := Error;
9539 else
9540 Process_Discriminant_Expression (Constr, Discr);
9541 Discr_Expr (D) := Constr;
9542 end if;
9544 Next_Discriminant (Discr);
9545 Next (Constr);
9546 end loop;
9548 if No (Discr) and then Present (Constr) then
9549 Error_Msg_N ("too many discriminants given in constraint", Constr);
9550 return New_Elmt_List;
9551 end if;
9553 -- Named associations can be given in any order, but if both positional
9554 -- and named associations are used in the same discriminant constraint,
9555 -- then positional associations must occur first, at their normal
9556 -- position. Hence once a named association is used, the rest of the
9557 -- discriminant constraint must use only named associations.
9559 while Present (Constr) loop
9561 -- Positional association forbidden after a named association
9563 if Nkind (Constr) /= N_Discriminant_Association then
9564 Error_Msg_N ("positional association follows named one", Constr);
9565 return New_Elmt_List;
9567 -- Otherwise it is a named association
9569 else
9570 -- E records the type of the discriminants in the named
9571 -- association. All the discriminants specified in the same name
9572 -- association must have the same type.
9574 E := Empty;
9576 -- Search the list of discriminants in T to see if the simple name
9577 -- given in the constraint matches any of them.
9579 Id := First (Selector_Names (Constr));
9580 while Present (Id) loop
9581 Found := False;
9583 -- If Original_Discriminant is present, we are processing a
9584 -- generic instantiation and this is an instance node. We need
9585 -- to find the name of the corresponding discriminant in the
9586 -- actual record type T and not the name of the discriminant in
9587 -- the generic formal. Example:
9589 -- generic
9590 -- type G (D : int) is private;
9591 -- package P is
9592 -- subtype W is G (D => 1);
9593 -- end package;
9594 -- type Rec (X : int) is record ... end record;
9595 -- package Q is new P (G => Rec);
9597 -- At the point of the instantiation, formal type G is Rec
9598 -- and therefore when reanalyzing "subtype W is G (D => 1);"
9599 -- which really looks like "subtype W is Rec (D => 1);" at
9600 -- the point of instantiation, we want to find the discriminant
9601 -- that corresponds to D in Rec, i.e. X.
9603 if Present (Original_Discriminant (Id))
9604 and then In_Instance
9605 then
9606 Discr := Find_Corresponding_Discriminant (Id, T);
9607 Found := True;
9609 else
9610 Discr := First_Discriminant (T);
9611 while Present (Discr) loop
9612 if Chars (Discr) = Chars (Id) then
9613 Found := True;
9614 exit;
9615 end if;
9617 Next_Discriminant (Discr);
9618 end loop;
9620 if not Found then
9621 Error_Msg_N ("& does not match any discriminant", Id);
9622 return New_Elmt_List;
9624 -- If the parent type is a generic formal, preserve the
9625 -- name of the discriminant for subsequent instances.
9626 -- see comment at the beginning of this if statement.
9628 elsif Is_Generic_Type (Root_Type (T)) then
9629 Set_Original_Discriminant (Id, Discr);
9630 end if;
9631 end if;
9633 Position := Pos_Of_Discr (T, Discr);
9635 if Present (Discr_Expr (Position)) then
9636 Error_Msg_N ("duplicate constraint for discriminant&", Id);
9638 else
9639 -- Each discriminant specified in the same named association
9640 -- must be associated with a separate copy of the
9641 -- corresponding expression.
9643 if Present (Next (Id)) then
9644 Expr := New_Copy_Tree (Expression (Constr));
9645 Set_Parent (Expr, Parent (Expression (Constr)));
9646 else
9647 Expr := Expression (Constr);
9648 end if;
9650 Discr_Expr (Position) := Expr;
9651 Process_Discriminant_Expression (Expr, Discr);
9652 end if;
9654 -- A discriminant association with more than one discriminant
9655 -- name is only allowed if the named discriminants are all of
9656 -- the same type (RM 3.7.1(8)).
9658 if E = Empty then
9659 E := Base_Type (Etype (Discr));
9661 elsif Base_Type (Etype (Discr)) /= E then
9662 Error_Msg_N
9663 ("all discriminants in an association " &
9664 "must have the same type", Id);
9665 end if;
9667 Next (Id);
9668 end loop;
9669 end if;
9671 Next (Constr);
9672 end loop;
9674 -- A discriminant constraint must provide exactly one value for each
9675 -- discriminant of the type (RM 3.7.1(8)).
9677 for J in Discr_Expr'Range loop
9678 if No (Discr_Expr (J)) then
9679 Error_Msg_N ("too few discriminants given in constraint", C);
9680 return New_Elmt_List;
9681 end if;
9682 end loop;
9684 -- Determine if there are discriminant expressions in the constraint
9686 for J in Discr_Expr'Range loop
9687 if Denotes_Discriminant
9688 (Discr_Expr (J), Check_Concurrent => True)
9689 then
9690 Discrim_Present := True;
9691 end if;
9692 end loop;
9694 -- Build an element list consisting of the expressions given in the
9695 -- discriminant constraint and apply the appropriate checks. The list
9696 -- is constructed after resolving any named discriminant associations
9697 -- and therefore the expressions appear in the textual order of the
9698 -- discriminants.
9700 Discr := First_Discriminant (T);
9701 for J in Discr_Expr'Range loop
9702 if Discr_Expr (J) /= Error then
9703 Append_Elmt (Discr_Expr (J), Elist);
9705 -- If any of the discriminant constraints is given by a
9706 -- discriminant and we are in a derived type declaration we
9707 -- have a discriminant renaming. Establish link between new
9708 -- and old discriminant. The new discriminant has an implicit
9709 -- dereference if the old one does.
9711 if Denotes_Discriminant (Discr_Expr (J)) then
9712 if Derived_Def then
9713 declare
9714 New_Discr : constant Entity_Id := Entity (Discr_Expr (J));
9716 begin
9717 Set_Corresponding_Discriminant (New_Discr, Discr);
9718 Set_Has_Implicit_Dereference (New_Discr,
9719 Has_Implicit_Dereference (Discr));
9720 end;
9721 end if;
9723 -- Force the evaluation of non-discriminant expressions.
9724 -- If we have found a discriminant in the constraint 3.4(26)
9725 -- and 3.8(18) demand that no range checks are performed are
9726 -- after evaluation. If the constraint is for a component
9727 -- definition that has a per-object constraint, expressions are
9728 -- evaluated but not checked either. In all other cases perform
9729 -- a range check.
9731 else
9732 if Discrim_Present then
9733 null;
9735 elsif Nkind (Parent (Parent (Def))) = N_Component_Declaration
9736 and then Has_Per_Object_Constraint
9737 (Defining_Identifier (Parent (Parent (Def))))
9738 then
9739 null;
9741 elsif Is_Access_Type (Etype (Discr)) then
9742 Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
9744 else
9745 Apply_Range_Check (Discr_Expr (J), Etype (Discr));
9746 end if;
9748 Force_Evaluation (Discr_Expr (J));
9749 end if;
9751 -- Check that the designated type of an access discriminant's
9752 -- expression is not a class-wide type unless the discriminant's
9753 -- designated type is also class-wide.
9755 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
9756 and then not Is_Class_Wide_Type
9757 (Designated_Type (Etype (Discr)))
9758 and then Etype (Discr_Expr (J)) /= Any_Type
9759 and then Is_Class_Wide_Type
9760 (Designated_Type (Etype (Discr_Expr (J))))
9761 then
9762 Wrong_Type (Discr_Expr (J), Etype (Discr));
9764 elsif Is_Access_Type (Etype (Discr))
9765 and then not Is_Access_Constant (Etype (Discr))
9766 and then Is_Access_Type (Etype (Discr_Expr (J)))
9767 and then Is_Access_Constant (Etype (Discr_Expr (J)))
9768 then
9769 Error_Msg_NE
9770 ("constraint for discriminant& must be access to variable",
9771 Def, Discr);
9772 end if;
9773 end if;
9775 Next_Discriminant (Discr);
9776 end loop;
9778 return Elist;
9779 end Build_Discriminant_Constraints;
9781 ---------------------------------
9782 -- Build_Discriminated_Subtype --
9783 ---------------------------------
9785 procedure Build_Discriminated_Subtype
9786 (T : Entity_Id;
9787 Def_Id : Entity_Id;
9788 Elist : Elist_Id;
9789 Related_Nod : Node_Id;
9790 For_Access : Boolean := False)
9792 Has_Discrs : constant Boolean := Has_Discriminants (T);
9793 Constrained : constant Boolean :=
9794 (Has_Discrs
9795 and then not Is_Empty_Elmt_List (Elist)
9796 and then not Is_Class_Wide_Type (T))
9797 or else Is_Constrained (T);
9799 begin
9800 if Ekind (T) = E_Record_Type then
9801 if For_Access then
9802 Set_Ekind (Def_Id, E_Private_Subtype);
9803 Set_Is_For_Access_Subtype (Def_Id, True);
9804 else
9805 Set_Ekind (Def_Id, E_Record_Subtype);
9806 end if;
9808 -- Inherit preelaboration flag from base, for types for which it
9809 -- may have been set: records, private types, protected types.
9811 Set_Known_To_Have_Preelab_Init
9812 (Def_Id, Known_To_Have_Preelab_Init (T));
9814 elsif Ekind (T) = E_Task_Type then
9815 Set_Ekind (Def_Id, E_Task_Subtype);
9817 elsif Ekind (T) = E_Protected_Type then
9818 Set_Ekind (Def_Id, E_Protected_Subtype);
9819 Set_Known_To_Have_Preelab_Init
9820 (Def_Id, Known_To_Have_Preelab_Init (T));
9822 elsif Is_Private_Type (T) then
9823 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
9824 Set_Known_To_Have_Preelab_Init
9825 (Def_Id, Known_To_Have_Preelab_Init (T));
9827 -- Private subtypes may have private dependents
9829 Set_Private_Dependents (Def_Id, New_Elmt_List);
9831 elsif Is_Class_Wide_Type (T) then
9832 Set_Ekind (Def_Id, E_Class_Wide_Subtype);
9834 else
9835 -- Incomplete type. Attach subtype to list of dependents, to be
9836 -- completed with full view of parent type, unless is it the
9837 -- designated subtype of a record component within an init_proc.
9838 -- This last case arises for a component of an access type whose
9839 -- designated type is incomplete (e.g. a Taft Amendment type).
9840 -- The designated subtype is within an inner scope, and needs no
9841 -- elaboration, because only the access type is needed in the
9842 -- initialization procedure.
9844 Set_Ekind (Def_Id, Ekind (T));
9846 if For_Access and then Within_Init_Proc then
9847 null;
9848 else
9849 Append_Elmt (Def_Id, Private_Dependents (T));
9850 end if;
9851 end if;
9853 Set_Etype (Def_Id, T);
9854 Init_Size_Align (Def_Id);
9855 Set_Has_Discriminants (Def_Id, Has_Discrs);
9856 Set_Is_Constrained (Def_Id, Constrained);
9858 Set_First_Entity (Def_Id, First_Entity (T));
9859 Set_Last_Entity (Def_Id, Last_Entity (T));
9860 Set_Has_Implicit_Dereference
9861 (Def_Id, Has_Implicit_Dereference (T));
9863 -- If the subtype is the completion of a private declaration, there may
9864 -- have been representation clauses for the partial view, and they must
9865 -- be preserved. Build_Derived_Type chains the inherited clauses with
9866 -- the ones appearing on the extension. If this comes from a subtype
9867 -- declaration, all clauses are inherited.
9869 if No (First_Rep_Item (Def_Id)) then
9870 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
9871 end if;
9873 if Is_Tagged_Type (T) then
9874 Set_Is_Tagged_Type (Def_Id);
9875 Set_No_Tagged_Streams_Pragma (Def_Id, No_Tagged_Streams_Pragma (T));
9876 Make_Class_Wide_Type (Def_Id);
9877 end if;
9879 Set_Stored_Constraint (Def_Id, No_Elist);
9881 if Has_Discrs then
9882 Set_Discriminant_Constraint (Def_Id, Elist);
9883 Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
9884 end if;
9886 if Is_Tagged_Type (T) then
9888 -- Ada 2005 (AI-251): In case of concurrent types we inherit the
9889 -- concurrent record type (which has the list of primitive
9890 -- operations).
9892 if Ada_Version >= Ada_2005
9893 and then Is_Concurrent_Type (T)
9894 then
9895 Set_Corresponding_Record_Type (Def_Id,
9896 Corresponding_Record_Type (T));
9897 else
9898 Set_Direct_Primitive_Operations (Def_Id,
9899 Direct_Primitive_Operations (T));
9900 end if;
9902 Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
9903 end if;
9905 -- Subtypes introduced by component declarations do not need to be
9906 -- marked as delayed, and do not get freeze nodes, because the semantics
9907 -- verifies that the parents of the subtypes are frozen before the
9908 -- enclosing record is frozen.
9910 if not Is_Type (Scope (Def_Id)) then
9911 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
9913 if Is_Private_Type (T)
9914 and then Present (Full_View (T))
9915 then
9916 Conditional_Delay (Def_Id, Full_View (T));
9917 else
9918 Conditional_Delay (Def_Id, T);
9919 end if;
9920 end if;
9922 if Is_Record_Type (T) then
9923 Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
9925 if Has_Discrs
9926 and then not Is_Empty_Elmt_List (Elist)
9927 and then not For_Access
9928 then
9929 Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
9930 elsif not For_Access then
9931 Set_Cloned_Subtype (Def_Id, T);
9932 end if;
9933 end if;
9934 end Build_Discriminated_Subtype;
9936 ---------------------------
9937 -- Build_Itype_Reference --
9938 ---------------------------
9940 procedure Build_Itype_Reference
9941 (Ityp : Entity_Id;
9942 Nod : Node_Id)
9944 IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
9945 begin
9947 -- Itype references are only created for use by the back-end
9949 if Inside_A_Generic then
9950 return;
9951 else
9952 Set_Itype (IR, Ityp);
9953 Insert_After (Nod, IR);
9954 end if;
9955 end Build_Itype_Reference;
9957 ------------------------
9958 -- Build_Scalar_Bound --
9959 ------------------------
9961 function Build_Scalar_Bound
9962 (Bound : Node_Id;
9963 Par_T : Entity_Id;
9964 Der_T : Entity_Id) return Node_Id
9966 New_Bound : Entity_Id;
9968 begin
9969 -- Note: not clear why this is needed, how can the original bound
9970 -- be unanalyzed at this point? and if it is, what business do we
9971 -- have messing around with it? and why is the base type of the
9972 -- parent type the right type for the resolution. It probably is
9973 -- not. It is OK for the new bound we are creating, but not for
9974 -- the old one??? Still if it never happens, no problem.
9976 Analyze_And_Resolve (Bound, Base_Type (Par_T));
9978 if Nkind_In (Bound, N_Integer_Literal, N_Real_Literal) then
9979 New_Bound := New_Copy (Bound);
9980 Set_Etype (New_Bound, Der_T);
9981 Set_Analyzed (New_Bound);
9983 elsif Is_Entity_Name (Bound) then
9984 New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
9986 -- The following is almost certainly wrong. What business do we have
9987 -- relocating a node (Bound) that is presumably still attached to
9988 -- the tree elsewhere???
9990 else
9991 New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
9992 end if;
9994 Set_Etype (New_Bound, Der_T);
9995 return New_Bound;
9996 end Build_Scalar_Bound;
9998 --------------------------------
9999 -- Build_Underlying_Full_View --
10000 --------------------------------
10002 procedure Build_Underlying_Full_View
10003 (N : Node_Id;
10004 Typ : Entity_Id;
10005 Par : Entity_Id)
10007 Loc : constant Source_Ptr := Sloc (N);
10008 Subt : constant Entity_Id :=
10009 Make_Defining_Identifier
10010 (Loc, New_External_Name (Chars (Typ), 'S'));
10012 Constr : Node_Id;
10013 Indic : Node_Id;
10014 C : Node_Id;
10015 Id : Node_Id;
10017 procedure Set_Discriminant_Name (Id : Node_Id);
10018 -- If the derived type has discriminants, they may rename discriminants
10019 -- of the parent. When building the full view of the parent, we need to
10020 -- recover the names of the original discriminants if the constraint is
10021 -- given by named associations.
10023 ---------------------------
10024 -- Set_Discriminant_Name --
10025 ---------------------------
10027 procedure Set_Discriminant_Name (Id : Node_Id) is
10028 Disc : Entity_Id;
10030 begin
10031 Set_Original_Discriminant (Id, Empty);
10033 if Has_Discriminants (Typ) then
10034 Disc := First_Discriminant (Typ);
10035 while Present (Disc) loop
10036 if Chars (Disc) = Chars (Id)
10037 and then Present (Corresponding_Discriminant (Disc))
10038 then
10039 Set_Chars (Id, Chars (Corresponding_Discriminant (Disc)));
10040 end if;
10041 Next_Discriminant (Disc);
10042 end loop;
10043 end if;
10044 end Set_Discriminant_Name;
10046 -- Start of processing for Build_Underlying_Full_View
10048 begin
10049 if Nkind (N) = N_Full_Type_Declaration then
10050 Constr := Constraint (Subtype_Indication (Type_Definition (N)));
10052 elsif Nkind (N) = N_Subtype_Declaration then
10053 Constr := New_Copy_Tree (Constraint (Subtype_Indication (N)));
10055 elsif Nkind (N) = N_Component_Declaration then
10056 Constr :=
10057 New_Copy_Tree
10058 (Constraint (Subtype_Indication (Component_Definition (N))));
10060 else
10061 raise Program_Error;
10062 end if;
10064 C := First (Constraints (Constr));
10065 while Present (C) loop
10066 if Nkind (C) = N_Discriminant_Association then
10067 Id := First (Selector_Names (C));
10068 while Present (Id) loop
10069 Set_Discriminant_Name (Id);
10070 Next (Id);
10071 end loop;
10072 end if;
10074 Next (C);
10075 end loop;
10077 Indic :=
10078 Make_Subtype_Declaration (Loc,
10079 Defining_Identifier => Subt,
10080 Subtype_Indication =>
10081 Make_Subtype_Indication (Loc,
10082 Subtype_Mark => New_Occurrence_Of (Par, Loc),
10083 Constraint => New_Copy_Tree (Constr)));
10085 -- If this is a component subtype for an outer itype, it is not
10086 -- a list member, so simply set the parent link for analysis: if
10087 -- the enclosing type does not need to be in a declarative list,
10088 -- neither do the components.
10090 if Is_List_Member (N)
10091 and then Nkind (N) /= N_Component_Declaration
10092 then
10093 Insert_Before (N, Indic);
10094 else
10095 Set_Parent (Indic, Parent (N));
10096 end if;
10098 Analyze (Indic);
10099 Set_Underlying_Full_View (Typ, Full_View (Subt));
10100 Set_Is_Underlying_Full_View (Full_View (Subt));
10101 end Build_Underlying_Full_View;
10103 -------------------------------
10104 -- Check_Abstract_Overriding --
10105 -------------------------------
10107 procedure Check_Abstract_Overriding (T : Entity_Id) is
10108 Alias_Subp : Entity_Id;
10109 Elmt : Elmt_Id;
10110 Op_List : Elist_Id;
10111 Subp : Entity_Id;
10112 Type_Def : Node_Id;
10114 procedure Check_Pragma_Implemented (Subp : Entity_Id);
10115 -- Ada 2012 (AI05-0030): Subprogram Subp overrides an interface routine
10116 -- which has pragma Implemented already set. Check whether Subp's entity
10117 -- kind conforms to the implementation kind of the overridden routine.
10119 procedure Check_Pragma_Implemented
10120 (Subp : Entity_Id;
10121 Iface_Subp : Entity_Id);
10122 -- Ada 2012 (AI05-0030): Subprogram Subp overrides interface routine
10123 -- Iface_Subp and both entities have pragma Implemented already set on
10124 -- them. Check whether the two implementation kinds are conforming.
10126 procedure Inherit_Pragma_Implemented
10127 (Subp : Entity_Id;
10128 Iface_Subp : Entity_Id);
10129 -- Ada 2012 (AI05-0030): Interface primitive Subp overrides interface
10130 -- subprogram Iface_Subp which has been marked by pragma Implemented.
10131 -- Propagate the implementation kind of Iface_Subp to Subp.
10133 ------------------------------
10134 -- Check_Pragma_Implemented --
10135 ------------------------------
10137 procedure Check_Pragma_Implemented (Subp : Entity_Id) is
10138 Iface_Alias : constant Entity_Id := Interface_Alias (Subp);
10139 Impl_Kind : constant Name_Id := Implementation_Kind (Iface_Alias);
10140 Subp_Alias : constant Entity_Id := Alias (Subp);
10141 Contr_Typ : Entity_Id;
10142 Impl_Subp : Entity_Id;
10144 begin
10145 -- Subp must have an alias since it is a hidden entity used to link
10146 -- an interface subprogram to its overriding counterpart.
10148 pragma Assert (Present (Subp_Alias));
10150 -- Handle aliases to synchronized wrappers
10152 Impl_Subp := Subp_Alias;
10154 if Is_Primitive_Wrapper (Impl_Subp) then
10155 Impl_Subp := Wrapped_Entity (Impl_Subp);
10156 end if;
10158 -- Extract the type of the controlling formal
10160 Contr_Typ := Etype (First_Formal (Subp_Alias));
10162 if Is_Concurrent_Record_Type (Contr_Typ) then
10163 Contr_Typ := Corresponding_Concurrent_Type (Contr_Typ);
10164 end if;
10166 -- An interface subprogram whose implementation kind is By_Entry must
10167 -- be implemented by an entry.
10169 if Impl_Kind = Name_By_Entry
10170 and then Ekind (Impl_Subp) /= E_Entry
10171 then
10172 Error_Msg_Node_2 := Iface_Alias;
10173 Error_Msg_NE
10174 ("type & must implement abstract subprogram & with an entry",
10175 Subp_Alias, Contr_Typ);
10177 elsif Impl_Kind = Name_By_Protected_Procedure then
10179 -- An interface subprogram whose implementation kind is By_
10180 -- Protected_Procedure cannot be implemented by a primitive
10181 -- procedure of a task type.
10183 if Ekind (Contr_Typ) /= E_Protected_Type then
10184 Error_Msg_Node_2 := Contr_Typ;
10185 Error_Msg_NE
10186 ("interface subprogram & cannot be implemented by a " &
10187 "primitive procedure of task type &", Subp_Alias,
10188 Iface_Alias);
10190 -- An interface subprogram whose implementation kind is By_
10191 -- Protected_Procedure must be implemented by a procedure.
10193 elsif Ekind (Impl_Subp) /= E_Procedure then
10194 Error_Msg_Node_2 := Iface_Alias;
10195 Error_Msg_NE
10196 ("type & must implement abstract subprogram & with a " &
10197 "procedure", Subp_Alias, Contr_Typ);
10199 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
10200 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
10201 then
10202 Error_Msg_Name_1 := Impl_Kind;
10203 Error_Msg_N
10204 ("overriding operation& must have synchronization%",
10205 Subp_Alias);
10206 end if;
10208 -- If primitive has Optional synchronization, overriding operation
10209 -- must match if it has an explicit synchronization..
10211 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
10212 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
10213 then
10214 Error_Msg_Name_1 := Impl_Kind;
10215 Error_Msg_N
10216 ("overriding operation& must have syncrhonization%",
10217 Subp_Alias);
10218 end if;
10219 end Check_Pragma_Implemented;
10221 ------------------------------
10222 -- Check_Pragma_Implemented --
10223 ------------------------------
10225 procedure Check_Pragma_Implemented
10226 (Subp : Entity_Id;
10227 Iface_Subp : Entity_Id)
10229 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
10230 Subp_Kind : constant Name_Id := Implementation_Kind (Subp);
10232 begin
10233 -- Ada 2012 (AI05-0030): The implementation kinds of an overridden
10234 -- and overriding subprogram are different. In general this is an
10235 -- error except when the implementation kind of the overridden
10236 -- subprograms is By_Any or Optional.
10238 if Iface_Kind /= Subp_Kind
10239 and then Iface_Kind /= Name_By_Any
10240 and then Iface_Kind /= Name_Optional
10241 then
10242 if Iface_Kind = Name_By_Entry then
10243 Error_Msg_N
10244 ("incompatible implementation kind, overridden subprogram " &
10245 "is marked By_Entry", Subp);
10246 else
10247 Error_Msg_N
10248 ("incompatible implementation kind, overridden subprogram " &
10249 "is marked By_Protected_Procedure", Subp);
10250 end if;
10251 end if;
10252 end Check_Pragma_Implemented;
10254 --------------------------------
10255 -- Inherit_Pragma_Implemented --
10256 --------------------------------
10258 procedure Inherit_Pragma_Implemented
10259 (Subp : Entity_Id;
10260 Iface_Subp : Entity_Id)
10262 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
10263 Loc : constant Source_Ptr := Sloc (Subp);
10264 Impl_Prag : Node_Id;
10266 begin
10267 -- Since the implementation kind is stored as a representation item
10268 -- rather than a flag, create a pragma node.
10270 Impl_Prag :=
10271 Make_Pragma (Loc,
10272 Chars => Name_Implemented,
10273 Pragma_Argument_Associations => New_List (
10274 Make_Pragma_Argument_Association (Loc,
10275 Expression => New_Occurrence_Of (Subp, Loc)),
10277 Make_Pragma_Argument_Association (Loc,
10278 Expression => Make_Identifier (Loc, Iface_Kind))));
10280 -- The pragma doesn't need to be analyzed because it is internally
10281 -- built. It is safe to directly register it as a rep item since we
10282 -- are only interested in the characters of the implementation kind.
10284 Record_Rep_Item (Subp, Impl_Prag);
10285 end Inherit_Pragma_Implemented;
10287 -- Start of processing for Check_Abstract_Overriding
10289 begin
10290 Op_List := Primitive_Operations (T);
10292 -- Loop to check primitive operations
10294 Elmt := First_Elmt (Op_List);
10295 while Present (Elmt) loop
10296 Subp := Node (Elmt);
10297 Alias_Subp := Alias (Subp);
10299 -- Inherited subprograms are identified by the fact that they do not
10300 -- come from source, and the associated source location is the
10301 -- location of the first subtype of the derived type.
10303 -- Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
10304 -- subprograms that "require overriding".
10306 -- Special exception, do not complain about failure to override the
10307 -- stream routines _Input and _Output, as well as the primitive
10308 -- operations used in dispatching selects since we always provide
10309 -- automatic overridings for these subprograms.
10311 -- The partial view of T may have been a private extension, for
10312 -- which inherited functions dispatching on result are abstract.
10313 -- If the full view is a null extension, there is no need for
10314 -- overriding in Ada 2005, but wrappers need to be built for them
10315 -- (see exp_ch3, Build_Controlling_Function_Wrappers).
10317 if Is_Null_Extension (T)
10318 and then Has_Controlling_Result (Subp)
10319 and then Ada_Version >= Ada_2005
10320 and then Present (Alias_Subp)
10321 and then not Comes_From_Source (Subp)
10322 and then not Is_Abstract_Subprogram (Alias_Subp)
10323 and then not Is_Access_Type (Etype (Subp))
10324 then
10325 null;
10327 -- Ada 2005 (AI-251): Internal entities of interfaces need no
10328 -- processing because this check is done with the aliased
10329 -- entity
10331 elsif Present (Interface_Alias (Subp)) then
10332 null;
10334 elsif (Is_Abstract_Subprogram (Subp)
10335 or else Requires_Overriding (Subp)
10336 or else
10337 (Has_Controlling_Result (Subp)
10338 and then Present (Alias_Subp)
10339 and then not Comes_From_Source (Subp)
10340 and then Sloc (Subp) = Sloc (First_Subtype (T))))
10341 and then not Is_TSS (Subp, TSS_Stream_Input)
10342 and then not Is_TSS (Subp, TSS_Stream_Output)
10343 and then not Is_Abstract_Type (T)
10344 and then not Is_Predefined_Interface_Primitive (Subp)
10346 -- Ada 2005 (AI-251): Do not consider hidden entities associated
10347 -- with abstract interface types because the check will be done
10348 -- with the aliased entity (otherwise we generate a duplicated
10349 -- error message).
10351 and then not Present (Interface_Alias (Subp))
10352 then
10353 if Present (Alias_Subp) then
10355 -- Only perform the check for a derived subprogram when the
10356 -- type has an explicit record extension. This avoids incorrect
10357 -- flagging of abstract subprograms for the case of a type
10358 -- without an extension that is derived from a formal type
10359 -- with a tagged actual (can occur within a private part).
10361 -- Ada 2005 (AI-391): In the case of an inherited function with
10362 -- a controlling result of the type, the rule does not apply if
10363 -- the type is a null extension (unless the parent function
10364 -- itself is abstract, in which case the function must still be
10365 -- be overridden). The expander will generate an overriding
10366 -- wrapper function calling the parent subprogram (see
10367 -- Exp_Ch3.Make_Controlling_Wrapper_Functions).
10369 Type_Def := Type_Definition (Parent (T));
10371 if Nkind (Type_Def) = N_Derived_Type_Definition
10372 and then Present (Record_Extension_Part (Type_Def))
10373 and then
10374 (Ada_Version < Ada_2005
10375 or else not Is_Null_Extension (T)
10376 or else Ekind (Subp) = E_Procedure
10377 or else not Has_Controlling_Result (Subp)
10378 or else Is_Abstract_Subprogram (Alias_Subp)
10379 or else Requires_Overriding (Subp)
10380 or else Is_Access_Type (Etype (Subp)))
10381 then
10382 -- Avoid reporting error in case of abstract predefined
10383 -- primitive inherited from interface type because the
10384 -- body of internally generated predefined primitives
10385 -- of tagged types are generated later by Freeze_Type
10387 if Is_Interface (Root_Type (T))
10388 and then Is_Abstract_Subprogram (Subp)
10389 and then Is_Predefined_Dispatching_Operation (Subp)
10390 and then not Comes_From_Source (Ultimate_Alias (Subp))
10391 then
10392 null;
10394 -- A null extension is not obliged to override an inherited
10395 -- procedure subject to pragma Extensions_Visible with value
10396 -- False and at least one controlling OUT parameter
10397 -- (SPARK RM 6.1.7(6)).
10399 elsif Is_Null_Extension (T)
10400 and then Is_EVF_Procedure (Subp)
10401 then
10402 null;
10404 else
10405 Error_Msg_NE
10406 ("type must be declared abstract or & overridden",
10407 T, Subp);
10409 -- Traverse the whole chain of aliased subprograms to
10410 -- complete the error notification. This is especially
10411 -- useful for traceability of the chain of entities when
10412 -- the subprogram corresponds with an interface
10413 -- subprogram (which may be defined in another package).
10415 if Present (Alias_Subp) then
10416 declare
10417 E : Entity_Id;
10419 begin
10420 E := Subp;
10421 while Present (Alias (E)) loop
10423 -- Avoid reporting redundant errors on entities
10424 -- inherited from interfaces
10426 if Sloc (E) /= Sloc (T) then
10427 Error_Msg_Sloc := Sloc (E);
10428 Error_Msg_NE
10429 ("\& has been inherited #", T, Subp);
10430 end if;
10432 E := Alias (E);
10433 end loop;
10435 Error_Msg_Sloc := Sloc (E);
10437 -- AI05-0068: report if there is an overriding
10438 -- non-abstract subprogram that is invisible.
10440 if Is_Hidden (E)
10441 and then not Is_Abstract_Subprogram (E)
10442 then
10443 Error_Msg_NE
10444 ("\& subprogram# is not visible",
10445 T, Subp);
10447 -- Clarify the case where a non-null extension must
10448 -- override inherited procedure subject to pragma
10449 -- Extensions_Visible with value False and at least
10450 -- one controlling OUT param.
10452 elsif Is_EVF_Procedure (E) then
10453 Error_Msg_NE
10454 ("\& # is subject to Extensions_Visible False",
10455 T, Subp);
10457 else
10458 Error_Msg_NE
10459 ("\& has been inherited from subprogram #",
10460 T, Subp);
10461 end if;
10462 end;
10463 end if;
10464 end if;
10466 -- Ada 2005 (AI-345): Protected or task type implementing
10467 -- abstract interfaces.
10469 elsif Is_Concurrent_Record_Type (T)
10470 and then Present (Interfaces (T))
10471 then
10472 -- There is no need to check here RM 9.4(11.9/3) since we
10473 -- are processing the corresponding record type and the
10474 -- mode of the overriding subprograms was verified by
10475 -- Check_Conformance when the corresponding concurrent
10476 -- type declaration was analyzed.
10478 Error_Msg_NE
10479 ("interface subprogram & must be overridden", T, Subp);
10481 -- Examine primitive operations of synchronized type to find
10482 -- homonyms that have the wrong profile.
10484 declare
10485 Prim : Entity_Id;
10487 begin
10488 Prim := First_Entity (Corresponding_Concurrent_Type (T));
10489 while Present (Prim) loop
10490 if Chars (Prim) = Chars (Subp) then
10491 Error_Msg_NE
10492 ("profile is not type conformant with prefixed "
10493 & "view profile of inherited operation&",
10494 Prim, Subp);
10495 end if;
10497 Next_Entity (Prim);
10498 end loop;
10499 end;
10500 end if;
10502 else
10503 Error_Msg_Node_2 := T;
10504 Error_Msg_N
10505 ("abstract subprogram& not allowed for type&", Subp);
10507 -- Also post unconditional warning on the type (unconditional
10508 -- so that if there are more than one of these cases, we get
10509 -- them all, and not just the first one).
10511 Error_Msg_Node_2 := Subp;
10512 Error_Msg_N ("nonabstract type& has abstract subprogram&!", T);
10513 end if;
10515 -- A subprogram subject to pragma Extensions_Visible with value
10516 -- "True" cannot override a subprogram subject to the same pragma
10517 -- with value "False" (SPARK RM 6.1.7(5)).
10519 elsif Extensions_Visible_Status (Subp) = Extensions_Visible_True
10520 and then Present (Overridden_Operation (Subp))
10521 and then Extensions_Visible_Status (Overridden_Operation (Subp)) =
10522 Extensions_Visible_False
10523 then
10524 Error_Msg_Sloc := Sloc (Overridden_Operation (Subp));
10525 Error_Msg_N
10526 ("subprogram & with Extensions_Visible True cannot override "
10527 & "subprogram # with Extensions_Visible False", Subp);
10528 end if;
10530 -- Ada 2012 (AI05-0030): Perform checks related to pragma Implemented
10532 -- Subp is an expander-generated procedure which maps an interface
10533 -- alias to a protected wrapper. The interface alias is flagged by
10534 -- pragma Implemented. Ensure that Subp is a procedure when the
10535 -- implementation kind is By_Protected_Procedure or an entry when
10536 -- By_Entry.
10538 if Ada_Version >= Ada_2012
10539 and then Is_Hidden (Subp)
10540 and then Present (Interface_Alias (Subp))
10541 and then Has_Rep_Pragma (Interface_Alias (Subp), Name_Implemented)
10542 then
10543 Check_Pragma_Implemented (Subp);
10544 end if;
10546 -- Subp is an interface primitive which overrides another interface
10547 -- primitive marked with pragma Implemented.
10549 if Ada_Version >= Ada_2012
10550 and then Present (Overridden_Operation (Subp))
10551 and then Has_Rep_Pragma
10552 (Overridden_Operation (Subp), Name_Implemented)
10553 then
10554 -- If the overriding routine is also marked by Implemented, check
10555 -- that the two implementation kinds are conforming.
10557 if Has_Rep_Pragma (Subp, Name_Implemented) then
10558 Check_Pragma_Implemented
10559 (Subp => Subp,
10560 Iface_Subp => Overridden_Operation (Subp));
10562 -- Otherwise the overriding routine inherits the implementation
10563 -- kind from the overridden subprogram.
10565 else
10566 Inherit_Pragma_Implemented
10567 (Subp => Subp,
10568 Iface_Subp => Overridden_Operation (Subp));
10569 end if;
10570 end if;
10572 -- If the operation is a wrapper for a synchronized primitive, it
10573 -- may be called indirectly through a dispatching select. We assume
10574 -- that it will be referenced elsewhere indirectly, and suppress
10575 -- warnings about an unused entity.
10577 if Is_Primitive_Wrapper (Subp)
10578 and then Present (Wrapped_Entity (Subp))
10579 then
10580 Set_Referenced (Wrapped_Entity (Subp));
10581 end if;
10583 Next_Elmt (Elmt);
10584 end loop;
10585 end Check_Abstract_Overriding;
10587 ------------------------------------------------
10588 -- Check_Access_Discriminant_Requires_Limited --
10589 ------------------------------------------------
10591 procedure Check_Access_Discriminant_Requires_Limited
10592 (D : Node_Id;
10593 Loc : Node_Id)
10595 begin
10596 -- A discriminant_specification for an access discriminant shall appear
10597 -- only in the declaration for a task or protected type, or for a type
10598 -- with the reserved word 'limited' in its definition or in one of its
10599 -- ancestors (RM 3.7(10)).
10601 -- AI-0063: The proper condition is that type must be immutably limited,
10602 -- or else be a partial view.
10604 if Nkind (Discriminant_Type (D)) = N_Access_Definition then
10605 if Is_Limited_View (Current_Scope)
10606 or else
10607 (Nkind (Parent (Current_Scope)) = N_Private_Type_Declaration
10608 and then Limited_Present (Parent (Current_Scope)))
10609 then
10610 null;
10612 else
10613 Error_Msg_N
10614 ("access discriminants allowed only for limited types", Loc);
10615 end if;
10616 end if;
10617 end Check_Access_Discriminant_Requires_Limited;
10619 -----------------------------------
10620 -- Check_Aliased_Component_Types --
10621 -----------------------------------
10623 procedure Check_Aliased_Component_Types (T : Entity_Id) is
10624 C : Entity_Id;
10626 begin
10627 -- ??? Also need to check components of record extensions, but not
10628 -- components of protected types (which are always limited).
10630 -- Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
10631 -- types to be unconstrained. This is safe because it is illegal to
10632 -- create access subtypes to such types with explicit discriminant
10633 -- constraints.
10635 if not Is_Limited_Type (T) then
10636 if Ekind (T) = E_Record_Type then
10637 C := First_Component (T);
10638 while Present (C) loop
10639 if Is_Aliased (C)
10640 and then Has_Discriminants (Etype (C))
10641 and then not Is_Constrained (Etype (C))
10642 and then not In_Instance_Body
10643 and then Ada_Version < Ada_2005
10644 then
10645 Error_Msg_N
10646 ("aliased component must be constrained (RM 3.6(11))",
10648 end if;
10650 Next_Component (C);
10651 end loop;
10653 elsif Ekind (T) = E_Array_Type then
10654 if Has_Aliased_Components (T)
10655 and then Has_Discriminants (Component_Type (T))
10656 and then not Is_Constrained (Component_Type (T))
10657 and then not In_Instance_Body
10658 and then Ada_Version < Ada_2005
10659 then
10660 Error_Msg_N
10661 ("aliased component type must be constrained (RM 3.6(11))",
10663 end if;
10664 end if;
10665 end if;
10666 end Check_Aliased_Component_Types;
10668 ---------------------------------------
10669 -- Check_Anonymous_Access_Components --
10670 ---------------------------------------
10672 procedure Check_Anonymous_Access_Components
10673 (Typ_Decl : Node_Id;
10674 Typ : Entity_Id;
10675 Prev : Entity_Id;
10676 Comp_List : Node_Id)
10678 Loc : constant Source_Ptr := Sloc (Typ_Decl);
10679 Anon_Access : Entity_Id;
10680 Acc_Def : Node_Id;
10681 Comp : Node_Id;
10682 Comp_Def : Node_Id;
10683 Decl : Node_Id;
10684 Type_Def : Node_Id;
10686 procedure Build_Incomplete_Type_Declaration;
10687 -- If the record type contains components that include an access to the
10688 -- current record, then create an incomplete type declaration for the
10689 -- record, to be used as the designated type of the anonymous access.
10690 -- This is done only once, and only if there is no previous partial
10691 -- view of the type.
10693 function Designates_T (Subt : Node_Id) return Boolean;
10694 -- Check whether a node designates the enclosing record type, or 'Class
10695 -- of that type
10697 function Mentions_T (Acc_Def : Node_Id) return Boolean;
10698 -- Check whether an access definition includes a reference to
10699 -- the enclosing record type. The reference can be a subtype mark
10700 -- in the access definition itself, a 'Class attribute reference, or
10701 -- recursively a reference appearing in a parameter specification
10702 -- or result definition of an access_to_subprogram definition.
10704 --------------------------------------
10705 -- Build_Incomplete_Type_Declaration --
10706 --------------------------------------
10708 procedure Build_Incomplete_Type_Declaration is
10709 Decl : Node_Id;
10710 Inc_T : Entity_Id;
10711 H : Entity_Id;
10713 -- Is_Tagged indicates whether the type is tagged. It is tagged if
10714 -- it's "is new ... with record" or else "is tagged record ...".
10716 Is_Tagged : constant Boolean :=
10717 (Nkind (Type_Definition (Typ_Decl)) = N_Derived_Type_Definition
10718 and then
10719 Present (Record_Extension_Part (Type_Definition (Typ_Decl))))
10720 or else
10721 (Nkind (Type_Definition (Typ_Decl)) = N_Record_Definition
10722 and then Tagged_Present (Type_Definition (Typ_Decl)));
10724 begin
10725 -- If there is a previous partial view, no need to create a new one
10726 -- If the partial view, given by Prev, is incomplete, If Prev is
10727 -- a private declaration, full declaration is flagged accordingly.
10729 if Prev /= Typ then
10730 if Is_Tagged then
10731 Make_Class_Wide_Type (Prev);
10732 Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
10733 Set_Etype (Class_Wide_Type (Typ), Typ);
10734 end if;
10736 return;
10738 elsif Has_Private_Declaration (Typ) then
10740 -- If we refer to T'Class inside T, and T is the completion of a
10741 -- private type, then make sure the class-wide type exists.
10743 if Is_Tagged then
10744 Make_Class_Wide_Type (Typ);
10745 end if;
10747 return;
10749 -- If there was a previous anonymous access type, the incomplete
10750 -- type declaration will have been created already.
10752 elsif Present (Current_Entity (Typ))
10753 and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
10754 and then Full_View (Current_Entity (Typ)) = Typ
10755 then
10756 if Is_Tagged
10757 and then Comes_From_Source (Current_Entity (Typ))
10758 and then not Is_Tagged_Type (Current_Entity (Typ))
10759 then
10760 Make_Class_Wide_Type (Typ);
10761 Error_Msg_N
10762 ("incomplete view of tagged type should be declared tagged??",
10763 Parent (Current_Entity (Typ)));
10764 end if;
10765 return;
10767 else
10768 Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
10769 Decl := Make_Incomplete_Type_Declaration (Loc, Inc_T);
10771 -- Type has already been inserted into the current scope. Remove
10772 -- it, and add incomplete declaration for type, so that subsequent
10773 -- anonymous access types can use it. The entity is unchained from
10774 -- the homonym list and from immediate visibility. After analysis,
10775 -- the entity in the incomplete declaration becomes immediately
10776 -- visible in the record declaration that follows.
10778 H := Current_Entity (Typ);
10780 if H = Typ then
10781 Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
10782 else
10783 while Present (H)
10784 and then Homonym (H) /= Typ
10785 loop
10786 H := Homonym (Typ);
10787 end loop;
10789 Set_Homonym (H, Homonym (Typ));
10790 end if;
10792 Insert_Before (Typ_Decl, Decl);
10793 Analyze (Decl);
10794 Set_Full_View (Inc_T, Typ);
10796 if Is_Tagged then
10798 -- Create a common class-wide type for both views, and set the
10799 -- Etype of the class-wide type to the full view.
10801 Make_Class_Wide_Type (Inc_T);
10802 Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
10803 Set_Etype (Class_Wide_Type (Typ), Typ);
10804 end if;
10805 end if;
10806 end Build_Incomplete_Type_Declaration;
10808 ------------------
10809 -- Designates_T --
10810 ------------------
10812 function Designates_T (Subt : Node_Id) return Boolean is
10813 Type_Id : constant Name_Id := Chars (Typ);
10815 function Names_T (Nam : Node_Id) return Boolean;
10816 -- The record type has not been introduced in the current scope
10817 -- yet, so we must examine the name of the type itself, either
10818 -- an identifier T, or an expanded name of the form P.T, where
10819 -- P denotes the current scope.
10821 -------------
10822 -- Names_T --
10823 -------------
10825 function Names_T (Nam : Node_Id) return Boolean is
10826 begin
10827 if Nkind (Nam) = N_Identifier then
10828 return Chars (Nam) = Type_Id;
10830 elsif Nkind (Nam) = N_Selected_Component then
10831 if Chars (Selector_Name (Nam)) = Type_Id then
10832 if Nkind (Prefix (Nam)) = N_Identifier then
10833 return Chars (Prefix (Nam)) = Chars (Current_Scope);
10835 elsif Nkind (Prefix (Nam)) = N_Selected_Component then
10836 return Chars (Selector_Name (Prefix (Nam))) =
10837 Chars (Current_Scope);
10838 else
10839 return False;
10840 end if;
10842 else
10843 return False;
10844 end if;
10846 else
10847 return False;
10848 end if;
10849 end Names_T;
10851 -- Start of processing for Designates_T
10853 begin
10854 if Nkind (Subt) = N_Identifier then
10855 return Chars (Subt) = Type_Id;
10857 -- Reference can be through an expanded name which has not been
10858 -- analyzed yet, and which designates enclosing scopes.
10860 elsif Nkind (Subt) = N_Selected_Component then
10861 if Names_T (Subt) then
10862 return True;
10864 -- Otherwise it must denote an entity that is already visible.
10865 -- The access definition may name a subtype of the enclosing
10866 -- type, if there is a previous incomplete declaration for it.
10868 else
10869 Find_Selected_Component (Subt);
10870 return
10871 Is_Entity_Name (Subt)
10872 and then Scope (Entity (Subt)) = Current_Scope
10873 and then
10874 (Chars (Base_Type (Entity (Subt))) = Type_Id
10875 or else
10876 (Is_Class_Wide_Type (Entity (Subt))
10877 and then
10878 Chars (Etype (Base_Type (Entity (Subt)))) =
10879 Type_Id));
10880 end if;
10882 -- A reference to the current type may appear as the prefix of
10883 -- a 'Class attribute.
10885 elsif Nkind (Subt) = N_Attribute_Reference
10886 and then Attribute_Name (Subt) = Name_Class
10887 then
10888 return Names_T (Prefix (Subt));
10890 else
10891 return False;
10892 end if;
10893 end Designates_T;
10895 ----------------
10896 -- Mentions_T --
10897 ----------------
10899 function Mentions_T (Acc_Def : Node_Id) return Boolean is
10900 Param_Spec : Node_Id;
10902 Acc_Subprg : constant Node_Id :=
10903 Access_To_Subprogram_Definition (Acc_Def);
10905 begin
10906 if No (Acc_Subprg) then
10907 return Designates_T (Subtype_Mark (Acc_Def));
10908 end if;
10910 -- Component is an access_to_subprogram: examine its formals,
10911 -- and result definition in the case of an access_to_function.
10913 Param_Spec := First (Parameter_Specifications (Acc_Subprg));
10914 while Present (Param_Spec) loop
10915 if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
10916 and then Mentions_T (Parameter_Type (Param_Spec))
10917 then
10918 return True;
10920 elsif Designates_T (Parameter_Type (Param_Spec)) then
10921 return True;
10922 end if;
10924 Next (Param_Spec);
10925 end loop;
10927 if Nkind (Acc_Subprg) = N_Access_Function_Definition then
10928 if Nkind (Result_Definition (Acc_Subprg)) =
10929 N_Access_Definition
10930 then
10931 return Mentions_T (Result_Definition (Acc_Subprg));
10932 else
10933 return Designates_T (Result_Definition (Acc_Subprg));
10934 end if;
10935 end if;
10937 return False;
10938 end Mentions_T;
10940 -- Start of processing for Check_Anonymous_Access_Components
10942 begin
10943 if No (Comp_List) then
10944 return;
10945 end if;
10947 Comp := First (Component_Items (Comp_List));
10948 while Present (Comp) loop
10949 if Nkind (Comp) = N_Component_Declaration
10950 and then Present
10951 (Access_Definition (Component_Definition (Comp)))
10952 and then
10953 Mentions_T (Access_Definition (Component_Definition (Comp)))
10954 then
10955 Comp_Def := Component_Definition (Comp);
10956 Acc_Def :=
10957 Access_To_Subprogram_Definition (Access_Definition (Comp_Def));
10959 Build_Incomplete_Type_Declaration;
10960 Anon_Access := Make_Temporary (Loc, 'S');
10962 -- Create a declaration for the anonymous access type: either
10963 -- an access_to_object or an access_to_subprogram.
10965 if Present (Acc_Def) then
10966 if Nkind (Acc_Def) = N_Access_Function_Definition then
10967 Type_Def :=
10968 Make_Access_Function_Definition (Loc,
10969 Parameter_Specifications =>
10970 Parameter_Specifications (Acc_Def),
10971 Result_Definition => Result_Definition (Acc_Def));
10972 else
10973 Type_Def :=
10974 Make_Access_Procedure_Definition (Loc,
10975 Parameter_Specifications =>
10976 Parameter_Specifications (Acc_Def));
10977 end if;
10979 else
10980 Type_Def :=
10981 Make_Access_To_Object_Definition (Loc,
10982 Subtype_Indication =>
10983 Relocate_Node
10984 (Subtype_Mark (Access_Definition (Comp_Def))));
10986 Set_Constant_Present
10987 (Type_Def, Constant_Present (Access_Definition (Comp_Def)));
10988 Set_All_Present
10989 (Type_Def, All_Present (Access_Definition (Comp_Def)));
10990 end if;
10992 Set_Null_Exclusion_Present
10993 (Type_Def,
10994 Null_Exclusion_Present (Access_Definition (Comp_Def)));
10996 Decl :=
10997 Make_Full_Type_Declaration (Loc,
10998 Defining_Identifier => Anon_Access,
10999 Type_Definition => Type_Def);
11001 Insert_Before (Typ_Decl, Decl);
11002 Analyze (Decl);
11004 -- If an access to subprogram, create the extra formals
11006 if Present (Acc_Def) then
11007 Create_Extra_Formals (Designated_Type (Anon_Access));
11009 -- If an access to object, preserve entity of designated type,
11010 -- for ASIS use, before rewriting the component definition.
11012 else
11013 declare
11014 Desig : Entity_Id;
11016 begin
11017 Desig := Entity (Subtype_Indication (Type_Def));
11019 -- If the access definition is to the current record,
11020 -- the visible entity at this point is an incomplete
11021 -- type. Retrieve the full view to simplify ASIS queries
11023 if Ekind (Desig) = E_Incomplete_Type then
11024 Desig := Full_View (Desig);
11025 end if;
11027 Set_Entity
11028 (Subtype_Mark (Access_Definition (Comp_Def)), Desig);
11029 end;
11030 end if;
11032 Rewrite (Comp_Def,
11033 Make_Component_Definition (Loc,
11034 Subtype_Indication =>
11035 New_Occurrence_Of (Anon_Access, Loc)));
11037 if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
11038 Set_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
11039 else
11040 Set_Ekind (Anon_Access, E_Anonymous_Access_Type);
11041 end if;
11043 Set_Is_Local_Anonymous_Access (Anon_Access);
11044 end if;
11046 Next (Comp);
11047 end loop;
11049 if Present (Variant_Part (Comp_List)) then
11050 declare
11051 V : Node_Id;
11052 begin
11053 V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
11054 while Present (V) loop
11055 Check_Anonymous_Access_Components
11056 (Typ_Decl, Typ, Prev, Component_List (V));
11057 Next_Non_Pragma (V);
11058 end loop;
11059 end;
11060 end if;
11061 end Check_Anonymous_Access_Components;
11063 ----------------------
11064 -- Check_Completion --
11065 ----------------------
11067 procedure Check_Completion (Body_Id : Node_Id := Empty) is
11068 E : Entity_Id;
11070 procedure Post_Error;
11071 -- Post error message for lack of completion for entity E
11073 ----------------
11074 -- Post_Error --
11075 ----------------
11077 procedure Post_Error is
11078 procedure Missing_Body;
11079 -- Output missing body message
11081 ------------------
11082 -- Missing_Body --
11083 ------------------
11085 procedure Missing_Body is
11086 begin
11087 -- Spec is in same unit, so we can post on spec
11089 if In_Same_Source_Unit (Body_Id, E) then
11090 Error_Msg_N ("missing body for &", E);
11092 -- Spec is in a separate unit, so we have to post on the body
11094 else
11095 Error_Msg_NE ("missing body for & declared#!", Body_Id, E);
11096 end if;
11097 end Missing_Body;
11099 -- Start of processing for Post_Error
11101 begin
11102 if not Comes_From_Source (E) then
11103 if Ekind_In (E, E_Task_Type, E_Protected_Type) then
11105 -- It may be an anonymous protected type created for a
11106 -- single variable. Post error on variable, if present.
11108 declare
11109 Var : Entity_Id;
11111 begin
11112 Var := First_Entity (Current_Scope);
11113 while Present (Var) loop
11114 exit when Etype (Var) = E
11115 and then Comes_From_Source (Var);
11117 Next_Entity (Var);
11118 end loop;
11120 if Present (Var) then
11121 E := Var;
11122 end if;
11123 end;
11124 end if;
11125 end if;
11127 -- If a generated entity has no completion, then either previous
11128 -- semantic errors have disabled the expansion phase, or else we had
11129 -- missing subunits, or else we are compiling without expansion,
11130 -- or else something is very wrong.
11132 if not Comes_From_Source (E) then
11133 pragma Assert
11134 (Serious_Errors_Detected > 0
11135 or else Configurable_Run_Time_Violations > 0
11136 or else Subunits_Missing
11137 or else not Expander_Active);
11138 return;
11140 -- Here for source entity
11142 else
11143 -- Here if no body to post the error message, so we post the error
11144 -- on the declaration that has no completion. This is not really
11145 -- the right place to post it, think about this later ???
11147 if No (Body_Id) then
11148 if Is_Type (E) then
11149 Error_Msg_NE
11150 ("missing full declaration for }", Parent (E), E);
11151 else
11152 Error_Msg_NE ("missing body for &", Parent (E), E);
11153 end if;
11155 -- Package body has no completion for a declaration that appears
11156 -- in the corresponding spec. Post error on the body, with a
11157 -- reference to the non-completed declaration.
11159 else
11160 Error_Msg_Sloc := Sloc (E);
11162 if Is_Type (E) then
11163 Error_Msg_NE ("missing full declaration for }!", Body_Id, E);
11165 elsif Is_Overloadable (E)
11166 and then Current_Entity_In_Scope (E) /= E
11167 then
11168 -- It may be that the completion is mistyped and appears as
11169 -- a distinct overloading of the entity.
11171 declare
11172 Candidate : constant Entity_Id :=
11173 Current_Entity_In_Scope (E);
11174 Decl : constant Node_Id :=
11175 Unit_Declaration_Node (Candidate);
11177 begin
11178 if Is_Overloadable (Candidate)
11179 and then Ekind (Candidate) = Ekind (E)
11180 and then Nkind (Decl) = N_Subprogram_Body
11181 and then Acts_As_Spec (Decl)
11182 then
11183 Check_Type_Conformant (Candidate, E);
11185 else
11186 Missing_Body;
11187 end if;
11188 end;
11190 else
11191 Missing_Body;
11192 end if;
11193 end if;
11194 end if;
11195 end Post_Error;
11197 -- Local variables
11199 Pack_Id : constant Entity_Id := Current_Scope;
11201 -- Start of processing for Check_Completion
11203 begin
11204 E := First_Entity (Pack_Id);
11205 while Present (E) loop
11206 if Is_Intrinsic_Subprogram (E) then
11207 null;
11209 -- The following situation requires special handling: a child unit
11210 -- that appears in the context clause of the body of its parent:
11212 -- procedure Parent.Child (...);
11214 -- with Parent.Child;
11215 -- package body Parent is
11217 -- Here Parent.Child appears as a local entity, but should not be
11218 -- flagged as requiring completion, because it is a compilation
11219 -- unit.
11221 -- Ignore missing completion for a subprogram that does not come from
11222 -- source (including the _Call primitive operation of RAS types,
11223 -- which has to have the flag Comes_From_Source for other purposes):
11224 -- we assume that the expander will provide the missing completion.
11225 -- In case of previous errors, other expansion actions that provide
11226 -- bodies for null procedures with not be invoked, so inhibit message
11227 -- in those cases.
11229 -- Note that E_Operator is not in the list that follows, because
11230 -- this kind is reserved for predefined operators, that are
11231 -- intrinsic and do not need completion.
11233 elsif Ekind_In (E, E_Function,
11234 E_Procedure,
11235 E_Generic_Function,
11236 E_Generic_Procedure)
11237 then
11238 if Has_Completion (E) then
11239 null;
11241 elsif Is_Subprogram (E) and then Is_Abstract_Subprogram (E) then
11242 null;
11244 elsif Is_Subprogram (E)
11245 and then (not Comes_From_Source (E)
11246 or else Chars (E) = Name_uCall)
11247 then
11248 null;
11250 elsif
11251 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
11252 then
11253 null;
11255 elsif Nkind (Parent (E)) = N_Procedure_Specification
11256 and then Null_Present (Parent (E))
11257 and then Serious_Errors_Detected > 0
11258 then
11259 null;
11261 else
11262 Post_Error;
11263 end if;
11265 elsif Is_Entry (E) then
11266 if not Has_Completion (E) and then
11267 (Ekind (Scope (E)) = E_Protected_Object
11268 or else Ekind (Scope (E)) = E_Protected_Type)
11269 then
11270 Post_Error;
11271 end if;
11273 elsif Is_Package_Or_Generic_Package (E) then
11274 if Unit_Requires_Body (E) then
11275 if not Has_Completion (E)
11276 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
11277 N_Compilation_Unit
11278 then
11279 Post_Error;
11280 end if;
11282 elsif not Is_Child_Unit (E) then
11283 May_Need_Implicit_Body (E);
11284 end if;
11286 -- A formal incomplete type (Ada 2012) does not require a completion;
11287 -- other incomplete type declarations do.
11289 elsif Ekind (E) = E_Incomplete_Type
11290 and then No (Underlying_Type (E))
11291 and then not Is_Generic_Type (E)
11292 then
11293 Post_Error;
11295 elsif Ekind_In (E, E_Task_Type, E_Protected_Type)
11296 and then not Has_Completion (E)
11297 then
11298 Post_Error;
11300 -- A single task declared in the current scope is a constant, verify
11301 -- that the body of its anonymous type is in the same scope. If the
11302 -- task is defined elsewhere, this may be a renaming declaration for
11303 -- which no completion is needed.
11305 elsif Ekind (E) = E_Constant
11306 and then Ekind (Etype (E)) = E_Task_Type
11307 and then not Has_Completion (Etype (E))
11308 and then Scope (Etype (E)) = Current_Scope
11309 then
11310 Post_Error;
11312 elsif Ekind (E) = E_Protected_Object
11313 and then not Has_Completion (Etype (E))
11314 then
11315 Post_Error;
11317 elsif Ekind (E) = E_Record_Type then
11318 if Is_Tagged_Type (E) then
11319 Check_Abstract_Overriding (E);
11320 Check_Conventions (E);
11321 end if;
11323 Check_Aliased_Component_Types (E);
11325 elsif Ekind (E) = E_Array_Type then
11326 Check_Aliased_Component_Types (E);
11328 end if;
11330 Next_Entity (E);
11331 end loop;
11332 end Check_Completion;
11334 ------------------------------------
11335 -- Check_CPP_Type_Has_No_Defaults --
11336 ------------------------------------
11338 procedure Check_CPP_Type_Has_No_Defaults (T : Entity_Id) is
11339 Tdef : constant Node_Id := Type_Definition (Declaration_Node (T));
11340 Clist : Node_Id;
11341 Comp : Node_Id;
11343 begin
11344 -- Obtain the component list
11346 if Nkind (Tdef) = N_Record_Definition then
11347 Clist := Component_List (Tdef);
11348 else pragma Assert (Nkind (Tdef) = N_Derived_Type_Definition);
11349 Clist := Component_List (Record_Extension_Part (Tdef));
11350 end if;
11352 -- Check all components to ensure no default expressions
11354 if Present (Clist) then
11355 Comp := First (Component_Items (Clist));
11356 while Present (Comp) loop
11357 if Present (Expression (Comp)) then
11358 Error_Msg_N
11359 ("component of imported 'C'P'P type cannot have "
11360 & "default expression", Expression (Comp));
11361 end if;
11363 Next (Comp);
11364 end loop;
11365 end if;
11366 end Check_CPP_Type_Has_No_Defaults;
11368 ----------------------------
11369 -- Check_Delta_Expression --
11370 ----------------------------
11372 procedure Check_Delta_Expression (E : Node_Id) is
11373 begin
11374 if not (Is_Real_Type (Etype (E))) then
11375 Wrong_Type (E, Any_Real);
11377 elsif not Is_OK_Static_Expression (E) then
11378 Flag_Non_Static_Expr
11379 ("non-static expression used for delta value!", E);
11381 elsif not UR_Is_Positive (Expr_Value_R (E)) then
11382 Error_Msg_N ("delta expression must be positive", E);
11384 else
11385 return;
11386 end if;
11388 -- If any of above errors occurred, then replace the incorrect
11389 -- expression by the real 0.1, which should prevent further errors.
11391 Rewrite (E,
11392 Make_Real_Literal (Sloc (E), Ureal_Tenth));
11393 Analyze_And_Resolve (E, Standard_Float);
11394 end Check_Delta_Expression;
11396 -----------------------------
11397 -- Check_Digits_Expression --
11398 -----------------------------
11400 procedure Check_Digits_Expression (E : Node_Id) is
11401 begin
11402 if not (Is_Integer_Type (Etype (E))) then
11403 Wrong_Type (E, Any_Integer);
11405 elsif not Is_OK_Static_Expression (E) then
11406 Flag_Non_Static_Expr
11407 ("non-static expression used for digits value!", E);
11409 elsif Expr_Value (E) <= 0 then
11410 Error_Msg_N ("digits value must be greater than zero", E);
11412 else
11413 return;
11414 end if;
11416 -- If any of above errors occurred, then replace the incorrect
11417 -- expression by the integer 1, which should prevent further errors.
11419 Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
11420 Analyze_And_Resolve (E, Standard_Integer);
11422 end Check_Digits_Expression;
11424 --------------------------
11425 -- Check_Initialization --
11426 --------------------------
11428 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
11429 begin
11430 -- Special processing for limited types
11432 if Is_Limited_Type (T)
11433 and then not In_Instance
11434 and then not In_Inlined_Body
11435 then
11436 if not OK_For_Limited_Init (T, Exp) then
11438 -- In GNAT mode, this is just a warning, to allow it to be evilly
11439 -- turned off. Otherwise it is a real error.
11441 if GNAT_Mode then
11442 Error_Msg_N
11443 ("??cannot initialize entities of limited type!", Exp);
11445 elsif Ada_Version < Ada_2005 then
11447 -- The side effect removal machinery may generate illegal Ada
11448 -- code to avoid the usage of access types and 'reference in
11449 -- SPARK mode. Since this is legal code with respect to theorem
11450 -- proving, do not emit the error.
11452 if GNATprove_Mode
11453 and then Nkind (Exp) = N_Function_Call
11454 and then Nkind (Parent (Exp)) = N_Object_Declaration
11455 and then not Comes_From_Source
11456 (Defining_Identifier (Parent (Exp)))
11457 then
11458 null;
11460 else
11461 Error_Msg_N
11462 ("cannot initialize entities of limited type", Exp);
11463 Explain_Limited_Type (T, Exp);
11464 end if;
11466 else
11467 -- Specialize error message according to kind of illegal
11468 -- initial expression.
11470 if Nkind (Exp) = N_Type_Conversion
11471 and then Nkind (Expression (Exp)) = N_Function_Call
11472 then
11473 Error_Msg_N
11474 ("illegal context for call"
11475 & " to function with limited result", Exp);
11477 else
11478 Error_Msg_N
11479 ("initialization of limited object requires aggregate "
11480 & "or function call", Exp);
11481 end if;
11482 end if;
11483 end if;
11484 end if;
11486 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag gets
11487 -- set unless we can be sure that no range check is required.
11489 if (GNATprove_Mode or not Expander_Active)
11490 and then Is_Scalar_Type (T)
11491 and then not Is_In_Range (Exp, T, Assume_Valid => True)
11492 then
11493 Set_Do_Range_Check (Exp);
11494 end if;
11495 end Check_Initialization;
11497 ----------------------
11498 -- Check_Interfaces --
11499 ----------------------
11501 procedure Check_Interfaces (N : Node_Id; Def : Node_Id) is
11502 Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
11504 Iface : Node_Id;
11505 Iface_Def : Node_Id;
11506 Iface_Typ : Entity_Id;
11507 Parent_Node : Node_Id;
11509 Is_Task : Boolean := False;
11510 -- Set True if parent type or any progenitor is a task interface
11512 Is_Protected : Boolean := False;
11513 -- Set True if parent type or any progenitor is a protected interface
11515 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
11516 -- Check that a progenitor is compatible with declaration. If an error
11517 -- message is output, it is posted on Error_Node.
11519 ------------------
11520 -- Check_Ifaces --
11521 ------------------
11523 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
11524 Iface_Id : constant Entity_Id :=
11525 Defining_Identifier (Parent (Iface_Def));
11526 Type_Def : Node_Id;
11528 begin
11529 if Nkind (N) = N_Private_Extension_Declaration then
11530 Type_Def := N;
11531 else
11532 Type_Def := Type_Definition (N);
11533 end if;
11535 if Is_Task_Interface (Iface_Id) then
11536 Is_Task := True;
11538 elsif Is_Protected_Interface (Iface_Id) then
11539 Is_Protected := True;
11540 end if;
11542 if Is_Synchronized_Interface (Iface_Id) then
11544 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
11545 -- extension derived from a synchronized interface must explicitly
11546 -- be declared synchronized, because the full view will be a
11547 -- synchronized type.
11549 if Nkind (N) = N_Private_Extension_Declaration then
11550 if not Synchronized_Present (N) then
11551 Error_Msg_NE
11552 ("private extension of& must be explicitly synchronized",
11553 N, Iface_Id);
11554 end if;
11556 -- However, by 3.9.4(16/2), a full type that is a record extension
11557 -- is never allowed to derive from a synchronized interface (note
11558 -- that interfaces must be excluded from this check, because those
11559 -- are represented by derived type definitions in some cases).
11561 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition
11562 and then not Interface_Present (Type_Definition (N))
11563 then
11564 Error_Msg_N ("record extension cannot derive from synchronized "
11565 & "interface", Error_Node);
11566 end if;
11567 end if;
11569 -- Check that the characteristics of the progenitor are compatible
11570 -- with the explicit qualifier in the declaration.
11571 -- The check only applies to qualifiers that come from source.
11572 -- Limited_Present also appears in the declaration of corresponding
11573 -- records, and the check does not apply to them.
11575 if Limited_Present (Type_Def)
11576 and then not
11577 Is_Concurrent_Record_Type (Defining_Identifier (N))
11578 then
11579 if Is_Limited_Interface (Parent_Type)
11580 and then not Is_Limited_Interface (Iface_Id)
11581 then
11582 Error_Msg_NE
11583 ("progenitor & must be limited interface",
11584 Error_Node, Iface_Id);
11586 elsif
11587 (Task_Present (Iface_Def)
11588 or else Protected_Present (Iface_Def)
11589 or else Synchronized_Present (Iface_Def))
11590 and then Nkind (N) /= N_Private_Extension_Declaration
11591 and then not Error_Posted (N)
11592 then
11593 Error_Msg_NE
11594 ("progenitor & must be limited interface",
11595 Error_Node, Iface_Id);
11596 end if;
11598 -- Protected interfaces can only inherit from limited, synchronized
11599 -- or protected interfaces.
11601 elsif Nkind (N) = N_Full_Type_Declaration
11602 and then Protected_Present (Type_Def)
11603 then
11604 if Limited_Present (Iface_Def)
11605 or else Synchronized_Present (Iface_Def)
11606 or else Protected_Present (Iface_Def)
11607 then
11608 null;
11610 elsif Task_Present (Iface_Def) then
11611 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
11612 & "from task interface", Error_Node);
11614 else
11615 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
11616 & "from non-limited interface", Error_Node);
11617 end if;
11619 -- Ada 2005 (AI-345): Synchronized interfaces can only inherit from
11620 -- limited and synchronized.
11622 elsif Synchronized_Present (Type_Def) then
11623 if Limited_Present (Iface_Def)
11624 or else Synchronized_Present (Iface_Def)
11625 then
11626 null;
11628 elsif Protected_Present (Iface_Def)
11629 and then Nkind (N) /= N_Private_Extension_Declaration
11630 then
11631 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11632 & "from protected interface", Error_Node);
11634 elsif Task_Present (Iface_Def)
11635 and then Nkind (N) /= N_Private_Extension_Declaration
11636 then
11637 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11638 & "from task interface", Error_Node);
11640 elsif not Is_Limited_Interface (Iface_Id) then
11641 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11642 & "from non-limited interface", Error_Node);
11643 end if;
11645 -- Ada 2005 (AI-345): Task interfaces can only inherit from limited,
11646 -- synchronized or task interfaces.
11648 elsif Nkind (N) = N_Full_Type_Declaration
11649 and then Task_Present (Type_Def)
11650 then
11651 if Limited_Present (Iface_Def)
11652 or else Synchronized_Present (Iface_Def)
11653 or else Task_Present (Iface_Def)
11654 then
11655 null;
11657 elsif Protected_Present (Iface_Def) then
11658 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
11659 & "protected interface", Error_Node);
11661 else
11662 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
11663 & "non-limited interface", Error_Node);
11664 end if;
11665 end if;
11666 end Check_Ifaces;
11668 -- Start of processing for Check_Interfaces
11670 begin
11671 if Is_Interface (Parent_Type) then
11672 if Is_Task_Interface (Parent_Type) then
11673 Is_Task := True;
11675 elsif Is_Protected_Interface (Parent_Type) then
11676 Is_Protected := True;
11677 end if;
11678 end if;
11680 if Nkind (N) = N_Private_Extension_Declaration then
11682 -- Check that progenitors are compatible with declaration
11684 Iface := First (Interface_List (Def));
11685 while Present (Iface) loop
11686 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
11688 Parent_Node := Parent (Base_Type (Iface_Typ));
11689 Iface_Def := Type_Definition (Parent_Node);
11691 if not Is_Interface (Iface_Typ) then
11692 Diagnose_Interface (Iface, Iface_Typ);
11693 else
11694 Check_Ifaces (Iface_Def, 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;
11705 return;
11706 end if;
11708 -- Full type declaration of derived type.
11709 -- Check compatibility with parent if it is interface type
11711 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
11712 and then Is_Interface (Parent_Type)
11713 then
11714 Parent_Node := Parent (Parent_Type);
11716 -- More detailed checks for interface varieties
11718 Check_Ifaces
11719 (Iface_Def => Type_Definition (Parent_Node),
11720 Error_Node => Subtype_Indication (Type_Definition (N)));
11721 end if;
11723 Iface := First (Interface_List (Def));
11724 while Present (Iface) loop
11725 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
11727 Parent_Node := Parent (Base_Type (Iface_Typ));
11728 Iface_Def := Type_Definition (Parent_Node);
11730 if not Is_Interface (Iface_Typ) then
11731 Diagnose_Interface (Iface, Iface_Typ);
11733 else
11734 -- "The declaration of a specific descendant of an interface
11735 -- type freezes the interface type" RM 13.14
11737 Freeze_Before (N, Iface_Typ);
11738 Check_Ifaces (Iface_Def, Error_Node => Iface);
11739 end if;
11741 Next (Iface);
11742 end loop;
11744 if Is_Task and Is_Protected then
11745 Error_Msg_N
11746 ("type cannot derive from task and protected interface", N);
11747 end if;
11748 end Check_Interfaces;
11750 ------------------------------------
11751 -- Check_Or_Process_Discriminants --
11752 ------------------------------------
11754 -- If an incomplete or private type declaration was already given for the
11755 -- type, the discriminants may have already been processed if they were
11756 -- present on the incomplete declaration. In this case a full conformance
11757 -- check has been performed in Find_Type_Name, and we then recheck here
11758 -- some properties that can't be checked on the partial view alone.
11759 -- Otherwise we call Process_Discriminants.
11761 procedure Check_Or_Process_Discriminants
11762 (N : Node_Id;
11763 T : Entity_Id;
11764 Prev : Entity_Id := Empty)
11766 begin
11767 if Has_Discriminants (T) then
11769 -- Discriminants are already set on T if they were already present
11770 -- on the partial view. Make them visible to component declarations.
11772 declare
11773 D : Entity_Id;
11774 -- Discriminant on T (full view) referencing expr on partial view
11776 Prev_D : Entity_Id;
11777 -- Entity of corresponding discriminant on partial view
11779 New_D : Node_Id;
11780 -- Discriminant specification for full view, expression is
11781 -- the syntactic copy on full view (which has been checked for
11782 -- conformance with partial view), only used here to post error
11783 -- message.
11785 begin
11786 D := First_Discriminant (T);
11787 New_D := First (Discriminant_Specifications (N));
11788 while Present (D) loop
11789 Prev_D := Current_Entity (D);
11790 Set_Current_Entity (D);
11791 Set_Is_Immediately_Visible (D);
11792 Set_Homonym (D, Prev_D);
11794 -- Handle the case where there is an untagged partial view and
11795 -- the full view is tagged: must disallow discriminants with
11796 -- defaults, unless compiling for Ada 2012, which allows a
11797 -- limited tagged type to have defaulted discriminants (see
11798 -- AI05-0214). However, suppress error here if it was already
11799 -- reported on the default expression of the partial view.
11801 if Is_Tagged_Type (T)
11802 and then Present (Expression (Parent (D)))
11803 and then (not Is_Limited_Type (Current_Scope)
11804 or else Ada_Version < Ada_2012)
11805 and then not Error_Posted (Expression (Parent (D)))
11806 then
11807 if Ada_Version >= Ada_2012 then
11808 Error_Msg_N
11809 ("discriminants of nonlimited tagged type cannot have "
11810 & "defaults",
11811 Expression (New_D));
11812 else
11813 Error_Msg_N
11814 ("discriminants of tagged type cannot have defaults",
11815 Expression (New_D));
11816 end if;
11817 end if;
11819 -- Ada 2005 (AI-230): Access discriminant allowed in
11820 -- non-limited record types.
11822 if Ada_Version < Ada_2005 then
11824 -- This restriction gets applied to the full type here. It
11825 -- has already been applied earlier to the partial view.
11827 Check_Access_Discriminant_Requires_Limited (Parent (D), N);
11828 end if;
11830 Next_Discriminant (D);
11831 Next (New_D);
11832 end loop;
11833 end;
11835 elsif Present (Discriminant_Specifications (N)) then
11836 Process_Discriminants (N, Prev);
11837 end if;
11838 end Check_Or_Process_Discriminants;
11840 ----------------------
11841 -- Check_Real_Bound --
11842 ----------------------
11844 procedure Check_Real_Bound (Bound : Node_Id) is
11845 begin
11846 if not Is_Real_Type (Etype (Bound)) then
11847 Error_Msg_N
11848 ("bound in real type definition must be of real type", Bound);
11850 elsif not Is_OK_Static_Expression (Bound) then
11851 Flag_Non_Static_Expr
11852 ("non-static expression used for real type bound!", Bound);
11854 else
11855 return;
11856 end if;
11858 Rewrite
11859 (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
11860 Analyze (Bound);
11861 Resolve (Bound, Standard_Float);
11862 end Check_Real_Bound;
11864 ------------------------------
11865 -- Complete_Private_Subtype --
11866 ------------------------------
11868 procedure Complete_Private_Subtype
11869 (Priv : Entity_Id;
11870 Full : Entity_Id;
11871 Full_Base : Entity_Id;
11872 Related_Nod : Node_Id)
11874 Save_Next_Entity : Entity_Id;
11875 Save_Homonym : Entity_Id;
11877 begin
11878 -- Set semantic attributes for (implicit) private subtype completion.
11879 -- If the full type has no discriminants, then it is a copy of the
11880 -- full view of the base. Otherwise, it is a subtype of the base with
11881 -- a possible discriminant constraint. Save and restore the original
11882 -- Next_Entity field of full to ensure that the calls to Copy_Node do
11883 -- not corrupt the entity chain.
11885 -- Note that the type of the full view is the same entity as the type
11886 -- of the partial view. In this fashion, the subtype has access to the
11887 -- correct view of the parent.
11889 Save_Next_Entity := Next_Entity (Full);
11890 Save_Homonym := Homonym (Priv);
11892 case Ekind (Full_Base) is
11893 when Class_Wide_Kind
11894 | Private_Kind
11895 | Protected_Kind
11896 | Task_Kind
11897 | E_Record_Subtype
11898 | E_Record_Type
11900 Copy_Node (Priv, Full);
11902 Set_Has_Discriminants
11903 (Full, Has_Discriminants (Full_Base));
11904 Set_Has_Unknown_Discriminants
11905 (Full, Has_Unknown_Discriminants (Full_Base));
11906 Set_First_Entity (Full, First_Entity (Full_Base));
11907 Set_Last_Entity (Full, Last_Entity (Full_Base));
11909 -- If the underlying base type is constrained, we know that the
11910 -- full view of the subtype is constrained as well (the converse
11911 -- is not necessarily true).
11913 if Is_Constrained (Full_Base) then
11914 Set_Is_Constrained (Full);
11915 end if;
11917 when others =>
11918 Copy_Node (Full_Base, Full);
11920 Set_Chars (Full, Chars (Priv));
11921 Conditional_Delay (Full, Priv);
11922 Set_Sloc (Full, Sloc (Priv));
11923 end case;
11925 Set_Next_Entity (Full, Save_Next_Entity);
11926 Set_Homonym (Full, Save_Homonym);
11927 Set_Associated_Node_For_Itype (Full, Related_Nod);
11929 -- Set common attributes for all subtypes: kind, convention, etc.
11931 Set_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
11932 Set_Convention (Full, Convention (Full_Base));
11934 -- The Etype of the full view is inconsistent. Gigi needs to see the
11935 -- structural full view, which is what the current scheme gives: the
11936 -- Etype of the full view is the etype of the full base. However, if the
11937 -- full base is a derived type, the full view then looks like a subtype
11938 -- of the parent, not a subtype of the full base. If instead we write:
11940 -- Set_Etype (Full, Full_Base);
11942 -- then we get inconsistencies in the front-end (confusion between
11943 -- views). Several outstanding bugs are related to this ???
11945 Set_Is_First_Subtype (Full, False);
11946 Set_Scope (Full, Scope (Priv));
11947 Set_Size_Info (Full, Full_Base);
11948 Set_RM_Size (Full, RM_Size (Full_Base));
11949 Set_Is_Itype (Full);
11951 -- A subtype of a private-type-without-discriminants, whose full-view
11952 -- has discriminants with default expressions, is not constrained.
11954 if not Has_Discriminants (Priv) then
11955 Set_Is_Constrained (Full, Is_Constrained (Full_Base));
11957 if Has_Discriminants (Full_Base) then
11958 Set_Discriminant_Constraint
11959 (Full, Discriminant_Constraint (Full_Base));
11961 -- The partial view may have been indefinite, the full view
11962 -- might not be.
11964 Set_Has_Unknown_Discriminants
11965 (Full, Has_Unknown_Discriminants (Full_Base));
11966 end if;
11967 end if;
11969 Set_First_Rep_Item (Full, First_Rep_Item (Full_Base));
11970 Set_Depends_On_Private (Full, Has_Private_Component (Full));
11972 -- Freeze the private subtype entity if its parent is delayed, and not
11973 -- already frozen. We skip this processing if the type is an anonymous
11974 -- subtype of a record component, or is the corresponding record of a
11975 -- protected type, since these are processed when the enclosing type
11976 -- is frozen. If the parent type is declared in a nested package then
11977 -- the freezing of the private and full views also happens later.
11979 if not Is_Type (Scope (Full)) then
11980 if Is_Itype (Priv)
11981 and then In_Same_Source_Unit (Full, Full_Base)
11982 and then Scope (Full_Base) /= Scope (Full)
11983 then
11984 Set_Has_Delayed_Freeze (Full);
11985 Set_Has_Delayed_Freeze (Priv);
11987 else
11988 Set_Has_Delayed_Freeze (Full,
11989 Has_Delayed_Freeze (Full_Base)
11990 and then not Is_Frozen (Full_Base));
11991 end if;
11992 end if;
11994 Set_Freeze_Node (Full, Empty);
11995 Set_Is_Frozen (Full, False);
11996 Set_Full_View (Priv, Full);
11998 if Has_Discriminants (Full) then
11999 Set_Stored_Constraint_From_Discriminant_Constraint (Full);
12000 Set_Stored_Constraint (Priv, Stored_Constraint (Full));
12002 if Has_Unknown_Discriminants (Full) then
12003 Set_Discriminant_Constraint (Full, No_Elist);
12004 end if;
12005 end if;
12007 if Ekind (Full_Base) = E_Record_Type
12008 and then Has_Discriminants (Full_Base)
12009 and then Has_Discriminants (Priv) -- might not, if errors
12010 and then not Has_Unknown_Discriminants (Priv)
12011 and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
12012 then
12013 Create_Constrained_Components
12014 (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
12016 -- If the full base is itself derived from private, build a congruent
12017 -- subtype of its underlying type, for use by the back end. For a
12018 -- constrained record component, the declaration cannot be placed on
12019 -- the component list, but it must nevertheless be built an analyzed, to
12020 -- supply enough information for Gigi to compute the size of component.
12022 elsif Ekind (Full_Base) in Private_Kind
12023 and then Is_Derived_Type (Full_Base)
12024 and then Has_Discriminants (Full_Base)
12025 and then (Ekind (Current_Scope) /= E_Record_Subtype)
12026 then
12027 if not Is_Itype (Priv)
12028 and then
12029 Nkind (Subtype_Indication (Parent (Priv))) = N_Subtype_Indication
12030 then
12031 Build_Underlying_Full_View
12032 (Parent (Priv), Full, Etype (Full_Base));
12034 elsif Nkind (Related_Nod) = N_Component_Declaration then
12035 Build_Underlying_Full_View (Related_Nod, Full, Etype (Full_Base));
12036 end if;
12038 elsif Is_Record_Type (Full_Base) then
12040 -- Show Full is simply a renaming of Full_Base
12042 Set_Cloned_Subtype (Full, Full_Base);
12043 end if;
12045 -- It is unsafe to share the bounds of a scalar type, because the Itype
12046 -- is elaborated on demand, and if a bound is non-static then different
12047 -- orders of elaboration in different units will lead to different
12048 -- external symbols.
12050 if Is_Scalar_Type (Full_Base) then
12051 Set_Scalar_Range (Full,
12052 Make_Range (Sloc (Related_Nod),
12053 Low_Bound =>
12054 Duplicate_Subexpr_No_Checks (Type_Low_Bound (Full_Base)),
12055 High_Bound =>
12056 Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
12058 -- This completion inherits the bounds of the full parent, but if
12059 -- the parent is an unconstrained floating point type, so is the
12060 -- completion.
12062 if Is_Floating_Point_Type (Full_Base) then
12063 Set_Includes_Infinities
12064 (Scalar_Range (Full), Has_Infinities (Full_Base));
12065 end if;
12066 end if;
12068 -- ??? It seems that a lot of fields are missing that should be copied
12069 -- from Full_Base to Full. Here are some that are introduced in a
12070 -- non-disruptive way but a cleanup is necessary.
12072 if Is_Tagged_Type (Full_Base) then
12073 Set_Is_Tagged_Type (Full);
12074 Set_Direct_Primitive_Operations
12075 (Full, Direct_Primitive_Operations (Full_Base));
12076 Set_No_Tagged_Streams_Pragma
12077 (Full, No_Tagged_Streams_Pragma (Full_Base));
12079 -- Inherit class_wide type of full_base in case the partial view was
12080 -- not tagged. Otherwise it has already been created when the private
12081 -- subtype was analyzed.
12083 if No (Class_Wide_Type (Full)) then
12084 Set_Class_Wide_Type (Full, Class_Wide_Type (Full_Base));
12085 end if;
12087 -- If this is a subtype of a protected or task type, constrain its
12088 -- corresponding record, unless this is a subtype without constraints,
12089 -- i.e. a simple renaming as with an actual subtype in an instance.
12091 elsif Is_Concurrent_Type (Full_Base) then
12092 if Has_Discriminants (Full)
12093 and then Present (Corresponding_Record_Type (Full_Base))
12094 and then
12095 not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
12096 then
12097 Set_Corresponding_Record_Type (Full,
12098 Constrain_Corresponding_Record
12099 (Full, Corresponding_Record_Type (Full_Base), Related_Nod));
12101 else
12102 Set_Corresponding_Record_Type (Full,
12103 Corresponding_Record_Type (Full_Base));
12104 end if;
12105 end if;
12107 -- Link rep item chain, and also setting of Has_Predicates from private
12108 -- subtype to full subtype, since we will need these on the full subtype
12109 -- to create the predicate function. Note that the full subtype may
12110 -- already have rep items, inherited from the full view of the base
12111 -- type, so we must be sure not to overwrite these entries.
12113 declare
12114 Append : Boolean;
12115 Item : Node_Id;
12116 Next_Item : Node_Id;
12117 Priv_Item : Node_Id;
12119 begin
12120 Item := First_Rep_Item (Full);
12121 Priv_Item := First_Rep_Item (Priv);
12123 -- If no existing rep items on full type, we can just link directly
12124 -- to the list of items on the private type, if any exist.. Same if
12125 -- the rep items are only those inherited from the base
12127 if (No (Item)
12128 or else Nkind (Item) /= N_Aspect_Specification
12129 or else Entity (Item) = Full_Base)
12130 and then Present (First_Rep_Item (Priv))
12131 then
12132 Set_First_Rep_Item (Full, Priv_Item);
12134 -- Otherwise, search to the end of items currently linked to the full
12135 -- subtype and append the private items to the end. However, if Priv
12136 -- and Full already have the same list of rep items, then the append
12137 -- is not done, as that would create a circularity.
12139 -- The partial view may have a predicate and the rep item lists of
12140 -- both views agree when inherited from the same ancestor. In that
12141 -- case, simply propagate the list from one view to the other.
12142 -- A more complex analysis needed here ???
12144 elsif Present (Priv_Item)
12145 and then Item = Next_Rep_Item (Priv_Item)
12146 then
12147 Set_First_Rep_Item (Full, Priv_Item);
12149 elsif Item /= Priv_Item then
12150 Append := True;
12151 loop
12152 Next_Item := Next_Rep_Item (Item);
12153 exit when No (Next_Item);
12154 Item := Next_Item;
12156 -- If the private view has aspect specifications, the full view
12157 -- inherits them. Since these aspects may already have been
12158 -- attached to the full view during derivation, do not append
12159 -- them if already present.
12161 if Item = First_Rep_Item (Priv) then
12162 Append := False;
12163 exit;
12164 end if;
12165 end loop;
12167 -- And link the private type items at the end of the chain
12169 if Append then
12170 Set_Next_Rep_Item (Item, First_Rep_Item (Priv));
12171 end if;
12172 end if;
12173 end;
12175 -- Make sure Has_Predicates is set on full type if it is set on the
12176 -- private type. Note that it may already be set on the full type and
12177 -- if so, we don't want to unset it. Similarly, propagate information
12178 -- about delayed aspects, because the corresponding pragmas must be
12179 -- analyzed when one of the views is frozen. This last step is needed
12180 -- in particular when the full type is a scalar type for which an
12181 -- anonymous base type is constructed.
12183 -- The predicate functions are generated either at the freeze point
12184 -- of the type or at the end of the visible part, and we must avoid
12185 -- generating them twice.
12187 if Has_Predicates (Priv) then
12188 Set_Has_Predicates (Full);
12190 if Present (Predicate_Function (Priv))
12191 and then No (Predicate_Function (Full))
12192 then
12193 Set_Predicate_Function (Full, Predicate_Function (Priv));
12194 end if;
12195 end if;
12197 if Has_Delayed_Aspects (Priv) then
12198 Set_Has_Delayed_Aspects (Full);
12199 end if;
12200 end Complete_Private_Subtype;
12202 ----------------------------
12203 -- Constant_Redeclaration --
12204 ----------------------------
12206 procedure Constant_Redeclaration
12207 (Id : Entity_Id;
12208 N : Node_Id;
12209 T : out Entity_Id)
12211 Prev : constant Entity_Id := Current_Entity_In_Scope (Id);
12212 Obj_Def : constant Node_Id := Object_Definition (N);
12213 New_T : Entity_Id;
12215 procedure Check_Possible_Deferred_Completion
12216 (Prev_Id : Entity_Id;
12217 Prev_Obj_Def : Node_Id;
12218 Curr_Obj_Def : Node_Id);
12219 -- Determine whether the two object definitions describe the partial
12220 -- and the full view of a constrained deferred constant. Generate
12221 -- a subtype for the full view and verify that it statically matches
12222 -- the subtype of the partial view.
12224 procedure Check_Recursive_Declaration (Typ : Entity_Id);
12225 -- If deferred constant is an access type initialized with an allocator,
12226 -- check whether there is an illegal recursion in the definition,
12227 -- through a default value of some record subcomponent. This is normally
12228 -- detected when generating init procs, but requires this additional
12229 -- mechanism when expansion is disabled.
12231 ----------------------------------------
12232 -- Check_Possible_Deferred_Completion --
12233 ----------------------------------------
12235 procedure Check_Possible_Deferred_Completion
12236 (Prev_Id : Entity_Id;
12237 Prev_Obj_Def : Node_Id;
12238 Curr_Obj_Def : Node_Id)
12240 begin
12241 if Nkind (Prev_Obj_Def) = N_Subtype_Indication
12242 and then Present (Constraint (Prev_Obj_Def))
12243 and then Nkind (Curr_Obj_Def) = N_Subtype_Indication
12244 and then Present (Constraint (Curr_Obj_Def))
12245 then
12246 declare
12247 Loc : constant Source_Ptr := Sloc (N);
12248 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
12249 Decl : constant Node_Id :=
12250 Make_Subtype_Declaration (Loc,
12251 Defining_Identifier => Def_Id,
12252 Subtype_Indication =>
12253 Relocate_Node (Curr_Obj_Def));
12255 begin
12256 Insert_Before_And_Analyze (N, Decl);
12257 Set_Etype (Id, Def_Id);
12259 if not Subtypes_Statically_Match (Etype (Prev_Id), Def_Id) then
12260 Error_Msg_Sloc := Sloc (Prev_Id);
12261 Error_Msg_N ("subtype does not statically match deferred "
12262 & "declaration #", N);
12263 end if;
12264 end;
12265 end if;
12266 end Check_Possible_Deferred_Completion;
12268 ---------------------------------
12269 -- Check_Recursive_Declaration --
12270 ---------------------------------
12272 procedure Check_Recursive_Declaration (Typ : Entity_Id) is
12273 Comp : Entity_Id;
12275 begin
12276 if Is_Record_Type (Typ) then
12277 Comp := First_Component (Typ);
12278 while Present (Comp) loop
12279 if Comes_From_Source (Comp) then
12280 if Present (Expression (Parent (Comp)))
12281 and then Is_Entity_Name (Expression (Parent (Comp)))
12282 and then Entity (Expression (Parent (Comp))) = Prev
12283 then
12284 Error_Msg_Sloc := Sloc (Parent (Comp));
12285 Error_Msg_NE
12286 ("illegal circularity with declaration for & #",
12287 N, Comp);
12288 return;
12290 elsif Is_Record_Type (Etype (Comp)) then
12291 Check_Recursive_Declaration (Etype (Comp));
12292 end if;
12293 end if;
12295 Next_Component (Comp);
12296 end loop;
12297 end if;
12298 end Check_Recursive_Declaration;
12300 -- Start of processing for Constant_Redeclaration
12302 begin
12303 if Nkind (Parent (Prev)) = N_Object_Declaration then
12304 if Nkind (Object_Definition
12305 (Parent (Prev))) = N_Subtype_Indication
12306 then
12307 -- Find type of new declaration. The constraints of the two
12308 -- views must match statically, but there is no point in
12309 -- creating an itype for the full view.
12311 if Nkind (Obj_Def) = N_Subtype_Indication then
12312 Find_Type (Subtype_Mark (Obj_Def));
12313 New_T := Entity (Subtype_Mark (Obj_Def));
12315 else
12316 Find_Type (Obj_Def);
12317 New_T := Entity (Obj_Def);
12318 end if;
12320 T := Etype (Prev);
12322 else
12323 -- The full view may impose a constraint, even if the partial
12324 -- view does not, so construct the subtype.
12326 New_T := Find_Type_Of_Object (Obj_Def, N);
12327 T := New_T;
12328 end if;
12330 else
12331 -- Current declaration is illegal, diagnosed below in Enter_Name
12333 T := Empty;
12334 New_T := Any_Type;
12335 end if;
12337 -- If previous full declaration or a renaming declaration exists, or if
12338 -- a homograph is present, let Enter_Name handle it, either with an
12339 -- error or with the removal of an overridden implicit subprogram.
12340 -- The previous one is a full declaration if it has an expression
12341 -- (which in the case of an aggregate is indicated by the Init flag).
12343 if Ekind (Prev) /= E_Constant
12344 or else Nkind (Parent (Prev)) = N_Object_Renaming_Declaration
12345 or else Present (Expression (Parent (Prev)))
12346 or else Has_Init_Expression (Parent (Prev))
12347 or else Present (Full_View (Prev))
12348 then
12349 Enter_Name (Id);
12351 -- Verify that types of both declarations match, or else that both types
12352 -- are anonymous access types whose designated subtypes statically match
12353 -- (as allowed in Ada 2005 by AI-385).
12355 elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
12356 and then
12357 (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
12358 or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
12359 or else Is_Access_Constant (Etype (New_T)) /=
12360 Is_Access_Constant (Etype (Prev))
12361 or else Can_Never_Be_Null (Etype (New_T)) /=
12362 Can_Never_Be_Null (Etype (Prev))
12363 or else Null_Exclusion_Present (Parent (Prev)) /=
12364 Null_Exclusion_Present (Parent (Id))
12365 or else not Subtypes_Statically_Match
12366 (Designated_Type (Etype (Prev)),
12367 Designated_Type (Etype (New_T))))
12368 then
12369 Error_Msg_Sloc := Sloc (Prev);
12370 Error_Msg_N ("type does not match declaration#", N);
12371 Set_Full_View (Prev, Id);
12372 Set_Etype (Id, Any_Type);
12374 -- A deferred constant whose type is an anonymous array is always
12375 -- illegal (unless imported). A detailed error message might be
12376 -- helpful for Ada beginners.
12378 if Nkind (Object_Definition (Parent (Prev)))
12379 = N_Constrained_Array_Definition
12380 and then Nkind (Object_Definition (N))
12381 = N_Constrained_Array_Definition
12382 then
12383 Error_Msg_N ("\each anonymous array is a distinct type", N);
12384 Error_Msg_N ("a deferred constant must have a named type",
12385 Object_Definition (Parent (Prev)));
12386 end if;
12388 elsif
12389 Null_Exclusion_Present (Parent (Prev))
12390 and then not Null_Exclusion_Present (N)
12391 then
12392 Error_Msg_Sloc := Sloc (Prev);
12393 Error_Msg_N ("null-exclusion does not match declaration#", N);
12394 Set_Full_View (Prev, Id);
12395 Set_Etype (Id, Any_Type);
12397 -- If so, process the full constant declaration
12399 else
12400 -- RM 7.4 (6): If the subtype defined by the subtype_indication in
12401 -- the deferred declaration is constrained, then the subtype defined
12402 -- by the subtype_indication in the full declaration shall match it
12403 -- statically.
12405 Check_Possible_Deferred_Completion
12406 (Prev_Id => Prev,
12407 Prev_Obj_Def => Object_Definition (Parent (Prev)),
12408 Curr_Obj_Def => Obj_Def);
12410 Set_Full_View (Prev, Id);
12411 Set_Is_Public (Id, Is_Public (Prev));
12412 Set_Is_Internal (Id);
12413 Append_Entity (Id, Current_Scope);
12415 -- Check ALIASED present if present before (RM 7.4(7))
12417 if Is_Aliased (Prev)
12418 and then not Aliased_Present (N)
12419 then
12420 Error_Msg_Sloc := Sloc (Prev);
12421 Error_Msg_N ("ALIASED required (see declaration #)", N);
12422 end if;
12424 -- Check that placement is in private part and that the incomplete
12425 -- declaration appeared in the visible part.
12427 if Ekind (Current_Scope) = E_Package
12428 and then not In_Private_Part (Current_Scope)
12429 then
12430 Error_Msg_Sloc := Sloc (Prev);
12431 Error_Msg_N
12432 ("full constant for declaration # must be in private part", N);
12434 elsif Ekind (Current_Scope) = E_Package
12435 and then
12436 List_Containing (Parent (Prev)) /=
12437 Visible_Declarations (Package_Specification (Current_Scope))
12438 then
12439 Error_Msg_N
12440 ("deferred constant must be declared in visible part",
12441 Parent (Prev));
12442 end if;
12444 if Is_Access_Type (T)
12445 and then Nkind (Expression (N)) = N_Allocator
12446 then
12447 Check_Recursive_Declaration (Designated_Type (T));
12448 end if;
12450 -- A deferred constant is a visible entity. If type has invariants,
12451 -- verify that the initial value satisfies them.
12453 if Has_Invariants (T) and then Present (Invariant_Procedure (T)) then
12454 Insert_After (N,
12455 Make_Invariant_Call (New_Occurrence_Of (Prev, Sloc (N))));
12456 end if;
12457 end if;
12458 end Constant_Redeclaration;
12460 ----------------------
12461 -- Constrain_Access --
12462 ----------------------
12464 procedure Constrain_Access
12465 (Def_Id : in out Entity_Id;
12466 S : Node_Id;
12467 Related_Nod : Node_Id)
12469 T : constant Entity_Id := Entity (Subtype_Mark (S));
12470 Desig_Type : constant Entity_Id := Designated_Type (T);
12471 Desig_Subtype : Entity_Id := Create_Itype (E_Void, Related_Nod);
12472 Constraint_OK : Boolean := True;
12474 begin
12475 if Is_Array_Type (Desig_Type) then
12476 Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
12478 elsif (Is_Record_Type (Desig_Type)
12479 or else Is_Incomplete_Or_Private_Type (Desig_Type))
12480 and then not Is_Constrained (Desig_Type)
12481 then
12482 -- ??? The following code is a temporary bypass to ignore a
12483 -- discriminant constraint on access type if it is constraining
12484 -- the current record. Avoid creating the implicit subtype of the
12485 -- record we are currently compiling since right now, we cannot
12486 -- handle these. For now, just return the access type itself.
12488 if Desig_Type = Current_Scope
12489 and then No (Def_Id)
12490 then
12491 Set_Ekind (Desig_Subtype, E_Record_Subtype);
12492 Def_Id := Entity (Subtype_Mark (S));
12494 -- This call added to ensure that the constraint is analyzed
12495 -- (needed for a B test). Note that we still return early from
12496 -- this procedure to avoid recursive processing. ???
12498 Constrain_Discriminated_Type
12499 (Desig_Subtype, S, Related_Nod, For_Access => True);
12500 return;
12501 end if;
12503 -- Enforce rule that the constraint is illegal if there is an
12504 -- unconstrained view of the designated type. This means that the
12505 -- partial view (either a private type declaration or a derivation
12506 -- from a private type) has no discriminants. (Defect Report
12507 -- 8652/0008, Technical Corrigendum 1, checked by ACATS B371001).
12509 -- Rule updated for Ada 2005: The private type is said to have
12510 -- a constrained partial view, given that objects of the type
12511 -- can be declared. Furthermore, the rule applies to all access
12512 -- types, unlike the rule concerning default discriminants (see
12513 -- RM 3.7.1(7/3))
12515 if (Ekind (T) = E_General_Access_Type or else Ada_Version >= Ada_2005)
12516 and then Has_Private_Declaration (Desig_Type)
12517 and then In_Open_Scopes (Scope (Desig_Type))
12518 and then Has_Discriminants (Desig_Type)
12519 then
12520 declare
12521 Pack : constant Node_Id :=
12522 Unit_Declaration_Node (Scope (Desig_Type));
12523 Decls : List_Id;
12524 Decl : Node_Id;
12526 begin
12527 if Nkind (Pack) = N_Package_Declaration then
12528 Decls := Visible_Declarations (Specification (Pack));
12529 Decl := First (Decls);
12530 while Present (Decl) loop
12531 if (Nkind (Decl) = N_Private_Type_Declaration
12532 and then Chars (Defining_Identifier (Decl)) =
12533 Chars (Desig_Type))
12535 or else
12536 (Nkind (Decl) = N_Full_Type_Declaration
12537 and then
12538 Chars (Defining_Identifier (Decl)) =
12539 Chars (Desig_Type)
12540 and then Is_Derived_Type (Desig_Type)
12541 and then
12542 Has_Private_Declaration (Etype (Desig_Type)))
12543 then
12544 if No (Discriminant_Specifications (Decl)) then
12545 Error_Msg_N
12546 ("cannot constrain access type if designated "
12547 & "type has constrained partial view", S);
12548 end if;
12550 exit;
12551 end if;
12553 Next (Decl);
12554 end loop;
12555 end if;
12556 end;
12557 end if;
12559 Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
12560 For_Access => True);
12562 elsif Is_Concurrent_Type (Desig_Type)
12563 and then not Is_Constrained (Desig_Type)
12564 then
12565 Constrain_Concurrent (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
12567 else
12568 Error_Msg_N ("invalid constraint on access type", S);
12570 -- We simply ignore an invalid constraint
12572 Desig_Subtype := Desig_Type;
12573 Constraint_OK := False;
12574 end if;
12576 if No (Def_Id) then
12577 Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
12578 else
12579 Set_Ekind (Def_Id, E_Access_Subtype);
12580 end if;
12582 if Constraint_OK then
12583 Set_Etype (Def_Id, Base_Type (T));
12585 if Is_Private_Type (Desig_Type) then
12586 Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
12587 end if;
12588 else
12589 Set_Etype (Def_Id, Any_Type);
12590 end if;
12592 Set_Size_Info (Def_Id, T);
12593 Set_Is_Constrained (Def_Id, Constraint_OK);
12594 Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
12595 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
12596 Set_Is_Access_Constant (Def_Id, Is_Access_Constant (T));
12598 Conditional_Delay (Def_Id, T);
12600 -- AI-363 : Subtypes of general access types whose designated types have
12601 -- default discriminants are disallowed. In instances, the rule has to
12602 -- be checked against the actual, of which T is the subtype. In a
12603 -- generic body, the rule is checked assuming that the actual type has
12604 -- defaulted discriminants.
12606 if Ada_Version >= Ada_2005 or else Warn_On_Ada_2005_Compatibility then
12607 if Ekind (Base_Type (T)) = E_General_Access_Type
12608 and then Has_Defaulted_Discriminants (Desig_Type)
12609 then
12610 if Ada_Version < Ada_2005 then
12611 Error_Msg_N
12612 ("access subtype of general access type would not " &
12613 "be allowed in Ada 2005?y?", S);
12614 else
12615 Error_Msg_N
12616 ("access subtype of general access type not allowed", S);
12617 end if;
12619 Error_Msg_N ("\discriminants have defaults", S);
12621 elsif Is_Access_Type (T)
12622 and then Is_Generic_Type (Desig_Type)
12623 and then Has_Discriminants (Desig_Type)
12624 and then In_Package_Body (Current_Scope)
12625 then
12626 if Ada_Version < Ada_2005 then
12627 Error_Msg_N
12628 ("access subtype would not be allowed in generic body "
12629 & "in Ada 2005?y?", S);
12630 else
12631 Error_Msg_N
12632 ("access subtype not allowed in generic body", S);
12633 end if;
12635 Error_Msg_N
12636 ("\designated type is a discriminated formal", S);
12637 end if;
12638 end if;
12639 end Constrain_Access;
12641 ---------------------
12642 -- Constrain_Array --
12643 ---------------------
12645 procedure Constrain_Array
12646 (Def_Id : in out Entity_Id;
12647 SI : Node_Id;
12648 Related_Nod : Node_Id;
12649 Related_Id : Entity_Id;
12650 Suffix : Character)
12652 C : constant Node_Id := Constraint (SI);
12653 Number_Of_Constraints : Nat := 0;
12654 Index : Node_Id;
12655 S, T : Entity_Id;
12656 Constraint_OK : Boolean := True;
12658 begin
12659 T := Entity (Subtype_Mark (SI));
12661 if Is_Access_Type (T) then
12662 T := Designated_Type (T);
12663 end if;
12665 -- If an index constraint follows a subtype mark in a subtype indication
12666 -- then the type or subtype denoted by the subtype mark must not already
12667 -- impose an index constraint. The subtype mark must denote either an
12668 -- unconstrained array type or an access type whose designated type
12669 -- is such an array type... (RM 3.6.1)
12671 if Is_Constrained (T) then
12672 Error_Msg_N ("array type is already constrained", Subtype_Mark (SI));
12673 Constraint_OK := False;
12675 else
12676 S := First (Constraints (C));
12677 while Present (S) loop
12678 Number_Of_Constraints := Number_Of_Constraints + 1;
12679 Next (S);
12680 end loop;
12682 -- In either case, the index constraint must provide a discrete
12683 -- range for each index of the array type and the type of each
12684 -- discrete range must be the same as that of the corresponding
12685 -- index. (RM 3.6.1)
12687 if Number_Of_Constraints /= Number_Dimensions (T) then
12688 Error_Msg_NE ("incorrect number of index constraints for }", C, T);
12689 Constraint_OK := False;
12691 else
12692 S := First (Constraints (C));
12693 Index := First_Index (T);
12694 Analyze (Index);
12696 -- Apply constraints to each index type
12698 for J in 1 .. Number_Of_Constraints loop
12699 Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
12700 Next (Index);
12701 Next (S);
12702 end loop;
12704 end if;
12705 end if;
12707 if No (Def_Id) then
12708 Def_Id :=
12709 Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
12710 Set_Parent (Def_Id, Related_Nod);
12712 else
12713 Set_Ekind (Def_Id, E_Array_Subtype);
12714 end if;
12716 Set_Size_Info (Def_Id, (T));
12717 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
12718 Set_Etype (Def_Id, Base_Type (T));
12720 if Constraint_OK then
12721 Set_First_Index (Def_Id, First (Constraints (C)));
12722 else
12723 Set_First_Index (Def_Id, First_Index (T));
12724 end if;
12726 Set_Is_Constrained (Def_Id, True);
12727 Set_Is_Aliased (Def_Id, Is_Aliased (T));
12728 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
12730 Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
12731 Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
12733 -- A subtype does not inherit the Packed_Array_Impl_Type of is parent.
12734 -- We need to initialize the attribute because if Def_Id is previously
12735 -- analyzed through a limited_with clause, it will have the attributes
12736 -- of an incomplete type, one of which is an Elist that overlaps the
12737 -- Packed_Array_Impl_Type field.
12739 Set_Packed_Array_Impl_Type (Def_Id, Empty);
12741 -- Build a freeze node if parent still needs one. Also make sure that
12742 -- the Depends_On_Private status is set because the subtype will need
12743 -- reprocessing at the time the base type does, and also we must set a
12744 -- conditional delay.
12746 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
12747 Conditional_Delay (Def_Id, T);
12748 end Constrain_Array;
12750 ------------------------------
12751 -- Constrain_Component_Type --
12752 ------------------------------
12754 function Constrain_Component_Type
12755 (Comp : Entity_Id;
12756 Constrained_Typ : Entity_Id;
12757 Related_Node : Node_Id;
12758 Typ : Entity_Id;
12759 Constraints : Elist_Id) return Entity_Id
12761 Loc : constant Source_Ptr := Sloc (Constrained_Typ);
12762 Compon_Type : constant Entity_Id := Etype (Comp);
12764 function Build_Constrained_Array_Type
12765 (Old_Type : Entity_Id) return Entity_Id;
12766 -- If Old_Type is an array type, one of whose indexes is constrained
12767 -- by a discriminant, build an Itype whose constraint replaces the
12768 -- discriminant with its value in the constraint.
12770 function Build_Constrained_Discriminated_Type
12771 (Old_Type : Entity_Id) return Entity_Id;
12772 -- Ditto for record components
12774 function Build_Constrained_Access_Type
12775 (Old_Type : Entity_Id) return Entity_Id;
12776 -- Ditto for access types. Makes use of previous two functions, to
12777 -- constrain designated type.
12779 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id;
12780 -- T is an array or discriminated type, C is a list of constraints
12781 -- that apply to T. This routine builds the constrained subtype.
12783 function Is_Discriminant (Expr : Node_Id) return Boolean;
12784 -- Returns True if Expr is a discriminant
12786 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id;
12787 -- Find the value of discriminant Discrim in Constraint
12789 -----------------------------------
12790 -- Build_Constrained_Access_Type --
12791 -----------------------------------
12793 function Build_Constrained_Access_Type
12794 (Old_Type : Entity_Id) return Entity_Id
12796 Desig_Type : constant Entity_Id := Designated_Type (Old_Type);
12797 Itype : Entity_Id;
12798 Desig_Subtype : Entity_Id;
12799 Scop : Entity_Id;
12801 begin
12802 -- if the original access type was not embedded in the enclosing
12803 -- type definition, there is no need to produce a new access
12804 -- subtype. In fact every access type with an explicit constraint
12805 -- generates an itype whose scope is the enclosing record.
12807 if not Is_Type (Scope (Old_Type)) then
12808 return Old_Type;
12810 elsif Is_Array_Type (Desig_Type) then
12811 Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
12813 elsif Has_Discriminants (Desig_Type) then
12815 -- This may be an access type to an enclosing record type for
12816 -- which we are constructing the constrained components. Return
12817 -- the enclosing record subtype. This is not always correct,
12818 -- but avoids infinite recursion. ???
12820 Desig_Subtype := Any_Type;
12822 for J in reverse 0 .. Scope_Stack.Last loop
12823 Scop := Scope_Stack.Table (J).Entity;
12825 if Is_Type (Scop)
12826 and then Base_Type (Scop) = Base_Type (Desig_Type)
12827 then
12828 Desig_Subtype := Scop;
12829 end if;
12831 exit when not Is_Type (Scop);
12832 end loop;
12834 if Desig_Subtype = Any_Type then
12835 Desig_Subtype :=
12836 Build_Constrained_Discriminated_Type (Desig_Type);
12837 end if;
12839 else
12840 return Old_Type;
12841 end if;
12843 if Desig_Subtype /= Desig_Type then
12845 -- The Related_Node better be here or else we won't be able
12846 -- to attach new itypes to a node in the tree.
12848 pragma Assert (Present (Related_Node));
12850 Itype := Create_Itype (E_Access_Subtype, Related_Node);
12852 Set_Etype (Itype, Base_Type (Old_Type));
12853 Set_Size_Info (Itype, (Old_Type));
12854 Set_Directly_Designated_Type (Itype, Desig_Subtype);
12855 Set_Depends_On_Private (Itype, Has_Private_Component
12856 (Old_Type));
12857 Set_Is_Access_Constant (Itype, Is_Access_Constant
12858 (Old_Type));
12860 -- The new itype needs freezing when it depends on a not frozen
12861 -- type and the enclosing subtype needs freezing.
12863 if Has_Delayed_Freeze (Constrained_Typ)
12864 and then not Is_Frozen (Constrained_Typ)
12865 then
12866 Conditional_Delay (Itype, Base_Type (Old_Type));
12867 end if;
12869 return Itype;
12871 else
12872 return Old_Type;
12873 end if;
12874 end Build_Constrained_Access_Type;
12876 ----------------------------------
12877 -- Build_Constrained_Array_Type --
12878 ----------------------------------
12880 function Build_Constrained_Array_Type
12881 (Old_Type : Entity_Id) return Entity_Id
12883 Lo_Expr : Node_Id;
12884 Hi_Expr : Node_Id;
12885 Old_Index : Node_Id;
12886 Range_Node : Node_Id;
12887 Constr_List : List_Id;
12889 Need_To_Create_Itype : Boolean := False;
12891 begin
12892 Old_Index := First_Index (Old_Type);
12893 while Present (Old_Index) loop
12894 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
12896 if Is_Discriminant (Lo_Expr)
12897 or else
12898 Is_Discriminant (Hi_Expr)
12899 then
12900 Need_To_Create_Itype := True;
12901 end if;
12903 Next_Index (Old_Index);
12904 end loop;
12906 if Need_To_Create_Itype then
12907 Constr_List := New_List;
12909 Old_Index := First_Index (Old_Type);
12910 while Present (Old_Index) loop
12911 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
12913 if Is_Discriminant (Lo_Expr) then
12914 Lo_Expr := Get_Discr_Value (Lo_Expr);
12915 end if;
12917 if Is_Discriminant (Hi_Expr) then
12918 Hi_Expr := Get_Discr_Value (Hi_Expr);
12919 end if;
12921 Range_Node :=
12922 Make_Range
12923 (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
12925 Append (Range_Node, To => Constr_List);
12927 Next_Index (Old_Index);
12928 end loop;
12930 return Build_Subtype (Old_Type, Constr_List);
12932 else
12933 return Old_Type;
12934 end if;
12935 end Build_Constrained_Array_Type;
12937 ------------------------------------------
12938 -- Build_Constrained_Discriminated_Type --
12939 ------------------------------------------
12941 function Build_Constrained_Discriminated_Type
12942 (Old_Type : Entity_Id) return Entity_Id
12944 Expr : Node_Id;
12945 Constr_List : List_Id;
12946 Old_Constraint : Elmt_Id;
12948 Need_To_Create_Itype : Boolean := False;
12950 begin
12951 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
12952 while Present (Old_Constraint) loop
12953 Expr := Node (Old_Constraint);
12955 if Is_Discriminant (Expr) then
12956 Need_To_Create_Itype := True;
12957 end if;
12959 Next_Elmt (Old_Constraint);
12960 end loop;
12962 if Need_To_Create_Itype then
12963 Constr_List := New_List;
12965 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
12966 while Present (Old_Constraint) loop
12967 Expr := Node (Old_Constraint);
12969 if Is_Discriminant (Expr) then
12970 Expr := Get_Discr_Value (Expr);
12971 end if;
12973 Append (New_Copy_Tree (Expr), To => Constr_List);
12975 Next_Elmt (Old_Constraint);
12976 end loop;
12978 return Build_Subtype (Old_Type, Constr_List);
12980 else
12981 return Old_Type;
12982 end if;
12983 end Build_Constrained_Discriminated_Type;
12985 -------------------
12986 -- Build_Subtype --
12987 -------------------
12989 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id is
12990 Indic : Node_Id;
12991 Subtyp_Decl : Node_Id;
12992 Def_Id : Entity_Id;
12993 Btyp : Entity_Id := Base_Type (T);
12995 begin
12996 -- The Related_Node better be here or else we won't be able to
12997 -- attach new itypes to a node in the tree.
12999 pragma Assert (Present (Related_Node));
13001 -- If the view of the component's type is incomplete or private
13002 -- with unknown discriminants, then the constraint must be applied
13003 -- to the full type.
13005 if Has_Unknown_Discriminants (Btyp)
13006 and then Present (Underlying_Type (Btyp))
13007 then
13008 Btyp := Underlying_Type (Btyp);
13009 end if;
13011 Indic :=
13012 Make_Subtype_Indication (Loc,
13013 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
13014 Constraint => Make_Index_Or_Discriminant_Constraint (Loc, C));
13016 Def_Id := Create_Itype (Ekind (T), Related_Node);
13018 Subtyp_Decl :=
13019 Make_Subtype_Declaration (Loc,
13020 Defining_Identifier => Def_Id,
13021 Subtype_Indication => Indic);
13023 Set_Parent (Subtyp_Decl, Parent (Related_Node));
13025 -- Itypes must be analyzed with checks off (see package Itypes)
13027 Analyze (Subtyp_Decl, Suppress => All_Checks);
13029 return Def_Id;
13030 end Build_Subtype;
13032 ---------------------
13033 -- Get_Discr_Value --
13034 ---------------------
13036 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id is
13037 D : Entity_Id;
13038 E : Elmt_Id;
13040 begin
13041 -- The discriminant may be declared for the type, in which case we
13042 -- find it by iterating over the list of discriminants. If the
13043 -- discriminant is inherited from a parent type, it appears as the
13044 -- corresponding discriminant of the current type. This will be the
13045 -- case when constraining an inherited component whose constraint is
13046 -- given by a discriminant of the parent.
13048 D := First_Discriminant (Typ);
13049 E := First_Elmt (Constraints);
13051 while Present (D) loop
13052 if D = Entity (Discrim)
13053 or else D = CR_Discriminant (Entity (Discrim))
13054 or else Corresponding_Discriminant (D) = Entity (Discrim)
13055 then
13056 return Node (E);
13057 end if;
13059 Next_Discriminant (D);
13060 Next_Elmt (E);
13061 end loop;
13063 -- The Corresponding_Discriminant mechanism is incomplete, because
13064 -- the correspondence between new and old discriminants is not one
13065 -- to one: one new discriminant can constrain several old ones. In
13066 -- that case, scan sequentially the stored_constraint, the list of
13067 -- discriminants of the parents, and the constraints.
13069 -- Previous code checked for the present of the Stored_Constraint
13070 -- list for the derived type, but did not use it at all. Should it
13071 -- be present when the component is a discriminated task type?
13073 if Is_Derived_Type (Typ)
13074 and then Scope (Entity (Discrim)) = Etype (Typ)
13075 then
13076 D := First_Discriminant (Etype (Typ));
13077 E := First_Elmt (Constraints);
13078 while Present (D) loop
13079 if D = Entity (Discrim) then
13080 return Node (E);
13081 end if;
13083 Next_Discriminant (D);
13084 Next_Elmt (E);
13085 end loop;
13086 end if;
13088 -- Something is wrong if we did not find the value
13090 raise Program_Error;
13091 end Get_Discr_Value;
13093 ---------------------
13094 -- Is_Discriminant --
13095 ---------------------
13097 function Is_Discriminant (Expr : Node_Id) return Boolean is
13098 Discrim_Scope : Entity_Id;
13100 begin
13101 if Denotes_Discriminant (Expr) then
13102 Discrim_Scope := Scope (Entity (Expr));
13104 -- Either we have a reference to one of Typ's discriminants,
13106 pragma Assert (Discrim_Scope = Typ
13108 -- or to the discriminants of the parent type, in the case
13109 -- of a derivation of a tagged type with variants.
13111 or else Discrim_Scope = Etype (Typ)
13112 or else Full_View (Discrim_Scope) = Etype (Typ)
13114 -- or same as above for the case where the discriminants
13115 -- were declared in Typ's private view.
13117 or else (Is_Private_Type (Discrim_Scope)
13118 and then Chars (Discrim_Scope) = Chars (Typ))
13120 -- or else we are deriving from the full view and the
13121 -- discriminant is declared in the private entity.
13123 or else (Is_Private_Type (Typ)
13124 and then Chars (Discrim_Scope) = Chars (Typ))
13126 -- Or we are constrained the corresponding record of a
13127 -- synchronized type that completes a private declaration.
13129 or else (Is_Concurrent_Record_Type (Typ)
13130 and then
13131 Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
13133 -- or we have a class-wide type, in which case make sure the
13134 -- discriminant found belongs to the root type.
13136 or else (Is_Class_Wide_Type (Typ)
13137 and then Etype (Typ) = Discrim_Scope));
13139 return True;
13140 end if;
13142 -- In all other cases we have something wrong
13144 return False;
13145 end Is_Discriminant;
13147 -- Start of processing for Constrain_Component_Type
13149 begin
13150 if Nkind (Parent (Comp)) = N_Component_Declaration
13151 and then Comes_From_Source (Parent (Comp))
13152 and then Comes_From_Source
13153 (Subtype_Indication (Component_Definition (Parent (Comp))))
13154 and then
13155 Is_Entity_Name
13156 (Subtype_Indication (Component_Definition (Parent (Comp))))
13157 then
13158 return Compon_Type;
13160 elsif Is_Array_Type (Compon_Type) then
13161 return Build_Constrained_Array_Type (Compon_Type);
13163 elsif Has_Discriminants (Compon_Type) then
13164 return Build_Constrained_Discriminated_Type (Compon_Type);
13166 elsif Is_Access_Type (Compon_Type) then
13167 return Build_Constrained_Access_Type (Compon_Type);
13169 else
13170 return Compon_Type;
13171 end if;
13172 end Constrain_Component_Type;
13174 --------------------------
13175 -- Constrain_Concurrent --
13176 --------------------------
13178 -- For concurrent types, the associated record value type carries the same
13179 -- discriminants, so when we constrain a concurrent type, we must constrain
13180 -- the corresponding record type as well.
13182 procedure Constrain_Concurrent
13183 (Def_Id : in out Entity_Id;
13184 SI : Node_Id;
13185 Related_Nod : Node_Id;
13186 Related_Id : Entity_Id;
13187 Suffix : Character)
13189 -- Retrieve Base_Type to ensure getting to the concurrent type in the
13190 -- case of a private subtype (needed when only doing semantic analysis).
13192 T_Ent : Entity_Id := Base_Type (Entity (Subtype_Mark (SI)));
13193 T_Val : Entity_Id;
13195 begin
13196 if Is_Access_Type (T_Ent) then
13197 T_Ent := Designated_Type (T_Ent);
13198 end if;
13200 T_Val := Corresponding_Record_Type (T_Ent);
13202 if Present (T_Val) then
13204 if No (Def_Id) then
13205 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
13207 -- Elaborate itype now, as it may be used in a subsequent
13208 -- synchronized operation in another scope.
13210 if Nkind (Related_Nod) = N_Full_Type_Declaration then
13211 Build_Itype_Reference (Def_Id, Related_Nod);
13212 end if;
13213 end if;
13215 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
13217 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13218 Set_Corresponding_Record_Type (Def_Id,
13219 Constrain_Corresponding_Record (Def_Id, T_Val, Related_Nod));
13221 else
13222 -- If there is no associated record, expansion is disabled and this
13223 -- is a generic context. Create a subtype in any case, so that
13224 -- semantic analysis can proceed.
13226 if No (Def_Id) then
13227 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
13228 end if;
13230 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
13231 end if;
13232 end Constrain_Concurrent;
13234 ------------------------------------
13235 -- Constrain_Corresponding_Record --
13236 ------------------------------------
13238 function Constrain_Corresponding_Record
13239 (Prot_Subt : Entity_Id;
13240 Corr_Rec : Entity_Id;
13241 Related_Nod : Node_Id) return Entity_Id
13243 T_Sub : constant Entity_Id :=
13244 Create_Itype (E_Record_Subtype, Related_Nod, Corr_Rec, 'C');
13246 begin
13247 Set_Etype (T_Sub, Corr_Rec);
13248 Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
13249 Set_Is_Constrained (T_Sub, True);
13250 Set_First_Entity (T_Sub, First_Entity (Corr_Rec));
13251 Set_Last_Entity (T_Sub, Last_Entity (Corr_Rec));
13253 if Has_Discriminants (Prot_Subt) then -- False only if errors.
13254 Set_Discriminant_Constraint
13255 (T_Sub, Discriminant_Constraint (Prot_Subt));
13256 Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
13257 Create_Constrained_Components
13258 (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
13259 end if;
13261 Set_Depends_On_Private (T_Sub, Has_Private_Component (T_Sub));
13263 if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
13264 Conditional_Delay (T_Sub, Corr_Rec);
13266 else
13267 -- This is a component subtype: it will be frozen in the context of
13268 -- the enclosing record's init_proc, so that discriminant references
13269 -- are resolved to discriminals. (Note: we used to skip freezing
13270 -- altogether in that case, which caused errors downstream for
13271 -- components of a bit packed array type).
13273 Set_Has_Delayed_Freeze (T_Sub);
13274 end if;
13276 return T_Sub;
13277 end Constrain_Corresponding_Record;
13279 -----------------------
13280 -- Constrain_Decimal --
13281 -----------------------
13283 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id) is
13284 T : constant Entity_Id := Entity (Subtype_Mark (S));
13285 C : constant Node_Id := Constraint (S);
13286 Loc : constant Source_Ptr := Sloc (C);
13287 Range_Expr : Node_Id;
13288 Digits_Expr : Node_Id;
13289 Digits_Val : Uint;
13290 Bound_Val : Ureal;
13292 begin
13293 Set_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
13295 if Nkind (C) = N_Range_Constraint then
13296 Range_Expr := Range_Expression (C);
13297 Digits_Val := Digits_Value (T);
13299 else
13300 pragma Assert (Nkind (C) = N_Digits_Constraint);
13302 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
13304 Digits_Expr := Digits_Expression (C);
13305 Analyze_And_Resolve (Digits_Expr, Any_Integer);
13307 Check_Digits_Expression (Digits_Expr);
13308 Digits_Val := Expr_Value (Digits_Expr);
13310 if Digits_Val > Digits_Value (T) then
13311 Error_Msg_N
13312 ("digits expression is incompatible with subtype", C);
13313 Digits_Val := Digits_Value (T);
13314 end if;
13316 if Present (Range_Constraint (C)) then
13317 Range_Expr := Range_Expression (Range_Constraint (C));
13318 else
13319 Range_Expr := Empty;
13320 end if;
13321 end if;
13323 Set_Etype (Def_Id, Base_Type (T));
13324 Set_Size_Info (Def_Id, (T));
13325 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13326 Set_Delta_Value (Def_Id, Delta_Value (T));
13327 Set_Scale_Value (Def_Id, Scale_Value (T));
13328 Set_Small_Value (Def_Id, Small_Value (T));
13329 Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
13330 Set_Digits_Value (Def_Id, Digits_Val);
13332 -- Manufacture range from given digits value if no range present
13334 if No (Range_Expr) then
13335 Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
13336 Range_Expr :=
13337 Make_Range (Loc,
13338 Low_Bound =>
13339 Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
13340 High_Bound =>
13341 Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
13342 end if;
13344 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
13345 Set_Discrete_RM_Size (Def_Id);
13347 -- Unconditionally delay the freeze, since we cannot set size
13348 -- information in all cases correctly until the freeze point.
13350 Set_Has_Delayed_Freeze (Def_Id);
13351 end Constrain_Decimal;
13353 ----------------------------------
13354 -- Constrain_Discriminated_Type --
13355 ----------------------------------
13357 procedure Constrain_Discriminated_Type
13358 (Def_Id : Entity_Id;
13359 S : Node_Id;
13360 Related_Nod : Node_Id;
13361 For_Access : Boolean := False)
13363 E : Entity_Id := Entity (Subtype_Mark (S));
13364 T : Entity_Id;
13366 procedure Fixup_Bad_Constraint;
13367 -- Called after finding a bad constraint, and after having posted an
13368 -- appropriate error message. The goal is to leave type Def_Id in as
13369 -- reasonable state as possible.
13371 --------------------------
13372 -- Fixup_Bad_Constraint --
13373 --------------------------
13375 procedure Fixup_Bad_Constraint is
13376 begin
13377 -- Set a reasonable Ekind for the entity. For an incomplete type,
13378 -- we can't do much, but for other types, we can set the proper
13379 -- corresponding subtype kind.
13381 if Ekind (T) = E_Incomplete_Type then
13382 Set_Ekind (Def_Id, Ekind (T));
13383 else
13384 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
13385 end if;
13387 -- Set Etype to the known type, to reduce chances of cascaded errors
13389 Set_Etype (Def_Id, E);
13390 Set_Error_Posted (Def_Id);
13391 end Fixup_Bad_Constraint;
13393 -- Local variables
13395 C : Node_Id;
13396 Constr : Elist_Id := New_Elmt_List;
13398 -- Start of processing for Constrain_Discriminated_Type
13400 begin
13401 C := Constraint (S);
13403 -- A discriminant constraint is only allowed in a subtype indication,
13404 -- after a subtype mark. This subtype mark must denote either a type
13405 -- with discriminants, or an access type whose designated type is a
13406 -- type with discriminants. A discriminant constraint specifies the
13407 -- values of these discriminants (RM 3.7.2(5)).
13409 T := Base_Type (Entity (Subtype_Mark (S)));
13411 if Is_Access_Type (T) then
13412 T := Designated_Type (T);
13413 end if;
13415 -- In an instance it may be necessary to retrieve the full view of a
13416 -- type with unknown discriminants, or a full view with defaulted
13417 -- discriminants. In other contexts the constraint is illegal.
13419 if In_Instance
13420 and then Is_Private_Type (T)
13421 and then Present (Full_View (T))
13422 and then
13423 (Has_Unknown_Discriminants (T)
13424 or else
13425 (not Has_Discriminants (T)
13426 and then Has_Discriminants (Full_View (T))
13427 and then Present (Discriminant_Default_Value
13428 (First_Discriminant (Full_View (T))))))
13429 then
13430 T := Full_View (T);
13431 E := Full_View (E);
13432 end if;
13434 -- Ada 2005 (AI-412): Constrained incomplete subtypes are illegal. Avoid
13435 -- generating an error for access-to-incomplete subtypes.
13437 if Ada_Version >= Ada_2005
13438 and then Ekind (T) = E_Incomplete_Type
13439 and then Nkind (Parent (S)) = N_Subtype_Declaration
13440 and then not Is_Itype (Def_Id)
13441 then
13442 -- A little sanity check: emit an error message if the type has
13443 -- discriminants to begin with. Type T may be a regular incomplete
13444 -- type or imported via a limited with clause.
13446 if Has_Discriminants (T)
13447 or else (From_Limited_With (T)
13448 and then Present (Non_Limited_View (T))
13449 and then Nkind (Parent (Non_Limited_View (T))) =
13450 N_Full_Type_Declaration
13451 and then Present (Discriminant_Specifications
13452 (Parent (Non_Limited_View (T)))))
13453 then
13454 Error_Msg_N
13455 ("(Ada 2005) incomplete subtype may not be constrained", C);
13456 else
13457 Error_Msg_N ("invalid constraint: type has no discriminant", C);
13458 end if;
13460 Fixup_Bad_Constraint;
13461 return;
13463 -- Check that the type has visible discriminants. The type may be
13464 -- a private type with unknown discriminants whose full view has
13465 -- discriminants which are invisible.
13467 elsif not Has_Discriminants (T)
13468 or else
13469 (Has_Unknown_Discriminants (T)
13470 and then Is_Private_Type (T))
13471 then
13472 Error_Msg_N ("invalid constraint: type has no discriminant", C);
13473 Fixup_Bad_Constraint;
13474 return;
13476 elsif Is_Constrained (E)
13477 or else (Ekind (E) = E_Class_Wide_Subtype
13478 and then Present (Discriminant_Constraint (E)))
13479 then
13480 Error_Msg_N ("type is already constrained", Subtype_Mark (S));
13481 Fixup_Bad_Constraint;
13482 return;
13483 end if;
13485 -- T may be an unconstrained subtype (e.g. a generic actual). Constraint
13486 -- applies to the base type.
13488 T := Base_Type (T);
13490 Constr := Build_Discriminant_Constraints (T, S);
13492 -- If the list returned was empty we had an error in building the
13493 -- discriminant constraint. We have also already signalled an error
13494 -- in the incomplete type case
13496 if Is_Empty_Elmt_List (Constr) then
13497 Fixup_Bad_Constraint;
13498 return;
13499 end if;
13501 Build_Discriminated_Subtype (T, Def_Id, Constr, Related_Nod, For_Access);
13502 end Constrain_Discriminated_Type;
13504 ---------------------------
13505 -- Constrain_Enumeration --
13506 ---------------------------
13508 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id) is
13509 T : constant Entity_Id := Entity (Subtype_Mark (S));
13510 C : constant Node_Id := Constraint (S);
13512 begin
13513 Set_Ekind (Def_Id, E_Enumeration_Subtype);
13515 Set_First_Literal (Def_Id, First_Literal (Base_Type (T)));
13517 Set_Etype (Def_Id, Base_Type (T));
13518 Set_Size_Info (Def_Id, (T));
13519 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13520 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
13522 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13524 Set_Discrete_RM_Size (Def_Id);
13525 end Constrain_Enumeration;
13527 ----------------------
13528 -- Constrain_Float --
13529 ----------------------
13531 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id) is
13532 T : constant Entity_Id := Entity (Subtype_Mark (S));
13533 C : Node_Id;
13534 D : Node_Id;
13535 Rais : Node_Id;
13537 begin
13538 Set_Ekind (Def_Id, E_Floating_Point_Subtype);
13540 Set_Etype (Def_Id, Base_Type (T));
13541 Set_Size_Info (Def_Id, (T));
13542 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13544 -- Process the constraint
13546 C := Constraint (S);
13548 -- Digits constraint present
13550 if Nkind (C) = N_Digits_Constraint then
13552 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
13553 Check_Restriction (No_Obsolescent_Features, C);
13555 if Warn_On_Obsolescent_Feature then
13556 Error_Msg_N
13557 ("subtype digits constraint is an " &
13558 "obsolescent feature (RM J.3(8))?j?", C);
13559 end if;
13561 D := Digits_Expression (C);
13562 Analyze_And_Resolve (D, Any_Integer);
13563 Check_Digits_Expression (D);
13564 Set_Digits_Value (Def_Id, Expr_Value (D));
13566 -- Check that digits value is in range. Obviously we can do this
13567 -- at compile time, but it is strictly a runtime check, and of
13568 -- course there is an ACVC test that checks this.
13570 if Digits_Value (Def_Id) > Digits_Value (T) then
13571 Error_Msg_Uint_1 := Digits_Value (T);
13572 Error_Msg_N ("??digits value is too large, maximum is ^", D);
13573 Rais :=
13574 Make_Raise_Constraint_Error (Sloc (D),
13575 Reason => CE_Range_Check_Failed);
13576 Insert_Action (Declaration_Node (Def_Id), Rais);
13577 end if;
13579 C := Range_Constraint (C);
13581 -- No digits constraint present
13583 else
13584 Set_Digits_Value (Def_Id, Digits_Value (T));
13585 end if;
13587 -- Range constraint present
13589 if Nkind (C) = N_Range_Constraint then
13590 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13592 -- No range constraint present
13594 else
13595 pragma Assert (No (C));
13596 Set_Scalar_Range (Def_Id, Scalar_Range (T));
13597 end if;
13599 Set_Is_Constrained (Def_Id);
13600 end Constrain_Float;
13602 ---------------------
13603 -- Constrain_Index --
13604 ---------------------
13606 procedure Constrain_Index
13607 (Index : Node_Id;
13608 S : Node_Id;
13609 Related_Nod : Node_Id;
13610 Related_Id : Entity_Id;
13611 Suffix : Character;
13612 Suffix_Index : Nat)
13614 Def_Id : Entity_Id;
13615 R : Node_Id := Empty;
13616 T : constant Entity_Id := Etype (Index);
13618 begin
13619 Def_Id :=
13620 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
13621 Set_Etype (Def_Id, Base_Type (T));
13623 if Nkind (S) = N_Range
13624 or else
13625 (Nkind (S) = N_Attribute_Reference
13626 and then Attribute_Name (S) = Name_Range)
13627 then
13628 -- A Range attribute will be transformed into N_Range by Resolve
13630 Analyze (S);
13631 Set_Etype (S, T);
13632 R := S;
13634 Process_Range_Expr_In_Decl (R, T);
13636 if not Error_Posted (S)
13637 and then
13638 (Nkind (S) /= N_Range
13639 or else not Covers (T, (Etype (Low_Bound (S))))
13640 or else not Covers (T, (Etype (High_Bound (S)))))
13641 then
13642 if Base_Type (T) /= Any_Type
13643 and then Etype (Low_Bound (S)) /= Any_Type
13644 and then Etype (High_Bound (S)) /= Any_Type
13645 then
13646 Error_Msg_N ("range expected", S);
13647 end if;
13648 end if;
13650 elsif Nkind (S) = N_Subtype_Indication then
13652 -- The parser has verified that this is a discrete indication
13654 Resolve_Discrete_Subtype_Indication (S, T);
13655 Bad_Predicated_Subtype_Use
13656 ("subtype& has predicate, not allowed in index constraint",
13657 S, Entity (Subtype_Mark (S)));
13659 R := Range_Expression (Constraint (S));
13661 -- Capture values of bounds and generate temporaries for them if
13662 -- needed, since checks may cause duplication of the expressions
13663 -- which must not be reevaluated.
13665 -- The forced evaluation removes side effects from expressions, which
13666 -- should occur also in GNATprove mode. Otherwise, we end up with
13667 -- unexpected insertions of actions at places where this is not
13668 -- supposed to occur, e.g. on default parameters of a call.
13670 if Expander_Active or GNATprove_Mode then
13671 Force_Evaluation
13672 (Low_Bound (R), Related_Id => Def_Id, Is_Low_Bound => True);
13673 Force_Evaluation
13674 (High_Bound (R), Related_Id => Def_Id, Is_High_Bound => True);
13675 end if;
13677 elsif Nkind (S) = N_Discriminant_Association then
13679 -- Syntactically valid in subtype indication
13681 Error_Msg_N ("invalid index constraint", S);
13682 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
13683 return;
13685 -- Subtype_Mark case, no anonymous subtypes to construct
13687 else
13688 Analyze (S);
13690 if Is_Entity_Name (S) then
13691 if not Is_Type (Entity (S)) then
13692 Error_Msg_N ("expect subtype mark for index constraint", S);
13694 elsif Base_Type (Entity (S)) /= Base_Type (T) then
13695 Wrong_Type (S, Base_Type (T));
13697 -- Check error of subtype with predicate in index constraint
13699 else
13700 Bad_Predicated_Subtype_Use
13701 ("subtype& has predicate, not allowed in index constraint",
13702 S, Entity (S));
13703 end if;
13705 return;
13707 else
13708 Error_Msg_N ("invalid index constraint", S);
13709 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
13710 return;
13711 end if;
13712 end if;
13714 -- Complete construction of the Itype
13716 if Is_Modular_Integer_Type (T) then
13717 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
13719 elsif Is_Integer_Type (T) then
13720 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
13722 else
13723 Set_Ekind (Def_Id, E_Enumeration_Subtype);
13724 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
13725 Set_First_Literal (Def_Id, First_Literal (T));
13726 end if;
13728 Set_Size_Info (Def_Id, (T));
13729 Set_RM_Size (Def_Id, RM_Size (T));
13730 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13732 Set_Scalar_Range (Def_Id, R);
13734 Set_Etype (S, Def_Id);
13735 Set_Discrete_RM_Size (Def_Id);
13736 end Constrain_Index;
13738 -----------------------
13739 -- Constrain_Integer --
13740 -----------------------
13742 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id) is
13743 T : constant Entity_Id := Entity (Subtype_Mark (S));
13744 C : constant Node_Id := Constraint (S);
13746 begin
13747 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13749 if Is_Modular_Integer_Type (T) then
13750 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
13751 else
13752 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
13753 end if;
13755 Set_Etype (Def_Id, Base_Type (T));
13756 Set_Size_Info (Def_Id, (T));
13757 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13758 Set_Discrete_RM_Size (Def_Id);
13759 end Constrain_Integer;
13761 ------------------------------
13762 -- Constrain_Ordinary_Fixed --
13763 ------------------------------
13765 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id) is
13766 T : constant Entity_Id := Entity (Subtype_Mark (S));
13767 C : Node_Id;
13768 D : Node_Id;
13769 Rais : Node_Id;
13771 begin
13772 Set_Ekind (Def_Id, E_Ordinary_Fixed_Point_Subtype);
13773 Set_Etype (Def_Id, Base_Type (T));
13774 Set_Size_Info (Def_Id, (T));
13775 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13776 Set_Small_Value (Def_Id, Small_Value (T));
13778 -- Process the constraint
13780 C := Constraint (S);
13782 -- Delta constraint present
13784 if Nkind (C) = N_Delta_Constraint then
13786 Check_SPARK_05_Restriction ("delta constraint is not allowed", S);
13787 Check_Restriction (No_Obsolescent_Features, C);
13789 if Warn_On_Obsolescent_Feature then
13790 Error_Msg_S
13791 ("subtype delta constraint is an " &
13792 "obsolescent feature (RM J.3(7))?j?");
13793 end if;
13795 D := Delta_Expression (C);
13796 Analyze_And_Resolve (D, Any_Real);
13797 Check_Delta_Expression (D);
13798 Set_Delta_Value (Def_Id, Expr_Value_R (D));
13800 -- Check that delta value is in range. Obviously we can do this
13801 -- at compile time, but it is strictly a runtime check, and of
13802 -- course there is an ACVC test that checks this.
13804 if Delta_Value (Def_Id) < Delta_Value (T) then
13805 Error_Msg_N ("??delta value is too small", D);
13806 Rais :=
13807 Make_Raise_Constraint_Error (Sloc (D),
13808 Reason => CE_Range_Check_Failed);
13809 Insert_Action (Declaration_Node (Def_Id), Rais);
13810 end if;
13812 C := Range_Constraint (C);
13814 -- No delta constraint present
13816 else
13817 Set_Delta_Value (Def_Id, Delta_Value (T));
13818 end if;
13820 -- Range constraint present
13822 if Nkind (C) = N_Range_Constraint then
13823 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13825 -- No range constraint present
13827 else
13828 pragma Assert (No (C));
13829 Set_Scalar_Range (Def_Id, Scalar_Range (T));
13830 end if;
13832 Set_Discrete_RM_Size (Def_Id);
13834 -- Unconditionally delay the freeze, since we cannot set size
13835 -- information in all cases correctly until the freeze point.
13837 Set_Has_Delayed_Freeze (Def_Id);
13838 end Constrain_Ordinary_Fixed;
13840 -----------------------
13841 -- Contain_Interface --
13842 -----------------------
13844 function Contain_Interface
13845 (Iface : Entity_Id;
13846 Ifaces : Elist_Id) return Boolean
13848 Iface_Elmt : Elmt_Id;
13850 begin
13851 if Present (Ifaces) then
13852 Iface_Elmt := First_Elmt (Ifaces);
13853 while Present (Iface_Elmt) loop
13854 if Node (Iface_Elmt) = Iface then
13855 return True;
13856 end if;
13858 Next_Elmt (Iface_Elmt);
13859 end loop;
13860 end if;
13862 return False;
13863 end Contain_Interface;
13865 ---------------------------
13866 -- Convert_Scalar_Bounds --
13867 ---------------------------
13869 procedure Convert_Scalar_Bounds
13870 (N : Node_Id;
13871 Parent_Type : Entity_Id;
13872 Derived_Type : Entity_Id;
13873 Loc : Source_Ptr)
13875 Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
13877 Lo : Node_Id;
13878 Hi : Node_Id;
13879 Rng : Node_Id;
13881 begin
13882 -- Defend against previous errors
13884 if No (Scalar_Range (Derived_Type)) then
13885 Check_Error_Detected;
13886 return;
13887 end if;
13889 Lo := Build_Scalar_Bound
13890 (Type_Low_Bound (Derived_Type),
13891 Parent_Type, Implicit_Base);
13893 Hi := Build_Scalar_Bound
13894 (Type_High_Bound (Derived_Type),
13895 Parent_Type, Implicit_Base);
13897 Rng :=
13898 Make_Range (Loc,
13899 Low_Bound => Lo,
13900 High_Bound => Hi);
13902 Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
13904 Set_Parent (Rng, N);
13905 Set_Scalar_Range (Derived_Type, Rng);
13907 -- Analyze the bounds
13909 Analyze_And_Resolve (Lo, Implicit_Base);
13910 Analyze_And_Resolve (Hi, Implicit_Base);
13912 -- Analyze the range itself, except that we do not analyze it if
13913 -- the bounds are real literals, and we have a fixed-point type.
13914 -- The reason for this is that we delay setting the bounds in this
13915 -- case till we know the final Small and Size values (see circuit
13916 -- in Freeze.Freeze_Fixed_Point_Type for further details).
13918 if Is_Fixed_Point_Type (Parent_Type)
13919 and then Nkind (Lo) = N_Real_Literal
13920 and then Nkind (Hi) = N_Real_Literal
13921 then
13922 return;
13924 -- Here we do the analysis of the range
13926 -- Note: we do this manually, since if we do a normal Analyze and
13927 -- Resolve call, there are problems with the conversions used for
13928 -- the derived type range.
13930 else
13931 Set_Etype (Rng, Implicit_Base);
13932 Set_Analyzed (Rng, True);
13933 end if;
13934 end Convert_Scalar_Bounds;
13936 -------------------
13937 -- Copy_And_Swap --
13938 -------------------
13940 procedure Copy_And_Swap (Priv, Full : Entity_Id) is
13941 begin
13942 -- Initialize new full declaration entity by copying the pertinent
13943 -- fields of the corresponding private declaration entity.
13945 -- We temporarily set Ekind to a value appropriate for a type to
13946 -- avoid assert failures in Einfo from checking for setting type
13947 -- attributes on something that is not a type. Ekind (Priv) is an
13948 -- appropriate choice, since it allowed the attributes to be set
13949 -- in the first place. This Ekind value will be modified later.
13951 Set_Ekind (Full, Ekind (Priv));
13953 -- Also set Etype temporarily to Any_Type, again, in the absence
13954 -- of errors, it will be properly reset, and if there are errors,
13955 -- then we want a value of Any_Type to remain.
13957 Set_Etype (Full, Any_Type);
13959 -- Now start copying attributes
13961 Set_Has_Discriminants (Full, Has_Discriminants (Priv));
13963 if Has_Discriminants (Full) then
13964 Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
13965 Set_Stored_Constraint (Full, Stored_Constraint (Priv));
13966 end if;
13968 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
13969 Set_Homonym (Full, Homonym (Priv));
13970 Set_Is_Immediately_Visible (Full, Is_Immediately_Visible (Priv));
13971 Set_Is_Public (Full, Is_Public (Priv));
13972 Set_Is_Pure (Full, Is_Pure (Priv));
13973 Set_Is_Tagged_Type (Full, Is_Tagged_Type (Priv));
13974 Set_Has_Pragma_Unmodified (Full, Has_Pragma_Unmodified (Priv));
13975 Set_Has_Pragma_Unreferenced (Full, Has_Pragma_Unreferenced (Priv));
13976 Set_Has_Pragma_Unreferenced_Objects
13977 (Full, Has_Pragma_Unreferenced_Objects
13978 (Priv));
13980 Conditional_Delay (Full, Priv);
13982 if Is_Tagged_Type (Full) then
13983 Set_Direct_Primitive_Operations
13984 (Full, Direct_Primitive_Operations (Priv));
13985 Set_No_Tagged_Streams_Pragma
13986 (Full, No_Tagged_Streams_Pragma (Priv));
13988 if Is_Base_Type (Priv) then
13989 Set_Class_Wide_Type (Full, Class_Wide_Type (Priv));
13990 end if;
13991 end if;
13993 Set_Is_Volatile (Full, Is_Volatile (Priv));
13994 Set_Treat_As_Volatile (Full, Treat_As_Volatile (Priv));
13995 Set_Scope (Full, Scope (Priv));
13996 Set_Next_Entity (Full, Next_Entity (Priv));
13997 Set_First_Entity (Full, First_Entity (Priv));
13998 Set_Last_Entity (Full, Last_Entity (Priv));
14000 -- If access types have been recorded for later handling, keep them in
14001 -- the full view so that they get handled when the full view freeze
14002 -- node is expanded.
14004 if Present (Freeze_Node (Priv))
14005 and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
14006 then
14007 Ensure_Freeze_Node (Full);
14008 Set_Access_Types_To_Process
14009 (Freeze_Node (Full),
14010 Access_Types_To_Process (Freeze_Node (Priv)));
14011 end if;
14013 -- Swap the two entities. Now Private is the full type entity and Full
14014 -- is the private one. They will be swapped back at the end of the
14015 -- private part. This swapping ensures that the entity that is visible
14016 -- in the private part is the full declaration.
14018 Exchange_Entities (Priv, Full);
14019 Append_Entity (Full, Scope (Full));
14020 end Copy_And_Swap;
14022 -------------------------------------
14023 -- Copy_Array_Base_Type_Attributes --
14024 -------------------------------------
14026 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
14027 begin
14028 Set_Component_Alignment (T1, Component_Alignment (T2));
14029 Set_Component_Type (T1, Component_Type (T2));
14030 Set_Component_Size (T1, Component_Size (T2));
14031 Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
14032 Set_Has_Non_Standard_Rep (T1, Has_Non_Standard_Rep (T2));
14033 Propagate_Concurrent_Flags (T1, T2);
14034 Set_Is_Packed (T1, Is_Packed (T2));
14035 Set_Has_Aliased_Components (T1, Has_Aliased_Components (T2));
14036 Set_Has_Atomic_Components (T1, Has_Atomic_Components (T2));
14037 Set_Has_Volatile_Components (T1, Has_Volatile_Components (T2));
14038 end Copy_Array_Base_Type_Attributes;
14040 -----------------------------------
14041 -- Copy_Array_Subtype_Attributes --
14042 -----------------------------------
14044 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
14045 begin
14046 Set_Size_Info (T1, T2);
14048 Set_First_Index (T1, First_Index (T2));
14049 Set_Is_Aliased (T1, Is_Aliased (T2));
14050 Set_Is_Volatile (T1, Is_Volatile (T2));
14051 Set_Treat_As_Volatile (T1, Treat_As_Volatile (T2));
14052 Set_Is_Constrained (T1, Is_Constrained (T2));
14053 Set_Depends_On_Private (T1, Has_Private_Component (T2));
14054 Inherit_Rep_Item_Chain (T1, T2);
14055 Set_Convention (T1, Convention (T2));
14056 Set_Is_Limited_Composite (T1, Is_Limited_Composite (T2));
14057 Set_Is_Private_Composite (T1, Is_Private_Composite (T2));
14058 Set_Packed_Array_Impl_Type (T1, Packed_Array_Impl_Type (T2));
14059 end Copy_Array_Subtype_Attributes;
14061 -----------------------------------
14062 -- Create_Constrained_Components --
14063 -----------------------------------
14065 procedure Create_Constrained_Components
14066 (Subt : Entity_Id;
14067 Decl_Node : Node_Id;
14068 Typ : Entity_Id;
14069 Constraints : Elist_Id)
14071 Loc : constant Source_Ptr := Sloc (Subt);
14072 Comp_List : constant Elist_Id := New_Elmt_List;
14073 Parent_Type : constant Entity_Id := Etype (Typ);
14074 Assoc_List : constant List_Id := New_List;
14075 Discr_Val : Elmt_Id;
14076 Errors : Boolean;
14077 New_C : Entity_Id;
14078 Old_C : Entity_Id;
14079 Is_Static : Boolean := True;
14081 procedure Collect_Fixed_Components (Typ : Entity_Id);
14082 -- Collect parent type components that do not appear in a variant part
14084 procedure Create_All_Components;
14085 -- Iterate over Comp_List to create the components of the subtype
14087 function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
14088 -- Creates a new component from Old_Compon, copying all the fields from
14089 -- it, including its Etype, inserts the new component in the Subt entity
14090 -- chain and returns the new component.
14092 function Is_Variant_Record (T : Entity_Id) return Boolean;
14093 -- If true, and discriminants are static, collect only components from
14094 -- variants selected by discriminant values.
14096 ------------------------------
14097 -- Collect_Fixed_Components --
14098 ------------------------------
14100 procedure Collect_Fixed_Components (Typ : Entity_Id) is
14101 begin
14102 -- Build association list for discriminants, and find components of the
14103 -- variant part selected by the values of the discriminants.
14105 Old_C := First_Discriminant (Typ);
14106 Discr_Val := First_Elmt (Constraints);
14107 while Present (Old_C) loop
14108 Append_To (Assoc_List,
14109 Make_Component_Association (Loc,
14110 Choices => New_List (New_Occurrence_Of (Old_C, Loc)),
14111 Expression => New_Copy (Node (Discr_Val))));
14113 Next_Elmt (Discr_Val);
14114 Next_Discriminant (Old_C);
14115 end loop;
14117 -- The tag and the possible parent component are unconditionally in
14118 -- the subtype.
14120 if Is_Tagged_Type (Typ) or else Has_Controlled_Component (Typ) then
14121 Old_C := First_Component (Typ);
14122 while Present (Old_C) loop
14123 if Nam_In (Chars (Old_C), Name_uTag, Name_uParent) then
14124 Append_Elmt (Old_C, Comp_List);
14125 end if;
14127 Next_Component (Old_C);
14128 end loop;
14129 end if;
14130 end Collect_Fixed_Components;
14132 ---------------------------
14133 -- Create_All_Components --
14134 ---------------------------
14136 procedure Create_All_Components is
14137 Comp : Elmt_Id;
14139 begin
14140 Comp := First_Elmt (Comp_List);
14141 while Present (Comp) loop
14142 Old_C := Node (Comp);
14143 New_C := Create_Component (Old_C);
14145 Set_Etype
14146 (New_C,
14147 Constrain_Component_Type
14148 (Old_C, Subt, Decl_Node, Typ, Constraints));
14149 Set_Is_Public (New_C, Is_Public (Subt));
14151 Next_Elmt (Comp);
14152 end loop;
14153 end Create_All_Components;
14155 ----------------------
14156 -- Create_Component --
14157 ----------------------
14159 function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
14160 New_Compon : constant Entity_Id := New_Copy (Old_Compon);
14162 begin
14163 if Ekind (Old_Compon) = E_Discriminant
14164 and then Is_Completely_Hidden (Old_Compon)
14165 then
14166 -- This is a shadow discriminant created for a discriminant of
14167 -- the parent type, which needs to be present in the subtype.
14168 -- Give the shadow discriminant an internal name that cannot
14169 -- conflict with that of visible components.
14171 Set_Chars (New_Compon, New_Internal_Name ('C'));
14172 end if;
14174 -- Set the parent so we have a proper link for freezing etc. This is
14175 -- not a real parent pointer, since of course our parent does not own
14176 -- up to us and reference us, we are an illegitimate child of the
14177 -- original parent.
14179 Set_Parent (New_Compon, Parent (Old_Compon));
14181 -- If the old component's Esize was already determined and is a
14182 -- static value, then the new component simply inherits it. Otherwise
14183 -- the old component's size may require run-time determination, but
14184 -- the new component's size still might be statically determinable
14185 -- (if, for example it has a static constraint). In that case we want
14186 -- Layout_Type to recompute the component's size, so we reset its
14187 -- size and positional fields.
14189 if Frontend_Layout_On_Target
14190 and then not Known_Static_Esize (Old_Compon)
14191 then
14192 Set_Esize (New_Compon, Uint_0);
14193 Init_Normalized_First_Bit (New_Compon);
14194 Init_Normalized_Position (New_Compon);
14195 Init_Normalized_Position_Max (New_Compon);
14196 end if;
14198 -- We do not want this node marked as Comes_From_Source, since
14199 -- otherwise it would get first class status and a separate cross-
14200 -- reference line would be generated. Illegitimate children do not
14201 -- rate such recognition.
14203 Set_Comes_From_Source (New_Compon, False);
14205 -- But it is a real entity, and a birth certificate must be properly
14206 -- registered by entering it into the entity list.
14208 Enter_Name (New_Compon);
14210 return New_Compon;
14211 end Create_Component;
14213 -----------------------
14214 -- Is_Variant_Record --
14215 -----------------------
14217 function Is_Variant_Record (T : Entity_Id) return Boolean is
14218 begin
14219 return Nkind (Parent (T)) = N_Full_Type_Declaration
14220 and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
14221 and then Present (Component_List (Type_Definition (Parent (T))))
14222 and then
14223 Present
14224 (Variant_Part (Component_List (Type_Definition (Parent (T)))));
14225 end Is_Variant_Record;
14227 -- Start of processing for Create_Constrained_Components
14229 begin
14230 pragma Assert (Subt /= Base_Type (Subt));
14231 pragma Assert (Typ = Base_Type (Typ));
14233 Set_First_Entity (Subt, Empty);
14234 Set_Last_Entity (Subt, Empty);
14236 -- Check whether constraint is fully static, in which case we can
14237 -- optimize the list of components.
14239 Discr_Val := First_Elmt (Constraints);
14240 while Present (Discr_Val) loop
14241 if not Is_OK_Static_Expression (Node (Discr_Val)) then
14242 Is_Static := False;
14243 exit;
14244 end if;
14246 Next_Elmt (Discr_Val);
14247 end loop;
14249 Set_Has_Static_Discriminants (Subt, Is_Static);
14251 Push_Scope (Subt);
14253 -- Inherit the discriminants of the parent type
14255 Add_Discriminants : declare
14256 Num_Disc : Nat;
14257 Num_Gird : Nat;
14259 begin
14260 Num_Disc := 0;
14261 Old_C := First_Discriminant (Typ);
14263 while Present (Old_C) loop
14264 Num_Disc := Num_Disc + 1;
14265 New_C := Create_Component (Old_C);
14266 Set_Is_Public (New_C, Is_Public (Subt));
14267 Next_Discriminant (Old_C);
14268 end loop;
14270 -- For an untagged derived subtype, the number of discriminants may
14271 -- be smaller than the number of inherited discriminants, because
14272 -- several of them may be renamed by a single new discriminant or
14273 -- constrained. In this case, add the hidden discriminants back into
14274 -- the subtype, because they need to be present if the optimizer of
14275 -- the GCC 4.x back-end decides to break apart assignments between
14276 -- objects using the parent view into member-wise assignments.
14278 Num_Gird := 0;
14280 if Is_Derived_Type (Typ)
14281 and then not Is_Tagged_Type (Typ)
14282 then
14283 Old_C := First_Stored_Discriminant (Typ);
14285 while Present (Old_C) loop
14286 Num_Gird := Num_Gird + 1;
14287 Next_Stored_Discriminant (Old_C);
14288 end loop;
14289 end if;
14291 if Num_Gird > Num_Disc then
14293 -- Find out multiple uses of new discriminants, and add hidden
14294 -- components for the extra renamed discriminants. We recognize
14295 -- multiple uses through the Corresponding_Discriminant of a
14296 -- new discriminant: if it constrains several old discriminants,
14297 -- this field points to the last one in the parent type. The
14298 -- stored discriminants of the derived type have the same name
14299 -- as those of the parent.
14301 declare
14302 Constr : Elmt_Id;
14303 New_Discr : Entity_Id;
14304 Old_Discr : Entity_Id;
14306 begin
14307 Constr := First_Elmt (Stored_Constraint (Typ));
14308 Old_Discr := First_Stored_Discriminant (Typ);
14309 while Present (Constr) loop
14310 if Is_Entity_Name (Node (Constr))
14311 and then Ekind (Entity (Node (Constr))) = E_Discriminant
14312 then
14313 New_Discr := Entity (Node (Constr));
14315 if Chars (Corresponding_Discriminant (New_Discr)) /=
14316 Chars (Old_Discr)
14317 then
14318 -- The new discriminant has been used to rename a
14319 -- subsequent old discriminant. Introduce a shadow
14320 -- component for the current old discriminant.
14322 New_C := Create_Component (Old_Discr);
14323 Set_Original_Record_Component (New_C, Old_Discr);
14324 end if;
14326 else
14327 -- The constraint has eliminated the old discriminant.
14328 -- Introduce a shadow component.
14330 New_C := Create_Component (Old_Discr);
14331 Set_Original_Record_Component (New_C, Old_Discr);
14332 end if;
14334 Next_Elmt (Constr);
14335 Next_Stored_Discriminant (Old_Discr);
14336 end loop;
14337 end;
14338 end if;
14339 end Add_Discriminants;
14341 if Is_Static
14342 and then Is_Variant_Record (Typ)
14343 then
14344 Collect_Fixed_Components (Typ);
14346 Gather_Components (
14347 Typ,
14348 Component_List (Type_Definition (Parent (Typ))),
14349 Governed_By => Assoc_List,
14350 Into => Comp_List,
14351 Report_Errors => Errors);
14352 pragma Assert (not Errors
14353 or else Serious_Errors_Detected > 0);
14355 Create_All_Components;
14357 -- If the subtype declaration is created for a tagged type derivation
14358 -- with constraints, we retrieve the record definition of the parent
14359 -- type to select the components of the proper variant.
14361 elsif Is_Static
14362 and then Is_Tagged_Type (Typ)
14363 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
14364 and then
14365 Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
14366 and then Is_Variant_Record (Parent_Type)
14367 then
14368 Collect_Fixed_Components (Typ);
14370 Gather_Components
14371 (Typ,
14372 Component_List (Type_Definition (Parent (Parent_Type))),
14373 Governed_By => Assoc_List,
14374 Into => Comp_List,
14375 Report_Errors => Errors);
14377 -- Note: previously there was a check at this point that no errors
14378 -- were detected. As a consequence of AI05-220 there may be an error
14379 -- if an inherited discriminant that controls a variant has a non-
14380 -- static constraint.
14382 -- If the tagged derivation has a type extension, collect all the
14383 -- new components therein.
14385 if Present (Record_Extension_Part (Type_Definition (Parent (Typ))))
14386 then
14387 Old_C := First_Component (Typ);
14388 while Present (Old_C) loop
14389 if Original_Record_Component (Old_C) = Old_C
14390 and then Chars (Old_C) /= Name_uTag
14391 and then Chars (Old_C) /= Name_uParent
14392 then
14393 Append_Elmt (Old_C, Comp_List);
14394 end if;
14396 Next_Component (Old_C);
14397 end loop;
14398 end if;
14400 Create_All_Components;
14402 else
14403 -- If discriminants are not static, or if this is a multi-level type
14404 -- extension, we have to include all components of the parent type.
14406 Old_C := First_Component (Typ);
14407 while Present (Old_C) loop
14408 New_C := Create_Component (Old_C);
14410 Set_Etype
14411 (New_C,
14412 Constrain_Component_Type
14413 (Old_C, Subt, Decl_Node, Typ, Constraints));
14414 Set_Is_Public (New_C, Is_Public (Subt));
14416 Next_Component (Old_C);
14417 end loop;
14418 end if;
14420 End_Scope;
14421 end Create_Constrained_Components;
14423 ------------------------------------------
14424 -- Decimal_Fixed_Point_Type_Declaration --
14425 ------------------------------------------
14427 procedure Decimal_Fixed_Point_Type_Declaration
14428 (T : Entity_Id;
14429 Def : Node_Id)
14431 Loc : constant Source_Ptr := Sloc (Def);
14432 Digs_Expr : constant Node_Id := Digits_Expression (Def);
14433 Delta_Expr : constant Node_Id := Delta_Expression (Def);
14434 Implicit_Base : Entity_Id;
14435 Digs_Val : Uint;
14436 Delta_Val : Ureal;
14437 Scale_Val : Uint;
14438 Bound_Val : Ureal;
14440 begin
14441 Check_SPARK_05_Restriction
14442 ("decimal fixed point type is not allowed", Def);
14443 Check_Restriction (No_Fixed_Point, Def);
14445 -- Create implicit base type
14447 Implicit_Base :=
14448 Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
14449 Set_Etype (Implicit_Base, Implicit_Base);
14451 -- Analyze and process delta expression
14453 Analyze_And_Resolve (Delta_Expr, Universal_Real);
14455 Check_Delta_Expression (Delta_Expr);
14456 Delta_Val := Expr_Value_R (Delta_Expr);
14458 -- Check delta is power of 10, and determine scale value from it
14460 declare
14461 Val : Ureal;
14463 begin
14464 Scale_Val := Uint_0;
14465 Val := Delta_Val;
14467 if Val < Ureal_1 then
14468 while Val < Ureal_1 loop
14469 Val := Val * Ureal_10;
14470 Scale_Val := Scale_Val + 1;
14471 end loop;
14473 if Scale_Val > 18 then
14474 Error_Msg_N ("scale exceeds maximum value of 18", Def);
14475 Scale_Val := UI_From_Int (+18);
14476 end if;
14478 else
14479 while Val > Ureal_1 loop
14480 Val := Val / Ureal_10;
14481 Scale_Val := Scale_Val - 1;
14482 end loop;
14484 if Scale_Val < -18 then
14485 Error_Msg_N ("scale is less than minimum value of -18", Def);
14486 Scale_Val := UI_From_Int (-18);
14487 end if;
14488 end if;
14490 if Val /= Ureal_1 then
14491 Error_Msg_N ("delta expression must be a power of 10", Def);
14492 Delta_Val := Ureal_10 ** (-Scale_Val);
14493 end if;
14494 end;
14496 -- Set delta, scale and small (small = delta for decimal type)
14498 Set_Delta_Value (Implicit_Base, Delta_Val);
14499 Set_Scale_Value (Implicit_Base, Scale_Val);
14500 Set_Small_Value (Implicit_Base, Delta_Val);
14502 -- Analyze and process digits expression
14504 Analyze_And_Resolve (Digs_Expr, Any_Integer);
14505 Check_Digits_Expression (Digs_Expr);
14506 Digs_Val := Expr_Value (Digs_Expr);
14508 if Digs_Val > 18 then
14509 Digs_Val := UI_From_Int (+18);
14510 Error_Msg_N ("digits value out of range, maximum is 18", Digs_Expr);
14511 end if;
14513 Set_Digits_Value (Implicit_Base, Digs_Val);
14514 Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
14516 -- Set range of base type from digits value for now. This will be
14517 -- expanded to represent the true underlying base range by Freeze.
14519 Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
14521 -- Note: We leave size as zero for now, size will be set at freeze
14522 -- time. We have to do this for ordinary fixed-point, because the size
14523 -- depends on the specified small, and we might as well do the same for
14524 -- decimal fixed-point.
14526 pragma Assert (Esize (Implicit_Base) = Uint_0);
14528 -- If there are bounds given in the declaration use them as the
14529 -- bounds of the first named subtype.
14531 if Present (Real_Range_Specification (Def)) then
14532 declare
14533 RRS : constant Node_Id := Real_Range_Specification (Def);
14534 Low : constant Node_Id := Low_Bound (RRS);
14535 High : constant Node_Id := High_Bound (RRS);
14536 Low_Val : Ureal;
14537 High_Val : Ureal;
14539 begin
14540 Analyze_And_Resolve (Low, Any_Real);
14541 Analyze_And_Resolve (High, Any_Real);
14542 Check_Real_Bound (Low);
14543 Check_Real_Bound (High);
14544 Low_Val := Expr_Value_R (Low);
14545 High_Val := Expr_Value_R (High);
14547 if Low_Val < (-Bound_Val) then
14548 Error_Msg_N
14549 ("range low bound too small for digits value", Low);
14550 Low_Val := -Bound_Val;
14551 end if;
14553 if High_Val > Bound_Val then
14554 Error_Msg_N
14555 ("range high bound too large for digits value", High);
14556 High_Val := Bound_Val;
14557 end if;
14559 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
14560 end;
14562 -- If no explicit range, use range that corresponds to given
14563 -- digits value. This will end up as the final range for the
14564 -- first subtype.
14566 else
14567 Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
14568 end if;
14570 -- Complete entity for first subtype. The inheritance of the rep item
14571 -- chain ensures that SPARK-related pragmas are not clobbered when the
14572 -- decimal fixed point type acts as a full view of a private type.
14574 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
14575 Set_Etype (T, Implicit_Base);
14576 Set_Size_Info (T, Implicit_Base);
14577 Inherit_Rep_Item_Chain (T, Implicit_Base);
14578 Set_Digits_Value (T, Digs_Val);
14579 Set_Delta_Value (T, Delta_Val);
14580 Set_Small_Value (T, Delta_Val);
14581 Set_Scale_Value (T, Scale_Val);
14582 Set_Is_Constrained (T);
14583 end Decimal_Fixed_Point_Type_Declaration;
14585 -----------------------------------
14586 -- Derive_Progenitor_Subprograms --
14587 -----------------------------------
14589 procedure Derive_Progenitor_Subprograms
14590 (Parent_Type : Entity_Id;
14591 Tagged_Type : Entity_Id)
14593 E : Entity_Id;
14594 Elmt : Elmt_Id;
14595 Iface : Entity_Id;
14596 Iface_Elmt : Elmt_Id;
14597 Iface_Subp : Entity_Id;
14598 New_Subp : Entity_Id := Empty;
14599 Prim_Elmt : Elmt_Id;
14600 Subp : Entity_Id;
14601 Typ : Entity_Id;
14603 begin
14604 pragma Assert (Ada_Version >= Ada_2005
14605 and then Is_Record_Type (Tagged_Type)
14606 and then Is_Tagged_Type (Tagged_Type)
14607 and then Has_Interfaces (Tagged_Type));
14609 -- Step 1: Transfer to the full-view primitives associated with the
14610 -- partial-view that cover interface primitives. Conceptually this
14611 -- work should be done later by Process_Full_View; done here to
14612 -- simplify its implementation at later stages. It can be safely
14613 -- done here because interfaces must be visible in the partial and
14614 -- private view (RM 7.3(7.3/2)).
14616 -- Small optimization: This work is only required if the parent may
14617 -- have entities whose Alias attribute reference an interface primitive.
14618 -- Such a situation may occur if the parent is an abstract type and the
14619 -- primitive has not been yet overridden or if the parent is a generic
14620 -- formal type covering interfaces.
14622 -- If the tagged type is not abstract, it cannot have abstract
14623 -- primitives (the only entities in the list of primitives of
14624 -- non-abstract tagged types that can reference abstract primitives
14625 -- through its Alias attribute are the internal entities that have
14626 -- attribute Interface_Alias, and these entities are generated later
14627 -- by Add_Internal_Interface_Entities).
14629 if In_Private_Part (Current_Scope)
14630 and then (Is_Abstract_Type (Parent_Type)
14631 or else
14632 Is_Generic_Type (Parent_Type))
14633 then
14634 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
14635 while Present (Elmt) loop
14636 Subp := Node (Elmt);
14638 -- At this stage it is not possible to have entities in the list
14639 -- of primitives that have attribute Interface_Alias.
14641 pragma Assert (No (Interface_Alias (Subp)));
14643 Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
14645 if Is_Interface (Typ) then
14646 E := Find_Primitive_Covering_Interface
14647 (Tagged_Type => Tagged_Type,
14648 Iface_Prim => Subp);
14650 if Present (E)
14651 and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
14652 then
14653 Replace_Elmt (Elmt, E);
14654 Remove_Homonym (Subp);
14655 end if;
14656 end if;
14658 Next_Elmt (Elmt);
14659 end loop;
14660 end if;
14662 -- Step 2: Add primitives of progenitors that are not implemented by
14663 -- parents of Tagged_Type.
14665 if Present (Interfaces (Base_Type (Tagged_Type))) then
14666 Iface_Elmt := First_Elmt (Interfaces (Base_Type (Tagged_Type)));
14667 while Present (Iface_Elmt) loop
14668 Iface := Node (Iface_Elmt);
14670 Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
14671 while Present (Prim_Elmt) loop
14672 Iface_Subp := Node (Prim_Elmt);
14674 -- Exclude derivation of predefined primitives except those
14675 -- that come from source, or are inherited from one that comes
14676 -- from source. Required to catch declarations of equality
14677 -- operators of interfaces. For example:
14679 -- type Iface is interface;
14680 -- function "=" (Left, Right : Iface) return Boolean;
14682 if not Is_Predefined_Dispatching_Operation (Iface_Subp)
14683 or else Comes_From_Source (Ultimate_Alias (Iface_Subp))
14684 then
14685 E := Find_Primitive_Covering_Interface
14686 (Tagged_Type => Tagged_Type,
14687 Iface_Prim => Iface_Subp);
14689 -- If not found we derive a new primitive leaving its alias
14690 -- attribute referencing the interface primitive.
14692 if No (E) then
14693 Derive_Subprogram
14694 (New_Subp, Iface_Subp, Tagged_Type, Iface);
14696 -- Ada 2012 (AI05-0197): If the covering primitive's name
14697 -- differs from the name of the interface primitive then it
14698 -- is a private primitive inherited from a parent type. In
14699 -- such case, given that Tagged_Type covers the interface,
14700 -- the inherited private primitive becomes visible. For such
14701 -- purpose we add a new entity that renames the inherited
14702 -- private primitive.
14704 elsif Chars (E) /= Chars (Iface_Subp) then
14705 pragma Assert (Has_Suffix (E, 'P'));
14706 Derive_Subprogram
14707 (New_Subp, Iface_Subp, Tagged_Type, Iface);
14708 Set_Alias (New_Subp, E);
14709 Set_Is_Abstract_Subprogram (New_Subp,
14710 Is_Abstract_Subprogram (E));
14712 -- Propagate to the full view interface entities associated
14713 -- with the partial view.
14715 elsif In_Private_Part (Current_Scope)
14716 and then Present (Alias (E))
14717 and then Alias (E) = Iface_Subp
14718 and then
14719 List_Containing (Parent (E)) /=
14720 Private_Declarations
14721 (Specification
14722 (Unit_Declaration_Node (Current_Scope)))
14723 then
14724 Append_Elmt (E, Primitive_Operations (Tagged_Type));
14725 end if;
14726 end if;
14728 Next_Elmt (Prim_Elmt);
14729 end loop;
14731 Next_Elmt (Iface_Elmt);
14732 end loop;
14733 end if;
14734 end Derive_Progenitor_Subprograms;
14736 -----------------------
14737 -- Derive_Subprogram --
14738 -----------------------
14740 procedure Derive_Subprogram
14741 (New_Subp : out Entity_Id;
14742 Parent_Subp : Entity_Id;
14743 Derived_Type : Entity_Id;
14744 Parent_Type : Entity_Id;
14745 Actual_Subp : Entity_Id := Empty)
14747 Formal : Entity_Id;
14748 -- Formal parameter of parent primitive operation
14750 Formal_Of_Actual : Entity_Id;
14751 -- Formal parameter of actual operation, when the derivation is to
14752 -- create a renaming for a primitive operation of an actual in an
14753 -- instantiation.
14755 New_Formal : Entity_Id;
14756 -- Formal of inherited operation
14758 Visible_Subp : Entity_Id := Parent_Subp;
14760 function Is_Private_Overriding return Boolean;
14761 -- If Subp is a private overriding of a visible operation, the inherited
14762 -- operation derives from the overridden op (even though its body is the
14763 -- overriding one) and the inherited operation is visible now. See
14764 -- sem_disp to see the full details of the handling of the overridden
14765 -- subprogram, which is removed from the list of primitive operations of
14766 -- the type. The overridden subprogram is saved locally in Visible_Subp,
14767 -- and used to diagnose abstract operations that need overriding in the
14768 -- derived type.
14770 procedure Replace_Type (Id, New_Id : Entity_Id);
14771 -- When the type is an anonymous access type, create a new access type
14772 -- designating the derived type.
14774 procedure Set_Derived_Name;
14775 -- This procedure sets the appropriate Chars name for New_Subp. This
14776 -- is normally just a copy of the parent name. An exception arises for
14777 -- type support subprograms, where the name is changed to reflect the
14778 -- name of the derived type, e.g. if type foo is derived from type bar,
14779 -- then a procedure barDA is derived with a name fooDA.
14781 ---------------------------
14782 -- Is_Private_Overriding --
14783 ---------------------------
14785 function Is_Private_Overriding return Boolean is
14786 Prev : Entity_Id;
14788 begin
14789 -- If the parent is not a dispatching operation there is no
14790 -- need to investigate overridings
14792 if not Is_Dispatching_Operation (Parent_Subp) then
14793 return False;
14794 end if;
14796 -- The visible operation that is overridden is a homonym of the
14797 -- parent subprogram. We scan the homonym chain to find the one
14798 -- whose alias is the subprogram we are deriving.
14800 Prev := Current_Entity (Parent_Subp);
14801 while Present (Prev) loop
14802 if Ekind (Prev) = Ekind (Parent_Subp)
14803 and then Alias (Prev) = Parent_Subp
14804 and then Scope (Parent_Subp) = Scope (Prev)
14805 and then not Is_Hidden (Prev)
14806 then
14807 Visible_Subp := Prev;
14808 return True;
14809 end if;
14811 Prev := Homonym (Prev);
14812 end loop;
14814 return False;
14815 end Is_Private_Overriding;
14817 ------------------
14818 -- Replace_Type --
14819 ------------------
14821 procedure Replace_Type (Id, New_Id : Entity_Id) is
14822 Id_Type : constant Entity_Id := Etype (Id);
14823 Acc_Type : Entity_Id;
14824 Par : constant Node_Id := Parent (Derived_Type);
14826 begin
14827 -- When the type is an anonymous access type, create a new access
14828 -- type designating the derived type. This itype must be elaborated
14829 -- at the point of the derivation, not on subsequent calls that may
14830 -- be out of the proper scope for Gigi, so we insert a reference to
14831 -- it after the derivation.
14833 if Ekind (Id_Type) = E_Anonymous_Access_Type then
14834 declare
14835 Desig_Typ : Entity_Id := Designated_Type (Id_Type);
14837 begin
14838 if Ekind (Desig_Typ) = E_Record_Type_With_Private
14839 and then Present (Full_View (Desig_Typ))
14840 and then not Is_Private_Type (Parent_Type)
14841 then
14842 Desig_Typ := Full_View (Desig_Typ);
14843 end if;
14845 if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
14847 -- Ada 2005 (AI-251): Handle also derivations of abstract
14848 -- interface primitives.
14850 or else (Is_Interface (Desig_Typ)
14851 and then not Is_Class_Wide_Type (Desig_Typ))
14852 then
14853 Acc_Type := New_Copy (Id_Type);
14854 Set_Etype (Acc_Type, Acc_Type);
14855 Set_Scope (Acc_Type, New_Subp);
14857 -- Set size of anonymous access type. If we have an access
14858 -- to an unconstrained array, this is a fat pointer, so it
14859 -- is sizes at twice addtress size.
14861 if Is_Array_Type (Desig_Typ)
14862 and then not Is_Constrained (Desig_Typ)
14863 then
14864 Init_Size (Acc_Type, 2 * System_Address_Size);
14866 -- Other cases use a thin pointer
14868 else
14869 Init_Size (Acc_Type, System_Address_Size);
14870 end if;
14872 -- Set remaining characterstics of anonymous access type
14874 Init_Alignment (Acc_Type);
14875 Set_Directly_Designated_Type (Acc_Type, Derived_Type);
14877 Set_Etype (New_Id, Acc_Type);
14878 Set_Scope (New_Id, New_Subp);
14880 -- Create a reference to it
14882 Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
14884 else
14885 Set_Etype (New_Id, Id_Type);
14886 end if;
14887 end;
14889 -- In Ada2012, a formal may have an incomplete type but the type
14890 -- derivation that inherits the primitive follows the full view.
14892 elsif Base_Type (Id_Type) = Base_Type (Parent_Type)
14893 or else
14894 (Ekind (Id_Type) = E_Record_Type_With_Private
14895 and then Present (Full_View (Id_Type))
14896 and then
14897 Base_Type (Full_View (Id_Type)) = Base_Type (Parent_Type))
14898 or else
14899 (Ada_Version >= Ada_2012
14900 and then Ekind (Id_Type) = E_Incomplete_Type
14901 and then Full_View (Id_Type) = Parent_Type)
14902 then
14903 -- Constraint checks on formals are generated during expansion,
14904 -- based on the signature of the original subprogram. The bounds
14905 -- of the derived type are not relevant, and thus we can use
14906 -- the base type for the formals. However, the return type may be
14907 -- used in a context that requires that the proper static bounds
14908 -- be used (a case statement, for example) and for those cases
14909 -- we must use the derived type (first subtype), not its base.
14911 -- If the derived_type_definition has no constraints, we know that
14912 -- the derived type has the same constraints as the first subtype
14913 -- of the parent, and we can also use it rather than its base,
14914 -- which can lead to more efficient code.
14916 if Etype (Id) = Parent_Type then
14917 if Is_Scalar_Type (Parent_Type)
14918 and then
14919 Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
14920 then
14921 Set_Etype (New_Id, Derived_Type);
14923 elsif Nkind (Par) = N_Full_Type_Declaration
14924 and then
14925 Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
14926 and then
14927 Is_Entity_Name
14928 (Subtype_Indication (Type_Definition (Par)))
14929 then
14930 Set_Etype (New_Id, Derived_Type);
14932 else
14933 Set_Etype (New_Id, Base_Type (Derived_Type));
14934 end if;
14936 else
14937 Set_Etype (New_Id, Base_Type (Derived_Type));
14938 end if;
14940 else
14941 Set_Etype (New_Id, Etype (Id));
14942 end if;
14943 end Replace_Type;
14945 ----------------------
14946 -- Set_Derived_Name --
14947 ----------------------
14949 procedure Set_Derived_Name is
14950 Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
14951 begin
14952 if Nm = TSS_Null then
14953 Set_Chars (New_Subp, Chars (Parent_Subp));
14954 else
14955 Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
14956 end if;
14957 end Set_Derived_Name;
14959 -- Start of processing for Derive_Subprogram
14961 begin
14962 New_Subp := New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
14963 Set_Ekind (New_Subp, Ekind (Parent_Subp));
14965 -- Check whether the inherited subprogram is a private operation that
14966 -- should be inherited but not yet made visible. Such subprograms can
14967 -- become visible at a later point (e.g., the private part of a public
14968 -- child unit) via Declare_Inherited_Private_Subprograms. If the
14969 -- following predicate is true, then this is not such a private
14970 -- operation and the subprogram simply inherits the name of the parent
14971 -- subprogram. Note the special check for the names of controlled
14972 -- operations, which are currently exempted from being inherited with
14973 -- a hidden name because they must be findable for generation of
14974 -- implicit run-time calls.
14976 if not Is_Hidden (Parent_Subp)
14977 or else Is_Internal (Parent_Subp)
14978 or else Is_Private_Overriding
14979 or else Is_Internal_Name (Chars (Parent_Subp))
14980 or else (Is_Controlled (Parent_Type)
14981 and then Nam_In (Chars (Parent_Subp), Name_Adjust,
14982 Name_Finalize,
14983 Name_Initialize))
14984 then
14985 Set_Derived_Name;
14987 -- An inherited dispatching equality will be overridden by an internally
14988 -- generated one, or by an explicit one, so preserve its name and thus
14989 -- its entry in the dispatch table. Otherwise, if Parent_Subp is a
14990 -- private operation it may become invisible if the full view has
14991 -- progenitors, and the dispatch table will be malformed.
14992 -- We check that the type is limited to handle the anomalous declaration
14993 -- of Limited_Controlled, which is derived from a non-limited type, and
14994 -- which is handled specially elsewhere as well.
14996 elsif Chars (Parent_Subp) = Name_Op_Eq
14997 and then Is_Dispatching_Operation (Parent_Subp)
14998 and then Etype (Parent_Subp) = Standard_Boolean
14999 and then not Is_Limited_Type (Etype (First_Formal (Parent_Subp)))
15000 and then
15001 Etype (First_Formal (Parent_Subp)) =
15002 Etype (Next_Formal (First_Formal (Parent_Subp)))
15003 then
15004 Set_Derived_Name;
15006 -- If parent is hidden, this can be a regular derivation if the
15007 -- parent is immediately visible in a non-instantiating context,
15008 -- or if we are in the private part of an instance. This test
15009 -- should still be refined ???
15011 -- The test for In_Instance_Not_Visible avoids inheriting the derived
15012 -- operation as a non-visible operation in cases where the parent
15013 -- subprogram might not be visible now, but was visible within the
15014 -- original generic, so it would be wrong to make the inherited
15015 -- subprogram non-visible now. (Not clear if this test is fully
15016 -- correct; are there any cases where we should declare the inherited
15017 -- operation as not visible to avoid it being overridden, e.g., when
15018 -- the parent type is a generic actual with private primitives ???)
15020 -- (they should be treated the same as other private inherited
15021 -- subprograms, but it's not clear how to do this cleanly). ???
15023 elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
15024 and then Is_Immediately_Visible (Parent_Subp)
15025 and then not In_Instance)
15026 or else In_Instance_Not_Visible
15027 then
15028 Set_Derived_Name;
15030 -- Ada 2005 (AI-251): Regular derivation if the parent subprogram
15031 -- overrides an interface primitive because interface primitives
15032 -- must be visible in the partial view of the parent (RM 7.3 (7.3/2))
15034 elsif Ada_Version >= Ada_2005
15035 and then Is_Dispatching_Operation (Parent_Subp)
15036 and then Covers_Some_Interface (Parent_Subp)
15037 then
15038 Set_Derived_Name;
15040 -- Otherwise, the type is inheriting a private operation, so enter it
15041 -- with a special name so it can't be overridden.
15043 else
15044 Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
15045 end if;
15047 Set_Parent (New_Subp, Parent (Derived_Type));
15049 if Present (Actual_Subp) then
15050 Replace_Type (Actual_Subp, New_Subp);
15051 else
15052 Replace_Type (Parent_Subp, New_Subp);
15053 end if;
15055 Conditional_Delay (New_Subp, Parent_Subp);
15057 -- If we are creating a renaming for a primitive operation of an
15058 -- actual of a generic derived type, we must examine the signature
15059 -- of the actual primitive, not that of the generic formal, which for
15060 -- example may be an interface. However the name and initial value
15061 -- of the inherited operation are those of the formal primitive.
15063 Formal := First_Formal (Parent_Subp);
15065 if Present (Actual_Subp) then
15066 Formal_Of_Actual := First_Formal (Actual_Subp);
15067 else
15068 Formal_Of_Actual := Empty;
15069 end if;
15071 while Present (Formal) loop
15072 New_Formal := New_Copy (Formal);
15074 -- Normally we do not go copying parents, but in the case of
15075 -- formals, we need to link up to the declaration (which is the
15076 -- parameter specification), and it is fine to link up to the
15077 -- original formal's parameter specification in this case.
15079 Set_Parent (New_Formal, Parent (Formal));
15080 Append_Entity (New_Formal, New_Subp);
15082 if Present (Formal_Of_Actual) then
15083 Replace_Type (Formal_Of_Actual, New_Formal);
15084 Next_Formal (Formal_Of_Actual);
15085 else
15086 Replace_Type (Formal, New_Formal);
15087 end if;
15089 Next_Formal (Formal);
15090 end loop;
15092 -- If this derivation corresponds to a tagged generic actual, then
15093 -- primitive operations rename those of the actual. Otherwise the
15094 -- primitive operations rename those of the parent type, If the parent
15095 -- renames an intrinsic operator, so does the new subprogram. We except
15096 -- concatenation, which is always properly typed, and does not get
15097 -- expanded as other intrinsic operations.
15099 if No (Actual_Subp) then
15100 if Is_Intrinsic_Subprogram (Parent_Subp) then
15101 Set_Is_Intrinsic_Subprogram (New_Subp);
15103 if Present (Alias (Parent_Subp))
15104 and then Chars (Parent_Subp) /= Name_Op_Concat
15105 then
15106 Set_Alias (New_Subp, Alias (Parent_Subp));
15107 else
15108 Set_Alias (New_Subp, Parent_Subp);
15109 end if;
15111 else
15112 Set_Alias (New_Subp, Parent_Subp);
15113 end if;
15115 else
15116 Set_Alias (New_Subp, Actual_Subp);
15117 end if;
15119 -- Derived subprograms of a tagged type must inherit the convention
15120 -- of the parent subprogram (a requirement of AI-117). Derived
15121 -- subprograms of untagged types simply get convention Ada by default.
15123 -- If the derived type is a tagged generic formal type with unknown
15124 -- discriminants, its convention is intrinsic (RM 6.3.1 (8)).
15126 -- However, if the type is derived from a generic formal, the further
15127 -- inherited subprogram has the convention of the non-generic ancestor.
15128 -- Otherwise there would be no way to override the operation.
15129 -- (This is subject to forthcoming ARG discussions).
15131 if Is_Tagged_Type (Derived_Type) then
15132 if Is_Generic_Type (Derived_Type)
15133 and then Has_Unknown_Discriminants (Derived_Type)
15134 then
15135 Set_Convention (New_Subp, Convention_Intrinsic);
15137 else
15138 if Is_Generic_Type (Parent_Type)
15139 and then Has_Unknown_Discriminants (Parent_Type)
15140 then
15141 Set_Convention (New_Subp, Convention (Alias (Parent_Subp)));
15142 else
15143 Set_Convention (New_Subp, Convention (Parent_Subp));
15144 end if;
15145 end if;
15146 end if;
15148 -- Predefined controlled operations retain their name even if the parent
15149 -- is hidden (see above), but they are not primitive operations if the
15150 -- ancestor is not visible, for example if the parent is a private
15151 -- extension completed with a controlled extension. Note that a full
15152 -- type that is controlled can break privacy: the flag Is_Controlled is
15153 -- set on both views of the type.
15155 if Is_Controlled (Parent_Type)
15156 and then Nam_In (Chars (Parent_Subp), Name_Initialize,
15157 Name_Adjust,
15158 Name_Finalize)
15159 and then Is_Hidden (Parent_Subp)
15160 and then not Is_Visibly_Controlled (Parent_Type)
15161 then
15162 Set_Is_Hidden (New_Subp);
15163 end if;
15165 Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
15166 Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
15168 if Ekind (Parent_Subp) = E_Procedure then
15169 Set_Is_Valued_Procedure
15170 (New_Subp, Is_Valued_Procedure (Parent_Subp));
15171 else
15172 Set_Has_Controlling_Result
15173 (New_Subp, Has_Controlling_Result (Parent_Subp));
15174 end if;
15176 -- No_Return must be inherited properly. If this is overridden in the
15177 -- case of a dispatching operation, then a check is made in Sem_Disp
15178 -- that the overriding operation is also No_Return (no such check is
15179 -- required for the case of non-dispatching operation.
15181 Set_No_Return (New_Subp, No_Return (Parent_Subp));
15183 -- A derived function with a controlling result is abstract. If the
15184 -- Derived_Type is a nonabstract formal generic derived type, then
15185 -- inherited operations are not abstract: the required check is done at
15186 -- instantiation time. If the derivation is for a generic actual, the
15187 -- function is not abstract unless the actual is.
15189 if Is_Generic_Type (Derived_Type)
15190 and then not Is_Abstract_Type (Derived_Type)
15191 then
15192 null;
15194 -- Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
15195 -- properties of the subprogram, as defined in RM-3.9.3(4/2-6/2).
15197 -- A subprogram subject to pragma Extensions_Visible with value False
15198 -- requires overriding if the subprogram has at least one controlling
15199 -- OUT parameter (SPARK RM 6.1.7(6)).
15201 elsif Ada_Version >= Ada_2005
15202 and then (Is_Abstract_Subprogram (Alias (New_Subp))
15203 or else (Is_Tagged_Type (Derived_Type)
15204 and then Etype (New_Subp) = Derived_Type
15205 and then not Is_Null_Extension (Derived_Type))
15206 or else (Is_Tagged_Type (Derived_Type)
15207 and then Ekind (Etype (New_Subp)) =
15208 E_Anonymous_Access_Type
15209 and then Designated_Type (Etype (New_Subp)) =
15210 Derived_Type
15211 and then not Is_Null_Extension (Derived_Type))
15212 or else (Comes_From_Source (Alias (New_Subp))
15213 and then Is_EVF_Procedure (Alias (New_Subp))))
15214 and then No (Actual_Subp)
15215 then
15216 if not Is_Tagged_Type (Derived_Type)
15217 or else Is_Abstract_Type (Derived_Type)
15218 or else Is_Abstract_Subprogram (Alias (New_Subp))
15219 then
15220 Set_Is_Abstract_Subprogram (New_Subp);
15221 else
15222 Set_Requires_Overriding (New_Subp);
15223 end if;
15225 elsif Ada_Version < Ada_2005
15226 and then (Is_Abstract_Subprogram (Alias (New_Subp))
15227 or else (Is_Tagged_Type (Derived_Type)
15228 and then Etype (New_Subp) = Derived_Type
15229 and then No (Actual_Subp)))
15230 then
15231 Set_Is_Abstract_Subprogram (New_Subp);
15233 -- AI05-0097 : an inherited operation that dispatches on result is
15234 -- abstract if the derived type is abstract, even if the parent type
15235 -- is concrete and the derived type is a null extension.
15237 elsif Has_Controlling_Result (Alias (New_Subp))
15238 and then Is_Abstract_Type (Etype (New_Subp))
15239 then
15240 Set_Is_Abstract_Subprogram (New_Subp);
15242 -- Finally, if the parent type is abstract we must verify that all
15243 -- inherited operations are either non-abstract or overridden, or that
15244 -- the derived type itself is abstract (this check is performed at the
15245 -- end of a package declaration, in Check_Abstract_Overriding). A
15246 -- private overriding in the parent type will not be visible in the
15247 -- derivation if we are not in an inner package or in a child unit of
15248 -- the parent type, in which case the abstractness of the inherited
15249 -- operation is carried to the new subprogram.
15251 elsif Is_Abstract_Type (Parent_Type)
15252 and then not In_Open_Scopes (Scope (Parent_Type))
15253 and then Is_Private_Overriding
15254 and then Is_Abstract_Subprogram (Visible_Subp)
15255 then
15256 if No (Actual_Subp) then
15257 Set_Alias (New_Subp, Visible_Subp);
15258 Set_Is_Abstract_Subprogram (New_Subp, True);
15260 else
15261 -- If this is a derivation for an instance of a formal derived
15262 -- type, abstractness comes from the primitive operation of the
15263 -- actual, not from the operation inherited from the ancestor.
15265 Set_Is_Abstract_Subprogram
15266 (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
15267 end if;
15268 end if;
15270 New_Overloaded_Entity (New_Subp, Derived_Type);
15272 -- Check for case of a derived subprogram for the instantiation of a
15273 -- formal derived tagged type, if so mark the subprogram as dispatching
15274 -- and inherit the dispatching attributes of the actual subprogram. The
15275 -- derived subprogram is effectively renaming of the actual subprogram,
15276 -- so it needs to have the same attributes as the actual.
15278 if Present (Actual_Subp)
15279 and then Is_Dispatching_Operation (Actual_Subp)
15280 then
15281 Set_Is_Dispatching_Operation (New_Subp);
15283 if Present (DTC_Entity (Actual_Subp)) then
15284 Set_DTC_Entity (New_Subp, DTC_Entity (Actual_Subp));
15285 Set_DT_Position_Value (New_Subp, DT_Position (Actual_Subp));
15286 end if;
15287 end if;
15289 -- Indicate that a derived subprogram does not require a body and that
15290 -- it does not require processing of default expressions.
15292 Set_Has_Completion (New_Subp);
15293 Set_Default_Expressions_Processed (New_Subp);
15295 if Ekind (New_Subp) = E_Function then
15296 Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
15297 end if;
15298 end Derive_Subprogram;
15300 ------------------------
15301 -- Derive_Subprograms --
15302 ------------------------
15304 procedure Derive_Subprograms
15305 (Parent_Type : Entity_Id;
15306 Derived_Type : Entity_Id;
15307 Generic_Actual : Entity_Id := Empty)
15309 Op_List : constant Elist_Id :=
15310 Collect_Primitive_Operations (Parent_Type);
15312 function Check_Derived_Type return Boolean;
15313 -- Check that all the entities derived from Parent_Type are found in
15314 -- the list of primitives of Derived_Type exactly in the same order.
15316 procedure Derive_Interface_Subprogram
15317 (New_Subp : out Entity_Id;
15318 Subp : Entity_Id;
15319 Actual_Subp : Entity_Id);
15320 -- Derive New_Subp from the ultimate alias of the parent subprogram Subp
15321 -- (which is an interface primitive). If Generic_Actual is present then
15322 -- Actual_Subp is the actual subprogram corresponding with the generic
15323 -- subprogram Subp.
15325 ------------------------
15326 -- Check_Derived_Type --
15327 ------------------------
15329 function Check_Derived_Type return Boolean is
15330 E : Entity_Id;
15331 Elmt : Elmt_Id;
15332 List : Elist_Id;
15333 New_Subp : Entity_Id;
15334 Op_Elmt : Elmt_Id;
15335 Subp : Entity_Id;
15337 begin
15338 -- Traverse list of entities in the current scope searching for
15339 -- an incomplete type whose full-view is derived type.
15341 E := First_Entity (Scope (Derived_Type));
15342 while Present (E) and then E /= Derived_Type loop
15343 if Ekind (E) = E_Incomplete_Type
15344 and then Present (Full_View (E))
15345 and then Full_View (E) = Derived_Type
15346 then
15347 -- Disable this test if Derived_Type completes an incomplete
15348 -- type because in such case more primitives can be added
15349 -- later to the list of primitives of Derived_Type by routine
15350 -- Process_Incomplete_Dependents
15352 return True;
15353 end if;
15355 E := Next_Entity (E);
15356 end loop;
15358 List := Collect_Primitive_Operations (Derived_Type);
15359 Elmt := First_Elmt (List);
15361 Op_Elmt := First_Elmt (Op_List);
15362 while Present (Op_Elmt) loop
15363 Subp := Node (Op_Elmt);
15364 New_Subp := Node (Elmt);
15366 -- At this early stage Derived_Type has no entities with attribute
15367 -- Interface_Alias. In addition, such primitives are always
15368 -- located at the end of the list of primitives of Parent_Type.
15369 -- Therefore, if found we can safely stop processing pending
15370 -- entities.
15372 exit when Present (Interface_Alias (Subp));
15374 -- Handle hidden entities
15376 if not Is_Predefined_Dispatching_Operation (Subp)
15377 and then Is_Hidden (Subp)
15378 then
15379 if Present (New_Subp)
15380 and then Primitive_Names_Match (Subp, New_Subp)
15381 then
15382 Next_Elmt (Elmt);
15383 end if;
15385 else
15386 if not Present (New_Subp)
15387 or else Ekind (Subp) /= Ekind (New_Subp)
15388 or else not Primitive_Names_Match (Subp, New_Subp)
15389 then
15390 return False;
15391 end if;
15393 Next_Elmt (Elmt);
15394 end if;
15396 Next_Elmt (Op_Elmt);
15397 end loop;
15399 return True;
15400 end Check_Derived_Type;
15402 ---------------------------------
15403 -- Derive_Interface_Subprogram --
15404 ---------------------------------
15406 procedure Derive_Interface_Subprogram
15407 (New_Subp : out Entity_Id;
15408 Subp : Entity_Id;
15409 Actual_Subp : Entity_Id)
15411 Iface_Subp : constant Entity_Id := Ultimate_Alias (Subp);
15412 Iface_Type : constant Entity_Id := Find_Dispatching_Type (Iface_Subp);
15414 begin
15415 pragma Assert (Is_Interface (Iface_Type));
15417 Derive_Subprogram
15418 (New_Subp => New_Subp,
15419 Parent_Subp => Iface_Subp,
15420 Derived_Type => Derived_Type,
15421 Parent_Type => Iface_Type,
15422 Actual_Subp => Actual_Subp);
15424 -- Given that this new interface entity corresponds with a primitive
15425 -- of the parent that was not overridden we must leave it associated
15426 -- with its parent primitive to ensure that it will share the same
15427 -- dispatch table slot when overridden. We must set the Alias to Subp
15428 -- (instead of Iface_Subp), and we must fix Is_Abstract_Subprogram
15429 -- (in case we inherited Subp from Iface_Type via a nonabstract
15430 -- generic formal type).
15432 if No (Actual_Subp) then
15433 Set_Alias (New_Subp, Subp);
15435 declare
15436 T : Entity_Id := Find_Dispatching_Type (Subp);
15437 begin
15438 while Etype (T) /= T loop
15439 if Is_Generic_Type (T) and then not Is_Abstract_Type (T) then
15440 Set_Is_Abstract_Subprogram (New_Subp, False);
15441 exit;
15442 end if;
15444 T := Etype (T);
15445 end loop;
15446 end;
15448 -- For instantiations this is not needed since the previous call to
15449 -- Derive_Subprogram leaves the entity well decorated.
15451 else
15452 pragma Assert (Alias (New_Subp) = Actual_Subp);
15453 null;
15454 end if;
15455 end Derive_Interface_Subprogram;
15457 -- Local variables
15459 Alias_Subp : Entity_Id;
15460 Act_List : Elist_Id;
15461 Act_Elmt : Elmt_Id;
15462 Act_Subp : Entity_Id := Empty;
15463 Elmt : Elmt_Id;
15464 Need_Search : Boolean := False;
15465 New_Subp : Entity_Id := Empty;
15466 Parent_Base : Entity_Id;
15467 Subp : Entity_Id;
15469 -- Start of processing for Derive_Subprograms
15471 begin
15472 if Ekind (Parent_Type) = E_Record_Type_With_Private
15473 and then Has_Discriminants (Parent_Type)
15474 and then Present (Full_View (Parent_Type))
15475 then
15476 Parent_Base := Full_View (Parent_Type);
15477 else
15478 Parent_Base := Parent_Type;
15479 end if;
15481 if Present (Generic_Actual) then
15482 Act_List := Collect_Primitive_Operations (Generic_Actual);
15483 Act_Elmt := First_Elmt (Act_List);
15484 else
15485 Act_List := No_Elist;
15486 Act_Elmt := No_Elmt;
15487 end if;
15489 -- Derive primitives inherited from the parent. Note that if the generic
15490 -- actual is present, this is not really a type derivation, it is a
15491 -- completion within an instance.
15493 -- Case 1: Derived_Type does not implement interfaces
15495 if not Is_Tagged_Type (Derived_Type)
15496 or else (not Has_Interfaces (Derived_Type)
15497 and then not (Present (Generic_Actual)
15498 and then Has_Interfaces (Generic_Actual)))
15499 then
15500 Elmt := First_Elmt (Op_List);
15501 while Present (Elmt) loop
15502 Subp := Node (Elmt);
15504 -- Literals are derived earlier in the process of building the
15505 -- derived type, and are skipped here.
15507 if Ekind (Subp) = E_Enumeration_Literal then
15508 null;
15510 -- The actual is a direct descendant and the common primitive
15511 -- operations appear in the same order.
15513 -- If the generic parent type is present, the derived type is an
15514 -- instance of a formal derived type, and within the instance its
15515 -- operations are those of the actual. We derive from the formal
15516 -- type but make the inherited operations aliases of the
15517 -- corresponding operations of the actual.
15519 else
15520 pragma Assert (No (Node (Act_Elmt))
15521 or else (Primitive_Names_Match (Subp, Node (Act_Elmt))
15522 and then
15523 Type_Conformant
15524 (Subp, Node (Act_Elmt),
15525 Skip_Controlling_Formals => True)));
15527 Derive_Subprogram
15528 (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
15530 if Present (Act_Elmt) then
15531 Next_Elmt (Act_Elmt);
15532 end if;
15533 end if;
15535 Next_Elmt (Elmt);
15536 end loop;
15538 -- Case 2: Derived_Type implements interfaces
15540 else
15541 -- If the parent type has no predefined primitives we remove
15542 -- predefined primitives from the list of primitives of generic
15543 -- actual to simplify the complexity of this algorithm.
15545 if Present (Generic_Actual) then
15546 declare
15547 Has_Predefined_Primitives : Boolean := False;
15549 begin
15550 -- Check if the parent type has predefined primitives
15552 Elmt := First_Elmt (Op_List);
15553 while Present (Elmt) loop
15554 Subp := Node (Elmt);
15556 if Is_Predefined_Dispatching_Operation (Subp)
15557 and then not Comes_From_Source (Ultimate_Alias (Subp))
15558 then
15559 Has_Predefined_Primitives := True;
15560 exit;
15561 end if;
15563 Next_Elmt (Elmt);
15564 end loop;
15566 -- Remove predefined primitives of Generic_Actual. We must use
15567 -- an auxiliary list because in case of tagged types the value
15568 -- returned by Collect_Primitive_Operations is the value stored
15569 -- in its Primitive_Operations attribute (and we don't want to
15570 -- modify its current contents).
15572 if not Has_Predefined_Primitives then
15573 declare
15574 Aux_List : constant Elist_Id := New_Elmt_List;
15576 begin
15577 Elmt := First_Elmt (Act_List);
15578 while Present (Elmt) loop
15579 Subp := Node (Elmt);
15581 if not Is_Predefined_Dispatching_Operation (Subp)
15582 or else Comes_From_Source (Subp)
15583 then
15584 Append_Elmt (Subp, Aux_List);
15585 end if;
15587 Next_Elmt (Elmt);
15588 end loop;
15590 Act_List := Aux_List;
15591 end;
15592 end if;
15594 Act_Elmt := First_Elmt (Act_List);
15595 Act_Subp := Node (Act_Elmt);
15596 end;
15597 end if;
15599 -- Stage 1: If the generic actual is not present we derive the
15600 -- primitives inherited from the parent type. If the generic parent
15601 -- type is present, the derived type is an instance of a formal
15602 -- derived type, and within the instance its operations are those of
15603 -- the actual. We derive from the formal type but make the inherited
15604 -- operations aliases of the corresponding operations of the actual.
15606 Elmt := First_Elmt (Op_List);
15607 while Present (Elmt) loop
15608 Subp := Node (Elmt);
15609 Alias_Subp := Ultimate_Alias (Subp);
15611 -- Do not derive internal entities of the parent that link
15612 -- interface primitives with their covering primitive. These
15613 -- entities will be added to this type when frozen.
15615 if Present (Interface_Alias (Subp)) then
15616 goto Continue;
15617 end if;
15619 -- If the generic actual is present find the corresponding
15620 -- operation in the generic actual. If the parent type is a
15621 -- direct ancestor of the derived type then, even if it is an
15622 -- interface, the operations are inherited from the primary
15623 -- dispatch table and are in the proper order. If we detect here
15624 -- that primitives are not in the same order we traverse the list
15625 -- of primitive operations of the actual to find the one that
15626 -- implements the interface primitive.
15628 if Need_Search
15629 or else
15630 (Present (Generic_Actual)
15631 and then Present (Act_Subp)
15632 and then not
15633 (Primitive_Names_Match (Subp, Act_Subp)
15634 and then
15635 Type_Conformant (Subp, Act_Subp,
15636 Skip_Controlling_Formals => True)))
15637 then
15638 pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual,
15639 Use_Full_View => True));
15641 -- Remember that we need searching for all pending primitives
15643 Need_Search := True;
15645 -- Handle entities associated with interface primitives
15647 if Present (Alias_Subp)
15648 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
15649 and then not Is_Predefined_Dispatching_Operation (Subp)
15650 then
15651 -- Search for the primitive in the homonym chain
15653 Act_Subp :=
15654 Find_Primitive_Covering_Interface
15655 (Tagged_Type => Generic_Actual,
15656 Iface_Prim => Alias_Subp);
15658 -- Previous search may not locate primitives covering
15659 -- interfaces defined in generics units or instantiations.
15660 -- (it fails if the covering primitive has formals whose
15661 -- type is also defined in generics or instantiations).
15662 -- In such case we search in the list of primitives of the
15663 -- generic actual for the internal entity that links the
15664 -- interface primitive and the covering primitive.
15666 if No (Act_Subp)
15667 and then Is_Generic_Type (Parent_Type)
15668 then
15669 -- This code has been designed to handle only generic
15670 -- formals that implement interfaces that are defined
15671 -- in a generic unit or instantiation. If this code is
15672 -- needed for other cases we must review it because
15673 -- (given that it relies on Original_Location to locate
15674 -- the primitive of Generic_Actual that covers the
15675 -- interface) it could leave linked through attribute
15676 -- Alias entities of unrelated instantiations).
15678 pragma Assert
15679 (Is_Generic_Unit
15680 (Scope (Find_Dispatching_Type (Alias_Subp)))
15681 or else
15682 Instantiation_Depth
15683 (Sloc (Find_Dispatching_Type (Alias_Subp))) > 0);
15685 declare
15686 Iface_Prim_Loc : constant Source_Ptr :=
15687 Original_Location (Sloc (Alias_Subp));
15689 Elmt : Elmt_Id;
15690 Prim : Entity_Id;
15692 begin
15693 Elmt :=
15694 First_Elmt (Primitive_Operations (Generic_Actual));
15696 Search : while Present (Elmt) loop
15697 Prim := Node (Elmt);
15699 if Present (Interface_Alias (Prim))
15700 and then Original_Location
15701 (Sloc (Interface_Alias (Prim))) =
15702 Iface_Prim_Loc
15703 then
15704 Act_Subp := Alias (Prim);
15705 exit Search;
15706 end if;
15708 Next_Elmt (Elmt);
15709 end loop Search;
15710 end;
15711 end if;
15713 pragma Assert (Present (Act_Subp)
15714 or else Is_Abstract_Type (Generic_Actual)
15715 or else Serious_Errors_Detected > 0);
15717 -- Handle predefined primitives plus the rest of user-defined
15718 -- primitives
15720 else
15721 Act_Elmt := First_Elmt (Act_List);
15722 while Present (Act_Elmt) loop
15723 Act_Subp := Node (Act_Elmt);
15725 exit when Primitive_Names_Match (Subp, Act_Subp)
15726 and then Type_Conformant
15727 (Subp, Act_Subp,
15728 Skip_Controlling_Formals => True)
15729 and then No (Interface_Alias (Act_Subp));
15731 Next_Elmt (Act_Elmt);
15732 end loop;
15734 if No (Act_Elmt) then
15735 Act_Subp := Empty;
15736 end if;
15737 end if;
15738 end if;
15740 -- Case 1: If the parent is a limited interface then it has the
15741 -- predefined primitives of synchronized interfaces. However, the
15742 -- actual type may be a non-limited type and hence it does not
15743 -- have such primitives.
15745 if Present (Generic_Actual)
15746 and then not Present (Act_Subp)
15747 and then Is_Limited_Interface (Parent_Base)
15748 and then Is_Predefined_Interface_Primitive (Subp)
15749 then
15750 null;
15752 -- Case 2: Inherit entities associated with interfaces that were
15753 -- not covered by the parent type. We exclude here null interface
15754 -- primitives because they do not need special management.
15756 -- We also exclude interface operations that are renamings. If the
15757 -- subprogram is an explicit renaming of an interface primitive,
15758 -- it is a regular primitive operation, and the presence of its
15759 -- alias is not relevant: it has to be derived like any other
15760 -- primitive.
15762 elsif Present (Alias (Subp))
15763 and then Nkind (Unit_Declaration_Node (Subp)) /=
15764 N_Subprogram_Renaming_Declaration
15765 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
15766 and then not
15767 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
15768 and then Null_Present (Parent (Alias_Subp)))
15769 then
15770 -- If this is an abstract private type then we transfer the
15771 -- derivation of the interface primitive from the partial view
15772 -- to the full view. This is safe because all the interfaces
15773 -- must be visible in the partial view. Done to avoid adding
15774 -- a new interface derivation to the private part of the
15775 -- enclosing package; otherwise this new derivation would be
15776 -- decorated as hidden when the analysis of the enclosing
15777 -- package completes.
15779 if Is_Abstract_Type (Derived_Type)
15780 and then In_Private_Part (Current_Scope)
15781 and then Has_Private_Declaration (Derived_Type)
15782 then
15783 declare
15784 Partial_View : Entity_Id;
15785 Elmt : Elmt_Id;
15786 Ent : Entity_Id;
15788 begin
15789 Partial_View := First_Entity (Current_Scope);
15790 loop
15791 exit when No (Partial_View)
15792 or else (Has_Private_Declaration (Partial_View)
15793 and then
15794 Full_View (Partial_View) = Derived_Type);
15796 Next_Entity (Partial_View);
15797 end loop;
15799 -- If the partial view was not found then the source code
15800 -- has errors and the derivation is not needed.
15802 if Present (Partial_View) then
15803 Elmt :=
15804 First_Elmt (Primitive_Operations (Partial_View));
15805 while Present (Elmt) loop
15806 Ent := Node (Elmt);
15808 if Present (Alias (Ent))
15809 and then Ultimate_Alias (Ent) = Alias (Subp)
15810 then
15811 Append_Elmt
15812 (Ent, Primitive_Operations (Derived_Type));
15813 exit;
15814 end if;
15816 Next_Elmt (Elmt);
15817 end loop;
15819 -- If the interface primitive was not found in the
15820 -- partial view then this interface primitive was
15821 -- overridden. We add a derivation to activate in
15822 -- Derive_Progenitor_Subprograms the machinery to
15823 -- search for it.
15825 if No (Elmt) then
15826 Derive_Interface_Subprogram
15827 (New_Subp => New_Subp,
15828 Subp => Subp,
15829 Actual_Subp => Act_Subp);
15830 end if;
15831 end if;
15832 end;
15833 else
15834 Derive_Interface_Subprogram
15835 (New_Subp => New_Subp,
15836 Subp => Subp,
15837 Actual_Subp => Act_Subp);
15838 end if;
15840 -- Case 3: Common derivation
15842 else
15843 Derive_Subprogram
15844 (New_Subp => New_Subp,
15845 Parent_Subp => Subp,
15846 Derived_Type => Derived_Type,
15847 Parent_Type => Parent_Base,
15848 Actual_Subp => Act_Subp);
15849 end if;
15851 -- No need to update Act_Elm if we must search for the
15852 -- corresponding operation in the generic actual
15854 if not Need_Search
15855 and then Present (Act_Elmt)
15856 then
15857 Next_Elmt (Act_Elmt);
15858 Act_Subp := Node (Act_Elmt);
15859 end if;
15861 <<Continue>>
15862 Next_Elmt (Elmt);
15863 end loop;
15865 -- Inherit additional operations from progenitors. If the derived
15866 -- type is a generic actual, there are not new primitive operations
15867 -- for the type because it has those of the actual, and therefore
15868 -- nothing needs to be done. The renamings generated above are not
15869 -- primitive operations, and their purpose is simply to make the
15870 -- proper operations visible within an instantiation.
15872 if No (Generic_Actual) then
15873 Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
15874 end if;
15875 end if;
15877 -- Final check: Direct descendants must have their primitives in the
15878 -- same order. We exclude from this test untagged types and instances
15879 -- of formal derived types. We skip this test if we have already
15880 -- reported serious errors in the sources.
15882 pragma Assert (not Is_Tagged_Type (Derived_Type)
15883 or else Present (Generic_Actual)
15884 or else Serious_Errors_Detected > 0
15885 or else Check_Derived_Type);
15886 end Derive_Subprograms;
15888 --------------------------------
15889 -- Derived_Standard_Character --
15890 --------------------------------
15892 procedure Derived_Standard_Character
15893 (N : Node_Id;
15894 Parent_Type : Entity_Id;
15895 Derived_Type : Entity_Id)
15897 Loc : constant Source_Ptr := Sloc (N);
15898 Def : constant Node_Id := Type_Definition (N);
15899 Indic : constant Node_Id := Subtype_Indication (Def);
15900 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
15901 Implicit_Base : constant Entity_Id :=
15902 Create_Itype
15903 (E_Enumeration_Type, N, Derived_Type, 'B');
15905 Lo : Node_Id;
15906 Hi : Node_Id;
15908 begin
15909 Discard_Node (Process_Subtype (Indic, N));
15911 Set_Etype (Implicit_Base, Parent_Base);
15912 Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
15913 Set_RM_Size (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
15915 Set_Is_Character_Type (Implicit_Base, True);
15916 Set_Has_Delayed_Freeze (Implicit_Base);
15918 -- The bounds of the implicit base are the bounds of the parent base.
15919 -- Note that their type is the parent base.
15921 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
15922 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
15924 Set_Scalar_Range (Implicit_Base,
15925 Make_Range (Loc,
15926 Low_Bound => Lo,
15927 High_Bound => Hi));
15929 Conditional_Delay (Derived_Type, Parent_Type);
15931 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
15932 Set_Etype (Derived_Type, Implicit_Base);
15933 Set_Size_Info (Derived_Type, Parent_Type);
15935 if Unknown_RM_Size (Derived_Type) then
15936 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
15937 end if;
15939 Set_Is_Character_Type (Derived_Type, True);
15941 if Nkind (Indic) /= N_Subtype_Indication then
15943 -- If no explicit constraint, the bounds are those
15944 -- of the parent type.
15946 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Type));
15947 Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
15948 Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
15949 end if;
15951 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
15953 -- Because the implicit base is used in the conversion of the bounds, we
15954 -- have to freeze it now. This is similar to what is done for numeric
15955 -- types, and it equally suspicious, but otherwise a non-static bound
15956 -- will have a reference to an unfrozen type, which is rejected by Gigi
15957 -- (???). This requires specific care for definition of stream
15958 -- attributes. For details, see comments at the end of
15959 -- Build_Derived_Numeric_Type.
15961 Freeze_Before (N, Implicit_Base);
15962 end Derived_Standard_Character;
15964 ------------------------------
15965 -- Derived_Type_Declaration --
15966 ------------------------------
15968 procedure Derived_Type_Declaration
15969 (T : Entity_Id;
15970 N : Node_Id;
15971 Is_Completion : Boolean)
15973 Parent_Type : Entity_Id;
15975 function Comes_From_Generic (Typ : Entity_Id) return Boolean;
15976 -- Check whether the parent type is a generic formal, or derives
15977 -- directly or indirectly from one.
15979 ------------------------
15980 -- Comes_From_Generic --
15981 ------------------------
15983 function Comes_From_Generic (Typ : Entity_Id) return Boolean is
15984 begin
15985 if Is_Generic_Type (Typ) then
15986 return True;
15988 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
15989 return True;
15991 elsif Is_Private_Type (Typ)
15992 and then Present (Full_View (Typ))
15993 and then Is_Generic_Type (Root_Type (Full_View (Typ)))
15994 then
15995 return True;
15997 elsif Is_Generic_Actual_Type (Typ) then
15998 return True;
16000 else
16001 return False;
16002 end if;
16003 end Comes_From_Generic;
16005 -- Local variables
16007 Def : constant Node_Id := Type_Definition (N);
16008 Iface_Def : Node_Id;
16009 Indic : constant Node_Id := Subtype_Indication (Def);
16010 Extension : constant Node_Id := Record_Extension_Part (Def);
16011 Parent_Node : Node_Id;
16012 Taggd : Boolean;
16014 -- Start of processing for Derived_Type_Declaration
16016 begin
16017 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
16019 -- Ada 2005 (AI-251): In case of interface derivation check that the
16020 -- parent is also an interface.
16022 if Interface_Present (Def) then
16023 Check_SPARK_05_Restriction ("interface is not allowed", Def);
16025 if not Is_Interface (Parent_Type) then
16026 Diagnose_Interface (Indic, Parent_Type);
16028 else
16029 Parent_Node := Parent (Base_Type (Parent_Type));
16030 Iface_Def := Type_Definition (Parent_Node);
16032 -- Ada 2005 (AI-251): Limited interfaces can only inherit from
16033 -- other limited interfaces.
16035 if Limited_Present (Def) then
16036 if Limited_Present (Iface_Def) then
16037 null;
16039 elsif Protected_Present (Iface_Def) then
16040 Error_Msg_NE
16041 ("descendant of & must be declared as a protected "
16042 & "interface", N, Parent_Type);
16044 elsif Synchronized_Present (Iface_Def) then
16045 Error_Msg_NE
16046 ("descendant of & must be declared as a synchronized "
16047 & "interface", N, Parent_Type);
16049 elsif Task_Present (Iface_Def) then
16050 Error_Msg_NE
16051 ("descendant of & must be declared as a task interface",
16052 N, Parent_Type);
16054 else
16055 Error_Msg_N
16056 ("(Ada 2005) limited interface cannot inherit from "
16057 & "non-limited interface", Indic);
16058 end if;
16060 -- Ada 2005 (AI-345): Non-limited interfaces can only inherit
16061 -- from non-limited or limited interfaces.
16063 elsif not Protected_Present (Def)
16064 and then not Synchronized_Present (Def)
16065 and then not Task_Present (Def)
16066 then
16067 if Limited_Present (Iface_Def) then
16068 null;
16070 elsif Protected_Present (Iface_Def) then
16071 Error_Msg_NE
16072 ("descendant of & must be declared as a protected "
16073 & "interface", N, Parent_Type);
16075 elsif Synchronized_Present (Iface_Def) then
16076 Error_Msg_NE
16077 ("descendant of & must be declared as a synchronized "
16078 & "interface", N, Parent_Type);
16080 elsif Task_Present (Iface_Def) then
16081 Error_Msg_NE
16082 ("descendant of & must be declared as a task interface",
16083 N, Parent_Type);
16084 else
16085 null;
16086 end if;
16087 end if;
16088 end if;
16089 end if;
16091 if Is_Tagged_Type (Parent_Type)
16092 and then Is_Concurrent_Type (Parent_Type)
16093 and then not Is_Interface (Parent_Type)
16094 then
16095 Error_Msg_N
16096 ("parent type of a record extension cannot be a synchronized "
16097 & "tagged type (RM 3.9.1 (3/1))", N);
16098 Set_Etype (T, Any_Type);
16099 return;
16100 end if;
16102 -- Ada 2005 (AI-251): Decorate all the names in the list of ancestor
16103 -- interfaces
16105 if Is_Tagged_Type (Parent_Type)
16106 and then Is_Non_Empty_List (Interface_List (Def))
16107 then
16108 declare
16109 Intf : Node_Id;
16110 T : Entity_Id;
16112 begin
16113 Intf := First (Interface_List (Def));
16114 while Present (Intf) loop
16115 T := Find_Type_Of_Subtype_Indic (Intf);
16117 if not Is_Interface (T) then
16118 Diagnose_Interface (Intf, T);
16120 -- Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
16121 -- a limited type from having a nonlimited progenitor.
16123 elsif (Limited_Present (Def)
16124 or else (not Is_Interface (Parent_Type)
16125 and then Is_Limited_Type (Parent_Type)))
16126 and then not Is_Limited_Interface (T)
16127 then
16128 Error_Msg_NE
16129 ("progenitor interface& of limited type must be limited",
16130 N, T);
16131 end if;
16133 Next (Intf);
16134 end loop;
16135 end;
16136 end if;
16138 if Parent_Type = Any_Type
16139 or else Etype (Parent_Type) = Any_Type
16140 or else (Is_Class_Wide_Type (Parent_Type)
16141 and then Etype (Parent_Type) = T)
16142 then
16143 -- If Parent_Type is undefined or illegal, make new type into a
16144 -- subtype of Any_Type, and set a few attributes to prevent cascaded
16145 -- errors. If this is a self-definition, emit error now.
16147 if T = Parent_Type or else T = Etype (Parent_Type) then
16148 Error_Msg_N ("type cannot be used in its own definition", Indic);
16149 end if;
16151 Set_Ekind (T, Ekind (Parent_Type));
16152 Set_Etype (T, Any_Type);
16153 Set_Scalar_Range (T, Scalar_Range (Any_Type));
16155 if Is_Tagged_Type (T)
16156 and then Is_Record_Type (T)
16157 then
16158 Set_Direct_Primitive_Operations (T, New_Elmt_List);
16159 end if;
16161 return;
16162 end if;
16164 -- Ada 2005 (AI-251): The case in which the parent of the full-view is
16165 -- an interface is special because the list of interfaces in the full
16166 -- view can be given in any order. For example:
16168 -- type A is interface;
16169 -- type B is interface and A;
16170 -- type D is new B with private;
16171 -- private
16172 -- type D is new A and B with null record; -- 1 --
16174 -- In this case we perform the following transformation of -1-:
16176 -- type D is new B and A with null record;
16178 -- If the parent of the full-view covers the parent of the partial-view
16179 -- we have two possible cases:
16181 -- 1) They have the same parent
16182 -- 2) The parent of the full-view implements some further interfaces
16184 -- In both cases we do not need to perform the transformation. In the
16185 -- first case the source program is correct and the transformation is
16186 -- not needed; in the second case the source program does not fulfill
16187 -- the no-hidden interfaces rule (AI-396) and the error will be reported
16188 -- later.
16190 -- This transformation not only simplifies the rest of the analysis of
16191 -- this type declaration but also simplifies the correct generation of
16192 -- the object layout to the expander.
16194 if In_Private_Part (Current_Scope)
16195 and then Is_Interface (Parent_Type)
16196 then
16197 declare
16198 Iface : Node_Id;
16199 Partial_View : Entity_Id;
16200 Partial_View_Parent : Entity_Id;
16201 New_Iface : Node_Id;
16203 begin
16204 -- Look for the associated private type declaration
16206 Partial_View := First_Entity (Current_Scope);
16207 loop
16208 exit when No (Partial_View)
16209 or else (Has_Private_Declaration (Partial_View)
16210 and then Full_View (Partial_View) = T);
16212 Next_Entity (Partial_View);
16213 end loop;
16215 -- If the partial view was not found then the source code has
16216 -- errors and the transformation is not needed.
16218 if Present (Partial_View) then
16219 Partial_View_Parent := Etype (Partial_View);
16221 -- If the parent of the full-view covers the parent of the
16222 -- partial-view we have nothing else to do.
16224 if Interface_Present_In_Ancestor
16225 (Parent_Type, Partial_View_Parent)
16226 then
16227 null;
16229 -- Traverse the list of interfaces of the full-view to look
16230 -- for the parent of the partial-view and perform the tree
16231 -- transformation.
16233 else
16234 Iface := First (Interface_List (Def));
16235 while Present (Iface) loop
16236 if Etype (Iface) = Etype (Partial_View) then
16237 Rewrite (Subtype_Indication (Def),
16238 New_Copy (Subtype_Indication
16239 (Parent (Partial_View))));
16241 New_Iface :=
16242 Make_Identifier (Sloc (N), Chars (Parent_Type));
16243 Append (New_Iface, Interface_List (Def));
16245 -- Analyze the transformed code
16247 Derived_Type_Declaration (T, N, Is_Completion);
16248 return;
16249 end if;
16251 Next (Iface);
16252 end loop;
16253 end if;
16254 end if;
16255 end;
16256 end if;
16258 -- Only composite types other than array types are allowed to have
16259 -- discriminants.
16261 if Present (Discriminant_Specifications (N)) then
16262 if (Is_Elementary_Type (Parent_Type)
16263 or else
16264 Is_Array_Type (Parent_Type))
16265 and then not Error_Posted (N)
16266 then
16267 Error_Msg_N
16268 ("elementary or array type cannot have discriminants",
16269 Defining_Identifier (First (Discriminant_Specifications (N))));
16270 Set_Has_Discriminants (T, False);
16272 -- The type is allowed to have discriminants
16274 else
16275 Check_SPARK_05_Restriction ("discriminant type is not allowed", N);
16276 end if;
16277 end if;
16279 -- In Ada 83, a derived type defined in a package specification cannot
16280 -- be used for further derivation until the end of its visible part.
16281 -- Note that derivation in the private part of the package is allowed.
16283 if Ada_Version = Ada_83
16284 and then Is_Derived_Type (Parent_Type)
16285 and then In_Visible_Part (Scope (Parent_Type))
16286 then
16287 if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
16288 Error_Msg_N
16289 ("(Ada 83): premature use of type for derivation", Indic);
16290 end if;
16291 end if;
16293 -- Check for early use of incomplete or private type
16295 if Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
16296 Error_Msg_N ("premature derivation of incomplete type", Indic);
16297 return;
16299 elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
16300 and then not Comes_From_Generic (Parent_Type))
16301 or else Has_Private_Component (Parent_Type)
16302 then
16303 -- The ancestor type of a formal type can be incomplete, in which
16304 -- case only the operations of the partial view are available in the
16305 -- generic. Subsequent checks may be required when the full view is
16306 -- analyzed to verify that a derivation from a tagged type has an
16307 -- extension.
16309 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
16310 null;
16312 elsif No (Underlying_Type (Parent_Type))
16313 or else Has_Private_Component (Parent_Type)
16314 then
16315 Error_Msg_N
16316 ("premature derivation of derived or private type", Indic);
16318 -- Flag the type itself as being in error, this prevents some
16319 -- nasty problems with subsequent uses of the malformed type.
16321 Set_Error_Posted (T);
16323 -- Check that within the immediate scope of an untagged partial
16324 -- view it's illegal to derive from the partial view if the
16325 -- full view is tagged. (7.3(7))
16327 -- We verify that the Parent_Type is a partial view by checking
16328 -- that it is not a Full_Type_Declaration (i.e. a private type or
16329 -- private extension declaration), to distinguish a partial view
16330 -- from a derivation from a private type which also appears as
16331 -- E_Private_Type. If the parent base type is not declared in an
16332 -- enclosing scope there is no need to check.
16334 elsif Present (Full_View (Parent_Type))
16335 and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
16336 and then not Is_Tagged_Type (Parent_Type)
16337 and then Is_Tagged_Type (Full_View (Parent_Type))
16338 and then In_Open_Scopes (Scope (Base_Type (Parent_Type)))
16339 then
16340 Error_Msg_N
16341 ("premature derivation from type with tagged full view",
16342 Indic);
16343 end if;
16344 end if;
16346 -- Check that form of derivation is appropriate
16348 Taggd := Is_Tagged_Type (Parent_Type);
16350 -- Set the parent type to the class-wide type's specific type in this
16351 -- case to prevent cascading errors
16353 if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
16354 Error_Msg_N ("parent type must not be a class-wide type", Indic);
16355 Set_Etype (T, Etype (Parent_Type));
16356 return;
16357 end if;
16359 if Present (Extension) and then not Taggd then
16360 Error_Msg_N
16361 ("type derived from untagged type cannot have extension", Indic);
16363 elsif No (Extension) and then Taggd then
16365 -- If this declaration is within a private part (or body) of a
16366 -- generic instantiation then the derivation is allowed (the parent
16367 -- type can only appear tagged in this case if it's a generic actual
16368 -- type, since it would otherwise have been rejected in the analysis
16369 -- of the generic template).
16371 if not Is_Generic_Actual_Type (Parent_Type)
16372 or else In_Visible_Part (Scope (Parent_Type))
16373 then
16374 if Is_Class_Wide_Type (Parent_Type) then
16375 Error_Msg_N
16376 ("parent type must not be a class-wide type", Indic);
16378 -- Use specific type to prevent cascaded errors.
16380 Parent_Type := Etype (Parent_Type);
16382 else
16383 Error_Msg_N
16384 ("type derived from tagged type must have extension", Indic);
16385 end if;
16386 end if;
16387 end if;
16389 -- AI-443: Synchronized formal derived types require a private
16390 -- extension. There is no point in checking the ancestor type or
16391 -- the progenitors since the construct is wrong to begin with.
16393 if Ada_Version >= Ada_2005
16394 and then Is_Generic_Type (T)
16395 and then Present (Original_Node (N))
16396 then
16397 declare
16398 Decl : constant Node_Id := Original_Node (N);
16400 begin
16401 if Nkind (Decl) = N_Formal_Type_Declaration
16402 and then Nkind (Formal_Type_Definition (Decl)) =
16403 N_Formal_Derived_Type_Definition
16404 and then Synchronized_Present (Formal_Type_Definition (Decl))
16405 and then No (Extension)
16407 -- Avoid emitting a duplicate error message
16409 and then not Error_Posted (Indic)
16410 then
16411 Error_Msg_N
16412 ("synchronized derived type must have extension", N);
16413 end if;
16414 end;
16415 end if;
16417 if Null_Exclusion_Present (Def)
16418 and then not Is_Access_Type (Parent_Type)
16419 then
16420 Error_Msg_N ("null exclusion can only apply to an access type", N);
16421 end if;
16423 -- Avoid deriving parent primitives of underlying record views
16425 Build_Derived_Type (N, Parent_Type, T, Is_Completion,
16426 Derive_Subps => not Is_Underlying_Record_View (T));
16428 -- AI-419: The parent type of an explicitly limited derived type must
16429 -- be a limited type or a limited interface.
16431 if Limited_Present (Def) then
16432 Set_Is_Limited_Record (T);
16434 if Is_Interface (T) then
16435 Set_Is_Limited_Interface (T);
16436 end if;
16438 if not Is_Limited_Type (Parent_Type)
16439 and then
16440 (not Is_Interface (Parent_Type)
16441 or else not Is_Limited_Interface (Parent_Type))
16442 then
16443 -- AI05-0096: a derivation in the private part of an instance is
16444 -- legal if the generic formal is untagged limited, and the actual
16445 -- is non-limited.
16447 if Is_Generic_Actual_Type (Parent_Type)
16448 and then In_Private_Part (Current_Scope)
16449 and then
16450 not Is_Tagged_Type
16451 (Generic_Parent_Type (Parent (Parent_Type)))
16452 then
16453 null;
16455 else
16456 Error_Msg_NE
16457 ("parent type& of limited type must be limited",
16458 N, Parent_Type);
16459 end if;
16460 end if;
16461 end if;
16463 -- In SPARK, there are no derived type definitions other than type
16464 -- extensions of tagged record types.
16466 if No (Extension) then
16467 Check_SPARK_05_Restriction
16468 ("derived type is not allowed", Original_Node (N));
16469 end if;
16470 end Derived_Type_Declaration;
16472 ------------------------
16473 -- Diagnose_Interface --
16474 ------------------------
16476 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id) is
16477 begin
16478 if not Is_Interface (E) and then E /= Any_Type then
16479 Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
16480 end if;
16481 end Diagnose_Interface;
16483 ----------------------------------
16484 -- Enumeration_Type_Declaration --
16485 ----------------------------------
16487 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
16488 Ev : Uint;
16489 L : Node_Id;
16490 R_Node : Node_Id;
16491 B_Node : Node_Id;
16493 begin
16494 -- Create identifier node representing lower bound
16496 B_Node := New_Node (N_Identifier, Sloc (Def));
16497 L := First (Literals (Def));
16498 Set_Chars (B_Node, Chars (L));
16499 Set_Entity (B_Node, L);
16500 Set_Etype (B_Node, T);
16501 Set_Is_Static_Expression (B_Node, True);
16503 R_Node := New_Node (N_Range, Sloc (Def));
16504 Set_Low_Bound (R_Node, B_Node);
16506 Set_Ekind (T, E_Enumeration_Type);
16507 Set_First_Literal (T, L);
16508 Set_Etype (T, T);
16509 Set_Is_Constrained (T);
16511 Ev := Uint_0;
16513 -- Loop through literals of enumeration type setting pos and rep values
16514 -- except that if the Ekind is already set, then it means the literal
16515 -- was already constructed (case of a derived type declaration and we
16516 -- should not disturb the Pos and Rep values.
16518 while Present (L) loop
16519 if Ekind (L) /= E_Enumeration_Literal then
16520 Set_Ekind (L, E_Enumeration_Literal);
16521 Set_Enumeration_Pos (L, Ev);
16522 Set_Enumeration_Rep (L, Ev);
16523 Set_Is_Known_Valid (L, True);
16524 end if;
16526 Set_Etype (L, T);
16527 New_Overloaded_Entity (L);
16528 Generate_Definition (L);
16529 Set_Convention (L, Convention_Intrinsic);
16531 -- Case of character literal
16533 if Nkind (L) = N_Defining_Character_Literal then
16534 Set_Is_Character_Type (T, True);
16536 -- Check violation of No_Wide_Characters
16538 if Restriction_Check_Required (No_Wide_Characters) then
16539 Get_Name_String (Chars (L));
16541 if Name_Len >= 3 and then Name_Buffer (1 .. 2) = "QW" then
16542 Check_Restriction (No_Wide_Characters, L);
16543 end if;
16544 end if;
16545 end if;
16547 Ev := Ev + 1;
16548 Next (L);
16549 end loop;
16551 -- Now create a node representing upper bound
16553 B_Node := New_Node (N_Identifier, Sloc (Def));
16554 Set_Chars (B_Node, Chars (Last (Literals (Def))));
16555 Set_Entity (B_Node, Last (Literals (Def)));
16556 Set_Etype (B_Node, T);
16557 Set_Is_Static_Expression (B_Node, True);
16559 Set_High_Bound (R_Node, B_Node);
16561 -- Initialize various fields of the type. Some of this information
16562 -- may be overwritten later through rep.clauses.
16564 Set_Scalar_Range (T, R_Node);
16565 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
16566 Set_Enum_Esize (T);
16567 Set_Enum_Pos_To_Rep (T, Empty);
16569 -- Set Discard_Names if configuration pragma set, or if there is
16570 -- a parameterless pragma in the current declarative region
16572 if Global_Discard_Names or else Discard_Names (Scope (T)) then
16573 Set_Discard_Names (T);
16574 end if;
16576 -- Process end label if there is one
16578 if Present (Def) then
16579 Process_End_Label (Def, 'e', T);
16580 end if;
16581 end Enumeration_Type_Declaration;
16583 ---------------------------------
16584 -- Expand_To_Stored_Constraint --
16585 ---------------------------------
16587 function Expand_To_Stored_Constraint
16588 (Typ : Entity_Id;
16589 Constraint : Elist_Id) return Elist_Id
16591 Explicitly_Discriminated_Type : Entity_Id;
16592 Expansion : Elist_Id;
16593 Discriminant : Entity_Id;
16595 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
16596 -- Find the nearest type that actually specifies discriminants
16598 ---------------------------------
16599 -- Type_With_Explicit_Discrims --
16600 ---------------------------------
16602 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
16603 Typ : constant E := Base_Type (Id);
16605 begin
16606 if Ekind (Typ) in Incomplete_Or_Private_Kind then
16607 if Present (Full_View (Typ)) then
16608 return Type_With_Explicit_Discrims (Full_View (Typ));
16609 end if;
16611 else
16612 if Has_Discriminants (Typ) then
16613 return Typ;
16614 end if;
16615 end if;
16617 if Etype (Typ) = Typ then
16618 return Empty;
16619 elsif Has_Discriminants (Typ) then
16620 return Typ;
16621 else
16622 return Type_With_Explicit_Discrims (Etype (Typ));
16623 end if;
16625 end Type_With_Explicit_Discrims;
16627 -- Start of processing for Expand_To_Stored_Constraint
16629 begin
16630 if No (Constraint) or else Is_Empty_Elmt_List (Constraint) then
16631 return No_Elist;
16632 end if;
16634 Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
16636 if No (Explicitly_Discriminated_Type) then
16637 return No_Elist;
16638 end if;
16640 Expansion := New_Elmt_List;
16642 Discriminant :=
16643 First_Stored_Discriminant (Explicitly_Discriminated_Type);
16644 while Present (Discriminant) loop
16645 Append_Elmt
16646 (Get_Discriminant_Value
16647 (Discriminant, Explicitly_Discriminated_Type, Constraint),
16648 To => Expansion);
16649 Next_Stored_Discriminant (Discriminant);
16650 end loop;
16652 return Expansion;
16653 end Expand_To_Stored_Constraint;
16655 ---------------------------
16656 -- Find_Hidden_Interface --
16657 ---------------------------
16659 function Find_Hidden_Interface
16660 (Src : Elist_Id;
16661 Dest : Elist_Id) return Entity_Id
16663 Iface : Entity_Id;
16664 Iface_Elmt : Elmt_Id;
16666 begin
16667 if Present (Src) and then Present (Dest) then
16668 Iface_Elmt := First_Elmt (Src);
16669 while Present (Iface_Elmt) loop
16670 Iface := Node (Iface_Elmt);
16672 if Is_Interface (Iface)
16673 and then not Contain_Interface (Iface, Dest)
16674 then
16675 return Iface;
16676 end if;
16678 Next_Elmt (Iface_Elmt);
16679 end loop;
16680 end if;
16682 return Empty;
16683 end Find_Hidden_Interface;
16685 --------------------
16686 -- Find_Type_Name --
16687 --------------------
16689 function Find_Type_Name (N : Node_Id) return Entity_Id is
16690 Id : constant Entity_Id := Defining_Identifier (N);
16691 New_Id : Entity_Id;
16692 Prev : Entity_Id;
16693 Prev_Par : Node_Id;
16695 procedure Check_Duplicate_Aspects;
16696 -- Check that aspects specified in a completion have not been specified
16697 -- already in the partial view.
16699 procedure Tag_Mismatch;
16700 -- Diagnose a tagged partial view whose full view is untagged. We post
16701 -- the message on the full view, with a reference to the previous
16702 -- partial view. The partial view can be private or incomplete, and
16703 -- these are handled in a different manner, so we determine the position
16704 -- of the error message from the respective slocs of both.
16706 -----------------------------
16707 -- Check_Duplicate_Aspects --
16708 -----------------------------
16710 procedure Check_Duplicate_Aspects is
16711 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id;
16712 -- Return the corresponding aspect of the partial view which matches
16713 -- the aspect id of Asp. Return Empty is no such aspect exists.
16715 -----------------------------
16716 -- Get_Partial_View_Aspect --
16717 -----------------------------
16719 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id is
16720 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp);
16721 Prev_Asps : constant List_Id := Aspect_Specifications (Prev_Par);
16722 Prev_Asp : Node_Id;
16724 begin
16725 if Present (Prev_Asps) then
16726 Prev_Asp := First (Prev_Asps);
16727 while Present (Prev_Asp) loop
16728 if Get_Aspect_Id (Prev_Asp) = Asp_Id then
16729 return Prev_Asp;
16730 end if;
16732 Next (Prev_Asp);
16733 end loop;
16734 end if;
16736 return Empty;
16737 end Get_Partial_View_Aspect;
16739 -- Local variables
16741 Full_Asps : constant List_Id := Aspect_Specifications (N);
16742 Full_Asp : Node_Id;
16743 Part_Asp : Node_Id;
16745 -- Start of processing for Check_Duplicate_Aspects
16747 begin
16748 if Present (Full_Asps) then
16749 Full_Asp := First (Full_Asps);
16750 while Present (Full_Asp) loop
16751 Part_Asp := Get_Partial_View_Aspect (Full_Asp);
16753 -- An aspect and its class-wide counterpart are two distinct
16754 -- aspects and may apply to both views of an entity.
16756 if Present (Part_Asp)
16757 and then Class_Present (Part_Asp) = Class_Present (Full_Asp)
16758 then
16759 Error_Msg_N
16760 ("aspect already specified in private declaration",
16761 Full_Asp);
16763 Remove (Full_Asp);
16764 return;
16765 end if;
16767 if Has_Discriminants (Prev)
16768 and then not Has_Unknown_Discriminants (Prev)
16769 and then Get_Aspect_Id (Full_Asp) =
16770 Aspect_Implicit_Dereference
16771 then
16772 Error_Msg_N
16773 ("cannot specify aspect if partial view has known "
16774 & "discriminants", Full_Asp);
16775 end if;
16777 Next (Full_Asp);
16778 end loop;
16779 end if;
16780 end Check_Duplicate_Aspects;
16782 ------------------
16783 -- Tag_Mismatch --
16784 ------------------
16786 procedure Tag_Mismatch is
16787 begin
16788 if Sloc (Prev) < Sloc (Id) then
16789 if Ada_Version >= Ada_2012
16790 and then Nkind (N) = N_Private_Type_Declaration
16791 then
16792 Error_Msg_NE
16793 ("declaration of private } must be a tagged type ", Id, Prev);
16794 else
16795 Error_Msg_NE
16796 ("full declaration of } must be a tagged type ", Id, Prev);
16797 end if;
16799 else
16800 if Ada_Version >= Ada_2012
16801 and then Nkind (N) = N_Private_Type_Declaration
16802 then
16803 Error_Msg_NE
16804 ("declaration of private } must be a tagged type ", Prev, Id);
16805 else
16806 Error_Msg_NE
16807 ("full declaration of } must be a tagged type ", Prev, Id);
16808 end if;
16809 end if;
16810 end Tag_Mismatch;
16812 -- Start of processing for Find_Type_Name
16814 begin
16815 -- Find incomplete declaration, if one was given
16817 Prev := Current_Entity_In_Scope (Id);
16819 -- New type declaration
16821 if No (Prev) then
16822 Enter_Name (Id);
16823 return Id;
16825 -- Previous declaration exists
16827 else
16828 Prev_Par := Parent (Prev);
16830 -- Error if not incomplete/private case except if previous
16831 -- declaration is implicit, etc. Enter_Name will emit error if
16832 -- appropriate.
16834 if not Is_Incomplete_Or_Private_Type (Prev) then
16835 Enter_Name (Id);
16836 New_Id := Id;
16838 -- Check invalid completion of private or incomplete type
16840 elsif not Nkind_In (N, N_Full_Type_Declaration,
16841 N_Task_Type_Declaration,
16842 N_Protected_Type_Declaration)
16843 and then
16844 (Ada_Version < Ada_2012
16845 or else not Is_Incomplete_Type (Prev)
16846 or else not Nkind_In (N, N_Private_Type_Declaration,
16847 N_Private_Extension_Declaration))
16848 then
16849 -- Completion must be a full type declarations (RM 7.3(4))
16851 Error_Msg_Sloc := Sloc (Prev);
16852 Error_Msg_NE ("invalid completion of }", Id, Prev);
16854 -- Set scope of Id to avoid cascaded errors. Entity is never
16855 -- examined again, except when saving globals in generics.
16857 Set_Scope (Id, Current_Scope);
16858 New_Id := Id;
16860 -- If this is a repeated incomplete declaration, no further
16861 -- checks are possible.
16863 if Nkind (N) = N_Incomplete_Type_Declaration then
16864 return Prev;
16865 end if;
16867 -- Case of full declaration of incomplete type
16869 elsif Ekind (Prev) = E_Incomplete_Type
16870 and then (Ada_Version < Ada_2012
16871 or else No (Full_View (Prev))
16872 or else not Is_Private_Type (Full_View (Prev)))
16873 then
16874 -- Indicate that the incomplete declaration has a matching full
16875 -- declaration. The defining occurrence of the incomplete
16876 -- declaration remains the visible one, and the procedure
16877 -- Get_Full_View dereferences it whenever the type is used.
16879 if Present (Full_View (Prev)) then
16880 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
16881 end if;
16883 Set_Full_View (Prev, Id);
16884 Append_Entity (Id, Current_Scope);
16885 Set_Is_Public (Id, Is_Public (Prev));
16886 Set_Is_Internal (Id);
16887 New_Id := Prev;
16889 -- If the incomplete view is tagged, a class_wide type has been
16890 -- created already. Use it for the private type as well, in order
16891 -- to prevent multiple incompatible class-wide types that may be
16892 -- created for self-referential anonymous access components.
16894 if Is_Tagged_Type (Prev)
16895 and then Present (Class_Wide_Type (Prev))
16896 then
16897 Set_Ekind (Id, Ekind (Prev)); -- will be reset later
16898 Set_Class_Wide_Type (Id, Class_Wide_Type (Prev));
16900 -- Type of the class-wide type is the current Id. Previously
16901 -- this was not done for private declarations because of order-
16902 -- of-elaboration issues in the back end, but gigi now handles
16903 -- this properly.
16905 Set_Etype (Class_Wide_Type (Id), Id);
16906 end if;
16908 -- Case of full declaration of private type
16910 else
16911 -- If the private type was a completion of an incomplete type then
16912 -- update Prev to reference the private type
16914 if Ada_Version >= Ada_2012
16915 and then Ekind (Prev) = E_Incomplete_Type
16916 and then Present (Full_View (Prev))
16917 and then Is_Private_Type (Full_View (Prev))
16918 then
16919 Prev := Full_View (Prev);
16920 Prev_Par := Parent (Prev);
16921 end if;
16923 if Nkind (N) = N_Full_Type_Declaration
16924 and then Nkind_In
16925 (Type_Definition (N), N_Record_Definition,
16926 N_Derived_Type_Definition)
16927 and then Interface_Present (Type_Definition (N))
16928 then
16929 Error_Msg_N
16930 ("completion of private type cannot be an interface", N);
16931 end if;
16933 if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
16934 if Etype (Prev) /= Prev then
16936 -- Prev is a private subtype or a derived type, and needs
16937 -- no completion.
16939 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
16940 New_Id := Id;
16942 elsif Ekind (Prev) = E_Private_Type
16943 and then Nkind_In (N, N_Task_Type_Declaration,
16944 N_Protected_Type_Declaration)
16945 then
16946 Error_Msg_N
16947 ("completion of nonlimited type cannot be limited", N);
16949 elsif Ekind (Prev) = E_Record_Type_With_Private
16950 and then Nkind_In (N, N_Task_Type_Declaration,
16951 N_Protected_Type_Declaration)
16952 then
16953 if not Is_Limited_Record (Prev) then
16954 Error_Msg_N
16955 ("completion of nonlimited type cannot be limited", N);
16957 elsif No (Interface_List (N)) then
16958 Error_Msg_N
16959 ("completion of tagged private type must be tagged",
16961 end if;
16962 end if;
16964 -- Ada 2005 (AI-251): Private extension declaration of a task
16965 -- type or a protected type. This case arises when covering
16966 -- interface types.
16968 elsif Nkind_In (N, N_Task_Type_Declaration,
16969 N_Protected_Type_Declaration)
16970 then
16971 null;
16973 elsif Nkind (N) /= N_Full_Type_Declaration
16974 or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
16975 then
16976 Error_Msg_N
16977 ("full view of private extension must be an extension", N);
16979 elsif not (Abstract_Present (Parent (Prev)))
16980 and then Abstract_Present (Type_Definition (N))
16981 then
16982 Error_Msg_N
16983 ("full view of non-abstract extension cannot be abstract", N);
16984 end if;
16986 if not In_Private_Part (Current_Scope) then
16987 Error_Msg_N
16988 ("declaration of full view must appear in private part", N);
16989 end if;
16991 if Ada_Version >= Ada_2012 then
16992 Check_Duplicate_Aspects;
16993 end if;
16995 Copy_And_Swap (Prev, Id);
16996 Set_Has_Private_Declaration (Prev);
16997 Set_Has_Private_Declaration (Id);
16999 -- AI12-0133: Indicate whether we have a partial view with
17000 -- unknown discriminants, in which case initialization of objects
17001 -- of the type do not receive an invariant check.
17003 Set_Partial_View_Has_Unknown_Discr
17004 (Prev, Has_Unknown_Discriminants (Id));
17006 -- Preserve aspect and iterator flags that may have been set on
17007 -- the partial view.
17009 Set_Has_Delayed_Aspects (Prev, Has_Delayed_Aspects (Id));
17010 Set_Has_Implicit_Dereference (Prev, Has_Implicit_Dereference (Id));
17012 -- If no error, propagate freeze_node from private to full view.
17013 -- It may have been generated for an early operational item.
17015 if Present (Freeze_Node (Id))
17016 and then Serious_Errors_Detected = 0
17017 and then No (Full_View (Id))
17018 then
17019 Set_Freeze_Node (Prev, Freeze_Node (Id));
17020 Set_Freeze_Node (Id, Empty);
17021 Set_First_Rep_Item (Prev, First_Rep_Item (Id));
17022 end if;
17024 Set_Full_View (Id, Prev);
17025 New_Id := Prev;
17026 end if;
17028 -- Verify that full declaration conforms to partial one
17030 if Is_Incomplete_Or_Private_Type (Prev)
17031 and then Present (Discriminant_Specifications (Prev_Par))
17032 then
17033 if Present (Discriminant_Specifications (N)) then
17034 if Ekind (Prev) = E_Incomplete_Type then
17035 Check_Discriminant_Conformance (N, Prev, Prev);
17036 else
17037 Check_Discriminant_Conformance (N, Prev, Id);
17038 end if;
17040 else
17041 Error_Msg_N
17042 ("missing discriminants in full type declaration", N);
17044 -- To avoid cascaded errors on subsequent use, share the
17045 -- discriminants of the partial view.
17047 Set_Discriminant_Specifications (N,
17048 Discriminant_Specifications (Prev_Par));
17049 end if;
17050 end if;
17052 -- A prior untagged partial view can have an associated class-wide
17053 -- type due to use of the class attribute, and in this case the full
17054 -- type must also be tagged. This Ada 95 usage is deprecated in favor
17055 -- of incomplete tagged declarations, but we check for it.
17057 if Is_Type (Prev)
17058 and then (Is_Tagged_Type (Prev)
17059 or else Present (Class_Wide_Type (Prev)))
17060 then
17061 -- Ada 2012 (AI05-0162): A private type may be the completion of
17062 -- an incomplete type.
17064 if Ada_Version >= Ada_2012
17065 and then Is_Incomplete_Type (Prev)
17066 and then Nkind_In (N, N_Private_Type_Declaration,
17067 N_Private_Extension_Declaration)
17068 then
17069 -- No need to check private extensions since they are tagged
17071 if Nkind (N) = N_Private_Type_Declaration
17072 and then not Tagged_Present (N)
17073 then
17074 Tag_Mismatch;
17075 end if;
17077 -- The full declaration is either a tagged type (including
17078 -- a synchronized type that implements interfaces) or a
17079 -- type extension, otherwise this is an error.
17081 elsif Nkind_In (N, N_Task_Type_Declaration,
17082 N_Protected_Type_Declaration)
17083 then
17084 if No (Interface_List (N)) and then not Error_Posted (N) then
17085 Tag_Mismatch;
17086 end if;
17088 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
17090 -- Indicate that the previous declaration (tagged incomplete
17091 -- or private declaration) requires the same on the full one.
17093 if not Tagged_Present (Type_Definition (N)) then
17094 Tag_Mismatch;
17095 Set_Is_Tagged_Type (Id);
17096 end if;
17098 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
17099 if No (Record_Extension_Part (Type_Definition (N))) then
17100 Error_Msg_NE
17101 ("full declaration of } must be a record extension",
17102 Prev, Id);
17104 -- Set some attributes to produce a usable full view
17106 Set_Is_Tagged_Type (Id);
17107 end if;
17109 else
17110 Tag_Mismatch;
17111 end if;
17112 end if;
17114 if Present (Prev)
17115 and then Nkind (Parent (Prev)) = N_Incomplete_Type_Declaration
17116 and then Present (Premature_Use (Parent (Prev)))
17117 then
17118 Error_Msg_Sloc := Sloc (N);
17119 Error_Msg_N
17120 ("\full declaration #", Premature_Use (Parent (Prev)));
17121 end if;
17123 return New_Id;
17124 end if;
17125 end Find_Type_Name;
17127 -------------------------
17128 -- Find_Type_Of_Object --
17129 -------------------------
17131 function Find_Type_Of_Object
17132 (Obj_Def : Node_Id;
17133 Related_Nod : Node_Id) return Entity_Id
17135 Def_Kind : constant Node_Kind := Nkind (Obj_Def);
17136 P : Node_Id := Parent (Obj_Def);
17137 T : Entity_Id;
17138 Nam : Name_Id;
17140 begin
17141 -- If the parent is a component_definition node we climb to the
17142 -- component_declaration node
17144 if Nkind (P) = N_Component_Definition then
17145 P := Parent (P);
17146 end if;
17148 -- Case of an anonymous array subtype
17150 if Nkind_In (Def_Kind, N_Constrained_Array_Definition,
17151 N_Unconstrained_Array_Definition)
17152 then
17153 T := Empty;
17154 Array_Type_Declaration (T, Obj_Def);
17156 -- Create an explicit subtype whenever possible
17158 elsif Nkind (P) /= N_Component_Declaration
17159 and then Def_Kind = N_Subtype_Indication
17160 then
17161 -- Base name of subtype on object name, which will be unique in
17162 -- the current scope.
17164 -- If this is a duplicate declaration, return base type, to avoid
17165 -- generating duplicate anonymous types.
17167 if Error_Posted (P) then
17168 Analyze (Subtype_Mark (Obj_Def));
17169 return Entity (Subtype_Mark (Obj_Def));
17170 end if;
17172 Nam :=
17173 New_External_Name
17174 (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
17176 T := Make_Defining_Identifier (Sloc (P), Nam);
17178 Insert_Action (Obj_Def,
17179 Make_Subtype_Declaration (Sloc (P),
17180 Defining_Identifier => T,
17181 Subtype_Indication => Relocate_Node (Obj_Def)));
17183 -- This subtype may need freezing, and this will not be done
17184 -- automatically if the object declaration is not in declarative
17185 -- part. Since this is an object declaration, the type cannot always
17186 -- be frozen here. Deferred constants do not freeze their type
17187 -- (which often enough will be private).
17189 if Nkind (P) = N_Object_Declaration
17190 and then Constant_Present (P)
17191 and then No (Expression (P))
17192 then
17193 null;
17195 -- Here we freeze the base type of object type to catch premature use
17196 -- of discriminated private type without a full view.
17198 else
17199 Insert_Actions (Obj_Def, Freeze_Entity (Base_Type (T), P));
17200 end if;
17202 -- Ada 2005 AI-406: the object definition in an object declaration
17203 -- can be an access definition.
17205 elsif Def_Kind = N_Access_Definition then
17206 T := Access_Definition (Related_Nod, Obj_Def);
17208 Set_Is_Local_Anonymous_Access
17210 V => (Ada_Version < Ada_2012)
17211 or else (Nkind (P) /= N_Object_Declaration)
17212 or else Is_Library_Level_Entity (Defining_Identifier (P)));
17214 -- Otherwise, the object definition is just a subtype_mark
17216 else
17217 T := Process_Subtype (Obj_Def, Related_Nod);
17219 -- If expansion is disabled an object definition that is an aggregate
17220 -- will not get expanded and may lead to scoping problems in the back
17221 -- end, if the object is referenced in an inner scope. In that case
17222 -- create an itype reference for the object definition now. This
17223 -- may be redundant in some cases, but harmless.
17225 if Is_Itype (T)
17226 and then Nkind (Related_Nod) = N_Object_Declaration
17227 and then ASIS_Mode
17228 then
17229 Build_Itype_Reference (T, Related_Nod);
17230 end if;
17231 end if;
17233 return T;
17234 end Find_Type_Of_Object;
17236 --------------------------------
17237 -- Find_Type_Of_Subtype_Indic --
17238 --------------------------------
17240 function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
17241 Typ : Entity_Id;
17243 begin
17244 -- Case of subtype mark with a constraint
17246 if Nkind (S) = N_Subtype_Indication then
17247 Find_Type (Subtype_Mark (S));
17248 Typ := Entity (Subtype_Mark (S));
17250 if not
17251 Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
17252 then
17253 Error_Msg_N
17254 ("incorrect constraint for this kind of type", Constraint (S));
17255 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
17256 end if;
17258 -- Otherwise we have a subtype mark without a constraint
17260 elsif Error_Posted (S) then
17261 Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
17262 return Any_Type;
17264 else
17265 Find_Type (S);
17266 Typ := Entity (S);
17267 end if;
17269 -- Check No_Wide_Characters restriction
17271 Check_Wide_Character_Restriction (Typ, S);
17273 return Typ;
17274 end Find_Type_Of_Subtype_Indic;
17276 -------------------------------------
17277 -- Floating_Point_Type_Declaration --
17278 -------------------------------------
17280 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
17281 Digs : constant Node_Id := Digits_Expression (Def);
17282 Max_Digs_Val : constant Uint := Digits_Value (Standard_Long_Long_Float);
17283 Digs_Val : Uint;
17284 Base_Typ : Entity_Id;
17285 Implicit_Base : Entity_Id;
17286 Bound : Node_Id;
17288 function Can_Derive_From (E : Entity_Id) return Boolean;
17289 -- Find if given digits value, and possibly a specified range, allows
17290 -- derivation from specified type
17292 function Find_Base_Type return Entity_Id;
17293 -- Find a predefined base type that Def can derive from, or generate
17294 -- an error and substitute Long_Long_Float if none exists.
17296 ---------------------
17297 -- Can_Derive_From --
17298 ---------------------
17300 function Can_Derive_From (E : Entity_Id) return Boolean is
17301 Spec : constant Entity_Id := Real_Range_Specification (Def);
17303 begin
17304 -- Check specified "digits" constraint
17306 if Digs_Val > Digits_Value (E) then
17307 return False;
17308 end if;
17310 -- Check for matching range, if specified
17312 if Present (Spec) then
17313 if Expr_Value_R (Type_Low_Bound (E)) >
17314 Expr_Value_R (Low_Bound (Spec))
17315 then
17316 return False;
17317 end if;
17319 if Expr_Value_R (Type_High_Bound (E)) <
17320 Expr_Value_R (High_Bound (Spec))
17321 then
17322 return False;
17323 end if;
17324 end if;
17326 return True;
17327 end Can_Derive_From;
17329 --------------------
17330 -- Find_Base_Type --
17331 --------------------
17333 function Find_Base_Type return Entity_Id is
17334 Choice : Elmt_Id := First_Elmt (Predefined_Float_Types);
17336 begin
17337 -- Iterate over the predefined types in order, returning the first
17338 -- one that Def can derive from.
17340 while Present (Choice) loop
17341 if Can_Derive_From (Node (Choice)) then
17342 return Node (Choice);
17343 end if;
17345 Next_Elmt (Choice);
17346 end loop;
17348 -- If we can't derive from any existing type, use Long_Long_Float
17349 -- and give appropriate message explaining the problem.
17351 if Digs_Val > Max_Digs_Val then
17352 -- It might be the case that there is a type with the requested
17353 -- range, just not the combination of digits and range.
17355 Error_Msg_N
17356 ("no predefined type has requested range and precision",
17357 Real_Range_Specification (Def));
17359 else
17360 Error_Msg_N
17361 ("range too large for any predefined type",
17362 Real_Range_Specification (Def));
17363 end if;
17365 return Standard_Long_Long_Float;
17366 end Find_Base_Type;
17368 -- Start of processing for Floating_Point_Type_Declaration
17370 begin
17371 Check_Restriction (No_Floating_Point, Def);
17373 -- Create an implicit base type
17375 Implicit_Base :=
17376 Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
17378 -- Analyze and verify digits value
17380 Analyze_And_Resolve (Digs, Any_Integer);
17381 Check_Digits_Expression (Digs);
17382 Digs_Val := Expr_Value (Digs);
17384 -- Process possible range spec and find correct type to derive from
17386 Process_Real_Range_Specification (Def);
17388 -- Check that requested number of digits is not too high.
17390 if Digs_Val > Max_Digs_Val then
17392 -- The check for Max_Base_Digits may be somewhat expensive, as it
17393 -- requires reading System, so only do it when necessary.
17395 declare
17396 Max_Base_Digits : constant Uint :=
17397 Expr_Value
17398 (Expression
17399 (Parent (RTE (RE_Max_Base_Digits))));
17401 begin
17402 if Digs_Val > Max_Base_Digits then
17403 Error_Msg_Uint_1 := Max_Base_Digits;
17404 Error_Msg_N ("digits value out of range, maximum is ^", Digs);
17406 elsif No (Real_Range_Specification (Def)) then
17407 Error_Msg_Uint_1 := Max_Digs_Val;
17408 Error_Msg_N ("types with more than ^ digits need range spec "
17409 & "(RM 3.5.7(6))", Digs);
17410 end if;
17411 end;
17412 end if;
17414 -- Find a suitable type to derive from or complain and use a substitute
17416 Base_Typ := Find_Base_Type;
17418 -- If there are bounds given in the declaration use them as the bounds
17419 -- of the type, otherwise use the bounds of the predefined base type
17420 -- that was chosen based on the Digits value.
17422 if Present (Real_Range_Specification (Def)) then
17423 Set_Scalar_Range (T, Real_Range_Specification (Def));
17424 Set_Is_Constrained (T);
17426 -- The bounds of this range must be converted to machine numbers
17427 -- in accordance with RM 4.9(38).
17429 Bound := Type_Low_Bound (T);
17431 if Nkind (Bound) = N_Real_Literal then
17432 Set_Realval
17433 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
17434 Set_Is_Machine_Number (Bound);
17435 end if;
17437 Bound := Type_High_Bound (T);
17439 if Nkind (Bound) = N_Real_Literal then
17440 Set_Realval
17441 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
17442 Set_Is_Machine_Number (Bound);
17443 end if;
17445 else
17446 Set_Scalar_Range (T, Scalar_Range (Base_Typ));
17447 end if;
17449 -- Complete definition of implicit base and declared first subtype. The
17450 -- inheritance of the rep item chain ensures that SPARK-related pragmas
17451 -- are not clobbered when the floating point type acts as a full view of
17452 -- a private type.
17454 Set_Etype (Implicit_Base, Base_Typ);
17455 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
17456 Set_Size_Info (Implicit_Base, Base_Typ);
17457 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
17458 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
17459 Set_Digits_Value (Implicit_Base, Digits_Value (Base_Typ));
17460 Set_Float_Rep (Implicit_Base, Float_Rep (Base_Typ));
17462 Set_Ekind (T, E_Floating_Point_Subtype);
17463 Set_Etype (T, Implicit_Base);
17464 Set_Size_Info (T, Implicit_Base);
17465 Set_RM_Size (T, RM_Size (Implicit_Base));
17466 Inherit_Rep_Item_Chain (T, Implicit_Base);
17467 Set_Digits_Value (T, Digs_Val);
17468 end Floating_Point_Type_Declaration;
17470 ----------------------------
17471 -- Get_Discriminant_Value --
17472 ----------------------------
17474 -- This is the situation:
17476 -- There is a non-derived type
17478 -- type T0 (Dx, Dy, Dz...)
17480 -- There are zero or more levels of derivation, with each derivation
17481 -- either purely inheriting the discriminants, or defining its own.
17483 -- type Ti is new Ti-1
17484 -- or
17485 -- type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
17486 -- or
17487 -- subtype Ti is ...
17489 -- The subtype issue is avoided by the use of Original_Record_Component,
17490 -- and the fact that derived subtypes also derive the constraints.
17492 -- This chain leads back from
17494 -- Typ_For_Constraint
17496 -- Typ_For_Constraint has discriminants, and the value for each
17497 -- discriminant is given by its corresponding Elmt of Constraints.
17499 -- Discriminant is some discriminant in this hierarchy
17501 -- We need to return its value
17503 -- We do this by recursively searching each level, and looking for
17504 -- Discriminant. Once we get to the bottom, we start backing up
17505 -- returning the value for it which may in turn be a discriminant
17506 -- further up, so on the backup we continue the substitution.
17508 function Get_Discriminant_Value
17509 (Discriminant : Entity_Id;
17510 Typ_For_Constraint : Entity_Id;
17511 Constraint : Elist_Id) return Node_Id
17513 function Root_Corresponding_Discriminant
17514 (Discr : Entity_Id) return Entity_Id;
17515 -- Given a discriminant, traverse the chain of inherited discriminants
17516 -- and return the topmost discriminant.
17518 function Search_Derivation_Levels
17519 (Ti : Entity_Id;
17520 Discrim_Values : Elist_Id;
17521 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
17522 -- This is the routine that performs the recursive search of levels
17523 -- as described above.
17525 -------------------------------------
17526 -- Root_Corresponding_Discriminant --
17527 -------------------------------------
17529 function Root_Corresponding_Discriminant
17530 (Discr : Entity_Id) return Entity_Id
17532 D : Entity_Id;
17534 begin
17535 D := Discr;
17536 while Present (Corresponding_Discriminant (D)) loop
17537 D := Corresponding_Discriminant (D);
17538 end loop;
17540 return D;
17541 end Root_Corresponding_Discriminant;
17543 ------------------------------
17544 -- Search_Derivation_Levels --
17545 ------------------------------
17547 function Search_Derivation_Levels
17548 (Ti : Entity_Id;
17549 Discrim_Values : Elist_Id;
17550 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
17552 Assoc : Elmt_Id;
17553 Disc : Entity_Id;
17554 Result : Node_Or_Entity_Id;
17555 Result_Entity : Node_Id;
17557 begin
17558 -- If inappropriate type, return Error, this happens only in
17559 -- cascaded error situations, and we want to avoid a blow up.
17561 if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
17562 return Error;
17563 end if;
17565 -- Look deeper if possible. Use Stored_Constraints only for
17566 -- untagged types. For tagged types use the given constraint.
17567 -- This asymmetry needs explanation???
17569 if not Stored_Discrim_Values
17570 and then Present (Stored_Constraint (Ti))
17571 and then not Is_Tagged_Type (Ti)
17572 then
17573 Result :=
17574 Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
17575 else
17576 declare
17577 Td : constant Entity_Id := Etype (Ti);
17579 begin
17580 if Td = Ti then
17581 Result := Discriminant;
17583 else
17584 if Present (Stored_Constraint (Ti)) then
17585 Result :=
17586 Search_Derivation_Levels
17587 (Td, Stored_Constraint (Ti), True);
17588 else
17589 Result :=
17590 Search_Derivation_Levels
17591 (Td, Discrim_Values, Stored_Discrim_Values);
17592 end if;
17593 end if;
17594 end;
17595 end if;
17597 -- Extra underlying places to search, if not found above. For
17598 -- concurrent types, the relevant discriminant appears in the
17599 -- corresponding record. For a type derived from a private type
17600 -- without discriminant, the full view inherits the discriminants
17601 -- of the full view of the parent.
17603 if Result = Discriminant then
17604 if Is_Concurrent_Type (Ti)
17605 and then Present (Corresponding_Record_Type (Ti))
17606 then
17607 Result :=
17608 Search_Derivation_Levels (
17609 Corresponding_Record_Type (Ti),
17610 Discrim_Values,
17611 Stored_Discrim_Values);
17613 elsif Is_Private_Type (Ti)
17614 and then not Has_Discriminants (Ti)
17615 and then Present (Full_View (Ti))
17616 and then Etype (Full_View (Ti)) /= Ti
17617 then
17618 Result :=
17619 Search_Derivation_Levels (
17620 Full_View (Ti),
17621 Discrim_Values,
17622 Stored_Discrim_Values);
17623 end if;
17624 end if;
17626 -- If Result is not a (reference to a) discriminant, return it,
17627 -- otherwise set Result_Entity to the discriminant.
17629 if Nkind (Result) = N_Defining_Identifier then
17630 pragma Assert (Result = Discriminant);
17631 Result_Entity := Result;
17633 else
17634 if not Denotes_Discriminant (Result) then
17635 return Result;
17636 end if;
17638 Result_Entity := Entity (Result);
17639 end if;
17641 -- See if this level of derivation actually has discriminants because
17642 -- tagged derivations can add them, hence the lower levels need not
17643 -- have any.
17645 if not Has_Discriminants (Ti) then
17646 return Result;
17647 end if;
17649 -- Scan Ti's discriminants for Result_Entity, and return its
17650 -- corresponding value, if any.
17652 Result_Entity := Original_Record_Component (Result_Entity);
17654 Assoc := First_Elmt (Discrim_Values);
17656 if Stored_Discrim_Values then
17657 Disc := First_Stored_Discriminant (Ti);
17658 else
17659 Disc := First_Discriminant (Ti);
17660 end if;
17662 while Present (Disc) loop
17663 pragma Assert (Present (Assoc));
17665 if Original_Record_Component (Disc) = Result_Entity then
17666 return Node (Assoc);
17667 end if;
17669 Next_Elmt (Assoc);
17671 if Stored_Discrim_Values then
17672 Next_Stored_Discriminant (Disc);
17673 else
17674 Next_Discriminant (Disc);
17675 end if;
17676 end loop;
17678 -- Could not find it
17680 return Result;
17681 end Search_Derivation_Levels;
17683 -- Local Variables
17685 Result : Node_Or_Entity_Id;
17687 -- Start of processing for Get_Discriminant_Value
17689 begin
17690 -- ??? This routine is a gigantic mess and will be deleted. For the
17691 -- time being just test for the trivial case before calling recurse.
17693 if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
17694 declare
17695 D : Entity_Id;
17696 E : Elmt_Id;
17698 begin
17699 D := First_Discriminant (Typ_For_Constraint);
17700 E := First_Elmt (Constraint);
17701 while Present (D) loop
17702 if Chars (D) = Chars (Discriminant) then
17703 return Node (E);
17704 end if;
17706 Next_Discriminant (D);
17707 Next_Elmt (E);
17708 end loop;
17709 end;
17710 end if;
17712 Result := Search_Derivation_Levels
17713 (Typ_For_Constraint, Constraint, False);
17715 -- ??? hack to disappear when this routine is gone
17717 if Nkind (Result) = N_Defining_Identifier then
17718 declare
17719 D : Entity_Id;
17720 E : Elmt_Id;
17722 begin
17723 D := First_Discriminant (Typ_For_Constraint);
17724 E := First_Elmt (Constraint);
17725 while Present (D) loop
17726 if Root_Corresponding_Discriminant (D) = Discriminant then
17727 return Node (E);
17728 end if;
17730 Next_Discriminant (D);
17731 Next_Elmt (E);
17732 end loop;
17733 end;
17734 end if;
17736 pragma Assert (Nkind (Result) /= N_Defining_Identifier);
17737 return Result;
17738 end Get_Discriminant_Value;
17740 --------------------------
17741 -- Has_Range_Constraint --
17742 --------------------------
17744 function Has_Range_Constraint (N : Node_Id) return Boolean is
17745 C : constant Node_Id := Constraint (N);
17747 begin
17748 if Nkind (C) = N_Range_Constraint then
17749 return True;
17751 elsif Nkind (C) = N_Digits_Constraint then
17752 return
17753 Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
17754 or else Present (Range_Constraint (C));
17756 elsif Nkind (C) = N_Delta_Constraint then
17757 return Present (Range_Constraint (C));
17759 else
17760 return False;
17761 end if;
17762 end Has_Range_Constraint;
17764 ------------------------
17765 -- Inherit_Components --
17766 ------------------------
17768 function Inherit_Components
17769 (N : Node_Id;
17770 Parent_Base : Entity_Id;
17771 Derived_Base : Entity_Id;
17772 Is_Tagged : Boolean;
17773 Inherit_Discr : Boolean;
17774 Discs : Elist_Id) return Elist_Id
17776 Assoc_List : constant Elist_Id := New_Elmt_List;
17778 procedure Inherit_Component
17779 (Old_C : Entity_Id;
17780 Plain_Discrim : Boolean := False;
17781 Stored_Discrim : Boolean := False);
17782 -- Inherits component Old_C from Parent_Base to the Derived_Base. If
17783 -- Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
17784 -- True, Old_C is a stored discriminant. If they are both false then
17785 -- Old_C is a regular component.
17787 -----------------------
17788 -- Inherit_Component --
17789 -----------------------
17791 procedure Inherit_Component
17792 (Old_C : Entity_Id;
17793 Plain_Discrim : Boolean := False;
17794 Stored_Discrim : Boolean := False)
17796 procedure Set_Anonymous_Type (Id : Entity_Id);
17797 -- Id denotes the entity of an access discriminant or anonymous
17798 -- access component. Set the type of Id to either the same type of
17799 -- Old_C or create a new one depending on whether the parent and
17800 -- the child types are in the same scope.
17802 ------------------------
17803 -- Set_Anonymous_Type --
17804 ------------------------
17806 procedure Set_Anonymous_Type (Id : Entity_Id) is
17807 Old_Typ : constant Entity_Id := Etype (Old_C);
17809 begin
17810 if Scope (Parent_Base) = Scope (Derived_Base) then
17811 Set_Etype (Id, Old_Typ);
17813 -- The parent and the derived type are in two different scopes.
17814 -- Reuse the type of the original discriminant / component by
17815 -- copying it in order to preserve all attributes.
17817 else
17818 declare
17819 Typ : constant Entity_Id := New_Copy (Old_Typ);
17821 begin
17822 Set_Etype (Id, Typ);
17824 -- Since we do not generate component declarations for
17825 -- inherited components, associate the itype with the
17826 -- derived type.
17828 Set_Associated_Node_For_Itype (Typ, Parent (Derived_Base));
17829 Set_Scope (Typ, Derived_Base);
17830 end;
17831 end if;
17832 end Set_Anonymous_Type;
17834 -- Local variables and constants
17836 New_C : constant Entity_Id := New_Copy (Old_C);
17838 Corr_Discrim : Entity_Id;
17839 Discrim : Entity_Id;
17841 -- Start of processing for Inherit_Component
17843 begin
17844 pragma Assert (not Is_Tagged or not Stored_Discrim);
17846 Set_Parent (New_C, Parent (Old_C));
17848 -- Regular discriminants and components must be inserted in the scope
17849 -- of the Derived_Base. Do it here.
17851 if not Stored_Discrim then
17852 Enter_Name (New_C);
17853 end if;
17855 -- For tagged types the Original_Record_Component must point to
17856 -- whatever this field was pointing to in the parent type. This has
17857 -- already been achieved by the call to New_Copy above.
17859 if not Is_Tagged then
17860 Set_Original_Record_Component (New_C, New_C);
17861 end if;
17863 -- Set the proper type of an access discriminant
17865 if Ekind (New_C) = E_Discriminant
17866 and then Ekind (Etype (New_C)) = E_Anonymous_Access_Type
17867 then
17868 Set_Anonymous_Type (New_C);
17869 end if;
17871 -- If we have inherited a component then see if its Etype contains
17872 -- references to Parent_Base discriminants. In this case, replace
17873 -- these references with the constraints given in Discs. We do not
17874 -- do this for the partial view of private types because this is
17875 -- not needed (only the components of the full view will be used
17876 -- for code generation) and cause problem. We also avoid this
17877 -- transformation in some error situations.
17879 if Ekind (New_C) = E_Component then
17881 -- Set the proper type of an anonymous access component
17883 if Ekind (Etype (New_C)) = E_Anonymous_Access_Type then
17884 Set_Anonymous_Type (New_C);
17886 elsif (Is_Private_Type (Derived_Base)
17887 and then not Is_Generic_Type (Derived_Base))
17888 or else (Is_Empty_Elmt_List (Discs)
17889 and then not Expander_Active)
17890 then
17891 Set_Etype (New_C, Etype (Old_C));
17893 else
17894 -- The current component introduces a circularity of the
17895 -- following kind:
17897 -- limited with Pack_2;
17898 -- package Pack_1 is
17899 -- type T_1 is tagged record
17900 -- Comp : access Pack_2.T_2;
17901 -- ...
17902 -- end record;
17903 -- end Pack_1;
17905 -- with Pack_1;
17906 -- package Pack_2 is
17907 -- type T_2 is new Pack_1.T_1 with ...;
17908 -- end Pack_2;
17910 Set_Etype
17911 (New_C,
17912 Constrain_Component_Type
17913 (Old_C, Derived_Base, N, Parent_Base, Discs));
17914 end if;
17915 end if;
17917 -- In derived tagged types it is illegal to reference a non
17918 -- discriminant component in the parent type. To catch this, mark
17919 -- these components with an Ekind of E_Void. This will be reset in
17920 -- Record_Type_Definition after processing the record extension of
17921 -- the derived type.
17923 -- If the declaration is a private extension, there is no further
17924 -- record extension to process, and the components retain their
17925 -- current kind, because they are visible at this point.
17927 if Is_Tagged and then Ekind (New_C) = E_Component
17928 and then Nkind (N) /= N_Private_Extension_Declaration
17929 then
17930 Set_Ekind (New_C, E_Void);
17931 end if;
17933 if Plain_Discrim then
17934 Set_Corresponding_Discriminant (New_C, Old_C);
17935 Build_Discriminal (New_C);
17937 -- If we are explicitly inheriting a stored discriminant it will be
17938 -- completely hidden.
17940 elsif Stored_Discrim then
17941 Set_Corresponding_Discriminant (New_C, Empty);
17942 Set_Discriminal (New_C, Empty);
17943 Set_Is_Completely_Hidden (New_C);
17945 -- Set the Original_Record_Component of each discriminant in the
17946 -- derived base to point to the corresponding stored that we just
17947 -- created.
17949 Discrim := First_Discriminant (Derived_Base);
17950 while Present (Discrim) loop
17951 Corr_Discrim := Corresponding_Discriminant (Discrim);
17953 -- Corr_Discrim could be missing in an error situation
17955 if Present (Corr_Discrim)
17956 and then Original_Record_Component (Corr_Discrim) = Old_C
17957 then
17958 Set_Original_Record_Component (Discrim, New_C);
17959 end if;
17961 Next_Discriminant (Discrim);
17962 end loop;
17964 Append_Entity (New_C, Derived_Base);
17965 end if;
17967 if not Is_Tagged then
17968 Append_Elmt (Old_C, Assoc_List);
17969 Append_Elmt (New_C, Assoc_List);
17970 end if;
17971 end Inherit_Component;
17973 -- Variables local to Inherit_Component
17975 Loc : constant Source_Ptr := Sloc (N);
17977 Parent_Discrim : Entity_Id;
17978 Stored_Discrim : Entity_Id;
17979 D : Entity_Id;
17980 Component : Entity_Id;
17982 -- Start of processing for Inherit_Components
17984 begin
17985 if not Is_Tagged then
17986 Append_Elmt (Parent_Base, Assoc_List);
17987 Append_Elmt (Derived_Base, Assoc_List);
17988 end if;
17990 -- Inherit parent discriminants if needed
17992 if Inherit_Discr then
17993 Parent_Discrim := First_Discriminant (Parent_Base);
17994 while Present (Parent_Discrim) loop
17995 Inherit_Component (Parent_Discrim, Plain_Discrim => True);
17996 Next_Discriminant (Parent_Discrim);
17997 end loop;
17998 end if;
18000 -- Create explicit stored discrims for untagged types when necessary
18002 if not Has_Unknown_Discriminants (Derived_Base)
18003 and then Has_Discriminants (Parent_Base)
18004 and then not Is_Tagged
18005 and then
18006 (not Inherit_Discr
18007 or else First_Discriminant (Parent_Base) /=
18008 First_Stored_Discriminant (Parent_Base))
18009 then
18010 Stored_Discrim := First_Stored_Discriminant (Parent_Base);
18011 while Present (Stored_Discrim) loop
18012 Inherit_Component (Stored_Discrim, Stored_Discrim => True);
18013 Next_Stored_Discriminant (Stored_Discrim);
18014 end loop;
18015 end if;
18017 -- See if we can apply the second transformation for derived types, as
18018 -- explained in point 6. in the comments above Build_Derived_Record_Type
18019 -- This is achieved by appending Derived_Base discriminants into Discs,
18020 -- which has the side effect of returning a non empty Discs list to the
18021 -- caller of Inherit_Components, which is what we want. This must be
18022 -- done for private derived types if there are explicit stored
18023 -- discriminants, to ensure that we can retrieve the values of the
18024 -- constraints provided in the ancestors.
18026 if Inherit_Discr
18027 and then Is_Empty_Elmt_List (Discs)
18028 and then Present (First_Discriminant (Derived_Base))
18029 and then
18030 (not Is_Private_Type (Derived_Base)
18031 or else Is_Completely_Hidden
18032 (First_Stored_Discriminant (Derived_Base))
18033 or else Is_Generic_Type (Derived_Base))
18034 then
18035 D := First_Discriminant (Derived_Base);
18036 while Present (D) loop
18037 Append_Elmt (New_Occurrence_Of (D, Loc), Discs);
18038 Next_Discriminant (D);
18039 end loop;
18040 end if;
18042 -- Finally, inherit non-discriminant components unless they are not
18043 -- visible because defined or inherited from the full view of the
18044 -- parent. Don't inherit the _parent field of the parent type.
18046 Component := First_Entity (Parent_Base);
18047 while Present (Component) loop
18049 -- Ada 2005 (AI-251): Do not inherit components associated with
18050 -- secondary tags of the parent.
18052 if Ekind (Component) = E_Component
18053 and then Present (Related_Type (Component))
18054 then
18055 null;
18057 elsif Ekind (Component) /= E_Component
18058 or else Chars (Component) = Name_uParent
18059 then
18060 null;
18062 -- If the derived type is within the parent type's declarative
18063 -- region, then the components can still be inherited even though
18064 -- they aren't visible at this point. This can occur for cases
18065 -- such as within public child units where the components must
18066 -- become visible upon entering the child unit's private part.
18068 elsif not Is_Visible_Component (Component)
18069 and then not In_Open_Scopes (Scope (Parent_Base))
18070 then
18071 null;
18073 elsif Ekind_In (Derived_Base, E_Private_Type,
18074 E_Limited_Private_Type)
18075 then
18076 null;
18078 else
18079 Inherit_Component (Component);
18080 end if;
18082 Next_Entity (Component);
18083 end loop;
18085 -- For tagged derived types, inherited discriminants cannot be used in
18086 -- component declarations of the record extension part. To achieve this
18087 -- we mark the inherited discriminants as not visible.
18089 if Is_Tagged and then Inherit_Discr then
18090 D := First_Discriminant (Derived_Base);
18091 while Present (D) loop
18092 Set_Is_Immediately_Visible (D, False);
18093 Next_Discriminant (D);
18094 end loop;
18095 end if;
18097 return Assoc_List;
18098 end Inherit_Components;
18100 -----------------------------
18101 -- Inherit_Predicate_Flags --
18102 -----------------------------
18104 procedure Inherit_Predicate_Flags (Subt, Par : Entity_Id) is
18105 begin
18106 Set_Has_Predicates (Subt, Has_Predicates (Par));
18107 Set_Has_Static_Predicate_Aspect
18108 (Subt, Has_Static_Predicate_Aspect (Par));
18109 Set_Has_Dynamic_Predicate_Aspect
18110 (Subt, Has_Dynamic_Predicate_Aspect (Par));
18111 end Inherit_Predicate_Flags;
18113 ----------------------
18114 -- Is_EVF_Procedure --
18115 ----------------------
18117 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean is
18118 Formal : Entity_Id;
18120 begin
18121 -- Examine the formals of an Extensions_Visible False procedure looking
18122 -- for a controlling OUT parameter.
18124 if Ekind (Subp) = E_Procedure
18125 and then Extensions_Visible_Status (Subp) = Extensions_Visible_False
18126 then
18127 Formal := First_Formal (Subp);
18128 while Present (Formal) loop
18129 if Ekind (Formal) = E_Out_Parameter
18130 and then Is_Controlling_Formal (Formal)
18131 then
18132 return True;
18133 end if;
18135 Next_Formal (Formal);
18136 end loop;
18137 end if;
18139 return False;
18140 end Is_EVF_Procedure;
18142 -----------------------
18143 -- Is_Null_Extension --
18144 -----------------------
18146 function Is_Null_Extension (T : Entity_Id) return Boolean is
18147 Type_Decl : constant Node_Id := Parent (Base_Type (T));
18148 Comp_List : Node_Id;
18149 Comp : Node_Id;
18151 begin
18152 if Nkind (Type_Decl) /= N_Full_Type_Declaration
18153 or else not Is_Tagged_Type (T)
18154 or else Nkind (Type_Definition (Type_Decl)) /=
18155 N_Derived_Type_Definition
18156 or else No (Record_Extension_Part (Type_Definition (Type_Decl)))
18157 then
18158 return False;
18159 end if;
18161 Comp_List :=
18162 Component_List (Record_Extension_Part (Type_Definition (Type_Decl)));
18164 if Present (Discriminant_Specifications (Type_Decl)) then
18165 return False;
18167 elsif Present (Comp_List)
18168 and then Is_Non_Empty_List (Component_Items (Comp_List))
18169 then
18170 Comp := First (Component_Items (Comp_List));
18172 -- Only user-defined components are relevant. The component list
18173 -- may also contain a parent component and internal components
18174 -- corresponding to secondary tags, but these do not determine
18175 -- whether this is a null extension.
18177 while Present (Comp) loop
18178 if Comes_From_Source (Comp) then
18179 return False;
18180 end if;
18182 Next (Comp);
18183 end loop;
18185 return True;
18187 else
18188 return True;
18189 end if;
18190 end Is_Null_Extension;
18192 ------------------------------
18193 -- Is_Valid_Constraint_Kind --
18194 ------------------------------
18196 function Is_Valid_Constraint_Kind
18197 (T_Kind : Type_Kind;
18198 Constraint_Kind : Node_Kind) return Boolean
18200 begin
18201 case T_Kind is
18202 when Enumeration_Kind
18203 | Integer_Kind
18205 return Constraint_Kind = N_Range_Constraint;
18207 when Decimal_Fixed_Point_Kind =>
18208 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
18209 N_Range_Constraint);
18211 when Ordinary_Fixed_Point_Kind =>
18212 return Nkind_In (Constraint_Kind, N_Delta_Constraint,
18213 N_Range_Constraint);
18215 when Float_Kind =>
18216 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
18217 N_Range_Constraint);
18219 when Access_Kind
18220 | Array_Kind
18221 | Class_Wide_Kind
18222 | Concurrent_Kind
18223 | Private_Kind
18224 | E_Incomplete_Type
18225 | E_Record_Subtype
18226 | E_Record_Type
18228 return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
18230 when others =>
18231 return True; -- Error will be detected later
18232 end case;
18233 end Is_Valid_Constraint_Kind;
18235 --------------------------
18236 -- Is_Visible_Component --
18237 --------------------------
18239 function Is_Visible_Component
18240 (C : Entity_Id;
18241 N : Node_Id := Empty) return Boolean
18243 Original_Comp : Entity_Id := Empty;
18244 Original_Type : Entity_Id;
18245 Type_Scope : Entity_Id;
18247 function Is_Local_Type (Typ : Entity_Id) return Boolean;
18248 -- Check whether parent type of inherited component is declared locally,
18249 -- possibly within a nested package or instance. The current scope is
18250 -- the derived record itself.
18252 -------------------
18253 -- Is_Local_Type --
18254 -------------------
18256 function Is_Local_Type (Typ : Entity_Id) return Boolean is
18257 Scop : Entity_Id;
18259 begin
18260 Scop := Scope (Typ);
18261 while Present (Scop)
18262 and then Scop /= Standard_Standard
18263 loop
18264 if Scop = Scope (Current_Scope) then
18265 return True;
18266 end if;
18268 Scop := Scope (Scop);
18269 end loop;
18271 return False;
18272 end Is_Local_Type;
18274 -- Start of processing for Is_Visible_Component
18276 begin
18277 if Ekind_In (C, E_Component, E_Discriminant) then
18278 Original_Comp := Original_Record_Component (C);
18279 end if;
18281 if No (Original_Comp) then
18283 -- Premature usage, or previous error
18285 return False;
18287 else
18288 Original_Type := Scope (Original_Comp);
18289 Type_Scope := Scope (Base_Type (Scope (C)));
18290 end if;
18292 -- This test only concerns tagged types
18294 if not Is_Tagged_Type (Original_Type) then
18295 return True;
18297 -- If it is _Parent or _Tag, there is no visibility issue
18299 elsif not Comes_From_Source (Original_Comp) then
18300 return True;
18302 -- Discriminants are visible unless the (private) type has unknown
18303 -- discriminants. If the discriminant reference is inserted for a
18304 -- discriminant check on a full view it is also visible.
18306 elsif Ekind (Original_Comp) = E_Discriminant
18307 and then
18308 (not Has_Unknown_Discriminants (Original_Type)
18309 or else (Present (N)
18310 and then Nkind (N) = N_Selected_Component
18311 and then Nkind (Prefix (N)) = N_Type_Conversion
18312 and then not Comes_From_Source (Prefix (N))))
18313 then
18314 return True;
18316 -- In the body of an instantiation, check the visibility of a component
18317 -- in case it has a homograph that is a primitive operation of a private
18318 -- type which was not visible in the generic unit.
18320 -- Should Is_Prefixed_Call be propagated from template to instance???
18322 elsif In_Instance_Body then
18323 if not Is_Tagged_Type (Original_Type)
18324 or else not Is_Private_Type (Original_Type)
18325 then
18326 return True;
18328 else
18329 declare
18330 Subp_Elmt : Elmt_Id;
18332 begin
18333 Subp_Elmt := First_Elmt (Primitive_Operations (Original_Type));
18334 while Present (Subp_Elmt) loop
18336 -- The component is hidden by a primitive operation
18338 if Chars (Node (Subp_Elmt)) = Chars (C) then
18339 return False;
18340 end if;
18342 Next_Elmt (Subp_Elmt);
18343 end loop;
18345 return True;
18346 end;
18347 end if;
18349 -- If the component has been declared in an ancestor which is currently
18350 -- a private type, then it is not visible. The same applies if the
18351 -- component's containing type is not in an open scope and the original
18352 -- component's enclosing type is a visible full view of a private type
18353 -- (which can occur in cases where an attempt is being made to reference
18354 -- a component in a sibling package that is inherited from a visible
18355 -- component of a type in an ancestor package; the component in the
18356 -- sibling package should not be visible even though the component it
18357 -- inherited from is visible). This does not apply however in the case
18358 -- where the scope of the type is a private child unit, or when the
18359 -- parent comes from a local package in which the ancestor is currently
18360 -- visible. The latter suppression of visibility is needed for cases
18361 -- that are tested in B730006.
18363 elsif Is_Private_Type (Original_Type)
18364 or else
18365 (not Is_Private_Descendant (Type_Scope)
18366 and then not In_Open_Scopes (Type_Scope)
18367 and then Has_Private_Declaration (Original_Type))
18368 then
18369 -- If the type derives from an entity in a formal package, there
18370 -- are no additional visible components.
18372 if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
18373 N_Formal_Package_Declaration
18374 then
18375 return False;
18377 -- if we are not in the private part of the current package, there
18378 -- are no additional visible components.
18380 elsif Ekind (Scope (Current_Scope)) = E_Package
18381 and then not In_Private_Part (Scope (Current_Scope))
18382 then
18383 return False;
18384 else
18385 return
18386 Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
18387 and then In_Open_Scopes (Scope (Original_Type))
18388 and then Is_Local_Type (Type_Scope);
18389 end if;
18391 -- There is another weird way in which a component may be invisible when
18392 -- the private and the full view are not derived from the same ancestor.
18393 -- Here is an example :
18395 -- type A1 is tagged record F1 : integer; end record;
18396 -- type A2 is new A1 with record F2 : integer; end record;
18397 -- type T is new A1 with private;
18398 -- private
18399 -- type T is new A2 with null record;
18401 -- In this case, the full view of T inherits F1 and F2 but the private
18402 -- view inherits only F1
18404 else
18405 declare
18406 Ancestor : Entity_Id := Scope (C);
18408 begin
18409 loop
18410 if Ancestor = Original_Type then
18411 return True;
18413 -- The ancestor may have a partial view of the original type,
18414 -- but if the full view is in scope, as in a child body, the
18415 -- component is visible.
18417 elsif In_Private_Part (Scope (Original_Type))
18418 and then Full_View (Ancestor) = Original_Type
18419 then
18420 return True;
18422 elsif Ancestor = Etype (Ancestor) then
18424 -- No further ancestors to examine
18426 return False;
18427 end if;
18429 Ancestor := Etype (Ancestor);
18430 end loop;
18431 end;
18432 end if;
18433 end Is_Visible_Component;
18435 --------------------------
18436 -- Make_Class_Wide_Type --
18437 --------------------------
18439 procedure Make_Class_Wide_Type (T : Entity_Id) is
18440 CW_Type : Entity_Id;
18441 CW_Name : Name_Id;
18442 Next_E : Entity_Id;
18444 begin
18445 if Present (Class_Wide_Type (T)) then
18447 -- The class-wide type is a partially decorated entity created for a
18448 -- unanalyzed tagged type referenced through a limited with clause.
18449 -- When the tagged type is analyzed, its class-wide type needs to be
18450 -- redecorated. Note that we reuse the entity created by Decorate_
18451 -- Tagged_Type in order to preserve all links.
18453 if Materialize_Entity (Class_Wide_Type (T)) then
18454 CW_Type := Class_Wide_Type (T);
18455 Set_Materialize_Entity (CW_Type, False);
18457 -- The class wide type can have been defined by the partial view, in
18458 -- which case everything is already done.
18460 else
18461 return;
18462 end if;
18464 -- Default case, we need to create a new class-wide type
18466 else
18467 CW_Type :=
18468 New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
18469 end if;
18471 -- Inherit root type characteristics
18473 CW_Name := Chars (CW_Type);
18474 Next_E := Next_Entity (CW_Type);
18475 Copy_Node (T, CW_Type);
18476 Set_Comes_From_Source (CW_Type, False);
18477 Set_Chars (CW_Type, CW_Name);
18478 Set_Parent (CW_Type, Parent (T));
18479 Set_Next_Entity (CW_Type, Next_E);
18481 -- Ensure we have a new freeze node for the class-wide type. The partial
18482 -- view may have freeze action of its own, requiring a proper freeze
18483 -- node, and the same freeze node cannot be shared between the two
18484 -- types.
18486 Set_Has_Delayed_Freeze (CW_Type);
18487 Set_Freeze_Node (CW_Type, Empty);
18489 -- Customize the class-wide type: It has no prim. op., it cannot be
18490 -- abstract, its Etype points back to the specific root type, and it
18491 -- cannot have any invariants.
18493 Set_Ekind (CW_Type, E_Class_Wide_Type);
18494 Set_Is_Tagged_Type (CW_Type, True);
18495 Set_Direct_Primitive_Operations (CW_Type, New_Elmt_List);
18496 Set_Is_Abstract_Type (CW_Type, False);
18497 Set_Is_Constrained (CW_Type, False);
18498 Set_Is_First_Subtype (CW_Type, Is_First_Subtype (T));
18499 Set_Default_SSO (CW_Type);
18500 Set_Has_Inheritable_Invariants (CW_Type, False);
18501 Set_Has_Inherited_Invariants (CW_Type, False);
18502 Set_Has_Own_Invariants (CW_Type, False);
18504 if Ekind (T) = E_Class_Wide_Subtype then
18505 Set_Etype (CW_Type, Etype (Base_Type (T)));
18506 else
18507 Set_Etype (CW_Type, T);
18508 end if;
18510 Set_No_Tagged_Streams_Pragma (CW_Type, No_Tagged_Streams);
18512 -- If this is the class_wide type of a constrained subtype, it does
18513 -- not have discriminants.
18515 Set_Has_Discriminants (CW_Type,
18516 Has_Discriminants (T) and then not Is_Constrained (T));
18518 Set_Has_Unknown_Discriminants (CW_Type, True);
18519 Set_Class_Wide_Type (T, CW_Type);
18520 Set_Equivalent_Type (CW_Type, Empty);
18522 -- The class-wide type of a class-wide type is itself (RM 3.9(14))
18524 Set_Class_Wide_Type (CW_Type, CW_Type);
18525 end Make_Class_Wide_Type;
18527 ----------------
18528 -- Make_Index --
18529 ----------------
18531 procedure Make_Index
18532 (N : Node_Id;
18533 Related_Nod : Node_Id;
18534 Related_Id : Entity_Id := Empty;
18535 Suffix_Index : Nat := 1;
18536 In_Iter_Schm : Boolean := False)
18538 R : Node_Id;
18539 T : Entity_Id;
18540 Def_Id : Entity_Id := Empty;
18541 Found : Boolean := False;
18543 begin
18544 -- For a discrete range used in a constrained array definition and
18545 -- defined by a range, an implicit conversion to the predefined type
18546 -- INTEGER is assumed if each bound is either a numeric literal, a named
18547 -- number, or an attribute, and the type of both bounds (prior to the
18548 -- implicit conversion) is the type universal_integer. Otherwise, both
18549 -- bounds must be of the same discrete type, other than universal
18550 -- integer; this type must be determinable independently of the
18551 -- context, but using the fact that the type must be discrete and that
18552 -- both bounds must have the same type.
18554 -- Character literals also have a universal type in the absence of
18555 -- of additional context, and are resolved to Standard_Character.
18557 if Nkind (N) = N_Range then
18559 -- The index is given by a range constraint. The bounds are known
18560 -- to be of a consistent type.
18562 if not Is_Overloaded (N) then
18563 T := Etype (N);
18565 -- For universal bounds, choose the specific predefined type
18567 if T = Universal_Integer then
18568 T := Standard_Integer;
18570 elsif T = Any_Character then
18571 Ambiguous_Character (Low_Bound (N));
18573 T := Standard_Character;
18574 end if;
18576 -- The node may be overloaded because some user-defined operators
18577 -- are available, but if a universal interpretation exists it is
18578 -- also the selected one.
18580 elsif Universal_Interpretation (N) = Universal_Integer then
18581 T := Standard_Integer;
18583 else
18584 T := Any_Type;
18586 declare
18587 Ind : Interp_Index;
18588 It : Interp;
18590 begin
18591 Get_First_Interp (N, Ind, It);
18592 while Present (It.Typ) loop
18593 if Is_Discrete_Type (It.Typ) then
18595 if Found
18596 and then not Covers (It.Typ, T)
18597 and then not Covers (T, It.Typ)
18598 then
18599 Error_Msg_N ("ambiguous bounds in discrete range", N);
18600 exit;
18601 else
18602 T := It.Typ;
18603 Found := True;
18604 end if;
18605 end if;
18607 Get_Next_Interp (Ind, It);
18608 end loop;
18610 if T = Any_Type then
18611 Error_Msg_N ("discrete type required for range", N);
18612 Set_Etype (N, Any_Type);
18613 return;
18615 elsif T = Universal_Integer then
18616 T := Standard_Integer;
18617 end if;
18618 end;
18619 end if;
18621 if not Is_Discrete_Type (T) then
18622 Error_Msg_N ("discrete type required for range", N);
18623 Set_Etype (N, Any_Type);
18624 return;
18625 end if;
18627 if Nkind (Low_Bound (N)) = N_Attribute_Reference
18628 and then Attribute_Name (Low_Bound (N)) = Name_First
18629 and then Is_Entity_Name (Prefix (Low_Bound (N)))
18630 and then Is_Type (Entity (Prefix (Low_Bound (N))))
18631 and then Is_Discrete_Type (Entity (Prefix (Low_Bound (N))))
18632 then
18633 -- The type of the index will be the type of the prefix, as long
18634 -- as the upper bound is 'Last of the same type.
18636 Def_Id := Entity (Prefix (Low_Bound (N)));
18638 if Nkind (High_Bound (N)) /= N_Attribute_Reference
18639 or else Attribute_Name (High_Bound (N)) /= Name_Last
18640 or else not Is_Entity_Name (Prefix (High_Bound (N)))
18641 or else Entity (Prefix (High_Bound (N))) /= Def_Id
18642 then
18643 Def_Id := Empty;
18644 end if;
18645 end if;
18647 R := N;
18648 Process_Range_Expr_In_Decl (R, T, In_Iter_Schm => In_Iter_Schm);
18650 elsif Nkind (N) = N_Subtype_Indication then
18652 -- The index is given by a subtype with a range constraint
18654 T := Base_Type (Entity (Subtype_Mark (N)));
18656 if not Is_Discrete_Type (T) then
18657 Error_Msg_N ("discrete type required for range", N);
18658 Set_Etype (N, Any_Type);
18659 return;
18660 end if;
18662 R := Range_Expression (Constraint (N));
18664 Resolve (R, T);
18665 Process_Range_Expr_In_Decl
18666 (R, Entity (Subtype_Mark (N)), In_Iter_Schm => In_Iter_Schm);
18668 elsif Nkind (N) = N_Attribute_Reference then
18670 -- Catch beginner's error (use of attribute other than 'Range)
18672 if Attribute_Name (N) /= Name_Range then
18673 Error_Msg_N ("expect attribute ''Range", N);
18674 Set_Etype (N, Any_Type);
18675 return;
18676 end if;
18678 -- If the node denotes the range of a type mark, that is also the
18679 -- resulting type, and we do not need to create an Itype for it.
18681 if Is_Entity_Name (Prefix (N))
18682 and then Comes_From_Source (N)
18683 and then Is_Type (Entity (Prefix (N)))
18684 and then Is_Discrete_Type (Entity (Prefix (N)))
18685 then
18686 Def_Id := Entity (Prefix (N));
18687 end if;
18689 Analyze_And_Resolve (N);
18690 T := Etype (N);
18691 R := N;
18693 -- If none of the above, must be a subtype. We convert this to a
18694 -- range attribute reference because in the case of declared first
18695 -- named subtypes, the types in the range reference can be different
18696 -- from the type of the entity. A range attribute normalizes the
18697 -- reference and obtains the correct types for the bounds.
18699 -- This transformation is in the nature of an expansion, is only
18700 -- done if expansion is active. In particular, it is not done on
18701 -- formal generic types, because we need to retain the name of the
18702 -- original index for instantiation purposes.
18704 else
18705 if not Is_Entity_Name (N) or else not Is_Type (Entity (N)) then
18706 Error_Msg_N ("invalid subtype mark in discrete range ", N);
18707 Set_Etype (N, Any_Integer);
18708 return;
18710 else
18711 -- The type mark may be that of an incomplete type. It is only
18712 -- now that we can get the full view, previous analysis does
18713 -- not look specifically for a type mark.
18715 Set_Entity (N, Get_Full_View (Entity (N)));
18716 Set_Etype (N, Entity (N));
18717 Def_Id := Entity (N);
18719 if not Is_Discrete_Type (Def_Id) then
18720 Error_Msg_N ("discrete type required for index", N);
18721 Set_Etype (N, Any_Type);
18722 return;
18723 end if;
18724 end if;
18726 if Expander_Active then
18727 Rewrite (N,
18728 Make_Attribute_Reference (Sloc (N),
18729 Attribute_Name => Name_Range,
18730 Prefix => Relocate_Node (N)));
18732 -- The original was a subtype mark that does not freeze. This
18733 -- means that the rewritten version must not freeze either.
18735 Set_Must_Not_Freeze (N);
18736 Set_Must_Not_Freeze (Prefix (N));
18737 Analyze_And_Resolve (N);
18738 T := Etype (N);
18739 R := N;
18741 -- If expander is inactive, type is legal, nothing else to construct
18743 else
18744 return;
18745 end if;
18746 end if;
18748 if not Is_Discrete_Type (T) then
18749 Error_Msg_N ("discrete type required for range", N);
18750 Set_Etype (N, Any_Type);
18751 return;
18753 elsif T = Any_Type then
18754 Set_Etype (N, Any_Type);
18755 return;
18756 end if;
18758 -- We will now create the appropriate Itype to describe the range, but
18759 -- first a check. If we originally had a subtype, then we just label
18760 -- the range with this subtype. Not only is there no need to construct
18761 -- a new subtype, but it is wrong to do so for two reasons:
18763 -- 1. A legality concern, if we have a subtype, it must not freeze,
18764 -- and the Itype would cause freezing incorrectly
18766 -- 2. An efficiency concern, if we created an Itype, it would not be
18767 -- recognized as the same type for the purposes of eliminating
18768 -- checks in some circumstances.
18770 -- We signal this case by setting the subtype entity in Def_Id
18772 if No (Def_Id) then
18773 Def_Id :=
18774 Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
18775 Set_Etype (Def_Id, Base_Type (T));
18777 if Is_Signed_Integer_Type (T) then
18778 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
18780 elsif Is_Modular_Integer_Type (T) then
18781 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
18783 else
18784 Set_Ekind (Def_Id, E_Enumeration_Subtype);
18785 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
18786 Set_First_Literal (Def_Id, First_Literal (T));
18787 end if;
18789 Set_Size_Info (Def_Id, (T));
18790 Set_RM_Size (Def_Id, RM_Size (T));
18791 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
18793 Set_Scalar_Range (Def_Id, R);
18794 Conditional_Delay (Def_Id, T);
18796 if Nkind (N) = N_Subtype_Indication then
18797 Inherit_Predicate_Flags (Def_Id, Entity (Subtype_Mark (N)));
18798 end if;
18800 -- In the subtype indication case, if the immediate parent of the
18801 -- new subtype is non-static, then the subtype we create is non-
18802 -- static, even if its bounds are static.
18804 if Nkind (N) = N_Subtype_Indication
18805 and then not Is_OK_Static_Subtype (Entity (Subtype_Mark (N)))
18806 then
18807 Set_Is_Non_Static_Subtype (Def_Id);
18808 end if;
18809 end if;
18811 -- Final step is to label the index with this constructed type
18813 Set_Etype (N, Def_Id);
18814 end Make_Index;
18816 ------------------------------
18817 -- Modular_Type_Declaration --
18818 ------------------------------
18820 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
18821 Mod_Expr : constant Node_Id := Expression (Def);
18822 M_Val : Uint;
18824 procedure Set_Modular_Size (Bits : Int);
18825 -- Sets RM_Size to Bits, and Esize to normal word size above this
18827 ----------------------
18828 -- Set_Modular_Size --
18829 ----------------------
18831 procedure Set_Modular_Size (Bits : Int) is
18832 begin
18833 Set_RM_Size (T, UI_From_Int (Bits));
18835 if Bits <= 8 then
18836 Init_Esize (T, 8);
18838 elsif Bits <= 16 then
18839 Init_Esize (T, 16);
18841 elsif Bits <= 32 then
18842 Init_Esize (T, 32);
18844 else
18845 Init_Esize (T, System_Max_Binary_Modulus_Power);
18846 end if;
18848 if not Non_Binary_Modulus (T) and then Esize (T) = RM_Size (T) then
18849 Set_Is_Known_Valid (T);
18850 end if;
18851 end Set_Modular_Size;
18853 -- Start of processing for Modular_Type_Declaration
18855 begin
18856 -- If the mod expression is (exactly) 2 * literal, where literal is
18857 -- 64 or less,then almost certainly the * was meant to be **. Warn.
18859 if Warn_On_Suspicious_Modulus_Value
18860 and then Nkind (Mod_Expr) = N_Op_Multiply
18861 and then Nkind (Left_Opnd (Mod_Expr)) = N_Integer_Literal
18862 and then Intval (Left_Opnd (Mod_Expr)) = Uint_2
18863 and then Nkind (Right_Opnd (Mod_Expr)) = N_Integer_Literal
18864 and then Intval (Right_Opnd (Mod_Expr)) <= Uint_64
18865 then
18866 Error_Msg_N
18867 ("suspicious MOD value, was '*'* intended'??M?", Mod_Expr);
18868 end if;
18870 -- Proceed with analysis of mod expression
18872 Analyze_And_Resolve (Mod_Expr, Any_Integer);
18873 Set_Etype (T, T);
18874 Set_Ekind (T, E_Modular_Integer_Type);
18875 Init_Alignment (T);
18876 Set_Is_Constrained (T);
18878 if not Is_OK_Static_Expression (Mod_Expr) then
18879 Flag_Non_Static_Expr
18880 ("non-static expression used for modular type bound!", Mod_Expr);
18881 M_Val := 2 ** System_Max_Binary_Modulus_Power;
18882 else
18883 M_Val := Expr_Value (Mod_Expr);
18884 end if;
18886 if M_Val < 1 then
18887 Error_Msg_N ("modulus value must be positive", Mod_Expr);
18888 M_Val := 2 ** System_Max_Binary_Modulus_Power;
18889 end if;
18891 if M_Val > 2 ** Standard_Long_Integer_Size then
18892 Check_Restriction (No_Long_Long_Integers, Mod_Expr);
18893 end if;
18895 Set_Modulus (T, M_Val);
18897 -- Create bounds for the modular type based on the modulus given in
18898 -- the type declaration and then analyze and resolve those bounds.
18900 Set_Scalar_Range (T,
18901 Make_Range (Sloc (Mod_Expr),
18902 Low_Bound => Make_Integer_Literal (Sloc (Mod_Expr), 0),
18903 High_Bound => Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
18905 -- Properly analyze the literals for the range. We do this manually
18906 -- because we can't go calling Resolve, since we are resolving these
18907 -- bounds with the type, and this type is certainly not complete yet.
18909 Set_Etype (Low_Bound (Scalar_Range (T)), T);
18910 Set_Etype (High_Bound (Scalar_Range (T)), T);
18911 Set_Is_Static_Expression (Low_Bound (Scalar_Range (T)));
18912 Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
18914 -- Loop through powers of two to find number of bits required
18916 for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
18918 -- Binary case
18920 if M_Val = 2 ** Bits then
18921 Set_Modular_Size (Bits);
18922 return;
18924 -- Nonbinary case
18926 elsif M_Val < 2 ** Bits then
18927 Check_SPARK_05_Restriction ("modulus should be a power of 2", T);
18928 Set_Non_Binary_Modulus (T);
18930 if Bits > System_Max_Nonbinary_Modulus_Power then
18931 Error_Msg_Uint_1 :=
18932 UI_From_Int (System_Max_Nonbinary_Modulus_Power);
18933 Error_Msg_F
18934 ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
18935 Set_Modular_Size (System_Max_Binary_Modulus_Power);
18936 return;
18938 else
18939 -- In the nonbinary case, set size as per RM 13.3(55)
18941 Set_Modular_Size (Bits);
18942 return;
18943 end if;
18944 end if;
18946 end loop;
18948 -- If we fall through, then the size exceed System.Max_Binary_Modulus
18949 -- so we just signal an error and set the maximum size.
18951 Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
18952 Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
18954 Set_Modular_Size (System_Max_Binary_Modulus_Power);
18955 Init_Alignment (T);
18957 end Modular_Type_Declaration;
18959 --------------------------
18960 -- New_Concatenation_Op --
18961 --------------------------
18963 procedure New_Concatenation_Op (Typ : Entity_Id) is
18964 Loc : constant Source_Ptr := Sloc (Typ);
18965 Op : Entity_Id;
18967 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
18968 -- Create abbreviated declaration for the formal of a predefined
18969 -- Operator 'Op' of type 'Typ'
18971 --------------------
18972 -- Make_Op_Formal --
18973 --------------------
18975 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
18976 Formal : Entity_Id;
18977 begin
18978 Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
18979 Set_Etype (Formal, Typ);
18980 Set_Mechanism (Formal, Default_Mechanism);
18981 return Formal;
18982 end Make_Op_Formal;
18984 -- Start of processing for New_Concatenation_Op
18986 begin
18987 Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
18989 Set_Ekind (Op, E_Operator);
18990 Set_Scope (Op, Current_Scope);
18991 Set_Etype (Op, Typ);
18992 Set_Homonym (Op, Get_Name_Entity_Id (Name_Op_Concat));
18993 Set_Is_Immediately_Visible (Op);
18994 Set_Is_Intrinsic_Subprogram (Op);
18995 Set_Has_Completion (Op);
18996 Append_Entity (Op, Current_Scope);
18998 Set_Name_Entity_Id (Name_Op_Concat, Op);
19000 Append_Entity (Make_Op_Formal (Typ, Op), Op);
19001 Append_Entity (Make_Op_Formal (Typ, Op), Op);
19002 end New_Concatenation_Op;
19004 -------------------------
19005 -- OK_For_Limited_Init --
19006 -------------------------
19008 -- ???Check all calls of this, and compare the conditions under which it's
19009 -- called.
19011 function OK_For_Limited_Init
19012 (Typ : Entity_Id;
19013 Exp : Node_Id) return Boolean
19015 begin
19016 return Is_CPP_Constructor_Call (Exp)
19017 or else (Ada_Version >= Ada_2005
19018 and then not Debug_Flag_Dot_L
19019 and then OK_For_Limited_Init_In_05 (Typ, Exp));
19020 end OK_For_Limited_Init;
19022 -------------------------------
19023 -- OK_For_Limited_Init_In_05 --
19024 -------------------------------
19026 function OK_For_Limited_Init_In_05
19027 (Typ : Entity_Id;
19028 Exp : Node_Id) return Boolean
19030 begin
19031 -- An object of a limited interface type can be initialized with any
19032 -- expression of a nonlimited descendant type. However this does not
19033 -- apply if this is a view conversion of some other expression. This
19034 -- is checked below.
19036 if Is_Class_Wide_Type (Typ)
19037 and then Is_Limited_Interface (Typ)
19038 and then not Is_Limited_Type (Etype (Exp))
19039 and then Nkind (Exp) /= N_Type_Conversion
19040 then
19041 return True;
19042 end if;
19044 -- Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
19045 -- case of limited aggregates (including extension aggregates), and
19046 -- function calls. The function call may have been given in prefixed
19047 -- notation, in which case the original node is an indexed component.
19048 -- If the function is parameterless, the original node was an explicit
19049 -- dereference. The function may also be parameterless, in which case
19050 -- the source node is just an identifier.
19052 -- A branch of a conditional expression may have been removed if the
19053 -- condition is statically known. This happens during expansion, and
19054 -- thus will not happen if previous errors were encountered. The check
19055 -- will have been performed on the chosen branch, which replaces the
19056 -- original conditional expression.
19058 if No (Exp) then
19059 return True;
19060 end if;
19062 case Nkind (Original_Node (Exp)) is
19063 when N_Aggregate
19064 | N_Extension_Aggregate
19065 | N_Function_Call
19066 | N_Op
19068 return True;
19070 when N_Identifier =>
19071 return Present (Entity (Original_Node (Exp)))
19072 and then Ekind (Entity (Original_Node (Exp))) = E_Function;
19074 when N_Qualified_Expression =>
19075 return
19076 OK_For_Limited_Init_In_05
19077 (Typ, Expression (Original_Node (Exp)));
19079 -- Ada 2005 (AI-251): If a class-wide interface object is initialized
19080 -- with a function call, the expander has rewritten the call into an
19081 -- N_Type_Conversion node to force displacement of the pointer to
19082 -- reference the component containing the secondary dispatch table.
19083 -- Otherwise a type conversion is not a legal context.
19084 -- A return statement for a build-in-place function returning a
19085 -- synchronized type also introduces an unchecked conversion.
19087 when N_Type_Conversion
19088 | N_Unchecked_Type_Conversion
19090 return not Comes_From_Source (Exp)
19091 and then
19092 OK_For_Limited_Init_In_05
19093 (Typ, Expression (Original_Node (Exp)));
19095 when N_Explicit_Dereference
19096 | N_Indexed_Component
19097 | N_Selected_Component
19099 return Nkind (Exp) = N_Function_Call;
19101 -- A use of 'Input is a function call, hence allowed. Normally the
19102 -- attribute will be changed to a call, but the attribute by itself
19103 -- can occur with -gnatc.
19105 when N_Attribute_Reference =>
19106 return Attribute_Name (Original_Node (Exp)) = Name_Input;
19108 -- For a case expression, all dependent expressions must be legal
19110 when N_Case_Expression =>
19111 declare
19112 Alt : Node_Id;
19114 begin
19115 Alt := First (Alternatives (Original_Node (Exp)));
19116 while Present (Alt) loop
19117 if not OK_For_Limited_Init_In_05 (Typ, Expression (Alt)) then
19118 return False;
19119 end if;
19121 Next (Alt);
19122 end loop;
19124 return True;
19125 end;
19127 -- For an if expression, all dependent expressions must be legal
19129 when N_If_Expression =>
19130 declare
19131 Then_Expr : constant Node_Id :=
19132 Next (First (Expressions (Original_Node (Exp))));
19133 Else_Expr : constant Node_Id := Next (Then_Expr);
19134 begin
19135 return OK_For_Limited_Init_In_05 (Typ, Then_Expr)
19136 and then
19137 OK_For_Limited_Init_In_05 (Typ, Else_Expr);
19138 end;
19140 when others =>
19141 return False;
19142 end case;
19143 end OK_For_Limited_Init_In_05;
19145 -------------------------------------------
19146 -- Ordinary_Fixed_Point_Type_Declaration --
19147 -------------------------------------------
19149 procedure Ordinary_Fixed_Point_Type_Declaration
19150 (T : Entity_Id;
19151 Def : Node_Id)
19153 Loc : constant Source_Ptr := Sloc (Def);
19154 Delta_Expr : constant Node_Id := Delta_Expression (Def);
19155 RRS : constant Node_Id := Real_Range_Specification (Def);
19156 Implicit_Base : Entity_Id;
19157 Delta_Val : Ureal;
19158 Small_Val : Ureal;
19159 Low_Val : Ureal;
19160 High_Val : Ureal;
19162 begin
19163 Check_Restriction (No_Fixed_Point, Def);
19165 -- Create implicit base type
19167 Implicit_Base :=
19168 Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
19169 Set_Etype (Implicit_Base, Implicit_Base);
19171 -- Analyze and process delta expression
19173 Analyze_And_Resolve (Delta_Expr, Any_Real);
19175 Check_Delta_Expression (Delta_Expr);
19176 Delta_Val := Expr_Value_R (Delta_Expr);
19178 Set_Delta_Value (Implicit_Base, Delta_Val);
19180 -- Compute default small from given delta, which is the largest power
19181 -- of two that does not exceed the given delta value.
19183 declare
19184 Tmp : Ureal;
19185 Scale : Int;
19187 begin
19188 Tmp := Ureal_1;
19189 Scale := 0;
19191 if Delta_Val < Ureal_1 then
19192 while Delta_Val < Tmp loop
19193 Tmp := Tmp / Ureal_2;
19194 Scale := Scale + 1;
19195 end loop;
19197 else
19198 loop
19199 Tmp := Tmp * Ureal_2;
19200 exit when Tmp > Delta_Val;
19201 Scale := Scale - 1;
19202 end loop;
19203 end if;
19205 Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
19206 end;
19208 Set_Small_Value (Implicit_Base, Small_Val);
19210 -- If no range was given, set a dummy range
19212 if RRS <= Empty_Or_Error then
19213 Low_Val := -Small_Val;
19214 High_Val := Small_Val;
19216 -- Otherwise analyze and process given range
19218 else
19219 declare
19220 Low : constant Node_Id := Low_Bound (RRS);
19221 High : constant Node_Id := High_Bound (RRS);
19223 begin
19224 Analyze_And_Resolve (Low, Any_Real);
19225 Analyze_And_Resolve (High, Any_Real);
19226 Check_Real_Bound (Low);
19227 Check_Real_Bound (High);
19229 -- Obtain and set the range
19231 Low_Val := Expr_Value_R (Low);
19232 High_Val := Expr_Value_R (High);
19234 if Low_Val > High_Val then
19235 Error_Msg_NE ("??fixed point type& has null range", Def, T);
19236 end if;
19237 end;
19238 end if;
19240 -- The range for both the implicit base and the declared first subtype
19241 -- cannot be set yet, so we use the special routine Set_Fixed_Range to
19242 -- set a temporary range in place. Note that the bounds of the base
19243 -- type will be widened to be symmetrical and to fill the available
19244 -- bits when the type is frozen.
19246 -- We could do this with all discrete types, and probably should, but
19247 -- we absolutely have to do it for fixed-point, since the end-points
19248 -- of the range and the size are determined by the small value, which
19249 -- could be reset before the freeze point.
19251 Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
19252 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
19254 -- Complete definition of first subtype. The inheritance of the rep item
19255 -- chain ensures that SPARK-related pragmas are not clobbered when the
19256 -- ordinary fixed point type acts as a full view of a private type.
19258 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
19259 Set_Etype (T, Implicit_Base);
19260 Init_Size_Align (T);
19261 Inherit_Rep_Item_Chain (T, Implicit_Base);
19262 Set_Small_Value (T, Small_Val);
19263 Set_Delta_Value (T, Delta_Val);
19264 Set_Is_Constrained (T);
19265 end Ordinary_Fixed_Point_Type_Declaration;
19267 ----------------------------------
19268 -- Preanalyze_Assert_Expression --
19269 ----------------------------------
19271 procedure Preanalyze_Assert_Expression (N : Node_Id; T : Entity_Id) is
19272 begin
19273 In_Assertion_Expr := In_Assertion_Expr + 1;
19274 Preanalyze_Spec_Expression (N, T);
19275 In_Assertion_Expr := In_Assertion_Expr - 1;
19276 end Preanalyze_Assert_Expression;
19278 -----------------------------------
19279 -- Preanalyze_Default_Expression --
19280 -----------------------------------
19282 procedure Preanalyze_Default_Expression (N : Node_Id; T : Entity_Id) is
19283 Save_In_Default_Expr : constant Boolean := In_Default_Expr;
19284 begin
19285 In_Default_Expr := True;
19286 Preanalyze_Spec_Expression (N, T);
19287 In_Default_Expr := Save_In_Default_Expr;
19288 end Preanalyze_Default_Expression;
19290 --------------------------------
19291 -- Preanalyze_Spec_Expression --
19292 --------------------------------
19294 procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
19295 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
19296 begin
19297 In_Spec_Expression := True;
19298 Preanalyze_And_Resolve (N, T);
19299 In_Spec_Expression := Save_In_Spec_Expression;
19300 end Preanalyze_Spec_Expression;
19302 ----------------------------------------
19303 -- Prepare_Private_Subtype_Completion --
19304 ----------------------------------------
19306 procedure Prepare_Private_Subtype_Completion
19307 (Id : Entity_Id;
19308 Related_Nod : Node_Id)
19310 Id_B : constant Entity_Id := Base_Type (Id);
19311 Full_B : Entity_Id := Full_View (Id_B);
19312 Full : Entity_Id;
19314 begin
19315 if Present (Full_B) then
19317 -- Get to the underlying full view if necessary
19319 if Is_Private_Type (Full_B)
19320 and then Present (Underlying_Full_View (Full_B))
19321 then
19322 Full_B := Underlying_Full_View (Full_B);
19323 end if;
19325 -- The Base_Type is already completed, we can complete the subtype
19326 -- now. We have to create a new entity with the same name, Thus we
19327 -- can't use Create_Itype.
19329 Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
19330 Set_Is_Itype (Full);
19331 Set_Associated_Node_For_Itype (Full, Related_Nod);
19332 Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
19333 end if;
19335 -- The parent subtype may be private, but the base might not, in some
19336 -- nested instances. In that case, the subtype does not need to be
19337 -- exchanged. It would still be nice to make private subtypes and their
19338 -- bases consistent at all times ???
19340 if Is_Private_Type (Id_B) then
19341 Append_Elmt (Id, Private_Dependents (Id_B));
19342 end if;
19343 end Prepare_Private_Subtype_Completion;
19345 ---------------------------
19346 -- Process_Discriminants --
19347 ---------------------------
19349 procedure Process_Discriminants
19350 (N : Node_Id;
19351 Prev : Entity_Id := Empty)
19353 Elist : constant Elist_Id := New_Elmt_List;
19354 Id : Node_Id;
19355 Discr : Node_Id;
19356 Discr_Number : Uint;
19357 Discr_Type : Entity_Id;
19358 Default_Present : Boolean := False;
19359 Default_Not_Present : Boolean := False;
19361 begin
19362 -- A composite type other than an array type can have discriminants.
19363 -- On entry, the current scope is the composite type.
19365 -- The discriminants are initially entered into the scope of the type
19366 -- via Enter_Name with the default Ekind of E_Void to prevent premature
19367 -- use, as explained at the end of this procedure.
19369 Discr := First (Discriminant_Specifications (N));
19370 while Present (Discr) loop
19371 Enter_Name (Defining_Identifier (Discr));
19373 -- For navigation purposes we add a reference to the discriminant
19374 -- in the entity for the type. If the current declaration is a
19375 -- completion, place references on the partial view. Otherwise the
19376 -- type is the current scope.
19378 if Present (Prev) then
19380 -- The references go on the partial view, if present. If the
19381 -- partial view has discriminants, the references have been
19382 -- generated already.
19384 if not Has_Discriminants (Prev) then
19385 Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
19386 end if;
19387 else
19388 Generate_Reference
19389 (Current_Scope, Defining_Identifier (Discr), 'd');
19390 end if;
19392 if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
19393 Discr_Type := Access_Definition (Discr, Discriminant_Type (Discr));
19395 -- Ada 2005 (AI-254)
19397 if Present (Access_To_Subprogram_Definition
19398 (Discriminant_Type (Discr)))
19399 and then Protected_Present (Access_To_Subprogram_Definition
19400 (Discriminant_Type (Discr)))
19401 then
19402 Discr_Type :=
19403 Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
19404 end if;
19406 else
19407 Find_Type (Discriminant_Type (Discr));
19408 Discr_Type := Etype (Discriminant_Type (Discr));
19410 if Error_Posted (Discriminant_Type (Discr)) then
19411 Discr_Type := Any_Type;
19412 end if;
19413 end if;
19415 -- Handling of discriminants that are access types
19417 if Is_Access_Type (Discr_Type) then
19419 -- Ada 2005 (AI-230): Access discriminant allowed in non-
19420 -- limited record types
19422 if Ada_Version < Ada_2005 then
19423 Check_Access_Discriminant_Requires_Limited
19424 (Discr, Discriminant_Type (Discr));
19425 end if;
19427 if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
19428 Error_Msg_N
19429 ("(Ada 83) access discriminant not allowed", Discr);
19430 end if;
19432 -- If not access type, must be a discrete type
19434 elsif not Is_Discrete_Type (Discr_Type) then
19435 Error_Msg_N
19436 ("discriminants must have a discrete or access type",
19437 Discriminant_Type (Discr));
19438 end if;
19440 Set_Etype (Defining_Identifier (Discr), Discr_Type);
19442 -- If a discriminant specification includes the assignment compound
19443 -- delimiter followed by an expression, the expression is the default
19444 -- expression of the discriminant; the default expression must be of
19445 -- the type of the discriminant. (RM 3.7.1) Since this expression is
19446 -- a default expression, we do the special preanalysis, since this
19447 -- expression does not freeze (see section "Handling of Default and
19448 -- Per-Object Expressions" in spec of package Sem).
19450 if Present (Expression (Discr)) then
19451 Preanalyze_Spec_Expression (Expression (Discr), Discr_Type);
19453 -- Legaity checks
19455 if Nkind (N) = N_Formal_Type_Declaration then
19456 Error_Msg_N
19457 ("discriminant defaults not allowed for formal type",
19458 Expression (Discr));
19460 -- Flag an error for a tagged type with defaulted discriminants,
19461 -- excluding limited tagged types when compiling for Ada 2012
19462 -- (see AI05-0214).
19464 elsif Is_Tagged_Type (Current_Scope)
19465 and then (not Is_Limited_Type (Current_Scope)
19466 or else Ada_Version < Ada_2012)
19467 and then Comes_From_Source (N)
19468 then
19469 -- Note: see similar test in Check_Or_Process_Discriminants, to
19470 -- handle the (illegal) case of the completion of an untagged
19471 -- view with discriminants with defaults by a tagged full view.
19472 -- We skip the check if Discr does not come from source, to
19473 -- account for the case of an untagged derived type providing
19474 -- defaults for a renamed discriminant from a private untagged
19475 -- ancestor with a tagged full view (ACATS B460006).
19477 if Ada_Version >= Ada_2012 then
19478 Error_Msg_N
19479 ("discriminants of nonlimited tagged type cannot have"
19480 & " defaults",
19481 Expression (Discr));
19482 else
19483 Error_Msg_N
19484 ("discriminants of tagged type cannot have defaults",
19485 Expression (Discr));
19486 end if;
19488 else
19489 Default_Present := True;
19490 Append_Elmt (Expression (Discr), Elist);
19492 -- Tag the defining identifiers for the discriminants with
19493 -- their corresponding default expressions from the tree.
19495 Set_Discriminant_Default_Value
19496 (Defining_Identifier (Discr), Expression (Discr));
19497 end if;
19499 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag
19500 -- gets set unless we can be sure that no range check is required.
19502 if (GNATprove_Mode or not Expander_Active)
19503 and then not
19504 Is_In_Range
19505 (Expression (Discr), Discr_Type, Assume_Valid => True)
19506 then
19507 Set_Do_Range_Check (Expression (Discr));
19508 end if;
19510 -- No default discriminant value given
19512 else
19513 Default_Not_Present := True;
19514 end if;
19516 -- Ada 2005 (AI-231): Create an Itype that is a duplicate of
19517 -- Discr_Type but with the null-exclusion attribute
19519 if Ada_Version >= Ada_2005 then
19521 -- Ada 2005 (AI-231): Static checks
19523 if Can_Never_Be_Null (Discr_Type) then
19524 Null_Exclusion_Static_Checks (Discr);
19526 elsif Is_Access_Type (Discr_Type)
19527 and then Null_Exclusion_Present (Discr)
19529 -- No need to check itypes because in their case this check
19530 -- was done at their point of creation
19532 and then not Is_Itype (Discr_Type)
19533 then
19534 if Can_Never_Be_Null (Discr_Type) then
19535 Error_Msg_NE
19536 ("`NOT NULL` not allowed (& already excludes null)",
19537 Discr,
19538 Discr_Type);
19539 end if;
19541 Set_Etype (Defining_Identifier (Discr),
19542 Create_Null_Excluding_Itype
19543 (T => Discr_Type,
19544 Related_Nod => Discr));
19546 -- Check for improper null exclusion if the type is otherwise
19547 -- legal for a discriminant.
19549 elsif Null_Exclusion_Present (Discr)
19550 and then Is_Discrete_Type (Discr_Type)
19551 then
19552 Error_Msg_N
19553 ("null exclusion can only apply to an access type", Discr);
19554 end if;
19556 -- Ada 2005 (AI-402): access discriminants of nonlimited types
19557 -- can't have defaults. Synchronized types, or types that are
19558 -- explicitly limited are fine, but special tests apply to derived
19559 -- types in generics: in a generic body we have to assume the
19560 -- worst, and therefore defaults are not allowed if the parent is
19561 -- a generic formal private type (see ACATS B370001).
19563 if Is_Access_Type (Discr_Type) and then Default_Present then
19564 if Ekind (Discr_Type) /= E_Anonymous_Access_Type
19565 or else Is_Limited_Record (Current_Scope)
19566 or else Is_Concurrent_Type (Current_Scope)
19567 or else Is_Concurrent_Record_Type (Current_Scope)
19568 or else Ekind (Current_Scope) = E_Limited_Private_Type
19569 then
19570 if not Is_Derived_Type (Current_Scope)
19571 or else not Is_Generic_Type (Etype (Current_Scope))
19572 or else not In_Package_Body (Scope (Etype (Current_Scope)))
19573 or else Limited_Present
19574 (Type_Definition (Parent (Current_Scope)))
19575 then
19576 null;
19578 else
19579 Error_Msg_N
19580 ("access discriminants of nonlimited types cannot "
19581 & "have defaults", Expression (Discr));
19582 end if;
19584 elsif Present (Expression (Discr)) then
19585 Error_Msg_N
19586 ("(Ada 2005) access discriminants of nonlimited types "
19587 & "cannot have defaults", Expression (Discr));
19588 end if;
19589 end if;
19590 end if;
19592 -- A discriminant cannot be effectively volatile (SPARK RM 7.1.3(6)).
19593 -- This check is relevant only when SPARK_Mode is on as it is not a
19594 -- standard Ada legality rule.
19596 if SPARK_Mode = On
19597 and then Is_Effectively_Volatile (Defining_Identifier (Discr))
19598 then
19599 Error_Msg_N ("discriminant cannot be volatile", Discr);
19600 end if;
19602 Next (Discr);
19603 end loop;
19605 -- An element list consisting of the default expressions of the
19606 -- discriminants is constructed in the above loop and used to set
19607 -- the Discriminant_Constraint attribute for the type. If an object
19608 -- is declared of this (record or task) type without any explicit
19609 -- discriminant constraint given, this element list will form the
19610 -- actual parameters for the corresponding initialization procedure
19611 -- for the type.
19613 Set_Discriminant_Constraint (Current_Scope, Elist);
19614 Set_Stored_Constraint (Current_Scope, No_Elist);
19616 -- Default expressions must be provided either for all or for none
19617 -- of the discriminants of a discriminant part. (RM 3.7.1)
19619 if Default_Present and then Default_Not_Present then
19620 Error_Msg_N
19621 ("incomplete specification of defaults for discriminants", N);
19622 end if;
19624 -- The use of the name of a discriminant is not allowed in default
19625 -- expressions of a discriminant part if the specification of the
19626 -- discriminant is itself given in the discriminant part. (RM 3.7.1)
19628 -- To detect this, the discriminant names are entered initially with an
19629 -- Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
19630 -- attempt to use a void entity (for example in an expression that is
19631 -- type-checked) produces the error message: premature usage. Now after
19632 -- completing the semantic analysis of the discriminant part, we can set
19633 -- the Ekind of all the discriminants appropriately.
19635 Discr := First (Discriminant_Specifications (N));
19636 Discr_Number := Uint_1;
19637 while Present (Discr) loop
19638 Id := Defining_Identifier (Discr);
19639 Set_Ekind (Id, E_Discriminant);
19640 Init_Component_Location (Id);
19641 Init_Esize (Id);
19642 Set_Discriminant_Number (Id, Discr_Number);
19644 -- Make sure this is always set, even in illegal programs
19646 Set_Corresponding_Discriminant (Id, Empty);
19648 -- Initialize the Original_Record_Component to the entity itself.
19649 -- Inherit_Components will propagate the right value to
19650 -- discriminants in derived record types.
19652 Set_Original_Record_Component (Id, Id);
19654 -- Create the discriminal for the discriminant
19656 Build_Discriminal (Id);
19658 Next (Discr);
19659 Discr_Number := Discr_Number + 1;
19660 end loop;
19662 Set_Has_Discriminants (Current_Scope);
19663 end Process_Discriminants;
19665 -----------------------
19666 -- Process_Full_View --
19667 -----------------------
19669 -- WARNING: This routine manages Ghost regions. Return statements must be
19670 -- replaced by gotos which jump to the end of the routine and restore the
19671 -- Ghost mode.
19673 procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
19674 procedure Collect_Implemented_Interfaces
19675 (Typ : Entity_Id;
19676 Ifaces : Elist_Id);
19677 -- Ada 2005: Gather all the interfaces that Typ directly or
19678 -- inherently implements. Duplicate entries are not added to
19679 -- the list Ifaces.
19681 ------------------------------------
19682 -- Collect_Implemented_Interfaces --
19683 ------------------------------------
19685 procedure Collect_Implemented_Interfaces
19686 (Typ : Entity_Id;
19687 Ifaces : Elist_Id)
19689 Iface : Entity_Id;
19690 Iface_Elmt : Elmt_Id;
19692 begin
19693 -- Abstract interfaces are only associated with tagged record types
19695 if not Is_Tagged_Type (Typ) or else not Is_Record_Type (Typ) then
19696 return;
19697 end if;
19699 -- Recursively climb to the ancestors
19701 if Etype (Typ) /= Typ
19703 -- Protect the frontend against wrong cyclic declarations like:
19705 -- type B is new A with private;
19706 -- type C is new A with private;
19707 -- private
19708 -- type B is new C with null record;
19709 -- type C is new B with null record;
19711 and then Etype (Typ) /= Priv_T
19712 and then Etype (Typ) /= Full_T
19713 then
19714 -- Keep separate the management of private type declarations
19716 if Ekind (Typ) = E_Record_Type_With_Private then
19718 -- Handle the following illegal usage:
19719 -- type Private_Type is tagged private;
19720 -- private
19721 -- type Private_Type is new Type_Implementing_Iface;
19723 if Present (Full_View (Typ))
19724 and then Etype (Typ) /= Full_View (Typ)
19725 then
19726 if Is_Interface (Etype (Typ)) then
19727 Append_Unique_Elmt (Etype (Typ), Ifaces);
19728 end if;
19730 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
19731 end if;
19733 -- Non-private types
19735 else
19736 if Is_Interface (Etype (Typ)) then
19737 Append_Unique_Elmt (Etype (Typ), Ifaces);
19738 end if;
19740 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
19741 end if;
19742 end if;
19744 -- Handle entities in the list of abstract interfaces
19746 if Present (Interfaces (Typ)) then
19747 Iface_Elmt := First_Elmt (Interfaces (Typ));
19748 while Present (Iface_Elmt) loop
19749 Iface := Node (Iface_Elmt);
19751 pragma Assert (Is_Interface (Iface));
19753 if not Contain_Interface (Iface, Ifaces) then
19754 Append_Elmt (Iface, Ifaces);
19755 Collect_Implemented_Interfaces (Iface, Ifaces);
19756 end if;
19758 Next_Elmt (Iface_Elmt);
19759 end loop;
19760 end if;
19761 end Collect_Implemented_Interfaces;
19763 -- Local variables
19765 Full_Indic : Node_Id;
19766 Full_Parent : Entity_Id;
19767 Mode : Ghost_Mode_Type;
19768 Priv_Parent : Entity_Id;
19770 -- Start of processing for Process_Full_View
19772 begin
19773 Mark_And_Set_Ghost_Completion (N, Priv_T, Mode);
19775 -- First some sanity checks that must be done after semantic
19776 -- decoration of the full view and thus cannot be placed with other
19777 -- similar checks in Find_Type_Name
19779 if not Is_Limited_Type (Priv_T)
19780 and then (Is_Limited_Type (Full_T)
19781 or else Is_Limited_Composite (Full_T))
19782 then
19783 if In_Instance then
19784 null;
19785 else
19786 Error_Msg_N
19787 ("completion of nonlimited type cannot be limited", Full_T);
19788 Explain_Limited_Type (Full_T, Full_T);
19789 end if;
19791 elsif Is_Abstract_Type (Full_T)
19792 and then not Is_Abstract_Type (Priv_T)
19793 then
19794 Error_Msg_N
19795 ("completion of nonabstract type cannot be abstract", Full_T);
19797 elsif Is_Tagged_Type (Priv_T)
19798 and then Is_Limited_Type (Priv_T)
19799 and then not Is_Limited_Type (Full_T)
19800 then
19801 -- If pragma CPP_Class was applied to the private declaration
19802 -- propagate the limitedness to the full-view
19804 if Is_CPP_Class (Priv_T) then
19805 Set_Is_Limited_Record (Full_T);
19807 -- GNAT allow its own definition of Limited_Controlled to disobey
19808 -- this rule in order in ease the implementation. This test is safe
19809 -- because Root_Controlled is defined in a child of System that
19810 -- normal programs are not supposed to use.
19812 elsif Is_RTE (Etype (Full_T), RE_Root_Controlled) then
19813 Set_Is_Limited_Composite (Full_T);
19814 else
19815 Error_Msg_N
19816 ("completion of limited tagged type must be limited", Full_T);
19817 end if;
19819 elsif Is_Generic_Type (Priv_T) then
19820 Error_Msg_N ("generic type cannot have a completion", Full_T);
19821 end if;
19823 -- Check that ancestor interfaces of private and full views are
19824 -- consistent. We omit this check for synchronized types because
19825 -- they are performed on the corresponding record type when frozen.
19827 if Ada_Version >= Ada_2005
19828 and then Is_Tagged_Type (Priv_T)
19829 and then Is_Tagged_Type (Full_T)
19830 and then not Is_Concurrent_Type (Full_T)
19831 then
19832 declare
19833 Iface : Entity_Id;
19834 Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
19835 Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
19837 begin
19838 Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
19839 Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
19841 -- Ada 2005 (AI-251): The partial view shall be a descendant of
19842 -- an interface type if and only if the full type is descendant
19843 -- of the interface type (AARM 7.3 (7.3/2)).
19845 Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
19847 if Present (Iface) then
19848 Error_Msg_NE
19849 ("interface in partial view& not implemented by full type "
19850 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
19851 end if;
19853 Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
19855 if Present (Iface) then
19856 Error_Msg_NE
19857 ("interface & not implemented by partial view "
19858 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
19859 end if;
19860 end;
19861 end if;
19863 if Is_Tagged_Type (Priv_T)
19864 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
19865 and then Is_Derived_Type (Full_T)
19866 then
19867 Priv_Parent := Etype (Priv_T);
19869 -- The full view of a private extension may have been transformed
19870 -- into an unconstrained derived type declaration and a subtype
19871 -- declaration (see build_derived_record_type for details).
19873 if Nkind (N) = N_Subtype_Declaration then
19874 Full_Indic := Subtype_Indication (N);
19875 Full_Parent := Etype (Base_Type (Full_T));
19876 else
19877 Full_Indic := Subtype_Indication (Type_Definition (N));
19878 Full_Parent := Etype (Full_T);
19879 end if;
19881 -- Check that the parent type of the full type is a descendant of
19882 -- the ancestor subtype given in the private extension. If either
19883 -- entity has an Etype equal to Any_Type then we had some previous
19884 -- error situation [7.3(8)].
19886 if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
19887 goto Leave;
19889 -- Ada 2005 (AI-251): Interfaces in the full type can be given in
19890 -- any order. Therefore we don't have to check that its parent must
19891 -- be a descendant of the parent of the private type declaration.
19893 elsif Is_Interface (Priv_Parent)
19894 and then Is_Interface (Full_Parent)
19895 then
19896 null;
19898 -- Ada 2005 (AI-251): If the parent of the private type declaration
19899 -- is an interface there is no need to check that it is an ancestor
19900 -- of the associated full type declaration. The required tests for
19901 -- this case are performed by Build_Derived_Record_Type.
19903 elsif not Is_Interface (Base_Type (Priv_Parent))
19904 and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
19905 then
19906 Error_Msg_N
19907 ("parent of full type must descend from parent of private "
19908 & "extension", Full_Indic);
19910 -- First check a formal restriction, and then proceed with checking
19911 -- Ada rules. Since the formal restriction is not a serious error, we
19912 -- don't prevent further error detection for this check, hence the
19913 -- ELSE.
19915 else
19916 -- In formal mode, when completing a private extension the type
19917 -- named in the private part must be exactly the same as that
19918 -- named in the visible part.
19920 if Priv_Parent /= Full_Parent then
19921 Error_Msg_Name_1 := Chars (Priv_Parent);
19922 Check_SPARK_05_Restriction ("% expected", Full_Indic);
19923 end if;
19925 -- Check the rules of 7.3(10): if the private extension inherits
19926 -- known discriminants, then the full type must also inherit those
19927 -- discriminants from the same (ancestor) type, and the parent
19928 -- subtype of the full type must be constrained if and only if
19929 -- the ancestor subtype of the private extension is constrained.
19931 if No (Discriminant_Specifications (Parent (Priv_T)))
19932 and then not Has_Unknown_Discriminants (Priv_T)
19933 and then Has_Discriminants (Base_Type (Priv_Parent))
19934 then
19935 declare
19936 Priv_Indic : constant Node_Id :=
19937 Subtype_Indication (Parent (Priv_T));
19939 Priv_Constr : constant Boolean :=
19940 Is_Constrained (Priv_Parent)
19941 or else
19942 Nkind (Priv_Indic) = N_Subtype_Indication
19943 or else
19944 Is_Constrained (Entity (Priv_Indic));
19946 Full_Constr : constant Boolean :=
19947 Is_Constrained (Full_Parent)
19948 or else
19949 Nkind (Full_Indic) = N_Subtype_Indication
19950 or else
19951 Is_Constrained (Entity (Full_Indic));
19953 Priv_Discr : Entity_Id;
19954 Full_Discr : Entity_Id;
19956 begin
19957 Priv_Discr := First_Discriminant (Priv_Parent);
19958 Full_Discr := First_Discriminant (Full_Parent);
19959 while Present (Priv_Discr) and then Present (Full_Discr) loop
19960 if Original_Record_Component (Priv_Discr) =
19961 Original_Record_Component (Full_Discr)
19962 or else
19963 Corresponding_Discriminant (Priv_Discr) =
19964 Corresponding_Discriminant (Full_Discr)
19965 then
19966 null;
19967 else
19968 exit;
19969 end if;
19971 Next_Discriminant (Priv_Discr);
19972 Next_Discriminant (Full_Discr);
19973 end loop;
19975 if Present (Priv_Discr) or else Present (Full_Discr) then
19976 Error_Msg_N
19977 ("full view must inherit discriminants of the parent "
19978 & "type used in the private extension", Full_Indic);
19980 elsif Priv_Constr and then not Full_Constr then
19981 Error_Msg_N
19982 ("parent subtype of full type must be constrained",
19983 Full_Indic);
19985 elsif Full_Constr and then not Priv_Constr then
19986 Error_Msg_N
19987 ("parent subtype of full type must be unconstrained",
19988 Full_Indic);
19989 end if;
19990 end;
19992 -- Check the rules of 7.3(12): if a partial view has neither
19993 -- known or unknown discriminants, then the full type
19994 -- declaration shall define a definite subtype.
19996 elsif not Has_Unknown_Discriminants (Priv_T)
19997 and then not Has_Discriminants (Priv_T)
19998 and then not Is_Constrained (Full_T)
19999 then
20000 Error_Msg_N
20001 ("full view must define a constrained type if partial view "
20002 & "has no discriminants", Full_T);
20003 end if;
20005 -- ??????? Do we implement the following properly ?????
20006 -- If the ancestor subtype of a private extension has constrained
20007 -- discriminants, then the parent subtype of the full view shall
20008 -- impose a statically matching constraint on those discriminants
20009 -- [7.3(13)].
20010 end if;
20012 else
20013 -- For untagged types, verify that a type without discriminants is
20014 -- not completed with an unconstrained type. A separate error message
20015 -- is produced if the full type has defaulted discriminants.
20017 if Is_Definite_Subtype (Priv_T)
20018 and then not Is_Definite_Subtype (Full_T)
20019 then
20020 Error_Msg_Sloc := Sloc (Parent (Priv_T));
20021 Error_Msg_NE
20022 ("full view of& not compatible with declaration#",
20023 Full_T, Priv_T);
20025 if not Is_Tagged_Type (Full_T) then
20026 Error_Msg_N
20027 ("\one is constrained, the other unconstrained", Full_T);
20028 end if;
20029 end if;
20030 end if;
20032 -- AI-419: verify that the use of "limited" is consistent
20034 declare
20035 Orig_Decl : constant Node_Id := Original_Node (N);
20037 begin
20038 if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
20039 and then Nkind (Orig_Decl) = N_Full_Type_Declaration
20040 and then Nkind
20041 (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
20042 then
20043 if not Limited_Present (Parent (Priv_T))
20044 and then not Synchronized_Present (Parent (Priv_T))
20045 and then Limited_Present (Type_Definition (Orig_Decl))
20046 then
20047 Error_Msg_N
20048 ("full view of non-limited extension cannot be limited", N);
20050 -- Conversely, if the partial view carries the limited keyword,
20051 -- the full view must as well, even if it may be redundant.
20053 elsif Limited_Present (Parent (Priv_T))
20054 and then not Limited_Present (Type_Definition (Orig_Decl))
20055 then
20056 Error_Msg_N
20057 ("full view of limited extension must be explicitly limited",
20059 end if;
20060 end if;
20061 end;
20063 -- Ada 2005 (AI-443): A synchronized private extension must be
20064 -- completed by a task or protected type.
20066 if Ada_Version >= Ada_2005
20067 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
20068 and then Synchronized_Present (Parent (Priv_T))
20069 and then not Is_Concurrent_Type (Full_T)
20070 then
20071 Error_Msg_N ("full view of synchronized extension must " &
20072 "be synchronized type", N);
20073 end if;
20075 -- Ada 2005 AI-363: if the full view has discriminants with
20076 -- defaults, it is illegal to declare constrained access subtypes
20077 -- whose designated type is the current type. This allows objects
20078 -- of the type that are declared in the heap to be unconstrained.
20080 if not Has_Unknown_Discriminants (Priv_T)
20081 and then not Has_Discriminants (Priv_T)
20082 and then Has_Discriminants (Full_T)
20083 and then
20084 Present (Discriminant_Default_Value (First_Discriminant (Full_T)))
20085 then
20086 Set_Has_Constrained_Partial_View (Full_T);
20087 Set_Has_Constrained_Partial_View (Priv_T);
20088 end if;
20090 -- Create a full declaration for all its subtypes recorded in
20091 -- Private_Dependents and swap them similarly to the base type. These
20092 -- are subtypes that have been define before the full declaration of
20093 -- the private type. We also swap the entry in Private_Dependents list
20094 -- so we can properly restore the private view on exit from the scope.
20096 declare
20097 Priv_Elmt : Elmt_Id;
20098 Priv_Scop : Entity_Id;
20099 Priv : Entity_Id;
20100 Full : Entity_Id;
20102 begin
20103 Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
20104 while Present (Priv_Elmt) loop
20105 Priv := Node (Priv_Elmt);
20106 Priv_Scop := Scope (Priv);
20108 if Ekind_In (Priv, E_Private_Subtype,
20109 E_Limited_Private_Subtype,
20110 E_Record_Subtype_With_Private)
20111 then
20112 Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
20113 Set_Is_Itype (Full);
20114 Set_Parent (Full, Parent (Priv));
20115 Set_Associated_Node_For_Itype (Full, N);
20117 -- Now we need to complete the private subtype, but since the
20118 -- base type has already been swapped, we must also swap the
20119 -- subtypes (and thus, reverse the arguments in the call to
20120 -- Complete_Private_Subtype). Also note that we may need to
20121 -- re-establish the scope of the private subtype.
20123 Copy_And_Swap (Priv, Full);
20125 if not In_Open_Scopes (Priv_Scop) then
20126 Push_Scope (Priv_Scop);
20128 else
20129 -- Reset Priv_Scop to Empty to indicate no scope was pushed
20131 Priv_Scop := Empty;
20132 end if;
20134 Complete_Private_Subtype (Full, Priv, Full_T, N);
20136 if Present (Priv_Scop) then
20137 Pop_Scope;
20138 end if;
20140 Replace_Elmt (Priv_Elmt, Full);
20141 end if;
20143 Next_Elmt (Priv_Elmt);
20144 end loop;
20145 end;
20147 -- If the private view was tagged, copy the new primitive operations
20148 -- from the private view to the full view.
20150 if Is_Tagged_Type (Full_T) then
20151 declare
20152 Disp_Typ : Entity_Id;
20153 Full_List : Elist_Id;
20154 Prim : Entity_Id;
20155 Prim_Elmt : Elmt_Id;
20156 Priv_List : Elist_Id;
20158 function Contains
20159 (E : Entity_Id;
20160 L : Elist_Id) return Boolean;
20161 -- Determine whether list L contains element E
20163 --------------
20164 -- Contains --
20165 --------------
20167 function Contains
20168 (E : Entity_Id;
20169 L : Elist_Id) return Boolean
20171 List_Elmt : Elmt_Id;
20173 begin
20174 List_Elmt := First_Elmt (L);
20175 while Present (List_Elmt) loop
20176 if Node (List_Elmt) = E then
20177 return True;
20178 end if;
20180 Next_Elmt (List_Elmt);
20181 end loop;
20183 return False;
20184 end Contains;
20186 -- Start of processing
20188 begin
20189 if Is_Tagged_Type (Priv_T) then
20190 Priv_List := Primitive_Operations (Priv_T);
20191 Prim_Elmt := First_Elmt (Priv_List);
20193 -- In the case of a concurrent type completing a private tagged
20194 -- type, primitives may have been declared in between the two
20195 -- views. These subprograms need to be wrapped the same way
20196 -- entries and protected procedures are handled because they
20197 -- cannot be directly shared by the two views.
20199 if Is_Concurrent_Type (Full_T) then
20200 declare
20201 Conc_Typ : constant Entity_Id :=
20202 Corresponding_Record_Type (Full_T);
20203 Curr_Nod : Node_Id := Parent (Conc_Typ);
20204 Wrap_Spec : Node_Id;
20206 begin
20207 while Present (Prim_Elmt) loop
20208 Prim := Node (Prim_Elmt);
20210 if Comes_From_Source (Prim)
20211 and then not Is_Abstract_Subprogram (Prim)
20212 then
20213 Wrap_Spec :=
20214 Make_Subprogram_Declaration (Sloc (Prim),
20215 Specification =>
20216 Build_Wrapper_Spec
20217 (Subp_Id => Prim,
20218 Obj_Typ => Conc_Typ,
20219 Formals =>
20220 Parameter_Specifications
20221 (Parent (Prim))));
20223 Insert_After (Curr_Nod, Wrap_Spec);
20224 Curr_Nod := Wrap_Spec;
20226 Analyze (Wrap_Spec);
20228 -- Remove the wrapper from visibility to avoid
20229 -- spurious conflict with the wrapped entity.
20231 Set_Is_Immediately_Visible
20232 (Defining_Entity (Specification (Wrap_Spec)),
20233 False);
20234 end if;
20236 Next_Elmt (Prim_Elmt);
20237 end loop;
20239 goto Leave;
20240 end;
20242 -- For non-concurrent types, transfer explicit primitives, but
20243 -- omit those inherited from the parent of the private view
20244 -- since they will be re-inherited later on.
20246 else
20247 Full_List := Primitive_Operations (Full_T);
20249 while Present (Prim_Elmt) loop
20250 Prim := Node (Prim_Elmt);
20252 if Comes_From_Source (Prim)
20253 and then not Contains (Prim, Full_List)
20254 then
20255 Append_Elmt (Prim, Full_List);
20256 end if;
20258 Next_Elmt (Prim_Elmt);
20259 end loop;
20260 end if;
20262 -- Untagged private view
20264 else
20265 Full_List := Primitive_Operations (Full_T);
20267 -- In this case the partial view is untagged, so here we locate
20268 -- all of the earlier primitives that need to be treated as
20269 -- dispatching (those that appear between the two views). Note
20270 -- that these additional operations must all be new operations
20271 -- (any earlier operations that override inherited operations
20272 -- of the full view will already have been inserted in the
20273 -- primitives list, marked by Check_Operation_From_Private_View
20274 -- as dispatching. Note that implicit "/=" operators are
20275 -- excluded from being added to the primitives list since they
20276 -- shouldn't be treated as dispatching (tagged "/=" is handled
20277 -- specially).
20279 Prim := Next_Entity (Full_T);
20280 while Present (Prim) and then Prim /= Priv_T loop
20281 if Ekind_In (Prim, E_Procedure, E_Function) then
20282 Disp_Typ := Find_Dispatching_Type (Prim);
20284 if Disp_Typ = Full_T
20285 and then (Chars (Prim) /= Name_Op_Ne
20286 or else Comes_From_Source (Prim))
20287 then
20288 Check_Controlling_Formals (Full_T, Prim);
20290 if not Is_Dispatching_Operation (Prim) then
20291 Append_Elmt (Prim, Full_List);
20292 Set_Is_Dispatching_Operation (Prim, True);
20293 Set_DT_Position_Value (Prim, No_Uint);
20294 end if;
20296 elsif Is_Dispatching_Operation (Prim)
20297 and then Disp_Typ /= Full_T
20298 then
20300 -- Verify that it is not otherwise controlled by a
20301 -- formal or a return value of type T.
20303 Check_Controlling_Formals (Disp_Typ, Prim);
20304 end if;
20305 end if;
20307 Next_Entity (Prim);
20308 end loop;
20309 end if;
20311 -- For the tagged case, the two views can share the same primitive
20312 -- operations list and the same class-wide type. Update attributes
20313 -- of the class-wide type which depend on the full declaration.
20315 if Is_Tagged_Type (Priv_T) then
20316 Set_Direct_Primitive_Operations (Priv_T, Full_List);
20317 Set_Class_Wide_Type
20318 (Base_Type (Full_T), Class_Wide_Type (Priv_T));
20320 Propagate_Concurrent_Flags (Class_Wide_Type (Priv_T), Full_T);
20321 end if;
20322 end;
20323 end if;
20325 -- Ada 2005 AI 161: Check preelaborable initialization consistency
20327 if Known_To_Have_Preelab_Init (Priv_T) then
20329 -- Case where there is a pragma Preelaborable_Initialization. We
20330 -- always allow this in predefined units, which is cheating a bit,
20331 -- but it means we don't have to struggle to meet the requirements in
20332 -- the RM for having Preelaborable Initialization. Otherwise we
20333 -- require that the type meets the RM rules. But we can't check that
20334 -- yet, because of the rule about overriding Initialize, so we simply
20335 -- set a flag that will be checked at freeze time.
20337 if not In_Predefined_Unit (Full_T) then
20338 Set_Must_Have_Preelab_Init (Full_T);
20339 end if;
20340 end if;
20342 -- If pragma CPP_Class was applied to the private type declaration,
20343 -- propagate it now to the full type declaration.
20345 if Is_CPP_Class (Priv_T) then
20346 Set_Is_CPP_Class (Full_T);
20347 Set_Convention (Full_T, Convention_CPP);
20349 -- Check that components of imported CPP types do not have default
20350 -- expressions.
20352 Check_CPP_Type_Has_No_Defaults (Full_T);
20353 end if;
20355 -- If the private view has user specified stream attributes, then so has
20356 -- the full view.
20358 -- Why the test, how could these flags be already set in Full_T ???
20360 if Has_Specified_Stream_Read (Priv_T) then
20361 Set_Has_Specified_Stream_Read (Full_T);
20362 end if;
20364 if Has_Specified_Stream_Write (Priv_T) then
20365 Set_Has_Specified_Stream_Write (Full_T);
20366 end if;
20368 if Has_Specified_Stream_Input (Priv_T) then
20369 Set_Has_Specified_Stream_Input (Full_T);
20370 end if;
20372 if Has_Specified_Stream_Output (Priv_T) then
20373 Set_Has_Specified_Stream_Output (Full_T);
20374 end if;
20376 -- Propagate Default_Initial_Condition-related attributes from the
20377 -- partial view to the full view and its base type.
20379 Propagate_DIC_Attributes (Full_T, From_Typ => Priv_T);
20380 Propagate_DIC_Attributes (Base_Type (Full_T), From_Typ => Priv_T);
20382 -- Propagate invariant-related attributes from the partial view to the
20383 -- full view and its base type.
20385 Propagate_Invariant_Attributes (Full_T, From_Typ => Priv_T);
20386 Propagate_Invariant_Attributes (Base_Type (Full_T), From_Typ => Priv_T);
20388 -- AI12-0041: Detect an attempt to inherit a class-wide type invariant
20389 -- in the full view without advertising the inheritance in the partial
20390 -- view. This can only occur when the partial view has no parent type
20391 -- and the full view has an interface as a parent. Any other scenarios
20392 -- are illegal because implemented interfaces must match between the
20393 -- two views.
20395 if Is_Tagged_Type (Priv_T) and then Is_Tagged_Type (Full_T) then
20396 declare
20397 Full_Par : constant Entity_Id := Etype (Full_T);
20398 Priv_Par : constant Entity_Id := Etype (Priv_T);
20400 begin
20401 if not Is_Interface (Priv_Par)
20402 and then Is_Interface (Full_Par)
20403 and then Has_Inheritable_Invariants (Full_Par)
20404 then
20405 Error_Msg_N
20406 ("hidden inheritance of class-wide type invariants not "
20407 & "allowed", N);
20408 end if;
20409 end;
20410 end if;
20412 -- Propagate predicates to full type, and predicate function if already
20413 -- defined. It is not clear that this can actually happen? the partial
20414 -- view cannot be frozen yet, and the predicate function has not been
20415 -- built. Still it is a cheap check and seems safer to make it.
20417 if Has_Predicates (Priv_T) then
20418 Set_Has_Predicates (Full_T);
20420 if Present (Predicate_Function (Priv_T)) then
20421 Set_Predicate_Function (Full_T, Predicate_Function (Priv_T));
20422 end if;
20423 end if;
20425 <<Leave>>
20426 Restore_Ghost_Mode (Mode);
20427 end Process_Full_View;
20429 -----------------------------------
20430 -- Process_Incomplete_Dependents --
20431 -----------------------------------
20433 procedure Process_Incomplete_Dependents
20434 (N : Node_Id;
20435 Full_T : Entity_Id;
20436 Inc_T : Entity_Id)
20438 Inc_Elmt : Elmt_Id;
20439 Priv_Dep : Entity_Id;
20440 New_Subt : Entity_Id;
20442 Disc_Constraint : Elist_Id;
20444 begin
20445 if No (Private_Dependents (Inc_T)) then
20446 return;
20447 end if;
20449 -- Itypes that may be generated by the completion of an incomplete
20450 -- subtype are not used by the back-end and not attached to the tree.
20451 -- They are created only for constraint-checking purposes.
20453 Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
20454 while Present (Inc_Elmt) loop
20455 Priv_Dep := Node (Inc_Elmt);
20457 if Ekind (Priv_Dep) = E_Subprogram_Type then
20459 -- An Access_To_Subprogram type may have a return type or a
20460 -- parameter type that is incomplete. Replace with the full view.
20462 if Etype (Priv_Dep) = Inc_T then
20463 Set_Etype (Priv_Dep, Full_T);
20464 end if;
20466 declare
20467 Formal : Entity_Id;
20469 begin
20470 Formal := First_Formal (Priv_Dep);
20471 while Present (Formal) loop
20472 if Etype (Formal) = Inc_T then
20473 Set_Etype (Formal, Full_T);
20474 end if;
20476 Next_Formal (Formal);
20477 end loop;
20478 end;
20480 elsif Is_Overloadable (Priv_Dep) then
20482 -- If a subprogram in the incomplete dependents list is primitive
20483 -- for a tagged full type then mark it as a dispatching operation,
20484 -- check whether it overrides an inherited subprogram, and check
20485 -- restrictions on its controlling formals. Note that a protected
20486 -- operation is never dispatching: only its wrapper operation
20487 -- (which has convention Ada) is.
20489 if Is_Tagged_Type (Full_T)
20490 and then Is_Primitive (Priv_Dep)
20491 and then Convention (Priv_Dep) /= Convention_Protected
20492 then
20493 Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
20494 Set_Is_Dispatching_Operation (Priv_Dep);
20495 Check_Controlling_Formals (Full_T, Priv_Dep);
20496 end if;
20498 elsif Ekind (Priv_Dep) = E_Subprogram_Body then
20500 -- Can happen during processing of a body before the completion
20501 -- of a TA type. Ignore, because spec is also on dependent list.
20503 return;
20505 -- Ada 2005 (AI-412): Transform a regular incomplete subtype into a
20506 -- corresponding subtype of the full view.
20508 elsif Ekind (Priv_Dep) = E_Incomplete_Subtype then
20509 Set_Subtype_Indication
20510 (Parent (Priv_Dep), New_Occurrence_Of (Full_T, Sloc (Priv_Dep)));
20511 Set_Etype (Priv_Dep, Full_T);
20512 Set_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
20513 Set_Analyzed (Parent (Priv_Dep), False);
20515 -- Reanalyze the declaration, suppressing the call to
20516 -- Enter_Name to avoid duplicate names.
20518 Analyze_Subtype_Declaration
20519 (N => Parent (Priv_Dep),
20520 Skip => True);
20522 -- Dependent is a subtype
20524 else
20525 -- We build a new subtype indication using the full view of the
20526 -- incomplete parent. The discriminant constraints have been
20527 -- elaborated already at the point of the subtype declaration.
20529 New_Subt := Create_Itype (E_Void, N);
20531 if Has_Discriminants (Full_T) then
20532 Disc_Constraint := Discriminant_Constraint (Priv_Dep);
20533 else
20534 Disc_Constraint := No_Elist;
20535 end if;
20537 Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
20538 Set_Full_View (Priv_Dep, New_Subt);
20539 end if;
20541 Next_Elmt (Inc_Elmt);
20542 end loop;
20543 end Process_Incomplete_Dependents;
20545 --------------------------------
20546 -- Process_Range_Expr_In_Decl --
20547 --------------------------------
20549 procedure Process_Range_Expr_In_Decl
20550 (R : Node_Id;
20551 T : Entity_Id;
20552 Subtyp : Entity_Id := Empty;
20553 Check_List : List_Id := Empty_List;
20554 R_Check_Off : Boolean := False;
20555 In_Iter_Schm : Boolean := False)
20557 Lo, Hi : Node_Id;
20558 R_Checks : Check_Result;
20559 Insert_Node : Node_Id;
20560 Def_Id : Entity_Id;
20562 begin
20563 Analyze_And_Resolve (R, Base_Type (T));
20565 if Nkind (R) = N_Range then
20567 -- In SPARK, all ranges should be static, with the exception of the
20568 -- discrete type definition of a loop parameter specification.
20570 if not In_Iter_Schm
20571 and then not Is_OK_Static_Range (R)
20572 then
20573 Check_SPARK_05_Restriction ("range should be static", R);
20574 end if;
20576 Lo := Low_Bound (R);
20577 Hi := High_Bound (R);
20579 -- Validity checks on the range of a quantified expression are
20580 -- delayed until the construct is transformed into a loop.
20582 if Nkind (Parent (R)) = N_Loop_Parameter_Specification
20583 and then Nkind (Parent (Parent (R))) = N_Quantified_Expression
20584 then
20585 null;
20587 -- We need to ensure validity of the bounds here, because if we
20588 -- go ahead and do the expansion, then the expanded code will get
20589 -- analyzed with range checks suppressed and we miss the check.
20591 -- WARNING: The capture of the range bounds with xxx_FIRST/_LAST and
20592 -- the temporaries generated by routine Remove_Side_Effects by means
20593 -- of validity checks must use the same names. When a range appears
20594 -- in the parent of a generic, the range is processed with checks
20595 -- disabled as part of the generic context and with checks enabled
20596 -- for code generation purposes. This leads to link issues as the
20597 -- generic contains references to xxx_FIRST/_LAST, but the inlined
20598 -- template sees the temporaries generated by Remove_Side_Effects.
20600 else
20601 Validity_Check_Range (R, Subtyp);
20602 end if;
20604 -- If there were errors in the declaration, try and patch up some
20605 -- common mistakes in the bounds. The cases handled are literals
20606 -- which are Integer where the expected type is Real and vice versa.
20607 -- These corrections allow the compilation process to proceed further
20608 -- along since some basic assumptions of the format of the bounds
20609 -- are guaranteed.
20611 if Etype (R) = Any_Type then
20612 if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
20613 Rewrite (Lo,
20614 Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
20616 elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
20617 Rewrite (Hi,
20618 Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
20620 elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
20621 Rewrite (Lo,
20622 Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
20624 elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
20625 Rewrite (Hi,
20626 Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
20627 end if;
20629 Set_Etype (Lo, T);
20630 Set_Etype (Hi, T);
20631 end if;
20633 -- If the bounds of the range have been mistakenly given as string
20634 -- literals (perhaps in place of character literals), then an error
20635 -- has already been reported, but we rewrite the string literal as a
20636 -- bound of the range's type to avoid blowups in later processing
20637 -- that looks at static values.
20639 if Nkind (Lo) = N_String_Literal then
20640 Rewrite (Lo,
20641 Make_Attribute_Reference (Sloc (Lo),
20642 Prefix => New_Occurrence_Of (T, Sloc (Lo)),
20643 Attribute_Name => Name_First));
20644 Analyze_And_Resolve (Lo);
20645 end if;
20647 if Nkind (Hi) = N_String_Literal then
20648 Rewrite (Hi,
20649 Make_Attribute_Reference (Sloc (Hi),
20650 Prefix => New_Occurrence_Of (T, Sloc (Hi)),
20651 Attribute_Name => Name_First));
20652 Analyze_And_Resolve (Hi);
20653 end if;
20655 -- If bounds aren't scalar at this point then exit, avoiding
20656 -- problems with further processing of the range in this procedure.
20658 if not Is_Scalar_Type (Etype (Lo)) then
20659 return;
20660 end if;
20662 -- Resolve (actually Sem_Eval) has checked that the bounds are in
20663 -- then range of the base type. Here we check whether the bounds
20664 -- are in the range of the subtype itself. Note that if the bounds
20665 -- represent the null range the Constraint_Error exception should
20666 -- not be raised.
20668 -- ??? The following code should be cleaned up as follows
20670 -- 1. The Is_Null_Range (Lo, Hi) test should disappear since it
20671 -- is done in the call to Range_Check (R, T); below
20673 -- 2. The use of R_Check_Off should be investigated and possibly
20674 -- removed, this would clean up things a bit.
20676 if Is_Null_Range (Lo, Hi) then
20677 null;
20679 else
20680 -- Capture values of bounds and generate temporaries for them
20681 -- if needed, before applying checks, since checks may cause
20682 -- duplication of the expression without forcing evaluation.
20684 -- The forced evaluation removes side effects from expressions,
20685 -- which should occur also in GNATprove mode. Otherwise, we end up
20686 -- with unexpected insertions of actions at places where this is
20687 -- not supposed to occur, e.g. on default parameters of a call.
20689 if Expander_Active or GNATprove_Mode then
20691 -- Call Force_Evaluation to create declarations as needed to
20692 -- deal with side effects, and also create typ_FIRST/LAST
20693 -- entities for bounds if we have a subtype name.
20695 -- Note: we do this transformation even if expansion is not
20696 -- active if we are in GNATprove_Mode since the transformation
20697 -- is in general required to ensure that the resulting tree has
20698 -- proper Ada semantics.
20700 Force_Evaluation
20701 (Lo, Related_Id => Subtyp, Is_Low_Bound => True);
20702 Force_Evaluation
20703 (Hi, Related_Id => Subtyp, Is_High_Bound => True);
20704 end if;
20706 -- We use a flag here instead of suppressing checks on the type
20707 -- because the type we check against isn't necessarily the place
20708 -- where we put the check.
20710 if not R_Check_Off then
20711 R_Checks := Get_Range_Checks (R, T);
20713 -- Look up tree to find an appropriate insertion point. We
20714 -- can't just use insert_actions because later processing
20715 -- depends on the insertion node. Prior to Ada 2012 the
20716 -- insertion point could only be a declaration or a loop, but
20717 -- quantified expressions can appear within any context in an
20718 -- expression, and the insertion point can be any statement,
20719 -- pragma, or declaration.
20721 Insert_Node := Parent (R);
20722 while Present (Insert_Node) loop
20723 exit when
20724 Nkind (Insert_Node) in N_Declaration
20725 and then
20726 not Nkind_In
20727 (Insert_Node, N_Component_Declaration,
20728 N_Loop_Parameter_Specification,
20729 N_Function_Specification,
20730 N_Procedure_Specification);
20732 exit when Nkind (Insert_Node) in N_Later_Decl_Item
20733 or else Nkind (Insert_Node) in
20734 N_Statement_Other_Than_Procedure_Call
20735 or else Nkind_In (Insert_Node, N_Procedure_Call_Statement,
20736 N_Pragma);
20738 Insert_Node := Parent (Insert_Node);
20739 end loop;
20741 -- Why would Type_Decl not be present??? Without this test,
20742 -- short regression tests fail.
20744 if Present (Insert_Node) then
20746 -- Case of loop statement. Verify that the range is part
20747 -- of the subtype indication of the iteration scheme.
20749 if Nkind (Insert_Node) = N_Loop_Statement then
20750 declare
20751 Indic : Node_Id;
20753 begin
20754 Indic := Parent (R);
20755 while Present (Indic)
20756 and then Nkind (Indic) /= N_Subtype_Indication
20757 loop
20758 Indic := Parent (Indic);
20759 end loop;
20761 if Present (Indic) then
20762 Def_Id := Etype (Subtype_Mark (Indic));
20764 Insert_Range_Checks
20765 (R_Checks,
20766 Insert_Node,
20767 Def_Id,
20768 Sloc (Insert_Node),
20770 Do_Before => True);
20771 end if;
20772 end;
20774 -- Insertion before a declaration. If the declaration
20775 -- includes discriminants, the list of applicable checks
20776 -- is given by the caller.
20778 elsif Nkind (Insert_Node) in N_Declaration then
20779 Def_Id := Defining_Identifier (Insert_Node);
20781 if (Ekind (Def_Id) = E_Record_Type
20782 and then Depends_On_Discriminant (R))
20783 or else
20784 (Ekind (Def_Id) = E_Protected_Type
20785 and then Has_Discriminants (Def_Id))
20786 then
20787 Append_Range_Checks
20788 (R_Checks,
20789 Check_List, Def_Id, Sloc (Insert_Node), R);
20791 else
20792 Insert_Range_Checks
20793 (R_Checks,
20794 Insert_Node, Def_Id, Sloc (Insert_Node), R);
20796 end if;
20798 -- Insertion before a statement. Range appears in the
20799 -- context of a quantified expression. Insertion will
20800 -- take place when expression is expanded.
20802 else
20803 null;
20804 end if;
20805 end if;
20806 end if;
20807 end if;
20809 -- Case of other than an explicit N_Range node
20811 -- The forced evaluation removes side effects from expressions, which
20812 -- should occur also in GNATprove mode. Otherwise, we end up with
20813 -- unexpected insertions of actions at places where this is not
20814 -- supposed to occur, e.g. on default parameters of a call.
20816 elsif Expander_Active or GNATprove_Mode then
20817 Get_Index_Bounds (R, Lo, Hi);
20818 Force_Evaluation (Lo);
20819 Force_Evaluation (Hi);
20820 end if;
20821 end Process_Range_Expr_In_Decl;
20823 --------------------------------------
20824 -- Process_Real_Range_Specification --
20825 --------------------------------------
20827 procedure Process_Real_Range_Specification (Def : Node_Id) is
20828 Spec : constant Node_Id := Real_Range_Specification (Def);
20829 Lo : Node_Id;
20830 Hi : Node_Id;
20831 Err : Boolean := False;
20833 procedure Analyze_Bound (N : Node_Id);
20834 -- Analyze and check one bound
20836 -------------------
20837 -- Analyze_Bound --
20838 -------------------
20840 procedure Analyze_Bound (N : Node_Id) is
20841 begin
20842 Analyze_And_Resolve (N, Any_Real);
20844 if not Is_OK_Static_Expression (N) then
20845 Flag_Non_Static_Expr
20846 ("bound in real type definition is not static!", N);
20847 Err := True;
20848 end if;
20849 end Analyze_Bound;
20851 -- Start of processing for Process_Real_Range_Specification
20853 begin
20854 if Present (Spec) then
20855 Lo := Low_Bound (Spec);
20856 Hi := High_Bound (Spec);
20857 Analyze_Bound (Lo);
20858 Analyze_Bound (Hi);
20860 -- If error, clear away junk range specification
20862 if Err then
20863 Set_Real_Range_Specification (Def, Empty);
20864 end if;
20865 end if;
20866 end Process_Real_Range_Specification;
20868 ---------------------
20869 -- Process_Subtype --
20870 ---------------------
20872 function Process_Subtype
20873 (S : Node_Id;
20874 Related_Nod : Node_Id;
20875 Related_Id : Entity_Id := Empty;
20876 Suffix : Character := ' ') return Entity_Id
20878 P : Node_Id;
20879 Def_Id : Entity_Id;
20880 Error_Node : Node_Id;
20881 Full_View_Id : Entity_Id;
20882 Subtype_Mark_Id : Entity_Id;
20884 May_Have_Null_Exclusion : Boolean;
20886 procedure Check_Incomplete (T : Node_Id);
20887 -- Called to verify that an incomplete type is not used prematurely
20889 ----------------------
20890 -- Check_Incomplete --
20891 ----------------------
20893 procedure Check_Incomplete (T : Node_Id) is
20894 begin
20895 -- Ada 2005 (AI-412): Incomplete subtypes are legal
20897 if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
20898 and then
20899 not (Ada_Version >= Ada_2005
20900 and then
20901 (Nkind (Parent (T)) = N_Subtype_Declaration
20902 or else (Nkind (Parent (T)) = N_Subtype_Indication
20903 and then Nkind (Parent (Parent (T))) =
20904 N_Subtype_Declaration)))
20905 then
20906 Error_Msg_N ("invalid use of type before its full declaration", T);
20907 end if;
20908 end Check_Incomplete;
20910 -- Start of processing for Process_Subtype
20912 begin
20913 -- Case of no constraints present
20915 if Nkind (S) /= N_Subtype_Indication then
20916 Find_Type (S);
20917 Check_Incomplete (S);
20918 P := Parent (S);
20920 -- Ada 2005 (AI-231): Static check
20922 if Ada_Version >= Ada_2005
20923 and then Present (P)
20924 and then Null_Exclusion_Present (P)
20925 and then Nkind (P) /= N_Access_To_Object_Definition
20926 and then not Is_Access_Type (Entity (S))
20927 then
20928 Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
20929 end if;
20931 -- The following is ugly, can't we have a range or even a flag???
20933 May_Have_Null_Exclusion :=
20934 Nkind_In (P, N_Access_Definition,
20935 N_Access_Function_Definition,
20936 N_Access_Procedure_Definition,
20937 N_Access_To_Object_Definition,
20938 N_Allocator,
20939 N_Component_Definition)
20940 or else
20941 Nkind_In (P, N_Derived_Type_Definition,
20942 N_Discriminant_Specification,
20943 N_Formal_Object_Declaration,
20944 N_Object_Declaration,
20945 N_Object_Renaming_Declaration,
20946 N_Parameter_Specification,
20947 N_Subtype_Declaration);
20949 -- Create an Itype that is a duplicate of Entity (S) but with the
20950 -- null-exclusion attribute.
20952 if May_Have_Null_Exclusion
20953 and then Is_Access_Type (Entity (S))
20954 and then Null_Exclusion_Present (P)
20956 -- No need to check the case of an access to object definition.
20957 -- It is correct to define double not-null pointers.
20959 -- Example:
20960 -- type Not_Null_Int_Ptr is not null access Integer;
20961 -- type Acc is not null access Not_Null_Int_Ptr;
20963 and then Nkind (P) /= N_Access_To_Object_Definition
20964 then
20965 if Can_Never_Be_Null (Entity (S)) then
20966 case Nkind (Related_Nod) is
20967 when N_Full_Type_Declaration =>
20968 if Nkind (Type_Definition (Related_Nod))
20969 in N_Array_Type_Definition
20970 then
20971 Error_Node :=
20972 Subtype_Indication
20973 (Component_Definition
20974 (Type_Definition (Related_Nod)));
20975 else
20976 Error_Node :=
20977 Subtype_Indication (Type_Definition (Related_Nod));
20978 end if;
20980 when N_Subtype_Declaration =>
20981 Error_Node := Subtype_Indication (Related_Nod);
20983 when N_Object_Declaration =>
20984 Error_Node := Object_Definition (Related_Nod);
20986 when N_Component_Declaration =>
20987 Error_Node :=
20988 Subtype_Indication (Component_Definition (Related_Nod));
20990 when N_Allocator =>
20991 Error_Node := Expression (Related_Nod);
20993 when others =>
20994 pragma Assert (False);
20995 Error_Node := Related_Nod;
20996 end case;
20998 Error_Msg_NE
20999 ("`NOT NULL` not allowed (& already excludes null)",
21000 Error_Node,
21001 Entity (S));
21002 end if;
21004 Set_Etype (S,
21005 Create_Null_Excluding_Itype
21006 (T => Entity (S),
21007 Related_Nod => P));
21008 Set_Entity (S, Etype (S));
21009 end if;
21011 return Entity (S);
21013 -- Case of constraint present, so that we have an N_Subtype_Indication
21014 -- node (this node is created only if constraints are present).
21016 else
21017 Find_Type (Subtype_Mark (S));
21019 if Nkind (Parent (S)) /= N_Access_To_Object_Definition
21020 and then not
21021 (Nkind (Parent (S)) = N_Subtype_Declaration
21022 and then Is_Itype (Defining_Identifier (Parent (S))))
21023 then
21024 Check_Incomplete (Subtype_Mark (S));
21025 end if;
21027 P := Parent (S);
21028 Subtype_Mark_Id := Entity (Subtype_Mark (S));
21030 -- Explicit subtype declaration case
21032 if Nkind (P) = N_Subtype_Declaration then
21033 Def_Id := Defining_Identifier (P);
21035 -- Explicit derived type definition case
21037 elsif Nkind (P) = N_Derived_Type_Definition then
21038 Def_Id := Defining_Identifier (Parent (P));
21040 -- Implicit case, the Def_Id must be created as an implicit type.
21041 -- The one exception arises in the case of concurrent types, array
21042 -- and access types, where other subsidiary implicit types may be
21043 -- created and must appear before the main implicit type. In these
21044 -- cases we leave Def_Id set to Empty as a signal that Create_Itype
21045 -- has not yet been called to create Def_Id.
21047 else
21048 if Is_Array_Type (Subtype_Mark_Id)
21049 or else Is_Concurrent_Type (Subtype_Mark_Id)
21050 or else Is_Access_Type (Subtype_Mark_Id)
21051 then
21052 Def_Id := Empty;
21054 -- For the other cases, we create a new unattached Itype,
21055 -- and set the indication to ensure it gets attached later.
21057 else
21058 Def_Id :=
21059 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
21060 end if;
21061 end if;
21063 -- If the kind of constraint is invalid for this kind of type,
21064 -- then give an error, and then pretend no constraint was given.
21066 if not Is_Valid_Constraint_Kind
21067 (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
21068 then
21069 Error_Msg_N
21070 ("incorrect constraint for this kind of type", Constraint (S));
21072 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
21074 -- Set Ekind of orphan itype, to prevent cascaded errors
21076 if Present (Def_Id) then
21077 Set_Ekind (Def_Id, Ekind (Any_Type));
21078 end if;
21080 -- Make recursive call, having got rid of the bogus constraint
21082 return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
21083 end if;
21085 -- Remaining processing depends on type. Select on Base_Type kind to
21086 -- ensure getting to the concrete type kind in the case of a private
21087 -- subtype (needed when only doing semantic analysis).
21089 case Ekind (Base_Type (Subtype_Mark_Id)) is
21090 when Access_Kind =>
21092 -- If this is a constraint on a class-wide type, discard it.
21093 -- There is currently no way to express a partial discriminant
21094 -- constraint on a type with unknown discriminants. This is
21095 -- a pathology that the ACATS wisely decides not to test.
21097 if Is_Class_Wide_Type (Designated_Type (Subtype_Mark_Id)) then
21098 if Comes_From_Source (S) then
21099 Error_Msg_N
21100 ("constraint on class-wide type ignored??",
21101 Constraint (S));
21102 end if;
21104 if Nkind (P) = N_Subtype_Declaration then
21105 Set_Subtype_Indication (P,
21106 New_Occurrence_Of (Subtype_Mark_Id, Sloc (S)));
21107 end if;
21109 return Subtype_Mark_Id;
21110 end if;
21112 Constrain_Access (Def_Id, S, Related_Nod);
21114 if Expander_Active
21115 and then Is_Itype (Designated_Type (Def_Id))
21116 and then Nkind (Related_Nod) = N_Subtype_Declaration
21117 and then not Is_Incomplete_Type (Designated_Type (Def_Id))
21118 then
21119 Build_Itype_Reference
21120 (Designated_Type (Def_Id), Related_Nod);
21121 end if;
21123 when Array_Kind =>
21124 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
21126 when Decimal_Fixed_Point_Kind =>
21127 Constrain_Decimal (Def_Id, S);
21129 when Enumeration_Kind =>
21130 Constrain_Enumeration (Def_Id, S);
21131 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
21133 when Ordinary_Fixed_Point_Kind =>
21134 Constrain_Ordinary_Fixed (Def_Id, S);
21136 when Float_Kind =>
21137 Constrain_Float (Def_Id, S);
21139 when Integer_Kind =>
21140 Constrain_Integer (Def_Id, S);
21141 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
21143 when Class_Wide_Kind
21144 | E_Incomplete_Type
21145 | E_Record_Subtype
21146 | E_Record_Type
21148 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
21150 if Ekind (Def_Id) = E_Incomplete_Type then
21151 Set_Private_Dependents (Def_Id, New_Elmt_List);
21152 end if;
21154 when Private_Kind =>
21155 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
21157 -- The base type may be private but Def_Id may be a full view
21158 -- in an instance.
21160 if Is_Private_Type (Def_Id) then
21161 Set_Private_Dependents (Def_Id, New_Elmt_List);
21162 end if;
21164 -- In case of an invalid constraint prevent further processing
21165 -- since the type constructed is missing expected fields.
21167 if Etype (Def_Id) = Any_Type then
21168 return Def_Id;
21169 end if;
21171 -- If the full view is that of a task with discriminants,
21172 -- we must constrain both the concurrent type and its
21173 -- corresponding record type. Otherwise we will just propagate
21174 -- the constraint to the full view, if available.
21176 if Present (Full_View (Subtype_Mark_Id))
21177 and then Has_Discriminants (Subtype_Mark_Id)
21178 and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
21179 then
21180 Full_View_Id :=
21181 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
21183 Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
21184 Constrain_Concurrent (Full_View_Id, S,
21185 Related_Nod, Related_Id, Suffix);
21186 Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
21187 Set_Full_View (Def_Id, Full_View_Id);
21189 -- Introduce an explicit reference to the private subtype,
21190 -- to prevent scope anomalies in gigi if first use appears
21191 -- in a nested context, e.g. a later function body.
21192 -- Should this be generated in other contexts than a full
21193 -- type declaration?
21195 if Is_Itype (Def_Id)
21196 and then
21197 Nkind (Parent (P)) = N_Full_Type_Declaration
21198 then
21199 Build_Itype_Reference (Def_Id, Parent (P));
21200 end if;
21202 else
21203 Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
21204 end if;
21206 when Concurrent_Kind =>
21207 Constrain_Concurrent (Def_Id, S,
21208 Related_Nod, Related_Id, Suffix);
21210 when others =>
21211 Error_Msg_N ("invalid subtype mark in subtype indication", S);
21212 end case;
21214 -- Size and Convention are always inherited from the base type
21216 Set_Size_Info (Def_Id, (Subtype_Mark_Id));
21217 Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
21219 return Def_Id;
21220 end if;
21221 end Process_Subtype;
21223 -----------------------------
21224 -- Record_Type_Declaration --
21225 -----------------------------
21227 procedure Record_Type_Declaration
21228 (T : Entity_Id;
21229 N : Node_Id;
21230 Prev : Entity_Id)
21232 Def : constant Node_Id := Type_Definition (N);
21233 Is_Tagged : Boolean;
21234 Tag_Comp : Entity_Id;
21236 begin
21237 -- These flags must be initialized before calling Process_Discriminants
21238 -- because this routine makes use of them.
21240 Set_Ekind (T, E_Record_Type);
21241 Set_Etype (T, T);
21242 Init_Size_Align (T);
21243 Set_Interfaces (T, No_Elist);
21244 Set_Stored_Constraint (T, No_Elist);
21245 Set_Default_SSO (T);
21247 -- Normal case
21249 if Ada_Version < Ada_2005 or else not Interface_Present (Def) then
21250 if Limited_Present (Def) then
21251 Check_SPARK_05_Restriction ("limited is not allowed", N);
21252 end if;
21254 if Abstract_Present (Def) then
21255 Check_SPARK_05_Restriction ("abstract is not allowed", N);
21256 end if;
21258 -- The flag Is_Tagged_Type might have already been set by
21259 -- Find_Type_Name if it detected an error for declaration T. This
21260 -- arises in the case of private tagged types where the full view
21261 -- omits the word tagged.
21263 Is_Tagged :=
21264 Tagged_Present (Def)
21265 or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
21267 Set_Is_Limited_Record (T, Limited_Present (Def));
21269 if Is_Tagged then
21270 Set_Is_Tagged_Type (T, True);
21271 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
21272 end if;
21274 -- Type is abstract if full declaration carries keyword, or if
21275 -- previous partial view did.
21277 Set_Is_Abstract_Type (T, Is_Abstract_Type (T)
21278 or else Abstract_Present (Def));
21280 else
21281 Check_SPARK_05_Restriction ("interface is not allowed", N);
21283 Is_Tagged := True;
21284 Analyze_Interface_Declaration (T, Def);
21286 if Present (Discriminant_Specifications (N)) then
21287 Error_Msg_N
21288 ("interface types cannot have discriminants",
21289 Defining_Identifier
21290 (First (Discriminant_Specifications (N))));
21291 end if;
21292 end if;
21294 -- First pass: if there are self-referential access components,
21295 -- create the required anonymous access type declarations, and if
21296 -- need be an incomplete type declaration for T itself.
21298 Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
21300 if Ada_Version >= Ada_2005
21301 and then Present (Interface_List (Def))
21302 then
21303 Check_Interfaces (N, Def);
21305 declare
21306 Ifaces_List : Elist_Id;
21308 begin
21309 -- Ada 2005 (AI-251): Collect the list of progenitors that are not
21310 -- already in the parents.
21312 Collect_Interfaces
21313 (T => T,
21314 Ifaces_List => Ifaces_List,
21315 Exclude_Parents => True);
21317 Set_Interfaces (T, Ifaces_List);
21318 end;
21319 end if;
21321 -- Records constitute a scope for the component declarations within.
21322 -- The scope is created prior to the processing of these declarations.
21323 -- Discriminants are processed first, so that they are visible when
21324 -- processing the other components. The Ekind of the record type itself
21325 -- is set to E_Record_Type (subtypes appear as E_Record_Subtype).
21327 -- Enter record scope
21329 Push_Scope (T);
21331 -- If an incomplete or private type declaration was already given for
21332 -- the type, then this scope already exists, and the discriminants have
21333 -- been declared within. We must verify that the full declaration
21334 -- matches the incomplete one.
21336 Check_Or_Process_Discriminants (N, T, Prev);
21338 Set_Is_Constrained (T, not Has_Discriminants (T));
21339 Set_Has_Delayed_Freeze (T, True);
21341 -- For tagged types add a manually analyzed component corresponding
21342 -- to the component _tag, the corresponding piece of tree will be
21343 -- expanded as part of the freezing actions if it is not a CPP_Class.
21345 if Is_Tagged then
21347 -- Do not add the tag unless we are in expansion mode
21349 if Expander_Active then
21350 Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
21351 Enter_Name (Tag_Comp);
21353 Set_Ekind (Tag_Comp, E_Component);
21354 Set_Is_Tag (Tag_Comp);
21355 Set_Is_Aliased (Tag_Comp);
21356 Set_Etype (Tag_Comp, RTE (RE_Tag));
21357 Set_DT_Entry_Count (Tag_Comp, No_Uint);
21358 Set_Original_Record_Component (Tag_Comp, Tag_Comp);
21359 Init_Component_Location (Tag_Comp);
21361 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
21362 -- implemented interfaces.
21364 if Has_Interfaces (T) then
21365 Add_Interface_Tag_Components (N, T);
21366 end if;
21367 end if;
21369 Make_Class_Wide_Type (T);
21370 Set_Direct_Primitive_Operations (T, New_Elmt_List);
21371 end if;
21373 -- We must suppress range checks when processing record components in
21374 -- the presence of discriminants, since we don't want spurious checks to
21375 -- be generated during their analysis, but Suppress_Range_Checks flags
21376 -- must be reset the after processing the record definition.
21378 -- Note: this is the only use of Kill_Range_Checks, and is a bit odd,
21379 -- couldn't we just use the normal range check suppression method here.
21380 -- That would seem cleaner ???
21382 if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
21383 Set_Kill_Range_Checks (T, True);
21384 Record_Type_Definition (Def, Prev);
21385 Set_Kill_Range_Checks (T, False);
21386 else
21387 Record_Type_Definition (Def, Prev);
21388 end if;
21390 -- Exit from record scope
21392 End_Scope;
21394 -- Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
21395 -- the implemented interfaces and associate them an aliased entity.
21397 if Is_Tagged
21398 and then not Is_Empty_List (Interface_List (Def))
21399 then
21400 Derive_Progenitor_Subprograms (T, T);
21401 end if;
21403 Check_Function_Writable_Actuals (N);
21404 end Record_Type_Declaration;
21406 ----------------------------
21407 -- Record_Type_Definition --
21408 ----------------------------
21410 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
21411 Component : Entity_Id;
21412 Ctrl_Components : Boolean := False;
21413 Final_Storage_Only : Boolean;
21414 T : Entity_Id;
21416 begin
21417 if Ekind (Prev_T) = E_Incomplete_Type then
21418 T := Full_View (Prev_T);
21419 else
21420 T := Prev_T;
21421 end if;
21423 -- In SPARK, tagged types and type extensions may only be declared in
21424 -- the specification of library unit packages.
21426 if Present (Def) and then Is_Tagged_Type (T) then
21427 declare
21428 Typ : Node_Id;
21429 Ctxt : Node_Id;
21431 begin
21432 if Nkind (Parent (Def)) = N_Full_Type_Declaration then
21433 Typ := Parent (Def);
21434 else
21435 pragma Assert
21436 (Nkind (Parent (Def)) = N_Derived_Type_Definition);
21437 Typ := Parent (Parent (Def));
21438 end if;
21440 Ctxt := Parent (Typ);
21442 if Nkind (Ctxt) = N_Package_Body
21443 and then Nkind (Parent (Ctxt)) = N_Compilation_Unit
21444 then
21445 Check_SPARK_05_Restriction
21446 ("type should be defined in package specification", Typ);
21448 elsif Nkind (Ctxt) /= N_Package_Specification
21449 or else Nkind (Parent (Parent (Ctxt))) /= N_Compilation_Unit
21450 then
21451 Check_SPARK_05_Restriction
21452 ("type should be defined in library unit package", Typ);
21453 end if;
21454 end;
21455 end if;
21457 Final_Storage_Only := not Is_Controlled_Active (T);
21459 -- Ada 2005: Check whether an explicit Limited is present in a derived
21460 -- type declaration.
21462 if Nkind (Parent (Def)) = N_Derived_Type_Definition
21463 and then Limited_Present (Parent (Def))
21464 then
21465 Set_Is_Limited_Record (T);
21466 end if;
21468 -- If the component list of a record type is defined by the reserved
21469 -- word null and there is no discriminant part, then the record type has
21470 -- no components and all records of the type are null records (RM 3.7)
21471 -- This procedure is also called to process the extension part of a
21472 -- record extension, in which case the current scope may have inherited
21473 -- components.
21475 if No (Def)
21476 or else No (Component_List (Def))
21477 or else Null_Present (Component_List (Def))
21478 then
21479 if not Is_Tagged_Type (T) then
21480 Check_SPARK_05_Restriction ("untagged record cannot be null", Def);
21481 end if;
21483 else
21484 Analyze_Declarations (Component_Items (Component_List (Def)));
21486 if Present (Variant_Part (Component_List (Def))) then
21487 Check_SPARK_05_Restriction ("variant part is not allowed", Def);
21488 Analyze (Variant_Part (Component_List (Def)));
21489 end if;
21490 end if;
21492 -- After completing the semantic analysis of the record definition,
21493 -- record components, both new and inherited, are accessible. Set their
21494 -- kind accordingly. Exclude malformed itypes from illegal declarations,
21495 -- whose Ekind may be void.
21497 Component := First_Entity (Current_Scope);
21498 while Present (Component) loop
21499 if Ekind (Component) = E_Void
21500 and then not Is_Itype (Component)
21501 then
21502 Set_Ekind (Component, E_Component);
21503 Init_Component_Location (Component);
21504 end if;
21506 Propagate_Concurrent_Flags (T, Etype (Component));
21508 if Ekind (Component) /= E_Component then
21509 null;
21511 -- Do not set Has_Controlled_Component on a class-wide equivalent
21512 -- type. See Make_CW_Equivalent_Type.
21514 elsif not Is_Class_Wide_Equivalent_Type (T)
21515 and then (Has_Controlled_Component (Etype (Component))
21516 or else (Chars (Component) /= Name_uParent
21517 and then Is_Controlled_Active
21518 (Etype (Component))))
21519 then
21520 Set_Has_Controlled_Component (T, True);
21521 Final_Storage_Only :=
21522 Final_Storage_Only
21523 and then Finalize_Storage_Only (Etype (Component));
21524 Ctrl_Components := True;
21525 end if;
21527 Next_Entity (Component);
21528 end loop;
21530 -- A Type is Finalize_Storage_Only only if all its controlled components
21531 -- are also.
21533 if Ctrl_Components then
21534 Set_Finalize_Storage_Only (T, Final_Storage_Only);
21535 end if;
21537 -- Place reference to end record on the proper entity, which may
21538 -- be a partial view.
21540 if Present (Def) then
21541 Process_End_Label (Def, 'e', Prev_T);
21542 end if;
21543 end Record_Type_Definition;
21545 ------------------------
21546 -- Replace_Components --
21547 ------------------------
21549 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id) is
21550 function Process (N : Node_Id) return Traverse_Result;
21552 -------------
21553 -- Process --
21554 -------------
21556 function Process (N : Node_Id) return Traverse_Result is
21557 Comp : Entity_Id;
21559 begin
21560 if Nkind (N) = N_Discriminant_Specification then
21561 Comp := First_Discriminant (Typ);
21562 while Present (Comp) loop
21563 if Chars (Comp) = Chars (Defining_Identifier (N)) then
21564 Set_Defining_Identifier (N, Comp);
21565 exit;
21566 end if;
21568 Next_Discriminant (Comp);
21569 end loop;
21571 elsif Nkind (N) = N_Component_Declaration then
21572 Comp := First_Component (Typ);
21573 while Present (Comp) loop
21574 if Chars (Comp) = Chars (Defining_Identifier (N)) then
21575 Set_Defining_Identifier (N, Comp);
21576 exit;
21577 end if;
21579 Next_Component (Comp);
21580 end loop;
21581 end if;
21583 return OK;
21584 end Process;
21586 procedure Replace is new Traverse_Proc (Process);
21588 -- Start of processing for Replace_Components
21590 begin
21591 Replace (Decl);
21592 end Replace_Components;
21594 -------------------------------
21595 -- Set_Completion_Referenced --
21596 -------------------------------
21598 procedure Set_Completion_Referenced (E : Entity_Id) is
21599 begin
21600 -- If in main unit, mark entity that is a completion as referenced,
21601 -- warnings go on the partial view when needed.
21603 if In_Extended_Main_Source_Unit (E) then
21604 Set_Referenced (E);
21605 end if;
21606 end Set_Completion_Referenced;
21608 ---------------------
21609 -- Set_Default_SSO --
21610 ---------------------
21612 procedure Set_Default_SSO (T : Entity_Id) is
21613 begin
21614 case Opt.Default_SSO is
21615 when ' ' =>
21616 null;
21617 when 'L' =>
21618 Set_SSO_Set_Low_By_Default (T, True);
21619 when 'H' =>
21620 Set_SSO_Set_High_By_Default (T, True);
21621 when others =>
21622 raise Program_Error;
21623 end case;
21624 end Set_Default_SSO;
21626 ---------------------
21627 -- Set_Fixed_Range --
21628 ---------------------
21630 -- The range for fixed-point types is complicated by the fact that we
21631 -- do not know the exact end points at the time of the declaration. This
21632 -- is true for three reasons:
21634 -- A size clause may affect the fudging of the end-points.
21635 -- A small clause may affect the values of the end-points.
21636 -- We try to include the end-points if it does not affect the size.
21638 -- This means that the actual end-points must be established at the
21639 -- point when the type is frozen. Meanwhile, we first narrow the range
21640 -- as permitted (so that it will fit if necessary in a small specified
21641 -- size), and then build a range subtree with these narrowed bounds.
21642 -- Set_Fixed_Range constructs the range from real literal values, and
21643 -- sets the range as the Scalar_Range of the given fixed-point type entity.
21645 -- The parent of this range is set to point to the entity so that it is
21646 -- properly hooked into the tree (unlike normal Scalar_Range entries for
21647 -- other scalar types, which are just pointers to the range in the
21648 -- original tree, this would otherwise be an orphan).
21650 -- The tree is left unanalyzed. When the type is frozen, the processing
21651 -- in Freeze.Freeze_Fixed_Point_Type notices that the range is not
21652 -- analyzed, and uses this as an indication that it should complete
21653 -- work on the range (it will know the final small and size values).
21655 procedure Set_Fixed_Range
21656 (E : Entity_Id;
21657 Loc : Source_Ptr;
21658 Lo : Ureal;
21659 Hi : Ureal)
21661 S : constant Node_Id :=
21662 Make_Range (Loc,
21663 Low_Bound => Make_Real_Literal (Loc, Lo),
21664 High_Bound => Make_Real_Literal (Loc, Hi));
21665 begin
21666 Set_Scalar_Range (E, S);
21667 Set_Parent (S, E);
21669 -- Before the freeze point, the bounds of a fixed point are universal
21670 -- and carry the corresponding type.
21672 Set_Etype (Low_Bound (S), Universal_Real);
21673 Set_Etype (High_Bound (S), Universal_Real);
21674 end Set_Fixed_Range;
21676 ----------------------------------
21677 -- Set_Scalar_Range_For_Subtype --
21678 ----------------------------------
21680 procedure Set_Scalar_Range_For_Subtype
21681 (Def_Id : Entity_Id;
21682 R : Node_Id;
21683 Subt : Entity_Id)
21685 Kind : constant Entity_Kind := Ekind (Def_Id);
21687 begin
21688 -- Defend against previous error
21690 if Nkind (R) = N_Error then
21691 return;
21692 end if;
21694 Set_Scalar_Range (Def_Id, R);
21696 -- We need to link the range into the tree before resolving it so
21697 -- that types that are referenced, including importantly the subtype
21698 -- itself, are properly frozen (Freeze_Expression requires that the
21699 -- expression be properly linked into the tree). Of course if it is
21700 -- already linked in, then we do not disturb the current link.
21702 if No (Parent (R)) then
21703 Set_Parent (R, Def_Id);
21704 end if;
21706 -- Reset the kind of the subtype during analysis of the range, to
21707 -- catch possible premature use in the bounds themselves.
21709 Set_Ekind (Def_Id, E_Void);
21710 Process_Range_Expr_In_Decl (R, Subt, Subtyp => Def_Id);
21711 Set_Ekind (Def_Id, Kind);
21712 end Set_Scalar_Range_For_Subtype;
21714 --------------------------------------------------------
21715 -- Set_Stored_Constraint_From_Discriminant_Constraint --
21716 --------------------------------------------------------
21718 procedure Set_Stored_Constraint_From_Discriminant_Constraint
21719 (E : Entity_Id)
21721 begin
21722 -- Make sure set if encountered during Expand_To_Stored_Constraint
21724 Set_Stored_Constraint (E, No_Elist);
21726 -- Give it the right value
21728 if Is_Constrained (E) and then Has_Discriminants (E) then
21729 Set_Stored_Constraint (E,
21730 Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
21731 end if;
21732 end Set_Stored_Constraint_From_Discriminant_Constraint;
21734 -------------------------------------
21735 -- Signed_Integer_Type_Declaration --
21736 -------------------------------------
21738 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
21739 Implicit_Base : Entity_Id;
21740 Base_Typ : Entity_Id;
21741 Lo_Val : Uint;
21742 Hi_Val : Uint;
21743 Errs : Boolean := False;
21744 Lo : Node_Id;
21745 Hi : Node_Id;
21747 function Can_Derive_From (E : Entity_Id) return Boolean;
21748 -- Determine whether given bounds allow derivation from specified type
21750 procedure Check_Bound (Expr : Node_Id);
21751 -- Check bound to make sure it is integral and static. If not, post
21752 -- appropriate error message and set Errs flag
21754 ---------------------
21755 -- Can_Derive_From --
21756 ---------------------
21758 -- Note we check both bounds against both end values, to deal with
21759 -- strange types like ones with a range of 0 .. -12341234.
21761 function Can_Derive_From (E : Entity_Id) return Boolean is
21762 Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
21763 Hi : constant Uint := Expr_Value (Type_High_Bound (E));
21764 begin
21765 return Lo <= Lo_Val and then Lo_Val <= Hi
21766 and then
21767 Lo <= Hi_Val and then Hi_Val <= Hi;
21768 end Can_Derive_From;
21770 -----------------
21771 -- Check_Bound --
21772 -----------------
21774 procedure Check_Bound (Expr : Node_Id) is
21775 begin
21776 -- If a range constraint is used as an integer type definition, each
21777 -- bound of the range must be defined by a static expression of some
21778 -- integer type, but the two bounds need not have the same integer
21779 -- type (Negative bounds are allowed.) (RM 3.5.4)
21781 if not Is_Integer_Type (Etype (Expr)) then
21782 Error_Msg_N
21783 ("integer type definition bounds must be of integer type", Expr);
21784 Errs := True;
21786 elsif not Is_OK_Static_Expression (Expr) then
21787 Flag_Non_Static_Expr
21788 ("non-static expression used for integer type bound!", Expr);
21789 Errs := True;
21791 -- The bounds are folded into literals, and we set their type to be
21792 -- universal, to avoid typing difficulties: we cannot set the type
21793 -- of the literal to the new type, because this would be a forward
21794 -- reference for the back end, and if the original type is user-
21795 -- defined this can lead to spurious semantic errors (e.g. 2928-003).
21797 else
21798 if Is_Entity_Name (Expr) then
21799 Fold_Uint (Expr, Expr_Value (Expr), True);
21800 end if;
21802 Set_Etype (Expr, Universal_Integer);
21803 end if;
21804 end Check_Bound;
21806 -- Start of processing for Signed_Integer_Type_Declaration
21808 begin
21809 -- Create an anonymous base type
21811 Implicit_Base :=
21812 Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
21814 -- Analyze and check the bounds, they can be of any integer type
21816 Lo := Low_Bound (Def);
21817 Hi := High_Bound (Def);
21819 -- Arbitrarily use Integer as the type if either bound had an error
21821 if Hi = Error or else Lo = Error then
21822 Base_Typ := Any_Integer;
21823 Set_Error_Posted (T, True);
21825 -- Here both bounds are OK expressions
21827 else
21828 Analyze_And_Resolve (Lo, Any_Integer);
21829 Analyze_And_Resolve (Hi, Any_Integer);
21831 Check_Bound (Lo);
21832 Check_Bound (Hi);
21834 if Errs then
21835 Hi := Type_High_Bound (Standard_Long_Long_Integer);
21836 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
21837 end if;
21839 -- Find type to derive from
21841 Lo_Val := Expr_Value (Lo);
21842 Hi_Val := Expr_Value (Hi);
21844 if Can_Derive_From (Standard_Short_Short_Integer) then
21845 Base_Typ := Base_Type (Standard_Short_Short_Integer);
21847 elsif Can_Derive_From (Standard_Short_Integer) then
21848 Base_Typ := Base_Type (Standard_Short_Integer);
21850 elsif Can_Derive_From (Standard_Integer) then
21851 Base_Typ := Base_Type (Standard_Integer);
21853 elsif Can_Derive_From (Standard_Long_Integer) then
21854 Base_Typ := Base_Type (Standard_Long_Integer);
21856 elsif Can_Derive_From (Standard_Long_Long_Integer) then
21857 Check_Restriction (No_Long_Long_Integers, Def);
21858 Base_Typ := Base_Type (Standard_Long_Long_Integer);
21860 else
21861 Base_Typ := Base_Type (Standard_Long_Long_Integer);
21862 Error_Msg_N ("integer type definition bounds out of range", Def);
21863 Hi := Type_High_Bound (Standard_Long_Long_Integer);
21864 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
21865 end if;
21866 end if;
21868 -- Complete both implicit base and declared first subtype entities. The
21869 -- inheritance of the rep item chain ensures that SPARK-related pragmas
21870 -- are not clobbered when the signed integer type acts as a full view of
21871 -- a private type.
21873 Set_Etype (Implicit_Base, Base_Typ);
21874 Set_Size_Info (Implicit_Base, Base_Typ);
21875 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
21876 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
21877 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
21879 Set_Ekind (T, E_Signed_Integer_Subtype);
21880 Set_Etype (T, Implicit_Base);
21881 Set_Size_Info (T, Implicit_Base);
21882 Inherit_Rep_Item_Chain (T, Implicit_Base);
21883 Set_Scalar_Range (T, Def);
21884 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
21885 Set_Is_Constrained (T);
21886 end Signed_Integer_Type_Declaration;
21888 end Sem_Ch3;