2015-05-20 François Dumont <fdumont@gcc.gnu.org>
[official-gcc.git] / gcc / ada / sem_ch3.adb
blob75bf87448a71ffa6d0a8055ba4c8c20c665c5c2c
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ C H 3 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2015, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Debug; use Debug;
30 with Elists; use Elists;
31 with Einfo; use Einfo;
32 with Errout; use Errout;
33 with Eval_Fat; use Eval_Fat;
34 with Exp_Ch3; use Exp_Ch3;
35 with Exp_Ch9; use Exp_Ch9;
36 with Exp_Disp; use Exp_Disp;
37 with Exp_Dist; use Exp_Dist;
38 with Exp_Tss; use Exp_Tss;
39 with Exp_Util; use Exp_Util;
40 with Fname; use Fname;
41 with Freeze; use Freeze;
42 with Ghost; use Ghost;
43 with Itypes; use Itypes;
44 with Layout; use Layout;
45 with Lib; use Lib;
46 with Lib.Xref; use Lib.Xref;
47 with Namet; use Namet;
48 with Nmake; use Nmake;
49 with Opt; use Opt;
50 with Restrict; use Restrict;
51 with Rident; use Rident;
52 with Rtsfind; use Rtsfind;
53 with Sem; use Sem;
54 with Sem_Aux; use Sem_Aux;
55 with Sem_Case; use Sem_Case;
56 with Sem_Cat; use Sem_Cat;
57 with Sem_Ch6; use Sem_Ch6;
58 with Sem_Ch7; use Sem_Ch7;
59 with Sem_Ch8; use Sem_Ch8;
60 with Sem_Ch10; use Sem_Ch10;
61 with Sem_Ch12; use Sem_Ch12;
62 with Sem_Ch13; use Sem_Ch13;
63 with Sem_Dim; use Sem_Dim;
64 with Sem_Disp; use Sem_Disp;
65 with Sem_Dist; use Sem_Dist;
66 with Sem_Elim; use Sem_Elim;
67 with Sem_Eval; use Sem_Eval;
68 with Sem_Mech; use Sem_Mech;
69 with Sem_Prag; use Sem_Prag;
70 with Sem_Res; use Sem_Res;
71 with Sem_Smem; use Sem_Smem;
72 with Sem_Type; use Sem_Type;
73 with Sem_Util; use Sem_Util;
74 with Sem_Warn; use Sem_Warn;
75 with Stand; use Stand;
76 with Sinfo; use Sinfo;
77 with Sinput; use Sinput;
78 with Snames; use Snames;
79 with Targparm; use Targparm;
80 with Tbuild; use Tbuild;
81 with Ttypes; use Ttypes;
82 with Uintp; use Uintp;
83 with Urealp; use Urealp;
85 package body Sem_Ch3 is
87 -----------------------
88 -- Local Subprograms --
89 -----------------------
91 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id);
92 -- Ada 2005 (AI-251): Add the tag components corresponding to all the
93 -- abstract interface types implemented by a record type or a derived
94 -- record type.
96 procedure Analyze_Object_Contract (Obj_Id : Entity_Id);
97 -- Analyze all delayed pragmas chained on the contract of object Obj_Id as
98 -- if they appeared at the end of the declarative region. The pragmas to be
99 -- considered are:
100 -- Async_Readers
101 -- Async_Writers
102 -- Effective_Reads
103 -- Effective_Writes
104 -- Part_Of
106 procedure Build_Derived_Type
107 (N : Node_Id;
108 Parent_Type : Entity_Id;
109 Derived_Type : Entity_Id;
110 Is_Completion : Boolean;
111 Derive_Subps : Boolean := True);
112 -- Create and decorate a Derived_Type given the Parent_Type entity. N is
113 -- the N_Full_Type_Declaration node containing the derived type definition.
114 -- Parent_Type is the entity for the parent type in the derived type
115 -- definition and Derived_Type the actual derived type. Is_Completion must
116 -- be set to False if Derived_Type is the N_Defining_Identifier node in N
117 -- (i.e. Derived_Type = Defining_Identifier (N)). In this case N is not the
118 -- completion of a private type declaration. If Is_Completion is set to
119 -- True, N is the completion of a private type declaration and Derived_Type
120 -- is different from the defining identifier inside N (i.e. Derived_Type /=
121 -- Defining_Identifier (N)). Derive_Subps indicates whether the parent
122 -- subprograms should be derived. The only case where this parameter is
123 -- False is when Build_Derived_Type is recursively called to process an
124 -- implicit derived full type for a type derived from a private type (in
125 -- that case the subprograms must only be derived for the private view of
126 -- the type).
128 -- ??? These flags need a bit of re-examination and re-documentation:
129 -- ??? are they both necessary (both seem related to the recursion)?
131 procedure Build_Derived_Access_Type
132 (N : Node_Id;
133 Parent_Type : Entity_Id;
134 Derived_Type : Entity_Id);
135 -- Subsidiary procedure to Build_Derived_Type. For a derived access type,
136 -- create an implicit base if the parent type is constrained or if the
137 -- subtype indication has a constraint.
139 procedure Build_Derived_Array_Type
140 (N : Node_Id;
141 Parent_Type : Entity_Id;
142 Derived_Type : Entity_Id);
143 -- Subsidiary procedure to Build_Derived_Type. For a derived array type,
144 -- create an implicit base if the parent type is constrained or if the
145 -- subtype indication has a constraint.
147 procedure Build_Derived_Concurrent_Type
148 (N : Node_Id;
149 Parent_Type : Entity_Id;
150 Derived_Type : Entity_Id);
151 -- Subsidiary procedure to Build_Derived_Type. For a derived task or
152 -- protected type, inherit entries and protected subprograms, check
153 -- legality of discriminant constraints if any.
155 procedure Build_Derived_Enumeration_Type
156 (N : Node_Id;
157 Parent_Type : Entity_Id;
158 Derived_Type : Entity_Id);
159 -- Subsidiary procedure to Build_Derived_Type. For a derived enumeration
160 -- type, we must create a new list of literals. Types derived from
161 -- Character and [Wide_]Wide_Character are special-cased.
163 procedure Build_Derived_Numeric_Type
164 (N : Node_Id;
165 Parent_Type : Entity_Id;
166 Derived_Type : Entity_Id);
167 -- Subsidiary procedure to Build_Derived_Type. For numeric types, create
168 -- an anonymous base type, and propagate constraint to subtype if needed.
170 procedure Build_Derived_Private_Type
171 (N : Node_Id;
172 Parent_Type : Entity_Id;
173 Derived_Type : Entity_Id;
174 Is_Completion : Boolean;
175 Derive_Subps : Boolean := True);
176 -- Subsidiary procedure to Build_Derived_Type. This procedure is complex
177 -- because the parent may or may not have a completion, and the derivation
178 -- may itself be a completion.
180 procedure Build_Derived_Record_Type
181 (N : Node_Id;
182 Parent_Type : Entity_Id;
183 Derived_Type : Entity_Id;
184 Derive_Subps : Boolean := True);
185 -- Subsidiary procedure used for tagged and untagged record types
186 -- by Build_Derived_Type and Analyze_Private_Extension_Declaration.
187 -- All parameters are as in Build_Derived_Type except that N, in
188 -- addition to being an N_Full_Type_Declaration node, can also be an
189 -- N_Private_Extension_Declaration node. See the definition of this routine
190 -- for much more info. Derive_Subps indicates whether subprograms should be
191 -- derived from the parent type. The only case where Derive_Subps is False
192 -- is for an implicit derived full type for a type derived from a private
193 -- type (see Build_Derived_Type).
195 procedure Build_Discriminal (Discrim : Entity_Id);
196 -- Create the discriminal corresponding to discriminant Discrim, that is
197 -- the parameter corresponding to Discrim to be used in initialization
198 -- procedures for the type where Discrim is a discriminant. Discriminals
199 -- are not used during semantic analysis, and are not fully defined
200 -- entities until expansion. Thus they are not given a scope until
201 -- initialization procedures are built.
203 function Build_Discriminant_Constraints
204 (T : Entity_Id;
205 Def : Node_Id;
206 Derived_Def : Boolean := False) return Elist_Id;
207 -- Validate discriminant constraints and return the list of the constraints
208 -- in order of discriminant declarations, where T is the discriminated
209 -- unconstrained type. Def is the N_Subtype_Indication node where the
210 -- discriminants constraints for T are specified. Derived_Def is True
211 -- when building the discriminant constraints in a derived type definition
212 -- of the form "type D (...) is new T (xxx)". In this case T is the parent
213 -- type and Def is the constraint "(xxx)" on T and this routine sets the
214 -- Corresponding_Discriminant field of the discriminants in the derived
215 -- type D to point to the corresponding discriminants in the parent type T.
217 procedure Build_Discriminated_Subtype
218 (T : Entity_Id;
219 Def_Id : Entity_Id;
220 Elist : Elist_Id;
221 Related_Nod : Node_Id;
222 For_Access : Boolean := False);
223 -- Subsidiary procedure to Constrain_Discriminated_Type and to
224 -- Process_Incomplete_Dependents. Given
226 -- T (a possibly discriminated base type)
227 -- Def_Id (a very partially built subtype for T),
229 -- the call completes Def_Id to be the appropriate E_*_Subtype.
231 -- The Elist is the list of discriminant constraints if any (it is set
232 -- to No_Elist if T is not a discriminated type, and to an empty list if
233 -- T has discriminants but there are no discriminant constraints). The
234 -- Related_Nod is the same as Decl_Node in Create_Constrained_Components.
235 -- The For_Access says whether or not this subtype is really constraining
236 -- an access type. That is its sole purpose is the designated type of an
237 -- access type -- in which case a Private_Subtype Is_For_Access_Subtype
238 -- is built to avoid freezing T when the access subtype is frozen.
240 function Build_Scalar_Bound
241 (Bound : Node_Id;
242 Par_T : Entity_Id;
243 Der_T : Entity_Id) return Node_Id;
244 -- The bounds of a derived scalar type are conversions of the bounds of
245 -- the parent type. Optimize the representation if the bounds are literals.
246 -- Needs a more complete spec--what are the parameters exactly, and what
247 -- exactly is the returned value, and how is Bound affected???
249 procedure Build_Underlying_Full_View
250 (N : Node_Id;
251 Typ : Entity_Id;
252 Par : Entity_Id);
253 -- If the completion of a private type is itself derived from a private
254 -- type, or if the full view of a private subtype is itself private, the
255 -- back-end has no way to compute the actual size of this type. We build
256 -- an internal subtype declaration of the proper parent type to convey
257 -- this information. This extra mechanism is needed because a full
258 -- view cannot itself have a full view (it would get clobbered during
259 -- view exchanges).
261 procedure Check_Access_Discriminant_Requires_Limited
262 (D : Node_Id;
263 Loc : Node_Id);
264 -- Check the restriction that the type to which an access discriminant
265 -- belongs must be a concurrent type or a descendant of a type with
266 -- the reserved word 'limited' in its declaration.
268 procedure Check_Anonymous_Access_Components
269 (Typ_Decl : Node_Id;
270 Typ : Entity_Id;
271 Prev : Entity_Id;
272 Comp_List : Node_Id);
273 -- Ada 2005 AI-382: an access component in a record definition can refer to
274 -- the enclosing record, in which case it denotes the type itself, and not
275 -- the current instance of the type. We create an anonymous access type for
276 -- the component, and flag it as an access to a component, so accessibility
277 -- checks are properly performed on it. The declaration of the access type
278 -- is placed ahead of that of the record to prevent order-of-elaboration
279 -- circularity issues in Gigi. We create an incomplete type for the record
280 -- declaration, which is the designated type of the anonymous access.
282 procedure Check_Delta_Expression (E : Node_Id);
283 -- Check that the expression represented by E is suitable for use as a
284 -- delta expression, i.e. it is of real type and is static.
286 procedure Check_Digits_Expression (E : Node_Id);
287 -- Check that the expression represented by E is suitable for use as a
288 -- digits expression, i.e. it is of integer type, positive and static.
290 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id);
291 -- Validate the initialization of an object declaration. T is the required
292 -- type, and Exp is the initialization expression.
294 procedure Check_Interfaces (N : Node_Id; Def : Node_Id);
295 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
297 procedure Check_Or_Process_Discriminants
298 (N : Node_Id;
299 T : Entity_Id;
300 Prev : Entity_Id := Empty);
301 -- If N is the full declaration of the completion T of an incomplete or
302 -- private type, check its discriminants (which are already known to be
303 -- conformant with those of the partial view, see Find_Type_Name),
304 -- otherwise process them. Prev is the entity of the partial declaration,
305 -- if any.
307 procedure Check_Real_Bound (Bound : Node_Id);
308 -- Check given bound for being of real type and static. If not, post an
309 -- appropriate message, and rewrite the bound with the real literal zero.
311 procedure Constant_Redeclaration
312 (Id : Entity_Id;
313 N : Node_Id;
314 T : out Entity_Id);
315 -- Various checks on legality of full declaration of deferred constant.
316 -- Id is the entity for the redeclaration, N is the N_Object_Declaration,
317 -- node. The caller has not yet set any attributes of this entity.
319 function Contain_Interface
320 (Iface : Entity_Id;
321 Ifaces : Elist_Id) return Boolean;
322 -- Ada 2005: Determine whether Iface is present in the list Ifaces
324 procedure Convert_Scalar_Bounds
325 (N : Node_Id;
326 Parent_Type : Entity_Id;
327 Derived_Type : Entity_Id;
328 Loc : Source_Ptr);
329 -- For derived scalar types, convert the bounds in the type definition to
330 -- the derived type, and complete their analysis. Given a constraint of the
331 -- form ".. new T range Lo .. Hi", Lo and Hi are analyzed and resolved with
332 -- T'Base, the parent_type. The bounds of the derived type (the anonymous
333 -- base) are copies of Lo and Hi. Finally, the bounds of the derived
334 -- subtype are conversions of those bounds to the derived_type, so that
335 -- their typing is consistent.
337 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id);
338 -- Copies attributes from array base type T2 to array base type T1. Copies
339 -- only attributes that apply to base types, but not subtypes.
341 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id);
342 -- Copies attributes from array subtype T2 to array subtype T1. Copies
343 -- attributes that apply to both subtypes and base types.
345 procedure Create_Constrained_Components
346 (Subt : Entity_Id;
347 Decl_Node : Node_Id;
348 Typ : Entity_Id;
349 Constraints : Elist_Id);
350 -- Build the list of entities for a constrained discriminated record
351 -- subtype. If a component depends on a discriminant, replace its subtype
352 -- using the discriminant values in the discriminant constraint. Subt
353 -- is the defining identifier for the subtype whose list of constrained
354 -- entities we will create. Decl_Node is the type declaration node where
355 -- we will attach all the itypes created. Typ is the base discriminated
356 -- type for the subtype Subt. Constraints is the list of discriminant
357 -- constraints for Typ.
359 function Constrain_Component_Type
360 (Comp : Entity_Id;
361 Constrained_Typ : Entity_Id;
362 Related_Node : Node_Id;
363 Typ : Entity_Id;
364 Constraints : Elist_Id) return Entity_Id;
365 -- Given a discriminated base type Typ, a list of discriminant constraints,
366 -- Constraints, for Typ and a component Comp of Typ, create and return the
367 -- type corresponding to Etype (Comp) where all discriminant references
368 -- are replaced with the corresponding constraint. If Etype (Comp) contains
369 -- no discriminant references then it is returned as-is. Constrained_Typ
370 -- is the final constrained subtype to which the constrained component
371 -- belongs. Related_Node is the node where we attach all created itypes.
373 procedure Constrain_Access
374 (Def_Id : in out Entity_Id;
375 S : Node_Id;
376 Related_Nod : Node_Id);
377 -- Apply a list of constraints to an access type. If Def_Id is empty, it is
378 -- an anonymous type created for a subtype indication. In that case it is
379 -- created in the procedure and attached to Related_Nod.
381 procedure Constrain_Array
382 (Def_Id : in out Entity_Id;
383 SI : Node_Id;
384 Related_Nod : Node_Id;
385 Related_Id : Entity_Id;
386 Suffix : Character);
387 -- Apply a list of index constraints to an unconstrained array type. The
388 -- first parameter is the entity for the resulting subtype. A value of
389 -- Empty for Def_Id indicates that an implicit type must be created, but
390 -- creation is delayed (and must be done by this procedure) because other
391 -- subsidiary implicit types must be created first (which is why Def_Id
392 -- is an in/out parameter). The second parameter is a subtype indication
393 -- node for the constrained array to be created (e.g. something of the
394 -- form string (1 .. 10)). Related_Nod gives the place where this type
395 -- has to be inserted in the tree. The Related_Id and Suffix parameters
396 -- are used to build the associated Implicit type name.
398 procedure Constrain_Concurrent
399 (Def_Id : in out Entity_Id;
400 SI : Node_Id;
401 Related_Nod : Node_Id;
402 Related_Id : Entity_Id;
403 Suffix : Character);
404 -- Apply list of discriminant constraints to an unconstrained concurrent
405 -- type.
407 -- SI is the N_Subtype_Indication node containing the constraint and
408 -- the unconstrained type to constrain.
410 -- Def_Id is the entity for the resulting constrained subtype. A value
411 -- of Empty for Def_Id indicates that an implicit type must be created,
412 -- but creation is delayed (and must be done by this procedure) because
413 -- other subsidiary implicit types must be created first (which is why
414 -- Def_Id is an in/out parameter).
416 -- Related_Nod gives the place where this type has to be inserted
417 -- in the tree.
419 -- The last two arguments are used to create its external name if needed.
421 function Constrain_Corresponding_Record
422 (Prot_Subt : Entity_Id;
423 Corr_Rec : Entity_Id;
424 Related_Nod : Node_Id) return Entity_Id;
425 -- When constraining a protected type or task type with discriminants,
426 -- constrain the corresponding record with the same discriminant values.
428 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id);
429 -- Constrain a decimal fixed point type with a digits constraint and/or a
430 -- range constraint, and build E_Decimal_Fixed_Point_Subtype entity.
432 procedure Constrain_Discriminated_Type
433 (Def_Id : Entity_Id;
434 S : Node_Id;
435 Related_Nod : Node_Id;
436 For_Access : Boolean := False);
437 -- Process discriminant constraints of composite type. Verify that values
438 -- have been provided for all discriminants, that the original type is
439 -- unconstrained, and that the types of the supplied expressions match
440 -- the discriminant types. The first three parameters are like in routine
441 -- Constrain_Concurrent. See Build_Discriminated_Subtype for an explanation
442 -- of For_Access.
444 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id);
445 -- Constrain an enumeration type with a range constraint. This is identical
446 -- to Constrain_Integer, but for the Ekind of the resulting subtype.
448 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id);
449 -- Constrain a floating point type with either a digits constraint
450 -- and/or a range constraint, building a E_Floating_Point_Subtype.
452 procedure Constrain_Index
453 (Index : Node_Id;
454 S : Node_Id;
455 Related_Nod : Node_Id;
456 Related_Id : Entity_Id;
457 Suffix : Character;
458 Suffix_Index : Nat);
459 -- Process an index constraint S in a constrained array declaration. The
460 -- constraint can be a subtype name, or a range with or without an explicit
461 -- subtype mark. The index is the corresponding index of the unconstrained
462 -- array. The Related_Id and Suffix parameters are used to build the
463 -- associated Implicit type name.
465 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id);
466 -- Build subtype of a signed or modular integer type
468 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id);
469 -- Constrain an ordinary fixed point type with a range constraint, and
470 -- build an E_Ordinary_Fixed_Point_Subtype entity.
472 procedure Copy_And_Swap (Priv, Full : Entity_Id);
473 -- Copy the Priv entity into the entity of its full declaration then swap
474 -- the two entities in such a manner that the former private type is now
475 -- seen as a full type.
477 procedure Decimal_Fixed_Point_Type_Declaration
478 (T : Entity_Id;
479 Def : Node_Id);
480 -- Create a new decimal fixed point type, and apply the constraint to
481 -- obtain a subtype of this new type.
483 procedure Complete_Private_Subtype
484 (Priv : Entity_Id;
485 Full : Entity_Id;
486 Full_Base : Entity_Id;
487 Related_Nod : Node_Id);
488 -- Complete the implicit full view of a private subtype by setting the
489 -- appropriate semantic fields. If the full view of the parent is a record
490 -- type, build constrained components of subtype.
492 procedure Derive_Progenitor_Subprograms
493 (Parent_Type : Entity_Id;
494 Tagged_Type : Entity_Id);
495 -- Ada 2005 (AI-251): To complete type derivation, collect the primitive
496 -- operations of progenitors of Tagged_Type, and replace the subsidiary
497 -- subtypes with Tagged_Type, to build the specs of the inherited interface
498 -- primitives. The derived primitives are aliased to those of the
499 -- interface. This routine takes care also of transferring to the full view
500 -- subprograms associated with the partial view of Tagged_Type that cover
501 -- interface primitives.
503 procedure Derived_Standard_Character
504 (N : Node_Id;
505 Parent_Type : Entity_Id;
506 Derived_Type : Entity_Id);
507 -- Subsidiary procedure to Build_Derived_Enumeration_Type which handles
508 -- derivations from types Standard.Character and Standard.Wide_Character.
510 procedure Derived_Type_Declaration
511 (T : Entity_Id;
512 N : Node_Id;
513 Is_Completion : Boolean);
514 -- Process a derived type declaration. Build_Derived_Type is invoked
515 -- to process the actual derived type definition. Parameters N and
516 -- Is_Completion have the same meaning as in Build_Derived_Type.
517 -- T is the N_Defining_Identifier for the entity defined in the
518 -- N_Full_Type_Declaration node N, that is T is the derived type.
520 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id);
521 -- Insert each literal in symbol table, as an overloadable identifier. Each
522 -- enumeration type is mapped into a sequence of integers, and each literal
523 -- is defined as a constant with integer value. If any of the literals are
524 -- character literals, the type is a character type, which means that
525 -- strings are legal aggregates for arrays of components of the type.
527 function Expand_To_Stored_Constraint
528 (Typ : Entity_Id;
529 Constraint : Elist_Id) return Elist_Id;
530 -- Given a constraint (i.e. a list of expressions) on the discriminants of
531 -- Typ, expand it into a constraint on the stored discriminants and return
532 -- the new list of expressions constraining the stored discriminants.
534 function Find_Type_Of_Object
535 (Obj_Def : Node_Id;
536 Related_Nod : Node_Id) return Entity_Id;
537 -- Get type entity for object referenced by Obj_Def, attaching the implicit
538 -- types generated to Related_Nod.
540 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id);
541 -- Create a new float and apply the constraint to obtain subtype of it
543 function Has_Range_Constraint (N : Node_Id) return Boolean;
544 -- Given an N_Subtype_Indication node N, return True if a range constraint
545 -- is present, either directly, or as part of a digits or delta constraint.
546 -- In addition, a digits constraint in the decimal case returns True, since
547 -- it establishes a default range if no explicit range is present.
549 function Inherit_Components
550 (N : Node_Id;
551 Parent_Base : Entity_Id;
552 Derived_Base : Entity_Id;
553 Is_Tagged : Boolean;
554 Inherit_Discr : Boolean;
555 Discs : Elist_Id) return Elist_Id;
556 -- Called from Build_Derived_Record_Type to inherit the components of
557 -- Parent_Base (a base type) into the Derived_Base (the derived base type).
558 -- For more information on derived types and component inheritance please
559 -- consult the comment above the body of Build_Derived_Record_Type.
561 -- N is the original derived type declaration
563 -- Is_Tagged is set if we are dealing with tagged types
565 -- If Inherit_Discr is set, Derived_Base inherits its discriminants from
566 -- Parent_Base, otherwise no discriminants are inherited.
568 -- Discs gives the list of constraints that apply to Parent_Base in the
569 -- derived type declaration. If Discs is set to No_Elist, then we have
570 -- the following situation:
572 -- type Parent (D1..Dn : ..) is [tagged] record ...;
573 -- type Derived is new Parent [with ...];
575 -- which gets treated as
577 -- type Derived (D1..Dn : ..) is new Parent (D1,..,Dn) [with ...];
579 -- For untagged types the returned value is an association list. The list
580 -- starts from the association (Parent_Base => Derived_Base), and then it
581 -- contains a sequence of the associations of the form
583 -- (Old_Component => New_Component),
585 -- where Old_Component is the Entity_Id of a component in Parent_Base and
586 -- New_Component is the Entity_Id of the corresponding component in
587 -- Derived_Base. For untagged records, this association list is needed when
588 -- copying the record declaration for the derived base. In the tagged case
589 -- the value returned is irrelevant.
591 procedure Inherit_Predicate_Flags (Subt, Par : Entity_Id);
592 -- Propagate static and dynamic predicate flags from a parent to the
593 -- subtype in a subtype declaration with and without constraints.
595 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean;
596 -- Subsidiary to Check_Abstract_Overriding and Derive_Subprogram.
597 -- Determine whether subprogram Subp is a procedure subject to pragma
598 -- Extensions_Visible with value False and has at least one controlling
599 -- parameter of mode OUT.
601 function Is_Valid_Constraint_Kind
602 (T_Kind : Type_Kind;
603 Constraint_Kind : Node_Kind) return Boolean;
604 -- Returns True if it is legal to apply the given kind of constraint to the
605 -- given kind of type (index constraint to an array type, for example).
607 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id);
608 -- Create new modular type. Verify that modulus is in bounds
610 procedure New_Concatenation_Op (Typ : Entity_Id);
611 -- Create an abbreviated declaration for an operator in order to
612 -- materialize concatenation on array types.
614 procedure Ordinary_Fixed_Point_Type_Declaration
615 (T : Entity_Id;
616 Def : Node_Id);
617 -- Create a new ordinary fixed point type, and apply the constraint to
618 -- obtain subtype of it.
620 procedure Prepare_Private_Subtype_Completion
621 (Id : Entity_Id;
622 Related_Nod : Node_Id);
623 -- Id is a subtype of some private type. Creates the full declaration
624 -- associated with Id whenever possible, i.e. when the full declaration
625 -- of the base type is already known. Records each subtype into
626 -- Private_Dependents of the base type.
628 procedure Process_Incomplete_Dependents
629 (N : Node_Id;
630 Full_T : Entity_Id;
631 Inc_T : Entity_Id);
632 -- Process all entities that depend on an incomplete type. There include
633 -- subtypes, subprogram types that mention the incomplete type in their
634 -- profiles, and subprogram with access parameters that designate the
635 -- incomplete type.
637 -- Inc_T is the defining identifier of an incomplete type declaration, its
638 -- Ekind is E_Incomplete_Type.
640 -- N is the corresponding N_Full_Type_Declaration for Inc_T.
642 -- Full_T is N's defining identifier.
644 -- Subtypes of incomplete types with discriminants are completed when the
645 -- parent type is. This is simpler than private subtypes, because they can
646 -- only appear in the same scope, and there is no need to exchange views.
647 -- Similarly, access_to_subprogram types may have a parameter or a return
648 -- type that is an incomplete type, and that must be replaced with the
649 -- full type.
651 -- If the full type is tagged, subprogram with access parameters that
652 -- designated the incomplete may be primitive operations of the full type,
653 -- and have to be processed accordingly.
655 procedure Process_Real_Range_Specification (Def : Node_Id);
656 -- Given the type definition for a real type, this procedure processes and
657 -- checks the real range specification of this type definition if one is
658 -- present. If errors are found, error messages are posted, and the
659 -- Real_Range_Specification of Def is reset to Empty.
661 procedure Propagate_Default_Init_Cond_Attributes
662 (From_Typ : Entity_Id;
663 To_Typ : Entity_Id;
664 Parent_To_Derivation : Boolean := False;
665 Private_To_Full_View : Boolean := False);
666 -- Subsidiary to routines Build_Derived_Type and Process_Full_View. Inherit
667 -- all attributes related to pragma Default_Initial_Condition from From_Typ
668 -- to To_Typ. Flag Parent_To_Derivation should be set when the context is
669 -- the creation of a derived type. Flag Private_To_Full_View should be set
670 -- when processing both views of a private type.
672 procedure Record_Type_Declaration
673 (T : Entity_Id;
674 N : Node_Id;
675 Prev : Entity_Id);
676 -- Process a record type declaration (for both untagged and tagged
677 -- records). Parameters T and N are exactly like in procedure
678 -- Derived_Type_Declaration, except that no flag Is_Completion is needed
679 -- for this routine. If this is the completion of an incomplete type
680 -- declaration, Prev is the entity of the incomplete declaration, used for
681 -- cross-referencing. Otherwise Prev = T.
683 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id);
684 -- This routine is used to process the actual record type definition (both
685 -- for untagged and tagged records). Def is a record type definition node.
686 -- This procedure analyzes the components in this record type definition.
687 -- Prev_T is the entity for the enclosing record type. It is provided so
688 -- that its Has_Task flag can be set if any of the component have Has_Task
689 -- set. If the declaration is the completion of an incomplete type
690 -- declaration, Prev_T is the original incomplete type, whose full view is
691 -- the record type.
693 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id);
694 -- Subsidiary to Build_Derived_Record_Type. For untagged records, we
695 -- build a copy of the declaration tree of the parent, and we create
696 -- independently the list of components for the derived type. Semantic
697 -- information uses the component entities, but record representation
698 -- clauses are validated on the declaration tree. This procedure replaces
699 -- discriminants and components in the declaration with those that have
700 -- been created by Inherit_Components.
702 procedure Set_Fixed_Range
703 (E : Entity_Id;
704 Loc : Source_Ptr;
705 Lo : Ureal;
706 Hi : Ureal);
707 -- Build a range node with the given bounds and set it as the Scalar_Range
708 -- of the given fixed-point type entity. Loc is the source location used
709 -- for the constructed range. See body for further details.
711 procedure Set_Scalar_Range_For_Subtype
712 (Def_Id : Entity_Id;
713 R : Node_Id;
714 Subt : Entity_Id);
715 -- This routine is used to set the scalar range field for a subtype given
716 -- Def_Id, the entity for the subtype, and R, the range expression for the
717 -- scalar range. Subt provides the parent subtype to be used to analyze,
718 -- resolve, and check the given range.
720 procedure Set_Default_SSO (T : Entity_Id);
721 -- T is the entity for an array or record being declared. This procedure
722 -- sets the flags SSO_Set_Low_By_Default/SSO_Set_High_By_Default according
723 -- to the setting of Opt.Default_SSO.
725 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id);
726 -- Create a new signed integer entity, and apply the constraint to obtain
727 -- the required first named subtype of this type.
729 procedure Set_Stored_Constraint_From_Discriminant_Constraint
730 (E : Entity_Id);
731 -- E is some record type. This routine computes E's Stored_Constraint
732 -- from its Discriminant_Constraint.
734 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id);
735 -- Check that an entity in a list of progenitors is an interface,
736 -- emit error otherwise.
738 -----------------------
739 -- Access_Definition --
740 -----------------------
742 function Access_Definition
743 (Related_Nod : Node_Id;
744 N : Node_Id) return Entity_Id
746 Anon_Type : Entity_Id;
747 Anon_Scope : Entity_Id;
748 Desig_Type : Entity_Id;
749 Enclosing_Prot_Type : Entity_Id := Empty;
751 begin
752 Check_SPARK_05_Restriction ("access type is not allowed", N);
754 if Is_Entry (Current_Scope)
755 and then Is_Task_Type (Etype (Scope (Current_Scope)))
756 then
757 Error_Msg_N ("task entries cannot have access parameters", N);
758 return Empty;
759 end if;
761 -- Ada 2005: For an object declaration the corresponding anonymous
762 -- type is declared in the current scope.
764 -- If the access definition is the return type of another access to
765 -- function, scope is the current one, because it is the one of the
766 -- current type declaration, except for the pathological case below.
768 if Nkind_In (Related_Nod, N_Object_Declaration,
769 N_Access_Function_Definition)
770 then
771 Anon_Scope := Current_Scope;
773 -- A pathological case: function returning access functions that
774 -- return access functions, etc. Each anonymous access type created
775 -- is in the enclosing scope of the outermost function.
777 declare
778 Par : Node_Id;
780 begin
781 Par := Related_Nod;
782 while Nkind_In (Par, N_Access_Function_Definition,
783 N_Access_Definition)
784 loop
785 Par := Parent (Par);
786 end loop;
788 if Nkind (Par) = N_Function_Specification then
789 Anon_Scope := Scope (Defining_Entity (Par));
790 end if;
791 end;
793 -- For the anonymous function result case, retrieve the scope of the
794 -- function specification's associated entity rather than using the
795 -- current scope. The current scope will be the function itself if the
796 -- formal part is currently being analyzed, but will be the parent scope
797 -- in the case of a parameterless function, and we always want to use
798 -- the function's parent scope. Finally, if the function is a child
799 -- unit, we must traverse the tree to retrieve the proper entity.
801 elsif Nkind (Related_Nod) = N_Function_Specification
802 and then Nkind (Parent (N)) /= N_Parameter_Specification
803 then
804 -- If the current scope is a protected type, the anonymous access
805 -- is associated with one of the protected operations, and must
806 -- be available in the scope that encloses the protected declaration.
807 -- Otherwise the type is in the scope enclosing the subprogram.
809 -- If the function has formals, The return type of a subprogram
810 -- declaration is analyzed in the scope of the subprogram (see
811 -- Process_Formals) and thus the protected type, if present, is
812 -- the scope of the current function scope.
814 if Ekind (Current_Scope) = E_Protected_Type then
815 Enclosing_Prot_Type := Current_Scope;
817 elsif Ekind (Current_Scope) = E_Function
818 and then Ekind (Scope (Current_Scope)) = E_Protected_Type
819 then
820 Enclosing_Prot_Type := Scope (Current_Scope);
821 end if;
823 if Present (Enclosing_Prot_Type) then
824 Anon_Scope := Scope (Enclosing_Prot_Type);
826 else
827 Anon_Scope := Scope (Defining_Entity (Related_Nod));
828 end if;
830 -- For an access type definition, if the current scope is a child
831 -- unit it is the scope of the type.
833 elsif Is_Compilation_Unit (Current_Scope) then
834 Anon_Scope := Current_Scope;
836 -- For access formals, access components, and access discriminants, the
837 -- scope is that of the enclosing declaration,
839 else
840 Anon_Scope := Scope (Current_Scope);
841 end if;
843 Anon_Type :=
844 Create_Itype
845 (E_Anonymous_Access_Type, Related_Nod, Scope_Id => Anon_Scope);
847 if All_Present (N)
848 and then Ada_Version >= Ada_2005
849 then
850 Error_Msg_N ("ALL is not permitted for anonymous access types", N);
851 end if;
853 -- Ada 2005 (AI-254): In case of anonymous access to subprograms call
854 -- the corresponding semantic routine
856 if Present (Access_To_Subprogram_Definition (N)) then
858 -- Compiler runtime units are compiled in Ada 2005 mode when building
859 -- the runtime library but must also be compilable in Ada 95 mode
860 -- (when bootstrapping the compiler).
862 Check_Compiler_Unit ("anonymous access to subprogram", N);
864 Access_Subprogram_Declaration
865 (T_Name => Anon_Type,
866 T_Def => Access_To_Subprogram_Definition (N));
868 if Ekind (Anon_Type) = E_Access_Protected_Subprogram_Type then
869 Set_Ekind
870 (Anon_Type, E_Anonymous_Access_Protected_Subprogram_Type);
871 else
872 Set_Ekind (Anon_Type, E_Anonymous_Access_Subprogram_Type);
873 end if;
875 Set_Can_Use_Internal_Rep
876 (Anon_Type, not Always_Compatible_Rep_On_Target);
878 -- If the anonymous access is associated with a protected operation,
879 -- create a reference to it after the enclosing protected definition
880 -- because the itype will be used in the subsequent bodies.
882 -- If the anonymous access itself is protected, a full type
883 -- declaratiton will be created for it, so that the equivalent
884 -- record type can be constructed. For further details, see
885 -- Replace_Anonymous_Access_To_Protected-Subprogram.
887 if Ekind (Current_Scope) = E_Protected_Type
888 and then not Protected_Present (Access_To_Subprogram_Definition (N))
889 then
890 Build_Itype_Reference (Anon_Type, Parent (Current_Scope));
891 end if;
893 return Anon_Type;
894 end if;
896 Find_Type (Subtype_Mark (N));
897 Desig_Type := Entity (Subtype_Mark (N));
899 Set_Directly_Designated_Type (Anon_Type, Desig_Type);
900 Set_Etype (Anon_Type, Anon_Type);
902 -- Make sure the anonymous access type has size and alignment fields
903 -- set, as required by gigi. This is necessary in the case of the
904 -- Task_Body_Procedure.
906 if not Has_Private_Component (Desig_Type) then
907 Layout_Type (Anon_Type);
908 end if;
910 -- Ada 2005 (AI-231): Ada 2005 semantics for anonymous access differs
911 -- from Ada 95 semantics. In Ada 2005, anonymous access must specify if
912 -- the null value is allowed. In Ada 95 the null value is never allowed.
914 if Ada_Version >= Ada_2005 then
915 Set_Can_Never_Be_Null (Anon_Type, Null_Exclusion_Present (N));
916 else
917 Set_Can_Never_Be_Null (Anon_Type, True);
918 end if;
920 -- The anonymous access type is as public as the discriminated type or
921 -- subprogram that defines it. It is imported (for back-end purposes)
922 -- if the designated type is.
924 Set_Is_Public (Anon_Type, Is_Public (Scope (Anon_Type)));
926 -- Ada 2005 (AI-231): Propagate the access-constant attribute
928 Set_Is_Access_Constant (Anon_Type, Constant_Present (N));
930 -- The context is either a subprogram declaration, object declaration,
931 -- or an access discriminant, in a private or a full type declaration.
932 -- In the case of a subprogram, if the designated type is incomplete,
933 -- the operation will be a primitive operation of the full type, to be
934 -- updated subsequently. If the type is imported through a limited_with
935 -- clause, the subprogram is not a primitive operation of the type
936 -- (which is declared elsewhere in some other scope).
938 if Ekind (Desig_Type) = E_Incomplete_Type
939 and then not From_Limited_With (Desig_Type)
940 and then Is_Overloadable (Current_Scope)
941 then
942 Append_Elmt (Current_Scope, Private_Dependents (Desig_Type));
943 Set_Has_Delayed_Freeze (Current_Scope);
944 end if;
946 -- Ada 2005: If the designated type is an interface that may contain
947 -- tasks, create a Master entity for the declaration. This must be done
948 -- before expansion of the full declaration, because the declaration may
949 -- include an expression that is an allocator, whose expansion needs the
950 -- proper Master for the created tasks.
952 if Nkind (Related_Nod) = N_Object_Declaration and then Expander_Active
953 then
954 if Is_Interface (Desig_Type) and then Is_Limited_Record (Desig_Type)
955 then
956 Build_Class_Wide_Master (Anon_Type);
958 -- Similarly, if the type is an anonymous access that designates
959 -- tasks, create a master entity for it in the current context.
961 elsif Has_Task (Desig_Type) and then Comes_From_Source (Related_Nod)
962 then
963 Build_Master_Entity (Defining_Identifier (Related_Nod));
964 Build_Master_Renaming (Anon_Type);
965 end if;
966 end if;
968 -- For a private component of a protected type, it is imperative that
969 -- the back-end elaborate the type immediately after the protected
970 -- declaration, because this type will be used in the declarations
971 -- created for the component within each protected body, so we must
972 -- create an itype reference for it now.
974 if Nkind (Parent (Related_Nod)) = N_Protected_Definition then
975 Build_Itype_Reference (Anon_Type, Parent (Parent (Related_Nod)));
977 -- Similarly, if the access definition is the return result of a
978 -- function, create an itype reference for it because it will be used
979 -- within the function body. For a regular function that is not a
980 -- compilation unit, insert reference after the declaration. For a
981 -- protected operation, insert it after the enclosing protected type
982 -- declaration. In either case, do not create a reference for a type
983 -- obtained through a limited_with clause, because this would introduce
984 -- semantic dependencies.
986 -- Similarly, do not create a reference if the designated type is a
987 -- generic formal, because no use of it will reach the backend.
989 elsif Nkind (Related_Nod) = N_Function_Specification
990 and then not From_Limited_With (Desig_Type)
991 and then not Is_Generic_Type (Desig_Type)
992 then
993 if Present (Enclosing_Prot_Type) then
994 Build_Itype_Reference (Anon_Type, Parent (Enclosing_Prot_Type));
996 elsif Is_List_Member (Parent (Related_Nod))
997 and then Nkind (Parent (N)) /= N_Parameter_Specification
998 then
999 Build_Itype_Reference (Anon_Type, Parent (Related_Nod));
1000 end if;
1002 -- Finally, create an itype reference for an object declaration of an
1003 -- anonymous access type. This is strictly necessary only for deferred
1004 -- constants, but in any case will avoid out-of-scope problems in the
1005 -- back-end.
1007 elsif Nkind (Related_Nod) = N_Object_Declaration then
1008 Build_Itype_Reference (Anon_Type, Related_Nod);
1009 end if;
1011 return Anon_Type;
1012 end Access_Definition;
1014 -----------------------------------
1015 -- Access_Subprogram_Declaration --
1016 -----------------------------------
1018 procedure Access_Subprogram_Declaration
1019 (T_Name : Entity_Id;
1020 T_Def : Node_Id)
1022 procedure Check_For_Premature_Usage (Def : Node_Id);
1023 -- Check that type T_Name is not used, directly or recursively, as a
1024 -- parameter or a return type in Def. Def is either a subtype, an
1025 -- access_definition, or an access_to_subprogram_definition.
1027 -------------------------------
1028 -- Check_For_Premature_Usage --
1029 -------------------------------
1031 procedure Check_For_Premature_Usage (Def : Node_Id) is
1032 Param : Node_Id;
1034 begin
1035 -- Check for a subtype mark
1037 if Nkind (Def) in N_Has_Etype then
1038 if Etype (Def) = T_Name then
1039 Error_Msg_N
1040 ("type& cannot be used before end of its declaration", Def);
1041 end if;
1043 -- If this is not a subtype, then this is an access_definition
1045 elsif Nkind (Def) = N_Access_Definition then
1046 if Present (Access_To_Subprogram_Definition (Def)) then
1047 Check_For_Premature_Usage
1048 (Access_To_Subprogram_Definition (Def));
1049 else
1050 Check_For_Premature_Usage (Subtype_Mark (Def));
1051 end if;
1053 -- The only cases left are N_Access_Function_Definition and
1054 -- N_Access_Procedure_Definition.
1056 else
1057 if Present (Parameter_Specifications (Def)) then
1058 Param := First (Parameter_Specifications (Def));
1059 while Present (Param) loop
1060 Check_For_Premature_Usage (Parameter_Type (Param));
1061 Param := Next (Param);
1062 end loop;
1063 end if;
1065 if Nkind (Def) = N_Access_Function_Definition then
1066 Check_For_Premature_Usage (Result_Definition (Def));
1067 end if;
1068 end if;
1069 end Check_For_Premature_Usage;
1071 -- Local variables
1073 Formals : constant List_Id := Parameter_Specifications (T_Def);
1074 Formal : Entity_Id;
1075 D_Ityp : Node_Id;
1076 Desig_Type : constant Entity_Id :=
1077 Create_Itype (E_Subprogram_Type, Parent (T_Def));
1079 -- Start of processing for Access_Subprogram_Declaration
1081 begin
1082 Check_SPARK_05_Restriction ("access type is not allowed", T_Def);
1084 -- Associate the Itype node with the inner full-type declaration or
1085 -- subprogram spec or entry body. This is required to handle nested
1086 -- anonymous declarations. For example:
1088 -- procedure P
1089 -- (X : access procedure
1090 -- (Y : access procedure
1091 -- (Z : access T)))
1093 D_Ityp := Associated_Node_For_Itype (Desig_Type);
1094 while not (Nkind_In (D_Ityp, N_Full_Type_Declaration,
1095 N_Private_Type_Declaration,
1096 N_Private_Extension_Declaration,
1097 N_Procedure_Specification,
1098 N_Function_Specification,
1099 N_Entry_Body)
1101 or else
1102 Nkind_In (D_Ityp, N_Object_Declaration,
1103 N_Object_Renaming_Declaration,
1104 N_Formal_Object_Declaration,
1105 N_Formal_Type_Declaration,
1106 N_Task_Type_Declaration,
1107 N_Protected_Type_Declaration))
1108 loop
1109 D_Ityp := Parent (D_Ityp);
1110 pragma Assert (D_Ityp /= Empty);
1111 end loop;
1113 Set_Associated_Node_For_Itype (Desig_Type, D_Ityp);
1115 if Nkind_In (D_Ityp, N_Procedure_Specification,
1116 N_Function_Specification)
1117 then
1118 Set_Scope (Desig_Type, Scope (Defining_Entity (D_Ityp)));
1120 elsif Nkind_In (D_Ityp, N_Full_Type_Declaration,
1121 N_Object_Declaration,
1122 N_Object_Renaming_Declaration,
1123 N_Formal_Type_Declaration)
1124 then
1125 Set_Scope (Desig_Type, Scope (Defining_Identifier (D_Ityp)));
1126 end if;
1128 if Nkind (T_Def) = N_Access_Function_Definition then
1129 if Nkind (Result_Definition (T_Def)) = N_Access_Definition then
1130 declare
1131 Acc : constant Node_Id := Result_Definition (T_Def);
1133 begin
1134 if Present (Access_To_Subprogram_Definition (Acc))
1135 and then
1136 Protected_Present (Access_To_Subprogram_Definition (Acc))
1137 then
1138 Set_Etype
1139 (Desig_Type,
1140 Replace_Anonymous_Access_To_Protected_Subprogram
1141 (T_Def));
1143 else
1144 Set_Etype
1145 (Desig_Type,
1146 Access_Definition (T_Def, Result_Definition (T_Def)));
1147 end if;
1148 end;
1150 else
1151 Analyze (Result_Definition (T_Def));
1153 declare
1154 Typ : constant Entity_Id := Entity (Result_Definition (T_Def));
1156 begin
1157 -- If a null exclusion is imposed on the result type, then
1158 -- create a null-excluding itype (an access subtype) and use
1159 -- it as the function's Etype.
1161 if Is_Access_Type (Typ)
1162 and then Null_Exclusion_In_Return_Present (T_Def)
1163 then
1164 Set_Etype (Desig_Type,
1165 Create_Null_Excluding_Itype
1166 (T => Typ,
1167 Related_Nod => T_Def,
1168 Scope_Id => Current_Scope));
1170 else
1171 if From_Limited_With (Typ) then
1173 -- AI05-151: Incomplete types are allowed in all basic
1174 -- declarations, including access to subprograms.
1176 if Ada_Version >= Ada_2012 then
1177 null;
1179 else
1180 Error_Msg_NE
1181 ("illegal use of incomplete type&",
1182 Result_Definition (T_Def), Typ);
1183 end if;
1185 elsif Ekind (Current_Scope) = E_Package
1186 and then In_Private_Part (Current_Scope)
1187 then
1188 if Ekind (Typ) = E_Incomplete_Type then
1189 Append_Elmt (Desig_Type, Private_Dependents (Typ));
1191 elsif Is_Class_Wide_Type (Typ)
1192 and then Ekind (Etype (Typ)) = E_Incomplete_Type
1193 then
1194 Append_Elmt
1195 (Desig_Type, Private_Dependents (Etype (Typ)));
1196 end if;
1197 end if;
1199 Set_Etype (Desig_Type, Typ);
1200 end if;
1201 end;
1202 end if;
1204 if not (Is_Type (Etype (Desig_Type))) then
1205 Error_Msg_N
1206 ("expect type in function specification",
1207 Result_Definition (T_Def));
1208 end if;
1210 else
1211 Set_Etype (Desig_Type, Standard_Void_Type);
1212 end if;
1214 if Present (Formals) then
1215 Push_Scope (Desig_Type);
1217 -- Some special tests here. These special tests can be removed
1218 -- if and when Itypes always have proper parent pointers to their
1219 -- declarations???
1221 -- Special test 1) Link defining_identifier of formals. Required by
1222 -- First_Formal to provide its functionality.
1224 declare
1225 F : Node_Id;
1227 begin
1228 F := First (Formals);
1230 -- In ASIS mode, the access_to_subprogram may be analyzed twice,
1231 -- when it is part of an unconstrained type and subtype expansion
1232 -- is disabled. To avoid back-end problems with shared profiles,
1233 -- use previous subprogram type as the designated type, and then
1234 -- remove scope added above.
1236 if ASIS_Mode and then Present (Scope (Defining_Identifier (F)))
1237 then
1238 Set_Etype (T_Name, T_Name);
1239 Init_Size_Align (T_Name);
1240 Set_Directly_Designated_Type (T_Name,
1241 Scope (Defining_Identifier (F)));
1242 End_Scope;
1243 return;
1244 end if;
1246 while Present (F) loop
1247 if No (Parent (Defining_Identifier (F))) then
1248 Set_Parent (Defining_Identifier (F), F);
1249 end if;
1251 Next (F);
1252 end loop;
1253 end;
1255 Process_Formals (Formals, Parent (T_Def));
1257 -- Special test 2) End_Scope requires that the parent pointer be set
1258 -- to something reasonable, but Itypes don't have parent pointers. So
1259 -- we set it and then unset it ???
1261 Set_Parent (Desig_Type, T_Name);
1262 End_Scope;
1263 Set_Parent (Desig_Type, Empty);
1264 end if;
1266 -- Check for premature usage of the type being defined
1268 Check_For_Premature_Usage (T_Def);
1270 -- The return type and/or any parameter type may be incomplete. Mark the
1271 -- subprogram_type as depending on the incomplete type, so that it can
1272 -- be updated when the full type declaration is seen. This only applies
1273 -- to incomplete types declared in some enclosing scope, not to limited
1274 -- views from other packages.
1276 -- Prior to Ada 2012, access to functions can only have in_parameters.
1278 if Present (Formals) then
1279 Formal := First_Formal (Desig_Type);
1280 while Present (Formal) loop
1281 if Ekind (Formal) /= E_In_Parameter
1282 and then Nkind (T_Def) = N_Access_Function_Definition
1283 and then Ada_Version < Ada_2012
1284 then
1285 Error_Msg_N ("functions can only have IN parameters", Formal);
1286 end if;
1288 if Ekind (Etype (Formal)) = E_Incomplete_Type
1289 and then In_Open_Scopes (Scope (Etype (Formal)))
1290 then
1291 Append_Elmt (Desig_Type, Private_Dependents (Etype (Formal)));
1292 Set_Has_Delayed_Freeze (Desig_Type);
1293 end if;
1295 Next_Formal (Formal);
1296 end loop;
1297 end if;
1299 -- Check whether an indirect call without actuals may be possible. This
1300 -- is used when resolving calls whose result is then indexed.
1302 May_Need_Actuals (Desig_Type);
1304 -- If the return type is incomplete, this is legal as long as the type
1305 -- is declared in the current scope and will be completed in it (rather
1306 -- than being part of limited view).
1308 if Ekind (Etype (Desig_Type)) = E_Incomplete_Type
1309 and then not Has_Delayed_Freeze (Desig_Type)
1310 and then In_Open_Scopes (Scope (Etype (Desig_Type)))
1311 then
1312 Append_Elmt (Desig_Type, Private_Dependents (Etype (Desig_Type)));
1313 Set_Has_Delayed_Freeze (Desig_Type);
1314 end if;
1316 Check_Delayed_Subprogram (Desig_Type);
1318 if Protected_Present (T_Def) then
1319 Set_Ekind (T_Name, E_Access_Protected_Subprogram_Type);
1320 Set_Convention (Desig_Type, Convention_Protected);
1321 else
1322 Set_Ekind (T_Name, E_Access_Subprogram_Type);
1323 end if;
1325 Set_Can_Use_Internal_Rep (T_Name, not Always_Compatible_Rep_On_Target);
1327 Set_Etype (T_Name, T_Name);
1328 Init_Size_Align (T_Name);
1329 Set_Directly_Designated_Type (T_Name, Desig_Type);
1331 Generate_Reference_To_Formals (T_Name);
1333 -- Ada 2005 (AI-231): Propagate the null-excluding attribute
1335 Set_Can_Never_Be_Null (T_Name, Null_Exclusion_Present (T_Def));
1337 Check_Restriction (No_Access_Subprograms, T_Def);
1338 end Access_Subprogram_Declaration;
1340 ----------------------------
1341 -- Access_Type_Declaration --
1342 ----------------------------
1344 procedure Access_Type_Declaration (T : Entity_Id; Def : Node_Id) is
1345 P : constant Node_Id := Parent (Def);
1346 S : constant Node_Id := Subtype_Indication (Def);
1348 Full_Desig : Entity_Id;
1350 begin
1351 Check_SPARK_05_Restriction ("access type is not allowed", Def);
1353 -- Check for permissible use of incomplete type
1355 if Nkind (S) /= N_Subtype_Indication then
1356 Analyze (S);
1358 if Ekind (Root_Type (Entity (S))) = E_Incomplete_Type then
1359 Set_Directly_Designated_Type (T, Entity (S));
1361 -- If the designated type is a limited view, we cannot tell if
1362 -- the full view contains tasks, and there is no way to handle
1363 -- that full view in a client. We create a master entity for the
1364 -- scope, which will be used when a client determines that one
1365 -- is needed.
1367 if From_Limited_With (Entity (S))
1368 and then not Is_Class_Wide_Type (Entity (S))
1369 then
1370 Set_Ekind (T, E_Access_Type);
1371 Build_Master_Entity (T);
1372 Build_Master_Renaming (T);
1373 end if;
1375 else
1376 Set_Directly_Designated_Type (T, Process_Subtype (S, P, T, 'P'));
1377 end if;
1379 -- If the access definition is of the form: ACCESS NOT NULL ..
1380 -- the subtype indication must be of an access type. Create
1381 -- a null-excluding subtype of it.
1383 if Null_Excluding_Subtype (Def) then
1384 if not Is_Access_Type (Entity (S)) then
1385 Error_Msg_N ("null exclusion must apply to access type", Def);
1387 else
1388 declare
1389 Loc : constant Source_Ptr := Sloc (S);
1390 Decl : Node_Id;
1391 Nam : constant Entity_Id := Make_Temporary (Loc, 'S');
1393 begin
1394 Decl :=
1395 Make_Subtype_Declaration (Loc,
1396 Defining_Identifier => Nam,
1397 Subtype_Indication =>
1398 New_Occurrence_Of (Entity (S), Loc));
1399 Set_Null_Exclusion_Present (Decl);
1400 Insert_Before (Parent (Def), Decl);
1401 Analyze (Decl);
1402 Set_Entity (S, Nam);
1403 end;
1404 end if;
1405 end if;
1407 else
1408 Set_Directly_Designated_Type (T,
1409 Process_Subtype (S, P, T, 'P'));
1410 end if;
1412 if All_Present (Def) or Constant_Present (Def) then
1413 Set_Ekind (T, E_General_Access_Type);
1414 else
1415 Set_Ekind (T, E_Access_Type);
1416 end if;
1418 Full_Desig := Designated_Type (T);
1420 if Base_Type (Full_Desig) = T then
1421 Error_Msg_N ("access type cannot designate itself", S);
1423 -- In Ada 2005, the type may have a limited view through some unit in
1424 -- its own context, allowing the following circularity that cannot be
1425 -- detected earlier.
1427 elsif Is_Class_Wide_Type (Full_Desig) and then Etype (Full_Desig) = T
1428 then
1429 Error_Msg_N
1430 ("access type cannot designate its own classwide type", S);
1432 -- Clean up indication of tagged status to prevent cascaded errors
1434 Set_Is_Tagged_Type (T, False);
1435 end if;
1437 Set_Etype (T, T);
1439 -- If the type has appeared already in a with_type clause, it is frozen
1440 -- and the pointer size is already set. Else, initialize.
1442 if not From_Limited_With (T) then
1443 Init_Size_Align (T);
1444 end if;
1446 -- Note that Has_Task is always false, since the access type itself
1447 -- is not a task type. See Einfo for more description on this point.
1448 -- Exactly the same consideration applies to Has_Controlled_Component
1449 -- and to Has_Protected.
1451 Set_Has_Task (T, False);
1452 Set_Has_Controlled_Component (T, False);
1453 Set_Has_Protected (T, False);
1455 -- Initialize field Finalization_Master explicitly to Empty, to avoid
1456 -- problems where an incomplete view of this entity has been previously
1457 -- established by a limited with and an overlaid version of this field
1458 -- (Stored_Constraint) was initialized for the incomplete view.
1460 -- This reset is performed in most cases except where the access type
1461 -- has been created for the purposes of allocating or deallocating a
1462 -- build-in-place object. Such access types have explicitly set pools
1463 -- and finalization masters.
1465 if No (Associated_Storage_Pool (T)) then
1466 Set_Finalization_Master (T, Empty);
1467 end if;
1469 -- Ada 2005 (AI-231): Propagate the null-excluding and access-constant
1470 -- attributes
1472 Set_Can_Never_Be_Null (T, Null_Exclusion_Present (Def));
1473 Set_Is_Access_Constant (T, Constant_Present (Def));
1474 end Access_Type_Declaration;
1476 ----------------------------------
1477 -- Add_Interface_Tag_Components --
1478 ----------------------------------
1480 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id) is
1481 Loc : constant Source_Ptr := Sloc (N);
1482 L : List_Id;
1483 Last_Tag : Node_Id;
1485 procedure Add_Tag (Iface : Entity_Id);
1486 -- Add tag for one of the progenitor interfaces
1488 -------------
1489 -- Add_Tag --
1490 -------------
1492 procedure Add_Tag (Iface : Entity_Id) is
1493 Decl : Node_Id;
1494 Def : Node_Id;
1495 Tag : Entity_Id;
1496 Offset : Entity_Id;
1498 begin
1499 pragma Assert (Is_Tagged_Type (Iface) and then Is_Interface (Iface));
1501 -- This is a reasonable place to propagate predicates
1503 if Has_Predicates (Iface) then
1504 Set_Has_Predicates (Typ);
1505 end if;
1507 Def :=
1508 Make_Component_Definition (Loc,
1509 Aliased_Present => True,
1510 Subtype_Indication =>
1511 New_Occurrence_Of (RTE (RE_Interface_Tag), Loc));
1513 Tag := Make_Temporary (Loc, 'V');
1515 Decl :=
1516 Make_Component_Declaration (Loc,
1517 Defining_Identifier => Tag,
1518 Component_Definition => Def);
1520 Analyze_Component_Declaration (Decl);
1522 Set_Analyzed (Decl);
1523 Set_Ekind (Tag, E_Component);
1524 Set_Is_Tag (Tag);
1525 Set_Is_Aliased (Tag);
1526 Set_Related_Type (Tag, Iface);
1527 Init_Component_Location (Tag);
1529 pragma Assert (Is_Frozen (Iface));
1531 Set_DT_Entry_Count (Tag,
1532 DT_Entry_Count (First_Entity (Iface)));
1534 if No (Last_Tag) then
1535 Prepend (Decl, L);
1536 else
1537 Insert_After (Last_Tag, Decl);
1538 end if;
1540 Last_Tag := Decl;
1542 -- If the ancestor has discriminants we need to give special support
1543 -- to store the offset_to_top value of the secondary dispatch tables.
1544 -- For this purpose we add a supplementary component just after the
1545 -- field that contains the tag associated with each secondary DT.
1547 if Typ /= Etype (Typ) and then Has_Discriminants (Etype (Typ)) then
1548 Def :=
1549 Make_Component_Definition (Loc,
1550 Subtype_Indication =>
1551 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
1553 Offset := Make_Temporary (Loc, 'V');
1555 Decl :=
1556 Make_Component_Declaration (Loc,
1557 Defining_Identifier => Offset,
1558 Component_Definition => Def);
1560 Analyze_Component_Declaration (Decl);
1562 Set_Analyzed (Decl);
1563 Set_Ekind (Offset, E_Component);
1564 Set_Is_Aliased (Offset);
1565 Set_Related_Type (Offset, Iface);
1566 Init_Component_Location (Offset);
1567 Insert_After (Last_Tag, Decl);
1568 Last_Tag := Decl;
1569 end if;
1570 end Add_Tag;
1572 -- Local variables
1574 Elmt : Elmt_Id;
1575 Ext : Node_Id;
1576 Comp : Node_Id;
1578 -- Start of processing for Add_Interface_Tag_Components
1580 begin
1581 if not RTE_Available (RE_Interface_Tag) then
1582 Error_Msg
1583 ("(Ada 2005) interface types not supported by this run-time!",
1584 Sloc (N));
1585 return;
1586 end if;
1588 if Ekind (Typ) /= E_Record_Type
1589 or else (Is_Concurrent_Record_Type (Typ)
1590 and then Is_Empty_List (Abstract_Interface_List (Typ)))
1591 or else (not Is_Concurrent_Record_Type (Typ)
1592 and then No (Interfaces (Typ))
1593 and then Is_Empty_Elmt_List (Interfaces (Typ)))
1594 then
1595 return;
1596 end if;
1598 -- Find the current last tag
1600 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1601 Ext := Record_Extension_Part (Type_Definition (N));
1602 else
1603 pragma Assert (Nkind (Type_Definition (N)) = N_Record_Definition);
1604 Ext := Type_Definition (N);
1605 end if;
1607 Last_Tag := Empty;
1609 if not (Present (Component_List (Ext))) then
1610 Set_Null_Present (Ext, False);
1611 L := New_List;
1612 Set_Component_List (Ext,
1613 Make_Component_List (Loc,
1614 Component_Items => L,
1615 Null_Present => False));
1616 else
1617 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1618 L := Component_Items
1619 (Component_List
1620 (Record_Extension_Part
1621 (Type_Definition (N))));
1622 else
1623 L := Component_Items
1624 (Component_List
1625 (Type_Definition (N)));
1626 end if;
1628 -- Find the last tag component
1630 Comp := First (L);
1631 while Present (Comp) loop
1632 if Nkind (Comp) = N_Component_Declaration
1633 and then Is_Tag (Defining_Identifier (Comp))
1634 then
1635 Last_Tag := Comp;
1636 end if;
1638 Next (Comp);
1639 end loop;
1640 end if;
1642 -- At this point L references the list of components and Last_Tag
1643 -- references the current last tag (if any). Now we add the tag
1644 -- corresponding with all the interfaces that are not implemented
1645 -- by the parent.
1647 if Present (Interfaces (Typ)) then
1648 Elmt := First_Elmt (Interfaces (Typ));
1649 while Present (Elmt) loop
1650 Add_Tag (Node (Elmt));
1651 Next_Elmt (Elmt);
1652 end loop;
1653 end if;
1654 end Add_Interface_Tag_Components;
1656 -------------------------------------
1657 -- Add_Internal_Interface_Entities --
1658 -------------------------------------
1660 procedure Add_Internal_Interface_Entities (Tagged_Type : Entity_Id) is
1661 Elmt : Elmt_Id;
1662 Iface : Entity_Id;
1663 Iface_Elmt : Elmt_Id;
1664 Iface_Prim : Entity_Id;
1665 Ifaces_List : Elist_Id;
1666 New_Subp : Entity_Id := Empty;
1667 Prim : Entity_Id;
1668 Restore_Scope : Boolean := False;
1670 begin
1671 pragma Assert (Ada_Version >= Ada_2005
1672 and then Is_Record_Type (Tagged_Type)
1673 and then Is_Tagged_Type (Tagged_Type)
1674 and then Has_Interfaces (Tagged_Type)
1675 and then not Is_Interface (Tagged_Type));
1677 -- Ensure that the internal entities are added to the scope of the type
1679 if Scope (Tagged_Type) /= Current_Scope then
1680 Push_Scope (Scope (Tagged_Type));
1681 Restore_Scope := True;
1682 end if;
1684 Collect_Interfaces (Tagged_Type, Ifaces_List);
1686 Iface_Elmt := First_Elmt (Ifaces_List);
1687 while Present (Iface_Elmt) loop
1688 Iface := Node (Iface_Elmt);
1690 -- Originally we excluded here from this processing interfaces that
1691 -- are parents of Tagged_Type because their primitives are located
1692 -- in the primary dispatch table (and hence no auxiliary internal
1693 -- entities are required to handle secondary dispatch tables in such
1694 -- case). However, these auxiliary entities are also required to
1695 -- handle derivations of interfaces in formals of generics (see
1696 -- Derive_Subprograms).
1698 Elmt := First_Elmt (Primitive_Operations (Iface));
1699 while Present (Elmt) loop
1700 Iface_Prim := Node (Elmt);
1702 if not Is_Predefined_Dispatching_Operation (Iface_Prim) then
1703 Prim :=
1704 Find_Primitive_Covering_Interface
1705 (Tagged_Type => Tagged_Type,
1706 Iface_Prim => Iface_Prim);
1708 if No (Prim) and then Serious_Errors_Detected > 0 then
1709 goto Continue;
1710 end if;
1712 pragma Assert (Present (Prim));
1714 -- Ada 2012 (AI05-0197): If the name of the covering primitive
1715 -- differs from the name of the interface primitive then it is
1716 -- a private primitive inherited from a parent type. In such
1717 -- case, given that Tagged_Type covers the interface, the
1718 -- inherited private primitive becomes visible. For such
1719 -- purpose we add a new entity that renames the inherited
1720 -- private primitive.
1722 if Chars (Prim) /= Chars (Iface_Prim) then
1723 pragma Assert (Has_Suffix (Prim, 'P'));
1724 Derive_Subprogram
1725 (New_Subp => New_Subp,
1726 Parent_Subp => Iface_Prim,
1727 Derived_Type => Tagged_Type,
1728 Parent_Type => Iface);
1729 Set_Alias (New_Subp, Prim);
1730 Set_Is_Abstract_Subprogram
1731 (New_Subp, Is_Abstract_Subprogram (Prim));
1732 end if;
1734 Derive_Subprogram
1735 (New_Subp => New_Subp,
1736 Parent_Subp => Iface_Prim,
1737 Derived_Type => Tagged_Type,
1738 Parent_Type => Iface);
1740 -- Ada 2005 (AI-251): Decorate internal entity Iface_Subp
1741 -- associated with interface types. These entities are
1742 -- only registered in the list of primitives of its
1743 -- corresponding tagged type because they are only used
1744 -- to fill the contents of the secondary dispatch tables.
1745 -- Therefore they are removed from the homonym chains.
1747 Set_Is_Hidden (New_Subp);
1748 Set_Is_Internal (New_Subp);
1749 Set_Alias (New_Subp, Prim);
1750 Set_Is_Abstract_Subprogram
1751 (New_Subp, Is_Abstract_Subprogram (Prim));
1752 Set_Interface_Alias (New_Subp, Iface_Prim);
1754 -- If the returned type is an interface then propagate it to
1755 -- the returned type. Needed by the thunk to generate the code
1756 -- which displaces "this" to reference the corresponding
1757 -- secondary dispatch table in the returned object.
1759 if Is_Interface (Etype (Iface_Prim)) then
1760 Set_Etype (New_Subp, Etype (Iface_Prim));
1761 end if;
1763 -- Internal entities associated with interface types are only
1764 -- registered in the list of primitives of the tagged type.
1765 -- They are only used to fill the contents of the secondary
1766 -- dispatch tables. Therefore they are not needed in the
1767 -- homonym chains.
1769 Remove_Homonym (New_Subp);
1771 -- Hidden entities associated with interfaces must have set
1772 -- the Has_Delay_Freeze attribute to ensure that, in case
1773 -- of locally defined tagged types (or compiling with static
1774 -- dispatch tables generation disabled) the corresponding
1775 -- entry of the secondary dispatch table is filled when such
1776 -- an entity is frozen. This is an expansion activity that must
1777 -- be suppressed for ASIS because it leads to gigi elaboration
1778 -- issues in annotate mode.
1780 if not ASIS_Mode then
1781 Set_Has_Delayed_Freeze (New_Subp);
1782 end if;
1783 end if;
1785 <<Continue>>
1786 Next_Elmt (Elmt);
1787 end loop;
1789 Next_Elmt (Iface_Elmt);
1790 end loop;
1792 if Restore_Scope then
1793 Pop_Scope;
1794 end if;
1795 end Add_Internal_Interface_Entities;
1797 -----------------------------------
1798 -- Analyze_Component_Declaration --
1799 -----------------------------------
1801 procedure Analyze_Component_Declaration (N : Node_Id) is
1802 Loc : constant Source_Ptr := Sloc (Component_Definition (N));
1803 Id : constant Entity_Id := Defining_Identifier (N);
1804 E : constant Node_Id := Expression (N);
1805 Typ : constant Node_Id :=
1806 Subtype_Indication (Component_Definition (N));
1807 T : Entity_Id;
1808 P : Entity_Id;
1810 function Contains_POC (Constr : Node_Id) return Boolean;
1811 -- Determines whether a constraint uses the discriminant of a record
1812 -- type thus becoming a per-object constraint (POC).
1814 function Is_Known_Limited (Typ : Entity_Id) return Boolean;
1815 -- Typ is the type of the current component, check whether this type is
1816 -- a limited type. Used to validate declaration against that of
1817 -- enclosing record.
1819 ------------------
1820 -- Contains_POC --
1821 ------------------
1823 function Contains_POC (Constr : Node_Id) return Boolean is
1824 begin
1825 -- Prevent cascaded errors
1827 if Error_Posted (Constr) then
1828 return False;
1829 end if;
1831 case Nkind (Constr) is
1832 when N_Attribute_Reference =>
1833 return Attribute_Name (Constr) = Name_Access
1834 and then Prefix (Constr) = Scope (Entity (Prefix (Constr)));
1836 when N_Discriminant_Association =>
1837 return Denotes_Discriminant (Expression (Constr));
1839 when N_Identifier =>
1840 return Denotes_Discriminant (Constr);
1842 when N_Index_Or_Discriminant_Constraint =>
1843 declare
1844 IDC : Node_Id;
1846 begin
1847 IDC := First (Constraints (Constr));
1848 while Present (IDC) loop
1850 -- One per-object constraint is sufficient
1852 if Contains_POC (IDC) then
1853 return True;
1854 end if;
1856 Next (IDC);
1857 end loop;
1859 return False;
1860 end;
1862 when N_Range =>
1863 return Denotes_Discriminant (Low_Bound (Constr))
1864 or else
1865 Denotes_Discriminant (High_Bound (Constr));
1867 when N_Range_Constraint =>
1868 return Denotes_Discriminant (Range_Expression (Constr));
1870 when others =>
1871 return False;
1873 end case;
1874 end Contains_POC;
1876 ----------------------
1877 -- Is_Known_Limited --
1878 ----------------------
1880 function Is_Known_Limited (Typ : Entity_Id) return Boolean is
1881 P : constant Entity_Id := Etype (Typ);
1882 R : constant Entity_Id := Root_Type (Typ);
1884 begin
1885 if Is_Limited_Record (Typ) then
1886 return True;
1888 -- If the root type is limited (and not a limited interface)
1889 -- so is the current type
1891 elsif Is_Limited_Record (R)
1892 and then (not Is_Interface (R) or else not Is_Limited_Interface (R))
1893 then
1894 return True;
1896 -- Else the type may have a limited interface progenitor, but a
1897 -- limited record parent.
1899 elsif R /= P and then Is_Limited_Record (P) then
1900 return True;
1902 else
1903 return False;
1904 end if;
1905 end Is_Known_Limited;
1907 -- Start of processing for Analyze_Component_Declaration
1909 begin
1910 Generate_Definition (Id);
1911 Enter_Name (Id);
1913 if Present (Typ) then
1914 T := Find_Type_Of_Object
1915 (Subtype_Indication (Component_Definition (N)), N);
1917 if not Nkind_In (Typ, N_Identifier, N_Expanded_Name) then
1918 Check_SPARK_05_Restriction ("subtype mark required", Typ);
1919 end if;
1921 -- Ada 2005 (AI-230): Access Definition case
1923 else
1924 pragma Assert (Present
1925 (Access_Definition (Component_Definition (N))));
1927 T := Access_Definition
1928 (Related_Nod => N,
1929 N => Access_Definition (Component_Definition (N)));
1930 Set_Is_Local_Anonymous_Access (T);
1932 -- Ada 2005 (AI-254)
1934 if Present (Access_To_Subprogram_Definition
1935 (Access_Definition (Component_Definition (N))))
1936 and then Protected_Present (Access_To_Subprogram_Definition
1937 (Access_Definition
1938 (Component_Definition (N))))
1939 then
1940 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1941 end if;
1942 end if;
1944 -- If the subtype is a constrained subtype of the enclosing record,
1945 -- (which must have a partial view) the back-end does not properly
1946 -- handle the recursion. Rewrite the component declaration with an
1947 -- explicit subtype indication, which is acceptable to Gigi. We can copy
1948 -- the tree directly because side effects have already been removed from
1949 -- discriminant constraints.
1951 if Ekind (T) = E_Access_Subtype
1952 and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
1953 and then Comes_From_Source (T)
1954 and then Nkind (Parent (T)) = N_Subtype_Declaration
1955 and then Etype (Directly_Designated_Type (T)) = Current_Scope
1956 then
1957 Rewrite
1958 (Subtype_Indication (Component_Definition (N)),
1959 New_Copy_Tree (Subtype_Indication (Parent (T))));
1960 T := Find_Type_Of_Object
1961 (Subtype_Indication (Component_Definition (N)), N);
1962 end if;
1964 -- If the component declaration includes a default expression, then we
1965 -- check that the component is not of a limited type (RM 3.7(5)),
1966 -- and do the special preanalysis of the expression (see section on
1967 -- "Handling of Default and Per-Object Expressions" in the spec of
1968 -- package Sem).
1970 if Present (E) then
1971 Check_SPARK_05_Restriction ("default expression is not allowed", E);
1972 Preanalyze_Default_Expression (E, T);
1973 Check_Initialization (T, E);
1975 if Ada_Version >= Ada_2005
1976 and then Ekind (T) = E_Anonymous_Access_Type
1977 and then Etype (E) /= Any_Type
1978 then
1979 -- Check RM 3.9.2(9): "if the expected type for an expression is
1980 -- an anonymous access-to-specific tagged type, then the object
1981 -- designated by the expression shall not be dynamically tagged
1982 -- unless it is a controlling operand in a call on a dispatching
1983 -- operation"
1985 if Is_Tagged_Type (Directly_Designated_Type (T))
1986 and then
1987 Ekind (Directly_Designated_Type (T)) /= E_Class_Wide_Type
1988 and then
1989 Ekind (Directly_Designated_Type (Etype (E))) =
1990 E_Class_Wide_Type
1991 then
1992 Error_Msg_N
1993 ("access to specific tagged type required (RM 3.9.2(9))", E);
1994 end if;
1996 -- (Ada 2005: AI-230): Accessibility check for anonymous
1997 -- components
1999 if Type_Access_Level (Etype (E)) >
2000 Deepest_Type_Access_Level (T)
2001 then
2002 Error_Msg_N
2003 ("expression has deeper access level than component " &
2004 "(RM 3.10.2 (12.2))", E);
2005 end if;
2007 -- The initialization expression is a reference to an access
2008 -- discriminant. The type of the discriminant is always deeper
2009 -- than any access type.
2011 if Ekind (Etype (E)) = E_Anonymous_Access_Type
2012 and then Is_Entity_Name (E)
2013 and then Ekind (Entity (E)) = E_In_Parameter
2014 and then Present (Discriminal_Link (Entity (E)))
2015 then
2016 Error_Msg_N
2017 ("discriminant has deeper accessibility level than target",
2019 end if;
2020 end if;
2021 end if;
2023 -- The parent type may be a private view with unknown discriminants,
2024 -- and thus unconstrained. Regular components must be constrained.
2026 if Is_Indefinite_Subtype (T) and then Chars (Id) /= Name_uParent then
2027 if Is_Class_Wide_Type (T) then
2028 Error_Msg_N
2029 ("class-wide subtype with unknown discriminants" &
2030 " in component declaration",
2031 Subtype_Indication (Component_Definition (N)));
2032 else
2033 Error_Msg_N
2034 ("unconstrained subtype in component declaration",
2035 Subtype_Indication (Component_Definition (N)));
2036 end if;
2038 -- Components cannot be abstract, except for the special case of
2039 -- the _Parent field (case of extending an abstract tagged type)
2041 elsif Is_Abstract_Type (T) and then Chars (Id) /= Name_uParent then
2042 Error_Msg_N ("type of a component cannot be abstract", N);
2043 end if;
2045 Set_Etype (Id, T);
2046 Set_Is_Aliased (Id, Aliased_Present (Component_Definition (N)));
2048 -- The component declaration may have a per-object constraint, set
2049 -- the appropriate flag in the defining identifier of the subtype.
2051 if Present (Subtype_Indication (Component_Definition (N))) then
2052 declare
2053 Sindic : constant Node_Id :=
2054 Subtype_Indication (Component_Definition (N));
2055 begin
2056 if Nkind (Sindic) = N_Subtype_Indication
2057 and then Present (Constraint (Sindic))
2058 and then Contains_POC (Constraint (Sindic))
2059 then
2060 Set_Has_Per_Object_Constraint (Id);
2061 end if;
2062 end;
2063 end if;
2065 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
2066 -- out some static checks.
2068 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (T) then
2069 Null_Exclusion_Static_Checks (N);
2070 end if;
2072 -- If this component is private (or depends on a private type), flag the
2073 -- record type to indicate that some operations are not available.
2075 P := Private_Component (T);
2077 if Present (P) then
2079 -- Check for circular definitions
2081 if P = Any_Type then
2082 Set_Etype (Id, Any_Type);
2084 -- There is a gap in the visibility of operations only if the
2085 -- component type is not defined in the scope of the record type.
2087 elsif Scope (P) = Scope (Current_Scope) then
2088 null;
2090 elsif Is_Limited_Type (P) then
2091 Set_Is_Limited_Composite (Current_Scope);
2093 else
2094 Set_Is_Private_Composite (Current_Scope);
2095 end if;
2096 end if;
2098 if P /= Any_Type
2099 and then Is_Limited_Type (T)
2100 and then Chars (Id) /= Name_uParent
2101 and then Is_Tagged_Type (Current_Scope)
2102 then
2103 if Is_Derived_Type (Current_Scope)
2104 and then not Is_Known_Limited (Current_Scope)
2105 then
2106 Error_Msg_N
2107 ("extension of nonlimited type cannot have limited components",
2110 if Is_Interface (Root_Type (Current_Scope)) then
2111 Error_Msg_N
2112 ("\limitedness is not inherited from limited interface", N);
2113 Error_Msg_N ("\add LIMITED to type indication", N);
2114 end if;
2116 Explain_Limited_Type (T, N);
2117 Set_Etype (Id, Any_Type);
2118 Set_Is_Limited_Composite (Current_Scope, False);
2120 elsif not Is_Derived_Type (Current_Scope)
2121 and then not Is_Limited_Record (Current_Scope)
2122 and then not Is_Concurrent_Type (Current_Scope)
2123 then
2124 Error_Msg_N
2125 ("nonlimited tagged type cannot have limited components", N);
2126 Explain_Limited_Type (T, N);
2127 Set_Etype (Id, Any_Type);
2128 Set_Is_Limited_Composite (Current_Scope, False);
2129 end if;
2130 end if;
2132 -- If the component is an unconstrained task or protected type with
2133 -- discriminants, the component and the enclosing record are limited
2134 -- and the component is constrained by its default values. Compute
2135 -- its actual subtype, else it may be allocated the maximum size by
2136 -- the backend, and possibly overflow.
2138 if Is_Concurrent_Type (T)
2139 and then not Is_Constrained (T)
2140 and then Has_Discriminants (T)
2141 and then not Has_Discriminants (Current_Scope)
2142 then
2143 declare
2144 Act_T : constant Entity_Id := Build_Default_Subtype (T, N);
2146 begin
2147 Set_Etype (Id, Act_T);
2149 -- Rewrite component definition to use the constrained subtype
2151 Rewrite (Component_Definition (N),
2152 Make_Component_Definition (Loc,
2153 Subtype_Indication => New_Occurrence_Of (Act_T, Loc)));
2154 end;
2155 end if;
2157 Set_Original_Record_Component (Id, Id);
2159 if Has_Aspects (N) then
2160 Analyze_Aspect_Specifications (N, Id);
2161 end if;
2163 Analyze_Dimension (N);
2164 end Analyze_Component_Declaration;
2166 --------------------------
2167 -- Analyze_Declarations --
2168 --------------------------
2170 procedure Analyze_Declarations (L : List_Id) is
2171 Decl : Node_Id;
2173 procedure Adjust_Decl;
2174 -- Adjust Decl not to include implicit label declarations, since these
2175 -- have strange Sloc values that result in elaboration check problems.
2176 -- (They have the sloc of the label as found in the source, and that
2177 -- is ahead of the current declarative part).
2179 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id);
2180 -- Determine whether Body_Decl denotes the body of a late controlled
2181 -- primitive (either Initialize, Adjust or Finalize). If this is the
2182 -- case, add a proper spec if the body lacks one. The spec is inserted
2183 -- before Body_Decl and immedately analyzed.
2185 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id);
2186 -- Spec_Id is the entity of a package that may define abstract states.
2187 -- If the states have visible refinement, remove the visibility of each
2188 -- constituent at the end of the package body declarations.
2190 -----------------
2191 -- Adjust_Decl --
2192 -----------------
2194 procedure Adjust_Decl is
2195 begin
2196 while Present (Prev (Decl))
2197 and then Nkind (Decl) = N_Implicit_Label_Declaration
2198 loop
2199 Prev (Decl);
2200 end loop;
2201 end Adjust_Decl;
2203 --------------------------------------
2204 -- Handle_Late_Controlled_Primitive --
2205 --------------------------------------
2207 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id) is
2208 Body_Spec : constant Node_Id := Specification (Body_Decl);
2209 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2210 Loc : constant Source_Ptr := Sloc (Body_Id);
2211 Params : constant List_Id :=
2212 Parameter_Specifications (Body_Spec);
2213 Spec : Node_Id;
2214 Spec_Id : Entity_Id;
2215 Typ : Node_Id;
2217 begin
2218 -- Consider only procedure bodies whose name matches one of the three
2219 -- controlled primitives.
2221 if Nkind (Body_Spec) /= N_Procedure_Specification
2222 or else not Nam_In (Chars (Body_Id), Name_Adjust,
2223 Name_Finalize,
2224 Name_Initialize)
2225 then
2226 return;
2228 -- A controlled primitive must have exactly one formal which is not
2229 -- an anonymous access type.
2231 elsif List_Length (Params) /= 1 then
2232 return;
2233 end if;
2235 Typ := Parameter_Type (First (Params));
2237 if Nkind (Typ) = N_Access_Definition then
2238 return;
2239 end if;
2241 Find_Type (Typ);
2243 -- The type of the formal must be derived from [Limited_]Controlled
2245 if not Is_Controlled (Entity (Typ)) then
2246 return;
2247 end if;
2249 -- Check whether a specification exists for this body. We do not
2250 -- analyze the spec of the body in full, because it will be analyzed
2251 -- again when the body is properly analyzed, and we cannot create
2252 -- duplicate entries in the formals chain. We look for an explicit
2253 -- specification because the body may be an overriding operation and
2254 -- an inherited spec may be present.
2256 Spec_Id := Current_Entity (Body_Id);
2258 while Present (Spec_Id) loop
2259 if Ekind_In (Spec_Id, E_Procedure, E_Generic_Procedure)
2260 and then Scope (Spec_Id) = Current_Scope
2261 and then Present (First_Formal (Spec_Id))
2262 and then No (Next_Formal (First_Formal (Spec_Id)))
2263 and then Etype (First_Formal (Spec_Id)) = Entity (Typ)
2264 and then Comes_From_Source (Spec_Id)
2265 then
2266 return;
2267 end if;
2269 Spec_Id := Homonym (Spec_Id);
2270 end loop;
2272 -- At this point the body is known to be a late controlled primitive.
2273 -- Generate a matching spec and insert it before the body. Note the
2274 -- use of Copy_Separate_Tree - we want an entirely separate semantic
2275 -- tree in this case.
2277 Spec := Copy_Separate_Tree (Body_Spec);
2279 -- Ensure that the subprogram declaration does not inherit the null
2280 -- indicator from the body as we now have a proper spec/body pair.
2282 Set_Null_Present (Spec, False);
2284 Insert_Before_And_Analyze (Body_Decl,
2285 Make_Subprogram_Declaration (Loc, Specification => Spec));
2286 end Handle_Late_Controlled_Primitive;
2288 --------------------------------
2289 -- Remove_Visible_Refinements --
2290 --------------------------------
2292 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id) is
2293 State_Elmt : Elmt_Id;
2294 begin
2295 if Present (Abstract_States (Spec_Id)) then
2296 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2297 while Present (State_Elmt) loop
2298 Set_Has_Visible_Refinement (Node (State_Elmt), False);
2299 Next_Elmt (State_Elmt);
2300 end loop;
2301 end if;
2302 end Remove_Visible_Refinements;
2304 -- Local variables
2306 Context : Node_Id := Empty;
2307 Freeze_From : Entity_Id := Empty;
2308 Next_Decl : Node_Id;
2309 Pack_Decl : Node_Id := Empty;
2311 Body_Seen : Boolean := False;
2312 -- Flag set when the first body [stub] is encountered
2314 -- Start of processing for Analyze_Declarations
2316 begin
2317 if Restriction_Check_Required (SPARK_05) then
2318 Check_Later_Vs_Basic_Declarations (L, During_Parsing => False);
2319 end if;
2321 Decl := First (L);
2322 while Present (Decl) loop
2324 -- Package spec cannot contain a package declaration in SPARK
2326 if Nkind (Decl) = N_Package_Declaration
2327 and then Nkind (Parent (L)) = N_Package_Specification
2328 then
2329 Check_SPARK_05_Restriction
2330 ("package specification cannot contain a package declaration",
2331 Decl);
2332 end if;
2334 -- Complete analysis of declaration
2336 Analyze (Decl);
2337 Next_Decl := Next (Decl);
2339 if No (Freeze_From) then
2340 Freeze_From := First_Entity (Current_Scope);
2341 end if;
2343 -- At the end of a declarative part, freeze remaining entities
2344 -- declared in it. The end of the visible declarations of package
2345 -- specification is not the end of a declarative part if private
2346 -- declarations are present. The end of a package declaration is a
2347 -- freezing point only if it a library package. A task definition or
2348 -- protected type definition is not a freeze point either. Finally,
2349 -- we do not freeze entities in generic scopes, because there is no
2350 -- code generated for them and freeze nodes will be generated for
2351 -- the instance.
2353 -- The end of a package instantiation is not a freeze point, but
2354 -- for now we make it one, because the generic body is inserted
2355 -- (currently) immediately after. Generic instantiations will not
2356 -- be a freeze point once delayed freezing of bodies is implemented.
2357 -- (This is needed in any case for early instantiations ???).
2359 if No (Next_Decl) then
2360 if Nkind_In (Parent (L), N_Component_List,
2361 N_Task_Definition,
2362 N_Protected_Definition)
2363 then
2364 null;
2366 elsif Nkind (Parent (L)) /= N_Package_Specification then
2367 if Nkind (Parent (L)) = N_Package_Body then
2368 Freeze_From := First_Entity (Current_Scope);
2369 end if;
2371 -- There may have been several freezing points previously,
2372 -- for example object declarations or subprogram bodies, but
2373 -- at the end of a declarative part we check freezing from
2374 -- the beginning, even though entities may already be frozen,
2375 -- in order to perform visibility checks on delayed aspects.
2377 Adjust_Decl;
2378 Freeze_All (First_Entity (Current_Scope), Decl);
2379 Freeze_From := Last_Entity (Current_Scope);
2381 elsif Scope (Current_Scope) /= Standard_Standard
2382 and then not Is_Child_Unit (Current_Scope)
2383 and then No (Generic_Parent (Parent (L)))
2384 then
2385 null;
2387 elsif L /= Visible_Declarations (Parent (L))
2388 or else No (Private_Declarations (Parent (L)))
2389 or else Is_Empty_List (Private_Declarations (Parent (L)))
2390 then
2391 Adjust_Decl;
2392 Freeze_All (First_Entity (Current_Scope), Decl);
2393 Freeze_From := Last_Entity (Current_Scope);
2394 end if;
2396 -- If next node is a body then freeze all types before the body.
2397 -- An exception occurs for some expander-generated bodies. If these
2398 -- are generated at places where in general language rules would not
2399 -- allow a freeze point, then we assume that the expander has
2400 -- explicitly checked that all required types are properly frozen,
2401 -- and we do not cause general freezing here. This special circuit
2402 -- is used when the encountered body is marked as having already
2403 -- been analyzed.
2405 -- In all other cases (bodies that come from source, and expander
2406 -- generated bodies that have not been analyzed yet), freeze all
2407 -- types now. Note that in the latter case, the expander must take
2408 -- care to attach the bodies at a proper place in the tree so as to
2409 -- not cause unwanted freezing at that point.
2411 elsif not Analyzed (Next_Decl) and then Is_Body (Next_Decl) then
2413 -- When a controlled type is frozen, the expander generates stream
2414 -- and controlled type support routines. If the freeze is caused
2415 -- by the stand alone body of Initialize, Adjust and Finalize, the
2416 -- expander will end up using the wrong version of these routines
2417 -- as the body has not been processed yet. To remedy this, detect
2418 -- a late controlled primitive and create a proper spec for it.
2419 -- This ensures that the primitive will override its inherited
2420 -- counterpart before the freeze takes place.
2422 -- If the declaration we just processed is a body, do not attempt
2423 -- to examine Next_Decl as the late primitive idiom can only apply
2424 -- to the first encountered body.
2426 -- The spec of the late primitive is not generated in ASIS mode to
2427 -- ensure a consistent list of primitives that indicates the true
2428 -- semantic structure of the program (which is not relevant when
2429 -- generating executable code.
2431 -- ??? a cleaner approach may be possible and/or this solution
2432 -- could be extended to general-purpose late primitives, TBD.
2434 if not ASIS_Mode and then not Body_Seen and then not Is_Body (Decl)
2435 then
2436 Body_Seen := True;
2438 if Nkind (Next_Decl) = N_Subprogram_Body then
2439 Handle_Late_Controlled_Primitive (Next_Decl);
2440 end if;
2441 end if;
2443 Adjust_Decl;
2444 Freeze_All (Freeze_From, Decl);
2445 Freeze_From := Last_Entity (Current_Scope);
2446 end if;
2448 Decl := Next_Decl;
2449 end loop;
2451 -- Analyze the contracts of packages and their bodies
2453 if Present (L) then
2454 Context := Parent (L);
2456 if Nkind (Context) = N_Package_Specification then
2457 Pack_Decl := Parent (Context);
2459 -- When a package has private declarations, its contract must be
2460 -- analyzed at the end of the said declarations. This way both the
2461 -- analysis and freeze actions are properly synchronized in case
2462 -- of private type use within the contract.
2464 if L = Private_Declarations (Context) then
2465 Analyze_Package_Contract (Defining_Entity (Context));
2467 -- Build the bodies of the default initial condition procedures
2468 -- for all types subject to pragma Default_Initial_Condition.
2469 -- From a purely Ada stand point, this is a freezing activity,
2470 -- however freezing is not available under GNATprove_Mode. To
2471 -- accomodate both scenarios, the bodies are build at the end
2472 -- of private declaration analysis.
2474 Build_Default_Init_Cond_Procedure_Bodies (L);
2476 -- Otherwise the contract is analyzed at the end of the visible
2477 -- declarations.
2479 elsif L = Visible_Declarations (Context)
2480 and then No (Private_Declarations (Context))
2481 then
2482 Analyze_Package_Contract (Defining_Entity (Context));
2483 end if;
2485 elsif Nkind (Context) = N_Package_Body then
2486 Pack_Decl := Context;
2487 Analyze_Package_Body_Contract (Defining_Entity (Context));
2488 end if;
2490 -- Analyze the contracts of all subprogram declarations, subprogram
2491 -- bodies and variables now due to the delayed visibility needs of
2492 -- of their aspects and pragmas. Capture global references in generic
2493 -- subprograms or bodies.
2495 Decl := First (L);
2496 while Present (Decl) loop
2497 if Nkind (Decl) = N_Object_Declaration then
2498 Analyze_Object_Contract (Defining_Entity (Decl));
2500 elsif Nkind_In (Decl, N_Abstract_Subprogram_Declaration,
2501 N_Generic_Subprogram_Declaration,
2502 N_Subprogram_Declaration)
2503 then
2504 Analyze_Subprogram_Contract (Defining_Entity (Decl));
2506 elsif Nkind (Decl) = N_Subprogram_Body then
2507 Analyze_Subprogram_Body_Contract (Defining_Entity (Decl));
2509 elsif Nkind (Decl) = N_Subprogram_Body_Stub then
2510 Analyze_Subprogram_Body_Stub_Contract (Defining_Entity (Decl));
2511 end if;
2513 -- Capture all global references in a generic subprogram or a body
2514 -- [stub] now that the contract has been analyzed.
2516 if Nkind_In (Decl, N_Generic_Subprogram_Declaration,
2517 N_Subprogram_Body,
2518 N_Subprogram_Body_Stub)
2519 and then Is_Generic_Declaration_Or_Body (Decl)
2520 then
2521 Save_Global_References_In_Contract
2522 (Templ => Original_Node (Decl),
2523 Gen_Id => Corresponding_Spec_Of (Decl));
2524 end if;
2526 Next (Decl);
2527 end loop;
2529 -- The owner of the declarations is a package [body]
2531 if Present (Pack_Decl) then
2533 -- Capture all global references in a generic package or a body
2534 -- after all nested generic subprograms and bodies were subjected
2535 -- to the same processing.
2537 if Is_Generic_Declaration_Or_Body (Pack_Decl) then
2538 Save_Global_References_In_Contract
2539 (Templ => Original_Node (Pack_Decl),
2540 Gen_Id => Corresponding_Spec_Of (Pack_Decl));
2541 end if;
2543 -- State refinements are visible upto the end the of the package
2544 -- body declarations. Hide the state refinements from visibility
2545 -- to restore the original state conditions.
2547 if Nkind (Pack_Decl) = N_Package_Body then
2548 Remove_Visible_Refinements (Corresponding_Spec (Pack_Decl));
2549 end if;
2550 end if;
2551 end if;
2552 end Analyze_Declarations;
2554 -----------------------------------
2555 -- Analyze_Full_Type_Declaration --
2556 -----------------------------------
2558 procedure Analyze_Full_Type_Declaration (N : Node_Id) is
2559 Def : constant Node_Id := Type_Definition (N);
2560 Def_Id : constant Entity_Id := Defining_Identifier (N);
2561 T : Entity_Id;
2562 Prev : Entity_Id;
2564 Is_Remote : constant Boolean :=
2565 (Is_Remote_Types (Current_Scope)
2566 or else Is_Remote_Call_Interface (Current_Scope))
2567 and then not (In_Private_Part (Current_Scope)
2568 or else In_Package_Body (Current_Scope));
2570 procedure Check_Ops_From_Incomplete_Type;
2571 -- If there is a tagged incomplete partial view of the type, traverse
2572 -- the primitives of the incomplete view and change the type of any
2573 -- controlling formals and result to indicate the full view. The
2574 -- primitives will be added to the full type's primitive operations
2575 -- list later in Sem_Disp.Check_Operation_From_Incomplete_Type (which
2576 -- is called from Process_Incomplete_Dependents).
2578 ------------------------------------
2579 -- Check_Ops_From_Incomplete_Type --
2580 ------------------------------------
2582 procedure Check_Ops_From_Incomplete_Type is
2583 Elmt : Elmt_Id;
2584 Formal : Entity_Id;
2585 Op : Entity_Id;
2587 begin
2588 if Prev /= T
2589 and then Ekind (Prev) = E_Incomplete_Type
2590 and then Is_Tagged_Type (Prev)
2591 and then Is_Tagged_Type (T)
2592 then
2593 Elmt := First_Elmt (Primitive_Operations (Prev));
2594 while Present (Elmt) loop
2595 Op := Node (Elmt);
2597 Formal := First_Formal (Op);
2598 while Present (Formal) loop
2599 if Etype (Formal) = Prev then
2600 Set_Etype (Formal, T);
2601 end if;
2603 Next_Formal (Formal);
2604 end loop;
2606 if Etype (Op) = Prev then
2607 Set_Etype (Op, T);
2608 end if;
2610 Next_Elmt (Elmt);
2611 end loop;
2612 end if;
2613 end Check_Ops_From_Incomplete_Type;
2615 -- Start of processing for Analyze_Full_Type_Declaration
2617 begin
2618 Prev := Find_Type_Name (N);
2620 -- The type declaration may be subject to pragma Ghost with policy
2621 -- Ignore. Set the mode now to ensure that any nodes generated during
2622 -- analysis and expansion are properly flagged as ignored Ghost.
2624 Set_Ghost_Mode (N, Prev);
2626 -- The full view, if present, now points to the current type. If there
2627 -- is an incomplete partial view, set a link to it, to simplify the
2628 -- retrieval of primitive operations of the type.
2630 -- Ada 2005 (AI-50217): If the type was previously decorated when
2631 -- imported through a LIMITED WITH clause, it appears as incomplete
2632 -- but has no full view.
2634 if Ekind (Prev) = E_Incomplete_Type
2635 and then Present (Full_View (Prev))
2636 then
2637 T := Full_View (Prev);
2638 Set_Incomplete_View (N, Parent (Prev));
2639 else
2640 T := Prev;
2641 end if;
2643 Set_Is_Pure (T, Is_Pure (Current_Scope));
2645 -- We set the flag Is_First_Subtype here. It is needed to set the
2646 -- corresponding flag for the Implicit class-wide-type created
2647 -- during tagged types processing.
2649 Set_Is_First_Subtype (T, True);
2651 -- Only composite types other than array types are allowed to have
2652 -- discriminants.
2654 case Nkind (Def) is
2656 -- For derived types, the rule will be checked once we've figured
2657 -- out the parent type.
2659 when N_Derived_Type_Definition =>
2660 null;
2662 -- For record types, discriminants are allowed, unless we are in
2663 -- SPARK.
2665 when N_Record_Definition =>
2666 if Present (Discriminant_Specifications (N)) then
2667 Check_SPARK_05_Restriction
2668 ("discriminant type is not allowed",
2669 Defining_Identifier
2670 (First (Discriminant_Specifications (N))));
2671 end if;
2673 when others =>
2674 if Present (Discriminant_Specifications (N)) then
2675 Error_Msg_N
2676 ("elementary or array type cannot have discriminants",
2677 Defining_Identifier
2678 (First (Discriminant_Specifications (N))));
2679 end if;
2680 end case;
2682 -- Elaborate the type definition according to kind, and generate
2683 -- subsidiary (implicit) subtypes where needed. We skip this if it was
2684 -- already done (this happens during the reanalysis that follows a call
2685 -- to the high level optimizer).
2687 if not Analyzed (T) then
2688 Set_Analyzed (T);
2690 case Nkind (Def) is
2691 when N_Access_To_Subprogram_Definition =>
2692 Access_Subprogram_Declaration (T, Def);
2694 -- If this is a remote access to subprogram, we must create the
2695 -- equivalent fat pointer type, and related subprograms.
2697 if Is_Remote then
2698 Process_Remote_AST_Declaration (N);
2699 end if;
2701 -- Validate categorization rule against access type declaration
2702 -- usually a violation in Pure unit, Shared_Passive unit.
2704 Validate_Access_Type_Declaration (T, N);
2706 when N_Access_To_Object_Definition =>
2707 Access_Type_Declaration (T, Def);
2709 -- Validate categorization rule against access type declaration
2710 -- usually a violation in Pure unit, Shared_Passive unit.
2712 Validate_Access_Type_Declaration (T, N);
2714 -- If we are in a Remote_Call_Interface package and define a
2715 -- RACW, then calling stubs and specific stream attributes
2716 -- must be added.
2718 if Is_Remote
2719 and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
2720 then
2721 Add_RACW_Features (Def_Id);
2722 end if;
2724 when N_Array_Type_Definition =>
2725 Array_Type_Declaration (T, Def);
2727 when N_Derived_Type_Definition =>
2728 Derived_Type_Declaration (T, N, T /= Def_Id);
2730 when N_Enumeration_Type_Definition =>
2731 Enumeration_Type_Declaration (T, Def);
2733 when N_Floating_Point_Definition =>
2734 Floating_Point_Type_Declaration (T, Def);
2736 when N_Decimal_Fixed_Point_Definition =>
2737 Decimal_Fixed_Point_Type_Declaration (T, Def);
2739 when N_Ordinary_Fixed_Point_Definition =>
2740 Ordinary_Fixed_Point_Type_Declaration (T, Def);
2742 when N_Signed_Integer_Type_Definition =>
2743 Signed_Integer_Type_Declaration (T, Def);
2745 when N_Modular_Type_Definition =>
2746 Modular_Type_Declaration (T, Def);
2748 when N_Record_Definition =>
2749 Record_Type_Declaration (T, N, Prev);
2751 -- If declaration has a parse error, nothing to elaborate.
2753 when N_Error =>
2754 null;
2756 when others =>
2757 raise Program_Error;
2759 end case;
2760 end if;
2762 if Etype (T) = Any_Type then
2763 return;
2764 end if;
2766 -- Controlled type is not allowed in SPARK
2768 if Is_Visibly_Controlled (T) then
2769 Check_SPARK_05_Restriction ("controlled type is not allowed", N);
2770 end if;
2772 -- A type declared within a Ghost region is automatically Ghost
2773 -- (SPARK RM 6.9(2)).
2775 if Comes_From_Source (T) and then Ghost_Mode > None then
2776 Set_Is_Ghost_Entity (T);
2777 end if;
2779 -- Some common processing for all types
2781 Set_Depends_On_Private (T, Has_Private_Component (T));
2782 Check_Ops_From_Incomplete_Type;
2784 -- Both the declared entity, and its anonymous base type if one was
2785 -- created, need freeze nodes allocated.
2787 declare
2788 B : constant Entity_Id := Base_Type (T);
2790 begin
2791 -- In the case where the base type differs from the first subtype, we
2792 -- pre-allocate a freeze node, and set the proper link to the first
2793 -- subtype. Freeze_Entity will use this preallocated freeze node when
2794 -- it freezes the entity.
2796 -- This does not apply if the base type is a generic type, whose
2797 -- declaration is independent of the current derived definition.
2799 if B /= T and then not Is_Generic_Type (B) then
2800 Ensure_Freeze_Node (B);
2801 Set_First_Subtype_Link (Freeze_Node (B), T);
2802 end if;
2804 -- A type that is imported through a limited_with clause cannot
2805 -- generate any code, and thus need not be frozen. However, an access
2806 -- type with an imported designated type needs a finalization list,
2807 -- which may be referenced in some other package that has non-limited
2808 -- visibility on the designated type. Thus we must create the
2809 -- finalization list at the point the access type is frozen, to
2810 -- prevent unsatisfied references at link time.
2812 if not From_Limited_With (T) or else Is_Access_Type (T) then
2813 Set_Has_Delayed_Freeze (T);
2814 end if;
2815 end;
2817 -- Case where T is the full declaration of some private type which has
2818 -- been swapped in Defining_Identifier (N).
2820 if T /= Def_Id and then Is_Private_Type (Def_Id) then
2821 Process_Full_View (N, T, Def_Id);
2823 -- Record the reference. The form of this is a little strange, since
2824 -- the full declaration has been swapped in. So the first parameter
2825 -- here represents the entity to which a reference is made which is
2826 -- the "real" entity, i.e. the one swapped in, and the second
2827 -- parameter provides the reference location.
2829 -- Also, we want to kill Has_Pragma_Unreferenced temporarily here
2830 -- since we don't want a complaint about the full type being an
2831 -- unwanted reference to the private type
2833 declare
2834 B : constant Boolean := Has_Pragma_Unreferenced (T);
2835 begin
2836 Set_Has_Pragma_Unreferenced (T, False);
2837 Generate_Reference (T, T, 'c');
2838 Set_Has_Pragma_Unreferenced (T, B);
2839 end;
2841 Set_Completion_Referenced (Def_Id);
2843 -- For completion of incomplete type, process incomplete dependents
2844 -- and always mark the full type as referenced (it is the incomplete
2845 -- type that we get for any real reference).
2847 elsif Ekind (Prev) = E_Incomplete_Type then
2848 Process_Incomplete_Dependents (N, T, Prev);
2849 Generate_Reference (Prev, Def_Id, 'c');
2850 Set_Completion_Referenced (Def_Id);
2852 -- If not private type or incomplete type completion, this is a real
2853 -- definition of a new entity, so record it.
2855 else
2856 Generate_Definition (Def_Id);
2857 end if;
2859 -- Propagate any pending access types whose finalization masters need to
2860 -- be fully initialized from the partial to the full view. Guard against
2861 -- an illegal full view that remains unanalyzed.
2863 if Is_Type (Def_Id) and then Is_Incomplete_Or_Private_Type (Prev) then
2864 Set_Pending_Access_Types (Def_Id, Pending_Access_Types (Prev));
2865 end if;
2867 if Chars (Scope (Def_Id)) = Name_System
2868 and then Chars (Def_Id) = Name_Address
2869 and then Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (N)))
2870 then
2871 Set_Is_Descendent_Of_Address (Def_Id);
2872 Set_Is_Descendent_Of_Address (Base_Type (Def_Id));
2873 Set_Is_Descendent_Of_Address (Prev);
2874 end if;
2876 Set_Optimize_Alignment_Flags (Def_Id);
2877 Check_Eliminated (Def_Id);
2879 -- If the declaration is a completion and aspects are present, apply
2880 -- them to the entity for the type which is currently the partial
2881 -- view, but which is the one that will be frozen.
2883 if Has_Aspects (N) then
2885 -- In most cases the partial view is a private type, and both views
2886 -- appear in different declarative parts. In the unusual case where
2887 -- the partial view is incomplete, perform the analysis on the
2888 -- full view, to prevent freezing anomalies with the corresponding
2889 -- class-wide type, which otherwise might be frozen before the
2890 -- dispatch table is built.
2892 if Prev /= Def_Id
2893 and then Ekind (Prev) /= E_Incomplete_Type
2894 then
2895 Analyze_Aspect_Specifications (N, Prev);
2897 -- Normal case
2899 else
2900 Analyze_Aspect_Specifications (N, Def_Id);
2901 end if;
2902 end if;
2903 end Analyze_Full_Type_Declaration;
2905 ----------------------------------
2906 -- Analyze_Incomplete_Type_Decl --
2907 ----------------------------------
2909 procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
2910 F : constant Boolean := Is_Pure (Current_Scope);
2911 T : Entity_Id;
2913 begin
2914 Check_SPARK_05_Restriction ("incomplete type is not allowed", N);
2916 Generate_Definition (Defining_Identifier (N));
2918 -- Process an incomplete declaration. The identifier must not have been
2919 -- declared already in the scope. However, an incomplete declaration may
2920 -- appear in the private part of a package, for a private type that has
2921 -- already been declared.
2923 -- In this case, the discriminants (if any) must match
2925 T := Find_Type_Name (N);
2927 Set_Ekind (T, E_Incomplete_Type);
2928 Init_Size_Align (T);
2929 Set_Is_First_Subtype (T, True);
2930 Set_Etype (T, T);
2932 -- An incomplete type declared within a Ghost region is automatically
2933 -- Ghost (SPARK RM 6.9(2)).
2935 if Ghost_Mode > None then
2936 Set_Is_Ghost_Entity (T);
2937 end if;
2939 -- Ada 2005 (AI-326): Minimum decoration to give support to tagged
2940 -- incomplete types.
2942 if Tagged_Present (N) then
2943 Set_Is_Tagged_Type (T, True);
2944 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
2945 Make_Class_Wide_Type (T);
2946 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2947 end if;
2949 Push_Scope (T);
2951 Set_Stored_Constraint (T, No_Elist);
2953 if Present (Discriminant_Specifications (N)) then
2954 Process_Discriminants (N);
2955 end if;
2957 End_Scope;
2959 -- If the type has discriminants, non-trivial subtypes may be
2960 -- declared before the full view of the type. The full views of those
2961 -- subtypes will be built after the full view of the type.
2963 Set_Private_Dependents (T, New_Elmt_List);
2964 Set_Is_Pure (T, F);
2965 end Analyze_Incomplete_Type_Decl;
2967 -----------------------------------
2968 -- Analyze_Interface_Declaration --
2969 -----------------------------------
2971 procedure Analyze_Interface_Declaration (T : Entity_Id; Def : Node_Id) is
2972 CW : constant Entity_Id := Class_Wide_Type (T);
2974 begin
2975 Set_Is_Tagged_Type (T);
2976 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
2978 Set_Is_Limited_Record (T, Limited_Present (Def)
2979 or else Task_Present (Def)
2980 or else Protected_Present (Def)
2981 or else Synchronized_Present (Def));
2983 -- Type is abstract if full declaration carries keyword, or if previous
2984 -- partial view did.
2986 Set_Is_Abstract_Type (T);
2987 Set_Is_Interface (T);
2989 -- Type is a limited interface if it includes the keyword limited, task,
2990 -- protected, or synchronized.
2992 Set_Is_Limited_Interface
2993 (T, Limited_Present (Def)
2994 or else Protected_Present (Def)
2995 or else Synchronized_Present (Def)
2996 or else Task_Present (Def));
2998 Set_Interfaces (T, New_Elmt_List);
2999 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3001 -- Complete the decoration of the class-wide entity if it was already
3002 -- built (i.e. during the creation of the limited view)
3004 if Present (CW) then
3005 Set_Is_Interface (CW);
3006 Set_Is_Limited_Interface (CW, Is_Limited_Interface (T));
3007 end if;
3009 -- Check runtime support for synchronized interfaces
3011 if VM_Target = No_VM
3012 and then (Is_Task_Interface (T)
3013 or else Is_Protected_Interface (T)
3014 or else Is_Synchronized_Interface (T))
3015 and then not RTE_Available (RE_Select_Specific_Data)
3016 then
3017 Error_Msg_CRT ("synchronized interfaces", T);
3018 end if;
3019 end Analyze_Interface_Declaration;
3021 -----------------------------
3022 -- Analyze_Itype_Reference --
3023 -----------------------------
3025 -- Nothing to do. This node is placed in the tree only for the benefit of
3026 -- back end processing, and has no effect on the semantic processing.
3028 procedure Analyze_Itype_Reference (N : Node_Id) is
3029 begin
3030 pragma Assert (Is_Itype (Itype (N)));
3031 null;
3032 end Analyze_Itype_Reference;
3034 --------------------------------
3035 -- Analyze_Number_Declaration --
3036 --------------------------------
3038 procedure Analyze_Number_Declaration (N : Node_Id) is
3039 Id : constant Entity_Id := Defining_Identifier (N);
3040 E : constant Node_Id := Expression (N);
3041 T : Entity_Id;
3042 Index : Interp_Index;
3043 It : Interp;
3045 begin
3046 -- The number declaration may be subject to pragma Ghost with policy
3047 -- Ignore. Set the mode now to ensure that any nodes generated during
3048 -- analysis and expansion are properly flagged as ignored Ghost.
3050 Set_Ghost_Mode (N);
3052 Generate_Definition (Id);
3053 Enter_Name (Id);
3055 -- A number declared within a Ghost region is automatically Ghost
3056 -- (SPARK RM 6.9(2)).
3058 if Ghost_Mode > None then
3059 Set_Is_Ghost_Entity (Id);
3060 end if;
3062 -- This is an optimization of a common case of an integer literal
3064 if Nkind (E) = N_Integer_Literal then
3065 Set_Is_Static_Expression (E, True);
3066 Set_Etype (E, Universal_Integer);
3068 Set_Etype (Id, Universal_Integer);
3069 Set_Ekind (Id, E_Named_Integer);
3070 Set_Is_Frozen (Id, True);
3071 return;
3072 end if;
3074 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3076 -- Process expression, replacing error by integer zero, to avoid
3077 -- cascaded errors or aborts further along in the processing
3079 -- Replace Error by integer zero, which seems least likely to cause
3080 -- cascaded errors.
3082 if E = Error then
3083 Rewrite (E, Make_Integer_Literal (Sloc (E), Uint_0));
3084 Set_Error_Posted (E);
3085 end if;
3087 Analyze (E);
3089 -- Verify that the expression is static and numeric. If
3090 -- the expression is overloaded, we apply the preference
3091 -- rule that favors root numeric types.
3093 if not Is_Overloaded (E) then
3094 T := Etype (E);
3095 if Has_Dynamic_Predicate_Aspect (T) then
3096 Error_Msg_N
3097 ("subtype has dynamic predicate, "
3098 & "not allowed in number declaration", N);
3099 end if;
3101 else
3102 T := Any_Type;
3104 Get_First_Interp (E, Index, It);
3105 while Present (It.Typ) loop
3106 if (Is_Integer_Type (It.Typ) or else Is_Real_Type (It.Typ))
3107 and then (Scope (Base_Type (It.Typ))) = Standard_Standard
3108 then
3109 if T = Any_Type then
3110 T := It.Typ;
3112 elsif It.Typ = Universal_Real
3113 or else
3114 It.Typ = Universal_Integer
3115 then
3116 -- Choose universal interpretation over any other
3118 T := It.Typ;
3119 exit;
3120 end if;
3121 end if;
3123 Get_Next_Interp (Index, It);
3124 end loop;
3125 end if;
3127 if Is_Integer_Type (T) then
3128 Resolve (E, T);
3129 Set_Etype (Id, Universal_Integer);
3130 Set_Ekind (Id, E_Named_Integer);
3132 elsif Is_Real_Type (T) then
3134 -- Because the real value is converted to universal_real, this is a
3135 -- legal context for a universal fixed expression.
3137 if T = Universal_Fixed then
3138 declare
3139 Loc : constant Source_Ptr := Sloc (N);
3140 Conv : constant Node_Id := Make_Type_Conversion (Loc,
3141 Subtype_Mark =>
3142 New_Occurrence_Of (Universal_Real, Loc),
3143 Expression => Relocate_Node (E));
3145 begin
3146 Rewrite (E, Conv);
3147 Analyze (E);
3148 end;
3150 elsif T = Any_Fixed then
3151 Error_Msg_N ("illegal context for mixed mode operation", E);
3153 -- Expression is of the form : universal_fixed * integer. Try to
3154 -- resolve as universal_real.
3156 T := Universal_Real;
3157 Set_Etype (E, T);
3158 end if;
3160 Resolve (E, T);
3161 Set_Etype (Id, Universal_Real);
3162 Set_Ekind (Id, E_Named_Real);
3164 else
3165 Wrong_Type (E, Any_Numeric);
3166 Resolve (E, T);
3168 Set_Etype (Id, T);
3169 Set_Ekind (Id, E_Constant);
3170 Set_Never_Set_In_Source (Id, True);
3171 Set_Is_True_Constant (Id, True);
3172 return;
3173 end if;
3175 if Nkind_In (E, N_Integer_Literal, N_Real_Literal) then
3176 Set_Etype (E, Etype (Id));
3177 end if;
3179 if not Is_OK_Static_Expression (E) then
3180 Flag_Non_Static_Expr
3181 ("non-static expression used in number declaration!", E);
3182 Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
3183 Set_Etype (E, Any_Type);
3184 end if;
3185 end Analyze_Number_Declaration;
3187 -----------------------------
3188 -- Analyze_Object_Contract --
3189 -----------------------------
3191 procedure Analyze_Object_Contract (Obj_Id : Entity_Id) is
3192 Obj_Typ : constant Entity_Id := Etype (Obj_Id);
3193 AR_Val : Boolean := False;
3194 AW_Val : Boolean := False;
3195 ER_Val : Boolean := False;
3196 EW_Val : Boolean := False;
3197 Prag : Node_Id;
3198 Seen : Boolean := False;
3200 begin
3201 -- The loop parameter in an element iterator over a formal container
3202 -- is declared with an object declaration but no contracts apply.
3204 if Ekind (Obj_Id) = E_Loop_Parameter then
3205 return;
3206 end if;
3208 if Ekind (Obj_Id) = E_Constant then
3210 -- A constant cannot be effectively volatile. This check is only
3211 -- relevant with SPARK_Mode on as it is not a standard Ada legality
3212 -- rule. Do not flag internally-generated constants that map generic
3213 -- formals to actuals in instantiations (SPARK RM 7.1.3(6)).
3215 if SPARK_Mode = On
3216 and then Is_Effectively_Volatile (Obj_Id)
3217 and then No (Corresponding_Generic_Association (Parent (Obj_Id)))
3219 -- Don't give this for internally generated entities (such as the
3220 -- FIRST and LAST temporaries generated for bounds).
3222 and then Comes_From_Source (Obj_Id)
3223 then
3224 Error_Msg_N ("constant cannot be volatile", Obj_Id);
3225 end if;
3227 else pragma Assert (Ekind (Obj_Id) = E_Variable);
3229 -- The following checks are only relevant when SPARK_Mode is on as
3230 -- they are not standard Ada legality rules. Internally generated
3231 -- temporaries are ignored.
3233 if SPARK_Mode = On and then Comes_From_Source (Obj_Id) then
3234 if Is_Effectively_Volatile (Obj_Id) then
3236 -- The declaration of an effectively volatile object must
3237 -- appear at the library level (SPARK RM 7.1.3(7), C.6(6)).
3239 if not Is_Library_Level_Entity (Obj_Id) then
3240 Error_Msg_N
3241 ("volatile variable & must be declared at library level",
3242 Obj_Id);
3244 -- An object of a discriminated type cannot be effectively
3245 -- volatile (SPARK RM C.6(4)).
3247 elsif Has_Discriminants (Obj_Typ) then
3248 Error_Msg_N
3249 ("discriminated object & cannot be volatile", Obj_Id);
3251 -- An object of a tagged type cannot be effectively volatile
3252 -- (SPARK RM C.6(5)).
3254 elsif Is_Tagged_Type (Obj_Typ) then
3255 Error_Msg_N ("tagged object & cannot be volatile", Obj_Id);
3256 end if;
3258 -- The object is not effectively volatile
3260 else
3261 -- A non-effectively volatile object cannot have effectively
3262 -- volatile components (SPARK RM 7.1.3(7)).
3264 if not Is_Effectively_Volatile (Obj_Id)
3265 and then Has_Volatile_Component (Obj_Typ)
3266 then
3267 Error_Msg_N
3268 ("non-volatile object & cannot have volatile components",
3269 Obj_Id);
3270 end if;
3271 end if;
3272 end if;
3274 if Is_Ghost_Entity (Obj_Id) then
3276 -- A Ghost object cannot be effectively volatile (SPARK RM 6.9(8))
3278 if Is_Effectively_Volatile (Obj_Id) then
3279 Error_Msg_N ("ghost variable & cannot be volatile", Obj_Id);
3281 -- A Ghost object cannot be imported or exported (SPARK RM 6.9(8))
3283 elsif Is_Imported (Obj_Id) then
3284 Error_Msg_N ("ghost object & cannot be imported", Obj_Id);
3286 elsif Is_Exported (Obj_Id) then
3287 Error_Msg_N ("ghost object & cannot be exported", Obj_Id);
3288 end if;
3289 end if;
3291 -- Analyze all external properties
3293 Prag := Get_Pragma (Obj_Id, Pragma_Async_Readers);
3295 if Present (Prag) then
3296 Analyze_External_Property_In_Decl_Part (Prag, AR_Val);
3297 Seen := True;
3298 end if;
3300 Prag := Get_Pragma (Obj_Id, Pragma_Async_Writers);
3302 if Present (Prag) then
3303 Analyze_External_Property_In_Decl_Part (Prag, AW_Val);
3304 Seen := True;
3305 end if;
3307 Prag := Get_Pragma (Obj_Id, Pragma_Effective_Reads);
3309 if Present (Prag) then
3310 Analyze_External_Property_In_Decl_Part (Prag, ER_Val);
3311 Seen := True;
3312 end if;
3314 Prag := Get_Pragma (Obj_Id, Pragma_Effective_Writes);
3316 if Present (Prag) then
3317 Analyze_External_Property_In_Decl_Part (Prag, EW_Val);
3318 Seen := True;
3319 end if;
3321 -- Verify the mutual interaction of the various external properties
3323 if Seen then
3324 Check_External_Properties (Obj_Id, AR_Val, AW_Val, ER_Val, EW_Val);
3325 end if;
3327 -- Check whether the lack of indicator Part_Of agrees with the
3328 -- placement of the variable with respect to the state space.
3330 Prag := Get_Pragma (Obj_Id, Pragma_Part_Of);
3332 if No (Prag) then
3333 Check_Missing_Part_Of (Obj_Id);
3334 end if;
3335 end if;
3337 -- A ghost object cannot be imported or exported (SPARK RM 6.9(8))
3339 if Is_Ghost_Entity (Obj_Id) then
3340 if Is_Exported (Obj_Id) then
3341 Error_Msg_N ("ghost object & cannot be exported", Obj_Id);
3343 elsif Is_Imported (Obj_Id) then
3344 Error_Msg_N ("ghost object & cannot be imported", Obj_Id);
3345 end if;
3346 end if;
3347 end Analyze_Object_Contract;
3349 --------------------------------
3350 -- Analyze_Object_Declaration --
3351 --------------------------------
3353 procedure Analyze_Object_Declaration (N : Node_Id) is
3354 Loc : constant Source_Ptr := Sloc (N);
3355 Id : constant Entity_Id := Defining_Identifier (N);
3356 T : Entity_Id;
3357 Act_T : Entity_Id;
3359 E : Node_Id := Expression (N);
3360 -- E is set to Expression (N) throughout this routine. When
3361 -- Expression (N) is modified, E is changed accordingly.
3363 Prev_Entity : Entity_Id := Empty;
3365 function Count_Tasks (T : Entity_Id) return Uint;
3366 -- This function is called when a non-generic library level object of a
3367 -- task type is declared. Its function is to count the static number of
3368 -- tasks declared within the type (it is only called if Has_Tasks is set
3369 -- for T). As a side effect, if an array of tasks with non-static bounds
3370 -- or a variant record type is encountered, Check_Restrictions is called
3371 -- indicating the count is unknown.
3373 function Delayed_Aspect_Present return Boolean;
3374 -- If the declaration has an expression that is an aggregate, and it
3375 -- has aspects that require delayed analysis, the resolution of the
3376 -- aggregate must be deferred to the freeze point of the objet. This
3377 -- special processing was created for address clauses, but it must
3378 -- also apply to Alignment. This must be done before the aspect
3379 -- specifications are analyzed because we must handle the aggregate
3380 -- before the analysis of the object declaration is complete.
3382 -- Any other relevant delayed aspects on object declarations ???
3384 -----------------
3385 -- Count_Tasks --
3386 -----------------
3388 function Count_Tasks (T : Entity_Id) return Uint is
3389 C : Entity_Id;
3390 X : Node_Id;
3391 V : Uint;
3393 begin
3394 if Is_Task_Type (T) then
3395 return Uint_1;
3397 elsif Is_Record_Type (T) then
3398 if Has_Discriminants (T) then
3399 Check_Restriction (Max_Tasks, N);
3400 return Uint_0;
3402 else
3403 V := Uint_0;
3404 C := First_Component (T);
3405 while Present (C) loop
3406 V := V + Count_Tasks (Etype (C));
3407 Next_Component (C);
3408 end loop;
3410 return V;
3411 end if;
3413 elsif Is_Array_Type (T) then
3414 X := First_Index (T);
3415 V := Count_Tasks (Component_Type (T));
3416 while Present (X) loop
3417 C := Etype (X);
3419 if not Is_OK_Static_Subtype (C) then
3420 Check_Restriction (Max_Tasks, N);
3421 return Uint_0;
3422 else
3423 V := V * (UI_Max (Uint_0,
3424 Expr_Value (Type_High_Bound (C)) -
3425 Expr_Value (Type_Low_Bound (C)) + Uint_1));
3426 end if;
3428 Next_Index (X);
3429 end loop;
3431 return V;
3433 else
3434 return Uint_0;
3435 end if;
3436 end Count_Tasks;
3438 ----------------------------
3439 -- Delayed_Aspect_Present --
3440 ----------------------------
3442 function Delayed_Aspect_Present return Boolean is
3443 A : Node_Id;
3444 A_Id : Aspect_Id;
3446 begin
3447 if Present (Aspect_Specifications (N)) then
3448 A := First (Aspect_Specifications (N));
3449 A_Id := Get_Aspect_Id (Chars (Identifier (A)));
3450 while Present (A) loop
3451 if A_Id = Aspect_Alignment or else A_Id = Aspect_Address then
3452 return True;
3453 end if;
3455 Next (A);
3456 end loop;
3457 end if;
3459 return False;
3460 end Delayed_Aspect_Present;
3462 -- Start of processing for Analyze_Object_Declaration
3464 begin
3465 -- There are three kinds of implicit types generated by an
3466 -- object declaration:
3468 -- 1. Those generated by the original Object Definition
3470 -- 2. Those generated by the Expression
3472 -- 3. Those used to constrain the Object Definition with the
3473 -- expression constraints when the definition is unconstrained.
3475 -- They must be generated in this order to avoid order of elaboration
3476 -- issues. Thus the first step (after entering the name) is to analyze
3477 -- the object definition.
3479 if Constant_Present (N) then
3480 Prev_Entity := Current_Entity_In_Scope (Id);
3482 if Present (Prev_Entity)
3483 and then
3484 -- If the homograph is an implicit subprogram, it is overridden
3485 -- by the current declaration.
3487 ((Is_Overloadable (Prev_Entity)
3488 and then Is_Inherited_Operation (Prev_Entity))
3490 -- The current object is a discriminal generated for an entry
3491 -- family index. Even though the index is a constant, in this
3492 -- particular context there is no true constant redeclaration.
3493 -- Enter_Name will handle the visibility.
3495 or else
3496 (Is_Discriminal (Id)
3497 and then Ekind (Discriminal_Link (Id)) =
3498 E_Entry_Index_Parameter)
3500 -- The current object is the renaming for a generic declared
3501 -- within the instance.
3503 or else
3504 (Ekind (Prev_Entity) = E_Package
3505 and then Nkind (Parent (Prev_Entity)) =
3506 N_Package_Renaming_Declaration
3507 and then not Comes_From_Source (Prev_Entity)
3508 and then
3509 Is_Generic_Instance (Renamed_Entity (Prev_Entity))))
3510 then
3511 Prev_Entity := Empty;
3512 end if;
3513 end if;
3515 -- The object declaration may be subject to pragma Ghost with policy
3516 -- Ignore. Set the mode now to ensure that any nodes generated during
3517 -- analysis and expansion are properly flagged as ignored Ghost.
3519 Set_Ghost_Mode (N, Prev_Entity);
3521 if Present (Prev_Entity) then
3522 Constant_Redeclaration (Id, N, T);
3524 Generate_Reference (Prev_Entity, Id, 'c');
3525 Set_Completion_Referenced (Id);
3527 if Error_Posted (N) then
3529 -- Type mismatch or illegal redeclaration, Do not analyze
3530 -- expression to avoid cascaded errors.
3532 T := Find_Type_Of_Object (Object_Definition (N), N);
3533 Set_Etype (Id, T);
3534 Set_Ekind (Id, E_Variable);
3535 goto Leave;
3536 end if;
3538 -- In the normal case, enter identifier at the start to catch premature
3539 -- usage in the initialization expression.
3541 else
3542 Generate_Definition (Id);
3543 Enter_Name (Id);
3545 Mark_Coextensions (N, Object_Definition (N));
3547 T := Find_Type_Of_Object (Object_Definition (N), N);
3549 if Nkind (Object_Definition (N)) = N_Access_Definition
3550 and then Present
3551 (Access_To_Subprogram_Definition (Object_Definition (N)))
3552 and then Protected_Present
3553 (Access_To_Subprogram_Definition (Object_Definition (N)))
3554 then
3555 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
3556 end if;
3558 if Error_Posted (Id) then
3559 Set_Etype (Id, T);
3560 Set_Ekind (Id, E_Variable);
3561 goto Leave;
3562 end if;
3563 end if;
3565 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
3566 -- out some static checks
3568 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (T) then
3570 -- In case of aggregates we must also take care of the correct
3571 -- initialization of nested aggregates bug this is done at the
3572 -- point of the analysis of the aggregate (see sem_aggr.adb).
3574 if Present (Expression (N))
3575 and then Nkind (Expression (N)) = N_Aggregate
3576 then
3577 null;
3579 else
3580 declare
3581 Save_Typ : constant Entity_Id := Etype (Id);
3582 begin
3583 Set_Etype (Id, T); -- Temp. decoration for static checks
3584 Null_Exclusion_Static_Checks (N);
3585 Set_Etype (Id, Save_Typ);
3586 end;
3587 end if;
3588 end if;
3590 -- Object is marked pure if it is in a pure scope
3592 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3594 -- If deferred constant, make sure context is appropriate. We detect
3595 -- a deferred constant as a constant declaration with no expression.
3596 -- A deferred constant can appear in a package body if its completion
3597 -- is by means of an interface pragma.
3599 if Constant_Present (N) and then No (E) then
3601 -- A deferred constant may appear in the declarative part of the
3602 -- following constructs:
3604 -- blocks
3605 -- entry bodies
3606 -- extended return statements
3607 -- package specs
3608 -- package bodies
3609 -- subprogram bodies
3610 -- task bodies
3612 -- When declared inside a package spec, a deferred constant must be
3613 -- completed by a full constant declaration or pragma Import. In all
3614 -- other cases, the only proper completion is pragma Import. Extended
3615 -- return statements are flagged as invalid contexts because they do
3616 -- not have a declarative part and so cannot accommodate the pragma.
3618 if Ekind (Current_Scope) = E_Return_Statement then
3619 Error_Msg_N
3620 ("invalid context for deferred constant declaration (RM 7.4)",
3622 Error_Msg_N
3623 ("\declaration requires an initialization expression",
3625 Set_Constant_Present (N, False);
3627 -- In Ada 83, deferred constant must be of private type
3629 elsif not Is_Private_Type (T) then
3630 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
3631 Error_Msg_N
3632 ("(Ada 83) deferred constant must be private type", N);
3633 end if;
3634 end if;
3636 -- If not a deferred constant, then the object declaration freezes
3637 -- its type, unless the object is of an anonymous type and has delayed
3638 -- aspects. In that case the type is frozen when the object itself is.
3640 else
3641 Check_Fully_Declared (T, N);
3643 if Has_Delayed_Aspects (Id)
3644 and then Is_Array_Type (T)
3645 and then Is_Itype (T)
3646 then
3647 Set_Has_Delayed_Freeze (T);
3648 else
3649 Freeze_Before (N, T);
3650 end if;
3651 end if;
3653 -- If the object was created by a constrained array definition, then
3654 -- set the link in both the anonymous base type and anonymous subtype
3655 -- that are built to represent the array type to point to the object.
3657 if Nkind (Object_Definition (Declaration_Node (Id))) =
3658 N_Constrained_Array_Definition
3659 then
3660 Set_Related_Array_Object (T, Id);
3661 Set_Related_Array_Object (Base_Type (T), Id);
3662 end if;
3664 -- Special checks for protected objects not at library level
3666 if Is_Protected_Type (T)
3667 and then not Is_Library_Level_Entity (Id)
3668 then
3669 Check_Restriction (No_Local_Protected_Objects, Id);
3671 -- Protected objects with interrupt handlers must be at library level
3673 -- Ada 2005: This test is not needed (and the corresponding clause
3674 -- in the RM is removed) because accessibility checks are sufficient
3675 -- to make handlers not at the library level illegal.
3677 -- AI05-0303: The AI is in fact a binding interpretation, and thus
3678 -- applies to the '95 version of the language as well.
3680 if Has_Interrupt_Handler (T) and then Ada_Version < Ada_95 then
3681 Error_Msg_N
3682 ("interrupt object can only be declared at library level", Id);
3683 end if;
3684 end if;
3686 -- The actual subtype of the object is the nominal subtype, unless
3687 -- the nominal one is unconstrained and obtained from the expression.
3689 Act_T := T;
3691 -- These checks should be performed before the initialization expression
3692 -- is considered, so that the Object_Definition node is still the same
3693 -- as in source code.
3695 -- In SPARK, the nominal subtype is always given by a subtype mark
3696 -- and must not be unconstrained. (The only exception to this is the
3697 -- acceptance of declarations of constants of type String.)
3699 if not Nkind_In (Object_Definition (N), N_Expanded_Name, N_Identifier)
3700 then
3701 Check_SPARK_05_Restriction
3702 ("subtype mark required", Object_Definition (N));
3704 elsif Is_Array_Type (T)
3705 and then not Is_Constrained (T)
3706 and then T /= Standard_String
3707 then
3708 Check_SPARK_05_Restriction
3709 ("subtype mark of constrained type expected",
3710 Object_Definition (N));
3711 end if;
3713 -- There are no aliased objects in SPARK
3715 if Aliased_Present (N) then
3716 Check_SPARK_05_Restriction ("aliased object is not allowed", N);
3717 end if;
3719 -- Process initialization expression if present and not in error
3721 if Present (E) and then E /= Error then
3723 -- Generate an error in case of CPP class-wide object initialization.
3724 -- Required because otherwise the expansion of the class-wide
3725 -- assignment would try to use 'size to initialize the object
3726 -- (primitive that is not available in CPP tagged types).
3728 if Is_Class_Wide_Type (Act_T)
3729 and then
3730 (Is_CPP_Class (Root_Type (Etype (Act_T)))
3731 or else
3732 (Present (Full_View (Root_Type (Etype (Act_T))))
3733 and then
3734 Is_CPP_Class (Full_View (Root_Type (Etype (Act_T))))))
3735 then
3736 Error_Msg_N
3737 ("predefined assignment not available for 'C'P'P tagged types",
3739 end if;
3741 Mark_Coextensions (N, E);
3742 Analyze (E);
3744 -- In case of errors detected in the analysis of the expression,
3745 -- decorate it with the expected type to avoid cascaded errors
3747 if No (Etype (E)) then
3748 Set_Etype (E, T);
3749 end if;
3751 -- If an initialization expression is present, then we set the
3752 -- Is_True_Constant flag. It will be reset if this is a variable
3753 -- and it is indeed modified.
3755 Set_Is_True_Constant (Id, True);
3757 -- If we are analyzing a constant declaration, set its completion
3758 -- flag after analyzing and resolving the expression.
3760 if Constant_Present (N) then
3761 Set_Has_Completion (Id);
3762 end if;
3764 -- Set type and resolve (type may be overridden later on). Note:
3765 -- Ekind (Id) must still be E_Void at this point so that incorrect
3766 -- early usage within E is properly diagnosed.
3768 Set_Etype (Id, T);
3770 -- If the expression is an aggregate we must look ahead to detect
3771 -- the possible presence of an address clause, and defer resolution
3772 -- and expansion of the aggregate to the freeze point of the entity.
3774 if Comes_From_Source (N)
3775 and then Expander_Active
3776 and then Nkind (E) = N_Aggregate
3777 and then (Present (Following_Address_Clause (N))
3778 or else Delayed_Aspect_Present)
3779 then
3780 Set_Etype (E, T);
3782 else
3783 Resolve (E, T);
3784 end if;
3786 -- No further action needed if E is a call to an inlined function
3787 -- which returns an unconstrained type and it has been expanded into
3788 -- a procedure call. In that case N has been replaced by an object
3789 -- declaration without initializing expression and it has been
3790 -- analyzed (see Expand_Inlined_Call).
3792 if Back_End_Inlining
3793 and then Expander_Active
3794 and then Nkind (E) = N_Function_Call
3795 and then Nkind (Name (E)) in N_Has_Entity
3796 and then Is_Inlined (Entity (Name (E)))
3797 and then not Is_Constrained (Etype (E))
3798 and then Analyzed (N)
3799 and then No (Expression (N))
3800 then
3801 return;
3802 end if;
3804 -- If E is null and has been replaced by an N_Raise_Constraint_Error
3805 -- node (which was marked already-analyzed), we need to set the type
3806 -- to something other than Any_Access in order to keep gigi happy.
3808 if Etype (E) = Any_Access then
3809 Set_Etype (E, T);
3810 end if;
3812 -- If the object is an access to variable, the initialization
3813 -- expression cannot be an access to constant.
3815 if Is_Access_Type (T)
3816 and then not Is_Access_Constant (T)
3817 and then Is_Access_Type (Etype (E))
3818 and then Is_Access_Constant (Etype (E))
3819 then
3820 Error_Msg_N
3821 ("access to variable cannot be initialized with an "
3822 & "access-to-constant expression", E);
3823 end if;
3825 if not Assignment_OK (N) then
3826 Check_Initialization (T, E);
3827 end if;
3829 Check_Unset_Reference (E);
3831 -- If this is a variable, then set current value. If this is a
3832 -- declared constant of a scalar type with a static expression,
3833 -- indicate that it is always valid.
3835 if not Constant_Present (N) then
3836 if Compile_Time_Known_Value (E) then
3837 Set_Current_Value (Id, E);
3838 end if;
3840 elsif Is_Scalar_Type (T) and then Is_OK_Static_Expression (E) then
3841 Set_Is_Known_Valid (Id);
3842 end if;
3844 -- Deal with setting of null flags
3846 if Is_Access_Type (T) then
3847 if Known_Non_Null (E) then
3848 Set_Is_Known_Non_Null (Id, True);
3849 elsif Known_Null (E) and then not Can_Never_Be_Null (Id) then
3850 Set_Is_Known_Null (Id, True);
3851 end if;
3852 end if;
3854 -- Check incorrect use of dynamically tagged expressions
3856 if Is_Tagged_Type (T) then
3857 Check_Dynamically_Tagged_Expression
3858 (Expr => E,
3859 Typ => T,
3860 Related_Nod => N);
3861 end if;
3863 Apply_Scalar_Range_Check (E, T);
3864 Apply_Static_Length_Check (E, T);
3866 if Nkind (Original_Node (N)) = N_Object_Declaration
3867 and then Comes_From_Source (Original_Node (N))
3869 -- Only call test if needed
3871 and then Restriction_Check_Required (SPARK_05)
3872 and then not Is_SPARK_05_Initialization_Expr (Original_Node (E))
3873 then
3874 Check_SPARK_05_Restriction
3875 ("initialization expression is not appropriate", E);
3876 end if;
3878 -- A formal parameter of a specific tagged type whose related
3879 -- subprogram is subject to pragma Extensions_Visible with value
3880 -- "False" cannot be implicitly converted to a class-wide type by
3881 -- means of an initialization expression (SPARK RM 6.1.7(3)).
3883 if Is_Class_Wide_Type (T) and then Is_EVF_Expression (E) then
3884 Error_Msg_N
3885 ("formal parameter with Extensions_Visible False cannot be "
3886 & "implicitly converted to class-wide type", E);
3887 end if;
3888 end if;
3890 -- If the No_Streams restriction is set, check that the type of the
3891 -- object is not, and does not contain, any subtype derived from
3892 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
3893 -- Has_Stream just for efficiency reasons. There is no point in
3894 -- spending time on a Has_Stream check if the restriction is not set.
3896 if Restriction_Check_Required (No_Streams) then
3897 if Has_Stream (T) then
3898 Check_Restriction (No_Streams, N);
3899 end if;
3900 end if;
3902 -- Deal with predicate check before we start to do major rewriting. It
3903 -- is OK to initialize and then check the initialized value, since the
3904 -- object goes out of scope if we get a predicate failure. Note that we
3905 -- do this in the analyzer and not the expander because the analyzer
3906 -- does some substantial rewriting in some cases.
3908 -- We need a predicate check if the type has predicates, and if either
3909 -- there is an initializing expression, or for default initialization
3910 -- when we have at least one case of an explicit default initial value
3911 -- and then this is not an internal declaration whose initialization
3912 -- comes later (as for an aggregate expansion).
3914 if not Suppress_Assignment_Checks (N)
3915 and then Present (Predicate_Function (T))
3916 and then not No_Initialization (N)
3917 and then
3918 (Present (E)
3919 or else
3920 Is_Partially_Initialized_Type (T, Include_Implicit => False))
3921 then
3922 -- If the type has a static predicate and the expression is known at
3923 -- compile time, see if the expression satisfies the predicate.
3925 if Present (E) then
3926 Check_Expression_Against_Static_Predicate (E, T);
3927 end if;
3929 Insert_After (N,
3930 Make_Predicate_Check (T, New_Occurrence_Of (Id, Loc)));
3931 end if;
3933 -- Case of unconstrained type
3935 if Is_Indefinite_Subtype (T) then
3937 -- In SPARK, a declaration of unconstrained type is allowed
3938 -- only for constants of type string.
3940 if Is_String_Type (T) and then not Constant_Present (N) then
3941 Check_SPARK_05_Restriction
3942 ("declaration of object of unconstrained type not allowed", N);
3943 end if;
3945 -- Nothing to do in deferred constant case
3947 if Constant_Present (N) and then No (E) then
3948 null;
3950 -- Case of no initialization present
3952 elsif No (E) then
3953 if No_Initialization (N) then
3954 null;
3956 elsif Is_Class_Wide_Type (T) then
3957 Error_Msg_N
3958 ("initialization required in class-wide declaration ", N);
3960 else
3961 Error_Msg_N
3962 ("unconstrained subtype not allowed (need initialization)",
3963 Object_Definition (N));
3965 if Is_Record_Type (T) and then Has_Discriminants (T) then
3966 Error_Msg_N
3967 ("\provide initial value or explicit discriminant values",
3968 Object_Definition (N));
3970 Error_Msg_NE
3971 ("\or give default discriminant values for type&",
3972 Object_Definition (N), T);
3974 elsif Is_Array_Type (T) then
3975 Error_Msg_N
3976 ("\provide initial value or explicit array bounds",
3977 Object_Definition (N));
3978 end if;
3979 end if;
3981 -- Case of initialization present but in error. Set initial
3982 -- expression as absent (but do not make above complaints)
3984 elsif E = Error then
3985 Set_Expression (N, Empty);
3986 E := Empty;
3988 -- Case of initialization present
3990 else
3991 -- Check restrictions in Ada 83
3993 if not Constant_Present (N) then
3995 -- Unconstrained variables not allowed in Ada 83 mode
3997 if Ada_Version = Ada_83
3998 and then Comes_From_Source (Object_Definition (N))
3999 then
4000 Error_Msg_N
4001 ("(Ada 83) unconstrained variable not allowed",
4002 Object_Definition (N));
4003 end if;
4004 end if;
4006 -- Now we constrain the variable from the initializing expression
4008 -- If the expression is an aggregate, it has been expanded into
4009 -- individual assignments. Retrieve the actual type from the
4010 -- expanded construct.
4012 if Is_Array_Type (T)
4013 and then No_Initialization (N)
4014 and then Nkind (Original_Node (E)) = N_Aggregate
4015 then
4016 Act_T := Etype (E);
4018 -- In case of class-wide interface object declarations we delay
4019 -- the generation of the equivalent record type declarations until
4020 -- its expansion because there are cases in they are not required.
4022 elsif Is_Interface (T) then
4023 null;
4025 -- In GNATprove mode, Expand_Subtype_From_Expr does nothing. Thus,
4026 -- we should prevent the generation of another Itype with the
4027 -- same name as the one already generated, or we end up with
4028 -- two identical types in GNATprove.
4030 elsif GNATprove_Mode then
4031 null;
4033 -- If the type is an unchecked union, no subtype can be built from
4034 -- the expression. Rewrite declaration as a renaming, which the
4035 -- back-end can handle properly. This is a rather unusual case,
4036 -- because most unchecked_union declarations have default values
4037 -- for discriminants and are thus not indefinite.
4039 elsif Is_Unchecked_Union (T) then
4040 if Constant_Present (N) or else Nkind (E) = N_Function_Call then
4041 Set_Ekind (Id, E_Constant);
4042 else
4043 Set_Ekind (Id, E_Variable);
4044 end if;
4046 -- An object declared within a Ghost region is automatically
4047 -- Ghost (SPARK RM 6.9(2)).
4049 if Comes_From_Source (Id) and then Ghost_Mode > None then
4050 Set_Is_Ghost_Entity (Id);
4052 -- The Ghost policy in effect at the point of declaration
4053 -- and at the point of completion must match
4054 -- (SPARK RM 6.9(15)).
4056 if Present (Prev_Entity)
4057 and then Is_Ghost_Entity (Prev_Entity)
4058 then
4059 Check_Ghost_Completion (Prev_Entity, Id);
4060 end if;
4061 end if;
4063 Rewrite (N,
4064 Make_Object_Renaming_Declaration (Loc,
4065 Defining_Identifier => Id,
4066 Subtype_Mark => New_Occurrence_Of (T, Loc),
4067 Name => E));
4069 Set_Renamed_Object (Id, E);
4070 Freeze_Before (N, T);
4071 Set_Is_Frozen (Id);
4072 return;
4074 else
4075 Expand_Subtype_From_Expr (N, T, Object_Definition (N), E);
4076 Act_T := Find_Type_Of_Object (Object_Definition (N), N);
4077 end if;
4079 Set_Is_Constr_Subt_For_U_Nominal (Act_T);
4081 if Aliased_Present (N) then
4082 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4083 end if;
4085 Freeze_Before (N, Act_T);
4086 Freeze_Before (N, T);
4087 end if;
4089 elsif Is_Array_Type (T)
4090 and then No_Initialization (N)
4091 and then Nkind (Original_Node (E)) = N_Aggregate
4092 then
4093 if not Is_Entity_Name (Object_Definition (N)) then
4094 Act_T := Etype (E);
4095 Check_Compile_Time_Size (Act_T);
4097 if Aliased_Present (N) then
4098 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4099 end if;
4100 end if;
4102 -- When the given object definition and the aggregate are specified
4103 -- independently, and their lengths might differ do a length check.
4104 -- This cannot happen if the aggregate is of the form (others =>...)
4106 if not Is_Constrained (T) then
4107 null;
4109 elsif Nkind (E) = N_Raise_Constraint_Error then
4111 -- Aggregate is statically illegal. Place back in declaration
4113 Set_Expression (N, E);
4114 Set_No_Initialization (N, False);
4116 elsif T = Etype (E) then
4117 null;
4119 elsif Nkind (E) = N_Aggregate
4120 and then Present (Component_Associations (E))
4121 and then Present (Choices (First (Component_Associations (E))))
4122 and then Nkind (First
4123 (Choices (First (Component_Associations (E))))) = N_Others_Choice
4124 then
4125 null;
4127 else
4128 Apply_Length_Check (E, T);
4129 end if;
4131 -- If the type is limited unconstrained with defaulted discriminants and
4132 -- there is no expression, then the object is constrained by the
4133 -- defaults, so it is worthwhile building the corresponding subtype.
4135 elsif (Is_Limited_Record (T) or else Is_Concurrent_Type (T))
4136 and then not Is_Constrained (T)
4137 and then Has_Discriminants (T)
4138 then
4139 if No (E) then
4140 Act_T := Build_Default_Subtype (T, N);
4141 else
4142 -- Ada 2005: A limited object may be initialized by means of an
4143 -- aggregate. If the type has default discriminants it has an
4144 -- unconstrained nominal type, Its actual subtype will be obtained
4145 -- from the aggregate, and not from the default discriminants.
4147 Act_T := Etype (E);
4148 end if;
4150 Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
4152 elsif Nkind (E) = N_Function_Call
4153 and then Constant_Present (N)
4154 and then Has_Unconstrained_Elements (Etype (E))
4155 then
4156 -- The back-end has problems with constants of a discriminated type
4157 -- with defaults, if the initial value is a function call. We
4158 -- generate an intermediate temporary that will receive a reference
4159 -- to the result of the call. The initialization expression then
4160 -- becomes a dereference of that temporary.
4162 Remove_Side_Effects (E);
4164 -- If this is a constant declaration of an unconstrained type and
4165 -- the initialization is an aggregate, we can use the subtype of the
4166 -- aggregate for the declared entity because it is immutable.
4168 elsif not Is_Constrained (T)
4169 and then Has_Discriminants (T)
4170 and then Constant_Present (N)
4171 and then not Has_Unchecked_Union (T)
4172 and then Nkind (E) = N_Aggregate
4173 then
4174 Act_T := Etype (E);
4175 end if;
4177 -- Check No_Wide_Characters restriction
4179 Check_Wide_Character_Restriction (T, Object_Definition (N));
4181 -- Indicate this is not set in source. Certainly true for constants, and
4182 -- true for variables so far (will be reset for a variable if and when
4183 -- we encounter a modification in the source).
4185 Set_Never_Set_In_Source (Id);
4187 -- Now establish the proper kind and type of the object
4189 if Constant_Present (N) then
4190 Set_Ekind (Id, E_Constant);
4191 Set_Is_True_Constant (Id);
4193 else
4194 Set_Ekind (Id, E_Variable);
4196 -- A variable is set as shared passive if it appears in a shared
4197 -- passive package, and is at the outer level. This is not done for
4198 -- entities generated during expansion, because those are always
4199 -- manipulated locally.
4201 if Is_Shared_Passive (Current_Scope)
4202 and then Is_Library_Level_Entity (Id)
4203 and then Comes_From_Source (Id)
4204 then
4205 Set_Is_Shared_Passive (Id);
4206 Check_Shared_Var (Id, T, N);
4207 end if;
4209 -- Set Has_Initial_Value if initializing expression present. Note
4210 -- that if there is no initializing expression, we leave the state
4211 -- of this flag unchanged (usually it will be False, but notably in
4212 -- the case of exception choice variables, it will already be true).
4214 if Present (E) then
4215 Set_Has_Initial_Value (Id);
4216 end if;
4217 end if;
4219 -- Initialize alignment and size and capture alignment setting
4221 Init_Alignment (Id);
4222 Init_Esize (Id);
4223 Set_Optimize_Alignment_Flags (Id);
4225 -- An object declared within a Ghost region is automatically Ghost
4226 -- (SPARK RM 6.9(2)).
4228 if Comes_From_Source (Id)
4229 and then (Ghost_Mode > None
4230 or else (Present (Prev_Entity)
4231 and then Is_Ghost_Entity (Prev_Entity)))
4232 then
4233 Set_Is_Ghost_Entity (Id);
4235 -- The Ghost policy in effect at the point of declaration and at the
4236 -- point of completion must match (SPARK RM 6.9(16)).
4238 if Present (Prev_Entity) and then Is_Ghost_Entity (Prev_Entity) then
4239 Check_Ghost_Completion (Prev_Entity, Id);
4240 end if;
4241 end if;
4243 -- Deal with aliased case
4245 if Aliased_Present (N) then
4246 Set_Is_Aliased (Id);
4248 -- If the object is aliased and the type is unconstrained with
4249 -- defaulted discriminants and there is no expression, then the
4250 -- object is constrained by the defaults, so it is worthwhile
4251 -- building the corresponding subtype.
4253 -- Ada 2005 (AI-363): If the aliased object is discriminated and
4254 -- unconstrained, then only establish an actual subtype if the
4255 -- nominal subtype is indefinite. In definite cases the object is
4256 -- unconstrained in Ada 2005.
4258 if No (E)
4259 and then Is_Record_Type (T)
4260 and then not Is_Constrained (T)
4261 and then Has_Discriminants (T)
4262 and then (Ada_Version < Ada_2005 or else Is_Indefinite_Subtype (T))
4263 then
4264 Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
4265 end if;
4266 end if;
4268 -- Now we can set the type of the object
4270 Set_Etype (Id, Act_T);
4272 -- Non-constant object is marked to be treated as volatile if type is
4273 -- volatile and we clear the Current_Value setting that may have been
4274 -- set above. Doing so for constants isn't required and might interfere
4275 -- with possible uses of the object as a static expression in contexts
4276 -- incompatible with volatility (e.g. as a case-statement alternative).
4278 if Ekind (Id) /= E_Constant and then Treat_As_Volatile (Etype (Id)) then
4279 Set_Treat_As_Volatile (Id);
4280 Set_Current_Value (Id, Empty);
4281 end if;
4283 -- Deal with controlled types
4285 if Has_Controlled_Component (Etype (Id))
4286 or else Is_Controlled (Etype (Id))
4287 then
4288 if not Is_Library_Level_Entity (Id) then
4289 Check_Restriction (No_Nested_Finalization, N);
4290 else
4291 Validate_Controlled_Object (Id);
4292 end if;
4293 end if;
4295 if Has_Task (Etype (Id)) then
4296 Check_Restriction (No_Tasking, N);
4298 -- Deal with counting max tasks
4300 -- Nothing to do if inside a generic
4302 if Inside_A_Generic then
4303 null;
4305 -- If library level entity, then count tasks
4307 elsif Is_Library_Level_Entity (Id) then
4308 Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
4310 -- If not library level entity, then indicate we don't know max
4311 -- tasks and also check task hierarchy restriction and blocking
4312 -- operation (since starting a task is definitely blocking).
4314 else
4315 Check_Restriction (Max_Tasks, N);
4316 Check_Restriction (No_Task_Hierarchy, N);
4317 Check_Potentially_Blocking_Operation (N);
4318 end if;
4320 -- A rather specialized test. If we see two tasks being declared
4321 -- of the same type in the same object declaration, and the task
4322 -- has an entry with an address clause, we know that program error
4323 -- will be raised at run time since we can't have two tasks with
4324 -- entries at the same address.
4326 if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
4327 declare
4328 E : Entity_Id;
4330 begin
4331 E := First_Entity (Etype (Id));
4332 while Present (E) loop
4333 if Ekind (E) = E_Entry
4334 and then Present (Get_Attribute_Definition_Clause
4335 (E, Attribute_Address))
4336 then
4337 Error_Msg_Warn := SPARK_Mode /= On;
4338 Error_Msg_N
4339 ("more than one task with same entry address<<", N);
4340 Error_Msg_N ("\Program_Error [<<", N);
4341 Insert_Action (N,
4342 Make_Raise_Program_Error (Loc,
4343 Reason => PE_Duplicated_Entry_Address));
4344 exit;
4345 end if;
4347 Next_Entity (E);
4348 end loop;
4349 end;
4350 end if;
4351 end if;
4353 -- Some simple constant-propagation: if the expression is a constant
4354 -- string initialized with a literal, share the literal. This avoids
4355 -- a run-time copy.
4357 if Present (E)
4358 and then Is_Entity_Name (E)
4359 and then Ekind (Entity (E)) = E_Constant
4360 and then Base_Type (Etype (E)) = Standard_String
4361 then
4362 declare
4363 Val : constant Node_Id := Constant_Value (Entity (E));
4364 begin
4365 if Present (Val) and then Nkind (Val) = N_String_Literal then
4366 Rewrite (E, New_Copy (Val));
4367 end if;
4368 end;
4369 end if;
4371 -- Another optimization: if the nominal subtype is unconstrained and
4372 -- the expression is a function call that returns an unconstrained
4373 -- type, rewrite the declaration as a renaming of the result of the
4374 -- call. The exceptions below are cases where the copy is expected,
4375 -- either by the back end (Aliased case) or by the semantics, as for
4376 -- initializing controlled types or copying tags for classwide types.
4378 if Present (E)
4379 and then Nkind (E) = N_Explicit_Dereference
4380 and then Nkind (Original_Node (E)) = N_Function_Call
4381 and then not Is_Library_Level_Entity (Id)
4382 and then not Is_Constrained (Underlying_Type (T))
4383 and then not Is_Aliased (Id)
4384 and then not Is_Class_Wide_Type (T)
4385 and then not Is_Controlled (T)
4386 and then not Has_Controlled_Component (Base_Type (T))
4387 and then Expander_Active
4388 then
4389 Rewrite (N,
4390 Make_Object_Renaming_Declaration (Loc,
4391 Defining_Identifier => Id,
4392 Access_Definition => Empty,
4393 Subtype_Mark => New_Occurrence_Of
4394 (Base_Type (Etype (Id)), Loc),
4395 Name => E));
4397 Set_Renamed_Object (Id, E);
4399 -- Force generation of debugging information for the constant and for
4400 -- the renamed function call.
4402 Set_Debug_Info_Needed (Id);
4403 Set_Debug_Info_Needed (Entity (Prefix (E)));
4404 end if;
4406 if Present (Prev_Entity)
4407 and then Is_Frozen (Prev_Entity)
4408 and then not Error_Posted (Id)
4409 then
4410 Error_Msg_N ("full constant declaration appears too late", N);
4411 end if;
4413 Check_Eliminated (Id);
4415 -- Deal with setting In_Private_Part flag if in private part
4417 if Ekind (Scope (Id)) = E_Package and then In_Private_Part (Scope (Id))
4418 then
4419 Set_In_Private_Part (Id);
4420 end if;
4422 -- Check for violation of No_Local_Timing_Events
4424 if Restriction_Check_Required (No_Local_Timing_Events)
4425 and then not Is_Library_Level_Entity (Id)
4426 and then Is_RTE (Etype (Id), RE_Timing_Event)
4427 then
4428 Check_Restriction (No_Local_Timing_Events, N);
4429 end if;
4431 <<Leave>>
4432 -- Initialize the refined state of a variable here because this is a
4433 -- common destination for legal and illegal object declarations.
4435 if Ekind (Id) = E_Variable then
4436 Set_Encapsulating_State (Id, Empty);
4437 end if;
4439 if Has_Aspects (N) then
4440 Analyze_Aspect_Specifications (N, Id);
4441 end if;
4443 Analyze_Dimension (N);
4445 -- Verify whether the object declaration introduces an illegal hidden
4446 -- state within a package subject to a null abstract state.
4448 if Ekind (Id) = E_Variable then
4449 Check_No_Hidden_State (Id);
4450 end if;
4451 end Analyze_Object_Declaration;
4453 ---------------------------
4454 -- Analyze_Others_Choice --
4455 ---------------------------
4457 -- Nothing to do for the others choice node itself, the semantic analysis
4458 -- of the others choice will occur as part of the processing of the parent
4460 procedure Analyze_Others_Choice (N : Node_Id) is
4461 pragma Warnings (Off, N);
4462 begin
4463 null;
4464 end Analyze_Others_Choice;
4466 -------------------------------------------
4467 -- Analyze_Private_Extension_Declaration --
4468 -------------------------------------------
4470 procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
4471 T : constant Entity_Id := Defining_Identifier (N);
4472 Indic : constant Node_Id := Subtype_Indication (N);
4473 Parent_Type : Entity_Id;
4474 Parent_Base : Entity_Id;
4476 begin
4477 -- The private extension declaration may be subject to pragma Ghost with
4478 -- policy Ignore. Set the mode now to ensure that any nodes generated
4479 -- during analysis and expansion are properly flagged as ignored Ghost.
4481 Set_Ghost_Mode (N);
4483 -- Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
4485 if Is_Non_Empty_List (Interface_List (N)) then
4486 declare
4487 Intf : Node_Id;
4488 T : Entity_Id;
4490 begin
4491 Intf := First (Interface_List (N));
4492 while Present (Intf) loop
4493 T := Find_Type_Of_Subtype_Indic (Intf);
4495 Diagnose_Interface (Intf, T);
4496 Next (Intf);
4497 end loop;
4498 end;
4499 end if;
4501 Generate_Definition (T);
4503 -- For other than Ada 2012, just enter the name in the current scope
4505 if Ada_Version < Ada_2012 then
4506 Enter_Name (T);
4508 -- Ada 2012 (AI05-0162): Enter the name in the current scope handling
4509 -- case of private type that completes an incomplete type.
4511 else
4512 declare
4513 Prev : Entity_Id;
4515 begin
4516 Prev := Find_Type_Name (N);
4518 pragma Assert (Prev = T
4519 or else (Ekind (Prev) = E_Incomplete_Type
4520 and then Present (Full_View (Prev))
4521 and then Full_View (Prev) = T));
4522 end;
4523 end if;
4525 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
4526 Parent_Base := Base_Type (Parent_Type);
4528 if Parent_Type = Any_Type or else Etype (Parent_Type) = Any_Type then
4529 Set_Ekind (T, Ekind (Parent_Type));
4530 Set_Etype (T, Any_Type);
4531 goto Leave;
4533 elsif not Is_Tagged_Type (Parent_Type) then
4534 Error_Msg_N
4535 ("parent of type extension must be a tagged type ", Indic);
4536 goto Leave;
4538 elsif Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
4539 Error_Msg_N ("premature derivation of incomplete type", Indic);
4540 goto Leave;
4542 elsif Is_Concurrent_Type (Parent_Type) then
4543 Error_Msg_N
4544 ("parent type of a private extension cannot be "
4545 & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
4547 Set_Etype (T, Any_Type);
4548 Set_Ekind (T, E_Limited_Private_Type);
4549 Set_Private_Dependents (T, New_Elmt_List);
4550 Set_Error_Posted (T);
4551 goto Leave;
4552 end if;
4554 -- Perhaps the parent type should be changed to the class-wide type's
4555 -- specific type in this case to prevent cascading errors ???
4557 if Is_Class_Wide_Type (Parent_Type) then
4558 Error_Msg_N
4559 ("parent of type extension must not be a class-wide type", Indic);
4560 goto Leave;
4561 end if;
4563 if (not Is_Package_Or_Generic_Package (Current_Scope)
4564 and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
4565 or else In_Private_Part (Current_Scope)
4567 then
4568 Error_Msg_N ("invalid context for private extension", N);
4569 end if;
4571 -- Set common attributes
4573 Set_Is_Pure (T, Is_Pure (Current_Scope));
4574 Set_Scope (T, Current_Scope);
4575 Set_Ekind (T, E_Record_Type_With_Private);
4576 Init_Size_Align (T);
4577 Set_Default_SSO (T);
4579 Set_Etype (T, Parent_Base);
4580 Set_Has_Task (T, Has_Task (Parent_Base));
4581 Set_Has_Protected (T, Has_Task (Parent_Base));
4583 Set_Convention (T, Convention (Parent_Type));
4584 Set_First_Rep_Item (T, First_Rep_Item (Parent_Type));
4585 Set_Is_First_Subtype (T);
4586 Make_Class_Wide_Type (T);
4588 if Unknown_Discriminants_Present (N) then
4589 Set_Discriminant_Constraint (T, No_Elist);
4590 end if;
4592 Build_Derived_Record_Type (N, Parent_Type, T);
4594 -- Propagate inherited invariant information. The new type has
4595 -- invariants, if the parent type has inheritable invariants,
4596 -- and these invariants can in turn be inherited.
4598 if Has_Inheritable_Invariants (Parent_Type) then
4599 Set_Has_Inheritable_Invariants (T);
4600 Set_Has_Invariants (T);
4601 end if;
4603 -- Ada 2005 (AI-443): Synchronized private extension or a rewritten
4604 -- synchronized formal derived type.
4606 if Ada_Version >= Ada_2005 and then Synchronized_Present (N) then
4607 Set_Is_Limited_Record (T);
4609 -- Formal derived type case
4611 if Is_Generic_Type (T) then
4613 -- The parent must be a tagged limited type or a synchronized
4614 -- interface.
4616 if (not Is_Tagged_Type (Parent_Type)
4617 or else not Is_Limited_Type (Parent_Type))
4618 and then
4619 (not Is_Interface (Parent_Type)
4620 or else not Is_Synchronized_Interface (Parent_Type))
4621 then
4622 Error_Msg_NE ("parent type of & must be tagged limited " &
4623 "or synchronized", N, T);
4624 end if;
4626 -- The progenitors (if any) must be limited or synchronized
4627 -- interfaces.
4629 if Present (Interfaces (T)) then
4630 declare
4631 Iface : Entity_Id;
4632 Iface_Elmt : Elmt_Id;
4634 begin
4635 Iface_Elmt := First_Elmt (Interfaces (T));
4636 while Present (Iface_Elmt) loop
4637 Iface := Node (Iface_Elmt);
4639 if not Is_Limited_Interface (Iface)
4640 and then not Is_Synchronized_Interface (Iface)
4641 then
4642 Error_Msg_NE ("progenitor & must be limited " &
4643 "or synchronized", N, Iface);
4644 end if;
4646 Next_Elmt (Iface_Elmt);
4647 end loop;
4648 end;
4649 end if;
4651 -- Regular derived extension, the parent must be a limited or
4652 -- synchronized interface.
4654 else
4655 if not Is_Interface (Parent_Type)
4656 or else (not Is_Limited_Interface (Parent_Type)
4657 and then not Is_Synchronized_Interface (Parent_Type))
4658 then
4659 Error_Msg_NE
4660 ("parent type of & must be limited interface", N, T);
4661 end if;
4662 end if;
4664 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
4665 -- extension with a synchronized parent must be explicitly declared
4666 -- synchronized, because the full view will be a synchronized type.
4667 -- This must be checked before the check for limited types below,
4668 -- to ensure that types declared limited are not allowed to extend
4669 -- synchronized interfaces.
4671 elsif Is_Interface (Parent_Type)
4672 and then Is_Synchronized_Interface (Parent_Type)
4673 and then not Synchronized_Present (N)
4674 then
4675 Error_Msg_NE
4676 ("private extension of& must be explicitly synchronized",
4677 N, Parent_Type);
4679 elsif Limited_Present (N) then
4680 Set_Is_Limited_Record (T);
4682 if not Is_Limited_Type (Parent_Type)
4683 and then
4684 (not Is_Interface (Parent_Type)
4685 or else not Is_Limited_Interface (Parent_Type))
4686 then
4687 Error_Msg_NE ("parent type& of limited extension must be limited",
4688 N, Parent_Type);
4689 end if;
4690 end if;
4692 <<Leave>>
4693 if Has_Aspects (N) then
4694 Analyze_Aspect_Specifications (N, T);
4695 end if;
4696 end Analyze_Private_Extension_Declaration;
4698 ---------------------------------
4699 -- Analyze_Subtype_Declaration --
4700 ---------------------------------
4702 procedure Analyze_Subtype_Declaration
4703 (N : Node_Id;
4704 Skip : Boolean := False)
4706 Id : constant Entity_Id := Defining_Identifier (N);
4707 T : Entity_Id;
4708 R_Checks : Check_Result;
4710 begin
4711 -- The subtype declaration may be subject to pragma Ghost with policy
4712 -- Ignore. Set the mode now to ensure that any nodes generated during
4713 -- analysis and expansion are properly flagged as ignored Ghost.
4715 Set_Ghost_Mode (N);
4717 Generate_Definition (Id);
4718 Set_Is_Pure (Id, Is_Pure (Current_Scope));
4719 Init_Size_Align (Id);
4721 -- The following guard condition on Enter_Name is to handle cases where
4722 -- the defining identifier has already been entered into the scope but
4723 -- the declaration as a whole needs to be analyzed.
4725 -- This case in particular happens for derived enumeration types. The
4726 -- derived enumeration type is processed as an inserted enumeration type
4727 -- declaration followed by a rewritten subtype declaration. The defining
4728 -- identifier, however, is entered into the name scope very early in the
4729 -- processing of the original type declaration and therefore needs to be
4730 -- avoided here, when the created subtype declaration is analyzed. (See
4731 -- Build_Derived_Types)
4733 -- This also happens when the full view of a private type is derived
4734 -- type with constraints. In this case the entity has been introduced
4735 -- in the private declaration.
4737 -- Finally this happens in some complex cases when validity checks are
4738 -- enabled, where the same subtype declaration may be analyzed twice.
4739 -- This can happen if the subtype is created by the pre-analysis of
4740 -- an attribute tht gives the range of a loop statement, and the loop
4741 -- itself appears within an if_statement that will be rewritten during
4742 -- expansion.
4744 if Skip
4745 or else (Present (Etype (Id))
4746 and then (Is_Private_Type (Etype (Id))
4747 or else Is_Task_Type (Etype (Id))
4748 or else Is_Rewrite_Substitution (N)))
4749 then
4750 null;
4752 elsif Current_Entity (Id) = Id then
4753 null;
4755 else
4756 Enter_Name (Id);
4757 end if;
4759 T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
4761 -- Class-wide equivalent types of records with unknown discriminants
4762 -- involve the generation of an itype which serves as the private view
4763 -- of a constrained record subtype. In such cases the base type of the
4764 -- current subtype we are processing is the private itype. Use the full
4765 -- of the private itype when decorating various attributes.
4767 if Is_Itype (T)
4768 and then Is_Private_Type (T)
4769 and then Present (Full_View (T))
4770 then
4771 T := Full_View (T);
4772 end if;
4774 -- Inherit common attributes
4776 Set_Is_Volatile (Id, Is_Volatile (T));
4777 Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
4778 Set_Is_Generic_Type (Id, Is_Generic_Type (Base_Type (T)));
4779 Set_Convention (Id, Convention (T));
4781 -- If ancestor has predicates then so does the subtype, and in addition
4782 -- we must delay the freeze to properly arrange predicate inheritance.
4784 -- The Ancestor_Type test is really unpleasant, there seem to be cases
4785 -- in which T = ID, so the above tests and assignments do nothing???
4787 if Has_Predicates (T)
4788 or else (Present (Ancestor_Subtype (T))
4789 and then Has_Predicates (Ancestor_Subtype (T)))
4790 then
4791 Set_Has_Predicates (Id);
4792 Set_Has_Delayed_Freeze (Id);
4793 end if;
4795 -- Subtype of Boolean cannot have a constraint in SPARK
4797 if Is_Boolean_Type (T)
4798 and then Nkind (Subtype_Indication (N)) = N_Subtype_Indication
4799 then
4800 Check_SPARK_05_Restriction
4801 ("subtype of Boolean cannot have constraint", N);
4802 end if;
4804 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
4805 declare
4806 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
4807 One_Cstr : Node_Id;
4808 Low : Node_Id;
4809 High : Node_Id;
4811 begin
4812 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint then
4813 One_Cstr := First (Constraints (Cstr));
4814 while Present (One_Cstr) loop
4816 -- Index or discriminant constraint in SPARK must be a
4817 -- subtype mark.
4819 if not
4820 Nkind_In (One_Cstr, N_Identifier, N_Expanded_Name)
4821 then
4822 Check_SPARK_05_Restriction
4823 ("subtype mark required", One_Cstr);
4825 -- String subtype must have a lower bound of 1 in SPARK.
4826 -- Note that we do not need to test for the non-static case
4827 -- here, since that was already taken care of in
4828 -- Process_Range_Expr_In_Decl.
4830 elsif Base_Type (T) = Standard_String then
4831 Get_Index_Bounds (One_Cstr, Low, High);
4833 if Is_OK_Static_Expression (Low)
4834 and then Expr_Value (Low) /= 1
4835 then
4836 Check_SPARK_05_Restriction
4837 ("String subtype must have lower bound of 1", N);
4838 end if;
4839 end if;
4841 Next (One_Cstr);
4842 end loop;
4843 end if;
4844 end;
4845 end if;
4847 -- In the case where there is no constraint given in the subtype
4848 -- indication, Process_Subtype just returns the Subtype_Mark, so its
4849 -- semantic attributes must be established here.
4851 if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
4852 Set_Etype (Id, Base_Type (T));
4854 -- Subtype of unconstrained array without constraint is not allowed
4855 -- in SPARK.
4857 if Is_Array_Type (T) and then not Is_Constrained (T) then
4858 Check_SPARK_05_Restriction
4859 ("subtype of unconstrained array must have constraint", N);
4860 end if;
4862 case Ekind (T) is
4863 when Array_Kind =>
4864 Set_Ekind (Id, E_Array_Subtype);
4865 Copy_Array_Subtype_Attributes (Id, T);
4867 when Decimal_Fixed_Point_Kind =>
4868 Set_Ekind (Id, E_Decimal_Fixed_Point_Subtype);
4869 Set_Digits_Value (Id, Digits_Value (T));
4870 Set_Delta_Value (Id, Delta_Value (T));
4871 Set_Scale_Value (Id, Scale_Value (T));
4872 Set_Small_Value (Id, Small_Value (T));
4873 Set_Scalar_Range (Id, Scalar_Range (T));
4874 Set_Machine_Radix_10 (Id, Machine_Radix_10 (T));
4875 Set_Is_Constrained (Id, Is_Constrained (T));
4876 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4877 Set_RM_Size (Id, RM_Size (T));
4879 when Enumeration_Kind =>
4880 Set_Ekind (Id, E_Enumeration_Subtype);
4881 Set_First_Literal (Id, First_Literal (Base_Type (T)));
4882 Set_Scalar_Range (Id, Scalar_Range (T));
4883 Set_Is_Character_Type (Id, Is_Character_Type (T));
4884 Set_Is_Constrained (Id, Is_Constrained (T));
4885 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4886 Set_RM_Size (Id, RM_Size (T));
4887 Inherit_Predicate_Flags (Id, T);
4889 when Ordinary_Fixed_Point_Kind =>
4890 Set_Ekind (Id, E_Ordinary_Fixed_Point_Subtype);
4891 Set_Scalar_Range (Id, Scalar_Range (T));
4892 Set_Small_Value (Id, Small_Value (T));
4893 Set_Delta_Value (Id, Delta_Value (T));
4894 Set_Is_Constrained (Id, Is_Constrained (T));
4895 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4896 Set_RM_Size (Id, RM_Size (T));
4898 when Float_Kind =>
4899 Set_Ekind (Id, E_Floating_Point_Subtype);
4900 Set_Scalar_Range (Id, Scalar_Range (T));
4901 Set_Digits_Value (Id, Digits_Value (T));
4902 Set_Is_Constrained (Id, Is_Constrained (T));
4904 when Signed_Integer_Kind =>
4905 Set_Ekind (Id, E_Signed_Integer_Subtype);
4906 Set_Scalar_Range (Id, Scalar_Range (T));
4907 Set_Is_Constrained (Id, Is_Constrained (T));
4908 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4909 Set_RM_Size (Id, RM_Size (T));
4910 Inherit_Predicate_Flags (Id, T);
4912 when Modular_Integer_Kind =>
4913 Set_Ekind (Id, E_Modular_Integer_Subtype);
4914 Set_Scalar_Range (Id, Scalar_Range (T));
4915 Set_Is_Constrained (Id, Is_Constrained (T));
4916 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4917 Set_RM_Size (Id, RM_Size (T));
4918 Inherit_Predicate_Flags (Id, T);
4920 when Class_Wide_Kind =>
4921 Set_Ekind (Id, E_Class_Wide_Subtype);
4922 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
4923 Set_Cloned_Subtype (Id, T);
4924 Set_Is_Tagged_Type (Id, True);
4925 Set_Has_Unknown_Discriminants
4926 (Id, True);
4927 Set_No_Tagged_Streams_Pragma
4928 (Id, No_Tagged_Streams_Pragma (T));
4930 if Ekind (T) = E_Class_Wide_Subtype then
4931 Set_Equivalent_Type (Id, Equivalent_Type (T));
4932 end if;
4934 when E_Record_Type | E_Record_Subtype =>
4935 Set_Ekind (Id, E_Record_Subtype);
4937 if Ekind (T) = E_Record_Subtype
4938 and then Present (Cloned_Subtype (T))
4939 then
4940 Set_Cloned_Subtype (Id, Cloned_Subtype (T));
4941 else
4942 Set_Cloned_Subtype (Id, T);
4943 end if;
4945 Set_First_Entity (Id, First_Entity (T));
4946 Set_Last_Entity (Id, Last_Entity (T));
4947 Set_Has_Discriminants (Id, Has_Discriminants (T));
4948 Set_Is_Constrained (Id, Is_Constrained (T));
4949 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
4950 Set_Has_Implicit_Dereference
4951 (Id, Has_Implicit_Dereference (T));
4952 Set_Has_Unknown_Discriminants
4953 (Id, Has_Unknown_Discriminants (T));
4955 if Has_Discriminants (T) then
4956 Set_Discriminant_Constraint
4957 (Id, Discriminant_Constraint (T));
4958 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
4960 elsif Has_Unknown_Discriminants (Id) then
4961 Set_Discriminant_Constraint (Id, No_Elist);
4962 end if;
4964 if Is_Tagged_Type (T) then
4965 Set_Is_Tagged_Type (Id, True);
4966 Set_No_Tagged_Streams_Pragma
4967 (Id, No_Tagged_Streams_Pragma (T));
4968 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
4969 Set_Direct_Primitive_Operations
4970 (Id, Direct_Primitive_Operations (T));
4971 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
4973 if Is_Interface (T) then
4974 Set_Is_Interface (Id);
4975 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
4976 end if;
4977 end if;
4979 when Private_Kind =>
4980 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
4981 Set_Has_Discriminants (Id, Has_Discriminants (T));
4982 Set_Is_Constrained (Id, Is_Constrained (T));
4983 Set_First_Entity (Id, First_Entity (T));
4984 Set_Last_Entity (Id, Last_Entity (T));
4985 Set_Private_Dependents (Id, New_Elmt_List);
4986 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
4987 Set_Has_Implicit_Dereference
4988 (Id, Has_Implicit_Dereference (T));
4989 Set_Has_Unknown_Discriminants
4990 (Id, Has_Unknown_Discriminants (T));
4991 Set_Known_To_Have_Preelab_Init
4992 (Id, Known_To_Have_Preelab_Init (T));
4994 if Is_Tagged_Type (T) then
4995 Set_Is_Tagged_Type (Id);
4996 Set_No_Tagged_Streams_Pragma (Id,
4997 No_Tagged_Streams_Pragma (T));
4998 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
4999 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5000 Set_Direct_Primitive_Operations (Id,
5001 Direct_Primitive_Operations (T));
5002 end if;
5004 -- In general the attributes of the subtype of a private type
5005 -- are the attributes of the partial view of parent. However,
5006 -- the full view may be a discriminated type, and the subtype
5007 -- must share the discriminant constraint to generate correct
5008 -- calls to initialization procedures.
5010 if Has_Discriminants (T) then
5011 Set_Discriminant_Constraint
5012 (Id, Discriminant_Constraint (T));
5013 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5015 elsif Present (Full_View (T))
5016 and then Has_Discriminants (Full_View (T))
5017 then
5018 Set_Discriminant_Constraint
5019 (Id, Discriminant_Constraint (Full_View (T)));
5020 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5022 -- This would seem semantically correct, but apparently
5023 -- generates spurious errors about missing components ???
5025 -- Set_Has_Discriminants (Id);
5026 end if;
5028 Prepare_Private_Subtype_Completion (Id, N);
5030 -- If this is the subtype of a constrained private type with
5031 -- discriminants that has got a full view and we also have
5032 -- built a completion just above, show that the completion
5033 -- is a clone of the full view to the back-end.
5035 if Has_Discriminants (T)
5036 and then not Has_Unknown_Discriminants (T)
5037 and then not Is_Empty_Elmt_List (Discriminant_Constraint (T))
5038 and then Present (Full_View (T))
5039 and then Present (Full_View (Id))
5040 then
5041 Set_Cloned_Subtype (Full_View (Id), Full_View (T));
5042 end if;
5044 when Access_Kind =>
5045 Set_Ekind (Id, E_Access_Subtype);
5046 Set_Is_Constrained (Id, Is_Constrained (T));
5047 Set_Is_Access_Constant
5048 (Id, Is_Access_Constant (T));
5049 Set_Directly_Designated_Type
5050 (Id, Designated_Type (T));
5051 Set_Can_Never_Be_Null (Id, Can_Never_Be_Null (T));
5053 -- A Pure library_item must not contain the declaration of a
5054 -- named access type, except within a subprogram, generic
5055 -- subprogram, task unit, or protected unit, or if it has
5056 -- a specified Storage_Size of zero (RM05-10.2.1(15.4-15.5)).
5058 if Comes_From_Source (Id)
5059 and then In_Pure_Unit
5060 and then not In_Subprogram_Task_Protected_Unit
5061 and then not No_Pool_Assigned (Id)
5062 then
5063 Error_Msg_N
5064 ("named access types not allowed in pure unit", N);
5065 end if;
5067 when Concurrent_Kind =>
5068 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
5069 Set_Corresponding_Record_Type (Id,
5070 Corresponding_Record_Type (T));
5071 Set_First_Entity (Id, First_Entity (T));
5072 Set_First_Private_Entity (Id, First_Private_Entity (T));
5073 Set_Has_Discriminants (Id, Has_Discriminants (T));
5074 Set_Is_Constrained (Id, Is_Constrained (T));
5075 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5076 Set_Last_Entity (Id, Last_Entity (T));
5078 if Is_Tagged_Type (T) then
5079 Set_No_Tagged_Streams_Pragma
5080 (Id, No_Tagged_Streams_Pragma (T));
5081 end if;
5083 if Has_Discriminants (T) then
5084 Set_Discriminant_Constraint
5085 (Id, Discriminant_Constraint (T));
5086 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5087 end if;
5089 when Incomplete_Kind =>
5090 if Ada_Version >= Ada_2005 then
5092 -- In Ada 2005 an incomplete type can be explicitly tagged:
5093 -- propagate indication. Note that we also have to include
5094 -- subtypes for Ada 2012 extended use of incomplete types.
5096 Set_Ekind (Id, E_Incomplete_Subtype);
5097 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5098 Set_Private_Dependents (Id, New_Elmt_List);
5100 if Is_Tagged_Type (Id) then
5101 Set_No_Tagged_Streams_Pragma
5102 (Id, No_Tagged_Streams_Pragma (T));
5103 Set_Direct_Primitive_Operations (Id, New_Elmt_List);
5104 end if;
5106 -- Ada 2005 (AI-412): Decorate an incomplete subtype of an
5107 -- incomplete type visible through a limited with clause.
5109 if From_Limited_With (T)
5110 and then Present (Non_Limited_View (T))
5111 then
5112 Set_From_Limited_With (Id);
5113 Set_Non_Limited_View (Id, Non_Limited_View (T));
5115 -- Ada 2005 (AI-412): Add the regular incomplete subtype
5116 -- to the private dependents of the original incomplete
5117 -- type for future transformation.
5119 else
5120 Append_Elmt (Id, Private_Dependents (T));
5121 end if;
5123 -- If the subtype name denotes an incomplete type an error
5124 -- was already reported by Process_Subtype.
5126 else
5127 Set_Etype (Id, Any_Type);
5128 end if;
5130 when others =>
5131 raise Program_Error;
5132 end case;
5133 end if;
5135 if Etype (Id) = Any_Type then
5136 goto Leave;
5137 end if;
5139 -- Some common processing on all types
5141 Set_Size_Info (Id, T);
5142 Set_First_Rep_Item (Id, First_Rep_Item (T));
5144 -- If the parent type is a generic actual, so is the subtype. This may
5145 -- happen in a nested instance. Why Comes_From_Source test???
5147 if not Comes_From_Source (N) then
5148 Set_Is_Generic_Actual_Type (Id, Is_Generic_Actual_Type (T));
5149 end if;
5151 T := Etype (Id);
5153 Set_Is_Immediately_Visible (Id, True);
5154 Set_Depends_On_Private (Id, Has_Private_Component (T));
5155 Set_Is_Descendent_Of_Address (Id, Is_Descendent_Of_Address (T));
5157 if Is_Interface (T) then
5158 Set_Is_Interface (Id);
5159 end if;
5161 if Present (Generic_Parent_Type (N))
5162 and then
5163 (Nkind (Parent (Generic_Parent_Type (N))) /=
5164 N_Formal_Type_Declaration
5165 or else Nkind (Formal_Type_Definition
5166 (Parent (Generic_Parent_Type (N)))) /=
5167 N_Formal_Private_Type_Definition)
5168 then
5169 if Is_Tagged_Type (Id) then
5171 -- If this is a generic actual subtype for a synchronized type,
5172 -- the primitive operations are those of the corresponding record
5173 -- for which there is a separate subtype declaration.
5175 if Is_Concurrent_Type (Id) then
5176 null;
5177 elsif Is_Class_Wide_Type (Id) then
5178 Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
5179 else
5180 Derive_Subprograms (Generic_Parent_Type (N), Id, T);
5181 end if;
5183 elsif Scope (Etype (Id)) /= Standard_Standard then
5184 Derive_Subprograms (Generic_Parent_Type (N), Id);
5185 end if;
5186 end if;
5188 if Is_Private_Type (T) and then Present (Full_View (T)) then
5189 Conditional_Delay (Id, Full_View (T));
5191 -- The subtypes of components or subcomponents of protected types
5192 -- do not need freeze nodes, which would otherwise appear in the
5193 -- wrong scope (before the freeze node for the protected type). The
5194 -- proper subtypes are those of the subcomponents of the corresponding
5195 -- record.
5197 elsif Ekind (Scope (Id)) /= E_Protected_Type
5198 and then Present (Scope (Scope (Id))) -- error defense
5199 and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
5200 then
5201 Conditional_Delay (Id, T);
5202 end if;
5204 -- Check that Constraint_Error is raised for a scalar subtype indication
5205 -- when the lower or upper bound of a non-null range lies outside the
5206 -- range of the type mark.
5208 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5209 if Is_Scalar_Type (Etype (Id))
5210 and then Scalar_Range (Id) /=
5211 Scalar_Range (Etype (Subtype_Mark
5212 (Subtype_Indication (N))))
5213 then
5214 Apply_Range_Check
5215 (Scalar_Range (Id),
5216 Etype (Subtype_Mark (Subtype_Indication (N))));
5218 -- In the array case, check compatibility for each index
5220 elsif Is_Array_Type (Etype (Id)) and then Present (First_Index (Id))
5221 then
5222 -- This really should be a subprogram that finds the indications
5223 -- to check???
5225 declare
5226 Subt_Index : Node_Id := First_Index (Id);
5227 Target_Index : Node_Id :=
5228 First_Index (Etype
5229 (Subtype_Mark (Subtype_Indication (N))));
5230 Has_Dyn_Chk : Boolean := Has_Dynamic_Range_Check (N);
5232 begin
5233 while Present (Subt_Index) loop
5234 if ((Nkind (Subt_Index) = N_Identifier
5235 and then Ekind (Entity (Subt_Index)) in Scalar_Kind)
5236 or else Nkind (Subt_Index) = N_Subtype_Indication)
5237 and then
5238 Nkind (Scalar_Range (Etype (Subt_Index))) = N_Range
5239 then
5240 declare
5241 Target_Typ : constant Entity_Id :=
5242 Etype (Target_Index);
5243 begin
5244 R_Checks :=
5245 Get_Range_Checks
5246 (Scalar_Range (Etype (Subt_Index)),
5247 Target_Typ,
5248 Etype (Subt_Index),
5249 Defining_Identifier (N));
5251 -- Reset Has_Dynamic_Range_Check on the subtype to
5252 -- prevent elision of the index check due to a dynamic
5253 -- check generated for a preceding index (needed since
5254 -- Insert_Range_Checks tries to avoid generating
5255 -- redundant checks on a given declaration).
5257 Set_Has_Dynamic_Range_Check (N, False);
5259 Insert_Range_Checks
5260 (R_Checks,
5262 Target_Typ,
5263 Sloc (Defining_Identifier (N)));
5265 -- Record whether this index involved a dynamic check
5267 Has_Dyn_Chk :=
5268 Has_Dyn_Chk or else Has_Dynamic_Range_Check (N);
5269 end;
5270 end if;
5272 Next_Index (Subt_Index);
5273 Next_Index (Target_Index);
5274 end loop;
5276 -- Finally, mark whether the subtype involves dynamic checks
5278 Set_Has_Dynamic_Range_Check (N, Has_Dyn_Chk);
5279 end;
5280 end if;
5281 end if;
5283 -- A type invariant applies to any subtype in its scope, in particular
5284 -- to a generic actual.
5286 if Has_Invariants (T) and then In_Open_Scopes (Scope (T)) then
5287 Set_Has_Invariants (Id);
5288 Set_Invariant_Procedure (Id, Invariant_Procedure (T));
5289 end if;
5291 -- Make sure that generic actual types are properly frozen. The subtype
5292 -- is marked as a generic actual type when the enclosing instance is
5293 -- analyzed, so here we identify the subtype from the tree structure.
5295 if Expander_Active
5296 and then Is_Generic_Actual_Type (Id)
5297 and then In_Instance
5298 and then not Comes_From_Source (N)
5299 and then Nkind (Subtype_Indication (N)) /= N_Subtype_Indication
5300 and then Is_Frozen (T)
5301 then
5302 Freeze_Before (N, Id);
5303 end if;
5305 Set_Optimize_Alignment_Flags (Id);
5306 Check_Eliminated (Id);
5308 <<Leave>>
5309 if Has_Aspects (N) then
5310 Analyze_Aspect_Specifications (N, Id);
5311 end if;
5313 Analyze_Dimension (N);
5314 end Analyze_Subtype_Declaration;
5316 --------------------------------
5317 -- Analyze_Subtype_Indication --
5318 --------------------------------
5320 procedure Analyze_Subtype_Indication (N : Node_Id) is
5321 T : constant Entity_Id := Subtype_Mark (N);
5322 R : constant Node_Id := Range_Expression (Constraint (N));
5324 begin
5325 Analyze (T);
5327 if R /= Error then
5328 Analyze (R);
5329 Set_Etype (N, Etype (R));
5330 Resolve (R, Entity (T));
5331 else
5332 Set_Error_Posted (R);
5333 Set_Error_Posted (T);
5334 end if;
5335 end Analyze_Subtype_Indication;
5337 --------------------------
5338 -- Analyze_Variant_Part --
5339 --------------------------
5341 procedure Analyze_Variant_Part (N : Node_Id) is
5342 Discr_Name : Node_Id;
5343 Discr_Type : Entity_Id;
5345 procedure Process_Variant (A : Node_Id);
5346 -- Analyze declarations for a single variant
5348 package Analyze_Variant_Choices is
5349 new Generic_Analyze_Choices (Process_Variant);
5350 use Analyze_Variant_Choices;
5352 ---------------------
5353 -- Process_Variant --
5354 ---------------------
5356 procedure Process_Variant (A : Node_Id) is
5357 CL : constant Node_Id := Component_List (A);
5358 begin
5359 if not Null_Present (CL) then
5360 Analyze_Declarations (Component_Items (CL));
5362 if Present (Variant_Part (CL)) then
5363 Analyze (Variant_Part (CL));
5364 end if;
5365 end if;
5366 end Process_Variant;
5368 -- Start of processing for Analyze_Variant_Part
5370 begin
5371 Discr_Name := Name (N);
5372 Analyze (Discr_Name);
5374 -- If Discr_Name bad, get out (prevent cascaded errors)
5376 if Etype (Discr_Name) = Any_Type then
5377 return;
5378 end if;
5380 -- Check invalid discriminant in variant part
5382 if Ekind (Entity (Discr_Name)) /= E_Discriminant then
5383 Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
5384 end if;
5386 Discr_Type := Etype (Entity (Discr_Name));
5388 if not Is_Discrete_Type (Discr_Type) then
5389 Error_Msg_N
5390 ("discriminant in a variant part must be of a discrete type",
5391 Name (N));
5392 return;
5393 end if;
5395 -- Now analyze the choices, which also analyzes the declarations that
5396 -- are associated with each choice.
5398 Analyze_Choices (Variants (N), Discr_Type);
5400 -- Note: we used to instantiate and call Check_Choices here to check
5401 -- that the choices covered the discriminant, but it's too early to do
5402 -- that because of statically predicated subtypes, whose analysis may
5403 -- be deferred to their freeze point which may be as late as the freeze
5404 -- point of the containing record. So this call is now to be found in
5405 -- Freeze_Record_Declaration.
5407 end Analyze_Variant_Part;
5409 ----------------------------
5410 -- Array_Type_Declaration --
5411 ----------------------------
5413 procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
5414 Component_Def : constant Node_Id := Component_Definition (Def);
5415 Component_Typ : constant Node_Id := Subtype_Indication (Component_Def);
5416 Element_Type : Entity_Id;
5417 Implicit_Base : Entity_Id;
5418 Index : Node_Id;
5419 Related_Id : Entity_Id := Empty;
5420 Nb_Index : Nat;
5421 P : constant Node_Id := Parent (Def);
5422 Priv : Entity_Id;
5424 begin
5425 if Nkind (Def) = N_Constrained_Array_Definition then
5426 Index := First (Discrete_Subtype_Definitions (Def));
5427 else
5428 Index := First (Subtype_Marks (Def));
5429 end if;
5431 -- Find proper names for the implicit types which may be public. In case
5432 -- of anonymous arrays we use the name of the first object of that type
5433 -- as prefix.
5435 if No (T) then
5436 Related_Id := Defining_Identifier (P);
5437 else
5438 Related_Id := T;
5439 end if;
5441 Nb_Index := 1;
5442 while Present (Index) loop
5443 Analyze (Index);
5445 -- Test for odd case of trying to index a type by the type itself
5447 if Is_Entity_Name (Index) and then Entity (Index) = T then
5448 Error_Msg_N ("type& cannot be indexed by itself", Index);
5449 Set_Entity (Index, Standard_Boolean);
5450 Set_Etype (Index, Standard_Boolean);
5451 end if;
5453 -- Check SPARK restriction requiring a subtype mark
5455 if not Nkind_In (Index, N_Identifier, N_Expanded_Name) then
5456 Check_SPARK_05_Restriction ("subtype mark required", Index);
5457 end if;
5459 -- Add a subtype declaration for each index of private array type
5460 -- declaration whose etype is also private. For example:
5462 -- package Pkg is
5463 -- type Index is private;
5464 -- private
5465 -- type Table is array (Index) of ...
5466 -- end;
5468 -- This is currently required by the expander for the internally
5469 -- generated equality subprogram of records with variant parts in
5470 -- which the etype of some component is such private type.
5472 if Ekind (Current_Scope) = E_Package
5473 and then In_Private_Part (Current_Scope)
5474 and then Has_Private_Declaration (Etype (Index))
5475 then
5476 declare
5477 Loc : constant Source_Ptr := Sloc (Def);
5478 New_E : Entity_Id;
5479 Decl : Entity_Id;
5481 begin
5482 New_E := Make_Temporary (Loc, 'T');
5483 Set_Is_Internal (New_E);
5485 Decl :=
5486 Make_Subtype_Declaration (Loc,
5487 Defining_Identifier => New_E,
5488 Subtype_Indication =>
5489 New_Occurrence_Of (Etype (Index), Loc));
5491 Insert_Before (Parent (Def), Decl);
5492 Analyze (Decl);
5493 Set_Etype (Index, New_E);
5495 -- If the index is a range the Entity attribute is not
5496 -- available. Example:
5498 -- package Pkg is
5499 -- type T is private;
5500 -- private
5501 -- type T is new Natural;
5502 -- Table : array (T(1) .. T(10)) of Boolean;
5503 -- end Pkg;
5505 if Nkind (Index) /= N_Range then
5506 Set_Entity (Index, New_E);
5507 end if;
5508 end;
5509 end if;
5511 Make_Index (Index, P, Related_Id, Nb_Index);
5513 -- Check error of subtype with predicate for index type
5515 Bad_Predicated_Subtype_Use
5516 ("subtype& has predicate, not allowed as index subtype",
5517 Index, Etype (Index));
5519 -- Move to next index
5521 Next_Index (Index);
5522 Nb_Index := Nb_Index + 1;
5523 end loop;
5525 -- Process subtype indication if one is present
5527 if Present (Component_Typ) then
5528 Element_Type := Process_Subtype (Component_Typ, P, Related_Id, 'C');
5530 Set_Etype (Component_Typ, Element_Type);
5532 if not Nkind_In (Component_Typ, N_Identifier, N_Expanded_Name) then
5533 Check_SPARK_05_Restriction
5534 ("subtype mark required", Component_Typ);
5535 end if;
5537 -- Ada 2005 (AI-230): Access Definition case
5539 else pragma Assert (Present (Access_Definition (Component_Def)));
5541 -- Indicate that the anonymous access type is created by the
5542 -- array type declaration.
5544 Element_Type := Access_Definition
5545 (Related_Nod => P,
5546 N => Access_Definition (Component_Def));
5547 Set_Is_Local_Anonymous_Access (Element_Type);
5549 -- Propagate the parent. This field is needed if we have to generate
5550 -- the master_id associated with an anonymous access to task type
5551 -- component (see Expand_N_Full_Type_Declaration.Build_Master)
5553 Set_Parent (Element_Type, Parent (T));
5555 -- Ada 2005 (AI-230): In case of components that are anonymous access
5556 -- types the level of accessibility depends on the enclosing type
5557 -- declaration
5559 Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
5561 -- Ada 2005 (AI-254)
5563 declare
5564 CD : constant Node_Id :=
5565 Access_To_Subprogram_Definition
5566 (Access_Definition (Component_Def));
5567 begin
5568 if Present (CD) and then Protected_Present (CD) then
5569 Element_Type :=
5570 Replace_Anonymous_Access_To_Protected_Subprogram (Def);
5571 end if;
5572 end;
5573 end if;
5575 -- Constrained array case
5577 if No (T) then
5578 T := Create_Itype (E_Void, P, Related_Id, 'T');
5579 end if;
5581 if Nkind (Def) = N_Constrained_Array_Definition then
5583 -- Establish Implicit_Base as unconstrained base type
5585 Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
5587 Set_Etype (Implicit_Base, Implicit_Base);
5588 Set_Scope (Implicit_Base, Current_Scope);
5589 Set_Has_Delayed_Freeze (Implicit_Base);
5590 Set_Default_SSO (Implicit_Base);
5592 -- The constrained array type is a subtype of the unconstrained one
5594 Set_Ekind (T, E_Array_Subtype);
5595 Init_Size_Align (T);
5596 Set_Etype (T, Implicit_Base);
5597 Set_Scope (T, Current_Scope);
5598 Set_Is_Constrained (T);
5599 Set_First_Index (T,
5600 First (Discrete_Subtype_Definitions (Def)));
5601 Set_Has_Delayed_Freeze (T);
5603 -- Complete setup of implicit base type
5605 Set_First_Index (Implicit_Base, First_Index (T));
5606 Set_Component_Type (Implicit_Base, Element_Type);
5607 Set_Has_Task (Implicit_Base, Has_Task (Element_Type));
5608 Set_Has_Protected (Implicit_Base, Has_Protected (Element_Type));
5609 Set_Component_Size (Implicit_Base, Uint_0);
5610 Set_Packed_Array_Impl_Type (Implicit_Base, Empty);
5611 Set_Has_Controlled_Component (Implicit_Base,
5612 Has_Controlled_Component (Element_Type)
5613 or else Is_Controlled (Element_Type));
5614 Set_Finalize_Storage_Only (Implicit_Base,
5615 Finalize_Storage_Only (Element_Type));
5617 -- Inherit the "ghostness" from the constrained array type
5619 if Is_Ghost_Entity (T) or else Ghost_Mode > None then
5620 Set_Is_Ghost_Entity (Implicit_Base);
5621 end if;
5623 -- Unconstrained array case
5625 else
5626 Set_Ekind (T, E_Array_Type);
5627 Init_Size_Align (T);
5628 Set_Etype (T, T);
5629 Set_Scope (T, Current_Scope);
5630 Set_Component_Size (T, Uint_0);
5631 Set_Is_Constrained (T, False);
5632 Set_First_Index (T, First (Subtype_Marks (Def)));
5633 Set_Has_Delayed_Freeze (T, True);
5634 Set_Has_Task (T, Has_Task (Element_Type));
5635 Set_Has_Protected (T, Has_Protected (Element_Type));
5636 Set_Has_Controlled_Component (T, Has_Controlled_Component
5637 (Element_Type)
5638 or else
5639 Is_Controlled (Element_Type));
5640 Set_Finalize_Storage_Only (T, Finalize_Storage_Only
5641 (Element_Type));
5642 Set_Default_SSO (T);
5643 end if;
5645 -- Common attributes for both cases
5647 Set_Component_Type (Base_Type (T), Element_Type);
5648 Set_Packed_Array_Impl_Type (T, Empty);
5650 if Aliased_Present (Component_Definition (Def)) then
5651 Check_SPARK_05_Restriction
5652 ("aliased is not allowed", Component_Definition (Def));
5653 Set_Has_Aliased_Components (Etype (T));
5654 end if;
5656 -- Ada 2005 (AI-231): Propagate the null-excluding attribute to the
5657 -- array type to ensure that objects of this type are initialized.
5659 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (Element_Type) then
5660 Set_Can_Never_Be_Null (T);
5662 if Null_Exclusion_Present (Component_Definition (Def))
5664 -- No need to check itypes because in their case this check was
5665 -- done at their point of creation
5667 and then not Is_Itype (Element_Type)
5668 then
5669 Error_Msg_N
5670 ("`NOT NULL` not allowed (null already excluded)",
5671 Subtype_Indication (Component_Definition (Def)));
5672 end if;
5673 end if;
5675 Priv := Private_Component (Element_Type);
5677 if Present (Priv) then
5679 -- Check for circular definitions
5681 if Priv = Any_Type then
5682 Set_Component_Type (Etype (T), Any_Type);
5684 -- There is a gap in the visibility of operations on the composite
5685 -- type only if the component type is defined in a different scope.
5687 elsif Scope (Priv) = Current_Scope then
5688 null;
5690 elsif Is_Limited_Type (Priv) then
5691 Set_Is_Limited_Composite (Etype (T));
5692 Set_Is_Limited_Composite (T);
5693 else
5694 Set_Is_Private_Composite (Etype (T));
5695 Set_Is_Private_Composite (T);
5696 end if;
5697 end if;
5699 -- A syntax error in the declaration itself may lead to an empty index
5700 -- list, in which case do a minimal patch.
5702 if No (First_Index (T)) then
5703 Error_Msg_N ("missing index definition in array type declaration", T);
5705 declare
5706 Indexes : constant List_Id :=
5707 New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
5708 begin
5709 Set_Discrete_Subtype_Definitions (Def, Indexes);
5710 Set_First_Index (T, First (Indexes));
5711 return;
5712 end;
5713 end if;
5715 -- Create a concatenation operator for the new type. Internal array
5716 -- types created for packed entities do not need such, they are
5717 -- compatible with the user-defined type.
5719 if Number_Dimensions (T) = 1
5720 and then not Is_Packed_Array_Impl_Type (T)
5721 then
5722 New_Concatenation_Op (T);
5723 end if;
5725 -- In the case of an unconstrained array the parser has already verified
5726 -- that all the indexes are unconstrained but we still need to make sure
5727 -- that the element type is constrained.
5729 if Is_Indefinite_Subtype (Element_Type) then
5730 Error_Msg_N
5731 ("unconstrained element type in array declaration",
5732 Subtype_Indication (Component_Def));
5734 elsif Is_Abstract_Type (Element_Type) then
5735 Error_Msg_N
5736 ("the type of a component cannot be abstract",
5737 Subtype_Indication (Component_Def));
5738 end if;
5740 -- There may be an invariant declared for the component type, but
5741 -- the construction of the component invariant checking procedure
5742 -- takes place during expansion.
5743 end Array_Type_Declaration;
5745 ------------------------------------------------------
5746 -- Replace_Anonymous_Access_To_Protected_Subprogram --
5747 ------------------------------------------------------
5749 function Replace_Anonymous_Access_To_Protected_Subprogram
5750 (N : Node_Id) return Entity_Id
5752 Loc : constant Source_Ptr := Sloc (N);
5754 Curr_Scope : constant Scope_Stack_Entry :=
5755 Scope_Stack.Table (Scope_Stack.Last);
5757 Anon : constant Entity_Id := Make_Temporary (Loc, 'S');
5759 Acc : Node_Id;
5760 -- Access definition in declaration
5762 Comp : Node_Id;
5763 -- Object definition or formal definition with an access definition
5765 Decl : Node_Id;
5766 -- Declaration of anonymous access to subprogram type
5768 Spec : Node_Id;
5769 -- Original specification in access to subprogram
5771 P : Node_Id;
5773 begin
5774 Set_Is_Internal (Anon);
5776 case Nkind (N) is
5777 when N_Component_Declaration |
5778 N_Unconstrained_Array_Definition |
5779 N_Constrained_Array_Definition =>
5780 Comp := Component_Definition (N);
5781 Acc := Access_Definition (Comp);
5783 when N_Discriminant_Specification =>
5784 Comp := Discriminant_Type (N);
5785 Acc := Comp;
5787 when N_Parameter_Specification =>
5788 Comp := Parameter_Type (N);
5789 Acc := Comp;
5791 when N_Access_Function_Definition =>
5792 Comp := Result_Definition (N);
5793 Acc := Comp;
5795 when N_Object_Declaration =>
5796 Comp := Object_Definition (N);
5797 Acc := Comp;
5799 when N_Function_Specification =>
5800 Comp := Result_Definition (N);
5801 Acc := Comp;
5803 when others =>
5804 raise Program_Error;
5805 end case;
5807 Spec := Access_To_Subprogram_Definition (Acc);
5809 Decl :=
5810 Make_Full_Type_Declaration (Loc,
5811 Defining_Identifier => Anon,
5812 Type_Definition => Copy_Separate_Tree (Spec));
5814 Mark_Rewrite_Insertion (Decl);
5816 -- In ASIS mode, analyze the profile on the original node, because
5817 -- the separate copy does not provide enough links to recover the
5818 -- original tree. Analysis is limited to type annotations, within
5819 -- a temporary scope that serves as an anonymous subprogram to collect
5820 -- otherwise useless temporaries and itypes.
5822 if ASIS_Mode then
5823 declare
5824 Typ : constant Entity_Id := Make_Temporary (Loc, 'S');
5826 begin
5827 if Nkind (Spec) = N_Access_Function_Definition then
5828 Set_Ekind (Typ, E_Function);
5829 else
5830 Set_Ekind (Typ, E_Procedure);
5831 end if;
5833 Set_Parent (Typ, N);
5834 Set_Scope (Typ, Current_Scope);
5835 Push_Scope (Typ);
5837 Process_Formals (Parameter_Specifications (Spec), Spec);
5839 if Nkind (Spec) = N_Access_Function_Definition then
5840 declare
5841 Def : constant Node_Id := Result_Definition (Spec);
5843 begin
5844 -- The result might itself be an anonymous access type, so
5845 -- have to recurse.
5847 if Nkind (Def) = N_Access_Definition then
5848 if Present (Access_To_Subprogram_Definition (Def)) then
5849 Set_Etype
5850 (Def,
5851 Replace_Anonymous_Access_To_Protected_Subprogram
5852 (Spec));
5853 else
5854 Find_Type (Subtype_Mark (Def));
5855 end if;
5857 else
5858 Find_Type (Def);
5859 end if;
5860 end;
5861 end if;
5863 End_Scope;
5864 end;
5865 end if;
5867 -- Insert the new declaration in the nearest enclosing scope. If the
5868 -- node is a body and N is its return type, the declaration belongs in
5869 -- the enclosing scope.
5871 P := Parent (N);
5873 if Nkind (P) = N_Subprogram_Body
5874 and then Nkind (N) = N_Function_Specification
5875 then
5876 P := Parent (P);
5877 end if;
5879 while Present (P) and then not Has_Declarations (P) loop
5880 P := Parent (P);
5881 end loop;
5883 pragma Assert (Present (P));
5885 if Nkind (P) = N_Package_Specification then
5886 Prepend (Decl, Visible_Declarations (P));
5887 else
5888 Prepend (Decl, Declarations (P));
5889 end if;
5891 -- Replace the anonymous type with an occurrence of the new declaration.
5892 -- In all cases the rewritten node does not have the null-exclusion
5893 -- attribute because (if present) it was already inherited by the
5894 -- anonymous entity (Anon). Thus, in case of components we do not
5895 -- inherit this attribute.
5897 if Nkind (N) = N_Parameter_Specification then
5898 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5899 Set_Etype (Defining_Identifier (N), Anon);
5900 Set_Null_Exclusion_Present (N, False);
5902 elsif Nkind (N) = N_Object_Declaration then
5903 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5904 Set_Etype (Defining_Identifier (N), Anon);
5906 elsif Nkind (N) = N_Access_Function_Definition then
5907 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5909 elsif Nkind (N) = N_Function_Specification then
5910 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5911 Set_Etype (Defining_Unit_Name (N), Anon);
5913 else
5914 Rewrite (Comp,
5915 Make_Component_Definition (Loc,
5916 Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
5917 end if;
5919 Mark_Rewrite_Insertion (Comp);
5921 if Nkind_In (N, N_Object_Declaration, N_Access_Function_Definition) then
5922 Analyze (Decl);
5924 else
5925 -- Temporarily remove the current scope (record or subprogram) from
5926 -- the stack to add the new declarations to the enclosing scope.
5928 Scope_Stack.Decrement_Last;
5929 Analyze (Decl);
5930 Set_Is_Itype (Anon);
5931 Scope_Stack.Append (Curr_Scope);
5932 end if;
5934 Set_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
5935 Set_Can_Use_Internal_Rep (Anon, not Always_Compatible_Rep_On_Target);
5936 return Anon;
5937 end Replace_Anonymous_Access_To_Protected_Subprogram;
5939 -------------------------------
5940 -- Build_Derived_Access_Type --
5941 -------------------------------
5943 procedure Build_Derived_Access_Type
5944 (N : Node_Id;
5945 Parent_Type : Entity_Id;
5946 Derived_Type : Entity_Id)
5948 S : constant Node_Id := Subtype_Indication (Type_Definition (N));
5950 Desig_Type : Entity_Id;
5951 Discr : Entity_Id;
5952 Discr_Con_Elist : Elist_Id;
5953 Discr_Con_El : Elmt_Id;
5954 Subt : Entity_Id;
5956 begin
5957 -- Set the designated type so it is available in case this is an access
5958 -- to a self-referential type, e.g. a standard list type with a next
5959 -- pointer. Will be reset after subtype is built.
5961 Set_Directly_Designated_Type
5962 (Derived_Type, Designated_Type (Parent_Type));
5964 Subt := Process_Subtype (S, N);
5966 if Nkind (S) /= N_Subtype_Indication
5967 and then Subt /= Base_Type (Subt)
5968 then
5969 Set_Ekind (Derived_Type, E_Access_Subtype);
5970 end if;
5972 if Ekind (Derived_Type) = E_Access_Subtype then
5973 declare
5974 Pbase : constant Entity_Id := Base_Type (Parent_Type);
5975 Ibase : constant Entity_Id :=
5976 Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
5977 Svg_Chars : constant Name_Id := Chars (Ibase);
5978 Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
5980 begin
5981 Copy_Node (Pbase, Ibase);
5983 Set_Chars (Ibase, Svg_Chars);
5984 Set_Next_Entity (Ibase, Svg_Next_E);
5985 Set_Sloc (Ibase, Sloc (Derived_Type));
5986 Set_Scope (Ibase, Scope (Derived_Type));
5987 Set_Freeze_Node (Ibase, Empty);
5988 Set_Is_Frozen (Ibase, False);
5989 Set_Comes_From_Source (Ibase, False);
5990 Set_Is_First_Subtype (Ibase, False);
5992 Set_Etype (Ibase, Pbase);
5993 Set_Etype (Derived_Type, Ibase);
5994 end;
5995 end if;
5997 Set_Directly_Designated_Type
5998 (Derived_Type, Designated_Type (Subt));
6000 Set_Is_Constrained (Derived_Type, Is_Constrained (Subt));
6001 Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
6002 Set_Size_Info (Derived_Type, Parent_Type);
6003 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
6004 Set_Depends_On_Private (Derived_Type,
6005 Has_Private_Component (Derived_Type));
6006 Conditional_Delay (Derived_Type, Subt);
6008 -- Ada 2005 (AI-231): Set the null-exclusion attribute, and verify
6009 -- that it is not redundant.
6011 if Null_Exclusion_Present (Type_Definition (N)) then
6012 Set_Can_Never_Be_Null (Derived_Type);
6014 -- What is with the "AND THEN FALSE" here ???
6016 if Can_Never_Be_Null (Parent_Type)
6017 and then False
6018 then
6019 Error_Msg_NE
6020 ("`NOT NULL` not allowed (& already excludes null)",
6021 N, Parent_Type);
6022 end if;
6024 elsif Can_Never_Be_Null (Parent_Type) then
6025 Set_Can_Never_Be_Null (Derived_Type);
6026 end if;
6028 -- Note: we do not copy the Storage_Size_Variable, since we always go to
6029 -- the root type for this information.
6031 -- Apply range checks to discriminants for derived record case
6032 -- ??? THIS CODE SHOULD NOT BE HERE REALLY.
6034 Desig_Type := Designated_Type (Derived_Type);
6035 if Is_Composite_Type (Desig_Type)
6036 and then (not Is_Array_Type (Desig_Type))
6037 and then Has_Discriminants (Desig_Type)
6038 and then Base_Type (Desig_Type) /= Desig_Type
6039 then
6040 Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
6041 Discr_Con_El := First_Elmt (Discr_Con_Elist);
6043 Discr := First_Discriminant (Base_Type (Desig_Type));
6044 while Present (Discr_Con_El) loop
6045 Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
6046 Next_Elmt (Discr_Con_El);
6047 Next_Discriminant (Discr);
6048 end loop;
6049 end if;
6050 end Build_Derived_Access_Type;
6052 ------------------------------
6053 -- Build_Derived_Array_Type --
6054 ------------------------------
6056 procedure Build_Derived_Array_Type
6057 (N : Node_Id;
6058 Parent_Type : Entity_Id;
6059 Derived_Type : Entity_Id)
6061 Loc : constant Source_Ptr := Sloc (N);
6062 Tdef : constant Node_Id := Type_Definition (N);
6063 Indic : constant Node_Id := Subtype_Indication (Tdef);
6064 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
6065 Implicit_Base : Entity_Id;
6066 New_Indic : Node_Id;
6068 procedure Make_Implicit_Base;
6069 -- If the parent subtype is constrained, the derived type is a subtype
6070 -- of an implicit base type derived from the parent base.
6072 ------------------------
6073 -- Make_Implicit_Base --
6074 ------------------------
6076 procedure Make_Implicit_Base is
6077 begin
6078 Implicit_Base :=
6079 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
6081 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
6082 Set_Etype (Implicit_Base, Parent_Base);
6084 Copy_Array_Subtype_Attributes (Implicit_Base, Parent_Base);
6085 Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
6087 Set_Has_Delayed_Freeze (Implicit_Base, True);
6089 -- Inherit the "ghostness" from the parent base type
6091 if Is_Ghost_Entity (Parent_Base) or else Ghost_Mode > None then
6092 Set_Is_Ghost_Entity (Implicit_Base);
6093 end if;
6094 end Make_Implicit_Base;
6096 -- Start of processing for Build_Derived_Array_Type
6098 begin
6099 if not Is_Constrained (Parent_Type) then
6100 if Nkind (Indic) /= N_Subtype_Indication then
6101 Set_Ekind (Derived_Type, E_Array_Type);
6103 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6104 Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
6106 Set_Has_Delayed_Freeze (Derived_Type, True);
6108 else
6109 Make_Implicit_Base;
6110 Set_Etype (Derived_Type, Implicit_Base);
6112 New_Indic :=
6113 Make_Subtype_Declaration (Loc,
6114 Defining_Identifier => Derived_Type,
6115 Subtype_Indication =>
6116 Make_Subtype_Indication (Loc,
6117 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
6118 Constraint => Constraint (Indic)));
6120 Rewrite (N, New_Indic);
6121 Analyze (N);
6122 end if;
6124 else
6125 if Nkind (Indic) /= N_Subtype_Indication then
6126 Make_Implicit_Base;
6128 Set_Ekind (Derived_Type, Ekind (Parent_Type));
6129 Set_Etype (Derived_Type, Implicit_Base);
6130 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6132 else
6133 Error_Msg_N ("illegal constraint on constrained type", Indic);
6134 end if;
6135 end if;
6137 -- If parent type is not a derived type itself, and is declared in
6138 -- closed scope (e.g. a subprogram), then we must explicitly introduce
6139 -- the new type's concatenation operator since Derive_Subprograms
6140 -- will not inherit the parent's operator. If the parent type is
6141 -- unconstrained, the operator is of the unconstrained base type.
6143 if Number_Dimensions (Parent_Type) = 1
6144 and then not Is_Limited_Type (Parent_Type)
6145 and then not Is_Derived_Type (Parent_Type)
6146 and then not Is_Package_Or_Generic_Package
6147 (Scope (Base_Type (Parent_Type)))
6148 then
6149 if not Is_Constrained (Parent_Type)
6150 and then Is_Constrained (Derived_Type)
6151 then
6152 New_Concatenation_Op (Implicit_Base);
6153 else
6154 New_Concatenation_Op (Derived_Type);
6155 end if;
6156 end if;
6157 end Build_Derived_Array_Type;
6159 -----------------------------------
6160 -- Build_Derived_Concurrent_Type --
6161 -----------------------------------
6163 procedure Build_Derived_Concurrent_Type
6164 (N : Node_Id;
6165 Parent_Type : Entity_Id;
6166 Derived_Type : Entity_Id)
6168 Loc : constant Source_Ptr := Sloc (N);
6170 Corr_Record : constant Entity_Id := Make_Temporary (Loc, 'C');
6171 Corr_Decl : Node_Id;
6172 Corr_Decl_Needed : Boolean;
6173 -- If the derived type has fewer discriminants than its parent, the
6174 -- corresponding record is also a derived type, in order to account for
6175 -- the bound discriminants. We create a full type declaration for it in
6176 -- this case.
6178 Constraint_Present : constant Boolean :=
6179 Nkind (Subtype_Indication (Type_Definition (N))) =
6180 N_Subtype_Indication;
6182 D_Constraint : Node_Id;
6183 New_Constraint : Elist_Id;
6184 Old_Disc : Entity_Id;
6185 New_Disc : Entity_Id;
6186 New_N : Node_Id;
6188 begin
6189 Set_Stored_Constraint (Derived_Type, No_Elist);
6190 Corr_Decl_Needed := False;
6191 Old_Disc := Empty;
6193 if Present (Discriminant_Specifications (N))
6194 and then Constraint_Present
6195 then
6196 Old_Disc := First_Discriminant (Parent_Type);
6197 New_Disc := First (Discriminant_Specifications (N));
6198 while Present (New_Disc) and then Present (Old_Disc) loop
6199 Next_Discriminant (Old_Disc);
6200 Next (New_Disc);
6201 end loop;
6202 end if;
6204 if Present (Old_Disc) and then Expander_Active then
6206 -- The new type has fewer discriminants, so we need to create a new
6207 -- corresponding record, which is derived from the corresponding
6208 -- record of the parent, and has a stored constraint that captures
6209 -- the values of the discriminant constraints. The corresponding
6210 -- record is needed only if expander is active and code generation is
6211 -- enabled.
6213 -- The type declaration for the derived corresponding record has the
6214 -- same discriminant part and constraints as the current declaration.
6215 -- Copy the unanalyzed tree to build declaration.
6217 Corr_Decl_Needed := True;
6218 New_N := Copy_Separate_Tree (N);
6220 Corr_Decl :=
6221 Make_Full_Type_Declaration (Loc,
6222 Defining_Identifier => Corr_Record,
6223 Discriminant_Specifications =>
6224 Discriminant_Specifications (New_N),
6225 Type_Definition =>
6226 Make_Derived_Type_Definition (Loc,
6227 Subtype_Indication =>
6228 Make_Subtype_Indication (Loc,
6229 Subtype_Mark =>
6230 New_Occurrence_Of
6231 (Corresponding_Record_Type (Parent_Type), Loc),
6232 Constraint =>
6233 Constraint
6234 (Subtype_Indication (Type_Definition (New_N))))));
6235 end if;
6237 -- Copy Storage_Size and Relative_Deadline variables if task case
6239 if Is_Task_Type (Parent_Type) then
6240 Set_Storage_Size_Variable (Derived_Type,
6241 Storage_Size_Variable (Parent_Type));
6242 Set_Relative_Deadline_Variable (Derived_Type,
6243 Relative_Deadline_Variable (Parent_Type));
6244 end if;
6246 if Present (Discriminant_Specifications (N)) then
6247 Push_Scope (Derived_Type);
6248 Check_Or_Process_Discriminants (N, Derived_Type);
6250 if Constraint_Present then
6251 New_Constraint :=
6252 Expand_To_Stored_Constraint
6253 (Parent_Type,
6254 Build_Discriminant_Constraints
6255 (Parent_Type,
6256 Subtype_Indication (Type_Definition (N)), True));
6257 end if;
6259 End_Scope;
6261 elsif Constraint_Present then
6263 -- Build constrained subtype, copying the constraint, and derive
6264 -- from it to create a derived constrained type.
6266 declare
6267 Loc : constant Source_Ptr := Sloc (N);
6268 Anon : constant Entity_Id :=
6269 Make_Defining_Identifier (Loc,
6270 Chars => New_External_Name (Chars (Derived_Type), 'T'));
6271 Decl : Node_Id;
6273 begin
6274 Decl :=
6275 Make_Subtype_Declaration (Loc,
6276 Defining_Identifier => Anon,
6277 Subtype_Indication =>
6278 New_Copy_Tree (Subtype_Indication (Type_Definition (N))));
6279 Insert_Before (N, Decl);
6280 Analyze (Decl);
6282 Rewrite (Subtype_Indication (Type_Definition (N)),
6283 New_Occurrence_Of (Anon, Loc));
6284 Set_Analyzed (Derived_Type, False);
6285 Analyze (N);
6286 return;
6287 end;
6288 end if;
6290 -- By default, operations and private data are inherited from parent.
6291 -- However, in the presence of bound discriminants, a new corresponding
6292 -- record will be created, see below.
6294 Set_Has_Discriminants
6295 (Derived_Type, Has_Discriminants (Parent_Type));
6296 Set_Corresponding_Record_Type
6297 (Derived_Type, Corresponding_Record_Type (Parent_Type));
6299 -- Is_Constrained is set according the parent subtype, but is set to
6300 -- False if the derived type is declared with new discriminants.
6302 Set_Is_Constrained
6303 (Derived_Type,
6304 (Is_Constrained (Parent_Type) or else Constraint_Present)
6305 and then not Present (Discriminant_Specifications (N)));
6307 if Constraint_Present then
6308 if not Has_Discriminants (Parent_Type) then
6309 Error_Msg_N ("untagged parent must have discriminants", N);
6311 elsif Present (Discriminant_Specifications (N)) then
6313 -- Verify that new discriminants are used to constrain old ones
6315 D_Constraint :=
6316 First
6317 (Constraints
6318 (Constraint (Subtype_Indication (Type_Definition (N)))));
6320 Old_Disc := First_Discriminant (Parent_Type);
6322 while Present (D_Constraint) loop
6323 if Nkind (D_Constraint) /= N_Discriminant_Association then
6325 -- Positional constraint. If it is a reference to a new
6326 -- discriminant, it constrains the corresponding old one.
6328 if Nkind (D_Constraint) = N_Identifier then
6329 New_Disc := First_Discriminant (Derived_Type);
6330 while Present (New_Disc) loop
6331 exit when Chars (New_Disc) = Chars (D_Constraint);
6332 Next_Discriminant (New_Disc);
6333 end loop;
6335 if Present (New_Disc) then
6336 Set_Corresponding_Discriminant (New_Disc, Old_Disc);
6337 end if;
6338 end if;
6340 Next_Discriminant (Old_Disc);
6342 -- if this is a named constraint, search by name for the old
6343 -- discriminants constrained by the new one.
6345 elsif Nkind (Expression (D_Constraint)) = N_Identifier then
6347 -- Find new discriminant with that name
6349 New_Disc := First_Discriminant (Derived_Type);
6350 while Present (New_Disc) loop
6351 exit when
6352 Chars (New_Disc) = Chars (Expression (D_Constraint));
6353 Next_Discriminant (New_Disc);
6354 end loop;
6356 if Present (New_Disc) then
6358 -- Verify that new discriminant renames some discriminant
6359 -- of the parent type, and associate the new discriminant
6360 -- with one or more old ones that it renames.
6362 declare
6363 Selector : Node_Id;
6365 begin
6366 Selector := First (Selector_Names (D_Constraint));
6367 while Present (Selector) loop
6368 Old_Disc := First_Discriminant (Parent_Type);
6369 while Present (Old_Disc) loop
6370 exit when Chars (Old_Disc) = Chars (Selector);
6371 Next_Discriminant (Old_Disc);
6372 end loop;
6374 if Present (Old_Disc) then
6375 Set_Corresponding_Discriminant
6376 (New_Disc, Old_Disc);
6377 end if;
6379 Next (Selector);
6380 end loop;
6381 end;
6382 end if;
6383 end if;
6385 Next (D_Constraint);
6386 end loop;
6388 New_Disc := First_Discriminant (Derived_Type);
6389 while Present (New_Disc) loop
6390 if No (Corresponding_Discriminant (New_Disc)) then
6391 Error_Msg_NE
6392 ("new discriminant& must constrain old one", N, New_Disc);
6394 elsif not
6395 Subtypes_Statically_Compatible
6396 (Etype (New_Disc),
6397 Etype (Corresponding_Discriminant (New_Disc)))
6398 then
6399 Error_Msg_NE
6400 ("& not statically compatible with parent discriminant",
6401 N, New_Disc);
6402 end if;
6404 Next_Discriminant (New_Disc);
6405 end loop;
6406 end if;
6408 elsif Present (Discriminant_Specifications (N)) then
6409 Error_Msg_N
6410 ("missing discriminant constraint in untagged derivation", N);
6411 end if;
6413 -- The entity chain of the derived type includes the new discriminants
6414 -- but shares operations with the parent.
6416 if Present (Discriminant_Specifications (N)) then
6417 Old_Disc := First_Discriminant (Parent_Type);
6418 while Present (Old_Disc) loop
6419 if No (Next_Entity (Old_Disc))
6420 or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
6421 then
6422 Set_Next_Entity
6423 (Last_Entity (Derived_Type), Next_Entity (Old_Disc));
6424 exit;
6425 end if;
6427 Next_Discriminant (Old_Disc);
6428 end loop;
6430 else
6431 Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
6432 if Has_Discriminants (Parent_Type) then
6433 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
6434 Set_Discriminant_Constraint (
6435 Derived_Type, Discriminant_Constraint (Parent_Type));
6436 end if;
6437 end if;
6439 Set_Last_Entity (Derived_Type, Last_Entity (Parent_Type));
6441 Set_Has_Completion (Derived_Type);
6443 if Corr_Decl_Needed then
6444 Set_Stored_Constraint (Derived_Type, New_Constraint);
6445 Insert_After (N, Corr_Decl);
6446 Analyze (Corr_Decl);
6447 Set_Corresponding_Record_Type (Derived_Type, Corr_Record);
6448 end if;
6449 end Build_Derived_Concurrent_Type;
6451 ------------------------------------
6452 -- Build_Derived_Enumeration_Type --
6453 ------------------------------------
6455 procedure Build_Derived_Enumeration_Type
6456 (N : Node_Id;
6457 Parent_Type : Entity_Id;
6458 Derived_Type : Entity_Id)
6460 Loc : constant Source_Ptr := Sloc (N);
6461 Def : constant Node_Id := Type_Definition (N);
6462 Indic : constant Node_Id := Subtype_Indication (Def);
6463 Implicit_Base : Entity_Id;
6464 Literal : Entity_Id;
6465 New_Lit : Entity_Id;
6466 Literals_List : List_Id;
6467 Type_Decl : Node_Id;
6468 Hi, Lo : Node_Id;
6469 Rang_Expr : Node_Id;
6471 begin
6472 -- Since types Standard.Character and Standard.[Wide_]Wide_Character do
6473 -- not have explicit literals lists we need to process types derived
6474 -- from them specially. This is handled by Derived_Standard_Character.
6475 -- If the parent type is a generic type, there are no literals either,
6476 -- and we construct the same skeletal representation as for the generic
6477 -- parent type.
6479 if Is_Standard_Character_Type (Parent_Type) then
6480 Derived_Standard_Character (N, Parent_Type, Derived_Type);
6482 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
6483 declare
6484 Lo : Node_Id;
6485 Hi : Node_Id;
6487 begin
6488 if Nkind (Indic) /= N_Subtype_Indication then
6489 Lo :=
6490 Make_Attribute_Reference (Loc,
6491 Attribute_Name => Name_First,
6492 Prefix => New_Occurrence_Of (Derived_Type, Loc));
6493 Set_Etype (Lo, Derived_Type);
6495 Hi :=
6496 Make_Attribute_Reference (Loc,
6497 Attribute_Name => Name_Last,
6498 Prefix => New_Occurrence_Of (Derived_Type, Loc));
6499 Set_Etype (Hi, Derived_Type);
6501 Set_Scalar_Range (Derived_Type,
6502 Make_Range (Loc,
6503 Low_Bound => Lo,
6504 High_Bound => Hi));
6505 else
6507 -- Analyze subtype indication and verify compatibility
6508 -- with parent type.
6510 if Base_Type (Process_Subtype (Indic, N)) /=
6511 Base_Type (Parent_Type)
6512 then
6513 Error_Msg_N
6514 ("illegal constraint for formal discrete type", N);
6515 end if;
6516 end if;
6517 end;
6519 else
6520 -- If a constraint is present, analyze the bounds to catch
6521 -- premature usage of the derived literals.
6523 if Nkind (Indic) = N_Subtype_Indication
6524 and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
6525 then
6526 Analyze (Low_Bound (Range_Expression (Constraint (Indic))));
6527 Analyze (High_Bound (Range_Expression (Constraint (Indic))));
6528 end if;
6530 -- Introduce an implicit base type for the derived type even if there
6531 -- is no constraint attached to it, since this seems closer to the
6532 -- Ada semantics. Build a full type declaration tree for the derived
6533 -- type using the implicit base type as the defining identifier. The
6534 -- build a subtype declaration tree which applies the constraint (if
6535 -- any) have it replace the derived type declaration.
6537 Literal := First_Literal (Parent_Type);
6538 Literals_List := New_List;
6539 while Present (Literal)
6540 and then Ekind (Literal) = E_Enumeration_Literal
6541 loop
6542 -- Literals of the derived type have the same representation as
6543 -- those of the parent type, but this representation can be
6544 -- overridden by an explicit representation clause. Indicate
6545 -- that there is no explicit representation given yet. These
6546 -- derived literals are implicit operations of the new type,
6547 -- and can be overridden by explicit ones.
6549 if Nkind (Literal) = N_Defining_Character_Literal then
6550 New_Lit :=
6551 Make_Defining_Character_Literal (Loc, Chars (Literal));
6552 else
6553 New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
6554 end if;
6556 Set_Ekind (New_Lit, E_Enumeration_Literal);
6557 Set_Enumeration_Pos (New_Lit, Enumeration_Pos (Literal));
6558 Set_Enumeration_Rep (New_Lit, Enumeration_Rep (Literal));
6559 Set_Enumeration_Rep_Expr (New_Lit, Empty);
6560 Set_Alias (New_Lit, Literal);
6561 Set_Is_Known_Valid (New_Lit, True);
6563 Append (New_Lit, Literals_List);
6564 Next_Literal (Literal);
6565 end loop;
6567 Implicit_Base :=
6568 Make_Defining_Identifier (Sloc (Derived_Type),
6569 Chars => New_External_Name (Chars (Derived_Type), 'B'));
6571 -- Indicate the proper nature of the derived type. This must be done
6572 -- before analysis of the literals, to recognize cases when a literal
6573 -- may be hidden by a previous explicit function definition (cf.
6574 -- c83031a).
6576 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
6577 Set_Etype (Derived_Type, Implicit_Base);
6579 Type_Decl :=
6580 Make_Full_Type_Declaration (Loc,
6581 Defining_Identifier => Implicit_Base,
6582 Discriminant_Specifications => No_List,
6583 Type_Definition =>
6584 Make_Enumeration_Type_Definition (Loc, Literals_List));
6586 Mark_Rewrite_Insertion (Type_Decl);
6587 Insert_Before (N, Type_Decl);
6588 Analyze (Type_Decl);
6590 -- The anonymous base now has a full declaration, but this base
6591 -- is not a first subtype.
6593 Set_Is_First_Subtype (Implicit_Base, False);
6595 -- After the implicit base is analyzed its Etype needs to be changed
6596 -- to reflect the fact that it is derived from the parent type which
6597 -- was ignored during analysis. We also set the size at this point.
6599 Set_Etype (Implicit_Base, Parent_Type);
6601 Set_Size_Info (Implicit_Base, Parent_Type);
6602 Set_RM_Size (Implicit_Base, RM_Size (Parent_Type));
6603 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
6605 -- Copy other flags from parent type
6607 Set_Has_Non_Standard_Rep
6608 (Implicit_Base, Has_Non_Standard_Rep
6609 (Parent_Type));
6610 Set_Has_Pragma_Ordered
6611 (Implicit_Base, Has_Pragma_Ordered
6612 (Parent_Type));
6613 Set_Has_Delayed_Freeze (Implicit_Base);
6615 -- Process the subtype indication including a validation check on the
6616 -- constraint, if any. If a constraint is given, its bounds must be
6617 -- implicitly converted to the new type.
6619 if Nkind (Indic) = N_Subtype_Indication then
6620 declare
6621 R : constant Node_Id :=
6622 Range_Expression (Constraint (Indic));
6624 begin
6625 if Nkind (R) = N_Range then
6626 Hi := Build_Scalar_Bound
6627 (High_Bound (R), Parent_Type, Implicit_Base);
6628 Lo := Build_Scalar_Bound
6629 (Low_Bound (R), Parent_Type, Implicit_Base);
6631 else
6632 -- Constraint is a Range attribute. Replace with explicit
6633 -- mention of the bounds of the prefix, which must be a
6634 -- subtype.
6636 Analyze (Prefix (R));
6637 Hi :=
6638 Convert_To (Implicit_Base,
6639 Make_Attribute_Reference (Loc,
6640 Attribute_Name => Name_Last,
6641 Prefix =>
6642 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
6644 Lo :=
6645 Convert_To (Implicit_Base,
6646 Make_Attribute_Reference (Loc,
6647 Attribute_Name => Name_First,
6648 Prefix =>
6649 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
6650 end if;
6651 end;
6653 else
6654 Hi :=
6655 Build_Scalar_Bound
6656 (Type_High_Bound (Parent_Type),
6657 Parent_Type, Implicit_Base);
6658 Lo :=
6659 Build_Scalar_Bound
6660 (Type_Low_Bound (Parent_Type),
6661 Parent_Type, Implicit_Base);
6662 end if;
6664 Rang_Expr :=
6665 Make_Range (Loc,
6666 Low_Bound => Lo,
6667 High_Bound => Hi);
6669 -- If we constructed a default range for the case where no range
6670 -- was given, then the expressions in the range must not freeze
6671 -- since they do not correspond to expressions in the source.
6673 if Nkind (Indic) /= N_Subtype_Indication then
6674 Set_Must_Not_Freeze (Lo);
6675 Set_Must_Not_Freeze (Hi);
6676 Set_Must_Not_Freeze (Rang_Expr);
6677 end if;
6679 Rewrite (N,
6680 Make_Subtype_Declaration (Loc,
6681 Defining_Identifier => Derived_Type,
6682 Subtype_Indication =>
6683 Make_Subtype_Indication (Loc,
6684 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
6685 Constraint =>
6686 Make_Range_Constraint (Loc,
6687 Range_Expression => Rang_Expr))));
6689 Analyze (N);
6691 -- Propagate the aspects from the original type declaration to the
6692 -- declaration of the implicit base.
6694 Move_Aspects (From => Original_Node (N), To => Type_Decl);
6696 -- Apply a range check. Since this range expression doesn't have an
6697 -- Etype, we have to specifically pass the Source_Typ parameter. Is
6698 -- this right???
6700 if Nkind (Indic) = N_Subtype_Indication then
6701 Apply_Range_Check
6702 (Range_Expression (Constraint (Indic)), Parent_Type,
6703 Source_Typ => Entity (Subtype_Mark (Indic)));
6704 end if;
6705 end if;
6706 end Build_Derived_Enumeration_Type;
6708 --------------------------------
6709 -- Build_Derived_Numeric_Type --
6710 --------------------------------
6712 procedure Build_Derived_Numeric_Type
6713 (N : Node_Id;
6714 Parent_Type : Entity_Id;
6715 Derived_Type : Entity_Id)
6717 Loc : constant Source_Ptr := Sloc (N);
6718 Tdef : constant Node_Id := Type_Definition (N);
6719 Indic : constant Node_Id := Subtype_Indication (Tdef);
6720 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
6721 No_Constraint : constant Boolean := Nkind (Indic) /=
6722 N_Subtype_Indication;
6723 Implicit_Base : Entity_Id;
6725 Lo : Node_Id;
6726 Hi : Node_Id;
6728 begin
6729 -- Process the subtype indication including a validation check on
6730 -- the constraint if any.
6732 Discard_Node (Process_Subtype (Indic, N));
6734 -- Introduce an implicit base type for the derived type even if there
6735 -- is no constraint attached to it, since this seems closer to the Ada
6736 -- semantics.
6738 Implicit_Base :=
6739 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
6741 Set_Etype (Implicit_Base, Parent_Base);
6742 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
6743 Set_Size_Info (Implicit_Base, Parent_Base);
6744 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
6745 Set_Parent (Implicit_Base, Parent (Derived_Type));
6746 Set_Is_Known_Valid (Implicit_Base, Is_Known_Valid (Parent_Base));
6748 -- Set RM Size for discrete type or decimal fixed-point type
6749 -- Ordinary fixed-point is excluded, why???
6751 if Is_Discrete_Type (Parent_Base)
6752 or else Is_Decimal_Fixed_Point_Type (Parent_Base)
6753 then
6754 Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
6755 end if;
6757 Set_Has_Delayed_Freeze (Implicit_Base);
6759 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
6760 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
6762 Set_Scalar_Range (Implicit_Base,
6763 Make_Range (Loc,
6764 Low_Bound => Lo,
6765 High_Bound => Hi));
6767 if Has_Infinities (Parent_Base) then
6768 Set_Includes_Infinities (Scalar_Range (Implicit_Base));
6769 end if;
6771 -- The Derived_Type, which is the entity of the declaration, is a
6772 -- subtype of the implicit base. Its Ekind is a subtype, even in the
6773 -- absence of an explicit constraint.
6775 Set_Etype (Derived_Type, Implicit_Base);
6777 -- If we did not have a constraint, then the Ekind is set from the
6778 -- parent type (otherwise Process_Subtype has set the bounds)
6780 if No_Constraint then
6781 Set_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
6782 end if;
6784 -- If we did not have a range constraint, then set the range from the
6785 -- parent type. Otherwise, the Process_Subtype call has set the bounds.
6787 if No_Constraint or else not Has_Range_Constraint (Indic) then
6788 Set_Scalar_Range (Derived_Type,
6789 Make_Range (Loc,
6790 Low_Bound => New_Copy_Tree (Type_Low_Bound (Parent_Type)),
6791 High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
6792 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
6794 if Has_Infinities (Parent_Type) then
6795 Set_Includes_Infinities (Scalar_Range (Derived_Type));
6796 end if;
6798 Set_Is_Known_Valid (Derived_Type, Is_Known_Valid (Parent_Type));
6799 end if;
6801 Set_Is_Descendent_Of_Address (Derived_Type,
6802 Is_Descendent_Of_Address (Parent_Type));
6803 Set_Is_Descendent_Of_Address (Implicit_Base,
6804 Is_Descendent_Of_Address (Parent_Type));
6806 -- Set remaining type-specific fields, depending on numeric type
6808 if Is_Modular_Integer_Type (Parent_Type) then
6809 Set_Modulus (Implicit_Base, Modulus (Parent_Base));
6811 Set_Non_Binary_Modulus
6812 (Implicit_Base, Non_Binary_Modulus (Parent_Base));
6814 Set_Is_Known_Valid
6815 (Implicit_Base, Is_Known_Valid (Parent_Base));
6817 elsif Is_Floating_Point_Type (Parent_Type) then
6819 -- Digits of base type is always copied from the digits value of
6820 -- the parent base type, but the digits of the derived type will
6821 -- already have been set if there was a constraint present.
6823 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
6824 Set_Float_Rep (Implicit_Base, Float_Rep (Parent_Base));
6826 if No_Constraint then
6827 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
6828 end if;
6830 elsif Is_Fixed_Point_Type (Parent_Type) then
6832 -- Small of base type and derived type are always copied from the
6833 -- parent base type, since smalls never change. The delta of the
6834 -- base type is also copied from the parent base type. However the
6835 -- delta of the derived type will have been set already if a
6836 -- constraint was present.
6838 Set_Small_Value (Derived_Type, Small_Value (Parent_Base));
6839 Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
6840 Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
6842 if No_Constraint then
6843 Set_Delta_Value (Derived_Type, Delta_Value (Parent_Type));
6844 end if;
6846 -- The scale and machine radix in the decimal case are always
6847 -- copied from the parent base type.
6849 if Is_Decimal_Fixed_Point_Type (Parent_Type) then
6850 Set_Scale_Value (Derived_Type, Scale_Value (Parent_Base));
6851 Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
6853 Set_Machine_Radix_10
6854 (Derived_Type, Machine_Radix_10 (Parent_Base));
6855 Set_Machine_Radix_10
6856 (Implicit_Base, Machine_Radix_10 (Parent_Base));
6858 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
6860 if No_Constraint then
6861 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
6863 else
6864 -- the analysis of the subtype_indication sets the
6865 -- digits value of the derived type.
6867 null;
6868 end if;
6869 end if;
6870 end if;
6872 if Is_Integer_Type (Parent_Type) then
6873 Set_Has_Shift_Operator
6874 (Implicit_Base, Has_Shift_Operator (Parent_Type));
6875 end if;
6877 -- The type of the bounds is that of the parent type, and they
6878 -- must be converted to the derived type.
6880 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
6882 -- The implicit_base should be frozen when the derived type is frozen,
6883 -- but note that it is used in the conversions of the bounds. For fixed
6884 -- types we delay the determination of the bounds until the proper
6885 -- freezing point. For other numeric types this is rejected by GCC, for
6886 -- reasons that are currently unclear (???), so we choose to freeze the
6887 -- implicit base now. In the case of integers and floating point types
6888 -- this is harmless because subsequent representation clauses cannot
6889 -- affect anything, but it is still baffling that we cannot use the
6890 -- same mechanism for all derived numeric types.
6892 -- There is a further complication: actually some representation
6893 -- clauses can affect the implicit base type. For example, attribute
6894 -- definition clauses for stream-oriented attributes need to set the
6895 -- corresponding TSS entries on the base type, and this normally
6896 -- cannot be done after the base type is frozen, so the circuitry in
6897 -- Sem_Ch13.New_Stream_Subprogram must account for this possibility
6898 -- and not use Set_TSS in this case.
6900 -- There are also consequences for the case of delayed representation
6901 -- aspects for some cases. For example, a Size aspect is delayed and
6902 -- should not be evaluated to the freeze point. This early freezing
6903 -- means that the size attribute evaluation happens too early???
6905 if Is_Fixed_Point_Type (Parent_Type) then
6906 Conditional_Delay (Implicit_Base, Parent_Type);
6907 else
6908 Freeze_Before (N, Implicit_Base);
6909 end if;
6910 end Build_Derived_Numeric_Type;
6912 --------------------------------
6913 -- Build_Derived_Private_Type --
6914 --------------------------------
6916 procedure Build_Derived_Private_Type
6917 (N : Node_Id;
6918 Parent_Type : Entity_Id;
6919 Derived_Type : Entity_Id;
6920 Is_Completion : Boolean;
6921 Derive_Subps : Boolean := True)
6923 Loc : constant Source_Ptr := Sloc (N);
6924 Par_Base : constant Entity_Id := Base_Type (Parent_Type);
6925 Par_Scope : constant Entity_Id := Scope (Par_Base);
6926 Full_N : constant Node_Id := New_Copy_Tree (N);
6927 Full_Der : Entity_Id := New_Copy (Derived_Type);
6928 Full_P : Entity_Id;
6930 procedure Build_Full_Derivation;
6931 -- Build full derivation, i.e. derive from the full view
6933 procedure Copy_And_Build;
6934 -- Copy derived type declaration, replace parent with its full view,
6935 -- and build derivation
6937 ---------------------------
6938 -- Build_Full_Derivation --
6939 ---------------------------
6941 procedure Build_Full_Derivation is
6942 begin
6943 -- If parent scope is not open, install the declarations
6945 if not In_Open_Scopes (Par_Scope) then
6946 Install_Private_Declarations (Par_Scope);
6947 Install_Visible_Declarations (Par_Scope);
6948 Copy_And_Build;
6949 Uninstall_Declarations (Par_Scope);
6951 -- If parent scope is open and in another unit, and parent has a
6952 -- completion, then the derivation is taking place in the visible
6953 -- part of a child unit. In that case retrieve the full view of
6954 -- the parent momentarily.
6956 elsif not In_Same_Source_Unit (N, Parent_Type) then
6957 Full_P := Full_View (Parent_Type);
6958 Exchange_Declarations (Parent_Type);
6959 Copy_And_Build;
6960 Exchange_Declarations (Full_P);
6962 -- Otherwise it is a local derivation
6964 else
6965 Copy_And_Build;
6966 end if;
6967 end Build_Full_Derivation;
6969 --------------------
6970 -- Copy_And_Build --
6971 --------------------
6973 procedure Copy_And_Build is
6974 Full_Parent : Entity_Id := Parent_Type;
6976 begin
6977 -- If the parent is itself derived from another private type,
6978 -- installing the private declarations has not affected its
6979 -- privacy status, so use its own full view explicitly.
6981 if Is_Private_Type (Full_Parent)
6982 and then Present (Full_View (Full_Parent))
6983 then
6984 Full_Parent := Full_View (Full_Parent);
6985 end if;
6987 -- And its underlying full view if necessary
6989 if Is_Private_Type (Full_Parent)
6990 and then Present (Underlying_Full_View (Full_Parent))
6991 then
6992 Full_Parent := Underlying_Full_View (Full_Parent);
6993 end if;
6995 -- For record, access and most enumeration types, derivation from
6996 -- the full view requires a fully-fledged declaration. In the other
6997 -- cases, just use an itype.
6999 if Ekind (Full_Parent) in Record_Kind
7000 or else Ekind (Full_Parent) in Access_Kind
7001 or else
7002 (Ekind (Full_Parent) in Enumeration_Kind
7003 and then not Is_Standard_Character_Type (Full_Parent)
7004 and then not Is_Generic_Type (Root_Type (Full_Parent)))
7005 then
7006 -- Copy and adjust declaration to provide a completion for what
7007 -- is originally a private declaration. Indicate that full view
7008 -- is internally generated.
7010 Set_Comes_From_Source (Full_N, False);
7011 Set_Comes_From_Source (Full_Der, False);
7012 Set_Parent (Full_Der, Full_N);
7013 Set_Defining_Identifier (Full_N, Full_Der);
7015 -- If there are no constraints, adjust the subtype mark
7017 if Nkind (Subtype_Indication (Type_Definition (Full_N))) /=
7018 N_Subtype_Indication
7019 then
7020 Set_Subtype_Indication
7021 (Type_Definition (Full_N),
7022 New_Occurrence_Of (Full_Parent, Sloc (Full_N)));
7023 end if;
7025 Insert_After (N, Full_N);
7027 -- Build full view of derived type from full view of parent which
7028 -- is now installed. Subprograms have been derived on the partial
7029 -- view, the completion does not derive them anew.
7031 if Ekind (Full_Parent) in Record_Kind then
7033 -- If parent type is tagged, the completion inherits the proper
7034 -- primitive operations.
7036 if Is_Tagged_Type (Parent_Type) then
7037 Build_Derived_Record_Type
7038 (Full_N, Full_Parent, Full_Der, Derive_Subps);
7039 else
7040 Build_Derived_Record_Type
7041 (Full_N, Full_Parent, Full_Der, Derive_Subps => False);
7042 end if;
7044 else
7045 Build_Derived_Type
7046 (Full_N, Full_Parent, Full_Der,
7047 Is_Completion => False, Derive_Subps => False);
7048 end if;
7050 -- The full declaration has been introduced into the tree and
7051 -- processed in the step above. It should not be analyzed again
7052 -- (when encountered later in the current list of declarations)
7053 -- to prevent spurious name conflicts. The full entity remains
7054 -- invisible.
7056 Set_Analyzed (Full_N);
7058 else
7059 Full_Der :=
7060 Make_Defining_Identifier (Sloc (Derived_Type),
7061 Chars => Chars (Derived_Type));
7062 Set_Is_Itype (Full_Der);
7063 Set_Associated_Node_For_Itype (Full_Der, N);
7064 Set_Parent (Full_Der, N);
7065 Build_Derived_Type
7066 (N, Full_Parent, Full_Der,
7067 Is_Completion => False, Derive_Subps => False);
7068 end if;
7070 Set_Has_Private_Declaration (Full_Der);
7071 Set_Has_Private_Declaration (Derived_Type);
7073 Set_Scope (Full_Der, Scope (Derived_Type));
7074 Set_Is_First_Subtype (Full_Der, Is_First_Subtype (Derived_Type));
7075 Set_Has_Size_Clause (Full_Der, False);
7076 Set_Has_Alignment_Clause (Full_Der, False);
7077 Set_Has_Delayed_Freeze (Full_Der);
7078 Set_Is_Frozen (Full_Der, False);
7079 Set_Freeze_Node (Full_Der, Empty);
7080 Set_Depends_On_Private (Full_Der, Has_Private_Component (Full_Der));
7081 Set_Is_Public (Full_Der, Is_Public (Derived_Type));
7083 -- The convention on the base type may be set in the private part
7084 -- and not propagated to the subtype until later, so we obtain the
7085 -- convention from the base type of the parent.
7087 Set_Convention (Full_Der, Convention (Base_Type (Full_Parent)));
7088 end Copy_And_Build;
7090 -- Start of processing for Build_Derived_Private_Type
7092 begin
7093 if Is_Tagged_Type (Parent_Type) then
7094 Full_P := Full_View (Parent_Type);
7096 -- A type extension of a type with unknown discriminants is an
7097 -- indefinite type that the back-end cannot handle directly.
7098 -- We treat it as a private type, and build a completion that is
7099 -- derived from the full view of the parent, and hopefully has
7100 -- known discriminants.
7102 -- If the full view of the parent type has an underlying record view,
7103 -- use it to generate the underlying record view of this derived type
7104 -- (required for chains of derivations with unknown discriminants).
7106 -- Minor optimization: we avoid the generation of useless underlying
7107 -- record view entities if the private type declaration has unknown
7108 -- discriminants but its corresponding full view has no
7109 -- discriminants.
7111 if Has_Unknown_Discriminants (Parent_Type)
7112 and then Present (Full_P)
7113 and then (Has_Discriminants (Full_P)
7114 or else Present (Underlying_Record_View (Full_P)))
7115 and then not In_Open_Scopes (Par_Scope)
7116 and then Expander_Active
7117 then
7118 declare
7119 Full_Der : constant Entity_Id := Make_Temporary (Loc, 'T');
7120 New_Ext : constant Node_Id :=
7121 Copy_Separate_Tree
7122 (Record_Extension_Part (Type_Definition (N)));
7123 Decl : Node_Id;
7125 begin
7126 Build_Derived_Record_Type
7127 (N, Parent_Type, Derived_Type, Derive_Subps);
7129 -- Build anonymous completion, as a derivation from the full
7130 -- view of the parent. This is not a completion in the usual
7131 -- sense, because the current type is not private.
7133 Decl :=
7134 Make_Full_Type_Declaration (Loc,
7135 Defining_Identifier => Full_Der,
7136 Type_Definition =>
7137 Make_Derived_Type_Definition (Loc,
7138 Subtype_Indication =>
7139 New_Copy_Tree
7140 (Subtype_Indication (Type_Definition (N))),
7141 Record_Extension_Part => New_Ext));
7143 -- If the parent type has an underlying record view, use it
7144 -- here to build the new underlying record view.
7146 if Present (Underlying_Record_View (Full_P)) then
7147 pragma Assert
7148 (Nkind (Subtype_Indication (Type_Definition (Decl)))
7149 = N_Identifier);
7150 Set_Entity (Subtype_Indication (Type_Definition (Decl)),
7151 Underlying_Record_View (Full_P));
7152 end if;
7154 Install_Private_Declarations (Par_Scope);
7155 Install_Visible_Declarations (Par_Scope);
7156 Insert_Before (N, Decl);
7158 -- Mark entity as an underlying record view before analysis,
7159 -- to avoid generating the list of its primitive operations
7160 -- (which is not really required for this entity) and thus
7161 -- prevent spurious errors associated with missing overriding
7162 -- of abstract primitives (overridden only for Derived_Type).
7164 Set_Ekind (Full_Der, E_Record_Type);
7165 Set_Is_Underlying_Record_View (Full_Der);
7166 Set_Default_SSO (Full_Der);
7168 Analyze (Decl);
7170 pragma Assert (Has_Discriminants (Full_Der)
7171 and then not Has_Unknown_Discriminants (Full_Der));
7173 Uninstall_Declarations (Par_Scope);
7175 -- Freeze the underlying record view, to prevent generation of
7176 -- useless dispatching information, which is simply shared with
7177 -- the real derived type.
7179 Set_Is_Frozen (Full_Der);
7181 -- If the derived type has access discriminants, create
7182 -- references to their anonymous types now, to prevent
7183 -- back-end problems when their first use is in generated
7184 -- bodies of primitives.
7186 declare
7187 E : Entity_Id;
7189 begin
7190 E := First_Entity (Full_Der);
7192 while Present (E) loop
7193 if Ekind (E) = E_Discriminant
7194 and then Ekind (Etype (E)) = E_Anonymous_Access_Type
7195 then
7196 Build_Itype_Reference (Etype (E), Decl);
7197 end if;
7199 Next_Entity (E);
7200 end loop;
7201 end;
7203 -- Set up links between real entity and underlying record view
7205 Set_Underlying_Record_View (Derived_Type, Base_Type (Full_Der));
7206 Set_Underlying_Record_View (Base_Type (Full_Der), Derived_Type);
7207 end;
7209 -- If discriminants are known, build derived record
7211 else
7212 Build_Derived_Record_Type
7213 (N, Parent_Type, Derived_Type, Derive_Subps);
7214 end if;
7216 return;
7218 elsif Has_Discriminants (Parent_Type) then
7220 -- Build partial view of derived type from partial view of parent.
7221 -- This must be done before building the full derivation because the
7222 -- second derivation will modify the discriminants of the first and
7223 -- the discriminants are chained with the rest of the components in
7224 -- the full derivation.
7226 Build_Derived_Record_Type
7227 (N, Parent_Type, Derived_Type, Derive_Subps);
7229 -- Build the full derivation if this is not the anonymous derived
7230 -- base type created by Build_Derived_Record_Type in the constrained
7231 -- case (see point 5. of its head comment) since we build it for the
7232 -- derived subtype. And skip it for protected types altogether, as
7233 -- gigi does not use these types directly.
7235 if Present (Full_View (Parent_Type))
7236 and then not Is_Itype (Derived_Type)
7237 and then not (Ekind (Full_View (Parent_Type)) in Protected_Kind)
7238 then
7239 declare
7240 Der_Base : constant Entity_Id := Base_Type (Derived_Type);
7241 Discr : Entity_Id;
7242 Last_Discr : Entity_Id;
7244 begin
7245 -- If this is not a completion, construct the implicit full
7246 -- view by deriving from the full view of the parent type.
7247 -- But if this is a completion, the derived private type
7248 -- being built is a full view and the full derivation can
7249 -- only be its underlying full view.
7251 Build_Full_Derivation;
7253 if not Is_Completion then
7254 Set_Full_View (Derived_Type, Full_Der);
7255 else
7256 Set_Underlying_Full_View (Derived_Type, Full_Der);
7257 end if;
7259 if not Is_Base_Type (Derived_Type) then
7260 Set_Full_View (Der_Base, Base_Type (Full_Der));
7261 end if;
7263 -- Copy the discriminant list from full view to the partial
7264 -- view (base type and its subtype). Gigi requires that the
7265 -- partial and full views have the same discriminants.
7267 -- Note that since the partial view points to discriminants
7268 -- in the full view, their scope will be that of the full
7269 -- view. This might cause some front end problems and need
7270 -- adjustment???
7272 Discr := First_Discriminant (Base_Type (Full_Der));
7273 Set_First_Entity (Der_Base, Discr);
7275 loop
7276 Last_Discr := Discr;
7277 Next_Discriminant (Discr);
7278 exit when No (Discr);
7279 end loop;
7281 Set_Last_Entity (Der_Base, Last_Discr);
7282 Set_First_Entity (Derived_Type, First_Entity (Der_Base));
7283 Set_Last_Entity (Derived_Type, Last_Entity (Der_Base));
7285 Set_Stored_Constraint
7286 (Full_Der, Stored_Constraint (Derived_Type));
7287 end;
7288 end if;
7290 elsif Present (Full_View (Parent_Type))
7291 and then Has_Discriminants (Full_View (Parent_Type))
7292 then
7293 if Has_Unknown_Discriminants (Parent_Type)
7294 and then Nkind (Subtype_Indication (Type_Definition (N))) =
7295 N_Subtype_Indication
7296 then
7297 Error_Msg_N
7298 ("cannot constrain type with unknown discriminants",
7299 Subtype_Indication (Type_Definition (N)));
7300 return;
7301 end if;
7303 -- If this is not a completion, construct the implicit full view by
7304 -- deriving from the full view of the parent type. But if this is a
7305 -- completion, the derived private type being built is a full view
7306 -- and the full derivation can only be its underlying full view.
7308 Build_Full_Derivation;
7310 if not Is_Completion then
7311 Set_Full_View (Derived_Type, Full_Der);
7312 else
7313 Set_Underlying_Full_View (Derived_Type, Full_Der);
7314 end if;
7316 -- In any case, the primitive operations are inherited from the
7317 -- parent type, not from the internal full view.
7319 Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
7321 if Derive_Subps then
7322 Derive_Subprograms (Parent_Type, Derived_Type);
7323 end if;
7325 Set_Stored_Constraint (Derived_Type, No_Elist);
7326 Set_Is_Constrained
7327 (Derived_Type, Is_Constrained (Full_View (Parent_Type)));
7329 else
7330 -- Untagged type, No discriminants on either view
7332 if Nkind (Subtype_Indication (Type_Definition (N))) =
7333 N_Subtype_Indication
7334 then
7335 Error_Msg_N
7336 ("illegal constraint on type without discriminants", N);
7337 end if;
7339 if Present (Discriminant_Specifications (N))
7340 and then Present (Full_View (Parent_Type))
7341 and then not Is_Tagged_Type (Full_View (Parent_Type))
7342 then
7343 Error_Msg_N ("cannot add discriminants to untagged type", N);
7344 end if;
7346 Set_Stored_Constraint (Derived_Type, No_Elist);
7347 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7348 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
7349 Set_Has_Controlled_Component
7350 (Derived_Type, Has_Controlled_Component
7351 (Parent_Type));
7353 -- Direct controlled types do not inherit Finalize_Storage_Only flag
7355 if not Is_Controlled (Parent_Type) then
7356 Set_Finalize_Storage_Only
7357 (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
7358 end if;
7360 -- If this is not a completion, construct the implicit full view by
7361 -- deriving from the full view of the parent type.
7363 -- ??? If the parent is untagged private and its completion is
7364 -- tagged, this mechanism will not work because we cannot derive from
7365 -- the tagged full view unless we have an extension.
7367 if Present (Full_View (Parent_Type))
7368 and then not Is_Tagged_Type (Full_View (Parent_Type))
7369 and then not Is_Completion
7370 then
7371 Build_Full_Derivation;
7372 Set_Full_View (Derived_Type, Full_Der);
7373 end if;
7374 end if;
7376 Set_Has_Unknown_Discriminants (Derived_Type,
7377 Has_Unknown_Discriminants (Parent_Type));
7379 if Is_Private_Type (Derived_Type) then
7380 Set_Private_Dependents (Derived_Type, New_Elmt_List);
7381 end if;
7383 -- If the parent base type is in scope, add the derived type to its
7384 -- list of private dependents, because its full view may become
7385 -- visible subsequently (in a nested private part, a body, or in a
7386 -- further child unit).
7388 if Is_Private_Type (Par_Base) and then In_Open_Scopes (Par_Scope) then
7389 Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
7391 -- Check for unusual case where a type completed by a private
7392 -- derivation occurs within a package nested in a child unit, and
7393 -- the parent is declared in an ancestor.
7395 if Is_Child_Unit (Scope (Current_Scope))
7396 and then Is_Completion
7397 and then In_Private_Part (Current_Scope)
7398 and then Scope (Parent_Type) /= Current_Scope
7400 -- Note that if the parent has a completion in the private part,
7401 -- (which is itself a derivation from some other private type)
7402 -- it is that completion that is visible, there is no full view
7403 -- available, and no special processing is needed.
7405 and then Present (Full_View (Parent_Type))
7406 then
7407 -- In this case, the full view of the parent type will become
7408 -- visible in the body of the enclosing child, and only then will
7409 -- the current type be possibly non-private. Build an underlying
7410 -- full view that will be installed when the enclosing child body
7411 -- is compiled.
7413 if Present (Underlying_Full_View (Derived_Type)) then
7414 Full_Der := Underlying_Full_View (Derived_Type);
7415 else
7416 Build_Full_Derivation;
7417 Set_Underlying_Full_View (Derived_Type, Full_Der);
7418 end if;
7420 -- The full view will be used to swap entities on entry/exit to
7421 -- the body, and must appear in the entity list for the package.
7423 Append_Entity (Full_Der, Scope (Derived_Type));
7424 end if;
7425 end if;
7426 end Build_Derived_Private_Type;
7428 -------------------------------
7429 -- Build_Derived_Record_Type --
7430 -------------------------------
7432 -- 1. INTRODUCTION
7434 -- Ideally we would like to use the same model of type derivation for
7435 -- tagged and untagged record types. Unfortunately this is not quite
7436 -- possible because the semantics of representation clauses is different
7437 -- for tagged and untagged records under inheritance. Consider the
7438 -- following:
7440 -- type R (...) is [tagged] record ... end record;
7441 -- type T (...) is new R (...) [with ...];
7443 -- The representation clauses for T can specify a completely different
7444 -- record layout from R's. Hence the same component can be placed in two
7445 -- very different positions in objects of type T and R. If R and T are
7446 -- tagged types, representation clauses for T can only specify the layout
7447 -- of non inherited components, thus components that are common in R and T
7448 -- have the same position in objects of type R and T.
7450 -- This has two implications. The first is that the entire tree for R's
7451 -- declaration needs to be copied for T in the untagged case, so that T
7452 -- can be viewed as a record type of its own with its own representation
7453 -- clauses. The second implication is the way we handle discriminants.
7454 -- Specifically, in the untagged case we need a way to communicate to Gigi
7455 -- what are the real discriminants in the record, while for the semantics
7456 -- we need to consider those introduced by the user to rename the
7457 -- discriminants in the parent type. This is handled by introducing the
7458 -- notion of stored discriminants. See below for more.
7460 -- Fortunately the way regular components are inherited can be handled in
7461 -- the same way in tagged and untagged types.
7463 -- To complicate things a bit more the private view of a private extension
7464 -- cannot be handled in the same way as the full view (for one thing the
7465 -- semantic rules are somewhat different). We will explain what differs
7466 -- below.
7468 -- 2. DISCRIMINANTS UNDER INHERITANCE
7470 -- The semantic rules governing the discriminants of derived types are
7471 -- quite subtle.
7473 -- type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
7474 -- [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
7476 -- If parent type has discriminants, then the discriminants that are
7477 -- declared in the derived type are [3.4 (11)]:
7479 -- o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
7480 -- there is one;
7482 -- o Otherwise, each discriminant of the parent type (implicitly declared
7483 -- in the same order with the same specifications). In this case, the
7484 -- discriminants are said to be "inherited", or if unknown in the parent
7485 -- are also unknown in the derived type.
7487 -- Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
7489 -- o The parent subtype must be constrained;
7491 -- o If the parent type is not a tagged type, then each discriminant of
7492 -- the derived type must be used in the constraint defining a parent
7493 -- subtype. [Implementation note: This ensures that the new discriminant
7494 -- can share storage with an existing discriminant.]
7496 -- For the derived type each discriminant of the parent type is either
7497 -- inherited, constrained to equal some new discriminant of the derived
7498 -- type, or constrained to the value of an expression.
7500 -- When inherited or constrained to equal some new discriminant, the
7501 -- parent discriminant and the discriminant of the derived type are said
7502 -- to "correspond".
7504 -- If a discriminant of the parent type is constrained to a specific value
7505 -- in the derived type definition, then the discriminant is said to be
7506 -- "specified" by that derived type definition.
7508 -- 3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
7510 -- We have spoken about stored discriminants in point 1 (introduction)
7511 -- above. There are two sort of stored discriminants: implicit and
7512 -- explicit. As long as the derived type inherits the same discriminants as
7513 -- the root record type, stored discriminants are the same as regular
7514 -- discriminants, and are said to be implicit. However, if any discriminant
7515 -- in the root type was renamed in the derived type, then the derived
7516 -- type will contain explicit stored discriminants. Explicit stored
7517 -- discriminants are discriminants in addition to the semantically visible
7518 -- discriminants defined for the derived type. Stored discriminants are
7519 -- used by Gigi to figure out what are the physical discriminants in
7520 -- objects of the derived type (see precise definition in einfo.ads).
7521 -- As an example, consider the following:
7523 -- type R (D1, D2, D3 : Int) is record ... end record;
7524 -- type T1 is new R;
7525 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
7526 -- type T3 is new T2;
7527 -- type T4 (Y : Int) is new T3 (Y, 99);
7529 -- The following table summarizes the discriminants and stored
7530 -- discriminants in R and T1 through T4.
7532 -- Type Discrim Stored Discrim Comment
7533 -- R (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in R
7534 -- T1 (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in T1
7535 -- T2 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T2
7536 -- T3 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T3
7537 -- T4 (Y) (D1, D2, D3) Girder discrims EXPLICIT in T4
7539 -- Field Corresponding_Discriminant (abbreviated CD below) allows us to
7540 -- find the corresponding discriminant in the parent type, while
7541 -- Original_Record_Component (abbreviated ORC below), the actual physical
7542 -- component that is renamed. Finally the field Is_Completely_Hidden
7543 -- (abbreviated ICH below) is set for all explicit stored discriminants
7544 -- (see einfo.ads for more info). For the above example this gives:
7546 -- Discrim CD ORC ICH
7547 -- ^^^^^^^ ^^ ^^^ ^^^
7548 -- D1 in R empty itself no
7549 -- D2 in R empty itself no
7550 -- D3 in R empty itself no
7552 -- D1 in T1 D1 in R itself no
7553 -- D2 in T1 D2 in R itself no
7554 -- D3 in T1 D3 in R itself no
7556 -- X1 in T2 D3 in T1 D3 in T2 no
7557 -- X2 in T2 D1 in T1 D1 in T2 no
7558 -- D1 in T2 empty itself yes
7559 -- D2 in T2 empty itself yes
7560 -- D3 in T2 empty itself yes
7562 -- X1 in T3 X1 in T2 D3 in T3 no
7563 -- X2 in T3 X2 in T2 D1 in T3 no
7564 -- D1 in T3 empty itself yes
7565 -- D2 in T3 empty itself yes
7566 -- D3 in T3 empty itself yes
7568 -- Y in T4 X1 in T3 D3 in T3 no
7569 -- D1 in T3 empty itself yes
7570 -- D2 in T3 empty itself yes
7571 -- D3 in T3 empty itself yes
7573 -- 4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
7575 -- Type derivation for tagged types is fairly straightforward. If no
7576 -- discriminants are specified by the derived type, these are inherited
7577 -- from the parent. No explicit stored discriminants are ever necessary.
7578 -- The only manipulation that is done to the tree is that of adding a
7579 -- _parent field with parent type and constrained to the same constraint
7580 -- specified for the parent in the derived type definition. For instance:
7582 -- type R (D1, D2, D3 : Int) is tagged record ... end record;
7583 -- type T1 is new R with null record;
7584 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
7586 -- are changed into:
7588 -- type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
7589 -- _parent : R (D1, D2, D3);
7590 -- end record;
7592 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
7593 -- _parent : T1 (X2, 88, X1);
7594 -- end record;
7596 -- The discriminants actually present in R, T1 and T2 as well as their CD,
7597 -- ORC and ICH fields are:
7599 -- Discrim CD ORC ICH
7600 -- ^^^^^^^ ^^ ^^^ ^^^
7601 -- D1 in R empty itself no
7602 -- D2 in R empty itself no
7603 -- D3 in R empty itself no
7605 -- D1 in T1 D1 in R D1 in R no
7606 -- D2 in T1 D2 in R D2 in R no
7607 -- D3 in T1 D3 in R D3 in R no
7609 -- X1 in T2 D3 in T1 D3 in R no
7610 -- X2 in T2 D1 in T1 D1 in R no
7612 -- 5. FIRST TRANSFORMATION FOR DERIVED RECORDS
7614 -- Regardless of whether we dealing with a tagged or untagged type
7615 -- we will transform all derived type declarations of the form
7617 -- type T is new R (...) [with ...];
7618 -- or
7619 -- subtype S is R (...);
7620 -- type T is new S [with ...];
7621 -- into
7622 -- type BT is new R [with ...];
7623 -- subtype T is BT (...);
7625 -- That is, the base derived type is constrained only if it has no
7626 -- discriminants. The reason for doing this is that GNAT's semantic model
7627 -- assumes that a base type with discriminants is unconstrained.
7629 -- Note that, strictly speaking, the above transformation is not always
7630 -- correct. Consider for instance the following excerpt from ACVC b34011a:
7632 -- procedure B34011A is
7633 -- type REC (D : integer := 0) is record
7634 -- I : Integer;
7635 -- end record;
7637 -- package P is
7638 -- type T6 is new Rec;
7639 -- function F return T6;
7640 -- end P;
7642 -- use P;
7643 -- package Q6 is
7644 -- type U is new T6 (Q6.F.I); -- ERROR: Q6.F.
7645 -- end Q6;
7647 -- The definition of Q6.U is illegal. However transforming Q6.U into
7649 -- type BaseU is new T6;
7650 -- subtype U is BaseU (Q6.F.I)
7652 -- turns U into a legal subtype, which is incorrect. To avoid this problem
7653 -- we always analyze the constraint (in this case (Q6.F.I)) before applying
7654 -- the transformation described above.
7656 -- There is another instance where the above transformation is incorrect.
7657 -- Consider:
7659 -- package Pack is
7660 -- type Base (D : Integer) is tagged null record;
7661 -- procedure P (X : Base);
7663 -- type Der is new Base (2) with null record;
7664 -- procedure P (X : Der);
7665 -- end Pack;
7667 -- Then the above transformation turns this into
7669 -- type Der_Base is new Base with null record;
7670 -- -- procedure P (X : Base) is implicitly inherited here
7671 -- -- as procedure P (X : Der_Base).
7673 -- subtype Der is Der_Base (2);
7674 -- procedure P (X : Der);
7675 -- -- The overriding of P (X : Der_Base) is illegal since we
7676 -- -- have a parameter conformance problem.
7678 -- To get around this problem, after having semantically processed Der_Base
7679 -- and the rewritten subtype declaration for Der, we copy Der_Base field
7680 -- Discriminant_Constraint from Der so that when parameter conformance is
7681 -- checked when P is overridden, no semantic errors are flagged.
7683 -- 6. SECOND TRANSFORMATION FOR DERIVED RECORDS
7685 -- Regardless of whether we are dealing with a tagged or untagged type
7686 -- we will transform all derived type declarations of the form
7688 -- type R (D1, .., Dn : ...) is [tagged] record ...;
7689 -- type T is new R [with ...];
7690 -- into
7691 -- type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
7693 -- The reason for such transformation is that it allows us to implement a
7694 -- very clean form of component inheritance as explained below.
7696 -- Note that this transformation is not achieved by direct tree rewriting
7697 -- and manipulation, but rather by redoing the semantic actions that the
7698 -- above transformation will entail. This is done directly in routine
7699 -- Inherit_Components.
7701 -- 7. TYPE DERIVATION AND COMPONENT INHERITANCE
7703 -- In both tagged and untagged derived types, regular non discriminant
7704 -- components are inherited in the derived type from the parent type. In
7705 -- the absence of discriminants component, inheritance is straightforward
7706 -- as components can simply be copied from the parent.
7708 -- If the parent has discriminants, inheriting components constrained with
7709 -- these discriminants requires caution. Consider the following example:
7711 -- type R (D1, D2 : Positive) is [tagged] record
7712 -- S : String (D1 .. D2);
7713 -- end record;
7715 -- type T1 is new R [with null record];
7716 -- type T2 (X : positive) is new R (1, X) [with null record];
7718 -- As explained in 6. above, T1 is rewritten as
7719 -- type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
7720 -- which makes the treatment for T1 and T2 identical.
7722 -- What we want when inheriting S, is that references to D1 and D2 in R are
7723 -- replaced with references to their correct constraints, i.e. D1 and D2 in
7724 -- T1 and 1 and X in T2. So all R's discriminant references are replaced
7725 -- with either discriminant references in the derived type or expressions.
7726 -- This replacement is achieved as follows: before inheriting R's
7727 -- components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
7728 -- created in the scope of T1 (resp. scope of T2) so that discriminants D1
7729 -- and D2 of T1 are visible (resp. discriminant X of T2 is visible).
7730 -- For T2, for instance, this has the effect of replacing String (D1 .. D2)
7731 -- by String (1 .. X).
7733 -- 8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
7735 -- We explain here the rules governing private type extensions relevant to
7736 -- type derivation. These rules are explained on the following example:
7738 -- type D [(...)] is new A [(...)] with private; <-- partial view
7739 -- type D [(...)] is new P [(...)] with null record; <-- full view
7741 -- Type A is called the ancestor subtype of the private extension.
7742 -- Type P is the parent type of the full view of the private extension. It
7743 -- must be A or a type derived from A.
7745 -- The rules concerning the discriminants of private type extensions are
7746 -- [7.3(10-13)]:
7748 -- o If a private extension inherits known discriminants from the ancestor
7749 -- subtype, then the full view must also inherit its discriminants from
7750 -- the ancestor subtype and the parent subtype of the full view must be
7751 -- constrained if and only if the ancestor subtype is constrained.
7753 -- o If a partial view has unknown discriminants, then the full view may
7754 -- define a definite or an indefinite subtype, with or without
7755 -- discriminants.
7757 -- o If a partial view has neither known nor unknown discriminants, then
7758 -- the full view must define a definite subtype.
7760 -- o If the ancestor subtype of a private extension has constrained
7761 -- discriminants, then the parent subtype of the full view must impose a
7762 -- statically matching constraint on those discriminants.
7764 -- This means that only the following forms of private extensions are
7765 -- allowed:
7767 -- type D is new A with private; <-- partial view
7768 -- type D is new P with null record; <-- full view
7770 -- If A has no discriminants than P has no discriminants, otherwise P must
7771 -- inherit A's discriminants.
7773 -- type D is new A (...) with private; <-- partial view
7774 -- type D is new P (:::) with null record; <-- full view
7776 -- P must inherit A's discriminants and (...) and (:::) must statically
7777 -- match.
7779 -- subtype A is R (...);
7780 -- type D is new A with private; <-- partial view
7781 -- type D is new P with null record; <-- full view
7783 -- P must have inherited R's discriminants and must be derived from A or
7784 -- any of its subtypes.
7786 -- type D (..) is new A with private; <-- partial view
7787 -- type D (..) is new P [(:::)] with null record; <-- full view
7789 -- No specific constraints on P's discriminants or constraint (:::).
7790 -- Note that A can be unconstrained, but the parent subtype P must either
7791 -- be constrained or (:::) must be present.
7793 -- type D (..) is new A [(...)] with private; <-- partial view
7794 -- type D (..) is new P [(:::)] with null record; <-- full view
7796 -- P's constraints on A's discriminants must statically match those
7797 -- imposed by (...).
7799 -- 9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
7801 -- The full view of a private extension is handled exactly as described
7802 -- above. The model chose for the private view of a private extension is
7803 -- the same for what concerns discriminants (i.e. they receive the same
7804 -- treatment as in the tagged case). However, the private view of the
7805 -- private extension always inherits the components of the parent base,
7806 -- without replacing any discriminant reference. Strictly speaking this is
7807 -- incorrect. However, Gigi never uses this view to generate code so this
7808 -- is a purely semantic issue. In theory, a set of transformations similar
7809 -- to those given in 5. and 6. above could be applied to private views of
7810 -- private extensions to have the same model of component inheritance as
7811 -- for non private extensions. However, this is not done because it would
7812 -- further complicate private type processing. Semantically speaking, this
7813 -- leaves us in an uncomfortable situation. As an example consider:
7815 -- package Pack is
7816 -- type R (D : integer) is tagged record
7817 -- S : String (1 .. D);
7818 -- end record;
7819 -- procedure P (X : R);
7820 -- type T is new R (1) with private;
7821 -- private
7822 -- type T is new R (1) with null record;
7823 -- end;
7825 -- This is transformed into:
7827 -- package Pack is
7828 -- type R (D : integer) is tagged record
7829 -- S : String (1 .. D);
7830 -- end record;
7831 -- procedure P (X : R);
7832 -- type T is new R (1) with private;
7833 -- private
7834 -- type BaseT is new R with null record;
7835 -- subtype T is BaseT (1);
7836 -- end;
7838 -- (strictly speaking the above is incorrect Ada)
7840 -- From the semantic standpoint the private view of private extension T
7841 -- should be flagged as constrained since one can clearly have
7843 -- Obj : T;
7845 -- in a unit withing Pack. However, when deriving subprograms for the
7846 -- private view of private extension T, T must be seen as unconstrained
7847 -- since T has discriminants (this is a constraint of the current
7848 -- subprogram derivation model). Thus, when processing the private view of
7849 -- a private extension such as T, we first mark T as unconstrained, we
7850 -- process it, we perform program derivation and just before returning from
7851 -- Build_Derived_Record_Type we mark T as constrained.
7853 -- ??? Are there are other uncomfortable cases that we will have to
7854 -- deal with.
7856 -- 10. RECORD_TYPE_WITH_PRIVATE complications
7858 -- Types that are derived from a visible record type and have a private
7859 -- extension present other peculiarities. They behave mostly like private
7860 -- types, but if they have primitive operations defined, these will not
7861 -- have the proper signatures for further inheritance, because other
7862 -- primitive operations will use the implicit base that we define for
7863 -- private derivations below. This affect subprogram inheritance (see
7864 -- Derive_Subprograms for details). We also derive the implicit base from
7865 -- the base type of the full view, so that the implicit base is a record
7866 -- type and not another private type, This avoids infinite loops.
7868 procedure Build_Derived_Record_Type
7869 (N : Node_Id;
7870 Parent_Type : Entity_Id;
7871 Derived_Type : Entity_Id;
7872 Derive_Subps : Boolean := True)
7874 Discriminant_Specs : constant Boolean :=
7875 Present (Discriminant_Specifications (N));
7876 Is_Tagged : constant Boolean := Is_Tagged_Type (Parent_Type);
7877 Loc : constant Source_Ptr := Sloc (N);
7878 Private_Extension : constant Boolean :=
7879 Nkind (N) = N_Private_Extension_Declaration;
7880 Assoc_List : Elist_Id;
7881 Constraint_Present : Boolean;
7882 Constrs : Elist_Id;
7883 Discrim : Entity_Id;
7884 Indic : Node_Id;
7885 Inherit_Discrims : Boolean := False;
7886 Last_Discrim : Entity_Id;
7887 New_Base : Entity_Id;
7888 New_Decl : Node_Id;
7889 New_Discrs : Elist_Id;
7890 New_Indic : Node_Id;
7891 Parent_Base : Entity_Id;
7892 Save_Etype : Entity_Id;
7893 Save_Discr_Constr : Elist_Id;
7894 Save_Next_Entity : Entity_Id;
7895 Type_Def : Node_Id;
7897 Discs : Elist_Id := New_Elmt_List;
7898 -- An empty Discs list means that there were no constraints in the
7899 -- subtype indication or that there was an error processing it.
7901 begin
7902 if Ekind (Parent_Type) = E_Record_Type_With_Private
7903 and then Present (Full_View (Parent_Type))
7904 and then Has_Discriminants (Parent_Type)
7905 then
7906 Parent_Base := Base_Type (Full_View (Parent_Type));
7907 else
7908 Parent_Base := Base_Type (Parent_Type);
7909 end if;
7911 -- AI05-0115 : if this is a derivation from a private type in some
7912 -- other scope that may lead to invisible components for the derived
7913 -- type, mark it accordingly.
7915 if Is_Private_Type (Parent_Type) then
7916 if Scope (Parent_Type) = Scope (Derived_Type) then
7917 null;
7919 elsif In_Open_Scopes (Scope (Parent_Type))
7920 and then In_Private_Part (Scope (Parent_Type))
7921 then
7922 null;
7924 else
7925 Set_Has_Private_Ancestor (Derived_Type);
7926 end if;
7928 else
7929 Set_Has_Private_Ancestor
7930 (Derived_Type, Has_Private_Ancestor (Parent_Type));
7931 end if;
7933 -- Before we start the previously documented transformations, here is
7934 -- little fix for size and alignment of tagged types. Normally when we
7935 -- derive type D from type P, we copy the size and alignment of P as the
7936 -- default for D, and in the absence of explicit representation clauses
7937 -- for D, the size and alignment are indeed the same as the parent.
7939 -- But this is wrong for tagged types, since fields may be added, and
7940 -- the default size may need to be larger, and the default alignment may
7941 -- need to be larger.
7943 -- We therefore reset the size and alignment fields in the tagged case.
7944 -- Note that the size and alignment will in any case be at least as
7945 -- large as the parent type (since the derived type has a copy of the
7946 -- parent type in the _parent field)
7948 -- The type is also marked as being tagged here, which is needed when
7949 -- processing components with a self-referential anonymous access type
7950 -- in the call to Check_Anonymous_Access_Components below. Note that
7951 -- this flag is also set later on for completeness.
7953 if Is_Tagged then
7954 Set_Is_Tagged_Type (Derived_Type);
7955 Init_Size_Align (Derived_Type);
7956 end if;
7958 -- STEP 0a: figure out what kind of derived type declaration we have
7960 if Private_Extension then
7961 Type_Def := N;
7962 Set_Ekind (Derived_Type, E_Record_Type_With_Private);
7963 Set_Default_SSO (Derived_Type);
7965 else
7966 Type_Def := Type_Definition (N);
7968 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
7969 -- Parent_Base can be a private type or private extension. However,
7970 -- for tagged types with an extension the newly added fields are
7971 -- visible and hence the Derived_Type is always an E_Record_Type.
7972 -- (except that the parent may have its own private fields).
7973 -- For untagged types we preserve the Ekind of the Parent_Base.
7975 if Present (Record_Extension_Part (Type_Def)) then
7976 Set_Ekind (Derived_Type, E_Record_Type);
7977 Set_Default_SSO (Derived_Type);
7979 -- Create internal access types for components with anonymous
7980 -- access types.
7982 if Ada_Version >= Ada_2005 then
7983 Check_Anonymous_Access_Components
7984 (N, Derived_Type, Derived_Type,
7985 Component_List (Record_Extension_Part (Type_Def)));
7986 end if;
7988 else
7989 Set_Ekind (Derived_Type, Ekind (Parent_Base));
7990 end if;
7991 end if;
7993 -- Indic can either be an N_Identifier if the subtype indication
7994 -- contains no constraint or an N_Subtype_Indication if the subtype
7995 -- indication has a constraint.
7997 Indic := Subtype_Indication (Type_Def);
7998 Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
8000 -- Check that the type has visible discriminants. The type may be
8001 -- a private type with unknown discriminants whose full view has
8002 -- discriminants which are invisible.
8004 if Constraint_Present then
8005 if not Has_Discriminants (Parent_Base)
8006 or else
8007 (Has_Unknown_Discriminants (Parent_Base)
8008 and then Is_Private_Type (Parent_Base))
8009 then
8010 Error_Msg_N
8011 ("invalid constraint: type has no discriminant",
8012 Constraint (Indic));
8014 Constraint_Present := False;
8015 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
8017 elsif Is_Constrained (Parent_Type) then
8018 Error_Msg_N
8019 ("invalid constraint: parent type is already constrained",
8020 Constraint (Indic));
8022 Constraint_Present := False;
8023 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
8024 end if;
8025 end if;
8027 -- STEP 0b: If needed, apply transformation given in point 5. above
8029 if not Private_Extension
8030 and then Has_Discriminants (Parent_Type)
8031 and then not Discriminant_Specs
8032 and then (Is_Constrained (Parent_Type) or else Constraint_Present)
8033 then
8034 -- First, we must analyze the constraint (see comment in point 5.)
8035 -- The constraint may come from the subtype indication of the full
8036 -- declaration.
8038 if Constraint_Present then
8039 New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
8041 -- If there is no explicit constraint, there might be one that is
8042 -- inherited from a constrained parent type. In that case verify that
8043 -- it conforms to the constraint in the partial view. In perverse
8044 -- cases the parent subtypes of the partial and full view can have
8045 -- different constraints.
8047 elsif Present (Stored_Constraint (Parent_Type)) then
8048 New_Discrs := Stored_Constraint (Parent_Type);
8050 else
8051 New_Discrs := No_Elist;
8052 end if;
8054 if Has_Discriminants (Derived_Type)
8055 and then Has_Private_Declaration (Derived_Type)
8056 and then Present (Discriminant_Constraint (Derived_Type))
8057 and then Present (New_Discrs)
8058 then
8059 -- Verify that constraints of the full view statically match
8060 -- those given in the partial view.
8062 declare
8063 C1, C2 : Elmt_Id;
8065 begin
8066 C1 := First_Elmt (New_Discrs);
8067 C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
8068 while Present (C1) and then Present (C2) loop
8069 if Fully_Conformant_Expressions (Node (C1), Node (C2))
8070 or else
8071 (Is_OK_Static_Expression (Node (C1))
8072 and then Is_OK_Static_Expression (Node (C2))
8073 and then
8074 Expr_Value (Node (C1)) = Expr_Value (Node (C2)))
8075 then
8076 null;
8078 else
8079 if Constraint_Present then
8080 Error_Msg_N
8081 ("constraint not conformant to previous declaration",
8082 Node (C1));
8083 else
8084 Error_Msg_N
8085 ("constraint of full view is incompatible "
8086 & "with partial view", N);
8087 end if;
8088 end if;
8090 Next_Elmt (C1);
8091 Next_Elmt (C2);
8092 end loop;
8093 end;
8094 end if;
8096 -- Insert and analyze the declaration for the unconstrained base type
8098 New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
8100 New_Decl :=
8101 Make_Full_Type_Declaration (Loc,
8102 Defining_Identifier => New_Base,
8103 Type_Definition =>
8104 Make_Derived_Type_Definition (Loc,
8105 Abstract_Present => Abstract_Present (Type_Def),
8106 Limited_Present => Limited_Present (Type_Def),
8107 Subtype_Indication =>
8108 New_Occurrence_Of (Parent_Base, Loc),
8109 Record_Extension_Part =>
8110 Relocate_Node (Record_Extension_Part (Type_Def)),
8111 Interface_List => Interface_List (Type_Def)));
8113 Set_Parent (New_Decl, Parent (N));
8114 Mark_Rewrite_Insertion (New_Decl);
8115 Insert_Before (N, New_Decl);
8117 -- In the extension case, make sure ancestor is frozen appropriately
8118 -- (see also non-discriminated case below).
8120 if Present (Record_Extension_Part (Type_Def))
8121 or else Is_Interface (Parent_Base)
8122 then
8123 Freeze_Before (New_Decl, Parent_Type);
8124 end if;
8126 -- Note that this call passes False for the Derive_Subps parameter
8127 -- because subprogram derivation is deferred until after creating
8128 -- the subtype (see below).
8130 Build_Derived_Type
8131 (New_Decl, Parent_Base, New_Base,
8132 Is_Completion => False, Derive_Subps => False);
8134 -- ??? This needs re-examination to determine whether the
8135 -- above call can simply be replaced by a call to Analyze.
8137 Set_Analyzed (New_Decl);
8139 -- Insert and analyze the declaration for the constrained subtype
8141 if Constraint_Present then
8142 New_Indic :=
8143 Make_Subtype_Indication (Loc,
8144 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
8145 Constraint => Relocate_Node (Constraint (Indic)));
8147 else
8148 declare
8149 Constr_List : constant List_Id := New_List;
8150 C : Elmt_Id;
8151 Expr : Node_Id;
8153 begin
8154 C := First_Elmt (Discriminant_Constraint (Parent_Type));
8155 while Present (C) loop
8156 Expr := Node (C);
8158 -- It is safe here to call New_Copy_Tree since we called
8159 -- Force_Evaluation on each constraint previously
8160 -- in Build_Discriminant_Constraints.
8162 Append (New_Copy_Tree (Expr), To => Constr_List);
8164 Next_Elmt (C);
8165 end loop;
8167 New_Indic :=
8168 Make_Subtype_Indication (Loc,
8169 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
8170 Constraint =>
8171 Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
8172 end;
8173 end if;
8175 Rewrite (N,
8176 Make_Subtype_Declaration (Loc,
8177 Defining_Identifier => Derived_Type,
8178 Subtype_Indication => New_Indic));
8180 Analyze (N);
8182 -- Derivation of subprograms must be delayed until the full subtype
8183 -- has been established, to ensure proper overriding of subprograms
8184 -- inherited by full types. If the derivations occurred as part of
8185 -- the call to Build_Derived_Type above, then the check for type
8186 -- conformance would fail because earlier primitive subprograms
8187 -- could still refer to the full type prior the change to the new
8188 -- subtype and hence would not match the new base type created here.
8189 -- Subprograms are not derived, however, when Derive_Subps is False
8190 -- (since otherwise there could be redundant derivations).
8192 if Derive_Subps then
8193 Derive_Subprograms (Parent_Type, Derived_Type);
8194 end if;
8196 -- For tagged types the Discriminant_Constraint of the new base itype
8197 -- is inherited from the first subtype so that no subtype conformance
8198 -- problem arise when the first subtype overrides primitive
8199 -- operations inherited by the implicit base type.
8201 if Is_Tagged then
8202 Set_Discriminant_Constraint
8203 (New_Base, Discriminant_Constraint (Derived_Type));
8204 end if;
8206 return;
8207 end if;
8209 -- If we get here Derived_Type will have no discriminants or it will be
8210 -- a discriminated unconstrained base type.
8212 -- STEP 1a: perform preliminary actions/checks for derived tagged types
8214 if Is_Tagged then
8216 -- The parent type is frozen for non-private extensions (RM 13.14(7))
8217 -- The declaration of a specific descendant of an interface type
8218 -- freezes the interface type (RM 13.14).
8220 if not Private_Extension or else Is_Interface (Parent_Base) then
8221 Freeze_Before (N, Parent_Type);
8222 end if;
8224 -- In Ada 2005 (AI-344), the restriction that a derived tagged type
8225 -- cannot be declared at a deeper level than its parent type is
8226 -- removed. The check on derivation within a generic body is also
8227 -- relaxed, but there's a restriction that a derived tagged type
8228 -- cannot be declared in a generic body if it's derived directly
8229 -- or indirectly from a formal type of that generic.
8231 if Ada_Version >= Ada_2005 then
8232 if Present (Enclosing_Generic_Body (Derived_Type)) then
8233 declare
8234 Ancestor_Type : Entity_Id;
8236 begin
8237 -- Check to see if any ancestor of the derived type is a
8238 -- formal type.
8240 Ancestor_Type := Parent_Type;
8241 while not Is_Generic_Type (Ancestor_Type)
8242 and then Etype (Ancestor_Type) /= Ancestor_Type
8243 loop
8244 Ancestor_Type := Etype (Ancestor_Type);
8245 end loop;
8247 -- If the derived type does have a formal type as an
8248 -- ancestor, then it's an error if the derived type is
8249 -- declared within the body of the generic unit that
8250 -- declares the formal type in its generic formal part. It's
8251 -- sufficient to check whether the ancestor type is declared
8252 -- inside the same generic body as the derived type (such as
8253 -- within a nested generic spec), in which case the
8254 -- derivation is legal. If the formal type is declared
8255 -- outside of that generic body, then it's guaranteed that
8256 -- the derived type is declared within the generic body of
8257 -- the generic unit declaring the formal type.
8259 if Is_Generic_Type (Ancestor_Type)
8260 and then Enclosing_Generic_Body (Ancestor_Type) /=
8261 Enclosing_Generic_Body (Derived_Type)
8262 then
8263 Error_Msg_NE
8264 ("parent type of& must not be descendant of formal type"
8265 & " of an enclosing generic body",
8266 Indic, Derived_Type);
8267 end if;
8268 end;
8269 end if;
8271 elsif Type_Access_Level (Derived_Type) /=
8272 Type_Access_Level (Parent_Type)
8273 and then not Is_Generic_Type (Derived_Type)
8274 then
8275 if Is_Controlled (Parent_Type) then
8276 Error_Msg_N
8277 ("controlled type must be declared at the library level",
8278 Indic);
8279 else
8280 Error_Msg_N
8281 ("type extension at deeper accessibility level than parent",
8282 Indic);
8283 end if;
8285 else
8286 declare
8287 GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
8288 begin
8289 if Present (GB)
8290 and then GB /= Enclosing_Generic_Body (Parent_Base)
8291 then
8292 Error_Msg_NE
8293 ("parent type of& must not be outside generic body"
8294 & " (RM 3.9.1(4))",
8295 Indic, Derived_Type);
8296 end if;
8297 end;
8298 end if;
8299 end if;
8301 -- Ada 2005 (AI-251)
8303 if Ada_Version >= Ada_2005 and then Is_Tagged then
8305 -- "The declaration of a specific descendant of an interface type
8306 -- freezes the interface type" (RM 13.14).
8308 declare
8309 Iface : Node_Id;
8310 begin
8311 if Is_Non_Empty_List (Interface_List (Type_Def)) then
8312 Iface := First (Interface_List (Type_Def));
8313 while Present (Iface) loop
8314 Freeze_Before (N, Etype (Iface));
8315 Next (Iface);
8316 end loop;
8317 end if;
8318 end;
8319 end if;
8321 -- STEP 1b : preliminary cleanup of the full view of private types
8323 -- If the type is already marked as having discriminants, then it's the
8324 -- completion of a private type or private extension and we need to
8325 -- retain the discriminants from the partial view if the current
8326 -- declaration has Discriminant_Specifications so that we can verify
8327 -- conformance. However, we must remove any existing components that
8328 -- were inherited from the parent (and attached in Copy_And_Swap)
8329 -- because the full type inherits all appropriate components anyway, and
8330 -- we do not want the partial view's components interfering.
8332 if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
8333 Discrim := First_Discriminant (Derived_Type);
8334 loop
8335 Last_Discrim := Discrim;
8336 Next_Discriminant (Discrim);
8337 exit when No (Discrim);
8338 end loop;
8340 Set_Last_Entity (Derived_Type, Last_Discrim);
8342 -- In all other cases wipe out the list of inherited components (even
8343 -- inherited discriminants), it will be properly rebuilt here.
8345 else
8346 Set_First_Entity (Derived_Type, Empty);
8347 Set_Last_Entity (Derived_Type, Empty);
8348 end if;
8350 -- STEP 1c: Initialize some flags for the Derived_Type
8352 -- The following flags must be initialized here so that
8353 -- Process_Discriminants can check that discriminants of tagged types do
8354 -- not have a default initial value and that access discriminants are
8355 -- only specified for limited records. For completeness, these flags are
8356 -- also initialized along with all the other flags below.
8358 -- AI-419: Limitedness is not inherited from an interface parent, so to
8359 -- be limited in that case the type must be explicitly declared as
8360 -- limited. However, task and protected interfaces are always limited.
8362 if Limited_Present (Type_Def) then
8363 Set_Is_Limited_Record (Derived_Type);
8365 elsif Is_Limited_Record (Parent_Type)
8366 or else (Present (Full_View (Parent_Type))
8367 and then Is_Limited_Record (Full_View (Parent_Type)))
8368 then
8369 if not Is_Interface (Parent_Type)
8370 or else Is_Synchronized_Interface (Parent_Type)
8371 or else Is_Protected_Interface (Parent_Type)
8372 or else Is_Task_Interface (Parent_Type)
8373 then
8374 Set_Is_Limited_Record (Derived_Type);
8375 end if;
8376 end if;
8378 -- STEP 2a: process discriminants of derived type if any
8380 Push_Scope (Derived_Type);
8382 if Discriminant_Specs then
8383 Set_Has_Unknown_Discriminants (Derived_Type, False);
8385 -- The following call initializes fields Has_Discriminants and
8386 -- Discriminant_Constraint, unless we are processing the completion
8387 -- of a private type declaration.
8389 Check_Or_Process_Discriminants (N, Derived_Type);
8391 -- For untagged types, the constraint on the Parent_Type must be
8392 -- present and is used to rename the discriminants.
8394 if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
8395 Error_Msg_N ("untagged parent must have discriminants", Indic);
8397 elsif not Is_Tagged and then not Constraint_Present then
8398 Error_Msg_N
8399 ("discriminant constraint needed for derived untagged records",
8400 Indic);
8402 -- Otherwise the parent subtype must be constrained unless we have a
8403 -- private extension.
8405 elsif not Constraint_Present
8406 and then not Private_Extension
8407 and then not Is_Constrained (Parent_Type)
8408 then
8409 Error_Msg_N
8410 ("unconstrained type not allowed in this context", Indic);
8412 elsif Constraint_Present then
8413 -- The following call sets the field Corresponding_Discriminant
8414 -- for the discriminants in the Derived_Type.
8416 Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
8418 -- For untagged types all new discriminants must rename
8419 -- discriminants in the parent. For private extensions new
8420 -- discriminants cannot rename old ones (implied by [7.3(13)]).
8422 Discrim := First_Discriminant (Derived_Type);
8423 while Present (Discrim) loop
8424 if not Is_Tagged
8425 and then No (Corresponding_Discriminant (Discrim))
8426 then
8427 Error_Msg_N
8428 ("new discriminants must constrain old ones", Discrim);
8430 elsif Private_Extension
8431 and then Present (Corresponding_Discriminant (Discrim))
8432 then
8433 Error_Msg_N
8434 ("only static constraints allowed for parent"
8435 & " discriminants in the partial view", Indic);
8436 exit;
8437 end if;
8439 -- If a new discriminant is used in the constraint, then its
8440 -- subtype must be statically compatible with the parent
8441 -- discriminant's subtype (3.7(15)).
8443 -- However, if the record contains an array constrained by
8444 -- the discriminant but with some different bound, the compiler
8445 -- attemps to create a smaller range for the discriminant type.
8446 -- (See exp_ch3.Adjust_Discriminants). In this case, where
8447 -- the discriminant type is a scalar type, the check must use
8448 -- the original discriminant type in the parent declaration.
8450 declare
8451 Corr_Disc : constant Entity_Id :=
8452 Corresponding_Discriminant (Discrim);
8453 Disc_Type : constant Entity_Id := Etype (Discrim);
8454 Corr_Type : Entity_Id;
8456 begin
8457 if Present (Corr_Disc) then
8458 if Is_Scalar_Type (Disc_Type) then
8459 Corr_Type :=
8460 Entity (Discriminant_Type (Parent (Corr_Disc)));
8461 else
8462 Corr_Type := Etype (Corr_Disc);
8463 end if;
8465 if not
8466 Subtypes_Statically_Compatible (Disc_Type, Corr_Type)
8467 then
8468 Error_Msg_N
8469 ("subtype must be compatible "
8470 & "with parent discriminant",
8471 Discrim);
8472 end if;
8473 end if;
8474 end;
8476 Next_Discriminant (Discrim);
8477 end loop;
8479 -- Check whether the constraints of the full view statically
8480 -- match those imposed by the parent subtype [7.3(13)].
8482 if Present (Stored_Constraint (Derived_Type)) then
8483 declare
8484 C1, C2 : Elmt_Id;
8486 begin
8487 C1 := First_Elmt (Discs);
8488 C2 := First_Elmt (Stored_Constraint (Derived_Type));
8489 while Present (C1) and then Present (C2) loop
8490 if not
8491 Fully_Conformant_Expressions (Node (C1), Node (C2))
8492 then
8493 Error_Msg_N
8494 ("not conformant with previous declaration",
8495 Node (C1));
8496 end if;
8498 Next_Elmt (C1);
8499 Next_Elmt (C2);
8500 end loop;
8501 end;
8502 end if;
8503 end if;
8505 -- STEP 2b: No new discriminants, inherit discriminants if any
8507 else
8508 if Private_Extension then
8509 Set_Has_Unknown_Discriminants
8510 (Derived_Type,
8511 Has_Unknown_Discriminants (Parent_Type)
8512 or else Unknown_Discriminants_Present (N));
8514 -- The partial view of the parent may have unknown discriminants,
8515 -- but if the full view has discriminants and the parent type is
8516 -- in scope they must be inherited.
8518 elsif Has_Unknown_Discriminants (Parent_Type)
8519 and then
8520 (not Has_Discriminants (Parent_Type)
8521 or else not In_Open_Scopes (Scope (Parent_Type)))
8522 then
8523 Set_Has_Unknown_Discriminants (Derived_Type);
8524 end if;
8526 if not Has_Unknown_Discriminants (Derived_Type)
8527 and then not Has_Unknown_Discriminants (Parent_Base)
8528 and then Has_Discriminants (Parent_Type)
8529 then
8530 Inherit_Discrims := True;
8531 Set_Has_Discriminants
8532 (Derived_Type, True);
8533 Set_Discriminant_Constraint
8534 (Derived_Type, Discriminant_Constraint (Parent_Base));
8535 end if;
8537 -- The following test is true for private types (remember
8538 -- transformation 5. is not applied to those) and in an error
8539 -- situation.
8541 if Constraint_Present then
8542 Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
8543 end if;
8545 -- For now mark a new derived type as constrained only if it has no
8546 -- discriminants. At the end of Build_Derived_Record_Type we properly
8547 -- set this flag in the case of private extensions. See comments in
8548 -- point 9. just before body of Build_Derived_Record_Type.
8550 Set_Is_Constrained
8551 (Derived_Type,
8552 not (Inherit_Discrims
8553 or else Has_Unknown_Discriminants (Derived_Type)));
8554 end if;
8556 -- STEP 3: initialize fields of derived type
8558 Set_Is_Tagged_Type (Derived_Type, Is_Tagged);
8559 Set_Stored_Constraint (Derived_Type, No_Elist);
8561 -- Ada 2005 (AI-251): Private type-declarations can implement interfaces
8562 -- but cannot be interfaces
8564 if not Private_Extension
8565 and then Ekind (Derived_Type) /= E_Private_Type
8566 and then Ekind (Derived_Type) /= E_Limited_Private_Type
8567 then
8568 if Interface_Present (Type_Def) then
8569 Analyze_Interface_Declaration (Derived_Type, Type_Def);
8570 end if;
8572 Set_Interfaces (Derived_Type, No_Elist);
8573 end if;
8575 -- Fields inherited from the Parent_Type
8577 Set_Has_Specified_Layout
8578 (Derived_Type, Has_Specified_Layout (Parent_Type));
8579 Set_Is_Limited_Composite
8580 (Derived_Type, Is_Limited_Composite (Parent_Type));
8581 Set_Is_Private_Composite
8582 (Derived_Type, Is_Private_Composite (Parent_Type));
8584 if Is_Tagged_Type (Parent_Type) then
8585 Set_No_Tagged_Streams_Pragma
8586 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
8587 end if;
8589 -- Fields inherited from the Parent_Base
8591 Set_Has_Controlled_Component
8592 (Derived_Type, Has_Controlled_Component (Parent_Base));
8593 Set_Has_Non_Standard_Rep
8594 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
8595 Set_Has_Primitive_Operations
8596 (Derived_Type, Has_Primitive_Operations (Parent_Base));
8598 -- Fields inherited from the Parent_Base in the non-private case
8600 if Ekind (Derived_Type) = E_Record_Type then
8601 Set_Has_Complex_Representation
8602 (Derived_Type, Has_Complex_Representation (Parent_Base));
8603 end if;
8605 -- Fields inherited from the Parent_Base for record types
8607 if Is_Record_Type (Derived_Type) then
8608 declare
8609 Parent_Full : Entity_Id;
8611 begin
8612 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
8613 -- Parent_Base can be a private type or private extension. Go
8614 -- to the full view here to get the E_Record_Type specific flags.
8616 if Present (Full_View (Parent_Base)) then
8617 Parent_Full := Full_View (Parent_Base);
8618 else
8619 Parent_Full := Parent_Base;
8620 end if;
8622 Set_OK_To_Reorder_Components
8623 (Derived_Type, OK_To_Reorder_Components (Parent_Full));
8624 end;
8625 end if;
8627 -- Set fields for private derived types
8629 if Is_Private_Type (Derived_Type) then
8630 Set_Depends_On_Private (Derived_Type, True);
8631 Set_Private_Dependents (Derived_Type, New_Elmt_List);
8633 -- Inherit fields from non private record types. If this is the
8634 -- completion of a derivation from a private type, the parent itself
8635 -- is private, and the attributes come from its full view, which must
8636 -- be present.
8638 else
8639 if Is_Private_Type (Parent_Base)
8640 and then not Is_Record_Type (Parent_Base)
8641 then
8642 Set_Component_Alignment
8643 (Derived_Type, Component_Alignment (Full_View (Parent_Base)));
8644 Set_C_Pass_By_Copy
8645 (Derived_Type, C_Pass_By_Copy (Full_View (Parent_Base)));
8646 else
8647 Set_Component_Alignment
8648 (Derived_Type, Component_Alignment (Parent_Base));
8649 Set_C_Pass_By_Copy
8650 (Derived_Type, C_Pass_By_Copy (Parent_Base));
8651 end if;
8652 end if;
8654 -- Set fields for tagged types
8656 if Is_Tagged then
8657 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
8659 -- All tagged types defined in Ada.Finalization are controlled
8661 if Chars (Scope (Derived_Type)) = Name_Finalization
8662 and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
8663 and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
8664 then
8665 Set_Is_Controlled (Derived_Type);
8666 else
8667 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Base));
8668 end if;
8670 -- Minor optimization: there is no need to generate the class-wide
8671 -- entity associated with an underlying record view.
8673 if not Is_Underlying_Record_View (Derived_Type) then
8674 Make_Class_Wide_Type (Derived_Type);
8675 end if;
8677 Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
8679 if Has_Discriminants (Derived_Type)
8680 and then Constraint_Present
8681 then
8682 Set_Stored_Constraint
8683 (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
8684 end if;
8686 if Ada_Version >= Ada_2005 then
8687 declare
8688 Ifaces_List : Elist_Id;
8690 begin
8691 -- Checks rules 3.9.4 (13/2 and 14/2)
8693 if Comes_From_Source (Derived_Type)
8694 and then not Is_Private_Type (Derived_Type)
8695 and then Is_Interface (Parent_Type)
8696 and then not Is_Interface (Derived_Type)
8697 then
8698 if Is_Task_Interface (Parent_Type) then
8699 Error_Msg_N
8700 ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
8701 Derived_Type);
8703 elsif Is_Protected_Interface (Parent_Type) then
8704 Error_Msg_N
8705 ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
8706 Derived_Type);
8707 end if;
8708 end if;
8710 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
8712 Check_Interfaces (N, Type_Def);
8714 -- Ada 2005 (AI-251): Collect the list of progenitors that are
8715 -- not already in the parents.
8717 Collect_Interfaces
8718 (T => Derived_Type,
8719 Ifaces_List => Ifaces_List,
8720 Exclude_Parents => True);
8722 Set_Interfaces (Derived_Type, Ifaces_List);
8724 -- If the derived type is the anonymous type created for
8725 -- a declaration whose parent has a constraint, propagate
8726 -- the interface list to the source type. This must be done
8727 -- prior to the completion of the analysis of the source type
8728 -- because the components in the extension may contain current
8729 -- instances whose legality depends on some ancestor.
8731 if Is_Itype (Derived_Type) then
8732 declare
8733 Def : constant Node_Id :=
8734 Associated_Node_For_Itype (Derived_Type);
8735 begin
8736 if Present (Def)
8737 and then Nkind (Def) = N_Full_Type_Declaration
8738 then
8739 Set_Interfaces
8740 (Defining_Identifier (Def), Ifaces_List);
8741 end if;
8742 end;
8743 end if;
8745 -- Propagate inherited invariant information of parents
8746 -- and progenitors
8748 if Ada_Version >= Ada_2012
8749 and then not Is_Interface (Derived_Type)
8750 then
8751 if Has_Inheritable_Invariants (Parent_Type) then
8752 Set_Has_Invariants (Derived_Type);
8753 Set_Has_Inheritable_Invariants (Derived_Type);
8755 elsif not Is_Empty_Elmt_List (Ifaces_List) then
8756 declare
8757 AI : Elmt_Id;
8759 begin
8760 AI := First_Elmt (Ifaces_List);
8761 while Present (AI) loop
8762 if Has_Inheritable_Invariants (Node (AI)) then
8763 Set_Has_Invariants (Derived_Type);
8764 Set_Has_Inheritable_Invariants (Derived_Type);
8766 exit;
8767 end if;
8769 Next_Elmt (AI);
8770 end loop;
8771 end;
8772 end if;
8773 end if;
8775 -- A type extension is automatically Ghost when one of its
8776 -- progenitors is Ghost (SPARK RM 6.9(9)). This property is
8777 -- also inherited when the parent type is Ghost, but this is
8778 -- done in Build_Derived_Type as the mechanism also handles
8779 -- untagged derivations.
8781 if Implements_Ghost_Interface (Derived_Type) then
8782 Set_Is_Ghost_Entity (Derived_Type);
8783 end if;
8784 end;
8785 end if;
8787 else
8788 Set_Is_Packed (Derived_Type, Is_Packed (Parent_Base));
8789 Set_Has_Non_Standard_Rep
8790 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
8791 end if;
8793 -- STEP 4: Inherit components from the parent base and constrain them.
8794 -- Apply the second transformation described in point 6. above.
8796 if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
8797 or else not Has_Discriminants (Parent_Type)
8798 or else not Is_Constrained (Parent_Type)
8799 then
8800 Constrs := Discs;
8801 else
8802 Constrs := Discriminant_Constraint (Parent_Type);
8803 end if;
8805 Assoc_List :=
8806 Inherit_Components
8807 (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
8809 -- STEP 5a: Copy the parent record declaration for untagged types
8811 if not Is_Tagged then
8813 -- Discriminant_Constraint (Derived_Type) has been properly
8814 -- constructed. Save it and temporarily set it to Empty because we
8815 -- do not want the call to New_Copy_Tree below to mess this list.
8817 if Has_Discriminants (Derived_Type) then
8818 Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
8819 Set_Discriminant_Constraint (Derived_Type, No_Elist);
8820 else
8821 Save_Discr_Constr := No_Elist;
8822 end if;
8824 -- Save the Etype field of Derived_Type. It is correctly set now,
8825 -- but the call to New_Copy tree may remap it to point to itself,
8826 -- which is not what we want. Ditto for the Next_Entity field.
8828 Save_Etype := Etype (Derived_Type);
8829 Save_Next_Entity := Next_Entity (Derived_Type);
8831 -- Assoc_List maps all stored discriminants in the Parent_Base to
8832 -- stored discriminants in the Derived_Type. It is fundamental that
8833 -- no types or itypes with discriminants other than the stored
8834 -- discriminants appear in the entities declared inside
8835 -- Derived_Type, since the back end cannot deal with it.
8837 New_Decl :=
8838 New_Copy_Tree
8839 (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
8841 -- Restore the fields saved prior to the New_Copy_Tree call
8842 -- and compute the stored constraint.
8844 Set_Etype (Derived_Type, Save_Etype);
8845 Set_Next_Entity (Derived_Type, Save_Next_Entity);
8847 if Has_Discriminants (Derived_Type) then
8848 Set_Discriminant_Constraint
8849 (Derived_Type, Save_Discr_Constr);
8850 Set_Stored_Constraint
8851 (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
8852 Replace_Components (Derived_Type, New_Decl);
8853 Set_Has_Implicit_Dereference
8854 (Derived_Type, Has_Implicit_Dereference (Parent_Type));
8855 end if;
8857 -- Insert the new derived type declaration
8859 Rewrite (N, New_Decl);
8861 -- STEP 5b: Complete the processing for record extensions in generics
8863 -- There is no completion for record extensions declared in the
8864 -- parameter part of a generic, so we need to complete processing for
8865 -- these generic record extensions here. The Record_Type_Definition call
8866 -- will change the Ekind of the components from E_Void to E_Component.
8868 elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
8869 Record_Type_Definition (Empty, Derived_Type);
8871 -- STEP 5c: Process the record extension for non private tagged types
8873 elsif not Private_Extension then
8874 Expand_Record_Extension (Derived_Type, Type_Def);
8876 -- Note : previously in ASIS mode we set the Parent_Subtype of the
8877 -- derived type to propagate some semantic information. This led
8878 -- to other ASIS failures and has been removed.
8880 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
8881 -- implemented interfaces if we are in expansion mode
8883 if Expander_Active
8884 and then Has_Interfaces (Derived_Type)
8885 then
8886 Add_Interface_Tag_Components (N, Derived_Type);
8887 end if;
8889 -- Analyze the record extension
8891 Record_Type_Definition
8892 (Record_Extension_Part (Type_Def), Derived_Type);
8893 end if;
8895 End_Scope;
8897 -- Nothing else to do if there is an error in the derivation.
8898 -- An unusual case: the full view may be derived from a type in an
8899 -- instance, when the partial view was used illegally as an actual
8900 -- in that instance, leading to a circular definition.
8902 if Etype (Derived_Type) = Any_Type
8903 or else Etype (Parent_Type) = Derived_Type
8904 then
8905 return;
8906 end if;
8908 -- Set delayed freeze and then derive subprograms, we need to do
8909 -- this in this order so that derived subprograms inherit the
8910 -- derived freeze if necessary.
8912 Set_Has_Delayed_Freeze (Derived_Type);
8914 if Derive_Subps then
8915 Derive_Subprograms (Parent_Type, Derived_Type);
8916 end if;
8918 -- If we have a private extension which defines a constrained derived
8919 -- type mark as constrained here after we have derived subprograms. See
8920 -- comment on point 9. just above the body of Build_Derived_Record_Type.
8922 if Private_Extension and then Inherit_Discrims then
8923 if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
8924 Set_Is_Constrained (Derived_Type, True);
8925 Set_Discriminant_Constraint (Derived_Type, Discs);
8927 elsif Is_Constrained (Parent_Type) then
8928 Set_Is_Constrained
8929 (Derived_Type, True);
8930 Set_Discriminant_Constraint
8931 (Derived_Type, Discriminant_Constraint (Parent_Type));
8932 end if;
8933 end if;
8935 -- Update the class-wide type, which shares the now-completed entity
8936 -- list with its specific type. In case of underlying record views,
8937 -- we do not generate the corresponding class wide entity.
8939 if Is_Tagged
8940 and then not Is_Underlying_Record_View (Derived_Type)
8941 then
8942 Set_First_Entity
8943 (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
8944 Set_Last_Entity
8945 (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
8946 end if;
8948 Check_Function_Writable_Actuals (N);
8949 end Build_Derived_Record_Type;
8951 ------------------------
8952 -- Build_Derived_Type --
8953 ------------------------
8955 procedure Build_Derived_Type
8956 (N : Node_Id;
8957 Parent_Type : Entity_Id;
8958 Derived_Type : Entity_Id;
8959 Is_Completion : Boolean;
8960 Derive_Subps : Boolean := True)
8962 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
8964 begin
8965 -- Set common attributes
8967 Set_Scope (Derived_Type, Current_Scope);
8969 Set_Etype (Derived_Type, Parent_Base);
8970 Set_Ekind (Derived_Type, Ekind (Parent_Base));
8971 Set_Has_Task (Derived_Type, Has_Task (Parent_Base));
8972 Set_Has_Protected (Derived_Type, Has_Protected (Parent_Base));
8974 Set_Size_Info (Derived_Type, Parent_Type);
8975 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
8976 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
8977 Set_Is_Tagged_Type (Derived_Type, Is_Tagged_Type (Parent_Type));
8978 Set_Is_Volatile (Derived_Type, Is_Volatile (Parent_Type));
8980 if Is_Tagged_Type (Derived_Type) then
8981 Set_No_Tagged_Streams_Pragma
8982 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
8983 end if;
8985 -- If the parent has primitive routines, set the derived type link
8987 if Has_Primitive_Operations (Parent_Type) then
8988 Set_Derived_Type_Link (Parent_Base, Derived_Type);
8989 end if;
8991 -- If the parent type is a private subtype, the convention on the base
8992 -- type may be set in the private part, and not propagated to the
8993 -- subtype until later, so we obtain the convention from the base type.
8995 Set_Convention (Derived_Type, Convention (Parent_Base));
8997 -- Set SSO default for record or array type
8999 if (Is_Array_Type (Derived_Type) or else Is_Record_Type (Derived_Type))
9000 and then Is_Base_Type (Derived_Type)
9001 then
9002 Set_Default_SSO (Derived_Type);
9003 end if;
9005 -- Propagate invariant information. The new type has invariants if
9006 -- they are inherited from the parent type, and these invariants can
9007 -- be further inherited, so both flags are set.
9009 -- We similarly inherit predicates
9011 if Has_Predicates (Parent_Type) then
9012 Set_Has_Predicates (Derived_Type);
9013 end if;
9015 -- The derived type inherits the representation clauses of the parent
9017 Inherit_Rep_Item_Chain (Derived_Type, Parent_Type);
9019 -- Propagate the attributes related to pragma Default_Initial_Condition
9020 -- from the parent type to the private extension. A derived type always
9021 -- inherits the default initial condition flag from the parent type. If
9022 -- the derived type carries its own Default_Initial_Condition pragma,
9023 -- the flag is later reset in Analyze_Pragma. Note that both flags are
9024 -- mutually exclusive.
9026 Propagate_Default_Init_Cond_Attributes
9027 (From_Typ => Parent_Type,
9028 To_Typ => Derived_Type,
9029 Parent_To_Derivation => True);
9031 -- If the parent type has delayed rep aspects, then mark the derived
9032 -- type as possibly inheriting a delayed rep aspect.
9034 if Has_Delayed_Rep_Aspects (Parent_Type) then
9035 Set_May_Inherit_Delayed_Rep_Aspects (Derived_Type);
9036 end if;
9038 -- Propagate the attributes related to pragma Ghost from the parent type
9039 -- to the derived type or type extension (SPARK RM 6.9(9)).
9041 if Is_Ghost_Entity (Parent_Type) then
9042 Set_Is_Ghost_Entity (Derived_Type);
9043 end if;
9045 -- Type dependent processing
9047 case Ekind (Parent_Type) is
9048 when Numeric_Kind =>
9049 Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
9051 when Array_Kind =>
9052 Build_Derived_Array_Type (N, Parent_Type, Derived_Type);
9054 when E_Record_Type
9055 | E_Record_Subtype
9056 | Class_Wide_Kind =>
9057 Build_Derived_Record_Type
9058 (N, Parent_Type, Derived_Type, Derive_Subps);
9059 return;
9061 when Enumeration_Kind =>
9062 Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
9064 when Access_Kind =>
9065 Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
9067 when Incomplete_Or_Private_Kind =>
9068 Build_Derived_Private_Type
9069 (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
9071 -- For discriminated types, the derivation includes deriving
9072 -- primitive operations. For others it is done below.
9074 if Is_Tagged_Type (Parent_Type)
9075 or else Has_Discriminants (Parent_Type)
9076 or else (Present (Full_View (Parent_Type))
9077 and then Has_Discriminants (Full_View (Parent_Type)))
9078 then
9079 return;
9080 end if;
9082 when Concurrent_Kind =>
9083 Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
9085 when others =>
9086 raise Program_Error;
9087 end case;
9089 -- Nothing more to do if some error occurred
9091 if Etype (Derived_Type) = Any_Type then
9092 return;
9093 end if;
9095 -- Set delayed freeze and then derive subprograms, we need to do this
9096 -- in this order so that derived subprograms inherit the derived freeze
9097 -- if necessary.
9099 Set_Has_Delayed_Freeze (Derived_Type);
9101 if Derive_Subps then
9102 Derive_Subprograms (Parent_Type, Derived_Type);
9103 end if;
9105 Set_Has_Primitive_Operations
9106 (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
9107 end Build_Derived_Type;
9109 -----------------------
9110 -- Build_Discriminal --
9111 -----------------------
9113 procedure Build_Discriminal (Discrim : Entity_Id) is
9114 D_Minal : Entity_Id;
9115 CR_Disc : Entity_Id;
9117 begin
9118 -- A discriminal has the same name as the discriminant
9120 D_Minal := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9122 Set_Ekind (D_Minal, E_In_Parameter);
9123 Set_Mechanism (D_Minal, Default_Mechanism);
9124 Set_Etype (D_Minal, Etype (Discrim));
9125 Set_Scope (D_Minal, Current_Scope);
9127 Set_Discriminal (Discrim, D_Minal);
9128 Set_Discriminal_Link (D_Minal, Discrim);
9130 -- For task types, build at once the discriminants of the corresponding
9131 -- record, which are needed if discriminants are used in entry defaults
9132 -- and in family bounds.
9134 if Is_Concurrent_Type (Current_Scope)
9135 or else
9136 Is_Limited_Type (Current_Scope)
9137 then
9138 CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9140 Set_Ekind (CR_Disc, E_In_Parameter);
9141 Set_Mechanism (CR_Disc, Default_Mechanism);
9142 Set_Etype (CR_Disc, Etype (Discrim));
9143 Set_Scope (CR_Disc, Current_Scope);
9144 Set_Discriminal_Link (CR_Disc, Discrim);
9145 Set_CR_Discriminant (Discrim, CR_Disc);
9146 end if;
9147 end Build_Discriminal;
9149 ------------------------------------
9150 -- Build_Discriminant_Constraints --
9151 ------------------------------------
9153 function Build_Discriminant_Constraints
9154 (T : Entity_Id;
9155 Def : Node_Id;
9156 Derived_Def : Boolean := False) return Elist_Id
9158 C : constant Node_Id := Constraint (Def);
9159 Nb_Discr : constant Nat := Number_Discriminants (T);
9161 Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
9162 -- Saves the expression corresponding to a given discriminant in T
9164 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
9165 -- Return the Position number within array Discr_Expr of a discriminant
9166 -- D within the discriminant list of the discriminated type T.
9168 procedure Process_Discriminant_Expression
9169 (Expr : Node_Id;
9170 D : Entity_Id);
9171 -- If this is a discriminant constraint on a partial view, do not
9172 -- generate an overflow check on the discriminant expression. The check
9173 -- will be generated when constraining the full view. Otherwise the
9174 -- backend creates duplicate symbols for the temporaries corresponding
9175 -- to the expressions to be checked, causing spurious assembler errors.
9177 ------------------
9178 -- Pos_Of_Discr --
9179 ------------------
9181 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
9182 Disc : Entity_Id;
9184 begin
9185 Disc := First_Discriminant (T);
9186 for J in Discr_Expr'Range loop
9187 if Disc = D then
9188 return J;
9189 end if;
9191 Next_Discriminant (Disc);
9192 end loop;
9194 -- Note: Since this function is called on discriminants that are
9195 -- known to belong to the discriminated type, falling through the
9196 -- loop with no match signals an internal compiler error.
9198 raise Program_Error;
9199 end Pos_Of_Discr;
9201 -------------------------------------
9202 -- Process_Discriminant_Expression --
9203 -------------------------------------
9205 procedure Process_Discriminant_Expression
9206 (Expr : Node_Id;
9207 D : Entity_Id)
9209 BDT : constant Entity_Id := Base_Type (Etype (D));
9211 begin
9212 -- If this is a discriminant constraint on a partial view, do
9213 -- not generate an overflow on the discriminant expression. The
9214 -- check will be generated when constraining the full view.
9216 if Is_Private_Type (T)
9217 and then Present (Full_View (T))
9218 then
9219 Analyze_And_Resolve (Expr, BDT, Suppress => Overflow_Check);
9220 else
9221 Analyze_And_Resolve (Expr, BDT);
9222 end if;
9223 end Process_Discriminant_Expression;
9225 -- Declarations local to Build_Discriminant_Constraints
9227 Discr : Entity_Id;
9228 E : Entity_Id;
9229 Elist : constant Elist_Id := New_Elmt_List;
9231 Constr : Node_Id;
9232 Expr : Node_Id;
9233 Id : Node_Id;
9234 Position : Nat;
9235 Found : Boolean;
9237 Discrim_Present : Boolean := False;
9239 -- Start of processing for Build_Discriminant_Constraints
9241 begin
9242 -- The following loop will process positional associations only.
9243 -- For a positional association, the (single) discriminant is
9244 -- implicitly specified by position, in textual order (RM 3.7.2).
9246 Discr := First_Discriminant (T);
9247 Constr := First (Constraints (C));
9248 for D in Discr_Expr'Range loop
9249 exit when Nkind (Constr) = N_Discriminant_Association;
9251 if No (Constr) then
9252 Error_Msg_N ("too few discriminants given in constraint", C);
9253 return New_Elmt_List;
9255 elsif Nkind (Constr) = N_Range
9256 or else (Nkind (Constr) = N_Attribute_Reference
9257 and then Attribute_Name (Constr) = Name_Range)
9258 then
9259 Error_Msg_N
9260 ("a range is not a valid discriminant constraint", Constr);
9261 Discr_Expr (D) := Error;
9263 else
9264 Process_Discriminant_Expression (Constr, Discr);
9265 Discr_Expr (D) := Constr;
9266 end if;
9268 Next_Discriminant (Discr);
9269 Next (Constr);
9270 end loop;
9272 if No (Discr) and then Present (Constr) then
9273 Error_Msg_N ("too many discriminants given in constraint", Constr);
9274 return New_Elmt_List;
9275 end if;
9277 -- Named associations can be given in any order, but if both positional
9278 -- and named associations are used in the same discriminant constraint,
9279 -- then positional associations must occur first, at their normal
9280 -- position. Hence once a named association is used, the rest of the
9281 -- discriminant constraint must use only named associations.
9283 while Present (Constr) loop
9285 -- Positional association forbidden after a named association
9287 if Nkind (Constr) /= N_Discriminant_Association then
9288 Error_Msg_N ("positional association follows named one", Constr);
9289 return New_Elmt_List;
9291 -- Otherwise it is a named association
9293 else
9294 -- E records the type of the discriminants in the named
9295 -- association. All the discriminants specified in the same name
9296 -- association must have the same type.
9298 E := Empty;
9300 -- Search the list of discriminants in T to see if the simple name
9301 -- given in the constraint matches any of them.
9303 Id := First (Selector_Names (Constr));
9304 while Present (Id) loop
9305 Found := False;
9307 -- If Original_Discriminant is present, we are processing a
9308 -- generic instantiation and this is an instance node. We need
9309 -- to find the name of the corresponding discriminant in the
9310 -- actual record type T and not the name of the discriminant in
9311 -- the generic formal. Example:
9313 -- generic
9314 -- type G (D : int) is private;
9315 -- package P is
9316 -- subtype W is G (D => 1);
9317 -- end package;
9318 -- type Rec (X : int) is record ... end record;
9319 -- package Q is new P (G => Rec);
9321 -- At the point of the instantiation, formal type G is Rec
9322 -- and therefore when reanalyzing "subtype W is G (D => 1);"
9323 -- which really looks like "subtype W is Rec (D => 1);" at
9324 -- the point of instantiation, we want to find the discriminant
9325 -- that corresponds to D in Rec, i.e. X.
9327 if Present (Original_Discriminant (Id))
9328 and then In_Instance
9329 then
9330 Discr := Find_Corresponding_Discriminant (Id, T);
9331 Found := True;
9333 else
9334 Discr := First_Discriminant (T);
9335 while Present (Discr) loop
9336 if Chars (Discr) = Chars (Id) then
9337 Found := True;
9338 exit;
9339 end if;
9341 Next_Discriminant (Discr);
9342 end loop;
9344 if not Found then
9345 Error_Msg_N ("& does not match any discriminant", Id);
9346 return New_Elmt_List;
9348 -- If the parent type is a generic formal, preserve the
9349 -- name of the discriminant for subsequent instances.
9350 -- see comment at the beginning of this if statement.
9352 elsif Is_Generic_Type (Root_Type (T)) then
9353 Set_Original_Discriminant (Id, Discr);
9354 end if;
9355 end if;
9357 Position := Pos_Of_Discr (T, Discr);
9359 if Present (Discr_Expr (Position)) then
9360 Error_Msg_N ("duplicate constraint for discriminant&", Id);
9362 else
9363 -- Each discriminant specified in the same named association
9364 -- must be associated with a separate copy of the
9365 -- corresponding expression.
9367 if Present (Next (Id)) then
9368 Expr := New_Copy_Tree (Expression (Constr));
9369 Set_Parent (Expr, Parent (Expression (Constr)));
9370 else
9371 Expr := Expression (Constr);
9372 end if;
9374 Discr_Expr (Position) := Expr;
9375 Process_Discriminant_Expression (Expr, Discr);
9376 end if;
9378 -- A discriminant association with more than one discriminant
9379 -- name is only allowed if the named discriminants are all of
9380 -- the same type (RM 3.7.1(8)).
9382 if E = Empty then
9383 E := Base_Type (Etype (Discr));
9385 elsif Base_Type (Etype (Discr)) /= E then
9386 Error_Msg_N
9387 ("all discriminants in an association " &
9388 "must have the same type", Id);
9389 end if;
9391 Next (Id);
9392 end loop;
9393 end if;
9395 Next (Constr);
9396 end loop;
9398 -- A discriminant constraint must provide exactly one value for each
9399 -- discriminant of the type (RM 3.7.1(8)).
9401 for J in Discr_Expr'Range loop
9402 if No (Discr_Expr (J)) then
9403 Error_Msg_N ("too few discriminants given in constraint", C);
9404 return New_Elmt_List;
9405 end if;
9406 end loop;
9408 -- Determine if there are discriminant expressions in the constraint
9410 for J in Discr_Expr'Range loop
9411 if Denotes_Discriminant
9412 (Discr_Expr (J), Check_Concurrent => True)
9413 then
9414 Discrim_Present := True;
9415 end if;
9416 end loop;
9418 -- Build an element list consisting of the expressions given in the
9419 -- discriminant constraint and apply the appropriate checks. The list
9420 -- is constructed after resolving any named discriminant associations
9421 -- and therefore the expressions appear in the textual order of the
9422 -- discriminants.
9424 Discr := First_Discriminant (T);
9425 for J in Discr_Expr'Range loop
9426 if Discr_Expr (J) /= Error then
9427 Append_Elmt (Discr_Expr (J), Elist);
9429 -- If any of the discriminant constraints is given by a
9430 -- discriminant and we are in a derived type declaration we
9431 -- have a discriminant renaming. Establish link between new
9432 -- and old discriminant.
9434 if Denotes_Discriminant (Discr_Expr (J)) then
9435 if Derived_Def then
9436 Set_Corresponding_Discriminant
9437 (Entity (Discr_Expr (J)), Discr);
9438 end if;
9440 -- Force the evaluation of non-discriminant expressions.
9441 -- If we have found a discriminant in the constraint 3.4(26)
9442 -- and 3.8(18) demand that no range checks are performed are
9443 -- after evaluation. If the constraint is for a component
9444 -- definition that has a per-object constraint, expressions are
9445 -- evaluated but not checked either. In all other cases perform
9446 -- a range check.
9448 else
9449 if Discrim_Present then
9450 null;
9452 elsif Nkind (Parent (Parent (Def))) = N_Component_Declaration
9453 and then
9454 Has_Per_Object_Constraint
9455 (Defining_Identifier (Parent (Parent (Def))))
9456 then
9457 null;
9459 elsif Is_Access_Type (Etype (Discr)) then
9460 Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
9462 else
9463 Apply_Range_Check (Discr_Expr (J), Etype (Discr));
9464 end if;
9466 Force_Evaluation (Discr_Expr (J));
9467 end if;
9469 -- Check that the designated type of an access discriminant's
9470 -- expression is not a class-wide type unless the discriminant's
9471 -- designated type is also class-wide.
9473 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
9474 and then not Is_Class_Wide_Type
9475 (Designated_Type (Etype (Discr)))
9476 and then Etype (Discr_Expr (J)) /= Any_Type
9477 and then Is_Class_Wide_Type
9478 (Designated_Type (Etype (Discr_Expr (J))))
9479 then
9480 Wrong_Type (Discr_Expr (J), Etype (Discr));
9482 elsif Is_Access_Type (Etype (Discr))
9483 and then not Is_Access_Constant (Etype (Discr))
9484 and then Is_Access_Type (Etype (Discr_Expr (J)))
9485 and then Is_Access_Constant (Etype (Discr_Expr (J)))
9486 then
9487 Error_Msg_NE
9488 ("constraint for discriminant& must be access to variable",
9489 Def, Discr);
9490 end if;
9491 end if;
9493 Next_Discriminant (Discr);
9494 end loop;
9496 return Elist;
9497 end Build_Discriminant_Constraints;
9499 ---------------------------------
9500 -- Build_Discriminated_Subtype --
9501 ---------------------------------
9503 procedure Build_Discriminated_Subtype
9504 (T : Entity_Id;
9505 Def_Id : Entity_Id;
9506 Elist : Elist_Id;
9507 Related_Nod : Node_Id;
9508 For_Access : Boolean := False)
9510 Has_Discrs : constant Boolean := Has_Discriminants (T);
9511 Constrained : constant Boolean :=
9512 (Has_Discrs
9513 and then not Is_Empty_Elmt_List (Elist)
9514 and then not Is_Class_Wide_Type (T))
9515 or else Is_Constrained (T);
9517 begin
9518 if Ekind (T) = E_Record_Type then
9519 if For_Access then
9520 Set_Ekind (Def_Id, E_Private_Subtype);
9521 Set_Is_For_Access_Subtype (Def_Id, True);
9522 else
9523 Set_Ekind (Def_Id, E_Record_Subtype);
9524 end if;
9526 -- Inherit preelaboration flag from base, for types for which it
9527 -- may have been set: records, private types, protected types.
9529 Set_Known_To_Have_Preelab_Init
9530 (Def_Id, Known_To_Have_Preelab_Init (T));
9532 elsif Ekind (T) = E_Task_Type then
9533 Set_Ekind (Def_Id, E_Task_Subtype);
9535 elsif Ekind (T) = E_Protected_Type then
9536 Set_Ekind (Def_Id, E_Protected_Subtype);
9537 Set_Known_To_Have_Preelab_Init
9538 (Def_Id, Known_To_Have_Preelab_Init (T));
9540 elsif Is_Private_Type (T) then
9541 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
9542 Set_Known_To_Have_Preelab_Init
9543 (Def_Id, Known_To_Have_Preelab_Init (T));
9545 -- Private subtypes may have private dependents
9547 Set_Private_Dependents (Def_Id, New_Elmt_List);
9549 elsif Is_Class_Wide_Type (T) then
9550 Set_Ekind (Def_Id, E_Class_Wide_Subtype);
9552 else
9553 -- Incomplete type. Attach subtype to list of dependents, to be
9554 -- completed with full view of parent type, unless is it the
9555 -- designated subtype of a record component within an init_proc.
9556 -- This last case arises for a component of an access type whose
9557 -- designated type is incomplete (e.g. a Taft Amendment type).
9558 -- The designated subtype is within an inner scope, and needs no
9559 -- elaboration, because only the access type is needed in the
9560 -- initialization procedure.
9562 Set_Ekind (Def_Id, Ekind (T));
9564 if For_Access and then Within_Init_Proc then
9565 null;
9566 else
9567 Append_Elmt (Def_Id, Private_Dependents (T));
9568 end if;
9569 end if;
9571 Set_Etype (Def_Id, T);
9572 Init_Size_Align (Def_Id);
9573 Set_Has_Discriminants (Def_Id, Has_Discrs);
9574 Set_Is_Constrained (Def_Id, Constrained);
9576 Set_First_Entity (Def_Id, First_Entity (T));
9577 Set_Last_Entity (Def_Id, Last_Entity (T));
9578 Set_Has_Implicit_Dereference
9579 (Def_Id, Has_Implicit_Dereference (T));
9581 -- If the subtype is the completion of a private declaration, there may
9582 -- have been representation clauses for the partial view, and they must
9583 -- be preserved. Build_Derived_Type chains the inherited clauses with
9584 -- the ones appearing on the extension. If this comes from a subtype
9585 -- declaration, all clauses are inherited.
9587 if No (First_Rep_Item (Def_Id)) then
9588 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
9589 end if;
9591 if Is_Tagged_Type (T) then
9592 Set_Is_Tagged_Type (Def_Id);
9593 Set_No_Tagged_Streams_Pragma (Def_Id, No_Tagged_Streams_Pragma (T));
9594 Make_Class_Wide_Type (Def_Id);
9595 end if;
9597 Set_Stored_Constraint (Def_Id, No_Elist);
9599 if Has_Discrs then
9600 Set_Discriminant_Constraint (Def_Id, Elist);
9601 Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
9602 end if;
9604 if Is_Tagged_Type (T) then
9606 -- Ada 2005 (AI-251): In case of concurrent types we inherit the
9607 -- concurrent record type (which has the list of primitive
9608 -- operations).
9610 if Ada_Version >= Ada_2005
9611 and then Is_Concurrent_Type (T)
9612 then
9613 Set_Corresponding_Record_Type (Def_Id,
9614 Corresponding_Record_Type (T));
9615 else
9616 Set_Direct_Primitive_Operations (Def_Id,
9617 Direct_Primitive_Operations (T));
9618 end if;
9620 Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
9621 end if;
9623 -- Subtypes introduced by component declarations do not need to be
9624 -- marked as delayed, and do not get freeze nodes, because the semantics
9625 -- verifies that the parents of the subtypes are frozen before the
9626 -- enclosing record is frozen.
9628 if not Is_Type (Scope (Def_Id)) then
9629 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
9631 if Is_Private_Type (T)
9632 and then Present (Full_View (T))
9633 then
9634 Conditional_Delay (Def_Id, Full_View (T));
9635 else
9636 Conditional_Delay (Def_Id, T);
9637 end if;
9638 end if;
9640 if Is_Record_Type (T) then
9641 Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
9643 if Has_Discrs
9644 and then not Is_Empty_Elmt_List (Elist)
9645 and then not For_Access
9646 then
9647 Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
9648 elsif not For_Access then
9649 Set_Cloned_Subtype (Def_Id, T);
9650 end if;
9651 end if;
9652 end Build_Discriminated_Subtype;
9654 ---------------------------
9655 -- Build_Itype_Reference --
9656 ---------------------------
9658 procedure Build_Itype_Reference
9659 (Ityp : Entity_Id;
9660 Nod : Node_Id)
9662 IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
9663 begin
9665 -- Itype references are only created for use by the back-end
9667 if Inside_A_Generic then
9668 return;
9669 else
9670 Set_Itype (IR, Ityp);
9671 Insert_After (Nod, IR);
9672 end if;
9673 end Build_Itype_Reference;
9675 ------------------------
9676 -- Build_Scalar_Bound --
9677 ------------------------
9679 function Build_Scalar_Bound
9680 (Bound : Node_Id;
9681 Par_T : Entity_Id;
9682 Der_T : Entity_Id) return Node_Id
9684 New_Bound : Entity_Id;
9686 begin
9687 -- Note: not clear why this is needed, how can the original bound
9688 -- be unanalyzed at this point? and if it is, what business do we
9689 -- have messing around with it? and why is the base type of the
9690 -- parent type the right type for the resolution. It probably is
9691 -- not. It is OK for the new bound we are creating, but not for
9692 -- the old one??? Still if it never happens, no problem.
9694 Analyze_And_Resolve (Bound, Base_Type (Par_T));
9696 if Nkind_In (Bound, N_Integer_Literal, N_Real_Literal) then
9697 New_Bound := New_Copy (Bound);
9698 Set_Etype (New_Bound, Der_T);
9699 Set_Analyzed (New_Bound);
9701 elsif Is_Entity_Name (Bound) then
9702 New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
9704 -- The following is almost certainly wrong. What business do we have
9705 -- relocating a node (Bound) that is presumably still attached to
9706 -- the tree elsewhere???
9708 else
9709 New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
9710 end if;
9712 Set_Etype (New_Bound, Der_T);
9713 return New_Bound;
9714 end Build_Scalar_Bound;
9716 --------------------------------
9717 -- Build_Underlying_Full_View --
9718 --------------------------------
9720 procedure Build_Underlying_Full_View
9721 (N : Node_Id;
9722 Typ : Entity_Id;
9723 Par : Entity_Id)
9725 Loc : constant Source_Ptr := Sloc (N);
9726 Subt : constant Entity_Id :=
9727 Make_Defining_Identifier
9728 (Loc, New_External_Name (Chars (Typ), 'S'));
9730 Constr : Node_Id;
9731 Indic : Node_Id;
9732 C : Node_Id;
9733 Id : Node_Id;
9735 procedure Set_Discriminant_Name (Id : Node_Id);
9736 -- If the derived type has discriminants, they may rename discriminants
9737 -- of the parent. When building the full view of the parent, we need to
9738 -- recover the names of the original discriminants if the constraint is
9739 -- given by named associations.
9741 ---------------------------
9742 -- Set_Discriminant_Name --
9743 ---------------------------
9745 procedure Set_Discriminant_Name (Id : Node_Id) is
9746 Disc : Entity_Id;
9748 begin
9749 Set_Original_Discriminant (Id, Empty);
9751 if Has_Discriminants (Typ) then
9752 Disc := First_Discriminant (Typ);
9753 while Present (Disc) loop
9754 if Chars (Disc) = Chars (Id)
9755 and then Present (Corresponding_Discriminant (Disc))
9756 then
9757 Set_Chars (Id, Chars (Corresponding_Discriminant (Disc)));
9758 end if;
9759 Next_Discriminant (Disc);
9760 end loop;
9761 end if;
9762 end Set_Discriminant_Name;
9764 -- Start of processing for Build_Underlying_Full_View
9766 begin
9767 if Nkind (N) = N_Full_Type_Declaration then
9768 Constr := Constraint (Subtype_Indication (Type_Definition (N)));
9770 elsif Nkind (N) = N_Subtype_Declaration then
9771 Constr := New_Copy_Tree (Constraint (Subtype_Indication (N)));
9773 elsif Nkind (N) = N_Component_Declaration then
9774 Constr :=
9775 New_Copy_Tree
9776 (Constraint (Subtype_Indication (Component_Definition (N))));
9778 else
9779 raise Program_Error;
9780 end if;
9782 C := First (Constraints (Constr));
9783 while Present (C) loop
9784 if Nkind (C) = N_Discriminant_Association then
9785 Id := First (Selector_Names (C));
9786 while Present (Id) loop
9787 Set_Discriminant_Name (Id);
9788 Next (Id);
9789 end loop;
9790 end if;
9792 Next (C);
9793 end loop;
9795 Indic :=
9796 Make_Subtype_Declaration (Loc,
9797 Defining_Identifier => Subt,
9798 Subtype_Indication =>
9799 Make_Subtype_Indication (Loc,
9800 Subtype_Mark => New_Occurrence_Of (Par, Loc),
9801 Constraint => New_Copy_Tree (Constr)));
9803 -- If this is a component subtype for an outer itype, it is not
9804 -- a list member, so simply set the parent link for analysis: if
9805 -- the enclosing type does not need to be in a declarative list,
9806 -- neither do the components.
9808 if Is_List_Member (N)
9809 and then Nkind (N) /= N_Component_Declaration
9810 then
9811 Insert_Before (N, Indic);
9812 else
9813 Set_Parent (Indic, Parent (N));
9814 end if;
9816 Analyze (Indic);
9817 Set_Underlying_Full_View (Typ, Full_View (Subt));
9818 end Build_Underlying_Full_View;
9820 -------------------------------
9821 -- Check_Abstract_Overriding --
9822 -------------------------------
9824 procedure Check_Abstract_Overriding (T : Entity_Id) is
9825 Alias_Subp : Entity_Id;
9826 Elmt : Elmt_Id;
9827 Op_List : Elist_Id;
9828 Subp : Entity_Id;
9829 Type_Def : Node_Id;
9831 procedure Check_Pragma_Implemented (Subp : Entity_Id);
9832 -- Ada 2012 (AI05-0030): Subprogram Subp overrides an interface routine
9833 -- which has pragma Implemented already set. Check whether Subp's entity
9834 -- kind conforms to the implementation kind of the overridden routine.
9836 procedure Check_Pragma_Implemented
9837 (Subp : Entity_Id;
9838 Iface_Subp : Entity_Id);
9839 -- Ada 2012 (AI05-0030): Subprogram Subp overrides interface routine
9840 -- Iface_Subp and both entities have pragma Implemented already set on
9841 -- them. Check whether the two implementation kinds are conforming.
9843 procedure Inherit_Pragma_Implemented
9844 (Subp : Entity_Id;
9845 Iface_Subp : Entity_Id);
9846 -- Ada 2012 (AI05-0030): Interface primitive Subp overrides interface
9847 -- subprogram Iface_Subp which has been marked by pragma Implemented.
9848 -- Propagate the implementation kind of Iface_Subp to Subp.
9850 ------------------------------
9851 -- Check_Pragma_Implemented --
9852 ------------------------------
9854 procedure Check_Pragma_Implemented (Subp : Entity_Id) is
9855 Iface_Alias : constant Entity_Id := Interface_Alias (Subp);
9856 Impl_Kind : constant Name_Id := Implementation_Kind (Iface_Alias);
9857 Subp_Alias : constant Entity_Id := Alias (Subp);
9858 Contr_Typ : Entity_Id;
9859 Impl_Subp : Entity_Id;
9861 begin
9862 -- Subp must have an alias since it is a hidden entity used to link
9863 -- an interface subprogram to its overriding counterpart.
9865 pragma Assert (Present (Subp_Alias));
9867 -- Handle aliases to synchronized wrappers
9869 Impl_Subp := Subp_Alias;
9871 if Is_Primitive_Wrapper (Impl_Subp) then
9872 Impl_Subp := Wrapped_Entity (Impl_Subp);
9873 end if;
9875 -- Extract the type of the controlling formal
9877 Contr_Typ := Etype (First_Formal (Subp_Alias));
9879 if Is_Concurrent_Record_Type (Contr_Typ) then
9880 Contr_Typ := Corresponding_Concurrent_Type (Contr_Typ);
9881 end if;
9883 -- An interface subprogram whose implementation kind is By_Entry must
9884 -- be implemented by an entry.
9886 if Impl_Kind = Name_By_Entry
9887 and then Ekind (Impl_Subp) /= E_Entry
9888 then
9889 Error_Msg_Node_2 := Iface_Alias;
9890 Error_Msg_NE
9891 ("type & must implement abstract subprogram & with an entry",
9892 Subp_Alias, Contr_Typ);
9894 elsif Impl_Kind = Name_By_Protected_Procedure then
9896 -- An interface subprogram whose implementation kind is By_
9897 -- Protected_Procedure cannot be implemented by a primitive
9898 -- procedure of a task type.
9900 if Ekind (Contr_Typ) /= E_Protected_Type then
9901 Error_Msg_Node_2 := Contr_Typ;
9902 Error_Msg_NE
9903 ("interface subprogram & cannot be implemented by a " &
9904 "primitive procedure of task type &", Subp_Alias,
9905 Iface_Alias);
9907 -- An interface subprogram whose implementation kind is By_
9908 -- Protected_Procedure must be implemented by a procedure.
9910 elsif Ekind (Impl_Subp) /= E_Procedure then
9911 Error_Msg_Node_2 := Iface_Alias;
9912 Error_Msg_NE
9913 ("type & must implement abstract subprogram & with a " &
9914 "procedure", Subp_Alias, Contr_Typ);
9916 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
9917 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
9918 then
9919 Error_Msg_Name_1 := Impl_Kind;
9920 Error_Msg_N
9921 ("overriding operation& must have synchronization%",
9922 Subp_Alias);
9923 end if;
9925 -- If primitive has Optional synchronization, overriding operation
9926 -- must match if it has an explicit synchronization..
9928 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
9929 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
9930 then
9931 Error_Msg_Name_1 := Impl_Kind;
9932 Error_Msg_N
9933 ("overriding operation& must have syncrhonization%",
9934 Subp_Alias);
9935 end if;
9936 end Check_Pragma_Implemented;
9938 ------------------------------
9939 -- Check_Pragma_Implemented --
9940 ------------------------------
9942 procedure Check_Pragma_Implemented
9943 (Subp : Entity_Id;
9944 Iface_Subp : Entity_Id)
9946 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
9947 Subp_Kind : constant Name_Id := Implementation_Kind (Subp);
9949 begin
9950 -- Ada 2012 (AI05-0030): The implementation kinds of an overridden
9951 -- and overriding subprogram are different. In general this is an
9952 -- error except when the implementation kind of the overridden
9953 -- subprograms is By_Any or Optional.
9955 if Iface_Kind /= Subp_Kind
9956 and then Iface_Kind /= Name_By_Any
9957 and then Iface_Kind /= Name_Optional
9958 then
9959 if Iface_Kind = Name_By_Entry then
9960 Error_Msg_N
9961 ("incompatible implementation kind, overridden subprogram " &
9962 "is marked By_Entry", Subp);
9963 else
9964 Error_Msg_N
9965 ("incompatible implementation kind, overridden subprogram " &
9966 "is marked By_Protected_Procedure", Subp);
9967 end if;
9968 end if;
9969 end Check_Pragma_Implemented;
9971 --------------------------------
9972 -- Inherit_Pragma_Implemented --
9973 --------------------------------
9975 procedure Inherit_Pragma_Implemented
9976 (Subp : Entity_Id;
9977 Iface_Subp : Entity_Id)
9979 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
9980 Loc : constant Source_Ptr := Sloc (Subp);
9981 Impl_Prag : Node_Id;
9983 begin
9984 -- Since the implementation kind is stored as a representation item
9985 -- rather than a flag, create a pragma node.
9987 Impl_Prag :=
9988 Make_Pragma (Loc,
9989 Chars => Name_Implemented,
9990 Pragma_Argument_Associations => New_List (
9991 Make_Pragma_Argument_Association (Loc,
9992 Expression => New_Occurrence_Of (Subp, Loc)),
9994 Make_Pragma_Argument_Association (Loc,
9995 Expression => Make_Identifier (Loc, Iface_Kind))));
9997 -- The pragma doesn't need to be analyzed because it is internally
9998 -- built. It is safe to directly register it as a rep item since we
9999 -- are only interested in the characters of the implementation kind.
10001 Record_Rep_Item (Subp, Impl_Prag);
10002 end Inherit_Pragma_Implemented;
10004 -- Start of processing for Check_Abstract_Overriding
10006 begin
10007 Op_List := Primitive_Operations (T);
10009 -- Loop to check primitive operations
10011 Elmt := First_Elmt (Op_List);
10012 while Present (Elmt) loop
10013 Subp := Node (Elmt);
10014 Alias_Subp := Alias (Subp);
10016 -- Inherited subprograms are identified by the fact that they do not
10017 -- come from source, and the associated source location is the
10018 -- location of the first subtype of the derived type.
10020 -- Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
10021 -- subprograms that "require overriding".
10023 -- Special exception, do not complain about failure to override the
10024 -- stream routines _Input and _Output, as well as the primitive
10025 -- operations used in dispatching selects since we always provide
10026 -- automatic overridings for these subprograms.
10028 -- Also ignore this rule for convention CIL since .NET libraries
10029 -- do bizarre things with interfaces???
10031 -- The partial view of T may have been a private extension, for
10032 -- which inherited functions dispatching on result are abstract.
10033 -- If the full view is a null extension, there is no need for
10034 -- overriding in Ada 2005, but wrappers need to be built for them
10035 -- (see exp_ch3, Build_Controlling_Function_Wrappers).
10037 if Is_Null_Extension (T)
10038 and then Has_Controlling_Result (Subp)
10039 and then Ada_Version >= Ada_2005
10040 and then Present (Alias_Subp)
10041 and then not Comes_From_Source (Subp)
10042 and then not Is_Abstract_Subprogram (Alias_Subp)
10043 and then not Is_Access_Type (Etype (Subp))
10044 then
10045 null;
10047 -- Ada 2005 (AI-251): Internal entities of interfaces need no
10048 -- processing because this check is done with the aliased
10049 -- entity
10051 elsif Present (Interface_Alias (Subp)) then
10052 null;
10054 elsif (Is_Abstract_Subprogram (Subp)
10055 or else Requires_Overriding (Subp)
10056 or else
10057 (Has_Controlling_Result (Subp)
10058 and then Present (Alias_Subp)
10059 and then not Comes_From_Source (Subp)
10060 and then Sloc (Subp) = Sloc (First_Subtype (T))))
10061 and then not Is_TSS (Subp, TSS_Stream_Input)
10062 and then not Is_TSS (Subp, TSS_Stream_Output)
10063 and then not Is_Abstract_Type (T)
10064 and then Convention (T) /= Convention_CIL
10065 and then not Is_Predefined_Interface_Primitive (Subp)
10067 -- Ada 2005 (AI-251): Do not consider hidden entities associated
10068 -- with abstract interface types because the check will be done
10069 -- with the aliased entity (otherwise we generate a duplicated
10070 -- error message).
10072 and then not Present (Interface_Alias (Subp))
10073 then
10074 if Present (Alias_Subp) then
10076 -- Only perform the check for a derived subprogram when the
10077 -- type has an explicit record extension. This avoids incorrect
10078 -- flagging of abstract subprograms for the case of a type
10079 -- without an extension that is derived from a formal type
10080 -- with a tagged actual (can occur within a private part).
10082 -- Ada 2005 (AI-391): In the case of an inherited function with
10083 -- a controlling result of the type, the rule does not apply if
10084 -- the type is a null extension (unless the parent function
10085 -- itself is abstract, in which case the function must still be
10086 -- be overridden). The expander will generate an overriding
10087 -- wrapper function calling the parent subprogram (see
10088 -- Exp_Ch3.Make_Controlling_Wrapper_Functions).
10090 Type_Def := Type_Definition (Parent (T));
10092 if Nkind (Type_Def) = N_Derived_Type_Definition
10093 and then Present (Record_Extension_Part (Type_Def))
10094 and then
10095 (Ada_Version < Ada_2005
10096 or else not Is_Null_Extension (T)
10097 or else Ekind (Subp) = E_Procedure
10098 or else not Has_Controlling_Result (Subp)
10099 or else Is_Abstract_Subprogram (Alias_Subp)
10100 or else Requires_Overriding (Subp)
10101 or else Is_Access_Type (Etype (Subp)))
10102 then
10103 -- Avoid reporting error in case of abstract predefined
10104 -- primitive inherited from interface type because the
10105 -- body of internally generated predefined primitives
10106 -- of tagged types are generated later by Freeze_Type
10108 if Is_Interface (Root_Type (T))
10109 and then Is_Abstract_Subprogram (Subp)
10110 and then Is_Predefined_Dispatching_Operation (Subp)
10111 and then not Comes_From_Source (Ultimate_Alias (Subp))
10112 then
10113 null;
10115 -- A null extension is not obliged to override an inherited
10116 -- procedure subject to pragma Extensions_Visible with value
10117 -- False and at least one controlling OUT parameter
10118 -- (SPARK RM 6.1.7(6)).
10120 elsif Is_Null_Extension (T)
10121 and then Is_EVF_Procedure (Subp)
10122 then
10123 null;
10125 else
10126 Error_Msg_NE
10127 ("type must be declared abstract or & overridden",
10128 T, Subp);
10130 -- Traverse the whole chain of aliased subprograms to
10131 -- complete the error notification. This is especially
10132 -- useful for traceability of the chain of entities when
10133 -- the subprogram corresponds with an interface
10134 -- subprogram (which may be defined in another package).
10136 if Present (Alias_Subp) then
10137 declare
10138 E : Entity_Id;
10140 begin
10141 E := Subp;
10142 while Present (Alias (E)) loop
10144 -- Avoid reporting redundant errors on entities
10145 -- inherited from interfaces
10147 if Sloc (E) /= Sloc (T) then
10148 Error_Msg_Sloc := Sloc (E);
10149 Error_Msg_NE
10150 ("\& has been inherited #", T, Subp);
10151 end if;
10153 E := Alias (E);
10154 end loop;
10156 Error_Msg_Sloc := Sloc (E);
10158 -- AI05-0068: report if there is an overriding
10159 -- non-abstract subprogram that is invisible.
10161 if Is_Hidden (E)
10162 and then not Is_Abstract_Subprogram (E)
10163 then
10164 Error_Msg_NE
10165 ("\& subprogram# is not visible",
10166 T, Subp);
10168 -- Clarify the case where a non-null extension must
10169 -- override inherited procedure subject to pragma
10170 -- Extensions_Visible with value False and at least
10171 -- one controlling OUT param.
10173 elsif Is_EVF_Procedure (E) then
10174 Error_Msg_NE
10175 ("\& # is subject to Extensions_Visible False",
10176 T, Subp);
10178 else
10179 Error_Msg_NE
10180 ("\& has been inherited from subprogram #",
10181 T, Subp);
10182 end if;
10183 end;
10184 end if;
10185 end if;
10187 -- Ada 2005 (AI-345): Protected or task type implementing
10188 -- abstract interfaces.
10190 elsif Is_Concurrent_Record_Type (T)
10191 and then Present (Interfaces (T))
10192 then
10193 -- There is no need to check here RM 9.4(11.9/3) since we
10194 -- are processing the corresponding record type and the
10195 -- mode of the overriding subprograms was verified by
10196 -- Check_Conformance when the corresponding concurrent
10197 -- type declaration was analyzed.
10199 Error_Msg_NE
10200 ("interface subprogram & must be overridden", T, Subp);
10202 -- Examine primitive operations of synchronized type to find
10203 -- homonyms that have the wrong profile.
10205 declare
10206 Prim : Entity_Id;
10208 begin
10209 Prim := First_Entity (Corresponding_Concurrent_Type (T));
10210 while Present (Prim) loop
10211 if Chars (Prim) = Chars (Subp) then
10212 Error_Msg_NE
10213 ("profile is not type conformant with prefixed "
10214 & "view profile of inherited operation&",
10215 Prim, Subp);
10216 end if;
10218 Next_Entity (Prim);
10219 end loop;
10220 end;
10221 end if;
10223 else
10224 Error_Msg_Node_2 := T;
10225 Error_Msg_N
10226 ("abstract subprogram& not allowed for type&", Subp);
10228 -- Also post unconditional warning on the type (unconditional
10229 -- so that if there are more than one of these cases, we get
10230 -- them all, and not just the first one).
10232 Error_Msg_Node_2 := Subp;
10233 Error_Msg_N ("nonabstract type& has abstract subprogram&!", T);
10234 end if;
10236 -- A subprogram subject to pragma Extensions_Visible with value
10237 -- "True" cannot override a subprogram subject to the same pragma
10238 -- with value "False" (SPARK RM 6.1.7(5)).
10240 elsif Extensions_Visible_Status (Subp) = Extensions_Visible_True
10241 and then Present (Overridden_Operation (Subp))
10242 and then Extensions_Visible_Status (Overridden_Operation (Subp)) =
10243 Extensions_Visible_False
10244 then
10245 Error_Msg_Sloc := Sloc (Overridden_Operation (Subp));
10246 Error_Msg_N
10247 ("subprogram & with Extensions_Visible True cannot override "
10248 & "subprogram # with Extensions_Visible False", Subp);
10249 end if;
10251 -- Ada 2012 (AI05-0030): Perform checks related to pragma Implemented
10253 -- Subp is an expander-generated procedure which maps an interface
10254 -- alias to a protected wrapper. The interface alias is flagged by
10255 -- pragma Implemented. Ensure that Subp is a procedure when the
10256 -- implementation kind is By_Protected_Procedure or an entry when
10257 -- By_Entry.
10259 if Ada_Version >= Ada_2012
10260 and then Is_Hidden (Subp)
10261 and then Present (Interface_Alias (Subp))
10262 and then Has_Rep_Pragma (Interface_Alias (Subp), Name_Implemented)
10263 then
10264 Check_Pragma_Implemented (Subp);
10265 end if;
10267 -- Subp is an interface primitive which overrides another interface
10268 -- primitive marked with pragma Implemented.
10270 if Ada_Version >= Ada_2012
10271 and then Present (Overridden_Operation (Subp))
10272 and then Has_Rep_Pragma
10273 (Overridden_Operation (Subp), Name_Implemented)
10274 then
10275 -- If the overriding routine is also marked by Implemented, check
10276 -- that the two implementation kinds are conforming.
10278 if Has_Rep_Pragma (Subp, Name_Implemented) then
10279 Check_Pragma_Implemented
10280 (Subp => Subp,
10281 Iface_Subp => Overridden_Operation (Subp));
10283 -- Otherwise the overriding routine inherits the implementation
10284 -- kind from the overridden subprogram.
10286 else
10287 Inherit_Pragma_Implemented
10288 (Subp => Subp,
10289 Iface_Subp => Overridden_Operation (Subp));
10290 end if;
10291 end if;
10293 -- If the operation is a wrapper for a synchronized primitive, it
10294 -- may be called indirectly through a dispatching select. We assume
10295 -- that it will be referenced elsewhere indirectly, and suppress
10296 -- warnings about an unused entity.
10298 if Is_Primitive_Wrapper (Subp)
10299 and then Present (Wrapped_Entity (Subp))
10300 then
10301 Set_Referenced (Wrapped_Entity (Subp));
10302 end if;
10304 Next_Elmt (Elmt);
10305 end loop;
10306 end Check_Abstract_Overriding;
10308 ------------------------------------------------
10309 -- Check_Access_Discriminant_Requires_Limited --
10310 ------------------------------------------------
10312 procedure Check_Access_Discriminant_Requires_Limited
10313 (D : Node_Id;
10314 Loc : Node_Id)
10316 begin
10317 -- A discriminant_specification for an access discriminant shall appear
10318 -- only in the declaration for a task or protected type, or for a type
10319 -- with the reserved word 'limited' in its definition or in one of its
10320 -- ancestors (RM 3.7(10)).
10322 -- AI-0063: The proper condition is that type must be immutably limited,
10323 -- or else be a partial view.
10325 if Nkind (Discriminant_Type (D)) = N_Access_Definition then
10326 if Is_Limited_View (Current_Scope)
10327 or else
10328 (Nkind (Parent (Current_Scope)) = N_Private_Type_Declaration
10329 and then Limited_Present (Parent (Current_Scope)))
10330 then
10331 null;
10333 else
10334 Error_Msg_N
10335 ("access discriminants allowed only for limited types", Loc);
10336 end if;
10337 end if;
10338 end Check_Access_Discriminant_Requires_Limited;
10340 -----------------------------------
10341 -- Check_Aliased_Component_Types --
10342 -----------------------------------
10344 procedure Check_Aliased_Component_Types (T : Entity_Id) is
10345 C : Entity_Id;
10347 begin
10348 -- ??? Also need to check components of record extensions, but not
10349 -- components of protected types (which are always limited).
10351 -- Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
10352 -- types to be unconstrained. This is safe because it is illegal to
10353 -- create access subtypes to such types with explicit discriminant
10354 -- constraints.
10356 if not Is_Limited_Type (T) then
10357 if Ekind (T) = E_Record_Type then
10358 C := First_Component (T);
10359 while Present (C) loop
10360 if Is_Aliased (C)
10361 and then Has_Discriminants (Etype (C))
10362 and then not Is_Constrained (Etype (C))
10363 and then not In_Instance_Body
10364 and then Ada_Version < Ada_2005
10365 then
10366 Error_Msg_N
10367 ("aliased component must be constrained (RM 3.6(11))",
10369 end if;
10371 Next_Component (C);
10372 end loop;
10374 elsif Ekind (T) = E_Array_Type then
10375 if Has_Aliased_Components (T)
10376 and then Has_Discriminants (Component_Type (T))
10377 and then not Is_Constrained (Component_Type (T))
10378 and then not In_Instance_Body
10379 and then Ada_Version < Ada_2005
10380 then
10381 Error_Msg_N
10382 ("aliased component type must be constrained (RM 3.6(11))",
10384 end if;
10385 end if;
10386 end if;
10387 end Check_Aliased_Component_Types;
10389 ---------------------------------------
10390 -- Check_Anonymous_Access_Components --
10391 ---------------------------------------
10393 procedure Check_Anonymous_Access_Components
10394 (Typ_Decl : Node_Id;
10395 Typ : Entity_Id;
10396 Prev : Entity_Id;
10397 Comp_List : Node_Id)
10399 Loc : constant Source_Ptr := Sloc (Typ_Decl);
10400 Anon_Access : Entity_Id;
10401 Acc_Def : Node_Id;
10402 Comp : Node_Id;
10403 Comp_Def : Node_Id;
10404 Decl : Node_Id;
10405 Type_Def : Node_Id;
10407 procedure Build_Incomplete_Type_Declaration;
10408 -- If the record type contains components that include an access to the
10409 -- current record, then create an incomplete type declaration for the
10410 -- record, to be used as the designated type of the anonymous access.
10411 -- This is done only once, and only if there is no previous partial
10412 -- view of the type.
10414 function Designates_T (Subt : Node_Id) return Boolean;
10415 -- Check whether a node designates the enclosing record type, or 'Class
10416 -- of that type
10418 function Mentions_T (Acc_Def : Node_Id) return Boolean;
10419 -- Check whether an access definition includes a reference to
10420 -- the enclosing record type. The reference can be a subtype mark
10421 -- in the access definition itself, a 'Class attribute reference, or
10422 -- recursively a reference appearing in a parameter specification
10423 -- or result definition of an access_to_subprogram definition.
10425 --------------------------------------
10426 -- Build_Incomplete_Type_Declaration --
10427 --------------------------------------
10429 procedure Build_Incomplete_Type_Declaration is
10430 Decl : Node_Id;
10431 Inc_T : Entity_Id;
10432 H : Entity_Id;
10434 -- Is_Tagged indicates whether the type is tagged. It is tagged if
10435 -- it's "is new ... with record" or else "is tagged record ...".
10437 Is_Tagged : constant Boolean :=
10438 (Nkind (Type_Definition (Typ_Decl)) = N_Derived_Type_Definition
10439 and then
10440 Present (Record_Extension_Part (Type_Definition (Typ_Decl))))
10441 or else
10442 (Nkind (Type_Definition (Typ_Decl)) = N_Record_Definition
10443 and then Tagged_Present (Type_Definition (Typ_Decl)));
10445 begin
10446 -- If there is a previous partial view, no need to create a new one
10447 -- If the partial view, given by Prev, is incomplete, If Prev is
10448 -- a private declaration, full declaration is flagged accordingly.
10450 if Prev /= Typ then
10451 if Is_Tagged then
10452 Make_Class_Wide_Type (Prev);
10453 Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
10454 Set_Etype (Class_Wide_Type (Typ), Typ);
10455 end if;
10457 return;
10459 elsif Has_Private_Declaration (Typ) then
10461 -- If we refer to T'Class inside T, and T is the completion of a
10462 -- private type, then make sure the class-wide type exists.
10464 if Is_Tagged then
10465 Make_Class_Wide_Type (Typ);
10466 end if;
10468 return;
10470 -- If there was a previous anonymous access type, the incomplete
10471 -- type declaration will have been created already.
10473 elsif Present (Current_Entity (Typ))
10474 and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
10475 and then Full_View (Current_Entity (Typ)) = Typ
10476 then
10477 if Is_Tagged
10478 and then Comes_From_Source (Current_Entity (Typ))
10479 and then not Is_Tagged_Type (Current_Entity (Typ))
10480 then
10481 Make_Class_Wide_Type (Typ);
10482 Error_Msg_N
10483 ("incomplete view of tagged type should be declared tagged??",
10484 Parent (Current_Entity (Typ)));
10485 end if;
10486 return;
10488 else
10489 Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
10490 Decl := Make_Incomplete_Type_Declaration (Loc, Inc_T);
10492 -- Type has already been inserted into the current scope. Remove
10493 -- it, and add incomplete declaration for type, so that subsequent
10494 -- anonymous access types can use it. The entity is unchained from
10495 -- the homonym list and from immediate visibility. After analysis,
10496 -- the entity in the incomplete declaration becomes immediately
10497 -- visible in the record declaration that follows.
10499 H := Current_Entity (Typ);
10501 if H = Typ then
10502 Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
10503 else
10504 while Present (H)
10505 and then Homonym (H) /= Typ
10506 loop
10507 H := Homonym (Typ);
10508 end loop;
10510 Set_Homonym (H, Homonym (Typ));
10511 end if;
10513 Insert_Before (Typ_Decl, Decl);
10514 Analyze (Decl);
10515 Set_Full_View (Inc_T, Typ);
10517 if Is_Tagged then
10519 -- Create a common class-wide type for both views, and set the
10520 -- Etype of the class-wide type to the full view.
10522 Make_Class_Wide_Type (Inc_T);
10523 Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
10524 Set_Etype (Class_Wide_Type (Typ), Typ);
10525 end if;
10526 end if;
10527 end Build_Incomplete_Type_Declaration;
10529 ------------------
10530 -- Designates_T --
10531 ------------------
10533 function Designates_T (Subt : Node_Id) return Boolean is
10534 Type_Id : constant Name_Id := Chars (Typ);
10536 function Names_T (Nam : Node_Id) return Boolean;
10537 -- The record type has not been introduced in the current scope
10538 -- yet, so we must examine the name of the type itself, either
10539 -- an identifier T, or an expanded name of the form P.T, where
10540 -- P denotes the current scope.
10542 -------------
10543 -- Names_T --
10544 -------------
10546 function Names_T (Nam : Node_Id) return Boolean is
10547 begin
10548 if Nkind (Nam) = N_Identifier then
10549 return Chars (Nam) = Type_Id;
10551 elsif Nkind (Nam) = N_Selected_Component then
10552 if Chars (Selector_Name (Nam)) = Type_Id then
10553 if Nkind (Prefix (Nam)) = N_Identifier then
10554 return Chars (Prefix (Nam)) = Chars (Current_Scope);
10556 elsif Nkind (Prefix (Nam)) = N_Selected_Component then
10557 return Chars (Selector_Name (Prefix (Nam))) =
10558 Chars (Current_Scope);
10559 else
10560 return False;
10561 end if;
10563 else
10564 return False;
10565 end if;
10567 else
10568 return False;
10569 end if;
10570 end Names_T;
10572 -- Start of processing for Designates_T
10574 begin
10575 if Nkind (Subt) = N_Identifier then
10576 return Chars (Subt) = Type_Id;
10578 -- Reference can be through an expanded name which has not been
10579 -- analyzed yet, and which designates enclosing scopes.
10581 elsif Nkind (Subt) = N_Selected_Component then
10582 if Names_T (Subt) then
10583 return True;
10585 -- Otherwise it must denote an entity that is already visible.
10586 -- The access definition may name a subtype of the enclosing
10587 -- type, if there is a previous incomplete declaration for it.
10589 else
10590 Find_Selected_Component (Subt);
10591 return
10592 Is_Entity_Name (Subt)
10593 and then Scope (Entity (Subt)) = Current_Scope
10594 and then
10595 (Chars (Base_Type (Entity (Subt))) = Type_Id
10596 or else
10597 (Is_Class_Wide_Type (Entity (Subt))
10598 and then
10599 Chars (Etype (Base_Type (Entity (Subt)))) =
10600 Type_Id));
10601 end if;
10603 -- A reference to the current type may appear as the prefix of
10604 -- a 'Class attribute.
10606 elsif Nkind (Subt) = N_Attribute_Reference
10607 and then Attribute_Name (Subt) = Name_Class
10608 then
10609 return Names_T (Prefix (Subt));
10611 else
10612 return False;
10613 end if;
10614 end Designates_T;
10616 ----------------
10617 -- Mentions_T --
10618 ----------------
10620 function Mentions_T (Acc_Def : Node_Id) return Boolean is
10621 Param_Spec : Node_Id;
10623 Acc_Subprg : constant Node_Id :=
10624 Access_To_Subprogram_Definition (Acc_Def);
10626 begin
10627 if No (Acc_Subprg) then
10628 return Designates_T (Subtype_Mark (Acc_Def));
10629 end if;
10631 -- Component is an access_to_subprogram: examine its formals,
10632 -- and result definition in the case of an access_to_function.
10634 Param_Spec := First (Parameter_Specifications (Acc_Subprg));
10635 while Present (Param_Spec) loop
10636 if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
10637 and then Mentions_T (Parameter_Type (Param_Spec))
10638 then
10639 return True;
10641 elsif Designates_T (Parameter_Type (Param_Spec)) then
10642 return True;
10643 end if;
10645 Next (Param_Spec);
10646 end loop;
10648 if Nkind (Acc_Subprg) = N_Access_Function_Definition then
10649 if Nkind (Result_Definition (Acc_Subprg)) =
10650 N_Access_Definition
10651 then
10652 return Mentions_T (Result_Definition (Acc_Subprg));
10653 else
10654 return Designates_T (Result_Definition (Acc_Subprg));
10655 end if;
10656 end if;
10658 return False;
10659 end Mentions_T;
10661 -- Start of processing for Check_Anonymous_Access_Components
10663 begin
10664 if No (Comp_List) then
10665 return;
10666 end if;
10668 Comp := First (Component_Items (Comp_List));
10669 while Present (Comp) loop
10670 if Nkind (Comp) = N_Component_Declaration
10671 and then Present
10672 (Access_Definition (Component_Definition (Comp)))
10673 and then
10674 Mentions_T (Access_Definition (Component_Definition (Comp)))
10675 then
10676 Comp_Def := Component_Definition (Comp);
10677 Acc_Def :=
10678 Access_To_Subprogram_Definition (Access_Definition (Comp_Def));
10680 Build_Incomplete_Type_Declaration;
10681 Anon_Access := Make_Temporary (Loc, 'S');
10683 -- Create a declaration for the anonymous access type: either
10684 -- an access_to_object or an access_to_subprogram.
10686 if Present (Acc_Def) then
10687 if Nkind (Acc_Def) = N_Access_Function_Definition then
10688 Type_Def :=
10689 Make_Access_Function_Definition (Loc,
10690 Parameter_Specifications =>
10691 Parameter_Specifications (Acc_Def),
10692 Result_Definition => Result_Definition (Acc_Def));
10693 else
10694 Type_Def :=
10695 Make_Access_Procedure_Definition (Loc,
10696 Parameter_Specifications =>
10697 Parameter_Specifications (Acc_Def));
10698 end if;
10700 else
10701 Type_Def :=
10702 Make_Access_To_Object_Definition (Loc,
10703 Subtype_Indication =>
10704 Relocate_Node
10705 (Subtype_Mark (Access_Definition (Comp_Def))));
10707 Set_Constant_Present
10708 (Type_Def, Constant_Present (Access_Definition (Comp_Def)));
10709 Set_All_Present
10710 (Type_Def, All_Present (Access_Definition (Comp_Def)));
10711 end if;
10713 Set_Null_Exclusion_Present
10714 (Type_Def,
10715 Null_Exclusion_Present (Access_Definition (Comp_Def)));
10717 Decl :=
10718 Make_Full_Type_Declaration (Loc,
10719 Defining_Identifier => Anon_Access,
10720 Type_Definition => Type_Def);
10722 Insert_Before (Typ_Decl, Decl);
10723 Analyze (Decl);
10725 -- If an access to subprogram, create the extra formals
10727 if Present (Acc_Def) then
10728 Create_Extra_Formals (Designated_Type (Anon_Access));
10730 -- If an access to object, preserve entity of designated type,
10731 -- for ASIS use, before rewriting the component definition.
10733 else
10734 declare
10735 Desig : Entity_Id;
10737 begin
10738 Desig := Entity (Subtype_Indication (Type_Def));
10740 -- If the access definition is to the current record,
10741 -- the visible entity at this point is an incomplete
10742 -- type. Retrieve the full view to simplify ASIS queries
10744 if Ekind (Desig) = E_Incomplete_Type then
10745 Desig := Full_View (Desig);
10746 end if;
10748 Set_Entity
10749 (Subtype_Mark (Access_Definition (Comp_Def)), Desig);
10750 end;
10751 end if;
10753 Rewrite (Comp_Def,
10754 Make_Component_Definition (Loc,
10755 Subtype_Indication =>
10756 New_Occurrence_Of (Anon_Access, Loc)));
10758 if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
10759 Set_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
10760 else
10761 Set_Ekind (Anon_Access, E_Anonymous_Access_Type);
10762 end if;
10764 Set_Is_Local_Anonymous_Access (Anon_Access);
10765 end if;
10767 Next (Comp);
10768 end loop;
10770 if Present (Variant_Part (Comp_List)) then
10771 declare
10772 V : Node_Id;
10773 begin
10774 V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
10775 while Present (V) loop
10776 Check_Anonymous_Access_Components
10777 (Typ_Decl, Typ, Prev, Component_List (V));
10778 Next_Non_Pragma (V);
10779 end loop;
10780 end;
10781 end if;
10782 end Check_Anonymous_Access_Components;
10784 ----------------------
10785 -- Check_Completion --
10786 ----------------------
10788 procedure Check_Completion (Body_Id : Node_Id := Empty) is
10789 E : Entity_Id;
10791 procedure Post_Error;
10792 -- Post error message for lack of completion for entity E
10794 ----------------
10795 -- Post_Error --
10796 ----------------
10798 procedure Post_Error is
10800 procedure Missing_Body;
10801 -- Output missing body message
10803 ------------------
10804 -- Missing_Body --
10805 ------------------
10807 procedure Missing_Body is
10808 begin
10809 -- Spec is in same unit, so we can post on spec
10811 if In_Same_Source_Unit (Body_Id, E) then
10812 Error_Msg_N ("missing body for &", E);
10814 -- Spec is in a separate unit, so we have to post on the body
10816 else
10817 Error_Msg_NE ("missing body for & declared#!", Body_Id, E);
10818 end if;
10819 end Missing_Body;
10821 -- Start of processing for Post_Error
10823 begin
10824 if not Comes_From_Source (E) then
10826 if Ekind_In (E, E_Task_Type, E_Protected_Type) then
10828 -- It may be an anonymous protected type created for a
10829 -- single variable. Post error on variable, if present.
10831 declare
10832 Var : Entity_Id;
10834 begin
10835 Var := First_Entity (Current_Scope);
10836 while Present (Var) loop
10837 exit when Etype (Var) = E
10838 and then Comes_From_Source (Var);
10840 Next_Entity (Var);
10841 end loop;
10843 if Present (Var) then
10844 E := Var;
10845 end if;
10846 end;
10847 end if;
10848 end if;
10850 -- If a generated entity has no completion, then either previous
10851 -- semantic errors have disabled the expansion phase, or else we had
10852 -- missing subunits, or else we are compiling without expansion,
10853 -- or else something is very wrong.
10855 if not Comes_From_Source (E) then
10856 pragma Assert
10857 (Serious_Errors_Detected > 0
10858 or else Configurable_Run_Time_Violations > 0
10859 or else Subunits_Missing
10860 or else not Expander_Active);
10861 return;
10863 -- Here for source entity
10865 else
10866 -- Here if no body to post the error message, so we post the error
10867 -- on the declaration that has no completion. This is not really
10868 -- the right place to post it, think about this later ???
10870 if No (Body_Id) then
10871 if Is_Type (E) then
10872 Error_Msg_NE
10873 ("missing full declaration for }", Parent (E), E);
10874 else
10875 Error_Msg_NE ("missing body for &", Parent (E), E);
10876 end if;
10878 -- Package body has no completion for a declaration that appears
10879 -- in the corresponding spec. Post error on the body, with a
10880 -- reference to the non-completed declaration.
10882 else
10883 Error_Msg_Sloc := Sloc (E);
10885 if Is_Type (E) then
10886 Error_Msg_NE ("missing full declaration for }!", Body_Id, E);
10888 elsif Is_Overloadable (E)
10889 and then Current_Entity_In_Scope (E) /= E
10890 then
10891 -- It may be that the completion is mistyped and appears as
10892 -- a distinct overloading of the entity.
10894 declare
10895 Candidate : constant Entity_Id :=
10896 Current_Entity_In_Scope (E);
10897 Decl : constant Node_Id :=
10898 Unit_Declaration_Node (Candidate);
10900 begin
10901 if Is_Overloadable (Candidate)
10902 and then Ekind (Candidate) = Ekind (E)
10903 and then Nkind (Decl) = N_Subprogram_Body
10904 and then Acts_As_Spec (Decl)
10905 then
10906 Check_Type_Conformant (Candidate, E);
10908 else
10909 Missing_Body;
10910 end if;
10911 end;
10913 else
10914 Missing_Body;
10915 end if;
10916 end if;
10917 end if;
10918 end Post_Error;
10920 -- Local variables
10922 Pack_Id : constant Entity_Id := Current_Scope;
10924 -- Start of processing for Check_Completion
10926 begin
10927 E := First_Entity (Pack_Id);
10928 while Present (E) loop
10929 if Is_Intrinsic_Subprogram (E) then
10930 null;
10932 -- A Ghost entity declared in a non-Ghost package does not force the
10933 -- need for a body (SPARK RM 6.9(11)).
10935 elsif not Is_Ghost_Entity (Pack_Id) and then Is_Ghost_Entity (E) then
10936 null;
10938 -- The following situation requires special handling: a child unit
10939 -- that appears in the context clause of the body of its parent:
10941 -- procedure Parent.Child (...);
10943 -- with Parent.Child;
10944 -- package body Parent is
10946 -- Here Parent.Child appears as a local entity, but should not be
10947 -- flagged as requiring completion, because it is a compilation
10948 -- unit.
10950 -- Ignore missing completion for a subprogram that does not come from
10951 -- source (including the _Call primitive operation of RAS types,
10952 -- which has to have the flag Comes_From_Source for other purposes):
10953 -- we assume that the expander will provide the missing completion.
10954 -- In case of previous errors, other expansion actions that provide
10955 -- bodies for null procedures with not be invoked, so inhibit message
10956 -- in those cases.
10958 -- Note that E_Operator is not in the list that follows, because
10959 -- this kind is reserved for predefined operators, that are
10960 -- intrinsic and do not need completion.
10962 elsif Ekind_In (E, E_Function,
10963 E_Procedure,
10964 E_Generic_Function,
10965 E_Generic_Procedure)
10966 then
10967 if Has_Completion (E) then
10968 null;
10970 elsif Is_Subprogram (E) and then Is_Abstract_Subprogram (E) then
10971 null;
10973 elsif Is_Subprogram (E)
10974 and then (not Comes_From_Source (E)
10975 or else Chars (E) = Name_uCall)
10976 then
10977 null;
10979 elsif
10980 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
10981 then
10982 null;
10984 elsif Nkind (Parent (E)) = N_Procedure_Specification
10985 and then Null_Present (Parent (E))
10986 and then Serious_Errors_Detected > 0
10987 then
10988 null;
10990 else
10991 Post_Error;
10992 end if;
10994 elsif Is_Entry (E) then
10995 if not Has_Completion (E) and then
10996 (Ekind (Scope (E)) = E_Protected_Object
10997 or else Ekind (Scope (E)) = E_Protected_Type)
10998 then
10999 Post_Error;
11000 end if;
11002 elsif Is_Package_Or_Generic_Package (E) then
11003 if Unit_Requires_Body (E) then
11004 if not Has_Completion (E)
11005 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
11006 N_Compilation_Unit
11007 then
11008 Post_Error;
11009 end if;
11011 elsif not Is_Child_Unit (E) then
11012 May_Need_Implicit_Body (E);
11013 end if;
11015 -- A formal incomplete type (Ada 2012) does not require a completion;
11016 -- other incomplete type declarations do.
11018 elsif Ekind (E) = E_Incomplete_Type
11019 and then No (Underlying_Type (E))
11020 and then not Is_Generic_Type (E)
11021 then
11022 Post_Error;
11024 elsif Ekind_In (E, E_Task_Type, E_Protected_Type)
11025 and then not Has_Completion (E)
11026 then
11027 Post_Error;
11029 -- A single task declared in the current scope is a constant, verify
11030 -- that the body of its anonymous type is in the same scope. If the
11031 -- task is defined elsewhere, this may be a renaming declaration for
11032 -- which no completion is needed.
11034 elsif Ekind (E) = E_Constant
11035 and then Ekind (Etype (E)) = E_Task_Type
11036 and then not Has_Completion (Etype (E))
11037 and then Scope (Etype (E)) = Current_Scope
11038 then
11039 Post_Error;
11041 elsif Ekind (E) = E_Protected_Object
11042 and then not Has_Completion (Etype (E))
11043 then
11044 Post_Error;
11046 elsif Ekind (E) = E_Record_Type then
11047 if Is_Tagged_Type (E) then
11048 Check_Abstract_Overriding (E);
11049 Check_Conventions (E);
11050 end if;
11052 Check_Aliased_Component_Types (E);
11054 elsif Ekind (E) = E_Array_Type then
11055 Check_Aliased_Component_Types (E);
11057 end if;
11059 Next_Entity (E);
11060 end loop;
11061 end Check_Completion;
11063 ------------------------------------
11064 -- Check_CPP_Type_Has_No_Defaults --
11065 ------------------------------------
11067 procedure Check_CPP_Type_Has_No_Defaults (T : Entity_Id) is
11068 Tdef : constant Node_Id := Type_Definition (Declaration_Node (T));
11069 Clist : Node_Id;
11070 Comp : Node_Id;
11072 begin
11073 -- Obtain the component list
11075 if Nkind (Tdef) = N_Record_Definition then
11076 Clist := Component_List (Tdef);
11077 else pragma Assert (Nkind (Tdef) = N_Derived_Type_Definition);
11078 Clist := Component_List (Record_Extension_Part (Tdef));
11079 end if;
11081 -- Check all components to ensure no default expressions
11083 if Present (Clist) then
11084 Comp := First (Component_Items (Clist));
11085 while Present (Comp) loop
11086 if Present (Expression (Comp)) then
11087 Error_Msg_N
11088 ("component of imported 'C'P'P type cannot have "
11089 & "default expression", Expression (Comp));
11090 end if;
11092 Next (Comp);
11093 end loop;
11094 end if;
11095 end Check_CPP_Type_Has_No_Defaults;
11097 ----------------------------
11098 -- Check_Delta_Expression --
11099 ----------------------------
11101 procedure Check_Delta_Expression (E : Node_Id) is
11102 begin
11103 if not (Is_Real_Type (Etype (E))) then
11104 Wrong_Type (E, Any_Real);
11106 elsif not Is_OK_Static_Expression (E) then
11107 Flag_Non_Static_Expr
11108 ("non-static expression used for delta value!", E);
11110 elsif not UR_Is_Positive (Expr_Value_R (E)) then
11111 Error_Msg_N ("delta expression must be positive", E);
11113 else
11114 return;
11115 end if;
11117 -- If any of above errors occurred, then replace the incorrect
11118 -- expression by the real 0.1, which should prevent further errors.
11120 Rewrite (E,
11121 Make_Real_Literal (Sloc (E), Ureal_Tenth));
11122 Analyze_And_Resolve (E, Standard_Float);
11123 end Check_Delta_Expression;
11125 -----------------------------
11126 -- Check_Digits_Expression --
11127 -----------------------------
11129 procedure Check_Digits_Expression (E : Node_Id) is
11130 begin
11131 if not (Is_Integer_Type (Etype (E))) then
11132 Wrong_Type (E, Any_Integer);
11134 elsif not Is_OK_Static_Expression (E) then
11135 Flag_Non_Static_Expr
11136 ("non-static expression used for digits value!", E);
11138 elsif Expr_Value (E) <= 0 then
11139 Error_Msg_N ("digits value must be greater than zero", E);
11141 else
11142 return;
11143 end if;
11145 -- If any of above errors occurred, then replace the incorrect
11146 -- expression by the integer 1, which should prevent further errors.
11148 Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
11149 Analyze_And_Resolve (E, Standard_Integer);
11151 end Check_Digits_Expression;
11153 --------------------------
11154 -- Check_Initialization --
11155 --------------------------
11157 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
11158 begin
11159 -- Special processing for limited types
11161 if Is_Limited_Type (T)
11162 and then not In_Instance
11163 and then not In_Inlined_Body
11164 then
11165 if not OK_For_Limited_Init (T, Exp) then
11167 -- In GNAT mode, this is just a warning, to allow it to be evilly
11168 -- turned off. Otherwise it is a real error.
11170 if GNAT_Mode then
11171 Error_Msg_N
11172 ("??cannot initialize entities of limited type!", Exp);
11174 elsif Ada_Version < Ada_2005 then
11176 -- The side effect removal machinery may generate illegal Ada
11177 -- code to avoid the usage of access types and 'reference in
11178 -- SPARK mode. Since this is legal code with respect to theorem
11179 -- proving, do not emit the error.
11181 if GNATprove_Mode
11182 and then Nkind (Exp) = N_Function_Call
11183 and then Nkind (Parent (Exp)) = N_Object_Declaration
11184 and then not Comes_From_Source
11185 (Defining_Identifier (Parent (Exp)))
11186 then
11187 null;
11189 else
11190 Error_Msg_N
11191 ("cannot initialize entities of limited type", Exp);
11192 Explain_Limited_Type (T, Exp);
11193 end if;
11195 else
11196 -- Specialize error message according to kind of illegal
11197 -- initial expression.
11199 if Nkind (Exp) = N_Type_Conversion
11200 and then Nkind (Expression (Exp)) = N_Function_Call
11201 then
11202 Error_Msg_N
11203 ("illegal context for call"
11204 & " to function with limited result", Exp);
11206 else
11207 Error_Msg_N
11208 ("initialization of limited object requires aggregate "
11209 & "or function call", Exp);
11210 end if;
11211 end if;
11212 end if;
11213 end if;
11215 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag gets
11216 -- set unless we can be sure that no range check is required.
11218 if (GNATprove_Mode or not Expander_Active)
11219 and then Is_Scalar_Type (T)
11220 and then not Is_In_Range (Exp, T, Assume_Valid => True)
11221 then
11222 Set_Do_Range_Check (Exp);
11223 end if;
11224 end Check_Initialization;
11226 ----------------------
11227 -- Check_Interfaces --
11228 ----------------------
11230 procedure Check_Interfaces (N : Node_Id; Def : Node_Id) is
11231 Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
11233 Iface : Node_Id;
11234 Iface_Def : Node_Id;
11235 Iface_Typ : Entity_Id;
11236 Parent_Node : Node_Id;
11238 Is_Task : Boolean := False;
11239 -- Set True if parent type or any progenitor is a task interface
11241 Is_Protected : Boolean := False;
11242 -- Set True if parent type or any progenitor is a protected interface
11244 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
11245 -- Check that a progenitor is compatible with declaration. If an error
11246 -- message is output, it is posted on Error_Node.
11248 ------------------
11249 -- Check_Ifaces --
11250 ------------------
11252 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
11253 Iface_Id : constant Entity_Id :=
11254 Defining_Identifier (Parent (Iface_Def));
11255 Type_Def : Node_Id;
11257 begin
11258 if Nkind (N) = N_Private_Extension_Declaration then
11259 Type_Def := N;
11260 else
11261 Type_Def := Type_Definition (N);
11262 end if;
11264 if Is_Task_Interface (Iface_Id) then
11265 Is_Task := True;
11267 elsif Is_Protected_Interface (Iface_Id) then
11268 Is_Protected := True;
11269 end if;
11271 if Is_Synchronized_Interface (Iface_Id) then
11273 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
11274 -- extension derived from a synchronized interface must explicitly
11275 -- be declared synchronized, because the full view will be a
11276 -- synchronized type.
11278 if Nkind (N) = N_Private_Extension_Declaration then
11279 if not Synchronized_Present (N) then
11280 Error_Msg_NE
11281 ("private extension of& must be explicitly synchronized",
11282 N, Iface_Id);
11283 end if;
11285 -- However, by 3.9.4(16/2), a full type that is a record extension
11286 -- is never allowed to derive from a synchronized interface (note
11287 -- that interfaces must be excluded from this check, because those
11288 -- are represented by derived type definitions in some cases).
11290 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition
11291 and then not Interface_Present (Type_Definition (N))
11292 then
11293 Error_Msg_N ("record extension cannot derive from synchronized "
11294 & "interface", Error_Node);
11295 end if;
11296 end if;
11298 -- Check that the characteristics of the progenitor are compatible
11299 -- with the explicit qualifier in the declaration.
11300 -- The check only applies to qualifiers that come from source.
11301 -- Limited_Present also appears in the declaration of corresponding
11302 -- records, and the check does not apply to them.
11304 if Limited_Present (Type_Def)
11305 and then not
11306 Is_Concurrent_Record_Type (Defining_Identifier (N))
11307 then
11308 if Is_Limited_Interface (Parent_Type)
11309 and then not Is_Limited_Interface (Iface_Id)
11310 then
11311 Error_Msg_NE
11312 ("progenitor & must be limited interface",
11313 Error_Node, Iface_Id);
11315 elsif
11316 (Task_Present (Iface_Def)
11317 or else Protected_Present (Iface_Def)
11318 or else Synchronized_Present (Iface_Def))
11319 and then Nkind (N) /= N_Private_Extension_Declaration
11320 and then not Error_Posted (N)
11321 then
11322 Error_Msg_NE
11323 ("progenitor & must be limited interface",
11324 Error_Node, Iface_Id);
11325 end if;
11327 -- Protected interfaces can only inherit from limited, synchronized
11328 -- or protected interfaces.
11330 elsif Nkind (N) = N_Full_Type_Declaration
11331 and then Protected_Present (Type_Def)
11332 then
11333 if Limited_Present (Iface_Def)
11334 or else Synchronized_Present (Iface_Def)
11335 or else Protected_Present (Iface_Def)
11336 then
11337 null;
11339 elsif Task_Present (Iface_Def) then
11340 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
11341 & "from task interface", Error_Node);
11343 else
11344 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
11345 & "from non-limited interface", Error_Node);
11346 end if;
11348 -- Ada 2005 (AI-345): Synchronized interfaces can only inherit from
11349 -- limited and synchronized.
11351 elsif Synchronized_Present (Type_Def) then
11352 if Limited_Present (Iface_Def)
11353 or else Synchronized_Present (Iface_Def)
11354 then
11355 null;
11357 elsif Protected_Present (Iface_Def)
11358 and then Nkind (N) /= N_Private_Extension_Declaration
11359 then
11360 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11361 & "from protected interface", Error_Node);
11363 elsif Task_Present (Iface_Def)
11364 and then Nkind (N) /= N_Private_Extension_Declaration
11365 then
11366 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11367 & "from task interface", Error_Node);
11369 elsif not Is_Limited_Interface (Iface_Id) then
11370 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11371 & "from non-limited interface", Error_Node);
11372 end if;
11374 -- Ada 2005 (AI-345): Task interfaces can only inherit from limited,
11375 -- synchronized or task interfaces.
11377 elsif Nkind (N) = N_Full_Type_Declaration
11378 and then Task_Present (Type_Def)
11379 then
11380 if Limited_Present (Iface_Def)
11381 or else Synchronized_Present (Iface_Def)
11382 or else Task_Present (Iface_Def)
11383 then
11384 null;
11386 elsif Protected_Present (Iface_Def) then
11387 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
11388 & "protected interface", Error_Node);
11390 else
11391 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
11392 & "non-limited interface", Error_Node);
11393 end if;
11394 end if;
11395 end Check_Ifaces;
11397 -- Start of processing for Check_Interfaces
11399 begin
11400 if Is_Interface (Parent_Type) then
11401 if Is_Task_Interface (Parent_Type) then
11402 Is_Task := True;
11404 elsif Is_Protected_Interface (Parent_Type) then
11405 Is_Protected := True;
11406 end if;
11407 end if;
11409 if Nkind (N) = N_Private_Extension_Declaration then
11411 -- Check that progenitors are compatible with declaration
11413 Iface := First (Interface_List (Def));
11414 while Present (Iface) loop
11415 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
11417 Parent_Node := Parent (Base_Type (Iface_Typ));
11418 Iface_Def := Type_Definition (Parent_Node);
11420 if not Is_Interface (Iface_Typ) then
11421 Diagnose_Interface (Iface, Iface_Typ);
11422 else
11423 Check_Ifaces (Iface_Def, Iface);
11424 end if;
11426 Next (Iface);
11427 end loop;
11429 if Is_Task and Is_Protected then
11430 Error_Msg_N
11431 ("type cannot derive from task and protected interface", N);
11432 end if;
11434 return;
11435 end if;
11437 -- Full type declaration of derived type.
11438 -- Check compatibility with parent if it is interface type
11440 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
11441 and then Is_Interface (Parent_Type)
11442 then
11443 Parent_Node := Parent (Parent_Type);
11445 -- More detailed checks for interface varieties
11447 Check_Ifaces
11448 (Iface_Def => Type_Definition (Parent_Node),
11449 Error_Node => Subtype_Indication (Type_Definition (N)));
11450 end if;
11452 Iface := First (Interface_List (Def));
11453 while Present (Iface) loop
11454 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
11456 Parent_Node := Parent (Base_Type (Iface_Typ));
11457 Iface_Def := Type_Definition (Parent_Node);
11459 if not Is_Interface (Iface_Typ) then
11460 Diagnose_Interface (Iface, Iface_Typ);
11462 else
11463 -- "The declaration of a specific descendant of an interface
11464 -- type freezes the interface type" RM 13.14
11466 Freeze_Before (N, Iface_Typ);
11467 Check_Ifaces (Iface_Def, Error_Node => Iface);
11468 end if;
11470 Next (Iface);
11471 end loop;
11473 if Is_Task and Is_Protected then
11474 Error_Msg_N
11475 ("type cannot derive from task and protected interface", N);
11476 end if;
11477 end Check_Interfaces;
11479 ------------------------------------
11480 -- Check_Or_Process_Discriminants --
11481 ------------------------------------
11483 -- If an incomplete or private type declaration was already given for the
11484 -- type, the discriminants may have already been processed if they were
11485 -- present on the incomplete declaration. In this case a full conformance
11486 -- check has been performed in Find_Type_Name, and we then recheck here
11487 -- some properties that can't be checked on the partial view alone.
11488 -- Otherwise we call Process_Discriminants.
11490 procedure Check_Or_Process_Discriminants
11491 (N : Node_Id;
11492 T : Entity_Id;
11493 Prev : Entity_Id := Empty)
11495 begin
11496 if Has_Discriminants (T) then
11498 -- Discriminants are already set on T if they were already present
11499 -- on the partial view. Make them visible to component declarations.
11501 declare
11502 D : Entity_Id;
11503 -- Discriminant on T (full view) referencing expr on partial view
11505 Prev_D : Entity_Id;
11506 -- Entity of corresponding discriminant on partial view
11508 New_D : Node_Id;
11509 -- Discriminant specification for full view, expression is
11510 -- the syntactic copy on full view (which has been checked for
11511 -- conformance with partial view), only used here to post error
11512 -- message.
11514 begin
11515 D := First_Discriminant (T);
11516 New_D := First (Discriminant_Specifications (N));
11517 while Present (D) loop
11518 Prev_D := Current_Entity (D);
11519 Set_Current_Entity (D);
11520 Set_Is_Immediately_Visible (D);
11521 Set_Homonym (D, Prev_D);
11523 -- Handle the case where there is an untagged partial view and
11524 -- the full view is tagged: must disallow discriminants with
11525 -- defaults, unless compiling for Ada 2012, which allows a
11526 -- limited tagged type to have defaulted discriminants (see
11527 -- AI05-0214). However, suppress error here if it was already
11528 -- reported on the default expression of the partial view.
11530 if Is_Tagged_Type (T)
11531 and then Present (Expression (Parent (D)))
11532 and then (not Is_Limited_Type (Current_Scope)
11533 or else Ada_Version < Ada_2012)
11534 and then not Error_Posted (Expression (Parent (D)))
11535 then
11536 if Ada_Version >= Ada_2012 then
11537 Error_Msg_N
11538 ("discriminants of nonlimited tagged type cannot have "
11539 & "defaults",
11540 Expression (New_D));
11541 else
11542 Error_Msg_N
11543 ("discriminants of tagged type cannot have defaults",
11544 Expression (New_D));
11545 end if;
11546 end if;
11548 -- Ada 2005 (AI-230): Access discriminant allowed in
11549 -- non-limited record types.
11551 if Ada_Version < Ada_2005 then
11553 -- This restriction gets applied to the full type here. It
11554 -- has already been applied earlier to the partial view.
11556 Check_Access_Discriminant_Requires_Limited (Parent (D), N);
11557 end if;
11559 Next_Discriminant (D);
11560 Next (New_D);
11561 end loop;
11562 end;
11564 elsif Present (Discriminant_Specifications (N)) then
11565 Process_Discriminants (N, Prev);
11566 end if;
11567 end Check_Or_Process_Discriminants;
11569 ----------------------
11570 -- Check_Real_Bound --
11571 ----------------------
11573 procedure Check_Real_Bound (Bound : Node_Id) is
11574 begin
11575 if not Is_Real_Type (Etype (Bound)) then
11576 Error_Msg_N
11577 ("bound in real type definition must be of real type", Bound);
11579 elsif not Is_OK_Static_Expression (Bound) then
11580 Flag_Non_Static_Expr
11581 ("non-static expression used for real type bound!", Bound);
11583 else
11584 return;
11585 end if;
11587 Rewrite
11588 (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
11589 Analyze (Bound);
11590 Resolve (Bound, Standard_Float);
11591 end Check_Real_Bound;
11593 ------------------------------
11594 -- Complete_Private_Subtype --
11595 ------------------------------
11597 procedure Complete_Private_Subtype
11598 (Priv : Entity_Id;
11599 Full : Entity_Id;
11600 Full_Base : Entity_Id;
11601 Related_Nod : Node_Id)
11603 Save_Next_Entity : Entity_Id;
11604 Save_Homonym : Entity_Id;
11606 begin
11607 -- Set semantic attributes for (implicit) private subtype completion.
11608 -- If the full type has no discriminants, then it is a copy of the
11609 -- full view of the base. Otherwise, it is a subtype of the base with
11610 -- a possible discriminant constraint. Save and restore the original
11611 -- Next_Entity field of full to ensure that the calls to Copy_Node do
11612 -- not corrupt the entity chain.
11614 -- Note that the type of the full view is the same entity as the type
11615 -- of the partial view. In this fashion, the subtype has access to the
11616 -- correct view of the parent.
11618 Save_Next_Entity := Next_Entity (Full);
11619 Save_Homonym := Homonym (Priv);
11621 case Ekind (Full_Base) is
11622 when E_Record_Type |
11623 E_Record_Subtype |
11624 Class_Wide_Kind |
11625 Private_Kind |
11626 Task_Kind |
11627 Protected_Kind =>
11628 Copy_Node (Priv, Full);
11630 Set_Has_Discriminants
11631 (Full, Has_Discriminants (Full_Base));
11632 Set_Has_Unknown_Discriminants
11633 (Full, Has_Unknown_Discriminants (Full_Base));
11634 Set_First_Entity (Full, First_Entity (Full_Base));
11635 Set_Last_Entity (Full, Last_Entity (Full_Base));
11637 -- If the underlying base type is constrained, we know that the
11638 -- full view of the subtype is constrained as well (the converse
11639 -- is not necessarily true).
11641 if Is_Constrained (Full_Base) then
11642 Set_Is_Constrained (Full);
11643 end if;
11645 when others =>
11646 Copy_Node (Full_Base, Full);
11648 Set_Chars (Full, Chars (Priv));
11649 Conditional_Delay (Full, Priv);
11650 Set_Sloc (Full, Sloc (Priv));
11651 end case;
11653 Set_Next_Entity (Full, Save_Next_Entity);
11654 Set_Homonym (Full, Save_Homonym);
11655 Set_Associated_Node_For_Itype (Full, Related_Nod);
11657 -- Set common attributes for all subtypes: kind, convention, etc.
11659 Set_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
11660 Set_Convention (Full, Convention (Full_Base));
11662 -- The Etype of the full view is inconsistent. Gigi needs to see the
11663 -- structural full view, which is what the current scheme gives: the
11664 -- Etype of the full view is the etype of the full base. However, if the
11665 -- full base is a derived type, the full view then looks like a subtype
11666 -- of the parent, not a subtype of the full base. If instead we write:
11668 -- Set_Etype (Full, Full_Base);
11670 -- then we get inconsistencies in the front-end (confusion between
11671 -- views). Several outstanding bugs are related to this ???
11673 Set_Is_First_Subtype (Full, False);
11674 Set_Scope (Full, Scope (Priv));
11675 Set_Size_Info (Full, Full_Base);
11676 Set_RM_Size (Full, RM_Size (Full_Base));
11677 Set_Is_Itype (Full);
11679 -- A subtype of a private-type-without-discriminants, whose full-view
11680 -- has discriminants with default expressions, is not constrained.
11682 if not Has_Discriminants (Priv) then
11683 Set_Is_Constrained (Full, Is_Constrained (Full_Base));
11685 if Has_Discriminants (Full_Base) then
11686 Set_Discriminant_Constraint
11687 (Full, Discriminant_Constraint (Full_Base));
11689 -- The partial view may have been indefinite, the full view
11690 -- might not be.
11692 Set_Has_Unknown_Discriminants
11693 (Full, Has_Unknown_Discriminants (Full_Base));
11694 end if;
11695 end if;
11697 Set_First_Rep_Item (Full, First_Rep_Item (Full_Base));
11698 Set_Depends_On_Private (Full, Has_Private_Component (Full));
11700 -- Freeze the private subtype entity if its parent is delayed, and not
11701 -- already frozen. We skip this processing if the type is an anonymous
11702 -- subtype of a record component, or is the corresponding record of a
11703 -- protected type, since these are processed when the enclosing type
11704 -- is frozen.
11706 if not Is_Type (Scope (Full)) then
11707 Set_Has_Delayed_Freeze (Full,
11708 Has_Delayed_Freeze (Full_Base)
11709 and then (not Is_Frozen (Full_Base)));
11710 end if;
11712 Set_Freeze_Node (Full, Empty);
11713 Set_Is_Frozen (Full, False);
11714 Set_Full_View (Priv, Full);
11716 if Has_Discriminants (Full) then
11717 Set_Stored_Constraint_From_Discriminant_Constraint (Full);
11718 Set_Stored_Constraint (Priv, Stored_Constraint (Full));
11720 if Has_Unknown_Discriminants (Full) then
11721 Set_Discriminant_Constraint (Full, No_Elist);
11722 end if;
11723 end if;
11725 if Ekind (Full_Base) = E_Record_Type
11726 and then Has_Discriminants (Full_Base)
11727 and then Has_Discriminants (Priv) -- might not, if errors
11728 and then not Has_Unknown_Discriminants (Priv)
11729 and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
11730 then
11731 Create_Constrained_Components
11732 (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
11734 -- If the full base is itself derived from private, build a congruent
11735 -- subtype of its underlying type, for use by the back end. For a
11736 -- constrained record component, the declaration cannot be placed on
11737 -- the component list, but it must nevertheless be built an analyzed, to
11738 -- supply enough information for Gigi to compute the size of component.
11740 elsif Ekind (Full_Base) in Private_Kind
11741 and then Is_Derived_Type (Full_Base)
11742 and then Has_Discriminants (Full_Base)
11743 and then (Ekind (Current_Scope) /= E_Record_Subtype)
11744 then
11745 if not Is_Itype (Priv)
11746 and then
11747 Nkind (Subtype_Indication (Parent (Priv))) = N_Subtype_Indication
11748 then
11749 Build_Underlying_Full_View
11750 (Parent (Priv), Full, Etype (Full_Base));
11752 elsif Nkind (Related_Nod) = N_Component_Declaration then
11753 Build_Underlying_Full_View (Related_Nod, Full, Etype (Full_Base));
11754 end if;
11756 elsif Is_Record_Type (Full_Base) then
11758 -- Show Full is simply a renaming of Full_Base
11760 Set_Cloned_Subtype (Full, Full_Base);
11761 end if;
11763 -- It is unsafe to share the bounds of a scalar type, because the Itype
11764 -- is elaborated on demand, and if a bound is non-static then different
11765 -- orders of elaboration in different units will lead to different
11766 -- external symbols.
11768 if Is_Scalar_Type (Full_Base) then
11769 Set_Scalar_Range (Full,
11770 Make_Range (Sloc (Related_Nod),
11771 Low_Bound =>
11772 Duplicate_Subexpr_No_Checks (Type_Low_Bound (Full_Base)),
11773 High_Bound =>
11774 Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
11776 -- This completion inherits the bounds of the full parent, but if
11777 -- the parent is an unconstrained floating point type, so is the
11778 -- completion.
11780 if Is_Floating_Point_Type (Full_Base) then
11781 Set_Includes_Infinities
11782 (Scalar_Range (Full), Has_Infinities (Full_Base));
11783 end if;
11784 end if;
11786 -- ??? It seems that a lot of fields are missing that should be copied
11787 -- from Full_Base to Full. Here are some that are introduced in a
11788 -- non-disruptive way but a cleanup is necessary.
11790 if Is_Tagged_Type (Full_Base) then
11791 Set_Is_Tagged_Type (Full);
11792 Set_Direct_Primitive_Operations
11793 (Full, Direct_Primitive_Operations (Full_Base));
11794 Set_No_Tagged_Streams_Pragma
11795 (Full, No_Tagged_Streams_Pragma (Full_Base));
11797 -- Inherit class_wide type of full_base in case the partial view was
11798 -- not tagged. Otherwise it has already been created when the private
11799 -- subtype was analyzed.
11801 if No (Class_Wide_Type (Full)) then
11802 Set_Class_Wide_Type (Full, Class_Wide_Type (Full_Base));
11803 end if;
11805 -- If this is a subtype of a protected or task type, constrain its
11806 -- corresponding record, unless this is a subtype without constraints,
11807 -- i.e. a simple renaming as with an actual subtype in an instance.
11809 elsif Is_Concurrent_Type (Full_Base) then
11810 if Has_Discriminants (Full)
11811 and then Present (Corresponding_Record_Type (Full_Base))
11812 and then
11813 not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
11814 then
11815 Set_Corresponding_Record_Type (Full,
11816 Constrain_Corresponding_Record
11817 (Full, Corresponding_Record_Type (Full_Base), Related_Nod));
11819 else
11820 Set_Corresponding_Record_Type (Full,
11821 Corresponding_Record_Type (Full_Base));
11822 end if;
11823 end if;
11825 -- Link rep item chain, and also setting of Has_Predicates from private
11826 -- subtype to full subtype, since we will need these on the full subtype
11827 -- to create the predicate function. Note that the full subtype may
11828 -- already have rep items, inherited from the full view of the base
11829 -- type, so we must be sure not to overwrite these entries.
11831 declare
11832 Append : Boolean;
11833 Item : Node_Id;
11834 Next_Item : Node_Id;
11836 begin
11837 Item := First_Rep_Item (Full);
11839 -- If no existing rep items on full type, we can just link directly
11840 -- to the list of items on the private type, if any exist.. Same if
11841 -- the rep items are only those inherited from the base
11843 if (No (Item)
11844 or else Nkind (Item) /= N_Aspect_Specification
11845 or else Entity (Item) = Full_Base)
11846 and then Present (First_Rep_Item (Priv))
11847 then
11848 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
11850 -- Otherwise, search to the end of items currently linked to the full
11851 -- subtype and append the private items to the end. However, if Priv
11852 -- and Full already have the same list of rep items, then the append
11853 -- is not done, as that would create a circularity.
11855 elsif Item /= First_Rep_Item (Priv) then
11856 Append := True;
11857 loop
11858 Next_Item := Next_Rep_Item (Item);
11859 exit when No (Next_Item);
11860 Item := Next_Item;
11862 -- If the private view has aspect specifications, the full view
11863 -- inherits them. Since these aspects may already have been
11864 -- attached to the full view during derivation, do not append
11865 -- them if already present.
11867 if Item = First_Rep_Item (Priv) then
11868 Append := False;
11869 exit;
11870 end if;
11871 end loop;
11873 -- And link the private type items at the end of the chain
11875 if Append then
11876 Set_Next_Rep_Item (Item, First_Rep_Item (Priv));
11877 end if;
11878 end if;
11879 end;
11881 -- Make sure Has_Predicates is set on full type if it is set on the
11882 -- private type. Note that it may already be set on the full type and
11883 -- if so, we don't want to unset it. Similarly, propagate information
11884 -- about delayed aspects, because the corresponding pragmas must be
11885 -- analyzed when one of the views is frozen. This last step is needed
11886 -- in particular when the full type is a scalar type for which an
11887 -- anonymous base type is constructed.
11889 if Has_Predicates (Priv) then
11890 Set_Has_Predicates (Full);
11891 end if;
11893 if Has_Delayed_Aspects (Priv) then
11894 Set_Has_Delayed_Aspects (Full);
11895 end if;
11896 end Complete_Private_Subtype;
11898 ----------------------------
11899 -- Constant_Redeclaration --
11900 ----------------------------
11902 procedure Constant_Redeclaration
11903 (Id : Entity_Id;
11904 N : Node_Id;
11905 T : out Entity_Id)
11907 Prev : constant Entity_Id := Current_Entity_In_Scope (Id);
11908 Obj_Def : constant Node_Id := Object_Definition (N);
11909 New_T : Entity_Id;
11911 procedure Check_Possible_Deferred_Completion
11912 (Prev_Id : Entity_Id;
11913 Prev_Obj_Def : Node_Id;
11914 Curr_Obj_Def : Node_Id);
11915 -- Determine whether the two object definitions describe the partial
11916 -- and the full view of a constrained deferred constant. Generate
11917 -- a subtype for the full view and verify that it statically matches
11918 -- the subtype of the partial view.
11920 procedure Check_Recursive_Declaration (Typ : Entity_Id);
11921 -- If deferred constant is an access type initialized with an allocator,
11922 -- check whether there is an illegal recursion in the definition,
11923 -- through a default value of some record subcomponent. This is normally
11924 -- detected when generating init procs, but requires this additional
11925 -- mechanism when expansion is disabled.
11927 ----------------------------------------
11928 -- Check_Possible_Deferred_Completion --
11929 ----------------------------------------
11931 procedure Check_Possible_Deferred_Completion
11932 (Prev_Id : Entity_Id;
11933 Prev_Obj_Def : Node_Id;
11934 Curr_Obj_Def : Node_Id)
11936 begin
11937 if Nkind (Prev_Obj_Def) = N_Subtype_Indication
11938 and then Present (Constraint (Prev_Obj_Def))
11939 and then Nkind (Curr_Obj_Def) = N_Subtype_Indication
11940 and then Present (Constraint (Curr_Obj_Def))
11941 then
11942 declare
11943 Loc : constant Source_Ptr := Sloc (N);
11944 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
11945 Decl : constant Node_Id :=
11946 Make_Subtype_Declaration (Loc,
11947 Defining_Identifier => Def_Id,
11948 Subtype_Indication =>
11949 Relocate_Node (Curr_Obj_Def));
11951 begin
11952 Insert_Before_And_Analyze (N, Decl);
11953 Set_Etype (Id, Def_Id);
11955 if not Subtypes_Statically_Match (Etype (Prev_Id), Def_Id) then
11956 Error_Msg_Sloc := Sloc (Prev_Id);
11957 Error_Msg_N ("subtype does not statically match deferred "
11958 & "declaration #", N);
11959 end if;
11960 end;
11961 end if;
11962 end Check_Possible_Deferred_Completion;
11964 ---------------------------------
11965 -- Check_Recursive_Declaration --
11966 ---------------------------------
11968 procedure Check_Recursive_Declaration (Typ : Entity_Id) is
11969 Comp : Entity_Id;
11971 begin
11972 if Is_Record_Type (Typ) then
11973 Comp := First_Component (Typ);
11974 while Present (Comp) loop
11975 if Comes_From_Source (Comp) then
11976 if Present (Expression (Parent (Comp)))
11977 and then Is_Entity_Name (Expression (Parent (Comp)))
11978 and then Entity (Expression (Parent (Comp))) = Prev
11979 then
11980 Error_Msg_Sloc := Sloc (Parent (Comp));
11981 Error_Msg_NE
11982 ("illegal circularity with declaration for & #",
11983 N, Comp);
11984 return;
11986 elsif Is_Record_Type (Etype (Comp)) then
11987 Check_Recursive_Declaration (Etype (Comp));
11988 end if;
11989 end if;
11991 Next_Component (Comp);
11992 end loop;
11993 end if;
11994 end Check_Recursive_Declaration;
11996 -- Start of processing for Constant_Redeclaration
11998 begin
11999 if Nkind (Parent (Prev)) = N_Object_Declaration then
12000 if Nkind (Object_Definition
12001 (Parent (Prev))) = N_Subtype_Indication
12002 then
12003 -- Find type of new declaration. The constraints of the two
12004 -- views must match statically, but there is no point in
12005 -- creating an itype for the full view.
12007 if Nkind (Obj_Def) = N_Subtype_Indication then
12008 Find_Type (Subtype_Mark (Obj_Def));
12009 New_T := Entity (Subtype_Mark (Obj_Def));
12011 else
12012 Find_Type (Obj_Def);
12013 New_T := Entity (Obj_Def);
12014 end if;
12016 T := Etype (Prev);
12018 else
12019 -- The full view may impose a constraint, even if the partial
12020 -- view does not, so construct the subtype.
12022 New_T := Find_Type_Of_Object (Obj_Def, N);
12023 T := New_T;
12024 end if;
12026 else
12027 -- Current declaration is illegal, diagnosed below in Enter_Name
12029 T := Empty;
12030 New_T := Any_Type;
12031 end if;
12033 -- If previous full declaration or a renaming declaration exists, or if
12034 -- a homograph is present, let Enter_Name handle it, either with an
12035 -- error or with the removal of an overridden implicit subprogram.
12036 -- The previous one is a full declaration if it has an expression
12037 -- (which in the case of an aggregate is indicated by the Init flag).
12039 if Ekind (Prev) /= E_Constant
12040 or else Nkind (Parent (Prev)) = N_Object_Renaming_Declaration
12041 or else Present (Expression (Parent (Prev)))
12042 or else Has_Init_Expression (Parent (Prev))
12043 or else Present (Full_View (Prev))
12044 then
12045 Enter_Name (Id);
12047 -- Verify that types of both declarations match, or else that both types
12048 -- are anonymous access types whose designated subtypes statically match
12049 -- (as allowed in Ada 2005 by AI-385).
12051 elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
12052 and then
12053 (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
12054 or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
12055 or else Is_Access_Constant (Etype (New_T)) /=
12056 Is_Access_Constant (Etype (Prev))
12057 or else Can_Never_Be_Null (Etype (New_T)) /=
12058 Can_Never_Be_Null (Etype (Prev))
12059 or else Null_Exclusion_Present (Parent (Prev)) /=
12060 Null_Exclusion_Present (Parent (Id))
12061 or else not Subtypes_Statically_Match
12062 (Designated_Type (Etype (Prev)),
12063 Designated_Type (Etype (New_T))))
12064 then
12065 Error_Msg_Sloc := Sloc (Prev);
12066 Error_Msg_N ("type does not match declaration#", N);
12067 Set_Full_View (Prev, Id);
12068 Set_Etype (Id, Any_Type);
12070 -- A deferred constant whose type is an anonymous array is always
12071 -- illegal (unless imported). A detailed error message might be
12072 -- helpful for Ada beginners.
12074 if Nkind (Object_Definition (Parent (Prev)))
12075 = N_Constrained_Array_Definition
12076 and then Nkind (Object_Definition (N))
12077 = N_Constrained_Array_Definition
12078 then
12079 Error_Msg_N ("\each anonymous array is a distinct type", N);
12080 Error_Msg_N ("a deferred constant must have a named type",
12081 Object_Definition (Parent (Prev)));
12082 end if;
12084 elsif
12085 Null_Exclusion_Present (Parent (Prev))
12086 and then not Null_Exclusion_Present (N)
12087 then
12088 Error_Msg_Sloc := Sloc (Prev);
12089 Error_Msg_N ("null-exclusion does not match declaration#", N);
12090 Set_Full_View (Prev, Id);
12091 Set_Etype (Id, Any_Type);
12093 -- If so, process the full constant declaration
12095 else
12096 -- RM 7.4 (6): If the subtype defined by the subtype_indication in
12097 -- the deferred declaration is constrained, then the subtype defined
12098 -- by the subtype_indication in the full declaration shall match it
12099 -- statically.
12101 Check_Possible_Deferred_Completion
12102 (Prev_Id => Prev,
12103 Prev_Obj_Def => Object_Definition (Parent (Prev)),
12104 Curr_Obj_Def => Obj_Def);
12106 Set_Full_View (Prev, Id);
12107 Set_Is_Public (Id, Is_Public (Prev));
12108 Set_Is_Internal (Id);
12109 Append_Entity (Id, Current_Scope);
12111 -- Check ALIASED present if present before (RM 7.4(7))
12113 if Is_Aliased (Prev)
12114 and then not Aliased_Present (N)
12115 then
12116 Error_Msg_Sloc := Sloc (Prev);
12117 Error_Msg_N ("ALIASED required (see declaration #)", N);
12118 end if;
12120 -- Check that placement is in private part and that the incomplete
12121 -- declaration appeared in the visible part.
12123 if Ekind (Current_Scope) = E_Package
12124 and then not In_Private_Part (Current_Scope)
12125 then
12126 Error_Msg_Sloc := Sloc (Prev);
12127 Error_Msg_N
12128 ("full constant for declaration # must be in private part", N);
12130 elsif Ekind (Current_Scope) = E_Package
12131 and then
12132 List_Containing (Parent (Prev)) /=
12133 Visible_Declarations (Package_Specification (Current_Scope))
12134 then
12135 Error_Msg_N
12136 ("deferred constant must be declared in visible part",
12137 Parent (Prev));
12138 end if;
12140 if Is_Access_Type (T)
12141 and then Nkind (Expression (N)) = N_Allocator
12142 then
12143 Check_Recursive_Declaration (Designated_Type (T));
12144 end if;
12146 -- A deferred constant is a visible entity. If type has invariants,
12147 -- verify that the initial value satisfies them.
12149 if Has_Invariants (T) and then Present (Invariant_Procedure (T)) then
12150 Insert_After (N,
12151 Make_Invariant_Call (New_Occurrence_Of (Prev, Sloc (N))));
12152 end if;
12153 end if;
12154 end Constant_Redeclaration;
12156 ----------------------
12157 -- Constrain_Access --
12158 ----------------------
12160 procedure Constrain_Access
12161 (Def_Id : in out Entity_Id;
12162 S : Node_Id;
12163 Related_Nod : Node_Id)
12165 T : constant Entity_Id := Entity (Subtype_Mark (S));
12166 Desig_Type : constant Entity_Id := Designated_Type (T);
12167 Desig_Subtype : Entity_Id := Create_Itype (E_Void, Related_Nod);
12168 Constraint_OK : Boolean := True;
12170 begin
12171 if Is_Array_Type (Desig_Type) then
12172 Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
12174 elsif (Is_Record_Type (Desig_Type)
12175 or else Is_Incomplete_Or_Private_Type (Desig_Type))
12176 and then not Is_Constrained (Desig_Type)
12177 then
12178 -- ??? The following code is a temporary bypass to ignore a
12179 -- discriminant constraint on access type if it is constraining
12180 -- the current record. Avoid creating the implicit subtype of the
12181 -- record we are currently compiling since right now, we cannot
12182 -- handle these. For now, just return the access type itself.
12184 if Desig_Type = Current_Scope
12185 and then No (Def_Id)
12186 then
12187 Set_Ekind (Desig_Subtype, E_Record_Subtype);
12188 Def_Id := Entity (Subtype_Mark (S));
12190 -- This call added to ensure that the constraint is analyzed
12191 -- (needed for a B test). Note that we still return early from
12192 -- this procedure to avoid recursive processing. ???
12194 Constrain_Discriminated_Type
12195 (Desig_Subtype, S, Related_Nod, For_Access => True);
12196 return;
12197 end if;
12199 -- Enforce rule that the constraint is illegal if there is an
12200 -- unconstrained view of the designated type. This means that the
12201 -- partial view (either a private type declaration or a derivation
12202 -- from a private type) has no discriminants. (Defect Report
12203 -- 8652/0008, Technical Corrigendum 1, checked by ACATS B371001).
12205 -- Rule updated for Ada 2005: The private type is said to have
12206 -- a constrained partial view, given that objects of the type
12207 -- can be declared. Furthermore, the rule applies to all access
12208 -- types, unlike the rule concerning default discriminants (see
12209 -- RM 3.7.1(7/3))
12211 if (Ekind (T) = E_General_Access_Type or else Ada_Version >= Ada_2005)
12212 and then Has_Private_Declaration (Desig_Type)
12213 and then In_Open_Scopes (Scope (Desig_Type))
12214 and then Has_Discriminants (Desig_Type)
12215 then
12216 declare
12217 Pack : constant Node_Id :=
12218 Unit_Declaration_Node (Scope (Desig_Type));
12219 Decls : List_Id;
12220 Decl : Node_Id;
12222 begin
12223 if Nkind (Pack) = N_Package_Declaration then
12224 Decls := Visible_Declarations (Specification (Pack));
12225 Decl := First (Decls);
12226 while Present (Decl) loop
12227 if (Nkind (Decl) = N_Private_Type_Declaration
12228 and then Chars (Defining_Identifier (Decl)) =
12229 Chars (Desig_Type))
12231 or else
12232 (Nkind (Decl) = N_Full_Type_Declaration
12233 and then
12234 Chars (Defining_Identifier (Decl)) =
12235 Chars (Desig_Type)
12236 and then Is_Derived_Type (Desig_Type)
12237 and then
12238 Has_Private_Declaration (Etype (Desig_Type)))
12239 then
12240 if No (Discriminant_Specifications (Decl)) then
12241 Error_Msg_N
12242 ("cannot constrain access type if designated "
12243 & "type has constrained partial view", S);
12244 end if;
12246 exit;
12247 end if;
12249 Next (Decl);
12250 end loop;
12251 end if;
12252 end;
12253 end if;
12255 Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
12256 For_Access => True);
12258 elsif Is_Concurrent_Type (Desig_Type)
12259 and then not Is_Constrained (Desig_Type)
12260 then
12261 Constrain_Concurrent (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
12263 else
12264 Error_Msg_N ("invalid constraint on access type", S);
12266 -- We simply ignore an invalid constraint
12268 Desig_Subtype := Desig_Type;
12269 Constraint_OK := False;
12270 end if;
12272 if No (Def_Id) then
12273 Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
12274 else
12275 Set_Ekind (Def_Id, E_Access_Subtype);
12276 end if;
12278 if Constraint_OK then
12279 Set_Etype (Def_Id, Base_Type (T));
12281 if Is_Private_Type (Desig_Type) then
12282 Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
12283 end if;
12284 else
12285 Set_Etype (Def_Id, Any_Type);
12286 end if;
12288 Set_Size_Info (Def_Id, T);
12289 Set_Is_Constrained (Def_Id, Constraint_OK);
12290 Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
12291 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
12292 Set_Is_Access_Constant (Def_Id, Is_Access_Constant (T));
12294 Conditional_Delay (Def_Id, T);
12296 -- AI-363 : Subtypes of general access types whose designated types have
12297 -- default discriminants are disallowed. In instances, the rule has to
12298 -- be checked against the actual, of which T is the subtype. In a
12299 -- generic body, the rule is checked assuming that the actual type has
12300 -- defaulted discriminants.
12302 if Ada_Version >= Ada_2005 or else Warn_On_Ada_2005_Compatibility then
12303 if Ekind (Base_Type (T)) = E_General_Access_Type
12304 and then Has_Defaulted_Discriminants (Desig_Type)
12305 then
12306 if Ada_Version < Ada_2005 then
12307 Error_Msg_N
12308 ("access subtype of general access type would not " &
12309 "be allowed in Ada 2005?y?", S);
12310 else
12311 Error_Msg_N
12312 ("access subtype of general access type not allowed", S);
12313 end if;
12315 Error_Msg_N ("\discriminants have defaults", S);
12317 elsif Is_Access_Type (T)
12318 and then Is_Generic_Type (Desig_Type)
12319 and then Has_Discriminants (Desig_Type)
12320 and then In_Package_Body (Current_Scope)
12321 then
12322 if Ada_Version < Ada_2005 then
12323 Error_Msg_N
12324 ("access subtype would not be allowed in generic body "
12325 & "in Ada 2005?y?", S);
12326 else
12327 Error_Msg_N
12328 ("access subtype not allowed in generic body", S);
12329 end if;
12331 Error_Msg_N
12332 ("\designated type is a discriminated formal", S);
12333 end if;
12334 end if;
12335 end Constrain_Access;
12337 ---------------------
12338 -- Constrain_Array --
12339 ---------------------
12341 procedure Constrain_Array
12342 (Def_Id : in out Entity_Id;
12343 SI : Node_Id;
12344 Related_Nod : Node_Id;
12345 Related_Id : Entity_Id;
12346 Suffix : Character)
12348 C : constant Node_Id := Constraint (SI);
12349 Number_Of_Constraints : Nat := 0;
12350 Index : Node_Id;
12351 S, T : Entity_Id;
12352 Constraint_OK : Boolean := True;
12354 begin
12355 T := Entity (Subtype_Mark (SI));
12357 if Is_Access_Type (T) then
12358 T := Designated_Type (T);
12359 end if;
12361 -- If an index constraint follows a subtype mark in a subtype indication
12362 -- then the type or subtype denoted by the subtype mark must not already
12363 -- impose an index constraint. The subtype mark must denote either an
12364 -- unconstrained array type or an access type whose designated type
12365 -- is such an array type... (RM 3.6.1)
12367 if Is_Constrained (T) then
12368 Error_Msg_N ("array type is already constrained", Subtype_Mark (SI));
12369 Constraint_OK := False;
12371 else
12372 S := First (Constraints (C));
12373 while Present (S) loop
12374 Number_Of_Constraints := Number_Of_Constraints + 1;
12375 Next (S);
12376 end loop;
12378 -- In either case, the index constraint must provide a discrete
12379 -- range for each index of the array type and the type of each
12380 -- discrete range must be the same as that of the corresponding
12381 -- index. (RM 3.6.1)
12383 if Number_Of_Constraints /= Number_Dimensions (T) then
12384 Error_Msg_NE ("incorrect number of index constraints for }", C, T);
12385 Constraint_OK := False;
12387 else
12388 S := First (Constraints (C));
12389 Index := First_Index (T);
12390 Analyze (Index);
12392 -- Apply constraints to each index type
12394 for J in 1 .. Number_Of_Constraints loop
12395 Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
12396 Next (Index);
12397 Next (S);
12398 end loop;
12400 end if;
12401 end if;
12403 if No (Def_Id) then
12404 Def_Id :=
12405 Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
12406 Set_Parent (Def_Id, Related_Nod);
12408 else
12409 Set_Ekind (Def_Id, E_Array_Subtype);
12410 end if;
12412 Set_Size_Info (Def_Id, (T));
12413 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
12414 Set_Etype (Def_Id, Base_Type (T));
12416 if Constraint_OK then
12417 Set_First_Index (Def_Id, First (Constraints (C)));
12418 else
12419 Set_First_Index (Def_Id, First_Index (T));
12420 end if;
12422 Set_Is_Constrained (Def_Id, True);
12423 Set_Is_Aliased (Def_Id, Is_Aliased (T));
12424 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
12426 Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
12427 Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
12429 -- A subtype does not inherit the Packed_Array_Impl_Type of is parent.
12430 -- We need to initialize the attribute because if Def_Id is previously
12431 -- analyzed through a limited_with clause, it will have the attributes
12432 -- of an incomplete type, one of which is an Elist that overlaps the
12433 -- Packed_Array_Impl_Type field.
12435 Set_Packed_Array_Impl_Type (Def_Id, Empty);
12437 -- Build a freeze node if parent still needs one. Also make sure that
12438 -- the Depends_On_Private status is set because the subtype will need
12439 -- reprocessing at the time the base type does, and also we must set a
12440 -- conditional delay.
12442 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
12443 Conditional_Delay (Def_Id, T);
12444 end Constrain_Array;
12446 ------------------------------
12447 -- Constrain_Component_Type --
12448 ------------------------------
12450 function Constrain_Component_Type
12451 (Comp : Entity_Id;
12452 Constrained_Typ : Entity_Id;
12453 Related_Node : Node_Id;
12454 Typ : Entity_Id;
12455 Constraints : Elist_Id) return Entity_Id
12457 Loc : constant Source_Ptr := Sloc (Constrained_Typ);
12458 Compon_Type : constant Entity_Id := Etype (Comp);
12460 function Build_Constrained_Array_Type
12461 (Old_Type : Entity_Id) return Entity_Id;
12462 -- If Old_Type is an array type, one of whose indexes is constrained
12463 -- by a discriminant, build an Itype whose constraint replaces the
12464 -- discriminant with its value in the constraint.
12466 function Build_Constrained_Discriminated_Type
12467 (Old_Type : Entity_Id) return Entity_Id;
12468 -- Ditto for record components
12470 function Build_Constrained_Access_Type
12471 (Old_Type : Entity_Id) return Entity_Id;
12472 -- Ditto for access types. Makes use of previous two functions, to
12473 -- constrain designated type.
12475 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id;
12476 -- T is an array or discriminated type, C is a list of constraints
12477 -- that apply to T. This routine builds the constrained subtype.
12479 function Is_Discriminant (Expr : Node_Id) return Boolean;
12480 -- Returns True if Expr is a discriminant
12482 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id;
12483 -- Find the value of discriminant Discrim in Constraint
12485 -----------------------------------
12486 -- Build_Constrained_Access_Type --
12487 -----------------------------------
12489 function Build_Constrained_Access_Type
12490 (Old_Type : Entity_Id) return Entity_Id
12492 Desig_Type : constant Entity_Id := Designated_Type (Old_Type);
12493 Itype : Entity_Id;
12494 Desig_Subtype : Entity_Id;
12495 Scop : Entity_Id;
12497 begin
12498 -- if the original access type was not embedded in the enclosing
12499 -- type definition, there is no need to produce a new access
12500 -- subtype. In fact every access type with an explicit constraint
12501 -- generates an itype whose scope is the enclosing record.
12503 if not Is_Type (Scope (Old_Type)) then
12504 return Old_Type;
12506 elsif Is_Array_Type (Desig_Type) then
12507 Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
12509 elsif Has_Discriminants (Desig_Type) then
12511 -- This may be an access type to an enclosing record type for
12512 -- which we are constructing the constrained components. Return
12513 -- the enclosing record subtype. This is not always correct,
12514 -- but avoids infinite recursion. ???
12516 Desig_Subtype := Any_Type;
12518 for J in reverse 0 .. Scope_Stack.Last loop
12519 Scop := Scope_Stack.Table (J).Entity;
12521 if Is_Type (Scop)
12522 and then Base_Type (Scop) = Base_Type (Desig_Type)
12523 then
12524 Desig_Subtype := Scop;
12525 end if;
12527 exit when not Is_Type (Scop);
12528 end loop;
12530 if Desig_Subtype = Any_Type then
12531 Desig_Subtype :=
12532 Build_Constrained_Discriminated_Type (Desig_Type);
12533 end if;
12535 else
12536 return Old_Type;
12537 end if;
12539 if Desig_Subtype /= Desig_Type then
12541 -- The Related_Node better be here or else we won't be able
12542 -- to attach new itypes to a node in the tree.
12544 pragma Assert (Present (Related_Node));
12546 Itype := Create_Itype (E_Access_Subtype, Related_Node);
12548 Set_Etype (Itype, Base_Type (Old_Type));
12549 Set_Size_Info (Itype, (Old_Type));
12550 Set_Directly_Designated_Type (Itype, Desig_Subtype);
12551 Set_Depends_On_Private (Itype, Has_Private_Component
12552 (Old_Type));
12553 Set_Is_Access_Constant (Itype, Is_Access_Constant
12554 (Old_Type));
12556 -- The new itype needs freezing when it depends on a not frozen
12557 -- type and the enclosing subtype needs freezing.
12559 if Has_Delayed_Freeze (Constrained_Typ)
12560 and then not Is_Frozen (Constrained_Typ)
12561 then
12562 Conditional_Delay (Itype, Base_Type (Old_Type));
12563 end if;
12565 return Itype;
12567 else
12568 return Old_Type;
12569 end if;
12570 end Build_Constrained_Access_Type;
12572 ----------------------------------
12573 -- Build_Constrained_Array_Type --
12574 ----------------------------------
12576 function Build_Constrained_Array_Type
12577 (Old_Type : Entity_Id) return Entity_Id
12579 Lo_Expr : Node_Id;
12580 Hi_Expr : Node_Id;
12581 Old_Index : Node_Id;
12582 Range_Node : Node_Id;
12583 Constr_List : List_Id;
12585 Need_To_Create_Itype : Boolean := False;
12587 begin
12588 Old_Index := First_Index (Old_Type);
12589 while Present (Old_Index) loop
12590 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
12592 if Is_Discriminant (Lo_Expr)
12593 or else
12594 Is_Discriminant (Hi_Expr)
12595 then
12596 Need_To_Create_Itype := True;
12597 end if;
12599 Next_Index (Old_Index);
12600 end loop;
12602 if Need_To_Create_Itype then
12603 Constr_List := New_List;
12605 Old_Index := First_Index (Old_Type);
12606 while Present (Old_Index) loop
12607 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
12609 if Is_Discriminant (Lo_Expr) then
12610 Lo_Expr := Get_Discr_Value (Lo_Expr);
12611 end if;
12613 if Is_Discriminant (Hi_Expr) then
12614 Hi_Expr := Get_Discr_Value (Hi_Expr);
12615 end if;
12617 Range_Node :=
12618 Make_Range
12619 (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
12621 Append (Range_Node, To => Constr_List);
12623 Next_Index (Old_Index);
12624 end loop;
12626 return Build_Subtype (Old_Type, Constr_List);
12628 else
12629 return Old_Type;
12630 end if;
12631 end Build_Constrained_Array_Type;
12633 ------------------------------------------
12634 -- Build_Constrained_Discriminated_Type --
12635 ------------------------------------------
12637 function Build_Constrained_Discriminated_Type
12638 (Old_Type : Entity_Id) return Entity_Id
12640 Expr : Node_Id;
12641 Constr_List : List_Id;
12642 Old_Constraint : Elmt_Id;
12644 Need_To_Create_Itype : Boolean := False;
12646 begin
12647 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
12648 while Present (Old_Constraint) loop
12649 Expr := Node (Old_Constraint);
12651 if Is_Discriminant (Expr) then
12652 Need_To_Create_Itype := True;
12653 end if;
12655 Next_Elmt (Old_Constraint);
12656 end loop;
12658 if Need_To_Create_Itype then
12659 Constr_List := New_List;
12661 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
12662 while Present (Old_Constraint) loop
12663 Expr := Node (Old_Constraint);
12665 if Is_Discriminant (Expr) then
12666 Expr := Get_Discr_Value (Expr);
12667 end if;
12669 Append (New_Copy_Tree (Expr), To => Constr_List);
12671 Next_Elmt (Old_Constraint);
12672 end loop;
12674 return Build_Subtype (Old_Type, Constr_List);
12676 else
12677 return Old_Type;
12678 end if;
12679 end Build_Constrained_Discriminated_Type;
12681 -------------------
12682 -- Build_Subtype --
12683 -------------------
12685 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id is
12686 Indic : Node_Id;
12687 Subtyp_Decl : Node_Id;
12688 Def_Id : Entity_Id;
12689 Btyp : Entity_Id := Base_Type (T);
12691 begin
12692 -- The Related_Node better be here or else we won't be able to
12693 -- attach new itypes to a node in the tree.
12695 pragma Assert (Present (Related_Node));
12697 -- If the view of the component's type is incomplete or private
12698 -- with unknown discriminants, then the constraint must be applied
12699 -- to the full type.
12701 if Has_Unknown_Discriminants (Btyp)
12702 and then Present (Underlying_Type (Btyp))
12703 then
12704 Btyp := Underlying_Type (Btyp);
12705 end if;
12707 Indic :=
12708 Make_Subtype_Indication (Loc,
12709 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
12710 Constraint => Make_Index_Or_Discriminant_Constraint (Loc, C));
12712 Def_Id := Create_Itype (Ekind (T), Related_Node);
12714 Subtyp_Decl :=
12715 Make_Subtype_Declaration (Loc,
12716 Defining_Identifier => Def_Id,
12717 Subtype_Indication => Indic);
12719 Set_Parent (Subtyp_Decl, Parent (Related_Node));
12721 -- Itypes must be analyzed with checks off (see package Itypes)
12723 Analyze (Subtyp_Decl, Suppress => All_Checks);
12725 return Def_Id;
12726 end Build_Subtype;
12728 ---------------------
12729 -- Get_Discr_Value --
12730 ---------------------
12732 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id is
12733 D : Entity_Id;
12734 E : Elmt_Id;
12736 begin
12737 -- The discriminant may be declared for the type, in which case we
12738 -- find it by iterating over the list of discriminants. If the
12739 -- discriminant is inherited from a parent type, it appears as the
12740 -- corresponding discriminant of the current type. This will be the
12741 -- case when constraining an inherited component whose constraint is
12742 -- given by a discriminant of the parent.
12744 D := First_Discriminant (Typ);
12745 E := First_Elmt (Constraints);
12747 while Present (D) loop
12748 if D = Entity (Discrim)
12749 or else D = CR_Discriminant (Entity (Discrim))
12750 or else Corresponding_Discriminant (D) = Entity (Discrim)
12751 then
12752 return Node (E);
12753 end if;
12755 Next_Discriminant (D);
12756 Next_Elmt (E);
12757 end loop;
12759 -- The Corresponding_Discriminant mechanism is incomplete, because
12760 -- the correspondence between new and old discriminants is not one
12761 -- to one: one new discriminant can constrain several old ones. In
12762 -- that case, scan sequentially the stored_constraint, the list of
12763 -- discriminants of the parents, and the constraints.
12765 -- Previous code checked for the present of the Stored_Constraint
12766 -- list for the derived type, but did not use it at all. Should it
12767 -- be present when the component is a discriminated task type?
12769 if Is_Derived_Type (Typ)
12770 and then Scope (Entity (Discrim)) = Etype (Typ)
12771 then
12772 D := First_Discriminant (Etype (Typ));
12773 E := First_Elmt (Constraints);
12774 while Present (D) loop
12775 if D = Entity (Discrim) then
12776 return Node (E);
12777 end if;
12779 Next_Discriminant (D);
12780 Next_Elmt (E);
12781 end loop;
12782 end if;
12784 -- Something is wrong if we did not find the value
12786 raise Program_Error;
12787 end Get_Discr_Value;
12789 ---------------------
12790 -- Is_Discriminant --
12791 ---------------------
12793 function Is_Discriminant (Expr : Node_Id) return Boolean is
12794 Discrim_Scope : Entity_Id;
12796 begin
12797 if Denotes_Discriminant (Expr) then
12798 Discrim_Scope := Scope (Entity (Expr));
12800 -- Either we have a reference to one of Typ's discriminants,
12802 pragma Assert (Discrim_Scope = Typ
12804 -- or to the discriminants of the parent type, in the case
12805 -- of a derivation of a tagged type with variants.
12807 or else Discrim_Scope = Etype (Typ)
12808 or else Full_View (Discrim_Scope) = Etype (Typ)
12810 -- or same as above for the case where the discriminants
12811 -- were declared in Typ's private view.
12813 or else (Is_Private_Type (Discrim_Scope)
12814 and then Chars (Discrim_Scope) = Chars (Typ))
12816 -- or else we are deriving from the full view and the
12817 -- discriminant is declared in the private entity.
12819 or else (Is_Private_Type (Typ)
12820 and then Chars (Discrim_Scope) = Chars (Typ))
12822 -- Or we are constrained the corresponding record of a
12823 -- synchronized type that completes a private declaration.
12825 or else (Is_Concurrent_Record_Type (Typ)
12826 and then
12827 Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
12829 -- or we have a class-wide type, in which case make sure the
12830 -- discriminant found belongs to the root type.
12832 or else (Is_Class_Wide_Type (Typ)
12833 and then Etype (Typ) = Discrim_Scope));
12835 return True;
12836 end if;
12838 -- In all other cases we have something wrong
12840 return False;
12841 end Is_Discriminant;
12843 -- Start of processing for Constrain_Component_Type
12845 begin
12846 if Nkind (Parent (Comp)) = N_Component_Declaration
12847 and then Comes_From_Source (Parent (Comp))
12848 and then Comes_From_Source
12849 (Subtype_Indication (Component_Definition (Parent (Comp))))
12850 and then
12851 Is_Entity_Name
12852 (Subtype_Indication (Component_Definition (Parent (Comp))))
12853 then
12854 return Compon_Type;
12856 elsif Is_Array_Type (Compon_Type) then
12857 return Build_Constrained_Array_Type (Compon_Type);
12859 elsif Has_Discriminants (Compon_Type) then
12860 return Build_Constrained_Discriminated_Type (Compon_Type);
12862 elsif Is_Access_Type (Compon_Type) then
12863 return Build_Constrained_Access_Type (Compon_Type);
12865 else
12866 return Compon_Type;
12867 end if;
12868 end Constrain_Component_Type;
12870 --------------------------
12871 -- Constrain_Concurrent --
12872 --------------------------
12874 -- For concurrent types, the associated record value type carries the same
12875 -- discriminants, so when we constrain a concurrent type, we must constrain
12876 -- the corresponding record type as well.
12878 procedure Constrain_Concurrent
12879 (Def_Id : in out Entity_Id;
12880 SI : Node_Id;
12881 Related_Nod : Node_Id;
12882 Related_Id : Entity_Id;
12883 Suffix : Character)
12885 -- Retrieve Base_Type to ensure getting to the concurrent type in the
12886 -- case of a private subtype (needed when only doing semantic analysis).
12888 T_Ent : Entity_Id := Base_Type (Entity (Subtype_Mark (SI)));
12889 T_Val : Entity_Id;
12891 begin
12892 if Is_Access_Type (T_Ent) then
12893 T_Ent := Designated_Type (T_Ent);
12894 end if;
12896 T_Val := Corresponding_Record_Type (T_Ent);
12898 if Present (T_Val) then
12900 if No (Def_Id) then
12901 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
12902 end if;
12904 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
12906 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
12907 Set_Corresponding_Record_Type (Def_Id,
12908 Constrain_Corresponding_Record (Def_Id, T_Val, Related_Nod));
12910 else
12911 -- If there is no associated record, expansion is disabled and this
12912 -- is a generic context. Create a subtype in any case, so that
12913 -- semantic analysis can proceed.
12915 if No (Def_Id) then
12916 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
12917 end if;
12919 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
12920 end if;
12921 end Constrain_Concurrent;
12923 ------------------------------------
12924 -- Constrain_Corresponding_Record --
12925 ------------------------------------
12927 function Constrain_Corresponding_Record
12928 (Prot_Subt : Entity_Id;
12929 Corr_Rec : Entity_Id;
12930 Related_Nod : Node_Id) return Entity_Id
12932 T_Sub : constant Entity_Id :=
12933 Create_Itype (E_Record_Subtype, Related_Nod, Corr_Rec, 'C');
12935 begin
12936 Set_Etype (T_Sub, Corr_Rec);
12937 Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
12938 Set_Is_Constrained (T_Sub, True);
12939 Set_First_Entity (T_Sub, First_Entity (Corr_Rec));
12940 Set_Last_Entity (T_Sub, Last_Entity (Corr_Rec));
12942 if Has_Discriminants (Prot_Subt) then -- False only if errors.
12943 Set_Discriminant_Constraint
12944 (T_Sub, Discriminant_Constraint (Prot_Subt));
12945 Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
12946 Create_Constrained_Components
12947 (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
12948 end if;
12950 Set_Depends_On_Private (T_Sub, Has_Private_Component (T_Sub));
12952 if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
12953 Conditional_Delay (T_Sub, Corr_Rec);
12955 else
12956 -- This is a component subtype: it will be frozen in the context of
12957 -- the enclosing record's init_proc, so that discriminant references
12958 -- are resolved to discriminals. (Note: we used to skip freezing
12959 -- altogether in that case, which caused errors downstream for
12960 -- components of a bit packed array type).
12962 Set_Has_Delayed_Freeze (T_Sub);
12963 end if;
12965 return T_Sub;
12966 end Constrain_Corresponding_Record;
12968 -----------------------
12969 -- Constrain_Decimal --
12970 -----------------------
12972 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id) is
12973 T : constant Entity_Id := Entity (Subtype_Mark (S));
12974 C : constant Node_Id := Constraint (S);
12975 Loc : constant Source_Ptr := Sloc (C);
12976 Range_Expr : Node_Id;
12977 Digits_Expr : Node_Id;
12978 Digits_Val : Uint;
12979 Bound_Val : Ureal;
12981 begin
12982 Set_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
12984 if Nkind (C) = N_Range_Constraint then
12985 Range_Expr := Range_Expression (C);
12986 Digits_Val := Digits_Value (T);
12988 else
12989 pragma Assert (Nkind (C) = N_Digits_Constraint);
12991 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
12993 Digits_Expr := Digits_Expression (C);
12994 Analyze_And_Resolve (Digits_Expr, Any_Integer);
12996 Check_Digits_Expression (Digits_Expr);
12997 Digits_Val := Expr_Value (Digits_Expr);
12999 if Digits_Val > Digits_Value (T) then
13000 Error_Msg_N
13001 ("digits expression is incompatible with subtype", C);
13002 Digits_Val := Digits_Value (T);
13003 end if;
13005 if Present (Range_Constraint (C)) then
13006 Range_Expr := Range_Expression (Range_Constraint (C));
13007 else
13008 Range_Expr := Empty;
13009 end if;
13010 end if;
13012 Set_Etype (Def_Id, Base_Type (T));
13013 Set_Size_Info (Def_Id, (T));
13014 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13015 Set_Delta_Value (Def_Id, Delta_Value (T));
13016 Set_Scale_Value (Def_Id, Scale_Value (T));
13017 Set_Small_Value (Def_Id, Small_Value (T));
13018 Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
13019 Set_Digits_Value (Def_Id, Digits_Val);
13021 -- Manufacture range from given digits value if no range present
13023 if No (Range_Expr) then
13024 Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
13025 Range_Expr :=
13026 Make_Range (Loc,
13027 Low_Bound =>
13028 Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
13029 High_Bound =>
13030 Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
13031 end if;
13033 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
13034 Set_Discrete_RM_Size (Def_Id);
13036 -- Unconditionally delay the freeze, since we cannot set size
13037 -- information in all cases correctly until the freeze point.
13039 Set_Has_Delayed_Freeze (Def_Id);
13040 end Constrain_Decimal;
13042 ----------------------------------
13043 -- Constrain_Discriminated_Type --
13044 ----------------------------------
13046 procedure Constrain_Discriminated_Type
13047 (Def_Id : Entity_Id;
13048 S : Node_Id;
13049 Related_Nod : Node_Id;
13050 For_Access : Boolean := False)
13052 E : constant Entity_Id := Entity (Subtype_Mark (S));
13053 T : Entity_Id;
13054 C : Node_Id;
13055 Elist : Elist_Id := New_Elmt_List;
13057 procedure Fixup_Bad_Constraint;
13058 -- This is called after finding a bad constraint, and after having
13059 -- posted an appropriate error message. The mission is to leave the
13060 -- entity T in as reasonable state as possible.
13062 --------------------------
13063 -- Fixup_Bad_Constraint --
13064 --------------------------
13066 procedure Fixup_Bad_Constraint is
13067 begin
13068 -- Set a reasonable Ekind for the entity. For an incomplete type,
13069 -- we can't do much, but for other types, we can set the proper
13070 -- corresponding subtype kind.
13072 if Ekind (T) = E_Incomplete_Type then
13073 Set_Ekind (Def_Id, Ekind (T));
13074 else
13075 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
13076 end if;
13078 -- Set Etype to the known type, to reduce chances of cascaded errors
13080 Set_Etype (Def_Id, E);
13081 Set_Error_Posted (Def_Id);
13082 end Fixup_Bad_Constraint;
13084 -- Start of processing for Constrain_Discriminated_Type
13086 begin
13087 C := Constraint (S);
13089 -- A discriminant constraint is only allowed in a subtype indication,
13090 -- after a subtype mark. This subtype mark must denote either a type
13091 -- with discriminants, or an access type whose designated type is a
13092 -- type with discriminants. A discriminant constraint specifies the
13093 -- values of these discriminants (RM 3.7.2(5)).
13095 T := Base_Type (Entity (Subtype_Mark (S)));
13097 if Is_Access_Type (T) then
13098 T := Designated_Type (T);
13099 end if;
13101 -- Ada 2005 (AI-412): Constrained incomplete subtypes are illegal.
13102 -- Avoid generating an error for access-to-incomplete subtypes.
13104 if Ada_Version >= Ada_2005
13105 and then Ekind (T) = E_Incomplete_Type
13106 and then Nkind (Parent (S)) = N_Subtype_Declaration
13107 and then not Is_Itype (Def_Id)
13108 then
13109 -- A little sanity check, emit an error message if the type
13110 -- has discriminants to begin with. Type T may be a regular
13111 -- incomplete type or imported via a limited with clause.
13113 if Has_Discriminants (T)
13114 or else (From_Limited_With (T)
13115 and then Present (Non_Limited_View (T))
13116 and then Nkind (Parent (Non_Limited_View (T))) =
13117 N_Full_Type_Declaration
13118 and then Present (Discriminant_Specifications
13119 (Parent (Non_Limited_View (T)))))
13120 then
13121 Error_Msg_N
13122 ("(Ada 2005) incomplete subtype may not be constrained", C);
13123 else
13124 Error_Msg_N ("invalid constraint: type has no discriminant", C);
13125 end if;
13127 Fixup_Bad_Constraint;
13128 return;
13130 -- Check that the type has visible discriminants. The type may be
13131 -- a private type with unknown discriminants whose full view has
13132 -- discriminants which are invisible.
13134 elsif not Has_Discriminants (T)
13135 or else
13136 (Has_Unknown_Discriminants (T)
13137 and then Is_Private_Type (T))
13138 then
13139 Error_Msg_N ("invalid constraint: type has no discriminant", C);
13140 Fixup_Bad_Constraint;
13141 return;
13143 elsif Is_Constrained (E)
13144 or else (Ekind (E) = E_Class_Wide_Subtype
13145 and then Present (Discriminant_Constraint (E)))
13146 then
13147 Error_Msg_N ("type is already constrained", Subtype_Mark (S));
13148 Fixup_Bad_Constraint;
13149 return;
13150 end if;
13152 -- T may be an unconstrained subtype (e.g. a generic actual).
13153 -- Constraint applies to the base type.
13155 T := Base_Type (T);
13157 Elist := Build_Discriminant_Constraints (T, S);
13159 -- If the list returned was empty we had an error in building the
13160 -- discriminant constraint. We have also already signalled an error
13161 -- in the incomplete type case
13163 if Is_Empty_Elmt_List (Elist) then
13164 Fixup_Bad_Constraint;
13165 return;
13166 end if;
13168 Build_Discriminated_Subtype (T, Def_Id, Elist, Related_Nod, For_Access);
13169 end Constrain_Discriminated_Type;
13171 ---------------------------
13172 -- Constrain_Enumeration --
13173 ---------------------------
13175 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id) is
13176 T : constant Entity_Id := Entity (Subtype_Mark (S));
13177 C : constant Node_Id := Constraint (S);
13179 begin
13180 Set_Ekind (Def_Id, E_Enumeration_Subtype);
13182 Set_First_Literal (Def_Id, First_Literal (Base_Type (T)));
13184 Set_Etype (Def_Id, Base_Type (T));
13185 Set_Size_Info (Def_Id, (T));
13186 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13187 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
13189 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13191 Set_Discrete_RM_Size (Def_Id);
13192 end Constrain_Enumeration;
13194 ----------------------
13195 -- Constrain_Float --
13196 ----------------------
13198 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id) is
13199 T : constant Entity_Id := Entity (Subtype_Mark (S));
13200 C : Node_Id;
13201 D : Node_Id;
13202 Rais : Node_Id;
13204 begin
13205 Set_Ekind (Def_Id, E_Floating_Point_Subtype);
13207 Set_Etype (Def_Id, Base_Type (T));
13208 Set_Size_Info (Def_Id, (T));
13209 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13211 -- Process the constraint
13213 C := Constraint (S);
13215 -- Digits constraint present
13217 if Nkind (C) = N_Digits_Constraint then
13219 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
13220 Check_Restriction (No_Obsolescent_Features, C);
13222 if Warn_On_Obsolescent_Feature then
13223 Error_Msg_N
13224 ("subtype digits constraint is an " &
13225 "obsolescent feature (RM J.3(8))?j?", C);
13226 end if;
13228 D := Digits_Expression (C);
13229 Analyze_And_Resolve (D, Any_Integer);
13230 Check_Digits_Expression (D);
13231 Set_Digits_Value (Def_Id, Expr_Value (D));
13233 -- Check that digits value is in range. Obviously we can do this
13234 -- at compile time, but it is strictly a runtime check, and of
13235 -- course there is an ACVC test that checks this.
13237 if Digits_Value (Def_Id) > Digits_Value (T) then
13238 Error_Msg_Uint_1 := Digits_Value (T);
13239 Error_Msg_N ("??digits value is too large, maximum is ^", D);
13240 Rais :=
13241 Make_Raise_Constraint_Error (Sloc (D),
13242 Reason => CE_Range_Check_Failed);
13243 Insert_Action (Declaration_Node (Def_Id), Rais);
13244 end if;
13246 C := Range_Constraint (C);
13248 -- No digits constraint present
13250 else
13251 Set_Digits_Value (Def_Id, Digits_Value (T));
13252 end if;
13254 -- Range constraint present
13256 if Nkind (C) = N_Range_Constraint then
13257 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13259 -- No range constraint present
13261 else
13262 pragma Assert (No (C));
13263 Set_Scalar_Range (Def_Id, Scalar_Range (T));
13264 end if;
13266 Set_Is_Constrained (Def_Id);
13267 end Constrain_Float;
13269 ---------------------
13270 -- Constrain_Index --
13271 ---------------------
13273 procedure Constrain_Index
13274 (Index : Node_Id;
13275 S : Node_Id;
13276 Related_Nod : Node_Id;
13277 Related_Id : Entity_Id;
13278 Suffix : Character;
13279 Suffix_Index : Nat)
13281 Def_Id : Entity_Id;
13282 R : Node_Id := Empty;
13283 T : constant Entity_Id := Etype (Index);
13285 begin
13286 Def_Id :=
13287 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
13288 Set_Etype (Def_Id, Base_Type (T));
13290 if Nkind (S) = N_Range
13291 or else
13292 (Nkind (S) = N_Attribute_Reference
13293 and then Attribute_Name (S) = Name_Range)
13294 then
13295 -- A Range attribute will be transformed into N_Range by Resolve
13297 Analyze (S);
13298 Set_Etype (S, T);
13299 R := S;
13301 Process_Range_Expr_In_Decl (R, T);
13303 if not Error_Posted (S)
13304 and then
13305 (Nkind (S) /= N_Range
13306 or else not Covers (T, (Etype (Low_Bound (S))))
13307 or else not Covers (T, (Etype (High_Bound (S)))))
13308 then
13309 if Base_Type (T) /= Any_Type
13310 and then Etype (Low_Bound (S)) /= Any_Type
13311 and then Etype (High_Bound (S)) /= Any_Type
13312 then
13313 Error_Msg_N ("range expected", S);
13314 end if;
13315 end if;
13317 elsif Nkind (S) = N_Subtype_Indication then
13319 -- The parser has verified that this is a discrete indication
13321 Resolve_Discrete_Subtype_Indication (S, T);
13322 Bad_Predicated_Subtype_Use
13323 ("subtype& has predicate, not allowed in index constraint",
13324 S, Entity (Subtype_Mark (S)));
13326 R := Range_Expression (Constraint (S));
13328 -- Capture values of bounds and generate temporaries for them if
13329 -- needed, since checks may cause duplication of the expressions
13330 -- which must not be reevaluated.
13332 -- The forced evaluation removes side effects from expressions, which
13333 -- should occur also in GNATprove mode. Otherwise, we end up with
13334 -- unexpected insertions of actions at places where this is not
13335 -- supposed to occur, e.g. on default parameters of a call.
13337 if Expander_Active or GNATprove_Mode then
13338 Force_Evaluation
13339 (Low_Bound (R), Related_Id => Def_Id, Is_Low_Bound => True);
13340 Force_Evaluation
13341 (High_Bound (R), Related_Id => Def_Id, Is_High_Bound => True);
13342 end if;
13344 elsif Nkind (S) = N_Discriminant_Association then
13346 -- Syntactically valid in subtype indication
13348 Error_Msg_N ("invalid index constraint", S);
13349 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
13350 return;
13352 -- Subtype_Mark case, no anonymous subtypes to construct
13354 else
13355 Analyze (S);
13357 if Is_Entity_Name (S) then
13358 if not Is_Type (Entity (S)) then
13359 Error_Msg_N ("expect subtype mark for index constraint", S);
13361 elsif Base_Type (Entity (S)) /= Base_Type (T) then
13362 Wrong_Type (S, Base_Type (T));
13364 -- Check error of subtype with predicate in index constraint
13366 else
13367 Bad_Predicated_Subtype_Use
13368 ("subtype& has predicate, not allowed in index constraint",
13369 S, Entity (S));
13370 end if;
13372 return;
13374 else
13375 Error_Msg_N ("invalid index constraint", S);
13376 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
13377 return;
13378 end if;
13379 end if;
13381 -- Complete construction of the Itype
13383 if Is_Modular_Integer_Type (T) then
13384 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
13386 elsif Is_Integer_Type (T) then
13387 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
13389 else
13390 Set_Ekind (Def_Id, E_Enumeration_Subtype);
13391 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
13392 Set_First_Literal (Def_Id, First_Literal (T));
13393 end if;
13395 Set_Size_Info (Def_Id, (T));
13396 Set_RM_Size (Def_Id, RM_Size (T));
13397 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13399 Set_Scalar_Range (Def_Id, R);
13401 Set_Etype (S, Def_Id);
13402 Set_Discrete_RM_Size (Def_Id);
13403 end Constrain_Index;
13405 -----------------------
13406 -- Constrain_Integer --
13407 -----------------------
13409 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id) is
13410 T : constant Entity_Id := Entity (Subtype_Mark (S));
13411 C : constant Node_Id := Constraint (S);
13413 begin
13414 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13416 if Is_Modular_Integer_Type (T) then
13417 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
13418 else
13419 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
13420 end if;
13422 Set_Etype (Def_Id, Base_Type (T));
13423 Set_Size_Info (Def_Id, (T));
13424 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13425 Set_Discrete_RM_Size (Def_Id);
13426 end Constrain_Integer;
13428 ------------------------------
13429 -- Constrain_Ordinary_Fixed --
13430 ------------------------------
13432 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id) is
13433 T : constant Entity_Id := Entity (Subtype_Mark (S));
13434 C : Node_Id;
13435 D : Node_Id;
13436 Rais : Node_Id;
13438 begin
13439 Set_Ekind (Def_Id, E_Ordinary_Fixed_Point_Subtype);
13440 Set_Etype (Def_Id, Base_Type (T));
13441 Set_Size_Info (Def_Id, (T));
13442 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13443 Set_Small_Value (Def_Id, Small_Value (T));
13445 -- Process the constraint
13447 C := Constraint (S);
13449 -- Delta constraint present
13451 if Nkind (C) = N_Delta_Constraint then
13453 Check_SPARK_05_Restriction ("delta constraint is not allowed", S);
13454 Check_Restriction (No_Obsolescent_Features, C);
13456 if Warn_On_Obsolescent_Feature then
13457 Error_Msg_S
13458 ("subtype delta constraint is an " &
13459 "obsolescent feature (RM J.3(7))?j?");
13460 end if;
13462 D := Delta_Expression (C);
13463 Analyze_And_Resolve (D, Any_Real);
13464 Check_Delta_Expression (D);
13465 Set_Delta_Value (Def_Id, Expr_Value_R (D));
13467 -- Check that delta value is in range. Obviously we can do this
13468 -- at compile time, but it is strictly a runtime check, and of
13469 -- course there is an ACVC test that checks this.
13471 if Delta_Value (Def_Id) < Delta_Value (T) then
13472 Error_Msg_N ("??delta value is too small", D);
13473 Rais :=
13474 Make_Raise_Constraint_Error (Sloc (D),
13475 Reason => CE_Range_Check_Failed);
13476 Insert_Action (Declaration_Node (Def_Id), Rais);
13477 end if;
13479 C := Range_Constraint (C);
13481 -- No delta constraint present
13483 else
13484 Set_Delta_Value (Def_Id, Delta_Value (T));
13485 end if;
13487 -- Range constraint present
13489 if Nkind (C) = N_Range_Constraint then
13490 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13492 -- No range constraint present
13494 else
13495 pragma Assert (No (C));
13496 Set_Scalar_Range (Def_Id, Scalar_Range (T));
13497 end if;
13499 Set_Discrete_RM_Size (Def_Id);
13501 -- Unconditionally delay the freeze, since we cannot set size
13502 -- information in all cases correctly until the freeze point.
13504 Set_Has_Delayed_Freeze (Def_Id);
13505 end Constrain_Ordinary_Fixed;
13507 -----------------------
13508 -- Contain_Interface --
13509 -----------------------
13511 function Contain_Interface
13512 (Iface : Entity_Id;
13513 Ifaces : Elist_Id) return Boolean
13515 Iface_Elmt : Elmt_Id;
13517 begin
13518 if Present (Ifaces) then
13519 Iface_Elmt := First_Elmt (Ifaces);
13520 while Present (Iface_Elmt) loop
13521 if Node (Iface_Elmt) = Iface then
13522 return True;
13523 end if;
13525 Next_Elmt (Iface_Elmt);
13526 end loop;
13527 end if;
13529 return False;
13530 end Contain_Interface;
13532 ---------------------------
13533 -- Convert_Scalar_Bounds --
13534 ---------------------------
13536 procedure Convert_Scalar_Bounds
13537 (N : Node_Id;
13538 Parent_Type : Entity_Id;
13539 Derived_Type : Entity_Id;
13540 Loc : Source_Ptr)
13542 Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
13544 Lo : Node_Id;
13545 Hi : Node_Id;
13546 Rng : Node_Id;
13548 begin
13549 -- Defend against previous errors
13551 if No (Scalar_Range (Derived_Type)) then
13552 Check_Error_Detected;
13553 return;
13554 end if;
13556 Lo := Build_Scalar_Bound
13557 (Type_Low_Bound (Derived_Type),
13558 Parent_Type, Implicit_Base);
13560 Hi := Build_Scalar_Bound
13561 (Type_High_Bound (Derived_Type),
13562 Parent_Type, Implicit_Base);
13564 Rng :=
13565 Make_Range (Loc,
13566 Low_Bound => Lo,
13567 High_Bound => Hi);
13569 Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
13571 Set_Parent (Rng, N);
13572 Set_Scalar_Range (Derived_Type, Rng);
13574 -- Analyze the bounds
13576 Analyze_And_Resolve (Lo, Implicit_Base);
13577 Analyze_And_Resolve (Hi, Implicit_Base);
13579 -- Analyze the range itself, except that we do not analyze it if
13580 -- the bounds are real literals, and we have a fixed-point type.
13581 -- The reason for this is that we delay setting the bounds in this
13582 -- case till we know the final Small and Size values (see circuit
13583 -- in Freeze.Freeze_Fixed_Point_Type for further details).
13585 if Is_Fixed_Point_Type (Parent_Type)
13586 and then Nkind (Lo) = N_Real_Literal
13587 and then Nkind (Hi) = N_Real_Literal
13588 then
13589 return;
13591 -- Here we do the analysis of the range
13593 -- Note: we do this manually, since if we do a normal Analyze and
13594 -- Resolve call, there are problems with the conversions used for
13595 -- the derived type range.
13597 else
13598 Set_Etype (Rng, Implicit_Base);
13599 Set_Analyzed (Rng, True);
13600 end if;
13601 end Convert_Scalar_Bounds;
13603 -------------------
13604 -- Copy_And_Swap --
13605 -------------------
13607 procedure Copy_And_Swap (Priv, Full : Entity_Id) is
13608 begin
13609 -- Initialize new full declaration entity by copying the pertinent
13610 -- fields of the corresponding private declaration entity.
13612 -- We temporarily set Ekind to a value appropriate for a type to
13613 -- avoid assert failures in Einfo from checking for setting type
13614 -- attributes on something that is not a type. Ekind (Priv) is an
13615 -- appropriate choice, since it allowed the attributes to be set
13616 -- in the first place. This Ekind value will be modified later.
13618 Set_Ekind (Full, Ekind (Priv));
13620 -- Also set Etype temporarily to Any_Type, again, in the absence
13621 -- of errors, it will be properly reset, and if there are errors,
13622 -- then we want a value of Any_Type to remain.
13624 Set_Etype (Full, Any_Type);
13626 -- Now start copying attributes
13628 Set_Has_Discriminants (Full, Has_Discriminants (Priv));
13630 if Has_Discriminants (Full) then
13631 Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
13632 Set_Stored_Constraint (Full, Stored_Constraint (Priv));
13633 end if;
13635 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
13636 Set_Homonym (Full, Homonym (Priv));
13637 Set_Is_Immediately_Visible (Full, Is_Immediately_Visible (Priv));
13638 Set_Is_Public (Full, Is_Public (Priv));
13639 Set_Is_Pure (Full, Is_Pure (Priv));
13640 Set_Is_Tagged_Type (Full, Is_Tagged_Type (Priv));
13641 Set_Has_Pragma_Unmodified (Full, Has_Pragma_Unmodified (Priv));
13642 Set_Has_Pragma_Unreferenced (Full, Has_Pragma_Unreferenced (Priv));
13643 Set_Has_Pragma_Unreferenced_Objects
13644 (Full, Has_Pragma_Unreferenced_Objects
13645 (Priv));
13647 Conditional_Delay (Full, Priv);
13649 if Is_Tagged_Type (Full) then
13650 Set_Direct_Primitive_Operations
13651 (Full, Direct_Primitive_Operations (Priv));
13652 Set_No_Tagged_Streams_Pragma
13653 (Full, No_Tagged_Streams_Pragma (Priv));
13655 if Is_Base_Type (Priv) then
13656 Set_Class_Wide_Type (Full, Class_Wide_Type (Priv));
13657 end if;
13658 end if;
13660 Set_Is_Volatile (Full, Is_Volatile (Priv));
13661 Set_Treat_As_Volatile (Full, Treat_As_Volatile (Priv));
13662 Set_Scope (Full, Scope (Priv));
13663 Set_Next_Entity (Full, Next_Entity (Priv));
13664 Set_First_Entity (Full, First_Entity (Priv));
13665 Set_Last_Entity (Full, Last_Entity (Priv));
13667 -- If access types have been recorded for later handling, keep them in
13668 -- the full view so that they get handled when the full view freeze
13669 -- node is expanded.
13671 if Present (Freeze_Node (Priv))
13672 and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
13673 then
13674 Ensure_Freeze_Node (Full);
13675 Set_Access_Types_To_Process
13676 (Freeze_Node (Full),
13677 Access_Types_To_Process (Freeze_Node (Priv)));
13678 end if;
13680 -- Swap the two entities. Now Private is the full type entity and Full
13681 -- is the private one. They will be swapped back at the end of the
13682 -- private part. This swapping ensures that the entity that is visible
13683 -- in the private part is the full declaration.
13685 Exchange_Entities (Priv, Full);
13686 Append_Entity (Full, Scope (Full));
13687 end Copy_And_Swap;
13689 -------------------------------------
13690 -- Copy_Array_Base_Type_Attributes --
13691 -------------------------------------
13693 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
13694 begin
13695 Set_Component_Alignment (T1, Component_Alignment (T2));
13696 Set_Component_Type (T1, Component_Type (T2));
13697 Set_Component_Size (T1, Component_Size (T2));
13698 Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
13699 Set_Has_Non_Standard_Rep (T1, Has_Non_Standard_Rep (T2));
13700 Set_Has_Protected (T1, Has_Protected (T2));
13701 Set_Has_Task (T1, Has_Task (T2));
13702 Set_Is_Packed (T1, Is_Packed (T2));
13703 Set_Has_Aliased_Components (T1, Has_Aliased_Components (T2));
13704 Set_Has_Atomic_Components (T1, Has_Atomic_Components (T2));
13705 Set_Has_Volatile_Components (T1, Has_Volatile_Components (T2));
13706 end Copy_Array_Base_Type_Attributes;
13708 -----------------------------------
13709 -- Copy_Array_Subtype_Attributes --
13710 -----------------------------------
13712 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
13713 begin
13714 Set_Size_Info (T1, T2);
13716 Set_First_Index (T1, First_Index (T2));
13717 Set_Is_Aliased (T1, Is_Aliased (T2));
13718 Set_Is_Volatile (T1, Is_Volatile (T2));
13719 Set_Treat_As_Volatile (T1, Treat_As_Volatile (T2));
13720 Set_Is_Constrained (T1, Is_Constrained (T2));
13721 Set_Depends_On_Private (T1, Has_Private_Component (T2));
13722 Inherit_Rep_Item_Chain (T1, T2);
13723 Set_Convention (T1, Convention (T2));
13724 Set_Is_Limited_Composite (T1, Is_Limited_Composite (T2));
13725 Set_Is_Private_Composite (T1, Is_Private_Composite (T2));
13726 Set_Packed_Array_Impl_Type (T1, Packed_Array_Impl_Type (T2));
13727 end Copy_Array_Subtype_Attributes;
13729 -----------------------------------
13730 -- Create_Constrained_Components --
13731 -----------------------------------
13733 procedure Create_Constrained_Components
13734 (Subt : Entity_Id;
13735 Decl_Node : Node_Id;
13736 Typ : Entity_Id;
13737 Constraints : Elist_Id)
13739 Loc : constant Source_Ptr := Sloc (Subt);
13740 Comp_List : constant Elist_Id := New_Elmt_List;
13741 Parent_Type : constant Entity_Id := Etype (Typ);
13742 Assoc_List : constant List_Id := New_List;
13743 Discr_Val : Elmt_Id;
13744 Errors : Boolean;
13745 New_C : Entity_Id;
13746 Old_C : Entity_Id;
13747 Is_Static : Boolean := True;
13749 procedure Collect_Fixed_Components (Typ : Entity_Id);
13750 -- Collect parent type components that do not appear in a variant part
13752 procedure Create_All_Components;
13753 -- Iterate over Comp_List to create the components of the subtype
13755 function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
13756 -- Creates a new component from Old_Compon, copying all the fields from
13757 -- it, including its Etype, inserts the new component in the Subt entity
13758 -- chain and returns the new component.
13760 function Is_Variant_Record (T : Entity_Id) return Boolean;
13761 -- If true, and discriminants are static, collect only components from
13762 -- variants selected by discriminant values.
13764 ------------------------------
13765 -- Collect_Fixed_Components --
13766 ------------------------------
13768 procedure Collect_Fixed_Components (Typ : Entity_Id) is
13769 begin
13770 -- Build association list for discriminants, and find components of the
13771 -- variant part selected by the values of the discriminants.
13773 Old_C := First_Discriminant (Typ);
13774 Discr_Val := First_Elmt (Constraints);
13775 while Present (Old_C) loop
13776 Append_To (Assoc_List,
13777 Make_Component_Association (Loc,
13778 Choices => New_List (New_Occurrence_Of (Old_C, Loc)),
13779 Expression => New_Copy (Node (Discr_Val))));
13781 Next_Elmt (Discr_Val);
13782 Next_Discriminant (Old_C);
13783 end loop;
13785 -- The tag and the possible parent component are unconditionally in
13786 -- the subtype.
13788 if Is_Tagged_Type (Typ) or else Has_Controlled_Component (Typ) then
13789 Old_C := First_Component (Typ);
13790 while Present (Old_C) loop
13791 if Nam_In (Chars (Old_C), Name_uTag, Name_uParent) then
13792 Append_Elmt (Old_C, Comp_List);
13793 end if;
13795 Next_Component (Old_C);
13796 end loop;
13797 end if;
13798 end Collect_Fixed_Components;
13800 ---------------------------
13801 -- Create_All_Components --
13802 ---------------------------
13804 procedure Create_All_Components is
13805 Comp : Elmt_Id;
13807 begin
13808 Comp := First_Elmt (Comp_List);
13809 while Present (Comp) loop
13810 Old_C := Node (Comp);
13811 New_C := Create_Component (Old_C);
13813 Set_Etype
13814 (New_C,
13815 Constrain_Component_Type
13816 (Old_C, Subt, Decl_Node, Typ, Constraints));
13817 Set_Is_Public (New_C, Is_Public (Subt));
13819 Next_Elmt (Comp);
13820 end loop;
13821 end Create_All_Components;
13823 ----------------------
13824 -- Create_Component --
13825 ----------------------
13827 function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
13828 New_Compon : constant Entity_Id := New_Copy (Old_Compon);
13830 begin
13831 if Ekind (Old_Compon) = E_Discriminant
13832 and then Is_Completely_Hidden (Old_Compon)
13833 then
13834 -- This is a shadow discriminant created for a discriminant of
13835 -- the parent type, which needs to be present in the subtype.
13836 -- Give the shadow discriminant an internal name that cannot
13837 -- conflict with that of visible components.
13839 Set_Chars (New_Compon, New_Internal_Name ('C'));
13840 end if;
13842 -- Set the parent so we have a proper link for freezing etc. This is
13843 -- not a real parent pointer, since of course our parent does not own
13844 -- up to us and reference us, we are an illegitimate child of the
13845 -- original parent.
13847 Set_Parent (New_Compon, Parent (Old_Compon));
13849 -- If the old component's Esize was already determined and is a
13850 -- static value, then the new component simply inherits it. Otherwise
13851 -- the old component's size may require run-time determination, but
13852 -- the new component's size still might be statically determinable
13853 -- (if, for example it has a static constraint). In that case we want
13854 -- Layout_Type to recompute the component's size, so we reset its
13855 -- size and positional fields.
13857 if Frontend_Layout_On_Target
13858 and then not Known_Static_Esize (Old_Compon)
13859 then
13860 Set_Esize (New_Compon, Uint_0);
13861 Init_Normalized_First_Bit (New_Compon);
13862 Init_Normalized_Position (New_Compon);
13863 Init_Normalized_Position_Max (New_Compon);
13864 end if;
13866 -- We do not want this node marked as Comes_From_Source, since
13867 -- otherwise it would get first class status and a separate cross-
13868 -- reference line would be generated. Illegitimate children do not
13869 -- rate such recognition.
13871 Set_Comes_From_Source (New_Compon, False);
13873 -- But it is a real entity, and a birth certificate must be properly
13874 -- registered by entering it into the entity list.
13876 Enter_Name (New_Compon);
13878 return New_Compon;
13879 end Create_Component;
13881 -----------------------
13882 -- Is_Variant_Record --
13883 -----------------------
13885 function Is_Variant_Record (T : Entity_Id) return Boolean is
13886 begin
13887 return Nkind (Parent (T)) = N_Full_Type_Declaration
13888 and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
13889 and then Present (Component_List (Type_Definition (Parent (T))))
13890 and then
13891 Present
13892 (Variant_Part (Component_List (Type_Definition (Parent (T)))));
13893 end Is_Variant_Record;
13895 -- Start of processing for Create_Constrained_Components
13897 begin
13898 pragma Assert (Subt /= Base_Type (Subt));
13899 pragma Assert (Typ = Base_Type (Typ));
13901 Set_First_Entity (Subt, Empty);
13902 Set_Last_Entity (Subt, Empty);
13904 -- Check whether constraint is fully static, in which case we can
13905 -- optimize the list of components.
13907 Discr_Val := First_Elmt (Constraints);
13908 while Present (Discr_Val) loop
13909 if not Is_OK_Static_Expression (Node (Discr_Val)) then
13910 Is_Static := False;
13911 exit;
13912 end if;
13914 Next_Elmt (Discr_Val);
13915 end loop;
13917 Set_Has_Static_Discriminants (Subt, Is_Static);
13919 Push_Scope (Subt);
13921 -- Inherit the discriminants of the parent type
13923 Add_Discriminants : declare
13924 Num_Disc : Int;
13925 Num_Gird : Int;
13927 begin
13928 Num_Disc := 0;
13929 Old_C := First_Discriminant (Typ);
13931 while Present (Old_C) loop
13932 Num_Disc := Num_Disc + 1;
13933 New_C := Create_Component (Old_C);
13934 Set_Is_Public (New_C, Is_Public (Subt));
13935 Next_Discriminant (Old_C);
13936 end loop;
13938 -- For an untagged derived subtype, the number of discriminants may
13939 -- be smaller than the number of inherited discriminants, because
13940 -- several of them may be renamed by a single new discriminant or
13941 -- constrained. In this case, add the hidden discriminants back into
13942 -- the subtype, because they need to be present if the optimizer of
13943 -- the GCC 4.x back-end decides to break apart assignments between
13944 -- objects using the parent view into member-wise assignments.
13946 Num_Gird := 0;
13948 if Is_Derived_Type (Typ)
13949 and then not Is_Tagged_Type (Typ)
13950 then
13951 Old_C := First_Stored_Discriminant (Typ);
13953 while Present (Old_C) loop
13954 Num_Gird := Num_Gird + 1;
13955 Next_Stored_Discriminant (Old_C);
13956 end loop;
13957 end if;
13959 if Num_Gird > Num_Disc then
13961 -- Find out multiple uses of new discriminants, and add hidden
13962 -- components for the extra renamed discriminants. We recognize
13963 -- multiple uses through the Corresponding_Discriminant of a
13964 -- new discriminant: if it constrains several old discriminants,
13965 -- this field points to the last one in the parent type. The
13966 -- stored discriminants of the derived type have the same name
13967 -- as those of the parent.
13969 declare
13970 Constr : Elmt_Id;
13971 New_Discr : Entity_Id;
13972 Old_Discr : Entity_Id;
13974 begin
13975 Constr := First_Elmt (Stored_Constraint (Typ));
13976 Old_Discr := First_Stored_Discriminant (Typ);
13977 while Present (Constr) loop
13978 if Is_Entity_Name (Node (Constr))
13979 and then Ekind (Entity (Node (Constr))) = E_Discriminant
13980 then
13981 New_Discr := Entity (Node (Constr));
13983 if Chars (Corresponding_Discriminant (New_Discr)) /=
13984 Chars (Old_Discr)
13985 then
13986 -- The new discriminant has been used to rename a
13987 -- subsequent old discriminant. Introduce a shadow
13988 -- component for the current old discriminant.
13990 New_C := Create_Component (Old_Discr);
13991 Set_Original_Record_Component (New_C, Old_Discr);
13992 end if;
13994 else
13995 -- The constraint has eliminated the old discriminant.
13996 -- Introduce a shadow component.
13998 New_C := Create_Component (Old_Discr);
13999 Set_Original_Record_Component (New_C, Old_Discr);
14000 end if;
14002 Next_Elmt (Constr);
14003 Next_Stored_Discriminant (Old_Discr);
14004 end loop;
14005 end;
14006 end if;
14007 end Add_Discriminants;
14009 if Is_Static
14010 and then Is_Variant_Record (Typ)
14011 then
14012 Collect_Fixed_Components (Typ);
14014 Gather_Components (
14015 Typ,
14016 Component_List (Type_Definition (Parent (Typ))),
14017 Governed_By => Assoc_List,
14018 Into => Comp_List,
14019 Report_Errors => Errors);
14020 pragma Assert (not Errors);
14022 Create_All_Components;
14024 -- If the subtype declaration is created for a tagged type derivation
14025 -- with constraints, we retrieve the record definition of the parent
14026 -- type to select the components of the proper variant.
14028 elsif Is_Static
14029 and then Is_Tagged_Type (Typ)
14030 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
14031 and then
14032 Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
14033 and then Is_Variant_Record (Parent_Type)
14034 then
14035 Collect_Fixed_Components (Typ);
14037 Gather_Components
14038 (Typ,
14039 Component_List (Type_Definition (Parent (Parent_Type))),
14040 Governed_By => Assoc_List,
14041 Into => Comp_List,
14042 Report_Errors => Errors);
14044 -- Note: previously there was a check at this point that no errors
14045 -- were detected. As a consequence of AI05-220 there may be an error
14046 -- if an inherited discriminant that controls a variant has a non-
14047 -- static constraint.
14049 -- If the tagged derivation has a type extension, collect all the
14050 -- new components therein.
14052 if Present (Record_Extension_Part (Type_Definition (Parent (Typ))))
14053 then
14054 Old_C := First_Component (Typ);
14055 while Present (Old_C) loop
14056 if Original_Record_Component (Old_C) = Old_C
14057 and then Chars (Old_C) /= Name_uTag
14058 and then Chars (Old_C) /= Name_uParent
14059 then
14060 Append_Elmt (Old_C, Comp_List);
14061 end if;
14063 Next_Component (Old_C);
14064 end loop;
14065 end if;
14067 Create_All_Components;
14069 else
14070 -- If discriminants are not static, or if this is a multi-level type
14071 -- extension, we have to include all components of the parent type.
14073 Old_C := First_Component (Typ);
14074 while Present (Old_C) loop
14075 New_C := Create_Component (Old_C);
14077 Set_Etype
14078 (New_C,
14079 Constrain_Component_Type
14080 (Old_C, Subt, Decl_Node, Typ, Constraints));
14081 Set_Is_Public (New_C, Is_Public (Subt));
14083 Next_Component (Old_C);
14084 end loop;
14085 end if;
14087 End_Scope;
14088 end Create_Constrained_Components;
14090 ------------------------------------------
14091 -- Decimal_Fixed_Point_Type_Declaration --
14092 ------------------------------------------
14094 procedure Decimal_Fixed_Point_Type_Declaration
14095 (T : Entity_Id;
14096 Def : Node_Id)
14098 Loc : constant Source_Ptr := Sloc (Def);
14099 Digs_Expr : constant Node_Id := Digits_Expression (Def);
14100 Delta_Expr : constant Node_Id := Delta_Expression (Def);
14101 Implicit_Base : Entity_Id;
14102 Digs_Val : Uint;
14103 Delta_Val : Ureal;
14104 Scale_Val : Uint;
14105 Bound_Val : Ureal;
14107 begin
14108 Check_SPARK_05_Restriction
14109 ("decimal fixed point type is not allowed", Def);
14110 Check_Restriction (No_Fixed_Point, Def);
14112 -- Create implicit base type
14114 Implicit_Base :=
14115 Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
14116 Set_Etype (Implicit_Base, Implicit_Base);
14118 -- Analyze and process delta expression
14120 Analyze_And_Resolve (Delta_Expr, Universal_Real);
14122 Check_Delta_Expression (Delta_Expr);
14123 Delta_Val := Expr_Value_R (Delta_Expr);
14125 -- Check delta is power of 10, and determine scale value from it
14127 declare
14128 Val : Ureal;
14130 begin
14131 Scale_Val := Uint_0;
14132 Val := Delta_Val;
14134 if Val < Ureal_1 then
14135 while Val < Ureal_1 loop
14136 Val := Val * Ureal_10;
14137 Scale_Val := Scale_Val + 1;
14138 end loop;
14140 if Scale_Val > 18 then
14141 Error_Msg_N ("scale exceeds maximum value of 18", Def);
14142 Scale_Val := UI_From_Int (+18);
14143 end if;
14145 else
14146 while Val > Ureal_1 loop
14147 Val := Val / Ureal_10;
14148 Scale_Val := Scale_Val - 1;
14149 end loop;
14151 if Scale_Val < -18 then
14152 Error_Msg_N ("scale is less than minimum value of -18", Def);
14153 Scale_Val := UI_From_Int (-18);
14154 end if;
14155 end if;
14157 if Val /= Ureal_1 then
14158 Error_Msg_N ("delta expression must be a power of 10", Def);
14159 Delta_Val := Ureal_10 ** (-Scale_Val);
14160 end if;
14161 end;
14163 -- Set delta, scale and small (small = delta for decimal type)
14165 Set_Delta_Value (Implicit_Base, Delta_Val);
14166 Set_Scale_Value (Implicit_Base, Scale_Val);
14167 Set_Small_Value (Implicit_Base, Delta_Val);
14169 -- Analyze and process digits expression
14171 Analyze_And_Resolve (Digs_Expr, Any_Integer);
14172 Check_Digits_Expression (Digs_Expr);
14173 Digs_Val := Expr_Value (Digs_Expr);
14175 if Digs_Val > 18 then
14176 Digs_Val := UI_From_Int (+18);
14177 Error_Msg_N ("digits value out of range, maximum is 18", Digs_Expr);
14178 end if;
14180 Set_Digits_Value (Implicit_Base, Digs_Val);
14181 Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
14183 -- Set range of base type from digits value for now. This will be
14184 -- expanded to represent the true underlying base range by Freeze.
14186 Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
14188 -- Note: We leave size as zero for now, size will be set at freeze
14189 -- time. We have to do this for ordinary fixed-point, because the size
14190 -- depends on the specified small, and we might as well do the same for
14191 -- decimal fixed-point.
14193 pragma Assert (Esize (Implicit_Base) = Uint_0);
14195 -- If there are bounds given in the declaration use them as the
14196 -- bounds of the first named subtype.
14198 if Present (Real_Range_Specification (Def)) then
14199 declare
14200 RRS : constant Node_Id := Real_Range_Specification (Def);
14201 Low : constant Node_Id := Low_Bound (RRS);
14202 High : constant Node_Id := High_Bound (RRS);
14203 Low_Val : Ureal;
14204 High_Val : Ureal;
14206 begin
14207 Analyze_And_Resolve (Low, Any_Real);
14208 Analyze_And_Resolve (High, Any_Real);
14209 Check_Real_Bound (Low);
14210 Check_Real_Bound (High);
14211 Low_Val := Expr_Value_R (Low);
14212 High_Val := Expr_Value_R (High);
14214 if Low_Val < (-Bound_Val) then
14215 Error_Msg_N
14216 ("range low bound too small for digits value", Low);
14217 Low_Val := -Bound_Val;
14218 end if;
14220 if High_Val > Bound_Val then
14221 Error_Msg_N
14222 ("range high bound too large for digits value", High);
14223 High_Val := Bound_Val;
14224 end if;
14226 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
14227 end;
14229 -- If no explicit range, use range that corresponds to given
14230 -- digits value. This will end up as the final range for the
14231 -- first subtype.
14233 else
14234 Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
14235 end if;
14237 -- Complete entity for first subtype. The inheritance of the rep item
14238 -- chain ensures that SPARK-related pragmas are not clobbered when the
14239 -- decimal fixed point type acts as a full view of a private type.
14241 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
14242 Set_Etype (T, Implicit_Base);
14243 Set_Size_Info (T, Implicit_Base);
14244 Inherit_Rep_Item_Chain (T, Implicit_Base);
14245 Set_Digits_Value (T, Digs_Val);
14246 Set_Delta_Value (T, Delta_Val);
14247 Set_Small_Value (T, Delta_Val);
14248 Set_Scale_Value (T, Scale_Val);
14249 Set_Is_Constrained (T);
14250 end Decimal_Fixed_Point_Type_Declaration;
14252 -----------------------------------
14253 -- Derive_Progenitor_Subprograms --
14254 -----------------------------------
14256 procedure Derive_Progenitor_Subprograms
14257 (Parent_Type : Entity_Id;
14258 Tagged_Type : Entity_Id)
14260 E : Entity_Id;
14261 Elmt : Elmt_Id;
14262 Iface : Entity_Id;
14263 Iface_Elmt : Elmt_Id;
14264 Iface_Subp : Entity_Id;
14265 New_Subp : Entity_Id := Empty;
14266 Prim_Elmt : Elmt_Id;
14267 Subp : Entity_Id;
14268 Typ : Entity_Id;
14270 begin
14271 pragma Assert (Ada_Version >= Ada_2005
14272 and then Is_Record_Type (Tagged_Type)
14273 and then Is_Tagged_Type (Tagged_Type)
14274 and then Has_Interfaces (Tagged_Type));
14276 -- Step 1: Transfer to the full-view primitives associated with the
14277 -- partial-view that cover interface primitives. Conceptually this
14278 -- work should be done later by Process_Full_View; done here to
14279 -- simplify its implementation at later stages. It can be safely
14280 -- done here because interfaces must be visible in the partial and
14281 -- private view (RM 7.3(7.3/2)).
14283 -- Small optimization: This work is only required if the parent may
14284 -- have entities whose Alias attribute reference an interface primitive.
14285 -- Such a situation may occur if the parent is an abstract type and the
14286 -- primitive has not been yet overridden or if the parent is a generic
14287 -- formal type covering interfaces.
14289 -- If the tagged type is not abstract, it cannot have abstract
14290 -- primitives (the only entities in the list of primitives of
14291 -- non-abstract tagged types that can reference abstract primitives
14292 -- through its Alias attribute are the internal entities that have
14293 -- attribute Interface_Alias, and these entities are generated later
14294 -- by Add_Internal_Interface_Entities).
14296 if In_Private_Part (Current_Scope)
14297 and then (Is_Abstract_Type (Parent_Type)
14298 or else
14299 Is_Generic_Type (Parent_Type))
14300 then
14301 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
14302 while Present (Elmt) loop
14303 Subp := Node (Elmt);
14305 -- At this stage it is not possible to have entities in the list
14306 -- of primitives that have attribute Interface_Alias.
14308 pragma Assert (No (Interface_Alias (Subp)));
14310 Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
14312 if Is_Interface (Typ) then
14313 E := Find_Primitive_Covering_Interface
14314 (Tagged_Type => Tagged_Type,
14315 Iface_Prim => Subp);
14317 if Present (E)
14318 and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
14319 then
14320 Replace_Elmt (Elmt, E);
14321 Remove_Homonym (Subp);
14322 end if;
14323 end if;
14325 Next_Elmt (Elmt);
14326 end loop;
14327 end if;
14329 -- Step 2: Add primitives of progenitors that are not implemented by
14330 -- parents of Tagged_Type.
14332 if Present (Interfaces (Base_Type (Tagged_Type))) then
14333 Iface_Elmt := First_Elmt (Interfaces (Base_Type (Tagged_Type)));
14334 while Present (Iface_Elmt) loop
14335 Iface := Node (Iface_Elmt);
14337 Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
14338 while Present (Prim_Elmt) loop
14339 Iface_Subp := Node (Prim_Elmt);
14341 -- Exclude derivation of predefined primitives except those
14342 -- that come from source, or are inherited from one that comes
14343 -- from source. Required to catch declarations of equality
14344 -- operators of interfaces. For example:
14346 -- type Iface is interface;
14347 -- function "=" (Left, Right : Iface) return Boolean;
14349 if not Is_Predefined_Dispatching_Operation (Iface_Subp)
14350 or else Comes_From_Source (Ultimate_Alias (Iface_Subp))
14351 then
14352 E := Find_Primitive_Covering_Interface
14353 (Tagged_Type => Tagged_Type,
14354 Iface_Prim => Iface_Subp);
14356 -- If not found we derive a new primitive leaving its alias
14357 -- attribute referencing the interface primitive.
14359 if No (E) then
14360 Derive_Subprogram
14361 (New_Subp, Iface_Subp, Tagged_Type, Iface);
14363 -- Ada 2012 (AI05-0197): If the covering primitive's name
14364 -- differs from the name of the interface primitive then it
14365 -- is a private primitive inherited from a parent type. In
14366 -- such case, given that Tagged_Type covers the interface,
14367 -- the inherited private primitive becomes visible. For such
14368 -- purpose we add a new entity that renames the inherited
14369 -- private primitive.
14371 elsif Chars (E) /= Chars (Iface_Subp) then
14372 pragma Assert (Has_Suffix (E, 'P'));
14373 Derive_Subprogram
14374 (New_Subp, Iface_Subp, Tagged_Type, Iface);
14375 Set_Alias (New_Subp, E);
14376 Set_Is_Abstract_Subprogram (New_Subp,
14377 Is_Abstract_Subprogram (E));
14379 -- Propagate to the full view interface entities associated
14380 -- with the partial view.
14382 elsif In_Private_Part (Current_Scope)
14383 and then Present (Alias (E))
14384 and then Alias (E) = Iface_Subp
14385 and then
14386 List_Containing (Parent (E)) /=
14387 Private_Declarations
14388 (Specification
14389 (Unit_Declaration_Node (Current_Scope)))
14390 then
14391 Append_Elmt (E, Primitive_Operations (Tagged_Type));
14392 end if;
14393 end if;
14395 Next_Elmt (Prim_Elmt);
14396 end loop;
14398 Next_Elmt (Iface_Elmt);
14399 end loop;
14400 end if;
14401 end Derive_Progenitor_Subprograms;
14403 -----------------------
14404 -- Derive_Subprogram --
14405 -----------------------
14407 procedure Derive_Subprogram
14408 (New_Subp : in out Entity_Id;
14409 Parent_Subp : Entity_Id;
14410 Derived_Type : Entity_Id;
14411 Parent_Type : Entity_Id;
14412 Actual_Subp : Entity_Id := Empty)
14414 Formal : Entity_Id;
14415 -- Formal parameter of parent primitive operation
14417 Formal_Of_Actual : Entity_Id;
14418 -- Formal parameter of actual operation, when the derivation is to
14419 -- create a renaming for a primitive operation of an actual in an
14420 -- instantiation.
14422 New_Formal : Entity_Id;
14423 -- Formal of inherited operation
14425 Visible_Subp : Entity_Id := Parent_Subp;
14427 function Is_Private_Overriding return Boolean;
14428 -- If Subp is a private overriding of a visible operation, the inherited
14429 -- operation derives from the overridden op (even though its body is the
14430 -- overriding one) and the inherited operation is visible now. See
14431 -- sem_disp to see the full details of the handling of the overridden
14432 -- subprogram, which is removed from the list of primitive operations of
14433 -- the type. The overridden subprogram is saved locally in Visible_Subp,
14434 -- and used to diagnose abstract operations that need overriding in the
14435 -- derived type.
14437 procedure Replace_Type (Id, New_Id : Entity_Id);
14438 -- When the type is an anonymous access type, create a new access type
14439 -- designating the derived type.
14441 procedure Set_Derived_Name;
14442 -- This procedure sets the appropriate Chars name for New_Subp. This
14443 -- is normally just a copy of the parent name. An exception arises for
14444 -- type support subprograms, where the name is changed to reflect the
14445 -- name of the derived type, e.g. if type foo is derived from type bar,
14446 -- then a procedure barDA is derived with a name fooDA.
14448 ---------------------------
14449 -- Is_Private_Overriding --
14450 ---------------------------
14452 function Is_Private_Overriding return Boolean is
14453 Prev : Entity_Id;
14455 begin
14456 -- If the parent is not a dispatching operation there is no
14457 -- need to investigate overridings
14459 if not Is_Dispatching_Operation (Parent_Subp) then
14460 return False;
14461 end if;
14463 -- The visible operation that is overridden is a homonym of the
14464 -- parent subprogram. We scan the homonym chain to find the one
14465 -- whose alias is the subprogram we are deriving.
14467 Prev := Current_Entity (Parent_Subp);
14468 while Present (Prev) loop
14469 if Ekind (Prev) = Ekind (Parent_Subp)
14470 and then Alias (Prev) = Parent_Subp
14471 and then Scope (Parent_Subp) = Scope (Prev)
14472 and then not Is_Hidden (Prev)
14473 then
14474 Visible_Subp := Prev;
14475 return True;
14476 end if;
14478 Prev := Homonym (Prev);
14479 end loop;
14481 return False;
14482 end Is_Private_Overriding;
14484 ------------------
14485 -- Replace_Type --
14486 ------------------
14488 procedure Replace_Type (Id, New_Id : Entity_Id) is
14489 Id_Type : constant Entity_Id := Etype (Id);
14490 Acc_Type : Entity_Id;
14491 Par : constant Node_Id := Parent (Derived_Type);
14493 begin
14494 -- When the type is an anonymous access type, create a new access
14495 -- type designating the derived type. This itype must be elaborated
14496 -- at the point of the derivation, not on subsequent calls that may
14497 -- be out of the proper scope for Gigi, so we insert a reference to
14498 -- it after the derivation.
14500 if Ekind (Id_Type) = E_Anonymous_Access_Type then
14501 declare
14502 Desig_Typ : Entity_Id := Designated_Type (Id_Type);
14504 begin
14505 if Ekind (Desig_Typ) = E_Record_Type_With_Private
14506 and then Present (Full_View (Desig_Typ))
14507 and then not Is_Private_Type (Parent_Type)
14508 then
14509 Desig_Typ := Full_View (Desig_Typ);
14510 end if;
14512 if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
14514 -- Ada 2005 (AI-251): Handle also derivations of abstract
14515 -- interface primitives.
14517 or else (Is_Interface (Desig_Typ)
14518 and then not Is_Class_Wide_Type (Desig_Typ))
14519 then
14520 Acc_Type := New_Copy (Id_Type);
14521 Set_Etype (Acc_Type, Acc_Type);
14522 Set_Scope (Acc_Type, New_Subp);
14524 -- Set size of anonymous access type. If we have an access
14525 -- to an unconstrained array, this is a fat pointer, so it
14526 -- is sizes at twice addtress size.
14528 if Is_Array_Type (Desig_Typ)
14529 and then not Is_Constrained (Desig_Typ)
14530 then
14531 Init_Size (Acc_Type, 2 * System_Address_Size);
14533 -- Other cases use a thin pointer
14535 else
14536 Init_Size (Acc_Type, System_Address_Size);
14537 end if;
14539 -- Set remaining characterstics of anonymous access type
14541 Init_Alignment (Acc_Type);
14542 Set_Directly_Designated_Type (Acc_Type, Derived_Type);
14544 Set_Etype (New_Id, Acc_Type);
14545 Set_Scope (New_Id, New_Subp);
14547 -- Create a reference to it
14549 Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
14551 else
14552 Set_Etype (New_Id, Id_Type);
14553 end if;
14554 end;
14556 -- In Ada2012, a formal may have an incomplete type but the type
14557 -- derivation that inherits the primitive follows the full view.
14559 elsif Base_Type (Id_Type) = Base_Type (Parent_Type)
14560 or else
14561 (Ekind (Id_Type) = E_Record_Type_With_Private
14562 and then Present (Full_View (Id_Type))
14563 and then
14564 Base_Type (Full_View (Id_Type)) = Base_Type (Parent_Type))
14565 or else
14566 (Ada_Version >= Ada_2012
14567 and then Ekind (Id_Type) = E_Incomplete_Type
14568 and then Full_View (Id_Type) = Parent_Type)
14569 then
14570 -- Constraint checks on formals are generated during expansion,
14571 -- based on the signature of the original subprogram. The bounds
14572 -- of the derived type are not relevant, and thus we can use
14573 -- the base type for the formals. However, the return type may be
14574 -- used in a context that requires that the proper static bounds
14575 -- be used (a case statement, for example) and for those cases
14576 -- we must use the derived type (first subtype), not its base.
14578 -- If the derived_type_definition has no constraints, we know that
14579 -- the derived type has the same constraints as the first subtype
14580 -- of the parent, and we can also use it rather than its base,
14581 -- which can lead to more efficient code.
14583 if Etype (Id) = Parent_Type then
14584 if Is_Scalar_Type (Parent_Type)
14585 and then
14586 Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
14587 then
14588 Set_Etype (New_Id, Derived_Type);
14590 elsif Nkind (Par) = N_Full_Type_Declaration
14591 and then
14592 Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
14593 and then
14594 Is_Entity_Name
14595 (Subtype_Indication (Type_Definition (Par)))
14596 then
14597 Set_Etype (New_Id, Derived_Type);
14599 else
14600 Set_Etype (New_Id, Base_Type (Derived_Type));
14601 end if;
14603 else
14604 Set_Etype (New_Id, Base_Type (Derived_Type));
14605 end if;
14607 else
14608 Set_Etype (New_Id, Etype (Id));
14609 end if;
14610 end Replace_Type;
14612 ----------------------
14613 -- Set_Derived_Name --
14614 ----------------------
14616 procedure Set_Derived_Name is
14617 Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
14618 begin
14619 if Nm = TSS_Null then
14620 Set_Chars (New_Subp, Chars (Parent_Subp));
14621 else
14622 Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
14623 end if;
14624 end Set_Derived_Name;
14626 -- Start of processing for Derive_Subprogram
14628 begin
14629 New_Subp := New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
14630 Set_Ekind (New_Subp, Ekind (Parent_Subp));
14632 -- Check whether the inherited subprogram is a private operation that
14633 -- should be inherited but not yet made visible. Such subprograms can
14634 -- become visible at a later point (e.g., the private part of a public
14635 -- child unit) via Declare_Inherited_Private_Subprograms. If the
14636 -- following predicate is true, then this is not such a private
14637 -- operation and the subprogram simply inherits the name of the parent
14638 -- subprogram. Note the special check for the names of controlled
14639 -- operations, which are currently exempted from being inherited with
14640 -- a hidden name because they must be findable for generation of
14641 -- implicit run-time calls.
14643 if not Is_Hidden (Parent_Subp)
14644 or else Is_Internal (Parent_Subp)
14645 or else Is_Private_Overriding
14646 or else Is_Internal_Name (Chars (Parent_Subp))
14647 or else Nam_In (Chars (Parent_Subp), Name_Initialize,
14648 Name_Adjust,
14649 Name_Finalize)
14650 then
14651 Set_Derived_Name;
14653 -- An inherited dispatching equality will be overridden by an internally
14654 -- generated one, or by an explicit one, so preserve its name and thus
14655 -- its entry in the dispatch table. Otherwise, if Parent_Subp is a
14656 -- private operation it may become invisible if the full view has
14657 -- progenitors, and the dispatch table will be malformed.
14658 -- We check that the type is limited to handle the anomalous declaration
14659 -- of Limited_Controlled, which is derived from a non-limited type, and
14660 -- which is handled specially elsewhere as well.
14662 elsif Chars (Parent_Subp) = Name_Op_Eq
14663 and then Is_Dispatching_Operation (Parent_Subp)
14664 and then Etype (Parent_Subp) = Standard_Boolean
14665 and then not Is_Limited_Type (Etype (First_Formal (Parent_Subp)))
14666 and then
14667 Etype (First_Formal (Parent_Subp)) =
14668 Etype (Next_Formal (First_Formal (Parent_Subp)))
14669 then
14670 Set_Derived_Name;
14672 -- If parent is hidden, this can be a regular derivation if the
14673 -- parent is immediately visible in a non-instantiating context,
14674 -- or if we are in the private part of an instance. This test
14675 -- should still be refined ???
14677 -- The test for In_Instance_Not_Visible avoids inheriting the derived
14678 -- operation as a non-visible operation in cases where the parent
14679 -- subprogram might not be visible now, but was visible within the
14680 -- original generic, so it would be wrong to make the inherited
14681 -- subprogram non-visible now. (Not clear if this test is fully
14682 -- correct; are there any cases where we should declare the inherited
14683 -- operation as not visible to avoid it being overridden, e.g., when
14684 -- the parent type is a generic actual with private primitives ???)
14686 -- (they should be treated the same as other private inherited
14687 -- subprograms, but it's not clear how to do this cleanly). ???
14689 elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
14690 and then Is_Immediately_Visible (Parent_Subp)
14691 and then not In_Instance)
14692 or else In_Instance_Not_Visible
14693 then
14694 Set_Derived_Name;
14696 -- Ada 2005 (AI-251): Regular derivation if the parent subprogram
14697 -- overrides an interface primitive because interface primitives
14698 -- must be visible in the partial view of the parent (RM 7.3 (7.3/2))
14700 elsif Ada_Version >= Ada_2005
14701 and then Is_Dispatching_Operation (Parent_Subp)
14702 and then Covers_Some_Interface (Parent_Subp)
14703 then
14704 Set_Derived_Name;
14706 -- Otherwise, the type is inheriting a private operation, so enter
14707 -- it with a special name so it can't be overridden.
14709 else
14710 Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
14711 end if;
14713 Set_Parent (New_Subp, Parent (Derived_Type));
14715 if Present (Actual_Subp) then
14716 Replace_Type (Actual_Subp, New_Subp);
14717 else
14718 Replace_Type (Parent_Subp, New_Subp);
14719 end if;
14721 Conditional_Delay (New_Subp, Parent_Subp);
14723 -- If we are creating a renaming for a primitive operation of an
14724 -- actual of a generic derived type, we must examine the signature
14725 -- of the actual primitive, not that of the generic formal, which for
14726 -- example may be an interface. However the name and initial value
14727 -- of the inherited operation are those of the formal primitive.
14729 Formal := First_Formal (Parent_Subp);
14731 if Present (Actual_Subp) then
14732 Formal_Of_Actual := First_Formal (Actual_Subp);
14733 else
14734 Formal_Of_Actual := Empty;
14735 end if;
14737 while Present (Formal) loop
14738 New_Formal := New_Copy (Formal);
14740 -- Normally we do not go copying parents, but in the case of
14741 -- formals, we need to link up to the declaration (which is the
14742 -- parameter specification), and it is fine to link up to the
14743 -- original formal's parameter specification in this case.
14745 Set_Parent (New_Formal, Parent (Formal));
14746 Append_Entity (New_Formal, New_Subp);
14748 if Present (Formal_Of_Actual) then
14749 Replace_Type (Formal_Of_Actual, New_Formal);
14750 Next_Formal (Formal_Of_Actual);
14751 else
14752 Replace_Type (Formal, New_Formal);
14753 end if;
14755 Next_Formal (Formal);
14756 end loop;
14758 -- If this derivation corresponds to a tagged generic actual, then
14759 -- primitive operations rename those of the actual. Otherwise the
14760 -- primitive operations rename those of the parent type, If the parent
14761 -- renames an intrinsic operator, so does the new subprogram. We except
14762 -- concatenation, which is always properly typed, and does not get
14763 -- expanded as other intrinsic operations.
14765 if No (Actual_Subp) then
14766 if Is_Intrinsic_Subprogram (Parent_Subp) then
14767 Set_Is_Intrinsic_Subprogram (New_Subp);
14769 if Present (Alias (Parent_Subp))
14770 and then Chars (Parent_Subp) /= Name_Op_Concat
14771 then
14772 Set_Alias (New_Subp, Alias (Parent_Subp));
14773 else
14774 Set_Alias (New_Subp, Parent_Subp);
14775 end if;
14777 else
14778 Set_Alias (New_Subp, Parent_Subp);
14779 end if;
14781 else
14782 Set_Alias (New_Subp, Actual_Subp);
14783 end if;
14785 -- Inherit the "ghostness" from the parent subprogram
14787 if Is_Ghost_Entity (Alias (New_Subp)) then
14788 Set_Is_Ghost_Entity (New_Subp);
14789 end if;
14791 -- Derived subprograms of a tagged type must inherit the convention
14792 -- of the parent subprogram (a requirement of AI-117). Derived
14793 -- subprograms of untagged types simply get convention Ada by default.
14795 -- If the derived type is a tagged generic formal type with unknown
14796 -- discriminants, its convention is intrinsic (RM 6.3.1 (8)).
14798 -- However, if the type is derived from a generic formal, the further
14799 -- inherited subprogram has the convention of the non-generic ancestor.
14800 -- Otherwise there would be no way to override the operation.
14801 -- (This is subject to forthcoming ARG discussions).
14803 if Is_Tagged_Type (Derived_Type) then
14804 if Is_Generic_Type (Derived_Type)
14805 and then Has_Unknown_Discriminants (Derived_Type)
14806 then
14807 Set_Convention (New_Subp, Convention_Intrinsic);
14809 else
14810 if Is_Generic_Type (Parent_Type)
14811 and then Has_Unknown_Discriminants (Parent_Type)
14812 then
14813 Set_Convention (New_Subp, Convention (Alias (Parent_Subp)));
14814 else
14815 Set_Convention (New_Subp, Convention (Parent_Subp));
14816 end if;
14817 end if;
14818 end if;
14820 -- Predefined controlled operations retain their name even if the parent
14821 -- is hidden (see above), but they are not primitive operations if the
14822 -- ancestor is not visible, for example if the parent is a private
14823 -- extension completed with a controlled extension. Note that a full
14824 -- type that is controlled can break privacy: the flag Is_Controlled is
14825 -- set on both views of the type.
14827 if Is_Controlled (Parent_Type)
14828 and then Nam_In (Chars (Parent_Subp), Name_Initialize,
14829 Name_Adjust,
14830 Name_Finalize)
14831 and then Is_Hidden (Parent_Subp)
14832 and then not Is_Visibly_Controlled (Parent_Type)
14833 then
14834 Set_Is_Hidden (New_Subp);
14835 end if;
14837 Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
14838 Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
14840 if Ekind (Parent_Subp) = E_Procedure then
14841 Set_Is_Valued_Procedure
14842 (New_Subp, Is_Valued_Procedure (Parent_Subp));
14843 else
14844 Set_Has_Controlling_Result
14845 (New_Subp, Has_Controlling_Result (Parent_Subp));
14846 end if;
14848 -- No_Return must be inherited properly. If this is overridden in the
14849 -- case of a dispatching operation, then a check is made in Sem_Disp
14850 -- that the overriding operation is also No_Return (no such check is
14851 -- required for the case of non-dispatching operation.
14853 Set_No_Return (New_Subp, No_Return (Parent_Subp));
14855 -- A derived function with a controlling result is abstract. If the
14856 -- Derived_Type is a nonabstract formal generic derived type, then
14857 -- inherited operations are not abstract: the required check is done at
14858 -- instantiation time. If the derivation is for a generic actual, the
14859 -- function is not abstract unless the actual is.
14861 if Is_Generic_Type (Derived_Type)
14862 and then not Is_Abstract_Type (Derived_Type)
14863 then
14864 null;
14866 -- Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
14867 -- properties of the subprogram, as defined in RM-3.9.3(4/2-6/2).
14869 -- A subprogram subject to pragma Extensions_Visible with value False
14870 -- requires overriding if the subprogram has at least one controlling
14871 -- OUT parameter (SPARK RM 6.1.7(6)).
14873 elsif Ada_Version >= Ada_2005
14874 and then (Is_Abstract_Subprogram (Alias (New_Subp))
14875 or else (Is_Tagged_Type (Derived_Type)
14876 and then Etype (New_Subp) = Derived_Type
14877 and then not Is_Null_Extension (Derived_Type))
14878 or else (Is_Tagged_Type (Derived_Type)
14879 and then Ekind (Etype (New_Subp)) =
14880 E_Anonymous_Access_Type
14881 and then Designated_Type (Etype (New_Subp)) =
14882 Derived_Type
14883 and then not Is_Null_Extension (Derived_Type))
14884 or else (Comes_From_Source (Alias (New_Subp))
14885 and then Is_EVF_Procedure (Alias (New_Subp))))
14886 and then No (Actual_Subp)
14887 then
14888 if not Is_Tagged_Type (Derived_Type)
14889 or else Is_Abstract_Type (Derived_Type)
14890 or else Is_Abstract_Subprogram (Alias (New_Subp))
14891 then
14892 Set_Is_Abstract_Subprogram (New_Subp);
14893 else
14894 Set_Requires_Overriding (New_Subp);
14895 end if;
14897 elsif Ada_Version < Ada_2005
14898 and then (Is_Abstract_Subprogram (Alias (New_Subp))
14899 or else (Is_Tagged_Type (Derived_Type)
14900 and then Etype (New_Subp) = Derived_Type
14901 and then No (Actual_Subp)))
14902 then
14903 Set_Is_Abstract_Subprogram (New_Subp);
14905 -- AI05-0097 : an inherited operation that dispatches on result is
14906 -- abstract if the derived type is abstract, even if the parent type
14907 -- is concrete and the derived type is a null extension.
14909 elsif Has_Controlling_Result (Alias (New_Subp))
14910 and then Is_Abstract_Type (Etype (New_Subp))
14911 then
14912 Set_Is_Abstract_Subprogram (New_Subp);
14914 -- Finally, if the parent type is abstract we must verify that all
14915 -- inherited operations are either non-abstract or overridden, or that
14916 -- the derived type itself is abstract (this check is performed at the
14917 -- end of a package declaration, in Check_Abstract_Overriding). A
14918 -- private overriding in the parent type will not be visible in the
14919 -- derivation if we are not in an inner package or in a child unit of
14920 -- the parent type, in which case the abstractness of the inherited
14921 -- operation is carried to the new subprogram.
14923 elsif Is_Abstract_Type (Parent_Type)
14924 and then not In_Open_Scopes (Scope (Parent_Type))
14925 and then Is_Private_Overriding
14926 and then Is_Abstract_Subprogram (Visible_Subp)
14927 then
14928 if No (Actual_Subp) then
14929 Set_Alias (New_Subp, Visible_Subp);
14930 Set_Is_Abstract_Subprogram (New_Subp, True);
14932 else
14933 -- If this is a derivation for an instance of a formal derived
14934 -- type, abstractness comes from the primitive operation of the
14935 -- actual, not from the operation inherited from the ancestor.
14937 Set_Is_Abstract_Subprogram
14938 (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
14939 end if;
14940 end if;
14942 New_Overloaded_Entity (New_Subp, Derived_Type);
14944 -- Check for case of a derived subprogram for the instantiation of a
14945 -- formal derived tagged type, if so mark the subprogram as dispatching
14946 -- and inherit the dispatching attributes of the actual subprogram. The
14947 -- derived subprogram is effectively renaming of the actual subprogram,
14948 -- so it needs to have the same attributes as the actual.
14950 if Present (Actual_Subp)
14951 and then Is_Dispatching_Operation (Actual_Subp)
14952 then
14953 Set_Is_Dispatching_Operation (New_Subp);
14955 if Present (DTC_Entity (Actual_Subp)) then
14956 Set_DTC_Entity (New_Subp, DTC_Entity (Actual_Subp));
14957 Set_DT_Position_Value (New_Subp, DT_Position (Actual_Subp));
14958 end if;
14959 end if;
14961 -- Indicate that a derived subprogram does not require a body and that
14962 -- it does not require processing of default expressions.
14964 Set_Has_Completion (New_Subp);
14965 Set_Default_Expressions_Processed (New_Subp);
14967 if Ekind (New_Subp) = E_Function then
14968 Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
14969 end if;
14970 end Derive_Subprogram;
14972 ------------------------
14973 -- Derive_Subprograms --
14974 ------------------------
14976 procedure Derive_Subprograms
14977 (Parent_Type : Entity_Id;
14978 Derived_Type : Entity_Id;
14979 Generic_Actual : Entity_Id := Empty)
14981 Op_List : constant Elist_Id :=
14982 Collect_Primitive_Operations (Parent_Type);
14984 function Check_Derived_Type return Boolean;
14985 -- Check that all the entities derived from Parent_Type are found in
14986 -- the list of primitives of Derived_Type exactly in the same order.
14988 procedure Derive_Interface_Subprogram
14989 (New_Subp : in out Entity_Id;
14990 Subp : Entity_Id;
14991 Actual_Subp : Entity_Id);
14992 -- Derive New_Subp from the ultimate alias of the parent subprogram Subp
14993 -- (which is an interface primitive). If Generic_Actual is present then
14994 -- Actual_Subp is the actual subprogram corresponding with the generic
14995 -- subprogram Subp.
14997 function Check_Derived_Type return Boolean is
14998 E : Entity_Id;
14999 Elmt : Elmt_Id;
15000 List : Elist_Id;
15001 New_Subp : Entity_Id;
15002 Op_Elmt : Elmt_Id;
15003 Subp : Entity_Id;
15005 begin
15006 -- Traverse list of entities in the current scope searching for
15007 -- an incomplete type whose full-view is derived type
15009 E := First_Entity (Scope (Derived_Type));
15010 while Present (E) and then E /= Derived_Type loop
15011 if Ekind (E) = E_Incomplete_Type
15012 and then Present (Full_View (E))
15013 and then Full_View (E) = Derived_Type
15014 then
15015 -- Disable this test if Derived_Type completes an incomplete
15016 -- type because in such case more primitives can be added
15017 -- later to the list of primitives of Derived_Type by routine
15018 -- Process_Incomplete_Dependents
15020 return True;
15021 end if;
15023 E := Next_Entity (E);
15024 end loop;
15026 List := Collect_Primitive_Operations (Derived_Type);
15027 Elmt := First_Elmt (List);
15029 Op_Elmt := First_Elmt (Op_List);
15030 while Present (Op_Elmt) loop
15031 Subp := Node (Op_Elmt);
15032 New_Subp := Node (Elmt);
15034 -- At this early stage Derived_Type has no entities with attribute
15035 -- Interface_Alias. In addition, such primitives are always
15036 -- located at the end of the list of primitives of Parent_Type.
15037 -- Therefore, if found we can safely stop processing pending
15038 -- entities.
15040 exit when Present (Interface_Alias (Subp));
15042 -- Handle hidden entities
15044 if not Is_Predefined_Dispatching_Operation (Subp)
15045 and then Is_Hidden (Subp)
15046 then
15047 if Present (New_Subp)
15048 and then Primitive_Names_Match (Subp, New_Subp)
15049 then
15050 Next_Elmt (Elmt);
15051 end if;
15053 else
15054 if not Present (New_Subp)
15055 or else Ekind (Subp) /= Ekind (New_Subp)
15056 or else not Primitive_Names_Match (Subp, New_Subp)
15057 then
15058 return False;
15059 end if;
15061 Next_Elmt (Elmt);
15062 end if;
15064 Next_Elmt (Op_Elmt);
15065 end loop;
15067 return True;
15068 end Check_Derived_Type;
15070 ---------------------------------
15071 -- Derive_Interface_Subprogram --
15072 ---------------------------------
15074 procedure Derive_Interface_Subprogram
15075 (New_Subp : in out Entity_Id;
15076 Subp : Entity_Id;
15077 Actual_Subp : Entity_Id)
15079 Iface_Subp : constant Entity_Id := Ultimate_Alias (Subp);
15080 Iface_Type : constant Entity_Id := Find_Dispatching_Type (Iface_Subp);
15082 begin
15083 pragma Assert (Is_Interface (Iface_Type));
15085 Derive_Subprogram
15086 (New_Subp => New_Subp,
15087 Parent_Subp => Iface_Subp,
15088 Derived_Type => Derived_Type,
15089 Parent_Type => Iface_Type,
15090 Actual_Subp => Actual_Subp);
15092 -- Given that this new interface entity corresponds with a primitive
15093 -- of the parent that was not overridden we must leave it associated
15094 -- with its parent primitive to ensure that it will share the same
15095 -- dispatch table slot when overridden.
15097 if No (Actual_Subp) then
15098 Set_Alias (New_Subp, Subp);
15100 -- For instantiations this is not needed since the previous call to
15101 -- Derive_Subprogram leaves the entity well decorated.
15103 else
15104 pragma Assert (Alias (New_Subp) = Actual_Subp);
15105 null;
15106 end if;
15107 end Derive_Interface_Subprogram;
15109 -- Local variables
15111 Alias_Subp : Entity_Id;
15112 Act_List : Elist_Id;
15113 Act_Elmt : Elmt_Id;
15114 Act_Subp : Entity_Id := Empty;
15115 Elmt : Elmt_Id;
15116 Need_Search : Boolean := False;
15117 New_Subp : Entity_Id := Empty;
15118 Parent_Base : Entity_Id;
15119 Subp : Entity_Id;
15121 -- Start of processing for Derive_Subprograms
15123 begin
15124 if Ekind (Parent_Type) = E_Record_Type_With_Private
15125 and then Has_Discriminants (Parent_Type)
15126 and then Present (Full_View (Parent_Type))
15127 then
15128 Parent_Base := Full_View (Parent_Type);
15129 else
15130 Parent_Base := Parent_Type;
15131 end if;
15133 if Present (Generic_Actual) then
15134 Act_List := Collect_Primitive_Operations (Generic_Actual);
15135 Act_Elmt := First_Elmt (Act_List);
15136 else
15137 Act_List := No_Elist;
15138 Act_Elmt := No_Elmt;
15139 end if;
15141 -- Derive primitives inherited from the parent. Note that if the generic
15142 -- actual is present, this is not really a type derivation, it is a
15143 -- completion within an instance.
15145 -- Case 1: Derived_Type does not implement interfaces
15147 if not Is_Tagged_Type (Derived_Type)
15148 or else (not Has_Interfaces (Derived_Type)
15149 and then not (Present (Generic_Actual)
15150 and then Has_Interfaces (Generic_Actual)))
15151 then
15152 Elmt := First_Elmt (Op_List);
15153 while Present (Elmt) loop
15154 Subp := Node (Elmt);
15156 -- Literals are derived earlier in the process of building the
15157 -- derived type, and are skipped here.
15159 if Ekind (Subp) = E_Enumeration_Literal then
15160 null;
15162 -- The actual is a direct descendant and the common primitive
15163 -- operations appear in the same order.
15165 -- If the generic parent type is present, the derived type is an
15166 -- instance of a formal derived type, and within the instance its
15167 -- operations are those of the actual. We derive from the formal
15168 -- type but make the inherited operations aliases of the
15169 -- corresponding operations of the actual.
15171 else
15172 pragma Assert (No (Node (Act_Elmt))
15173 or else (Primitive_Names_Match (Subp, Node (Act_Elmt))
15174 and then
15175 Type_Conformant
15176 (Subp, Node (Act_Elmt),
15177 Skip_Controlling_Formals => True)));
15179 Derive_Subprogram
15180 (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
15182 if Present (Act_Elmt) then
15183 Next_Elmt (Act_Elmt);
15184 end if;
15185 end if;
15187 Next_Elmt (Elmt);
15188 end loop;
15190 -- Case 2: Derived_Type implements interfaces
15192 else
15193 -- If the parent type has no predefined primitives we remove
15194 -- predefined primitives from the list of primitives of generic
15195 -- actual to simplify the complexity of this algorithm.
15197 if Present (Generic_Actual) then
15198 declare
15199 Has_Predefined_Primitives : Boolean := False;
15201 begin
15202 -- Check if the parent type has predefined primitives
15204 Elmt := First_Elmt (Op_List);
15205 while Present (Elmt) loop
15206 Subp := Node (Elmt);
15208 if Is_Predefined_Dispatching_Operation (Subp)
15209 and then not Comes_From_Source (Ultimate_Alias (Subp))
15210 then
15211 Has_Predefined_Primitives := True;
15212 exit;
15213 end if;
15215 Next_Elmt (Elmt);
15216 end loop;
15218 -- Remove predefined primitives of Generic_Actual. We must use
15219 -- an auxiliary list because in case of tagged types the value
15220 -- returned by Collect_Primitive_Operations is the value stored
15221 -- in its Primitive_Operations attribute (and we don't want to
15222 -- modify its current contents).
15224 if not Has_Predefined_Primitives then
15225 declare
15226 Aux_List : constant Elist_Id := New_Elmt_List;
15228 begin
15229 Elmt := First_Elmt (Act_List);
15230 while Present (Elmt) loop
15231 Subp := Node (Elmt);
15233 if not Is_Predefined_Dispatching_Operation (Subp)
15234 or else Comes_From_Source (Subp)
15235 then
15236 Append_Elmt (Subp, Aux_List);
15237 end if;
15239 Next_Elmt (Elmt);
15240 end loop;
15242 Act_List := Aux_List;
15243 end;
15244 end if;
15246 Act_Elmt := First_Elmt (Act_List);
15247 Act_Subp := Node (Act_Elmt);
15248 end;
15249 end if;
15251 -- Stage 1: If the generic actual is not present we derive the
15252 -- primitives inherited from the parent type. If the generic parent
15253 -- type is present, the derived type is an instance of a formal
15254 -- derived type, and within the instance its operations are those of
15255 -- the actual. We derive from the formal type but make the inherited
15256 -- operations aliases of the corresponding operations of the actual.
15258 Elmt := First_Elmt (Op_List);
15259 while Present (Elmt) loop
15260 Subp := Node (Elmt);
15261 Alias_Subp := Ultimate_Alias (Subp);
15263 -- Do not derive internal entities of the parent that link
15264 -- interface primitives with their covering primitive. These
15265 -- entities will be added to this type when frozen.
15267 if Present (Interface_Alias (Subp)) then
15268 goto Continue;
15269 end if;
15271 -- If the generic actual is present find the corresponding
15272 -- operation in the generic actual. If the parent type is a
15273 -- direct ancestor of the derived type then, even if it is an
15274 -- interface, the operations are inherited from the primary
15275 -- dispatch table and are in the proper order. If we detect here
15276 -- that primitives are not in the same order we traverse the list
15277 -- of primitive operations of the actual to find the one that
15278 -- implements the interface primitive.
15280 if Need_Search
15281 or else
15282 (Present (Generic_Actual)
15283 and then Present (Act_Subp)
15284 and then not
15285 (Primitive_Names_Match (Subp, Act_Subp)
15286 and then
15287 Type_Conformant (Subp, Act_Subp,
15288 Skip_Controlling_Formals => True)))
15289 then
15290 pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual,
15291 Use_Full_View => True));
15293 -- Remember that we need searching for all pending primitives
15295 Need_Search := True;
15297 -- Handle entities associated with interface primitives
15299 if Present (Alias_Subp)
15300 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
15301 and then not Is_Predefined_Dispatching_Operation (Subp)
15302 then
15303 -- Search for the primitive in the homonym chain
15305 Act_Subp :=
15306 Find_Primitive_Covering_Interface
15307 (Tagged_Type => Generic_Actual,
15308 Iface_Prim => Alias_Subp);
15310 -- Previous search may not locate primitives covering
15311 -- interfaces defined in generics units or instantiations.
15312 -- (it fails if the covering primitive has formals whose
15313 -- type is also defined in generics or instantiations).
15314 -- In such case we search in the list of primitives of the
15315 -- generic actual for the internal entity that links the
15316 -- interface primitive and the covering primitive.
15318 if No (Act_Subp)
15319 and then Is_Generic_Type (Parent_Type)
15320 then
15321 -- This code has been designed to handle only generic
15322 -- formals that implement interfaces that are defined
15323 -- in a generic unit or instantiation. If this code is
15324 -- needed for other cases we must review it because
15325 -- (given that it relies on Original_Location to locate
15326 -- the primitive of Generic_Actual that covers the
15327 -- interface) it could leave linked through attribute
15328 -- Alias entities of unrelated instantiations).
15330 pragma Assert
15331 (Is_Generic_Unit
15332 (Scope (Find_Dispatching_Type (Alias_Subp)))
15333 or else
15334 Instantiation_Depth
15335 (Sloc (Find_Dispatching_Type (Alias_Subp))) > 0);
15337 declare
15338 Iface_Prim_Loc : constant Source_Ptr :=
15339 Original_Location (Sloc (Alias_Subp));
15341 Elmt : Elmt_Id;
15342 Prim : Entity_Id;
15344 begin
15345 Elmt :=
15346 First_Elmt (Primitive_Operations (Generic_Actual));
15348 Search : while Present (Elmt) loop
15349 Prim := Node (Elmt);
15351 if Present (Interface_Alias (Prim))
15352 and then Original_Location
15353 (Sloc (Interface_Alias (Prim))) =
15354 Iface_Prim_Loc
15355 then
15356 Act_Subp := Alias (Prim);
15357 exit Search;
15358 end if;
15360 Next_Elmt (Elmt);
15361 end loop Search;
15362 end;
15363 end if;
15365 pragma Assert (Present (Act_Subp)
15366 or else Is_Abstract_Type (Generic_Actual)
15367 or else Serious_Errors_Detected > 0);
15369 -- Handle predefined primitives plus the rest of user-defined
15370 -- primitives
15372 else
15373 Act_Elmt := First_Elmt (Act_List);
15374 while Present (Act_Elmt) loop
15375 Act_Subp := Node (Act_Elmt);
15377 exit when Primitive_Names_Match (Subp, Act_Subp)
15378 and then Type_Conformant
15379 (Subp, Act_Subp,
15380 Skip_Controlling_Formals => True)
15381 and then No (Interface_Alias (Act_Subp));
15383 Next_Elmt (Act_Elmt);
15384 end loop;
15386 if No (Act_Elmt) then
15387 Act_Subp := Empty;
15388 end if;
15389 end if;
15390 end if;
15392 -- Case 1: If the parent is a limited interface then it has the
15393 -- predefined primitives of synchronized interfaces. However, the
15394 -- actual type may be a non-limited type and hence it does not
15395 -- have such primitives.
15397 if Present (Generic_Actual)
15398 and then not Present (Act_Subp)
15399 and then Is_Limited_Interface (Parent_Base)
15400 and then Is_Predefined_Interface_Primitive (Subp)
15401 then
15402 null;
15404 -- Case 2: Inherit entities associated with interfaces that were
15405 -- not covered by the parent type. We exclude here null interface
15406 -- primitives because they do not need special management.
15408 -- We also exclude interface operations that are renamings. If the
15409 -- subprogram is an explicit renaming of an interface primitive,
15410 -- it is a regular primitive operation, and the presence of its
15411 -- alias is not relevant: it has to be derived like any other
15412 -- primitive.
15414 elsif Present (Alias (Subp))
15415 and then Nkind (Unit_Declaration_Node (Subp)) /=
15416 N_Subprogram_Renaming_Declaration
15417 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
15418 and then not
15419 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
15420 and then Null_Present (Parent (Alias_Subp)))
15421 then
15422 -- If this is an abstract private type then we transfer the
15423 -- derivation of the interface primitive from the partial view
15424 -- to the full view. This is safe because all the interfaces
15425 -- must be visible in the partial view. Done to avoid adding
15426 -- a new interface derivation to the private part of the
15427 -- enclosing package; otherwise this new derivation would be
15428 -- decorated as hidden when the analysis of the enclosing
15429 -- package completes.
15431 if Is_Abstract_Type (Derived_Type)
15432 and then In_Private_Part (Current_Scope)
15433 and then Has_Private_Declaration (Derived_Type)
15434 then
15435 declare
15436 Partial_View : Entity_Id;
15437 Elmt : Elmt_Id;
15438 Ent : Entity_Id;
15440 begin
15441 Partial_View := First_Entity (Current_Scope);
15442 loop
15443 exit when No (Partial_View)
15444 or else (Has_Private_Declaration (Partial_View)
15445 and then
15446 Full_View (Partial_View) = Derived_Type);
15448 Next_Entity (Partial_View);
15449 end loop;
15451 -- If the partial view was not found then the source code
15452 -- has errors and the derivation is not needed.
15454 if Present (Partial_View) then
15455 Elmt :=
15456 First_Elmt (Primitive_Operations (Partial_View));
15457 while Present (Elmt) loop
15458 Ent := Node (Elmt);
15460 if Present (Alias (Ent))
15461 and then Ultimate_Alias (Ent) = Alias (Subp)
15462 then
15463 Append_Elmt
15464 (Ent, Primitive_Operations (Derived_Type));
15465 exit;
15466 end if;
15468 Next_Elmt (Elmt);
15469 end loop;
15471 -- If the interface primitive was not found in the
15472 -- partial view then this interface primitive was
15473 -- overridden. We add a derivation to activate in
15474 -- Derive_Progenitor_Subprograms the machinery to
15475 -- search for it.
15477 if No (Elmt) then
15478 Derive_Interface_Subprogram
15479 (New_Subp => New_Subp,
15480 Subp => Subp,
15481 Actual_Subp => Act_Subp);
15482 end if;
15483 end if;
15484 end;
15485 else
15486 Derive_Interface_Subprogram
15487 (New_Subp => New_Subp,
15488 Subp => Subp,
15489 Actual_Subp => Act_Subp);
15490 end if;
15492 -- Case 3: Common derivation
15494 else
15495 Derive_Subprogram
15496 (New_Subp => New_Subp,
15497 Parent_Subp => Subp,
15498 Derived_Type => Derived_Type,
15499 Parent_Type => Parent_Base,
15500 Actual_Subp => Act_Subp);
15501 end if;
15503 -- No need to update Act_Elm if we must search for the
15504 -- corresponding operation in the generic actual
15506 if not Need_Search
15507 and then Present (Act_Elmt)
15508 then
15509 Next_Elmt (Act_Elmt);
15510 Act_Subp := Node (Act_Elmt);
15511 end if;
15513 <<Continue>>
15514 Next_Elmt (Elmt);
15515 end loop;
15517 -- Inherit additional operations from progenitors. If the derived
15518 -- type is a generic actual, there are not new primitive operations
15519 -- for the type because it has those of the actual, and therefore
15520 -- nothing needs to be done. The renamings generated above are not
15521 -- primitive operations, and their purpose is simply to make the
15522 -- proper operations visible within an instantiation.
15524 if No (Generic_Actual) then
15525 Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
15526 end if;
15527 end if;
15529 -- Final check: Direct descendants must have their primitives in the
15530 -- same order. We exclude from this test untagged types and instances
15531 -- of formal derived types. We skip this test if we have already
15532 -- reported serious errors in the sources.
15534 pragma Assert (not Is_Tagged_Type (Derived_Type)
15535 or else Present (Generic_Actual)
15536 or else Serious_Errors_Detected > 0
15537 or else Check_Derived_Type);
15538 end Derive_Subprograms;
15540 --------------------------------
15541 -- Derived_Standard_Character --
15542 --------------------------------
15544 procedure Derived_Standard_Character
15545 (N : Node_Id;
15546 Parent_Type : Entity_Id;
15547 Derived_Type : Entity_Id)
15549 Loc : constant Source_Ptr := Sloc (N);
15550 Def : constant Node_Id := Type_Definition (N);
15551 Indic : constant Node_Id := Subtype_Indication (Def);
15552 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
15553 Implicit_Base : constant Entity_Id :=
15554 Create_Itype
15555 (E_Enumeration_Type, N, Derived_Type, 'B');
15557 Lo : Node_Id;
15558 Hi : Node_Id;
15560 begin
15561 Discard_Node (Process_Subtype (Indic, N));
15563 Set_Etype (Implicit_Base, Parent_Base);
15564 Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
15565 Set_RM_Size (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
15567 Set_Is_Character_Type (Implicit_Base, True);
15568 Set_Has_Delayed_Freeze (Implicit_Base);
15570 -- The bounds of the implicit base are the bounds of the parent base.
15571 -- Note that their type is the parent base.
15573 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
15574 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
15576 Set_Scalar_Range (Implicit_Base,
15577 Make_Range (Loc,
15578 Low_Bound => Lo,
15579 High_Bound => Hi));
15581 Conditional_Delay (Derived_Type, Parent_Type);
15583 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
15584 Set_Etype (Derived_Type, Implicit_Base);
15585 Set_Size_Info (Derived_Type, Parent_Type);
15587 if Unknown_RM_Size (Derived_Type) then
15588 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
15589 end if;
15591 Set_Is_Character_Type (Derived_Type, True);
15593 if Nkind (Indic) /= N_Subtype_Indication then
15595 -- If no explicit constraint, the bounds are those
15596 -- of the parent type.
15598 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Type));
15599 Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
15600 Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
15601 end if;
15603 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
15605 -- Because the implicit base is used in the conversion of the bounds, we
15606 -- have to freeze it now. This is similar to what is done for numeric
15607 -- types, and it equally suspicious, but otherwise a non-static bound
15608 -- will have a reference to an unfrozen type, which is rejected by Gigi
15609 -- (???). This requires specific care for definition of stream
15610 -- attributes. For details, see comments at the end of
15611 -- Build_Derived_Numeric_Type.
15613 Freeze_Before (N, Implicit_Base);
15614 end Derived_Standard_Character;
15616 ------------------------------
15617 -- Derived_Type_Declaration --
15618 ------------------------------
15620 procedure Derived_Type_Declaration
15621 (T : Entity_Id;
15622 N : Node_Id;
15623 Is_Completion : Boolean)
15625 Parent_Type : Entity_Id;
15627 function Comes_From_Generic (Typ : Entity_Id) return Boolean;
15628 -- Check whether the parent type is a generic formal, or derives
15629 -- directly or indirectly from one.
15631 ------------------------
15632 -- Comes_From_Generic --
15633 ------------------------
15635 function Comes_From_Generic (Typ : Entity_Id) return Boolean is
15636 begin
15637 if Is_Generic_Type (Typ) then
15638 return True;
15640 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
15641 return True;
15643 elsif Is_Private_Type (Typ)
15644 and then Present (Full_View (Typ))
15645 and then Is_Generic_Type (Root_Type (Full_View (Typ)))
15646 then
15647 return True;
15649 elsif Is_Generic_Actual_Type (Typ) then
15650 return True;
15652 else
15653 return False;
15654 end if;
15655 end Comes_From_Generic;
15657 -- Local variables
15659 Def : constant Node_Id := Type_Definition (N);
15660 Iface_Def : Node_Id;
15661 Indic : constant Node_Id := Subtype_Indication (Def);
15662 Extension : constant Node_Id := Record_Extension_Part (Def);
15663 Parent_Node : Node_Id;
15664 Taggd : Boolean;
15666 -- Start of processing for Derived_Type_Declaration
15668 begin
15669 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
15671 -- Ada 2005 (AI-251): In case of interface derivation check that the
15672 -- parent is also an interface.
15674 if Interface_Present (Def) then
15675 Check_SPARK_05_Restriction ("interface is not allowed", Def);
15677 if not Is_Interface (Parent_Type) then
15678 Diagnose_Interface (Indic, Parent_Type);
15680 else
15681 Parent_Node := Parent (Base_Type (Parent_Type));
15682 Iface_Def := Type_Definition (Parent_Node);
15684 -- Ada 2005 (AI-251): Limited interfaces can only inherit from
15685 -- other limited interfaces.
15687 if Limited_Present (Def) then
15688 if Limited_Present (Iface_Def) then
15689 null;
15691 elsif Protected_Present (Iface_Def) then
15692 Error_Msg_NE
15693 ("descendant of& must be declared"
15694 & " as a protected interface",
15695 N, Parent_Type);
15697 elsif Synchronized_Present (Iface_Def) then
15698 Error_Msg_NE
15699 ("descendant of& must be declared"
15700 & " as a synchronized interface",
15701 N, Parent_Type);
15703 elsif Task_Present (Iface_Def) then
15704 Error_Msg_NE
15705 ("descendant of& must be declared as a task interface",
15706 N, Parent_Type);
15708 else
15709 Error_Msg_N
15710 ("(Ada 2005) limited interface cannot "
15711 & "inherit from non-limited interface", Indic);
15712 end if;
15714 -- Ada 2005 (AI-345): Non-limited interfaces can only inherit
15715 -- from non-limited or limited interfaces.
15717 elsif not Protected_Present (Def)
15718 and then not Synchronized_Present (Def)
15719 and then not Task_Present (Def)
15720 then
15721 if Limited_Present (Iface_Def) then
15722 null;
15724 elsif Protected_Present (Iface_Def) then
15725 Error_Msg_NE
15726 ("descendant of& must be declared"
15727 & " as a protected interface",
15728 N, Parent_Type);
15730 elsif Synchronized_Present (Iface_Def) then
15731 Error_Msg_NE
15732 ("descendant of& must be declared"
15733 & " as a synchronized interface",
15734 N, Parent_Type);
15736 elsif Task_Present (Iface_Def) then
15737 Error_Msg_NE
15738 ("descendant of& must be declared as a task interface",
15739 N, Parent_Type);
15740 else
15741 null;
15742 end if;
15743 end if;
15744 end if;
15745 end if;
15747 if Is_Tagged_Type (Parent_Type)
15748 and then Is_Concurrent_Type (Parent_Type)
15749 and then not Is_Interface (Parent_Type)
15750 then
15751 Error_Msg_N
15752 ("parent type of a record extension cannot be "
15753 & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
15754 Set_Etype (T, Any_Type);
15755 return;
15756 end if;
15758 -- Ada 2005 (AI-251): Decorate all the names in the list of ancestor
15759 -- interfaces
15761 if Is_Tagged_Type (Parent_Type)
15762 and then Is_Non_Empty_List (Interface_List (Def))
15763 then
15764 declare
15765 Intf : Node_Id;
15766 T : Entity_Id;
15768 begin
15769 Intf := First (Interface_List (Def));
15770 while Present (Intf) loop
15771 T := Find_Type_Of_Subtype_Indic (Intf);
15773 if not Is_Interface (T) then
15774 Diagnose_Interface (Intf, T);
15776 -- Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
15777 -- a limited type from having a nonlimited progenitor.
15779 elsif (Limited_Present (Def)
15780 or else (not Is_Interface (Parent_Type)
15781 and then Is_Limited_Type (Parent_Type)))
15782 and then not Is_Limited_Interface (T)
15783 then
15784 Error_Msg_NE
15785 ("progenitor interface& of limited type must be limited",
15786 N, T);
15787 end if;
15789 Next (Intf);
15790 end loop;
15791 end;
15792 end if;
15794 if Parent_Type = Any_Type
15795 or else Etype (Parent_Type) = Any_Type
15796 or else (Is_Class_Wide_Type (Parent_Type)
15797 and then Etype (Parent_Type) = T)
15798 then
15799 -- If Parent_Type is undefined or illegal, make new type into a
15800 -- subtype of Any_Type, and set a few attributes to prevent cascaded
15801 -- errors. If this is a self-definition, emit error now.
15803 if T = Parent_Type or else T = Etype (Parent_Type) then
15804 Error_Msg_N ("type cannot be used in its own definition", Indic);
15805 end if;
15807 Set_Ekind (T, Ekind (Parent_Type));
15808 Set_Etype (T, Any_Type);
15809 Set_Scalar_Range (T, Scalar_Range (Any_Type));
15811 if Is_Tagged_Type (T)
15812 and then Is_Record_Type (T)
15813 then
15814 Set_Direct_Primitive_Operations (T, New_Elmt_List);
15815 end if;
15817 return;
15818 end if;
15820 -- Ada 2005 (AI-251): The case in which the parent of the full-view is
15821 -- an interface is special because the list of interfaces in the full
15822 -- view can be given in any order. For example:
15824 -- type A is interface;
15825 -- type B is interface and A;
15826 -- type D is new B with private;
15827 -- private
15828 -- type D is new A and B with null record; -- 1 --
15830 -- In this case we perform the following transformation of -1-:
15832 -- type D is new B and A with null record;
15834 -- If the parent of the full-view covers the parent of the partial-view
15835 -- we have two possible cases:
15837 -- 1) They have the same parent
15838 -- 2) The parent of the full-view implements some further interfaces
15840 -- In both cases we do not need to perform the transformation. In the
15841 -- first case the source program is correct and the transformation is
15842 -- not needed; in the second case the source program does not fulfill
15843 -- the no-hidden interfaces rule (AI-396) and the error will be reported
15844 -- later.
15846 -- This transformation not only simplifies the rest of the analysis of
15847 -- this type declaration but also simplifies the correct generation of
15848 -- the object layout to the expander.
15850 if In_Private_Part (Current_Scope)
15851 and then Is_Interface (Parent_Type)
15852 then
15853 declare
15854 Iface : Node_Id;
15855 Partial_View : Entity_Id;
15856 Partial_View_Parent : Entity_Id;
15857 New_Iface : Node_Id;
15859 begin
15860 -- Look for the associated private type declaration
15862 Partial_View := First_Entity (Current_Scope);
15863 loop
15864 exit when No (Partial_View)
15865 or else (Has_Private_Declaration (Partial_View)
15866 and then Full_View (Partial_View) = T);
15868 Next_Entity (Partial_View);
15869 end loop;
15871 -- If the partial view was not found then the source code has
15872 -- errors and the transformation is not needed.
15874 if Present (Partial_View) then
15875 Partial_View_Parent := Etype (Partial_View);
15877 -- If the parent of the full-view covers the parent of the
15878 -- partial-view we have nothing else to do.
15880 if Interface_Present_In_Ancestor
15881 (Parent_Type, Partial_View_Parent)
15882 then
15883 null;
15885 -- Traverse the list of interfaces of the full-view to look
15886 -- for the parent of the partial-view and perform the tree
15887 -- transformation.
15889 else
15890 Iface := First (Interface_List (Def));
15891 while Present (Iface) loop
15892 if Etype (Iface) = Etype (Partial_View) then
15893 Rewrite (Subtype_Indication (Def),
15894 New_Copy (Subtype_Indication
15895 (Parent (Partial_View))));
15897 New_Iface :=
15898 Make_Identifier (Sloc (N), Chars (Parent_Type));
15899 Append (New_Iface, Interface_List (Def));
15901 -- Analyze the transformed code
15903 Derived_Type_Declaration (T, N, Is_Completion);
15904 return;
15905 end if;
15907 Next (Iface);
15908 end loop;
15909 end if;
15910 end if;
15911 end;
15912 end if;
15914 -- Only composite types other than array types are allowed to have
15915 -- discriminants.
15917 if Present (Discriminant_Specifications (N)) then
15918 if (Is_Elementary_Type (Parent_Type)
15919 or else
15920 Is_Array_Type (Parent_Type))
15921 and then not Error_Posted (N)
15922 then
15923 Error_Msg_N
15924 ("elementary or array type cannot have discriminants",
15925 Defining_Identifier (First (Discriminant_Specifications (N))));
15926 Set_Has_Discriminants (T, False);
15928 -- The type is allowed to have discriminants
15930 else
15931 Check_SPARK_05_Restriction ("discriminant type is not allowed", N);
15932 end if;
15933 end if;
15935 -- In Ada 83, a derived type defined in a package specification cannot
15936 -- be used for further derivation until the end of its visible part.
15937 -- Note that derivation in the private part of the package is allowed.
15939 if Ada_Version = Ada_83
15940 and then Is_Derived_Type (Parent_Type)
15941 and then In_Visible_Part (Scope (Parent_Type))
15942 then
15943 if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
15944 Error_Msg_N
15945 ("(Ada 83): premature use of type for derivation", Indic);
15946 end if;
15947 end if;
15949 -- Check for early use of incomplete or private type
15951 if Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
15952 Error_Msg_N ("premature derivation of incomplete type", Indic);
15953 return;
15955 elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
15956 and then not Comes_From_Generic (Parent_Type))
15957 or else Has_Private_Component (Parent_Type)
15958 then
15959 -- The ancestor type of a formal type can be incomplete, in which
15960 -- case only the operations of the partial view are available in the
15961 -- generic. Subsequent checks may be required when the full view is
15962 -- analyzed to verify that a derivation from a tagged type has an
15963 -- extension.
15965 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
15966 null;
15968 elsif No (Underlying_Type (Parent_Type))
15969 or else Has_Private_Component (Parent_Type)
15970 then
15971 Error_Msg_N
15972 ("premature derivation of derived or private type", Indic);
15974 -- Flag the type itself as being in error, this prevents some
15975 -- nasty problems with subsequent uses of the malformed type.
15977 Set_Error_Posted (T);
15979 -- Check that within the immediate scope of an untagged partial
15980 -- view it's illegal to derive from the partial view if the
15981 -- full view is tagged. (7.3(7))
15983 -- We verify that the Parent_Type is a partial view by checking
15984 -- that it is not a Full_Type_Declaration (i.e. a private type or
15985 -- private extension declaration), to distinguish a partial view
15986 -- from a derivation from a private type which also appears as
15987 -- E_Private_Type. If the parent base type is not declared in an
15988 -- enclosing scope there is no need to check.
15990 elsif Present (Full_View (Parent_Type))
15991 and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
15992 and then not Is_Tagged_Type (Parent_Type)
15993 and then Is_Tagged_Type (Full_View (Parent_Type))
15994 and then In_Open_Scopes (Scope (Base_Type (Parent_Type)))
15995 then
15996 Error_Msg_N
15997 ("premature derivation from type with tagged full view",
15998 Indic);
15999 end if;
16000 end if;
16002 -- Check that form of derivation is appropriate
16004 Taggd := Is_Tagged_Type (Parent_Type);
16006 -- Set the parent type to the class-wide type's specific type in this
16007 -- case to prevent cascading errors
16009 if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
16010 Error_Msg_N ("parent type must not be a class-wide type", Indic);
16011 Set_Etype (T, Etype (Parent_Type));
16012 return;
16013 end if;
16015 if Present (Extension) and then not Taggd then
16016 Error_Msg_N
16017 ("type derived from untagged type cannot have extension", Indic);
16019 elsif No (Extension) and then Taggd then
16021 -- If this declaration is within a private part (or body) of a
16022 -- generic instantiation then the derivation is allowed (the parent
16023 -- type can only appear tagged in this case if it's a generic actual
16024 -- type, since it would otherwise have been rejected in the analysis
16025 -- of the generic template).
16027 if not Is_Generic_Actual_Type (Parent_Type)
16028 or else In_Visible_Part (Scope (Parent_Type))
16029 then
16030 if Is_Class_Wide_Type (Parent_Type) then
16031 Error_Msg_N
16032 ("parent type must not be a class-wide type", Indic);
16034 -- Use specific type to prevent cascaded errors.
16036 Parent_Type := Etype (Parent_Type);
16038 else
16039 Error_Msg_N
16040 ("type derived from tagged type must have extension", Indic);
16041 end if;
16042 end if;
16043 end if;
16045 -- AI-443: Synchronized formal derived types require a private
16046 -- extension. There is no point in checking the ancestor type or
16047 -- the progenitors since the construct is wrong to begin with.
16049 if Ada_Version >= Ada_2005
16050 and then Is_Generic_Type (T)
16051 and then Present (Original_Node (N))
16052 then
16053 declare
16054 Decl : constant Node_Id := Original_Node (N);
16056 begin
16057 if Nkind (Decl) = N_Formal_Type_Declaration
16058 and then Nkind (Formal_Type_Definition (Decl)) =
16059 N_Formal_Derived_Type_Definition
16060 and then Synchronized_Present (Formal_Type_Definition (Decl))
16061 and then No (Extension)
16063 -- Avoid emitting a duplicate error message
16065 and then not Error_Posted (Indic)
16066 then
16067 Error_Msg_N
16068 ("synchronized derived type must have extension", N);
16069 end if;
16070 end;
16071 end if;
16073 if Null_Exclusion_Present (Def)
16074 and then not Is_Access_Type (Parent_Type)
16075 then
16076 Error_Msg_N ("null exclusion can only apply to an access type", N);
16077 end if;
16079 -- Avoid deriving parent primitives of underlying record views
16081 Build_Derived_Type (N, Parent_Type, T, Is_Completion,
16082 Derive_Subps => not Is_Underlying_Record_View (T));
16084 -- AI-419: The parent type of an explicitly limited derived type must
16085 -- be a limited type or a limited interface.
16087 if Limited_Present (Def) then
16088 Set_Is_Limited_Record (T);
16090 if Is_Interface (T) then
16091 Set_Is_Limited_Interface (T);
16092 end if;
16094 if not Is_Limited_Type (Parent_Type)
16095 and then
16096 (not Is_Interface (Parent_Type)
16097 or else not Is_Limited_Interface (Parent_Type))
16098 then
16099 -- AI05-0096: a derivation in the private part of an instance is
16100 -- legal if the generic formal is untagged limited, and the actual
16101 -- is non-limited.
16103 if Is_Generic_Actual_Type (Parent_Type)
16104 and then In_Private_Part (Current_Scope)
16105 and then
16106 not Is_Tagged_Type
16107 (Generic_Parent_Type (Parent (Parent_Type)))
16108 then
16109 null;
16111 else
16112 Error_Msg_NE
16113 ("parent type& of limited type must be limited",
16114 N, Parent_Type);
16115 end if;
16116 end if;
16117 end if;
16119 -- In SPARK, there are no derived type definitions other than type
16120 -- extensions of tagged record types.
16122 if No (Extension) then
16123 Check_SPARK_05_Restriction
16124 ("derived type is not allowed", Original_Node (N));
16125 end if;
16126 end Derived_Type_Declaration;
16128 ------------------------
16129 -- Diagnose_Interface --
16130 ------------------------
16132 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id) is
16133 begin
16134 if not Is_Interface (E) and then E /= Any_Type then
16135 Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
16136 end if;
16137 end Diagnose_Interface;
16139 ----------------------------------
16140 -- Enumeration_Type_Declaration --
16141 ----------------------------------
16143 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
16144 Ev : Uint;
16145 L : Node_Id;
16146 R_Node : Node_Id;
16147 B_Node : Node_Id;
16149 begin
16150 -- Create identifier node representing lower bound
16152 B_Node := New_Node (N_Identifier, Sloc (Def));
16153 L := First (Literals (Def));
16154 Set_Chars (B_Node, Chars (L));
16155 Set_Entity (B_Node, L);
16156 Set_Etype (B_Node, T);
16157 Set_Is_Static_Expression (B_Node, True);
16159 R_Node := New_Node (N_Range, Sloc (Def));
16160 Set_Low_Bound (R_Node, B_Node);
16162 Set_Ekind (T, E_Enumeration_Type);
16163 Set_First_Literal (T, L);
16164 Set_Etype (T, T);
16165 Set_Is_Constrained (T);
16167 Ev := Uint_0;
16169 -- Loop through literals of enumeration type setting pos and rep values
16170 -- except that if the Ekind is already set, then it means the literal
16171 -- was already constructed (case of a derived type declaration and we
16172 -- should not disturb the Pos and Rep values.
16174 while Present (L) loop
16175 if Ekind (L) /= E_Enumeration_Literal then
16176 Set_Ekind (L, E_Enumeration_Literal);
16177 Set_Enumeration_Pos (L, Ev);
16178 Set_Enumeration_Rep (L, Ev);
16179 Set_Is_Known_Valid (L, True);
16180 end if;
16182 Set_Etype (L, T);
16183 New_Overloaded_Entity (L);
16184 Generate_Definition (L);
16185 Set_Convention (L, Convention_Intrinsic);
16187 -- Case of character literal
16189 if Nkind (L) = N_Defining_Character_Literal then
16190 Set_Is_Character_Type (T, True);
16192 -- Check violation of No_Wide_Characters
16194 if Restriction_Check_Required (No_Wide_Characters) then
16195 Get_Name_String (Chars (L));
16197 if Name_Len >= 3 and then Name_Buffer (1 .. 2) = "QW" then
16198 Check_Restriction (No_Wide_Characters, L);
16199 end if;
16200 end if;
16201 end if;
16203 Ev := Ev + 1;
16204 Next (L);
16205 end loop;
16207 -- Now create a node representing upper bound
16209 B_Node := New_Node (N_Identifier, Sloc (Def));
16210 Set_Chars (B_Node, Chars (Last (Literals (Def))));
16211 Set_Entity (B_Node, Last (Literals (Def)));
16212 Set_Etype (B_Node, T);
16213 Set_Is_Static_Expression (B_Node, True);
16215 Set_High_Bound (R_Node, B_Node);
16217 -- Initialize various fields of the type. Some of this information
16218 -- may be overwritten later through rep.clauses.
16220 Set_Scalar_Range (T, R_Node);
16221 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
16222 Set_Enum_Esize (T);
16223 Set_Enum_Pos_To_Rep (T, Empty);
16225 -- Set Discard_Names if configuration pragma set, or if there is
16226 -- a parameterless pragma in the current declarative region
16228 if Global_Discard_Names or else Discard_Names (Scope (T)) then
16229 Set_Discard_Names (T);
16230 end if;
16232 -- Process end label if there is one
16234 if Present (Def) then
16235 Process_End_Label (Def, 'e', T);
16236 end if;
16237 end Enumeration_Type_Declaration;
16239 ---------------------------------
16240 -- Expand_To_Stored_Constraint --
16241 ---------------------------------
16243 function Expand_To_Stored_Constraint
16244 (Typ : Entity_Id;
16245 Constraint : Elist_Id) return Elist_Id
16247 Explicitly_Discriminated_Type : Entity_Id;
16248 Expansion : Elist_Id;
16249 Discriminant : Entity_Id;
16251 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
16252 -- Find the nearest type that actually specifies discriminants
16254 ---------------------------------
16255 -- Type_With_Explicit_Discrims --
16256 ---------------------------------
16258 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
16259 Typ : constant E := Base_Type (Id);
16261 begin
16262 if Ekind (Typ) in Incomplete_Or_Private_Kind then
16263 if Present (Full_View (Typ)) then
16264 return Type_With_Explicit_Discrims (Full_View (Typ));
16265 end if;
16267 else
16268 if Has_Discriminants (Typ) then
16269 return Typ;
16270 end if;
16271 end if;
16273 if Etype (Typ) = Typ then
16274 return Empty;
16275 elsif Has_Discriminants (Typ) then
16276 return Typ;
16277 else
16278 return Type_With_Explicit_Discrims (Etype (Typ));
16279 end if;
16281 end Type_With_Explicit_Discrims;
16283 -- Start of processing for Expand_To_Stored_Constraint
16285 begin
16286 if No (Constraint) or else Is_Empty_Elmt_List (Constraint) then
16287 return No_Elist;
16288 end if;
16290 Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
16292 if No (Explicitly_Discriminated_Type) then
16293 return No_Elist;
16294 end if;
16296 Expansion := New_Elmt_List;
16298 Discriminant :=
16299 First_Stored_Discriminant (Explicitly_Discriminated_Type);
16300 while Present (Discriminant) loop
16301 Append_Elmt
16302 (Get_Discriminant_Value
16303 (Discriminant, Explicitly_Discriminated_Type, Constraint),
16304 To => Expansion);
16305 Next_Stored_Discriminant (Discriminant);
16306 end loop;
16308 return Expansion;
16309 end Expand_To_Stored_Constraint;
16311 ---------------------------
16312 -- Find_Hidden_Interface --
16313 ---------------------------
16315 function Find_Hidden_Interface
16316 (Src : Elist_Id;
16317 Dest : Elist_Id) return Entity_Id
16319 Iface : Entity_Id;
16320 Iface_Elmt : Elmt_Id;
16322 begin
16323 if Present (Src) and then Present (Dest) then
16324 Iface_Elmt := First_Elmt (Src);
16325 while Present (Iface_Elmt) loop
16326 Iface := Node (Iface_Elmt);
16328 if Is_Interface (Iface)
16329 and then not Contain_Interface (Iface, Dest)
16330 then
16331 return Iface;
16332 end if;
16334 Next_Elmt (Iface_Elmt);
16335 end loop;
16336 end if;
16338 return Empty;
16339 end Find_Hidden_Interface;
16341 --------------------
16342 -- Find_Type_Name --
16343 --------------------
16345 function Find_Type_Name (N : Node_Id) return Entity_Id is
16346 Id : constant Entity_Id := Defining_Identifier (N);
16347 Prev : Entity_Id;
16348 New_Id : Entity_Id;
16349 Prev_Par : Node_Id;
16351 procedure Check_Duplicate_Aspects;
16352 -- Check that aspects specified in a completion have not been specified
16353 -- already in the partial view. Type_Invariant and others can be
16354 -- specified on either view but never on both.
16356 procedure Tag_Mismatch;
16357 -- Diagnose a tagged partial view whose full view is untagged.
16358 -- We post the message on the full view, with a reference to
16359 -- the previous partial view. The partial view can be private
16360 -- or incomplete, and these are handled in a different manner,
16361 -- so we determine the position of the error message from the
16362 -- respective slocs of both.
16364 -----------------------------
16365 -- Check_Duplicate_Aspects --
16366 -----------------------------
16367 procedure Check_Duplicate_Aspects is
16368 Prev_Aspects : constant List_Id := Aspect_Specifications (Prev_Par);
16369 Full_Aspects : constant List_Id := Aspect_Specifications (N);
16370 F_Spec, P_Spec : Node_Id;
16372 begin
16373 if Present (Prev_Aspects) and then Present (Full_Aspects) then
16374 F_Spec := First (Full_Aspects);
16375 while Present (F_Spec) loop
16376 P_Spec := First (Prev_Aspects);
16377 while Present (P_Spec) loop
16378 if Chars (Identifier (P_Spec)) = Chars (Identifier (F_Spec))
16379 then
16380 Error_Msg_N
16381 ("aspect already specified in private declaration",
16382 F_Spec);
16383 Remove (F_Spec);
16384 return;
16385 end if;
16387 Next (P_Spec);
16388 end loop;
16390 Next (F_Spec);
16391 end loop;
16392 end if;
16393 end Check_Duplicate_Aspects;
16395 ------------------
16396 -- Tag_Mismatch --
16397 ------------------
16399 procedure Tag_Mismatch is
16400 begin
16401 if Sloc (Prev) < Sloc (Id) then
16402 if Ada_Version >= Ada_2012
16403 and then Nkind (N) = N_Private_Type_Declaration
16404 then
16405 Error_Msg_NE
16406 ("declaration of private } must be a tagged type ", Id, Prev);
16407 else
16408 Error_Msg_NE
16409 ("full declaration of } must be a tagged type ", Id, Prev);
16410 end if;
16412 else
16413 if Ada_Version >= Ada_2012
16414 and then Nkind (N) = N_Private_Type_Declaration
16415 then
16416 Error_Msg_NE
16417 ("declaration of private } must be a tagged type ", Prev, Id);
16418 else
16419 Error_Msg_NE
16420 ("full declaration of } must be a tagged type ", Prev, Id);
16421 end if;
16422 end if;
16423 end Tag_Mismatch;
16425 -- Start of processing for Find_Type_Name
16427 begin
16428 -- Find incomplete declaration, if one was given
16430 Prev := Current_Entity_In_Scope (Id);
16432 -- New type declaration
16434 if No (Prev) then
16435 Enter_Name (Id);
16436 return Id;
16438 -- Previous declaration exists
16440 else
16441 Prev_Par := Parent (Prev);
16443 -- Error if not incomplete/private case except if previous
16444 -- declaration is implicit, etc. Enter_Name will emit error if
16445 -- appropriate.
16447 if not Is_Incomplete_Or_Private_Type (Prev) then
16448 Enter_Name (Id);
16449 New_Id := Id;
16451 -- Check invalid completion of private or incomplete type
16453 elsif not Nkind_In (N, N_Full_Type_Declaration,
16454 N_Task_Type_Declaration,
16455 N_Protected_Type_Declaration)
16456 and then
16457 (Ada_Version < Ada_2012
16458 or else not Is_Incomplete_Type (Prev)
16459 or else not Nkind_In (N, N_Private_Type_Declaration,
16460 N_Private_Extension_Declaration))
16461 then
16462 -- Completion must be a full type declarations (RM 7.3(4))
16464 Error_Msg_Sloc := Sloc (Prev);
16465 Error_Msg_NE ("invalid completion of }", Id, Prev);
16467 -- Set scope of Id to avoid cascaded errors. Entity is never
16468 -- examined again, except when saving globals in generics.
16470 Set_Scope (Id, Current_Scope);
16471 New_Id := Id;
16473 -- If this is a repeated incomplete declaration, no further
16474 -- checks are possible.
16476 if Nkind (N) = N_Incomplete_Type_Declaration then
16477 return Prev;
16478 end if;
16480 -- Case of full declaration of incomplete type
16482 elsif Ekind (Prev) = E_Incomplete_Type
16483 and then (Ada_Version < Ada_2012
16484 or else No (Full_View (Prev))
16485 or else not Is_Private_Type (Full_View (Prev)))
16486 then
16487 -- Indicate that the incomplete declaration has a matching full
16488 -- declaration. The defining occurrence of the incomplete
16489 -- declaration remains the visible one, and the procedure
16490 -- Get_Full_View dereferences it whenever the type is used.
16492 if Present (Full_View (Prev)) then
16493 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
16494 end if;
16496 Set_Full_View (Prev, Id);
16497 Append_Entity (Id, Current_Scope);
16498 Set_Is_Public (Id, Is_Public (Prev));
16499 Set_Is_Internal (Id);
16500 New_Id := Prev;
16502 -- If the incomplete view is tagged, a class_wide type has been
16503 -- created already. Use it for the private type as well, in order
16504 -- to prevent multiple incompatible class-wide types that may be
16505 -- created for self-referential anonymous access components.
16507 if Is_Tagged_Type (Prev)
16508 and then Present (Class_Wide_Type (Prev))
16509 then
16510 Set_Ekind (Id, Ekind (Prev)); -- will be reset later
16511 Set_Class_Wide_Type (Id, Class_Wide_Type (Prev));
16513 -- The type of the classwide type is the current Id. Previously
16514 -- this was not done for private declarations because of order-
16515 -- of elaboration issues in the back-end, but gigi now handles
16516 -- this properly.
16518 Set_Etype (Class_Wide_Type (Id), Id);
16519 end if;
16521 -- Case of full declaration of private type
16523 else
16524 -- If the private type was a completion of an incomplete type then
16525 -- update Prev to reference the private type
16527 if Ada_Version >= Ada_2012
16528 and then Ekind (Prev) = E_Incomplete_Type
16529 and then Present (Full_View (Prev))
16530 and then Is_Private_Type (Full_View (Prev))
16531 then
16532 Prev := Full_View (Prev);
16533 Prev_Par := Parent (Prev);
16534 end if;
16536 if Nkind (N) = N_Full_Type_Declaration
16537 and then Nkind_In
16538 (Type_Definition (N), N_Record_Definition,
16539 N_Derived_Type_Definition)
16540 and then Interface_Present (Type_Definition (N))
16541 then
16542 Error_Msg_N
16543 ("completion of private type cannot be an interface", N);
16544 end if;
16546 if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
16547 if Etype (Prev) /= Prev then
16549 -- Prev is a private subtype or a derived type, and needs
16550 -- no completion.
16552 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
16553 New_Id := Id;
16555 elsif Ekind (Prev) = E_Private_Type
16556 and then Nkind_In (N, N_Task_Type_Declaration,
16557 N_Protected_Type_Declaration)
16558 then
16559 Error_Msg_N
16560 ("completion of nonlimited type cannot be limited", N);
16562 elsif Ekind (Prev) = E_Record_Type_With_Private
16563 and then Nkind_In (N, N_Task_Type_Declaration,
16564 N_Protected_Type_Declaration)
16565 then
16566 if not Is_Limited_Record (Prev) then
16567 Error_Msg_N
16568 ("completion of nonlimited type cannot be limited", N);
16570 elsif No (Interface_List (N)) then
16571 Error_Msg_N
16572 ("completion of tagged private type must be tagged",
16574 end if;
16575 end if;
16577 -- Ada 2005 (AI-251): Private extension declaration of a task
16578 -- type or a protected type. This case arises when covering
16579 -- interface types.
16581 elsif Nkind_In (N, N_Task_Type_Declaration,
16582 N_Protected_Type_Declaration)
16583 then
16584 null;
16586 elsif Nkind (N) /= N_Full_Type_Declaration
16587 or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
16588 then
16589 Error_Msg_N
16590 ("full view of private extension must be an extension", N);
16592 elsif not (Abstract_Present (Parent (Prev)))
16593 and then Abstract_Present (Type_Definition (N))
16594 then
16595 Error_Msg_N
16596 ("full view of non-abstract extension cannot be abstract", N);
16597 end if;
16599 if not In_Private_Part (Current_Scope) then
16600 Error_Msg_N
16601 ("declaration of full view must appear in private part", N);
16602 end if;
16604 if Ada_Version >= Ada_2012 then
16605 Check_Duplicate_Aspects;
16606 end if;
16608 Copy_And_Swap (Prev, Id);
16609 Set_Has_Private_Declaration (Prev);
16610 Set_Has_Private_Declaration (Id);
16612 -- AI12-0133: Indicate whether we have a partial view with
16613 -- unknown discriminants, in which case initialization of objects
16614 -- of the type do not receive an invariant check.
16616 Set_Partial_View_Has_Unknown_Discr
16617 (Prev, Has_Unknown_Discriminants (Id));
16619 -- Preserve aspect and iterator flags that may have been set on
16620 -- the partial view.
16622 Set_Has_Delayed_Aspects (Prev, Has_Delayed_Aspects (Id));
16623 Set_Has_Implicit_Dereference (Prev, Has_Implicit_Dereference (Id));
16625 -- If no error, propagate freeze_node from private to full view.
16626 -- It may have been generated for an early operational item.
16628 if Present (Freeze_Node (Id))
16629 and then Serious_Errors_Detected = 0
16630 and then No (Full_View (Id))
16631 then
16632 Set_Freeze_Node (Prev, Freeze_Node (Id));
16633 Set_Freeze_Node (Id, Empty);
16634 Set_First_Rep_Item (Prev, First_Rep_Item (Id));
16635 end if;
16637 Set_Full_View (Id, Prev);
16638 New_Id := Prev;
16639 end if;
16641 -- Verify that full declaration conforms to partial one
16643 if Is_Incomplete_Or_Private_Type (Prev)
16644 and then Present (Discriminant_Specifications (Prev_Par))
16645 then
16646 if Present (Discriminant_Specifications (N)) then
16647 if Ekind (Prev) = E_Incomplete_Type then
16648 Check_Discriminant_Conformance (N, Prev, Prev);
16649 else
16650 Check_Discriminant_Conformance (N, Prev, Id);
16651 end if;
16653 else
16654 Error_Msg_N
16655 ("missing discriminants in full type declaration", N);
16657 -- To avoid cascaded errors on subsequent use, share the
16658 -- discriminants of the partial view.
16660 Set_Discriminant_Specifications (N,
16661 Discriminant_Specifications (Prev_Par));
16662 end if;
16663 end if;
16665 -- A prior untagged partial view can have an associated class-wide
16666 -- type due to use of the class attribute, and in this case the full
16667 -- type must also be tagged. This Ada 95 usage is deprecated in favor
16668 -- of incomplete tagged declarations, but we check for it.
16670 if Is_Type (Prev)
16671 and then (Is_Tagged_Type (Prev)
16672 or else Present (Class_Wide_Type (Prev)))
16673 then
16674 -- Ada 2012 (AI05-0162): A private type may be the completion of
16675 -- an incomplete type.
16677 if Ada_Version >= Ada_2012
16678 and then Is_Incomplete_Type (Prev)
16679 and then Nkind_In (N, N_Private_Type_Declaration,
16680 N_Private_Extension_Declaration)
16681 then
16682 -- No need to check private extensions since they are tagged
16684 if Nkind (N) = N_Private_Type_Declaration
16685 and then not Tagged_Present (N)
16686 then
16687 Tag_Mismatch;
16688 end if;
16690 -- The full declaration is either a tagged type (including
16691 -- a synchronized type that implements interfaces) or a
16692 -- type extension, otherwise this is an error.
16694 elsif Nkind_In (N, N_Task_Type_Declaration,
16695 N_Protected_Type_Declaration)
16696 then
16697 if No (Interface_List (N)) and then not Error_Posted (N) then
16698 Tag_Mismatch;
16699 end if;
16701 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
16703 -- Indicate that the previous declaration (tagged incomplete
16704 -- or private declaration) requires the same on the full one.
16706 if not Tagged_Present (Type_Definition (N)) then
16707 Tag_Mismatch;
16708 Set_Is_Tagged_Type (Id);
16709 end if;
16711 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
16712 if No (Record_Extension_Part (Type_Definition (N))) then
16713 Error_Msg_NE
16714 ("full declaration of } must be a record extension",
16715 Prev, Id);
16717 -- Set some attributes to produce a usable full view
16719 Set_Is_Tagged_Type (Id);
16720 end if;
16722 else
16723 Tag_Mismatch;
16724 end if;
16725 end if;
16727 if Present (Prev)
16728 and then Nkind (Parent (Prev)) = N_Incomplete_Type_Declaration
16729 and then Present (Premature_Use (Parent (Prev)))
16730 then
16731 Error_Msg_Sloc := Sloc (N);
16732 Error_Msg_N
16733 ("\full declaration #", Premature_Use (Parent (Prev)));
16734 end if;
16736 return New_Id;
16737 end if;
16738 end Find_Type_Name;
16740 -------------------------
16741 -- Find_Type_Of_Object --
16742 -------------------------
16744 function Find_Type_Of_Object
16745 (Obj_Def : Node_Id;
16746 Related_Nod : Node_Id) return Entity_Id
16748 Def_Kind : constant Node_Kind := Nkind (Obj_Def);
16749 P : Node_Id := Parent (Obj_Def);
16750 T : Entity_Id;
16751 Nam : Name_Id;
16753 begin
16754 -- If the parent is a component_definition node we climb to the
16755 -- component_declaration node
16757 if Nkind (P) = N_Component_Definition then
16758 P := Parent (P);
16759 end if;
16761 -- Case of an anonymous array subtype
16763 if Nkind_In (Def_Kind, N_Constrained_Array_Definition,
16764 N_Unconstrained_Array_Definition)
16765 then
16766 T := Empty;
16767 Array_Type_Declaration (T, Obj_Def);
16769 -- Create an explicit subtype whenever possible
16771 elsif Nkind (P) /= N_Component_Declaration
16772 and then Def_Kind = N_Subtype_Indication
16773 then
16774 -- Base name of subtype on object name, which will be unique in
16775 -- the current scope.
16777 -- If this is a duplicate declaration, return base type, to avoid
16778 -- generating duplicate anonymous types.
16780 if Error_Posted (P) then
16781 Analyze (Subtype_Mark (Obj_Def));
16782 return Entity (Subtype_Mark (Obj_Def));
16783 end if;
16785 Nam :=
16786 New_External_Name
16787 (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
16789 T := Make_Defining_Identifier (Sloc (P), Nam);
16791 Insert_Action (Obj_Def,
16792 Make_Subtype_Declaration (Sloc (P),
16793 Defining_Identifier => T,
16794 Subtype_Indication => Relocate_Node (Obj_Def)));
16796 -- This subtype may need freezing, and this will not be done
16797 -- automatically if the object declaration is not in declarative
16798 -- part. Since this is an object declaration, the type cannot always
16799 -- be frozen here. Deferred constants do not freeze their type
16800 -- (which often enough will be private).
16802 if Nkind (P) = N_Object_Declaration
16803 and then Constant_Present (P)
16804 and then No (Expression (P))
16805 then
16806 null;
16808 -- Here we freeze the base type of object type to catch premature use
16809 -- of discriminated private type without a full view.
16811 else
16812 Insert_Actions (Obj_Def, Freeze_Entity (Base_Type (T), P));
16813 end if;
16815 -- Ada 2005 AI-406: the object definition in an object declaration
16816 -- can be an access definition.
16818 elsif Def_Kind = N_Access_Definition then
16819 T := Access_Definition (Related_Nod, Obj_Def);
16821 Set_Is_Local_Anonymous_Access
16823 V => (Ada_Version < Ada_2012)
16824 or else (Nkind (P) /= N_Object_Declaration)
16825 or else Is_Library_Level_Entity (Defining_Identifier (P)));
16827 -- Otherwise, the object definition is just a subtype_mark
16829 else
16830 T := Process_Subtype (Obj_Def, Related_Nod);
16832 -- If expansion is disabled an object definition that is an aggregate
16833 -- will not get expanded and may lead to scoping problems in the back
16834 -- end, if the object is referenced in an inner scope. In that case
16835 -- create an itype reference for the object definition now. This
16836 -- may be redundant in some cases, but harmless.
16838 if Is_Itype (T)
16839 and then Nkind (Related_Nod) = N_Object_Declaration
16840 and then ASIS_Mode
16841 then
16842 Build_Itype_Reference (T, Related_Nod);
16843 end if;
16844 end if;
16846 return T;
16847 end Find_Type_Of_Object;
16849 --------------------------------
16850 -- Find_Type_Of_Subtype_Indic --
16851 --------------------------------
16853 function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
16854 Typ : Entity_Id;
16856 begin
16857 -- Case of subtype mark with a constraint
16859 if Nkind (S) = N_Subtype_Indication then
16860 Find_Type (Subtype_Mark (S));
16861 Typ := Entity (Subtype_Mark (S));
16863 if not
16864 Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
16865 then
16866 Error_Msg_N
16867 ("incorrect constraint for this kind of type", Constraint (S));
16868 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
16869 end if;
16871 -- Otherwise we have a subtype mark without a constraint
16873 elsif Error_Posted (S) then
16874 Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
16875 return Any_Type;
16877 else
16878 Find_Type (S);
16879 Typ := Entity (S);
16880 end if;
16882 -- Check No_Wide_Characters restriction
16884 Check_Wide_Character_Restriction (Typ, S);
16886 return Typ;
16887 end Find_Type_Of_Subtype_Indic;
16889 -------------------------------------
16890 -- Floating_Point_Type_Declaration --
16891 -------------------------------------
16893 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
16894 Digs : constant Node_Id := Digits_Expression (Def);
16895 Max_Digs_Val : constant Uint := Digits_Value (Standard_Long_Long_Float);
16896 Digs_Val : Uint;
16897 Base_Typ : Entity_Id;
16898 Implicit_Base : Entity_Id;
16899 Bound : Node_Id;
16901 function Can_Derive_From (E : Entity_Id) return Boolean;
16902 -- Find if given digits value, and possibly a specified range, allows
16903 -- derivation from specified type
16905 function Find_Base_Type return Entity_Id;
16906 -- Find a predefined base type that Def can derive from, or generate
16907 -- an error and substitute Long_Long_Float if none exists.
16909 ---------------------
16910 -- Can_Derive_From --
16911 ---------------------
16913 function Can_Derive_From (E : Entity_Id) return Boolean is
16914 Spec : constant Entity_Id := Real_Range_Specification (Def);
16916 begin
16917 -- Check specified "digits" constraint
16919 if Digs_Val > Digits_Value (E) then
16920 return False;
16921 end if;
16923 -- Check for matching range, if specified
16925 if Present (Spec) then
16926 if Expr_Value_R (Type_Low_Bound (E)) >
16927 Expr_Value_R (Low_Bound (Spec))
16928 then
16929 return False;
16930 end if;
16932 if Expr_Value_R (Type_High_Bound (E)) <
16933 Expr_Value_R (High_Bound (Spec))
16934 then
16935 return False;
16936 end if;
16937 end if;
16939 return True;
16940 end Can_Derive_From;
16942 --------------------
16943 -- Find_Base_Type --
16944 --------------------
16946 function Find_Base_Type return Entity_Id is
16947 Choice : Elmt_Id := First_Elmt (Predefined_Float_Types);
16949 begin
16950 -- Iterate over the predefined types in order, returning the first
16951 -- one that Def can derive from.
16953 while Present (Choice) loop
16954 if Can_Derive_From (Node (Choice)) then
16955 return Node (Choice);
16956 end if;
16958 Next_Elmt (Choice);
16959 end loop;
16961 -- If we can't derive from any existing type, use Long_Long_Float
16962 -- and give appropriate message explaining the problem.
16964 if Digs_Val > Max_Digs_Val then
16965 -- It might be the case that there is a type with the requested
16966 -- range, just not the combination of digits and range.
16968 Error_Msg_N
16969 ("no predefined type has requested range and precision",
16970 Real_Range_Specification (Def));
16972 else
16973 Error_Msg_N
16974 ("range too large for any predefined type",
16975 Real_Range_Specification (Def));
16976 end if;
16978 return Standard_Long_Long_Float;
16979 end Find_Base_Type;
16981 -- Start of processing for Floating_Point_Type_Declaration
16983 begin
16984 Check_Restriction (No_Floating_Point, Def);
16986 -- Create an implicit base type
16988 Implicit_Base :=
16989 Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
16991 -- Analyze and verify digits value
16993 Analyze_And_Resolve (Digs, Any_Integer);
16994 Check_Digits_Expression (Digs);
16995 Digs_Val := Expr_Value (Digs);
16997 -- Process possible range spec and find correct type to derive from
16999 Process_Real_Range_Specification (Def);
17001 -- Check that requested number of digits is not too high.
17003 if Digs_Val > Max_Digs_Val then
17005 -- The check for Max_Base_Digits may be somewhat expensive, as it
17006 -- requires reading System, so only do it when necessary.
17008 declare
17009 Max_Base_Digits : constant Uint :=
17010 Expr_Value
17011 (Expression
17012 (Parent (RTE (RE_Max_Base_Digits))));
17014 begin
17015 if Digs_Val > Max_Base_Digits then
17016 Error_Msg_Uint_1 := Max_Base_Digits;
17017 Error_Msg_N ("digits value out of range, maximum is ^", Digs);
17019 elsif No (Real_Range_Specification (Def)) then
17020 Error_Msg_Uint_1 := Max_Digs_Val;
17021 Error_Msg_N ("types with more than ^ digits need range spec "
17022 & "(RM 3.5.7(6))", Digs);
17023 end if;
17024 end;
17025 end if;
17027 -- Find a suitable type to derive from or complain and use a substitute
17029 Base_Typ := Find_Base_Type;
17031 -- If there are bounds given in the declaration use them as the bounds
17032 -- of the type, otherwise use the bounds of the predefined base type
17033 -- that was chosen based on the Digits value.
17035 if Present (Real_Range_Specification (Def)) then
17036 Set_Scalar_Range (T, Real_Range_Specification (Def));
17037 Set_Is_Constrained (T);
17039 -- The bounds of this range must be converted to machine numbers
17040 -- in accordance with RM 4.9(38).
17042 Bound := Type_Low_Bound (T);
17044 if Nkind (Bound) = N_Real_Literal then
17045 Set_Realval
17046 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
17047 Set_Is_Machine_Number (Bound);
17048 end if;
17050 Bound := Type_High_Bound (T);
17052 if Nkind (Bound) = N_Real_Literal then
17053 Set_Realval
17054 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
17055 Set_Is_Machine_Number (Bound);
17056 end if;
17058 else
17059 Set_Scalar_Range (T, Scalar_Range (Base_Typ));
17060 end if;
17062 -- Complete definition of implicit base and declared first subtype. The
17063 -- inheritance of the rep item chain ensures that SPARK-related pragmas
17064 -- are not clobbered when the floating point type acts as a full view of
17065 -- a private type.
17067 Set_Etype (Implicit_Base, Base_Typ);
17068 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
17069 Set_Size_Info (Implicit_Base, Base_Typ);
17070 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
17071 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
17072 Set_Digits_Value (Implicit_Base, Digits_Value (Base_Typ));
17073 Set_Float_Rep (Implicit_Base, Float_Rep (Base_Typ));
17075 Set_Ekind (T, E_Floating_Point_Subtype);
17076 Set_Etype (T, Implicit_Base);
17077 Set_Size_Info (T, Implicit_Base);
17078 Set_RM_Size (T, RM_Size (Implicit_Base));
17079 Inherit_Rep_Item_Chain (T, Implicit_Base);
17080 Set_Digits_Value (T, Digs_Val);
17081 end Floating_Point_Type_Declaration;
17083 ----------------------------
17084 -- Get_Discriminant_Value --
17085 ----------------------------
17087 -- This is the situation:
17089 -- There is a non-derived type
17091 -- type T0 (Dx, Dy, Dz...)
17093 -- There are zero or more levels of derivation, with each derivation
17094 -- either purely inheriting the discriminants, or defining its own.
17096 -- type Ti is new Ti-1
17097 -- or
17098 -- type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
17099 -- or
17100 -- subtype Ti is ...
17102 -- The subtype issue is avoided by the use of Original_Record_Component,
17103 -- and the fact that derived subtypes also derive the constraints.
17105 -- This chain leads back from
17107 -- Typ_For_Constraint
17109 -- Typ_For_Constraint has discriminants, and the value for each
17110 -- discriminant is given by its corresponding Elmt of Constraints.
17112 -- Discriminant is some discriminant in this hierarchy
17114 -- We need to return its value
17116 -- We do this by recursively searching each level, and looking for
17117 -- Discriminant. Once we get to the bottom, we start backing up
17118 -- returning the value for it which may in turn be a discriminant
17119 -- further up, so on the backup we continue the substitution.
17121 function Get_Discriminant_Value
17122 (Discriminant : Entity_Id;
17123 Typ_For_Constraint : Entity_Id;
17124 Constraint : Elist_Id) return Node_Id
17126 function Root_Corresponding_Discriminant
17127 (Discr : Entity_Id) return Entity_Id;
17128 -- Given a discriminant, traverse the chain of inherited discriminants
17129 -- and return the topmost discriminant.
17131 function Search_Derivation_Levels
17132 (Ti : Entity_Id;
17133 Discrim_Values : Elist_Id;
17134 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
17135 -- This is the routine that performs the recursive search of levels
17136 -- as described above.
17138 -------------------------------------
17139 -- Root_Corresponding_Discriminant --
17140 -------------------------------------
17142 function Root_Corresponding_Discriminant
17143 (Discr : Entity_Id) return Entity_Id
17145 D : Entity_Id;
17147 begin
17148 D := Discr;
17149 while Present (Corresponding_Discriminant (D)) loop
17150 D := Corresponding_Discriminant (D);
17151 end loop;
17153 return D;
17154 end Root_Corresponding_Discriminant;
17156 ------------------------------
17157 -- Search_Derivation_Levels --
17158 ------------------------------
17160 function Search_Derivation_Levels
17161 (Ti : Entity_Id;
17162 Discrim_Values : Elist_Id;
17163 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
17165 Assoc : Elmt_Id;
17166 Disc : Entity_Id;
17167 Result : Node_Or_Entity_Id;
17168 Result_Entity : Node_Id;
17170 begin
17171 -- If inappropriate type, return Error, this happens only in
17172 -- cascaded error situations, and we want to avoid a blow up.
17174 if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
17175 return Error;
17176 end if;
17178 -- Look deeper if possible. Use Stored_Constraints only for
17179 -- untagged types. For tagged types use the given constraint.
17180 -- This asymmetry needs explanation???
17182 if not Stored_Discrim_Values
17183 and then Present (Stored_Constraint (Ti))
17184 and then not Is_Tagged_Type (Ti)
17185 then
17186 Result :=
17187 Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
17188 else
17189 declare
17190 Td : constant Entity_Id := Etype (Ti);
17192 begin
17193 if Td = Ti then
17194 Result := Discriminant;
17196 else
17197 if Present (Stored_Constraint (Ti)) then
17198 Result :=
17199 Search_Derivation_Levels
17200 (Td, Stored_Constraint (Ti), True);
17201 else
17202 Result :=
17203 Search_Derivation_Levels
17204 (Td, Discrim_Values, Stored_Discrim_Values);
17205 end if;
17206 end if;
17207 end;
17208 end if;
17210 -- Extra underlying places to search, if not found above. For
17211 -- concurrent types, the relevant discriminant appears in the
17212 -- corresponding record. For a type derived from a private type
17213 -- without discriminant, the full view inherits the discriminants
17214 -- of the full view of the parent.
17216 if Result = Discriminant then
17217 if Is_Concurrent_Type (Ti)
17218 and then Present (Corresponding_Record_Type (Ti))
17219 then
17220 Result :=
17221 Search_Derivation_Levels (
17222 Corresponding_Record_Type (Ti),
17223 Discrim_Values,
17224 Stored_Discrim_Values);
17226 elsif Is_Private_Type (Ti)
17227 and then not Has_Discriminants (Ti)
17228 and then Present (Full_View (Ti))
17229 and then Etype (Full_View (Ti)) /= Ti
17230 then
17231 Result :=
17232 Search_Derivation_Levels (
17233 Full_View (Ti),
17234 Discrim_Values,
17235 Stored_Discrim_Values);
17236 end if;
17237 end if;
17239 -- If Result is not a (reference to a) discriminant, return it,
17240 -- otherwise set Result_Entity to the discriminant.
17242 if Nkind (Result) = N_Defining_Identifier then
17243 pragma Assert (Result = Discriminant);
17244 Result_Entity := Result;
17246 else
17247 if not Denotes_Discriminant (Result) then
17248 return Result;
17249 end if;
17251 Result_Entity := Entity (Result);
17252 end if;
17254 -- See if this level of derivation actually has discriminants because
17255 -- tagged derivations can add them, hence the lower levels need not
17256 -- have any.
17258 if not Has_Discriminants (Ti) then
17259 return Result;
17260 end if;
17262 -- Scan Ti's discriminants for Result_Entity, and return its
17263 -- corresponding value, if any.
17265 Result_Entity := Original_Record_Component (Result_Entity);
17267 Assoc := First_Elmt (Discrim_Values);
17269 if Stored_Discrim_Values then
17270 Disc := First_Stored_Discriminant (Ti);
17271 else
17272 Disc := First_Discriminant (Ti);
17273 end if;
17275 while Present (Disc) loop
17276 pragma Assert (Present (Assoc));
17278 if Original_Record_Component (Disc) = Result_Entity then
17279 return Node (Assoc);
17280 end if;
17282 Next_Elmt (Assoc);
17284 if Stored_Discrim_Values then
17285 Next_Stored_Discriminant (Disc);
17286 else
17287 Next_Discriminant (Disc);
17288 end if;
17289 end loop;
17291 -- Could not find it
17293 return Result;
17294 end Search_Derivation_Levels;
17296 -- Local Variables
17298 Result : Node_Or_Entity_Id;
17300 -- Start of processing for Get_Discriminant_Value
17302 begin
17303 -- ??? This routine is a gigantic mess and will be deleted. For the
17304 -- time being just test for the trivial case before calling recurse.
17306 if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
17307 declare
17308 D : Entity_Id;
17309 E : Elmt_Id;
17311 begin
17312 D := First_Discriminant (Typ_For_Constraint);
17313 E := First_Elmt (Constraint);
17314 while Present (D) loop
17315 if Chars (D) = Chars (Discriminant) then
17316 return Node (E);
17317 end if;
17319 Next_Discriminant (D);
17320 Next_Elmt (E);
17321 end loop;
17322 end;
17323 end if;
17325 Result := Search_Derivation_Levels
17326 (Typ_For_Constraint, Constraint, False);
17328 -- ??? hack to disappear when this routine is gone
17330 if Nkind (Result) = N_Defining_Identifier then
17331 declare
17332 D : Entity_Id;
17333 E : Elmt_Id;
17335 begin
17336 D := First_Discriminant (Typ_For_Constraint);
17337 E := First_Elmt (Constraint);
17338 while Present (D) loop
17339 if Root_Corresponding_Discriminant (D) = Discriminant then
17340 return Node (E);
17341 end if;
17343 Next_Discriminant (D);
17344 Next_Elmt (E);
17345 end loop;
17346 end;
17347 end if;
17349 pragma Assert (Nkind (Result) /= N_Defining_Identifier);
17350 return Result;
17351 end Get_Discriminant_Value;
17353 --------------------------
17354 -- Has_Range_Constraint --
17355 --------------------------
17357 function Has_Range_Constraint (N : Node_Id) return Boolean is
17358 C : constant Node_Id := Constraint (N);
17360 begin
17361 if Nkind (C) = N_Range_Constraint then
17362 return True;
17364 elsif Nkind (C) = N_Digits_Constraint then
17365 return
17366 Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
17367 or else Present (Range_Constraint (C));
17369 elsif Nkind (C) = N_Delta_Constraint then
17370 return Present (Range_Constraint (C));
17372 else
17373 return False;
17374 end if;
17375 end Has_Range_Constraint;
17377 ------------------------
17378 -- Inherit_Components --
17379 ------------------------
17381 function Inherit_Components
17382 (N : Node_Id;
17383 Parent_Base : Entity_Id;
17384 Derived_Base : Entity_Id;
17385 Is_Tagged : Boolean;
17386 Inherit_Discr : Boolean;
17387 Discs : Elist_Id) return Elist_Id
17389 Assoc_List : constant Elist_Id := New_Elmt_List;
17391 procedure Inherit_Component
17392 (Old_C : Entity_Id;
17393 Plain_Discrim : Boolean := False;
17394 Stored_Discrim : Boolean := False);
17395 -- Inherits component Old_C from Parent_Base to the Derived_Base. If
17396 -- Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
17397 -- True, Old_C is a stored discriminant. If they are both false then
17398 -- Old_C is a regular component.
17400 -----------------------
17401 -- Inherit_Component --
17402 -----------------------
17404 procedure Inherit_Component
17405 (Old_C : Entity_Id;
17406 Plain_Discrim : Boolean := False;
17407 Stored_Discrim : Boolean := False)
17409 procedure Set_Anonymous_Type (Id : Entity_Id);
17410 -- Id denotes the entity of an access discriminant or anonymous
17411 -- access component. Set the type of Id to either the same type of
17412 -- Old_C or create a new one depending on whether the parent and
17413 -- the child types are in the same scope.
17415 ------------------------
17416 -- Set_Anonymous_Type --
17417 ------------------------
17419 procedure Set_Anonymous_Type (Id : Entity_Id) is
17420 Old_Typ : constant Entity_Id := Etype (Old_C);
17422 begin
17423 if Scope (Parent_Base) = Scope (Derived_Base) then
17424 Set_Etype (Id, Old_Typ);
17426 -- The parent and the derived type are in two different scopes.
17427 -- Reuse the type of the original discriminant / component by
17428 -- copying it in order to preserve all attributes.
17430 else
17431 declare
17432 Typ : constant Entity_Id := New_Copy (Old_Typ);
17434 begin
17435 Set_Etype (Id, Typ);
17437 -- Since we do not generate component declarations for
17438 -- inherited components, associate the itype with the
17439 -- derived type.
17441 Set_Associated_Node_For_Itype (Typ, Parent (Derived_Base));
17442 Set_Scope (Typ, Derived_Base);
17443 end;
17444 end if;
17445 end Set_Anonymous_Type;
17447 -- Local variables and constants
17449 New_C : constant Entity_Id := New_Copy (Old_C);
17451 Corr_Discrim : Entity_Id;
17452 Discrim : Entity_Id;
17454 -- Start of processing for Inherit_Component
17456 begin
17457 pragma Assert (not Is_Tagged or not Stored_Discrim);
17459 Set_Parent (New_C, Parent (Old_C));
17461 -- Regular discriminants and components must be inserted in the scope
17462 -- of the Derived_Base. Do it here.
17464 if not Stored_Discrim then
17465 Enter_Name (New_C);
17466 end if;
17468 -- For tagged types the Original_Record_Component must point to
17469 -- whatever this field was pointing to in the parent type. This has
17470 -- already been achieved by the call to New_Copy above.
17472 if not Is_Tagged then
17473 Set_Original_Record_Component (New_C, New_C);
17474 end if;
17476 -- Set the proper type of an access discriminant
17478 if Ekind (New_C) = E_Discriminant
17479 and then Ekind (Etype (New_C)) = E_Anonymous_Access_Type
17480 then
17481 Set_Anonymous_Type (New_C);
17482 end if;
17484 -- If we have inherited a component then see if its Etype contains
17485 -- references to Parent_Base discriminants. In this case, replace
17486 -- these references with the constraints given in Discs. We do not
17487 -- do this for the partial view of private types because this is
17488 -- not needed (only the components of the full view will be used
17489 -- for code generation) and cause problem. We also avoid this
17490 -- transformation in some error situations.
17492 if Ekind (New_C) = E_Component then
17494 -- Set the proper type of an anonymous access component
17496 if Ekind (Etype (New_C)) = E_Anonymous_Access_Type then
17497 Set_Anonymous_Type (New_C);
17499 elsif (Is_Private_Type (Derived_Base)
17500 and then not Is_Generic_Type (Derived_Base))
17501 or else (Is_Empty_Elmt_List (Discs)
17502 and then not Expander_Active)
17503 then
17504 Set_Etype (New_C, Etype (Old_C));
17506 else
17507 -- The current component introduces a circularity of the
17508 -- following kind:
17510 -- limited with Pack_2;
17511 -- package Pack_1 is
17512 -- type T_1 is tagged record
17513 -- Comp : access Pack_2.T_2;
17514 -- ...
17515 -- end record;
17516 -- end Pack_1;
17518 -- with Pack_1;
17519 -- package Pack_2 is
17520 -- type T_2 is new Pack_1.T_1 with ...;
17521 -- end Pack_2;
17523 Set_Etype
17524 (New_C,
17525 Constrain_Component_Type
17526 (Old_C, Derived_Base, N, Parent_Base, Discs));
17527 end if;
17528 end if;
17530 -- In derived tagged types it is illegal to reference a non
17531 -- discriminant component in the parent type. To catch this, mark
17532 -- these components with an Ekind of E_Void. This will be reset in
17533 -- Record_Type_Definition after processing the record extension of
17534 -- the derived type.
17536 -- If the declaration is a private extension, there is no further
17537 -- record extension to process, and the components retain their
17538 -- current kind, because they are visible at this point.
17540 if Is_Tagged and then Ekind (New_C) = E_Component
17541 and then Nkind (N) /= N_Private_Extension_Declaration
17542 then
17543 Set_Ekind (New_C, E_Void);
17544 end if;
17546 if Plain_Discrim then
17547 Set_Corresponding_Discriminant (New_C, Old_C);
17548 Build_Discriminal (New_C);
17550 -- If we are explicitly inheriting a stored discriminant it will be
17551 -- completely hidden.
17553 elsif Stored_Discrim then
17554 Set_Corresponding_Discriminant (New_C, Empty);
17555 Set_Discriminal (New_C, Empty);
17556 Set_Is_Completely_Hidden (New_C);
17558 -- Set the Original_Record_Component of each discriminant in the
17559 -- derived base to point to the corresponding stored that we just
17560 -- created.
17562 Discrim := First_Discriminant (Derived_Base);
17563 while Present (Discrim) loop
17564 Corr_Discrim := Corresponding_Discriminant (Discrim);
17566 -- Corr_Discrim could be missing in an error situation
17568 if Present (Corr_Discrim)
17569 and then Original_Record_Component (Corr_Discrim) = Old_C
17570 then
17571 Set_Original_Record_Component (Discrim, New_C);
17572 end if;
17574 Next_Discriminant (Discrim);
17575 end loop;
17577 Append_Entity (New_C, Derived_Base);
17578 end if;
17580 if not Is_Tagged then
17581 Append_Elmt (Old_C, Assoc_List);
17582 Append_Elmt (New_C, Assoc_List);
17583 end if;
17584 end Inherit_Component;
17586 -- Variables local to Inherit_Component
17588 Loc : constant Source_Ptr := Sloc (N);
17590 Parent_Discrim : Entity_Id;
17591 Stored_Discrim : Entity_Id;
17592 D : Entity_Id;
17593 Component : Entity_Id;
17595 -- Start of processing for Inherit_Components
17597 begin
17598 if not Is_Tagged then
17599 Append_Elmt (Parent_Base, Assoc_List);
17600 Append_Elmt (Derived_Base, Assoc_List);
17601 end if;
17603 -- Inherit parent discriminants if needed
17605 if Inherit_Discr then
17606 Parent_Discrim := First_Discriminant (Parent_Base);
17607 while Present (Parent_Discrim) loop
17608 Inherit_Component (Parent_Discrim, Plain_Discrim => True);
17609 Next_Discriminant (Parent_Discrim);
17610 end loop;
17611 end if;
17613 -- Create explicit stored discrims for untagged types when necessary
17615 if not Has_Unknown_Discriminants (Derived_Base)
17616 and then Has_Discriminants (Parent_Base)
17617 and then not Is_Tagged
17618 and then
17619 (not Inherit_Discr
17620 or else First_Discriminant (Parent_Base) /=
17621 First_Stored_Discriminant (Parent_Base))
17622 then
17623 Stored_Discrim := First_Stored_Discriminant (Parent_Base);
17624 while Present (Stored_Discrim) loop
17625 Inherit_Component (Stored_Discrim, Stored_Discrim => True);
17626 Next_Stored_Discriminant (Stored_Discrim);
17627 end loop;
17628 end if;
17630 -- See if we can apply the second transformation for derived types, as
17631 -- explained in point 6. in the comments above Build_Derived_Record_Type
17632 -- This is achieved by appending Derived_Base discriminants into Discs,
17633 -- which has the side effect of returning a non empty Discs list to the
17634 -- caller of Inherit_Components, which is what we want. This must be
17635 -- done for private derived types if there are explicit stored
17636 -- discriminants, to ensure that we can retrieve the values of the
17637 -- constraints provided in the ancestors.
17639 if Inherit_Discr
17640 and then Is_Empty_Elmt_List (Discs)
17641 and then Present (First_Discriminant (Derived_Base))
17642 and then
17643 (not Is_Private_Type (Derived_Base)
17644 or else Is_Completely_Hidden
17645 (First_Stored_Discriminant (Derived_Base))
17646 or else Is_Generic_Type (Derived_Base))
17647 then
17648 D := First_Discriminant (Derived_Base);
17649 while Present (D) loop
17650 Append_Elmt (New_Occurrence_Of (D, Loc), Discs);
17651 Next_Discriminant (D);
17652 end loop;
17653 end if;
17655 -- Finally, inherit non-discriminant components unless they are not
17656 -- visible because defined or inherited from the full view of the
17657 -- parent. Don't inherit the _parent field of the parent type.
17659 Component := First_Entity (Parent_Base);
17660 while Present (Component) loop
17662 -- Ada 2005 (AI-251): Do not inherit components associated with
17663 -- secondary tags of the parent.
17665 if Ekind (Component) = E_Component
17666 and then Present (Related_Type (Component))
17667 then
17668 null;
17670 elsif Ekind (Component) /= E_Component
17671 or else Chars (Component) = Name_uParent
17672 then
17673 null;
17675 -- If the derived type is within the parent type's declarative
17676 -- region, then the components can still be inherited even though
17677 -- they aren't visible at this point. This can occur for cases
17678 -- such as within public child units where the components must
17679 -- become visible upon entering the child unit's private part.
17681 elsif not Is_Visible_Component (Component)
17682 and then not In_Open_Scopes (Scope (Parent_Base))
17683 then
17684 null;
17686 elsif Ekind_In (Derived_Base, E_Private_Type,
17687 E_Limited_Private_Type)
17688 then
17689 null;
17691 else
17692 Inherit_Component (Component);
17693 end if;
17695 Next_Entity (Component);
17696 end loop;
17698 -- For tagged derived types, inherited discriminants cannot be used in
17699 -- component declarations of the record extension part. To achieve this
17700 -- we mark the inherited discriminants as not visible.
17702 if Is_Tagged and then Inherit_Discr then
17703 D := First_Discriminant (Derived_Base);
17704 while Present (D) loop
17705 Set_Is_Immediately_Visible (D, False);
17706 Next_Discriminant (D);
17707 end loop;
17708 end if;
17710 return Assoc_List;
17711 end Inherit_Components;
17713 -----------------------------
17714 -- Inherit_Predicate_Flags --
17715 -----------------------------
17717 procedure Inherit_Predicate_Flags (Subt, Par : Entity_Id) is
17718 begin
17719 Set_Has_Predicates (Subt, Has_Predicates (Par));
17720 Set_Has_Static_Predicate_Aspect
17721 (Subt, Has_Static_Predicate_Aspect (Par));
17722 Set_Has_Dynamic_Predicate_Aspect
17723 (Subt, Has_Dynamic_Predicate_Aspect (Par));
17724 end Inherit_Predicate_Flags;
17726 ----------------------
17727 -- Is_EVF_Procedure --
17728 ----------------------
17730 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean is
17731 Formal : Entity_Id;
17733 begin
17734 -- Examine the formals of an Extensions_Visible False procedure looking
17735 -- for a controlling OUT parameter.
17737 if Ekind (Subp) = E_Procedure
17738 and then Extensions_Visible_Status (Subp) = Extensions_Visible_False
17739 then
17740 Formal := First_Formal (Subp);
17741 while Present (Formal) loop
17742 if Ekind (Formal) = E_Out_Parameter
17743 and then Is_Controlling_Formal (Formal)
17744 then
17745 return True;
17746 end if;
17748 Next_Formal (Formal);
17749 end loop;
17750 end if;
17752 return False;
17753 end Is_EVF_Procedure;
17755 -----------------------
17756 -- Is_Null_Extension --
17757 -----------------------
17759 function Is_Null_Extension (T : Entity_Id) return Boolean is
17760 Type_Decl : constant Node_Id := Parent (Base_Type (T));
17761 Comp_List : Node_Id;
17762 Comp : Node_Id;
17764 begin
17765 if Nkind (Type_Decl) /= N_Full_Type_Declaration
17766 or else not Is_Tagged_Type (T)
17767 or else Nkind (Type_Definition (Type_Decl)) /=
17768 N_Derived_Type_Definition
17769 or else No (Record_Extension_Part (Type_Definition (Type_Decl)))
17770 then
17771 return False;
17772 end if;
17774 Comp_List :=
17775 Component_List (Record_Extension_Part (Type_Definition (Type_Decl)));
17777 if Present (Discriminant_Specifications (Type_Decl)) then
17778 return False;
17780 elsif Present (Comp_List)
17781 and then Is_Non_Empty_List (Component_Items (Comp_List))
17782 then
17783 Comp := First (Component_Items (Comp_List));
17785 -- Only user-defined components are relevant. The component list
17786 -- may also contain a parent component and internal components
17787 -- corresponding to secondary tags, but these do not determine
17788 -- whether this is a null extension.
17790 while Present (Comp) loop
17791 if Comes_From_Source (Comp) then
17792 return False;
17793 end if;
17795 Next (Comp);
17796 end loop;
17798 return True;
17800 else
17801 return True;
17802 end if;
17803 end Is_Null_Extension;
17805 ------------------------------
17806 -- Is_Valid_Constraint_Kind --
17807 ------------------------------
17809 function Is_Valid_Constraint_Kind
17810 (T_Kind : Type_Kind;
17811 Constraint_Kind : Node_Kind) return Boolean
17813 begin
17814 case T_Kind is
17815 when Enumeration_Kind |
17816 Integer_Kind =>
17817 return Constraint_Kind = N_Range_Constraint;
17819 when Decimal_Fixed_Point_Kind =>
17820 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
17821 N_Range_Constraint);
17823 when Ordinary_Fixed_Point_Kind =>
17824 return Nkind_In (Constraint_Kind, N_Delta_Constraint,
17825 N_Range_Constraint);
17827 when Float_Kind =>
17828 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
17829 N_Range_Constraint);
17831 when Access_Kind |
17832 Array_Kind |
17833 E_Record_Type |
17834 E_Record_Subtype |
17835 Class_Wide_Kind |
17836 E_Incomplete_Type |
17837 Private_Kind |
17838 Concurrent_Kind =>
17839 return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
17841 when others =>
17842 return True; -- Error will be detected later
17843 end case;
17844 end Is_Valid_Constraint_Kind;
17846 --------------------------
17847 -- Is_Visible_Component --
17848 --------------------------
17850 function Is_Visible_Component
17851 (C : Entity_Id;
17852 N : Node_Id := Empty) return Boolean
17854 Original_Comp : Entity_Id := Empty;
17855 Original_Scope : Entity_Id;
17856 Type_Scope : Entity_Id;
17858 function Is_Local_Type (Typ : Entity_Id) return Boolean;
17859 -- Check whether parent type of inherited component is declared locally,
17860 -- possibly within a nested package or instance. The current scope is
17861 -- the derived record itself.
17863 -------------------
17864 -- Is_Local_Type --
17865 -------------------
17867 function Is_Local_Type (Typ : Entity_Id) return Boolean is
17868 Scop : Entity_Id;
17870 begin
17871 Scop := Scope (Typ);
17872 while Present (Scop)
17873 and then Scop /= Standard_Standard
17874 loop
17875 if Scop = Scope (Current_Scope) then
17876 return True;
17877 end if;
17879 Scop := Scope (Scop);
17880 end loop;
17882 return False;
17883 end Is_Local_Type;
17885 -- Start of processing for Is_Visible_Component
17887 begin
17888 if Ekind_In (C, E_Component, E_Discriminant) then
17889 Original_Comp := Original_Record_Component (C);
17890 end if;
17892 if No (Original_Comp) then
17894 -- Premature usage, or previous error
17896 return False;
17898 else
17899 Original_Scope := Scope (Original_Comp);
17900 Type_Scope := Scope (Base_Type (Scope (C)));
17901 end if;
17903 -- This test only concerns tagged types
17905 if not Is_Tagged_Type (Original_Scope) then
17906 return True;
17908 -- If it is _Parent or _Tag, there is no visibility issue
17910 elsif not Comes_From_Source (Original_Comp) then
17911 return True;
17913 -- Discriminants are visible unless the (private) type has unknown
17914 -- discriminants. If the discriminant reference is inserted for a
17915 -- discriminant check on a full view it is also visible.
17917 elsif Ekind (Original_Comp) = E_Discriminant
17918 and then
17919 (not Has_Unknown_Discriminants (Original_Scope)
17920 or else (Present (N)
17921 and then Nkind (N) = N_Selected_Component
17922 and then Nkind (Prefix (N)) = N_Type_Conversion
17923 and then not Comes_From_Source (Prefix (N))))
17924 then
17925 return True;
17927 -- In the body of an instantiation, no need to check for the visibility
17928 -- of a component.
17930 elsif In_Instance_Body then
17931 return True;
17933 -- If the component has been declared in an ancestor which is currently
17934 -- a private type, then it is not visible. The same applies if the
17935 -- component's containing type is not in an open scope and the original
17936 -- component's enclosing type is a visible full view of a private type
17937 -- (which can occur in cases where an attempt is being made to reference
17938 -- a component in a sibling package that is inherited from a visible
17939 -- component of a type in an ancestor package; the component in the
17940 -- sibling package should not be visible even though the component it
17941 -- inherited from is visible). This does not apply however in the case
17942 -- where the scope of the type is a private child unit, or when the
17943 -- parent comes from a local package in which the ancestor is currently
17944 -- visible. The latter suppression of visibility is needed for cases
17945 -- that are tested in B730006.
17947 elsif Is_Private_Type (Original_Scope)
17948 or else
17949 (not Is_Private_Descendant (Type_Scope)
17950 and then not In_Open_Scopes (Type_Scope)
17951 and then Has_Private_Declaration (Original_Scope))
17952 then
17953 -- If the type derives from an entity in a formal package, there
17954 -- are no additional visible components.
17956 if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
17957 N_Formal_Package_Declaration
17958 then
17959 return False;
17961 -- if we are not in the private part of the current package, there
17962 -- are no additional visible components.
17964 elsif Ekind (Scope (Current_Scope)) = E_Package
17965 and then not In_Private_Part (Scope (Current_Scope))
17966 then
17967 return False;
17968 else
17969 return
17970 Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
17971 and then In_Open_Scopes (Scope (Original_Scope))
17972 and then Is_Local_Type (Type_Scope);
17973 end if;
17975 -- There is another weird way in which a component may be invisible when
17976 -- the private and the full view are not derived from the same ancestor.
17977 -- Here is an example :
17979 -- type A1 is tagged record F1 : integer; end record;
17980 -- type A2 is new A1 with record F2 : integer; end record;
17981 -- type T is new A1 with private;
17982 -- private
17983 -- type T is new A2 with null record;
17985 -- In this case, the full view of T inherits F1 and F2 but the private
17986 -- view inherits only F1
17988 else
17989 declare
17990 Ancestor : Entity_Id := Scope (C);
17992 begin
17993 loop
17994 if Ancestor = Original_Scope then
17995 return True;
17996 elsif Ancestor = Etype (Ancestor) then
17997 return False;
17998 end if;
18000 Ancestor := Etype (Ancestor);
18001 end loop;
18002 end;
18003 end if;
18004 end Is_Visible_Component;
18006 --------------------------
18007 -- Make_Class_Wide_Type --
18008 --------------------------
18010 procedure Make_Class_Wide_Type (T : Entity_Id) is
18011 CW_Type : Entity_Id;
18012 CW_Name : Name_Id;
18013 Next_E : Entity_Id;
18015 begin
18016 if Present (Class_Wide_Type (T)) then
18018 -- The class-wide type is a partially decorated entity created for a
18019 -- unanalyzed tagged type referenced through a limited with clause.
18020 -- When the tagged type is analyzed, its class-wide type needs to be
18021 -- redecorated. Note that we reuse the entity created by Decorate_
18022 -- Tagged_Type in order to preserve all links.
18024 if Materialize_Entity (Class_Wide_Type (T)) then
18025 CW_Type := Class_Wide_Type (T);
18026 Set_Materialize_Entity (CW_Type, False);
18028 -- The class wide type can have been defined by the partial view, in
18029 -- which case everything is already done.
18031 else
18032 return;
18033 end if;
18035 -- Default case, we need to create a new class-wide type
18037 else
18038 CW_Type :=
18039 New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
18040 end if;
18042 -- Inherit root type characteristics
18044 CW_Name := Chars (CW_Type);
18045 Next_E := Next_Entity (CW_Type);
18046 Copy_Node (T, CW_Type);
18047 Set_Comes_From_Source (CW_Type, False);
18048 Set_Chars (CW_Type, CW_Name);
18049 Set_Parent (CW_Type, Parent (T));
18050 Set_Next_Entity (CW_Type, Next_E);
18052 -- Ensure we have a new freeze node for the class-wide type. The partial
18053 -- view may have freeze action of its own, requiring a proper freeze
18054 -- node, and the same freeze node cannot be shared between the two
18055 -- types.
18057 Set_Has_Delayed_Freeze (CW_Type);
18058 Set_Freeze_Node (CW_Type, Empty);
18060 -- Customize the class-wide type: It has no prim. op., it cannot be
18061 -- abstract and its Etype points back to the specific root type.
18063 Set_Ekind (CW_Type, E_Class_Wide_Type);
18064 Set_Is_Tagged_Type (CW_Type, True);
18065 Set_Direct_Primitive_Operations (CW_Type, New_Elmt_List);
18066 Set_Is_Abstract_Type (CW_Type, False);
18067 Set_Is_Constrained (CW_Type, False);
18068 Set_Is_First_Subtype (CW_Type, Is_First_Subtype (T));
18069 Set_Default_SSO (CW_Type);
18071 if Ekind (T) = E_Class_Wide_Subtype then
18072 Set_Etype (CW_Type, Etype (Base_Type (T)));
18073 else
18074 Set_Etype (CW_Type, T);
18075 end if;
18077 Set_No_Tagged_Streams_Pragma (CW_Type, No_Tagged_Streams);
18079 -- If this is the class_wide type of a constrained subtype, it does
18080 -- not have discriminants.
18082 Set_Has_Discriminants (CW_Type,
18083 Has_Discriminants (T) and then not Is_Constrained (T));
18085 Set_Has_Unknown_Discriminants (CW_Type, True);
18086 Set_Class_Wide_Type (T, CW_Type);
18087 Set_Equivalent_Type (CW_Type, Empty);
18089 -- The class-wide type of a class-wide type is itself (RM 3.9(14))
18091 Set_Class_Wide_Type (CW_Type, CW_Type);
18092 end Make_Class_Wide_Type;
18094 ----------------
18095 -- Make_Index --
18096 ----------------
18098 procedure Make_Index
18099 (N : Node_Id;
18100 Related_Nod : Node_Id;
18101 Related_Id : Entity_Id := Empty;
18102 Suffix_Index : Nat := 1;
18103 In_Iter_Schm : Boolean := False)
18105 R : Node_Id;
18106 T : Entity_Id;
18107 Def_Id : Entity_Id := Empty;
18108 Found : Boolean := False;
18110 begin
18111 -- For a discrete range used in a constrained array definition and
18112 -- defined by a range, an implicit conversion to the predefined type
18113 -- INTEGER is assumed if each bound is either a numeric literal, a named
18114 -- number, or an attribute, and the type of both bounds (prior to the
18115 -- implicit conversion) is the type universal_integer. Otherwise, both
18116 -- bounds must be of the same discrete type, other than universal
18117 -- integer; this type must be determinable independently of the
18118 -- context, but using the fact that the type must be discrete and that
18119 -- both bounds must have the same type.
18121 -- Character literals also have a universal type in the absence of
18122 -- of additional context, and are resolved to Standard_Character.
18124 if Nkind (N) = N_Range then
18126 -- The index is given by a range constraint. The bounds are known
18127 -- to be of a consistent type.
18129 if not Is_Overloaded (N) then
18130 T := Etype (N);
18132 -- For universal bounds, choose the specific predefined type
18134 if T = Universal_Integer then
18135 T := Standard_Integer;
18137 elsif T = Any_Character then
18138 Ambiguous_Character (Low_Bound (N));
18140 T := Standard_Character;
18141 end if;
18143 -- The node may be overloaded because some user-defined operators
18144 -- are available, but if a universal interpretation exists it is
18145 -- also the selected one.
18147 elsif Universal_Interpretation (N) = Universal_Integer then
18148 T := Standard_Integer;
18150 else
18151 T := Any_Type;
18153 declare
18154 Ind : Interp_Index;
18155 It : Interp;
18157 begin
18158 Get_First_Interp (N, Ind, It);
18159 while Present (It.Typ) loop
18160 if Is_Discrete_Type (It.Typ) then
18162 if Found
18163 and then not Covers (It.Typ, T)
18164 and then not Covers (T, It.Typ)
18165 then
18166 Error_Msg_N ("ambiguous bounds in discrete range", N);
18167 exit;
18168 else
18169 T := It.Typ;
18170 Found := True;
18171 end if;
18172 end if;
18174 Get_Next_Interp (Ind, It);
18175 end loop;
18177 if T = Any_Type then
18178 Error_Msg_N ("discrete type required for range", N);
18179 Set_Etype (N, Any_Type);
18180 return;
18182 elsif T = Universal_Integer then
18183 T := Standard_Integer;
18184 end if;
18185 end;
18186 end if;
18188 if not Is_Discrete_Type (T) then
18189 Error_Msg_N ("discrete type required for range", N);
18190 Set_Etype (N, Any_Type);
18191 return;
18192 end if;
18194 if Nkind (Low_Bound (N)) = N_Attribute_Reference
18195 and then Attribute_Name (Low_Bound (N)) = Name_First
18196 and then Is_Entity_Name (Prefix (Low_Bound (N)))
18197 and then Is_Type (Entity (Prefix (Low_Bound (N))))
18198 and then Is_Discrete_Type (Entity (Prefix (Low_Bound (N))))
18199 then
18200 -- The type of the index will be the type of the prefix, as long
18201 -- as the upper bound is 'Last of the same type.
18203 Def_Id := Entity (Prefix (Low_Bound (N)));
18205 if Nkind (High_Bound (N)) /= N_Attribute_Reference
18206 or else Attribute_Name (High_Bound (N)) /= Name_Last
18207 or else not Is_Entity_Name (Prefix (High_Bound (N)))
18208 or else Entity (Prefix (High_Bound (N))) /= Def_Id
18209 then
18210 Def_Id := Empty;
18211 end if;
18212 end if;
18214 R := N;
18215 Process_Range_Expr_In_Decl (R, T, In_Iter_Schm => In_Iter_Schm);
18217 elsif Nkind (N) = N_Subtype_Indication then
18219 -- The index is given by a subtype with a range constraint
18221 T := Base_Type (Entity (Subtype_Mark (N)));
18223 if not Is_Discrete_Type (T) then
18224 Error_Msg_N ("discrete type required for range", N);
18225 Set_Etype (N, Any_Type);
18226 return;
18227 end if;
18229 R := Range_Expression (Constraint (N));
18231 Resolve (R, T);
18232 Process_Range_Expr_In_Decl
18233 (R, Entity (Subtype_Mark (N)), In_Iter_Schm => In_Iter_Schm);
18235 elsif Nkind (N) = N_Attribute_Reference then
18237 -- Catch beginner's error (use of attribute other than 'Range)
18239 if Attribute_Name (N) /= Name_Range then
18240 Error_Msg_N ("expect attribute ''Range", N);
18241 Set_Etype (N, Any_Type);
18242 return;
18243 end if;
18245 -- If the node denotes the range of a type mark, that is also the
18246 -- resulting type, and we do not need to create an Itype for it.
18248 if Is_Entity_Name (Prefix (N))
18249 and then Comes_From_Source (N)
18250 and then Is_Type (Entity (Prefix (N)))
18251 and then Is_Discrete_Type (Entity (Prefix (N)))
18252 then
18253 Def_Id := Entity (Prefix (N));
18254 end if;
18256 Analyze_And_Resolve (N);
18257 T := Etype (N);
18258 R := N;
18260 -- If none of the above, must be a subtype. We convert this to a
18261 -- range attribute reference because in the case of declared first
18262 -- named subtypes, the types in the range reference can be different
18263 -- from the type of the entity. A range attribute normalizes the
18264 -- reference and obtains the correct types for the bounds.
18266 -- This transformation is in the nature of an expansion, is only
18267 -- done if expansion is active. In particular, it is not done on
18268 -- formal generic types, because we need to retain the name of the
18269 -- original index for instantiation purposes.
18271 else
18272 if not Is_Entity_Name (N) or else not Is_Type (Entity (N)) then
18273 Error_Msg_N ("invalid subtype mark in discrete range ", N);
18274 Set_Etype (N, Any_Integer);
18275 return;
18277 else
18278 -- The type mark may be that of an incomplete type. It is only
18279 -- now that we can get the full view, previous analysis does
18280 -- not look specifically for a type mark.
18282 Set_Entity (N, Get_Full_View (Entity (N)));
18283 Set_Etype (N, Entity (N));
18284 Def_Id := Entity (N);
18286 if not Is_Discrete_Type (Def_Id) then
18287 Error_Msg_N ("discrete type required for index", N);
18288 Set_Etype (N, Any_Type);
18289 return;
18290 end if;
18291 end if;
18293 if Expander_Active then
18294 Rewrite (N,
18295 Make_Attribute_Reference (Sloc (N),
18296 Attribute_Name => Name_Range,
18297 Prefix => Relocate_Node (N)));
18299 -- The original was a subtype mark that does not freeze. This
18300 -- means that the rewritten version must not freeze either.
18302 Set_Must_Not_Freeze (N);
18303 Set_Must_Not_Freeze (Prefix (N));
18304 Analyze_And_Resolve (N);
18305 T := Etype (N);
18306 R := N;
18308 -- If expander is inactive, type is legal, nothing else to construct
18310 else
18311 return;
18312 end if;
18313 end if;
18315 if not Is_Discrete_Type (T) then
18316 Error_Msg_N ("discrete type required for range", N);
18317 Set_Etype (N, Any_Type);
18318 return;
18320 elsif T = Any_Type then
18321 Set_Etype (N, Any_Type);
18322 return;
18323 end if;
18325 -- We will now create the appropriate Itype to describe the range, but
18326 -- first a check. If we originally had a subtype, then we just label
18327 -- the range with this subtype. Not only is there no need to construct
18328 -- a new subtype, but it is wrong to do so for two reasons:
18330 -- 1. A legality concern, if we have a subtype, it must not freeze,
18331 -- and the Itype would cause freezing incorrectly
18333 -- 2. An efficiency concern, if we created an Itype, it would not be
18334 -- recognized as the same type for the purposes of eliminating
18335 -- checks in some circumstances.
18337 -- We signal this case by setting the subtype entity in Def_Id
18339 if No (Def_Id) then
18340 Def_Id :=
18341 Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
18342 Set_Etype (Def_Id, Base_Type (T));
18344 if Is_Signed_Integer_Type (T) then
18345 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
18347 elsif Is_Modular_Integer_Type (T) then
18348 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
18350 else
18351 Set_Ekind (Def_Id, E_Enumeration_Subtype);
18352 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
18353 Set_First_Literal (Def_Id, First_Literal (T));
18354 end if;
18356 Set_Size_Info (Def_Id, (T));
18357 Set_RM_Size (Def_Id, RM_Size (T));
18358 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
18360 Set_Scalar_Range (Def_Id, R);
18361 Conditional_Delay (Def_Id, T);
18363 if Nkind (N) = N_Subtype_Indication then
18364 Inherit_Predicate_Flags (Def_Id, Entity (Subtype_Mark (N)));
18365 end if;
18367 -- In the subtype indication case, if the immediate parent of the
18368 -- new subtype is non-static, then the subtype we create is non-
18369 -- static, even if its bounds are static.
18371 if Nkind (N) = N_Subtype_Indication
18372 and then not Is_OK_Static_Subtype (Entity (Subtype_Mark (N)))
18373 then
18374 Set_Is_Non_Static_Subtype (Def_Id);
18375 end if;
18376 end if;
18378 -- Final step is to label the index with this constructed type
18380 Set_Etype (N, Def_Id);
18381 end Make_Index;
18383 ------------------------------
18384 -- Modular_Type_Declaration --
18385 ------------------------------
18387 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
18388 Mod_Expr : constant Node_Id := Expression (Def);
18389 M_Val : Uint;
18391 procedure Set_Modular_Size (Bits : Int);
18392 -- Sets RM_Size to Bits, and Esize to normal word size above this
18394 ----------------------
18395 -- Set_Modular_Size --
18396 ----------------------
18398 procedure Set_Modular_Size (Bits : Int) is
18399 begin
18400 Set_RM_Size (T, UI_From_Int (Bits));
18402 if Bits <= 8 then
18403 Init_Esize (T, 8);
18405 elsif Bits <= 16 then
18406 Init_Esize (T, 16);
18408 elsif Bits <= 32 then
18409 Init_Esize (T, 32);
18411 else
18412 Init_Esize (T, System_Max_Binary_Modulus_Power);
18413 end if;
18415 if not Non_Binary_Modulus (T) and then Esize (T) = RM_Size (T) then
18416 Set_Is_Known_Valid (T);
18417 end if;
18418 end Set_Modular_Size;
18420 -- Start of processing for Modular_Type_Declaration
18422 begin
18423 -- If the mod expression is (exactly) 2 * literal, where literal is
18424 -- 64 or less,then almost certainly the * was meant to be **. Warn.
18426 if Warn_On_Suspicious_Modulus_Value
18427 and then Nkind (Mod_Expr) = N_Op_Multiply
18428 and then Nkind (Left_Opnd (Mod_Expr)) = N_Integer_Literal
18429 and then Intval (Left_Opnd (Mod_Expr)) = Uint_2
18430 and then Nkind (Right_Opnd (Mod_Expr)) = N_Integer_Literal
18431 and then Intval (Right_Opnd (Mod_Expr)) <= Uint_64
18432 then
18433 Error_Msg_N
18434 ("suspicious MOD value, was '*'* intended'??M?", Mod_Expr);
18435 end if;
18437 -- Proceed with analysis of mod expression
18439 Analyze_And_Resolve (Mod_Expr, Any_Integer);
18440 Set_Etype (T, T);
18441 Set_Ekind (T, E_Modular_Integer_Type);
18442 Init_Alignment (T);
18443 Set_Is_Constrained (T);
18445 if not Is_OK_Static_Expression (Mod_Expr) then
18446 Flag_Non_Static_Expr
18447 ("non-static expression used for modular type bound!", Mod_Expr);
18448 M_Val := 2 ** System_Max_Binary_Modulus_Power;
18449 else
18450 M_Val := Expr_Value (Mod_Expr);
18451 end if;
18453 if M_Val < 1 then
18454 Error_Msg_N ("modulus value must be positive", Mod_Expr);
18455 M_Val := 2 ** System_Max_Binary_Modulus_Power;
18456 end if;
18458 if M_Val > 2 ** Standard_Long_Integer_Size then
18459 Check_Restriction (No_Long_Long_Integers, Mod_Expr);
18460 end if;
18462 Set_Modulus (T, M_Val);
18464 -- Create bounds for the modular type based on the modulus given in
18465 -- the type declaration and then analyze and resolve those bounds.
18467 Set_Scalar_Range (T,
18468 Make_Range (Sloc (Mod_Expr),
18469 Low_Bound => Make_Integer_Literal (Sloc (Mod_Expr), 0),
18470 High_Bound => Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
18472 -- Properly analyze the literals for the range. We do this manually
18473 -- because we can't go calling Resolve, since we are resolving these
18474 -- bounds with the type, and this type is certainly not complete yet.
18476 Set_Etype (Low_Bound (Scalar_Range (T)), T);
18477 Set_Etype (High_Bound (Scalar_Range (T)), T);
18478 Set_Is_Static_Expression (Low_Bound (Scalar_Range (T)));
18479 Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
18481 -- Loop through powers of two to find number of bits required
18483 for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
18485 -- Binary case
18487 if M_Val = 2 ** Bits then
18488 Set_Modular_Size (Bits);
18489 return;
18491 -- Non-binary case
18493 elsif M_Val < 2 ** Bits then
18494 Check_SPARK_05_Restriction ("modulus should be a power of 2", T);
18495 Set_Non_Binary_Modulus (T);
18497 if Bits > System_Max_Nonbinary_Modulus_Power then
18498 Error_Msg_Uint_1 :=
18499 UI_From_Int (System_Max_Nonbinary_Modulus_Power);
18500 Error_Msg_F
18501 ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
18502 Set_Modular_Size (System_Max_Binary_Modulus_Power);
18503 return;
18505 else
18506 -- In the non-binary case, set size as per RM 13.3(55)
18508 Set_Modular_Size (Bits);
18509 return;
18510 end if;
18511 end if;
18513 end loop;
18515 -- If we fall through, then the size exceed System.Max_Binary_Modulus
18516 -- so we just signal an error and set the maximum size.
18518 Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
18519 Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
18521 Set_Modular_Size (System_Max_Binary_Modulus_Power);
18522 Init_Alignment (T);
18524 end Modular_Type_Declaration;
18526 --------------------------
18527 -- New_Concatenation_Op --
18528 --------------------------
18530 procedure New_Concatenation_Op (Typ : Entity_Id) is
18531 Loc : constant Source_Ptr := Sloc (Typ);
18532 Op : Entity_Id;
18534 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
18535 -- Create abbreviated declaration for the formal of a predefined
18536 -- Operator 'Op' of type 'Typ'
18538 --------------------
18539 -- Make_Op_Formal --
18540 --------------------
18542 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
18543 Formal : Entity_Id;
18544 begin
18545 Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
18546 Set_Etype (Formal, Typ);
18547 Set_Mechanism (Formal, Default_Mechanism);
18548 return Formal;
18549 end Make_Op_Formal;
18551 -- Start of processing for New_Concatenation_Op
18553 begin
18554 Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
18556 Set_Ekind (Op, E_Operator);
18557 Set_Scope (Op, Current_Scope);
18558 Set_Etype (Op, Typ);
18559 Set_Homonym (Op, Get_Name_Entity_Id (Name_Op_Concat));
18560 Set_Is_Immediately_Visible (Op);
18561 Set_Is_Intrinsic_Subprogram (Op);
18562 Set_Has_Completion (Op);
18563 Append_Entity (Op, Current_Scope);
18565 Set_Name_Entity_Id (Name_Op_Concat, Op);
18567 Append_Entity (Make_Op_Formal (Typ, Op), Op);
18568 Append_Entity (Make_Op_Formal (Typ, Op), Op);
18569 end New_Concatenation_Op;
18571 -------------------------
18572 -- OK_For_Limited_Init --
18573 -------------------------
18575 -- ???Check all calls of this, and compare the conditions under which it's
18576 -- called.
18578 function OK_For_Limited_Init
18579 (Typ : Entity_Id;
18580 Exp : Node_Id) return Boolean
18582 begin
18583 return Is_CPP_Constructor_Call (Exp)
18584 or else (Ada_Version >= Ada_2005
18585 and then not Debug_Flag_Dot_L
18586 and then OK_For_Limited_Init_In_05 (Typ, Exp));
18587 end OK_For_Limited_Init;
18589 -------------------------------
18590 -- OK_For_Limited_Init_In_05 --
18591 -------------------------------
18593 function OK_For_Limited_Init_In_05
18594 (Typ : Entity_Id;
18595 Exp : Node_Id) return Boolean
18597 begin
18598 -- An object of a limited interface type can be initialized with any
18599 -- expression of a nonlimited descendant type.
18601 if Is_Class_Wide_Type (Typ)
18602 and then Is_Limited_Interface (Typ)
18603 and then not Is_Limited_Type (Etype (Exp))
18604 then
18605 return True;
18606 end if;
18608 -- Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
18609 -- case of limited aggregates (including extension aggregates), and
18610 -- function calls. The function call may have been given in prefixed
18611 -- notation, in which case the original node is an indexed component.
18612 -- If the function is parameterless, the original node was an explicit
18613 -- dereference. The function may also be parameterless, in which case
18614 -- the source node is just an identifier.
18616 case Nkind (Original_Node (Exp)) is
18617 when N_Aggregate | N_Extension_Aggregate | N_Function_Call | N_Op =>
18618 return True;
18620 when N_Identifier =>
18621 return Present (Entity (Original_Node (Exp)))
18622 and then Ekind (Entity (Original_Node (Exp))) = E_Function;
18624 when N_Qualified_Expression =>
18625 return
18626 OK_For_Limited_Init_In_05
18627 (Typ, Expression (Original_Node (Exp)));
18629 -- Ada 2005 (AI-251): If a class-wide interface object is initialized
18630 -- with a function call, the expander has rewritten the call into an
18631 -- N_Type_Conversion node to force displacement of the pointer to
18632 -- reference the component containing the secondary dispatch table.
18633 -- Otherwise a type conversion is not a legal context.
18634 -- A return statement for a build-in-place function returning a
18635 -- synchronized type also introduces an unchecked conversion.
18637 when N_Type_Conversion |
18638 N_Unchecked_Type_Conversion =>
18639 return not Comes_From_Source (Exp)
18640 and then
18641 OK_For_Limited_Init_In_05
18642 (Typ, Expression (Original_Node (Exp)));
18644 when N_Indexed_Component |
18645 N_Selected_Component |
18646 N_Explicit_Dereference =>
18647 return Nkind (Exp) = N_Function_Call;
18649 -- A use of 'Input is a function call, hence allowed. Normally the
18650 -- attribute will be changed to a call, but the attribute by itself
18651 -- can occur with -gnatc.
18653 when N_Attribute_Reference =>
18654 return Attribute_Name (Original_Node (Exp)) = Name_Input;
18656 -- For a case expression, all dependent expressions must be legal
18658 when N_Case_Expression =>
18659 declare
18660 Alt : Node_Id;
18662 begin
18663 Alt := First (Alternatives (Original_Node (Exp)));
18664 while Present (Alt) loop
18665 if not OK_For_Limited_Init_In_05 (Typ, Expression (Alt)) then
18666 return False;
18667 end if;
18669 Next (Alt);
18670 end loop;
18672 return True;
18673 end;
18675 -- For an if expression, all dependent expressions must be legal
18677 when N_If_Expression =>
18678 declare
18679 Then_Expr : constant Node_Id :=
18680 Next (First (Expressions (Original_Node (Exp))));
18681 Else_Expr : constant Node_Id := Next (Then_Expr);
18682 begin
18683 return OK_For_Limited_Init_In_05 (Typ, Then_Expr)
18684 and then
18685 OK_For_Limited_Init_In_05 (Typ, Else_Expr);
18686 end;
18688 when others =>
18689 return False;
18690 end case;
18691 end OK_For_Limited_Init_In_05;
18693 -------------------------------------------
18694 -- Ordinary_Fixed_Point_Type_Declaration --
18695 -------------------------------------------
18697 procedure Ordinary_Fixed_Point_Type_Declaration
18698 (T : Entity_Id;
18699 Def : Node_Id)
18701 Loc : constant Source_Ptr := Sloc (Def);
18702 Delta_Expr : constant Node_Id := Delta_Expression (Def);
18703 RRS : constant Node_Id := Real_Range_Specification (Def);
18704 Implicit_Base : Entity_Id;
18705 Delta_Val : Ureal;
18706 Small_Val : Ureal;
18707 Low_Val : Ureal;
18708 High_Val : Ureal;
18710 begin
18711 Check_Restriction (No_Fixed_Point, Def);
18713 -- Create implicit base type
18715 Implicit_Base :=
18716 Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
18717 Set_Etype (Implicit_Base, Implicit_Base);
18719 -- Analyze and process delta expression
18721 Analyze_And_Resolve (Delta_Expr, Any_Real);
18723 Check_Delta_Expression (Delta_Expr);
18724 Delta_Val := Expr_Value_R (Delta_Expr);
18726 Set_Delta_Value (Implicit_Base, Delta_Val);
18728 -- Compute default small from given delta, which is the largest power
18729 -- of two that does not exceed the given delta value.
18731 declare
18732 Tmp : Ureal;
18733 Scale : Int;
18735 begin
18736 Tmp := Ureal_1;
18737 Scale := 0;
18739 if Delta_Val < Ureal_1 then
18740 while Delta_Val < Tmp loop
18741 Tmp := Tmp / Ureal_2;
18742 Scale := Scale + 1;
18743 end loop;
18745 else
18746 loop
18747 Tmp := Tmp * Ureal_2;
18748 exit when Tmp > Delta_Val;
18749 Scale := Scale - 1;
18750 end loop;
18751 end if;
18753 Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
18754 end;
18756 Set_Small_Value (Implicit_Base, Small_Val);
18758 -- If no range was given, set a dummy range
18760 if RRS <= Empty_Or_Error then
18761 Low_Val := -Small_Val;
18762 High_Val := Small_Val;
18764 -- Otherwise analyze and process given range
18766 else
18767 declare
18768 Low : constant Node_Id := Low_Bound (RRS);
18769 High : constant Node_Id := High_Bound (RRS);
18771 begin
18772 Analyze_And_Resolve (Low, Any_Real);
18773 Analyze_And_Resolve (High, Any_Real);
18774 Check_Real_Bound (Low);
18775 Check_Real_Bound (High);
18777 -- Obtain and set the range
18779 Low_Val := Expr_Value_R (Low);
18780 High_Val := Expr_Value_R (High);
18782 if Low_Val > High_Val then
18783 Error_Msg_NE ("??fixed point type& has null range", Def, T);
18784 end if;
18785 end;
18786 end if;
18788 -- The range for both the implicit base and the declared first subtype
18789 -- cannot be set yet, so we use the special routine Set_Fixed_Range to
18790 -- set a temporary range in place. Note that the bounds of the base
18791 -- type will be widened to be symmetrical and to fill the available
18792 -- bits when the type is frozen.
18794 -- We could do this with all discrete types, and probably should, but
18795 -- we absolutely have to do it for fixed-point, since the end-points
18796 -- of the range and the size are determined by the small value, which
18797 -- could be reset before the freeze point.
18799 Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
18800 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
18802 -- Complete definition of first subtype. The inheritance of the rep item
18803 -- chain ensures that SPARK-related pragmas are not clobbered when the
18804 -- ordinary fixed point type acts as a full view of a private type.
18806 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
18807 Set_Etype (T, Implicit_Base);
18808 Init_Size_Align (T);
18809 Inherit_Rep_Item_Chain (T, Implicit_Base);
18810 Set_Small_Value (T, Small_Val);
18811 Set_Delta_Value (T, Delta_Val);
18812 Set_Is_Constrained (T);
18813 end Ordinary_Fixed_Point_Type_Declaration;
18815 ----------------------------------
18816 -- Preanalyze_Assert_Expression --
18817 ----------------------------------
18819 procedure Preanalyze_Assert_Expression (N : Node_Id; T : Entity_Id) is
18820 begin
18821 In_Assertion_Expr := In_Assertion_Expr + 1;
18822 Preanalyze_Spec_Expression (N, T);
18823 In_Assertion_Expr := In_Assertion_Expr - 1;
18824 end Preanalyze_Assert_Expression;
18826 -----------------------------------
18827 -- Preanalyze_Default_Expression --
18828 -----------------------------------
18830 procedure Preanalyze_Default_Expression (N : Node_Id; T : Entity_Id) is
18831 Save_In_Default_Expr : constant Boolean := In_Default_Expr;
18832 begin
18833 In_Default_Expr := True;
18834 Preanalyze_Spec_Expression (N, T);
18835 In_Default_Expr := Save_In_Default_Expr;
18836 end Preanalyze_Default_Expression;
18838 --------------------------------
18839 -- Preanalyze_Spec_Expression --
18840 --------------------------------
18842 procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
18843 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
18844 begin
18845 In_Spec_Expression := True;
18846 Preanalyze_And_Resolve (N, T);
18847 In_Spec_Expression := Save_In_Spec_Expression;
18848 end Preanalyze_Spec_Expression;
18850 ----------------------------------------
18851 -- Prepare_Private_Subtype_Completion --
18852 ----------------------------------------
18854 procedure Prepare_Private_Subtype_Completion
18855 (Id : Entity_Id;
18856 Related_Nod : Node_Id)
18858 Id_B : constant Entity_Id := Base_Type (Id);
18859 Full_B : Entity_Id := Full_View (Id_B);
18860 Full : Entity_Id;
18862 begin
18863 if Present (Full_B) then
18865 -- Get to the underlying full view if necessary
18867 if Is_Private_Type (Full_B)
18868 and then Present (Underlying_Full_View (Full_B))
18869 then
18870 Full_B := Underlying_Full_View (Full_B);
18871 end if;
18873 -- The Base_Type is already completed, we can complete the subtype
18874 -- now. We have to create a new entity with the same name, Thus we
18875 -- can't use Create_Itype.
18877 Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
18878 Set_Is_Itype (Full);
18879 Set_Associated_Node_For_Itype (Full, Related_Nod);
18880 Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
18881 end if;
18883 -- The parent subtype may be private, but the base might not, in some
18884 -- nested instances. In that case, the subtype does not need to be
18885 -- exchanged. It would still be nice to make private subtypes and their
18886 -- bases consistent at all times ???
18888 if Is_Private_Type (Id_B) then
18889 Append_Elmt (Id, Private_Dependents (Id_B));
18890 end if;
18891 end Prepare_Private_Subtype_Completion;
18893 ---------------------------
18894 -- Process_Discriminants --
18895 ---------------------------
18897 procedure Process_Discriminants
18898 (N : Node_Id;
18899 Prev : Entity_Id := Empty)
18901 Elist : constant Elist_Id := New_Elmt_List;
18902 Id : Node_Id;
18903 Discr : Node_Id;
18904 Discr_Number : Uint;
18905 Discr_Type : Entity_Id;
18906 Default_Present : Boolean := False;
18907 Default_Not_Present : Boolean := False;
18909 begin
18910 -- A composite type other than an array type can have discriminants.
18911 -- On entry, the current scope is the composite type.
18913 -- The discriminants are initially entered into the scope of the type
18914 -- via Enter_Name with the default Ekind of E_Void to prevent premature
18915 -- use, as explained at the end of this procedure.
18917 Discr := First (Discriminant_Specifications (N));
18918 while Present (Discr) loop
18919 Enter_Name (Defining_Identifier (Discr));
18921 -- For navigation purposes we add a reference to the discriminant
18922 -- in the entity for the type. If the current declaration is a
18923 -- completion, place references on the partial view. Otherwise the
18924 -- type is the current scope.
18926 if Present (Prev) then
18928 -- The references go on the partial view, if present. If the
18929 -- partial view has discriminants, the references have been
18930 -- generated already.
18932 if not Has_Discriminants (Prev) then
18933 Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
18934 end if;
18935 else
18936 Generate_Reference
18937 (Current_Scope, Defining_Identifier (Discr), 'd');
18938 end if;
18940 if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
18941 Discr_Type := Access_Definition (Discr, Discriminant_Type (Discr));
18943 -- Ada 2005 (AI-254)
18945 if Present (Access_To_Subprogram_Definition
18946 (Discriminant_Type (Discr)))
18947 and then Protected_Present (Access_To_Subprogram_Definition
18948 (Discriminant_Type (Discr)))
18949 then
18950 Discr_Type :=
18951 Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
18952 end if;
18954 else
18955 Find_Type (Discriminant_Type (Discr));
18956 Discr_Type := Etype (Discriminant_Type (Discr));
18958 if Error_Posted (Discriminant_Type (Discr)) then
18959 Discr_Type := Any_Type;
18960 end if;
18961 end if;
18963 -- Handling of discriminants that are access types
18965 if Is_Access_Type (Discr_Type) then
18967 -- Ada 2005 (AI-230): Access discriminant allowed in non-
18968 -- limited record types
18970 if Ada_Version < Ada_2005 then
18971 Check_Access_Discriminant_Requires_Limited
18972 (Discr, Discriminant_Type (Discr));
18973 end if;
18975 if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
18976 Error_Msg_N
18977 ("(Ada 83) access discriminant not allowed", Discr);
18978 end if;
18980 -- If not access type, must be a discrete type
18982 elsif not Is_Discrete_Type (Discr_Type) then
18983 Error_Msg_N
18984 ("discriminants must have a discrete or access type",
18985 Discriminant_Type (Discr));
18986 end if;
18988 Set_Etype (Defining_Identifier (Discr), Discr_Type);
18990 -- If a discriminant specification includes the assignment compound
18991 -- delimiter followed by an expression, the expression is the default
18992 -- expression of the discriminant; the default expression must be of
18993 -- the type of the discriminant. (RM 3.7.1) Since this expression is
18994 -- a default expression, we do the special preanalysis, since this
18995 -- expression does not freeze (see section "Handling of Default and
18996 -- Per-Object Expressions" in spec of package Sem).
18998 if Present (Expression (Discr)) then
18999 Preanalyze_Spec_Expression (Expression (Discr), Discr_Type);
19001 -- Legaity checks
19003 if Nkind (N) = N_Formal_Type_Declaration then
19004 Error_Msg_N
19005 ("discriminant defaults not allowed for formal type",
19006 Expression (Discr));
19008 -- Flag an error for a tagged type with defaulted discriminants,
19009 -- excluding limited tagged types when compiling for Ada 2012
19010 -- (see AI05-0214).
19012 elsif Is_Tagged_Type (Current_Scope)
19013 and then (not Is_Limited_Type (Current_Scope)
19014 or else Ada_Version < Ada_2012)
19015 and then Comes_From_Source (N)
19016 then
19017 -- Note: see similar test in Check_Or_Process_Discriminants, to
19018 -- handle the (illegal) case of the completion of an untagged
19019 -- view with discriminants with defaults by a tagged full view.
19020 -- We skip the check if Discr does not come from source, to
19021 -- account for the case of an untagged derived type providing
19022 -- defaults for a renamed discriminant from a private untagged
19023 -- ancestor with a tagged full view (ACATS B460006).
19025 if Ada_Version >= Ada_2012 then
19026 Error_Msg_N
19027 ("discriminants of nonlimited tagged type cannot have"
19028 & " defaults",
19029 Expression (Discr));
19030 else
19031 Error_Msg_N
19032 ("discriminants of tagged type cannot have defaults",
19033 Expression (Discr));
19034 end if;
19036 else
19037 Default_Present := True;
19038 Append_Elmt (Expression (Discr), Elist);
19040 -- Tag the defining identifiers for the discriminants with
19041 -- their corresponding default expressions from the tree.
19043 Set_Discriminant_Default_Value
19044 (Defining_Identifier (Discr), Expression (Discr));
19045 end if;
19047 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag
19048 -- gets set unless we can be sure that no range check is required.
19050 if (GNATprove_Mode or not Expander_Active)
19051 and then not
19052 Is_In_Range
19053 (Expression (Discr), Discr_Type, Assume_Valid => True)
19054 then
19055 Set_Do_Range_Check (Expression (Discr));
19056 end if;
19058 -- No default discriminant value given
19060 else
19061 Default_Not_Present := True;
19062 end if;
19064 -- Ada 2005 (AI-231): Create an Itype that is a duplicate of
19065 -- Discr_Type but with the null-exclusion attribute
19067 if Ada_Version >= Ada_2005 then
19069 -- Ada 2005 (AI-231): Static checks
19071 if Can_Never_Be_Null (Discr_Type) then
19072 Null_Exclusion_Static_Checks (Discr);
19074 elsif Is_Access_Type (Discr_Type)
19075 and then Null_Exclusion_Present (Discr)
19077 -- No need to check itypes because in their case this check
19078 -- was done at their point of creation
19080 and then not Is_Itype (Discr_Type)
19081 then
19082 if Can_Never_Be_Null (Discr_Type) then
19083 Error_Msg_NE
19084 ("`NOT NULL` not allowed (& already excludes null)",
19085 Discr,
19086 Discr_Type);
19087 end if;
19089 Set_Etype (Defining_Identifier (Discr),
19090 Create_Null_Excluding_Itype
19091 (T => Discr_Type,
19092 Related_Nod => Discr));
19094 -- Check for improper null exclusion if the type is otherwise
19095 -- legal for a discriminant.
19097 elsif Null_Exclusion_Present (Discr)
19098 and then Is_Discrete_Type (Discr_Type)
19099 then
19100 Error_Msg_N
19101 ("null exclusion can only apply to an access type", Discr);
19102 end if;
19104 -- Ada 2005 (AI-402): access discriminants of nonlimited types
19105 -- can't have defaults. Synchronized types, or types that are
19106 -- explicitly limited are fine, but special tests apply to derived
19107 -- types in generics: in a generic body we have to assume the
19108 -- worst, and therefore defaults are not allowed if the parent is
19109 -- a generic formal private type (see ACATS B370001).
19111 if Is_Access_Type (Discr_Type) and then Default_Present then
19112 if Ekind (Discr_Type) /= E_Anonymous_Access_Type
19113 or else Is_Limited_Record (Current_Scope)
19114 or else Is_Concurrent_Type (Current_Scope)
19115 or else Is_Concurrent_Record_Type (Current_Scope)
19116 or else Ekind (Current_Scope) = E_Limited_Private_Type
19117 then
19118 if not Is_Derived_Type (Current_Scope)
19119 or else not Is_Generic_Type (Etype (Current_Scope))
19120 or else not In_Package_Body (Scope (Etype (Current_Scope)))
19121 or else Limited_Present
19122 (Type_Definition (Parent (Current_Scope)))
19123 then
19124 null;
19126 else
19127 Error_Msg_N
19128 ("access discriminants of nonlimited types cannot "
19129 & "have defaults", Expression (Discr));
19130 end if;
19132 elsif Present (Expression (Discr)) then
19133 Error_Msg_N
19134 ("(Ada 2005) access discriminants of nonlimited types "
19135 & "cannot have defaults", Expression (Discr));
19136 end if;
19137 end if;
19138 end if;
19140 -- A discriminant cannot be effectively volatile. This check is only
19141 -- relevant when SPARK_Mode is on as it is not standard Ada legality
19142 -- rule (SPARK RM 7.1.3(6)).
19144 if SPARK_Mode = On
19145 and then Is_Effectively_Volatile (Defining_Identifier (Discr))
19146 then
19147 Error_Msg_N ("discriminant cannot be volatile", Discr);
19148 end if;
19150 Next (Discr);
19151 end loop;
19153 -- An element list consisting of the default expressions of the
19154 -- discriminants is constructed in the above loop and used to set
19155 -- the Discriminant_Constraint attribute for the type. If an object
19156 -- is declared of this (record or task) type without any explicit
19157 -- discriminant constraint given, this element list will form the
19158 -- actual parameters for the corresponding initialization procedure
19159 -- for the type.
19161 Set_Discriminant_Constraint (Current_Scope, Elist);
19162 Set_Stored_Constraint (Current_Scope, No_Elist);
19164 -- Default expressions must be provided either for all or for none
19165 -- of the discriminants of a discriminant part. (RM 3.7.1)
19167 if Default_Present and then Default_Not_Present then
19168 Error_Msg_N
19169 ("incomplete specification of defaults for discriminants", N);
19170 end if;
19172 -- The use of the name of a discriminant is not allowed in default
19173 -- expressions of a discriminant part if the specification of the
19174 -- discriminant is itself given in the discriminant part. (RM 3.7.1)
19176 -- To detect this, the discriminant names are entered initially with an
19177 -- Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
19178 -- attempt to use a void entity (for example in an expression that is
19179 -- type-checked) produces the error message: premature usage. Now after
19180 -- completing the semantic analysis of the discriminant part, we can set
19181 -- the Ekind of all the discriminants appropriately.
19183 Discr := First (Discriminant_Specifications (N));
19184 Discr_Number := Uint_1;
19185 while Present (Discr) loop
19186 Id := Defining_Identifier (Discr);
19187 Set_Ekind (Id, E_Discriminant);
19188 Init_Component_Location (Id);
19189 Init_Esize (Id);
19190 Set_Discriminant_Number (Id, Discr_Number);
19192 -- Make sure this is always set, even in illegal programs
19194 Set_Corresponding_Discriminant (Id, Empty);
19196 -- Initialize the Original_Record_Component to the entity itself.
19197 -- Inherit_Components will propagate the right value to
19198 -- discriminants in derived record types.
19200 Set_Original_Record_Component (Id, Id);
19202 -- Create the discriminal for the discriminant
19204 Build_Discriminal (Id);
19206 Next (Discr);
19207 Discr_Number := Discr_Number + 1;
19208 end loop;
19210 Set_Has_Discriminants (Current_Scope);
19211 end Process_Discriminants;
19213 -----------------------
19214 -- Process_Full_View --
19215 -----------------------
19217 procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
19218 procedure Collect_Implemented_Interfaces
19219 (Typ : Entity_Id;
19220 Ifaces : Elist_Id);
19221 -- Ada 2005: Gather all the interfaces that Typ directly or
19222 -- inherently implements. Duplicate entries are not added to
19223 -- the list Ifaces.
19225 ------------------------------------
19226 -- Collect_Implemented_Interfaces --
19227 ------------------------------------
19229 procedure Collect_Implemented_Interfaces
19230 (Typ : Entity_Id;
19231 Ifaces : Elist_Id)
19233 Iface : Entity_Id;
19234 Iface_Elmt : Elmt_Id;
19236 begin
19237 -- Abstract interfaces are only associated with tagged record types
19239 if not Is_Tagged_Type (Typ) or else not Is_Record_Type (Typ) then
19240 return;
19241 end if;
19243 -- Recursively climb to the ancestors
19245 if Etype (Typ) /= Typ
19247 -- Protect the frontend against wrong cyclic declarations like:
19249 -- type B is new A with private;
19250 -- type C is new A with private;
19251 -- private
19252 -- type B is new C with null record;
19253 -- type C is new B with null record;
19255 and then Etype (Typ) /= Priv_T
19256 and then Etype (Typ) /= Full_T
19257 then
19258 -- Keep separate the management of private type declarations
19260 if Ekind (Typ) = E_Record_Type_With_Private then
19262 -- Handle the following illegal usage:
19263 -- type Private_Type is tagged private;
19264 -- private
19265 -- type Private_Type is new Type_Implementing_Iface;
19267 if Present (Full_View (Typ))
19268 and then Etype (Typ) /= Full_View (Typ)
19269 then
19270 if Is_Interface (Etype (Typ)) then
19271 Append_Unique_Elmt (Etype (Typ), Ifaces);
19272 end if;
19274 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
19275 end if;
19277 -- Non-private types
19279 else
19280 if Is_Interface (Etype (Typ)) then
19281 Append_Unique_Elmt (Etype (Typ), Ifaces);
19282 end if;
19284 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
19285 end if;
19286 end if;
19288 -- Handle entities in the list of abstract interfaces
19290 if Present (Interfaces (Typ)) then
19291 Iface_Elmt := First_Elmt (Interfaces (Typ));
19292 while Present (Iface_Elmt) loop
19293 Iface := Node (Iface_Elmt);
19295 pragma Assert (Is_Interface (Iface));
19297 if not Contain_Interface (Iface, Ifaces) then
19298 Append_Elmt (Iface, Ifaces);
19299 Collect_Implemented_Interfaces (Iface, Ifaces);
19300 end if;
19302 Next_Elmt (Iface_Elmt);
19303 end loop;
19304 end if;
19305 end Collect_Implemented_Interfaces;
19307 -- Local variables
19309 Full_Indic : Node_Id;
19310 Full_Parent : Entity_Id;
19311 Priv_Parent : Entity_Id;
19313 -- Start of processing for Process_Full_View
19315 begin
19316 -- First some sanity checks that must be done after semantic
19317 -- decoration of the full view and thus cannot be placed with other
19318 -- similar checks in Find_Type_Name
19320 if not Is_Limited_Type (Priv_T)
19321 and then (Is_Limited_Type (Full_T)
19322 or else Is_Limited_Composite (Full_T))
19323 then
19324 if In_Instance then
19325 null;
19326 else
19327 Error_Msg_N
19328 ("completion of nonlimited type cannot be limited", Full_T);
19329 Explain_Limited_Type (Full_T, Full_T);
19330 end if;
19332 elsif Is_Abstract_Type (Full_T)
19333 and then not Is_Abstract_Type (Priv_T)
19334 then
19335 Error_Msg_N
19336 ("completion of nonabstract type cannot be abstract", Full_T);
19338 elsif Is_Tagged_Type (Priv_T)
19339 and then Is_Limited_Type (Priv_T)
19340 and then not Is_Limited_Type (Full_T)
19341 then
19342 -- If pragma CPP_Class was applied to the private declaration
19343 -- propagate the limitedness to the full-view
19345 if Is_CPP_Class (Priv_T) then
19346 Set_Is_Limited_Record (Full_T);
19348 -- GNAT allow its own definition of Limited_Controlled to disobey
19349 -- this rule in order in ease the implementation. This test is safe
19350 -- because Root_Controlled is defined in a child of System that
19351 -- normal programs are not supposed to use.
19353 elsif Is_RTE (Etype (Full_T), RE_Root_Controlled) then
19354 Set_Is_Limited_Composite (Full_T);
19355 else
19356 Error_Msg_N
19357 ("completion of limited tagged type must be limited", Full_T);
19358 end if;
19360 elsif Is_Generic_Type (Priv_T) then
19361 Error_Msg_N ("generic type cannot have a completion", Full_T);
19362 end if;
19364 -- Check that ancestor interfaces of private and full views are
19365 -- consistent. We omit this check for synchronized types because
19366 -- they are performed on the corresponding record type when frozen.
19368 if Ada_Version >= Ada_2005
19369 and then Is_Tagged_Type (Priv_T)
19370 and then Is_Tagged_Type (Full_T)
19371 and then not Is_Concurrent_Type (Full_T)
19372 then
19373 declare
19374 Iface : Entity_Id;
19375 Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
19376 Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
19378 begin
19379 Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
19380 Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
19382 -- Ada 2005 (AI-251): The partial view shall be a descendant of
19383 -- an interface type if and only if the full type is descendant
19384 -- of the interface type (AARM 7.3 (7.3/2)).
19386 Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
19388 if Present (Iface) then
19389 Error_Msg_NE
19390 ("interface in partial view& not implemented by full type "
19391 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
19392 end if;
19394 Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
19396 if Present (Iface) then
19397 Error_Msg_NE
19398 ("interface & not implemented by partial view "
19399 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
19400 end if;
19401 end;
19402 end if;
19404 if Is_Tagged_Type (Priv_T)
19405 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
19406 and then Is_Derived_Type (Full_T)
19407 then
19408 Priv_Parent := Etype (Priv_T);
19410 -- The full view of a private extension may have been transformed
19411 -- into an unconstrained derived type declaration and a subtype
19412 -- declaration (see build_derived_record_type for details).
19414 if Nkind (N) = N_Subtype_Declaration then
19415 Full_Indic := Subtype_Indication (N);
19416 Full_Parent := Etype (Base_Type (Full_T));
19417 else
19418 Full_Indic := Subtype_Indication (Type_Definition (N));
19419 Full_Parent := Etype (Full_T);
19420 end if;
19422 -- Check that the parent type of the full type is a descendant of
19423 -- the ancestor subtype given in the private extension. If either
19424 -- entity has an Etype equal to Any_Type then we had some previous
19425 -- error situation [7.3(8)].
19427 if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
19428 return;
19430 -- Ada 2005 (AI-251): Interfaces in the full type can be given in
19431 -- any order. Therefore we don't have to check that its parent must
19432 -- be a descendant of the parent of the private type declaration.
19434 elsif Is_Interface (Priv_Parent)
19435 and then Is_Interface (Full_Parent)
19436 then
19437 null;
19439 -- Ada 2005 (AI-251): If the parent of the private type declaration
19440 -- is an interface there is no need to check that it is an ancestor
19441 -- of the associated full type declaration. The required tests for
19442 -- this case are performed by Build_Derived_Record_Type.
19444 elsif not Is_Interface (Base_Type (Priv_Parent))
19445 and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
19446 then
19447 Error_Msg_N
19448 ("parent of full type must descend from parent"
19449 & " of private extension", Full_Indic);
19451 -- First check a formal restriction, and then proceed with checking
19452 -- Ada rules. Since the formal restriction is not a serious error, we
19453 -- don't prevent further error detection for this check, hence the
19454 -- ELSE.
19456 else
19457 -- In formal mode, when completing a private extension the type
19458 -- named in the private part must be exactly the same as that
19459 -- named in the visible part.
19461 if Priv_Parent /= Full_Parent then
19462 Error_Msg_Name_1 := Chars (Priv_Parent);
19463 Check_SPARK_05_Restriction ("% expected", Full_Indic);
19464 end if;
19466 -- Check the rules of 7.3(10): if the private extension inherits
19467 -- known discriminants, then the full type must also inherit those
19468 -- discriminants from the same (ancestor) type, and the parent
19469 -- subtype of the full type must be constrained if and only if
19470 -- the ancestor subtype of the private extension is constrained.
19472 if No (Discriminant_Specifications (Parent (Priv_T)))
19473 and then not Has_Unknown_Discriminants (Priv_T)
19474 and then Has_Discriminants (Base_Type (Priv_Parent))
19475 then
19476 declare
19477 Priv_Indic : constant Node_Id :=
19478 Subtype_Indication (Parent (Priv_T));
19480 Priv_Constr : constant Boolean :=
19481 Is_Constrained (Priv_Parent)
19482 or else
19483 Nkind (Priv_Indic) = N_Subtype_Indication
19484 or else
19485 Is_Constrained (Entity (Priv_Indic));
19487 Full_Constr : constant Boolean :=
19488 Is_Constrained (Full_Parent)
19489 or else
19490 Nkind (Full_Indic) = N_Subtype_Indication
19491 or else
19492 Is_Constrained (Entity (Full_Indic));
19494 Priv_Discr : Entity_Id;
19495 Full_Discr : Entity_Id;
19497 begin
19498 Priv_Discr := First_Discriminant (Priv_Parent);
19499 Full_Discr := First_Discriminant (Full_Parent);
19500 while Present (Priv_Discr) and then Present (Full_Discr) loop
19501 if Original_Record_Component (Priv_Discr) =
19502 Original_Record_Component (Full_Discr)
19503 or else
19504 Corresponding_Discriminant (Priv_Discr) =
19505 Corresponding_Discriminant (Full_Discr)
19506 then
19507 null;
19508 else
19509 exit;
19510 end if;
19512 Next_Discriminant (Priv_Discr);
19513 Next_Discriminant (Full_Discr);
19514 end loop;
19516 if Present (Priv_Discr) or else Present (Full_Discr) then
19517 Error_Msg_N
19518 ("full view must inherit discriminants of the parent"
19519 & " type used in the private extension", Full_Indic);
19521 elsif Priv_Constr and then not Full_Constr then
19522 Error_Msg_N
19523 ("parent subtype of full type must be constrained",
19524 Full_Indic);
19526 elsif Full_Constr and then not Priv_Constr then
19527 Error_Msg_N
19528 ("parent subtype of full type must be unconstrained",
19529 Full_Indic);
19530 end if;
19531 end;
19533 -- Check the rules of 7.3(12): if a partial view has neither
19534 -- known or unknown discriminants, then the full type
19535 -- declaration shall define a definite subtype.
19537 elsif not Has_Unknown_Discriminants (Priv_T)
19538 and then not Has_Discriminants (Priv_T)
19539 and then not Is_Constrained (Full_T)
19540 then
19541 Error_Msg_N
19542 ("full view must define a constrained type if partial view"
19543 & " has no discriminants", Full_T);
19544 end if;
19546 -- ??????? Do we implement the following properly ?????
19547 -- If the ancestor subtype of a private extension has constrained
19548 -- discriminants, then the parent subtype of the full view shall
19549 -- impose a statically matching constraint on those discriminants
19550 -- [7.3(13)].
19551 end if;
19553 else
19554 -- For untagged types, verify that a type without discriminants is
19555 -- not completed with an unconstrained type. A separate error message
19556 -- is produced if the full type has defaulted discriminants.
19558 if not Is_Indefinite_Subtype (Priv_T)
19559 and then Is_Indefinite_Subtype (Full_T)
19560 then
19561 Error_Msg_Sloc := Sloc (Parent (Priv_T));
19562 Error_Msg_NE
19563 ("full view of& not compatible with declaration#",
19564 Full_T, Priv_T);
19566 if not Is_Tagged_Type (Full_T) then
19567 Error_Msg_N
19568 ("\one is constrained, the other unconstrained", Full_T);
19569 end if;
19570 end if;
19571 end if;
19573 -- AI-419: verify that the use of "limited" is consistent
19575 declare
19576 Orig_Decl : constant Node_Id := Original_Node (N);
19578 begin
19579 if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
19580 and then Nkind (Orig_Decl) = N_Full_Type_Declaration
19581 and then Nkind
19582 (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
19583 then
19584 if not Limited_Present (Parent (Priv_T))
19585 and then not Synchronized_Present (Parent (Priv_T))
19586 and then Limited_Present (Type_Definition (Orig_Decl))
19587 then
19588 Error_Msg_N
19589 ("full view of non-limited extension cannot be limited", N);
19591 -- Conversely, if the partial view carries the limited keyword,
19592 -- the full view must as well, even if it may be redundant.
19594 elsif Limited_Present (Parent (Priv_T))
19595 and then not Limited_Present (Type_Definition (Orig_Decl))
19596 then
19597 Error_Msg_N
19598 ("full view of limited extension must be explicitly limited",
19600 end if;
19601 end if;
19602 end;
19604 -- Ada 2005 (AI-443): A synchronized private extension must be
19605 -- completed by a task or protected type.
19607 if Ada_Version >= Ada_2005
19608 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
19609 and then Synchronized_Present (Parent (Priv_T))
19610 and then not Is_Concurrent_Type (Full_T)
19611 then
19612 Error_Msg_N ("full view of synchronized extension must " &
19613 "be synchronized type", N);
19614 end if;
19616 -- Ada 2005 AI-363: if the full view has discriminants with
19617 -- defaults, it is illegal to declare constrained access subtypes
19618 -- whose designated type is the current type. This allows objects
19619 -- of the type that are declared in the heap to be unconstrained.
19621 if not Has_Unknown_Discriminants (Priv_T)
19622 and then not Has_Discriminants (Priv_T)
19623 and then Has_Discriminants (Full_T)
19624 and then
19625 Present (Discriminant_Default_Value (First_Discriminant (Full_T)))
19626 then
19627 Set_Has_Constrained_Partial_View (Full_T);
19628 Set_Has_Constrained_Partial_View (Priv_T);
19629 end if;
19631 -- Create a full declaration for all its subtypes recorded in
19632 -- Private_Dependents and swap them similarly to the base type. These
19633 -- are subtypes that have been define before the full declaration of
19634 -- the private type. We also swap the entry in Private_Dependents list
19635 -- so we can properly restore the private view on exit from the scope.
19637 declare
19638 Priv_Elmt : Elmt_Id;
19639 Priv_Scop : Entity_Id;
19640 Priv : Entity_Id;
19641 Full : Entity_Id;
19643 begin
19644 Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
19645 while Present (Priv_Elmt) loop
19646 Priv := Node (Priv_Elmt);
19647 Priv_Scop := Scope (Priv);
19649 if Ekind_In (Priv, E_Private_Subtype,
19650 E_Limited_Private_Subtype,
19651 E_Record_Subtype_With_Private)
19652 then
19653 Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
19654 Set_Is_Itype (Full);
19655 Set_Parent (Full, Parent (Priv));
19656 Set_Associated_Node_For_Itype (Full, N);
19658 -- Now we need to complete the private subtype, but since the
19659 -- base type has already been swapped, we must also swap the
19660 -- subtypes (and thus, reverse the arguments in the call to
19661 -- Complete_Private_Subtype). Also note that we may need to
19662 -- re-establish the scope of the private subtype.
19664 Copy_And_Swap (Priv, Full);
19666 if not In_Open_Scopes (Priv_Scop) then
19667 Push_Scope (Priv_Scop);
19669 else
19670 -- Reset Priv_Scop to Empty to indicate no scope was pushed
19672 Priv_Scop := Empty;
19673 end if;
19675 Complete_Private_Subtype (Full, Priv, Full_T, N);
19677 if Present (Priv_Scop) then
19678 Pop_Scope;
19679 end if;
19681 Replace_Elmt (Priv_Elmt, Full);
19682 end if;
19684 Next_Elmt (Priv_Elmt);
19685 end loop;
19686 end;
19688 -- If the private view was tagged, copy the new primitive operations
19689 -- from the private view to the full view.
19691 if Is_Tagged_Type (Full_T) then
19692 declare
19693 Disp_Typ : Entity_Id;
19694 Full_List : Elist_Id;
19695 Prim : Entity_Id;
19696 Prim_Elmt : Elmt_Id;
19697 Priv_List : Elist_Id;
19699 function Contains
19700 (E : Entity_Id;
19701 L : Elist_Id) return Boolean;
19702 -- Determine whether list L contains element E
19704 --------------
19705 -- Contains --
19706 --------------
19708 function Contains
19709 (E : Entity_Id;
19710 L : Elist_Id) return Boolean
19712 List_Elmt : Elmt_Id;
19714 begin
19715 List_Elmt := First_Elmt (L);
19716 while Present (List_Elmt) loop
19717 if Node (List_Elmt) = E then
19718 return True;
19719 end if;
19721 Next_Elmt (List_Elmt);
19722 end loop;
19724 return False;
19725 end Contains;
19727 -- Start of processing
19729 begin
19730 if Is_Tagged_Type (Priv_T) then
19731 Priv_List := Primitive_Operations (Priv_T);
19732 Prim_Elmt := First_Elmt (Priv_List);
19734 -- In the case of a concurrent type completing a private tagged
19735 -- type, primitives may have been declared in between the two
19736 -- views. These subprograms need to be wrapped the same way
19737 -- entries and protected procedures are handled because they
19738 -- cannot be directly shared by the two views.
19740 if Is_Concurrent_Type (Full_T) then
19741 declare
19742 Conc_Typ : constant Entity_Id :=
19743 Corresponding_Record_Type (Full_T);
19744 Curr_Nod : Node_Id := Parent (Conc_Typ);
19745 Wrap_Spec : Node_Id;
19747 begin
19748 while Present (Prim_Elmt) loop
19749 Prim := Node (Prim_Elmt);
19751 if Comes_From_Source (Prim)
19752 and then not Is_Abstract_Subprogram (Prim)
19753 then
19754 Wrap_Spec :=
19755 Make_Subprogram_Declaration (Sloc (Prim),
19756 Specification =>
19757 Build_Wrapper_Spec
19758 (Subp_Id => Prim,
19759 Obj_Typ => Conc_Typ,
19760 Formals =>
19761 Parameter_Specifications (
19762 Parent (Prim))));
19764 Insert_After (Curr_Nod, Wrap_Spec);
19765 Curr_Nod := Wrap_Spec;
19767 Analyze (Wrap_Spec);
19768 end if;
19770 Next_Elmt (Prim_Elmt);
19771 end loop;
19773 return;
19774 end;
19776 -- For non-concurrent types, transfer explicit primitives, but
19777 -- omit those inherited from the parent of the private view
19778 -- since they will be re-inherited later on.
19780 else
19781 Full_List := Primitive_Operations (Full_T);
19783 while Present (Prim_Elmt) loop
19784 Prim := Node (Prim_Elmt);
19786 if Comes_From_Source (Prim)
19787 and then not Contains (Prim, Full_List)
19788 then
19789 Append_Elmt (Prim, Full_List);
19790 end if;
19792 Next_Elmt (Prim_Elmt);
19793 end loop;
19794 end if;
19796 -- Untagged private view
19798 else
19799 Full_List := Primitive_Operations (Full_T);
19801 -- In this case the partial view is untagged, so here we locate
19802 -- all of the earlier primitives that need to be treated as
19803 -- dispatching (those that appear between the two views). Note
19804 -- that these additional operations must all be new operations
19805 -- (any earlier operations that override inherited operations
19806 -- of the full view will already have been inserted in the
19807 -- primitives list, marked by Check_Operation_From_Private_View
19808 -- as dispatching. Note that implicit "/=" operators are
19809 -- excluded from being added to the primitives list since they
19810 -- shouldn't be treated as dispatching (tagged "/=" is handled
19811 -- specially).
19813 Prim := Next_Entity (Full_T);
19814 while Present (Prim) and then Prim /= Priv_T loop
19815 if Ekind_In (Prim, E_Procedure, E_Function) then
19816 Disp_Typ := Find_Dispatching_Type (Prim);
19818 if Disp_Typ = Full_T
19819 and then (Chars (Prim) /= Name_Op_Ne
19820 or else Comes_From_Source (Prim))
19821 then
19822 Check_Controlling_Formals (Full_T, Prim);
19824 if not Is_Dispatching_Operation (Prim) then
19825 Append_Elmt (Prim, Full_List);
19826 Set_Is_Dispatching_Operation (Prim, True);
19827 Set_DT_Position_Value (Prim, No_Uint);
19828 end if;
19830 elsif Is_Dispatching_Operation (Prim)
19831 and then Disp_Typ /= Full_T
19832 then
19834 -- Verify that it is not otherwise controlled by a
19835 -- formal or a return value of type T.
19837 Check_Controlling_Formals (Disp_Typ, Prim);
19838 end if;
19839 end if;
19841 Next_Entity (Prim);
19842 end loop;
19843 end if;
19845 -- For the tagged case, the two views can share the same primitive
19846 -- operations list and the same class-wide type. Update attributes
19847 -- of the class-wide type which depend on the full declaration.
19849 if Is_Tagged_Type (Priv_T) then
19850 Set_Direct_Primitive_Operations (Priv_T, Full_List);
19851 Set_Class_Wide_Type
19852 (Base_Type (Full_T), Class_Wide_Type (Priv_T));
19854 Set_Has_Task (Class_Wide_Type (Priv_T), Has_Task (Full_T));
19855 Set_Has_Protected
19856 (Class_Wide_Type (Priv_T), Has_Protected (Full_T));
19857 end if;
19858 end;
19859 end if;
19861 -- Ada 2005 AI 161: Check preelaborable initialization consistency
19863 if Known_To_Have_Preelab_Init (Priv_T) then
19865 -- Case where there is a pragma Preelaborable_Initialization. We
19866 -- always allow this in predefined units, which is cheating a bit,
19867 -- but it means we don't have to struggle to meet the requirements in
19868 -- the RM for having Preelaborable Initialization. Otherwise we
19869 -- require that the type meets the RM rules. But we can't check that
19870 -- yet, because of the rule about overriding Initialize, so we simply
19871 -- set a flag that will be checked at freeze time.
19873 if not In_Predefined_Unit (Full_T) then
19874 Set_Must_Have_Preelab_Init (Full_T);
19875 end if;
19876 end if;
19878 -- If pragma CPP_Class was applied to the private type declaration,
19879 -- propagate it now to the full type declaration.
19881 if Is_CPP_Class (Priv_T) then
19882 Set_Is_CPP_Class (Full_T);
19883 Set_Convention (Full_T, Convention_CPP);
19885 -- Check that components of imported CPP types do not have default
19886 -- expressions.
19888 Check_CPP_Type_Has_No_Defaults (Full_T);
19889 end if;
19891 -- If the private view has user specified stream attributes, then so has
19892 -- the full view.
19894 -- Why the test, how could these flags be already set in Full_T ???
19896 if Has_Specified_Stream_Read (Priv_T) then
19897 Set_Has_Specified_Stream_Read (Full_T);
19898 end if;
19900 if Has_Specified_Stream_Write (Priv_T) then
19901 Set_Has_Specified_Stream_Write (Full_T);
19902 end if;
19904 if Has_Specified_Stream_Input (Priv_T) then
19905 Set_Has_Specified_Stream_Input (Full_T);
19906 end if;
19908 if Has_Specified_Stream_Output (Priv_T) then
19909 Set_Has_Specified_Stream_Output (Full_T);
19910 end if;
19912 -- Propagate the attributes related to pragma Default_Initial_Condition
19913 -- from the private to the full view. Note that both flags are mutually
19914 -- exclusive.
19916 if Has_Default_Init_Cond (Priv_T)
19917 or else Has_Inherited_Default_Init_Cond (Priv_T)
19918 then
19919 Propagate_Default_Init_Cond_Attributes
19920 (From_Typ => Priv_T,
19921 To_Typ => Full_T,
19922 Private_To_Full_View => True);
19924 -- In the case where the full view is derived from another private type,
19925 -- the attributes related to pragma Default_Initial_Condition must be
19926 -- propagated from the full to the private view to maintain consistency
19927 -- of views.
19929 -- package Pack is
19930 -- type Parent_Typ is private
19931 -- with Default_Initial_Condition ...;
19932 -- private
19933 -- type Parent_Typ is ...;
19934 -- end Pack;
19936 -- with Pack; use Pack;
19937 -- package Pack_2 is
19938 -- type Deriv_Typ is private; -- must inherit
19939 -- private
19940 -- type Deriv_Typ is new Parent_Typ; -- must inherit
19941 -- end Pack_2;
19943 elsif Has_Default_Init_Cond (Full_T)
19944 or else Has_Inherited_Default_Init_Cond (Full_T)
19945 then
19946 Propagate_Default_Init_Cond_Attributes
19947 (From_Typ => Full_T,
19948 To_Typ => Priv_T,
19949 Private_To_Full_View => True);
19950 end if;
19952 -- Propagate the attributes related to pragma Ghost from the private to
19953 -- the full view.
19955 if Is_Ghost_Entity (Priv_T) then
19956 Set_Is_Ghost_Entity (Full_T);
19958 -- The Ghost policy in effect at the point of declaration and at the
19959 -- point of completion must match (SPARK RM 6.9(15)).
19961 Check_Ghost_Completion (Priv_T, Full_T);
19963 -- In the case where the private view of a tagged type lacks a parent
19964 -- type and is subject to pragma Ghost, ensure that the parent type
19965 -- specified by the full view is also Ghost (SPARK RM 6.9(9)).
19967 if Is_Derived_Type (Full_T) then
19968 Check_Ghost_Derivation (Full_T);
19969 end if;
19970 end if;
19972 -- Propagate invariants to full type
19974 if Has_Invariants (Priv_T) then
19975 Set_Has_Invariants (Full_T);
19976 Set_Invariant_Procedure (Full_T, Invariant_Procedure (Priv_T));
19977 end if;
19979 if Has_Inheritable_Invariants (Priv_T) then
19980 Set_Has_Inheritable_Invariants (Full_T);
19981 end if;
19983 -- Check hidden inheritance of class-wide type invariants
19985 if Ada_Version >= Ada_2012
19986 and then not Has_Inheritable_Invariants (Full_T)
19987 and then In_Private_Part (Current_Scope)
19988 and then Has_Interfaces (Full_T)
19989 then
19990 declare
19991 Ifaces : Elist_Id;
19992 AI : Elmt_Id;
19994 begin
19995 Collect_Interfaces (Full_T, Ifaces, Exclude_Parents => True);
19997 AI := First_Elmt (Ifaces);
19998 while Present (AI) loop
19999 if Has_Inheritable_Invariants (Node (AI)) then
20000 Error_Msg_N
20001 ("hidden inheritance of class-wide type invariants " &
20002 "not allowed", N);
20003 exit;
20004 end if;
20006 Next_Elmt (AI);
20007 end loop;
20008 end;
20009 end if;
20011 -- Propagate predicates to full type, and predicate function if already
20012 -- defined. It is not clear that this can actually happen? the partial
20013 -- view cannot be frozen yet, and the predicate function has not been
20014 -- built. Still it is a cheap check and seems safer to make it.
20016 if Has_Predicates (Priv_T) then
20017 if Present (Predicate_Function (Priv_T)) then
20018 Set_Predicate_Function (Full_T, Predicate_Function (Priv_T));
20019 end if;
20021 Set_Has_Predicates (Full_T);
20022 end if;
20023 end Process_Full_View;
20025 -----------------------------------
20026 -- Process_Incomplete_Dependents --
20027 -----------------------------------
20029 procedure Process_Incomplete_Dependents
20030 (N : Node_Id;
20031 Full_T : Entity_Id;
20032 Inc_T : Entity_Id)
20034 Inc_Elmt : Elmt_Id;
20035 Priv_Dep : Entity_Id;
20036 New_Subt : Entity_Id;
20038 Disc_Constraint : Elist_Id;
20040 begin
20041 if No (Private_Dependents (Inc_T)) then
20042 return;
20043 end if;
20045 -- Itypes that may be generated by the completion of an incomplete
20046 -- subtype are not used by the back-end and not attached to the tree.
20047 -- They are created only for constraint-checking purposes.
20049 Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
20050 while Present (Inc_Elmt) loop
20051 Priv_Dep := Node (Inc_Elmt);
20053 if Ekind (Priv_Dep) = E_Subprogram_Type then
20055 -- An Access_To_Subprogram type may have a return type or a
20056 -- parameter type that is incomplete. Replace with the full view.
20058 if Etype (Priv_Dep) = Inc_T then
20059 Set_Etype (Priv_Dep, Full_T);
20060 end if;
20062 declare
20063 Formal : Entity_Id;
20065 begin
20066 Formal := First_Formal (Priv_Dep);
20067 while Present (Formal) loop
20068 if Etype (Formal) = Inc_T then
20069 Set_Etype (Formal, Full_T);
20070 end if;
20072 Next_Formal (Formal);
20073 end loop;
20074 end;
20076 elsif Is_Overloadable (Priv_Dep) then
20078 -- If a subprogram in the incomplete dependents list is primitive
20079 -- for a tagged full type then mark it as a dispatching operation,
20080 -- check whether it overrides an inherited subprogram, and check
20081 -- restrictions on its controlling formals. Note that a protected
20082 -- operation is never dispatching: only its wrapper operation
20083 -- (which has convention Ada) is.
20085 if Is_Tagged_Type (Full_T)
20086 and then Is_Primitive (Priv_Dep)
20087 and then Convention (Priv_Dep) /= Convention_Protected
20088 then
20089 Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
20090 Set_Is_Dispatching_Operation (Priv_Dep);
20091 Check_Controlling_Formals (Full_T, Priv_Dep);
20092 end if;
20094 elsif Ekind (Priv_Dep) = E_Subprogram_Body then
20096 -- Can happen during processing of a body before the completion
20097 -- of a TA type. Ignore, because spec is also on dependent list.
20099 return;
20101 -- Ada 2005 (AI-412): Transform a regular incomplete subtype into a
20102 -- corresponding subtype of the full view.
20104 elsif Ekind (Priv_Dep) = E_Incomplete_Subtype then
20105 Set_Subtype_Indication
20106 (Parent (Priv_Dep), New_Occurrence_Of (Full_T, Sloc (Priv_Dep)));
20107 Set_Etype (Priv_Dep, Full_T);
20108 Set_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
20109 Set_Analyzed (Parent (Priv_Dep), False);
20111 -- Reanalyze the declaration, suppressing the call to
20112 -- Enter_Name to avoid duplicate names.
20114 Analyze_Subtype_Declaration
20115 (N => Parent (Priv_Dep),
20116 Skip => True);
20118 -- Dependent is a subtype
20120 else
20121 -- We build a new subtype indication using the full view of the
20122 -- incomplete parent. The discriminant constraints have been
20123 -- elaborated already at the point of the subtype declaration.
20125 New_Subt := Create_Itype (E_Void, N);
20127 if Has_Discriminants (Full_T) then
20128 Disc_Constraint := Discriminant_Constraint (Priv_Dep);
20129 else
20130 Disc_Constraint := No_Elist;
20131 end if;
20133 Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
20134 Set_Full_View (Priv_Dep, New_Subt);
20135 end if;
20137 Next_Elmt (Inc_Elmt);
20138 end loop;
20139 end Process_Incomplete_Dependents;
20141 --------------------------------
20142 -- Process_Range_Expr_In_Decl --
20143 --------------------------------
20145 procedure Process_Range_Expr_In_Decl
20146 (R : Node_Id;
20147 T : Entity_Id;
20148 Subtyp : Entity_Id := Empty;
20149 Check_List : List_Id := Empty_List;
20150 R_Check_Off : Boolean := False;
20151 In_Iter_Schm : Boolean := False)
20153 Lo, Hi : Node_Id;
20154 R_Checks : Check_Result;
20155 Insert_Node : Node_Id;
20156 Def_Id : Entity_Id;
20158 begin
20159 Analyze_And_Resolve (R, Base_Type (T));
20161 if Nkind (R) = N_Range then
20163 -- In SPARK, all ranges should be static, with the exception of the
20164 -- discrete type definition of a loop parameter specification.
20166 if not In_Iter_Schm
20167 and then not Is_OK_Static_Range (R)
20168 then
20169 Check_SPARK_05_Restriction ("range should be static", R);
20170 end if;
20172 Lo := Low_Bound (R);
20173 Hi := High_Bound (R);
20175 -- Validity checks on the range of a quantified expression are
20176 -- delayed until the construct is transformed into a loop.
20178 if Nkind (Parent (R)) = N_Loop_Parameter_Specification
20179 and then Nkind (Parent (Parent (R))) = N_Quantified_Expression
20180 then
20181 null;
20183 -- We need to ensure validity of the bounds here, because if we
20184 -- go ahead and do the expansion, then the expanded code will get
20185 -- analyzed with range checks suppressed and we miss the check.
20187 -- WARNING: The capture of the range bounds with xxx_FIRST/_LAST and
20188 -- the temporaries generated by routine Remove_Side_Effects by means
20189 -- of validity checks must use the same names. When a range appears
20190 -- in the parent of a generic, the range is processed with checks
20191 -- disabled as part of the generic context and with checks enabled
20192 -- for code generation purposes. This leads to link issues as the
20193 -- generic contains references to xxx_FIRST/_LAST, but the inlined
20194 -- template sees the temporaries generated by Remove_Side_Effects.
20196 else
20197 Validity_Check_Range (R, Subtyp);
20198 end if;
20200 -- If there were errors in the declaration, try and patch up some
20201 -- common mistakes in the bounds. The cases handled are literals
20202 -- which are Integer where the expected type is Real and vice versa.
20203 -- These corrections allow the compilation process to proceed further
20204 -- along since some basic assumptions of the format of the bounds
20205 -- are guaranteed.
20207 if Etype (R) = Any_Type then
20208 if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
20209 Rewrite (Lo,
20210 Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
20212 elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
20213 Rewrite (Hi,
20214 Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
20216 elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
20217 Rewrite (Lo,
20218 Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
20220 elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
20221 Rewrite (Hi,
20222 Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
20223 end if;
20225 Set_Etype (Lo, T);
20226 Set_Etype (Hi, T);
20227 end if;
20229 -- If the bounds of the range have been mistakenly given as string
20230 -- literals (perhaps in place of character literals), then an error
20231 -- has already been reported, but we rewrite the string literal as a
20232 -- bound of the range's type to avoid blowups in later processing
20233 -- that looks at static values.
20235 if Nkind (Lo) = N_String_Literal then
20236 Rewrite (Lo,
20237 Make_Attribute_Reference (Sloc (Lo),
20238 Prefix => New_Occurrence_Of (T, Sloc (Lo)),
20239 Attribute_Name => Name_First));
20240 Analyze_And_Resolve (Lo);
20241 end if;
20243 if Nkind (Hi) = N_String_Literal then
20244 Rewrite (Hi,
20245 Make_Attribute_Reference (Sloc (Hi),
20246 Prefix => New_Occurrence_Of (T, Sloc (Hi)),
20247 Attribute_Name => Name_First));
20248 Analyze_And_Resolve (Hi);
20249 end if;
20251 -- If bounds aren't scalar at this point then exit, avoiding
20252 -- problems with further processing of the range in this procedure.
20254 if not Is_Scalar_Type (Etype (Lo)) then
20255 return;
20256 end if;
20258 -- Resolve (actually Sem_Eval) has checked that the bounds are in
20259 -- then range of the base type. Here we check whether the bounds
20260 -- are in the range of the subtype itself. Note that if the bounds
20261 -- represent the null range the Constraint_Error exception should
20262 -- not be raised.
20264 -- ??? The following code should be cleaned up as follows
20266 -- 1. The Is_Null_Range (Lo, Hi) test should disappear since it
20267 -- is done in the call to Range_Check (R, T); below
20269 -- 2. The use of R_Check_Off should be investigated and possibly
20270 -- removed, this would clean up things a bit.
20272 if Is_Null_Range (Lo, Hi) then
20273 null;
20275 else
20276 -- Capture values of bounds and generate temporaries for them
20277 -- if needed, before applying checks, since checks may cause
20278 -- duplication of the expression without forcing evaluation.
20280 -- The forced evaluation removes side effects from expressions,
20281 -- which should occur also in GNATprove mode. Otherwise, we end up
20282 -- with unexpected insertions of actions at places where this is
20283 -- not supposed to occur, e.g. on default parameters of a call.
20285 if Expander_Active or GNATprove_Mode then
20287 -- Call Force_Evaluation to create declarations as needed to
20288 -- deal with side effects, and also create typ_FIRST/LAST
20289 -- entities for bounds if we have a subtype name.
20291 -- Note: we do this transformation even if expansion is not
20292 -- active if we are in GNATprove_Mode since the transformation
20293 -- is in general required to ensure that the resulting tree has
20294 -- proper Ada semantics.
20296 Force_Evaluation
20297 (Lo, Related_Id => Subtyp, Is_Low_Bound => True);
20298 Force_Evaluation
20299 (Hi, Related_Id => Subtyp, Is_High_Bound => True);
20300 end if;
20302 -- We use a flag here instead of suppressing checks on the type
20303 -- because the type we check against isn't necessarily the place
20304 -- where we put the check.
20306 if not R_Check_Off then
20307 R_Checks := Get_Range_Checks (R, T);
20309 -- Look up tree to find an appropriate insertion point. We
20310 -- can't just use insert_actions because later processing
20311 -- depends on the insertion node. Prior to Ada 2012 the
20312 -- insertion point could only be a declaration or a loop, but
20313 -- quantified expressions can appear within any context in an
20314 -- expression, and the insertion point can be any statement,
20315 -- pragma, or declaration.
20317 Insert_Node := Parent (R);
20318 while Present (Insert_Node) loop
20319 exit when
20320 Nkind (Insert_Node) in N_Declaration
20321 and then
20322 not Nkind_In
20323 (Insert_Node, N_Component_Declaration,
20324 N_Loop_Parameter_Specification,
20325 N_Function_Specification,
20326 N_Procedure_Specification);
20328 exit when Nkind (Insert_Node) in N_Later_Decl_Item
20329 or else Nkind (Insert_Node) in
20330 N_Statement_Other_Than_Procedure_Call
20331 or else Nkind_In (Insert_Node, N_Procedure_Call_Statement,
20332 N_Pragma);
20334 Insert_Node := Parent (Insert_Node);
20335 end loop;
20337 -- Why would Type_Decl not be present??? Without this test,
20338 -- short regression tests fail.
20340 if Present (Insert_Node) then
20342 -- Case of loop statement. Verify that the range is part
20343 -- of the subtype indication of the iteration scheme.
20345 if Nkind (Insert_Node) = N_Loop_Statement then
20346 declare
20347 Indic : Node_Id;
20349 begin
20350 Indic := Parent (R);
20351 while Present (Indic)
20352 and then Nkind (Indic) /= N_Subtype_Indication
20353 loop
20354 Indic := Parent (Indic);
20355 end loop;
20357 if Present (Indic) then
20358 Def_Id := Etype (Subtype_Mark (Indic));
20360 Insert_Range_Checks
20361 (R_Checks,
20362 Insert_Node,
20363 Def_Id,
20364 Sloc (Insert_Node),
20366 Do_Before => True);
20367 end if;
20368 end;
20370 -- Insertion before a declaration. If the declaration
20371 -- includes discriminants, the list of applicable checks
20372 -- is given by the caller.
20374 elsif Nkind (Insert_Node) in N_Declaration then
20375 Def_Id := Defining_Identifier (Insert_Node);
20377 if (Ekind (Def_Id) = E_Record_Type
20378 and then Depends_On_Discriminant (R))
20379 or else
20380 (Ekind (Def_Id) = E_Protected_Type
20381 and then Has_Discriminants (Def_Id))
20382 then
20383 Append_Range_Checks
20384 (R_Checks,
20385 Check_List, Def_Id, Sloc (Insert_Node), R);
20387 else
20388 Insert_Range_Checks
20389 (R_Checks,
20390 Insert_Node, Def_Id, Sloc (Insert_Node), R);
20392 end if;
20394 -- Insertion before a statement. Range appears in the
20395 -- context of a quantified expression. Insertion will
20396 -- take place when expression is expanded.
20398 else
20399 null;
20400 end if;
20401 end if;
20402 end if;
20403 end if;
20405 -- Case of other than an explicit N_Range node
20407 -- The forced evaluation removes side effects from expressions, which
20408 -- should occur also in GNATprove mode. Otherwise, we end up with
20409 -- unexpected insertions of actions at places where this is not
20410 -- supposed to occur, e.g. on default parameters of a call.
20412 elsif Expander_Active or GNATprove_Mode then
20413 Get_Index_Bounds (R, Lo, Hi);
20414 Force_Evaluation (Lo);
20415 Force_Evaluation (Hi);
20416 end if;
20417 end Process_Range_Expr_In_Decl;
20419 --------------------------------------
20420 -- Process_Real_Range_Specification --
20421 --------------------------------------
20423 procedure Process_Real_Range_Specification (Def : Node_Id) is
20424 Spec : constant Node_Id := Real_Range_Specification (Def);
20425 Lo : Node_Id;
20426 Hi : Node_Id;
20427 Err : Boolean := False;
20429 procedure Analyze_Bound (N : Node_Id);
20430 -- Analyze and check one bound
20432 -------------------
20433 -- Analyze_Bound --
20434 -------------------
20436 procedure Analyze_Bound (N : Node_Id) is
20437 begin
20438 Analyze_And_Resolve (N, Any_Real);
20440 if not Is_OK_Static_Expression (N) then
20441 Flag_Non_Static_Expr
20442 ("bound in real type definition is not static!", N);
20443 Err := True;
20444 end if;
20445 end Analyze_Bound;
20447 -- Start of processing for Process_Real_Range_Specification
20449 begin
20450 if Present (Spec) then
20451 Lo := Low_Bound (Spec);
20452 Hi := High_Bound (Spec);
20453 Analyze_Bound (Lo);
20454 Analyze_Bound (Hi);
20456 -- If error, clear away junk range specification
20458 if Err then
20459 Set_Real_Range_Specification (Def, Empty);
20460 end if;
20461 end if;
20462 end Process_Real_Range_Specification;
20464 ---------------------
20465 -- Process_Subtype --
20466 ---------------------
20468 function Process_Subtype
20469 (S : Node_Id;
20470 Related_Nod : Node_Id;
20471 Related_Id : Entity_Id := Empty;
20472 Suffix : Character := ' ') return Entity_Id
20474 P : Node_Id;
20475 Def_Id : Entity_Id;
20476 Error_Node : Node_Id;
20477 Full_View_Id : Entity_Id;
20478 Subtype_Mark_Id : Entity_Id;
20480 May_Have_Null_Exclusion : Boolean;
20482 procedure Check_Incomplete (T : Entity_Id);
20483 -- Called to verify that an incomplete type is not used prematurely
20485 ----------------------
20486 -- Check_Incomplete --
20487 ----------------------
20489 procedure Check_Incomplete (T : Entity_Id) is
20490 begin
20491 -- Ada 2005 (AI-412): Incomplete subtypes are legal
20493 if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
20494 and then
20495 not (Ada_Version >= Ada_2005
20496 and then
20497 (Nkind (Parent (T)) = N_Subtype_Declaration
20498 or else (Nkind (Parent (T)) = N_Subtype_Indication
20499 and then Nkind (Parent (Parent (T))) =
20500 N_Subtype_Declaration)))
20501 then
20502 Error_Msg_N ("invalid use of type before its full declaration", T);
20503 end if;
20504 end Check_Incomplete;
20506 -- Start of processing for Process_Subtype
20508 begin
20509 -- Case of no constraints present
20511 if Nkind (S) /= N_Subtype_Indication then
20512 Find_Type (S);
20513 Check_Incomplete (S);
20514 P := Parent (S);
20516 -- Ada 2005 (AI-231): Static check
20518 if Ada_Version >= Ada_2005
20519 and then Present (P)
20520 and then Null_Exclusion_Present (P)
20521 and then Nkind (P) /= N_Access_To_Object_Definition
20522 and then not Is_Access_Type (Entity (S))
20523 then
20524 Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
20525 end if;
20527 -- The following is ugly, can't we have a range or even a flag???
20529 May_Have_Null_Exclusion :=
20530 Nkind_In (P, N_Access_Definition,
20531 N_Access_Function_Definition,
20532 N_Access_Procedure_Definition,
20533 N_Access_To_Object_Definition,
20534 N_Allocator,
20535 N_Component_Definition)
20536 or else
20537 Nkind_In (P, N_Derived_Type_Definition,
20538 N_Discriminant_Specification,
20539 N_Formal_Object_Declaration,
20540 N_Object_Declaration,
20541 N_Object_Renaming_Declaration,
20542 N_Parameter_Specification,
20543 N_Subtype_Declaration);
20545 -- Create an Itype that is a duplicate of Entity (S) but with the
20546 -- null-exclusion attribute.
20548 if May_Have_Null_Exclusion
20549 and then Is_Access_Type (Entity (S))
20550 and then Null_Exclusion_Present (P)
20552 -- No need to check the case of an access to object definition.
20553 -- It is correct to define double not-null pointers.
20555 -- Example:
20556 -- type Not_Null_Int_Ptr is not null access Integer;
20557 -- type Acc is not null access Not_Null_Int_Ptr;
20559 and then Nkind (P) /= N_Access_To_Object_Definition
20560 then
20561 if Can_Never_Be_Null (Entity (S)) then
20562 case Nkind (Related_Nod) is
20563 when N_Full_Type_Declaration =>
20564 if Nkind (Type_Definition (Related_Nod))
20565 in N_Array_Type_Definition
20566 then
20567 Error_Node :=
20568 Subtype_Indication
20569 (Component_Definition
20570 (Type_Definition (Related_Nod)));
20571 else
20572 Error_Node :=
20573 Subtype_Indication (Type_Definition (Related_Nod));
20574 end if;
20576 when N_Subtype_Declaration =>
20577 Error_Node := Subtype_Indication (Related_Nod);
20579 when N_Object_Declaration =>
20580 Error_Node := Object_Definition (Related_Nod);
20582 when N_Component_Declaration =>
20583 Error_Node :=
20584 Subtype_Indication (Component_Definition (Related_Nod));
20586 when N_Allocator =>
20587 Error_Node := Expression (Related_Nod);
20589 when others =>
20590 pragma Assert (False);
20591 Error_Node := Related_Nod;
20592 end case;
20594 Error_Msg_NE
20595 ("`NOT NULL` not allowed (& already excludes null)",
20596 Error_Node,
20597 Entity (S));
20598 end if;
20600 Set_Etype (S,
20601 Create_Null_Excluding_Itype
20602 (T => Entity (S),
20603 Related_Nod => P));
20604 Set_Entity (S, Etype (S));
20605 end if;
20607 return Entity (S);
20609 -- Case of constraint present, so that we have an N_Subtype_Indication
20610 -- node (this node is created only if constraints are present).
20612 else
20613 Find_Type (Subtype_Mark (S));
20615 if Nkind (Parent (S)) /= N_Access_To_Object_Definition
20616 and then not
20617 (Nkind (Parent (S)) = N_Subtype_Declaration
20618 and then Is_Itype (Defining_Identifier (Parent (S))))
20619 then
20620 Check_Incomplete (Subtype_Mark (S));
20621 end if;
20623 P := Parent (S);
20624 Subtype_Mark_Id := Entity (Subtype_Mark (S));
20626 -- Explicit subtype declaration case
20628 if Nkind (P) = N_Subtype_Declaration then
20629 Def_Id := Defining_Identifier (P);
20631 -- Explicit derived type definition case
20633 elsif Nkind (P) = N_Derived_Type_Definition then
20634 Def_Id := Defining_Identifier (Parent (P));
20636 -- Implicit case, the Def_Id must be created as an implicit type.
20637 -- The one exception arises in the case of concurrent types, array
20638 -- and access types, where other subsidiary implicit types may be
20639 -- created and must appear before the main implicit type. In these
20640 -- cases we leave Def_Id set to Empty as a signal that Create_Itype
20641 -- has not yet been called to create Def_Id.
20643 else
20644 if Is_Array_Type (Subtype_Mark_Id)
20645 or else Is_Concurrent_Type (Subtype_Mark_Id)
20646 or else Is_Access_Type (Subtype_Mark_Id)
20647 then
20648 Def_Id := Empty;
20650 -- For the other cases, we create a new unattached Itype,
20651 -- and set the indication to ensure it gets attached later.
20653 else
20654 Def_Id :=
20655 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
20656 end if;
20657 end if;
20659 -- If the kind of constraint is invalid for this kind of type,
20660 -- then give an error, and then pretend no constraint was given.
20662 if not Is_Valid_Constraint_Kind
20663 (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
20664 then
20665 Error_Msg_N
20666 ("incorrect constraint for this kind of type", Constraint (S));
20668 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
20670 -- Set Ekind of orphan itype, to prevent cascaded errors
20672 if Present (Def_Id) then
20673 Set_Ekind (Def_Id, Ekind (Any_Type));
20674 end if;
20676 -- Make recursive call, having got rid of the bogus constraint
20678 return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
20679 end if;
20681 -- Remaining processing depends on type. Select on Base_Type kind to
20682 -- ensure getting to the concrete type kind in the case of a private
20683 -- subtype (needed when only doing semantic analysis).
20685 case Ekind (Base_Type (Subtype_Mark_Id)) is
20686 when Access_Kind =>
20688 -- If this is a constraint on a class-wide type, discard it.
20689 -- There is currently no way to express a partial discriminant
20690 -- constraint on a type with unknown discriminants. This is
20691 -- a pathology that the ACATS wisely decides not to test.
20693 if Is_Class_Wide_Type (Designated_Type (Subtype_Mark_Id)) then
20694 if Comes_From_Source (S) then
20695 Error_Msg_N
20696 ("constraint on class-wide type ignored??",
20697 Constraint (S));
20698 end if;
20700 if Nkind (P) = N_Subtype_Declaration then
20701 Set_Subtype_Indication (P,
20702 New_Occurrence_Of (Subtype_Mark_Id, Sloc (S)));
20703 end if;
20705 return Subtype_Mark_Id;
20706 end if;
20708 Constrain_Access (Def_Id, S, Related_Nod);
20710 if Expander_Active
20711 and then Is_Itype (Designated_Type (Def_Id))
20712 and then Nkind (Related_Nod) = N_Subtype_Declaration
20713 and then not Is_Incomplete_Type (Designated_Type (Def_Id))
20714 then
20715 Build_Itype_Reference
20716 (Designated_Type (Def_Id), Related_Nod);
20717 end if;
20719 when Array_Kind =>
20720 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
20722 when Decimal_Fixed_Point_Kind =>
20723 Constrain_Decimal (Def_Id, S);
20725 when Enumeration_Kind =>
20726 Constrain_Enumeration (Def_Id, S);
20727 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
20729 when Ordinary_Fixed_Point_Kind =>
20730 Constrain_Ordinary_Fixed (Def_Id, S);
20732 when Float_Kind =>
20733 Constrain_Float (Def_Id, S);
20735 when Integer_Kind =>
20736 Constrain_Integer (Def_Id, S);
20737 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
20739 when E_Record_Type |
20740 E_Record_Subtype |
20741 Class_Wide_Kind |
20742 E_Incomplete_Type =>
20743 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
20745 if Ekind (Def_Id) = E_Incomplete_Type then
20746 Set_Private_Dependents (Def_Id, New_Elmt_List);
20747 end if;
20749 when Private_Kind =>
20750 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
20751 Set_Private_Dependents (Def_Id, New_Elmt_List);
20753 -- In case of an invalid constraint prevent further processing
20754 -- since the type constructed is missing expected fields.
20756 if Etype (Def_Id) = Any_Type then
20757 return Def_Id;
20758 end if;
20760 -- If the full view is that of a task with discriminants,
20761 -- we must constrain both the concurrent type and its
20762 -- corresponding record type. Otherwise we will just propagate
20763 -- the constraint to the full view, if available.
20765 if Present (Full_View (Subtype_Mark_Id))
20766 and then Has_Discriminants (Subtype_Mark_Id)
20767 and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
20768 then
20769 Full_View_Id :=
20770 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
20772 Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
20773 Constrain_Concurrent (Full_View_Id, S,
20774 Related_Nod, Related_Id, Suffix);
20775 Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
20776 Set_Full_View (Def_Id, Full_View_Id);
20778 -- Introduce an explicit reference to the private subtype,
20779 -- to prevent scope anomalies in gigi if first use appears
20780 -- in a nested context, e.g. a later function body.
20781 -- Should this be generated in other contexts than a full
20782 -- type declaration?
20784 if Is_Itype (Def_Id)
20785 and then
20786 Nkind (Parent (P)) = N_Full_Type_Declaration
20787 then
20788 Build_Itype_Reference (Def_Id, Parent (P));
20789 end if;
20791 else
20792 Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
20793 end if;
20795 when Concurrent_Kind =>
20796 Constrain_Concurrent (Def_Id, S,
20797 Related_Nod, Related_Id, Suffix);
20799 when others =>
20800 Error_Msg_N ("invalid subtype mark in subtype indication", S);
20801 end case;
20803 -- Size and Convention are always inherited from the base type
20805 Set_Size_Info (Def_Id, (Subtype_Mark_Id));
20806 Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
20808 return Def_Id;
20809 end if;
20810 end Process_Subtype;
20812 --------------------------------------------
20813 -- Propagate_Default_Init_Cond_Attributes --
20814 --------------------------------------------
20816 procedure Propagate_Default_Init_Cond_Attributes
20817 (From_Typ : Entity_Id;
20818 To_Typ : Entity_Id;
20819 Parent_To_Derivation : Boolean := False;
20820 Private_To_Full_View : Boolean := False)
20822 procedure Remove_Default_Init_Cond_Procedure (Typ : Entity_Id);
20823 -- Remove the default initial procedure (if any) from the rep chain of
20824 -- type Typ.
20826 ----------------------------------------
20827 -- Remove_Default_Init_Cond_Procedure --
20828 ----------------------------------------
20830 procedure Remove_Default_Init_Cond_Procedure (Typ : Entity_Id) is
20831 Found : Boolean := False;
20832 Prev : Entity_Id;
20833 Subp : Entity_Id;
20835 begin
20836 Prev := Typ;
20837 Subp := Subprograms_For_Type (Typ);
20838 while Present (Subp) loop
20839 if Is_Default_Init_Cond_Procedure (Subp) then
20840 Found := True;
20841 exit;
20842 end if;
20844 Prev := Subp;
20845 Subp := Subprograms_For_Type (Subp);
20846 end loop;
20848 if Found then
20849 Set_Subprograms_For_Type (Prev, Subprograms_For_Type (Subp));
20850 Set_Subprograms_For_Type (Subp, Empty);
20851 end if;
20852 end Remove_Default_Init_Cond_Procedure;
20854 -- Local variables
20856 Inherit_Procedure : Boolean := False;
20858 -- Start of processing for Propagate_Default_Init_Cond_Attributes
20860 begin
20861 if Has_Default_Init_Cond (From_Typ) then
20863 -- A derived type inherits the attributes from its parent type
20865 if Parent_To_Derivation then
20866 Set_Has_Inherited_Default_Init_Cond (To_Typ);
20868 -- A full view shares the attributes with its private view
20870 else
20871 Set_Has_Default_Init_Cond (To_Typ);
20872 end if;
20874 Inherit_Procedure := True;
20876 -- Due to the order of expansion, a derived private type is processed
20877 -- by two routines which both attempt to set the attributes related
20878 -- to pragma Default_Initial_Condition - Build_Derived_Type and then
20879 -- Process_Full_View.
20881 -- package Pack is
20882 -- type Parent_Typ is private
20883 -- with Default_Initial_Condition ...;
20884 -- private
20885 -- type Parent_Typ is ...;
20886 -- end Pack;
20888 -- with Pack; use Pack;
20889 -- package Pack_2 is
20890 -- type Deriv_Typ is private
20891 -- with Default_Initial_Condition ...;
20892 -- private
20893 -- type Deriv_Typ is new Parent_Typ;
20894 -- end Pack_2;
20896 -- When Build_Derived_Type operates, it sets the attributes on the
20897 -- full view without taking into account that the private view may
20898 -- define its own default initial condition procedure. This becomes
20899 -- apparent in Process_Full_View which must undo some of the work by
20900 -- Build_Derived_Type and propagate the attributes from the private
20901 -- to the full view.
20903 if Private_To_Full_View then
20904 Set_Has_Inherited_Default_Init_Cond (To_Typ, False);
20905 Remove_Default_Init_Cond_Procedure (To_Typ);
20906 end if;
20908 -- A type must inherit the default initial condition procedure from a
20909 -- parent type when the parent itself is inheriting the procedure or
20910 -- when it is defining one. This circuitry is also used when dealing
20911 -- with the private / full view of a type.
20913 elsif Has_Inherited_Default_Init_Cond (From_Typ)
20914 or (Parent_To_Derivation
20915 and Present (Get_Pragma
20916 (From_Typ, Pragma_Default_Initial_Condition)))
20917 then
20918 Set_Has_Inherited_Default_Init_Cond (To_Typ);
20919 Inherit_Procedure := True;
20920 end if;
20922 if Inherit_Procedure
20923 and then No (Default_Init_Cond_Procedure (To_Typ))
20924 then
20925 Set_Default_Init_Cond_Procedure
20926 (To_Typ, Default_Init_Cond_Procedure (From_Typ));
20927 end if;
20928 end Propagate_Default_Init_Cond_Attributes;
20930 -----------------------------
20931 -- Record_Type_Declaration --
20932 -----------------------------
20934 procedure Record_Type_Declaration
20935 (T : Entity_Id;
20936 N : Node_Id;
20937 Prev : Entity_Id)
20939 Def : constant Node_Id := Type_Definition (N);
20940 Is_Tagged : Boolean;
20941 Tag_Comp : Entity_Id;
20943 begin
20944 -- These flags must be initialized before calling Process_Discriminants
20945 -- because this routine makes use of them.
20947 Set_Ekind (T, E_Record_Type);
20948 Set_Etype (T, T);
20949 Init_Size_Align (T);
20950 Set_Interfaces (T, No_Elist);
20951 Set_Stored_Constraint (T, No_Elist);
20952 Set_Default_SSO (T);
20954 -- Normal case
20956 if Ada_Version < Ada_2005 or else not Interface_Present (Def) then
20957 if Limited_Present (Def) then
20958 Check_SPARK_05_Restriction ("limited is not allowed", N);
20959 end if;
20961 if Abstract_Present (Def) then
20962 Check_SPARK_05_Restriction ("abstract is not allowed", N);
20963 end if;
20965 -- The flag Is_Tagged_Type might have already been set by
20966 -- Find_Type_Name if it detected an error for declaration T. This
20967 -- arises in the case of private tagged types where the full view
20968 -- omits the word tagged.
20970 Is_Tagged :=
20971 Tagged_Present (Def)
20972 or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
20974 Set_Is_Limited_Record (T, Limited_Present (Def));
20976 if Is_Tagged then
20977 Set_Is_Tagged_Type (T, True);
20978 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
20979 end if;
20981 -- Type is abstract if full declaration carries keyword, or if
20982 -- previous partial view did.
20984 Set_Is_Abstract_Type (T, Is_Abstract_Type (T)
20985 or else Abstract_Present (Def));
20987 else
20988 Check_SPARK_05_Restriction ("interface is not allowed", N);
20990 Is_Tagged := True;
20991 Analyze_Interface_Declaration (T, Def);
20993 if Present (Discriminant_Specifications (N)) then
20994 Error_Msg_N
20995 ("interface types cannot have discriminants",
20996 Defining_Identifier
20997 (First (Discriminant_Specifications (N))));
20998 end if;
20999 end if;
21001 -- First pass: if there are self-referential access components,
21002 -- create the required anonymous access type declarations, and if
21003 -- need be an incomplete type declaration for T itself.
21005 Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
21007 if Ada_Version >= Ada_2005
21008 and then Present (Interface_List (Def))
21009 then
21010 Check_Interfaces (N, Def);
21012 declare
21013 Ifaces_List : Elist_Id;
21015 begin
21016 -- Ada 2005 (AI-251): Collect the list of progenitors that are not
21017 -- already in the parents.
21019 Collect_Interfaces
21020 (T => T,
21021 Ifaces_List => Ifaces_List,
21022 Exclude_Parents => True);
21024 Set_Interfaces (T, Ifaces_List);
21025 end;
21026 end if;
21028 -- Records constitute a scope for the component declarations within.
21029 -- The scope is created prior to the processing of these declarations.
21030 -- Discriminants are processed first, so that they are visible when
21031 -- processing the other components. The Ekind of the record type itself
21032 -- is set to E_Record_Type (subtypes appear as E_Record_Subtype).
21034 -- Enter record scope
21036 Push_Scope (T);
21038 -- If an incomplete or private type declaration was already given for
21039 -- the type, then this scope already exists, and the discriminants have
21040 -- been declared within. We must verify that the full declaration
21041 -- matches the incomplete one.
21043 Check_Or_Process_Discriminants (N, T, Prev);
21045 Set_Is_Constrained (T, not Has_Discriminants (T));
21046 Set_Has_Delayed_Freeze (T, True);
21048 -- For tagged types add a manually analyzed component corresponding
21049 -- to the component _tag, the corresponding piece of tree will be
21050 -- expanded as part of the freezing actions if it is not a CPP_Class.
21052 if Is_Tagged then
21054 -- Do not add the tag unless we are in expansion mode
21056 if Expander_Active then
21057 Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
21058 Enter_Name (Tag_Comp);
21060 Set_Ekind (Tag_Comp, E_Component);
21061 Set_Is_Tag (Tag_Comp);
21062 Set_Is_Aliased (Tag_Comp);
21063 Set_Etype (Tag_Comp, RTE (RE_Tag));
21064 Set_DT_Entry_Count (Tag_Comp, No_Uint);
21065 Set_Original_Record_Component (Tag_Comp, Tag_Comp);
21066 Init_Component_Location (Tag_Comp);
21068 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
21069 -- implemented interfaces.
21071 if Has_Interfaces (T) then
21072 Add_Interface_Tag_Components (N, T);
21073 end if;
21074 end if;
21076 Make_Class_Wide_Type (T);
21077 Set_Direct_Primitive_Operations (T, New_Elmt_List);
21078 end if;
21080 -- We must suppress range checks when processing record components in
21081 -- the presence of discriminants, since we don't want spurious checks to
21082 -- be generated during their analysis, but Suppress_Range_Checks flags
21083 -- must be reset the after processing the record definition.
21085 -- Note: this is the only use of Kill_Range_Checks, and is a bit odd,
21086 -- couldn't we just use the normal range check suppression method here.
21087 -- That would seem cleaner ???
21089 if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
21090 Set_Kill_Range_Checks (T, True);
21091 Record_Type_Definition (Def, Prev);
21092 Set_Kill_Range_Checks (T, False);
21093 else
21094 Record_Type_Definition (Def, Prev);
21095 end if;
21097 -- Exit from record scope
21099 End_Scope;
21101 -- Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
21102 -- the implemented interfaces and associate them an aliased entity.
21104 if Is_Tagged
21105 and then not Is_Empty_List (Interface_List (Def))
21106 then
21107 Derive_Progenitor_Subprograms (T, T);
21108 end if;
21110 Check_Function_Writable_Actuals (N);
21111 end Record_Type_Declaration;
21113 ----------------------------
21114 -- Record_Type_Definition --
21115 ----------------------------
21117 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
21118 Component : Entity_Id;
21119 Ctrl_Components : Boolean := False;
21120 Final_Storage_Only : Boolean;
21121 T : Entity_Id;
21123 begin
21124 if Ekind (Prev_T) = E_Incomplete_Type then
21125 T := Full_View (Prev_T);
21126 else
21127 T := Prev_T;
21128 end if;
21130 -- In SPARK, tagged types and type extensions may only be declared in
21131 -- the specification of library unit packages.
21133 if Present (Def) and then Is_Tagged_Type (T) then
21134 declare
21135 Typ : Node_Id;
21136 Ctxt : Node_Id;
21138 begin
21139 if Nkind (Parent (Def)) = N_Full_Type_Declaration then
21140 Typ := Parent (Def);
21141 else
21142 pragma Assert
21143 (Nkind (Parent (Def)) = N_Derived_Type_Definition);
21144 Typ := Parent (Parent (Def));
21145 end if;
21147 Ctxt := Parent (Typ);
21149 if Nkind (Ctxt) = N_Package_Body
21150 and then Nkind (Parent (Ctxt)) = N_Compilation_Unit
21151 then
21152 Check_SPARK_05_Restriction
21153 ("type should be defined in package specification", Typ);
21155 elsif Nkind (Ctxt) /= N_Package_Specification
21156 or else Nkind (Parent (Parent (Ctxt))) /= N_Compilation_Unit
21157 then
21158 Check_SPARK_05_Restriction
21159 ("type should be defined in library unit package", Typ);
21160 end if;
21161 end;
21162 end if;
21164 Final_Storage_Only := not Is_Controlled (T);
21166 -- Ada 2005: Check whether an explicit Limited is present in a derived
21167 -- type declaration.
21169 if Nkind (Parent (Def)) = N_Derived_Type_Definition
21170 and then Limited_Present (Parent (Def))
21171 then
21172 Set_Is_Limited_Record (T);
21173 end if;
21175 -- If the component list of a record type is defined by the reserved
21176 -- word null and there is no discriminant part, then the record type has
21177 -- no components and all records of the type are null records (RM 3.7)
21178 -- This procedure is also called to process the extension part of a
21179 -- record extension, in which case the current scope may have inherited
21180 -- components.
21182 if No (Def)
21183 or else No (Component_List (Def))
21184 or else Null_Present (Component_List (Def))
21185 then
21186 if not Is_Tagged_Type (T) then
21187 Check_SPARK_05_Restriction ("untagged record cannot be null", Def);
21188 end if;
21190 else
21191 Analyze_Declarations (Component_Items (Component_List (Def)));
21193 if Present (Variant_Part (Component_List (Def))) then
21194 Check_SPARK_05_Restriction ("variant part is not allowed", Def);
21195 Analyze (Variant_Part (Component_List (Def)));
21196 end if;
21197 end if;
21199 -- After completing the semantic analysis of the record definition,
21200 -- record components, both new and inherited, are accessible. Set their
21201 -- kind accordingly. Exclude malformed itypes from illegal declarations,
21202 -- whose Ekind may be void.
21204 Component := First_Entity (Current_Scope);
21205 while Present (Component) loop
21206 if Ekind (Component) = E_Void
21207 and then not Is_Itype (Component)
21208 then
21209 Set_Ekind (Component, E_Component);
21210 Init_Component_Location (Component);
21211 end if;
21213 if Has_Task (Etype (Component)) then
21214 Set_Has_Task (T);
21215 end if;
21217 if Has_Protected (Etype (Component)) then
21218 Set_Has_Protected (T);
21219 end if;
21221 if Ekind (Component) /= E_Component then
21222 null;
21224 -- Do not set Has_Controlled_Component on a class-wide equivalent
21225 -- type. See Make_CW_Equivalent_Type.
21227 elsif not Is_Class_Wide_Equivalent_Type (T)
21228 and then (Has_Controlled_Component (Etype (Component))
21229 or else (Chars (Component) /= Name_uParent
21230 and then Is_Controlled (Etype (Component))))
21231 then
21232 Set_Has_Controlled_Component (T, True);
21233 Final_Storage_Only :=
21234 Final_Storage_Only
21235 and then Finalize_Storage_Only (Etype (Component));
21236 Ctrl_Components := True;
21237 end if;
21239 Next_Entity (Component);
21240 end loop;
21242 -- A Type is Finalize_Storage_Only only if all its controlled components
21243 -- are also.
21245 if Ctrl_Components then
21246 Set_Finalize_Storage_Only (T, Final_Storage_Only);
21247 end if;
21249 -- Place reference to end record on the proper entity, which may
21250 -- be a partial view.
21252 if Present (Def) then
21253 Process_End_Label (Def, 'e', Prev_T);
21254 end if;
21255 end Record_Type_Definition;
21257 ------------------------
21258 -- Replace_Components --
21259 ------------------------
21261 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id) is
21262 function Process (N : Node_Id) return Traverse_Result;
21264 -------------
21265 -- Process --
21266 -------------
21268 function Process (N : Node_Id) return Traverse_Result is
21269 Comp : Entity_Id;
21271 begin
21272 if Nkind (N) = N_Discriminant_Specification then
21273 Comp := First_Discriminant (Typ);
21274 while Present (Comp) loop
21275 if Chars (Comp) = Chars (Defining_Identifier (N)) then
21276 Set_Defining_Identifier (N, Comp);
21277 exit;
21278 end if;
21280 Next_Discriminant (Comp);
21281 end loop;
21283 elsif Nkind (N) = N_Component_Declaration then
21284 Comp := First_Component (Typ);
21285 while Present (Comp) loop
21286 if Chars (Comp) = Chars (Defining_Identifier (N)) then
21287 Set_Defining_Identifier (N, Comp);
21288 exit;
21289 end if;
21291 Next_Component (Comp);
21292 end loop;
21293 end if;
21295 return OK;
21296 end Process;
21298 procedure Replace is new Traverse_Proc (Process);
21300 -- Start of processing for Replace_Components
21302 begin
21303 Replace (Decl);
21304 end Replace_Components;
21306 -------------------------------
21307 -- Set_Completion_Referenced --
21308 -------------------------------
21310 procedure Set_Completion_Referenced (E : Entity_Id) is
21311 begin
21312 -- If in main unit, mark entity that is a completion as referenced,
21313 -- warnings go on the partial view when needed.
21315 if In_Extended_Main_Source_Unit (E) then
21316 Set_Referenced (E);
21317 end if;
21318 end Set_Completion_Referenced;
21320 ---------------------
21321 -- Set_Default_SSO --
21322 ---------------------
21324 procedure Set_Default_SSO (T : Entity_Id) is
21325 begin
21326 case Opt.Default_SSO is
21327 when ' ' =>
21328 null;
21329 when 'L' =>
21330 Set_SSO_Set_Low_By_Default (T, True);
21331 when 'H' =>
21332 Set_SSO_Set_High_By_Default (T, True);
21333 when others =>
21334 raise Program_Error;
21335 end case;
21336 end Set_Default_SSO;
21338 ---------------------
21339 -- Set_Fixed_Range --
21340 ---------------------
21342 -- The range for fixed-point types is complicated by the fact that we
21343 -- do not know the exact end points at the time of the declaration. This
21344 -- is true for three reasons:
21346 -- A size clause may affect the fudging of the end-points.
21347 -- A small clause may affect the values of the end-points.
21348 -- We try to include the end-points if it does not affect the size.
21350 -- This means that the actual end-points must be established at the
21351 -- point when the type is frozen. Meanwhile, we first narrow the range
21352 -- as permitted (so that it will fit if necessary in a small specified
21353 -- size), and then build a range subtree with these narrowed bounds.
21354 -- Set_Fixed_Range constructs the range from real literal values, and
21355 -- sets the range as the Scalar_Range of the given fixed-point type entity.
21357 -- The parent of this range is set to point to the entity so that it is
21358 -- properly hooked into the tree (unlike normal Scalar_Range entries for
21359 -- other scalar types, which are just pointers to the range in the
21360 -- original tree, this would otherwise be an orphan).
21362 -- The tree is left unanalyzed. When the type is frozen, the processing
21363 -- in Freeze.Freeze_Fixed_Point_Type notices that the range is not
21364 -- analyzed, and uses this as an indication that it should complete
21365 -- work on the range (it will know the final small and size values).
21367 procedure Set_Fixed_Range
21368 (E : Entity_Id;
21369 Loc : Source_Ptr;
21370 Lo : Ureal;
21371 Hi : Ureal)
21373 S : constant Node_Id :=
21374 Make_Range (Loc,
21375 Low_Bound => Make_Real_Literal (Loc, Lo),
21376 High_Bound => Make_Real_Literal (Loc, Hi));
21377 begin
21378 Set_Scalar_Range (E, S);
21379 Set_Parent (S, E);
21381 -- Before the freeze point, the bounds of a fixed point are universal
21382 -- and carry the corresponding type.
21384 Set_Etype (Low_Bound (S), Universal_Real);
21385 Set_Etype (High_Bound (S), Universal_Real);
21386 end Set_Fixed_Range;
21388 ----------------------------------
21389 -- Set_Scalar_Range_For_Subtype --
21390 ----------------------------------
21392 procedure Set_Scalar_Range_For_Subtype
21393 (Def_Id : Entity_Id;
21394 R : Node_Id;
21395 Subt : Entity_Id)
21397 Kind : constant Entity_Kind := Ekind (Def_Id);
21399 begin
21400 -- Defend against previous error
21402 if Nkind (R) = N_Error then
21403 return;
21404 end if;
21406 Set_Scalar_Range (Def_Id, R);
21408 -- We need to link the range into the tree before resolving it so
21409 -- that types that are referenced, including importantly the subtype
21410 -- itself, are properly frozen (Freeze_Expression requires that the
21411 -- expression be properly linked into the tree). Of course if it is
21412 -- already linked in, then we do not disturb the current link.
21414 if No (Parent (R)) then
21415 Set_Parent (R, Def_Id);
21416 end if;
21418 -- Reset the kind of the subtype during analysis of the range, to
21419 -- catch possible premature use in the bounds themselves.
21421 Set_Ekind (Def_Id, E_Void);
21422 Process_Range_Expr_In_Decl (R, Subt, Subtyp => Def_Id);
21423 Set_Ekind (Def_Id, Kind);
21424 end Set_Scalar_Range_For_Subtype;
21426 --------------------------------------------------------
21427 -- Set_Stored_Constraint_From_Discriminant_Constraint --
21428 --------------------------------------------------------
21430 procedure Set_Stored_Constraint_From_Discriminant_Constraint
21431 (E : Entity_Id)
21433 begin
21434 -- Make sure set if encountered during Expand_To_Stored_Constraint
21436 Set_Stored_Constraint (E, No_Elist);
21438 -- Give it the right value
21440 if Is_Constrained (E) and then Has_Discriminants (E) then
21441 Set_Stored_Constraint (E,
21442 Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
21443 end if;
21444 end Set_Stored_Constraint_From_Discriminant_Constraint;
21446 -------------------------------------
21447 -- Signed_Integer_Type_Declaration --
21448 -------------------------------------
21450 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
21451 Implicit_Base : Entity_Id;
21452 Base_Typ : Entity_Id;
21453 Lo_Val : Uint;
21454 Hi_Val : Uint;
21455 Errs : Boolean := False;
21456 Lo : Node_Id;
21457 Hi : Node_Id;
21459 function Can_Derive_From (E : Entity_Id) return Boolean;
21460 -- Determine whether given bounds allow derivation from specified type
21462 procedure Check_Bound (Expr : Node_Id);
21463 -- Check bound to make sure it is integral and static. If not, post
21464 -- appropriate error message and set Errs flag
21466 ---------------------
21467 -- Can_Derive_From --
21468 ---------------------
21470 -- Note we check both bounds against both end values, to deal with
21471 -- strange types like ones with a range of 0 .. -12341234.
21473 function Can_Derive_From (E : Entity_Id) return Boolean is
21474 Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
21475 Hi : constant Uint := Expr_Value (Type_High_Bound (E));
21476 begin
21477 return Lo <= Lo_Val and then Lo_Val <= Hi
21478 and then
21479 Lo <= Hi_Val and then Hi_Val <= Hi;
21480 end Can_Derive_From;
21482 -----------------
21483 -- Check_Bound --
21484 -----------------
21486 procedure Check_Bound (Expr : Node_Id) is
21487 begin
21488 -- If a range constraint is used as an integer type definition, each
21489 -- bound of the range must be defined by a static expression of some
21490 -- integer type, but the two bounds need not have the same integer
21491 -- type (Negative bounds are allowed.) (RM 3.5.4)
21493 if not Is_Integer_Type (Etype (Expr)) then
21494 Error_Msg_N
21495 ("integer type definition bounds must be of integer type", Expr);
21496 Errs := True;
21498 elsif not Is_OK_Static_Expression (Expr) then
21499 Flag_Non_Static_Expr
21500 ("non-static expression used for integer type bound!", Expr);
21501 Errs := True;
21503 -- The bounds are folded into literals, and we set their type to be
21504 -- universal, to avoid typing difficulties: we cannot set the type
21505 -- of the literal to the new type, because this would be a forward
21506 -- reference for the back end, and if the original type is user-
21507 -- defined this can lead to spurious semantic errors (e.g. 2928-003).
21509 else
21510 if Is_Entity_Name (Expr) then
21511 Fold_Uint (Expr, Expr_Value (Expr), True);
21512 end if;
21514 Set_Etype (Expr, Universal_Integer);
21515 end if;
21516 end Check_Bound;
21518 -- Start of processing for Signed_Integer_Type_Declaration
21520 begin
21521 -- Create an anonymous base type
21523 Implicit_Base :=
21524 Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
21526 -- Analyze and check the bounds, they can be of any integer type
21528 Lo := Low_Bound (Def);
21529 Hi := High_Bound (Def);
21531 -- Arbitrarily use Integer as the type if either bound had an error
21533 if Hi = Error or else Lo = Error then
21534 Base_Typ := Any_Integer;
21535 Set_Error_Posted (T, True);
21537 -- Here both bounds are OK expressions
21539 else
21540 Analyze_And_Resolve (Lo, Any_Integer);
21541 Analyze_And_Resolve (Hi, Any_Integer);
21543 Check_Bound (Lo);
21544 Check_Bound (Hi);
21546 if Errs then
21547 Hi := Type_High_Bound (Standard_Long_Long_Integer);
21548 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
21549 end if;
21551 -- Find type to derive from
21553 Lo_Val := Expr_Value (Lo);
21554 Hi_Val := Expr_Value (Hi);
21556 if Can_Derive_From (Standard_Short_Short_Integer) then
21557 Base_Typ := Base_Type (Standard_Short_Short_Integer);
21559 elsif Can_Derive_From (Standard_Short_Integer) then
21560 Base_Typ := Base_Type (Standard_Short_Integer);
21562 elsif Can_Derive_From (Standard_Integer) then
21563 Base_Typ := Base_Type (Standard_Integer);
21565 elsif Can_Derive_From (Standard_Long_Integer) then
21566 Base_Typ := Base_Type (Standard_Long_Integer);
21568 elsif Can_Derive_From (Standard_Long_Long_Integer) then
21569 Check_Restriction (No_Long_Long_Integers, Def);
21570 Base_Typ := Base_Type (Standard_Long_Long_Integer);
21572 else
21573 Base_Typ := Base_Type (Standard_Long_Long_Integer);
21574 Error_Msg_N ("integer type definition bounds out of range", Def);
21575 Hi := Type_High_Bound (Standard_Long_Long_Integer);
21576 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
21577 end if;
21578 end if;
21580 -- Complete both implicit base and declared first subtype entities. The
21581 -- inheritance of the rep item chain ensures that SPARK-related pragmas
21582 -- are not clobbered when the signed integer type acts as a full view of
21583 -- a private type.
21585 Set_Etype (Implicit_Base, Base_Typ);
21586 Set_Size_Info (Implicit_Base, Base_Typ);
21587 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
21588 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
21589 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
21591 Set_Ekind (T, E_Signed_Integer_Subtype);
21592 Set_Etype (T, Implicit_Base);
21593 Set_Size_Info (T, Implicit_Base);
21594 Inherit_Rep_Item_Chain (T, Implicit_Base);
21595 Set_Scalar_Range (T, Def);
21596 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
21597 Set_Is_Constrained (T);
21598 end Signed_Integer_Type_Declaration;
21600 end Sem_Ch3;