2015-05-22 Eric Botcazou <ebotcazou@adacore.com>
[official-gcc.git] / gcc / ada / sem_ch3.adb
blob65c85762ef7177be74dc5bee2adb6858845e1baf
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 -- Constant related checks
3210 if Ekind (Obj_Id) = E_Constant then
3212 -- A constant cannot be effectively volatile. This check is only
3213 -- relevant with SPARK_Mode on as it is not a standard Ada legality
3214 -- rule. Do not flag internally-generated constants that map generic
3215 -- formals to actuals in instantiations (SPARK RM 7.1.3(6)).
3217 if SPARK_Mode = On
3218 and then Is_Effectively_Volatile (Obj_Id)
3219 and then No (Corresponding_Generic_Association (Parent (Obj_Id)))
3221 -- Don't give this for internally generated entities (such as the
3222 -- FIRST and LAST temporaries generated for bounds).
3224 and then Comes_From_Source (Obj_Id)
3225 then
3226 Error_Msg_N ("constant cannot be volatile", Obj_Id);
3227 end if;
3229 -- Variable related checks
3231 else pragma Assert (Ekind (Obj_Id) = E_Variable);
3233 -- The following checks are only relevant when SPARK_Mode is on as
3234 -- they are not standard Ada legality rules. Internally generated
3235 -- temporaries are ignored.
3237 if SPARK_Mode = On and then Comes_From_Source (Obj_Id) then
3238 if Is_Effectively_Volatile (Obj_Id) then
3240 -- The declaration of an effectively volatile object must
3241 -- appear at the library level (SPARK RM 7.1.3(7), C.6(6)).
3243 if not Is_Library_Level_Entity (Obj_Id) then
3244 Error_Msg_N
3245 ("volatile variable & must be declared at library level",
3246 Obj_Id);
3248 -- An object of a discriminated type cannot be effectively
3249 -- volatile (SPARK RM C.6(4)).
3251 elsif Has_Discriminants (Obj_Typ) then
3252 Error_Msg_N
3253 ("discriminated object & cannot be volatile", Obj_Id);
3255 -- An object of a tagged type cannot be effectively volatile
3256 -- (SPARK RM C.6(5)).
3258 elsif Is_Tagged_Type (Obj_Typ) then
3259 Error_Msg_N ("tagged object & cannot be volatile", Obj_Id);
3260 end if;
3262 -- The object is not effectively volatile
3264 else
3265 -- A non-effectively volatile object cannot have effectively
3266 -- volatile components (SPARK RM 7.1.3(7)).
3268 if not Is_Effectively_Volatile (Obj_Id)
3269 and then Has_Volatile_Component (Obj_Typ)
3270 then
3271 Error_Msg_N
3272 ("non-volatile object & cannot have volatile components",
3273 Obj_Id);
3274 end if;
3275 end if;
3276 end if;
3278 if Is_Ghost_Entity (Obj_Id) then
3280 -- A Ghost object cannot be effectively volatile (SPARK RM 6.9(8))
3282 if Is_Effectively_Volatile (Obj_Id) then
3283 Error_Msg_N ("ghost variable & cannot be volatile", Obj_Id);
3285 -- A Ghost object cannot be imported or exported (SPARK RM 6.9(8))
3287 elsif Is_Imported (Obj_Id) then
3288 Error_Msg_N ("ghost object & cannot be imported", Obj_Id);
3290 elsif Is_Exported (Obj_Id) then
3291 Error_Msg_N ("ghost object & cannot be exported", Obj_Id);
3292 end if;
3293 end if;
3295 -- Analyze all external properties
3297 Prag := Get_Pragma (Obj_Id, Pragma_Async_Readers);
3299 if Present (Prag) then
3300 Analyze_External_Property_In_Decl_Part (Prag, AR_Val);
3301 Seen := True;
3302 end if;
3304 Prag := Get_Pragma (Obj_Id, Pragma_Async_Writers);
3306 if Present (Prag) then
3307 Analyze_External_Property_In_Decl_Part (Prag, AW_Val);
3308 Seen := True;
3309 end if;
3311 Prag := Get_Pragma (Obj_Id, Pragma_Effective_Reads);
3313 if Present (Prag) then
3314 Analyze_External_Property_In_Decl_Part (Prag, ER_Val);
3315 Seen := True;
3316 end if;
3318 Prag := Get_Pragma (Obj_Id, Pragma_Effective_Writes);
3320 if Present (Prag) then
3321 Analyze_External_Property_In_Decl_Part (Prag, EW_Val);
3322 Seen := True;
3323 end if;
3325 -- Verify the mutual interaction of the various external properties
3327 if Seen then
3328 Check_External_Properties (Obj_Id, AR_Val, AW_Val, ER_Val, EW_Val);
3329 end if;
3330 end if;
3332 -- Check whether the lack of indicator Part_Of agrees with the placement
3333 -- of the object with respect to the state space.
3335 Prag := Get_Pragma (Obj_Id, Pragma_Part_Of);
3337 if No (Prag) then
3338 Check_Missing_Part_Of (Obj_Id);
3339 end if;
3341 -- A ghost object cannot be imported or exported (SPARK RM 6.9(8))
3343 if Is_Ghost_Entity (Obj_Id) then
3344 if Is_Exported (Obj_Id) then
3345 Error_Msg_N ("ghost object & cannot be exported", Obj_Id);
3347 elsif Is_Imported (Obj_Id) then
3348 Error_Msg_N ("ghost object & cannot be imported", Obj_Id);
3349 end if;
3350 end if;
3351 end Analyze_Object_Contract;
3353 --------------------------------
3354 -- Analyze_Object_Declaration --
3355 --------------------------------
3357 procedure Analyze_Object_Declaration (N : Node_Id) is
3358 Loc : constant Source_Ptr := Sloc (N);
3359 Id : constant Entity_Id := Defining_Identifier (N);
3360 T : Entity_Id;
3361 Act_T : Entity_Id;
3363 E : Node_Id := Expression (N);
3364 -- E is set to Expression (N) throughout this routine. When
3365 -- Expression (N) is modified, E is changed accordingly.
3367 Prev_Entity : Entity_Id := Empty;
3369 function Count_Tasks (T : Entity_Id) return Uint;
3370 -- This function is called when a non-generic library level object of a
3371 -- task type is declared. Its function is to count the static number of
3372 -- tasks declared within the type (it is only called if Has_Tasks is set
3373 -- for T). As a side effect, if an array of tasks with non-static bounds
3374 -- or a variant record type is encountered, Check_Restrictions is called
3375 -- indicating the count is unknown.
3377 function Delayed_Aspect_Present return Boolean;
3378 -- If the declaration has an expression that is an aggregate, and it
3379 -- has aspects that require delayed analysis, the resolution of the
3380 -- aggregate must be deferred to the freeze point of the objet. This
3381 -- special processing was created for address clauses, but it must
3382 -- also apply to Alignment. This must be done before the aspect
3383 -- specifications are analyzed because we must handle the aggregate
3384 -- before the analysis of the object declaration is complete.
3386 -- Any other relevant delayed aspects on object declarations ???
3388 -----------------
3389 -- Count_Tasks --
3390 -----------------
3392 function Count_Tasks (T : Entity_Id) return Uint is
3393 C : Entity_Id;
3394 X : Node_Id;
3395 V : Uint;
3397 begin
3398 if Is_Task_Type (T) then
3399 return Uint_1;
3401 elsif Is_Record_Type (T) then
3402 if Has_Discriminants (T) then
3403 Check_Restriction (Max_Tasks, N);
3404 return Uint_0;
3406 else
3407 V := Uint_0;
3408 C := First_Component (T);
3409 while Present (C) loop
3410 V := V + Count_Tasks (Etype (C));
3411 Next_Component (C);
3412 end loop;
3414 return V;
3415 end if;
3417 elsif Is_Array_Type (T) then
3418 X := First_Index (T);
3419 V := Count_Tasks (Component_Type (T));
3420 while Present (X) loop
3421 C := Etype (X);
3423 if not Is_OK_Static_Subtype (C) then
3424 Check_Restriction (Max_Tasks, N);
3425 return Uint_0;
3426 else
3427 V := V * (UI_Max (Uint_0,
3428 Expr_Value (Type_High_Bound (C)) -
3429 Expr_Value (Type_Low_Bound (C)) + Uint_1));
3430 end if;
3432 Next_Index (X);
3433 end loop;
3435 return V;
3437 else
3438 return Uint_0;
3439 end if;
3440 end Count_Tasks;
3442 ----------------------------
3443 -- Delayed_Aspect_Present --
3444 ----------------------------
3446 function Delayed_Aspect_Present return Boolean is
3447 A : Node_Id;
3448 A_Id : Aspect_Id;
3450 begin
3451 if Present (Aspect_Specifications (N)) then
3452 A := First (Aspect_Specifications (N));
3453 A_Id := Get_Aspect_Id (Chars (Identifier (A)));
3454 while Present (A) loop
3455 if A_Id = Aspect_Alignment or else A_Id = Aspect_Address then
3456 return True;
3457 end if;
3459 Next (A);
3460 end loop;
3461 end if;
3463 return False;
3464 end Delayed_Aspect_Present;
3466 -- Start of processing for Analyze_Object_Declaration
3468 begin
3469 -- There are three kinds of implicit types generated by an
3470 -- object declaration:
3472 -- 1. Those generated by the original Object Definition
3474 -- 2. Those generated by the Expression
3476 -- 3. Those used to constrain the Object Definition with the
3477 -- expression constraints when the definition is unconstrained.
3479 -- They must be generated in this order to avoid order of elaboration
3480 -- issues. Thus the first step (after entering the name) is to analyze
3481 -- the object definition.
3483 if Constant_Present (N) then
3484 Prev_Entity := Current_Entity_In_Scope (Id);
3486 if Present (Prev_Entity)
3487 and then
3488 -- If the homograph is an implicit subprogram, it is overridden
3489 -- by the current declaration.
3491 ((Is_Overloadable (Prev_Entity)
3492 and then Is_Inherited_Operation (Prev_Entity))
3494 -- The current object is a discriminal generated for an entry
3495 -- family index. Even though the index is a constant, in this
3496 -- particular context there is no true constant redeclaration.
3497 -- Enter_Name will handle the visibility.
3499 or else
3500 (Is_Discriminal (Id)
3501 and then Ekind (Discriminal_Link (Id)) =
3502 E_Entry_Index_Parameter)
3504 -- The current object is the renaming for a generic declared
3505 -- within the instance.
3507 or else
3508 (Ekind (Prev_Entity) = E_Package
3509 and then Nkind (Parent (Prev_Entity)) =
3510 N_Package_Renaming_Declaration
3511 and then not Comes_From_Source (Prev_Entity)
3512 and then
3513 Is_Generic_Instance (Renamed_Entity (Prev_Entity))))
3514 then
3515 Prev_Entity := Empty;
3516 end if;
3517 end if;
3519 -- The object declaration may be subject to pragma Ghost with policy
3520 -- Ignore. Set the mode now to ensure that any nodes generated during
3521 -- analysis and expansion are properly flagged as ignored Ghost.
3523 Set_Ghost_Mode (N, Prev_Entity);
3525 if Present (Prev_Entity) then
3526 Constant_Redeclaration (Id, N, T);
3528 Generate_Reference (Prev_Entity, Id, 'c');
3529 Set_Completion_Referenced (Id);
3531 if Error_Posted (N) then
3533 -- Type mismatch or illegal redeclaration, Do not analyze
3534 -- expression to avoid cascaded errors.
3536 T := Find_Type_Of_Object (Object_Definition (N), N);
3537 Set_Etype (Id, T);
3538 Set_Ekind (Id, E_Variable);
3539 goto Leave;
3540 end if;
3542 -- In the normal case, enter identifier at the start to catch premature
3543 -- usage in the initialization expression.
3545 else
3546 Generate_Definition (Id);
3547 Enter_Name (Id);
3549 Mark_Coextensions (N, Object_Definition (N));
3551 T := Find_Type_Of_Object (Object_Definition (N), N);
3553 if Nkind (Object_Definition (N)) = N_Access_Definition
3554 and then Present
3555 (Access_To_Subprogram_Definition (Object_Definition (N)))
3556 and then Protected_Present
3557 (Access_To_Subprogram_Definition (Object_Definition (N)))
3558 then
3559 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
3560 end if;
3562 if Error_Posted (Id) then
3563 Set_Etype (Id, T);
3564 Set_Ekind (Id, E_Variable);
3565 goto Leave;
3566 end if;
3567 end if;
3569 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
3570 -- out some static checks
3572 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (T) then
3574 -- In case of aggregates we must also take care of the correct
3575 -- initialization of nested aggregates bug this is done at the
3576 -- point of the analysis of the aggregate (see sem_aggr.adb).
3578 if Present (Expression (N))
3579 and then Nkind (Expression (N)) = N_Aggregate
3580 then
3581 null;
3583 else
3584 declare
3585 Save_Typ : constant Entity_Id := Etype (Id);
3586 begin
3587 Set_Etype (Id, T); -- Temp. decoration for static checks
3588 Null_Exclusion_Static_Checks (N);
3589 Set_Etype (Id, Save_Typ);
3590 end;
3591 end if;
3592 end if;
3594 -- Object is marked pure if it is in a pure scope
3596 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3598 -- If deferred constant, make sure context is appropriate. We detect
3599 -- a deferred constant as a constant declaration with no expression.
3600 -- A deferred constant can appear in a package body if its completion
3601 -- is by means of an interface pragma.
3603 if Constant_Present (N) and then No (E) then
3605 -- A deferred constant may appear in the declarative part of the
3606 -- following constructs:
3608 -- blocks
3609 -- entry bodies
3610 -- extended return statements
3611 -- package specs
3612 -- package bodies
3613 -- subprogram bodies
3614 -- task bodies
3616 -- When declared inside a package spec, a deferred constant must be
3617 -- completed by a full constant declaration or pragma Import. In all
3618 -- other cases, the only proper completion is pragma Import. Extended
3619 -- return statements are flagged as invalid contexts because they do
3620 -- not have a declarative part and so cannot accommodate the pragma.
3622 if Ekind (Current_Scope) = E_Return_Statement then
3623 Error_Msg_N
3624 ("invalid context for deferred constant declaration (RM 7.4)",
3626 Error_Msg_N
3627 ("\declaration requires an initialization expression",
3629 Set_Constant_Present (N, False);
3631 -- In Ada 83, deferred constant must be of private type
3633 elsif not Is_Private_Type (T) then
3634 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
3635 Error_Msg_N
3636 ("(Ada 83) deferred constant must be private type", N);
3637 end if;
3638 end if;
3640 -- If not a deferred constant, then the object declaration freezes
3641 -- its type, unless the object is of an anonymous type and has delayed
3642 -- aspects. In that case the type is frozen when the object itself is.
3644 else
3645 Check_Fully_Declared (T, N);
3647 if Has_Delayed_Aspects (Id)
3648 and then Is_Array_Type (T)
3649 and then Is_Itype (T)
3650 then
3651 Set_Has_Delayed_Freeze (T);
3652 else
3653 Freeze_Before (N, T);
3654 end if;
3655 end if;
3657 -- If the object was created by a constrained array definition, then
3658 -- set the link in both the anonymous base type and anonymous subtype
3659 -- that are built to represent the array type to point to the object.
3661 if Nkind (Object_Definition (Declaration_Node (Id))) =
3662 N_Constrained_Array_Definition
3663 then
3664 Set_Related_Array_Object (T, Id);
3665 Set_Related_Array_Object (Base_Type (T), Id);
3666 end if;
3668 -- Special checks for protected objects not at library level
3670 if Is_Protected_Type (T)
3671 and then not Is_Library_Level_Entity (Id)
3672 then
3673 Check_Restriction (No_Local_Protected_Objects, Id);
3675 -- Protected objects with interrupt handlers must be at library level
3677 -- Ada 2005: This test is not needed (and the corresponding clause
3678 -- in the RM is removed) because accessibility checks are sufficient
3679 -- to make handlers not at the library level illegal.
3681 -- AI05-0303: The AI is in fact a binding interpretation, and thus
3682 -- applies to the '95 version of the language as well.
3684 if Has_Interrupt_Handler (T) and then Ada_Version < Ada_95 then
3685 Error_Msg_N
3686 ("interrupt object can only be declared at library level", Id);
3687 end if;
3688 end if;
3690 -- The actual subtype of the object is the nominal subtype, unless
3691 -- the nominal one is unconstrained and obtained from the expression.
3693 Act_T := T;
3695 -- These checks should be performed before the initialization expression
3696 -- is considered, so that the Object_Definition node is still the same
3697 -- as in source code.
3699 -- In SPARK, the nominal subtype is always given by a subtype mark
3700 -- and must not be unconstrained. (The only exception to this is the
3701 -- acceptance of declarations of constants of type String.)
3703 if not Nkind_In (Object_Definition (N), N_Expanded_Name, N_Identifier)
3704 then
3705 Check_SPARK_05_Restriction
3706 ("subtype mark required", Object_Definition (N));
3708 elsif Is_Array_Type (T)
3709 and then not Is_Constrained (T)
3710 and then T /= Standard_String
3711 then
3712 Check_SPARK_05_Restriction
3713 ("subtype mark of constrained type expected",
3714 Object_Definition (N));
3715 end if;
3717 -- There are no aliased objects in SPARK
3719 if Aliased_Present (N) then
3720 Check_SPARK_05_Restriction ("aliased object is not allowed", N);
3721 end if;
3723 -- Process initialization expression if present and not in error
3725 if Present (E) and then E /= Error then
3727 -- Generate an error in case of CPP class-wide object initialization.
3728 -- Required because otherwise the expansion of the class-wide
3729 -- assignment would try to use 'size to initialize the object
3730 -- (primitive that is not available in CPP tagged types).
3732 if Is_Class_Wide_Type (Act_T)
3733 and then
3734 (Is_CPP_Class (Root_Type (Etype (Act_T)))
3735 or else
3736 (Present (Full_View (Root_Type (Etype (Act_T))))
3737 and then
3738 Is_CPP_Class (Full_View (Root_Type (Etype (Act_T))))))
3739 then
3740 Error_Msg_N
3741 ("predefined assignment not available for 'C'P'P tagged types",
3743 end if;
3745 Mark_Coextensions (N, E);
3746 Analyze (E);
3748 -- In case of errors detected in the analysis of the expression,
3749 -- decorate it with the expected type to avoid cascaded errors
3751 if No (Etype (E)) then
3752 Set_Etype (E, T);
3753 end if;
3755 -- If an initialization expression is present, then we set the
3756 -- Is_True_Constant flag. It will be reset if this is a variable
3757 -- and it is indeed modified.
3759 Set_Is_True_Constant (Id, True);
3761 -- If we are analyzing a constant declaration, set its completion
3762 -- flag after analyzing and resolving the expression.
3764 if Constant_Present (N) then
3765 Set_Has_Completion (Id);
3766 end if;
3768 -- Set type and resolve (type may be overridden later on). Note:
3769 -- Ekind (Id) must still be E_Void at this point so that incorrect
3770 -- early usage within E is properly diagnosed.
3772 Set_Etype (Id, T);
3774 -- If the expression is an aggregate we must look ahead to detect
3775 -- the possible presence of an address clause, and defer resolution
3776 -- and expansion of the aggregate to the freeze point of the entity.
3778 if Comes_From_Source (N)
3779 and then Expander_Active
3780 and then Nkind (E) = N_Aggregate
3781 and then (Present (Following_Address_Clause (N))
3782 or else Delayed_Aspect_Present)
3783 then
3784 Set_Etype (E, T);
3786 else
3787 Resolve (E, T);
3788 end if;
3790 -- No further action needed if E is a call to an inlined function
3791 -- which returns an unconstrained type and it has been expanded into
3792 -- a procedure call. In that case N has been replaced by an object
3793 -- declaration without initializing expression and it has been
3794 -- analyzed (see Expand_Inlined_Call).
3796 if Back_End_Inlining
3797 and then Expander_Active
3798 and then Nkind (E) = N_Function_Call
3799 and then Nkind (Name (E)) in N_Has_Entity
3800 and then Is_Inlined (Entity (Name (E)))
3801 and then not Is_Constrained (Etype (E))
3802 and then Analyzed (N)
3803 and then No (Expression (N))
3804 then
3805 return;
3806 end if;
3808 -- If E is null and has been replaced by an N_Raise_Constraint_Error
3809 -- node (which was marked already-analyzed), we need to set the type
3810 -- to something other than Any_Access in order to keep gigi happy.
3812 if Etype (E) = Any_Access then
3813 Set_Etype (E, T);
3814 end if;
3816 -- If the object is an access to variable, the initialization
3817 -- expression cannot be an access to constant.
3819 if Is_Access_Type (T)
3820 and then not Is_Access_Constant (T)
3821 and then Is_Access_Type (Etype (E))
3822 and then Is_Access_Constant (Etype (E))
3823 then
3824 Error_Msg_N
3825 ("access to variable cannot be initialized with an "
3826 & "access-to-constant expression", E);
3827 end if;
3829 if not Assignment_OK (N) then
3830 Check_Initialization (T, E);
3831 end if;
3833 Check_Unset_Reference (E);
3835 -- If this is a variable, then set current value. If this is a
3836 -- declared constant of a scalar type with a static expression,
3837 -- indicate that it is always valid.
3839 if not Constant_Present (N) then
3840 if Compile_Time_Known_Value (E) then
3841 Set_Current_Value (Id, E);
3842 end if;
3844 elsif Is_Scalar_Type (T) and then Is_OK_Static_Expression (E) then
3845 Set_Is_Known_Valid (Id);
3846 end if;
3848 -- Deal with setting of null flags
3850 if Is_Access_Type (T) then
3851 if Known_Non_Null (E) then
3852 Set_Is_Known_Non_Null (Id, True);
3853 elsif Known_Null (E) and then not Can_Never_Be_Null (Id) then
3854 Set_Is_Known_Null (Id, True);
3855 end if;
3856 end if;
3858 -- Check incorrect use of dynamically tagged expressions
3860 if Is_Tagged_Type (T) then
3861 Check_Dynamically_Tagged_Expression
3862 (Expr => E,
3863 Typ => T,
3864 Related_Nod => N);
3865 end if;
3867 Apply_Scalar_Range_Check (E, T);
3868 Apply_Static_Length_Check (E, T);
3870 if Nkind (Original_Node (N)) = N_Object_Declaration
3871 and then Comes_From_Source (Original_Node (N))
3873 -- Only call test if needed
3875 and then Restriction_Check_Required (SPARK_05)
3876 and then not Is_SPARK_05_Initialization_Expr (Original_Node (E))
3877 then
3878 Check_SPARK_05_Restriction
3879 ("initialization expression is not appropriate", E);
3880 end if;
3882 -- A formal parameter of a specific tagged type whose related
3883 -- subprogram is subject to pragma Extensions_Visible with value
3884 -- "False" cannot be implicitly converted to a class-wide type by
3885 -- means of an initialization expression (SPARK RM 6.1.7(3)).
3887 if Is_Class_Wide_Type (T) and then Is_EVF_Expression (E) then
3888 Error_Msg_N
3889 ("formal parameter with Extensions_Visible False cannot be "
3890 & "implicitly converted to class-wide type", E);
3891 end if;
3892 end if;
3894 -- If the No_Streams restriction is set, check that the type of the
3895 -- object is not, and does not contain, any subtype derived from
3896 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
3897 -- Has_Stream just for efficiency reasons. There is no point in
3898 -- spending time on a Has_Stream check if the restriction is not set.
3900 if Restriction_Check_Required (No_Streams) then
3901 if Has_Stream (T) then
3902 Check_Restriction (No_Streams, N);
3903 end if;
3904 end if;
3906 -- Deal with predicate check before we start to do major rewriting. It
3907 -- is OK to initialize and then check the initialized value, since the
3908 -- object goes out of scope if we get a predicate failure. Note that we
3909 -- do this in the analyzer and not the expander because the analyzer
3910 -- does some substantial rewriting in some cases.
3912 -- We need a predicate check if the type has predicates, and if either
3913 -- there is an initializing expression, or for default initialization
3914 -- when we have at least one case of an explicit default initial value
3915 -- and then this is not an internal declaration whose initialization
3916 -- comes later (as for an aggregate expansion).
3918 if not Suppress_Assignment_Checks (N)
3919 and then Present (Predicate_Function (T))
3920 and then not No_Initialization (N)
3921 and then
3922 (Present (E)
3923 or else
3924 Is_Partially_Initialized_Type (T, Include_Implicit => False))
3925 then
3926 -- If the type has a static predicate and the expression is known at
3927 -- compile time, see if the expression satisfies the predicate.
3929 if Present (E) then
3930 Check_Expression_Against_Static_Predicate (E, T);
3931 end if;
3933 Insert_After (N,
3934 Make_Predicate_Check (T, New_Occurrence_Of (Id, Loc)));
3935 end if;
3937 -- Case of unconstrained type
3939 if Is_Indefinite_Subtype (T) then
3941 -- In SPARK, a declaration of unconstrained type is allowed
3942 -- only for constants of type string.
3944 if Is_String_Type (T) and then not Constant_Present (N) then
3945 Check_SPARK_05_Restriction
3946 ("declaration of object of unconstrained type not allowed", N);
3947 end if;
3949 -- Nothing to do in deferred constant case
3951 if Constant_Present (N) and then No (E) then
3952 null;
3954 -- Case of no initialization present
3956 elsif No (E) then
3957 if No_Initialization (N) then
3958 null;
3960 elsif Is_Class_Wide_Type (T) then
3961 Error_Msg_N
3962 ("initialization required in class-wide declaration ", N);
3964 else
3965 Error_Msg_N
3966 ("unconstrained subtype not allowed (need initialization)",
3967 Object_Definition (N));
3969 if Is_Record_Type (T) and then Has_Discriminants (T) then
3970 Error_Msg_N
3971 ("\provide initial value or explicit discriminant values",
3972 Object_Definition (N));
3974 Error_Msg_NE
3975 ("\or give default discriminant values for type&",
3976 Object_Definition (N), T);
3978 elsif Is_Array_Type (T) then
3979 Error_Msg_N
3980 ("\provide initial value or explicit array bounds",
3981 Object_Definition (N));
3982 end if;
3983 end if;
3985 -- Case of initialization present but in error. Set initial
3986 -- expression as absent (but do not make above complaints)
3988 elsif E = Error then
3989 Set_Expression (N, Empty);
3990 E := Empty;
3992 -- Case of initialization present
3994 else
3995 -- Check restrictions in Ada 83
3997 if not Constant_Present (N) then
3999 -- Unconstrained variables not allowed in Ada 83 mode
4001 if Ada_Version = Ada_83
4002 and then Comes_From_Source (Object_Definition (N))
4003 then
4004 Error_Msg_N
4005 ("(Ada 83) unconstrained variable not allowed",
4006 Object_Definition (N));
4007 end if;
4008 end if;
4010 -- Now we constrain the variable from the initializing expression
4012 -- If the expression is an aggregate, it has been expanded into
4013 -- individual assignments. Retrieve the actual type from the
4014 -- expanded construct.
4016 if Is_Array_Type (T)
4017 and then No_Initialization (N)
4018 and then Nkind (Original_Node (E)) = N_Aggregate
4019 then
4020 Act_T := Etype (E);
4022 -- In case of class-wide interface object declarations we delay
4023 -- the generation of the equivalent record type declarations until
4024 -- its expansion because there are cases in they are not required.
4026 elsif Is_Interface (T) then
4027 null;
4029 -- In GNATprove mode, Expand_Subtype_From_Expr does nothing. Thus,
4030 -- we should prevent the generation of another Itype with the
4031 -- same name as the one already generated, or we end up with
4032 -- two identical types in GNATprove.
4034 elsif GNATprove_Mode then
4035 null;
4037 -- If the type is an unchecked union, no subtype can be built from
4038 -- the expression. Rewrite declaration as a renaming, which the
4039 -- back-end can handle properly. This is a rather unusual case,
4040 -- because most unchecked_union declarations have default values
4041 -- for discriminants and are thus not indefinite.
4043 elsif Is_Unchecked_Union (T) then
4044 if Constant_Present (N) or else Nkind (E) = N_Function_Call then
4045 Set_Ekind (Id, E_Constant);
4046 else
4047 Set_Ekind (Id, E_Variable);
4048 end if;
4050 -- An object declared within a Ghost region is automatically
4051 -- Ghost (SPARK RM 6.9(2)).
4053 if Comes_From_Source (Id) and then Ghost_Mode > None then
4054 Set_Is_Ghost_Entity (Id);
4056 -- The Ghost policy in effect at the point of declaration
4057 -- and at the point of completion must match
4058 -- (SPARK RM 6.9(14)).
4060 if Present (Prev_Entity)
4061 and then Is_Ghost_Entity (Prev_Entity)
4062 then
4063 Check_Ghost_Completion (Prev_Entity, Id);
4064 end if;
4065 end if;
4067 Rewrite (N,
4068 Make_Object_Renaming_Declaration (Loc,
4069 Defining_Identifier => Id,
4070 Subtype_Mark => New_Occurrence_Of (T, Loc),
4071 Name => E));
4073 Set_Renamed_Object (Id, E);
4074 Freeze_Before (N, T);
4075 Set_Is_Frozen (Id);
4076 return;
4078 else
4079 Expand_Subtype_From_Expr (N, T, Object_Definition (N), E);
4080 Act_T := Find_Type_Of_Object (Object_Definition (N), N);
4081 end if;
4083 Set_Is_Constr_Subt_For_U_Nominal (Act_T);
4085 if Aliased_Present (N) then
4086 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4087 end if;
4089 Freeze_Before (N, Act_T);
4090 Freeze_Before (N, T);
4091 end if;
4093 elsif Is_Array_Type (T)
4094 and then No_Initialization (N)
4095 and then Nkind (Original_Node (E)) = N_Aggregate
4096 then
4097 if not Is_Entity_Name (Object_Definition (N)) then
4098 Act_T := Etype (E);
4099 Check_Compile_Time_Size (Act_T);
4101 if Aliased_Present (N) then
4102 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4103 end if;
4104 end if;
4106 -- When the given object definition and the aggregate are specified
4107 -- independently, and their lengths might differ do a length check.
4108 -- This cannot happen if the aggregate is of the form (others =>...)
4110 if not Is_Constrained (T) then
4111 null;
4113 elsif Nkind (E) = N_Raise_Constraint_Error then
4115 -- Aggregate is statically illegal. Place back in declaration
4117 Set_Expression (N, E);
4118 Set_No_Initialization (N, False);
4120 elsif T = Etype (E) then
4121 null;
4123 elsif Nkind (E) = N_Aggregate
4124 and then Present (Component_Associations (E))
4125 and then Present (Choices (First (Component_Associations (E))))
4126 and then Nkind (First
4127 (Choices (First (Component_Associations (E))))) = N_Others_Choice
4128 then
4129 null;
4131 else
4132 Apply_Length_Check (E, T);
4133 end if;
4135 -- If the type is limited unconstrained with defaulted discriminants and
4136 -- there is no expression, then the object is constrained by the
4137 -- defaults, so it is worthwhile building the corresponding subtype.
4139 elsif (Is_Limited_Record (T) or else Is_Concurrent_Type (T))
4140 and then not Is_Constrained (T)
4141 and then Has_Discriminants (T)
4142 then
4143 if No (E) then
4144 Act_T := Build_Default_Subtype (T, N);
4145 else
4146 -- Ada 2005: A limited object may be initialized by means of an
4147 -- aggregate. If the type has default discriminants it has an
4148 -- unconstrained nominal type, Its actual subtype will be obtained
4149 -- from the aggregate, and not from the default discriminants.
4151 Act_T := Etype (E);
4152 end if;
4154 Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
4156 elsif Nkind (E) = N_Function_Call
4157 and then Constant_Present (N)
4158 and then Has_Unconstrained_Elements (Etype (E))
4159 then
4160 -- The back-end has problems with constants of a discriminated type
4161 -- with defaults, if the initial value is a function call. We
4162 -- generate an intermediate temporary that will receive a reference
4163 -- to the result of the call. The initialization expression then
4164 -- becomes a dereference of that temporary.
4166 Remove_Side_Effects (E);
4168 -- If this is a constant declaration of an unconstrained type and
4169 -- the initialization is an aggregate, we can use the subtype of the
4170 -- aggregate for the declared entity because it is immutable.
4172 elsif not Is_Constrained (T)
4173 and then Has_Discriminants (T)
4174 and then Constant_Present (N)
4175 and then not Has_Unchecked_Union (T)
4176 and then Nkind (E) = N_Aggregate
4177 then
4178 Act_T := Etype (E);
4179 end if;
4181 -- Check No_Wide_Characters restriction
4183 Check_Wide_Character_Restriction (T, Object_Definition (N));
4185 -- Indicate this is not set in source. Certainly true for constants, and
4186 -- true for variables so far (will be reset for a variable if and when
4187 -- we encounter a modification in the source).
4189 Set_Never_Set_In_Source (Id);
4191 -- Now establish the proper kind and type of the object
4193 if Constant_Present (N) then
4194 Set_Ekind (Id, E_Constant);
4195 Set_Is_True_Constant (Id);
4197 else
4198 Set_Ekind (Id, E_Variable);
4200 -- A variable is set as shared passive if it appears in a shared
4201 -- passive package, and is at the outer level. This is not done for
4202 -- entities generated during expansion, because those are always
4203 -- manipulated locally.
4205 if Is_Shared_Passive (Current_Scope)
4206 and then Is_Library_Level_Entity (Id)
4207 and then Comes_From_Source (Id)
4208 then
4209 Set_Is_Shared_Passive (Id);
4210 Check_Shared_Var (Id, T, N);
4211 end if;
4213 -- Set Has_Initial_Value if initializing expression present. Note
4214 -- that if there is no initializing expression, we leave the state
4215 -- of this flag unchanged (usually it will be False, but notably in
4216 -- the case of exception choice variables, it will already be true).
4218 if Present (E) then
4219 Set_Has_Initial_Value (Id);
4220 end if;
4221 end if;
4223 -- Initialize alignment and size and capture alignment setting
4225 Init_Alignment (Id);
4226 Init_Esize (Id);
4227 Set_Optimize_Alignment_Flags (Id);
4229 -- An object declared within a Ghost region is automatically Ghost
4230 -- (SPARK RM 6.9(2)).
4232 if Comes_From_Source (Id)
4233 and then (Ghost_Mode > None
4234 or else (Present (Prev_Entity)
4235 and then Is_Ghost_Entity (Prev_Entity)))
4236 then
4237 Set_Is_Ghost_Entity (Id);
4239 -- The Ghost policy in effect at the point of declaration and at the
4240 -- point of completion must match (SPARK RM 6.9(14)).
4242 if Present (Prev_Entity) and then Is_Ghost_Entity (Prev_Entity) then
4243 Check_Ghost_Completion (Prev_Entity, Id);
4244 end if;
4245 end if;
4247 -- Deal with aliased case
4249 if Aliased_Present (N) then
4250 Set_Is_Aliased (Id);
4252 -- If the object is aliased and the type is unconstrained with
4253 -- defaulted discriminants and there is no expression, then the
4254 -- object is constrained by the defaults, so it is worthwhile
4255 -- building the corresponding subtype.
4257 -- Ada 2005 (AI-363): If the aliased object is discriminated and
4258 -- unconstrained, then only establish an actual subtype if the
4259 -- nominal subtype is indefinite. In definite cases the object is
4260 -- unconstrained in Ada 2005.
4262 if No (E)
4263 and then Is_Record_Type (T)
4264 and then not Is_Constrained (T)
4265 and then Has_Discriminants (T)
4266 and then (Ada_Version < Ada_2005 or else Is_Indefinite_Subtype (T))
4267 then
4268 Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
4269 end if;
4270 end if;
4272 -- Now we can set the type of the object
4274 Set_Etype (Id, Act_T);
4276 -- Non-constant object is marked to be treated as volatile if type is
4277 -- volatile and we clear the Current_Value setting that may have been
4278 -- set above. Doing so for constants isn't required and might interfere
4279 -- with possible uses of the object as a static expression in contexts
4280 -- incompatible with volatility (e.g. as a case-statement alternative).
4282 if Ekind (Id) /= E_Constant and then Treat_As_Volatile (Etype (Id)) then
4283 Set_Treat_As_Volatile (Id);
4284 Set_Current_Value (Id, Empty);
4285 end if;
4287 -- Deal with controlled types
4289 if Has_Controlled_Component (Etype (Id))
4290 or else Is_Controlled (Etype (Id))
4291 then
4292 if not Is_Library_Level_Entity (Id) then
4293 Check_Restriction (No_Nested_Finalization, N);
4294 else
4295 Validate_Controlled_Object (Id);
4296 end if;
4297 end if;
4299 if Has_Task (Etype (Id)) then
4300 Check_Restriction (No_Tasking, N);
4302 -- Deal with counting max tasks
4304 -- Nothing to do if inside a generic
4306 if Inside_A_Generic then
4307 null;
4309 -- If library level entity, then count tasks
4311 elsif Is_Library_Level_Entity (Id) then
4312 Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
4314 -- If not library level entity, then indicate we don't know max
4315 -- tasks and also check task hierarchy restriction and blocking
4316 -- operation (since starting a task is definitely blocking).
4318 else
4319 Check_Restriction (Max_Tasks, N);
4320 Check_Restriction (No_Task_Hierarchy, N);
4321 Check_Potentially_Blocking_Operation (N);
4322 end if;
4324 -- A rather specialized test. If we see two tasks being declared
4325 -- of the same type in the same object declaration, and the task
4326 -- has an entry with an address clause, we know that program error
4327 -- will be raised at run time since we can't have two tasks with
4328 -- entries at the same address.
4330 if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
4331 declare
4332 E : Entity_Id;
4334 begin
4335 E := First_Entity (Etype (Id));
4336 while Present (E) loop
4337 if Ekind (E) = E_Entry
4338 and then Present (Get_Attribute_Definition_Clause
4339 (E, Attribute_Address))
4340 then
4341 Error_Msg_Warn := SPARK_Mode /= On;
4342 Error_Msg_N
4343 ("more than one task with same entry address<<", N);
4344 Error_Msg_N ("\Program_Error [<<", N);
4345 Insert_Action (N,
4346 Make_Raise_Program_Error (Loc,
4347 Reason => PE_Duplicated_Entry_Address));
4348 exit;
4349 end if;
4351 Next_Entity (E);
4352 end loop;
4353 end;
4354 end if;
4355 end if;
4357 -- Some simple constant-propagation: if the expression is a constant
4358 -- string initialized with a literal, share the literal. This avoids
4359 -- a run-time copy.
4361 if Present (E)
4362 and then Is_Entity_Name (E)
4363 and then Ekind (Entity (E)) = E_Constant
4364 and then Base_Type (Etype (E)) = Standard_String
4365 then
4366 declare
4367 Val : constant Node_Id := Constant_Value (Entity (E));
4368 begin
4369 if Present (Val) and then Nkind (Val) = N_String_Literal then
4370 Rewrite (E, New_Copy (Val));
4371 end if;
4372 end;
4373 end if;
4375 -- Another optimization: if the nominal subtype is unconstrained and
4376 -- the expression is a function call that returns an unconstrained
4377 -- type, rewrite the declaration as a renaming of the result of the
4378 -- call. The exceptions below are cases where the copy is expected,
4379 -- either by the back end (Aliased case) or by the semantics, as for
4380 -- initializing controlled types or copying tags for classwide types.
4382 if Present (E)
4383 and then Nkind (E) = N_Explicit_Dereference
4384 and then Nkind (Original_Node (E)) = N_Function_Call
4385 and then not Is_Library_Level_Entity (Id)
4386 and then not Is_Constrained (Underlying_Type (T))
4387 and then not Is_Aliased (Id)
4388 and then not Is_Class_Wide_Type (T)
4389 and then not Is_Controlled (T)
4390 and then not Has_Controlled_Component (Base_Type (T))
4391 and then Expander_Active
4392 then
4393 Rewrite (N,
4394 Make_Object_Renaming_Declaration (Loc,
4395 Defining_Identifier => Id,
4396 Access_Definition => Empty,
4397 Subtype_Mark => New_Occurrence_Of
4398 (Base_Type (Etype (Id)), Loc),
4399 Name => E));
4401 Set_Renamed_Object (Id, E);
4403 -- Force generation of debugging information for the constant and for
4404 -- the renamed function call.
4406 Set_Debug_Info_Needed (Id);
4407 Set_Debug_Info_Needed (Entity (Prefix (E)));
4408 end if;
4410 if Present (Prev_Entity)
4411 and then Is_Frozen (Prev_Entity)
4412 and then not Error_Posted (Id)
4413 then
4414 Error_Msg_N ("full constant declaration appears too late", N);
4415 end if;
4417 Check_Eliminated (Id);
4419 -- Deal with setting In_Private_Part flag if in private part
4421 if Ekind (Scope (Id)) = E_Package and then In_Private_Part (Scope (Id))
4422 then
4423 Set_In_Private_Part (Id);
4424 end if;
4426 -- Check for violation of No_Local_Timing_Events
4428 if Restriction_Check_Required (No_Local_Timing_Events)
4429 and then not Is_Library_Level_Entity (Id)
4430 and then Is_RTE (Etype (Id), RE_Timing_Event)
4431 then
4432 Check_Restriction (No_Local_Timing_Events, N);
4433 end if;
4435 <<Leave>>
4436 -- Initialize the refined state of a variable here because this is a
4437 -- common destination for legal and illegal object declarations.
4439 if Ekind (Id) = E_Variable then
4440 Set_Encapsulating_State (Id, Empty);
4441 end if;
4443 if Has_Aspects (N) then
4444 Analyze_Aspect_Specifications (N, Id);
4445 end if;
4447 Analyze_Dimension (N);
4449 -- Verify whether the object declaration introduces an illegal hidden
4450 -- state within a package subject to a null abstract state.
4452 if Ekind (Id) = E_Variable then
4453 Check_No_Hidden_State (Id);
4454 end if;
4455 end Analyze_Object_Declaration;
4457 ---------------------------
4458 -- Analyze_Others_Choice --
4459 ---------------------------
4461 -- Nothing to do for the others choice node itself, the semantic analysis
4462 -- of the others choice will occur as part of the processing of the parent
4464 procedure Analyze_Others_Choice (N : Node_Id) is
4465 pragma Warnings (Off, N);
4466 begin
4467 null;
4468 end Analyze_Others_Choice;
4470 -------------------------------------------
4471 -- Analyze_Private_Extension_Declaration --
4472 -------------------------------------------
4474 procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
4475 T : constant Entity_Id := Defining_Identifier (N);
4476 Indic : constant Node_Id := Subtype_Indication (N);
4477 Parent_Type : Entity_Id;
4478 Parent_Base : Entity_Id;
4480 begin
4481 -- The private extension declaration may be subject to pragma Ghost with
4482 -- policy Ignore. Set the mode now to ensure that any nodes generated
4483 -- during analysis and expansion are properly flagged as ignored Ghost.
4485 Set_Ghost_Mode (N);
4487 -- Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
4489 if Is_Non_Empty_List (Interface_List (N)) then
4490 declare
4491 Intf : Node_Id;
4492 T : Entity_Id;
4494 begin
4495 Intf := First (Interface_List (N));
4496 while Present (Intf) loop
4497 T := Find_Type_Of_Subtype_Indic (Intf);
4499 Diagnose_Interface (Intf, T);
4500 Next (Intf);
4501 end loop;
4502 end;
4503 end if;
4505 Generate_Definition (T);
4507 -- For other than Ada 2012, just enter the name in the current scope
4509 if Ada_Version < Ada_2012 then
4510 Enter_Name (T);
4512 -- Ada 2012 (AI05-0162): Enter the name in the current scope handling
4513 -- case of private type that completes an incomplete type.
4515 else
4516 declare
4517 Prev : Entity_Id;
4519 begin
4520 Prev := Find_Type_Name (N);
4522 pragma Assert (Prev = T
4523 or else (Ekind (Prev) = E_Incomplete_Type
4524 and then Present (Full_View (Prev))
4525 and then Full_View (Prev) = T));
4526 end;
4527 end if;
4529 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
4530 Parent_Base := Base_Type (Parent_Type);
4532 if Parent_Type = Any_Type or else Etype (Parent_Type) = Any_Type then
4533 Set_Ekind (T, Ekind (Parent_Type));
4534 Set_Etype (T, Any_Type);
4535 goto Leave;
4537 elsif not Is_Tagged_Type (Parent_Type) then
4538 Error_Msg_N
4539 ("parent of type extension must be a tagged type ", Indic);
4540 goto Leave;
4542 elsif Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
4543 Error_Msg_N ("premature derivation of incomplete type", Indic);
4544 goto Leave;
4546 elsif Is_Concurrent_Type (Parent_Type) then
4547 Error_Msg_N
4548 ("parent type of a private extension cannot be "
4549 & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
4551 Set_Etype (T, Any_Type);
4552 Set_Ekind (T, E_Limited_Private_Type);
4553 Set_Private_Dependents (T, New_Elmt_List);
4554 Set_Error_Posted (T);
4555 goto Leave;
4556 end if;
4558 -- Perhaps the parent type should be changed to the class-wide type's
4559 -- specific type in this case to prevent cascading errors ???
4561 if Is_Class_Wide_Type (Parent_Type) then
4562 Error_Msg_N
4563 ("parent of type extension must not be a class-wide type", Indic);
4564 goto Leave;
4565 end if;
4567 if (not Is_Package_Or_Generic_Package (Current_Scope)
4568 and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
4569 or else In_Private_Part (Current_Scope)
4571 then
4572 Error_Msg_N ("invalid context for private extension", N);
4573 end if;
4575 -- Set common attributes
4577 Set_Is_Pure (T, Is_Pure (Current_Scope));
4578 Set_Scope (T, Current_Scope);
4579 Set_Ekind (T, E_Record_Type_With_Private);
4580 Init_Size_Align (T);
4581 Set_Default_SSO (T);
4583 Set_Etype (T, Parent_Base);
4584 Set_Has_Task (T, Has_Task (Parent_Base));
4585 Set_Has_Protected (T, Has_Task (Parent_Base));
4587 Set_Convention (T, Convention (Parent_Type));
4588 Set_First_Rep_Item (T, First_Rep_Item (Parent_Type));
4589 Set_Is_First_Subtype (T);
4590 Make_Class_Wide_Type (T);
4592 if Unknown_Discriminants_Present (N) then
4593 Set_Discriminant_Constraint (T, No_Elist);
4594 end if;
4596 Build_Derived_Record_Type (N, Parent_Type, T);
4598 -- Propagate inherited invariant information. The new type has
4599 -- invariants, if the parent type has inheritable invariants,
4600 -- and these invariants can in turn be inherited.
4602 if Has_Inheritable_Invariants (Parent_Type) then
4603 Set_Has_Inheritable_Invariants (T);
4604 Set_Has_Invariants (T);
4605 end if;
4607 -- Ada 2005 (AI-443): Synchronized private extension or a rewritten
4608 -- synchronized formal derived type.
4610 if Ada_Version >= Ada_2005 and then Synchronized_Present (N) then
4611 Set_Is_Limited_Record (T);
4613 -- Formal derived type case
4615 if Is_Generic_Type (T) then
4617 -- The parent must be a tagged limited type or a synchronized
4618 -- interface.
4620 if (not Is_Tagged_Type (Parent_Type)
4621 or else not Is_Limited_Type (Parent_Type))
4622 and then
4623 (not Is_Interface (Parent_Type)
4624 or else not Is_Synchronized_Interface (Parent_Type))
4625 then
4626 Error_Msg_NE ("parent type of & must be tagged limited " &
4627 "or synchronized", N, T);
4628 end if;
4630 -- The progenitors (if any) must be limited or synchronized
4631 -- interfaces.
4633 if Present (Interfaces (T)) then
4634 declare
4635 Iface : Entity_Id;
4636 Iface_Elmt : Elmt_Id;
4638 begin
4639 Iface_Elmt := First_Elmt (Interfaces (T));
4640 while Present (Iface_Elmt) loop
4641 Iface := Node (Iface_Elmt);
4643 if not Is_Limited_Interface (Iface)
4644 and then not Is_Synchronized_Interface (Iface)
4645 then
4646 Error_Msg_NE ("progenitor & must be limited " &
4647 "or synchronized", N, Iface);
4648 end if;
4650 Next_Elmt (Iface_Elmt);
4651 end loop;
4652 end;
4653 end if;
4655 -- Regular derived extension, the parent must be a limited or
4656 -- synchronized interface.
4658 else
4659 if not Is_Interface (Parent_Type)
4660 or else (not Is_Limited_Interface (Parent_Type)
4661 and then not Is_Synchronized_Interface (Parent_Type))
4662 then
4663 Error_Msg_NE
4664 ("parent type of & must be limited interface", N, T);
4665 end if;
4666 end if;
4668 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
4669 -- extension with a synchronized parent must be explicitly declared
4670 -- synchronized, because the full view will be a synchronized type.
4671 -- This must be checked before the check for limited types below,
4672 -- to ensure that types declared limited are not allowed to extend
4673 -- synchronized interfaces.
4675 elsif Is_Interface (Parent_Type)
4676 and then Is_Synchronized_Interface (Parent_Type)
4677 and then not Synchronized_Present (N)
4678 then
4679 Error_Msg_NE
4680 ("private extension of& must be explicitly synchronized",
4681 N, Parent_Type);
4683 elsif Limited_Present (N) then
4684 Set_Is_Limited_Record (T);
4686 if not Is_Limited_Type (Parent_Type)
4687 and then
4688 (not Is_Interface (Parent_Type)
4689 or else not Is_Limited_Interface (Parent_Type))
4690 then
4691 Error_Msg_NE ("parent type& of limited extension must be limited",
4692 N, Parent_Type);
4693 end if;
4694 end if;
4696 <<Leave>>
4697 if Has_Aspects (N) then
4698 Analyze_Aspect_Specifications (N, T);
4699 end if;
4700 end Analyze_Private_Extension_Declaration;
4702 ---------------------------------
4703 -- Analyze_Subtype_Declaration --
4704 ---------------------------------
4706 procedure Analyze_Subtype_Declaration
4707 (N : Node_Id;
4708 Skip : Boolean := False)
4710 Id : constant Entity_Id := Defining_Identifier (N);
4711 T : Entity_Id;
4712 R_Checks : Check_Result;
4714 begin
4715 -- The subtype declaration may be subject to pragma Ghost with policy
4716 -- Ignore. Set the mode now to ensure that any nodes generated during
4717 -- analysis and expansion are properly flagged as ignored Ghost.
4719 Set_Ghost_Mode (N);
4721 Generate_Definition (Id);
4722 Set_Is_Pure (Id, Is_Pure (Current_Scope));
4723 Init_Size_Align (Id);
4725 -- The following guard condition on Enter_Name is to handle cases where
4726 -- the defining identifier has already been entered into the scope but
4727 -- the declaration as a whole needs to be analyzed.
4729 -- This case in particular happens for derived enumeration types. The
4730 -- derived enumeration type is processed as an inserted enumeration type
4731 -- declaration followed by a rewritten subtype declaration. The defining
4732 -- identifier, however, is entered into the name scope very early in the
4733 -- processing of the original type declaration and therefore needs to be
4734 -- avoided here, when the created subtype declaration is analyzed. (See
4735 -- Build_Derived_Types)
4737 -- This also happens when the full view of a private type is derived
4738 -- type with constraints. In this case the entity has been introduced
4739 -- in the private declaration.
4741 -- Finally this happens in some complex cases when validity checks are
4742 -- enabled, where the same subtype declaration may be analyzed twice.
4743 -- This can happen if the subtype is created by the pre-analysis of
4744 -- an attribute tht gives the range of a loop statement, and the loop
4745 -- itself appears within an if_statement that will be rewritten during
4746 -- expansion.
4748 if Skip
4749 or else (Present (Etype (Id))
4750 and then (Is_Private_Type (Etype (Id))
4751 or else Is_Task_Type (Etype (Id))
4752 or else Is_Rewrite_Substitution (N)))
4753 then
4754 null;
4756 elsif Current_Entity (Id) = Id then
4757 null;
4759 else
4760 Enter_Name (Id);
4761 end if;
4763 T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
4765 -- Class-wide equivalent types of records with unknown discriminants
4766 -- involve the generation of an itype which serves as the private view
4767 -- of a constrained record subtype. In such cases the base type of the
4768 -- current subtype we are processing is the private itype. Use the full
4769 -- of the private itype when decorating various attributes.
4771 if Is_Itype (T)
4772 and then Is_Private_Type (T)
4773 and then Present (Full_View (T))
4774 then
4775 T := Full_View (T);
4776 end if;
4778 -- Inherit common attributes
4780 Set_Is_Volatile (Id, Is_Volatile (T));
4781 Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
4782 Set_Is_Generic_Type (Id, Is_Generic_Type (Base_Type (T)));
4783 Set_Convention (Id, Convention (T));
4785 -- If ancestor has predicates then so does the subtype, and in addition
4786 -- we must delay the freeze to properly arrange predicate inheritance.
4788 -- The Ancestor_Type test is really unpleasant, there seem to be cases
4789 -- in which T = ID, so the above tests and assignments do nothing???
4791 if Has_Predicates (T)
4792 or else (Present (Ancestor_Subtype (T))
4793 and then Has_Predicates (Ancestor_Subtype (T)))
4794 then
4795 Set_Has_Predicates (Id);
4796 Set_Has_Delayed_Freeze (Id);
4797 end if;
4799 -- Subtype of Boolean cannot have a constraint in SPARK
4801 if Is_Boolean_Type (T)
4802 and then Nkind (Subtype_Indication (N)) = N_Subtype_Indication
4803 then
4804 Check_SPARK_05_Restriction
4805 ("subtype of Boolean cannot have constraint", N);
4806 end if;
4808 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
4809 declare
4810 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
4811 One_Cstr : Node_Id;
4812 Low : Node_Id;
4813 High : Node_Id;
4815 begin
4816 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint then
4817 One_Cstr := First (Constraints (Cstr));
4818 while Present (One_Cstr) loop
4820 -- Index or discriminant constraint in SPARK must be a
4821 -- subtype mark.
4823 if not
4824 Nkind_In (One_Cstr, N_Identifier, N_Expanded_Name)
4825 then
4826 Check_SPARK_05_Restriction
4827 ("subtype mark required", One_Cstr);
4829 -- String subtype must have a lower bound of 1 in SPARK.
4830 -- Note that we do not need to test for the non-static case
4831 -- here, since that was already taken care of in
4832 -- Process_Range_Expr_In_Decl.
4834 elsif Base_Type (T) = Standard_String then
4835 Get_Index_Bounds (One_Cstr, Low, High);
4837 if Is_OK_Static_Expression (Low)
4838 and then Expr_Value (Low) /= 1
4839 then
4840 Check_SPARK_05_Restriction
4841 ("String subtype must have lower bound of 1", N);
4842 end if;
4843 end if;
4845 Next (One_Cstr);
4846 end loop;
4847 end if;
4848 end;
4849 end if;
4851 -- In the case where there is no constraint given in the subtype
4852 -- indication, Process_Subtype just returns the Subtype_Mark, so its
4853 -- semantic attributes must be established here.
4855 if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
4856 Set_Etype (Id, Base_Type (T));
4858 -- Subtype of unconstrained array without constraint is not allowed
4859 -- in SPARK.
4861 if Is_Array_Type (T) and then not Is_Constrained (T) then
4862 Check_SPARK_05_Restriction
4863 ("subtype of unconstrained array must have constraint", N);
4864 end if;
4866 case Ekind (T) is
4867 when Array_Kind =>
4868 Set_Ekind (Id, E_Array_Subtype);
4869 Copy_Array_Subtype_Attributes (Id, T);
4871 when Decimal_Fixed_Point_Kind =>
4872 Set_Ekind (Id, E_Decimal_Fixed_Point_Subtype);
4873 Set_Digits_Value (Id, Digits_Value (T));
4874 Set_Delta_Value (Id, Delta_Value (T));
4875 Set_Scale_Value (Id, Scale_Value (T));
4876 Set_Small_Value (Id, Small_Value (T));
4877 Set_Scalar_Range (Id, Scalar_Range (T));
4878 Set_Machine_Radix_10 (Id, Machine_Radix_10 (T));
4879 Set_Is_Constrained (Id, Is_Constrained (T));
4880 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4881 Set_RM_Size (Id, RM_Size (T));
4883 when Enumeration_Kind =>
4884 Set_Ekind (Id, E_Enumeration_Subtype);
4885 Set_First_Literal (Id, First_Literal (Base_Type (T)));
4886 Set_Scalar_Range (Id, Scalar_Range (T));
4887 Set_Is_Character_Type (Id, Is_Character_Type (T));
4888 Set_Is_Constrained (Id, Is_Constrained (T));
4889 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4890 Set_RM_Size (Id, RM_Size (T));
4891 Inherit_Predicate_Flags (Id, T);
4893 when Ordinary_Fixed_Point_Kind =>
4894 Set_Ekind (Id, E_Ordinary_Fixed_Point_Subtype);
4895 Set_Scalar_Range (Id, Scalar_Range (T));
4896 Set_Small_Value (Id, Small_Value (T));
4897 Set_Delta_Value (Id, Delta_Value (T));
4898 Set_Is_Constrained (Id, Is_Constrained (T));
4899 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4900 Set_RM_Size (Id, RM_Size (T));
4902 when Float_Kind =>
4903 Set_Ekind (Id, E_Floating_Point_Subtype);
4904 Set_Scalar_Range (Id, Scalar_Range (T));
4905 Set_Digits_Value (Id, Digits_Value (T));
4906 Set_Is_Constrained (Id, Is_Constrained (T));
4908 when Signed_Integer_Kind =>
4909 Set_Ekind (Id, E_Signed_Integer_Subtype);
4910 Set_Scalar_Range (Id, Scalar_Range (T));
4911 Set_Is_Constrained (Id, Is_Constrained (T));
4912 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4913 Set_RM_Size (Id, RM_Size (T));
4914 Inherit_Predicate_Flags (Id, T);
4916 when Modular_Integer_Kind =>
4917 Set_Ekind (Id, E_Modular_Integer_Subtype);
4918 Set_Scalar_Range (Id, Scalar_Range (T));
4919 Set_Is_Constrained (Id, Is_Constrained (T));
4920 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4921 Set_RM_Size (Id, RM_Size (T));
4922 Inherit_Predicate_Flags (Id, T);
4924 when Class_Wide_Kind =>
4925 Set_Ekind (Id, E_Class_Wide_Subtype);
4926 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
4927 Set_Cloned_Subtype (Id, T);
4928 Set_Is_Tagged_Type (Id, True);
4929 Set_Has_Unknown_Discriminants
4930 (Id, True);
4931 Set_No_Tagged_Streams_Pragma
4932 (Id, No_Tagged_Streams_Pragma (T));
4934 if Ekind (T) = E_Class_Wide_Subtype then
4935 Set_Equivalent_Type (Id, Equivalent_Type (T));
4936 end if;
4938 when E_Record_Type | E_Record_Subtype =>
4939 Set_Ekind (Id, E_Record_Subtype);
4941 if Ekind (T) = E_Record_Subtype
4942 and then Present (Cloned_Subtype (T))
4943 then
4944 Set_Cloned_Subtype (Id, Cloned_Subtype (T));
4945 else
4946 Set_Cloned_Subtype (Id, T);
4947 end if;
4949 Set_First_Entity (Id, First_Entity (T));
4950 Set_Last_Entity (Id, Last_Entity (T));
4951 Set_Has_Discriminants (Id, Has_Discriminants (T));
4952 Set_Is_Constrained (Id, Is_Constrained (T));
4953 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
4954 Set_Has_Implicit_Dereference
4955 (Id, Has_Implicit_Dereference (T));
4956 Set_Has_Unknown_Discriminants
4957 (Id, Has_Unknown_Discriminants (T));
4959 if Has_Discriminants (T) then
4960 Set_Discriminant_Constraint
4961 (Id, Discriminant_Constraint (T));
4962 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
4964 elsif Has_Unknown_Discriminants (Id) then
4965 Set_Discriminant_Constraint (Id, No_Elist);
4966 end if;
4968 if Is_Tagged_Type (T) then
4969 Set_Is_Tagged_Type (Id, True);
4970 Set_No_Tagged_Streams_Pragma
4971 (Id, No_Tagged_Streams_Pragma (T));
4972 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
4973 Set_Direct_Primitive_Operations
4974 (Id, Direct_Primitive_Operations (T));
4975 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
4977 if Is_Interface (T) then
4978 Set_Is_Interface (Id);
4979 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
4980 end if;
4981 end if;
4983 when Private_Kind =>
4984 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
4985 Set_Has_Discriminants (Id, Has_Discriminants (T));
4986 Set_Is_Constrained (Id, Is_Constrained (T));
4987 Set_First_Entity (Id, First_Entity (T));
4988 Set_Last_Entity (Id, Last_Entity (T));
4989 Set_Private_Dependents (Id, New_Elmt_List);
4990 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
4991 Set_Has_Implicit_Dereference
4992 (Id, Has_Implicit_Dereference (T));
4993 Set_Has_Unknown_Discriminants
4994 (Id, Has_Unknown_Discriminants (T));
4995 Set_Known_To_Have_Preelab_Init
4996 (Id, Known_To_Have_Preelab_Init (T));
4998 if Is_Tagged_Type (T) then
4999 Set_Is_Tagged_Type (Id);
5000 Set_No_Tagged_Streams_Pragma (Id,
5001 No_Tagged_Streams_Pragma (T));
5002 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5003 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5004 Set_Direct_Primitive_Operations (Id,
5005 Direct_Primitive_Operations (T));
5006 end if;
5008 -- In general the attributes of the subtype of a private type
5009 -- are the attributes of the partial view of parent. However,
5010 -- the full view may be a discriminated type, and the subtype
5011 -- must share the discriminant constraint to generate correct
5012 -- calls to initialization procedures.
5014 if Has_Discriminants (T) then
5015 Set_Discriminant_Constraint
5016 (Id, Discriminant_Constraint (T));
5017 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5019 elsif Present (Full_View (T))
5020 and then Has_Discriminants (Full_View (T))
5021 then
5022 Set_Discriminant_Constraint
5023 (Id, Discriminant_Constraint (Full_View (T)));
5024 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5026 -- This would seem semantically correct, but apparently
5027 -- generates spurious errors about missing components ???
5029 -- Set_Has_Discriminants (Id);
5030 end if;
5032 Prepare_Private_Subtype_Completion (Id, N);
5034 -- If this is the subtype of a constrained private type with
5035 -- discriminants that has got a full view and we also have
5036 -- built a completion just above, show that the completion
5037 -- is a clone of the full view to the back-end.
5039 if Has_Discriminants (T)
5040 and then not Has_Unknown_Discriminants (T)
5041 and then not Is_Empty_Elmt_List (Discriminant_Constraint (T))
5042 and then Present (Full_View (T))
5043 and then Present (Full_View (Id))
5044 then
5045 Set_Cloned_Subtype (Full_View (Id), Full_View (T));
5046 end if;
5048 when Access_Kind =>
5049 Set_Ekind (Id, E_Access_Subtype);
5050 Set_Is_Constrained (Id, Is_Constrained (T));
5051 Set_Is_Access_Constant
5052 (Id, Is_Access_Constant (T));
5053 Set_Directly_Designated_Type
5054 (Id, Designated_Type (T));
5055 Set_Can_Never_Be_Null (Id, Can_Never_Be_Null (T));
5057 -- A Pure library_item must not contain the declaration of a
5058 -- named access type, except within a subprogram, generic
5059 -- subprogram, task unit, or protected unit, or if it has
5060 -- a specified Storage_Size of zero (RM05-10.2.1(15.4-15.5)).
5062 if Comes_From_Source (Id)
5063 and then In_Pure_Unit
5064 and then not In_Subprogram_Task_Protected_Unit
5065 and then not No_Pool_Assigned (Id)
5066 then
5067 Error_Msg_N
5068 ("named access types not allowed in pure unit", N);
5069 end if;
5071 when Concurrent_Kind =>
5072 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
5073 Set_Corresponding_Record_Type (Id,
5074 Corresponding_Record_Type (T));
5075 Set_First_Entity (Id, First_Entity (T));
5076 Set_First_Private_Entity (Id, First_Private_Entity (T));
5077 Set_Has_Discriminants (Id, Has_Discriminants (T));
5078 Set_Is_Constrained (Id, Is_Constrained (T));
5079 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5080 Set_Last_Entity (Id, Last_Entity (T));
5082 if Is_Tagged_Type (T) then
5083 Set_No_Tagged_Streams_Pragma
5084 (Id, No_Tagged_Streams_Pragma (T));
5085 end if;
5087 if Has_Discriminants (T) then
5088 Set_Discriminant_Constraint
5089 (Id, Discriminant_Constraint (T));
5090 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5091 end if;
5093 when Incomplete_Kind =>
5094 if Ada_Version >= Ada_2005 then
5096 -- In Ada 2005 an incomplete type can be explicitly tagged:
5097 -- propagate indication. Note that we also have to include
5098 -- subtypes for Ada 2012 extended use of incomplete types.
5100 Set_Ekind (Id, E_Incomplete_Subtype);
5101 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5102 Set_Private_Dependents (Id, New_Elmt_List);
5104 if Is_Tagged_Type (Id) then
5105 Set_No_Tagged_Streams_Pragma
5106 (Id, No_Tagged_Streams_Pragma (T));
5107 Set_Direct_Primitive_Operations (Id, New_Elmt_List);
5108 end if;
5110 -- Ada 2005 (AI-412): Decorate an incomplete subtype of an
5111 -- incomplete type visible through a limited with clause.
5113 if From_Limited_With (T)
5114 and then Present (Non_Limited_View (T))
5115 then
5116 Set_From_Limited_With (Id);
5117 Set_Non_Limited_View (Id, Non_Limited_View (T));
5119 -- Ada 2005 (AI-412): Add the regular incomplete subtype
5120 -- to the private dependents of the original incomplete
5121 -- type for future transformation.
5123 else
5124 Append_Elmt (Id, Private_Dependents (T));
5125 end if;
5127 -- If the subtype name denotes an incomplete type an error
5128 -- was already reported by Process_Subtype.
5130 else
5131 Set_Etype (Id, Any_Type);
5132 end if;
5134 when others =>
5135 raise Program_Error;
5136 end case;
5137 end if;
5139 if Etype (Id) = Any_Type then
5140 goto Leave;
5141 end if;
5143 -- Some common processing on all types
5145 Set_Size_Info (Id, T);
5146 Set_First_Rep_Item (Id, First_Rep_Item (T));
5148 -- If the parent type is a generic actual, so is the subtype. This may
5149 -- happen in a nested instance. Why Comes_From_Source test???
5151 if not Comes_From_Source (N) then
5152 Set_Is_Generic_Actual_Type (Id, Is_Generic_Actual_Type (T));
5153 end if;
5155 T := Etype (Id);
5157 Set_Is_Immediately_Visible (Id, True);
5158 Set_Depends_On_Private (Id, Has_Private_Component (T));
5159 Set_Is_Descendent_Of_Address (Id, Is_Descendent_Of_Address (T));
5161 if Is_Interface (T) then
5162 Set_Is_Interface (Id);
5163 end if;
5165 if Present (Generic_Parent_Type (N))
5166 and then
5167 (Nkind (Parent (Generic_Parent_Type (N))) /=
5168 N_Formal_Type_Declaration
5169 or else Nkind (Formal_Type_Definition
5170 (Parent (Generic_Parent_Type (N)))) /=
5171 N_Formal_Private_Type_Definition)
5172 then
5173 if Is_Tagged_Type (Id) then
5175 -- If this is a generic actual subtype for a synchronized type,
5176 -- the primitive operations are those of the corresponding record
5177 -- for which there is a separate subtype declaration.
5179 if Is_Concurrent_Type (Id) then
5180 null;
5181 elsif Is_Class_Wide_Type (Id) then
5182 Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
5183 else
5184 Derive_Subprograms (Generic_Parent_Type (N), Id, T);
5185 end if;
5187 elsif Scope (Etype (Id)) /= Standard_Standard then
5188 Derive_Subprograms (Generic_Parent_Type (N), Id);
5189 end if;
5190 end if;
5192 if Is_Private_Type (T) and then Present (Full_View (T)) then
5193 Conditional_Delay (Id, Full_View (T));
5195 -- The subtypes of components or subcomponents of protected types
5196 -- do not need freeze nodes, which would otherwise appear in the
5197 -- wrong scope (before the freeze node for the protected type). The
5198 -- proper subtypes are those of the subcomponents of the corresponding
5199 -- record.
5201 elsif Ekind (Scope (Id)) /= E_Protected_Type
5202 and then Present (Scope (Scope (Id))) -- error defense
5203 and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
5204 then
5205 Conditional_Delay (Id, T);
5206 end if;
5208 -- Check that Constraint_Error is raised for a scalar subtype indication
5209 -- when the lower or upper bound of a non-null range lies outside the
5210 -- range of the type mark.
5212 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5213 if Is_Scalar_Type (Etype (Id))
5214 and then Scalar_Range (Id) /=
5215 Scalar_Range (Etype (Subtype_Mark
5216 (Subtype_Indication (N))))
5217 then
5218 Apply_Range_Check
5219 (Scalar_Range (Id),
5220 Etype (Subtype_Mark (Subtype_Indication (N))));
5222 -- In the array case, check compatibility for each index
5224 elsif Is_Array_Type (Etype (Id)) and then Present (First_Index (Id))
5225 then
5226 -- This really should be a subprogram that finds the indications
5227 -- to check???
5229 declare
5230 Subt_Index : Node_Id := First_Index (Id);
5231 Target_Index : Node_Id :=
5232 First_Index (Etype
5233 (Subtype_Mark (Subtype_Indication (N))));
5234 Has_Dyn_Chk : Boolean := Has_Dynamic_Range_Check (N);
5236 begin
5237 while Present (Subt_Index) loop
5238 if ((Nkind (Subt_Index) = N_Identifier
5239 and then Ekind (Entity (Subt_Index)) in Scalar_Kind)
5240 or else Nkind (Subt_Index) = N_Subtype_Indication)
5241 and then
5242 Nkind (Scalar_Range (Etype (Subt_Index))) = N_Range
5243 then
5244 declare
5245 Target_Typ : constant Entity_Id :=
5246 Etype (Target_Index);
5247 begin
5248 R_Checks :=
5249 Get_Range_Checks
5250 (Scalar_Range (Etype (Subt_Index)),
5251 Target_Typ,
5252 Etype (Subt_Index),
5253 Defining_Identifier (N));
5255 -- Reset Has_Dynamic_Range_Check on the subtype to
5256 -- prevent elision of the index check due to a dynamic
5257 -- check generated for a preceding index (needed since
5258 -- Insert_Range_Checks tries to avoid generating
5259 -- redundant checks on a given declaration).
5261 Set_Has_Dynamic_Range_Check (N, False);
5263 Insert_Range_Checks
5264 (R_Checks,
5266 Target_Typ,
5267 Sloc (Defining_Identifier (N)));
5269 -- Record whether this index involved a dynamic check
5271 Has_Dyn_Chk :=
5272 Has_Dyn_Chk or else Has_Dynamic_Range_Check (N);
5273 end;
5274 end if;
5276 Next_Index (Subt_Index);
5277 Next_Index (Target_Index);
5278 end loop;
5280 -- Finally, mark whether the subtype involves dynamic checks
5282 Set_Has_Dynamic_Range_Check (N, Has_Dyn_Chk);
5283 end;
5284 end if;
5285 end if;
5287 -- A type invariant applies to any subtype in its scope, in particular
5288 -- to a generic actual.
5290 if Has_Invariants (T) and then In_Open_Scopes (Scope (T)) then
5291 Set_Has_Invariants (Id);
5292 Set_Invariant_Procedure (Id, Invariant_Procedure (T));
5293 end if;
5295 -- Make sure that generic actual types are properly frozen. The subtype
5296 -- is marked as a generic actual type when the enclosing instance is
5297 -- analyzed, so here we identify the subtype from the tree structure.
5299 if Expander_Active
5300 and then Is_Generic_Actual_Type (Id)
5301 and then In_Instance
5302 and then not Comes_From_Source (N)
5303 and then Nkind (Subtype_Indication (N)) /= N_Subtype_Indication
5304 and then Is_Frozen (T)
5305 then
5306 Freeze_Before (N, Id);
5307 end if;
5309 Set_Optimize_Alignment_Flags (Id);
5310 Check_Eliminated (Id);
5312 <<Leave>>
5313 if Has_Aspects (N) then
5314 Analyze_Aspect_Specifications (N, Id);
5315 end if;
5317 Analyze_Dimension (N);
5318 end Analyze_Subtype_Declaration;
5320 --------------------------------
5321 -- Analyze_Subtype_Indication --
5322 --------------------------------
5324 procedure Analyze_Subtype_Indication (N : Node_Id) is
5325 T : constant Entity_Id := Subtype_Mark (N);
5326 R : constant Node_Id := Range_Expression (Constraint (N));
5328 begin
5329 Analyze (T);
5331 if R /= Error then
5332 Analyze (R);
5333 Set_Etype (N, Etype (R));
5334 Resolve (R, Entity (T));
5335 else
5336 Set_Error_Posted (R);
5337 Set_Error_Posted (T);
5338 end if;
5339 end Analyze_Subtype_Indication;
5341 --------------------------
5342 -- Analyze_Variant_Part --
5343 --------------------------
5345 procedure Analyze_Variant_Part (N : Node_Id) is
5346 Discr_Name : Node_Id;
5347 Discr_Type : Entity_Id;
5349 procedure Process_Variant (A : Node_Id);
5350 -- Analyze declarations for a single variant
5352 package Analyze_Variant_Choices is
5353 new Generic_Analyze_Choices (Process_Variant);
5354 use Analyze_Variant_Choices;
5356 ---------------------
5357 -- Process_Variant --
5358 ---------------------
5360 procedure Process_Variant (A : Node_Id) is
5361 CL : constant Node_Id := Component_List (A);
5362 begin
5363 if not Null_Present (CL) then
5364 Analyze_Declarations (Component_Items (CL));
5366 if Present (Variant_Part (CL)) then
5367 Analyze (Variant_Part (CL));
5368 end if;
5369 end if;
5370 end Process_Variant;
5372 -- Start of processing for Analyze_Variant_Part
5374 begin
5375 Discr_Name := Name (N);
5376 Analyze (Discr_Name);
5378 -- If Discr_Name bad, get out (prevent cascaded errors)
5380 if Etype (Discr_Name) = Any_Type then
5381 return;
5382 end if;
5384 -- Check invalid discriminant in variant part
5386 if Ekind (Entity (Discr_Name)) /= E_Discriminant then
5387 Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
5388 end if;
5390 Discr_Type := Etype (Entity (Discr_Name));
5392 if not Is_Discrete_Type (Discr_Type) then
5393 Error_Msg_N
5394 ("discriminant in a variant part must be of a discrete type",
5395 Name (N));
5396 return;
5397 end if;
5399 -- Now analyze the choices, which also analyzes the declarations that
5400 -- are associated with each choice.
5402 Analyze_Choices (Variants (N), Discr_Type);
5404 -- Note: we used to instantiate and call Check_Choices here to check
5405 -- that the choices covered the discriminant, but it's too early to do
5406 -- that because of statically predicated subtypes, whose analysis may
5407 -- be deferred to their freeze point which may be as late as the freeze
5408 -- point of the containing record. So this call is now to be found in
5409 -- Freeze_Record_Declaration.
5411 end Analyze_Variant_Part;
5413 ----------------------------
5414 -- Array_Type_Declaration --
5415 ----------------------------
5417 procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
5418 Component_Def : constant Node_Id := Component_Definition (Def);
5419 Component_Typ : constant Node_Id := Subtype_Indication (Component_Def);
5420 Element_Type : Entity_Id;
5421 Implicit_Base : Entity_Id;
5422 Index : Node_Id;
5423 Related_Id : Entity_Id := Empty;
5424 Nb_Index : Nat;
5425 P : constant Node_Id := Parent (Def);
5426 Priv : Entity_Id;
5428 begin
5429 if Nkind (Def) = N_Constrained_Array_Definition then
5430 Index := First (Discrete_Subtype_Definitions (Def));
5431 else
5432 Index := First (Subtype_Marks (Def));
5433 end if;
5435 -- Find proper names for the implicit types which may be public. In case
5436 -- of anonymous arrays we use the name of the first object of that type
5437 -- as prefix.
5439 if No (T) then
5440 Related_Id := Defining_Identifier (P);
5441 else
5442 Related_Id := T;
5443 end if;
5445 Nb_Index := 1;
5446 while Present (Index) loop
5447 Analyze (Index);
5449 -- Test for odd case of trying to index a type by the type itself
5451 if Is_Entity_Name (Index) and then Entity (Index) = T then
5452 Error_Msg_N ("type& cannot be indexed by itself", Index);
5453 Set_Entity (Index, Standard_Boolean);
5454 Set_Etype (Index, Standard_Boolean);
5455 end if;
5457 -- Check SPARK restriction requiring a subtype mark
5459 if not Nkind_In (Index, N_Identifier, N_Expanded_Name) then
5460 Check_SPARK_05_Restriction ("subtype mark required", Index);
5461 end if;
5463 -- Add a subtype declaration for each index of private array type
5464 -- declaration whose etype is also private. For example:
5466 -- package Pkg is
5467 -- type Index is private;
5468 -- private
5469 -- type Table is array (Index) of ...
5470 -- end;
5472 -- This is currently required by the expander for the internally
5473 -- generated equality subprogram of records with variant parts in
5474 -- which the etype of some component is such private type.
5476 if Ekind (Current_Scope) = E_Package
5477 and then In_Private_Part (Current_Scope)
5478 and then Has_Private_Declaration (Etype (Index))
5479 then
5480 declare
5481 Loc : constant Source_Ptr := Sloc (Def);
5482 New_E : Entity_Id;
5483 Decl : Entity_Id;
5485 begin
5486 New_E := Make_Temporary (Loc, 'T');
5487 Set_Is_Internal (New_E);
5489 Decl :=
5490 Make_Subtype_Declaration (Loc,
5491 Defining_Identifier => New_E,
5492 Subtype_Indication =>
5493 New_Occurrence_Of (Etype (Index), Loc));
5495 Insert_Before (Parent (Def), Decl);
5496 Analyze (Decl);
5497 Set_Etype (Index, New_E);
5499 -- If the index is a range the Entity attribute is not
5500 -- available. Example:
5502 -- package Pkg is
5503 -- type T is private;
5504 -- private
5505 -- type T is new Natural;
5506 -- Table : array (T(1) .. T(10)) of Boolean;
5507 -- end Pkg;
5509 if Nkind (Index) /= N_Range then
5510 Set_Entity (Index, New_E);
5511 end if;
5512 end;
5513 end if;
5515 Make_Index (Index, P, Related_Id, Nb_Index);
5517 -- Check error of subtype with predicate for index type
5519 Bad_Predicated_Subtype_Use
5520 ("subtype& has predicate, not allowed as index subtype",
5521 Index, Etype (Index));
5523 -- Move to next index
5525 Next_Index (Index);
5526 Nb_Index := Nb_Index + 1;
5527 end loop;
5529 -- Process subtype indication if one is present
5531 if Present (Component_Typ) then
5532 Element_Type := Process_Subtype (Component_Typ, P, Related_Id, 'C');
5534 Set_Etype (Component_Typ, Element_Type);
5536 if not Nkind_In (Component_Typ, N_Identifier, N_Expanded_Name) then
5537 Check_SPARK_05_Restriction
5538 ("subtype mark required", Component_Typ);
5539 end if;
5541 -- Ada 2005 (AI-230): Access Definition case
5543 else pragma Assert (Present (Access_Definition (Component_Def)));
5545 -- Indicate that the anonymous access type is created by the
5546 -- array type declaration.
5548 Element_Type := Access_Definition
5549 (Related_Nod => P,
5550 N => Access_Definition (Component_Def));
5551 Set_Is_Local_Anonymous_Access (Element_Type);
5553 -- Propagate the parent. This field is needed if we have to generate
5554 -- the master_id associated with an anonymous access to task type
5555 -- component (see Expand_N_Full_Type_Declaration.Build_Master)
5557 Set_Parent (Element_Type, Parent (T));
5559 -- Ada 2005 (AI-230): In case of components that are anonymous access
5560 -- types the level of accessibility depends on the enclosing type
5561 -- declaration
5563 Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
5565 -- Ada 2005 (AI-254)
5567 declare
5568 CD : constant Node_Id :=
5569 Access_To_Subprogram_Definition
5570 (Access_Definition (Component_Def));
5571 begin
5572 if Present (CD) and then Protected_Present (CD) then
5573 Element_Type :=
5574 Replace_Anonymous_Access_To_Protected_Subprogram (Def);
5575 end if;
5576 end;
5577 end if;
5579 -- Constrained array case
5581 if No (T) then
5582 T := Create_Itype (E_Void, P, Related_Id, 'T');
5583 end if;
5585 if Nkind (Def) = N_Constrained_Array_Definition then
5587 -- Establish Implicit_Base as unconstrained base type
5589 Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
5591 Set_Etype (Implicit_Base, Implicit_Base);
5592 Set_Scope (Implicit_Base, Current_Scope);
5593 Set_Has_Delayed_Freeze (Implicit_Base);
5594 Set_Default_SSO (Implicit_Base);
5596 -- The constrained array type is a subtype of the unconstrained one
5598 Set_Ekind (T, E_Array_Subtype);
5599 Init_Size_Align (T);
5600 Set_Etype (T, Implicit_Base);
5601 Set_Scope (T, Current_Scope);
5602 Set_Is_Constrained (T);
5603 Set_First_Index (T,
5604 First (Discrete_Subtype_Definitions (Def)));
5605 Set_Has_Delayed_Freeze (T);
5607 -- Complete setup of implicit base type
5609 Set_First_Index (Implicit_Base, First_Index (T));
5610 Set_Component_Type (Implicit_Base, Element_Type);
5611 Set_Has_Task (Implicit_Base, Has_Task (Element_Type));
5612 Set_Has_Protected (Implicit_Base, Has_Protected (Element_Type));
5613 Set_Component_Size (Implicit_Base, Uint_0);
5614 Set_Packed_Array_Impl_Type (Implicit_Base, Empty);
5615 Set_Has_Controlled_Component (Implicit_Base,
5616 Has_Controlled_Component (Element_Type)
5617 or else Is_Controlled (Element_Type));
5618 Set_Finalize_Storage_Only (Implicit_Base,
5619 Finalize_Storage_Only (Element_Type));
5621 -- Inherit the "ghostness" from the constrained array type
5623 if Is_Ghost_Entity (T) or else Ghost_Mode > None then
5624 Set_Is_Ghost_Entity (Implicit_Base);
5625 end if;
5627 -- Unconstrained array case
5629 else
5630 Set_Ekind (T, E_Array_Type);
5631 Init_Size_Align (T);
5632 Set_Etype (T, T);
5633 Set_Scope (T, Current_Scope);
5634 Set_Component_Size (T, Uint_0);
5635 Set_Is_Constrained (T, False);
5636 Set_First_Index (T, First (Subtype_Marks (Def)));
5637 Set_Has_Delayed_Freeze (T, True);
5638 Set_Has_Task (T, Has_Task (Element_Type));
5639 Set_Has_Protected (T, Has_Protected (Element_Type));
5640 Set_Has_Controlled_Component (T, Has_Controlled_Component
5641 (Element_Type)
5642 or else
5643 Is_Controlled (Element_Type));
5644 Set_Finalize_Storage_Only (T, Finalize_Storage_Only
5645 (Element_Type));
5646 Set_Default_SSO (T);
5647 end if;
5649 -- Common attributes for both cases
5651 Set_Component_Type (Base_Type (T), Element_Type);
5652 Set_Packed_Array_Impl_Type (T, Empty);
5654 if Aliased_Present (Component_Definition (Def)) then
5655 Check_SPARK_05_Restriction
5656 ("aliased is not allowed", Component_Definition (Def));
5657 Set_Has_Aliased_Components (Etype (T));
5658 end if;
5660 -- Ada 2005 (AI-231): Propagate the null-excluding attribute to the
5661 -- array type to ensure that objects of this type are initialized.
5663 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (Element_Type) then
5664 Set_Can_Never_Be_Null (T);
5666 if Null_Exclusion_Present (Component_Definition (Def))
5668 -- No need to check itypes because in their case this check was
5669 -- done at their point of creation
5671 and then not Is_Itype (Element_Type)
5672 then
5673 Error_Msg_N
5674 ("`NOT NULL` not allowed (null already excluded)",
5675 Subtype_Indication (Component_Definition (Def)));
5676 end if;
5677 end if;
5679 Priv := Private_Component (Element_Type);
5681 if Present (Priv) then
5683 -- Check for circular definitions
5685 if Priv = Any_Type then
5686 Set_Component_Type (Etype (T), Any_Type);
5688 -- There is a gap in the visibility of operations on the composite
5689 -- type only if the component type is defined in a different scope.
5691 elsif Scope (Priv) = Current_Scope then
5692 null;
5694 elsif Is_Limited_Type (Priv) then
5695 Set_Is_Limited_Composite (Etype (T));
5696 Set_Is_Limited_Composite (T);
5697 else
5698 Set_Is_Private_Composite (Etype (T));
5699 Set_Is_Private_Composite (T);
5700 end if;
5701 end if;
5703 -- A syntax error in the declaration itself may lead to an empty index
5704 -- list, in which case do a minimal patch.
5706 if No (First_Index (T)) then
5707 Error_Msg_N ("missing index definition in array type declaration", T);
5709 declare
5710 Indexes : constant List_Id :=
5711 New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
5712 begin
5713 Set_Discrete_Subtype_Definitions (Def, Indexes);
5714 Set_First_Index (T, First (Indexes));
5715 return;
5716 end;
5717 end if;
5719 -- Create a concatenation operator for the new type. Internal array
5720 -- types created for packed entities do not need such, they are
5721 -- compatible with the user-defined type.
5723 if Number_Dimensions (T) = 1
5724 and then not Is_Packed_Array_Impl_Type (T)
5725 then
5726 New_Concatenation_Op (T);
5727 end if;
5729 -- In the case of an unconstrained array the parser has already verified
5730 -- that all the indexes are unconstrained but we still need to make sure
5731 -- that the element type is constrained.
5733 if Is_Indefinite_Subtype (Element_Type) then
5734 Error_Msg_N
5735 ("unconstrained element type in array declaration",
5736 Subtype_Indication (Component_Def));
5738 elsif Is_Abstract_Type (Element_Type) then
5739 Error_Msg_N
5740 ("the type of a component cannot be abstract",
5741 Subtype_Indication (Component_Def));
5742 end if;
5744 -- There may be an invariant declared for the component type, but
5745 -- the construction of the component invariant checking procedure
5746 -- takes place during expansion.
5747 end Array_Type_Declaration;
5749 ------------------------------------------------------
5750 -- Replace_Anonymous_Access_To_Protected_Subprogram --
5751 ------------------------------------------------------
5753 function Replace_Anonymous_Access_To_Protected_Subprogram
5754 (N : Node_Id) return Entity_Id
5756 Loc : constant Source_Ptr := Sloc (N);
5758 Curr_Scope : constant Scope_Stack_Entry :=
5759 Scope_Stack.Table (Scope_Stack.Last);
5761 Anon : constant Entity_Id := Make_Temporary (Loc, 'S');
5763 Acc : Node_Id;
5764 -- Access definition in declaration
5766 Comp : Node_Id;
5767 -- Object definition or formal definition with an access definition
5769 Decl : Node_Id;
5770 -- Declaration of anonymous access to subprogram type
5772 Spec : Node_Id;
5773 -- Original specification in access to subprogram
5775 P : Node_Id;
5777 begin
5778 Set_Is_Internal (Anon);
5780 case Nkind (N) is
5781 when N_Component_Declaration |
5782 N_Unconstrained_Array_Definition |
5783 N_Constrained_Array_Definition =>
5784 Comp := Component_Definition (N);
5785 Acc := Access_Definition (Comp);
5787 when N_Discriminant_Specification =>
5788 Comp := Discriminant_Type (N);
5789 Acc := Comp;
5791 when N_Parameter_Specification =>
5792 Comp := Parameter_Type (N);
5793 Acc := Comp;
5795 when N_Access_Function_Definition =>
5796 Comp := Result_Definition (N);
5797 Acc := Comp;
5799 when N_Object_Declaration =>
5800 Comp := Object_Definition (N);
5801 Acc := Comp;
5803 when N_Function_Specification =>
5804 Comp := Result_Definition (N);
5805 Acc := Comp;
5807 when others =>
5808 raise Program_Error;
5809 end case;
5811 Spec := Access_To_Subprogram_Definition (Acc);
5813 Decl :=
5814 Make_Full_Type_Declaration (Loc,
5815 Defining_Identifier => Anon,
5816 Type_Definition => Copy_Separate_Tree (Spec));
5818 Mark_Rewrite_Insertion (Decl);
5820 -- In ASIS mode, analyze the profile on the original node, because
5821 -- the separate copy does not provide enough links to recover the
5822 -- original tree. Analysis is limited to type annotations, within
5823 -- a temporary scope that serves as an anonymous subprogram to collect
5824 -- otherwise useless temporaries and itypes.
5826 if ASIS_Mode then
5827 declare
5828 Typ : constant Entity_Id := Make_Temporary (Loc, 'S');
5830 begin
5831 if Nkind (Spec) = N_Access_Function_Definition then
5832 Set_Ekind (Typ, E_Function);
5833 else
5834 Set_Ekind (Typ, E_Procedure);
5835 end if;
5837 Set_Parent (Typ, N);
5838 Set_Scope (Typ, Current_Scope);
5839 Push_Scope (Typ);
5841 -- Nothing to do if procedure is parameterless
5843 if Present (Parameter_Specifications (Spec)) then
5844 Process_Formals (Parameter_Specifications (Spec), Spec);
5845 end if;
5847 if Nkind (Spec) = N_Access_Function_Definition then
5848 declare
5849 Def : constant Node_Id := Result_Definition (Spec);
5851 begin
5852 -- The result might itself be an anonymous access type, so
5853 -- have to recurse.
5855 if Nkind (Def) = N_Access_Definition then
5856 if Present (Access_To_Subprogram_Definition (Def)) then
5857 Set_Etype
5858 (Def,
5859 Replace_Anonymous_Access_To_Protected_Subprogram
5860 (Spec));
5861 else
5862 Find_Type (Subtype_Mark (Def));
5863 end if;
5865 else
5866 Find_Type (Def);
5867 end if;
5868 end;
5869 end if;
5871 End_Scope;
5872 end;
5873 end if;
5875 -- Insert the new declaration in the nearest enclosing scope. If the
5876 -- node is a body and N is its return type, the declaration belongs in
5877 -- the enclosing scope.
5879 P := Parent (N);
5881 if Nkind (P) = N_Subprogram_Body
5882 and then Nkind (N) = N_Function_Specification
5883 then
5884 P := Parent (P);
5885 end if;
5887 while Present (P) and then not Has_Declarations (P) loop
5888 P := Parent (P);
5889 end loop;
5891 pragma Assert (Present (P));
5893 if Nkind (P) = N_Package_Specification then
5894 Prepend (Decl, Visible_Declarations (P));
5895 else
5896 Prepend (Decl, Declarations (P));
5897 end if;
5899 -- Replace the anonymous type with an occurrence of the new declaration.
5900 -- In all cases the rewritten node does not have the null-exclusion
5901 -- attribute because (if present) it was already inherited by the
5902 -- anonymous entity (Anon). Thus, in case of components we do not
5903 -- inherit this attribute.
5905 if Nkind (N) = N_Parameter_Specification then
5906 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5907 Set_Etype (Defining_Identifier (N), Anon);
5908 Set_Null_Exclusion_Present (N, False);
5910 elsif Nkind (N) = N_Object_Declaration then
5911 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5912 Set_Etype (Defining_Identifier (N), Anon);
5914 elsif Nkind (N) = N_Access_Function_Definition then
5915 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5917 elsif Nkind (N) = N_Function_Specification then
5918 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5919 Set_Etype (Defining_Unit_Name (N), Anon);
5921 else
5922 Rewrite (Comp,
5923 Make_Component_Definition (Loc,
5924 Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
5925 end if;
5927 Mark_Rewrite_Insertion (Comp);
5929 if Nkind_In (N, N_Object_Declaration, N_Access_Function_Definition) then
5930 Analyze (Decl);
5932 else
5933 -- Temporarily remove the current scope (record or subprogram) from
5934 -- the stack to add the new declarations to the enclosing scope.
5936 Scope_Stack.Decrement_Last;
5937 Analyze (Decl);
5938 Set_Is_Itype (Anon);
5939 Scope_Stack.Append (Curr_Scope);
5940 end if;
5942 Set_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
5943 Set_Can_Use_Internal_Rep (Anon, not Always_Compatible_Rep_On_Target);
5944 return Anon;
5945 end Replace_Anonymous_Access_To_Protected_Subprogram;
5947 -------------------------------
5948 -- Build_Derived_Access_Type --
5949 -------------------------------
5951 procedure Build_Derived_Access_Type
5952 (N : Node_Id;
5953 Parent_Type : Entity_Id;
5954 Derived_Type : Entity_Id)
5956 S : constant Node_Id := Subtype_Indication (Type_Definition (N));
5958 Desig_Type : Entity_Id;
5959 Discr : Entity_Id;
5960 Discr_Con_Elist : Elist_Id;
5961 Discr_Con_El : Elmt_Id;
5962 Subt : Entity_Id;
5964 begin
5965 -- Set the designated type so it is available in case this is an access
5966 -- to a self-referential type, e.g. a standard list type with a next
5967 -- pointer. Will be reset after subtype is built.
5969 Set_Directly_Designated_Type
5970 (Derived_Type, Designated_Type (Parent_Type));
5972 Subt := Process_Subtype (S, N);
5974 if Nkind (S) /= N_Subtype_Indication
5975 and then Subt /= Base_Type (Subt)
5976 then
5977 Set_Ekind (Derived_Type, E_Access_Subtype);
5978 end if;
5980 if Ekind (Derived_Type) = E_Access_Subtype then
5981 declare
5982 Pbase : constant Entity_Id := Base_Type (Parent_Type);
5983 Ibase : constant Entity_Id :=
5984 Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
5985 Svg_Chars : constant Name_Id := Chars (Ibase);
5986 Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
5988 begin
5989 Copy_Node (Pbase, Ibase);
5991 Set_Chars (Ibase, Svg_Chars);
5992 Set_Next_Entity (Ibase, Svg_Next_E);
5993 Set_Sloc (Ibase, Sloc (Derived_Type));
5994 Set_Scope (Ibase, Scope (Derived_Type));
5995 Set_Freeze_Node (Ibase, Empty);
5996 Set_Is_Frozen (Ibase, False);
5997 Set_Comes_From_Source (Ibase, False);
5998 Set_Is_First_Subtype (Ibase, False);
6000 Set_Etype (Ibase, Pbase);
6001 Set_Etype (Derived_Type, Ibase);
6002 end;
6003 end if;
6005 Set_Directly_Designated_Type
6006 (Derived_Type, Designated_Type (Subt));
6008 Set_Is_Constrained (Derived_Type, Is_Constrained (Subt));
6009 Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
6010 Set_Size_Info (Derived_Type, Parent_Type);
6011 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
6012 Set_Depends_On_Private (Derived_Type,
6013 Has_Private_Component (Derived_Type));
6014 Conditional_Delay (Derived_Type, Subt);
6016 -- Ada 2005 (AI-231): Set the null-exclusion attribute, and verify
6017 -- that it is not redundant.
6019 if Null_Exclusion_Present (Type_Definition (N)) then
6020 Set_Can_Never_Be_Null (Derived_Type);
6022 -- What is with the "AND THEN FALSE" here ???
6024 if Can_Never_Be_Null (Parent_Type)
6025 and then False
6026 then
6027 Error_Msg_NE
6028 ("`NOT NULL` not allowed (& already excludes null)",
6029 N, Parent_Type);
6030 end if;
6032 elsif Can_Never_Be_Null (Parent_Type) then
6033 Set_Can_Never_Be_Null (Derived_Type);
6034 end if;
6036 -- Note: we do not copy the Storage_Size_Variable, since we always go to
6037 -- the root type for this information.
6039 -- Apply range checks to discriminants for derived record case
6040 -- ??? THIS CODE SHOULD NOT BE HERE REALLY.
6042 Desig_Type := Designated_Type (Derived_Type);
6043 if Is_Composite_Type (Desig_Type)
6044 and then (not Is_Array_Type (Desig_Type))
6045 and then Has_Discriminants (Desig_Type)
6046 and then Base_Type (Desig_Type) /= Desig_Type
6047 then
6048 Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
6049 Discr_Con_El := First_Elmt (Discr_Con_Elist);
6051 Discr := First_Discriminant (Base_Type (Desig_Type));
6052 while Present (Discr_Con_El) loop
6053 Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
6054 Next_Elmt (Discr_Con_El);
6055 Next_Discriminant (Discr);
6056 end loop;
6057 end if;
6058 end Build_Derived_Access_Type;
6060 ------------------------------
6061 -- Build_Derived_Array_Type --
6062 ------------------------------
6064 procedure Build_Derived_Array_Type
6065 (N : Node_Id;
6066 Parent_Type : Entity_Id;
6067 Derived_Type : Entity_Id)
6069 Loc : constant Source_Ptr := Sloc (N);
6070 Tdef : constant Node_Id := Type_Definition (N);
6071 Indic : constant Node_Id := Subtype_Indication (Tdef);
6072 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
6073 Implicit_Base : Entity_Id;
6074 New_Indic : Node_Id;
6076 procedure Make_Implicit_Base;
6077 -- If the parent subtype is constrained, the derived type is a subtype
6078 -- of an implicit base type derived from the parent base.
6080 ------------------------
6081 -- Make_Implicit_Base --
6082 ------------------------
6084 procedure Make_Implicit_Base is
6085 begin
6086 Implicit_Base :=
6087 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
6089 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
6090 Set_Etype (Implicit_Base, Parent_Base);
6092 Copy_Array_Subtype_Attributes (Implicit_Base, Parent_Base);
6093 Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
6095 Set_Has_Delayed_Freeze (Implicit_Base, True);
6097 -- Inherit the "ghostness" from the parent base type
6099 if Is_Ghost_Entity (Parent_Base) or else Ghost_Mode > None then
6100 Set_Is_Ghost_Entity (Implicit_Base);
6101 end if;
6102 end Make_Implicit_Base;
6104 -- Start of processing for Build_Derived_Array_Type
6106 begin
6107 if not Is_Constrained (Parent_Type) then
6108 if Nkind (Indic) /= N_Subtype_Indication then
6109 Set_Ekind (Derived_Type, E_Array_Type);
6111 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6112 Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
6114 Set_Has_Delayed_Freeze (Derived_Type, True);
6116 else
6117 Make_Implicit_Base;
6118 Set_Etype (Derived_Type, Implicit_Base);
6120 New_Indic :=
6121 Make_Subtype_Declaration (Loc,
6122 Defining_Identifier => Derived_Type,
6123 Subtype_Indication =>
6124 Make_Subtype_Indication (Loc,
6125 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
6126 Constraint => Constraint (Indic)));
6128 Rewrite (N, New_Indic);
6129 Analyze (N);
6130 end if;
6132 else
6133 if Nkind (Indic) /= N_Subtype_Indication then
6134 Make_Implicit_Base;
6136 Set_Ekind (Derived_Type, Ekind (Parent_Type));
6137 Set_Etype (Derived_Type, Implicit_Base);
6138 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6140 else
6141 Error_Msg_N ("illegal constraint on constrained type", Indic);
6142 end if;
6143 end if;
6145 -- If parent type is not a derived type itself, and is declared in
6146 -- closed scope (e.g. a subprogram), then we must explicitly introduce
6147 -- the new type's concatenation operator since Derive_Subprograms
6148 -- will not inherit the parent's operator. If the parent type is
6149 -- unconstrained, the operator is of the unconstrained base type.
6151 if Number_Dimensions (Parent_Type) = 1
6152 and then not Is_Limited_Type (Parent_Type)
6153 and then not Is_Derived_Type (Parent_Type)
6154 and then not Is_Package_Or_Generic_Package
6155 (Scope (Base_Type (Parent_Type)))
6156 then
6157 if not Is_Constrained (Parent_Type)
6158 and then Is_Constrained (Derived_Type)
6159 then
6160 New_Concatenation_Op (Implicit_Base);
6161 else
6162 New_Concatenation_Op (Derived_Type);
6163 end if;
6164 end if;
6165 end Build_Derived_Array_Type;
6167 -----------------------------------
6168 -- Build_Derived_Concurrent_Type --
6169 -----------------------------------
6171 procedure Build_Derived_Concurrent_Type
6172 (N : Node_Id;
6173 Parent_Type : Entity_Id;
6174 Derived_Type : Entity_Id)
6176 Loc : constant Source_Ptr := Sloc (N);
6178 Corr_Record : constant Entity_Id := Make_Temporary (Loc, 'C');
6179 Corr_Decl : Node_Id;
6180 Corr_Decl_Needed : Boolean;
6181 -- If the derived type has fewer discriminants than its parent, the
6182 -- corresponding record is also a derived type, in order to account for
6183 -- the bound discriminants. We create a full type declaration for it in
6184 -- this case.
6186 Constraint_Present : constant Boolean :=
6187 Nkind (Subtype_Indication (Type_Definition (N))) =
6188 N_Subtype_Indication;
6190 D_Constraint : Node_Id;
6191 New_Constraint : Elist_Id;
6192 Old_Disc : Entity_Id;
6193 New_Disc : Entity_Id;
6194 New_N : Node_Id;
6196 begin
6197 Set_Stored_Constraint (Derived_Type, No_Elist);
6198 Corr_Decl_Needed := False;
6199 Old_Disc := Empty;
6201 if Present (Discriminant_Specifications (N))
6202 and then Constraint_Present
6203 then
6204 Old_Disc := First_Discriminant (Parent_Type);
6205 New_Disc := First (Discriminant_Specifications (N));
6206 while Present (New_Disc) and then Present (Old_Disc) loop
6207 Next_Discriminant (Old_Disc);
6208 Next (New_Disc);
6209 end loop;
6210 end if;
6212 if Present (Old_Disc) and then Expander_Active then
6214 -- The new type has fewer discriminants, so we need to create a new
6215 -- corresponding record, which is derived from the corresponding
6216 -- record of the parent, and has a stored constraint that captures
6217 -- the values of the discriminant constraints. The corresponding
6218 -- record is needed only if expander is active and code generation is
6219 -- enabled.
6221 -- The type declaration for the derived corresponding record has the
6222 -- same discriminant part and constraints as the current declaration.
6223 -- Copy the unanalyzed tree to build declaration.
6225 Corr_Decl_Needed := True;
6226 New_N := Copy_Separate_Tree (N);
6228 Corr_Decl :=
6229 Make_Full_Type_Declaration (Loc,
6230 Defining_Identifier => Corr_Record,
6231 Discriminant_Specifications =>
6232 Discriminant_Specifications (New_N),
6233 Type_Definition =>
6234 Make_Derived_Type_Definition (Loc,
6235 Subtype_Indication =>
6236 Make_Subtype_Indication (Loc,
6237 Subtype_Mark =>
6238 New_Occurrence_Of
6239 (Corresponding_Record_Type (Parent_Type), Loc),
6240 Constraint =>
6241 Constraint
6242 (Subtype_Indication (Type_Definition (New_N))))));
6243 end if;
6245 -- Copy Storage_Size and Relative_Deadline variables if task case
6247 if Is_Task_Type (Parent_Type) then
6248 Set_Storage_Size_Variable (Derived_Type,
6249 Storage_Size_Variable (Parent_Type));
6250 Set_Relative_Deadline_Variable (Derived_Type,
6251 Relative_Deadline_Variable (Parent_Type));
6252 end if;
6254 if Present (Discriminant_Specifications (N)) then
6255 Push_Scope (Derived_Type);
6256 Check_Or_Process_Discriminants (N, Derived_Type);
6258 if Constraint_Present then
6259 New_Constraint :=
6260 Expand_To_Stored_Constraint
6261 (Parent_Type,
6262 Build_Discriminant_Constraints
6263 (Parent_Type,
6264 Subtype_Indication (Type_Definition (N)), True));
6265 end if;
6267 End_Scope;
6269 elsif Constraint_Present then
6271 -- Build constrained subtype, copying the constraint, and derive
6272 -- from it to create a derived constrained type.
6274 declare
6275 Loc : constant Source_Ptr := Sloc (N);
6276 Anon : constant Entity_Id :=
6277 Make_Defining_Identifier (Loc,
6278 Chars => New_External_Name (Chars (Derived_Type), 'T'));
6279 Decl : Node_Id;
6281 begin
6282 Decl :=
6283 Make_Subtype_Declaration (Loc,
6284 Defining_Identifier => Anon,
6285 Subtype_Indication =>
6286 New_Copy_Tree (Subtype_Indication (Type_Definition (N))));
6287 Insert_Before (N, Decl);
6288 Analyze (Decl);
6290 Rewrite (Subtype_Indication (Type_Definition (N)),
6291 New_Occurrence_Of (Anon, Loc));
6292 Set_Analyzed (Derived_Type, False);
6293 Analyze (N);
6294 return;
6295 end;
6296 end if;
6298 -- By default, operations and private data are inherited from parent.
6299 -- However, in the presence of bound discriminants, a new corresponding
6300 -- record will be created, see below.
6302 Set_Has_Discriminants
6303 (Derived_Type, Has_Discriminants (Parent_Type));
6304 Set_Corresponding_Record_Type
6305 (Derived_Type, Corresponding_Record_Type (Parent_Type));
6307 -- Is_Constrained is set according the parent subtype, but is set to
6308 -- False if the derived type is declared with new discriminants.
6310 Set_Is_Constrained
6311 (Derived_Type,
6312 (Is_Constrained (Parent_Type) or else Constraint_Present)
6313 and then not Present (Discriminant_Specifications (N)));
6315 if Constraint_Present then
6316 if not Has_Discriminants (Parent_Type) then
6317 Error_Msg_N ("untagged parent must have discriminants", N);
6319 elsif Present (Discriminant_Specifications (N)) then
6321 -- Verify that new discriminants are used to constrain old ones
6323 D_Constraint :=
6324 First
6325 (Constraints
6326 (Constraint (Subtype_Indication (Type_Definition (N)))));
6328 Old_Disc := First_Discriminant (Parent_Type);
6330 while Present (D_Constraint) loop
6331 if Nkind (D_Constraint) /= N_Discriminant_Association then
6333 -- Positional constraint. If it is a reference to a new
6334 -- discriminant, it constrains the corresponding old one.
6336 if Nkind (D_Constraint) = N_Identifier then
6337 New_Disc := First_Discriminant (Derived_Type);
6338 while Present (New_Disc) loop
6339 exit when Chars (New_Disc) = Chars (D_Constraint);
6340 Next_Discriminant (New_Disc);
6341 end loop;
6343 if Present (New_Disc) then
6344 Set_Corresponding_Discriminant (New_Disc, Old_Disc);
6345 end if;
6346 end if;
6348 Next_Discriminant (Old_Disc);
6350 -- if this is a named constraint, search by name for the old
6351 -- discriminants constrained by the new one.
6353 elsif Nkind (Expression (D_Constraint)) = N_Identifier then
6355 -- Find new discriminant with that name
6357 New_Disc := First_Discriminant (Derived_Type);
6358 while Present (New_Disc) loop
6359 exit when
6360 Chars (New_Disc) = Chars (Expression (D_Constraint));
6361 Next_Discriminant (New_Disc);
6362 end loop;
6364 if Present (New_Disc) then
6366 -- Verify that new discriminant renames some discriminant
6367 -- of the parent type, and associate the new discriminant
6368 -- with one or more old ones that it renames.
6370 declare
6371 Selector : Node_Id;
6373 begin
6374 Selector := First (Selector_Names (D_Constraint));
6375 while Present (Selector) loop
6376 Old_Disc := First_Discriminant (Parent_Type);
6377 while Present (Old_Disc) loop
6378 exit when Chars (Old_Disc) = Chars (Selector);
6379 Next_Discriminant (Old_Disc);
6380 end loop;
6382 if Present (Old_Disc) then
6383 Set_Corresponding_Discriminant
6384 (New_Disc, Old_Disc);
6385 end if;
6387 Next (Selector);
6388 end loop;
6389 end;
6390 end if;
6391 end if;
6393 Next (D_Constraint);
6394 end loop;
6396 New_Disc := First_Discriminant (Derived_Type);
6397 while Present (New_Disc) loop
6398 if No (Corresponding_Discriminant (New_Disc)) then
6399 Error_Msg_NE
6400 ("new discriminant& must constrain old one", N, New_Disc);
6402 elsif not
6403 Subtypes_Statically_Compatible
6404 (Etype (New_Disc),
6405 Etype (Corresponding_Discriminant (New_Disc)))
6406 then
6407 Error_Msg_NE
6408 ("& not statically compatible with parent discriminant",
6409 N, New_Disc);
6410 end if;
6412 Next_Discriminant (New_Disc);
6413 end loop;
6414 end if;
6416 elsif Present (Discriminant_Specifications (N)) then
6417 Error_Msg_N
6418 ("missing discriminant constraint in untagged derivation", N);
6419 end if;
6421 -- The entity chain of the derived type includes the new discriminants
6422 -- but shares operations with the parent.
6424 if Present (Discriminant_Specifications (N)) then
6425 Old_Disc := First_Discriminant (Parent_Type);
6426 while Present (Old_Disc) loop
6427 if No (Next_Entity (Old_Disc))
6428 or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
6429 then
6430 Set_Next_Entity
6431 (Last_Entity (Derived_Type), Next_Entity (Old_Disc));
6432 exit;
6433 end if;
6435 Next_Discriminant (Old_Disc);
6436 end loop;
6438 else
6439 Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
6440 if Has_Discriminants (Parent_Type) then
6441 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
6442 Set_Discriminant_Constraint (
6443 Derived_Type, Discriminant_Constraint (Parent_Type));
6444 end if;
6445 end if;
6447 Set_Last_Entity (Derived_Type, Last_Entity (Parent_Type));
6449 Set_Has_Completion (Derived_Type);
6451 if Corr_Decl_Needed then
6452 Set_Stored_Constraint (Derived_Type, New_Constraint);
6453 Insert_After (N, Corr_Decl);
6454 Analyze (Corr_Decl);
6455 Set_Corresponding_Record_Type (Derived_Type, Corr_Record);
6456 end if;
6457 end Build_Derived_Concurrent_Type;
6459 ------------------------------------
6460 -- Build_Derived_Enumeration_Type --
6461 ------------------------------------
6463 procedure Build_Derived_Enumeration_Type
6464 (N : Node_Id;
6465 Parent_Type : Entity_Id;
6466 Derived_Type : Entity_Id)
6468 Loc : constant Source_Ptr := Sloc (N);
6469 Def : constant Node_Id := Type_Definition (N);
6470 Indic : constant Node_Id := Subtype_Indication (Def);
6471 Implicit_Base : Entity_Id;
6472 Literal : Entity_Id;
6473 New_Lit : Entity_Id;
6474 Literals_List : List_Id;
6475 Type_Decl : Node_Id;
6476 Hi, Lo : Node_Id;
6477 Rang_Expr : Node_Id;
6479 begin
6480 -- Since types Standard.Character and Standard.[Wide_]Wide_Character do
6481 -- not have explicit literals lists we need to process types derived
6482 -- from them specially. This is handled by Derived_Standard_Character.
6483 -- If the parent type is a generic type, there are no literals either,
6484 -- and we construct the same skeletal representation as for the generic
6485 -- parent type.
6487 if Is_Standard_Character_Type (Parent_Type) then
6488 Derived_Standard_Character (N, Parent_Type, Derived_Type);
6490 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
6491 declare
6492 Lo : Node_Id;
6493 Hi : Node_Id;
6495 begin
6496 if Nkind (Indic) /= N_Subtype_Indication then
6497 Lo :=
6498 Make_Attribute_Reference (Loc,
6499 Attribute_Name => Name_First,
6500 Prefix => New_Occurrence_Of (Derived_Type, Loc));
6501 Set_Etype (Lo, Derived_Type);
6503 Hi :=
6504 Make_Attribute_Reference (Loc,
6505 Attribute_Name => Name_Last,
6506 Prefix => New_Occurrence_Of (Derived_Type, Loc));
6507 Set_Etype (Hi, Derived_Type);
6509 Set_Scalar_Range (Derived_Type,
6510 Make_Range (Loc,
6511 Low_Bound => Lo,
6512 High_Bound => Hi));
6513 else
6515 -- Analyze subtype indication and verify compatibility
6516 -- with parent type.
6518 if Base_Type (Process_Subtype (Indic, N)) /=
6519 Base_Type (Parent_Type)
6520 then
6521 Error_Msg_N
6522 ("illegal constraint for formal discrete type", N);
6523 end if;
6524 end if;
6525 end;
6527 else
6528 -- If a constraint is present, analyze the bounds to catch
6529 -- premature usage of the derived literals.
6531 if Nkind (Indic) = N_Subtype_Indication
6532 and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
6533 then
6534 Analyze (Low_Bound (Range_Expression (Constraint (Indic))));
6535 Analyze (High_Bound (Range_Expression (Constraint (Indic))));
6536 end if;
6538 -- Introduce an implicit base type for the derived type even if there
6539 -- is no constraint attached to it, since this seems closer to the
6540 -- Ada semantics. Build a full type declaration tree for the derived
6541 -- type using the implicit base type as the defining identifier. The
6542 -- build a subtype declaration tree which applies the constraint (if
6543 -- any) have it replace the derived type declaration.
6545 Literal := First_Literal (Parent_Type);
6546 Literals_List := New_List;
6547 while Present (Literal)
6548 and then Ekind (Literal) = E_Enumeration_Literal
6549 loop
6550 -- Literals of the derived type have the same representation as
6551 -- those of the parent type, but this representation can be
6552 -- overridden by an explicit representation clause. Indicate
6553 -- that there is no explicit representation given yet. These
6554 -- derived literals are implicit operations of the new type,
6555 -- and can be overridden by explicit ones.
6557 if Nkind (Literal) = N_Defining_Character_Literal then
6558 New_Lit :=
6559 Make_Defining_Character_Literal (Loc, Chars (Literal));
6560 else
6561 New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
6562 end if;
6564 Set_Ekind (New_Lit, E_Enumeration_Literal);
6565 Set_Enumeration_Pos (New_Lit, Enumeration_Pos (Literal));
6566 Set_Enumeration_Rep (New_Lit, Enumeration_Rep (Literal));
6567 Set_Enumeration_Rep_Expr (New_Lit, Empty);
6568 Set_Alias (New_Lit, Literal);
6569 Set_Is_Known_Valid (New_Lit, True);
6571 Append (New_Lit, Literals_List);
6572 Next_Literal (Literal);
6573 end loop;
6575 Implicit_Base :=
6576 Make_Defining_Identifier (Sloc (Derived_Type),
6577 Chars => New_External_Name (Chars (Derived_Type), 'B'));
6579 -- Indicate the proper nature of the derived type. This must be done
6580 -- before analysis of the literals, to recognize cases when a literal
6581 -- may be hidden by a previous explicit function definition (cf.
6582 -- c83031a).
6584 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
6585 Set_Etype (Derived_Type, Implicit_Base);
6587 Type_Decl :=
6588 Make_Full_Type_Declaration (Loc,
6589 Defining_Identifier => Implicit_Base,
6590 Discriminant_Specifications => No_List,
6591 Type_Definition =>
6592 Make_Enumeration_Type_Definition (Loc, Literals_List));
6594 Mark_Rewrite_Insertion (Type_Decl);
6595 Insert_Before (N, Type_Decl);
6596 Analyze (Type_Decl);
6598 -- The anonymous base now has a full declaration, but this base
6599 -- is not a first subtype.
6601 Set_Is_First_Subtype (Implicit_Base, False);
6603 -- After the implicit base is analyzed its Etype needs to be changed
6604 -- to reflect the fact that it is derived from the parent type which
6605 -- was ignored during analysis. We also set the size at this point.
6607 Set_Etype (Implicit_Base, Parent_Type);
6609 Set_Size_Info (Implicit_Base, Parent_Type);
6610 Set_RM_Size (Implicit_Base, RM_Size (Parent_Type));
6611 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
6613 -- Copy other flags from parent type
6615 Set_Has_Non_Standard_Rep
6616 (Implicit_Base, Has_Non_Standard_Rep
6617 (Parent_Type));
6618 Set_Has_Pragma_Ordered
6619 (Implicit_Base, Has_Pragma_Ordered
6620 (Parent_Type));
6621 Set_Has_Delayed_Freeze (Implicit_Base);
6623 -- Process the subtype indication including a validation check on the
6624 -- constraint, if any. If a constraint is given, its bounds must be
6625 -- implicitly converted to the new type.
6627 if Nkind (Indic) = N_Subtype_Indication then
6628 declare
6629 R : constant Node_Id :=
6630 Range_Expression (Constraint (Indic));
6632 begin
6633 if Nkind (R) = N_Range then
6634 Hi := Build_Scalar_Bound
6635 (High_Bound (R), Parent_Type, Implicit_Base);
6636 Lo := Build_Scalar_Bound
6637 (Low_Bound (R), Parent_Type, Implicit_Base);
6639 else
6640 -- Constraint is a Range attribute. Replace with explicit
6641 -- mention of the bounds of the prefix, which must be a
6642 -- subtype.
6644 Analyze (Prefix (R));
6645 Hi :=
6646 Convert_To (Implicit_Base,
6647 Make_Attribute_Reference (Loc,
6648 Attribute_Name => Name_Last,
6649 Prefix =>
6650 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
6652 Lo :=
6653 Convert_To (Implicit_Base,
6654 Make_Attribute_Reference (Loc,
6655 Attribute_Name => Name_First,
6656 Prefix =>
6657 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
6658 end if;
6659 end;
6661 else
6662 Hi :=
6663 Build_Scalar_Bound
6664 (Type_High_Bound (Parent_Type),
6665 Parent_Type, Implicit_Base);
6666 Lo :=
6667 Build_Scalar_Bound
6668 (Type_Low_Bound (Parent_Type),
6669 Parent_Type, Implicit_Base);
6670 end if;
6672 Rang_Expr :=
6673 Make_Range (Loc,
6674 Low_Bound => Lo,
6675 High_Bound => Hi);
6677 -- If we constructed a default range for the case where no range
6678 -- was given, then the expressions in the range must not freeze
6679 -- since they do not correspond to expressions in the source.
6681 if Nkind (Indic) /= N_Subtype_Indication then
6682 Set_Must_Not_Freeze (Lo);
6683 Set_Must_Not_Freeze (Hi);
6684 Set_Must_Not_Freeze (Rang_Expr);
6685 end if;
6687 Rewrite (N,
6688 Make_Subtype_Declaration (Loc,
6689 Defining_Identifier => Derived_Type,
6690 Subtype_Indication =>
6691 Make_Subtype_Indication (Loc,
6692 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
6693 Constraint =>
6694 Make_Range_Constraint (Loc,
6695 Range_Expression => Rang_Expr))));
6697 Analyze (N);
6699 -- Propagate the aspects from the original type declaration to the
6700 -- declaration of the implicit base.
6702 Move_Aspects (From => Original_Node (N), To => Type_Decl);
6704 -- Apply a range check. Since this range expression doesn't have an
6705 -- Etype, we have to specifically pass the Source_Typ parameter. Is
6706 -- this right???
6708 if Nkind (Indic) = N_Subtype_Indication then
6709 Apply_Range_Check
6710 (Range_Expression (Constraint (Indic)), Parent_Type,
6711 Source_Typ => Entity (Subtype_Mark (Indic)));
6712 end if;
6713 end if;
6714 end Build_Derived_Enumeration_Type;
6716 --------------------------------
6717 -- Build_Derived_Numeric_Type --
6718 --------------------------------
6720 procedure Build_Derived_Numeric_Type
6721 (N : Node_Id;
6722 Parent_Type : Entity_Id;
6723 Derived_Type : Entity_Id)
6725 Loc : constant Source_Ptr := Sloc (N);
6726 Tdef : constant Node_Id := Type_Definition (N);
6727 Indic : constant Node_Id := Subtype_Indication (Tdef);
6728 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
6729 No_Constraint : constant Boolean := Nkind (Indic) /=
6730 N_Subtype_Indication;
6731 Implicit_Base : Entity_Id;
6733 Lo : Node_Id;
6734 Hi : Node_Id;
6736 begin
6737 -- Process the subtype indication including a validation check on
6738 -- the constraint if any.
6740 Discard_Node (Process_Subtype (Indic, N));
6742 -- Introduce an implicit base type for the derived type even if there
6743 -- is no constraint attached to it, since this seems closer to the Ada
6744 -- semantics.
6746 Implicit_Base :=
6747 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
6749 Set_Etype (Implicit_Base, Parent_Base);
6750 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
6751 Set_Size_Info (Implicit_Base, Parent_Base);
6752 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
6753 Set_Parent (Implicit_Base, Parent (Derived_Type));
6754 Set_Is_Known_Valid (Implicit_Base, Is_Known_Valid (Parent_Base));
6756 -- Set RM Size for discrete type or decimal fixed-point type
6757 -- Ordinary fixed-point is excluded, why???
6759 if Is_Discrete_Type (Parent_Base)
6760 or else Is_Decimal_Fixed_Point_Type (Parent_Base)
6761 then
6762 Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
6763 end if;
6765 Set_Has_Delayed_Freeze (Implicit_Base);
6767 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
6768 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
6770 Set_Scalar_Range (Implicit_Base,
6771 Make_Range (Loc,
6772 Low_Bound => Lo,
6773 High_Bound => Hi));
6775 if Has_Infinities (Parent_Base) then
6776 Set_Includes_Infinities (Scalar_Range (Implicit_Base));
6777 end if;
6779 -- The Derived_Type, which is the entity of the declaration, is a
6780 -- subtype of the implicit base. Its Ekind is a subtype, even in the
6781 -- absence of an explicit constraint.
6783 Set_Etype (Derived_Type, Implicit_Base);
6785 -- If we did not have a constraint, then the Ekind is set from the
6786 -- parent type (otherwise Process_Subtype has set the bounds)
6788 if No_Constraint then
6789 Set_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
6790 end if;
6792 -- If we did not have a range constraint, then set the range from the
6793 -- parent type. Otherwise, the Process_Subtype call has set the bounds.
6795 if No_Constraint or else not Has_Range_Constraint (Indic) then
6796 Set_Scalar_Range (Derived_Type,
6797 Make_Range (Loc,
6798 Low_Bound => New_Copy_Tree (Type_Low_Bound (Parent_Type)),
6799 High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
6800 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
6802 if Has_Infinities (Parent_Type) then
6803 Set_Includes_Infinities (Scalar_Range (Derived_Type));
6804 end if;
6806 Set_Is_Known_Valid (Derived_Type, Is_Known_Valid (Parent_Type));
6807 end if;
6809 Set_Is_Descendent_Of_Address (Derived_Type,
6810 Is_Descendent_Of_Address (Parent_Type));
6811 Set_Is_Descendent_Of_Address (Implicit_Base,
6812 Is_Descendent_Of_Address (Parent_Type));
6814 -- Set remaining type-specific fields, depending on numeric type
6816 if Is_Modular_Integer_Type (Parent_Type) then
6817 Set_Modulus (Implicit_Base, Modulus (Parent_Base));
6819 Set_Non_Binary_Modulus
6820 (Implicit_Base, Non_Binary_Modulus (Parent_Base));
6822 Set_Is_Known_Valid
6823 (Implicit_Base, Is_Known_Valid (Parent_Base));
6825 elsif Is_Floating_Point_Type (Parent_Type) then
6827 -- Digits of base type is always copied from the digits value of
6828 -- the parent base type, but the digits of the derived type will
6829 -- already have been set if there was a constraint present.
6831 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
6832 Set_Float_Rep (Implicit_Base, Float_Rep (Parent_Base));
6834 if No_Constraint then
6835 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
6836 end if;
6838 elsif Is_Fixed_Point_Type (Parent_Type) then
6840 -- Small of base type and derived type are always copied from the
6841 -- parent base type, since smalls never change. The delta of the
6842 -- base type is also copied from the parent base type. However the
6843 -- delta of the derived type will have been set already if a
6844 -- constraint was present.
6846 Set_Small_Value (Derived_Type, Small_Value (Parent_Base));
6847 Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
6848 Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
6850 if No_Constraint then
6851 Set_Delta_Value (Derived_Type, Delta_Value (Parent_Type));
6852 end if;
6854 -- The scale and machine radix in the decimal case are always
6855 -- copied from the parent base type.
6857 if Is_Decimal_Fixed_Point_Type (Parent_Type) then
6858 Set_Scale_Value (Derived_Type, Scale_Value (Parent_Base));
6859 Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
6861 Set_Machine_Radix_10
6862 (Derived_Type, Machine_Radix_10 (Parent_Base));
6863 Set_Machine_Radix_10
6864 (Implicit_Base, Machine_Radix_10 (Parent_Base));
6866 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
6868 if No_Constraint then
6869 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
6871 else
6872 -- the analysis of the subtype_indication sets the
6873 -- digits value of the derived type.
6875 null;
6876 end if;
6877 end if;
6878 end if;
6880 if Is_Integer_Type (Parent_Type) then
6881 Set_Has_Shift_Operator
6882 (Implicit_Base, Has_Shift_Operator (Parent_Type));
6883 end if;
6885 -- The type of the bounds is that of the parent type, and they
6886 -- must be converted to the derived type.
6888 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
6890 -- The implicit_base should be frozen when the derived type is frozen,
6891 -- but note that it is used in the conversions of the bounds. For fixed
6892 -- types we delay the determination of the bounds until the proper
6893 -- freezing point. For other numeric types this is rejected by GCC, for
6894 -- reasons that are currently unclear (???), so we choose to freeze the
6895 -- implicit base now. In the case of integers and floating point types
6896 -- this is harmless because subsequent representation clauses cannot
6897 -- affect anything, but it is still baffling that we cannot use the
6898 -- same mechanism for all derived numeric types.
6900 -- There is a further complication: actually some representation
6901 -- clauses can affect the implicit base type. For example, attribute
6902 -- definition clauses for stream-oriented attributes need to set the
6903 -- corresponding TSS entries on the base type, and this normally
6904 -- cannot be done after the base type is frozen, so the circuitry in
6905 -- Sem_Ch13.New_Stream_Subprogram must account for this possibility
6906 -- and not use Set_TSS in this case.
6908 -- There are also consequences for the case of delayed representation
6909 -- aspects for some cases. For example, a Size aspect is delayed and
6910 -- should not be evaluated to the freeze point. This early freezing
6911 -- means that the size attribute evaluation happens too early???
6913 if Is_Fixed_Point_Type (Parent_Type) then
6914 Conditional_Delay (Implicit_Base, Parent_Type);
6915 else
6916 Freeze_Before (N, Implicit_Base);
6917 end if;
6918 end Build_Derived_Numeric_Type;
6920 --------------------------------
6921 -- Build_Derived_Private_Type --
6922 --------------------------------
6924 procedure Build_Derived_Private_Type
6925 (N : Node_Id;
6926 Parent_Type : Entity_Id;
6927 Derived_Type : Entity_Id;
6928 Is_Completion : Boolean;
6929 Derive_Subps : Boolean := True)
6931 Loc : constant Source_Ptr := Sloc (N);
6932 Par_Base : constant Entity_Id := Base_Type (Parent_Type);
6933 Par_Scope : constant Entity_Id := Scope (Par_Base);
6934 Full_N : constant Node_Id := New_Copy_Tree (N);
6935 Full_Der : Entity_Id := New_Copy (Derived_Type);
6936 Full_P : Entity_Id;
6938 procedure Build_Full_Derivation;
6939 -- Build full derivation, i.e. derive from the full view
6941 procedure Copy_And_Build;
6942 -- Copy derived type declaration, replace parent with its full view,
6943 -- and build derivation
6945 ---------------------------
6946 -- Build_Full_Derivation --
6947 ---------------------------
6949 procedure Build_Full_Derivation is
6950 begin
6951 -- If parent scope is not open, install the declarations
6953 if not In_Open_Scopes (Par_Scope) then
6954 Install_Private_Declarations (Par_Scope);
6955 Install_Visible_Declarations (Par_Scope);
6956 Copy_And_Build;
6957 Uninstall_Declarations (Par_Scope);
6959 -- If parent scope is open and in another unit, and parent has a
6960 -- completion, then the derivation is taking place in the visible
6961 -- part of a child unit. In that case retrieve the full view of
6962 -- the parent momentarily.
6964 elsif not In_Same_Source_Unit (N, Parent_Type) then
6965 Full_P := Full_View (Parent_Type);
6966 Exchange_Declarations (Parent_Type);
6967 Copy_And_Build;
6968 Exchange_Declarations (Full_P);
6970 -- Otherwise it is a local derivation
6972 else
6973 Copy_And_Build;
6974 end if;
6975 end Build_Full_Derivation;
6977 --------------------
6978 -- Copy_And_Build --
6979 --------------------
6981 procedure Copy_And_Build is
6982 Full_Parent : Entity_Id := Parent_Type;
6984 begin
6985 -- If the parent is itself derived from another private type,
6986 -- installing the private declarations has not affected its
6987 -- privacy status, so use its own full view explicitly.
6989 if Is_Private_Type (Full_Parent)
6990 and then Present (Full_View (Full_Parent))
6991 then
6992 Full_Parent := Full_View (Full_Parent);
6993 end if;
6995 -- And its underlying full view if necessary
6997 if Is_Private_Type (Full_Parent)
6998 and then Present (Underlying_Full_View (Full_Parent))
6999 then
7000 Full_Parent := Underlying_Full_View (Full_Parent);
7001 end if;
7003 -- For record, access and most enumeration types, derivation from
7004 -- the full view requires a fully-fledged declaration. In the other
7005 -- cases, just use an itype.
7007 if Ekind (Full_Parent) in Record_Kind
7008 or else Ekind (Full_Parent) in Access_Kind
7009 or else
7010 (Ekind (Full_Parent) in Enumeration_Kind
7011 and then not Is_Standard_Character_Type (Full_Parent)
7012 and then not Is_Generic_Type (Root_Type (Full_Parent)))
7013 then
7014 -- Copy and adjust declaration to provide a completion for what
7015 -- is originally a private declaration. Indicate that full view
7016 -- is internally generated.
7018 Set_Comes_From_Source (Full_N, False);
7019 Set_Comes_From_Source (Full_Der, False);
7020 Set_Parent (Full_Der, Full_N);
7021 Set_Defining_Identifier (Full_N, Full_Der);
7023 -- If there are no constraints, adjust the subtype mark
7025 if Nkind (Subtype_Indication (Type_Definition (Full_N))) /=
7026 N_Subtype_Indication
7027 then
7028 Set_Subtype_Indication
7029 (Type_Definition (Full_N),
7030 New_Occurrence_Of (Full_Parent, Sloc (Full_N)));
7031 end if;
7033 Insert_After (N, Full_N);
7035 -- Build full view of derived type from full view of parent which
7036 -- is now installed. Subprograms have been derived on the partial
7037 -- view, the completion does not derive them anew.
7039 if Ekind (Full_Parent) in Record_Kind then
7041 -- If parent type is tagged, the completion inherits the proper
7042 -- primitive operations.
7044 if Is_Tagged_Type (Parent_Type) then
7045 Build_Derived_Record_Type
7046 (Full_N, Full_Parent, Full_Der, Derive_Subps);
7047 else
7048 Build_Derived_Record_Type
7049 (Full_N, Full_Parent, Full_Der, Derive_Subps => False);
7050 end if;
7052 else
7053 Build_Derived_Type
7054 (Full_N, Full_Parent, Full_Der,
7055 Is_Completion => False, Derive_Subps => False);
7056 end if;
7058 -- The full declaration has been introduced into the tree and
7059 -- processed in the step above. It should not be analyzed again
7060 -- (when encountered later in the current list of declarations)
7061 -- to prevent spurious name conflicts. The full entity remains
7062 -- invisible.
7064 Set_Analyzed (Full_N);
7066 else
7067 Full_Der :=
7068 Make_Defining_Identifier (Sloc (Derived_Type),
7069 Chars => Chars (Derived_Type));
7070 Set_Is_Itype (Full_Der);
7071 Set_Associated_Node_For_Itype (Full_Der, N);
7072 Set_Parent (Full_Der, N);
7073 Build_Derived_Type
7074 (N, Full_Parent, Full_Der,
7075 Is_Completion => False, Derive_Subps => False);
7076 end if;
7078 Set_Has_Private_Declaration (Full_Der);
7079 Set_Has_Private_Declaration (Derived_Type);
7081 Set_Scope (Full_Der, Scope (Derived_Type));
7082 Set_Is_First_Subtype (Full_Der, Is_First_Subtype (Derived_Type));
7083 Set_Has_Size_Clause (Full_Der, False);
7084 Set_Has_Alignment_Clause (Full_Der, False);
7085 Set_Has_Delayed_Freeze (Full_Der);
7086 Set_Is_Frozen (Full_Der, False);
7087 Set_Freeze_Node (Full_Der, Empty);
7088 Set_Depends_On_Private (Full_Der, Has_Private_Component (Full_Der));
7089 Set_Is_Public (Full_Der, Is_Public (Derived_Type));
7091 -- The convention on the base type may be set in the private part
7092 -- and not propagated to the subtype until later, so we obtain the
7093 -- convention from the base type of the parent.
7095 Set_Convention (Full_Der, Convention (Base_Type (Full_Parent)));
7096 end Copy_And_Build;
7098 -- Start of processing for Build_Derived_Private_Type
7100 begin
7101 if Is_Tagged_Type (Parent_Type) then
7102 Full_P := Full_View (Parent_Type);
7104 -- A type extension of a type with unknown discriminants is an
7105 -- indefinite type that the back-end cannot handle directly.
7106 -- We treat it as a private type, and build a completion that is
7107 -- derived from the full view of the parent, and hopefully has
7108 -- known discriminants.
7110 -- If the full view of the parent type has an underlying record view,
7111 -- use it to generate the underlying record view of this derived type
7112 -- (required for chains of derivations with unknown discriminants).
7114 -- Minor optimization: we avoid the generation of useless underlying
7115 -- record view entities if the private type declaration has unknown
7116 -- discriminants but its corresponding full view has no
7117 -- discriminants.
7119 if Has_Unknown_Discriminants (Parent_Type)
7120 and then Present (Full_P)
7121 and then (Has_Discriminants (Full_P)
7122 or else Present (Underlying_Record_View (Full_P)))
7123 and then not In_Open_Scopes (Par_Scope)
7124 and then Expander_Active
7125 then
7126 declare
7127 Full_Der : constant Entity_Id := Make_Temporary (Loc, 'T');
7128 New_Ext : constant Node_Id :=
7129 Copy_Separate_Tree
7130 (Record_Extension_Part (Type_Definition (N)));
7131 Decl : Node_Id;
7133 begin
7134 Build_Derived_Record_Type
7135 (N, Parent_Type, Derived_Type, Derive_Subps);
7137 -- Build anonymous completion, as a derivation from the full
7138 -- view of the parent. This is not a completion in the usual
7139 -- sense, because the current type is not private.
7141 Decl :=
7142 Make_Full_Type_Declaration (Loc,
7143 Defining_Identifier => Full_Der,
7144 Type_Definition =>
7145 Make_Derived_Type_Definition (Loc,
7146 Subtype_Indication =>
7147 New_Copy_Tree
7148 (Subtype_Indication (Type_Definition (N))),
7149 Record_Extension_Part => New_Ext));
7151 -- If the parent type has an underlying record view, use it
7152 -- here to build the new underlying record view.
7154 if Present (Underlying_Record_View (Full_P)) then
7155 pragma Assert
7156 (Nkind (Subtype_Indication (Type_Definition (Decl)))
7157 = N_Identifier);
7158 Set_Entity (Subtype_Indication (Type_Definition (Decl)),
7159 Underlying_Record_View (Full_P));
7160 end if;
7162 Install_Private_Declarations (Par_Scope);
7163 Install_Visible_Declarations (Par_Scope);
7164 Insert_Before (N, Decl);
7166 -- Mark entity as an underlying record view before analysis,
7167 -- to avoid generating the list of its primitive operations
7168 -- (which is not really required for this entity) and thus
7169 -- prevent spurious errors associated with missing overriding
7170 -- of abstract primitives (overridden only for Derived_Type).
7172 Set_Ekind (Full_Der, E_Record_Type);
7173 Set_Is_Underlying_Record_View (Full_Der);
7174 Set_Default_SSO (Full_Der);
7176 Analyze (Decl);
7178 pragma Assert (Has_Discriminants (Full_Der)
7179 and then not Has_Unknown_Discriminants (Full_Der));
7181 Uninstall_Declarations (Par_Scope);
7183 -- Freeze the underlying record view, to prevent generation of
7184 -- useless dispatching information, which is simply shared with
7185 -- the real derived type.
7187 Set_Is_Frozen (Full_Der);
7189 -- If the derived type has access discriminants, create
7190 -- references to their anonymous types now, to prevent
7191 -- back-end problems when their first use is in generated
7192 -- bodies of primitives.
7194 declare
7195 E : Entity_Id;
7197 begin
7198 E := First_Entity (Full_Der);
7200 while Present (E) loop
7201 if Ekind (E) = E_Discriminant
7202 and then Ekind (Etype (E)) = E_Anonymous_Access_Type
7203 then
7204 Build_Itype_Reference (Etype (E), Decl);
7205 end if;
7207 Next_Entity (E);
7208 end loop;
7209 end;
7211 -- Set up links between real entity and underlying record view
7213 Set_Underlying_Record_View (Derived_Type, Base_Type (Full_Der));
7214 Set_Underlying_Record_View (Base_Type (Full_Der), Derived_Type);
7215 end;
7217 -- If discriminants are known, build derived record
7219 else
7220 Build_Derived_Record_Type
7221 (N, Parent_Type, Derived_Type, Derive_Subps);
7222 end if;
7224 return;
7226 elsif Has_Discriminants (Parent_Type) then
7228 -- Build partial view of derived type from partial view of parent.
7229 -- This must be done before building the full derivation because the
7230 -- second derivation will modify the discriminants of the first and
7231 -- the discriminants are chained with the rest of the components in
7232 -- the full derivation.
7234 Build_Derived_Record_Type
7235 (N, Parent_Type, Derived_Type, Derive_Subps);
7237 -- Build the full derivation if this is not the anonymous derived
7238 -- base type created by Build_Derived_Record_Type in the constrained
7239 -- case (see point 5. of its head comment) since we build it for the
7240 -- derived subtype. And skip it for protected types altogether, as
7241 -- gigi does not use these types directly.
7243 if Present (Full_View (Parent_Type))
7244 and then not Is_Itype (Derived_Type)
7245 and then not (Ekind (Full_View (Parent_Type)) in Protected_Kind)
7246 then
7247 declare
7248 Der_Base : constant Entity_Id := Base_Type (Derived_Type);
7249 Discr : Entity_Id;
7250 Last_Discr : Entity_Id;
7252 begin
7253 -- If this is not a completion, construct the implicit full
7254 -- view by deriving from the full view of the parent type.
7255 -- But if this is a completion, the derived private type
7256 -- being built is a full view and the full derivation can
7257 -- only be its underlying full view.
7259 Build_Full_Derivation;
7261 if not Is_Completion then
7262 Set_Full_View (Derived_Type, Full_Der);
7263 else
7264 Set_Underlying_Full_View (Derived_Type, Full_Der);
7265 end if;
7267 if not Is_Base_Type (Derived_Type) then
7268 Set_Full_View (Der_Base, Base_Type (Full_Der));
7269 end if;
7271 -- Copy the discriminant list from full view to the partial
7272 -- view (base type and its subtype). Gigi requires that the
7273 -- partial and full views have the same discriminants.
7275 -- Note that since the partial view points to discriminants
7276 -- in the full view, their scope will be that of the full
7277 -- view. This might cause some front end problems and need
7278 -- adjustment???
7280 Discr := First_Discriminant (Base_Type (Full_Der));
7281 Set_First_Entity (Der_Base, Discr);
7283 loop
7284 Last_Discr := Discr;
7285 Next_Discriminant (Discr);
7286 exit when No (Discr);
7287 end loop;
7289 Set_Last_Entity (Der_Base, Last_Discr);
7290 Set_First_Entity (Derived_Type, First_Entity (Der_Base));
7291 Set_Last_Entity (Derived_Type, Last_Entity (Der_Base));
7293 Set_Stored_Constraint
7294 (Full_Der, Stored_Constraint (Derived_Type));
7295 end;
7296 end if;
7298 elsif Present (Full_View (Parent_Type))
7299 and then Has_Discriminants (Full_View (Parent_Type))
7300 then
7301 if Has_Unknown_Discriminants (Parent_Type)
7302 and then Nkind (Subtype_Indication (Type_Definition (N))) =
7303 N_Subtype_Indication
7304 then
7305 Error_Msg_N
7306 ("cannot constrain type with unknown discriminants",
7307 Subtype_Indication (Type_Definition (N)));
7308 return;
7309 end if;
7311 -- If this is not a completion, construct the implicit full view by
7312 -- deriving from the full view of the parent type. But if this is a
7313 -- completion, the derived private type being built is a full view
7314 -- and the full derivation can only be its underlying full view.
7316 Build_Full_Derivation;
7318 if not Is_Completion then
7319 Set_Full_View (Derived_Type, Full_Der);
7320 else
7321 Set_Underlying_Full_View (Derived_Type, Full_Der);
7322 end if;
7324 -- In any case, the primitive operations are inherited from the
7325 -- parent type, not from the internal full view.
7327 Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
7329 if Derive_Subps then
7330 Derive_Subprograms (Parent_Type, Derived_Type);
7331 end if;
7333 Set_Stored_Constraint (Derived_Type, No_Elist);
7334 Set_Is_Constrained
7335 (Derived_Type, Is_Constrained (Full_View (Parent_Type)));
7337 else
7338 -- Untagged type, No discriminants on either view
7340 if Nkind (Subtype_Indication (Type_Definition (N))) =
7341 N_Subtype_Indication
7342 then
7343 Error_Msg_N
7344 ("illegal constraint on type without discriminants", N);
7345 end if;
7347 if Present (Discriminant_Specifications (N))
7348 and then Present (Full_View (Parent_Type))
7349 and then not Is_Tagged_Type (Full_View (Parent_Type))
7350 then
7351 Error_Msg_N ("cannot add discriminants to untagged type", N);
7352 end if;
7354 Set_Stored_Constraint (Derived_Type, No_Elist);
7355 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7356 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
7357 Set_Has_Controlled_Component
7358 (Derived_Type, Has_Controlled_Component
7359 (Parent_Type));
7361 -- Direct controlled types do not inherit Finalize_Storage_Only flag
7363 if not Is_Controlled (Parent_Type) then
7364 Set_Finalize_Storage_Only
7365 (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
7366 end if;
7368 -- If this is not a completion, construct the implicit full view by
7369 -- deriving from the full view of the parent type.
7371 -- ??? If the parent is untagged private and its completion is
7372 -- tagged, this mechanism will not work because we cannot derive from
7373 -- the tagged full view unless we have an extension.
7375 if Present (Full_View (Parent_Type))
7376 and then not Is_Tagged_Type (Full_View (Parent_Type))
7377 and then not Is_Completion
7378 then
7379 Build_Full_Derivation;
7380 Set_Full_View (Derived_Type, Full_Der);
7381 end if;
7382 end if;
7384 Set_Has_Unknown_Discriminants (Derived_Type,
7385 Has_Unknown_Discriminants (Parent_Type));
7387 if Is_Private_Type (Derived_Type) then
7388 Set_Private_Dependents (Derived_Type, New_Elmt_List);
7389 end if;
7391 -- If the parent base type is in scope, add the derived type to its
7392 -- list of private dependents, because its full view may become
7393 -- visible subsequently (in a nested private part, a body, or in a
7394 -- further child unit).
7396 if Is_Private_Type (Par_Base) and then In_Open_Scopes (Par_Scope) then
7397 Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
7399 -- Check for unusual case where a type completed by a private
7400 -- derivation occurs within a package nested in a child unit, and
7401 -- the parent is declared in an ancestor.
7403 if Is_Child_Unit (Scope (Current_Scope))
7404 and then Is_Completion
7405 and then In_Private_Part (Current_Scope)
7406 and then Scope (Parent_Type) /= Current_Scope
7408 -- Note that if the parent has a completion in the private part,
7409 -- (which is itself a derivation from some other private type)
7410 -- it is that completion that is visible, there is no full view
7411 -- available, and no special processing is needed.
7413 and then Present (Full_View (Parent_Type))
7414 then
7415 -- In this case, the full view of the parent type will become
7416 -- visible in the body of the enclosing child, and only then will
7417 -- the current type be possibly non-private. Build an underlying
7418 -- full view that will be installed when the enclosing child body
7419 -- is compiled.
7421 if Present (Underlying_Full_View (Derived_Type)) then
7422 Full_Der := Underlying_Full_View (Derived_Type);
7423 else
7424 Build_Full_Derivation;
7425 Set_Underlying_Full_View (Derived_Type, Full_Der);
7426 end if;
7428 -- The full view will be used to swap entities on entry/exit to
7429 -- the body, and must appear in the entity list for the package.
7431 Append_Entity (Full_Der, Scope (Derived_Type));
7432 end if;
7433 end if;
7434 end Build_Derived_Private_Type;
7436 -------------------------------
7437 -- Build_Derived_Record_Type --
7438 -------------------------------
7440 -- 1. INTRODUCTION
7442 -- Ideally we would like to use the same model of type derivation for
7443 -- tagged and untagged record types. Unfortunately this is not quite
7444 -- possible because the semantics of representation clauses is different
7445 -- for tagged and untagged records under inheritance. Consider the
7446 -- following:
7448 -- type R (...) is [tagged] record ... end record;
7449 -- type T (...) is new R (...) [with ...];
7451 -- The representation clauses for T can specify a completely different
7452 -- record layout from R's. Hence the same component can be placed in two
7453 -- very different positions in objects of type T and R. If R and T are
7454 -- tagged types, representation clauses for T can only specify the layout
7455 -- of non inherited components, thus components that are common in R and T
7456 -- have the same position in objects of type R and T.
7458 -- This has two implications. The first is that the entire tree for R's
7459 -- declaration needs to be copied for T in the untagged case, so that T
7460 -- can be viewed as a record type of its own with its own representation
7461 -- clauses. The second implication is the way we handle discriminants.
7462 -- Specifically, in the untagged case we need a way to communicate to Gigi
7463 -- what are the real discriminants in the record, while for the semantics
7464 -- we need to consider those introduced by the user to rename the
7465 -- discriminants in the parent type. This is handled by introducing the
7466 -- notion of stored discriminants. See below for more.
7468 -- Fortunately the way regular components are inherited can be handled in
7469 -- the same way in tagged and untagged types.
7471 -- To complicate things a bit more the private view of a private extension
7472 -- cannot be handled in the same way as the full view (for one thing the
7473 -- semantic rules are somewhat different). We will explain what differs
7474 -- below.
7476 -- 2. DISCRIMINANTS UNDER INHERITANCE
7478 -- The semantic rules governing the discriminants of derived types are
7479 -- quite subtle.
7481 -- type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
7482 -- [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
7484 -- If parent type has discriminants, then the discriminants that are
7485 -- declared in the derived type are [3.4 (11)]:
7487 -- o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
7488 -- there is one;
7490 -- o Otherwise, each discriminant of the parent type (implicitly declared
7491 -- in the same order with the same specifications). In this case, the
7492 -- discriminants are said to be "inherited", or if unknown in the parent
7493 -- are also unknown in the derived type.
7495 -- Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
7497 -- o The parent subtype must be constrained;
7499 -- o If the parent type is not a tagged type, then each discriminant of
7500 -- the derived type must be used in the constraint defining a parent
7501 -- subtype. [Implementation note: This ensures that the new discriminant
7502 -- can share storage with an existing discriminant.]
7504 -- For the derived type each discriminant of the parent type is either
7505 -- inherited, constrained to equal some new discriminant of the derived
7506 -- type, or constrained to the value of an expression.
7508 -- When inherited or constrained to equal some new discriminant, the
7509 -- parent discriminant and the discriminant of the derived type are said
7510 -- to "correspond".
7512 -- If a discriminant of the parent type is constrained to a specific value
7513 -- in the derived type definition, then the discriminant is said to be
7514 -- "specified" by that derived type definition.
7516 -- 3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
7518 -- We have spoken about stored discriminants in point 1 (introduction)
7519 -- above. There are two sort of stored discriminants: implicit and
7520 -- explicit. As long as the derived type inherits the same discriminants as
7521 -- the root record type, stored discriminants are the same as regular
7522 -- discriminants, and are said to be implicit. However, if any discriminant
7523 -- in the root type was renamed in the derived type, then the derived
7524 -- type will contain explicit stored discriminants. Explicit stored
7525 -- discriminants are discriminants in addition to the semantically visible
7526 -- discriminants defined for the derived type. Stored discriminants are
7527 -- used by Gigi to figure out what are the physical discriminants in
7528 -- objects of the derived type (see precise definition in einfo.ads).
7529 -- As an example, consider the following:
7531 -- type R (D1, D2, D3 : Int) is record ... end record;
7532 -- type T1 is new R;
7533 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
7534 -- type T3 is new T2;
7535 -- type T4 (Y : Int) is new T3 (Y, 99);
7537 -- The following table summarizes the discriminants and stored
7538 -- discriminants in R and T1 through T4.
7540 -- Type Discrim Stored Discrim Comment
7541 -- R (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in R
7542 -- T1 (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in T1
7543 -- T2 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T2
7544 -- T3 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T3
7545 -- T4 (Y) (D1, D2, D3) Girder discrims EXPLICIT in T4
7547 -- Field Corresponding_Discriminant (abbreviated CD below) allows us to
7548 -- find the corresponding discriminant in the parent type, while
7549 -- Original_Record_Component (abbreviated ORC below), the actual physical
7550 -- component that is renamed. Finally the field Is_Completely_Hidden
7551 -- (abbreviated ICH below) is set for all explicit stored discriminants
7552 -- (see einfo.ads for more info). For the above example this gives:
7554 -- Discrim CD ORC ICH
7555 -- ^^^^^^^ ^^ ^^^ ^^^
7556 -- D1 in R empty itself no
7557 -- D2 in R empty itself no
7558 -- D3 in R empty itself no
7560 -- D1 in T1 D1 in R itself no
7561 -- D2 in T1 D2 in R itself no
7562 -- D3 in T1 D3 in R itself no
7564 -- X1 in T2 D3 in T1 D3 in T2 no
7565 -- X2 in T2 D1 in T1 D1 in T2 no
7566 -- D1 in T2 empty itself yes
7567 -- D2 in T2 empty itself yes
7568 -- D3 in T2 empty itself yes
7570 -- X1 in T3 X1 in T2 D3 in T3 no
7571 -- X2 in T3 X2 in T2 D1 in T3 no
7572 -- D1 in T3 empty itself yes
7573 -- D2 in T3 empty itself yes
7574 -- D3 in T3 empty itself yes
7576 -- Y in T4 X1 in T3 D3 in T3 no
7577 -- D1 in T3 empty itself yes
7578 -- D2 in T3 empty itself yes
7579 -- D3 in T3 empty itself yes
7581 -- 4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
7583 -- Type derivation for tagged types is fairly straightforward. If no
7584 -- discriminants are specified by the derived type, these are inherited
7585 -- from the parent. No explicit stored discriminants are ever necessary.
7586 -- The only manipulation that is done to the tree is that of adding a
7587 -- _parent field with parent type and constrained to the same constraint
7588 -- specified for the parent in the derived type definition. For instance:
7590 -- type R (D1, D2, D3 : Int) is tagged record ... end record;
7591 -- type T1 is new R with null record;
7592 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
7594 -- are changed into:
7596 -- type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
7597 -- _parent : R (D1, D2, D3);
7598 -- end record;
7600 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
7601 -- _parent : T1 (X2, 88, X1);
7602 -- end record;
7604 -- The discriminants actually present in R, T1 and T2 as well as their CD,
7605 -- ORC and ICH fields are:
7607 -- Discrim CD ORC ICH
7608 -- ^^^^^^^ ^^ ^^^ ^^^
7609 -- D1 in R empty itself no
7610 -- D2 in R empty itself no
7611 -- D3 in R empty itself no
7613 -- D1 in T1 D1 in R D1 in R no
7614 -- D2 in T1 D2 in R D2 in R no
7615 -- D3 in T1 D3 in R D3 in R no
7617 -- X1 in T2 D3 in T1 D3 in R no
7618 -- X2 in T2 D1 in T1 D1 in R no
7620 -- 5. FIRST TRANSFORMATION FOR DERIVED RECORDS
7622 -- Regardless of whether we dealing with a tagged or untagged type
7623 -- we will transform all derived type declarations of the form
7625 -- type T is new R (...) [with ...];
7626 -- or
7627 -- subtype S is R (...);
7628 -- type T is new S [with ...];
7629 -- into
7630 -- type BT is new R [with ...];
7631 -- subtype T is BT (...);
7633 -- That is, the base derived type is constrained only if it has no
7634 -- discriminants. The reason for doing this is that GNAT's semantic model
7635 -- assumes that a base type with discriminants is unconstrained.
7637 -- Note that, strictly speaking, the above transformation is not always
7638 -- correct. Consider for instance the following excerpt from ACVC b34011a:
7640 -- procedure B34011A is
7641 -- type REC (D : integer := 0) is record
7642 -- I : Integer;
7643 -- end record;
7645 -- package P is
7646 -- type T6 is new Rec;
7647 -- function F return T6;
7648 -- end P;
7650 -- use P;
7651 -- package Q6 is
7652 -- type U is new T6 (Q6.F.I); -- ERROR: Q6.F.
7653 -- end Q6;
7655 -- The definition of Q6.U is illegal. However transforming Q6.U into
7657 -- type BaseU is new T6;
7658 -- subtype U is BaseU (Q6.F.I)
7660 -- turns U into a legal subtype, which is incorrect. To avoid this problem
7661 -- we always analyze the constraint (in this case (Q6.F.I)) before applying
7662 -- the transformation described above.
7664 -- There is another instance where the above transformation is incorrect.
7665 -- Consider:
7667 -- package Pack is
7668 -- type Base (D : Integer) is tagged null record;
7669 -- procedure P (X : Base);
7671 -- type Der is new Base (2) with null record;
7672 -- procedure P (X : Der);
7673 -- end Pack;
7675 -- Then the above transformation turns this into
7677 -- type Der_Base is new Base with null record;
7678 -- -- procedure P (X : Base) is implicitly inherited here
7679 -- -- as procedure P (X : Der_Base).
7681 -- subtype Der is Der_Base (2);
7682 -- procedure P (X : Der);
7683 -- -- The overriding of P (X : Der_Base) is illegal since we
7684 -- -- have a parameter conformance problem.
7686 -- To get around this problem, after having semantically processed Der_Base
7687 -- and the rewritten subtype declaration for Der, we copy Der_Base field
7688 -- Discriminant_Constraint from Der so that when parameter conformance is
7689 -- checked when P is overridden, no semantic errors are flagged.
7691 -- 6. SECOND TRANSFORMATION FOR DERIVED RECORDS
7693 -- Regardless of whether we are dealing with a tagged or untagged type
7694 -- we will transform all derived type declarations of the form
7696 -- type R (D1, .., Dn : ...) is [tagged] record ...;
7697 -- type T is new R [with ...];
7698 -- into
7699 -- type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
7701 -- The reason for such transformation is that it allows us to implement a
7702 -- very clean form of component inheritance as explained below.
7704 -- Note that this transformation is not achieved by direct tree rewriting
7705 -- and manipulation, but rather by redoing the semantic actions that the
7706 -- above transformation will entail. This is done directly in routine
7707 -- Inherit_Components.
7709 -- 7. TYPE DERIVATION AND COMPONENT INHERITANCE
7711 -- In both tagged and untagged derived types, regular non discriminant
7712 -- components are inherited in the derived type from the parent type. In
7713 -- the absence of discriminants component, inheritance is straightforward
7714 -- as components can simply be copied from the parent.
7716 -- If the parent has discriminants, inheriting components constrained with
7717 -- these discriminants requires caution. Consider the following example:
7719 -- type R (D1, D2 : Positive) is [tagged] record
7720 -- S : String (D1 .. D2);
7721 -- end record;
7723 -- type T1 is new R [with null record];
7724 -- type T2 (X : positive) is new R (1, X) [with null record];
7726 -- As explained in 6. above, T1 is rewritten as
7727 -- type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
7728 -- which makes the treatment for T1 and T2 identical.
7730 -- What we want when inheriting S, is that references to D1 and D2 in R are
7731 -- replaced with references to their correct constraints, i.e. D1 and D2 in
7732 -- T1 and 1 and X in T2. So all R's discriminant references are replaced
7733 -- with either discriminant references in the derived type or expressions.
7734 -- This replacement is achieved as follows: before inheriting R's
7735 -- components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
7736 -- created in the scope of T1 (resp. scope of T2) so that discriminants D1
7737 -- and D2 of T1 are visible (resp. discriminant X of T2 is visible).
7738 -- For T2, for instance, this has the effect of replacing String (D1 .. D2)
7739 -- by String (1 .. X).
7741 -- 8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
7743 -- We explain here the rules governing private type extensions relevant to
7744 -- type derivation. These rules are explained on the following example:
7746 -- type D [(...)] is new A [(...)] with private; <-- partial view
7747 -- type D [(...)] is new P [(...)] with null record; <-- full view
7749 -- Type A is called the ancestor subtype of the private extension.
7750 -- Type P is the parent type of the full view of the private extension. It
7751 -- must be A or a type derived from A.
7753 -- The rules concerning the discriminants of private type extensions are
7754 -- [7.3(10-13)]:
7756 -- o If a private extension inherits known discriminants from the ancestor
7757 -- subtype, then the full view must also inherit its discriminants from
7758 -- the ancestor subtype and the parent subtype of the full view must be
7759 -- constrained if and only if the ancestor subtype is constrained.
7761 -- o If a partial view has unknown discriminants, then the full view may
7762 -- define a definite or an indefinite subtype, with or without
7763 -- discriminants.
7765 -- o If a partial view has neither known nor unknown discriminants, then
7766 -- the full view must define a definite subtype.
7768 -- o If the ancestor subtype of a private extension has constrained
7769 -- discriminants, then the parent subtype of the full view must impose a
7770 -- statically matching constraint on those discriminants.
7772 -- This means that only the following forms of private extensions are
7773 -- allowed:
7775 -- type D is new A with private; <-- partial view
7776 -- type D is new P with null record; <-- full view
7778 -- If A has no discriminants than P has no discriminants, otherwise P must
7779 -- inherit A's discriminants.
7781 -- type D is new A (...) with private; <-- partial view
7782 -- type D is new P (:::) with null record; <-- full view
7784 -- P must inherit A's discriminants and (...) and (:::) must statically
7785 -- match.
7787 -- subtype A is R (...);
7788 -- type D is new A with private; <-- partial view
7789 -- type D is new P with null record; <-- full view
7791 -- P must have inherited R's discriminants and must be derived from A or
7792 -- any of its subtypes.
7794 -- type D (..) is new A with private; <-- partial view
7795 -- type D (..) is new P [(:::)] with null record; <-- full view
7797 -- No specific constraints on P's discriminants or constraint (:::).
7798 -- Note that A can be unconstrained, but the parent subtype P must either
7799 -- be constrained or (:::) must be present.
7801 -- type D (..) is new A [(...)] with private; <-- partial view
7802 -- type D (..) is new P [(:::)] with null record; <-- full view
7804 -- P's constraints on A's discriminants must statically match those
7805 -- imposed by (...).
7807 -- 9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
7809 -- The full view of a private extension is handled exactly as described
7810 -- above. The model chose for the private view of a private extension is
7811 -- the same for what concerns discriminants (i.e. they receive the same
7812 -- treatment as in the tagged case). However, the private view of the
7813 -- private extension always inherits the components of the parent base,
7814 -- without replacing any discriminant reference. Strictly speaking this is
7815 -- incorrect. However, Gigi never uses this view to generate code so this
7816 -- is a purely semantic issue. In theory, a set of transformations similar
7817 -- to those given in 5. and 6. above could be applied to private views of
7818 -- private extensions to have the same model of component inheritance as
7819 -- for non private extensions. However, this is not done because it would
7820 -- further complicate private type processing. Semantically speaking, this
7821 -- leaves us in an uncomfortable situation. As an example consider:
7823 -- package Pack is
7824 -- type R (D : integer) is tagged record
7825 -- S : String (1 .. D);
7826 -- end record;
7827 -- procedure P (X : R);
7828 -- type T is new R (1) with private;
7829 -- private
7830 -- type T is new R (1) with null record;
7831 -- end;
7833 -- This is transformed into:
7835 -- package Pack is
7836 -- type R (D : integer) is tagged record
7837 -- S : String (1 .. D);
7838 -- end record;
7839 -- procedure P (X : R);
7840 -- type T is new R (1) with private;
7841 -- private
7842 -- type BaseT is new R with null record;
7843 -- subtype T is BaseT (1);
7844 -- end;
7846 -- (strictly speaking the above is incorrect Ada)
7848 -- From the semantic standpoint the private view of private extension T
7849 -- should be flagged as constrained since one can clearly have
7851 -- Obj : T;
7853 -- in a unit withing Pack. However, when deriving subprograms for the
7854 -- private view of private extension T, T must be seen as unconstrained
7855 -- since T has discriminants (this is a constraint of the current
7856 -- subprogram derivation model). Thus, when processing the private view of
7857 -- a private extension such as T, we first mark T as unconstrained, we
7858 -- process it, we perform program derivation and just before returning from
7859 -- Build_Derived_Record_Type we mark T as constrained.
7861 -- ??? Are there are other uncomfortable cases that we will have to
7862 -- deal with.
7864 -- 10. RECORD_TYPE_WITH_PRIVATE complications
7866 -- Types that are derived from a visible record type and have a private
7867 -- extension present other peculiarities. They behave mostly like private
7868 -- types, but if they have primitive operations defined, these will not
7869 -- have the proper signatures for further inheritance, because other
7870 -- primitive operations will use the implicit base that we define for
7871 -- private derivations below. This affect subprogram inheritance (see
7872 -- Derive_Subprograms for details). We also derive the implicit base from
7873 -- the base type of the full view, so that the implicit base is a record
7874 -- type and not another private type, This avoids infinite loops.
7876 procedure Build_Derived_Record_Type
7877 (N : Node_Id;
7878 Parent_Type : Entity_Id;
7879 Derived_Type : Entity_Id;
7880 Derive_Subps : Boolean := True)
7882 Discriminant_Specs : constant Boolean :=
7883 Present (Discriminant_Specifications (N));
7884 Is_Tagged : constant Boolean := Is_Tagged_Type (Parent_Type);
7885 Loc : constant Source_Ptr := Sloc (N);
7886 Private_Extension : constant Boolean :=
7887 Nkind (N) = N_Private_Extension_Declaration;
7888 Assoc_List : Elist_Id;
7889 Constraint_Present : Boolean;
7890 Constrs : Elist_Id;
7891 Discrim : Entity_Id;
7892 Indic : Node_Id;
7893 Inherit_Discrims : Boolean := False;
7894 Last_Discrim : Entity_Id;
7895 New_Base : Entity_Id;
7896 New_Decl : Node_Id;
7897 New_Discrs : Elist_Id;
7898 New_Indic : Node_Id;
7899 Parent_Base : Entity_Id;
7900 Save_Etype : Entity_Id;
7901 Save_Discr_Constr : Elist_Id;
7902 Save_Next_Entity : Entity_Id;
7903 Type_Def : Node_Id;
7905 Discs : Elist_Id := New_Elmt_List;
7906 -- An empty Discs list means that there were no constraints in the
7907 -- subtype indication or that there was an error processing it.
7909 begin
7910 if Ekind (Parent_Type) = E_Record_Type_With_Private
7911 and then Present (Full_View (Parent_Type))
7912 and then Has_Discriminants (Parent_Type)
7913 then
7914 Parent_Base := Base_Type (Full_View (Parent_Type));
7915 else
7916 Parent_Base := Base_Type (Parent_Type);
7917 end if;
7919 -- AI05-0115 : if this is a derivation from a private type in some
7920 -- other scope that may lead to invisible components for the derived
7921 -- type, mark it accordingly.
7923 if Is_Private_Type (Parent_Type) then
7924 if Scope (Parent_Type) = Scope (Derived_Type) then
7925 null;
7927 elsif In_Open_Scopes (Scope (Parent_Type))
7928 and then In_Private_Part (Scope (Parent_Type))
7929 then
7930 null;
7932 else
7933 Set_Has_Private_Ancestor (Derived_Type);
7934 end if;
7936 else
7937 Set_Has_Private_Ancestor
7938 (Derived_Type, Has_Private_Ancestor (Parent_Type));
7939 end if;
7941 -- Before we start the previously documented transformations, here is
7942 -- little fix for size and alignment of tagged types. Normally when we
7943 -- derive type D from type P, we copy the size and alignment of P as the
7944 -- default for D, and in the absence of explicit representation clauses
7945 -- for D, the size and alignment are indeed the same as the parent.
7947 -- But this is wrong for tagged types, since fields may be added, and
7948 -- the default size may need to be larger, and the default alignment may
7949 -- need to be larger.
7951 -- We therefore reset the size and alignment fields in the tagged case.
7952 -- Note that the size and alignment will in any case be at least as
7953 -- large as the parent type (since the derived type has a copy of the
7954 -- parent type in the _parent field)
7956 -- The type is also marked as being tagged here, which is needed when
7957 -- processing components with a self-referential anonymous access type
7958 -- in the call to Check_Anonymous_Access_Components below. Note that
7959 -- this flag is also set later on for completeness.
7961 if Is_Tagged then
7962 Set_Is_Tagged_Type (Derived_Type);
7963 Init_Size_Align (Derived_Type);
7964 end if;
7966 -- STEP 0a: figure out what kind of derived type declaration we have
7968 if Private_Extension then
7969 Type_Def := N;
7970 Set_Ekind (Derived_Type, E_Record_Type_With_Private);
7971 Set_Default_SSO (Derived_Type);
7973 else
7974 Type_Def := Type_Definition (N);
7976 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
7977 -- Parent_Base can be a private type or private extension. However,
7978 -- for tagged types with an extension the newly added fields are
7979 -- visible and hence the Derived_Type is always an E_Record_Type.
7980 -- (except that the parent may have its own private fields).
7981 -- For untagged types we preserve the Ekind of the Parent_Base.
7983 if Present (Record_Extension_Part (Type_Def)) then
7984 Set_Ekind (Derived_Type, E_Record_Type);
7985 Set_Default_SSO (Derived_Type);
7987 -- Create internal access types for components with anonymous
7988 -- access types.
7990 if Ada_Version >= Ada_2005 then
7991 Check_Anonymous_Access_Components
7992 (N, Derived_Type, Derived_Type,
7993 Component_List (Record_Extension_Part (Type_Def)));
7994 end if;
7996 else
7997 Set_Ekind (Derived_Type, Ekind (Parent_Base));
7998 end if;
7999 end if;
8001 -- Indic can either be an N_Identifier if the subtype indication
8002 -- contains no constraint or an N_Subtype_Indication if the subtype
8003 -- indication has a constraint.
8005 Indic := Subtype_Indication (Type_Def);
8006 Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
8008 -- Check that the type has visible discriminants. The type may be
8009 -- a private type with unknown discriminants whose full view has
8010 -- discriminants which are invisible.
8012 if Constraint_Present then
8013 if not Has_Discriminants (Parent_Base)
8014 or else
8015 (Has_Unknown_Discriminants (Parent_Base)
8016 and then Is_Private_Type (Parent_Base))
8017 then
8018 Error_Msg_N
8019 ("invalid constraint: type has no discriminant",
8020 Constraint (Indic));
8022 Constraint_Present := False;
8023 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
8025 elsif Is_Constrained (Parent_Type) then
8026 Error_Msg_N
8027 ("invalid constraint: parent type is already constrained",
8028 Constraint (Indic));
8030 Constraint_Present := False;
8031 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
8032 end if;
8033 end if;
8035 -- STEP 0b: If needed, apply transformation given in point 5. above
8037 if not Private_Extension
8038 and then Has_Discriminants (Parent_Type)
8039 and then not Discriminant_Specs
8040 and then (Is_Constrained (Parent_Type) or else Constraint_Present)
8041 then
8042 -- First, we must analyze the constraint (see comment in point 5.)
8043 -- The constraint may come from the subtype indication of the full
8044 -- declaration.
8046 if Constraint_Present then
8047 New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
8049 -- If there is no explicit constraint, there might be one that is
8050 -- inherited from a constrained parent type. In that case verify that
8051 -- it conforms to the constraint in the partial view. In perverse
8052 -- cases the parent subtypes of the partial and full view can have
8053 -- different constraints.
8055 elsif Present (Stored_Constraint (Parent_Type)) then
8056 New_Discrs := Stored_Constraint (Parent_Type);
8058 else
8059 New_Discrs := No_Elist;
8060 end if;
8062 if Has_Discriminants (Derived_Type)
8063 and then Has_Private_Declaration (Derived_Type)
8064 and then Present (Discriminant_Constraint (Derived_Type))
8065 and then Present (New_Discrs)
8066 then
8067 -- Verify that constraints of the full view statically match
8068 -- those given in the partial view.
8070 declare
8071 C1, C2 : Elmt_Id;
8073 begin
8074 C1 := First_Elmt (New_Discrs);
8075 C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
8076 while Present (C1) and then Present (C2) loop
8077 if Fully_Conformant_Expressions (Node (C1), Node (C2))
8078 or else
8079 (Is_OK_Static_Expression (Node (C1))
8080 and then Is_OK_Static_Expression (Node (C2))
8081 and then
8082 Expr_Value (Node (C1)) = Expr_Value (Node (C2)))
8083 then
8084 null;
8086 else
8087 if Constraint_Present then
8088 Error_Msg_N
8089 ("constraint not conformant to previous declaration",
8090 Node (C1));
8091 else
8092 Error_Msg_N
8093 ("constraint of full view is incompatible "
8094 & "with partial view", N);
8095 end if;
8096 end if;
8098 Next_Elmt (C1);
8099 Next_Elmt (C2);
8100 end loop;
8101 end;
8102 end if;
8104 -- Insert and analyze the declaration for the unconstrained base type
8106 New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
8108 New_Decl :=
8109 Make_Full_Type_Declaration (Loc,
8110 Defining_Identifier => New_Base,
8111 Type_Definition =>
8112 Make_Derived_Type_Definition (Loc,
8113 Abstract_Present => Abstract_Present (Type_Def),
8114 Limited_Present => Limited_Present (Type_Def),
8115 Subtype_Indication =>
8116 New_Occurrence_Of (Parent_Base, Loc),
8117 Record_Extension_Part =>
8118 Relocate_Node (Record_Extension_Part (Type_Def)),
8119 Interface_List => Interface_List (Type_Def)));
8121 Set_Parent (New_Decl, Parent (N));
8122 Mark_Rewrite_Insertion (New_Decl);
8123 Insert_Before (N, New_Decl);
8125 -- In the extension case, make sure ancestor is frozen appropriately
8126 -- (see also non-discriminated case below).
8128 if Present (Record_Extension_Part (Type_Def))
8129 or else Is_Interface (Parent_Base)
8130 then
8131 Freeze_Before (New_Decl, Parent_Type);
8132 end if;
8134 -- Note that this call passes False for the Derive_Subps parameter
8135 -- because subprogram derivation is deferred until after creating
8136 -- the subtype (see below).
8138 Build_Derived_Type
8139 (New_Decl, Parent_Base, New_Base,
8140 Is_Completion => False, Derive_Subps => False);
8142 -- ??? This needs re-examination to determine whether the
8143 -- above call can simply be replaced by a call to Analyze.
8145 Set_Analyzed (New_Decl);
8147 -- Insert and analyze the declaration for the constrained subtype
8149 if Constraint_Present then
8150 New_Indic :=
8151 Make_Subtype_Indication (Loc,
8152 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
8153 Constraint => Relocate_Node (Constraint (Indic)));
8155 else
8156 declare
8157 Constr_List : constant List_Id := New_List;
8158 C : Elmt_Id;
8159 Expr : Node_Id;
8161 begin
8162 C := First_Elmt (Discriminant_Constraint (Parent_Type));
8163 while Present (C) loop
8164 Expr := Node (C);
8166 -- It is safe here to call New_Copy_Tree since we called
8167 -- Force_Evaluation on each constraint previously
8168 -- in Build_Discriminant_Constraints.
8170 Append (New_Copy_Tree (Expr), To => Constr_List);
8172 Next_Elmt (C);
8173 end loop;
8175 New_Indic :=
8176 Make_Subtype_Indication (Loc,
8177 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
8178 Constraint =>
8179 Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
8180 end;
8181 end if;
8183 Rewrite (N,
8184 Make_Subtype_Declaration (Loc,
8185 Defining_Identifier => Derived_Type,
8186 Subtype_Indication => New_Indic));
8188 Analyze (N);
8190 -- Derivation of subprograms must be delayed until the full subtype
8191 -- has been established, to ensure proper overriding of subprograms
8192 -- inherited by full types. If the derivations occurred as part of
8193 -- the call to Build_Derived_Type above, then the check for type
8194 -- conformance would fail because earlier primitive subprograms
8195 -- could still refer to the full type prior the change to the new
8196 -- subtype and hence would not match the new base type created here.
8197 -- Subprograms are not derived, however, when Derive_Subps is False
8198 -- (since otherwise there could be redundant derivations).
8200 if Derive_Subps then
8201 Derive_Subprograms (Parent_Type, Derived_Type);
8202 end if;
8204 -- For tagged types the Discriminant_Constraint of the new base itype
8205 -- is inherited from the first subtype so that no subtype conformance
8206 -- problem arise when the first subtype overrides primitive
8207 -- operations inherited by the implicit base type.
8209 if Is_Tagged then
8210 Set_Discriminant_Constraint
8211 (New_Base, Discriminant_Constraint (Derived_Type));
8212 end if;
8214 return;
8215 end if;
8217 -- If we get here Derived_Type will have no discriminants or it will be
8218 -- a discriminated unconstrained base type.
8220 -- STEP 1a: perform preliminary actions/checks for derived tagged types
8222 if Is_Tagged then
8224 -- The parent type is frozen for non-private extensions (RM 13.14(7))
8225 -- The declaration of a specific descendant of an interface type
8226 -- freezes the interface type (RM 13.14).
8228 if not Private_Extension or else Is_Interface (Parent_Base) then
8229 Freeze_Before (N, Parent_Type);
8230 end if;
8232 -- In Ada 2005 (AI-344), the restriction that a derived tagged type
8233 -- cannot be declared at a deeper level than its parent type is
8234 -- removed. The check on derivation within a generic body is also
8235 -- relaxed, but there's a restriction that a derived tagged type
8236 -- cannot be declared in a generic body if it's derived directly
8237 -- or indirectly from a formal type of that generic.
8239 if Ada_Version >= Ada_2005 then
8240 if Present (Enclosing_Generic_Body (Derived_Type)) then
8241 declare
8242 Ancestor_Type : Entity_Id;
8244 begin
8245 -- Check to see if any ancestor of the derived type is a
8246 -- formal type.
8248 Ancestor_Type := Parent_Type;
8249 while not Is_Generic_Type (Ancestor_Type)
8250 and then Etype (Ancestor_Type) /= Ancestor_Type
8251 loop
8252 Ancestor_Type := Etype (Ancestor_Type);
8253 end loop;
8255 -- If the derived type does have a formal type as an
8256 -- ancestor, then it's an error if the derived type is
8257 -- declared within the body of the generic unit that
8258 -- declares the formal type in its generic formal part. It's
8259 -- sufficient to check whether the ancestor type is declared
8260 -- inside the same generic body as the derived type (such as
8261 -- within a nested generic spec), in which case the
8262 -- derivation is legal. If the formal type is declared
8263 -- outside of that generic body, then it's guaranteed that
8264 -- the derived type is declared within the generic body of
8265 -- the generic unit declaring the formal type.
8267 if Is_Generic_Type (Ancestor_Type)
8268 and then Enclosing_Generic_Body (Ancestor_Type) /=
8269 Enclosing_Generic_Body (Derived_Type)
8270 then
8271 Error_Msg_NE
8272 ("parent type of& must not be descendant of formal type"
8273 & " of an enclosing generic body",
8274 Indic, Derived_Type);
8275 end if;
8276 end;
8277 end if;
8279 elsif Type_Access_Level (Derived_Type) /=
8280 Type_Access_Level (Parent_Type)
8281 and then not Is_Generic_Type (Derived_Type)
8282 then
8283 if Is_Controlled (Parent_Type) then
8284 Error_Msg_N
8285 ("controlled type must be declared at the library level",
8286 Indic);
8287 else
8288 Error_Msg_N
8289 ("type extension at deeper accessibility level than parent",
8290 Indic);
8291 end if;
8293 else
8294 declare
8295 GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
8296 begin
8297 if Present (GB)
8298 and then GB /= Enclosing_Generic_Body (Parent_Base)
8299 then
8300 Error_Msg_NE
8301 ("parent type of& must not be outside generic body"
8302 & " (RM 3.9.1(4))",
8303 Indic, Derived_Type);
8304 end if;
8305 end;
8306 end if;
8307 end if;
8309 -- Ada 2005 (AI-251)
8311 if Ada_Version >= Ada_2005 and then Is_Tagged then
8313 -- "The declaration of a specific descendant of an interface type
8314 -- freezes the interface type" (RM 13.14).
8316 declare
8317 Iface : Node_Id;
8318 begin
8319 if Is_Non_Empty_List (Interface_List (Type_Def)) then
8320 Iface := First (Interface_List (Type_Def));
8321 while Present (Iface) loop
8322 Freeze_Before (N, Etype (Iface));
8323 Next (Iface);
8324 end loop;
8325 end if;
8326 end;
8327 end if;
8329 -- STEP 1b : preliminary cleanup of the full view of private types
8331 -- If the type is already marked as having discriminants, then it's the
8332 -- completion of a private type or private extension and we need to
8333 -- retain the discriminants from the partial view if the current
8334 -- declaration has Discriminant_Specifications so that we can verify
8335 -- conformance. However, we must remove any existing components that
8336 -- were inherited from the parent (and attached in Copy_And_Swap)
8337 -- because the full type inherits all appropriate components anyway, and
8338 -- we do not want the partial view's components interfering.
8340 if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
8341 Discrim := First_Discriminant (Derived_Type);
8342 loop
8343 Last_Discrim := Discrim;
8344 Next_Discriminant (Discrim);
8345 exit when No (Discrim);
8346 end loop;
8348 Set_Last_Entity (Derived_Type, Last_Discrim);
8350 -- In all other cases wipe out the list of inherited components (even
8351 -- inherited discriminants), it will be properly rebuilt here.
8353 else
8354 Set_First_Entity (Derived_Type, Empty);
8355 Set_Last_Entity (Derived_Type, Empty);
8356 end if;
8358 -- STEP 1c: Initialize some flags for the Derived_Type
8360 -- The following flags must be initialized here so that
8361 -- Process_Discriminants can check that discriminants of tagged types do
8362 -- not have a default initial value and that access discriminants are
8363 -- only specified for limited records. For completeness, these flags are
8364 -- also initialized along with all the other flags below.
8366 -- AI-419: Limitedness is not inherited from an interface parent, so to
8367 -- be limited in that case the type must be explicitly declared as
8368 -- limited. However, task and protected interfaces are always limited.
8370 if Limited_Present (Type_Def) then
8371 Set_Is_Limited_Record (Derived_Type);
8373 elsif Is_Limited_Record (Parent_Type)
8374 or else (Present (Full_View (Parent_Type))
8375 and then Is_Limited_Record (Full_View (Parent_Type)))
8376 then
8377 if not Is_Interface (Parent_Type)
8378 or else Is_Synchronized_Interface (Parent_Type)
8379 or else Is_Protected_Interface (Parent_Type)
8380 or else Is_Task_Interface (Parent_Type)
8381 then
8382 Set_Is_Limited_Record (Derived_Type);
8383 end if;
8384 end if;
8386 -- STEP 2a: process discriminants of derived type if any
8388 Push_Scope (Derived_Type);
8390 if Discriminant_Specs then
8391 Set_Has_Unknown_Discriminants (Derived_Type, False);
8393 -- The following call initializes fields Has_Discriminants and
8394 -- Discriminant_Constraint, unless we are processing the completion
8395 -- of a private type declaration.
8397 Check_Or_Process_Discriminants (N, Derived_Type);
8399 -- For untagged types, the constraint on the Parent_Type must be
8400 -- present and is used to rename the discriminants.
8402 if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
8403 Error_Msg_N ("untagged parent must have discriminants", Indic);
8405 elsif not Is_Tagged and then not Constraint_Present then
8406 Error_Msg_N
8407 ("discriminant constraint needed for derived untagged records",
8408 Indic);
8410 -- Otherwise the parent subtype must be constrained unless we have a
8411 -- private extension.
8413 elsif not Constraint_Present
8414 and then not Private_Extension
8415 and then not Is_Constrained (Parent_Type)
8416 then
8417 Error_Msg_N
8418 ("unconstrained type not allowed in this context", Indic);
8420 elsif Constraint_Present then
8421 -- The following call sets the field Corresponding_Discriminant
8422 -- for the discriminants in the Derived_Type.
8424 Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
8426 -- For untagged types all new discriminants must rename
8427 -- discriminants in the parent. For private extensions new
8428 -- discriminants cannot rename old ones (implied by [7.3(13)]).
8430 Discrim := First_Discriminant (Derived_Type);
8431 while Present (Discrim) loop
8432 if not Is_Tagged
8433 and then No (Corresponding_Discriminant (Discrim))
8434 then
8435 Error_Msg_N
8436 ("new discriminants must constrain old ones", Discrim);
8438 elsif Private_Extension
8439 and then Present (Corresponding_Discriminant (Discrim))
8440 then
8441 Error_Msg_N
8442 ("only static constraints allowed for parent"
8443 & " discriminants in the partial view", Indic);
8444 exit;
8445 end if;
8447 -- If a new discriminant is used in the constraint, then its
8448 -- subtype must be statically compatible with the parent
8449 -- discriminant's subtype (3.7(15)).
8451 -- However, if the record contains an array constrained by
8452 -- the discriminant but with some different bound, the compiler
8453 -- attemps to create a smaller range for the discriminant type.
8454 -- (See exp_ch3.Adjust_Discriminants). In this case, where
8455 -- the discriminant type is a scalar type, the check must use
8456 -- the original discriminant type in the parent declaration.
8458 declare
8459 Corr_Disc : constant Entity_Id :=
8460 Corresponding_Discriminant (Discrim);
8461 Disc_Type : constant Entity_Id := Etype (Discrim);
8462 Corr_Type : Entity_Id;
8464 begin
8465 if Present (Corr_Disc) then
8466 if Is_Scalar_Type (Disc_Type) then
8467 Corr_Type :=
8468 Entity (Discriminant_Type (Parent (Corr_Disc)));
8469 else
8470 Corr_Type := Etype (Corr_Disc);
8471 end if;
8473 if not
8474 Subtypes_Statically_Compatible (Disc_Type, Corr_Type)
8475 then
8476 Error_Msg_N
8477 ("subtype must be compatible "
8478 & "with parent discriminant",
8479 Discrim);
8480 end if;
8481 end if;
8482 end;
8484 Next_Discriminant (Discrim);
8485 end loop;
8487 -- Check whether the constraints of the full view statically
8488 -- match those imposed by the parent subtype [7.3(13)].
8490 if Present (Stored_Constraint (Derived_Type)) then
8491 declare
8492 C1, C2 : Elmt_Id;
8494 begin
8495 C1 := First_Elmt (Discs);
8496 C2 := First_Elmt (Stored_Constraint (Derived_Type));
8497 while Present (C1) and then Present (C2) loop
8498 if not
8499 Fully_Conformant_Expressions (Node (C1), Node (C2))
8500 then
8501 Error_Msg_N
8502 ("not conformant with previous declaration",
8503 Node (C1));
8504 end if;
8506 Next_Elmt (C1);
8507 Next_Elmt (C2);
8508 end loop;
8509 end;
8510 end if;
8511 end if;
8513 -- STEP 2b: No new discriminants, inherit discriminants if any
8515 else
8516 if Private_Extension then
8517 Set_Has_Unknown_Discriminants
8518 (Derived_Type,
8519 Has_Unknown_Discriminants (Parent_Type)
8520 or else Unknown_Discriminants_Present (N));
8522 -- The partial view of the parent may have unknown discriminants,
8523 -- but if the full view has discriminants and the parent type is
8524 -- in scope they must be inherited.
8526 elsif Has_Unknown_Discriminants (Parent_Type)
8527 and then
8528 (not Has_Discriminants (Parent_Type)
8529 or else not In_Open_Scopes (Scope (Parent_Type)))
8530 then
8531 Set_Has_Unknown_Discriminants (Derived_Type);
8532 end if;
8534 if not Has_Unknown_Discriminants (Derived_Type)
8535 and then not Has_Unknown_Discriminants (Parent_Base)
8536 and then Has_Discriminants (Parent_Type)
8537 then
8538 Inherit_Discrims := True;
8539 Set_Has_Discriminants
8540 (Derived_Type, True);
8541 Set_Discriminant_Constraint
8542 (Derived_Type, Discriminant_Constraint (Parent_Base));
8543 end if;
8545 -- The following test is true for private types (remember
8546 -- transformation 5. is not applied to those) and in an error
8547 -- situation.
8549 if Constraint_Present then
8550 Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
8551 end if;
8553 -- For now mark a new derived type as constrained only if it has no
8554 -- discriminants. At the end of Build_Derived_Record_Type we properly
8555 -- set this flag in the case of private extensions. See comments in
8556 -- point 9. just before body of Build_Derived_Record_Type.
8558 Set_Is_Constrained
8559 (Derived_Type,
8560 not (Inherit_Discrims
8561 or else Has_Unknown_Discriminants (Derived_Type)));
8562 end if;
8564 -- STEP 3: initialize fields of derived type
8566 Set_Is_Tagged_Type (Derived_Type, Is_Tagged);
8567 Set_Stored_Constraint (Derived_Type, No_Elist);
8569 -- Ada 2005 (AI-251): Private type-declarations can implement interfaces
8570 -- but cannot be interfaces
8572 if not Private_Extension
8573 and then Ekind (Derived_Type) /= E_Private_Type
8574 and then Ekind (Derived_Type) /= E_Limited_Private_Type
8575 then
8576 if Interface_Present (Type_Def) then
8577 Analyze_Interface_Declaration (Derived_Type, Type_Def);
8578 end if;
8580 Set_Interfaces (Derived_Type, No_Elist);
8581 end if;
8583 -- Fields inherited from the Parent_Type
8585 Set_Has_Specified_Layout
8586 (Derived_Type, Has_Specified_Layout (Parent_Type));
8587 Set_Is_Limited_Composite
8588 (Derived_Type, Is_Limited_Composite (Parent_Type));
8589 Set_Is_Private_Composite
8590 (Derived_Type, Is_Private_Composite (Parent_Type));
8592 if Is_Tagged_Type (Parent_Type) then
8593 Set_No_Tagged_Streams_Pragma
8594 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
8595 end if;
8597 -- Fields inherited from the Parent_Base
8599 Set_Has_Controlled_Component
8600 (Derived_Type, Has_Controlled_Component (Parent_Base));
8601 Set_Has_Non_Standard_Rep
8602 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
8603 Set_Has_Primitive_Operations
8604 (Derived_Type, Has_Primitive_Operations (Parent_Base));
8606 -- Fields inherited from the Parent_Base in the non-private case
8608 if Ekind (Derived_Type) = E_Record_Type then
8609 Set_Has_Complex_Representation
8610 (Derived_Type, Has_Complex_Representation (Parent_Base));
8611 end if;
8613 -- Fields inherited from the Parent_Base for record types
8615 if Is_Record_Type (Derived_Type) then
8616 declare
8617 Parent_Full : Entity_Id;
8619 begin
8620 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
8621 -- Parent_Base can be a private type or private extension. Go
8622 -- to the full view here to get the E_Record_Type specific flags.
8624 if Present (Full_View (Parent_Base)) then
8625 Parent_Full := Full_View (Parent_Base);
8626 else
8627 Parent_Full := Parent_Base;
8628 end if;
8630 Set_OK_To_Reorder_Components
8631 (Derived_Type, OK_To_Reorder_Components (Parent_Full));
8632 end;
8633 end if;
8635 -- Set fields for private derived types
8637 if Is_Private_Type (Derived_Type) then
8638 Set_Depends_On_Private (Derived_Type, True);
8639 Set_Private_Dependents (Derived_Type, New_Elmt_List);
8641 -- Inherit fields from non private record types. If this is the
8642 -- completion of a derivation from a private type, the parent itself
8643 -- is private, and the attributes come from its full view, which must
8644 -- be present.
8646 else
8647 if Is_Private_Type (Parent_Base)
8648 and then not Is_Record_Type (Parent_Base)
8649 then
8650 Set_Component_Alignment
8651 (Derived_Type, Component_Alignment (Full_View (Parent_Base)));
8652 Set_C_Pass_By_Copy
8653 (Derived_Type, C_Pass_By_Copy (Full_View (Parent_Base)));
8654 else
8655 Set_Component_Alignment
8656 (Derived_Type, Component_Alignment (Parent_Base));
8657 Set_C_Pass_By_Copy
8658 (Derived_Type, C_Pass_By_Copy (Parent_Base));
8659 end if;
8660 end if;
8662 -- Set fields for tagged types
8664 if Is_Tagged then
8665 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
8667 -- All tagged types defined in Ada.Finalization are controlled
8669 if Chars (Scope (Derived_Type)) = Name_Finalization
8670 and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
8671 and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
8672 then
8673 Set_Is_Controlled (Derived_Type);
8674 else
8675 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Base));
8676 end if;
8678 -- Minor optimization: there is no need to generate the class-wide
8679 -- entity associated with an underlying record view.
8681 if not Is_Underlying_Record_View (Derived_Type) then
8682 Make_Class_Wide_Type (Derived_Type);
8683 end if;
8685 Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
8687 if Has_Discriminants (Derived_Type)
8688 and then Constraint_Present
8689 then
8690 Set_Stored_Constraint
8691 (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
8692 end if;
8694 if Ada_Version >= Ada_2005 then
8695 declare
8696 Ifaces_List : Elist_Id;
8698 begin
8699 -- Checks rules 3.9.4 (13/2 and 14/2)
8701 if Comes_From_Source (Derived_Type)
8702 and then not Is_Private_Type (Derived_Type)
8703 and then Is_Interface (Parent_Type)
8704 and then not Is_Interface (Derived_Type)
8705 then
8706 if Is_Task_Interface (Parent_Type) then
8707 Error_Msg_N
8708 ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
8709 Derived_Type);
8711 elsif Is_Protected_Interface (Parent_Type) then
8712 Error_Msg_N
8713 ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
8714 Derived_Type);
8715 end if;
8716 end if;
8718 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
8720 Check_Interfaces (N, Type_Def);
8722 -- Ada 2005 (AI-251): Collect the list of progenitors that are
8723 -- not already in the parents.
8725 Collect_Interfaces
8726 (T => Derived_Type,
8727 Ifaces_List => Ifaces_List,
8728 Exclude_Parents => True);
8730 Set_Interfaces (Derived_Type, Ifaces_List);
8732 -- If the derived type is the anonymous type created for
8733 -- a declaration whose parent has a constraint, propagate
8734 -- the interface list to the source type. This must be done
8735 -- prior to the completion of the analysis of the source type
8736 -- because the components in the extension may contain current
8737 -- instances whose legality depends on some ancestor.
8739 if Is_Itype (Derived_Type) then
8740 declare
8741 Def : constant Node_Id :=
8742 Associated_Node_For_Itype (Derived_Type);
8743 begin
8744 if Present (Def)
8745 and then Nkind (Def) = N_Full_Type_Declaration
8746 then
8747 Set_Interfaces
8748 (Defining_Identifier (Def), Ifaces_List);
8749 end if;
8750 end;
8751 end if;
8753 -- Propagate inherited invariant information of parents
8754 -- and progenitors
8756 if Ada_Version >= Ada_2012
8757 and then not Is_Interface (Derived_Type)
8758 then
8759 if Has_Inheritable_Invariants (Parent_Type) then
8760 Set_Has_Invariants (Derived_Type);
8761 Set_Has_Inheritable_Invariants (Derived_Type);
8763 elsif not Is_Empty_Elmt_List (Ifaces_List) then
8764 declare
8765 AI : Elmt_Id;
8767 begin
8768 AI := First_Elmt (Ifaces_List);
8769 while Present (AI) loop
8770 if Has_Inheritable_Invariants (Node (AI)) then
8771 Set_Has_Invariants (Derived_Type);
8772 Set_Has_Inheritable_Invariants (Derived_Type);
8774 exit;
8775 end if;
8777 Next_Elmt (AI);
8778 end loop;
8779 end;
8780 end if;
8781 end if;
8783 -- A type extension is automatically Ghost when one of its
8784 -- progenitors is Ghost (SPARK RM 6.9(9)). This property is
8785 -- also inherited when the parent type is Ghost, but this is
8786 -- done in Build_Derived_Type as the mechanism also handles
8787 -- untagged derivations.
8789 if Implements_Ghost_Interface (Derived_Type) then
8790 Set_Is_Ghost_Entity (Derived_Type);
8791 end if;
8792 end;
8793 end if;
8795 else
8796 Set_Is_Packed (Derived_Type, Is_Packed (Parent_Base));
8797 Set_Has_Non_Standard_Rep
8798 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
8799 end if;
8801 -- STEP 4: Inherit components from the parent base and constrain them.
8802 -- Apply the second transformation described in point 6. above.
8804 if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
8805 or else not Has_Discriminants (Parent_Type)
8806 or else not Is_Constrained (Parent_Type)
8807 then
8808 Constrs := Discs;
8809 else
8810 Constrs := Discriminant_Constraint (Parent_Type);
8811 end if;
8813 Assoc_List :=
8814 Inherit_Components
8815 (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
8817 -- STEP 5a: Copy the parent record declaration for untagged types
8819 if not Is_Tagged then
8821 -- Discriminant_Constraint (Derived_Type) has been properly
8822 -- constructed. Save it and temporarily set it to Empty because we
8823 -- do not want the call to New_Copy_Tree below to mess this list.
8825 if Has_Discriminants (Derived_Type) then
8826 Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
8827 Set_Discriminant_Constraint (Derived_Type, No_Elist);
8828 else
8829 Save_Discr_Constr := No_Elist;
8830 end if;
8832 -- Save the Etype field of Derived_Type. It is correctly set now,
8833 -- but the call to New_Copy tree may remap it to point to itself,
8834 -- which is not what we want. Ditto for the Next_Entity field.
8836 Save_Etype := Etype (Derived_Type);
8837 Save_Next_Entity := Next_Entity (Derived_Type);
8839 -- Assoc_List maps all stored discriminants in the Parent_Base to
8840 -- stored discriminants in the Derived_Type. It is fundamental that
8841 -- no types or itypes with discriminants other than the stored
8842 -- discriminants appear in the entities declared inside
8843 -- Derived_Type, since the back end cannot deal with it.
8845 New_Decl :=
8846 New_Copy_Tree
8847 (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
8849 -- Restore the fields saved prior to the New_Copy_Tree call
8850 -- and compute the stored constraint.
8852 Set_Etype (Derived_Type, Save_Etype);
8853 Set_Next_Entity (Derived_Type, Save_Next_Entity);
8855 if Has_Discriminants (Derived_Type) then
8856 Set_Discriminant_Constraint
8857 (Derived_Type, Save_Discr_Constr);
8858 Set_Stored_Constraint
8859 (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
8860 Replace_Components (Derived_Type, New_Decl);
8861 Set_Has_Implicit_Dereference
8862 (Derived_Type, Has_Implicit_Dereference (Parent_Type));
8863 end if;
8865 -- Insert the new derived type declaration
8867 Rewrite (N, New_Decl);
8869 -- STEP 5b: Complete the processing for record extensions in generics
8871 -- There is no completion for record extensions declared in the
8872 -- parameter part of a generic, so we need to complete processing for
8873 -- these generic record extensions here. The Record_Type_Definition call
8874 -- will change the Ekind of the components from E_Void to E_Component.
8876 elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
8877 Record_Type_Definition (Empty, Derived_Type);
8879 -- STEP 5c: Process the record extension for non private tagged types
8881 elsif not Private_Extension then
8882 Expand_Record_Extension (Derived_Type, Type_Def);
8884 -- Note : previously in ASIS mode we set the Parent_Subtype of the
8885 -- derived type to propagate some semantic information. This led
8886 -- to other ASIS failures and has been removed.
8888 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
8889 -- implemented interfaces if we are in expansion mode
8891 if Expander_Active
8892 and then Has_Interfaces (Derived_Type)
8893 then
8894 Add_Interface_Tag_Components (N, Derived_Type);
8895 end if;
8897 -- Analyze the record extension
8899 Record_Type_Definition
8900 (Record_Extension_Part (Type_Def), Derived_Type);
8901 end if;
8903 End_Scope;
8905 -- Nothing else to do if there is an error in the derivation.
8906 -- An unusual case: the full view may be derived from a type in an
8907 -- instance, when the partial view was used illegally as an actual
8908 -- in that instance, leading to a circular definition.
8910 if Etype (Derived_Type) = Any_Type
8911 or else Etype (Parent_Type) = Derived_Type
8912 then
8913 return;
8914 end if;
8916 -- Set delayed freeze and then derive subprograms, we need to do
8917 -- this in this order so that derived subprograms inherit the
8918 -- derived freeze if necessary.
8920 Set_Has_Delayed_Freeze (Derived_Type);
8922 if Derive_Subps then
8923 Derive_Subprograms (Parent_Type, Derived_Type);
8924 end if;
8926 -- If we have a private extension which defines a constrained derived
8927 -- type mark as constrained here after we have derived subprograms. See
8928 -- comment on point 9. just above the body of Build_Derived_Record_Type.
8930 if Private_Extension and then Inherit_Discrims then
8931 if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
8932 Set_Is_Constrained (Derived_Type, True);
8933 Set_Discriminant_Constraint (Derived_Type, Discs);
8935 elsif Is_Constrained (Parent_Type) then
8936 Set_Is_Constrained
8937 (Derived_Type, True);
8938 Set_Discriminant_Constraint
8939 (Derived_Type, Discriminant_Constraint (Parent_Type));
8940 end if;
8941 end if;
8943 -- Update the class-wide type, which shares the now-completed entity
8944 -- list with its specific type. In case of underlying record views,
8945 -- we do not generate the corresponding class wide entity.
8947 if Is_Tagged
8948 and then not Is_Underlying_Record_View (Derived_Type)
8949 then
8950 Set_First_Entity
8951 (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
8952 Set_Last_Entity
8953 (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
8954 end if;
8956 Check_Function_Writable_Actuals (N);
8957 end Build_Derived_Record_Type;
8959 ------------------------
8960 -- Build_Derived_Type --
8961 ------------------------
8963 procedure Build_Derived_Type
8964 (N : Node_Id;
8965 Parent_Type : Entity_Id;
8966 Derived_Type : Entity_Id;
8967 Is_Completion : Boolean;
8968 Derive_Subps : Boolean := True)
8970 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
8972 begin
8973 -- Set common attributes
8975 Set_Scope (Derived_Type, Current_Scope);
8977 Set_Etype (Derived_Type, Parent_Base);
8978 Set_Ekind (Derived_Type, Ekind (Parent_Base));
8979 Set_Has_Task (Derived_Type, Has_Task (Parent_Base));
8980 Set_Has_Protected (Derived_Type, Has_Protected (Parent_Base));
8982 Set_Size_Info (Derived_Type, Parent_Type);
8983 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
8984 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
8985 Set_Is_Tagged_Type (Derived_Type, Is_Tagged_Type (Parent_Type));
8986 Set_Is_Volatile (Derived_Type, Is_Volatile (Parent_Type));
8988 if Is_Tagged_Type (Derived_Type) then
8989 Set_No_Tagged_Streams_Pragma
8990 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
8991 end if;
8993 -- If the parent has primitive routines, set the derived type link
8995 if Has_Primitive_Operations (Parent_Type) then
8996 Set_Derived_Type_Link (Parent_Base, Derived_Type);
8997 end if;
8999 -- If the parent type is a private subtype, the convention on the base
9000 -- type may be set in the private part, and not propagated to the
9001 -- subtype until later, so we obtain the convention from the base type.
9003 Set_Convention (Derived_Type, Convention (Parent_Base));
9005 -- Set SSO default for record or array type
9007 if (Is_Array_Type (Derived_Type) or else Is_Record_Type (Derived_Type))
9008 and then Is_Base_Type (Derived_Type)
9009 then
9010 Set_Default_SSO (Derived_Type);
9011 end if;
9013 -- Propagate invariant information. The new type has invariants if
9014 -- they are inherited from the parent type, and these invariants can
9015 -- be further inherited, so both flags are set.
9017 -- We similarly inherit predicates
9019 if Has_Predicates (Parent_Type) then
9020 Set_Has_Predicates (Derived_Type);
9021 end if;
9023 -- The derived type inherits the representation clauses of the parent
9025 Inherit_Rep_Item_Chain (Derived_Type, Parent_Type);
9027 -- Propagate the attributes related to pragma Default_Initial_Condition
9028 -- from the parent type to the private extension. A derived type always
9029 -- inherits the default initial condition flag from the parent type. If
9030 -- the derived type carries its own Default_Initial_Condition pragma,
9031 -- the flag is later reset in Analyze_Pragma. Note that both flags are
9032 -- mutually exclusive.
9034 Propagate_Default_Init_Cond_Attributes
9035 (From_Typ => Parent_Type,
9036 To_Typ => Derived_Type,
9037 Parent_To_Derivation => True);
9039 -- If the parent type has delayed rep aspects, then mark the derived
9040 -- type as possibly inheriting a delayed rep aspect.
9042 if Has_Delayed_Rep_Aspects (Parent_Type) then
9043 Set_May_Inherit_Delayed_Rep_Aspects (Derived_Type);
9044 end if;
9046 -- Propagate the attributes related to pragma Ghost from the parent type
9047 -- to the derived type or type extension (SPARK RM 6.9(9)).
9049 if Is_Ghost_Entity (Parent_Type) then
9050 Set_Is_Ghost_Entity (Derived_Type);
9051 end if;
9053 -- Type dependent processing
9055 case Ekind (Parent_Type) is
9056 when Numeric_Kind =>
9057 Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
9059 when Array_Kind =>
9060 Build_Derived_Array_Type (N, Parent_Type, Derived_Type);
9062 when E_Record_Type
9063 | E_Record_Subtype
9064 | Class_Wide_Kind =>
9065 Build_Derived_Record_Type
9066 (N, Parent_Type, Derived_Type, Derive_Subps);
9067 return;
9069 when Enumeration_Kind =>
9070 Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
9072 when Access_Kind =>
9073 Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
9075 when Incomplete_Or_Private_Kind =>
9076 Build_Derived_Private_Type
9077 (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
9079 -- For discriminated types, the derivation includes deriving
9080 -- primitive operations. For others it is done below.
9082 if Is_Tagged_Type (Parent_Type)
9083 or else Has_Discriminants (Parent_Type)
9084 or else (Present (Full_View (Parent_Type))
9085 and then Has_Discriminants (Full_View (Parent_Type)))
9086 then
9087 return;
9088 end if;
9090 when Concurrent_Kind =>
9091 Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
9093 when others =>
9094 raise Program_Error;
9095 end case;
9097 -- Nothing more to do if some error occurred
9099 if Etype (Derived_Type) = Any_Type then
9100 return;
9101 end if;
9103 -- Set delayed freeze and then derive subprograms, we need to do this
9104 -- in this order so that derived subprograms inherit the derived freeze
9105 -- if necessary.
9107 Set_Has_Delayed_Freeze (Derived_Type);
9109 if Derive_Subps then
9110 Derive_Subprograms (Parent_Type, Derived_Type);
9111 end if;
9113 Set_Has_Primitive_Operations
9114 (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
9115 end Build_Derived_Type;
9117 -----------------------
9118 -- Build_Discriminal --
9119 -----------------------
9121 procedure Build_Discriminal (Discrim : Entity_Id) is
9122 D_Minal : Entity_Id;
9123 CR_Disc : Entity_Id;
9125 begin
9126 -- A discriminal has the same name as the discriminant
9128 D_Minal := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9130 Set_Ekind (D_Minal, E_In_Parameter);
9131 Set_Mechanism (D_Minal, Default_Mechanism);
9132 Set_Etype (D_Minal, Etype (Discrim));
9133 Set_Scope (D_Minal, Current_Scope);
9135 Set_Discriminal (Discrim, D_Minal);
9136 Set_Discriminal_Link (D_Minal, Discrim);
9138 -- For task types, build at once the discriminants of the corresponding
9139 -- record, which are needed if discriminants are used in entry defaults
9140 -- and in family bounds.
9142 if Is_Concurrent_Type (Current_Scope)
9143 or else
9144 Is_Limited_Type (Current_Scope)
9145 then
9146 CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9148 Set_Ekind (CR_Disc, E_In_Parameter);
9149 Set_Mechanism (CR_Disc, Default_Mechanism);
9150 Set_Etype (CR_Disc, Etype (Discrim));
9151 Set_Scope (CR_Disc, Current_Scope);
9152 Set_Discriminal_Link (CR_Disc, Discrim);
9153 Set_CR_Discriminant (Discrim, CR_Disc);
9154 end if;
9155 end Build_Discriminal;
9157 ------------------------------------
9158 -- Build_Discriminant_Constraints --
9159 ------------------------------------
9161 function Build_Discriminant_Constraints
9162 (T : Entity_Id;
9163 Def : Node_Id;
9164 Derived_Def : Boolean := False) return Elist_Id
9166 C : constant Node_Id := Constraint (Def);
9167 Nb_Discr : constant Nat := Number_Discriminants (T);
9169 Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
9170 -- Saves the expression corresponding to a given discriminant in T
9172 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
9173 -- Return the Position number within array Discr_Expr of a discriminant
9174 -- D within the discriminant list of the discriminated type T.
9176 procedure Process_Discriminant_Expression
9177 (Expr : Node_Id;
9178 D : Entity_Id);
9179 -- If this is a discriminant constraint on a partial view, do not
9180 -- generate an overflow check on the discriminant expression. The check
9181 -- will be generated when constraining the full view. Otherwise the
9182 -- backend creates duplicate symbols for the temporaries corresponding
9183 -- to the expressions to be checked, causing spurious assembler errors.
9185 ------------------
9186 -- Pos_Of_Discr --
9187 ------------------
9189 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
9190 Disc : Entity_Id;
9192 begin
9193 Disc := First_Discriminant (T);
9194 for J in Discr_Expr'Range loop
9195 if Disc = D then
9196 return J;
9197 end if;
9199 Next_Discriminant (Disc);
9200 end loop;
9202 -- Note: Since this function is called on discriminants that are
9203 -- known to belong to the discriminated type, falling through the
9204 -- loop with no match signals an internal compiler error.
9206 raise Program_Error;
9207 end Pos_Of_Discr;
9209 -------------------------------------
9210 -- Process_Discriminant_Expression --
9211 -------------------------------------
9213 procedure Process_Discriminant_Expression
9214 (Expr : Node_Id;
9215 D : Entity_Id)
9217 BDT : constant Entity_Id := Base_Type (Etype (D));
9219 begin
9220 -- If this is a discriminant constraint on a partial view, do
9221 -- not generate an overflow on the discriminant expression. The
9222 -- check will be generated when constraining the full view.
9224 if Is_Private_Type (T)
9225 and then Present (Full_View (T))
9226 then
9227 Analyze_And_Resolve (Expr, BDT, Suppress => Overflow_Check);
9228 else
9229 Analyze_And_Resolve (Expr, BDT);
9230 end if;
9231 end Process_Discriminant_Expression;
9233 -- Declarations local to Build_Discriminant_Constraints
9235 Discr : Entity_Id;
9236 E : Entity_Id;
9237 Elist : constant Elist_Id := New_Elmt_List;
9239 Constr : Node_Id;
9240 Expr : Node_Id;
9241 Id : Node_Id;
9242 Position : Nat;
9243 Found : Boolean;
9245 Discrim_Present : Boolean := False;
9247 -- Start of processing for Build_Discriminant_Constraints
9249 begin
9250 -- The following loop will process positional associations only.
9251 -- For a positional association, the (single) discriminant is
9252 -- implicitly specified by position, in textual order (RM 3.7.2).
9254 Discr := First_Discriminant (T);
9255 Constr := First (Constraints (C));
9256 for D in Discr_Expr'Range loop
9257 exit when Nkind (Constr) = N_Discriminant_Association;
9259 if No (Constr) then
9260 Error_Msg_N ("too few discriminants given in constraint", C);
9261 return New_Elmt_List;
9263 elsif Nkind (Constr) = N_Range
9264 or else (Nkind (Constr) = N_Attribute_Reference
9265 and then Attribute_Name (Constr) = Name_Range)
9266 then
9267 Error_Msg_N
9268 ("a range is not a valid discriminant constraint", Constr);
9269 Discr_Expr (D) := Error;
9271 else
9272 Process_Discriminant_Expression (Constr, Discr);
9273 Discr_Expr (D) := Constr;
9274 end if;
9276 Next_Discriminant (Discr);
9277 Next (Constr);
9278 end loop;
9280 if No (Discr) and then Present (Constr) then
9281 Error_Msg_N ("too many discriminants given in constraint", Constr);
9282 return New_Elmt_List;
9283 end if;
9285 -- Named associations can be given in any order, but if both positional
9286 -- and named associations are used in the same discriminant constraint,
9287 -- then positional associations must occur first, at their normal
9288 -- position. Hence once a named association is used, the rest of the
9289 -- discriminant constraint must use only named associations.
9291 while Present (Constr) loop
9293 -- Positional association forbidden after a named association
9295 if Nkind (Constr) /= N_Discriminant_Association then
9296 Error_Msg_N ("positional association follows named one", Constr);
9297 return New_Elmt_List;
9299 -- Otherwise it is a named association
9301 else
9302 -- E records the type of the discriminants in the named
9303 -- association. All the discriminants specified in the same name
9304 -- association must have the same type.
9306 E := Empty;
9308 -- Search the list of discriminants in T to see if the simple name
9309 -- given in the constraint matches any of them.
9311 Id := First (Selector_Names (Constr));
9312 while Present (Id) loop
9313 Found := False;
9315 -- If Original_Discriminant is present, we are processing a
9316 -- generic instantiation and this is an instance node. We need
9317 -- to find the name of the corresponding discriminant in the
9318 -- actual record type T and not the name of the discriminant in
9319 -- the generic formal. Example:
9321 -- generic
9322 -- type G (D : int) is private;
9323 -- package P is
9324 -- subtype W is G (D => 1);
9325 -- end package;
9326 -- type Rec (X : int) is record ... end record;
9327 -- package Q is new P (G => Rec);
9329 -- At the point of the instantiation, formal type G is Rec
9330 -- and therefore when reanalyzing "subtype W is G (D => 1);"
9331 -- which really looks like "subtype W is Rec (D => 1);" at
9332 -- the point of instantiation, we want to find the discriminant
9333 -- that corresponds to D in Rec, i.e. X.
9335 if Present (Original_Discriminant (Id))
9336 and then In_Instance
9337 then
9338 Discr := Find_Corresponding_Discriminant (Id, T);
9339 Found := True;
9341 else
9342 Discr := First_Discriminant (T);
9343 while Present (Discr) loop
9344 if Chars (Discr) = Chars (Id) then
9345 Found := True;
9346 exit;
9347 end if;
9349 Next_Discriminant (Discr);
9350 end loop;
9352 if not Found then
9353 Error_Msg_N ("& does not match any discriminant", Id);
9354 return New_Elmt_List;
9356 -- If the parent type is a generic formal, preserve the
9357 -- name of the discriminant for subsequent instances.
9358 -- see comment at the beginning of this if statement.
9360 elsif Is_Generic_Type (Root_Type (T)) then
9361 Set_Original_Discriminant (Id, Discr);
9362 end if;
9363 end if;
9365 Position := Pos_Of_Discr (T, Discr);
9367 if Present (Discr_Expr (Position)) then
9368 Error_Msg_N ("duplicate constraint for discriminant&", Id);
9370 else
9371 -- Each discriminant specified in the same named association
9372 -- must be associated with a separate copy of the
9373 -- corresponding expression.
9375 if Present (Next (Id)) then
9376 Expr := New_Copy_Tree (Expression (Constr));
9377 Set_Parent (Expr, Parent (Expression (Constr)));
9378 else
9379 Expr := Expression (Constr);
9380 end if;
9382 Discr_Expr (Position) := Expr;
9383 Process_Discriminant_Expression (Expr, Discr);
9384 end if;
9386 -- A discriminant association with more than one discriminant
9387 -- name is only allowed if the named discriminants are all of
9388 -- the same type (RM 3.7.1(8)).
9390 if E = Empty then
9391 E := Base_Type (Etype (Discr));
9393 elsif Base_Type (Etype (Discr)) /= E then
9394 Error_Msg_N
9395 ("all discriminants in an association " &
9396 "must have the same type", Id);
9397 end if;
9399 Next (Id);
9400 end loop;
9401 end if;
9403 Next (Constr);
9404 end loop;
9406 -- A discriminant constraint must provide exactly one value for each
9407 -- discriminant of the type (RM 3.7.1(8)).
9409 for J in Discr_Expr'Range loop
9410 if No (Discr_Expr (J)) then
9411 Error_Msg_N ("too few discriminants given in constraint", C);
9412 return New_Elmt_List;
9413 end if;
9414 end loop;
9416 -- Determine if there are discriminant expressions in the constraint
9418 for J in Discr_Expr'Range loop
9419 if Denotes_Discriminant
9420 (Discr_Expr (J), Check_Concurrent => True)
9421 then
9422 Discrim_Present := True;
9423 end if;
9424 end loop;
9426 -- Build an element list consisting of the expressions given in the
9427 -- discriminant constraint and apply the appropriate checks. The list
9428 -- is constructed after resolving any named discriminant associations
9429 -- and therefore the expressions appear in the textual order of the
9430 -- discriminants.
9432 Discr := First_Discriminant (T);
9433 for J in Discr_Expr'Range loop
9434 if Discr_Expr (J) /= Error then
9435 Append_Elmt (Discr_Expr (J), Elist);
9437 -- If any of the discriminant constraints is given by a
9438 -- discriminant and we are in a derived type declaration we
9439 -- have a discriminant renaming. Establish link between new
9440 -- and old discriminant.
9442 if Denotes_Discriminant (Discr_Expr (J)) then
9443 if Derived_Def then
9444 Set_Corresponding_Discriminant
9445 (Entity (Discr_Expr (J)), Discr);
9446 end if;
9448 -- Force the evaluation of non-discriminant expressions.
9449 -- If we have found a discriminant in the constraint 3.4(26)
9450 -- and 3.8(18) demand that no range checks are performed are
9451 -- after evaluation. If the constraint is for a component
9452 -- definition that has a per-object constraint, expressions are
9453 -- evaluated but not checked either. In all other cases perform
9454 -- a range check.
9456 else
9457 if Discrim_Present then
9458 null;
9460 elsif Nkind (Parent (Parent (Def))) = N_Component_Declaration
9461 and then
9462 Has_Per_Object_Constraint
9463 (Defining_Identifier (Parent (Parent (Def))))
9464 then
9465 null;
9467 elsif Is_Access_Type (Etype (Discr)) then
9468 Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
9470 else
9471 Apply_Range_Check (Discr_Expr (J), Etype (Discr));
9472 end if;
9474 Force_Evaluation (Discr_Expr (J));
9475 end if;
9477 -- Check that the designated type of an access discriminant's
9478 -- expression is not a class-wide type unless the discriminant's
9479 -- designated type is also class-wide.
9481 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
9482 and then not Is_Class_Wide_Type
9483 (Designated_Type (Etype (Discr)))
9484 and then Etype (Discr_Expr (J)) /= Any_Type
9485 and then Is_Class_Wide_Type
9486 (Designated_Type (Etype (Discr_Expr (J))))
9487 then
9488 Wrong_Type (Discr_Expr (J), Etype (Discr));
9490 elsif Is_Access_Type (Etype (Discr))
9491 and then not Is_Access_Constant (Etype (Discr))
9492 and then Is_Access_Type (Etype (Discr_Expr (J)))
9493 and then Is_Access_Constant (Etype (Discr_Expr (J)))
9494 then
9495 Error_Msg_NE
9496 ("constraint for discriminant& must be access to variable",
9497 Def, Discr);
9498 end if;
9499 end if;
9501 Next_Discriminant (Discr);
9502 end loop;
9504 return Elist;
9505 end Build_Discriminant_Constraints;
9507 ---------------------------------
9508 -- Build_Discriminated_Subtype --
9509 ---------------------------------
9511 procedure Build_Discriminated_Subtype
9512 (T : Entity_Id;
9513 Def_Id : Entity_Id;
9514 Elist : Elist_Id;
9515 Related_Nod : Node_Id;
9516 For_Access : Boolean := False)
9518 Has_Discrs : constant Boolean := Has_Discriminants (T);
9519 Constrained : constant Boolean :=
9520 (Has_Discrs
9521 and then not Is_Empty_Elmt_List (Elist)
9522 and then not Is_Class_Wide_Type (T))
9523 or else Is_Constrained (T);
9525 begin
9526 if Ekind (T) = E_Record_Type then
9527 if For_Access then
9528 Set_Ekind (Def_Id, E_Private_Subtype);
9529 Set_Is_For_Access_Subtype (Def_Id, True);
9530 else
9531 Set_Ekind (Def_Id, E_Record_Subtype);
9532 end if;
9534 -- Inherit preelaboration flag from base, for types for which it
9535 -- may have been set: records, private types, protected types.
9537 Set_Known_To_Have_Preelab_Init
9538 (Def_Id, Known_To_Have_Preelab_Init (T));
9540 elsif Ekind (T) = E_Task_Type then
9541 Set_Ekind (Def_Id, E_Task_Subtype);
9543 elsif Ekind (T) = E_Protected_Type then
9544 Set_Ekind (Def_Id, E_Protected_Subtype);
9545 Set_Known_To_Have_Preelab_Init
9546 (Def_Id, Known_To_Have_Preelab_Init (T));
9548 elsif Is_Private_Type (T) then
9549 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
9550 Set_Known_To_Have_Preelab_Init
9551 (Def_Id, Known_To_Have_Preelab_Init (T));
9553 -- Private subtypes may have private dependents
9555 Set_Private_Dependents (Def_Id, New_Elmt_List);
9557 elsif Is_Class_Wide_Type (T) then
9558 Set_Ekind (Def_Id, E_Class_Wide_Subtype);
9560 else
9561 -- Incomplete type. Attach subtype to list of dependents, to be
9562 -- completed with full view of parent type, unless is it the
9563 -- designated subtype of a record component within an init_proc.
9564 -- This last case arises for a component of an access type whose
9565 -- designated type is incomplete (e.g. a Taft Amendment type).
9566 -- The designated subtype is within an inner scope, and needs no
9567 -- elaboration, because only the access type is needed in the
9568 -- initialization procedure.
9570 Set_Ekind (Def_Id, Ekind (T));
9572 if For_Access and then Within_Init_Proc then
9573 null;
9574 else
9575 Append_Elmt (Def_Id, Private_Dependents (T));
9576 end if;
9577 end if;
9579 Set_Etype (Def_Id, T);
9580 Init_Size_Align (Def_Id);
9581 Set_Has_Discriminants (Def_Id, Has_Discrs);
9582 Set_Is_Constrained (Def_Id, Constrained);
9584 Set_First_Entity (Def_Id, First_Entity (T));
9585 Set_Last_Entity (Def_Id, Last_Entity (T));
9586 Set_Has_Implicit_Dereference
9587 (Def_Id, Has_Implicit_Dereference (T));
9589 -- If the subtype is the completion of a private declaration, there may
9590 -- have been representation clauses for the partial view, and they must
9591 -- be preserved. Build_Derived_Type chains the inherited clauses with
9592 -- the ones appearing on the extension. If this comes from a subtype
9593 -- declaration, all clauses are inherited.
9595 if No (First_Rep_Item (Def_Id)) then
9596 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
9597 end if;
9599 if Is_Tagged_Type (T) then
9600 Set_Is_Tagged_Type (Def_Id);
9601 Set_No_Tagged_Streams_Pragma (Def_Id, No_Tagged_Streams_Pragma (T));
9602 Make_Class_Wide_Type (Def_Id);
9603 end if;
9605 Set_Stored_Constraint (Def_Id, No_Elist);
9607 if Has_Discrs then
9608 Set_Discriminant_Constraint (Def_Id, Elist);
9609 Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
9610 end if;
9612 if Is_Tagged_Type (T) then
9614 -- Ada 2005 (AI-251): In case of concurrent types we inherit the
9615 -- concurrent record type (which has the list of primitive
9616 -- operations).
9618 if Ada_Version >= Ada_2005
9619 and then Is_Concurrent_Type (T)
9620 then
9621 Set_Corresponding_Record_Type (Def_Id,
9622 Corresponding_Record_Type (T));
9623 else
9624 Set_Direct_Primitive_Operations (Def_Id,
9625 Direct_Primitive_Operations (T));
9626 end if;
9628 Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
9629 end if;
9631 -- Subtypes introduced by component declarations do not need to be
9632 -- marked as delayed, and do not get freeze nodes, because the semantics
9633 -- verifies that the parents of the subtypes are frozen before the
9634 -- enclosing record is frozen.
9636 if not Is_Type (Scope (Def_Id)) then
9637 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
9639 if Is_Private_Type (T)
9640 and then Present (Full_View (T))
9641 then
9642 Conditional_Delay (Def_Id, Full_View (T));
9643 else
9644 Conditional_Delay (Def_Id, T);
9645 end if;
9646 end if;
9648 if Is_Record_Type (T) then
9649 Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
9651 if Has_Discrs
9652 and then not Is_Empty_Elmt_List (Elist)
9653 and then not For_Access
9654 then
9655 Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
9656 elsif not For_Access then
9657 Set_Cloned_Subtype (Def_Id, T);
9658 end if;
9659 end if;
9660 end Build_Discriminated_Subtype;
9662 ---------------------------
9663 -- Build_Itype_Reference --
9664 ---------------------------
9666 procedure Build_Itype_Reference
9667 (Ityp : Entity_Id;
9668 Nod : Node_Id)
9670 IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
9671 begin
9673 -- Itype references are only created for use by the back-end
9675 if Inside_A_Generic then
9676 return;
9677 else
9678 Set_Itype (IR, Ityp);
9679 Insert_After (Nod, IR);
9680 end if;
9681 end Build_Itype_Reference;
9683 ------------------------
9684 -- Build_Scalar_Bound --
9685 ------------------------
9687 function Build_Scalar_Bound
9688 (Bound : Node_Id;
9689 Par_T : Entity_Id;
9690 Der_T : Entity_Id) return Node_Id
9692 New_Bound : Entity_Id;
9694 begin
9695 -- Note: not clear why this is needed, how can the original bound
9696 -- be unanalyzed at this point? and if it is, what business do we
9697 -- have messing around with it? and why is the base type of the
9698 -- parent type the right type for the resolution. It probably is
9699 -- not. It is OK for the new bound we are creating, but not for
9700 -- the old one??? Still if it never happens, no problem.
9702 Analyze_And_Resolve (Bound, Base_Type (Par_T));
9704 if Nkind_In (Bound, N_Integer_Literal, N_Real_Literal) then
9705 New_Bound := New_Copy (Bound);
9706 Set_Etype (New_Bound, Der_T);
9707 Set_Analyzed (New_Bound);
9709 elsif Is_Entity_Name (Bound) then
9710 New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
9712 -- The following is almost certainly wrong. What business do we have
9713 -- relocating a node (Bound) that is presumably still attached to
9714 -- the tree elsewhere???
9716 else
9717 New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
9718 end if;
9720 Set_Etype (New_Bound, Der_T);
9721 return New_Bound;
9722 end Build_Scalar_Bound;
9724 --------------------------------
9725 -- Build_Underlying_Full_View --
9726 --------------------------------
9728 procedure Build_Underlying_Full_View
9729 (N : Node_Id;
9730 Typ : Entity_Id;
9731 Par : Entity_Id)
9733 Loc : constant Source_Ptr := Sloc (N);
9734 Subt : constant Entity_Id :=
9735 Make_Defining_Identifier
9736 (Loc, New_External_Name (Chars (Typ), 'S'));
9738 Constr : Node_Id;
9739 Indic : Node_Id;
9740 C : Node_Id;
9741 Id : Node_Id;
9743 procedure Set_Discriminant_Name (Id : Node_Id);
9744 -- If the derived type has discriminants, they may rename discriminants
9745 -- of the parent. When building the full view of the parent, we need to
9746 -- recover the names of the original discriminants if the constraint is
9747 -- given by named associations.
9749 ---------------------------
9750 -- Set_Discriminant_Name --
9751 ---------------------------
9753 procedure Set_Discriminant_Name (Id : Node_Id) is
9754 Disc : Entity_Id;
9756 begin
9757 Set_Original_Discriminant (Id, Empty);
9759 if Has_Discriminants (Typ) then
9760 Disc := First_Discriminant (Typ);
9761 while Present (Disc) loop
9762 if Chars (Disc) = Chars (Id)
9763 and then Present (Corresponding_Discriminant (Disc))
9764 then
9765 Set_Chars (Id, Chars (Corresponding_Discriminant (Disc)));
9766 end if;
9767 Next_Discriminant (Disc);
9768 end loop;
9769 end if;
9770 end Set_Discriminant_Name;
9772 -- Start of processing for Build_Underlying_Full_View
9774 begin
9775 if Nkind (N) = N_Full_Type_Declaration then
9776 Constr := Constraint (Subtype_Indication (Type_Definition (N)));
9778 elsif Nkind (N) = N_Subtype_Declaration then
9779 Constr := New_Copy_Tree (Constraint (Subtype_Indication (N)));
9781 elsif Nkind (N) = N_Component_Declaration then
9782 Constr :=
9783 New_Copy_Tree
9784 (Constraint (Subtype_Indication (Component_Definition (N))));
9786 else
9787 raise Program_Error;
9788 end if;
9790 C := First (Constraints (Constr));
9791 while Present (C) loop
9792 if Nkind (C) = N_Discriminant_Association then
9793 Id := First (Selector_Names (C));
9794 while Present (Id) loop
9795 Set_Discriminant_Name (Id);
9796 Next (Id);
9797 end loop;
9798 end if;
9800 Next (C);
9801 end loop;
9803 Indic :=
9804 Make_Subtype_Declaration (Loc,
9805 Defining_Identifier => Subt,
9806 Subtype_Indication =>
9807 Make_Subtype_Indication (Loc,
9808 Subtype_Mark => New_Occurrence_Of (Par, Loc),
9809 Constraint => New_Copy_Tree (Constr)));
9811 -- If this is a component subtype for an outer itype, it is not
9812 -- a list member, so simply set the parent link for analysis: if
9813 -- the enclosing type does not need to be in a declarative list,
9814 -- neither do the components.
9816 if Is_List_Member (N)
9817 and then Nkind (N) /= N_Component_Declaration
9818 then
9819 Insert_Before (N, Indic);
9820 else
9821 Set_Parent (Indic, Parent (N));
9822 end if;
9824 Analyze (Indic);
9825 Set_Underlying_Full_View (Typ, Full_View (Subt));
9826 end Build_Underlying_Full_View;
9828 -------------------------------
9829 -- Check_Abstract_Overriding --
9830 -------------------------------
9832 procedure Check_Abstract_Overriding (T : Entity_Id) is
9833 Alias_Subp : Entity_Id;
9834 Elmt : Elmt_Id;
9835 Op_List : Elist_Id;
9836 Subp : Entity_Id;
9837 Type_Def : Node_Id;
9839 procedure Check_Pragma_Implemented (Subp : Entity_Id);
9840 -- Ada 2012 (AI05-0030): Subprogram Subp overrides an interface routine
9841 -- which has pragma Implemented already set. Check whether Subp's entity
9842 -- kind conforms to the implementation kind of the overridden routine.
9844 procedure Check_Pragma_Implemented
9845 (Subp : Entity_Id;
9846 Iface_Subp : Entity_Id);
9847 -- Ada 2012 (AI05-0030): Subprogram Subp overrides interface routine
9848 -- Iface_Subp and both entities have pragma Implemented already set on
9849 -- them. Check whether the two implementation kinds are conforming.
9851 procedure Inherit_Pragma_Implemented
9852 (Subp : Entity_Id;
9853 Iface_Subp : Entity_Id);
9854 -- Ada 2012 (AI05-0030): Interface primitive Subp overrides interface
9855 -- subprogram Iface_Subp which has been marked by pragma Implemented.
9856 -- Propagate the implementation kind of Iface_Subp to Subp.
9858 ------------------------------
9859 -- Check_Pragma_Implemented --
9860 ------------------------------
9862 procedure Check_Pragma_Implemented (Subp : Entity_Id) is
9863 Iface_Alias : constant Entity_Id := Interface_Alias (Subp);
9864 Impl_Kind : constant Name_Id := Implementation_Kind (Iface_Alias);
9865 Subp_Alias : constant Entity_Id := Alias (Subp);
9866 Contr_Typ : Entity_Id;
9867 Impl_Subp : Entity_Id;
9869 begin
9870 -- Subp must have an alias since it is a hidden entity used to link
9871 -- an interface subprogram to its overriding counterpart.
9873 pragma Assert (Present (Subp_Alias));
9875 -- Handle aliases to synchronized wrappers
9877 Impl_Subp := Subp_Alias;
9879 if Is_Primitive_Wrapper (Impl_Subp) then
9880 Impl_Subp := Wrapped_Entity (Impl_Subp);
9881 end if;
9883 -- Extract the type of the controlling formal
9885 Contr_Typ := Etype (First_Formal (Subp_Alias));
9887 if Is_Concurrent_Record_Type (Contr_Typ) then
9888 Contr_Typ := Corresponding_Concurrent_Type (Contr_Typ);
9889 end if;
9891 -- An interface subprogram whose implementation kind is By_Entry must
9892 -- be implemented by an entry.
9894 if Impl_Kind = Name_By_Entry
9895 and then Ekind (Impl_Subp) /= E_Entry
9896 then
9897 Error_Msg_Node_2 := Iface_Alias;
9898 Error_Msg_NE
9899 ("type & must implement abstract subprogram & with an entry",
9900 Subp_Alias, Contr_Typ);
9902 elsif Impl_Kind = Name_By_Protected_Procedure then
9904 -- An interface subprogram whose implementation kind is By_
9905 -- Protected_Procedure cannot be implemented by a primitive
9906 -- procedure of a task type.
9908 if Ekind (Contr_Typ) /= E_Protected_Type then
9909 Error_Msg_Node_2 := Contr_Typ;
9910 Error_Msg_NE
9911 ("interface subprogram & cannot be implemented by a " &
9912 "primitive procedure of task type &", Subp_Alias,
9913 Iface_Alias);
9915 -- An interface subprogram whose implementation kind is By_
9916 -- Protected_Procedure must be implemented by a procedure.
9918 elsif Ekind (Impl_Subp) /= E_Procedure then
9919 Error_Msg_Node_2 := Iface_Alias;
9920 Error_Msg_NE
9921 ("type & must implement abstract subprogram & with a " &
9922 "procedure", Subp_Alias, Contr_Typ);
9924 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
9925 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
9926 then
9927 Error_Msg_Name_1 := Impl_Kind;
9928 Error_Msg_N
9929 ("overriding operation& must have synchronization%",
9930 Subp_Alias);
9931 end if;
9933 -- If primitive has Optional synchronization, overriding operation
9934 -- must match if it has an explicit synchronization..
9936 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
9937 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
9938 then
9939 Error_Msg_Name_1 := Impl_Kind;
9940 Error_Msg_N
9941 ("overriding operation& must have syncrhonization%",
9942 Subp_Alias);
9943 end if;
9944 end Check_Pragma_Implemented;
9946 ------------------------------
9947 -- Check_Pragma_Implemented --
9948 ------------------------------
9950 procedure Check_Pragma_Implemented
9951 (Subp : Entity_Id;
9952 Iface_Subp : Entity_Id)
9954 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
9955 Subp_Kind : constant Name_Id := Implementation_Kind (Subp);
9957 begin
9958 -- Ada 2012 (AI05-0030): The implementation kinds of an overridden
9959 -- and overriding subprogram are different. In general this is an
9960 -- error except when the implementation kind of the overridden
9961 -- subprograms is By_Any or Optional.
9963 if Iface_Kind /= Subp_Kind
9964 and then Iface_Kind /= Name_By_Any
9965 and then Iface_Kind /= Name_Optional
9966 then
9967 if Iface_Kind = Name_By_Entry then
9968 Error_Msg_N
9969 ("incompatible implementation kind, overridden subprogram " &
9970 "is marked By_Entry", Subp);
9971 else
9972 Error_Msg_N
9973 ("incompatible implementation kind, overridden subprogram " &
9974 "is marked By_Protected_Procedure", Subp);
9975 end if;
9976 end if;
9977 end Check_Pragma_Implemented;
9979 --------------------------------
9980 -- Inherit_Pragma_Implemented --
9981 --------------------------------
9983 procedure Inherit_Pragma_Implemented
9984 (Subp : Entity_Id;
9985 Iface_Subp : Entity_Id)
9987 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
9988 Loc : constant Source_Ptr := Sloc (Subp);
9989 Impl_Prag : Node_Id;
9991 begin
9992 -- Since the implementation kind is stored as a representation item
9993 -- rather than a flag, create a pragma node.
9995 Impl_Prag :=
9996 Make_Pragma (Loc,
9997 Chars => Name_Implemented,
9998 Pragma_Argument_Associations => New_List (
9999 Make_Pragma_Argument_Association (Loc,
10000 Expression => New_Occurrence_Of (Subp, Loc)),
10002 Make_Pragma_Argument_Association (Loc,
10003 Expression => Make_Identifier (Loc, Iface_Kind))));
10005 -- The pragma doesn't need to be analyzed because it is internally
10006 -- built. It is safe to directly register it as a rep item since we
10007 -- are only interested in the characters of the implementation kind.
10009 Record_Rep_Item (Subp, Impl_Prag);
10010 end Inherit_Pragma_Implemented;
10012 -- Start of processing for Check_Abstract_Overriding
10014 begin
10015 Op_List := Primitive_Operations (T);
10017 -- Loop to check primitive operations
10019 Elmt := First_Elmt (Op_List);
10020 while Present (Elmt) loop
10021 Subp := Node (Elmt);
10022 Alias_Subp := Alias (Subp);
10024 -- Inherited subprograms are identified by the fact that they do not
10025 -- come from source, and the associated source location is the
10026 -- location of the first subtype of the derived type.
10028 -- Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
10029 -- subprograms that "require overriding".
10031 -- Special exception, do not complain about failure to override the
10032 -- stream routines _Input and _Output, as well as the primitive
10033 -- operations used in dispatching selects since we always provide
10034 -- automatic overridings for these subprograms.
10036 -- Also ignore this rule for convention CIL since .NET libraries
10037 -- do bizarre things with interfaces???
10039 -- The partial view of T may have been a private extension, for
10040 -- which inherited functions dispatching on result are abstract.
10041 -- If the full view is a null extension, there is no need for
10042 -- overriding in Ada 2005, but wrappers need to be built for them
10043 -- (see exp_ch3, Build_Controlling_Function_Wrappers).
10045 if Is_Null_Extension (T)
10046 and then Has_Controlling_Result (Subp)
10047 and then Ada_Version >= Ada_2005
10048 and then Present (Alias_Subp)
10049 and then not Comes_From_Source (Subp)
10050 and then not Is_Abstract_Subprogram (Alias_Subp)
10051 and then not Is_Access_Type (Etype (Subp))
10052 then
10053 null;
10055 -- Ada 2005 (AI-251): Internal entities of interfaces need no
10056 -- processing because this check is done with the aliased
10057 -- entity
10059 elsif Present (Interface_Alias (Subp)) then
10060 null;
10062 elsif (Is_Abstract_Subprogram (Subp)
10063 or else Requires_Overriding (Subp)
10064 or else
10065 (Has_Controlling_Result (Subp)
10066 and then Present (Alias_Subp)
10067 and then not Comes_From_Source (Subp)
10068 and then Sloc (Subp) = Sloc (First_Subtype (T))))
10069 and then not Is_TSS (Subp, TSS_Stream_Input)
10070 and then not Is_TSS (Subp, TSS_Stream_Output)
10071 and then not Is_Abstract_Type (T)
10072 and then Convention (T) /= Convention_CIL
10073 and then not Is_Predefined_Interface_Primitive (Subp)
10075 -- Ada 2005 (AI-251): Do not consider hidden entities associated
10076 -- with abstract interface types because the check will be done
10077 -- with the aliased entity (otherwise we generate a duplicated
10078 -- error message).
10080 and then not Present (Interface_Alias (Subp))
10081 then
10082 if Present (Alias_Subp) then
10084 -- Only perform the check for a derived subprogram when the
10085 -- type has an explicit record extension. This avoids incorrect
10086 -- flagging of abstract subprograms for the case of a type
10087 -- without an extension that is derived from a formal type
10088 -- with a tagged actual (can occur within a private part).
10090 -- Ada 2005 (AI-391): In the case of an inherited function with
10091 -- a controlling result of the type, the rule does not apply if
10092 -- the type is a null extension (unless the parent function
10093 -- itself is abstract, in which case the function must still be
10094 -- be overridden). The expander will generate an overriding
10095 -- wrapper function calling the parent subprogram (see
10096 -- Exp_Ch3.Make_Controlling_Wrapper_Functions).
10098 Type_Def := Type_Definition (Parent (T));
10100 if Nkind (Type_Def) = N_Derived_Type_Definition
10101 and then Present (Record_Extension_Part (Type_Def))
10102 and then
10103 (Ada_Version < Ada_2005
10104 or else not Is_Null_Extension (T)
10105 or else Ekind (Subp) = E_Procedure
10106 or else not Has_Controlling_Result (Subp)
10107 or else Is_Abstract_Subprogram (Alias_Subp)
10108 or else Requires_Overriding (Subp)
10109 or else Is_Access_Type (Etype (Subp)))
10110 then
10111 -- Avoid reporting error in case of abstract predefined
10112 -- primitive inherited from interface type because the
10113 -- body of internally generated predefined primitives
10114 -- of tagged types are generated later by Freeze_Type
10116 if Is_Interface (Root_Type (T))
10117 and then Is_Abstract_Subprogram (Subp)
10118 and then Is_Predefined_Dispatching_Operation (Subp)
10119 and then not Comes_From_Source (Ultimate_Alias (Subp))
10120 then
10121 null;
10123 -- A null extension is not obliged to override an inherited
10124 -- procedure subject to pragma Extensions_Visible with value
10125 -- False and at least one controlling OUT parameter
10126 -- (SPARK RM 6.1.7(6)).
10128 elsif Is_Null_Extension (T)
10129 and then Is_EVF_Procedure (Subp)
10130 then
10131 null;
10133 else
10134 Error_Msg_NE
10135 ("type must be declared abstract or & overridden",
10136 T, Subp);
10138 -- Traverse the whole chain of aliased subprograms to
10139 -- complete the error notification. This is especially
10140 -- useful for traceability of the chain of entities when
10141 -- the subprogram corresponds with an interface
10142 -- subprogram (which may be defined in another package).
10144 if Present (Alias_Subp) then
10145 declare
10146 E : Entity_Id;
10148 begin
10149 E := Subp;
10150 while Present (Alias (E)) loop
10152 -- Avoid reporting redundant errors on entities
10153 -- inherited from interfaces
10155 if Sloc (E) /= Sloc (T) then
10156 Error_Msg_Sloc := Sloc (E);
10157 Error_Msg_NE
10158 ("\& has been inherited #", T, Subp);
10159 end if;
10161 E := Alias (E);
10162 end loop;
10164 Error_Msg_Sloc := Sloc (E);
10166 -- AI05-0068: report if there is an overriding
10167 -- non-abstract subprogram that is invisible.
10169 if Is_Hidden (E)
10170 and then not Is_Abstract_Subprogram (E)
10171 then
10172 Error_Msg_NE
10173 ("\& subprogram# is not visible",
10174 T, Subp);
10176 -- Clarify the case where a non-null extension must
10177 -- override inherited procedure subject to pragma
10178 -- Extensions_Visible with value False and at least
10179 -- one controlling OUT param.
10181 elsif Is_EVF_Procedure (E) then
10182 Error_Msg_NE
10183 ("\& # is subject to Extensions_Visible False",
10184 T, Subp);
10186 else
10187 Error_Msg_NE
10188 ("\& has been inherited from subprogram #",
10189 T, Subp);
10190 end if;
10191 end;
10192 end if;
10193 end if;
10195 -- Ada 2005 (AI-345): Protected or task type implementing
10196 -- abstract interfaces.
10198 elsif Is_Concurrent_Record_Type (T)
10199 and then Present (Interfaces (T))
10200 then
10201 -- There is no need to check here RM 9.4(11.9/3) since we
10202 -- are processing the corresponding record type and the
10203 -- mode of the overriding subprograms was verified by
10204 -- Check_Conformance when the corresponding concurrent
10205 -- type declaration was analyzed.
10207 Error_Msg_NE
10208 ("interface subprogram & must be overridden", T, Subp);
10210 -- Examine primitive operations of synchronized type to find
10211 -- homonyms that have the wrong profile.
10213 declare
10214 Prim : Entity_Id;
10216 begin
10217 Prim := First_Entity (Corresponding_Concurrent_Type (T));
10218 while Present (Prim) loop
10219 if Chars (Prim) = Chars (Subp) then
10220 Error_Msg_NE
10221 ("profile is not type conformant with prefixed "
10222 & "view profile of inherited operation&",
10223 Prim, Subp);
10224 end if;
10226 Next_Entity (Prim);
10227 end loop;
10228 end;
10229 end if;
10231 else
10232 Error_Msg_Node_2 := T;
10233 Error_Msg_N
10234 ("abstract subprogram& not allowed for type&", Subp);
10236 -- Also post unconditional warning on the type (unconditional
10237 -- so that if there are more than one of these cases, we get
10238 -- them all, and not just the first one).
10240 Error_Msg_Node_2 := Subp;
10241 Error_Msg_N ("nonabstract type& has abstract subprogram&!", T);
10242 end if;
10244 -- A subprogram subject to pragma Extensions_Visible with value
10245 -- "True" cannot override a subprogram subject to the same pragma
10246 -- with value "False" (SPARK RM 6.1.7(5)).
10248 elsif Extensions_Visible_Status (Subp) = Extensions_Visible_True
10249 and then Present (Overridden_Operation (Subp))
10250 and then Extensions_Visible_Status (Overridden_Operation (Subp)) =
10251 Extensions_Visible_False
10252 then
10253 Error_Msg_Sloc := Sloc (Overridden_Operation (Subp));
10254 Error_Msg_N
10255 ("subprogram & with Extensions_Visible True cannot override "
10256 & "subprogram # with Extensions_Visible False", Subp);
10257 end if;
10259 -- Ada 2012 (AI05-0030): Perform checks related to pragma Implemented
10261 -- Subp is an expander-generated procedure which maps an interface
10262 -- alias to a protected wrapper. The interface alias is flagged by
10263 -- pragma Implemented. Ensure that Subp is a procedure when the
10264 -- implementation kind is By_Protected_Procedure or an entry when
10265 -- By_Entry.
10267 if Ada_Version >= Ada_2012
10268 and then Is_Hidden (Subp)
10269 and then Present (Interface_Alias (Subp))
10270 and then Has_Rep_Pragma (Interface_Alias (Subp), Name_Implemented)
10271 then
10272 Check_Pragma_Implemented (Subp);
10273 end if;
10275 -- Subp is an interface primitive which overrides another interface
10276 -- primitive marked with pragma Implemented.
10278 if Ada_Version >= Ada_2012
10279 and then Present (Overridden_Operation (Subp))
10280 and then Has_Rep_Pragma
10281 (Overridden_Operation (Subp), Name_Implemented)
10282 then
10283 -- If the overriding routine is also marked by Implemented, check
10284 -- that the two implementation kinds are conforming.
10286 if Has_Rep_Pragma (Subp, Name_Implemented) then
10287 Check_Pragma_Implemented
10288 (Subp => Subp,
10289 Iface_Subp => Overridden_Operation (Subp));
10291 -- Otherwise the overriding routine inherits the implementation
10292 -- kind from the overridden subprogram.
10294 else
10295 Inherit_Pragma_Implemented
10296 (Subp => Subp,
10297 Iface_Subp => Overridden_Operation (Subp));
10298 end if;
10299 end if;
10301 -- If the operation is a wrapper for a synchronized primitive, it
10302 -- may be called indirectly through a dispatching select. We assume
10303 -- that it will be referenced elsewhere indirectly, and suppress
10304 -- warnings about an unused entity.
10306 if Is_Primitive_Wrapper (Subp)
10307 and then Present (Wrapped_Entity (Subp))
10308 then
10309 Set_Referenced (Wrapped_Entity (Subp));
10310 end if;
10312 Next_Elmt (Elmt);
10313 end loop;
10314 end Check_Abstract_Overriding;
10316 ------------------------------------------------
10317 -- Check_Access_Discriminant_Requires_Limited --
10318 ------------------------------------------------
10320 procedure Check_Access_Discriminant_Requires_Limited
10321 (D : Node_Id;
10322 Loc : Node_Id)
10324 begin
10325 -- A discriminant_specification for an access discriminant shall appear
10326 -- only in the declaration for a task or protected type, or for a type
10327 -- with the reserved word 'limited' in its definition or in one of its
10328 -- ancestors (RM 3.7(10)).
10330 -- AI-0063: The proper condition is that type must be immutably limited,
10331 -- or else be a partial view.
10333 if Nkind (Discriminant_Type (D)) = N_Access_Definition then
10334 if Is_Limited_View (Current_Scope)
10335 or else
10336 (Nkind (Parent (Current_Scope)) = N_Private_Type_Declaration
10337 and then Limited_Present (Parent (Current_Scope)))
10338 then
10339 null;
10341 else
10342 Error_Msg_N
10343 ("access discriminants allowed only for limited types", Loc);
10344 end if;
10345 end if;
10346 end Check_Access_Discriminant_Requires_Limited;
10348 -----------------------------------
10349 -- Check_Aliased_Component_Types --
10350 -----------------------------------
10352 procedure Check_Aliased_Component_Types (T : Entity_Id) is
10353 C : Entity_Id;
10355 begin
10356 -- ??? Also need to check components of record extensions, but not
10357 -- components of protected types (which are always limited).
10359 -- Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
10360 -- types to be unconstrained. This is safe because it is illegal to
10361 -- create access subtypes to such types with explicit discriminant
10362 -- constraints.
10364 if not Is_Limited_Type (T) then
10365 if Ekind (T) = E_Record_Type then
10366 C := First_Component (T);
10367 while Present (C) loop
10368 if Is_Aliased (C)
10369 and then Has_Discriminants (Etype (C))
10370 and then not Is_Constrained (Etype (C))
10371 and then not In_Instance_Body
10372 and then Ada_Version < Ada_2005
10373 then
10374 Error_Msg_N
10375 ("aliased component must be constrained (RM 3.6(11))",
10377 end if;
10379 Next_Component (C);
10380 end loop;
10382 elsif Ekind (T) = E_Array_Type then
10383 if Has_Aliased_Components (T)
10384 and then Has_Discriminants (Component_Type (T))
10385 and then not Is_Constrained (Component_Type (T))
10386 and then not In_Instance_Body
10387 and then Ada_Version < Ada_2005
10388 then
10389 Error_Msg_N
10390 ("aliased component type must be constrained (RM 3.6(11))",
10392 end if;
10393 end if;
10394 end if;
10395 end Check_Aliased_Component_Types;
10397 ---------------------------------------
10398 -- Check_Anonymous_Access_Components --
10399 ---------------------------------------
10401 procedure Check_Anonymous_Access_Components
10402 (Typ_Decl : Node_Id;
10403 Typ : Entity_Id;
10404 Prev : Entity_Id;
10405 Comp_List : Node_Id)
10407 Loc : constant Source_Ptr := Sloc (Typ_Decl);
10408 Anon_Access : Entity_Id;
10409 Acc_Def : Node_Id;
10410 Comp : Node_Id;
10411 Comp_Def : Node_Id;
10412 Decl : Node_Id;
10413 Type_Def : Node_Id;
10415 procedure Build_Incomplete_Type_Declaration;
10416 -- If the record type contains components that include an access to the
10417 -- current record, then create an incomplete type declaration for the
10418 -- record, to be used as the designated type of the anonymous access.
10419 -- This is done only once, and only if there is no previous partial
10420 -- view of the type.
10422 function Designates_T (Subt : Node_Id) return Boolean;
10423 -- Check whether a node designates the enclosing record type, or 'Class
10424 -- of that type
10426 function Mentions_T (Acc_Def : Node_Id) return Boolean;
10427 -- Check whether an access definition includes a reference to
10428 -- the enclosing record type. The reference can be a subtype mark
10429 -- in the access definition itself, a 'Class attribute reference, or
10430 -- recursively a reference appearing in a parameter specification
10431 -- or result definition of an access_to_subprogram definition.
10433 --------------------------------------
10434 -- Build_Incomplete_Type_Declaration --
10435 --------------------------------------
10437 procedure Build_Incomplete_Type_Declaration is
10438 Decl : Node_Id;
10439 Inc_T : Entity_Id;
10440 H : Entity_Id;
10442 -- Is_Tagged indicates whether the type is tagged. It is tagged if
10443 -- it's "is new ... with record" or else "is tagged record ...".
10445 Is_Tagged : constant Boolean :=
10446 (Nkind (Type_Definition (Typ_Decl)) = N_Derived_Type_Definition
10447 and then
10448 Present (Record_Extension_Part (Type_Definition (Typ_Decl))))
10449 or else
10450 (Nkind (Type_Definition (Typ_Decl)) = N_Record_Definition
10451 and then Tagged_Present (Type_Definition (Typ_Decl)));
10453 begin
10454 -- If there is a previous partial view, no need to create a new one
10455 -- If the partial view, given by Prev, is incomplete, If Prev is
10456 -- a private declaration, full declaration is flagged accordingly.
10458 if Prev /= Typ then
10459 if Is_Tagged then
10460 Make_Class_Wide_Type (Prev);
10461 Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
10462 Set_Etype (Class_Wide_Type (Typ), Typ);
10463 end if;
10465 return;
10467 elsif Has_Private_Declaration (Typ) then
10469 -- If we refer to T'Class inside T, and T is the completion of a
10470 -- private type, then make sure the class-wide type exists.
10472 if Is_Tagged then
10473 Make_Class_Wide_Type (Typ);
10474 end if;
10476 return;
10478 -- If there was a previous anonymous access type, the incomplete
10479 -- type declaration will have been created already.
10481 elsif Present (Current_Entity (Typ))
10482 and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
10483 and then Full_View (Current_Entity (Typ)) = Typ
10484 then
10485 if Is_Tagged
10486 and then Comes_From_Source (Current_Entity (Typ))
10487 and then not Is_Tagged_Type (Current_Entity (Typ))
10488 then
10489 Make_Class_Wide_Type (Typ);
10490 Error_Msg_N
10491 ("incomplete view of tagged type should be declared tagged??",
10492 Parent (Current_Entity (Typ)));
10493 end if;
10494 return;
10496 else
10497 Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
10498 Decl := Make_Incomplete_Type_Declaration (Loc, Inc_T);
10500 -- Type has already been inserted into the current scope. Remove
10501 -- it, and add incomplete declaration for type, so that subsequent
10502 -- anonymous access types can use it. The entity is unchained from
10503 -- the homonym list and from immediate visibility. After analysis,
10504 -- the entity in the incomplete declaration becomes immediately
10505 -- visible in the record declaration that follows.
10507 H := Current_Entity (Typ);
10509 if H = Typ then
10510 Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
10511 else
10512 while Present (H)
10513 and then Homonym (H) /= Typ
10514 loop
10515 H := Homonym (Typ);
10516 end loop;
10518 Set_Homonym (H, Homonym (Typ));
10519 end if;
10521 Insert_Before (Typ_Decl, Decl);
10522 Analyze (Decl);
10523 Set_Full_View (Inc_T, Typ);
10525 if Is_Tagged then
10527 -- Create a common class-wide type for both views, and set the
10528 -- Etype of the class-wide type to the full view.
10530 Make_Class_Wide_Type (Inc_T);
10531 Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
10532 Set_Etype (Class_Wide_Type (Typ), Typ);
10533 end if;
10534 end if;
10535 end Build_Incomplete_Type_Declaration;
10537 ------------------
10538 -- Designates_T --
10539 ------------------
10541 function Designates_T (Subt : Node_Id) return Boolean is
10542 Type_Id : constant Name_Id := Chars (Typ);
10544 function Names_T (Nam : Node_Id) return Boolean;
10545 -- The record type has not been introduced in the current scope
10546 -- yet, so we must examine the name of the type itself, either
10547 -- an identifier T, or an expanded name of the form P.T, where
10548 -- P denotes the current scope.
10550 -------------
10551 -- Names_T --
10552 -------------
10554 function Names_T (Nam : Node_Id) return Boolean is
10555 begin
10556 if Nkind (Nam) = N_Identifier then
10557 return Chars (Nam) = Type_Id;
10559 elsif Nkind (Nam) = N_Selected_Component then
10560 if Chars (Selector_Name (Nam)) = Type_Id then
10561 if Nkind (Prefix (Nam)) = N_Identifier then
10562 return Chars (Prefix (Nam)) = Chars (Current_Scope);
10564 elsif Nkind (Prefix (Nam)) = N_Selected_Component then
10565 return Chars (Selector_Name (Prefix (Nam))) =
10566 Chars (Current_Scope);
10567 else
10568 return False;
10569 end if;
10571 else
10572 return False;
10573 end if;
10575 else
10576 return False;
10577 end if;
10578 end Names_T;
10580 -- Start of processing for Designates_T
10582 begin
10583 if Nkind (Subt) = N_Identifier then
10584 return Chars (Subt) = Type_Id;
10586 -- Reference can be through an expanded name which has not been
10587 -- analyzed yet, and which designates enclosing scopes.
10589 elsif Nkind (Subt) = N_Selected_Component then
10590 if Names_T (Subt) then
10591 return True;
10593 -- Otherwise it must denote an entity that is already visible.
10594 -- The access definition may name a subtype of the enclosing
10595 -- type, if there is a previous incomplete declaration for it.
10597 else
10598 Find_Selected_Component (Subt);
10599 return
10600 Is_Entity_Name (Subt)
10601 and then Scope (Entity (Subt)) = Current_Scope
10602 and then
10603 (Chars (Base_Type (Entity (Subt))) = Type_Id
10604 or else
10605 (Is_Class_Wide_Type (Entity (Subt))
10606 and then
10607 Chars (Etype (Base_Type (Entity (Subt)))) =
10608 Type_Id));
10609 end if;
10611 -- A reference to the current type may appear as the prefix of
10612 -- a 'Class attribute.
10614 elsif Nkind (Subt) = N_Attribute_Reference
10615 and then Attribute_Name (Subt) = Name_Class
10616 then
10617 return Names_T (Prefix (Subt));
10619 else
10620 return False;
10621 end if;
10622 end Designates_T;
10624 ----------------
10625 -- Mentions_T --
10626 ----------------
10628 function Mentions_T (Acc_Def : Node_Id) return Boolean is
10629 Param_Spec : Node_Id;
10631 Acc_Subprg : constant Node_Id :=
10632 Access_To_Subprogram_Definition (Acc_Def);
10634 begin
10635 if No (Acc_Subprg) then
10636 return Designates_T (Subtype_Mark (Acc_Def));
10637 end if;
10639 -- Component is an access_to_subprogram: examine its formals,
10640 -- and result definition in the case of an access_to_function.
10642 Param_Spec := First (Parameter_Specifications (Acc_Subprg));
10643 while Present (Param_Spec) loop
10644 if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
10645 and then Mentions_T (Parameter_Type (Param_Spec))
10646 then
10647 return True;
10649 elsif Designates_T (Parameter_Type (Param_Spec)) then
10650 return True;
10651 end if;
10653 Next (Param_Spec);
10654 end loop;
10656 if Nkind (Acc_Subprg) = N_Access_Function_Definition then
10657 if Nkind (Result_Definition (Acc_Subprg)) =
10658 N_Access_Definition
10659 then
10660 return Mentions_T (Result_Definition (Acc_Subprg));
10661 else
10662 return Designates_T (Result_Definition (Acc_Subprg));
10663 end if;
10664 end if;
10666 return False;
10667 end Mentions_T;
10669 -- Start of processing for Check_Anonymous_Access_Components
10671 begin
10672 if No (Comp_List) then
10673 return;
10674 end if;
10676 Comp := First (Component_Items (Comp_List));
10677 while Present (Comp) loop
10678 if Nkind (Comp) = N_Component_Declaration
10679 and then Present
10680 (Access_Definition (Component_Definition (Comp)))
10681 and then
10682 Mentions_T (Access_Definition (Component_Definition (Comp)))
10683 then
10684 Comp_Def := Component_Definition (Comp);
10685 Acc_Def :=
10686 Access_To_Subprogram_Definition (Access_Definition (Comp_Def));
10688 Build_Incomplete_Type_Declaration;
10689 Anon_Access := Make_Temporary (Loc, 'S');
10691 -- Create a declaration for the anonymous access type: either
10692 -- an access_to_object or an access_to_subprogram.
10694 if Present (Acc_Def) then
10695 if Nkind (Acc_Def) = N_Access_Function_Definition then
10696 Type_Def :=
10697 Make_Access_Function_Definition (Loc,
10698 Parameter_Specifications =>
10699 Parameter_Specifications (Acc_Def),
10700 Result_Definition => Result_Definition (Acc_Def));
10701 else
10702 Type_Def :=
10703 Make_Access_Procedure_Definition (Loc,
10704 Parameter_Specifications =>
10705 Parameter_Specifications (Acc_Def));
10706 end if;
10708 else
10709 Type_Def :=
10710 Make_Access_To_Object_Definition (Loc,
10711 Subtype_Indication =>
10712 Relocate_Node
10713 (Subtype_Mark (Access_Definition (Comp_Def))));
10715 Set_Constant_Present
10716 (Type_Def, Constant_Present (Access_Definition (Comp_Def)));
10717 Set_All_Present
10718 (Type_Def, All_Present (Access_Definition (Comp_Def)));
10719 end if;
10721 Set_Null_Exclusion_Present
10722 (Type_Def,
10723 Null_Exclusion_Present (Access_Definition (Comp_Def)));
10725 Decl :=
10726 Make_Full_Type_Declaration (Loc,
10727 Defining_Identifier => Anon_Access,
10728 Type_Definition => Type_Def);
10730 Insert_Before (Typ_Decl, Decl);
10731 Analyze (Decl);
10733 -- If an access to subprogram, create the extra formals
10735 if Present (Acc_Def) then
10736 Create_Extra_Formals (Designated_Type (Anon_Access));
10738 -- If an access to object, preserve entity of designated type,
10739 -- for ASIS use, before rewriting the component definition.
10741 else
10742 declare
10743 Desig : Entity_Id;
10745 begin
10746 Desig := Entity (Subtype_Indication (Type_Def));
10748 -- If the access definition is to the current record,
10749 -- the visible entity at this point is an incomplete
10750 -- type. Retrieve the full view to simplify ASIS queries
10752 if Ekind (Desig) = E_Incomplete_Type then
10753 Desig := Full_View (Desig);
10754 end if;
10756 Set_Entity
10757 (Subtype_Mark (Access_Definition (Comp_Def)), Desig);
10758 end;
10759 end if;
10761 Rewrite (Comp_Def,
10762 Make_Component_Definition (Loc,
10763 Subtype_Indication =>
10764 New_Occurrence_Of (Anon_Access, Loc)));
10766 if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
10767 Set_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
10768 else
10769 Set_Ekind (Anon_Access, E_Anonymous_Access_Type);
10770 end if;
10772 Set_Is_Local_Anonymous_Access (Anon_Access);
10773 end if;
10775 Next (Comp);
10776 end loop;
10778 if Present (Variant_Part (Comp_List)) then
10779 declare
10780 V : Node_Id;
10781 begin
10782 V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
10783 while Present (V) loop
10784 Check_Anonymous_Access_Components
10785 (Typ_Decl, Typ, Prev, Component_List (V));
10786 Next_Non_Pragma (V);
10787 end loop;
10788 end;
10789 end if;
10790 end Check_Anonymous_Access_Components;
10792 ----------------------
10793 -- Check_Completion --
10794 ----------------------
10796 procedure Check_Completion (Body_Id : Node_Id := Empty) is
10797 E : Entity_Id;
10799 procedure Post_Error;
10800 -- Post error message for lack of completion for entity E
10802 ----------------
10803 -- Post_Error --
10804 ----------------
10806 procedure Post_Error is
10808 procedure Missing_Body;
10809 -- Output missing body message
10811 ------------------
10812 -- Missing_Body --
10813 ------------------
10815 procedure Missing_Body is
10816 begin
10817 -- Spec is in same unit, so we can post on spec
10819 if In_Same_Source_Unit (Body_Id, E) then
10820 Error_Msg_N ("missing body for &", E);
10822 -- Spec is in a separate unit, so we have to post on the body
10824 else
10825 Error_Msg_NE ("missing body for & declared#!", Body_Id, E);
10826 end if;
10827 end Missing_Body;
10829 -- Start of processing for Post_Error
10831 begin
10832 if not Comes_From_Source (E) then
10834 if Ekind_In (E, E_Task_Type, E_Protected_Type) then
10836 -- It may be an anonymous protected type created for a
10837 -- single variable. Post error on variable, if present.
10839 declare
10840 Var : Entity_Id;
10842 begin
10843 Var := First_Entity (Current_Scope);
10844 while Present (Var) loop
10845 exit when Etype (Var) = E
10846 and then Comes_From_Source (Var);
10848 Next_Entity (Var);
10849 end loop;
10851 if Present (Var) then
10852 E := Var;
10853 end if;
10854 end;
10855 end if;
10856 end if;
10858 -- If a generated entity has no completion, then either previous
10859 -- semantic errors have disabled the expansion phase, or else we had
10860 -- missing subunits, or else we are compiling without expansion,
10861 -- or else something is very wrong.
10863 if not Comes_From_Source (E) then
10864 pragma Assert
10865 (Serious_Errors_Detected > 0
10866 or else Configurable_Run_Time_Violations > 0
10867 or else Subunits_Missing
10868 or else not Expander_Active);
10869 return;
10871 -- Here for source entity
10873 else
10874 -- Here if no body to post the error message, so we post the error
10875 -- on the declaration that has no completion. This is not really
10876 -- the right place to post it, think about this later ???
10878 if No (Body_Id) then
10879 if Is_Type (E) then
10880 Error_Msg_NE
10881 ("missing full declaration for }", Parent (E), E);
10882 else
10883 Error_Msg_NE ("missing body for &", Parent (E), E);
10884 end if;
10886 -- Package body has no completion for a declaration that appears
10887 -- in the corresponding spec. Post error on the body, with a
10888 -- reference to the non-completed declaration.
10890 else
10891 Error_Msg_Sloc := Sloc (E);
10893 if Is_Type (E) then
10894 Error_Msg_NE ("missing full declaration for }!", Body_Id, E);
10896 elsif Is_Overloadable (E)
10897 and then Current_Entity_In_Scope (E) /= E
10898 then
10899 -- It may be that the completion is mistyped and appears as
10900 -- a distinct overloading of the entity.
10902 declare
10903 Candidate : constant Entity_Id :=
10904 Current_Entity_In_Scope (E);
10905 Decl : constant Node_Id :=
10906 Unit_Declaration_Node (Candidate);
10908 begin
10909 if Is_Overloadable (Candidate)
10910 and then Ekind (Candidate) = Ekind (E)
10911 and then Nkind (Decl) = N_Subprogram_Body
10912 and then Acts_As_Spec (Decl)
10913 then
10914 Check_Type_Conformant (Candidate, E);
10916 else
10917 Missing_Body;
10918 end if;
10919 end;
10921 else
10922 Missing_Body;
10923 end if;
10924 end if;
10925 end if;
10926 end Post_Error;
10928 -- Local variables
10930 Pack_Id : constant Entity_Id := Current_Scope;
10932 -- Start of processing for Check_Completion
10934 begin
10935 E := First_Entity (Pack_Id);
10936 while Present (E) loop
10937 if Is_Intrinsic_Subprogram (E) then
10938 null;
10940 -- The following situation requires special handling: a child unit
10941 -- that appears in the context clause of the body of its parent:
10943 -- procedure Parent.Child (...);
10945 -- with Parent.Child;
10946 -- package body Parent is
10948 -- Here Parent.Child appears as a local entity, but should not be
10949 -- flagged as requiring completion, because it is a compilation
10950 -- unit.
10952 -- Ignore missing completion for a subprogram that does not come from
10953 -- source (including the _Call primitive operation of RAS types,
10954 -- which has to have the flag Comes_From_Source for other purposes):
10955 -- we assume that the expander will provide the missing completion.
10956 -- In case of previous errors, other expansion actions that provide
10957 -- bodies for null procedures with not be invoked, so inhibit message
10958 -- in those cases.
10960 -- Note that E_Operator is not in the list that follows, because
10961 -- this kind is reserved for predefined operators, that are
10962 -- intrinsic and do not need completion.
10964 elsif Ekind_In (E, E_Function,
10965 E_Procedure,
10966 E_Generic_Function,
10967 E_Generic_Procedure)
10968 then
10969 if Has_Completion (E) then
10970 null;
10972 elsif Is_Subprogram (E) and then Is_Abstract_Subprogram (E) then
10973 null;
10975 elsif Is_Subprogram (E)
10976 and then (not Comes_From_Source (E)
10977 or else Chars (E) = Name_uCall)
10978 then
10979 null;
10981 elsif
10982 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
10983 then
10984 null;
10986 elsif Nkind (Parent (E)) = N_Procedure_Specification
10987 and then Null_Present (Parent (E))
10988 and then Serious_Errors_Detected > 0
10989 then
10990 null;
10992 else
10993 Post_Error;
10994 end if;
10996 elsif Is_Entry (E) then
10997 if not Has_Completion (E) and then
10998 (Ekind (Scope (E)) = E_Protected_Object
10999 or else Ekind (Scope (E)) = E_Protected_Type)
11000 then
11001 Post_Error;
11002 end if;
11004 elsif Is_Package_Or_Generic_Package (E) then
11005 if Unit_Requires_Body (E) then
11006 if not Has_Completion (E)
11007 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
11008 N_Compilation_Unit
11009 then
11010 Post_Error;
11011 end if;
11013 elsif not Is_Child_Unit (E) then
11014 May_Need_Implicit_Body (E);
11015 end if;
11017 -- A formal incomplete type (Ada 2012) does not require a completion;
11018 -- other incomplete type declarations do.
11020 elsif Ekind (E) = E_Incomplete_Type
11021 and then No (Underlying_Type (E))
11022 and then not Is_Generic_Type (E)
11023 then
11024 Post_Error;
11026 elsif Ekind_In (E, E_Task_Type, E_Protected_Type)
11027 and then not Has_Completion (E)
11028 then
11029 Post_Error;
11031 -- A single task declared in the current scope is a constant, verify
11032 -- that the body of its anonymous type is in the same scope. If the
11033 -- task is defined elsewhere, this may be a renaming declaration for
11034 -- which no completion is needed.
11036 elsif Ekind (E) = E_Constant
11037 and then Ekind (Etype (E)) = E_Task_Type
11038 and then not Has_Completion (Etype (E))
11039 and then Scope (Etype (E)) = Current_Scope
11040 then
11041 Post_Error;
11043 elsif Ekind (E) = E_Protected_Object
11044 and then not Has_Completion (Etype (E))
11045 then
11046 Post_Error;
11048 elsif Ekind (E) = E_Record_Type then
11049 if Is_Tagged_Type (E) then
11050 Check_Abstract_Overriding (E);
11051 Check_Conventions (E);
11052 end if;
11054 Check_Aliased_Component_Types (E);
11056 elsif Ekind (E) = E_Array_Type then
11057 Check_Aliased_Component_Types (E);
11059 end if;
11061 Next_Entity (E);
11062 end loop;
11063 end Check_Completion;
11065 ------------------------------------
11066 -- Check_CPP_Type_Has_No_Defaults --
11067 ------------------------------------
11069 procedure Check_CPP_Type_Has_No_Defaults (T : Entity_Id) is
11070 Tdef : constant Node_Id := Type_Definition (Declaration_Node (T));
11071 Clist : Node_Id;
11072 Comp : Node_Id;
11074 begin
11075 -- Obtain the component list
11077 if Nkind (Tdef) = N_Record_Definition then
11078 Clist := Component_List (Tdef);
11079 else pragma Assert (Nkind (Tdef) = N_Derived_Type_Definition);
11080 Clist := Component_List (Record_Extension_Part (Tdef));
11081 end if;
11083 -- Check all components to ensure no default expressions
11085 if Present (Clist) then
11086 Comp := First (Component_Items (Clist));
11087 while Present (Comp) loop
11088 if Present (Expression (Comp)) then
11089 Error_Msg_N
11090 ("component of imported 'C'P'P type cannot have "
11091 & "default expression", Expression (Comp));
11092 end if;
11094 Next (Comp);
11095 end loop;
11096 end if;
11097 end Check_CPP_Type_Has_No_Defaults;
11099 ----------------------------
11100 -- Check_Delta_Expression --
11101 ----------------------------
11103 procedure Check_Delta_Expression (E : Node_Id) is
11104 begin
11105 if not (Is_Real_Type (Etype (E))) then
11106 Wrong_Type (E, Any_Real);
11108 elsif not Is_OK_Static_Expression (E) then
11109 Flag_Non_Static_Expr
11110 ("non-static expression used for delta value!", E);
11112 elsif not UR_Is_Positive (Expr_Value_R (E)) then
11113 Error_Msg_N ("delta expression must be positive", E);
11115 else
11116 return;
11117 end if;
11119 -- If any of above errors occurred, then replace the incorrect
11120 -- expression by the real 0.1, which should prevent further errors.
11122 Rewrite (E,
11123 Make_Real_Literal (Sloc (E), Ureal_Tenth));
11124 Analyze_And_Resolve (E, Standard_Float);
11125 end Check_Delta_Expression;
11127 -----------------------------
11128 -- Check_Digits_Expression --
11129 -----------------------------
11131 procedure Check_Digits_Expression (E : Node_Id) is
11132 begin
11133 if not (Is_Integer_Type (Etype (E))) then
11134 Wrong_Type (E, Any_Integer);
11136 elsif not Is_OK_Static_Expression (E) then
11137 Flag_Non_Static_Expr
11138 ("non-static expression used for digits value!", E);
11140 elsif Expr_Value (E) <= 0 then
11141 Error_Msg_N ("digits value must be greater than zero", E);
11143 else
11144 return;
11145 end if;
11147 -- If any of above errors occurred, then replace the incorrect
11148 -- expression by the integer 1, which should prevent further errors.
11150 Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
11151 Analyze_And_Resolve (E, Standard_Integer);
11153 end Check_Digits_Expression;
11155 --------------------------
11156 -- Check_Initialization --
11157 --------------------------
11159 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
11160 begin
11161 -- Special processing for limited types
11163 if Is_Limited_Type (T)
11164 and then not In_Instance
11165 and then not In_Inlined_Body
11166 then
11167 if not OK_For_Limited_Init (T, Exp) then
11169 -- In GNAT mode, this is just a warning, to allow it to be evilly
11170 -- turned off. Otherwise it is a real error.
11172 if GNAT_Mode then
11173 Error_Msg_N
11174 ("??cannot initialize entities of limited type!", Exp);
11176 elsif Ada_Version < Ada_2005 then
11178 -- The side effect removal machinery may generate illegal Ada
11179 -- code to avoid the usage of access types and 'reference in
11180 -- SPARK mode. Since this is legal code with respect to theorem
11181 -- proving, do not emit the error.
11183 if GNATprove_Mode
11184 and then Nkind (Exp) = N_Function_Call
11185 and then Nkind (Parent (Exp)) = N_Object_Declaration
11186 and then not Comes_From_Source
11187 (Defining_Identifier (Parent (Exp)))
11188 then
11189 null;
11191 else
11192 Error_Msg_N
11193 ("cannot initialize entities of limited type", Exp);
11194 Explain_Limited_Type (T, Exp);
11195 end if;
11197 else
11198 -- Specialize error message according to kind of illegal
11199 -- initial expression.
11201 if Nkind (Exp) = N_Type_Conversion
11202 and then Nkind (Expression (Exp)) = N_Function_Call
11203 then
11204 Error_Msg_N
11205 ("illegal context for call"
11206 & " to function with limited result", Exp);
11208 else
11209 Error_Msg_N
11210 ("initialization of limited object requires aggregate "
11211 & "or function call", Exp);
11212 end if;
11213 end if;
11214 end if;
11215 end if;
11217 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag gets
11218 -- set unless we can be sure that no range check is required.
11220 if (GNATprove_Mode or not Expander_Active)
11221 and then Is_Scalar_Type (T)
11222 and then not Is_In_Range (Exp, T, Assume_Valid => True)
11223 then
11224 Set_Do_Range_Check (Exp);
11225 end if;
11226 end Check_Initialization;
11228 ----------------------
11229 -- Check_Interfaces --
11230 ----------------------
11232 procedure Check_Interfaces (N : Node_Id; Def : Node_Id) is
11233 Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
11235 Iface : Node_Id;
11236 Iface_Def : Node_Id;
11237 Iface_Typ : Entity_Id;
11238 Parent_Node : Node_Id;
11240 Is_Task : Boolean := False;
11241 -- Set True if parent type or any progenitor is a task interface
11243 Is_Protected : Boolean := False;
11244 -- Set True if parent type or any progenitor is a protected interface
11246 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
11247 -- Check that a progenitor is compatible with declaration. If an error
11248 -- message is output, it is posted on Error_Node.
11250 ------------------
11251 -- Check_Ifaces --
11252 ------------------
11254 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
11255 Iface_Id : constant Entity_Id :=
11256 Defining_Identifier (Parent (Iface_Def));
11257 Type_Def : Node_Id;
11259 begin
11260 if Nkind (N) = N_Private_Extension_Declaration then
11261 Type_Def := N;
11262 else
11263 Type_Def := Type_Definition (N);
11264 end if;
11266 if Is_Task_Interface (Iface_Id) then
11267 Is_Task := True;
11269 elsif Is_Protected_Interface (Iface_Id) then
11270 Is_Protected := True;
11271 end if;
11273 if Is_Synchronized_Interface (Iface_Id) then
11275 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
11276 -- extension derived from a synchronized interface must explicitly
11277 -- be declared synchronized, because the full view will be a
11278 -- synchronized type.
11280 if Nkind (N) = N_Private_Extension_Declaration then
11281 if not Synchronized_Present (N) then
11282 Error_Msg_NE
11283 ("private extension of& must be explicitly synchronized",
11284 N, Iface_Id);
11285 end if;
11287 -- However, by 3.9.4(16/2), a full type that is a record extension
11288 -- is never allowed to derive from a synchronized interface (note
11289 -- that interfaces must be excluded from this check, because those
11290 -- are represented by derived type definitions in some cases).
11292 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition
11293 and then not Interface_Present (Type_Definition (N))
11294 then
11295 Error_Msg_N ("record extension cannot derive from synchronized "
11296 & "interface", Error_Node);
11297 end if;
11298 end if;
11300 -- Check that the characteristics of the progenitor are compatible
11301 -- with the explicit qualifier in the declaration.
11302 -- The check only applies to qualifiers that come from source.
11303 -- Limited_Present also appears in the declaration of corresponding
11304 -- records, and the check does not apply to them.
11306 if Limited_Present (Type_Def)
11307 and then not
11308 Is_Concurrent_Record_Type (Defining_Identifier (N))
11309 then
11310 if Is_Limited_Interface (Parent_Type)
11311 and then not Is_Limited_Interface (Iface_Id)
11312 then
11313 Error_Msg_NE
11314 ("progenitor & must be limited interface",
11315 Error_Node, Iface_Id);
11317 elsif
11318 (Task_Present (Iface_Def)
11319 or else Protected_Present (Iface_Def)
11320 or else Synchronized_Present (Iface_Def))
11321 and then Nkind (N) /= N_Private_Extension_Declaration
11322 and then not Error_Posted (N)
11323 then
11324 Error_Msg_NE
11325 ("progenitor & must be limited interface",
11326 Error_Node, Iface_Id);
11327 end if;
11329 -- Protected interfaces can only inherit from limited, synchronized
11330 -- or protected interfaces.
11332 elsif Nkind (N) = N_Full_Type_Declaration
11333 and then Protected_Present (Type_Def)
11334 then
11335 if Limited_Present (Iface_Def)
11336 or else Synchronized_Present (Iface_Def)
11337 or else Protected_Present (Iface_Def)
11338 then
11339 null;
11341 elsif Task_Present (Iface_Def) then
11342 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
11343 & "from task interface", Error_Node);
11345 else
11346 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
11347 & "from non-limited interface", Error_Node);
11348 end if;
11350 -- Ada 2005 (AI-345): Synchronized interfaces can only inherit from
11351 -- limited and synchronized.
11353 elsif Synchronized_Present (Type_Def) then
11354 if Limited_Present (Iface_Def)
11355 or else Synchronized_Present (Iface_Def)
11356 then
11357 null;
11359 elsif Protected_Present (Iface_Def)
11360 and then Nkind (N) /= N_Private_Extension_Declaration
11361 then
11362 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11363 & "from protected interface", Error_Node);
11365 elsif Task_Present (Iface_Def)
11366 and then Nkind (N) /= N_Private_Extension_Declaration
11367 then
11368 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11369 & "from task interface", Error_Node);
11371 elsif not Is_Limited_Interface (Iface_Id) then
11372 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11373 & "from non-limited interface", Error_Node);
11374 end if;
11376 -- Ada 2005 (AI-345): Task interfaces can only inherit from limited,
11377 -- synchronized or task interfaces.
11379 elsif Nkind (N) = N_Full_Type_Declaration
11380 and then Task_Present (Type_Def)
11381 then
11382 if Limited_Present (Iface_Def)
11383 or else Synchronized_Present (Iface_Def)
11384 or else Task_Present (Iface_Def)
11385 then
11386 null;
11388 elsif Protected_Present (Iface_Def) then
11389 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
11390 & "protected interface", Error_Node);
11392 else
11393 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
11394 & "non-limited interface", Error_Node);
11395 end if;
11396 end if;
11397 end Check_Ifaces;
11399 -- Start of processing for Check_Interfaces
11401 begin
11402 if Is_Interface (Parent_Type) then
11403 if Is_Task_Interface (Parent_Type) then
11404 Is_Task := True;
11406 elsif Is_Protected_Interface (Parent_Type) then
11407 Is_Protected := True;
11408 end if;
11409 end if;
11411 if Nkind (N) = N_Private_Extension_Declaration then
11413 -- Check that progenitors are compatible with declaration
11415 Iface := First (Interface_List (Def));
11416 while Present (Iface) loop
11417 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
11419 Parent_Node := Parent (Base_Type (Iface_Typ));
11420 Iface_Def := Type_Definition (Parent_Node);
11422 if not Is_Interface (Iface_Typ) then
11423 Diagnose_Interface (Iface, Iface_Typ);
11424 else
11425 Check_Ifaces (Iface_Def, Iface);
11426 end if;
11428 Next (Iface);
11429 end loop;
11431 if Is_Task and Is_Protected then
11432 Error_Msg_N
11433 ("type cannot derive from task and protected interface", N);
11434 end if;
11436 return;
11437 end if;
11439 -- Full type declaration of derived type.
11440 -- Check compatibility with parent if it is interface type
11442 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
11443 and then Is_Interface (Parent_Type)
11444 then
11445 Parent_Node := Parent (Parent_Type);
11447 -- More detailed checks for interface varieties
11449 Check_Ifaces
11450 (Iface_Def => Type_Definition (Parent_Node),
11451 Error_Node => Subtype_Indication (Type_Definition (N)));
11452 end if;
11454 Iface := First (Interface_List (Def));
11455 while Present (Iface) loop
11456 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
11458 Parent_Node := Parent (Base_Type (Iface_Typ));
11459 Iface_Def := Type_Definition (Parent_Node);
11461 if not Is_Interface (Iface_Typ) then
11462 Diagnose_Interface (Iface, Iface_Typ);
11464 else
11465 -- "The declaration of a specific descendant of an interface
11466 -- type freezes the interface type" RM 13.14
11468 Freeze_Before (N, Iface_Typ);
11469 Check_Ifaces (Iface_Def, Error_Node => Iface);
11470 end if;
11472 Next (Iface);
11473 end loop;
11475 if Is_Task and Is_Protected then
11476 Error_Msg_N
11477 ("type cannot derive from task and protected interface", N);
11478 end if;
11479 end Check_Interfaces;
11481 ------------------------------------
11482 -- Check_Or_Process_Discriminants --
11483 ------------------------------------
11485 -- If an incomplete or private type declaration was already given for the
11486 -- type, the discriminants may have already been processed if they were
11487 -- present on the incomplete declaration. In this case a full conformance
11488 -- check has been performed in Find_Type_Name, and we then recheck here
11489 -- some properties that can't be checked on the partial view alone.
11490 -- Otherwise we call Process_Discriminants.
11492 procedure Check_Or_Process_Discriminants
11493 (N : Node_Id;
11494 T : Entity_Id;
11495 Prev : Entity_Id := Empty)
11497 begin
11498 if Has_Discriminants (T) then
11500 -- Discriminants are already set on T if they were already present
11501 -- on the partial view. Make them visible to component declarations.
11503 declare
11504 D : Entity_Id;
11505 -- Discriminant on T (full view) referencing expr on partial view
11507 Prev_D : Entity_Id;
11508 -- Entity of corresponding discriminant on partial view
11510 New_D : Node_Id;
11511 -- Discriminant specification for full view, expression is
11512 -- the syntactic copy on full view (which has been checked for
11513 -- conformance with partial view), only used here to post error
11514 -- message.
11516 begin
11517 D := First_Discriminant (T);
11518 New_D := First (Discriminant_Specifications (N));
11519 while Present (D) loop
11520 Prev_D := Current_Entity (D);
11521 Set_Current_Entity (D);
11522 Set_Is_Immediately_Visible (D);
11523 Set_Homonym (D, Prev_D);
11525 -- Handle the case where there is an untagged partial view and
11526 -- the full view is tagged: must disallow discriminants with
11527 -- defaults, unless compiling for Ada 2012, which allows a
11528 -- limited tagged type to have defaulted discriminants (see
11529 -- AI05-0214). However, suppress error here if it was already
11530 -- reported on the default expression of the partial view.
11532 if Is_Tagged_Type (T)
11533 and then Present (Expression (Parent (D)))
11534 and then (not Is_Limited_Type (Current_Scope)
11535 or else Ada_Version < Ada_2012)
11536 and then not Error_Posted (Expression (Parent (D)))
11537 then
11538 if Ada_Version >= Ada_2012 then
11539 Error_Msg_N
11540 ("discriminants of nonlimited tagged type cannot have "
11541 & "defaults",
11542 Expression (New_D));
11543 else
11544 Error_Msg_N
11545 ("discriminants of tagged type cannot have defaults",
11546 Expression (New_D));
11547 end if;
11548 end if;
11550 -- Ada 2005 (AI-230): Access discriminant allowed in
11551 -- non-limited record types.
11553 if Ada_Version < Ada_2005 then
11555 -- This restriction gets applied to the full type here. It
11556 -- has already been applied earlier to the partial view.
11558 Check_Access_Discriminant_Requires_Limited (Parent (D), N);
11559 end if;
11561 Next_Discriminant (D);
11562 Next (New_D);
11563 end loop;
11564 end;
11566 elsif Present (Discriminant_Specifications (N)) then
11567 Process_Discriminants (N, Prev);
11568 end if;
11569 end Check_Or_Process_Discriminants;
11571 ----------------------
11572 -- Check_Real_Bound --
11573 ----------------------
11575 procedure Check_Real_Bound (Bound : Node_Id) is
11576 begin
11577 if not Is_Real_Type (Etype (Bound)) then
11578 Error_Msg_N
11579 ("bound in real type definition must be of real type", Bound);
11581 elsif not Is_OK_Static_Expression (Bound) then
11582 Flag_Non_Static_Expr
11583 ("non-static expression used for real type bound!", Bound);
11585 else
11586 return;
11587 end if;
11589 Rewrite
11590 (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
11591 Analyze (Bound);
11592 Resolve (Bound, Standard_Float);
11593 end Check_Real_Bound;
11595 ------------------------------
11596 -- Complete_Private_Subtype --
11597 ------------------------------
11599 procedure Complete_Private_Subtype
11600 (Priv : Entity_Id;
11601 Full : Entity_Id;
11602 Full_Base : Entity_Id;
11603 Related_Nod : Node_Id)
11605 Save_Next_Entity : Entity_Id;
11606 Save_Homonym : Entity_Id;
11608 begin
11609 -- Set semantic attributes for (implicit) private subtype completion.
11610 -- If the full type has no discriminants, then it is a copy of the
11611 -- full view of the base. Otherwise, it is a subtype of the base with
11612 -- a possible discriminant constraint. Save and restore the original
11613 -- Next_Entity field of full to ensure that the calls to Copy_Node do
11614 -- not corrupt the entity chain.
11616 -- Note that the type of the full view is the same entity as the type
11617 -- of the partial view. In this fashion, the subtype has access to the
11618 -- correct view of the parent.
11620 Save_Next_Entity := Next_Entity (Full);
11621 Save_Homonym := Homonym (Priv);
11623 case Ekind (Full_Base) is
11624 when E_Record_Type |
11625 E_Record_Subtype |
11626 Class_Wide_Kind |
11627 Private_Kind |
11628 Task_Kind |
11629 Protected_Kind =>
11630 Copy_Node (Priv, Full);
11632 Set_Has_Discriminants
11633 (Full, Has_Discriminants (Full_Base));
11634 Set_Has_Unknown_Discriminants
11635 (Full, Has_Unknown_Discriminants (Full_Base));
11636 Set_First_Entity (Full, First_Entity (Full_Base));
11637 Set_Last_Entity (Full, Last_Entity (Full_Base));
11639 -- If the underlying base type is constrained, we know that the
11640 -- full view of the subtype is constrained as well (the converse
11641 -- is not necessarily true).
11643 if Is_Constrained (Full_Base) then
11644 Set_Is_Constrained (Full);
11645 end if;
11647 when others =>
11648 Copy_Node (Full_Base, Full);
11650 Set_Chars (Full, Chars (Priv));
11651 Conditional_Delay (Full, Priv);
11652 Set_Sloc (Full, Sloc (Priv));
11653 end case;
11655 Set_Next_Entity (Full, Save_Next_Entity);
11656 Set_Homonym (Full, Save_Homonym);
11657 Set_Associated_Node_For_Itype (Full, Related_Nod);
11659 -- Set common attributes for all subtypes: kind, convention, etc.
11661 Set_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
11662 Set_Convention (Full, Convention (Full_Base));
11664 -- The Etype of the full view is inconsistent. Gigi needs to see the
11665 -- structural full view, which is what the current scheme gives: the
11666 -- Etype of the full view is the etype of the full base. However, if the
11667 -- full base is a derived type, the full view then looks like a subtype
11668 -- of the parent, not a subtype of the full base. If instead we write:
11670 -- Set_Etype (Full, Full_Base);
11672 -- then we get inconsistencies in the front-end (confusion between
11673 -- views). Several outstanding bugs are related to this ???
11675 Set_Is_First_Subtype (Full, False);
11676 Set_Scope (Full, Scope (Priv));
11677 Set_Size_Info (Full, Full_Base);
11678 Set_RM_Size (Full, RM_Size (Full_Base));
11679 Set_Is_Itype (Full);
11681 -- A subtype of a private-type-without-discriminants, whose full-view
11682 -- has discriminants with default expressions, is not constrained.
11684 if not Has_Discriminants (Priv) then
11685 Set_Is_Constrained (Full, Is_Constrained (Full_Base));
11687 if Has_Discriminants (Full_Base) then
11688 Set_Discriminant_Constraint
11689 (Full, Discriminant_Constraint (Full_Base));
11691 -- The partial view may have been indefinite, the full view
11692 -- might not be.
11694 Set_Has_Unknown_Discriminants
11695 (Full, Has_Unknown_Discriminants (Full_Base));
11696 end if;
11697 end if;
11699 Set_First_Rep_Item (Full, First_Rep_Item (Full_Base));
11700 Set_Depends_On_Private (Full, Has_Private_Component (Full));
11702 -- Freeze the private subtype entity if its parent is delayed, and not
11703 -- already frozen. We skip this processing if the type is an anonymous
11704 -- subtype of a record component, or is the corresponding record of a
11705 -- protected type, since these are processed when the enclosing type
11706 -- is frozen.
11708 if not Is_Type (Scope (Full)) then
11709 Set_Has_Delayed_Freeze (Full,
11710 Has_Delayed_Freeze (Full_Base)
11711 and then (not Is_Frozen (Full_Base)));
11712 end if;
11714 Set_Freeze_Node (Full, Empty);
11715 Set_Is_Frozen (Full, False);
11716 Set_Full_View (Priv, Full);
11718 if Has_Discriminants (Full) then
11719 Set_Stored_Constraint_From_Discriminant_Constraint (Full);
11720 Set_Stored_Constraint (Priv, Stored_Constraint (Full));
11722 if Has_Unknown_Discriminants (Full) then
11723 Set_Discriminant_Constraint (Full, No_Elist);
11724 end if;
11725 end if;
11727 if Ekind (Full_Base) = E_Record_Type
11728 and then Has_Discriminants (Full_Base)
11729 and then Has_Discriminants (Priv) -- might not, if errors
11730 and then not Has_Unknown_Discriminants (Priv)
11731 and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
11732 then
11733 Create_Constrained_Components
11734 (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
11736 -- If the full base is itself derived from private, build a congruent
11737 -- subtype of its underlying type, for use by the back end. For a
11738 -- constrained record component, the declaration cannot be placed on
11739 -- the component list, but it must nevertheless be built an analyzed, to
11740 -- supply enough information for Gigi to compute the size of component.
11742 elsif Ekind (Full_Base) in Private_Kind
11743 and then Is_Derived_Type (Full_Base)
11744 and then Has_Discriminants (Full_Base)
11745 and then (Ekind (Current_Scope) /= E_Record_Subtype)
11746 then
11747 if not Is_Itype (Priv)
11748 and then
11749 Nkind (Subtype_Indication (Parent (Priv))) = N_Subtype_Indication
11750 then
11751 Build_Underlying_Full_View
11752 (Parent (Priv), Full, Etype (Full_Base));
11754 elsif Nkind (Related_Nod) = N_Component_Declaration then
11755 Build_Underlying_Full_View (Related_Nod, Full, Etype (Full_Base));
11756 end if;
11758 elsif Is_Record_Type (Full_Base) then
11760 -- Show Full is simply a renaming of Full_Base
11762 Set_Cloned_Subtype (Full, Full_Base);
11763 end if;
11765 -- It is unsafe to share the bounds of a scalar type, because the Itype
11766 -- is elaborated on demand, and if a bound is non-static then different
11767 -- orders of elaboration in different units will lead to different
11768 -- external symbols.
11770 if Is_Scalar_Type (Full_Base) then
11771 Set_Scalar_Range (Full,
11772 Make_Range (Sloc (Related_Nod),
11773 Low_Bound =>
11774 Duplicate_Subexpr_No_Checks (Type_Low_Bound (Full_Base)),
11775 High_Bound =>
11776 Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
11778 -- This completion inherits the bounds of the full parent, but if
11779 -- the parent is an unconstrained floating point type, so is the
11780 -- completion.
11782 if Is_Floating_Point_Type (Full_Base) then
11783 Set_Includes_Infinities
11784 (Scalar_Range (Full), Has_Infinities (Full_Base));
11785 end if;
11786 end if;
11788 -- ??? It seems that a lot of fields are missing that should be copied
11789 -- from Full_Base to Full. Here are some that are introduced in a
11790 -- non-disruptive way but a cleanup is necessary.
11792 if Is_Tagged_Type (Full_Base) then
11793 Set_Is_Tagged_Type (Full);
11794 Set_Direct_Primitive_Operations
11795 (Full, Direct_Primitive_Operations (Full_Base));
11796 Set_No_Tagged_Streams_Pragma
11797 (Full, No_Tagged_Streams_Pragma (Full_Base));
11799 -- Inherit class_wide type of full_base in case the partial view was
11800 -- not tagged. Otherwise it has already been created when the private
11801 -- subtype was analyzed.
11803 if No (Class_Wide_Type (Full)) then
11804 Set_Class_Wide_Type (Full, Class_Wide_Type (Full_Base));
11805 end if;
11807 -- If this is a subtype of a protected or task type, constrain its
11808 -- corresponding record, unless this is a subtype without constraints,
11809 -- i.e. a simple renaming as with an actual subtype in an instance.
11811 elsif Is_Concurrent_Type (Full_Base) then
11812 if Has_Discriminants (Full)
11813 and then Present (Corresponding_Record_Type (Full_Base))
11814 and then
11815 not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
11816 then
11817 Set_Corresponding_Record_Type (Full,
11818 Constrain_Corresponding_Record
11819 (Full, Corresponding_Record_Type (Full_Base), Related_Nod));
11821 else
11822 Set_Corresponding_Record_Type (Full,
11823 Corresponding_Record_Type (Full_Base));
11824 end if;
11825 end if;
11827 -- Link rep item chain, and also setting of Has_Predicates from private
11828 -- subtype to full subtype, since we will need these on the full subtype
11829 -- to create the predicate function. Note that the full subtype may
11830 -- already have rep items, inherited from the full view of the base
11831 -- type, so we must be sure not to overwrite these entries.
11833 declare
11834 Append : Boolean;
11835 Item : Node_Id;
11836 Next_Item : Node_Id;
11838 begin
11839 Item := First_Rep_Item (Full);
11841 -- If no existing rep items on full type, we can just link directly
11842 -- to the list of items on the private type, if any exist.. Same if
11843 -- the rep items are only those inherited from the base
11845 if (No (Item)
11846 or else Nkind (Item) /= N_Aspect_Specification
11847 or else Entity (Item) = Full_Base)
11848 and then Present (First_Rep_Item (Priv))
11849 then
11850 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
11852 -- Otherwise, search to the end of items currently linked to the full
11853 -- subtype and append the private items to the end. However, if Priv
11854 -- and Full already have the same list of rep items, then the append
11855 -- is not done, as that would create a circularity.
11857 elsif Item /= First_Rep_Item (Priv) then
11858 Append := True;
11859 loop
11860 Next_Item := Next_Rep_Item (Item);
11861 exit when No (Next_Item);
11862 Item := Next_Item;
11864 -- If the private view has aspect specifications, the full view
11865 -- inherits them. Since these aspects may already have been
11866 -- attached to the full view during derivation, do not append
11867 -- them if already present.
11869 if Item = First_Rep_Item (Priv) then
11870 Append := False;
11871 exit;
11872 end if;
11873 end loop;
11875 -- And link the private type items at the end of the chain
11877 if Append then
11878 Set_Next_Rep_Item (Item, First_Rep_Item (Priv));
11879 end if;
11880 end if;
11881 end;
11883 -- Make sure Has_Predicates is set on full type if it is set on the
11884 -- private type. Note that it may already be set on the full type and
11885 -- if so, we don't want to unset it. Similarly, propagate information
11886 -- about delayed aspects, because the corresponding pragmas must be
11887 -- analyzed when one of the views is frozen. This last step is needed
11888 -- in particular when the full type is a scalar type for which an
11889 -- anonymous base type is constructed.
11891 if Has_Predicates (Priv) then
11892 Set_Has_Predicates (Full);
11893 end if;
11895 if Has_Delayed_Aspects (Priv) then
11896 Set_Has_Delayed_Aspects (Full);
11897 end if;
11898 end Complete_Private_Subtype;
11900 ----------------------------
11901 -- Constant_Redeclaration --
11902 ----------------------------
11904 procedure Constant_Redeclaration
11905 (Id : Entity_Id;
11906 N : Node_Id;
11907 T : out Entity_Id)
11909 Prev : constant Entity_Id := Current_Entity_In_Scope (Id);
11910 Obj_Def : constant Node_Id := Object_Definition (N);
11911 New_T : Entity_Id;
11913 procedure Check_Possible_Deferred_Completion
11914 (Prev_Id : Entity_Id;
11915 Prev_Obj_Def : Node_Id;
11916 Curr_Obj_Def : Node_Id);
11917 -- Determine whether the two object definitions describe the partial
11918 -- and the full view of a constrained deferred constant. Generate
11919 -- a subtype for the full view and verify that it statically matches
11920 -- the subtype of the partial view.
11922 procedure Check_Recursive_Declaration (Typ : Entity_Id);
11923 -- If deferred constant is an access type initialized with an allocator,
11924 -- check whether there is an illegal recursion in the definition,
11925 -- through a default value of some record subcomponent. This is normally
11926 -- detected when generating init procs, but requires this additional
11927 -- mechanism when expansion is disabled.
11929 ----------------------------------------
11930 -- Check_Possible_Deferred_Completion --
11931 ----------------------------------------
11933 procedure Check_Possible_Deferred_Completion
11934 (Prev_Id : Entity_Id;
11935 Prev_Obj_Def : Node_Id;
11936 Curr_Obj_Def : Node_Id)
11938 begin
11939 if Nkind (Prev_Obj_Def) = N_Subtype_Indication
11940 and then Present (Constraint (Prev_Obj_Def))
11941 and then Nkind (Curr_Obj_Def) = N_Subtype_Indication
11942 and then Present (Constraint (Curr_Obj_Def))
11943 then
11944 declare
11945 Loc : constant Source_Ptr := Sloc (N);
11946 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
11947 Decl : constant Node_Id :=
11948 Make_Subtype_Declaration (Loc,
11949 Defining_Identifier => Def_Id,
11950 Subtype_Indication =>
11951 Relocate_Node (Curr_Obj_Def));
11953 begin
11954 Insert_Before_And_Analyze (N, Decl);
11955 Set_Etype (Id, Def_Id);
11957 if not Subtypes_Statically_Match (Etype (Prev_Id), Def_Id) then
11958 Error_Msg_Sloc := Sloc (Prev_Id);
11959 Error_Msg_N ("subtype does not statically match deferred "
11960 & "declaration #", N);
11961 end if;
11962 end;
11963 end if;
11964 end Check_Possible_Deferred_Completion;
11966 ---------------------------------
11967 -- Check_Recursive_Declaration --
11968 ---------------------------------
11970 procedure Check_Recursive_Declaration (Typ : Entity_Id) is
11971 Comp : Entity_Id;
11973 begin
11974 if Is_Record_Type (Typ) then
11975 Comp := First_Component (Typ);
11976 while Present (Comp) loop
11977 if Comes_From_Source (Comp) then
11978 if Present (Expression (Parent (Comp)))
11979 and then Is_Entity_Name (Expression (Parent (Comp)))
11980 and then Entity (Expression (Parent (Comp))) = Prev
11981 then
11982 Error_Msg_Sloc := Sloc (Parent (Comp));
11983 Error_Msg_NE
11984 ("illegal circularity with declaration for & #",
11985 N, Comp);
11986 return;
11988 elsif Is_Record_Type (Etype (Comp)) then
11989 Check_Recursive_Declaration (Etype (Comp));
11990 end if;
11991 end if;
11993 Next_Component (Comp);
11994 end loop;
11995 end if;
11996 end Check_Recursive_Declaration;
11998 -- Start of processing for Constant_Redeclaration
12000 begin
12001 if Nkind (Parent (Prev)) = N_Object_Declaration then
12002 if Nkind (Object_Definition
12003 (Parent (Prev))) = N_Subtype_Indication
12004 then
12005 -- Find type of new declaration. The constraints of the two
12006 -- views must match statically, but there is no point in
12007 -- creating an itype for the full view.
12009 if Nkind (Obj_Def) = N_Subtype_Indication then
12010 Find_Type (Subtype_Mark (Obj_Def));
12011 New_T := Entity (Subtype_Mark (Obj_Def));
12013 else
12014 Find_Type (Obj_Def);
12015 New_T := Entity (Obj_Def);
12016 end if;
12018 T := Etype (Prev);
12020 else
12021 -- The full view may impose a constraint, even if the partial
12022 -- view does not, so construct the subtype.
12024 New_T := Find_Type_Of_Object (Obj_Def, N);
12025 T := New_T;
12026 end if;
12028 else
12029 -- Current declaration is illegal, diagnosed below in Enter_Name
12031 T := Empty;
12032 New_T := Any_Type;
12033 end if;
12035 -- If previous full declaration or a renaming declaration exists, or if
12036 -- a homograph is present, let Enter_Name handle it, either with an
12037 -- error or with the removal of an overridden implicit subprogram.
12038 -- The previous one is a full declaration if it has an expression
12039 -- (which in the case of an aggregate is indicated by the Init flag).
12041 if Ekind (Prev) /= E_Constant
12042 or else Nkind (Parent (Prev)) = N_Object_Renaming_Declaration
12043 or else Present (Expression (Parent (Prev)))
12044 or else Has_Init_Expression (Parent (Prev))
12045 or else Present (Full_View (Prev))
12046 then
12047 Enter_Name (Id);
12049 -- Verify that types of both declarations match, or else that both types
12050 -- are anonymous access types whose designated subtypes statically match
12051 -- (as allowed in Ada 2005 by AI-385).
12053 elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
12054 and then
12055 (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
12056 or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
12057 or else Is_Access_Constant (Etype (New_T)) /=
12058 Is_Access_Constant (Etype (Prev))
12059 or else Can_Never_Be_Null (Etype (New_T)) /=
12060 Can_Never_Be_Null (Etype (Prev))
12061 or else Null_Exclusion_Present (Parent (Prev)) /=
12062 Null_Exclusion_Present (Parent (Id))
12063 or else not Subtypes_Statically_Match
12064 (Designated_Type (Etype (Prev)),
12065 Designated_Type (Etype (New_T))))
12066 then
12067 Error_Msg_Sloc := Sloc (Prev);
12068 Error_Msg_N ("type does not match declaration#", N);
12069 Set_Full_View (Prev, Id);
12070 Set_Etype (Id, Any_Type);
12072 -- A deferred constant whose type is an anonymous array is always
12073 -- illegal (unless imported). A detailed error message might be
12074 -- helpful for Ada beginners.
12076 if Nkind (Object_Definition (Parent (Prev)))
12077 = N_Constrained_Array_Definition
12078 and then Nkind (Object_Definition (N))
12079 = N_Constrained_Array_Definition
12080 then
12081 Error_Msg_N ("\each anonymous array is a distinct type", N);
12082 Error_Msg_N ("a deferred constant must have a named type",
12083 Object_Definition (Parent (Prev)));
12084 end if;
12086 elsif
12087 Null_Exclusion_Present (Parent (Prev))
12088 and then not Null_Exclusion_Present (N)
12089 then
12090 Error_Msg_Sloc := Sloc (Prev);
12091 Error_Msg_N ("null-exclusion does not match declaration#", N);
12092 Set_Full_View (Prev, Id);
12093 Set_Etype (Id, Any_Type);
12095 -- If so, process the full constant declaration
12097 else
12098 -- RM 7.4 (6): If the subtype defined by the subtype_indication in
12099 -- the deferred declaration is constrained, then the subtype defined
12100 -- by the subtype_indication in the full declaration shall match it
12101 -- statically.
12103 Check_Possible_Deferred_Completion
12104 (Prev_Id => Prev,
12105 Prev_Obj_Def => Object_Definition (Parent (Prev)),
12106 Curr_Obj_Def => Obj_Def);
12108 Set_Full_View (Prev, Id);
12109 Set_Is_Public (Id, Is_Public (Prev));
12110 Set_Is_Internal (Id);
12111 Append_Entity (Id, Current_Scope);
12113 -- Check ALIASED present if present before (RM 7.4(7))
12115 if Is_Aliased (Prev)
12116 and then not Aliased_Present (N)
12117 then
12118 Error_Msg_Sloc := Sloc (Prev);
12119 Error_Msg_N ("ALIASED required (see declaration #)", N);
12120 end if;
12122 -- Check that placement is in private part and that the incomplete
12123 -- declaration appeared in the visible part.
12125 if Ekind (Current_Scope) = E_Package
12126 and then not In_Private_Part (Current_Scope)
12127 then
12128 Error_Msg_Sloc := Sloc (Prev);
12129 Error_Msg_N
12130 ("full constant for declaration # must be in private part", N);
12132 elsif Ekind (Current_Scope) = E_Package
12133 and then
12134 List_Containing (Parent (Prev)) /=
12135 Visible_Declarations (Package_Specification (Current_Scope))
12136 then
12137 Error_Msg_N
12138 ("deferred constant must be declared in visible part",
12139 Parent (Prev));
12140 end if;
12142 if Is_Access_Type (T)
12143 and then Nkind (Expression (N)) = N_Allocator
12144 then
12145 Check_Recursive_Declaration (Designated_Type (T));
12146 end if;
12148 -- A deferred constant is a visible entity. If type has invariants,
12149 -- verify that the initial value satisfies them.
12151 if Has_Invariants (T) and then Present (Invariant_Procedure (T)) then
12152 Insert_After (N,
12153 Make_Invariant_Call (New_Occurrence_Of (Prev, Sloc (N))));
12154 end if;
12155 end if;
12156 end Constant_Redeclaration;
12158 ----------------------
12159 -- Constrain_Access --
12160 ----------------------
12162 procedure Constrain_Access
12163 (Def_Id : in out Entity_Id;
12164 S : Node_Id;
12165 Related_Nod : Node_Id)
12167 T : constant Entity_Id := Entity (Subtype_Mark (S));
12168 Desig_Type : constant Entity_Id := Designated_Type (T);
12169 Desig_Subtype : Entity_Id := Create_Itype (E_Void, Related_Nod);
12170 Constraint_OK : Boolean := True;
12172 begin
12173 if Is_Array_Type (Desig_Type) then
12174 Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
12176 elsif (Is_Record_Type (Desig_Type)
12177 or else Is_Incomplete_Or_Private_Type (Desig_Type))
12178 and then not Is_Constrained (Desig_Type)
12179 then
12180 -- ??? The following code is a temporary bypass to ignore a
12181 -- discriminant constraint on access type if it is constraining
12182 -- the current record. Avoid creating the implicit subtype of the
12183 -- record we are currently compiling since right now, we cannot
12184 -- handle these. For now, just return the access type itself.
12186 if Desig_Type = Current_Scope
12187 and then No (Def_Id)
12188 then
12189 Set_Ekind (Desig_Subtype, E_Record_Subtype);
12190 Def_Id := Entity (Subtype_Mark (S));
12192 -- This call added to ensure that the constraint is analyzed
12193 -- (needed for a B test). Note that we still return early from
12194 -- this procedure to avoid recursive processing. ???
12196 Constrain_Discriminated_Type
12197 (Desig_Subtype, S, Related_Nod, For_Access => True);
12198 return;
12199 end if;
12201 -- Enforce rule that the constraint is illegal if there is an
12202 -- unconstrained view of the designated type. This means that the
12203 -- partial view (either a private type declaration or a derivation
12204 -- from a private type) has no discriminants. (Defect Report
12205 -- 8652/0008, Technical Corrigendum 1, checked by ACATS B371001).
12207 -- Rule updated for Ada 2005: The private type is said to have
12208 -- a constrained partial view, given that objects of the type
12209 -- can be declared. Furthermore, the rule applies to all access
12210 -- types, unlike the rule concerning default discriminants (see
12211 -- RM 3.7.1(7/3))
12213 if (Ekind (T) = E_General_Access_Type or else Ada_Version >= Ada_2005)
12214 and then Has_Private_Declaration (Desig_Type)
12215 and then In_Open_Scopes (Scope (Desig_Type))
12216 and then Has_Discriminants (Desig_Type)
12217 then
12218 declare
12219 Pack : constant Node_Id :=
12220 Unit_Declaration_Node (Scope (Desig_Type));
12221 Decls : List_Id;
12222 Decl : Node_Id;
12224 begin
12225 if Nkind (Pack) = N_Package_Declaration then
12226 Decls := Visible_Declarations (Specification (Pack));
12227 Decl := First (Decls);
12228 while Present (Decl) loop
12229 if (Nkind (Decl) = N_Private_Type_Declaration
12230 and then Chars (Defining_Identifier (Decl)) =
12231 Chars (Desig_Type))
12233 or else
12234 (Nkind (Decl) = N_Full_Type_Declaration
12235 and then
12236 Chars (Defining_Identifier (Decl)) =
12237 Chars (Desig_Type)
12238 and then Is_Derived_Type (Desig_Type)
12239 and then
12240 Has_Private_Declaration (Etype (Desig_Type)))
12241 then
12242 if No (Discriminant_Specifications (Decl)) then
12243 Error_Msg_N
12244 ("cannot constrain access type if designated "
12245 & "type has constrained partial view", S);
12246 end if;
12248 exit;
12249 end if;
12251 Next (Decl);
12252 end loop;
12253 end if;
12254 end;
12255 end if;
12257 Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
12258 For_Access => True);
12260 elsif Is_Concurrent_Type (Desig_Type)
12261 and then not Is_Constrained (Desig_Type)
12262 then
12263 Constrain_Concurrent (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
12265 else
12266 Error_Msg_N ("invalid constraint on access type", S);
12268 -- We simply ignore an invalid constraint
12270 Desig_Subtype := Desig_Type;
12271 Constraint_OK := False;
12272 end if;
12274 if No (Def_Id) then
12275 Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
12276 else
12277 Set_Ekind (Def_Id, E_Access_Subtype);
12278 end if;
12280 if Constraint_OK then
12281 Set_Etype (Def_Id, Base_Type (T));
12283 if Is_Private_Type (Desig_Type) then
12284 Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
12285 end if;
12286 else
12287 Set_Etype (Def_Id, Any_Type);
12288 end if;
12290 Set_Size_Info (Def_Id, T);
12291 Set_Is_Constrained (Def_Id, Constraint_OK);
12292 Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
12293 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
12294 Set_Is_Access_Constant (Def_Id, Is_Access_Constant (T));
12296 Conditional_Delay (Def_Id, T);
12298 -- AI-363 : Subtypes of general access types whose designated types have
12299 -- default discriminants are disallowed. In instances, the rule has to
12300 -- be checked against the actual, of which T is the subtype. In a
12301 -- generic body, the rule is checked assuming that the actual type has
12302 -- defaulted discriminants.
12304 if Ada_Version >= Ada_2005 or else Warn_On_Ada_2005_Compatibility then
12305 if Ekind (Base_Type (T)) = E_General_Access_Type
12306 and then Has_Defaulted_Discriminants (Desig_Type)
12307 then
12308 if Ada_Version < Ada_2005 then
12309 Error_Msg_N
12310 ("access subtype of general access type would not " &
12311 "be allowed in Ada 2005?y?", S);
12312 else
12313 Error_Msg_N
12314 ("access subtype of general access type not allowed", S);
12315 end if;
12317 Error_Msg_N ("\discriminants have defaults", S);
12319 elsif Is_Access_Type (T)
12320 and then Is_Generic_Type (Desig_Type)
12321 and then Has_Discriminants (Desig_Type)
12322 and then In_Package_Body (Current_Scope)
12323 then
12324 if Ada_Version < Ada_2005 then
12325 Error_Msg_N
12326 ("access subtype would not be allowed in generic body "
12327 & "in Ada 2005?y?", S);
12328 else
12329 Error_Msg_N
12330 ("access subtype not allowed in generic body", S);
12331 end if;
12333 Error_Msg_N
12334 ("\designated type is a discriminated formal", S);
12335 end if;
12336 end if;
12337 end Constrain_Access;
12339 ---------------------
12340 -- Constrain_Array --
12341 ---------------------
12343 procedure Constrain_Array
12344 (Def_Id : in out Entity_Id;
12345 SI : Node_Id;
12346 Related_Nod : Node_Id;
12347 Related_Id : Entity_Id;
12348 Suffix : Character)
12350 C : constant Node_Id := Constraint (SI);
12351 Number_Of_Constraints : Nat := 0;
12352 Index : Node_Id;
12353 S, T : Entity_Id;
12354 Constraint_OK : Boolean := True;
12356 begin
12357 T := Entity (Subtype_Mark (SI));
12359 if Is_Access_Type (T) then
12360 T := Designated_Type (T);
12361 end if;
12363 -- If an index constraint follows a subtype mark in a subtype indication
12364 -- then the type or subtype denoted by the subtype mark must not already
12365 -- impose an index constraint. The subtype mark must denote either an
12366 -- unconstrained array type or an access type whose designated type
12367 -- is such an array type... (RM 3.6.1)
12369 if Is_Constrained (T) then
12370 Error_Msg_N ("array type is already constrained", Subtype_Mark (SI));
12371 Constraint_OK := False;
12373 else
12374 S := First (Constraints (C));
12375 while Present (S) loop
12376 Number_Of_Constraints := Number_Of_Constraints + 1;
12377 Next (S);
12378 end loop;
12380 -- In either case, the index constraint must provide a discrete
12381 -- range for each index of the array type and the type of each
12382 -- discrete range must be the same as that of the corresponding
12383 -- index. (RM 3.6.1)
12385 if Number_Of_Constraints /= Number_Dimensions (T) then
12386 Error_Msg_NE ("incorrect number of index constraints for }", C, T);
12387 Constraint_OK := False;
12389 else
12390 S := First (Constraints (C));
12391 Index := First_Index (T);
12392 Analyze (Index);
12394 -- Apply constraints to each index type
12396 for J in 1 .. Number_Of_Constraints loop
12397 Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
12398 Next (Index);
12399 Next (S);
12400 end loop;
12402 end if;
12403 end if;
12405 if No (Def_Id) then
12406 Def_Id :=
12407 Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
12408 Set_Parent (Def_Id, Related_Nod);
12410 else
12411 Set_Ekind (Def_Id, E_Array_Subtype);
12412 end if;
12414 Set_Size_Info (Def_Id, (T));
12415 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
12416 Set_Etype (Def_Id, Base_Type (T));
12418 if Constraint_OK then
12419 Set_First_Index (Def_Id, First (Constraints (C)));
12420 else
12421 Set_First_Index (Def_Id, First_Index (T));
12422 end if;
12424 Set_Is_Constrained (Def_Id, True);
12425 Set_Is_Aliased (Def_Id, Is_Aliased (T));
12426 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
12428 Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
12429 Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
12431 -- A subtype does not inherit the Packed_Array_Impl_Type of is parent.
12432 -- We need to initialize the attribute because if Def_Id is previously
12433 -- analyzed through a limited_with clause, it will have the attributes
12434 -- of an incomplete type, one of which is an Elist that overlaps the
12435 -- Packed_Array_Impl_Type field.
12437 Set_Packed_Array_Impl_Type (Def_Id, Empty);
12439 -- Build a freeze node if parent still needs one. Also make sure that
12440 -- the Depends_On_Private status is set because the subtype will need
12441 -- reprocessing at the time the base type does, and also we must set a
12442 -- conditional delay.
12444 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
12445 Conditional_Delay (Def_Id, T);
12446 end Constrain_Array;
12448 ------------------------------
12449 -- Constrain_Component_Type --
12450 ------------------------------
12452 function Constrain_Component_Type
12453 (Comp : Entity_Id;
12454 Constrained_Typ : Entity_Id;
12455 Related_Node : Node_Id;
12456 Typ : Entity_Id;
12457 Constraints : Elist_Id) return Entity_Id
12459 Loc : constant Source_Ptr := Sloc (Constrained_Typ);
12460 Compon_Type : constant Entity_Id := Etype (Comp);
12462 function Build_Constrained_Array_Type
12463 (Old_Type : Entity_Id) return Entity_Id;
12464 -- If Old_Type is an array type, one of whose indexes is constrained
12465 -- by a discriminant, build an Itype whose constraint replaces the
12466 -- discriminant with its value in the constraint.
12468 function Build_Constrained_Discriminated_Type
12469 (Old_Type : Entity_Id) return Entity_Id;
12470 -- Ditto for record components
12472 function Build_Constrained_Access_Type
12473 (Old_Type : Entity_Id) return Entity_Id;
12474 -- Ditto for access types. Makes use of previous two functions, to
12475 -- constrain designated type.
12477 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id;
12478 -- T is an array or discriminated type, C is a list of constraints
12479 -- that apply to T. This routine builds the constrained subtype.
12481 function Is_Discriminant (Expr : Node_Id) return Boolean;
12482 -- Returns True if Expr is a discriminant
12484 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id;
12485 -- Find the value of discriminant Discrim in Constraint
12487 -----------------------------------
12488 -- Build_Constrained_Access_Type --
12489 -----------------------------------
12491 function Build_Constrained_Access_Type
12492 (Old_Type : Entity_Id) return Entity_Id
12494 Desig_Type : constant Entity_Id := Designated_Type (Old_Type);
12495 Itype : Entity_Id;
12496 Desig_Subtype : Entity_Id;
12497 Scop : Entity_Id;
12499 begin
12500 -- if the original access type was not embedded in the enclosing
12501 -- type definition, there is no need to produce a new access
12502 -- subtype. In fact every access type with an explicit constraint
12503 -- generates an itype whose scope is the enclosing record.
12505 if not Is_Type (Scope (Old_Type)) then
12506 return Old_Type;
12508 elsif Is_Array_Type (Desig_Type) then
12509 Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
12511 elsif Has_Discriminants (Desig_Type) then
12513 -- This may be an access type to an enclosing record type for
12514 -- which we are constructing the constrained components. Return
12515 -- the enclosing record subtype. This is not always correct,
12516 -- but avoids infinite recursion. ???
12518 Desig_Subtype := Any_Type;
12520 for J in reverse 0 .. Scope_Stack.Last loop
12521 Scop := Scope_Stack.Table (J).Entity;
12523 if Is_Type (Scop)
12524 and then Base_Type (Scop) = Base_Type (Desig_Type)
12525 then
12526 Desig_Subtype := Scop;
12527 end if;
12529 exit when not Is_Type (Scop);
12530 end loop;
12532 if Desig_Subtype = Any_Type then
12533 Desig_Subtype :=
12534 Build_Constrained_Discriminated_Type (Desig_Type);
12535 end if;
12537 else
12538 return Old_Type;
12539 end if;
12541 if Desig_Subtype /= Desig_Type then
12543 -- The Related_Node better be here or else we won't be able
12544 -- to attach new itypes to a node in the tree.
12546 pragma Assert (Present (Related_Node));
12548 Itype := Create_Itype (E_Access_Subtype, Related_Node);
12550 Set_Etype (Itype, Base_Type (Old_Type));
12551 Set_Size_Info (Itype, (Old_Type));
12552 Set_Directly_Designated_Type (Itype, Desig_Subtype);
12553 Set_Depends_On_Private (Itype, Has_Private_Component
12554 (Old_Type));
12555 Set_Is_Access_Constant (Itype, Is_Access_Constant
12556 (Old_Type));
12558 -- The new itype needs freezing when it depends on a not frozen
12559 -- type and the enclosing subtype needs freezing.
12561 if Has_Delayed_Freeze (Constrained_Typ)
12562 and then not Is_Frozen (Constrained_Typ)
12563 then
12564 Conditional_Delay (Itype, Base_Type (Old_Type));
12565 end if;
12567 return Itype;
12569 else
12570 return Old_Type;
12571 end if;
12572 end Build_Constrained_Access_Type;
12574 ----------------------------------
12575 -- Build_Constrained_Array_Type --
12576 ----------------------------------
12578 function Build_Constrained_Array_Type
12579 (Old_Type : Entity_Id) return Entity_Id
12581 Lo_Expr : Node_Id;
12582 Hi_Expr : Node_Id;
12583 Old_Index : Node_Id;
12584 Range_Node : Node_Id;
12585 Constr_List : List_Id;
12587 Need_To_Create_Itype : Boolean := False;
12589 begin
12590 Old_Index := First_Index (Old_Type);
12591 while Present (Old_Index) loop
12592 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
12594 if Is_Discriminant (Lo_Expr)
12595 or else
12596 Is_Discriminant (Hi_Expr)
12597 then
12598 Need_To_Create_Itype := True;
12599 end if;
12601 Next_Index (Old_Index);
12602 end loop;
12604 if Need_To_Create_Itype then
12605 Constr_List := New_List;
12607 Old_Index := First_Index (Old_Type);
12608 while Present (Old_Index) loop
12609 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
12611 if Is_Discriminant (Lo_Expr) then
12612 Lo_Expr := Get_Discr_Value (Lo_Expr);
12613 end if;
12615 if Is_Discriminant (Hi_Expr) then
12616 Hi_Expr := Get_Discr_Value (Hi_Expr);
12617 end if;
12619 Range_Node :=
12620 Make_Range
12621 (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
12623 Append (Range_Node, To => Constr_List);
12625 Next_Index (Old_Index);
12626 end loop;
12628 return Build_Subtype (Old_Type, Constr_List);
12630 else
12631 return Old_Type;
12632 end if;
12633 end Build_Constrained_Array_Type;
12635 ------------------------------------------
12636 -- Build_Constrained_Discriminated_Type --
12637 ------------------------------------------
12639 function Build_Constrained_Discriminated_Type
12640 (Old_Type : Entity_Id) return Entity_Id
12642 Expr : Node_Id;
12643 Constr_List : List_Id;
12644 Old_Constraint : Elmt_Id;
12646 Need_To_Create_Itype : Boolean := False;
12648 begin
12649 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
12650 while Present (Old_Constraint) loop
12651 Expr := Node (Old_Constraint);
12653 if Is_Discriminant (Expr) then
12654 Need_To_Create_Itype := True;
12655 end if;
12657 Next_Elmt (Old_Constraint);
12658 end loop;
12660 if Need_To_Create_Itype then
12661 Constr_List := New_List;
12663 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
12664 while Present (Old_Constraint) loop
12665 Expr := Node (Old_Constraint);
12667 if Is_Discriminant (Expr) then
12668 Expr := Get_Discr_Value (Expr);
12669 end if;
12671 Append (New_Copy_Tree (Expr), To => Constr_List);
12673 Next_Elmt (Old_Constraint);
12674 end loop;
12676 return Build_Subtype (Old_Type, Constr_List);
12678 else
12679 return Old_Type;
12680 end if;
12681 end Build_Constrained_Discriminated_Type;
12683 -------------------
12684 -- Build_Subtype --
12685 -------------------
12687 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id is
12688 Indic : Node_Id;
12689 Subtyp_Decl : Node_Id;
12690 Def_Id : Entity_Id;
12691 Btyp : Entity_Id := Base_Type (T);
12693 begin
12694 -- The Related_Node better be here or else we won't be able to
12695 -- attach new itypes to a node in the tree.
12697 pragma Assert (Present (Related_Node));
12699 -- If the view of the component's type is incomplete or private
12700 -- with unknown discriminants, then the constraint must be applied
12701 -- to the full type.
12703 if Has_Unknown_Discriminants (Btyp)
12704 and then Present (Underlying_Type (Btyp))
12705 then
12706 Btyp := Underlying_Type (Btyp);
12707 end if;
12709 Indic :=
12710 Make_Subtype_Indication (Loc,
12711 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
12712 Constraint => Make_Index_Or_Discriminant_Constraint (Loc, C));
12714 Def_Id := Create_Itype (Ekind (T), Related_Node);
12716 Subtyp_Decl :=
12717 Make_Subtype_Declaration (Loc,
12718 Defining_Identifier => Def_Id,
12719 Subtype_Indication => Indic);
12721 Set_Parent (Subtyp_Decl, Parent (Related_Node));
12723 -- Itypes must be analyzed with checks off (see package Itypes)
12725 Analyze (Subtyp_Decl, Suppress => All_Checks);
12727 return Def_Id;
12728 end Build_Subtype;
12730 ---------------------
12731 -- Get_Discr_Value --
12732 ---------------------
12734 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id is
12735 D : Entity_Id;
12736 E : Elmt_Id;
12738 begin
12739 -- The discriminant may be declared for the type, in which case we
12740 -- find it by iterating over the list of discriminants. If the
12741 -- discriminant is inherited from a parent type, it appears as the
12742 -- corresponding discriminant of the current type. This will be the
12743 -- case when constraining an inherited component whose constraint is
12744 -- given by a discriminant of the parent.
12746 D := First_Discriminant (Typ);
12747 E := First_Elmt (Constraints);
12749 while Present (D) loop
12750 if D = Entity (Discrim)
12751 or else D = CR_Discriminant (Entity (Discrim))
12752 or else Corresponding_Discriminant (D) = Entity (Discrim)
12753 then
12754 return Node (E);
12755 end if;
12757 Next_Discriminant (D);
12758 Next_Elmt (E);
12759 end loop;
12761 -- The Corresponding_Discriminant mechanism is incomplete, because
12762 -- the correspondence between new and old discriminants is not one
12763 -- to one: one new discriminant can constrain several old ones. In
12764 -- that case, scan sequentially the stored_constraint, the list of
12765 -- discriminants of the parents, and the constraints.
12767 -- Previous code checked for the present of the Stored_Constraint
12768 -- list for the derived type, but did not use it at all. Should it
12769 -- be present when the component is a discriminated task type?
12771 if Is_Derived_Type (Typ)
12772 and then Scope (Entity (Discrim)) = Etype (Typ)
12773 then
12774 D := First_Discriminant (Etype (Typ));
12775 E := First_Elmt (Constraints);
12776 while Present (D) loop
12777 if D = Entity (Discrim) then
12778 return Node (E);
12779 end if;
12781 Next_Discriminant (D);
12782 Next_Elmt (E);
12783 end loop;
12784 end if;
12786 -- Something is wrong if we did not find the value
12788 raise Program_Error;
12789 end Get_Discr_Value;
12791 ---------------------
12792 -- Is_Discriminant --
12793 ---------------------
12795 function Is_Discriminant (Expr : Node_Id) return Boolean is
12796 Discrim_Scope : Entity_Id;
12798 begin
12799 if Denotes_Discriminant (Expr) then
12800 Discrim_Scope := Scope (Entity (Expr));
12802 -- Either we have a reference to one of Typ's discriminants,
12804 pragma Assert (Discrim_Scope = Typ
12806 -- or to the discriminants of the parent type, in the case
12807 -- of a derivation of a tagged type with variants.
12809 or else Discrim_Scope = Etype (Typ)
12810 or else Full_View (Discrim_Scope) = Etype (Typ)
12812 -- or same as above for the case where the discriminants
12813 -- were declared in Typ's private view.
12815 or else (Is_Private_Type (Discrim_Scope)
12816 and then Chars (Discrim_Scope) = Chars (Typ))
12818 -- or else we are deriving from the full view and the
12819 -- discriminant is declared in the private entity.
12821 or else (Is_Private_Type (Typ)
12822 and then Chars (Discrim_Scope) = Chars (Typ))
12824 -- Or we are constrained the corresponding record of a
12825 -- synchronized type that completes a private declaration.
12827 or else (Is_Concurrent_Record_Type (Typ)
12828 and then
12829 Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
12831 -- or we have a class-wide type, in which case make sure the
12832 -- discriminant found belongs to the root type.
12834 or else (Is_Class_Wide_Type (Typ)
12835 and then Etype (Typ) = Discrim_Scope));
12837 return True;
12838 end if;
12840 -- In all other cases we have something wrong
12842 return False;
12843 end Is_Discriminant;
12845 -- Start of processing for Constrain_Component_Type
12847 begin
12848 if Nkind (Parent (Comp)) = N_Component_Declaration
12849 and then Comes_From_Source (Parent (Comp))
12850 and then Comes_From_Source
12851 (Subtype_Indication (Component_Definition (Parent (Comp))))
12852 and then
12853 Is_Entity_Name
12854 (Subtype_Indication (Component_Definition (Parent (Comp))))
12855 then
12856 return Compon_Type;
12858 elsif Is_Array_Type (Compon_Type) then
12859 return Build_Constrained_Array_Type (Compon_Type);
12861 elsif Has_Discriminants (Compon_Type) then
12862 return Build_Constrained_Discriminated_Type (Compon_Type);
12864 elsif Is_Access_Type (Compon_Type) then
12865 return Build_Constrained_Access_Type (Compon_Type);
12867 else
12868 return Compon_Type;
12869 end if;
12870 end Constrain_Component_Type;
12872 --------------------------
12873 -- Constrain_Concurrent --
12874 --------------------------
12876 -- For concurrent types, the associated record value type carries the same
12877 -- discriminants, so when we constrain a concurrent type, we must constrain
12878 -- the corresponding record type as well.
12880 procedure Constrain_Concurrent
12881 (Def_Id : in out Entity_Id;
12882 SI : Node_Id;
12883 Related_Nod : Node_Id;
12884 Related_Id : Entity_Id;
12885 Suffix : Character)
12887 -- Retrieve Base_Type to ensure getting to the concurrent type in the
12888 -- case of a private subtype (needed when only doing semantic analysis).
12890 T_Ent : Entity_Id := Base_Type (Entity (Subtype_Mark (SI)));
12891 T_Val : Entity_Id;
12893 begin
12894 if Is_Access_Type (T_Ent) then
12895 T_Ent := Designated_Type (T_Ent);
12896 end if;
12898 T_Val := Corresponding_Record_Type (T_Ent);
12900 if Present (T_Val) then
12902 if No (Def_Id) then
12903 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
12904 end if;
12906 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
12908 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
12909 Set_Corresponding_Record_Type (Def_Id,
12910 Constrain_Corresponding_Record (Def_Id, T_Val, Related_Nod));
12912 else
12913 -- If there is no associated record, expansion is disabled and this
12914 -- is a generic context. Create a subtype in any case, so that
12915 -- semantic analysis can proceed.
12917 if No (Def_Id) then
12918 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
12919 end if;
12921 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
12922 end if;
12923 end Constrain_Concurrent;
12925 ------------------------------------
12926 -- Constrain_Corresponding_Record --
12927 ------------------------------------
12929 function Constrain_Corresponding_Record
12930 (Prot_Subt : Entity_Id;
12931 Corr_Rec : Entity_Id;
12932 Related_Nod : Node_Id) return Entity_Id
12934 T_Sub : constant Entity_Id :=
12935 Create_Itype (E_Record_Subtype, Related_Nod, Corr_Rec, 'C');
12937 begin
12938 Set_Etype (T_Sub, Corr_Rec);
12939 Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
12940 Set_Is_Constrained (T_Sub, True);
12941 Set_First_Entity (T_Sub, First_Entity (Corr_Rec));
12942 Set_Last_Entity (T_Sub, Last_Entity (Corr_Rec));
12944 if Has_Discriminants (Prot_Subt) then -- False only if errors.
12945 Set_Discriminant_Constraint
12946 (T_Sub, Discriminant_Constraint (Prot_Subt));
12947 Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
12948 Create_Constrained_Components
12949 (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
12950 end if;
12952 Set_Depends_On_Private (T_Sub, Has_Private_Component (T_Sub));
12954 if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
12955 Conditional_Delay (T_Sub, Corr_Rec);
12957 else
12958 -- This is a component subtype: it will be frozen in the context of
12959 -- the enclosing record's init_proc, so that discriminant references
12960 -- are resolved to discriminals. (Note: we used to skip freezing
12961 -- altogether in that case, which caused errors downstream for
12962 -- components of a bit packed array type).
12964 Set_Has_Delayed_Freeze (T_Sub);
12965 end if;
12967 return T_Sub;
12968 end Constrain_Corresponding_Record;
12970 -----------------------
12971 -- Constrain_Decimal --
12972 -----------------------
12974 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id) is
12975 T : constant Entity_Id := Entity (Subtype_Mark (S));
12976 C : constant Node_Id := Constraint (S);
12977 Loc : constant Source_Ptr := Sloc (C);
12978 Range_Expr : Node_Id;
12979 Digits_Expr : Node_Id;
12980 Digits_Val : Uint;
12981 Bound_Val : Ureal;
12983 begin
12984 Set_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
12986 if Nkind (C) = N_Range_Constraint then
12987 Range_Expr := Range_Expression (C);
12988 Digits_Val := Digits_Value (T);
12990 else
12991 pragma Assert (Nkind (C) = N_Digits_Constraint);
12993 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
12995 Digits_Expr := Digits_Expression (C);
12996 Analyze_And_Resolve (Digits_Expr, Any_Integer);
12998 Check_Digits_Expression (Digits_Expr);
12999 Digits_Val := Expr_Value (Digits_Expr);
13001 if Digits_Val > Digits_Value (T) then
13002 Error_Msg_N
13003 ("digits expression is incompatible with subtype", C);
13004 Digits_Val := Digits_Value (T);
13005 end if;
13007 if Present (Range_Constraint (C)) then
13008 Range_Expr := Range_Expression (Range_Constraint (C));
13009 else
13010 Range_Expr := Empty;
13011 end if;
13012 end if;
13014 Set_Etype (Def_Id, Base_Type (T));
13015 Set_Size_Info (Def_Id, (T));
13016 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13017 Set_Delta_Value (Def_Id, Delta_Value (T));
13018 Set_Scale_Value (Def_Id, Scale_Value (T));
13019 Set_Small_Value (Def_Id, Small_Value (T));
13020 Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
13021 Set_Digits_Value (Def_Id, Digits_Val);
13023 -- Manufacture range from given digits value if no range present
13025 if No (Range_Expr) then
13026 Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
13027 Range_Expr :=
13028 Make_Range (Loc,
13029 Low_Bound =>
13030 Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
13031 High_Bound =>
13032 Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
13033 end if;
13035 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
13036 Set_Discrete_RM_Size (Def_Id);
13038 -- Unconditionally delay the freeze, since we cannot set size
13039 -- information in all cases correctly until the freeze point.
13041 Set_Has_Delayed_Freeze (Def_Id);
13042 end Constrain_Decimal;
13044 ----------------------------------
13045 -- Constrain_Discriminated_Type --
13046 ----------------------------------
13048 procedure Constrain_Discriminated_Type
13049 (Def_Id : Entity_Id;
13050 S : Node_Id;
13051 Related_Nod : Node_Id;
13052 For_Access : Boolean := False)
13054 E : constant Entity_Id := Entity (Subtype_Mark (S));
13055 T : Entity_Id;
13056 C : Node_Id;
13057 Elist : Elist_Id := New_Elmt_List;
13059 procedure Fixup_Bad_Constraint;
13060 -- This is called after finding a bad constraint, and after having
13061 -- posted an appropriate error message. The mission is to leave the
13062 -- entity T in as reasonable state as possible.
13064 --------------------------
13065 -- Fixup_Bad_Constraint --
13066 --------------------------
13068 procedure Fixup_Bad_Constraint is
13069 begin
13070 -- Set a reasonable Ekind for the entity. For an incomplete type,
13071 -- we can't do much, but for other types, we can set the proper
13072 -- corresponding subtype kind.
13074 if Ekind (T) = E_Incomplete_Type then
13075 Set_Ekind (Def_Id, Ekind (T));
13076 else
13077 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
13078 end if;
13080 -- Set Etype to the known type, to reduce chances of cascaded errors
13082 Set_Etype (Def_Id, E);
13083 Set_Error_Posted (Def_Id);
13084 end Fixup_Bad_Constraint;
13086 -- Start of processing for Constrain_Discriminated_Type
13088 begin
13089 C := Constraint (S);
13091 -- A discriminant constraint is only allowed in a subtype indication,
13092 -- after a subtype mark. This subtype mark must denote either a type
13093 -- with discriminants, or an access type whose designated type is a
13094 -- type with discriminants. A discriminant constraint specifies the
13095 -- values of these discriminants (RM 3.7.2(5)).
13097 T := Base_Type (Entity (Subtype_Mark (S)));
13099 if Is_Access_Type (T) then
13100 T := Designated_Type (T);
13101 end if;
13103 -- Ada 2005 (AI-412): Constrained incomplete subtypes are illegal.
13104 -- Avoid generating an error for access-to-incomplete subtypes.
13106 if Ada_Version >= Ada_2005
13107 and then Ekind (T) = E_Incomplete_Type
13108 and then Nkind (Parent (S)) = N_Subtype_Declaration
13109 and then not Is_Itype (Def_Id)
13110 then
13111 -- A little sanity check, emit an error message if the type
13112 -- has discriminants to begin with. Type T may be a regular
13113 -- incomplete type or imported via a limited with clause.
13115 if Has_Discriminants (T)
13116 or else (From_Limited_With (T)
13117 and then Present (Non_Limited_View (T))
13118 and then Nkind (Parent (Non_Limited_View (T))) =
13119 N_Full_Type_Declaration
13120 and then Present (Discriminant_Specifications
13121 (Parent (Non_Limited_View (T)))))
13122 then
13123 Error_Msg_N
13124 ("(Ada 2005) incomplete subtype may not be constrained", C);
13125 else
13126 Error_Msg_N ("invalid constraint: type has no discriminant", C);
13127 end if;
13129 Fixup_Bad_Constraint;
13130 return;
13132 -- Check that the type has visible discriminants. The type may be
13133 -- a private type with unknown discriminants whose full view has
13134 -- discriminants which are invisible.
13136 elsif not Has_Discriminants (T)
13137 or else
13138 (Has_Unknown_Discriminants (T)
13139 and then Is_Private_Type (T))
13140 then
13141 Error_Msg_N ("invalid constraint: type has no discriminant", C);
13142 Fixup_Bad_Constraint;
13143 return;
13145 elsif Is_Constrained (E)
13146 or else (Ekind (E) = E_Class_Wide_Subtype
13147 and then Present (Discriminant_Constraint (E)))
13148 then
13149 Error_Msg_N ("type is already constrained", Subtype_Mark (S));
13150 Fixup_Bad_Constraint;
13151 return;
13152 end if;
13154 -- T may be an unconstrained subtype (e.g. a generic actual).
13155 -- Constraint applies to the base type.
13157 T := Base_Type (T);
13159 Elist := Build_Discriminant_Constraints (T, S);
13161 -- If the list returned was empty we had an error in building the
13162 -- discriminant constraint. We have also already signalled an error
13163 -- in the incomplete type case
13165 if Is_Empty_Elmt_List (Elist) then
13166 Fixup_Bad_Constraint;
13167 return;
13168 end if;
13170 Build_Discriminated_Subtype (T, Def_Id, Elist, Related_Nod, For_Access);
13171 end Constrain_Discriminated_Type;
13173 ---------------------------
13174 -- Constrain_Enumeration --
13175 ---------------------------
13177 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id) is
13178 T : constant Entity_Id := Entity (Subtype_Mark (S));
13179 C : constant Node_Id := Constraint (S);
13181 begin
13182 Set_Ekind (Def_Id, E_Enumeration_Subtype);
13184 Set_First_Literal (Def_Id, First_Literal (Base_Type (T)));
13186 Set_Etype (Def_Id, Base_Type (T));
13187 Set_Size_Info (Def_Id, (T));
13188 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13189 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
13191 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13193 Set_Discrete_RM_Size (Def_Id);
13194 end Constrain_Enumeration;
13196 ----------------------
13197 -- Constrain_Float --
13198 ----------------------
13200 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id) is
13201 T : constant Entity_Id := Entity (Subtype_Mark (S));
13202 C : Node_Id;
13203 D : Node_Id;
13204 Rais : Node_Id;
13206 begin
13207 Set_Ekind (Def_Id, E_Floating_Point_Subtype);
13209 Set_Etype (Def_Id, Base_Type (T));
13210 Set_Size_Info (Def_Id, (T));
13211 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13213 -- Process the constraint
13215 C := Constraint (S);
13217 -- Digits constraint present
13219 if Nkind (C) = N_Digits_Constraint then
13221 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
13222 Check_Restriction (No_Obsolescent_Features, C);
13224 if Warn_On_Obsolescent_Feature then
13225 Error_Msg_N
13226 ("subtype digits constraint is an " &
13227 "obsolescent feature (RM J.3(8))?j?", C);
13228 end if;
13230 D := Digits_Expression (C);
13231 Analyze_And_Resolve (D, Any_Integer);
13232 Check_Digits_Expression (D);
13233 Set_Digits_Value (Def_Id, Expr_Value (D));
13235 -- Check that digits value is in range. Obviously we can do this
13236 -- at compile time, but it is strictly a runtime check, and of
13237 -- course there is an ACVC test that checks this.
13239 if Digits_Value (Def_Id) > Digits_Value (T) then
13240 Error_Msg_Uint_1 := Digits_Value (T);
13241 Error_Msg_N ("??digits value is too large, maximum is ^", D);
13242 Rais :=
13243 Make_Raise_Constraint_Error (Sloc (D),
13244 Reason => CE_Range_Check_Failed);
13245 Insert_Action (Declaration_Node (Def_Id), Rais);
13246 end if;
13248 C := Range_Constraint (C);
13250 -- No digits constraint present
13252 else
13253 Set_Digits_Value (Def_Id, Digits_Value (T));
13254 end if;
13256 -- Range constraint present
13258 if Nkind (C) = N_Range_Constraint then
13259 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13261 -- No range constraint present
13263 else
13264 pragma Assert (No (C));
13265 Set_Scalar_Range (Def_Id, Scalar_Range (T));
13266 end if;
13268 Set_Is_Constrained (Def_Id);
13269 end Constrain_Float;
13271 ---------------------
13272 -- Constrain_Index --
13273 ---------------------
13275 procedure Constrain_Index
13276 (Index : Node_Id;
13277 S : Node_Id;
13278 Related_Nod : Node_Id;
13279 Related_Id : Entity_Id;
13280 Suffix : Character;
13281 Suffix_Index : Nat)
13283 Def_Id : Entity_Id;
13284 R : Node_Id := Empty;
13285 T : constant Entity_Id := Etype (Index);
13287 begin
13288 Def_Id :=
13289 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
13290 Set_Etype (Def_Id, Base_Type (T));
13292 if Nkind (S) = N_Range
13293 or else
13294 (Nkind (S) = N_Attribute_Reference
13295 and then Attribute_Name (S) = Name_Range)
13296 then
13297 -- A Range attribute will be transformed into N_Range by Resolve
13299 Analyze (S);
13300 Set_Etype (S, T);
13301 R := S;
13303 Process_Range_Expr_In_Decl (R, T);
13305 if not Error_Posted (S)
13306 and then
13307 (Nkind (S) /= N_Range
13308 or else not Covers (T, (Etype (Low_Bound (S))))
13309 or else not Covers (T, (Etype (High_Bound (S)))))
13310 then
13311 if Base_Type (T) /= Any_Type
13312 and then Etype (Low_Bound (S)) /= Any_Type
13313 and then Etype (High_Bound (S)) /= Any_Type
13314 then
13315 Error_Msg_N ("range expected", S);
13316 end if;
13317 end if;
13319 elsif Nkind (S) = N_Subtype_Indication then
13321 -- The parser has verified that this is a discrete indication
13323 Resolve_Discrete_Subtype_Indication (S, T);
13324 Bad_Predicated_Subtype_Use
13325 ("subtype& has predicate, not allowed in index constraint",
13326 S, Entity (Subtype_Mark (S)));
13328 R := Range_Expression (Constraint (S));
13330 -- Capture values of bounds and generate temporaries for them if
13331 -- needed, since checks may cause duplication of the expressions
13332 -- which must not be reevaluated.
13334 -- The forced evaluation removes side effects from expressions, which
13335 -- should occur also in GNATprove mode. Otherwise, we end up with
13336 -- unexpected insertions of actions at places where this is not
13337 -- supposed to occur, e.g. on default parameters of a call.
13339 if Expander_Active or GNATprove_Mode then
13340 Force_Evaluation
13341 (Low_Bound (R), Related_Id => Def_Id, Is_Low_Bound => True);
13342 Force_Evaluation
13343 (High_Bound (R), Related_Id => Def_Id, Is_High_Bound => True);
13344 end if;
13346 elsif Nkind (S) = N_Discriminant_Association then
13348 -- Syntactically valid in subtype indication
13350 Error_Msg_N ("invalid index constraint", S);
13351 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
13352 return;
13354 -- Subtype_Mark case, no anonymous subtypes to construct
13356 else
13357 Analyze (S);
13359 if Is_Entity_Name (S) then
13360 if not Is_Type (Entity (S)) then
13361 Error_Msg_N ("expect subtype mark for index constraint", S);
13363 elsif Base_Type (Entity (S)) /= Base_Type (T) then
13364 Wrong_Type (S, Base_Type (T));
13366 -- Check error of subtype with predicate in index constraint
13368 else
13369 Bad_Predicated_Subtype_Use
13370 ("subtype& has predicate, not allowed in index constraint",
13371 S, Entity (S));
13372 end if;
13374 return;
13376 else
13377 Error_Msg_N ("invalid index constraint", S);
13378 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
13379 return;
13380 end if;
13381 end if;
13383 -- Complete construction of the Itype
13385 if Is_Modular_Integer_Type (T) then
13386 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
13388 elsif Is_Integer_Type (T) then
13389 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
13391 else
13392 Set_Ekind (Def_Id, E_Enumeration_Subtype);
13393 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
13394 Set_First_Literal (Def_Id, First_Literal (T));
13395 end if;
13397 Set_Size_Info (Def_Id, (T));
13398 Set_RM_Size (Def_Id, RM_Size (T));
13399 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13401 Set_Scalar_Range (Def_Id, R);
13403 Set_Etype (S, Def_Id);
13404 Set_Discrete_RM_Size (Def_Id);
13405 end Constrain_Index;
13407 -----------------------
13408 -- Constrain_Integer --
13409 -----------------------
13411 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id) is
13412 T : constant Entity_Id := Entity (Subtype_Mark (S));
13413 C : constant Node_Id := Constraint (S);
13415 begin
13416 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13418 if Is_Modular_Integer_Type (T) then
13419 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
13420 else
13421 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
13422 end if;
13424 Set_Etype (Def_Id, Base_Type (T));
13425 Set_Size_Info (Def_Id, (T));
13426 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13427 Set_Discrete_RM_Size (Def_Id);
13428 end Constrain_Integer;
13430 ------------------------------
13431 -- Constrain_Ordinary_Fixed --
13432 ------------------------------
13434 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id) is
13435 T : constant Entity_Id := Entity (Subtype_Mark (S));
13436 C : Node_Id;
13437 D : Node_Id;
13438 Rais : Node_Id;
13440 begin
13441 Set_Ekind (Def_Id, E_Ordinary_Fixed_Point_Subtype);
13442 Set_Etype (Def_Id, Base_Type (T));
13443 Set_Size_Info (Def_Id, (T));
13444 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13445 Set_Small_Value (Def_Id, Small_Value (T));
13447 -- Process the constraint
13449 C := Constraint (S);
13451 -- Delta constraint present
13453 if Nkind (C) = N_Delta_Constraint then
13455 Check_SPARK_05_Restriction ("delta constraint is not allowed", S);
13456 Check_Restriction (No_Obsolescent_Features, C);
13458 if Warn_On_Obsolescent_Feature then
13459 Error_Msg_S
13460 ("subtype delta constraint is an " &
13461 "obsolescent feature (RM J.3(7))?j?");
13462 end if;
13464 D := Delta_Expression (C);
13465 Analyze_And_Resolve (D, Any_Real);
13466 Check_Delta_Expression (D);
13467 Set_Delta_Value (Def_Id, Expr_Value_R (D));
13469 -- Check that delta value is in range. Obviously we can do this
13470 -- at compile time, but it is strictly a runtime check, and of
13471 -- course there is an ACVC test that checks this.
13473 if Delta_Value (Def_Id) < Delta_Value (T) then
13474 Error_Msg_N ("??delta value is too small", D);
13475 Rais :=
13476 Make_Raise_Constraint_Error (Sloc (D),
13477 Reason => CE_Range_Check_Failed);
13478 Insert_Action (Declaration_Node (Def_Id), Rais);
13479 end if;
13481 C := Range_Constraint (C);
13483 -- No delta constraint present
13485 else
13486 Set_Delta_Value (Def_Id, Delta_Value (T));
13487 end if;
13489 -- Range constraint present
13491 if Nkind (C) = N_Range_Constraint then
13492 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13494 -- No range constraint present
13496 else
13497 pragma Assert (No (C));
13498 Set_Scalar_Range (Def_Id, Scalar_Range (T));
13499 end if;
13501 Set_Discrete_RM_Size (Def_Id);
13503 -- Unconditionally delay the freeze, since we cannot set size
13504 -- information in all cases correctly until the freeze point.
13506 Set_Has_Delayed_Freeze (Def_Id);
13507 end Constrain_Ordinary_Fixed;
13509 -----------------------
13510 -- Contain_Interface --
13511 -----------------------
13513 function Contain_Interface
13514 (Iface : Entity_Id;
13515 Ifaces : Elist_Id) return Boolean
13517 Iface_Elmt : Elmt_Id;
13519 begin
13520 if Present (Ifaces) then
13521 Iface_Elmt := First_Elmt (Ifaces);
13522 while Present (Iface_Elmt) loop
13523 if Node (Iface_Elmt) = Iface then
13524 return True;
13525 end if;
13527 Next_Elmt (Iface_Elmt);
13528 end loop;
13529 end if;
13531 return False;
13532 end Contain_Interface;
13534 ---------------------------
13535 -- Convert_Scalar_Bounds --
13536 ---------------------------
13538 procedure Convert_Scalar_Bounds
13539 (N : Node_Id;
13540 Parent_Type : Entity_Id;
13541 Derived_Type : Entity_Id;
13542 Loc : Source_Ptr)
13544 Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
13546 Lo : Node_Id;
13547 Hi : Node_Id;
13548 Rng : Node_Id;
13550 begin
13551 -- Defend against previous errors
13553 if No (Scalar_Range (Derived_Type)) then
13554 Check_Error_Detected;
13555 return;
13556 end if;
13558 Lo := Build_Scalar_Bound
13559 (Type_Low_Bound (Derived_Type),
13560 Parent_Type, Implicit_Base);
13562 Hi := Build_Scalar_Bound
13563 (Type_High_Bound (Derived_Type),
13564 Parent_Type, Implicit_Base);
13566 Rng :=
13567 Make_Range (Loc,
13568 Low_Bound => Lo,
13569 High_Bound => Hi);
13571 Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
13573 Set_Parent (Rng, N);
13574 Set_Scalar_Range (Derived_Type, Rng);
13576 -- Analyze the bounds
13578 Analyze_And_Resolve (Lo, Implicit_Base);
13579 Analyze_And_Resolve (Hi, Implicit_Base);
13581 -- Analyze the range itself, except that we do not analyze it if
13582 -- the bounds are real literals, and we have a fixed-point type.
13583 -- The reason for this is that we delay setting the bounds in this
13584 -- case till we know the final Small and Size values (see circuit
13585 -- in Freeze.Freeze_Fixed_Point_Type for further details).
13587 if Is_Fixed_Point_Type (Parent_Type)
13588 and then Nkind (Lo) = N_Real_Literal
13589 and then Nkind (Hi) = N_Real_Literal
13590 then
13591 return;
13593 -- Here we do the analysis of the range
13595 -- Note: we do this manually, since if we do a normal Analyze and
13596 -- Resolve call, there are problems with the conversions used for
13597 -- the derived type range.
13599 else
13600 Set_Etype (Rng, Implicit_Base);
13601 Set_Analyzed (Rng, True);
13602 end if;
13603 end Convert_Scalar_Bounds;
13605 -------------------
13606 -- Copy_And_Swap --
13607 -------------------
13609 procedure Copy_And_Swap (Priv, Full : Entity_Id) is
13610 begin
13611 -- Initialize new full declaration entity by copying the pertinent
13612 -- fields of the corresponding private declaration entity.
13614 -- We temporarily set Ekind to a value appropriate for a type to
13615 -- avoid assert failures in Einfo from checking for setting type
13616 -- attributes on something that is not a type. Ekind (Priv) is an
13617 -- appropriate choice, since it allowed the attributes to be set
13618 -- in the first place. This Ekind value will be modified later.
13620 Set_Ekind (Full, Ekind (Priv));
13622 -- Also set Etype temporarily to Any_Type, again, in the absence
13623 -- of errors, it will be properly reset, and if there are errors,
13624 -- then we want a value of Any_Type to remain.
13626 Set_Etype (Full, Any_Type);
13628 -- Now start copying attributes
13630 Set_Has_Discriminants (Full, Has_Discriminants (Priv));
13632 if Has_Discriminants (Full) then
13633 Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
13634 Set_Stored_Constraint (Full, Stored_Constraint (Priv));
13635 end if;
13637 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
13638 Set_Homonym (Full, Homonym (Priv));
13639 Set_Is_Immediately_Visible (Full, Is_Immediately_Visible (Priv));
13640 Set_Is_Public (Full, Is_Public (Priv));
13641 Set_Is_Pure (Full, Is_Pure (Priv));
13642 Set_Is_Tagged_Type (Full, Is_Tagged_Type (Priv));
13643 Set_Has_Pragma_Unmodified (Full, Has_Pragma_Unmodified (Priv));
13644 Set_Has_Pragma_Unreferenced (Full, Has_Pragma_Unreferenced (Priv));
13645 Set_Has_Pragma_Unreferenced_Objects
13646 (Full, Has_Pragma_Unreferenced_Objects
13647 (Priv));
13649 Conditional_Delay (Full, Priv);
13651 if Is_Tagged_Type (Full) then
13652 Set_Direct_Primitive_Operations
13653 (Full, Direct_Primitive_Operations (Priv));
13654 Set_No_Tagged_Streams_Pragma
13655 (Full, No_Tagged_Streams_Pragma (Priv));
13657 if Is_Base_Type (Priv) then
13658 Set_Class_Wide_Type (Full, Class_Wide_Type (Priv));
13659 end if;
13660 end if;
13662 Set_Is_Volatile (Full, Is_Volatile (Priv));
13663 Set_Treat_As_Volatile (Full, Treat_As_Volatile (Priv));
13664 Set_Scope (Full, Scope (Priv));
13665 Set_Next_Entity (Full, Next_Entity (Priv));
13666 Set_First_Entity (Full, First_Entity (Priv));
13667 Set_Last_Entity (Full, Last_Entity (Priv));
13669 -- If access types have been recorded for later handling, keep them in
13670 -- the full view so that they get handled when the full view freeze
13671 -- node is expanded.
13673 if Present (Freeze_Node (Priv))
13674 and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
13675 then
13676 Ensure_Freeze_Node (Full);
13677 Set_Access_Types_To_Process
13678 (Freeze_Node (Full),
13679 Access_Types_To_Process (Freeze_Node (Priv)));
13680 end if;
13682 -- Swap the two entities. Now Private is the full type entity and Full
13683 -- is the private one. They will be swapped back at the end of the
13684 -- private part. This swapping ensures that the entity that is visible
13685 -- in the private part is the full declaration.
13687 Exchange_Entities (Priv, Full);
13688 Append_Entity (Full, Scope (Full));
13689 end Copy_And_Swap;
13691 -------------------------------------
13692 -- Copy_Array_Base_Type_Attributes --
13693 -------------------------------------
13695 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
13696 begin
13697 Set_Component_Alignment (T1, Component_Alignment (T2));
13698 Set_Component_Type (T1, Component_Type (T2));
13699 Set_Component_Size (T1, Component_Size (T2));
13700 Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
13701 Set_Has_Non_Standard_Rep (T1, Has_Non_Standard_Rep (T2));
13702 Set_Has_Protected (T1, Has_Protected (T2));
13703 Set_Has_Task (T1, Has_Task (T2));
13704 Set_Is_Packed (T1, Is_Packed (T2));
13705 Set_Has_Aliased_Components (T1, Has_Aliased_Components (T2));
13706 Set_Has_Atomic_Components (T1, Has_Atomic_Components (T2));
13707 Set_Has_Volatile_Components (T1, Has_Volatile_Components (T2));
13708 end Copy_Array_Base_Type_Attributes;
13710 -----------------------------------
13711 -- Copy_Array_Subtype_Attributes --
13712 -----------------------------------
13714 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
13715 begin
13716 Set_Size_Info (T1, T2);
13718 Set_First_Index (T1, First_Index (T2));
13719 Set_Is_Aliased (T1, Is_Aliased (T2));
13720 Set_Is_Volatile (T1, Is_Volatile (T2));
13721 Set_Treat_As_Volatile (T1, Treat_As_Volatile (T2));
13722 Set_Is_Constrained (T1, Is_Constrained (T2));
13723 Set_Depends_On_Private (T1, Has_Private_Component (T2));
13724 Inherit_Rep_Item_Chain (T1, T2);
13725 Set_Convention (T1, Convention (T2));
13726 Set_Is_Limited_Composite (T1, Is_Limited_Composite (T2));
13727 Set_Is_Private_Composite (T1, Is_Private_Composite (T2));
13728 Set_Packed_Array_Impl_Type (T1, Packed_Array_Impl_Type (T2));
13729 end Copy_Array_Subtype_Attributes;
13731 -----------------------------------
13732 -- Create_Constrained_Components --
13733 -----------------------------------
13735 procedure Create_Constrained_Components
13736 (Subt : Entity_Id;
13737 Decl_Node : Node_Id;
13738 Typ : Entity_Id;
13739 Constraints : Elist_Id)
13741 Loc : constant Source_Ptr := Sloc (Subt);
13742 Comp_List : constant Elist_Id := New_Elmt_List;
13743 Parent_Type : constant Entity_Id := Etype (Typ);
13744 Assoc_List : constant List_Id := New_List;
13745 Discr_Val : Elmt_Id;
13746 Errors : Boolean;
13747 New_C : Entity_Id;
13748 Old_C : Entity_Id;
13749 Is_Static : Boolean := True;
13751 procedure Collect_Fixed_Components (Typ : Entity_Id);
13752 -- Collect parent type components that do not appear in a variant part
13754 procedure Create_All_Components;
13755 -- Iterate over Comp_List to create the components of the subtype
13757 function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
13758 -- Creates a new component from Old_Compon, copying all the fields from
13759 -- it, including its Etype, inserts the new component in the Subt entity
13760 -- chain and returns the new component.
13762 function Is_Variant_Record (T : Entity_Id) return Boolean;
13763 -- If true, and discriminants are static, collect only components from
13764 -- variants selected by discriminant values.
13766 ------------------------------
13767 -- Collect_Fixed_Components --
13768 ------------------------------
13770 procedure Collect_Fixed_Components (Typ : Entity_Id) is
13771 begin
13772 -- Build association list for discriminants, and find components of the
13773 -- variant part selected by the values of the discriminants.
13775 Old_C := First_Discriminant (Typ);
13776 Discr_Val := First_Elmt (Constraints);
13777 while Present (Old_C) loop
13778 Append_To (Assoc_List,
13779 Make_Component_Association (Loc,
13780 Choices => New_List (New_Occurrence_Of (Old_C, Loc)),
13781 Expression => New_Copy (Node (Discr_Val))));
13783 Next_Elmt (Discr_Val);
13784 Next_Discriminant (Old_C);
13785 end loop;
13787 -- The tag and the possible parent component are unconditionally in
13788 -- the subtype.
13790 if Is_Tagged_Type (Typ) or else Has_Controlled_Component (Typ) then
13791 Old_C := First_Component (Typ);
13792 while Present (Old_C) loop
13793 if Nam_In (Chars (Old_C), Name_uTag, Name_uParent) then
13794 Append_Elmt (Old_C, Comp_List);
13795 end if;
13797 Next_Component (Old_C);
13798 end loop;
13799 end if;
13800 end Collect_Fixed_Components;
13802 ---------------------------
13803 -- Create_All_Components --
13804 ---------------------------
13806 procedure Create_All_Components is
13807 Comp : Elmt_Id;
13809 begin
13810 Comp := First_Elmt (Comp_List);
13811 while Present (Comp) loop
13812 Old_C := Node (Comp);
13813 New_C := Create_Component (Old_C);
13815 Set_Etype
13816 (New_C,
13817 Constrain_Component_Type
13818 (Old_C, Subt, Decl_Node, Typ, Constraints));
13819 Set_Is_Public (New_C, Is_Public (Subt));
13821 Next_Elmt (Comp);
13822 end loop;
13823 end Create_All_Components;
13825 ----------------------
13826 -- Create_Component --
13827 ----------------------
13829 function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
13830 New_Compon : constant Entity_Id := New_Copy (Old_Compon);
13832 begin
13833 if Ekind (Old_Compon) = E_Discriminant
13834 and then Is_Completely_Hidden (Old_Compon)
13835 then
13836 -- This is a shadow discriminant created for a discriminant of
13837 -- the parent type, which needs to be present in the subtype.
13838 -- Give the shadow discriminant an internal name that cannot
13839 -- conflict with that of visible components.
13841 Set_Chars (New_Compon, New_Internal_Name ('C'));
13842 end if;
13844 -- Set the parent so we have a proper link for freezing etc. This is
13845 -- not a real parent pointer, since of course our parent does not own
13846 -- up to us and reference us, we are an illegitimate child of the
13847 -- original parent.
13849 Set_Parent (New_Compon, Parent (Old_Compon));
13851 -- If the old component's Esize was already determined and is a
13852 -- static value, then the new component simply inherits it. Otherwise
13853 -- the old component's size may require run-time determination, but
13854 -- the new component's size still might be statically determinable
13855 -- (if, for example it has a static constraint). In that case we want
13856 -- Layout_Type to recompute the component's size, so we reset its
13857 -- size and positional fields.
13859 if Frontend_Layout_On_Target
13860 and then not Known_Static_Esize (Old_Compon)
13861 then
13862 Set_Esize (New_Compon, Uint_0);
13863 Init_Normalized_First_Bit (New_Compon);
13864 Init_Normalized_Position (New_Compon);
13865 Init_Normalized_Position_Max (New_Compon);
13866 end if;
13868 -- We do not want this node marked as Comes_From_Source, since
13869 -- otherwise it would get first class status and a separate cross-
13870 -- reference line would be generated. Illegitimate children do not
13871 -- rate such recognition.
13873 Set_Comes_From_Source (New_Compon, False);
13875 -- But it is a real entity, and a birth certificate must be properly
13876 -- registered by entering it into the entity list.
13878 Enter_Name (New_Compon);
13880 return New_Compon;
13881 end Create_Component;
13883 -----------------------
13884 -- Is_Variant_Record --
13885 -----------------------
13887 function Is_Variant_Record (T : Entity_Id) return Boolean is
13888 begin
13889 return Nkind (Parent (T)) = N_Full_Type_Declaration
13890 and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
13891 and then Present (Component_List (Type_Definition (Parent (T))))
13892 and then
13893 Present
13894 (Variant_Part (Component_List (Type_Definition (Parent (T)))));
13895 end Is_Variant_Record;
13897 -- Start of processing for Create_Constrained_Components
13899 begin
13900 pragma Assert (Subt /= Base_Type (Subt));
13901 pragma Assert (Typ = Base_Type (Typ));
13903 Set_First_Entity (Subt, Empty);
13904 Set_Last_Entity (Subt, Empty);
13906 -- Check whether constraint is fully static, in which case we can
13907 -- optimize the list of components.
13909 Discr_Val := First_Elmt (Constraints);
13910 while Present (Discr_Val) loop
13911 if not Is_OK_Static_Expression (Node (Discr_Val)) then
13912 Is_Static := False;
13913 exit;
13914 end if;
13916 Next_Elmt (Discr_Val);
13917 end loop;
13919 Set_Has_Static_Discriminants (Subt, Is_Static);
13921 Push_Scope (Subt);
13923 -- Inherit the discriminants of the parent type
13925 Add_Discriminants : declare
13926 Num_Disc : Int;
13927 Num_Gird : Int;
13929 begin
13930 Num_Disc := 0;
13931 Old_C := First_Discriminant (Typ);
13933 while Present (Old_C) loop
13934 Num_Disc := Num_Disc + 1;
13935 New_C := Create_Component (Old_C);
13936 Set_Is_Public (New_C, Is_Public (Subt));
13937 Next_Discriminant (Old_C);
13938 end loop;
13940 -- For an untagged derived subtype, the number of discriminants may
13941 -- be smaller than the number of inherited discriminants, because
13942 -- several of them may be renamed by a single new discriminant or
13943 -- constrained. In this case, add the hidden discriminants back into
13944 -- the subtype, because they need to be present if the optimizer of
13945 -- the GCC 4.x back-end decides to break apart assignments between
13946 -- objects using the parent view into member-wise assignments.
13948 Num_Gird := 0;
13950 if Is_Derived_Type (Typ)
13951 and then not Is_Tagged_Type (Typ)
13952 then
13953 Old_C := First_Stored_Discriminant (Typ);
13955 while Present (Old_C) loop
13956 Num_Gird := Num_Gird + 1;
13957 Next_Stored_Discriminant (Old_C);
13958 end loop;
13959 end if;
13961 if Num_Gird > Num_Disc then
13963 -- Find out multiple uses of new discriminants, and add hidden
13964 -- components for the extra renamed discriminants. We recognize
13965 -- multiple uses through the Corresponding_Discriminant of a
13966 -- new discriminant: if it constrains several old discriminants,
13967 -- this field points to the last one in the parent type. The
13968 -- stored discriminants of the derived type have the same name
13969 -- as those of the parent.
13971 declare
13972 Constr : Elmt_Id;
13973 New_Discr : Entity_Id;
13974 Old_Discr : Entity_Id;
13976 begin
13977 Constr := First_Elmt (Stored_Constraint (Typ));
13978 Old_Discr := First_Stored_Discriminant (Typ);
13979 while Present (Constr) loop
13980 if Is_Entity_Name (Node (Constr))
13981 and then Ekind (Entity (Node (Constr))) = E_Discriminant
13982 then
13983 New_Discr := Entity (Node (Constr));
13985 if Chars (Corresponding_Discriminant (New_Discr)) /=
13986 Chars (Old_Discr)
13987 then
13988 -- The new discriminant has been used to rename a
13989 -- subsequent old discriminant. Introduce a shadow
13990 -- component for the current old discriminant.
13992 New_C := Create_Component (Old_Discr);
13993 Set_Original_Record_Component (New_C, Old_Discr);
13994 end if;
13996 else
13997 -- The constraint has eliminated the old discriminant.
13998 -- Introduce a shadow component.
14000 New_C := Create_Component (Old_Discr);
14001 Set_Original_Record_Component (New_C, Old_Discr);
14002 end if;
14004 Next_Elmt (Constr);
14005 Next_Stored_Discriminant (Old_Discr);
14006 end loop;
14007 end;
14008 end if;
14009 end Add_Discriminants;
14011 if Is_Static
14012 and then Is_Variant_Record (Typ)
14013 then
14014 Collect_Fixed_Components (Typ);
14016 Gather_Components (
14017 Typ,
14018 Component_List (Type_Definition (Parent (Typ))),
14019 Governed_By => Assoc_List,
14020 Into => Comp_List,
14021 Report_Errors => Errors);
14022 pragma Assert (not Errors);
14024 Create_All_Components;
14026 -- If the subtype declaration is created for a tagged type derivation
14027 -- with constraints, we retrieve the record definition of the parent
14028 -- type to select the components of the proper variant.
14030 elsif Is_Static
14031 and then Is_Tagged_Type (Typ)
14032 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
14033 and then
14034 Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
14035 and then Is_Variant_Record (Parent_Type)
14036 then
14037 Collect_Fixed_Components (Typ);
14039 Gather_Components
14040 (Typ,
14041 Component_List (Type_Definition (Parent (Parent_Type))),
14042 Governed_By => Assoc_List,
14043 Into => Comp_List,
14044 Report_Errors => Errors);
14046 -- Note: previously there was a check at this point that no errors
14047 -- were detected. As a consequence of AI05-220 there may be an error
14048 -- if an inherited discriminant that controls a variant has a non-
14049 -- static constraint.
14051 -- If the tagged derivation has a type extension, collect all the
14052 -- new components therein.
14054 if Present (Record_Extension_Part (Type_Definition (Parent (Typ))))
14055 then
14056 Old_C := First_Component (Typ);
14057 while Present (Old_C) loop
14058 if Original_Record_Component (Old_C) = Old_C
14059 and then Chars (Old_C) /= Name_uTag
14060 and then Chars (Old_C) /= Name_uParent
14061 then
14062 Append_Elmt (Old_C, Comp_List);
14063 end if;
14065 Next_Component (Old_C);
14066 end loop;
14067 end if;
14069 Create_All_Components;
14071 else
14072 -- If discriminants are not static, or if this is a multi-level type
14073 -- extension, we have to include all components of the parent type.
14075 Old_C := First_Component (Typ);
14076 while Present (Old_C) loop
14077 New_C := Create_Component (Old_C);
14079 Set_Etype
14080 (New_C,
14081 Constrain_Component_Type
14082 (Old_C, Subt, Decl_Node, Typ, Constraints));
14083 Set_Is_Public (New_C, Is_Public (Subt));
14085 Next_Component (Old_C);
14086 end loop;
14087 end if;
14089 End_Scope;
14090 end Create_Constrained_Components;
14092 ------------------------------------------
14093 -- Decimal_Fixed_Point_Type_Declaration --
14094 ------------------------------------------
14096 procedure Decimal_Fixed_Point_Type_Declaration
14097 (T : Entity_Id;
14098 Def : Node_Id)
14100 Loc : constant Source_Ptr := Sloc (Def);
14101 Digs_Expr : constant Node_Id := Digits_Expression (Def);
14102 Delta_Expr : constant Node_Id := Delta_Expression (Def);
14103 Implicit_Base : Entity_Id;
14104 Digs_Val : Uint;
14105 Delta_Val : Ureal;
14106 Scale_Val : Uint;
14107 Bound_Val : Ureal;
14109 begin
14110 Check_SPARK_05_Restriction
14111 ("decimal fixed point type is not allowed", Def);
14112 Check_Restriction (No_Fixed_Point, Def);
14114 -- Create implicit base type
14116 Implicit_Base :=
14117 Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
14118 Set_Etype (Implicit_Base, Implicit_Base);
14120 -- Analyze and process delta expression
14122 Analyze_And_Resolve (Delta_Expr, Universal_Real);
14124 Check_Delta_Expression (Delta_Expr);
14125 Delta_Val := Expr_Value_R (Delta_Expr);
14127 -- Check delta is power of 10, and determine scale value from it
14129 declare
14130 Val : Ureal;
14132 begin
14133 Scale_Val := Uint_0;
14134 Val := Delta_Val;
14136 if Val < Ureal_1 then
14137 while Val < Ureal_1 loop
14138 Val := Val * Ureal_10;
14139 Scale_Val := Scale_Val + 1;
14140 end loop;
14142 if Scale_Val > 18 then
14143 Error_Msg_N ("scale exceeds maximum value of 18", Def);
14144 Scale_Val := UI_From_Int (+18);
14145 end if;
14147 else
14148 while Val > Ureal_1 loop
14149 Val := Val / Ureal_10;
14150 Scale_Val := Scale_Val - 1;
14151 end loop;
14153 if Scale_Val < -18 then
14154 Error_Msg_N ("scale is less than minimum value of -18", Def);
14155 Scale_Val := UI_From_Int (-18);
14156 end if;
14157 end if;
14159 if Val /= Ureal_1 then
14160 Error_Msg_N ("delta expression must be a power of 10", Def);
14161 Delta_Val := Ureal_10 ** (-Scale_Val);
14162 end if;
14163 end;
14165 -- Set delta, scale and small (small = delta for decimal type)
14167 Set_Delta_Value (Implicit_Base, Delta_Val);
14168 Set_Scale_Value (Implicit_Base, Scale_Val);
14169 Set_Small_Value (Implicit_Base, Delta_Val);
14171 -- Analyze and process digits expression
14173 Analyze_And_Resolve (Digs_Expr, Any_Integer);
14174 Check_Digits_Expression (Digs_Expr);
14175 Digs_Val := Expr_Value (Digs_Expr);
14177 if Digs_Val > 18 then
14178 Digs_Val := UI_From_Int (+18);
14179 Error_Msg_N ("digits value out of range, maximum is 18", Digs_Expr);
14180 end if;
14182 Set_Digits_Value (Implicit_Base, Digs_Val);
14183 Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
14185 -- Set range of base type from digits value for now. This will be
14186 -- expanded to represent the true underlying base range by Freeze.
14188 Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
14190 -- Note: We leave size as zero for now, size will be set at freeze
14191 -- time. We have to do this for ordinary fixed-point, because the size
14192 -- depends on the specified small, and we might as well do the same for
14193 -- decimal fixed-point.
14195 pragma Assert (Esize (Implicit_Base) = Uint_0);
14197 -- If there are bounds given in the declaration use them as the
14198 -- bounds of the first named subtype.
14200 if Present (Real_Range_Specification (Def)) then
14201 declare
14202 RRS : constant Node_Id := Real_Range_Specification (Def);
14203 Low : constant Node_Id := Low_Bound (RRS);
14204 High : constant Node_Id := High_Bound (RRS);
14205 Low_Val : Ureal;
14206 High_Val : Ureal;
14208 begin
14209 Analyze_And_Resolve (Low, Any_Real);
14210 Analyze_And_Resolve (High, Any_Real);
14211 Check_Real_Bound (Low);
14212 Check_Real_Bound (High);
14213 Low_Val := Expr_Value_R (Low);
14214 High_Val := Expr_Value_R (High);
14216 if Low_Val < (-Bound_Val) then
14217 Error_Msg_N
14218 ("range low bound too small for digits value", Low);
14219 Low_Val := -Bound_Val;
14220 end if;
14222 if High_Val > Bound_Val then
14223 Error_Msg_N
14224 ("range high bound too large for digits value", High);
14225 High_Val := Bound_Val;
14226 end if;
14228 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
14229 end;
14231 -- If no explicit range, use range that corresponds to given
14232 -- digits value. This will end up as the final range for the
14233 -- first subtype.
14235 else
14236 Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
14237 end if;
14239 -- Complete entity for first subtype. The inheritance of the rep item
14240 -- chain ensures that SPARK-related pragmas are not clobbered when the
14241 -- decimal fixed point type acts as a full view of a private type.
14243 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
14244 Set_Etype (T, Implicit_Base);
14245 Set_Size_Info (T, Implicit_Base);
14246 Inherit_Rep_Item_Chain (T, Implicit_Base);
14247 Set_Digits_Value (T, Digs_Val);
14248 Set_Delta_Value (T, Delta_Val);
14249 Set_Small_Value (T, Delta_Val);
14250 Set_Scale_Value (T, Scale_Val);
14251 Set_Is_Constrained (T);
14252 end Decimal_Fixed_Point_Type_Declaration;
14254 -----------------------------------
14255 -- Derive_Progenitor_Subprograms --
14256 -----------------------------------
14258 procedure Derive_Progenitor_Subprograms
14259 (Parent_Type : Entity_Id;
14260 Tagged_Type : Entity_Id)
14262 E : Entity_Id;
14263 Elmt : Elmt_Id;
14264 Iface : Entity_Id;
14265 Iface_Elmt : Elmt_Id;
14266 Iface_Subp : Entity_Id;
14267 New_Subp : Entity_Id := Empty;
14268 Prim_Elmt : Elmt_Id;
14269 Subp : Entity_Id;
14270 Typ : Entity_Id;
14272 begin
14273 pragma Assert (Ada_Version >= Ada_2005
14274 and then Is_Record_Type (Tagged_Type)
14275 and then Is_Tagged_Type (Tagged_Type)
14276 and then Has_Interfaces (Tagged_Type));
14278 -- Step 1: Transfer to the full-view primitives associated with the
14279 -- partial-view that cover interface primitives. Conceptually this
14280 -- work should be done later by Process_Full_View; done here to
14281 -- simplify its implementation at later stages. It can be safely
14282 -- done here because interfaces must be visible in the partial and
14283 -- private view (RM 7.3(7.3/2)).
14285 -- Small optimization: This work is only required if the parent may
14286 -- have entities whose Alias attribute reference an interface primitive.
14287 -- Such a situation may occur if the parent is an abstract type and the
14288 -- primitive has not been yet overridden or if the parent is a generic
14289 -- formal type covering interfaces.
14291 -- If the tagged type is not abstract, it cannot have abstract
14292 -- primitives (the only entities in the list of primitives of
14293 -- non-abstract tagged types that can reference abstract primitives
14294 -- through its Alias attribute are the internal entities that have
14295 -- attribute Interface_Alias, and these entities are generated later
14296 -- by Add_Internal_Interface_Entities).
14298 if In_Private_Part (Current_Scope)
14299 and then (Is_Abstract_Type (Parent_Type)
14300 or else
14301 Is_Generic_Type (Parent_Type))
14302 then
14303 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
14304 while Present (Elmt) loop
14305 Subp := Node (Elmt);
14307 -- At this stage it is not possible to have entities in the list
14308 -- of primitives that have attribute Interface_Alias.
14310 pragma Assert (No (Interface_Alias (Subp)));
14312 Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
14314 if Is_Interface (Typ) then
14315 E := Find_Primitive_Covering_Interface
14316 (Tagged_Type => Tagged_Type,
14317 Iface_Prim => Subp);
14319 if Present (E)
14320 and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
14321 then
14322 Replace_Elmt (Elmt, E);
14323 Remove_Homonym (Subp);
14324 end if;
14325 end if;
14327 Next_Elmt (Elmt);
14328 end loop;
14329 end if;
14331 -- Step 2: Add primitives of progenitors that are not implemented by
14332 -- parents of Tagged_Type.
14334 if Present (Interfaces (Base_Type (Tagged_Type))) then
14335 Iface_Elmt := First_Elmt (Interfaces (Base_Type (Tagged_Type)));
14336 while Present (Iface_Elmt) loop
14337 Iface := Node (Iface_Elmt);
14339 Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
14340 while Present (Prim_Elmt) loop
14341 Iface_Subp := Node (Prim_Elmt);
14343 -- Exclude derivation of predefined primitives except those
14344 -- that come from source, or are inherited from one that comes
14345 -- from source. Required to catch declarations of equality
14346 -- operators of interfaces. For example:
14348 -- type Iface is interface;
14349 -- function "=" (Left, Right : Iface) return Boolean;
14351 if not Is_Predefined_Dispatching_Operation (Iface_Subp)
14352 or else Comes_From_Source (Ultimate_Alias (Iface_Subp))
14353 then
14354 E := Find_Primitive_Covering_Interface
14355 (Tagged_Type => Tagged_Type,
14356 Iface_Prim => Iface_Subp);
14358 -- If not found we derive a new primitive leaving its alias
14359 -- attribute referencing the interface primitive.
14361 if No (E) then
14362 Derive_Subprogram
14363 (New_Subp, Iface_Subp, Tagged_Type, Iface);
14365 -- Ada 2012 (AI05-0197): If the covering primitive's name
14366 -- differs from the name of the interface primitive then it
14367 -- is a private primitive inherited from a parent type. In
14368 -- such case, given that Tagged_Type covers the interface,
14369 -- the inherited private primitive becomes visible. For such
14370 -- purpose we add a new entity that renames the inherited
14371 -- private primitive.
14373 elsif Chars (E) /= Chars (Iface_Subp) then
14374 pragma Assert (Has_Suffix (E, 'P'));
14375 Derive_Subprogram
14376 (New_Subp, Iface_Subp, Tagged_Type, Iface);
14377 Set_Alias (New_Subp, E);
14378 Set_Is_Abstract_Subprogram (New_Subp,
14379 Is_Abstract_Subprogram (E));
14381 -- Propagate to the full view interface entities associated
14382 -- with the partial view.
14384 elsif In_Private_Part (Current_Scope)
14385 and then Present (Alias (E))
14386 and then Alias (E) = Iface_Subp
14387 and then
14388 List_Containing (Parent (E)) /=
14389 Private_Declarations
14390 (Specification
14391 (Unit_Declaration_Node (Current_Scope)))
14392 then
14393 Append_Elmt (E, Primitive_Operations (Tagged_Type));
14394 end if;
14395 end if;
14397 Next_Elmt (Prim_Elmt);
14398 end loop;
14400 Next_Elmt (Iface_Elmt);
14401 end loop;
14402 end if;
14403 end Derive_Progenitor_Subprograms;
14405 -----------------------
14406 -- Derive_Subprogram --
14407 -----------------------
14409 procedure Derive_Subprogram
14410 (New_Subp : in out Entity_Id;
14411 Parent_Subp : Entity_Id;
14412 Derived_Type : Entity_Id;
14413 Parent_Type : Entity_Id;
14414 Actual_Subp : Entity_Id := Empty)
14416 Formal : Entity_Id;
14417 -- Formal parameter of parent primitive operation
14419 Formal_Of_Actual : Entity_Id;
14420 -- Formal parameter of actual operation, when the derivation is to
14421 -- create a renaming for a primitive operation of an actual in an
14422 -- instantiation.
14424 New_Formal : Entity_Id;
14425 -- Formal of inherited operation
14427 Visible_Subp : Entity_Id := Parent_Subp;
14429 function Is_Private_Overriding return Boolean;
14430 -- If Subp is a private overriding of a visible operation, the inherited
14431 -- operation derives from the overridden op (even though its body is the
14432 -- overriding one) and the inherited operation is visible now. See
14433 -- sem_disp to see the full details of the handling of the overridden
14434 -- subprogram, which is removed from the list of primitive operations of
14435 -- the type. The overridden subprogram is saved locally in Visible_Subp,
14436 -- and used to diagnose abstract operations that need overriding in the
14437 -- derived type.
14439 procedure Replace_Type (Id, New_Id : Entity_Id);
14440 -- When the type is an anonymous access type, create a new access type
14441 -- designating the derived type.
14443 procedure Set_Derived_Name;
14444 -- This procedure sets the appropriate Chars name for New_Subp. This
14445 -- is normally just a copy of the parent name. An exception arises for
14446 -- type support subprograms, where the name is changed to reflect the
14447 -- name of the derived type, e.g. if type foo is derived from type bar,
14448 -- then a procedure barDA is derived with a name fooDA.
14450 ---------------------------
14451 -- Is_Private_Overriding --
14452 ---------------------------
14454 function Is_Private_Overriding return Boolean is
14455 Prev : Entity_Id;
14457 begin
14458 -- If the parent is not a dispatching operation there is no
14459 -- need to investigate overridings
14461 if not Is_Dispatching_Operation (Parent_Subp) then
14462 return False;
14463 end if;
14465 -- The visible operation that is overridden is a homonym of the
14466 -- parent subprogram. We scan the homonym chain to find the one
14467 -- whose alias is the subprogram we are deriving.
14469 Prev := Current_Entity (Parent_Subp);
14470 while Present (Prev) loop
14471 if Ekind (Prev) = Ekind (Parent_Subp)
14472 and then Alias (Prev) = Parent_Subp
14473 and then Scope (Parent_Subp) = Scope (Prev)
14474 and then not Is_Hidden (Prev)
14475 then
14476 Visible_Subp := Prev;
14477 return True;
14478 end if;
14480 Prev := Homonym (Prev);
14481 end loop;
14483 return False;
14484 end Is_Private_Overriding;
14486 ------------------
14487 -- Replace_Type --
14488 ------------------
14490 procedure Replace_Type (Id, New_Id : Entity_Id) is
14491 Id_Type : constant Entity_Id := Etype (Id);
14492 Acc_Type : Entity_Id;
14493 Par : constant Node_Id := Parent (Derived_Type);
14495 begin
14496 -- When the type is an anonymous access type, create a new access
14497 -- type designating the derived type. This itype must be elaborated
14498 -- at the point of the derivation, not on subsequent calls that may
14499 -- be out of the proper scope for Gigi, so we insert a reference to
14500 -- it after the derivation.
14502 if Ekind (Id_Type) = E_Anonymous_Access_Type then
14503 declare
14504 Desig_Typ : Entity_Id := Designated_Type (Id_Type);
14506 begin
14507 if Ekind (Desig_Typ) = E_Record_Type_With_Private
14508 and then Present (Full_View (Desig_Typ))
14509 and then not Is_Private_Type (Parent_Type)
14510 then
14511 Desig_Typ := Full_View (Desig_Typ);
14512 end if;
14514 if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
14516 -- Ada 2005 (AI-251): Handle also derivations of abstract
14517 -- interface primitives.
14519 or else (Is_Interface (Desig_Typ)
14520 and then not Is_Class_Wide_Type (Desig_Typ))
14521 then
14522 Acc_Type := New_Copy (Id_Type);
14523 Set_Etype (Acc_Type, Acc_Type);
14524 Set_Scope (Acc_Type, New_Subp);
14526 -- Set size of anonymous access type. If we have an access
14527 -- to an unconstrained array, this is a fat pointer, so it
14528 -- is sizes at twice addtress size.
14530 if Is_Array_Type (Desig_Typ)
14531 and then not Is_Constrained (Desig_Typ)
14532 then
14533 Init_Size (Acc_Type, 2 * System_Address_Size);
14535 -- Other cases use a thin pointer
14537 else
14538 Init_Size (Acc_Type, System_Address_Size);
14539 end if;
14541 -- Set remaining characterstics of anonymous access type
14543 Init_Alignment (Acc_Type);
14544 Set_Directly_Designated_Type (Acc_Type, Derived_Type);
14546 Set_Etype (New_Id, Acc_Type);
14547 Set_Scope (New_Id, New_Subp);
14549 -- Create a reference to it
14551 Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
14553 else
14554 Set_Etype (New_Id, Id_Type);
14555 end if;
14556 end;
14558 -- In Ada2012, a formal may have an incomplete type but the type
14559 -- derivation that inherits the primitive follows the full view.
14561 elsif Base_Type (Id_Type) = Base_Type (Parent_Type)
14562 or else
14563 (Ekind (Id_Type) = E_Record_Type_With_Private
14564 and then Present (Full_View (Id_Type))
14565 and then
14566 Base_Type (Full_View (Id_Type)) = Base_Type (Parent_Type))
14567 or else
14568 (Ada_Version >= Ada_2012
14569 and then Ekind (Id_Type) = E_Incomplete_Type
14570 and then Full_View (Id_Type) = Parent_Type)
14571 then
14572 -- Constraint checks on formals are generated during expansion,
14573 -- based on the signature of the original subprogram. The bounds
14574 -- of the derived type are not relevant, and thus we can use
14575 -- the base type for the formals. However, the return type may be
14576 -- used in a context that requires that the proper static bounds
14577 -- be used (a case statement, for example) and for those cases
14578 -- we must use the derived type (first subtype), not its base.
14580 -- If the derived_type_definition has no constraints, we know that
14581 -- the derived type has the same constraints as the first subtype
14582 -- of the parent, and we can also use it rather than its base,
14583 -- which can lead to more efficient code.
14585 if Etype (Id) = Parent_Type then
14586 if Is_Scalar_Type (Parent_Type)
14587 and then
14588 Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
14589 then
14590 Set_Etype (New_Id, Derived_Type);
14592 elsif Nkind (Par) = N_Full_Type_Declaration
14593 and then
14594 Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
14595 and then
14596 Is_Entity_Name
14597 (Subtype_Indication (Type_Definition (Par)))
14598 then
14599 Set_Etype (New_Id, Derived_Type);
14601 else
14602 Set_Etype (New_Id, Base_Type (Derived_Type));
14603 end if;
14605 else
14606 Set_Etype (New_Id, Base_Type (Derived_Type));
14607 end if;
14609 else
14610 Set_Etype (New_Id, Etype (Id));
14611 end if;
14612 end Replace_Type;
14614 ----------------------
14615 -- Set_Derived_Name --
14616 ----------------------
14618 procedure Set_Derived_Name is
14619 Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
14620 begin
14621 if Nm = TSS_Null then
14622 Set_Chars (New_Subp, Chars (Parent_Subp));
14623 else
14624 Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
14625 end if;
14626 end Set_Derived_Name;
14628 -- Start of processing for Derive_Subprogram
14630 begin
14631 New_Subp := New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
14632 Set_Ekind (New_Subp, Ekind (Parent_Subp));
14634 -- Check whether the inherited subprogram is a private operation that
14635 -- should be inherited but not yet made visible. Such subprograms can
14636 -- become visible at a later point (e.g., the private part of a public
14637 -- child unit) via Declare_Inherited_Private_Subprograms. If the
14638 -- following predicate is true, then this is not such a private
14639 -- operation and the subprogram simply inherits the name of the parent
14640 -- subprogram. Note the special check for the names of controlled
14641 -- operations, which are currently exempted from being inherited with
14642 -- a hidden name because they must be findable for generation of
14643 -- implicit run-time calls.
14645 if not Is_Hidden (Parent_Subp)
14646 or else Is_Internal (Parent_Subp)
14647 or else Is_Private_Overriding
14648 or else Is_Internal_Name (Chars (Parent_Subp))
14649 or else Nam_In (Chars (Parent_Subp), Name_Initialize,
14650 Name_Adjust,
14651 Name_Finalize)
14652 then
14653 Set_Derived_Name;
14655 -- An inherited dispatching equality will be overridden by an internally
14656 -- generated one, or by an explicit one, so preserve its name and thus
14657 -- its entry in the dispatch table. Otherwise, if Parent_Subp is a
14658 -- private operation it may become invisible if the full view has
14659 -- progenitors, and the dispatch table will be malformed.
14660 -- We check that the type is limited to handle the anomalous declaration
14661 -- of Limited_Controlled, which is derived from a non-limited type, and
14662 -- which is handled specially elsewhere as well.
14664 elsif Chars (Parent_Subp) = Name_Op_Eq
14665 and then Is_Dispatching_Operation (Parent_Subp)
14666 and then Etype (Parent_Subp) = Standard_Boolean
14667 and then not Is_Limited_Type (Etype (First_Formal (Parent_Subp)))
14668 and then
14669 Etype (First_Formal (Parent_Subp)) =
14670 Etype (Next_Formal (First_Formal (Parent_Subp)))
14671 then
14672 Set_Derived_Name;
14674 -- If parent is hidden, this can be a regular derivation if the
14675 -- parent is immediately visible in a non-instantiating context,
14676 -- or if we are in the private part of an instance. This test
14677 -- should still be refined ???
14679 -- The test for In_Instance_Not_Visible avoids inheriting the derived
14680 -- operation as a non-visible operation in cases where the parent
14681 -- subprogram might not be visible now, but was visible within the
14682 -- original generic, so it would be wrong to make the inherited
14683 -- subprogram non-visible now. (Not clear if this test is fully
14684 -- correct; are there any cases where we should declare the inherited
14685 -- operation as not visible to avoid it being overridden, e.g., when
14686 -- the parent type is a generic actual with private primitives ???)
14688 -- (they should be treated the same as other private inherited
14689 -- subprograms, but it's not clear how to do this cleanly). ???
14691 elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
14692 and then Is_Immediately_Visible (Parent_Subp)
14693 and then not In_Instance)
14694 or else In_Instance_Not_Visible
14695 then
14696 Set_Derived_Name;
14698 -- Ada 2005 (AI-251): Regular derivation if the parent subprogram
14699 -- overrides an interface primitive because interface primitives
14700 -- must be visible in the partial view of the parent (RM 7.3 (7.3/2))
14702 elsif Ada_Version >= Ada_2005
14703 and then Is_Dispatching_Operation (Parent_Subp)
14704 and then Covers_Some_Interface (Parent_Subp)
14705 then
14706 Set_Derived_Name;
14708 -- Otherwise, the type is inheriting a private operation, so enter
14709 -- it with a special name so it can't be overridden.
14711 else
14712 Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
14713 end if;
14715 Set_Parent (New_Subp, Parent (Derived_Type));
14717 if Present (Actual_Subp) then
14718 Replace_Type (Actual_Subp, New_Subp);
14719 else
14720 Replace_Type (Parent_Subp, New_Subp);
14721 end if;
14723 Conditional_Delay (New_Subp, Parent_Subp);
14725 -- If we are creating a renaming for a primitive operation of an
14726 -- actual of a generic derived type, we must examine the signature
14727 -- of the actual primitive, not that of the generic formal, which for
14728 -- example may be an interface. However the name and initial value
14729 -- of the inherited operation are those of the formal primitive.
14731 Formal := First_Formal (Parent_Subp);
14733 if Present (Actual_Subp) then
14734 Formal_Of_Actual := First_Formal (Actual_Subp);
14735 else
14736 Formal_Of_Actual := Empty;
14737 end if;
14739 while Present (Formal) loop
14740 New_Formal := New_Copy (Formal);
14742 -- Normally we do not go copying parents, but in the case of
14743 -- formals, we need to link up to the declaration (which is the
14744 -- parameter specification), and it is fine to link up to the
14745 -- original formal's parameter specification in this case.
14747 Set_Parent (New_Formal, Parent (Formal));
14748 Append_Entity (New_Formal, New_Subp);
14750 if Present (Formal_Of_Actual) then
14751 Replace_Type (Formal_Of_Actual, New_Formal);
14752 Next_Formal (Formal_Of_Actual);
14753 else
14754 Replace_Type (Formal, New_Formal);
14755 end if;
14757 Next_Formal (Formal);
14758 end loop;
14760 -- If this derivation corresponds to a tagged generic actual, then
14761 -- primitive operations rename those of the actual. Otherwise the
14762 -- primitive operations rename those of the parent type, If the parent
14763 -- renames an intrinsic operator, so does the new subprogram. We except
14764 -- concatenation, which is always properly typed, and does not get
14765 -- expanded as other intrinsic operations.
14767 if No (Actual_Subp) then
14768 if Is_Intrinsic_Subprogram (Parent_Subp) then
14769 Set_Is_Intrinsic_Subprogram (New_Subp);
14771 if Present (Alias (Parent_Subp))
14772 and then Chars (Parent_Subp) /= Name_Op_Concat
14773 then
14774 Set_Alias (New_Subp, Alias (Parent_Subp));
14775 else
14776 Set_Alias (New_Subp, Parent_Subp);
14777 end if;
14779 else
14780 Set_Alias (New_Subp, Parent_Subp);
14781 end if;
14783 else
14784 Set_Alias (New_Subp, Actual_Subp);
14785 end if;
14787 -- Inherit the "ghostness" from the parent subprogram
14789 if Is_Ghost_Entity (Alias (New_Subp)) then
14790 Set_Is_Ghost_Entity (New_Subp);
14791 end if;
14793 -- Derived subprograms of a tagged type must inherit the convention
14794 -- of the parent subprogram (a requirement of AI-117). Derived
14795 -- subprograms of untagged types simply get convention Ada by default.
14797 -- If the derived type is a tagged generic formal type with unknown
14798 -- discriminants, its convention is intrinsic (RM 6.3.1 (8)).
14800 -- However, if the type is derived from a generic formal, the further
14801 -- inherited subprogram has the convention of the non-generic ancestor.
14802 -- Otherwise there would be no way to override the operation.
14803 -- (This is subject to forthcoming ARG discussions).
14805 if Is_Tagged_Type (Derived_Type) then
14806 if Is_Generic_Type (Derived_Type)
14807 and then Has_Unknown_Discriminants (Derived_Type)
14808 then
14809 Set_Convention (New_Subp, Convention_Intrinsic);
14811 else
14812 if Is_Generic_Type (Parent_Type)
14813 and then Has_Unknown_Discriminants (Parent_Type)
14814 then
14815 Set_Convention (New_Subp, Convention (Alias (Parent_Subp)));
14816 else
14817 Set_Convention (New_Subp, Convention (Parent_Subp));
14818 end if;
14819 end if;
14820 end if;
14822 -- Predefined controlled operations retain their name even if the parent
14823 -- is hidden (see above), but they are not primitive operations if the
14824 -- ancestor is not visible, for example if the parent is a private
14825 -- extension completed with a controlled extension. Note that a full
14826 -- type that is controlled can break privacy: the flag Is_Controlled is
14827 -- set on both views of the type.
14829 if Is_Controlled (Parent_Type)
14830 and then Nam_In (Chars (Parent_Subp), Name_Initialize,
14831 Name_Adjust,
14832 Name_Finalize)
14833 and then Is_Hidden (Parent_Subp)
14834 and then not Is_Visibly_Controlled (Parent_Type)
14835 then
14836 Set_Is_Hidden (New_Subp);
14837 end if;
14839 Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
14840 Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
14842 if Ekind (Parent_Subp) = E_Procedure then
14843 Set_Is_Valued_Procedure
14844 (New_Subp, Is_Valued_Procedure (Parent_Subp));
14845 else
14846 Set_Has_Controlling_Result
14847 (New_Subp, Has_Controlling_Result (Parent_Subp));
14848 end if;
14850 -- No_Return must be inherited properly. If this is overridden in the
14851 -- case of a dispatching operation, then a check is made in Sem_Disp
14852 -- that the overriding operation is also No_Return (no such check is
14853 -- required for the case of non-dispatching operation.
14855 Set_No_Return (New_Subp, No_Return (Parent_Subp));
14857 -- A derived function with a controlling result is abstract. If the
14858 -- Derived_Type is a nonabstract formal generic derived type, then
14859 -- inherited operations are not abstract: the required check is done at
14860 -- instantiation time. If the derivation is for a generic actual, the
14861 -- function is not abstract unless the actual is.
14863 if Is_Generic_Type (Derived_Type)
14864 and then not Is_Abstract_Type (Derived_Type)
14865 then
14866 null;
14868 -- Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
14869 -- properties of the subprogram, as defined in RM-3.9.3(4/2-6/2).
14871 -- A subprogram subject to pragma Extensions_Visible with value False
14872 -- requires overriding if the subprogram has at least one controlling
14873 -- OUT parameter (SPARK RM 6.1.7(6)).
14875 elsif Ada_Version >= Ada_2005
14876 and then (Is_Abstract_Subprogram (Alias (New_Subp))
14877 or else (Is_Tagged_Type (Derived_Type)
14878 and then Etype (New_Subp) = Derived_Type
14879 and then not Is_Null_Extension (Derived_Type))
14880 or else (Is_Tagged_Type (Derived_Type)
14881 and then Ekind (Etype (New_Subp)) =
14882 E_Anonymous_Access_Type
14883 and then Designated_Type (Etype (New_Subp)) =
14884 Derived_Type
14885 and then not Is_Null_Extension (Derived_Type))
14886 or else (Comes_From_Source (Alias (New_Subp))
14887 and then Is_EVF_Procedure (Alias (New_Subp))))
14888 and then No (Actual_Subp)
14889 then
14890 if not Is_Tagged_Type (Derived_Type)
14891 or else Is_Abstract_Type (Derived_Type)
14892 or else Is_Abstract_Subprogram (Alias (New_Subp))
14893 then
14894 Set_Is_Abstract_Subprogram (New_Subp);
14895 else
14896 Set_Requires_Overriding (New_Subp);
14897 end if;
14899 elsif Ada_Version < Ada_2005
14900 and then (Is_Abstract_Subprogram (Alias (New_Subp))
14901 or else (Is_Tagged_Type (Derived_Type)
14902 and then Etype (New_Subp) = Derived_Type
14903 and then No (Actual_Subp)))
14904 then
14905 Set_Is_Abstract_Subprogram (New_Subp);
14907 -- AI05-0097 : an inherited operation that dispatches on result is
14908 -- abstract if the derived type is abstract, even if the parent type
14909 -- is concrete and the derived type is a null extension.
14911 elsif Has_Controlling_Result (Alias (New_Subp))
14912 and then Is_Abstract_Type (Etype (New_Subp))
14913 then
14914 Set_Is_Abstract_Subprogram (New_Subp);
14916 -- Finally, if the parent type is abstract we must verify that all
14917 -- inherited operations are either non-abstract or overridden, or that
14918 -- the derived type itself is abstract (this check is performed at the
14919 -- end of a package declaration, in Check_Abstract_Overriding). A
14920 -- private overriding in the parent type will not be visible in the
14921 -- derivation if we are not in an inner package or in a child unit of
14922 -- the parent type, in which case the abstractness of the inherited
14923 -- operation is carried to the new subprogram.
14925 elsif Is_Abstract_Type (Parent_Type)
14926 and then not In_Open_Scopes (Scope (Parent_Type))
14927 and then Is_Private_Overriding
14928 and then Is_Abstract_Subprogram (Visible_Subp)
14929 then
14930 if No (Actual_Subp) then
14931 Set_Alias (New_Subp, Visible_Subp);
14932 Set_Is_Abstract_Subprogram (New_Subp, True);
14934 else
14935 -- If this is a derivation for an instance of a formal derived
14936 -- type, abstractness comes from the primitive operation of the
14937 -- actual, not from the operation inherited from the ancestor.
14939 Set_Is_Abstract_Subprogram
14940 (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
14941 end if;
14942 end if;
14944 New_Overloaded_Entity (New_Subp, Derived_Type);
14946 -- Check for case of a derived subprogram for the instantiation of a
14947 -- formal derived tagged type, if so mark the subprogram as dispatching
14948 -- and inherit the dispatching attributes of the actual subprogram. The
14949 -- derived subprogram is effectively renaming of the actual subprogram,
14950 -- so it needs to have the same attributes as the actual.
14952 if Present (Actual_Subp)
14953 and then Is_Dispatching_Operation (Actual_Subp)
14954 then
14955 Set_Is_Dispatching_Operation (New_Subp);
14957 if Present (DTC_Entity (Actual_Subp)) then
14958 Set_DTC_Entity (New_Subp, DTC_Entity (Actual_Subp));
14959 Set_DT_Position_Value (New_Subp, DT_Position (Actual_Subp));
14960 end if;
14961 end if;
14963 -- Indicate that a derived subprogram does not require a body and that
14964 -- it does not require processing of default expressions.
14966 Set_Has_Completion (New_Subp);
14967 Set_Default_Expressions_Processed (New_Subp);
14969 if Ekind (New_Subp) = E_Function then
14970 Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
14971 end if;
14972 end Derive_Subprogram;
14974 ------------------------
14975 -- Derive_Subprograms --
14976 ------------------------
14978 procedure Derive_Subprograms
14979 (Parent_Type : Entity_Id;
14980 Derived_Type : Entity_Id;
14981 Generic_Actual : Entity_Id := Empty)
14983 Op_List : constant Elist_Id :=
14984 Collect_Primitive_Operations (Parent_Type);
14986 function Check_Derived_Type return Boolean;
14987 -- Check that all the entities derived from Parent_Type are found in
14988 -- the list of primitives of Derived_Type exactly in the same order.
14990 procedure Derive_Interface_Subprogram
14991 (New_Subp : in out Entity_Id;
14992 Subp : Entity_Id;
14993 Actual_Subp : Entity_Id);
14994 -- Derive New_Subp from the ultimate alias of the parent subprogram Subp
14995 -- (which is an interface primitive). If Generic_Actual is present then
14996 -- Actual_Subp is the actual subprogram corresponding with the generic
14997 -- subprogram Subp.
14999 function Check_Derived_Type return Boolean is
15000 E : Entity_Id;
15001 Elmt : Elmt_Id;
15002 List : Elist_Id;
15003 New_Subp : Entity_Id;
15004 Op_Elmt : Elmt_Id;
15005 Subp : Entity_Id;
15007 begin
15008 -- Traverse list of entities in the current scope searching for
15009 -- an incomplete type whose full-view is derived type
15011 E := First_Entity (Scope (Derived_Type));
15012 while Present (E) and then E /= Derived_Type loop
15013 if Ekind (E) = E_Incomplete_Type
15014 and then Present (Full_View (E))
15015 and then Full_View (E) = Derived_Type
15016 then
15017 -- Disable this test if Derived_Type completes an incomplete
15018 -- type because in such case more primitives can be added
15019 -- later to the list of primitives of Derived_Type by routine
15020 -- Process_Incomplete_Dependents
15022 return True;
15023 end if;
15025 E := Next_Entity (E);
15026 end loop;
15028 List := Collect_Primitive_Operations (Derived_Type);
15029 Elmt := First_Elmt (List);
15031 Op_Elmt := First_Elmt (Op_List);
15032 while Present (Op_Elmt) loop
15033 Subp := Node (Op_Elmt);
15034 New_Subp := Node (Elmt);
15036 -- At this early stage Derived_Type has no entities with attribute
15037 -- Interface_Alias. In addition, such primitives are always
15038 -- located at the end of the list of primitives of Parent_Type.
15039 -- Therefore, if found we can safely stop processing pending
15040 -- entities.
15042 exit when Present (Interface_Alias (Subp));
15044 -- Handle hidden entities
15046 if not Is_Predefined_Dispatching_Operation (Subp)
15047 and then Is_Hidden (Subp)
15048 then
15049 if Present (New_Subp)
15050 and then Primitive_Names_Match (Subp, New_Subp)
15051 then
15052 Next_Elmt (Elmt);
15053 end if;
15055 else
15056 if not Present (New_Subp)
15057 or else Ekind (Subp) /= Ekind (New_Subp)
15058 or else not Primitive_Names_Match (Subp, New_Subp)
15059 then
15060 return False;
15061 end if;
15063 Next_Elmt (Elmt);
15064 end if;
15066 Next_Elmt (Op_Elmt);
15067 end loop;
15069 return True;
15070 end Check_Derived_Type;
15072 ---------------------------------
15073 -- Derive_Interface_Subprogram --
15074 ---------------------------------
15076 procedure Derive_Interface_Subprogram
15077 (New_Subp : in out Entity_Id;
15078 Subp : Entity_Id;
15079 Actual_Subp : Entity_Id)
15081 Iface_Subp : constant Entity_Id := Ultimate_Alias (Subp);
15082 Iface_Type : constant Entity_Id := Find_Dispatching_Type (Iface_Subp);
15084 begin
15085 pragma Assert (Is_Interface (Iface_Type));
15087 Derive_Subprogram
15088 (New_Subp => New_Subp,
15089 Parent_Subp => Iface_Subp,
15090 Derived_Type => Derived_Type,
15091 Parent_Type => Iface_Type,
15092 Actual_Subp => Actual_Subp);
15094 -- Given that this new interface entity corresponds with a primitive
15095 -- of the parent that was not overridden we must leave it associated
15096 -- with its parent primitive to ensure that it will share the same
15097 -- dispatch table slot when overridden.
15099 if No (Actual_Subp) then
15100 Set_Alias (New_Subp, Subp);
15102 -- For instantiations this is not needed since the previous call to
15103 -- Derive_Subprogram leaves the entity well decorated.
15105 else
15106 pragma Assert (Alias (New_Subp) = Actual_Subp);
15107 null;
15108 end if;
15109 end Derive_Interface_Subprogram;
15111 -- Local variables
15113 Alias_Subp : Entity_Id;
15114 Act_List : Elist_Id;
15115 Act_Elmt : Elmt_Id;
15116 Act_Subp : Entity_Id := Empty;
15117 Elmt : Elmt_Id;
15118 Need_Search : Boolean := False;
15119 New_Subp : Entity_Id := Empty;
15120 Parent_Base : Entity_Id;
15121 Subp : Entity_Id;
15123 -- Start of processing for Derive_Subprograms
15125 begin
15126 if Ekind (Parent_Type) = E_Record_Type_With_Private
15127 and then Has_Discriminants (Parent_Type)
15128 and then Present (Full_View (Parent_Type))
15129 then
15130 Parent_Base := Full_View (Parent_Type);
15131 else
15132 Parent_Base := Parent_Type;
15133 end if;
15135 if Present (Generic_Actual) then
15136 Act_List := Collect_Primitive_Operations (Generic_Actual);
15137 Act_Elmt := First_Elmt (Act_List);
15138 else
15139 Act_List := No_Elist;
15140 Act_Elmt := No_Elmt;
15141 end if;
15143 -- Derive primitives inherited from the parent. Note that if the generic
15144 -- actual is present, this is not really a type derivation, it is a
15145 -- completion within an instance.
15147 -- Case 1: Derived_Type does not implement interfaces
15149 if not Is_Tagged_Type (Derived_Type)
15150 or else (not Has_Interfaces (Derived_Type)
15151 and then not (Present (Generic_Actual)
15152 and then Has_Interfaces (Generic_Actual)))
15153 then
15154 Elmt := First_Elmt (Op_List);
15155 while Present (Elmt) loop
15156 Subp := Node (Elmt);
15158 -- Literals are derived earlier in the process of building the
15159 -- derived type, and are skipped here.
15161 if Ekind (Subp) = E_Enumeration_Literal then
15162 null;
15164 -- The actual is a direct descendant and the common primitive
15165 -- operations appear in the same order.
15167 -- If the generic parent type is present, the derived type is an
15168 -- instance of a formal derived type, and within the instance its
15169 -- operations are those of the actual. We derive from the formal
15170 -- type but make the inherited operations aliases of the
15171 -- corresponding operations of the actual.
15173 else
15174 pragma Assert (No (Node (Act_Elmt))
15175 or else (Primitive_Names_Match (Subp, Node (Act_Elmt))
15176 and then
15177 Type_Conformant
15178 (Subp, Node (Act_Elmt),
15179 Skip_Controlling_Formals => True)));
15181 Derive_Subprogram
15182 (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
15184 if Present (Act_Elmt) then
15185 Next_Elmt (Act_Elmt);
15186 end if;
15187 end if;
15189 Next_Elmt (Elmt);
15190 end loop;
15192 -- Case 2: Derived_Type implements interfaces
15194 else
15195 -- If the parent type has no predefined primitives we remove
15196 -- predefined primitives from the list of primitives of generic
15197 -- actual to simplify the complexity of this algorithm.
15199 if Present (Generic_Actual) then
15200 declare
15201 Has_Predefined_Primitives : Boolean := False;
15203 begin
15204 -- Check if the parent type has predefined primitives
15206 Elmt := First_Elmt (Op_List);
15207 while Present (Elmt) loop
15208 Subp := Node (Elmt);
15210 if Is_Predefined_Dispatching_Operation (Subp)
15211 and then not Comes_From_Source (Ultimate_Alias (Subp))
15212 then
15213 Has_Predefined_Primitives := True;
15214 exit;
15215 end if;
15217 Next_Elmt (Elmt);
15218 end loop;
15220 -- Remove predefined primitives of Generic_Actual. We must use
15221 -- an auxiliary list because in case of tagged types the value
15222 -- returned by Collect_Primitive_Operations is the value stored
15223 -- in its Primitive_Operations attribute (and we don't want to
15224 -- modify its current contents).
15226 if not Has_Predefined_Primitives then
15227 declare
15228 Aux_List : constant Elist_Id := New_Elmt_List;
15230 begin
15231 Elmt := First_Elmt (Act_List);
15232 while Present (Elmt) loop
15233 Subp := Node (Elmt);
15235 if not Is_Predefined_Dispatching_Operation (Subp)
15236 or else Comes_From_Source (Subp)
15237 then
15238 Append_Elmt (Subp, Aux_List);
15239 end if;
15241 Next_Elmt (Elmt);
15242 end loop;
15244 Act_List := Aux_List;
15245 end;
15246 end if;
15248 Act_Elmt := First_Elmt (Act_List);
15249 Act_Subp := Node (Act_Elmt);
15250 end;
15251 end if;
15253 -- Stage 1: If the generic actual is not present we derive the
15254 -- primitives inherited from the parent type. If the generic parent
15255 -- type is present, the derived type is an instance of a formal
15256 -- derived type, and within the instance its operations are those of
15257 -- the actual. We derive from the formal type but make the inherited
15258 -- operations aliases of the corresponding operations of the actual.
15260 Elmt := First_Elmt (Op_List);
15261 while Present (Elmt) loop
15262 Subp := Node (Elmt);
15263 Alias_Subp := Ultimate_Alias (Subp);
15265 -- Do not derive internal entities of the parent that link
15266 -- interface primitives with their covering primitive. These
15267 -- entities will be added to this type when frozen.
15269 if Present (Interface_Alias (Subp)) then
15270 goto Continue;
15271 end if;
15273 -- If the generic actual is present find the corresponding
15274 -- operation in the generic actual. If the parent type is a
15275 -- direct ancestor of the derived type then, even if it is an
15276 -- interface, the operations are inherited from the primary
15277 -- dispatch table and are in the proper order. If we detect here
15278 -- that primitives are not in the same order we traverse the list
15279 -- of primitive operations of the actual to find the one that
15280 -- implements the interface primitive.
15282 if Need_Search
15283 or else
15284 (Present (Generic_Actual)
15285 and then Present (Act_Subp)
15286 and then not
15287 (Primitive_Names_Match (Subp, Act_Subp)
15288 and then
15289 Type_Conformant (Subp, Act_Subp,
15290 Skip_Controlling_Formals => True)))
15291 then
15292 pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual,
15293 Use_Full_View => True));
15295 -- Remember that we need searching for all pending primitives
15297 Need_Search := True;
15299 -- Handle entities associated with interface primitives
15301 if Present (Alias_Subp)
15302 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
15303 and then not Is_Predefined_Dispatching_Operation (Subp)
15304 then
15305 -- Search for the primitive in the homonym chain
15307 Act_Subp :=
15308 Find_Primitive_Covering_Interface
15309 (Tagged_Type => Generic_Actual,
15310 Iface_Prim => Alias_Subp);
15312 -- Previous search may not locate primitives covering
15313 -- interfaces defined in generics units or instantiations.
15314 -- (it fails if the covering primitive has formals whose
15315 -- type is also defined in generics or instantiations).
15316 -- In such case we search in the list of primitives of the
15317 -- generic actual for the internal entity that links the
15318 -- interface primitive and the covering primitive.
15320 if No (Act_Subp)
15321 and then Is_Generic_Type (Parent_Type)
15322 then
15323 -- This code has been designed to handle only generic
15324 -- formals that implement interfaces that are defined
15325 -- in a generic unit or instantiation. If this code is
15326 -- needed for other cases we must review it because
15327 -- (given that it relies on Original_Location to locate
15328 -- the primitive of Generic_Actual that covers the
15329 -- interface) it could leave linked through attribute
15330 -- Alias entities of unrelated instantiations).
15332 pragma Assert
15333 (Is_Generic_Unit
15334 (Scope (Find_Dispatching_Type (Alias_Subp)))
15335 or else
15336 Instantiation_Depth
15337 (Sloc (Find_Dispatching_Type (Alias_Subp))) > 0);
15339 declare
15340 Iface_Prim_Loc : constant Source_Ptr :=
15341 Original_Location (Sloc (Alias_Subp));
15343 Elmt : Elmt_Id;
15344 Prim : Entity_Id;
15346 begin
15347 Elmt :=
15348 First_Elmt (Primitive_Operations (Generic_Actual));
15350 Search : while Present (Elmt) loop
15351 Prim := Node (Elmt);
15353 if Present (Interface_Alias (Prim))
15354 and then Original_Location
15355 (Sloc (Interface_Alias (Prim))) =
15356 Iface_Prim_Loc
15357 then
15358 Act_Subp := Alias (Prim);
15359 exit Search;
15360 end if;
15362 Next_Elmt (Elmt);
15363 end loop Search;
15364 end;
15365 end if;
15367 pragma Assert (Present (Act_Subp)
15368 or else Is_Abstract_Type (Generic_Actual)
15369 or else Serious_Errors_Detected > 0);
15371 -- Handle predefined primitives plus the rest of user-defined
15372 -- primitives
15374 else
15375 Act_Elmt := First_Elmt (Act_List);
15376 while Present (Act_Elmt) loop
15377 Act_Subp := Node (Act_Elmt);
15379 exit when Primitive_Names_Match (Subp, Act_Subp)
15380 and then Type_Conformant
15381 (Subp, Act_Subp,
15382 Skip_Controlling_Formals => True)
15383 and then No (Interface_Alias (Act_Subp));
15385 Next_Elmt (Act_Elmt);
15386 end loop;
15388 if No (Act_Elmt) then
15389 Act_Subp := Empty;
15390 end if;
15391 end if;
15392 end if;
15394 -- Case 1: If the parent is a limited interface then it has the
15395 -- predefined primitives of synchronized interfaces. However, the
15396 -- actual type may be a non-limited type and hence it does not
15397 -- have such primitives.
15399 if Present (Generic_Actual)
15400 and then not Present (Act_Subp)
15401 and then Is_Limited_Interface (Parent_Base)
15402 and then Is_Predefined_Interface_Primitive (Subp)
15403 then
15404 null;
15406 -- Case 2: Inherit entities associated with interfaces that were
15407 -- not covered by the parent type. We exclude here null interface
15408 -- primitives because they do not need special management.
15410 -- We also exclude interface operations that are renamings. If the
15411 -- subprogram is an explicit renaming of an interface primitive,
15412 -- it is a regular primitive operation, and the presence of its
15413 -- alias is not relevant: it has to be derived like any other
15414 -- primitive.
15416 elsif Present (Alias (Subp))
15417 and then Nkind (Unit_Declaration_Node (Subp)) /=
15418 N_Subprogram_Renaming_Declaration
15419 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
15420 and then not
15421 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
15422 and then Null_Present (Parent (Alias_Subp)))
15423 then
15424 -- If this is an abstract private type then we transfer the
15425 -- derivation of the interface primitive from the partial view
15426 -- to the full view. This is safe because all the interfaces
15427 -- must be visible in the partial view. Done to avoid adding
15428 -- a new interface derivation to the private part of the
15429 -- enclosing package; otherwise this new derivation would be
15430 -- decorated as hidden when the analysis of the enclosing
15431 -- package completes.
15433 if Is_Abstract_Type (Derived_Type)
15434 and then In_Private_Part (Current_Scope)
15435 and then Has_Private_Declaration (Derived_Type)
15436 then
15437 declare
15438 Partial_View : Entity_Id;
15439 Elmt : Elmt_Id;
15440 Ent : Entity_Id;
15442 begin
15443 Partial_View := First_Entity (Current_Scope);
15444 loop
15445 exit when No (Partial_View)
15446 or else (Has_Private_Declaration (Partial_View)
15447 and then
15448 Full_View (Partial_View) = Derived_Type);
15450 Next_Entity (Partial_View);
15451 end loop;
15453 -- If the partial view was not found then the source code
15454 -- has errors and the derivation is not needed.
15456 if Present (Partial_View) then
15457 Elmt :=
15458 First_Elmt (Primitive_Operations (Partial_View));
15459 while Present (Elmt) loop
15460 Ent := Node (Elmt);
15462 if Present (Alias (Ent))
15463 and then Ultimate_Alias (Ent) = Alias (Subp)
15464 then
15465 Append_Elmt
15466 (Ent, Primitive_Operations (Derived_Type));
15467 exit;
15468 end if;
15470 Next_Elmt (Elmt);
15471 end loop;
15473 -- If the interface primitive was not found in the
15474 -- partial view then this interface primitive was
15475 -- overridden. We add a derivation to activate in
15476 -- Derive_Progenitor_Subprograms the machinery to
15477 -- search for it.
15479 if No (Elmt) then
15480 Derive_Interface_Subprogram
15481 (New_Subp => New_Subp,
15482 Subp => Subp,
15483 Actual_Subp => Act_Subp);
15484 end if;
15485 end if;
15486 end;
15487 else
15488 Derive_Interface_Subprogram
15489 (New_Subp => New_Subp,
15490 Subp => Subp,
15491 Actual_Subp => Act_Subp);
15492 end if;
15494 -- Case 3: Common derivation
15496 else
15497 Derive_Subprogram
15498 (New_Subp => New_Subp,
15499 Parent_Subp => Subp,
15500 Derived_Type => Derived_Type,
15501 Parent_Type => Parent_Base,
15502 Actual_Subp => Act_Subp);
15503 end if;
15505 -- No need to update Act_Elm if we must search for the
15506 -- corresponding operation in the generic actual
15508 if not Need_Search
15509 and then Present (Act_Elmt)
15510 then
15511 Next_Elmt (Act_Elmt);
15512 Act_Subp := Node (Act_Elmt);
15513 end if;
15515 <<Continue>>
15516 Next_Elmt (Elmt);
15517 end loop;
15519 -- Inherit additional operations from progenitors. If the derived
15520 -- type is a generic actual, there are not new primitive operations
15521 -- for the type because it has those of the actual, and therefore
15522 -- nothing needs to be done. The renamings generated above are not
15523 -- primitive operations, and their purpose is simply to make the
15524 -- proper operations visible within an instantiation.
15526 if No (Generic_Actual) then
15527 Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
15528 end if;
15529 end if;
15531 -- Final check: Direct descendants must have their primitives in the
15532 -- same order. We exclude from this test untagged types and instances
15533 -- of formal derived types. We skip this test if we have already
15534 -- reported serious errors in the sources.
15536 pragma Assert (not Is_Tagged_Type (Derived_Type)
15537 or else Present (Generic_Actual)
15538 or else Serious_Errors_Detected > 0
15539 or else Check_Derived_Type);
15540 end Derive_Subprograms;
15542 --------------------------------
15543 -- Derived_Standard_Character --
15544 --------------------------------
15546 procedure Derived_Standard_Character
15547 (N : Node_Id;
15548 Parent_Type : Entity_Id;
15549 Derived_Type : Entity_Id)
15551 Loc : constant Source_Ptr := Sloc (N);
15552 Def : constant Node_Id := Type_Definition (N);
15553 Indic : constant Node_Id := Subtype_Indication (Def);
15554 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
15555 Implicit_Base : constant Entity_Id :=
15556 Create_Itype
15557 (E_Enumeration_Type, N, Derived_Type, 'B');
15559 Lo : Node_Id;
15560 Hi : Node_Id;
15562 begin
15563 Discard_Node (Process_Subtype (Indic, N));
15565 Set_Etype (Implicit_Base, Parent_Base);
15566 Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
15567 Set_RM_Size (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
15569 Set_Is_Character_Type (Implicit_Base, True);
15570 Set_Has_Delayed_Freeze (Implicit_Base);
15572 -- The bounds of the implicit base are the bounds of the parent base.
15573 -- Note that their type is the parent base.
15575 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
15576 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
15578 Set_Scalar_Range (Implicit_Base,
15579 Make_Range (Loc,
15580 Low_Bound => Lo,
15581 High_Bound => Hi));
15583 Conditional_Delay (Derived_Type, Parent_Type);
15585 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
15586 Set_Etype (Derived_Type, Implicit_Base);
15587 Set_Size_Info (Derived_Type, Parent_Type);
15589 if Unknown_RM_Size (Derived_Type) then
15590 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
15591 end if;
15593 Set_Is_Character_Type (Derived_Type, True);
15595 if Nkind (Indic) /= N_Subtype_Indication then
15597 -- If no explicit constraint, the bounds are those
15598 -- of the parent type.
15600 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Type));
15601 Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
15602 Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
15603 end if;
15605 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
15607 -- Because the implicit base is used in the conversion of the bounds, we
15608 -- have to freeze it now. This is similar to what is done for numeric
15609 -- types, and it equally suspicious, but otherwise a non-static bound
15610 -- will have a reference to an unfrozen type, which is rejected by Gigi
15611 -- (???). This requires specific care for definition of stream
15612 -- attributes. For details, see comments at the end of
15613 -- Build_Derived_Numeric_Type.
15615 Freeze_Before (N, Implicit_Base);
15616 end Derived_Standard_Character;
15618 ------------------------------
15619 -- Derived_Type_Declaration --
15620 ------------------------------
15622 procedure Derived_Type_Declaration
15623 (T : Entity_Id;
15624 N : Node_Id;
15625 Is_Completion : Boolean)
15627 Parent_Type : Entity_Id;
15629 function Comes_From_Generic (Typ : Entity_Id) return Boolean;
15630 -- Check whether the parent type is a generic formal, or derives
15631 -- directly or indirectly from one.
15633 ------------------------
15634 -- Comes_From_Generic --
15635 ------------------------
15637 function Comes_From_Generic (Typ : Entity_Id) return Boolean is
15638 begin
15639 if Is_Generic_Type (Typ) then
15640 return True;
15642 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
15643 return True;
15645 elsif Is_Private_Type (Typ)
15646 and then Present (Full_View (Typ))
15647 and then Is_Generic_Type (Root_Type (Full_View (Typ)))
15648 then
15649 return True;
15651 elsif Is_Generic_Actual_Type (Typ) then
15652 return True;
15654 else
15655 return False;
15656 end if;
15657 end Comes_From_Generic;
15659 -- Local variables
15661 Def : constant Node_Id := Type_Definition (N);
15662 Iface_Def : Node_Id;
15663 Indic : constant Node_Id := Subtype_Indication (Def);
15664 Extension : constant Node_Id := Record_Extension_Part (Def);
15665 Parent_Node : Node_Id;
15666 Taggd : Boolean;
15668 -- Start of processing for Derived_Type_Declaration
15670 begin
15671 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
15673 -- Ada 2005 (AI-251): In case of interface derivation check that the
15674 -- parent is also an interface.
15676 if Interface_Present (Def) then
15677 Check_SPARK_05_Restriction ("interface is not allowed", Def);
15679 if not Is_Interface (Parent_Type) then
15680 Diagnose_Interface (Indic, Parent_Type);
15682 else
15683 Parent_Node := Parent (Base_Type (Parent_Type));
15684 Iface_Def := Type_Definition (Parent_Node);
15686 -- Ada 2005 (AI-251): Limited interfaces can only inherit from
15687 -- other limited interfaces.
15689 if Limited_Present (Def) then
15690 if Limited_Present (Iface_Def) then
15691 null;
15693 elsif Protected_Present (Iface_Def) then
15694 Error_Msg_NE
15695 ("descendant of& must be declared"
15696 & " as a protected interface",
15697 N, Parent_Type);
15699 elsif Synchronized_Present (Iface_Def) then
15700 Error_Msg_NE
15701 ("descendant of& must be declared"
15702 & " as a synchronized interface",
15703 N, Parent_Type);
15705 elsif Task_Present (Iface_Def) then
15706 Error_Msg_NE
15707 ("descendant of& must be declared as a task interface",
15708 N, Parent_Type);
15710 else
15711 Error_Msg_N
15712 ("(Ada 2005) limited interface cannot "
15713 & "inherit from non-limited interface", Indic);
15714 end if;
15716 -- Ada 2005 (AI-345): Non-limited interfaces can only inherit
15717 -- from non-limited or limited interfaces.
15719 elsif not Protected_Present (Def)
15720 and then not Synchronized_Present (Def)
15721 and then not Task_Present (Def)
15722 then
15723 if Limited_Present (Iface_Def) then
15724 null;
15726 elsif Protected_Present (Iface_Def) then
15727 Error_Msg_NE
15728 ("descendant of& must be declared"
15729 & " as a protected interface",
15730 N, Parent_Type);
15732 elsif Synchronized_Present (Iface_Def) then
15733 Error_Msg_NE
15734 ("descendant of& must be declared"
15735 & " as a synchronized interface",
15736 N, Parent_Type);
15738 elsif Task_Present (Iface_Def) then
15739 Error_Msg_NE
15740 ("descendant of& must be declared as a task interface",
15741 N, Parent_Type);
15742 else
15743 null;
15744 end if;
15745 end if;
15746 end if;
15747 end if;
15749 if Is_Tagged_Type (Parent_Type)
15750 and then Is_Concurrent_Type (Parent_Type)
15751 and then not Is_Interface (Parent_Type)
15752 then
15753 Error_Msg_N
15754 ("parent type of a record extension cannot be "
15755 & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
15756 Set_Etype (T, Any_Type);
15757 return;
15758 end if;
15760 -- Ada 2005 (AI-251): Decorate all the names in the list of ancestor
15761 -- interfaces
15763 if Is_Tagged_Type (Parent_Type)
15764 and then Is_Non_Empty_List (Interface_List (Def))
15765 then
15766 declare
15767 Intf : Node_Id;
15768 T : Entity_Id;
15770 begin
15771 Intf := First (Interface_List (Def));
15772 while Present (Intf) loop
15773 T := Find_Type_Of_Subtype_Indic (Intf);
15775 if not Is_Interface (T) then
15776 Diagnose_Interface (Intf, T);
15778 -- Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
15779 -- a limited type from having a nonlimited progenitor.
15781 elsif (Limited_Present (Def)
15782 or else (not Is_Interface (Parent_Type)
15783 and then Is_Limited_Type (Parent_Type)))
15784 and then not Is_Limited_Interface (T)
15785 then
15786 Error_Msg_NE
15787 ("progenitor interface& of limited type must be limited",
15788 N, T);
15789 end if;
15791 Next (Intf);
15792 end loop;
15793 end;
15794 end if;
15796 if Parent_Type = Any_Type
15797 or else Etype (Parent_Type) = Any_Type
15798 or else (Is_Class_Wide_Type (Parent_Type)
15799 and then Etype (Parent_Type) = T)
15800 then
15801 -- If Parent_Type is undefined or illegal, make new type into a
15802 -- subtype of Any_Type, and set a few attributes to prevent cascaded
15803 -- errors. If this is a self-definition, emit error now.
15805 if T = Parent_Type or else T = Etype (Parent_Type) then
15806 Error_Msg_N ("type cannot be used in its own definition", Indic);
15807 end if;
15809 Set_Ekind (T, Ekind (Parent_Type));
15810 Set_Etype (T, Any_Type);
15811 Set_Scalar_Range (T, Scalar_Range (Any_Type));
15813 if Is_Tagged_Type (T)
15814 and then Is_Record_Type (T)
15815 then
15816 Set_Direct_Primitive_Operations (T, New_Elmt_List);
15817 end if;
15819 return;
15820 end if;
15822 -- Ada 2005 (AI-251): The case in which the parent of the full-view is
15823 -- an interface is special because the list of interfaces in the full
15824 -- view can be given in any order. For example:
15826 -- type A is interface;
15827 -- type B is interface and A;
15828 -- type D is new B with private;
15829 -- private
15830 -- type D is new A and B with null record; -- 1 --
15832 -- In this case we perform the following transformation of -1-:
15834 -- type D is new B and A with null record;
15836 -- If the parent of the full-view covers the parent of the partial-view
15837 -- we have two possible cases:
15839 -- 1) They have the same parent
15840 -- 2) The parent of the full-view implements some further interfaces
15842 -- In both cases we do not need to perform the transformation. In the
15843 -- first case the source program is correct and the transformation is
15844 -- not needed; in the second case the source program does not fulfill
15845 -- the no-hidden interfaces rule (AI-396) and the error will be reported
15846 -- later.
15848 -- This transformation not only simplifies the rest of the analysis of
15849 -- this type declaration but also simplifies the correct generation of
15850 -- the object layout to the expander.
15852 if In_Private_Part (Current_Scope)
15853 and then Is_Interface (Parent_Type)
15854 then
15855 declare
15856 Iface : Node_Id;
15857 Partial_View : Entity_Id;
15858 Partial_View_Parent : Entity_Id;
15859 New_Iface : Node_Id;
15861 begin
15862 -- Look for the associated private type declaration
15864 Partial_View := First_Entity (Current_Scope);
15865 loop
15866 exit when No (Partial_View)
15867 or else (Has_Private_Declaration (Partial_View)
15868 and then Full_View (Partial_View) = T);
15870 Next_Entity (Partial_View);
15871 end loop;
15873 -- If the partial view was not found then the source code has
15874 -- errors and the transformation is not needed.
15876 if Present (Partial_View) then
15877 Partial_View_Parent := Etype (Partial_View);
15879 -- If the parent of the full-view covers the parent of the
15880 -- partial-view we have nothing else to do.
15882 if Interface_Present_In_Ancestor
15883 (Parent_Type, Partial_View_Parent)
15884 then
15885 null;
15887 -- Traverse the list of interfaces of the full-view to look
15888 -- for the parent of the partial-view and perform the tree
15889 -- transformation.
15891 else
15892 Iface := First (Interface_List (Def));
15893 while Present (Iface) loop
15894 if Etype (Iface) = Etype (Partial_View) then
15895 Rewrite (Subtype_Indication (Def),
15896 New_Copy (Subtype_Indication
15897 (Parent (Partial_View))));
15899 New_Iface :=
15900 Make_Identifier (Sloc (N), Chars (Parent_Type));
15901 Append (New_Iface, Interface_List (Def));
15903 -- Analyze the transformed code
15905 Derived_Type_Declaration (T, N, Is_Completion);
15906 return;
15907 end if;
15909 Next (Iface);
15910 end loop;
15911 end if;
15912 end if;
15913 end;
15914 end if;
15916 -- Only composite types other than array types are allowed to have
15917 -- discriminants.
15919 if Present (Discriminant_Specifications (N)) then
15920 if (Is_Elementary_Type (Parent_Type)
15921 or else
15922 Is_Array_Type (Parent_Type))
15923 and then not Error_Posted (N)
15924 then
15925 Error_Msg_N
15926 ("elementary or array type cannot have discriminants",
15927 Defining_Identifier (First (Discriminant_Specifications (N))));
15928 Set_Has_Discriminants (T, False);
15930 -- The type is allowed to have discriminants
15932 else
15933 Check_SPARK_05_Restriction ("discriminant type is not allowed", N);
15934 end if;
15935 end if;
15937 -- In Ada 83, a derived type defined in a package specification cannot
15938 -- be used for further derivation until the end of its visible part.
15939 -- Note that derivation in the private part of the package is allowed.
15941 if Ada_Version = Ada_83
15942 and then Is_Derived_Type (Parent_Type)
15943 and then In_Visible_Part (Scope (Parent_Type))
15944 then
15945 if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
15946 Error_Msg_N
15947 ("(Ada 83): premature use of type for derivation", Indic);
15948 end if;
15949 end if;
15951 -- Check for early use of incomplete or private type
15953 if Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
15954 Error_Msg_N ("premature derivation of incomplete type", Indic);
15955 return;
15957 elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
15958 and then not Comes_From_Generic (Parent_Type))
15959 or else Has_Private_Component (Parent_Type)
15960 then
15961 -- The ancestor type of a formal type can be incomplete, in which
15962 -- case only the operations of the partial view are available in the
15963 -- generic. Subsequent checks may be required when the full view is
15964 -- analyzed to verify that a derivation from a tagged type has an
15965 -- extension.
15967 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
15968 null;
15970 elsif No (Underlying_Type (Parent_Type))
15971 or else Has_Private_Component (Parent_Type)
15972 then
15973 Error_Msg_N
15974 ("premature derivation of derived or private type", Indic);
15976 -- Flag the type itself as being in error, this prevents some
15977 -- nasty problems with subsequent uses of the malformed type.
15979 Set_Error_Posted (T);
15981 -- Check that within the immediate scope of an untagged partial
15982 -- view it's illegal to derive from the partial view if the
15983 -- full view is tagged. (7.3(7))
15985 -- We verify that the Parent_Type is a partial view by checking
15986 -- that it is not a Full_Type_Declaration (i.e. a private type or
15987 -- private extension declaration), to distinguish a partial view
15988 -- from a derivation from a private type which also appears as
15989 -- E_Private_Type. If the parent base type is not declared in an
15990 -- enclosing scope there is no need to check.
15992 elsif Present (Full_View (Parent_Type))
15993 and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
15994 and then not Is_Tagged_Type (Parent_Type)
15995 and then Is_Tagged_Type (Full_View (Parent_Type))
15996 and then In_Open_Scopes (Scope (Base_Type (Parent_Type)))
15997 then
15998 Error_Msg_N
15999 ("premature derivation from type with tagged full view",
16000 Indic);
16001 end if;
16002 end if;
16004 -- Check that form of derivation is appropriate
16006 Taggd := Is_Tagged_Type (Parent_Type);
16008 -- Set the parent type to the class-wide type's specific type in this
16009 -- case to prevent cascading errors
16011 if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
16012 Error_Msg_N ("parent type must not be a class-wide type", Indic);
16013 Set_Etype (T, Etype (Parent_Type));
16014 return;
16015 end if;
16017 if Present (Extension) and then not Taggd then
16018 Error_Msg_N
16019 ("type derived from untagged type cannot have extension", Indic);
16021 elsif No (Extension) and then Taggd then
16023 -- If this declaration is within a private part (or body) of a
16024 -- generic instantiation then the derivation is allowed (the parent
16025 -- type can only appear tagged in this case if it's a generic actual
16026 -- type, since it would otherwise have been rejected in the analysis
16027 -- of the generic template).
16029 if not Is_Generic_Actual_Type (Parent_Type)
16030 or else In_Visible_Part (Scope (Parent_Type))
16031 then
16032 if Is_Class_Wide_Type (Parent_Type) then
16033 Error_Msg_N
16034 ("parent type must not be a class-wide type", Indic);
16036 -- Use specific type to prevent cascaded errors.
16038 Parent_Type := Etype (Parent_Type);
16040 else
16041 Error_Msg_N
16042 ("type derived from tagged type must have extension", Indic);
16043 end if;
16044 end if;
16045 end if;
16047 -- AI-443: Synchronized formal derived types require a private
16048 -- extension. There is no point in checking the ancestor type or
16049 -- the progenitors since the construct is wrong to begin with.
16051 if Ada_Version >= Ada_2005
16052 and then Is_Generic_Type (T)
16053 and then Present (Original_Node (N))
16054 then
16055 declare
16056 Decl : constant Node_Id := Original_Node (N);
16058 begin
16059 if Nkind (Decl) = N_Formal_Type_Declaration
16060 and then Nkind (Formal_Type_Definition (Decl)) =
16061 N_Formal_Derived_Type_Definition
16062 and then Synchronized_Present (Formal_Type_Definition (Decl))
16063 and then No (Extension)
16065 -- Avoid emitting a duplicate error message
16067 and then not Error_Posted (Indic)
16068 then
16069 Error_Msg_N
16070 ("synchronized derived type must have extension", N);
16071 end if;
16072 end;
16073 end if;
16075 if Null_Exclusion_Present (Def)
16076 and then not Is_Access_Type (Parent_Type)
16077 then
16078 Error_Msg_N ("null exclusion can only apply to an access type", N);
16079 end if;
16081 -- Avoid deriving parent primitives of underlying record views
16083 Build_Derived_Type (N, Parent_Type, T, Is_Completion,
16084 Derive_Subps => not Is_Underlying_Record_View (T));
16086 -- AI-419: The parent type of an explicitly limited derived type must
16087 -- be a limited type or a limited interface.
16089 if Limited_Present (Def) then
16090 Set_Is_Limited_Record (T);
16092 if Is_Interface (T) then
16093 Set_Is_Limited_Interface (T);
16094 end if;
16096 if not Is_Limited_Type (Parent_Type)
16097 and then
16098 (not Is_Interface (Parent_Type)
16099 or else not Is_Limited_Interface (Parent_Type))
16100 then
16101 -- AI05-0096: a derivation in the private part of an instance is
16102 -- legal if the generic formal is untagged limited, and the actual
16103 -- is non-limited.
16105 if Is_Generic_Actual_Type (Parent_Type)
16106 and then In_Private_Part (Current_Scope)
16107 and then
16108 not Is_Tagged_Type
16109 (Generic_Parent_Type (Parent (Parent_Type)))
16110 then
16111 null;
16113 else
16114 Error_Msg_NE
16115 ("parent type& of limited type must be limited",
16116 N, Parent_Type);
16117 end if;
16118 end if;
16119 end if;
16121 -- In SPARK, there are no derived type definitions other than type
16122 -- extensions of tagged record types.
16124 if No (Extension) then
16125 Check_SPARK_05_Restriction
16126 ("derived type is not allowed", Original_Node (N));
16127 end if;
16128 end Derived_Type_Declaration;
16130 ------------------------
16131 -- Diagnose_Interface --
16132 ------------------------
16134 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id) is
16135 begin
16136 if not Is_Interface (E) and then E /= Any_Type then
16137 Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
16138 end if;
16139 end Diagnose_Interface;
16141 ----------------------------------
16142 -- Enumeration_Type_Declaration --
16143 ----------------------------------
16145 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
16146 Ev : Uint;
16147 L : Node_Id;
16148 R_Node : Node_Id;
16149 B_Node : Node_Id;
16151 begin
16152 -- Create identifier node representing lower bound
16154 B_Node := New_Node (N_Identifier, Sloc (Def));
16155 L := First (Literals (Def));
16156 Set_Chars (B_Node, Chars (L));
16157 Set_Entity (B_Node, L);
16158 Set_Etype (B_Node, T);
16159 Set_Is_Static_Expression (B_Node, True);
16161 R_Node := New_Node (N_Range, Sloc (Def));
16162 Set_Low_Bound (R_Node, B_Node);
16164 Set_Ekind (T, E_Enumeration_Type);
16165 Set_First_Literal (T, L);
16166 Set_Etype (T, T);
16167 Set_Is_Constrained (T);
16169 Ev := Uint_0;
16171 -- Loop through literals of enumeration type setting pos and rep values
16172 -- except that if the Ekind is already set, then it means the literal
16173 -- was already constructed (case of a derived type declaration and we
16174 -- should not disturb the Pos and Rep values.
16176 while Present (L) loop
16177 if Ekind (L) /= E_Enumeration_Literal then
16178 Set_Ekind (L, E_Enumeration_Literal);
16179 Set_Enumeration_Pos (L, Ev);
16180 Set_Enumeration_Rep (L, Ev);
16181 Set_Is_Known_Valid (L, True);
16182 end if;
16184 Set_Etype (L, T);
16185 New_Overloaded_Entity (L);
16186 Generate_Definition (L);
16187 Set_Convention (L, Convention_Intrinsic);
16189 -- Case of character literal
16191 if Nkind (L) = N_Defining_Character_Literal then
16192 Set_Is_Character_Type (T, True);
16194 -- Check violation of No_Wide_Characters
16196 if Restriction_Check_Required (No_Wide_Characters) then
16197 Get_Name_String (Chars (L));
16199 if Name_Len >= 3 and then Name_Buffer (1 .. 2) = "QW" then
16200 Check_Restriction (No_Wide_Characters, L);
16201 end if;
16202 end if;
16203 end if;
16205 Ev := Ev + 1;
16206 Next (L);
16207 end loop;
16209 -- Now create a node representing upper bound
16211 B_Node := New_Node (N_Identifier, Sloc (Def));
16212 Set_Chars (B_Node, Chars (Last (Literals (Def))));
16213 Set_Entity (B_Node, Last (Literals (Def)));
16214 Set_Etype (B_Node, T);
16215 Set_Is_Static_Expression (B_Node, True);
16217 Set_High_Bound (R_Node, B_Node);
16219 -- Initialize various fields of the type. Some of this information
16220 -- may be overwritten later through rep.clauses.
16222 Set_Scalar_Range (T, R_Node);
16223 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
16224 Set_Enum_Esize (T);
16225 Set_Enum_Pos_To_Rep (T, Empty);
16227 -- Set Discard_Names if configuration pragma set, or if there is
16228 -- a parameterless pragma in the current declarative region
16230 if Global_Discard_Names or else Discard_Names (Scope (T)) then
16231 Set_Discard_Names (T);
16232 end if;
16234 -- Process end label if there is one
16236 if Present (Def) then
16237 Process_End_Label (Def, 'e', T);
16238 end if;
16239 end Enumeration_Type_Declaration;
16241 ---------------------------------
16242 -- Expand_To_Stored_Constraint --
16243 ---------------------------------
16245 function Expand_To_Stored_Constraint
16246 (Typ : Entity_Id;
16247 Constraint : Elist_Id) return Elist_Id
16249 Explicitly_Discriminated_Type : Entity_Id;
16250 Expansion : Elist_Id;
16251 Discriminant : Entity_Id;
16253 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
16254 -- Find the nearest type that actually specifies discriminants
16256 ---------------------------------
16257 -- Type_With_Explicit_Discrims --
16258 ---------------------------------
16260 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
16261 Typ : constant E := Base_Type (Id);
16263 begin
16264 if Ekind (Typ) in Incomplete_Or_Private_Kind then
16265 if Present (Full_View (Typ)) then
16266 return Type_With_Explicit_Discrims (Full_View (Typ));
16267 end if;
16269 else
16270 if Has_Discriminants (Typ) then
16271 return Typ;
16272 end if;
16273 end if;
16275 if Etype (Typ) = Typ then
16276 return Empty;
16277 elsif Has_Discriminants (Typ) then
16278 return Typ;
16279 else
16280 return Type_With_Explicit_Discrims (Etype (Typ));
16281 end if;
16283 end Type_With_Explicit_Discrims;
16285 -- Start of processing for Expand_To_Stored_Constraint
16287 begin
16288 if No (Constraint) or else Is_Empty_Elmt_List (Constraint) then
16289 return No_Elist;
16290 end if;
16292 Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
16294 if No (Explicitly_Discriminated_Type) then
16295 return No_Elist;
16296 end if;
16298 Expansion := New_Elmt_List;
16300 Discriminant :=
16301 First_Stored_Discriminant (Explicitly_Discriminated_Type);
16302 while Present (Discriminant) loop
16303 Append_Elmt
16304 (Get_Discriminant_Value
16305 (Discriminant, Explicitly_Discriminated_Type, Constraint),
16306 To => Expansion);
16307 Next_Stored_Discriminant (Discriminant);
16308 end loop;
16310 return Expansion;
16311 end Expand_To_Stored_Constraint;
16313 ---------------------------
16314 -- Find_Hidden_Interface --
16315 ---------------------------
16317 function Find_Hidden_Interface
16318 (Src : Elist_Id;
16319 Dest : Elist_Id) return Entity_Id
16321 Iface : Entity_Id;
16322 Iface_Elmt : Elmt_Id;
16324 begin
16325 if Present (Src) and then Present (Dest) then
16326 Iface_Elmt := First_Elmt (Src);
16327 while Present (Iface_Elmt) loop
16328 Iface := Node (Iface_Elmt);
16330 if Is_Interface (Iface)
16331 and then not Contain_Interface (Iface, Dest)
16332 then
16333 return Iface;
16334 end if;
16336 Next_Elmt (Iface_Elmt);
16337 end loop;
16338 end if;
16340 return Empty;
16341 end Find_Hidden_Interface;
16343 --------------------
16344 -- Find_Type_Name --
16345 --------------------
16347 function Find_Type_Name (N : Node_Id) return Entity_Id is
16348 Id : constant Entity_Id := Defining_Identifier (N);
16349 Prev : Entity_Id;
16350 New_Id : Entity_Id;
16351 Prev_Par : Node_Id;
16353 procedure Check_Duplicate_Aspects;
16354 -- Check that aspects specified in a completion have not been specified
16355 -- already in the partial view. Type_Invariant and others can be
16356 -- specified on either view but never on both.
16358 procedure Tag_Mismatch;
16359 -- Diagnose a tagged partial view whose full view is untagged.
16360 -- We post the message on the full view, with a reference to
16361 -- the previous partial view. The partial view can be private
16362 -- or incomplete, and these are handled in a different manner,
16363 -- so we determine the position of the error message from the
16364 -- respective slocs of both.
16366 -----------------------------
16367 -- Check_Duplicate_Aspects --
16368 -----------------------------
16369 procedure Check_Duplicate_Aspects is
16370 Prev_Aspects : constant List_Id := Aspect_Specifications (Prev_Par);
16371 Full_Aspects : constant List_Id := Aspect_Specifications (N);
16372 F_Spec, P_Spec : Node_Id;
16374 begin
16375 if Present (Prev_Aspects) and then Present (Full_Aspects) then
16376 F_Spec := First (Full_Aspects);
16377 while Present (F_Spec) loop
16378 P_Spec := First (Prev_Aspects);
16379 while Present (P_Spec) loop
16380 if Chars (Identifier (P_Spec)) = Chars (Identifier (F_Spec))
16381 then
16382 Error_Msg_N
16383 ("aspect already specified in private declaration",
16384 F_Spec);
16385 Remove (F_Spec);
16386 return;
16387 end if;
16389 Next (P_Spec);
16390 end loop;
16392 Next (F_Spec);
16393 end loop;
16394 end if;
16395 end Check_Duplicate_Aspects;
16397 ------------------
16398 -- Tag_Mismatch --
16399 ------------------
16401 procedure Tag_Mismatch is
16402 begin
16403 if Sloc (Prev) < Sloc (Id) then
16404 if Ada_Version >= Ada_2012
16405 and then Nkind (N) = N_Private_Type_Declaration
16406 then
16407 Error_Msg_NE
16408 ("declaration of private } must be a tagged type ", Id, Prev);
16409 else
16410 Error_Msg_NE
16411 ("full declaration of } must be a tagged type ", Id, Prev);
16412 end if;
16414 else
16415 if Ada_Version >= Ada_2012
16416 and then Nkind (N) = N_Private_Type_Declaration
16417 then
16418 Error_Msg_NE
16419 ("declaration of private } must be a tagged type ", Prev, Id);
16420 else
16421 Error_Msg_NE
16422 ("full declaration of } must be a tagged type ", Prev, Id);
16423 end if;
16424 end if;
16425 end Tag_Mismatch;
16427 -- Start of processing for Find_Type_Name
16429 begin
16430 -- Find incomplete declaration, if one was given
16432 Prev := Current_Entity_In_Scope (Id);
16434 -- New type declaration
16436 if No (Prev) then
16437 Enter_Name (Id);
16438 return Id;
16440 -- Previous declaration exists
16442 else
16443 Prev_Par := Parent (Prev);
16445 -- Error if not incomplete/private case except if previous
16446 -- declaration is implicit, etc. Enter_Name will emit error if
16447 -- appropriate.
16449 if not Is_Incomplete_Or_Private_Type (Prev) then
16450 Enter_Name (Id);
16451 New_Id := Id;
16453 -- Check invalid completion of private or incomplete type
16455 elsif not Nkind_In (N, N_Full_Type_Declaration,
16456 N_Task_Type_Declaration,
16457 N_Protected_Type_Declaration)
16458 and then
16459 (Ada_Version < Ada_2012
16460 or else not Is_Incomplete_Type (Prev)
16461 or else not Nkind_In (N, N_Private_Type_Declaration,
16462 N_Private_Extension_Declaration))
16463 then
16464 -- Completion must be a full type declarations (RM 7.3(4))
16466 Error_Msg_Sloc := Sloc (Prev);
16467 Error_Msg_NE ("invalid completion of }", Id, Prev);
16469 -- Set scope of Id to avoid cascaded errors. Entity is never
16470 -- examined again, except when saving globals in generics.
16472 Set_Scope (Id, Current_Scope);
16473 New_Id := Id;
16475 -- If this is a repeated incomplete declaration, no further
16476 -- checks are possible.
16478 if Nkind (N) = N_Incomplete_Type_Declaration then
16479 return Prev;
16480 end if;
16482 -- Case of full declaration of incomplete type
16484 elsif Ekind (Prev) = E_Incomplete_Type
16485 and then (Ada_Version < Ada_2012
16486 or else No (Full_View (Prev))
16487 or else not Is_Private_Type (Full_View (Prev)))
16488 then
16489 -- Indicate that the incomplete declaration has a matching full
16490 -- declaration. The defining occurrence of the incomplete
16491 -- declaration remains the visible one, and the procedure
16492 -- Get_Full_View dereferences it whenever the type is used.
16494 if Present (Full_View (Prev)) then
16495 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
16496 end if;
16498 Set_Full_View (Prev, Id);
16499 Append_Entity (Id, Current_Scope);
16500 Set_Is_Public (Id, Is_Public (Prev));
16501 Set_Is_Internal (Id);
16502 New_Id := Prev;
16504 -- If the incomplete view is tagged, a class_wide type has been
16505 -- created already. Use it for the private type as well, in order
16506 -- to prevent multiple incompatible class-wide types that may be
16507 -- created for self-referential anonymous access components.
16509 if Is_Tagged_Type (Prev)
16510 and then Present (Class_Wide_Type (Prev))
16511 then
16512 Set_Ekind (Id, Ekind (Prev)); -- will be reset later
16513 Set_Class_Wide_Type (Id, Class_Wide_Type (Prev));
16515 -- The type of the classwide type is the current Id. Previously
16516 -- this was not done for private declarations because of order-
16517 -- of elaboration issues in the back-end, but gigi now handles
16518 -- this properly.
16520 Set_Etype (Class_Wide_Type (Id), Id);
16521 end if;
16523 -- Case of full declaration of private type
16525 else
16526 -- If the private type was a completion of an incomplete type then
16527 -- update Prev to reference the private type
16529 if Ada_Version >= Ada_2012
16530 and then Ekind (Prev) = E_Incomplete_Type
16531 and then Present (Full_View (Prev))
16532 and then Is_Private_Type (Full_View (Prev))
16533 then
16534 Prev := Full_View (Prev);
16535 Prev_Par := Parent (Prev);
16536 end if;
16538 if Nkind (N) = N_Full_Type_Declaration
16539 and then Nkind_In
16540 (Type_Definition (N), N_Record_Definition,
16541 N_Derived_Type_Definition)
16542 and then Interface_Present (Type_Definition (N))
16543 then
16544 Error_Msg_N
16545 ("completion of private type cannot be an interface", N);
16546 end if;
16548 if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
16549 if Etype (Prev) /= Prev then
16551 -- Prev is a private subtype or a derived type, and needs
16552 -- no completion.
16554 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
16555 New_Id := Id;
16557 elsif Ekind (Prev) = E_Private_Type
16558 and then Nkind_In (N, N_Task_Type_Declaration,
16559 N_Protected_Type_Declaration)
16560 then
16561 Error_Msg_N
16562 ("completion of nonlimited type cannot be limited", N);
16564 elsif Ekind (Prev) = E_Record_Type_With_Private
16565 and then Nkind_In (N, N_Task_Type_Declaration,
16566 N_Protected_Type_Declaration)
16567 then
16568 if not Is_Limited_Record (Prev) then
16569 Error_Msg_N
16570 ("completion of nonlimited type cannot be limited", N);
16572 elsif No (Interface_List (N)) then
16573 Error_Msg_N
16574 ("completion of tagged private type must be tagged",
16576 end if;
16577 end if;
16579 -- Ada 2005 (AI-251): Private extension declaration of a task
16580 -- type or a protected type. This case arises when covering
16581 -- interface types.
16583 elsif Nkind_In (N, N_Task_Type_Declaration,
16584 N_Protected_Type_Declaration)
16585 then
16586 null;
16588 elsif Nkind (N) /= N_Full_Type_Declaration
16589 or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
16590 then
16591 Error_Msg_N
16592 ("full view of private extension must be an extension", N);
16594 elsif not (Abstract_Present (Parent (Prev)))
16595 and then Abstract_Present (Type_Definition (N))
16596 then
16597 Error_Msg_N
16598 ("full view of non-abstract extension cannot be abstract", N);
16599 end if;
16601 if not In_Private_Part (Current_Scope) then
16602 Error_Msg_N
16603 ("declaration of full view must appear in private part", N);
16604 end if;
16606 if Ada_Version >= Ada_2012 then
16607 Check_Duplicate_Aspects;
16608 end if;
16610 Copy_And_Swap (Prev, Id);
16611 Set_Has_Private_Declaration (Prev);
16612 Set_Has_Private_Declaration (Id);
16614 -- AI12-0133: Indicate whether we have a partial view with
16615 -- unknown discriminants, in which case initialization of objects
16616 -- of the type do not receive an invariant check.
16618 Set_Partial_View_Has_Unknown_Discr
16619 (Prev, Has_Unknown_Discriminants (Id));
16621 -- Preserve aspect and iterator flags that may have been set on
16622 -- the partial view.
16624 Set_Has_Delayed_Aspects (Prev, Has_Delayed_Aspects (Id));
16625 Set_Has_Implicit_Dereference (Prev, Has_Implicit_Dereference (Id));
16627 -- If no error, propagate freeze_node from private to full view.
16628 -- It may have been generated for an early operational item.
16630 if Present (Freeze_Node (Id))
16631 and then Serious_Errors_Detected = 0
16632 and then No (Full_View (Id))
16633 then
16634 Set_Freeze_Node (Prev, Freeze_Node (Id));
16635 Set_Freeze_Node (Id, Empty);
16636 Set_First_Rep_Item (Prev, First_Rep_Item (Id));
16637 end if;
16639 Set_Full_View (Id, Prev);
16640 New_Id := Prev;
16641 end if;
16643 -- Verify that full declaration conforms to partial one
16645 if Is_Incomplete_Or_Private_Type (Prev)
16646 and then Present (Discriminant_Specifications (Prev_Par))
16647 then
16648 if Present (Discriminant_Specifications (N)) then
16649 if Ekind (Prev) = E_Incomplete_Type then
16650 Check_Discriminant_Conformance (N, Prev, Prev);
16651 else
16652 Check_Discriminant_Conformance (N, Prev, Id);
16653 end if;
16655 else
16656 Error_Msg_N
16657 ("missing discriminants in full type declaration", N);
16659 -- To avoid cascaded errors on subsequent use, share the
16660 -- discriminants of the partial view.
16662 Set_Discriminant_Specifications (N,
16663 Discriminant_Specifications (Prev_Par));
16664 end if;
16665 end if;
16667 -- A prior untagged partial view can have an associated class-wide
16668 -- type due to use of the class attribute, and in this case the full
16669 -- type must also be tagged. This Ada 95 usage is deprecated in favor
16670 -- of incomplete tagged declarations, but we check for it.
16672 if Is_Type (Prev)
16673 and then (Is_Tagged_Type (Prev)
16674 or else Present (Class_Wide_Type (Prev)))
16675 then
16676 -- Ada 2012 (AI05-0162): A private type may be the completion of
16677 -- an incomplete type.
16679 if Ada_Version >= Ada_2012
16680 and then Is_Incomplete_Type (Prev)
16681 and then Nkind_In (N, N_Private_Type_Declaration,
16682 N_Private_Extension_Declaration)
16683 then
16684 -- No need to check private extensions since they are tagged
16686 if Nkind (N) = N_Private_Type_Declaration
16687 and then not Tagged_Present (N)
16688 then
16689 Tag_Mismatch;
16690 end if;
16692 -- The full declaration is either a tagged type (including
16693 -- a synchronized type that implements interfaces) or a
16694 -- type extension, otherwise this is an error.
16696 elsif Nkind_In (N, N_Task_Type_Declaration,
16697 N_Protected_Type_Declaration)
16698 then
16699 if No (Interface_List (N)) and then not Error_Posted (N) then
16700 Tag_Mismatch;
16701 end if;
16703 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
16705 -- Indicate that the previous declaration (tagged incomplete
16706 -- or private declaration) requires the same on the full one.
16708 if not Tagged_Present (Type_Definition (N)) then
16709 Tag_Mismatch;
16710 Set_Is_Tagged_Type (Id);
16711 end if;
16713 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
16714 if No (Record_Extension_Part (Type_Definition (N))) then
16715 Error_Msg_NE
16716 ("full declaration of } must be a record extension",
16717 Prev, Id);
16719 -- Set some attributes to produce a usable full view
16721 Set_Is_Tagged_Type (Id);
16722 end if;
16724 else
16725 Tag_Mismatch;
16726 end if;
16727 end if;
16729 if Present (Prev)
16730 and then Nkind (Parent (Prev)) = N_Incomplete_Type_Declaration
16731 and then Present (Premature_Use (Parent (Prev)))
16732 then
16733 Error_Msg_Sloc := Sloc (N);
16734 Error_Msg_N
16735 ("\full declaration #", Premature_Use (Parent (Prev)));
16736 end if;
16738 return New_Id;
16739 end if;
16740 end Find_Type_Name;
16742 -------------------------
16743 -- Find_Type_Of_Object --
16744 -------------------------
16746 function Find_Type_Of_Object
16747 (Obj_Def : Node_Id;
16748 Related_Nod : Node_Id) return Entity_Id
16750 Def_Kind : constant Node_Kind := Nkind (Obj_Def);
16751 P : Node_Id := Parent (Obj_Def);
16752 T : Entity_Id;
16753 Nam : Name_Id;
16755 begin
16756 -- If the parent is a component_definition node we climb to the
16757 -- component_declaration node
16759 if Nkind (P) = N_Component_Definition then
16760 P := Parent (P);
16761 end if;
16763 -- Case of an anonymous array subtype
16765 if Nkind_In (Def_Kind, N_Constrained_Array_Definition,
16766 N_Unconstrained_Array_Definition)
16767 then
16768 T := Empty;
16769 Array_Type_Declaration (T, Obj_Def);
16771 -- Create an explicit subtype whenever possible
16773 elsif Nkind (P) /= N_Component_Declaration
16774 and then Def_Kind = N_Subtype_Indication
16775 then
16776 -- Base name of subtype on object name, which will be unique in
16777 -- the current scope.
16779 -- If this is a duplicate declaration, return base type, to avoid
16780 -- generating duplicate anonymous types.
16782 if Error_Posted (P) then
16783 Analyze (Subtype_Mark (Obj_Def));
16784 return Entity (Subtype_Mark (Obj_Def));
16785 end if;
16787 Nam :=
16788 New_External_Name
16789 (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
16791 T := Make_Defining_Identifier (Sloc (P), Nam);
16793 Insert_Action (Obj_Def,
16794 Make_Subtype_Declaration (Sloc (P),
16795 Defining_Identifier => T,
16796 Subtype_Indication => Relocate_Node (Obj_Def)));
16798 -- This subtype may need freezing, and this will not be done
16799 -- automatically if the object declaration is not in declarative
16800 -- part. Since this is an object declaration, the type cannot always
16801 -- be frozen here. Deferred constants do not freeze their type
16802 -- (which often enough will be private).
16804 if Nkind (P) = N_Object_Declaration
16805 and then Constant_Present (P)
16806 and then No (Expression (P))
16807 then
16808 null;
16810 -- Here we freeze the base type of object type to catch premature use
16811 -- of discriminated private type without a full view.
16813 else
16814 Insert_Actions (Obj_Def, Freeze_Entity (Base_Type (T), P));
16815 end if;
16817 -- Ada 2005 AI-406: the object definition in an object declaration
16818 -- can be an access definition.
16820 elsif Def_Kind = N_Access_Definition then
16821 T := Access_Definition (Related_Nod, Obj_Def);
16823 Set_Is_Local_Anonymous_Access
16825 V => (Ada_Version < Ada_2012)
16826 or else (Nkind (P) /= N_Object_Declaration)
16827 or else Is_Library_Level_Entity (Defining_Identifier (P)));
16829 -- Otherwise, the object definition is just a subtype_mark
16831 else
16832 T := Process_Subtype (Obj_Def, Related_Nod);
16834 -- If expansion is disabled an object definition that is an aggregate
16835 -- will not get expanded and may lead to scoping problems in the back
16836 -- end, if the object is referenced in an inner scope. In that case
16837 -- create an itype reference for the object definition now. This
16838 -- may be redundant in some cases, but harmless.
16840 if Is_Itype (T)
16841 and then Nkind (Related_Nod) = N_Object_Declaration
16842 and then ASIS_Mode
16843 then
16844 Build_Itype_Reference (T, Related_Nod);
16845 end if;
16846 end if;
16848 return T;
16849 end Find_Type_Of_Object;
16851 --------------------------------
16852 -- Find_Type_Of_Subtype_Indic --
16853 --------------------------------
16855 function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
16856 Typ : Entity_Id;
16858 begin
16859 -- Case of subtype mark with a constraint
16861 if Nkind (S) = N_Subtype_Indication then
16862 Find_Type (Subtype_Mark (S));
16863 Typ := Entity (Subtype_Mark (S));
16865 if not
16866 Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
16867 then
16868 Error_Msg_N
16869 ("incorrect constraint for this kind of type", Constraint (S));
16870 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
16871 end if;
16873 -- Otherwise we have a subtype mark without a constraint
16875 elsif Error_Posted (S) then
16876 Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
16877 return Any_Type;
16879 else
16880 Find_Type (S);
16881 Typ := Entity (S);
16882 end if;
16884 -- Check No_Wide_Characters restriction
16886 Check_Wide_Character_Restriction (Typ, S);
16888 return Typ;
16889 end Find_Type_Of_Subtype_Indic;
16891 -------------------------------------
16892 -- Floating_Point_Type_Declaration --
16893 -------------------------------------
16895 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
16896 Digs : constant Node_Id := Digits_Expression (Def);
16897 Max_Digs_Val : constant Uint := Digits_Value (Standard_Long_Long_Float);
16898 Digs_Val : Uint;
16899 Base_Typ : Entity_Id;
16900 Implicit_Base : Entity_Id;
16901 Bound : Node_Id;
16903 function Can_Derive_From (E : Entity_Id) return Boolean;
16904 -- Find if given digits value, and possibly a specified range, allows
16905 -- derivation from specified type
16907 function Find_Base_Type return Entity_Id;
16908 -- Find a predefined base type that Def can derive from, or generate
16909 -- an error and substitute Long_Long_Float if none exists.
16911 ---------------------
16912 -- Can_Derive_From --
16913 ---------------------
16915 function Can_Derive_From (E : Entity_Id) return Boolean is
16916 Spec : constant Entity_Id := Real_Range_Specification (Def);
16918 begin
16919 -- Check specified "digits" constraint
16921 if Digs_Val > Digits_Value (E) then
16922 return False;
16923 end if;
16925 -- Check for matching range, if specified
16927 if Present (Spec) then
16928 if Expr_Value_R (Type_Low_Bound (E)) >
16929 Expr_Value_R (Low_Bound (Spec))
16930 then
16931 return False;
16932 end if;
16934 if Expr_Value_R (Type_High_Bound (E)) <
16935 Expr_Value_R (High_Bound (Spec))
16936 then
16937 return False;
16938 end if;
16939 end if;
16941 return True;
16942 end Can_Derive_From;
16944 --------------------
16945 -- Find_Base_Type --
16946 --------------------
16948 function Find_Base_Type return Entity_Id is
16949 Choice : Elmt_Id := First_Elmt (Predefined_Float_Types);
16951 begin
16952 -- Iterate over the predefined types in order, returning the first
16953 -- one that Def can derive from.
16955 while Present (Choice) loop
16956 if Can_Derive_From (Node (Choice)) then
16957 return Node (Choice);
16958 end if;
16960 Next_Elmt (Choice);
16961 end loop;
16963 -- If we can't derive from any existing type, use Long_Long_Float
16964 -- and give appropriate message explaining the problem.
16966 if Digs_Val > Max_Digs_Val then
16967 -- It might be the case that there is a type with the requested
16968 -- range, just not the combination of digits and range.
16970 Error_Msg_N
16971 ("no predefined type has requested range and precision",
16972 Real_Range_Specification (Def));
16974 else
16975 Error_Msg_N
16976 ("range too large for any predefined type",
16977 Real_Range_Specification (Def));
16978 end if;
16980 return Standard_Long_Long_Float;
16981 end Find_Base_Type;
16983 -- Start of processing for Floating_Point_Type_Declaration
16985 begin
16986 Check_Restriction (No_Floating_Point, Def);
16988 -- Create an implicit base type
16990 Implicit_Base :=
16991 Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
16993 -- Analyze and verify digits value
16995 Analyze_And_Resolve (Digs, Any_Integer);
16996 Check_Digits_Expression (Digs);
16997 Digs_Val := Expr_Value (Digs);
16999 -- Process possible range spec and find correct type to derive from
17001 Process_Real_Range_Specification (Def);
17003 -- Check that requested number of digits is not too high.
17005 if Digs_Val > Max_Digs_Val then
17007 -- The check for Max_Base_Digits may be somewhat expensive, as it
17008 -- requires reading System, so only do it when necessary.
17010 declare
17011 Max_Base_Digits : constant Uint :=
17012 Expr_Value
17013 (Expression
17014 (Parent (RTE (RE_Max_Base_Digits))));
17016 begin
17017 if Digs_Val > Max_Base_Digits then
17018 Error_Msg_Uint_1 := Max_Base_Digits;
17019 Error_Msg_N ("digits value out of range, maximum is ^", Digs);
17021 elsif No (Real_Range_Specification (Def)) then
17022 Error_Msg_Uint_1 := Max_Digs_Val;
17023 Error_Msg_N ("types with more than ^ digits need range spec "
17024 & "(RM 3.5.7(6))", Digs);
17025 end if;
17026 end;
17027 end if;
17029 -- Find a suitable type to derive from or complain and use a substitute
17031 Base_Typ := Find_Base_Type;
17033 -- If there are bounds given in the declaration use them as the bounds
17034 -- of the type, otherwise use the bounds of the predefined base type
17035 -- that was chosen based on the Digits value.
17037 if Present (Real_Range_Specification (Def)) then
17038 Set_Scalar_Range (T, Real_Range_Specification (Def));
17039 Set_Is_Constrained (T);
17041 -- The bounds of this range must be converted to machine numbers
17042 -- in accordance with RM 4.9(38).
17044 Bound := Type_Low_Bound (T);
17046 if Nkind (Bound) = N_Real_Literal then
17047 Set_Realval
17048 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
17049 Set_Is_Machine_Number (Bound);
17050 end if;
17052 Bound := Type_High_Bound (T);
17054 if Nkind (Bound) = N_Real_Literal then
17055 Set_Realval
17056 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
17057 Set_Is_Machine_Number (Bound);
17058 end if;
17060 else
17061 Set_Scalar_Range (T, Scalar_Range (Base_Typ));
17062 end if;
17064 -- Complete definition of implicit base and declared first subtype. The
17065 -- inheritance of the rep item chain ensures that SPARK-related pragmas
17066 -- are not clobbered when the floating point type acts as a full view of
17067 -- a private type.
17069 Set_Etype (Implicit_Base, Base_Typ);
17070 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
17071 Set_Size_Info (Implicit_Base, Base_Typ);
17072 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
17073 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
17074 Set_Digits_Value (Implicit_Base, Digits_Value (Base_Typ));
17075 Set_Float_Rep (Implicit_Base, Float_Rep (Base_Typ));
17077 Set_Ekind (T, E_Floating_Point_Subtype);
17078 Set_Etype (T, Implicit_Base);
17079 Set_Size_Info (T, Implicit_Base);
17080 Set_RM_Size (T, RM_Size (Implicit_Base));
17081 Inherit_Rep_Item_Chain (T, Implicit_Base);
17082 Set_Digits_Value (T, Digs_Val);
17083 end Floating_Point_Type_Declaration;
17085 ----------------------------
17086 -- Get_Discriminant_Value --
17087 ----------------------------
17089 -- This is the situation:
17091 -- There is a non-derived type
17093 -- type T0 (Dx, Dy, Dz...)
17095 -- There are zero or more levels of derivation, with each derivation
17096 -- either purely inheriting the discriminants, or defining its own.
17098 -- type Ti is new Ti-1
17099 -- or
17100 -- type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
17101 -- or
17102 -- subtype Ti is ...
17104 -- The subtype issue is avoided by the use of Original_Record_Component,
17105 -- and the fact that derived subtypes also derive the constraints.
17107 -- This chain leads back from
17109 -- Typ_For_Constraint
17111 -- Typ_For_Constraint has discriminants, and the value for each
17112 -- discriminant is given by its corresponding Elmt of Constraints.
17114 -- Discriminant is some discriminant in this hierarchy
17116 -- We need to return its value
17118 -- We do this by recursively searching each level, and looking for
17119 -- Discriminant. Once we get to the bottom, we start backing up
17120 -- returning the value for it which may in turn be a discriminant
17121 -- further up, so on the backup we continue the substitution.
17123 function Get_Discriminant_Value
17124 (Discriminant : Entity_Id;
17125 Typ_For_Constraint : Entity_Id;
17126 Constraint : Elist_Id) return Node_Id
17128 function Root_Corresponding_Discriminant
17129 (Discr : Entity_Id) return Entity_Id;
17130 -- Given a discriminant, traverse the chain of inherited discriminants
17131 -- and return the topmost discriminant.
17133 function Search_Derivation_Levels
17134 (Ti : Entity_Id;
17135 Discrim_Values : Elist_Id;
17136 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
17137 -- This is the routine that performs the recursive search of levels
17138 -- as described above.
17140 -------------------------------------
17141 -- Root_Corresponding_Discriminant --
17142 -------------------------------------
17144 function Root_Corresponding_Discriminant
17145 (Discr : Entity_Id) return Entity_Id
17147 D : Entity_Id;
17149 begin
17150 D := Discr;
17151 while Present (Corresponding_Discriminant (D)) loop
17152 D := Corresponding_Discriminant (D);
17153 end loop;
17155 return D;
17156 end Root_Corresponding_Discriminant;
17158 ------------------------------
17159 -- Search_Derivation_Levels --
17160 ------------------------------
17162 function Search_Derivation_Levels
17163 (Ti : Entity_Id;
17164 Discrim_Values : Elist_Id;
17165 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
17167 Assoc : Elmt_Id;
17168 Disc : Entity_Id;
17169 Result : Node_Or_Entity_Id;
17170 Result_Entity : Node_Id;
17172 begin
17173 -- If inappropriate type, return Error, this happens only in
17174 -- cascaded error situations, and we want to avoid a blow up.
17176 if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
17177 return Error;
17178 end if;
17180 -- Look deeper if possible. Use Stored_Constraints only for
17181 -- untagged types. For tagged types use the given constraint.
17182 -- This asymmetry needs explanation???
17184 if not Stored_Discrim_Values
17185 and then Present (Stored_Constraint (Ti))
17186 and then not Is_Tagged_Type (Ti)
17187 then
17188 Result :=
17189 Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
17190 else
17191 declare
17192 Td : constant Entity_Id := Etype (Ti);
17194 begin
17195 if Td = Ti then
17196 Result := Discriminant;
17198 else
17199 if Present (Stored_Constraint (Ti)) then
17200 Result :=
17201 Search_Derivation_Levels
17202 (Td, Stored_Constraint (Ti), True);
17203 else
17204 Result :=
17205 Search_Derivation_Levels
17206 (Td, Discrim_Values, Stored_Discrim_Values);
17207 end if;
17208 end if;
17209 end;
17210 end if;
17212 -- Extra underlying places to search, if not found above. For
17213 -- concurrent types, the relevant discriminant appears in the
17214 -- corresponding record. For a type derived from a private type
17215 -- without discriminant, the full view inherits the discriminants
17216 -- of the full view of the parent.
17218 if Result = Discriminant then
17219 if Is_Concurrent_Type (Ti)
17220 and then Present (Corresponding_Record_Type (Ti))
17221 then
17222 Result :=
17223 Search_Derivation_Levels (
17224 Corresponding_Record_Type (Ti),
17225 Discrim_Values,
17226 Stored_Discrim_Values);
17228 elsif Is_Private_Type (Ti)
17229 and then not Has_Discriminants (Ti)
17230 and then Present (Full_View (Ti))
17231 and then Etype (Full_View (Ti)) /= Ti
17232 then
17233 Result :=
17234 Search_Derivation_Levels (
17235 Full_View (Ti),
17236 Discrim_Values,
17237 Stored_Discrim_Values);
17238 end if;
17239 end if;
17241 -- If Result is not a (reference to a) discriminant, return it,
17242 -- otherwise set Result_Entity to the discriminant.
17244 if Nkind (Result) = N_Defining_Identifier then
17245 pragma Assert (Result = Discriminant);
17246 Result_Entity := Result;
17248 else
17249 if not Denotes_Discriminant (Result) then
17250 return Result;
17251 end if;
17253 Result_Entity := Entity (Result);
17254 end if;
17256 -- See if this level of derivation actually has discriminants because
17257 -- tagged derivations can add them, hence the lower levels need not
17258 -- have any.
17260 if not Has_Discriminants (Ti) then
17261 return Result;
17262 end if;
17264 -- Scan Ti's discriminants for Result_Entity, and return its
17265 -- corresponding value, if any.
17267 Result_Entity := Original_Record_Component (Result_Entity);
17269 Assoc := First_Elmt (Discrim_Values);
17271 if Stored_Discrim_Values then
17272 Disc := First_Stored_Discriminant (Ti);
17273 else
17274 Disc := First_Discriminant (Ti);
17275 end if;
17277 while Present (Disc) loop
17278 pragma Assert (Present (Assoc));
17280 if Original_Record_Component (Disc) = Result_Entity then
17281 return Node (Assoc);
17282 end if;
17284 Next_Elmt (Assoc);
17286 if Stored_Discrim_Values then
17287 Next_Stored_Discriminant (Disc);
17288 else
17289 Next_Discriminant (Disc);
17290 end if;
17291 end loop;
17293 -- Could not find it
17295 return Result;
17296 end Search_Derivation_Levels;
17298 -- Local Variables
17300 Result : Node_Or_Entity_Id;
17302 -- Start of processing for Get_Discriminant_Value
17304 begin
17305 -- ??? This routine is a gigantic mess and will be deleted. For the
17306 -- time being just test for the trivial case before calling recurse.
17308 if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
17309 declare
17310 D : Entity_Id;
17311 E : Elmt_Id;
17313 begin
17314 D := First_Discriminant (Typ_For_Constraint);
17315 E := First_Elmt (Constraint);
17316 while Present (D) loop
17317 if Chars (D) = Chars (Discriminant) then
17318 return Node (E);
17319 end if;
17321 Next_Discriminant (D);
17322 Next_Elmt (E);
17323 end loop;
17324 end;
17325 end if;
17327 Result := Search_Derivation_Levels
17328 (Typ_For_Constraint, Constraint, False);
17330 -- ??? hack to disappear when this routine is gone
17332 if Nkind (Result) = N_Defining_Identifier then
17333 declare
17334 D : Entity_Id;
17335 E : Elmt_Id;
17337 begin
17338 D := First_Discriminant (Typ_For_Constraint);
17339 E := First_Elmt (Constraint);
17340 while Present (D) loop
17341 if Root_Corresponding_Discriminant (D) = Discriminant then
17342 return Node (E);
17343 end if;
17345 Next_Discriminant (D);
17346 Next_Elmt (E);
17347 end loop;
17348 end;
17349 end if;
17351 pragma Assert (Nkind (Result) /= N_Defining_Identifier);
17352 return Result;
17353 end Get_Discriminant_Value;
17355 --------------------------
17356 -- Has_Range_Constraint --
17357 --------------------------
17359 function Has_Range_Constraint (N : Node_Id) return Boolean is
17360 C : constant Node_Id := Constraint (N);
17362 begin
17363 if Nkind (C) = N_Range_Constraint then
17364 return True;
17366 elsif Nkind (C) = N_Digits_Constraint then
17367 return
17368 Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
17369 or else Present (Range_Constraint (C));
17371 elsif Nkind (C) = N_Delta_Constraint then
17372 return Present (Range_Constraint (C));
17374 else
17375 return False;
17376 end if;
17377 end Has_Range_Constraint;
17379 ------------------------
17380 -- Inherit_Components --
17381 ------------------------
17383 function Inherit_Components
17384 (N : Node_Id;
17385 Parent_Base : Entity_Id;
17386 Derived_Base : Entity_Id;
17387 Is_Tagged : Boolean;
17388 Inherit_Discr : Boolean;
17389 Discs : Elist_Id) return Elist_Id
17391 Assoc_List : constant Elist_Id := New_Elmt_List;
17393 procedure Inherit_Component
17394 (Old_C : Entity_Id;
17395 Plain_Discrim : Boolean := False;
17396 Stored_Discrim : Boolean := False);
17397 -- Inherits component Old_C from Parent_Base to the Derived_Base. If
17398 -- Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
17399 -- True, Old_C is a stored discriminant. If they are both false then
17400 -- Old_C is a regular component.
17402 -----------------------
17403 -- Inherit_Component --
17404 -----------------------
17406 procedure Inherit_Component
17407 (Old_C : Entity_Id;
17408 Plain_Discrim : Boolean := False;
17409 Stored_Discrim : Boolean := False)
17411 procedure Set_Anonymous_Type (Id : Entity_Id);
17412 -- Id denotes the entity of an access discriminant or anonymous
17413 -- access component. Set the type of Id to either the same type of
17414 -- Old_C or create a new one depending on whether the parent and
17415 -- the child types are in the same scope.
17417 ------------------------
17418 -- Set_Anonymous_Type --
17419 ------------------------
17421 procedure Set_Anonymous_Type (Id : Entity_Id) is
17422 Old_Typ : constant Entity_Id := Etype (Old_C);
17424 begin
17425 if Scope (Parent_Base) = Scope (Derived_Base) then
17426 Set_Etype (Id, Old_Typ);
17428 -- The parent and the derived type are in two different scopes.
17429 -- Reuse the type of the original discriminant / component by
17430 -- copying it in order to preserve all attributes.
17432 else
17433 declare
17434 Typ : constant Entity_Id := New_Copy (Old_Typ);
17436 begin
17437 Set_Etype (Id, Typ);
17439 -- Since we do not generate component declarations for
17440 -- inherited components, associate the itype with the
17441 -- derived type.
17443 Set_Associated_Node_For_Itype (Typ, Parent (Derived_Base));
17444 Set_Scope (Typ, Derived_Base);
17445 end;
17446 end if;
17447 end Set_Anonymous_Type;
17449 -- Local variables and constants
17451 New_C : constant Entity_Id := New_Copy (Old_C);
17453 Corr_Discrim : Entity_Id;
17454 Discrim : Entity_Id;
17456 -- Start of processing for Inherit_Component
17458 begin
17459 pragma Assert (not Is_Tagged or not Stored_Discrim);
17461 Set_Parent (New_C, Parent (Old_C));
17463 -- Regular discriminants and components must be inserted in the scope
17464 -- of the Derived_Base. Do it here.
17466 if not Stored_Discrim then
17467 Enter_Name (New_C);
17468 end if;
17470 -- For tagged types the Original_Record_Component must point to
17471 -- whatever this field was pointing to in the parent type. This has
17472 -- already been achieved by the call to New_Copy above.
17474 if not Is_Tagged then
17475 Set_Original_Record_Component (New_C, New_C);
17476 end if;
17478 -- Set the proper type of an access discriminant
17480 if Ekind (New_C) = E_Discriminant
17481 and then Ekind (Etype (New_C)) = E_Anonymous_Access_Type
17482 then
17483 Set_Anonymous_Type (New_C);
17484 end if;
17486 -- If we have inherited a component then see if its Etype contains
17487 -- references to Parent_Base discriminants. In this case, replace
17488 -- these references with the constraints given in Discs. We do not
17489 -- do this for the partial view of private types because this is
17490 -- not needed (only the components of the full view will be used
17491 -- for code generation) and cause problem. We also avoid this
17492 -- transformation in some error situations.
17494 if Ekind (New_C) = E_Component then
17496 -- Set the proper type of an anonymous access component
17498 if Ekind (Etype (New_C)) = E_Anonymous_Access_Type then
17499 Set_Anonymous_Type (New_C);
17501 elsif (Is_Private_Type (Derived_Base)
17502 and then not Is_Generic_Type (Derived_Base))
17503 or else (Is_Empty_Elmt_List (Discs)
17504 and then not Expander_Active)
17505 then
17506 Set_Etype (New_C, Etype (Old_C));
17508 else
17509 -- The current component introduces a circularity of the
17510 -- following kind:
17512 -- limited with Pack_2;
17513 -- package Pack_1 is
17514 -- type T_1 is tagged record
17515 -- Comp : access Pack_2.T_2;
17516 -- ...
17517 -- end record;
17518 -- end Pack_1;
17520 -- with Pack_1;
17521 -- package Pack_2 is
17522 -- type T_2 is new Pack_1.T_1 with ...;
17523 -- end Pack_2;
17525 Set_Etype
17526 (New_C,
17527 Constrain_Component_Type
17528 (Old_C, Derived_Base, N, Parent_Base, Discs));
17529 end if;
17530 end if;
17532 -- In derived tagged types it is illegal to reference a non
17533 -- discriminant component in the parent type. To catch this, mark
17534 -- these components with an Ekind of E_Void. This will be reset in
17535 -- Record_Type_Definition after processing the record extension of
17536 -- the derived type.
17538 -- If the declaration is a private extension, there is no further
17539 -- record extension to process, and the components retain their
17540 -- current kind, because they are visible at this point.
17542 if Is_Tagged and then Ekind (New_C) = E_Component
17543 and then Nkind (N) /= N_Private_Extension_Declaration
17544 then
17545 Set_Ekind (New_C, E_Void);
17546 end if;
17548 if Plain_Discrim then
17549 Set_Corresponding_Discriminant (New_C, Old_C);
17550 Build_Discriminal (New_C);
17552 -- If we are explicitly inheriting a stored discriminant it will be
17553 -- completely hidden.
17555 elsif Stored_Discrim then
17556 Set_Corresponding_Discriminant (New_C, Empty);
17557 Set_Discriminal (New_C, Empty);
17558 Set_Is_Completely_Hidden (New_C);
17560 -- Set the Original_Record_Component of each discriminant in the
17561 -- derived base to point to the corresponding stored that we just
17562 -- created.
17564 Discrim := First_Discriminant (Derived_Base);
17565 while Present (Discrim) loop
17566 Corr_Discrim := Corresponding_Discriminant (Discrim);
17568 -- Corr_Discrim could be missing in an error situation
17570 if Present (Corr_Discrim)
17571 and then Original_Record_Component (Corr_Discrim) = Old_C
17572 then
17573 Set_Original_Record_Component (Discrim, New_C);
17574 end if;
17576 Next_Discriminant (Discrim);
17577 end loop;
17579 Append_Entity (New_C, Derived_Base);
17580 end if;
17582 if not Is_Tagged then
17583 Append_Elmt (Old_C, Assoc_List);
17584 Append_Elmt (New_C, Assoc_List);
17585 end if;
17586 end Inherit_Component;
17588 -- Variables local to Inherit_Component
17590 Loc : constant Source_Ptr := Sloc (N);
17592 Parent_Discrim : Entity_Id;
17593 Stored_Discrim : Entity_Id;
17594 D : Entity_Id;
17595 Component : Entity_Id;
17597 -- Start of processing for Inherit_Components
17599 begin
17600 if not Is_Tagged then
17601 Append_Elmt (Parent_Base, Assoc_List);
17602 Append_Elmt (Derived_Base, Assoc_List);
17603 end if;
17605 -- Inherit parent discriminants if needed
17607 if Inherit_Discr then
17608 Parent_Discrim := First_Discriminant (Parent_Base);
17609 while Present (Parent_Discrim) loop
17610 Inherit_Component (Parent_Discrim, Plain_Discrim => True);
17611 Next_Discriminant (Parent_Discrim);
17612 end loop;
17613 end if;
17615 -- Create explicit stored discrims for untagged types when necessary
17617 if not Has_Unknown_Discriminants (Derived_Base)
17618 and then Has_Discriminants (Parent_Base)
17619 and then not Is_Tagged
17620 and then
17621 (not Inherit_Discr
17622 or else First_Discriminant (Parent_Base) /=
17623 First_Stored_Discriminant (Parent_Base))
17624 then
17625 Stored_Discrim := First_Stored_Discriminant (Parent_Base);
17626 while Present (Stored_Discrim) loop
17627 Inherit_Component (Stored_Discrim, Stored_Discrim => True);
17628 Next_Stored_Discriminant (Stored_Discrim);
17629 end loop;
17630 end if;
17632 -- See if we can apply the second transformation for derived types, as
17633 -- explained in point 6. in the comments above Build_Derived_Record_Type
17634 -- This is achieved by appending Derived_Base discriminants into Discs,
17635 -- which has the side effect of returning a non empty Discs list to the
17636 -- caller of Inherit_Components, which is what we want. This must be
17637 -- done for private derived types if there are explicit stored
17638 -- discriminants, to ensure that we can retrieve the values of the
17639 -- constraints provided in the ancestors.
17641 if Inherit_Discr
17642 and then Is_Empty_Elmt_List (Discs)
17643 and then Present (First_Discriminant (Derived_Base))
17644 and then
17645 (not Is_Private_Type (Derived_Base)
17646 or else Is_Completely_Hidden
17647 (First_Stored_Discriminant (Derived_Base))
17648 or else Is_Generic_Type (Derived_Base))
17649 then
17650 D := First_Discriminant (Derived_Base);
17651 while Present (D) loop
17652 Append_Elmt (New_Occurrence_Of (D, Loc), Discs);
17653 Next_Discriminant (D);
17654 end loop;
17655 end if;
17657 -- Finally, inherit non-discriminant components unless they are not
17658 -- visible because defined or inherited from the full view of the
17659 -- parent. Don't inherit the _parent field of the parent type.
17661 Component := First_Entity (Parent_Base);
17662 while Present (Component) loop
17664 -- Ada 2005 (AI-251): Do not inherit components associated with
17665 -- secondary tags of the parent.
17667 if Ekind (Component) = E_Component
17668 and then Present (Related_Type (Component))
17669 then
17670 null;
17672 elsif Ekind (Component) /= E_Component
17673 or else Chars (Component) = Name_uParent
17674 then
17675 null;
17677 -- If the derived type is within the parent type's declarative
17678 -- region, then the components can still be inherited even though
17679 -- they aren't visible at this point. This can occur for cases
17680 -- such as within public child units where the components must
17681 -- become visible upon entering the child unit's private part.
17683 elsif not Is_Visible_Component (Component)
17684 and then not In_Open_Scopes (Scope (Parent_Base))
17685 then
17686 null;
17688 elsif Ekind_In (Derived_Base, E_Private_Type,
17689 E_Limited_Private_Type)
17690 then
17691 null;
17693 else
17694 Inherit_Component (Component);
17695 end if;
17697 Next_Entity (Component);
17698 end loop;
17700 -- For tagged derived types, inherited discriminants cannot be used in
17701 -- component declarations of the record extension part. To achieve this
17702 -- we mark the inherited discriminants as not visible.
17704 if Is_Tagged and then Inherit_Discr then
17705 D := First_Discriminant (Derived_Base);
17706 while Present (D) loop
17707 Set_Is_Immediately_Visible (D, False);
17708 Next_Discriminant (D);
17709 end loop;
17710 end if;
17712 return Assoc_List;
17713 end Inherit_Components;
17715 -----------------------------
17716 -- Inherit_Predicate_Flags --
17717 -----------------------------
17719 procedure Inherit_Predicate_Flags (Subt, Par : Entity_Id) is
17720 begin
17721 Set_Has_Predicates (Subt, Has_Predicates (Par));
17722 Set_Has_Static_Predicate_Aspect
17723 (Subt, Has_Static_Predicate_Aspect (Par));
17724 Set_Has_Dynamic_Predicate_Aspect
17725 (Subt, Has_Dynamic_Predicate_Aspect (Par));
17726 end Inherit_Predicate_Flags;
17728 ----------------------
17729 -- Is_EVF_Procedure --
17730 ----------------------
17732 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean is
17733 Formal : Entity_Id;
17735 begin
17736 -- Examine the formals of an Extensions_Visible False procedure looking
17737 -- for a controlling OUT parameter.
17739 if Ekind (Subp) = E_Procedure
17740 and then Extensions_Visible_Status (Subp) = Extensions_Visible_False
17741 then
17742 Formal := First_Formal (Subp);
17743 while Present (Formal) loop
17744 if Ekind (Formal) = E_Out_Parameter
17745 and then Is_Controlling_Formal (Formal)
17746 then
17747 return True;
17748 end if;
17750 Next_Formal (Formal);
17751 end loop;
17752 end if;
17754 return False;
17755 end Is_EVF_Procedure;
17757 -----------------------
17758 -- Is_Null_Extension --
17759 -----------------------
17761 function Is_Null_Extension (T : Entity_Id) return Boolean is
17762 Type_Decl : constant Node_Id := Parent (Base_Type (T));
17763 Comp_List : Node_Id;
17764 Comp : Node_Id;
17766 begin
17767 if Nkind (Type_Decl) /= N_Full_Type_Declaration
17768 or else not Is_Tagged_Type (T)
17769 or else Nkind (Type_Definition (Type_Decl)) /=
17770 N_Derived_Type_Definition
17771 or else No (Record_Extension_Part (Type_Definition (Type_Decl)))
17772 then
17773 return False;
17774 end if;
17776 Comp_List :=
17777 Component_List (Record_Extension_Part (Type_Definition (Type_Decl)));
17779 if Present (Discriminant_Specifications (Type_Decl)) then
17780 return False;
17782 elsif Present (Comp_List)
17783 and then Is_Non_Empty_List (Component_Items (Comp_List))
17784 then
17785 Comp := First (Component_Items (Comp_List));
17787 -- Only user-defined components are relevant. The component list
17788 -- may also contain a parent component and internal components
17789 -- corresponding to secondary tags, but these do not determine
17790 -- whether this is a null extension.
17792 while Present (Comp) loop
17793 if Comes_From_Source (Comp) then
17794 return False;
17795 end if;
17797 Next (Comp);
17798 end loop;
17800 return True;
17802 else
17803 return True;
17804 end if;
17805 end Is_Null_Extension;
17807 ------------------------------
17808 -- Is_Valid_Constraint_Kind --
17809 ------------------------------
17811 function Is_Valid_Constraint_Kind
17812 (T_Kind : Type_Kind;
17813 Constraint_Kind : Node_Kind) return Boolean
17815 begin
17816 case T_Kind is
17817 when Enumeration_Kind |
17818 Integer_Kind =>
17819 return Constraint_Kind = N_Range_Constraint;
17821 when Decimal_Fixed_Point_Kind =>
17822 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
17823 N_Range_Constraint);
17825 when Ordinary_Fixed_Point_Kind =>
17826 return Nkind_In (Constraint_Kind, N_Delta_Constraint,
17827 N_Range_Constraint);
17829 when Float_Kind =>
17830 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
17831 N_Range_Constraint);
17833 when Access_Kind |
17834 Array_Kind |
17835 E_Record_Type |
17836 E_Record_Subtype |
17837 Class_Wide_Kind |
17838 E_Incomplete_Type |
17839 Private_Kind |
17840 Concurrent_Kind =>
17841 return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
17843 when others =>
17844 return True; -- Error will be detected later
17845 end case;
17846 end Is_Valid_Constraint_Kind;
17848 --------------------------
17849 -- Is_Visible_Component --
17850 --------------------------
17852 function Is_Visible_Component
17853 (C : Entity_Id;
17854 N : Node_Id := Empty) return Boolean
17856 Original_Comp : Entity_Id := Empty;
17857 Original_Scope : Entity_Id;
17858 Type_Scope : Entity_Id;
17860 function Is_Local_Type (Typ : Entity_Id) return Boolean;
17861 -- Check whether parent type of inherited component is declared locally,
17862 -- possibly within a nested package or instance. The current scope is
17863 -- the derived record itself.
17865 -------------------
17866 -- Is_Local_Type --
17867 -------------------
17869 function Is_Local_Type (Typ : Entity_Id) return Boolean is
17870 Scop : Entity_Id;
17872 begin
17873 Scop := Scope (Typ);
17874 while Present (Scop)
17875 and then Scop /= Standard_Standard
17876 loop
17877 if Scop = Scope (Current_Scope) then
17878 return True;
17879 end if;
17881 Scop := Scope (Scop);
17882 end loop;
17884 return False;
17885 end Is_Local_Type;
17887 -- Start of processing for Is_Visible_Component
17889 begin
17890 if Ekind_In (C, E_Component, E_Discriminant) then
17891 Original_Comp := Original_Record_Component (C);
17892 end if;
17894 if No (Original_Comp) then
17896 -- Premature usage, or previous error
17898 return False;
17900 else
17901 Original_Scope := Scope (Original_Comp);
17902 Type_Scope := Scope (Base_Type (Scope (C)));
17903 end if;
17905 -- This test only concerns tagged types
17907 if not Is_Tagged_Type (Original_Scope) then
17908 return True;
17910 -- If it is _Parent or _Tag, there is no visibility issue
17912 elsif not Comes_From_Source (Original_Comp) then
17913 return True;
17915 -- Discriminants are visible unless the (private) type has unknown
17916 -- discriminants. If the discriminant reference is inserted for a
17917 -- discriminant check on a full view it is also visible.
17919 elsif Ekind (Original_Comp) = E_Discriminant
17920 and then
17921 (not Has_Unknown_Discriminants (Original_Scope)
17922 or else (Present (N)
17923 and then Nkind (N) = N_Selected_Component
17924 and then Nkind (Prefix (N)) = N_Type_Conversion
17925 and then not Comes_From_Source (Prefix (N))))
17926 then
17927 return True;
17929 -- In the body of an instantiation, no need to check for the visibility
17930 -- of a component.
17932 elsif In_Instance_Body then
17933 return True;
17935 -- If the component has been declared in an ancestor which is currently
17936 -- a private type, then it is not visible. The same applies if the
17937 -- component's containing type is not in an open scope and the original
17938 -- component's enclosing type is a visible full view of a private type
17939 -- (which can occur in cases where an attempt is being made to reference
17940 -- a component in a sibling package that is inherited from a visible
17941 -- component of a type in an ancestor package; the component in the
17942 -- sibling package should not be visible even though the component it
17943 -- inherited from is visible). This does not apply however in the case
17944 -- where the scope of the type is a private child unit, or when the
17945 -- parent comes from a local package in which the ancestor is currently
17946 -- visible. The latter suppression of visibility is needed for cases
17947 -- that are tested in B730006.
17949 elsif Is_Private_Type (Original_Scope)
17950 or else
17951 (not Is_Private_Descendant (Type_Scope)
17952 and then not In_Open_Scopes (Type_Scope)
17953 and then Has_Private_Declaration (Original_Scope))
17954 then
17955 -- If the type derives from an entity in a formal package, there
17956 -- are no additional visible components.
17958 if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
17959 N_Formal_Package_Declaration
17960 then
17961 return False;
17963 -- if we are not in the private part of the current package, there
17964 -- are no additional visible components.
17966 elsif Ekind (Scope (Current_Scope)) = E_Package
17967 and then not In_Private_Part (Scope (Current_Scope))
17968 then
17969 return False;
17970 else
17971 return
17972 Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
17973 and then In_Open_Scopes (Scope (Original_Scope))
17974 and then Is_Local_Type (Type_Scope);
17975 end if;
17977 -- There is another weird way in which a component may be invisible when
17978 -- the private and the full view are not derived from the same ancestor.
17979 -- Here is an example :
17981 -- type A1 is tagged record F1 : integer; end record;
17982 -- type A2 is new A1 with record F2 : integer; end record;
17983 -- type T is new A1 with private;
17984 -- private
17985 -- type T is new A2 with null record;
17987 -- In this case, the full view of T inherits F1 and F2 but the private
17988 -- view inherits only F1
17990 else
17991 declare
17992 Ancestor : Entity_Id := Scope (C);
17994 begin
17995 loop
17996 if Ancestor = Original_Scope then
17997 return True;
17998 elsif Ancestor = Etype (Ancestor) then
17999 return False;
18000 end if;
18002 Ancestor := Etype (Ancestor);
18003 end loop;
18004 end;
18005 end if;
18006 end Is_Visible_Component;
18008 --------------------------
18009 -- Make_Class_Wide_Type --
18010 --------------------------
18012 procedure Make_Class_Wide_Type (T : Entity_Id) is
18013 CW_Type : Entity_Id;
18014 CW_Name : Name_Id;
18015 Next_E : Entity_Id;
18017 begin
18018 if Present (Class_Wide_Type (T)) then
18020 -- The class-wide type is a partially decorated entity created for a
18021 -- unanalyzed tagged type referenced through a limited with clause.
18022 -- When the tagged type is analyzed, its class-wide type needs to be
18023 -- redecorated. Note that we reuse the entity created by Decorate_
18024 -- Tagged_Type in order to preserve all links.
18026 if Materialize_Entity (Class_Wide_Type (T)) then
18027 CW_Type := Class_Wide_Type (T);
18028 Set_Materialize_Entity (CW_Type, False);
18030 -- The class wide type can have been defined by the partial view, in
18031 -- which case everything is already done.
18033 else
18034 return;
18035 end if;
18037 -- Default case, we need to create a new class-wide type
18039 else
18040 CW_Type :=
18041 New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
18042 end if;
18044 -- Inherit root type characteristics
18046 CW_Name := Chars (CW_Type);
18047 Next_E := Next_Entity (CW_Type);
18048 Copy_Node (T, CW_Type);
18049 Set_Comes_From_Source (CW_Type, False);
18050 Set_Chars (CW_Type, CW_Name);
18051 Set_Parent (CW_Type, Parent (T));
18052 Set_Next_Entity (CW_Type, Next_E);
18054 -- Ensure we have a new freeze node for the class-wide type. The partial
18055 -- view may have freeze action of its own, requiring a proper freeze
18056 -- node, and the same freeze node cannot be shared between the two
18057 -- types.
18059 Set_Has_Delayed_Freeze (CW_Type);
18060 Set_Freeze_Node (CW_Type, Empty);
18062 -- Customize the class-wide type: It has no prim. op., it cannot be
18063 -- abstract and its Etype points back to the specific root type.
18065 Set_Ekind (CW_Type, E_Class_Wide_Type);
18066 Set_Is_Tagged_Type (CW_Type, True);
18067 Set_Direct_Primitive_Operations (CW_Type, New_Elmt_List);
18068 Set_Is_Abstract_Type (CW_Type, False);
18069 Set_Is_Constrained (CW_Type, False);
18070 Set_Is_First_Subtype (CW_Type, Is_First_Subtype (T));
18071 Set_Default_SSO (CW_Type);
18073 if Ekind (T) = E_Class_Wide_Subtype then
18074 Set_Etype (CW_Type, Etype (Base_Type (T)));
18075 else
18076 Set_Etype (CW_Type, T);
18077 end if;
18079 Set_No_Tagged_Streams_Pragma (CW_Type, No_Tagged_Streams);
18081 -- If this is the class_wide type of a constrained subtype, it does
18082 -- not have discriminants.
18084 Set_Has_Discriminants (CW_Type,
18085 Has_Discriminants (T) and then not Is_Constrained (T));
18087 Set_Has_Unknown_Discriminants (CW_Type, True);
18088 Set_Class_Wide_Type (T, CW_Type);
18089 Set_Equivalent_Type (CW_Type, Empty);
18091 -- The class-wide type of a class-wide type is itself (RM 3.9(14))
18093 Set_Class_Wide_Type (CW_Type, CW_Type);
18094 end Make_Class_Wide_Type;
18096 ----------------
18097 -- Make_Index --
18098 ----------------
18100 procedure Make_Index
18101 (N : Node_Id;
18102 Related_Nod : Node_Id;
18103 Related_Id : Entity_Id := Empty;
18104 Suffix_Index : Nat := 1;
18105 In_Iter_Schm : Boolean := False)
18107 R : Node_Id;
18108 T : Entity_Id;
18109 Def_Id : Entity_Id := Empty;
18110 Found : Boolean := False;
18112 begin
18113 -- For a discrete range used in a constrained array definition and
18114 -- defined by a range, an implicit conversion to the predefined type
18115 -- INTEGER is assumed if each bound is either a numeric literal, a named
18116 -- number, or an attribute, and the type of both bounds (prior to the
18117 -- implicit conversion) is the type universal_integer. Otherwise, both
18118 -- bounds must be of the same discrete type, other than universal
18119 -- integer; this type must be determinable independently of the
18120 -- context, but using the fact that the type must be discrete and that
18121 -- both bounds must have the same type.
18123 -- Character literals also have a universal type in the absence of
18124 -- of additional context, and are resolved to Standard_Character.
18126 if Nkind (N) = N_Range then
18128 -- The index is given by a range constraint. The bounds are known
18129 -- to be of a consistent type.
18131 if not Is_Overloaded (N) then
18132 T := Etype (N);
18134 -- For universal bounds, choose the specific predefined type
18136 if T = Universal_Integer then
18137 T := Standard_Integer;
18139 elsif T = Any_Character then
18140 Ambiguous_Character (Low_Bound (N));
18142 T := Standard_Character;
18143 end if;
18145 -- The node may be overloaded because some user-defined operators
18146 -- are available, but if a universal interpretation exists it is
18147 -- also the selected one.
18149 elsif Universal_Interpretation (N) = Universal_Integer then
18150 T := Standard_Integer;
18152 else
18153 T := Any_Type;
18155 declare
18156 Ind : Interp_Index;
18157 It : Interp;
18159 begin
18160 Get_First_Interp (N, Ind, It);
18161 while Present (It.Typ) loop
18162 if Is_Discrete_Type (It.Typ) then
18164 if Found
18165 and then not Covers (It.Typ, T)
18166 and then not Covers (T, It.Typ)
18167 then
18168 Error_Msg_N ("ambiguous bounds in discrete range", N);
18169 exit;
18170 else
18171 T := It.Typ;
18172 Found := True;
18173 end if;
18174 end if;
18176 Get_Next_Interp (Ind, It);
18177 end loop;
18179 if T = Any_Type then
18180 Error_Msg_N ("discrete type required for range", N);
18181 Set_Etype (N, Any_Type);
18182 return;
18184 elsif T = Universal_Integer then
18185 T := Standard_Integer;
18186 end if;
18187 end;
18188 end if;
18190 if not Is_Discrete_Type (T) then
18191 Error_Msg_N ("discrete type required for range", N);
18192 Set_Etype (N, Any_Type);
18193 return;
18194 end if;
18196 if Nkind (Low_Bound (N)) = N_Attribute_Reference
18197 and then Attribute_Name (Low_Bound (N)) = Name_First
18198 and then Is_Entity_Name (Prefix (Low_Bound (N)))
18199 and then Is_Type (Entity (Prefix (Low_Bound (N))))
18200 and then Is_Discrete_Type (Entity (Prefix (Low_Bound (N))))
18201 then
18202 -- The type of the index will be the type of the prefix, as long
18203 -- as the upper bound is 'Last of the same type.
18205 Def_Id := Entity (Prefix (Low_Bound (N)));
18207 if Nkind (High_Bound (N)) /= N_Attribute_Reference
18208 or else Attribute_Name (High_Bound (N)) /= Name_Last
18209 or else not Is_Entity_Name (Prefix (High_Bound (N)))
18210 or else Entity (Prefix (High_Bound (N))) /= Def_Id
18211 then
18212 Def_Id := Empty;
18213 end if;
18214 end if;
18216 R := N;
18217 Process_Range_Expr_In_Decl (R, T, In_Iter_Schm => In_Iter_Schm);
18219 elsif Nkind (N) = N_Subtype_Indication then
18221 -- The index is given by a subtype with a range constraint
18223 T := Base_Type (Entity (Subtype_Mark (N)));
18225 if not Is_Discrete_Type (T) then
18226 Error_Msg_N ("discrete type required for range", N);
18227 Set_Etype (N, Any_Type);
18228 return;
18229 end if;
18231 R := Range_Expression (Constraint (N));
18233 Resolve (R, T);
18234 Process_Range_Expr_In_Decl
18235 (R, Entity (Subtype_Mark (N)), In_Iter_Schm => In_Iter_Schm);
18237 elsif Nkind (N) = N_Attribute_Reference then
18239 -- Catch beginner's error (use of attribute other than 'Range)
18241 if Attribute_Name (N) /= Name_Range then
18242 Error_Msg_N ("expect attribute ''Range", N);
18243 Set_Etype (N, Any_Type);
18244 return;
18245 end if;
18247 -- If the node denotes the range of a type mark, that is also the
18248 -- resulting type, and we do not need to create an Itype for it.
18250 if Is_Entity_Name (Prefix (N))
18251 and then Comes_From_Source (N)
18252 and then Is_Type (Entity (Prefix (N)))
18253 and then Is_Discrete_Type (Entity (Prefix (N)))
18254 then
18255 Def_Id := Entity (Prefix (N));
18256 end if;
18258 Analyze_And_Resolve (N);
18259 T := Etype (N);
18260 R := N;
18262 -- If none of the above, must be a subtype. We convert this to a
18263 -- range attribute reference because in the case of declared first
18264 -- named subtypes, the types in the range reference can be different
18265 -- from the type of the entity. A range attribute normalizes the
18266 -- reference and obtains the correct types for the bounds.
18268 -- This transformation is in the nature of an expansion, is only
18269 -- done if expansion is active. In particular, it is not done on
18270 -- formal generic types, because we need to retain the name of the
18271 -- original index for instantiation purposes.
18273 else
18274 if not Is_Entity_Name (N) or else not Is_Type (Entity (N)) then
18275 Error_Msg_N ("invalid subtype mark in discrete range ", N);
18276 Set_Etype (N, Any_Integer);
18277 return;
18279 else
18280 -- The type mark may be that of an incomplete type. It is only
18281 -- now that we can get the full view, previous analysis does
18282 -- not look specifically for a type mark.
18284 Set_Entity (N, Get_Full_View (Entity (N)));
18285 Set_Etype (N, Entity (N));
18286 Def_Id := Entity (N);
18288 if not Is_Discrete_Type (Def_Id) then
18289 Error_Msg_N ("discrete type required for index", N);
18290 Set_Etype (N, Any_Type);
18291 return;
18292 end if;
18293 end if;
18295 if Expander_Active then
18296 Rewrite (N,
18297 Make_Attribute_Reference (Sloc (N),
18298 Attribute_Name => Name_Range,
18299 Prefix => Relocate_Node (N)));
18301 -- The original was a subtype mark that does not freeze. This
18302 -- means that the rewritten version must not freeze either.
18304 Set_Must_Not_Freeze (N);
18305 Set_Must_Not_Freeze (Prefix (N));
18306 Analyze_And_Resolve (N);
18307 T := Etype (N);
18308 R := N;
18310 -- If expander is inactive, type is legal, nothing else to construct
18312 else
18313 return;
18314 end if;
18315 end if;
18317 if not Is_Discrete_Type (T) then
18318 Error_Msg_N ("discrete type required for range", N);
18319 Set_Etype (N, Any_Type);
18320 return;
18322 elsif T = Any_Type then
18323 Set_Etype (N, Any_Type);
18324 return;
18325 end if;
18327 -- We will now create the appropriate Itype to describe the range, but
18328 -- first a check. If we originally had a subtype, then we just label
18329 -- the range with this subtype. Not only is there no need to construct
18330 -- a new subtype, but it is wrong to do so for two reasons:
18332 -- 1. A legality concern, if we have a subtype, it must not freeze,
18333 -- and the Itype would cause freezing incorrectly
18335 -- 2. An efficiency concern, if we created an Itype, it would not be
18336 -- recognized as the same type for the purposes of eliminating
18337 -- checks in some circumstances.
18339 -- We signal this case by setting the subtype entity in Def_Id
18341 if No (Def_Id) then
18342 Def_Id :=
18343 Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
18344 Set_Etype (Def_Id, Base_Type (T));
18346 if Is_Signed_Integer_Type (T) then
18347 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
18349 elsif Is_Modular_Integer_Type (T) then
18350 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
18352 else
18353 Set_Ekind (Def_Id, E_Enumeration_Subtype);
18354 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
18355 Set_First_Literal (Def_Id, First_Literal (T));
18356 end if;
18358 Set_Size_Info (Def_Id, (T));
18359 Set_RM_Size (Def_Id, RM_Size (T));
18360 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
18362 Set_Scalar_Range (Def_Id, R);
18363 Conditional_Delay (Def_Id, T);
18365 if Nkind (N) = N_Subtype_Indication then
18366 Inherit_Predicate_Flags (Def_Id, Entity (Subtype_Mark (N)));
18367 end if;
18369 -- In the subtype indication case, if the immediate parent of the
18370 -- new subtype is non-static, then the subtype we create is non-
18371 -- static, even if its bounds are static.
18373 if Nkind (N) = N_Subtype_Indication
18374 and then not Is_OK_Static_Subtype (Entity (Subtype_Mark (N)))
18375 then
18376 Set_Is_Non_Static_Subtype (Def_Id);
18377 end if;
18378 end if;
18380 -- Final step is to label the index with this constructed type
18382 Set_Etype (N, Def_Id);
18383 end Make_Index;
18385 ------------------------------
18386 -- Modular_Type_Declaration --
18387 ------------------------------
18389 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
18390 Mod_Expr : constant Node_Id := Expression (Def);
18391 M_Val : Uint;
18393 procedure Set_Modular_Size (Bits : Int);
18394 -- Sets RM_Size to Bits, and Esize to normal word size above this
18396 ----------------------
18397 -- Set_Modular_Size --
18398 ----------------------
18400 procedure Set_Modular_Size (Bits : Int) is
18401 begin
18402 Set_RM_Size (T, UI_From_Int (Bits));
18404 if Bits <= 8 then
18405 Init_Esize (T, 8);
18407 elsif Bits <= 16 then
18408 Init_Esize (T, 16);
18410 elsif Bits <= 32 then
18411 Init_Esize (T, 32);
18413 else
18414 Init_Esize (T, System_Max_Binary_Modulus_Power);
18415 end if;
18417 if not Non_Binary_Modulus (T) and then Esize (T) = RM_Size (T) then
18418 Set_Is_Known_Valid (T);
18419 end if;
18420 end Set_Modular_Size;
18422 -- Start of processing for Modular_Type_Declaration
18424 begin
18425 -- If the mod expression is (exactly) 2 * literal, where literal is
18426 -- 64 or less,then almost certainly the * was meant to be **. Warn.
18428 if Warn_On_Suspicious_Modulus_Value
18429 and then Nkind (Mod_Expr) = N_Op_Multiply
18430 and then Nkind (Left_Opnd (Mod_Expr)) = N_Integer_Literal
18431 and then Intval (Left_Opnd (Mod_Expr)) = Uint_2
18432 and then Nkind (Right_Opnd (Mod_Expr)) = N_Integer_Literal
18433 and then Intval (Right_Opnd (Mod_Expr)) <= Uint_64
18434 then
18435 Error_Msg_N
18436 ("suspicious MOD value, was '*'* intended'??M?", Mod_Expr);
18437 end if;
18439 -- Proceed with analysis of mod expression
18441 Analyze_And_Resolve (Mod_Expr, Any_Integer);
18442 Set_Etype (T, T);
18443 Set_Ekind (T, E_Modular_Integer_Type);
18444 Init_Alignment (T);
18445 Set_Is_Constrained (T);
18447 if not Is_OK_Static_Expression (Mod_Expr) then
18448 Flag_Non_Static_Expr
18449 ("non-static expression used for modular type bound!", Mod_Expr);
18450 M_Val := 2 ** System_Max_Binary_Modulus_Power;
18451 else
18452 M_Val := Expr_Value (Mod_Expr);
18453 end if;
18455 if M_Val < 1 then
18456 Error_Msg_N ("modulus value must be positive", Mod_Expr);
18457 M_Val := 2 ** System_Max_Binary_Modulus_Power;
18458 end if;
18460 if M_Val > 2 ** Standard_Long_Integer_Size then
18461 Check_Restriction (No_Long_Long_Integers, Mod_Expr);
18462 end if;
18464 Set_Modulus (T, M_Val);
18466 -- Create bounds for the modular type based on the modulus given in
18467 -- the type declaration and then analyze and resolve those bounds.
18469 Set_Scalar_Range (T,
18470 Make_Range (Sloc (Mod_Expr),
18471 Low_Bound => Make_Integer_Literal (Sloc (Mod_Expr), 0),
18472 High_Bound => Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
18474 -- Properly analyze the literals for the range. We do this manually
18475 -- because we can't go calling Resolve, since we are resolving these
18476 -- bounds with the type, and this type is certainly not complete yet.
18478 Set_Etype (Low_Bound (Scalar_Range (T)), T);
18479 Set_Etype (High_Bound (Scalar_Range (T)), T);
18480 Set_Is_Static_Expression (Low_Bound (Scalar_Range (T)));
18481 Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
18483 -- Loop through powers of two to find number of bits required
18485 for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
18487 -- Binary case
18489 if M_Val = 2 ** Bits then
18490 Set_Modular_Size (Bits);
18491 return;
18493 -- Non-binary case
18495 elsif M_Val < 2 ** Bits then
18496 Check_SPARK_05_Restriction ("modulus should be a power of 2", T);
18497 Set_Non_Binary_Modulus (T);
18499 if Bits > System_Max_Nonbinary_Modulus_Power then
18500 Error_Msg_Uint_1 :=
18501 UI_From_Int (System_Max_Nonbinary_Modulus_Power);
18502 Error_Msg_F
18503 ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
18504 Set_Modular_Size (System_Max_Binary_Modulus_Power);
18505 return;
18507 else
18508 -- In the non-binary case, set size as per RM 13.3(55)
18510 Set_Modular_Size (Bits);
18511 return;
18512 end if;
18513 end if;
18515 end loop;
18517 -- If we fall through, then the size exceed System.Max_Binary_Modulus
18518 -- so we just signal an error and set the maximum size.
18520 Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
18521 Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
18523 Set_Modular_Size (System_Max_Binary_Modulus_Power);
18524 Init_Alignment (T);
18526 end Modular_Type_Declaration;
18528 --------------------------
18529 -- New_Concatenation_Op --
18530 --------------------------
18532 procedure New_Concatenation_Op (Typ : Entity_Id) is
18533 Loc : constant Source_Ptr := Sloc (Typ);
18534 Op : Entity_Id;
18536 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
18537 -- Create abbreviated declaration for the formal of a predefined
18538 -- Operator 'Op' of type 'Typ'
18540 --------------------
18541 -- Make_Op_Formal --
18542 --------------------
18544 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
18545 Formal : Entity_Id;
18546 begin
18547 Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
18548 Set_Etype (Formal, Typ);
18549 Set_Mechanism (Formal, Default_Mechanism);
18550 return Formal;
18551 end Make_Op_Formal;
18553 -- Start of processing for New_Concatenation_Op
18555 begin
18556 Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
18558 Set_Ekind (Op, E_Operator);
18559 Set_Scope (Op, Current_Scope);
18560 Set_Etype (Op, Typ);
18561 Set_Homonym (Op, Get_Name_Entity_Id (Name_Op_Concat));
18562 Set_Is_Immediately_Visible (Op);
18563 Set_Is_Intrinsic_Subprogram (Op);
18564 Set_Has_Completion (Op);
18565 Append_Entity (Op, Current_Scope);
18567 Set_Name_Entity_Id (Name_Op_Concat, Op);
18569 Append_Entity (Make_Op_Formal (Typ, Op), Op);
18570 Append_Entity (Make_Op_Formal (Typ, Op), Op);
18571 end New_Concatenation_Op;
18573 -------------------------
18574 -- OK_For_Limited_Init --
18575 -------------------------
18577 -- ???Check all calls of this, and compare the conditions under which it's
18578 -- called.
18580 function OK_For_Limited_Init
18581 (Typ : Entity_Id;
18582 Exp : Node_Id) return Boolean
18584 begin
18585 return Is_CPP_Constructor_Call (Exp)
18586 or else (Ada_Version >= Ada_2005
18587 and then not Debug_Flag_Dot_L
18588 and then OK_For_Limited_Init_In_05 (Typ, Exp));
18589 end OK_For_Limited_Init;
18591 -------------------------------
18592 -- OK_For_Limited_Init_In_05 --
18593 -------------------------------
18595 function OK_For_Limited_Init_In_05
18596 (Typ : Entity_Id;
18597 Exp : Node_Id) return Boolean
18599 begin
18600 -- An object of a limited interface type can be initialized with any
18601 -- expression of a nonlimited descendant type.
18603 if Is_Class_Wide_Type (Typ)
18604 and then Is_Limited_Interface (Typ)
18605 and then not Is_Limited_Type (Etype (Exp))
18606 then
18607 return True;
18608 end if;
18610 -- Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
18611 -- case of limited aggregates (including extension aggregates), and
18612 -- function calls. The function call may have been given in prefixed
18613 -- notation, in which case the original node is an indexed component.
18614 -- If the function is parameterless, the original node was an explicit
18615 -- dereference. The function may also be parameterless, in which case
18616 -- the source node is just an identifier.
18618 case Nkind (Original_Node (Exp)) is
18619 when N_Aggregate | N_Extension_Aggregate | N_Function_Call | N_Op =>
18620 return True;
18622 when N_Identifier =>
18623 return Present (Entity (Original_Node (Exp)))
18624 and then Ekind (Entity (Original_Node (Exp))) = E_Function;
18626 when N_Qualified_Expression =>
18627 return
18628 OK_For_Limited_Init_In_05
18629 (Typ, Expression (Original_Node (Exp)));
18631 -- Ada 2005 (AI-251): If a class-wide interface object is initialized
18632 -- with a function call, the expander has rewritten the call into an
18633 -- N_Type_Conversion node to force displacement of the pointer to
18634 -- reference the component containing the secondary dispatch table.
18635 -- Otherwise a type conversion is not a legal context.
18636 -- A return statement for a build-in-place function returning a
18637 -- synchronized type also introduces an unchecked conversion.
18639 when N_Type_Conversion |
18640 N_Unchecked_Type_Conversion =>
18641 return not Comes_From_Source (Exp)
18642 and then
18643 OK_For_Limited_Init_In_05
18644 (Typ, Expression (Original_Node (Exp)));
18646 when N_Indexed_Component |
18647 N_Selected_Component |
18648 N_Explicit_Dereference =>
18649 return Nkind (Exp) = N_Function_Call;
18651 -- A use of 'Input is a function call, hence allowed. Normally the
18652 -- attribute will be changed to a call, but the attribute by itself
18653 -- can occur with -gnatc.
18655 when N_Attribute_Reference =>
18656 return Attribute_Name (Original_Node (Exp)) = Name_Input;
18658 -- For a case expression, all dependent expressions must be legal
18660 when N_Case_Expression =>
18661 declare
18662 Alt : Node_Id;
18664 begin
18665 Alt := First (Alternatives (Original_Node (Exp)));
18666 while Present (Alt) loop
18667 if not OK_For_Limited_Init_In_05 (Typ, Expression (Alt)) then
18668 return False;
18669 end if;
18671 Next (Alt);
18672 end loop;
18674 return True;
18675 end;
18677 -- For an if expression, all dependent expressions must be legal
18679 when N_If_Expression =>
18680 declare
18681 Then_Expr : constant Node_Id :=
18682 Next (First (Expressions (Original_Node (Exp))));
18683 Else_Expr : constant Node_Id := Next (Then_Expr);
18684 begin
18685 return OK_For_Limited_Init_In_05 (Typ, Then_Expr)
18686 and then
18687 OK_For_Limited_Init_In_05 (Typ, Else_Expr);
18688 end;
18690 when others =>
18691 return False;
18692 end case;
18693 end OK_For_Limited_Init_In_05;
18695 -------------------------------------------
18696 -- Ordinary_Fixed_Point_Type_Declaration --
18697 -------------------------------------------
18699 procedure Ordinary_Fixed_Point_Type_Declaration
18700 (T : Entity_Id;
18701 Def : Node_Id)
18703 Loc : constant Source_Ptr := Sloc (Def);
18704 Delta_Expr : constant Node_Id := Delta_Expression (Def);
18705 RRS : constant Node_Id := Real_Range_Specification (Def);
18706 Implicit_Base : Entity_Id;
18707 Delta_Val : Ureal;
18708 Small_Val : Ureal;
18709 Low_Val : Ureal;
18710 High_Val : Ureal;
18712 begin
18713 Check_Restriction (No_Fixed_Point, Def);
18715 -- Create implicit base type
18717 Implicit_Base :=
18718 Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
18719 Set_Etype (Implicit_Base, Implicit_Base);
18721 -- Analyze and process delta expression
18723 Analyze_And_Resolve (Delta_Expr, Any_Real);
18725 Check_Delta_Expression (Delta_Expr);
18726 Delta_Val := Expr_Value_R (Delta_Expr);
18728 Set_Delta_Value (Implicit_Base, Delta_Val);
18730 -- Compute default small from given delta, which is the largest power
18731 -- of two that does not exceed the given delta value.
18733 declare
18734 Tmp : Ureal;
18735 Scale : Int;
18737 begin
18738 Tmp := Ureal_1;
18739 Scale := 0;
18741 if Delta_Val < Ureal_1 then
18742 while Delta_Val < Tmp loop
18743 Tmp := Tmp / Ureal_2;
18744 Scale := Scale + 1;
18745 end loop;
18747 else
18748 loop
18749 Tmp := Tmp * Ureal_2;
18750 exit when Tmp > Delta_Val;
18751 Scale := Scale - 1;
18752 end loop;
18753 end if;
18755 Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
18756 end;
18758 Set_Small_Value (Implicit_Base, Small_Val);
18760 -- If no range was given, set a dummy range
18762 if RRS <= Empty_Or_Error then
18763 Low_Val := -Small_Val;
18764 High_Val := Small_Val;
18766 -- Otherwise analyze and process given range
18768 else
18769 declare
18770 Low : constant Node_Id := Low_Bound (RRS);
18771 High : constant Node_Id := High_Bound (RRS);
18773 begin
18774 Analyze_And_Resolve (Low, Any_Real);
18775 Analyze_And_Resolve (High, Any_Real);
18776 Check_Real_Bound (Low);
18777 Check_Real_Bound (High);
18779 -- Obtain and set the range
18781 Low_Val := Expr_Value_R (Low);
18782 High_Val := Expr_Value_R (High);
18784 if Low_Val > High_Val then
18785 Error_Msg_NE ("??fixed point type& has null range", Def, T);
18786 end if;
18787 end;
18788 end if;
18790 -- The range for both the implicit base and the declared first subtype
18791 -- cannot be set yet, so we use the special routine Set_Fixed_Range to
18792 -- set a temporary range in place. Note that the bounds of the base
18793 -- type will be widened to be symmetrical and to fill the available
18794 -- bits when the type is frozen.
18796 -- We could do this with all discrete types, and probably should, but
18797 -- we absolutely have to do it for fixed-point, since the end-points
18798 -- of the range and the size are determined by the small value, which
18799 -- could be reset before the freeze point.
18801 Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
18802 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
18804 -- Complete definition of first subtype. The inheritance of the rep item
18805 -- chain ensures that SPARK-related pragmas are not clobbered when the
18806 -- ordinary fixed point type acts as a full view of a private type.
18808 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
18809 Set_Etype (T, Implicit_Base);
18810 Init_Size_Align (T);
18811 Inherit_Rep_Item_Chain (T, Implicit_Base);
18812 Set_Small_Value (T, Small_Val);
18813 Set_Delta_Value (T, Delta_Val);
18814 Set_Is_Constrained (T);
18815 end Ordinary_Fixed_Point_Type_Declaration;
18817 ----------------------------------
18818 -- Preanalyze_Assert_Expression --
18819 ----------------------------------
18821 procedure Preanalyze_Assert_Expression (N : Node_Id; T : Entity_Id) is
18822 begin
18823 In_Assertion_Expr := In_Assertion_Expr + 1;
18824 Preanalyze_Spec_Expression (N, T);
18825 In_Assertion_Expr := In_Assertion_Expr - 1;
18826 end Preanalyze_Assert_Expression;
18828 -----------------------------------
18829 -- Preanalyze_Default_Expression --
18830 -----------------------------------
18832 procedure Preanalyze_Default_Expression (N : Node_Id; T : Entity_Id) is
18833 Save_In_Default_Expr : constant Boolean := In_Default_Expr;
18834 begin
18835 In_Default_Expr := True;
18836 Preanalyze_Spec_Expression (N, T);
18837 In_Default_Expr := Save_In_Default_Expr;
18838 end Preanalyze_Default_Expression;
18840 --------------------------------
18841 -- Preanalyze_Spec_Expression --
18842 --------------------------------
18844 procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
18845 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
18846 begin
18847 In_Spec_Expression := True;
18848 Preanalyze_And_Resolve (N, T);
18849 In_Spec_Expression := Save_In_Spec_Expression;
18850 end Preanalyze_Spec_Expression;
18852 ----------------------------------------
18853 -- Prepare_Private_Subtype_Completion --
18854 ----------------------------------------
18856 procedure Prepare_Private_Subtype_Completion
18857 (Id : Entity_Id;
18858 Related_Nod : Node_Id)
18860 Id_B : constant Entity_Id := Base_Type (Id);
18861 Full_B : Entity_Id := Full_View (Id_B);
18862 Full : Entity_Id;
18864 begin
18865 if Present (Full_B) then
18867 -- Get to the underlying full view if necessary
18869 if Is_Private_Type (Full_B)
18870 and then Present (Underlying_Full_View (Full_B))
18871 then
18872 Full_B := Underlying_Full_View (Full_B);
18873 end if;
18875 -- The Base_Type is already completed, we can complete the subtype
18876 -- now. We have to create a new entity with the same name, Thus we
18877 -- can't use Create_Itype.
18879 Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
18880 Set_Is_Itype (Full);
18881 Set_Associated_Node_For_Itype (Full, Related_Nod);
18882 Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
18883 end if;
18885 -- The parent subtype may be private, but the base might not, in some
18886 -- nested instances. In that case, the subtype does not need to be
18887 -- exchanged. It would still be nice to make private subtypes and their
18888 -- bases consistent at all times ???
18890 if Is_Private_Type (Id_B) then
18891 Append_Elmt (Id, Private_Dependents (Id_B));
18892 end if;
18893 end Prepare_Private_Subtype_Completion;
18895 ---------------------------
18896 -- Process_Discriminants --
18897 ---------------------------
18899 procedure Process_Discriminants
18900 (N : Node_Id;
18901 Prev : Entity_Id := Empty)
18903 Elist : constant Elist_Id := New_Elmt_List;
18904 Id : Node_Id;
18905 Discr : Node_Id;
18906 Discr_Number : Uint;
18907 Discr_Type : Entity_Id;
18908 Default_Present : Boolean := False;
18909 Default_Not_Present : Boolean := False;
18911 begin
18912 -- A composite type other than an array type can have discriminants.
18913 -- On entry, the current scope is the composite type.
18915 -- The discriminants are initially entered into the scope of the type
18916 -- via Enter_Name with the default Ekind of E_Void to prevent premature
18917 -- use, as explained at the end of this procedure.
18919 Discr := First (Discriminant_Specifications (N));
18920 while Present (Discr) loop
18921 Enter_Name (Defining_Identifier (Discr));
18923 -- For navigation purposes we add a reference to the discriminant
18924 -- in the entity for the type. If the current declaration is a
18925 -- completion, place references on the partial view. Otherwise the
18926 -- type is the current scope.
18928 if Present (Prev) then
18930 -- The references go on the partial view, if present. If the
18931 -- partial view has discriminants, the references have been
18932 -- generated already.
18934 if not Has_Discriminants (Prev) then
18935 Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
18936 end if;
18937 else
18938 Generate_Reference
18939 (Current_Scope, Defining_Identifier (Discr), 'd');
18940 end if;
18942 if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
18943 Discr_Type := Access_Definition (Discr, Discriminant_Type (Discr));
18945 -- Ada 2005 (AI-254)
18947 if Present (Access_To_Subprogram_Definition
18948 (Discriminant_Type (Discr)))
18949 and then Protected_Present (Access_To_Subprogram_Definition
18950 (Discriminant_Type (Discr)))
18951 then
18952 Discr_Type :=
18953 Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
18954 end if;
18956 else
18957 Find_Type (Discriminant_Type (Discr));
18958 Discr_Type := Etype (Discriminant_Type (Discr));
18960 if Error_Posted (Discriminant_Type (Discr)) then
18961 Discr_Type := Any_Type;
18962 end if;
18963 end if;
18965 -- Handling of discriminants that are access types
18967 if Is_Access_Type (Discr_Type) then
18969 -- Ada 2005 (AI-230): Access discriminant allowed in non-
18970 -- limited record types
18972 if Ada_Version < Ada_2005 then
18973 Check_Access_Discriminant_Requires_Limited
18974 (Discr, Discriminant_Type (Discr));
18975 end if;
18977 if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
18978 Error_Msg_N
18979 ("(Ada 83) access discriminant not allowed", Discr);
18980 end if;
18982 -- If not access type, must be a discrete type
18984 elsif not Is_Discrete_Type (Discr_Type) then
18985 Error_Msg_N
18986 ("discriminants must have a discrete or access type",
18987 Discriminant_Type (Discr));
18988 end if;
18990 Set_Etype (Defining_Identifier (Discr), Discr_Type);
18992 -- If a discriminant specification includes the assignment compound
18993 -- delimiter followed by an expression, the expression is the default
18994 -- expression of the discriminant; the default expression must be of
18995 -- the type of the discriminant. (RM 3.7.1) Since this expression is
18996 -- a default expression, we do the special preanalysis, since this
18997 -- expression does not freeze (see section "Handling of Default and
18998 -- Per-Object Expressions" in spec of package Sem).
19000 if Present (Expression (Discr)) then
19001 Preanalyze_Spec_Expression (Expression (Discr), Discr_Type);
19003 -- Legaity checks
19005 if Nkind (N) = N_Formal_Type_Declaration then
19006 Error_Msg_N
19007 ("discriminant defaults not allowed for formal type",
19008 Expression (Discr));
19010 -- Flag an error for a tagged type with defaulted discriminants,
19011 -- excluding limited tagged types when compiling for Ada 2012
19012 -- (see AI05-0214).
19014 elsif Is_Tagged_Type (Current_Scope)
19015 and then (not Is_Limited_Type (Current_Scope)
19016 or else Ada_Version < Ada_2012)
19017 and then Comes_From_Source (N)
19018 then
19019 -- Note: see similar test in Check_Or_Process_Discriminants, to
19020 -- handle the (illegal) case of the completion of an untagged
19021 -- view with discriminants with defaults by a tagged full view.
19022 -- We skip the check if Discr does not come from source, to
19023 -- account for the case of an untagged derived type providing
19024 -- defaults for a renamed discriminant from a private untagged
19025 -- ancestor with a tagged full view (ACATS B460006).
19027 if Ada_Version >= Ada_2012 then
19028 Error_Msg_N
19029 ("discriminants of nonlimited tagged type cannot have"
19030 & " defaults",
19031 Expression (Discr));
19032 else
19033 Error_Msg_N
19034 ("discriminants of tagged type cannot have defaults",
19035 Expression (Discr));
19036 end if;
19038 else
19039 Default_Present := True;
19040 Append_Elmt (Expression (Discr), Elist);
19042 -- Tag the defining identifiers for the discriminants with
19043 -- their corresponding default expressions from the tree.
19045 Set_Discriminant_Default_Value
19046 (Defining_Identifier (Discr), Expression (Discr));
19047 end if;
19049 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag
19050 -- gets set unless we can be sure that no range check is required.
19052 if (GNATprove_Mode or not Expander_Active)
19053 and then not
19054 Is_In_Range
19055 (Expression (Discr), Discr_Type, Assume_Valid => True)
19056 then
19057 Set_Do_Range_Check (Expression (Discr));
19058 end if;
19060 -- No default discriminant value given
19062 else
19063 Default_Not_Present := True;
19064 end if;
19066 -- Ada 2005 (AI-231): Create an Itype that is a duplicate of
19067 -- Discr_Type but with the null-exclusion attribute
19069 if Ada_Version >= Ada_2005 then
19071 -- Ada 2005 (AI-231): Static checks
19073 if Can_Never_Be_Null (Discr_Type) then
19074 Null_Exclusion_Static_Checks (Discr);
19076 elsif Is_Access_Type (Discr_Type)
19077 and then Null_Exclusion_Present (Discr)
19079 -- No need to check itypes because in their case this check
19080 -- was done at their point of creation
19082 and then not Is_Itype (Discr_Type)
19083 then
19084 if Can_Never_Be_Null (Discr_Type) then
19085 Error_Msg_NE
19086 ("`NOT NULL` not allowed (& already excludes null)",
19087 Discr,
19088 Discr_Type);
19089 end if;
19091 Set_Etype (Defining_Identifier (Discr),
19092 Create_Null_Excluding_Itype
19093 (T => Discr_Type,
19094 Related_Nod => Discr));
19096 -- Check for improper null exclusion if the type is otherwise
19097 -- legal for a discriminant.
19099 elsif Null_Exclusion_Present (Discr)
19100 and then Is_Discrete_Type (Discr_Type)
19101 then
19102 Error_Msg_N
19103 ("null exclusion can only apply to an access type", Discr);
19104 end if;
19106 -- Ada 2005 (AI-402): access discriminants of nonlimited types
19107 -- can't have defaults. Synchronized types, or types that are
19108 -- explicitly limited are fine, but special tests apply to derived
19109 -- types in generics: in a generic body we have to assume the
19110 -- worst, and therefore defaults are not allowed if the parent is
19111 -- a generic formal private type (see ACATS B370001).
19113 if Is_Access_Type (Discr_Type) and then Default_Present then
19114 if Ekind (Discr_Type) /= E_Anonymous_Access_Type
19115 or else Is_Limited_Record (Current_Scope)
19116 or else Is_Concurrent_Type (Current_Scope)
19117 or else Is_Concurrent_Record_Type (Current_Scope)
19118 or else Ekind (Current_Scope) = E_Limited_Private_Type
19119 then
19120 if not Is_Derived_Type (Current_Scope)
19121 or else not Is_Generic_Type (Etype (Current_Scope))
19122 or else not In_Package_Body (Scope (Etype (Current_Scope)))
19123 or else Limited_Present
19124 (Type_Definition (Parent (Current_Scope)))
19125 then
19126 null;
19128 else
19129 Error_Msg_N
19130 ("access discriminants of nonlimited types cannot "
19131 & "have defaults", Expression (Discr));
19132 end if;
19134 elsif Present (Expression (Discr)) then
19135 Error_Msg_N
19136 ("(Ada 2005) access discriminants of nonlimited types "
19137 & "cannot have defaults", Expression (Discr));
19138 end if;
19139 end if;
19140 end if;
19142 -- A discriminant cannot be effectively volatile. This check is only
19143 -- relevant when SPARK_Mode is on as it is not standard Ada legality
19144 -- rule (SPARK RM 7.1.3(6)).
19146 if SPARK_Mode = On
19147 and then Is_Effectively_Volatile (Defining_Identifier (Discr))
19148 then
19149 Error_Msg_N ("discriminant cannot be volatile", Discr);
19150 end if;
19152 Next (Discr);
19153 end loop;
19155 -- An element list consisting of the default expressions of the
19156 -- discriminants is constructed in the above loop and used to set
19157 -- the Discriminant_Constraint attribute for the type. If an object
19158 -- is declared of this (record or task) type without any explicit
19159 -- discriminant constraint given, this element list will form the
19160 -- actual parameters for the corresponding initialization procedure
19161 -- for the type.
19163 Set_Discriminant_Constraint (Current_Scope, Elist);
19164 Set_Stored_Constraint (Current_Scope, No_Elist);
19166 -- Default expressions must be provided either for all or for none
19167 -- of the discriminants of a discriminant part. (RM 3.7.1)
19169 if Default_Present and then Default_Not_Present then
19170 Error_Msg_N
19171 ("incomplete specification of defaults for discriminants", N);
19172 end if;
19174 -- The use of the name of a discriminant is not allowed in default
19175 -- expressions of a discriminant part if the specification of the
19176 -- discriminant is itself given in the discriminant part. (RM 3.7.1)
19178 -- To detect this, the discriminant names are entered initially with an
19179 -- Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
19180 -- attempt to use a void entity (for example in an expression that is
19181 -- type-checked) produces the error message: premature usage. Now after
19182 -- completing the semantic analysis of the discriminant part, we can set
19183 -- the Ekind of all the discriminants appropriately.
19185 Discr := First (Discriminant_Specifications (N));
19186 Discr_Number := Uint_1;
19187 while Present (Discr) loop
19188 Id := Defining_Identifier (Discr);
19189 Set_Ekind (Id, E_Discriminant);
19190 Init_Component_Location (Id);
19191 Init_Esize (Id);
19192 Set_Discriminant_Number (Id, Discr_Number);
19194 -- Make sure this is always set, even in illegal programs
19196 Set_Corresponding_Discriminant (Id, Empty);
19198 -- Initialize the Original_Record_Component to the entity itself.
19199 -- Inherit_Components will propagate the right value to
19200 -- discriminants in derived record types.
19202 Set_Original_Record_Component (Id, Id);
19204 -- Create the discriminal for the discriminant
19206 Build_Discriminal (Id);
19208 Next (Discr);
19209 Discr_Number := Discr_Number + 1;
19210 end loop;
19212 Set_Has_Discriminants (Current_Scope);
19213 end Process_Discriminants;
19215 -----------------------
19216 -- Process_Full_View --
19217 -----------------------
19219 procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
19220 procedure Collect_Implemented_Interfaces
19221 (Typ : Entity_Id;
19222 Ifaces : Elist_Id);
19223 -- Ada 2005: Gather all the interfaces that Typ directly or
19224 -- inherently implements. Duplicate entries are not added to
19225 -- the list Ifaces.
19227 ------------------------------------
19228 -- Collect_Implemented_Interfaces --
19229 ------------------------------------
19231 procedure Collect_Implemented_Interfaces
19232 (Typ : Entity_Id;
19233 Ifaces : Elist_Id)
19235 Iface : Entity_Id;
19236 Iface_Elmt : Elmt_Id;
19238 begin
19239 -- Abstract interfaces are only associated with tagged record types
19241 if not Is_Tagged_Type (Typ) or else not Is_Record_Type (Typ) then
19242 return;
19243 end if;
19245 -- Recursively climb to the ancestors
19247 if Etype (Typ) /= Typ
19249 -- Protect the frontend against wrong cyclic declarations like:
19251 -- type B is new A with private;
19252 -- type C is new A with private;
19253 -- private
19254 -- type B is new C with null record;
19255 -- type C is new B with null record;
19257 and then Etype (Typ) /= Priv_T
19258 and then Etype (Typ) /= Full_T
19259 then
19260 -- Keep separate the management of private type declarations
19262 if Ekind (Typ) = E_Record_Type_With_Private then
19264 -- Handle the following illegal usage:
19265 -- type Private_Type is tagged private;
19266 -- private
19267 -- type Private_Type is new Type_Implementing_Iface;
19269 if Present (Full_View (Typ))
19270 and then Etype (Typ) /= Full_View (Typ)
19271 then
19272 if Is_Interface (Etype (Typ)) then
19273 Append_Unique_Elmt (Etype (Typ), Ifaces);
19274 end if;
19276 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
19277 end if;
19279 -- Non-private types
19281 else
19282 if Is_Interface (Etype (Typ)) then
19283 Append_Unique_Elmt (Etype (Typ), Ifaces);
19284 end if;
19286 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
19287 end if;
19288 end if;
19290 -- Handle entities in the list of abstract interfaces
19292 if Present (Interfaces (Typ)) then
19293 Iface_Elmt := First_Elmt (Interfaces (Typ));
19294 while Present (Iface_Elmt) loop
19295 Iface := Node (Iface_Elmt);
19297 pragma Assert (Is_Interface (Iface));
19299 if not Contain_Interface (Iface, Ifaces) then
19300 Append_Elmt (Iface, Ifaces);
19301 Collect_Implemented_Interfaces (Iface, Ifaces);
19302 end if;
19304 Next_Elmt (Iface_Elmt);
19305 end loop;
19306 end if;
19307 end Collect_Implemented_Interfaces;
19309 -- Local variables
19311 Full_Indic : Node_Id;
19312 Full_Parent : Entity_Id;
19313 Priv_Parent : Entity_Id;
19315 -- Start of processing for Process_Full_View
19317 begin
19318 -- First some sanity checks that must be done after semantic
19319 -- decoration of the full view and thus cannot be placed with other
19320 -- similar checks in Find_Type_Name
19322 if not Is_Limited_Type (Priv_T)
19323 and then (Is_Limited_Type (Full_T)
19324 or else Is_Limited_Composite (Full_T))
19325 then
19326 if In_Instance then
19327 null;
19328 else
19329 Error_Msg_N
19330 ("completion of nonlimited type cannot be limited", Full_T);
19331 Explain_Limited_Type (Full_T, Full_T);
19332 end if;
19334 elsif Is_Abstract_Type (Full_T)
19335 and then not Is_Abstract_Type (Priv_T)
19336 then
19337 Error_Msg_N
19338 ("completion of nonabstract type cannot be abstract", Full_T);
19340 elsif Is_Tagged_Type (Priv_T)
19341 and then Is_Limited_Type (Priv_T)
19342 and then not Is_Limited_Type (Full_T)
19343 then
19344 -- If pragma CPP_Class was applied to the private declaration
19345 -- propagate the limitedness to the full-view
19347 if Is_CPP_Class (Priv_T) then
19348 Set_Is_Limited_Record (Full_T);
19350 -- GNAT allow its own definition of Limited_Controlled to disobey
19351 -- this rule in order in ease the implementation. This test is safe
19352 -- because Root_Controlled is defined in a child of System that
19353 -- normal programs are not supposed to use.
19355 elsif Is_RTE (Etype (Full_T), RE_Root_Controlled) then
19356 Set_Is_Limited_Composite (Full_T);
19357 else
19358 Error_Msg_N
19359 ("completion of limited tagged type must be limited", Full_T);
19360 end if;
19362 elsif Is_Generic_Type (Priv_T) then
19363 Error_Msg_N ("generic type cannot have a completion", Full_T);
19364 end if;
19366 -- Check that ancestor interfaces of private and full views are
19367 -- consistent. We omit this check for synchronized types because
19368 -- they are performed on the corresponding record type when frozen.
19370 if Ada_Version >= Ada_2005
19371 and then Is_Tagged_Type (Priv_T)
19372 and then Is_Tagged_Type (Full_T)
19373 and then not Is_Concurrent_Type (Full_T)
19374 then
19375 declare
19376 Iface : Entity_Id;
19377 Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
19378 Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
19380 begin
19381 Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
19382 Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
19384 -- Ada 2005 (AI-251): The partial view shall be a descendant of
19385 -- an interface type if and only if the full type is descendant
19386 -- of the interface type (AARM 7.3 (7.3/2)).
19388 Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
19390 if Present (Iface) then
19391 Error_Msg_NE
19392 ("interface in partial view& not implemented by full type "
19393 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
19394 end if;
19396 Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
19398 if Present (Iface) then
19399 Error_Msg_NE
19400 ("interface & not implemented by partial view "
19401 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
19402 end if;
19403 end;
19404 end if;
19406 if Is_Tagged_Type (Priv_T)
19407 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
19408 and then Is_Derived_Type (Full_T)
19409 then
19410 Priv_Parent := Etype (Priv_T);
19412 -- The full view of a private extension may have been transformed
19413 -- into an unconstrained derived type declaration and a subtype
19414 -- declaration (see build_derived_record_type for details).
19416 if Nkind (N) = N_Subtype_Declaration then
19417 Full_Indic := Subtype_Indication (N);
19418 Full_Parent := Etype (Base_Type (Full_T));
19419 else
19420 Full_Indic := Subtype_Indication (Type_Definition (N));
19421 Full_Parent := Etype (Full_T);
19422 end if;
19424 -- Check that the parent type of the full type is a descendant of
19425 -- the ancestor subtype given in the private extension. If either
19426 -- entity has an Etype equal to Any_Type then we had some previous
19427 -- error situation [7.3(8)].
19429 if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
19430 return;
19432 -- Ada 2005 (AI-251): Interfaces in the full type can be given in
19433 -- any order. Therefore we don't have to check that its parent must
19434 -- be a descendant of the parent of the private type declaration.
19436 elsif Is_Interface (Priv_Parent)
19437 and then Is_Interface (Full_Parent)
19438 then
19439 null;
19441 -- Ada 2005 (AI-251): If the parent of the private type declaration
19442 -- is an interface there is no need to check that it is an ancestor
19443 -- of the associated full type declaration. The required tests for
19444 -- this case are performed by Build_Derived_Record_Type.
19446 elsif not Is_Interface (Base_Type (Priv_Parent))
19447 and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
19448 then
19449 Error_Msg_N
19450 ("parent of full type must descend from parent"
19451 & " of private extension", Full_Indic);
19453 -- First check a formal restriction, and then proceed with checking
19454 -- Ada rules. Since the formal restriction is not a serious error, we
19455 -- don't prevent further error detection for this check, hence the
19456 -- ELSE.
19458 else
19459 -- In formal mode, when completing a private extension the type
19460 -- named in the private part must be exactly the same as that
19461 -- named in the visible part.
19463 if Priv_Parent /= Full_Parent then
19464 Error_Msg_Name_1 := Chars (Priv_Parent);
19465 Check_SPARK_05_Restriction ("% expected", Full_Indic);
19466 end if;
19468 -- Check the rules of 7.3(10): if the private extension inherits
19469 -- known discriminants, then the full type must also inherit those
19470 -- discriminants from the same (ancestor) type, and the parent
19471 -- subtype of the full type must be constrained if and only if
19472 -- the ancestor subtype of the private extension is constrained.
19474 if No (Discriminant_Specifications (Parent (Priv_T)))
19475 and then not Has_Unknown_Discriminants (Priv_T)
19476 and then Has_Discriminants (Base_Type (Priv_Parent))
19477 then
19478 declare
19479 Priv_Indic : constant Node_Id :=
19480 Subtype_Indication (Parent (Priv_T));
19482 Priv_Constr : constant Boolean :=
19483 Is_Constrained (Priv_Parent)
19484 or else
19485 Nkind (Priv_Indic) = N_Subtype_Indication
19486 or else
19487 Is_Constrained (Entity (Priv_Indic));
19489 Full_Constr : constant Boolean :=
19490 Is_Constrained (Full_Parent)
19491 or else
19492 Nkind (Full_Indic) = N_Subtype_Indication
19493 or else
19494 Is_Constrained (Entity (Full_Indic));
19496 Priv_Discr : Entity_Id;
19497 Full_Discr : Entity_Id;
19499 begin
19500 Priv_Discr := First_Discriminant (Priv_Parent);
19501 Full_Discr := First_Discriminant (Full_Parent);
19502 while Present (Priv_Discr) and then Present (Full_Discr) loop
19503 if Original_Record_Component (Priv_Discr) =
19504 Original_Record_Component (Full_Discr)
19505 or else
19506 Corresponding_Discriminant (Priv_Discr) =
19507 Corresponding_Discriminant (Full_Discr)
19508 then
19509 null;
19510 else
19511 exit;
19512 end if;
19514 Next_Discriminant (Priv_Discr);
19515 Next_Discriminant (Full_Discr);
19516 end loop;
19518 if Present (Priv_Discr) or else Present (Full_Discr) then
19519 Error_Msg_N
19520 ("full view must inherit discriminants of the parent"
19521 & " type used in the private extension", Full_Indic);
19523 elsif Priv_Constr and then not Full_Constr then
19524 Error_Msg_N
19525 ("parent subtype of full type must be constrained",
19526 Full_Indic);
19528 elsif Full_Constr and then not Priv_Constr then
19529 Error_Msg_N
19530 ("parent subtype of full type must be unconstrained",
19531 Full_Indic);
19532 end if;
19533 end;
19535 -- Check the rules of 7.3(12): if a partial view has neither
19536 -- known or unknown discriminants, then the full type
19537 -- declaration shall define a definite subtype.
19539 elsif not Has_Unknown_Discriminants (Priv_T)
19540 and then not Has_Discriminants (Priv_T)
19541 and then not Is_Constrained (Full_T)
19542 then
19543 Error_Msg_N
19544 ("full view must define a constrained type if partial view"
19545 & " has no discriminants", Full_T);
19546 end if;
19548 -- ??????? Do we implement the following properly ?????
19549 -- If the ancestor subtype of a private extension has constrained
19550 -- discriminants, then the parent subtype of the full view shall
19551 -- impose a statically matching constraint on those discriminants
19552 -- [7.3(13)].
19553 end if;
19555 else
19556 -- For untagged types, verify that a type without discriminants is
19557 -- not completed with an unconstrained type. A separate error message
19558 -- is produced if the full type has defaulted discriminants.
19560 if not Is_Indefinite_Subtype (Priv_T)
19561 and then Is_Indefinite_Subtype (Full_T)
19562 then
19563 Error_Msg_Sloc := Sloc (Parent (Priv_T));
19564 Error_Msg_NE
19565 ("full view of& not compatible with declaration#",
19566 Full_T, Priv_T);
19568 if not Is_Tagged_Type (Full_T) then
19569 Error_Msg_N
19570 ("\one is constrained, the other unconstrained", Full_T);
19571 end if;
19572 end if;
19573 end if;
19575 -- AI-419: verify that the use of "limited" is consistent
19577 declare
19578 Orig_Decl : constant Node_Id := Original_Node (N);
19580 begin
19581 if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
19582 and then Nkind (Orig_Decl) = N_Full_Type_Declaration
19583 and then Nkind
19584 (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
19585 then
19586 if not Limited_Present (Parent (Priv_T))
19587 and then not Synchronized_Present (Parent (Priv_T))
19588 and then Limited_Present (Type_Definition (Orig_Decl))
19589 then
19590 Error_Msg_N
19591 ("full view of non-limited extension cannot be limited", N);
19593 -- Conversely, if the partial view carries the limited keyword,
19594 -- the full view must as well, even if it may be redundant.
19596 elsif Limited_Present (Parent (Priv_T))
19597 and then not Limited_Present (Type_Definition (Orig_Decl))
19598 then
19599 Error_Msg_N
19600 ("full view of limited extension must be explicitly limited",
19602 end if;
19603 end if;
19604 end;
19606 -- Ada 2005 (AI-443): A synchronized private extension must be
19607 -- completed by a task or protected type.
19609 if Ada_Version >= Ada_2005
19610 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
19611 and then Synchronized_Present (Parent (Priv_T))
19612 and then not Is_Concurrent_Type (Full_T)
19613 then
19614 Error_Msg_N ("full view of synchronized extension must " &
19615 "be synchronized type", N);
19616 end if;
19618 -- Ada 2005 AI-363: if the full view has discriminants with
19619 -- defaults, it is illegal to declare constrained access subtypes
19620 -- whose designated type is the current type. This allows objects
19621 -- of the type that are declared in the heap to be unconstrained.
19623 if not Has_Unknown_Discriminants (Priv_T)
19624 and then not Has_Discriminants (Priv_T)
19625 and then Has_Discriminants (Full_T)
19626 and then
19627 Present (Discriminant_Default_Value (First_Discriminant (Full_T)))
19628 then
19629 Set_Has_Constrained_Partial_View (Full_T);
19630 Set_Has_Constrained_Partial_View (Priv_T);
19631 end if;
19633 -- Create a full declaration for all its subtypes recorded in
19634 -- Private_Dependents and swap them similarly to the base type. These
19635 -- are subtypes that have been define before the full declaration of
19636 -- the private type. We also swap the entry in Private_Dependents list
19637 -- so we can properly restore the private view on exit from the scope.
19639 declare
19640 Priv_Elmt : Elmt_Id;
19641 Priv_Scop : Entity_Id;
19642 Priv : Entity_Id;
19643 Full : Entity_Id;
19645 begin
19646 Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
19647 while Present (Priv_Elmt) loop
19648 Priv := Node (Priv_Elmt);
19649 Priv_Scop := Scope (Priv);
19651 if Ekind_In (Priv, E_Private_Subtype,
19652 E_Limited_Private_Subtype,
19653 E_Record_Subtype_With_Private)
19654 then
19655 Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
19656 Set_Is_Itype (Full);
19657 Set_Parent (Full, Parent (Priv));
19658 Set_Associated_Node_For_Itype (Full, N);
19660 -- Now we need to complete the private subtype, but since the
19661 -- base type has already been swapped, we must also swap the
19662 -- subtypes (and thus, reverse the arguments in the call to
19663 -- Complete_Private_Subtype). Also note that we may need to
19664 -- re-establish the scope of the private subtype.
19666 Copy_And_Swap (Priv, Full);
19668 if not In_Open_Scopes (Priv_Scop) then
19669 Push_Scope (Priv_Scop);
19671 else
19672 -- Reset Priv_Scop to Empty to indicate no scope was pushed
19674 Priv_Scop := Empty;
19675 end if;
19677 Complete_Private_Subtype (Full, Priv, Full_T, N);
19679 if Present (Priv_Scop) then
19680 Pop_Scope;
19681 end if;
19683 Replace_Elmt (Priv_Elmt, Full);
19684 end if;
19686 Next_Elmt (Priv_Elmt);
19687 end loop;
19688 end;
19690 -- If the private view was tagged, copy the new primitive operations
19691 -- from the private view to the full view.
19693 if Is_Tagged_Type (Full_T) then
19694 declare
19695 Disp_Typ : Entity_Id;
19696 Full_List : Elist_Id;
19697 Prim : Entity_Id;
19698 Prim_Elmt : Elmt_Id;
19699 Priv_List : Elist_Id;
19701 function Contains
19702 (E : Entity_Id;
19703 L : Elist_Id) return Boolean;
19704 -- Determine whether list L contains element E
19706 --------------
19707 -- Contains --
19708 --------------
19710 function Contains
19711 (E : Entity_Id;
19712 L : Elist_Id) return Boolean
19714 List_Elmt : Elmt_Id;
19716 begin
19717 List_Elmt := First_Elmt (L);
19718 while Present (List_Elmt) loop
19719 if Node (List_Elmt) = E then
19720 return True;
19721 end if;
19723 Next_Elmt (List_Elmt);
19724 end loop;
19726 return False;
19727 end Contains;
19729 -- Start of processing
19731 begin
19732 if Is_Tagged_Type (Priv_T) then
19733 Priv_List := Primitive_Operations (Priv_T);
19734 Prim_Elmt := First_Elmt (Priv_List);
19736 -- In the case of a concurrent type completing a private tagged
19737 -- type, primitives may have been declared in between the two
19738 -- views. These subprograms need to be wrapped the same way
19739 -- entries and protected procedures are handled because they
19740 -- cannot be directly shared by the two views.
19742 if Is_Concurrent_Type (Full_T) then
19743 declare
19744 Conc_Typ : constant Entity_Id :=
19745 Corresponding_Record_Type (Full_T);
19746 Curr_Nod : Node_Id := Parent (Conc_Typ);
19747 Wrap_Spec : Node_Id;
19749 begin
19750 while Present (Prim_Elmt) loop
19751 Prim := Node (Prim_Elmt);
19753 if Comes_From_Source (Prim)
19754 and then not Is_Abstract_Subprogram (Prim)
19755 then
19756 Wrap_Spec :=
19757 Make_Subprogram_Declaration (Sloc (Prim),
19758 Specification =>
19759 Build_Wrapper_Spec
19760 (Subp_Id => Prim,
19761 Obj_Typ => Conc_Typ,
19762 Formals =>
19763 Parameter_Specifications (
19764 Parent (Prim))));
19766 Insert_After (Curr_Nod, Wrap_Spec);
19767 Curr_Nod := Wrap_Spec;
19769 Analyze (Wrap_Spec);
19770 end if;
19772 Next_Elmt (Prim_Elmt);
19773 end loop;
19775 return;
19776 end;
19778 -- For non-concurrent types, transfer explicit primitives, but
19779 -- omit those inherited from the parent of the private view
19780 -- since they will be re-inherited later on.
19782 else
19783 Full_List := Primitive_Operations (Full_T);
19785 while Present (Prim_Elmt) loop
19786 Prim := Node (Prim_Elmt);
19788 if Comes_From_Source (Prim)
19789 and then not Contains (Prim, Full_List)
19790 then
19791 Append_Elmt (Prim, Full_List);
19792 end if;
19794 Next_Elmt (Prim_Elmt);
19795 end loop;
19796 end if;
19798 -- Untagged private view
19800 else
19801 Full_List := Primitive_Operations (Full_T);
19803 -- In this case the partial view is untagged, so here we locate
19804 -- all of the earlier primitives that need to be treated as
19805 -- dispatching (those that appear between the two views). Note
19806 -- that these additional operations must all be new operations
19807 -- (any earlier operations that override inherited operations
19808 -- of the full view will already have been inserted in the
19809 -- primitives list, marked by Check_Operation_From_Private_View
19810 -- as dispatching. Note that implicit "/=" operators are
19811 -- excluded from being added to the primitives list since they
19812 -- shouldn't be treated as dispatching (tagged "/=" is handled
19813 -- specially).
19815 Prim := Next_Entity (Full_T);
19816 while Present (Prim) and then Prim /= Priv_T loop
19817 if Ekind_In (Prim, E_Procedure, E_Function) then
19818 Disp_Typ := Find_Dispatching_Type (Prim);
19820 if Disp_Typ = Full_T
19821 and then (Chars (Prim) /= Name_Op_Ne
19822 or else Comes_From_Source (Prim))
19823 then
19824 Check_Controlling_Formals (Full_T, Prim);
19826 if not Is_Dispatching_Operation (Prim) then
19827 Append_Elmt (Prim, Full_List);
19828 Set_Is_Dispatching_Operation (Prim, True);
19829 Set_DT_Position_Value (Prim, No_Uint);
19830 end if;
19832 elsif Is_Dispatching_Operation (Prim)
19833 and then Disp_Typ /= Full_T
19834 then
19836 -- Verify that it is not otherwise controlled by a
19837 -- formal or a return value of type T.
19839 Check_Controlling_Formals (Disp_Typ, Prim);
19840 end if;
19841 end if;
19843 Next_Entity (Prim);
19844 end loop;
19845 end if;
19847 -- For the tagged case, the two views can share the same primitive
19848 -- operations list and the same class-wide type. Update attributes
19849 -- of the class-wide type which depend on the full declaration.
19851 if Is_Tagged_Type (Priv_T) then
19852 Set_Direct_Primitive_Operations (Priv_T, Full_List);
19853 Set_Class_Wide_Type
19854 (Base_Type (Full_T), Class_Wide_Type (Priv_T));
19856 Set_Has_Task (Class_Wide_Type (Priv_T), Has_Task (Full_T));
19857 Set_Has_Protected
19858 (Class_Wide_Type (Priv_T), Has_Protected (Full_T));
19859 end if;
19860 end;
19861 end if;
19863 -- Ada 2005 AI 161: Check preelaborable initialization consistency
19865 if Known_To_Have_Preelab_Init (Priv_T) then
19867 -- Case where there is a pragma Preelaborable_Initialization. We
19868 -- always allow this in predefined units, which is cheating a bit,
19869 -- but it means we don't have to struggle to meet the requirements in
19870 -- the RM for having Preelaborable Initialization. Otherwise we
19871 -- require that the type meets the RM rules. But we can't check that
19872 -- yet, because of the rule about overriding Initialize, so we simply
19873 -- set a flag that will be checked at freeze time.
19875 if not In_Predefined_Unit (Full_T) then
19876 Set_Must_Have_Preelab_Init (Full_T);
19877 end if;
19878 end if;
19880 -- If pragma CPP_Class was applied to the private type declaration,
19881 -- propagate it now to the full type declaration.
19883 if Is_CPP_Class (Priv_T) then
19884 Set_Is_CPP_Class (Full_T);
19885 Set_Convention (Full_T, Convention_CPP);
19887 -- Check that components of imported CPP types do not have default
19888 -- expressions.
19890 Check_CPP_Type_Has_No_Defaults (Full_T);
19891 end if;
19893 -- If the private view has user specified stream attributes, then so has
19894 -- the full view.
19896 -- Why the test, how could these flags be already set in Full_T ???
19898 if Has_Specified_Stream_Read (Priv_T) then
19899 Set_Has_Specified_Stream_Read (Full_T);
19900 end if;
19902 if Has_Specified_Stream_Write (Priv_T) then
19903 Set_Has_Specified_Stream_Write (Full_T);
19904 end if;
19906 if Has_Specified_Stream_Input (Priv_T) then
19907 Set_Has_Specified_Stream_Input (Full_T);
19908 end if;
19910 if Has_Specified_Stream_Output (Priv_T) then
19911 Set_Has_Specified_Stream_Output (Full_T);
19912 end if;
19914 -- Propagate the attributes related to pragma Default_Initial_Condition
19915 -- from the private to the full view. Note that both flags are mutually
19916 -- exclusive.
19918 if Has_Default_Init_Cond (Priv_T)
19919 or else Has_Inherited_Default_Init_Cond (Priv_T)
19920 then
19921 Propagate_Default_Init_Cond_Attributes
19922 (From_Typ => Priv_T,
19923 To_Typ => Full_T,
19924 Private_To_Full_View => True);
19926 -- In the case where the full view is derived from another private type,
19927 -- the attributes related to pragma Default_Initial_Condition must be
19928 -- propagated from the full to the private view to maintain consistency
19929 -- of views.
19931 -- package Pack is
19932 -- type Parent_Typ is private
19933 -- with Default_Initial_Condition ...;
19934 -- private
19935 -- type Parent_Typ is ...;
19936 -- end Pack;
19938 -- with Pack; use Pack;
19939 -- package Pack_2 is
19940 -- type Deriv_Typ is private; -- must inherit
19941 -- private
19942 -- type Deriv_Typ is new Parent_Typ; -- must inherit
19943 -- end Pack_2;
19945 elsif Has_Default_Init_Cond (Full_T)
19946 or else Has_Inherited_Default_Init_Cond (Full_T)
19947 then
19948 Propagate_Default_Init_Cond_Attributes
19949 (From_Typ => Full_T,
19950 To_Typ => Priv_T,
19951 Private_To_Full_View => True);
19952 end if;
19954 -- Propagate the attributes related to pragma Ghost from the private to
19955 -- the full view.
19957 if Is_Ghost_Entity (Priv_T) then
19958 Set_Is_Ghost_Entity (Full_T);
19960 -- The Ghost policy in effect at the point of declaration and at the
19961 -- point of completion must match (SPARK RM 6.9(14)).
19963 Check_Ghost_Completion (Priv_T, Full_T);
19965 -- In the case where the private view of a tagged type lacks a parent
19966 -- type and is subject to pragma Ghost, ensure that the parent type
19967 -- specified by the full view is also Ghost (SPARK RM 6.9(9)).
19969 if Is_Derived_Type (Full_T) then
19970 Check_Ghost_Derivation (Full_T);
19971 end if;
19972 end if;
19974 -- Propagate invariants to full type
19976 if Has_Invariants (Priv_T) then
19977 Set_Has_Invariants (Full_T);
19978 Set_Invariant_Procedure (Full_T, Invariant_Procedure (Priv_T));
19979 end if;
19981 if Has_Inheritable_Invariants (Priv_T) then
19982 Set_Has_Inheritable_Invariants (Full_T);
19983 end if;
19985 -- Check hidden inheritance of class-wide type invariants
19987 if Ada_Version >= Ada_2012
19988 and then not Has_Inheritable_Invariants (Full_T)
19989 and then In_Private_Part (Current_Scope)
19990 and then Has_Interfaces (Full_T)
19991 then
19992 declare
19993 Ifaces : Elist_Id;
19994 AI : Elmt_Id;
19996 begin
19997 Collect_Interfaces (Full_T, Ifaces, Exclude_Parents => True);
19999 AI := First_Elmt (Ifaces);
20000 while Present (AI) loop
20001 if Has_Inheritable_Invariants (Node (AI)) then
20002 Error_Msg_N
20003 ("hidden inheritance of class-wide type invariants " &
20004 "not allowed", N);
20005 exit;
20006 end if;
20008 Next_Elmt (AI);
20009 end loop;
20010 end;
20011 end if;
20013 -- Propagate predicates to full type, and predicate function if already
20014 -- defined. It is not clear that this can actually happen? the partial
20015 -- view cannot be frozen yet, and the predicate function has not been
20016 -- built. Still it is a cheap check and seems safer to make it.
20018 if Has_Predicates (Priv_T) then
20019 if Present (Predicate_Function (Priv_T)) then
20020 Set_Predicate_Function (Full_T, Predicate_Function (Priv_T));
20021 end if;
20023 Set_Has_Predicates (Full_T);
20024 end if;
20025 end Process_Full_View;
20027 -----------------------------------
20028 -- Process_Incomplete_Dependents --
20029 -----------------------------------
20031 procedure Process_Incomplete_Dependents
20032 (N : Node_Id;
20033 Full_T : Entity_Id;
20034 Inc_T : Entity_Id)
20036 Inc_Elmt : Elmt_Id;
20037 Priv_Dep : Entity_Id;
20038 New_Subt : Entity_Id;
20040 Disc_Constraint : Elist_Id;
20042 begin
20043 if No (Private_Dependents (Inc_T)) then
20044 return;
20045 end if;
20047 -- Itypes that may be generated by the completion of an incomplete
20048 -- subtype are not used by the back-end and not attached to the tree.
20049 -- They are created only for constraint-checking purposes.
20051 Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
20052 while Present (Inc_Elmt) loop
20053 Priv_Dep := Node (Inc_Elmt);
20055 if Ekind (Priv_Dep) = E_Subprogram_Type then
20057 -- An Access_To_Subprogram type may have a return type or a
20058 -- parameter type that is incomplete. Replace with the full view.
20060 if Etype (Priv_Dep) = Inc_T then
20061 Set_Etype (Priv_Dep, Full_T);
20062 end if;
20064 declare
20065 Formal : Entity_Id;
20067 begin
20068 Formal := First_Formal (Priv_Dep);
20069 while Present (Formal) loop
20070 if Etype (Formal) = Inc_T then
20071 Set_Etype (Formal, Full_T);
20072 end if;
20074 Next_Formal (Formal);
20075 end loop;
20076 end;
20078 elsif Is_Overloadable (Priv_Dep) then
20080 -- If a subprogram in the incomplete dependents list is primitive
20081 -- for a tagged full type then mark it as a dispatching operation,
20082 -- check whether it overrides an inherited subprogram, and check
20083 -- restrictions on its controlling formals. Note that a protected
20084 -- operation is never dispatching: only its wrapper operation
20085 -- (which has convention Ada) is.
20087 if Is_Tagged_Type (Full_T)
20088 and then Is_Primitive (Priv_Dep)
20089 and then Convention (Priv_Dep) /= Convention_Protected
20090 then
20091 Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
20092 Set_Is_Dispatching_Operation (Priv_Dep);
20093 Check_Controlling_Formals (Full_T, Priv_Dep);
20094 end if;
20096 elsif Ekind (Priv_Dep) = E_Subprogram_Body then
20098 -- Can happen during processing of a body before the completion
20099 -- of a TA type. Ignore, because spec is also on dependent list.
20101 return;
20103 -- Ada 2005 (AI-412): Transform a regular incomplete subtype into a
20104 -- corresponding subtype of the full view.
20106 elsif Ekind (Priv_Dep) = E_Incomplete_Subtype then
20107 Set_Subtype_Indication
20108 (Parent (Priv_Dep), New_Occurrence_Of (Full_T, Sloc (Priv_Dep)));
20109 Set_Etype (Priv_Dep, Full_T);
20110 Set_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
20111 Set_Analyzed (Parent (Priv_Dep), False);
20113 -- Reanalyze the declaration, suppressing the call to
20114 -- Enter_Name to avoid duplicate names.
20116 Analyze_Subtype_Declaration
20117 (N => Parent (Priv_Dep),
20118 Skip => True);
20120 -- Dependent is a subtype
20122 else
20123 -- We build a new subtype indication using the full view of the
20124 -- incomplete parent. The discriminant constraints have been
20125 -- elaborated already at the point of the subtype declaration.
20127 New_Subt := Create_Itype (E_Void, N);
20129 if Has_Discriminants (Full_T) then
20130 Disc_Constraint := Discriminant_Constraint (Priv_Dep);
20131 else
20132 Disc_Constraint := No_Elist;
20133 end if;
20135 Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
20136 Set_Full_View (Priv_Dep, New_Subt);
20137 end if;
20139 Next_Elmt (Inc_Elmt);
20140 end loop;
20141 end Process_Incomplete_Dependents;
20143 --------------------------------
20144 -- Process_Range_Expr_In_Decl --
20145 --------------------------------
20147 procedure Process_Range_Expr_In_Decl
20148 (R : Node_Id;
20149 T : Entity_Id;
20150 Subtyp : Entity_Id := Empty;
20151 Check_List : List_Id := Empty_List;
20152 R_Check_Off : Boolean := False;
20153 In_Iter_Schm : Boolean := False)
20155 Lo, Hi : Node_Id;
20156 R_Checks : Check_Result;
20157 Insert_Node : Node_Id;
20158 Def_Id : Entity_Id;
20160 begin
20161 Analyze_And_Resolve (R, Base_Type (T));
20163 if Nkind (R) = N_Range then
20165 -- In SPARK, all ranges should be static, with the exception of the
20166 -- discrete type definition of a loop parameter specification.
20168 if not In_Iter_Schm
20169 and then not Is_OK_Static_Range (R)
20170 then
20171 Check_SPARK_05_Restriction ("range should be static", R);
20172 end if;
20174 Lo := Low_Bound (R);
20175 Hi := High_Bound (R);
20177 -- Validity checks on the range of a quantified expression are
20178 -- delayed until the construct is transformed into a loop.
20180 if Nkind (Parent (R)) = N_Loop_Parameter_Specification
20181 and then Nkind (Parent (Parent (R))) = N_Quantified_Expression
20182 then
20183 null;
20185 -- We need to ensure validity of the bounds here, because if we
20186 -- go ahead and do the expansion, then the expanded code will get
20187 -- analyzed with range checks suppressed and we miss the check.
20189 -- WARNING: The capture of the range bounds with xxx_FIRST/_LAST and
20190 -- the temporaries generated by routine Remove_Side_Effects by means
20191 -- of validity checks must use the same names. When a range appears
20192 -- in the parent of a generic, the range is processed with checks
20193 -- disabled as part of the generic context and with checks enabled
20194 -- for code generation purposes. This leads to link issues as the
20195 -- generic contains references to xxx_FIRST/_LAST, but the inlined
20196 -- template sees the temporaries generated by Remove_Side_Effects.
20198 else
20199 Validity_Check_Range (R, Subtyp);
20200 end if;
20202 -- If there were errors in the declaration, try and patch up some
20203 -- common mistakes in the bounds. The cases handled are literals
20204 -- which are Integer where the expected type is Real and vice versa.
20205 -- These corrections allow the compilation process to proceed further
20206 -- along since some basic assumptions of the format of the bounds
20207 -- are guaranteed.
20209 if Etype (R) = Any_Type then
20210 if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
20211 Rewrite (Lo,
20212 Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
20214 elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
20215 Rewrite (Hi,
20216 Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
20218 elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
20219 Rewrite (Lo,
20220 Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
20222 elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
20223 Rewrite (Hi,
20224 Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
20225 end if;
20227 Set_Etype (Lo, T);
20228 Set_Etype (Hi, T);
20229 end if;
20231 -- If the bounds of the range have been mistakenly given as string
20232 -- literals (perhaps in place of character literals), then an error
20233 -- has already been reported, but we rewrite the string literal as a
20234 -- bound of the range's type to avoid blowups in later processing
20235 -- that looks at static values.
20237 if Nkind (Lo) = N_String_Literal then
20238 Rewrite (Lo,
20239 Make_Attribute_Reference (Sloc (Lo),
20240 Prefix => New_Occurrence_Of (T, Sloc (Lo)),
20241 Attribute_Name => Name_First));
20242 Analyze_And_Resolve (Lo);
20243 end if;
20245 if Nkind (Hi) = N_String_Literal then
20246 Rewrite (Hi,
20247 Make_Attribute_Reference (Sloc (Hi),
20248 Prefix => New_Occurrence_Of (T, Sloc (Hi)),
20249 Attribute_Name => Name_First));
20250 Analyze_And_Resolve (Hi);
20251 end if;
20253 -- If bounds aren't scalar at this point then exit, avoiding
20254 -- problems with further processing of the range in this procedure.
20256 if not Is_Scalar_Type (Etype (Lo)) then
20257 return;
20258 end if;
20260 -- Resolve (actually Sem_Eval) has checked that the bounds are in
20261 -- then range of the base type. Here we check whether the bounds
20262 -- are in the range of the subtype itself. Note that if the bounds
20263 -- represent the null range the Constraint_Error exception should
20264 -- not be raised.
20266 -- ??? The following code should be cleaned up as follows
20268 -- 1. The Is_Null_Range (Lo, Hi) test should disappear since it
20269 -- is done in the call to Range_Check (R, T); below
20271 -- 2. The use of R_Check_Off should be investigated and possibly
20272 -- removed, this would clean up things a bit.
20274 if Is_Null_Range (Lo, Hi) then
20275 null;
20277 else
20278 -- Capture values of bounds and generate temporaries for them
20279 -- if needed, before applying checks, since checks may cause
20280 -- duplication of the expression without forcing evaluation.
20282 -- The forced evaluation removes side effects from expressions,
20283 -- which should occur also in GNATprove mode. Otherwise, we end up
20284 -- with unexpected insertions of actions at places where this is
20285 -- not supposed to occur, e.g. on default parameters of a call.
20287 if Expander_Active or GNATprove_Mode then
20289 -- Call Force_Evaluation to create declarations as needed to
20290 -- deal with side effects, and also create typ_FIRST/LAST
20291 -- entities for bounds if we have a subtype name.
20293 -- Note: we do this transformation even if expansion is not
20294 -- active if we are in GNATprove_Mode since the transformation
20295 -- is in general required to ensure that the resulting tree has
20296 -- proper Ada semantics.
20298 Force_Evaluation
20299 (Lo, Related_Id => Subtyp, Is_Low_Bound => True);
20300 Force_Evaluation
20301 (Hi, Related_Id => Subtyp, Is_High_Bound => True);
20302 end if;
20304 -- We use a flag here instead of suppressing checks on the type
20305 -- because the type we check against isn't necessarily the place
20306 -- where we put the check.
20308 if not R_Check_Off then
20309 R_Checks := Get_Range_Checks (R, T);
20311 -- Look up tree to find an appropriate insertion point. We
20312 -- can't just use insert_actions because later processing
20313 -- depends on the insertion node. Prior to Ada 2012 the
20314 -- insertion point could only be a declaration or a loop, but
20315 -- quantified expressions can appear within any context in an
20316 -- expression, and the insertion point can be any statement,
20317 -- pragma, or declaration.
20319 Insert_Node := Parent (R);
20320 while Present (Insert_Node) loop
20321 exit when
20322 Nkind (Insert_Node) in N_Declaration
20323 and then
20324 not Nkind_In
20325 (Insert_Node, N_Component_Declaration,
20326 N_Loop_Parameter_Specification,
20327 N_Function_Specification,
20328 N_Procedure_Specification);
20330 exit when Nkind (Insert_Node) in N_Later_Decl_Item
20331 or else Nkind (Insert_Node) in
20332 N_Statement_Other_Than_Procedure_Call
20333 or else Nkind_In (Insert_Node, N_Procedure_Call_Statement,
20334 N_Pragma);
20336 Insert_Node := Parent (Insert_Node);
20337 end loop;
20339 -- Why would Type_Decl not be present??? Without this test,
20340 -- short regression tests fail.
20342 if Present (Insert_Node) then
20344 -- Case of loop statement. Verify that the range is part
20345 -- of the subtype indication of the iteration scheme.
20347 if Nkind (Insert_Node) = N_Loop_Statement then
20348 declare
20349 Indic : Node_Id;
20351 begin
20352 Indic := Parent (R);
20353 while Present (Indic)
20354 and then Nkind (Indic) /= N_Subtype_Indication
20355 loop
20356 Indic := Parent (Indic);
20357 end loop;
20359 if Present (Indic) then
20360 Def_Id := Etype (Subtype_Mark (Indic));
20362 Insert_Range_Checks
20363 (R_Checks,
20364 Insert_Node,
20365 Def_Id,
20366 Sloc (Insert_Node),
20368 Do_Before => True);
20369 end if;
20370 end;
20372 -- Insertion before a declaration. If the declaration
20373 -- includes discriminants, the list of applicable checks
20374 -- is given by the caller.
20376 elsif Nkind (Insert_Node) in N_Declaration then
20377 Def_Id := Defining_Identifier (Insert_Node);
20379 if (Ekind (Def_Id) = E_Record_Type
20380 and then Depends_On_Discriminant (R))
20381 or else
20382 (Ekind (Def_Id) = E_Protected_Type
20383 and then Has_Discriminants (Def_Id))
20384 then
20385 Append_Range_Checks
20386 (R_Checks,
20387 Check_List, Def_Id, Sloc (Insert_Node), R);
20389 else
20390 Insert_Range_Checks
20391 (R_Checks,
20392 Insert_Node, Def_Id, Sloc (Insert_Node), R);
20394 end if;
20396 -- Insertion before a statement. Range appears in the
20397 -- context of a quantified expression. Insertion will
20398 -- take place when expression is expanded.
20400 else
20401 null;
20402 end if;
20403 end if;
20404 end if;
20405 end if;
20407 -- Case of other than an explicit N_Range node
20409 -- The forced evaluation removes side effects from expressions, which
20410 -- should occur also in GNATprove mode. Otherwise, we end up with
20411 -- unexpected insertions of actions at places where this is not
20412 -- supposed to occur, e.g. on default parameters of a call.
20414 elsif Expander_Active or GNATprove_Mode then
20415 Get_Index_Bounds (R, Lo, Hi);
20416 Force_Evaluation (Lo);
20417 Force_Evaluation (Hi);
20418 end if;
20419 end Process_Range_Expr_In_Decl;
20421 --------------------------------------
20422 -- Process_Real_Range_Specification --
20423 --------------------------------------
20425 procedure Process_Real_Range_Specification (Def : Node_Id) is
20426 Spec : constant Node_Id := Real_Range_Specification (Def);
20427 Lo : Node_Id;
20428 Hi : Node_Id;
20429 Err : Boolean := False;
20431 procedure Analyze_Bound (N : Node_Id);
20432 -- Analyze and check one bound
20434 -------------------
20435 -- Analyze_Bound --
20436 -------------------
20438 procedure Analyze_Bound (N : Node_Id) is
20439 begin
20440 Analyze_And_Resolve (N, Any_Real);
20442 if not Is_OK_Static_Expression (N) then
20443 Flag_Non_Static_Expr
20444 ("bound in real type definition is not static!", N);
20445 Err := True;
20446 end if;
20447 end Analyze_Bound;
20449 -- Start of processing for Process_Real_Range_Specification
20451 begin
20452 if Present (Spec) then
20453 Lo := Low_Bound (Spec);
20454 Hi := High_Bound (Spec);
20455 Analyze_Bound (Lo);
20456 Analyze_Bound (Hi);
20458 -- If error, clear away junk range specification
20460 if Err then
20461 Set_Real_Range_Specification (Def, Empty);
20462 end if;
20463 end if;
20464 end Process_Real_Range_Specification;
20466 ---------------------
20467 -- Process_Subtype --
20468 ---------------------
20470 function Process_Subtype
20471 (S : Node_Id;
20472 Related_Nod : Node_Id;
20473 Related_Id : Entity_Id := Empty;
20474 Suffix : Character := ' ') return Entity_Id
20476 P : Node_Id;
20477 Def_Id : Entity_Id;
20478 Error_Node : Node_Id;
20479 Full_View_Id : Entity_Id;
20480 Subtype_Mark_Id : Entity_Id;
20482 May_Have_Null_Exclusion : Boolean;
20484 procedure Check_Incomplete (T : Entity_Id);
20485 -- Called to verify that an incomplete type is not used prematurely
20487 ----------------------
20488 -- Check_Incomplete --
20489 ----------------------
20491 procedure Check_Incomplete (T : Entity_Id) is
20492 begin
20493 -- Ada 2005 (AI-412): Incomplete subtypes are legal
20495 if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
20496 and then
20497 not (Ada_Version >= Ada_2005
20498 and then
20499 (Nkind (Parent (T)) = N_Subtype_Declaration
20500 or else (Nkind (Parent (T)) = N_Subtype_Indication
20501 and then Nkind (Parent (Parent (T))) =
20502 N_Subtype_Declaration)))
20503 then
20504 Error_Msg_N ("invalid use of type before its full declaration", T);
20505 end if;
20506 end Check_Incomplete;
20508 -- Start of processing for Process_Subtype
20510 begin
20511 -- Case of no constraints present
20513 if Nkind (S) /= N_Subtype_Indication then
20514 Find_Type (S);
20515 Check_Incomplete (S);
20516 P := Parent (S);
20518 -- Ada 2005 (AI-231): Static check
20520 if Ada_Version >= Ada_2005
20521 and then Present (P)
20522 and then Null_Exclusion_Present (P)
20523 and then Nkind (P) /= N_Access_To_Object_Definition
20524 and then not Is_Access_Type (Entity (S))
20525 then
20526 Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
20527 end if;
20529 -- The following is ugly, can't we have a range or even a flag???
20531 May_Have_Null_Exclusion :=
20532 Nkind_In (P, N_Access_Definition,
20533 N_Access_Function_Definition,
20534 N_Access_Procedure_Definition,
20535 N_Access_To_Object_Definition,
20536 N_Allocator,
20537 N_Component_Definition)
20538 or else
20539 Nkind_In (P, N_Derived_Type_Definition,
20540 N_Discriminant_Specification,
20541 N_Formal_Object_Declaration,
20542 N_Object_Declaration,
20543 N_Object_Renaming_Declaration,
20544 N_Parameter_Specification,
20545 N_Subtype_Declaration);
20547 -- Create an Itype that is a duplicate of Entity (S) but with the
20548 -- null-exclusion attribute.
20550 if May_Have_Null_Exclusion
20551 and then Is_Access_Type (Entity (S))
20552 and then Null_Exclusion_Present (P)
20554 -- No need to check the case of an access to object definition.
20555 -- It is correct to define double not-null pointers.
20557 -- Example:
20558 -- type Not_Null_Int_Ptr is not null access Integer;
20559 -- type Acc is not null access Not_Null_Int_Ptr;
20561 and then Nkind (P) /= N_Access_To_Object_Definition
20562 then
20563 if Can_Never_Be_Null (Entity (S)) then
20564 case Nkind (Related_Nod) is
20565 when N_Full_Type_Declaration =>
20566 if Nkind (Type_Definition (Related_Nod))
20567 in N_Array_Type_Definition
20568 then
20569 Error_Node :=
20570 Subtype_Indication
20571 (Component_Definition
20572 (Type_Definition (Related_Nod)));
20573 else
20574 Error_Node :=
20575 Subtype_Indication (Type_Definition (Related_Nod));
20576 end if;
20578 when N_Subtype_Declaration =>
20579 Error_Node := Subtype_Indication (Related_Nod);
20581 when N_Object_Declaration =>
20582 Error_Node := Object_Definition (Related_Nod);
20584 when N_Component_Declaration =>
20585 Error_Node :=
20586 Subtype_Indication (Component_Definition (Related_Nod));
20588 when N_Allocator =>
20589 Error_Node := Expression (Related_Nod);
20591 when others =>
20592 pragma Assert (False);
20593 Error_Node := Related_Nod;
20594 end case;
20596 Error_Msg_NE
20597 ("`NOT NULL` not allowed (& already excludes null)",
20598 Error_Node,
20599 Entity (S));
20600 end if;
20602 Set_Etype (S,
20603 Create_Null_Excluding_Itype
20604 (T => Entity (S),
20605 Related_Nod => P));
20606 Set_Entity (S, Etype (S));
20607 end if;
20609 return Entity (S);
20611 -- Case of constraint present, so that we have an N_Subtype_Indication
20612 -- node (this node is created only if constraints are present).
20614 else
20615 Find_Type (Subtype_Mark (S));
20617 if Nkind (Parent (S)) /= N_Access_To_Object_Definition
20618 and then not
20619 (Nkind (Parent (S)) = N_Subtype_Declaration
20620 and then Is_Itype (Defining_Identifier (Parent (S))))
20621 then
20622 Check_Incomplete (Subtype_Mark (S));
20623 end if;
20625 P := Parent (S);
20626 Subtype_Mark_Id := Entity (Subtype_Mark (S));
20628 -- Explicit subtype declaration case
20630 if Nkind (P) = N_Subtype_Declaration then
20631 Def_Id := Defining_Identifier (P);
20633 -- Explicit derived type definition case
20635 elsif Nkind (P) = N_Derived_Type_Definition then
20636 Def_Id := Defining_Identifier (Parent (P));
20638 -- Implicit case, the Def_Id must be created as an implicit type.
20639 -- The one exception arises in the case of concurrent types, array
20640 -- and access types, where other subsidiary implicit types may be
20641 -- created and must appear before the main implicit type. In these
20642 -- cases we leave Def_Id set to Empty as a signal that Create_Itype
20643 -- has not yet been called to create Def_Id.
20645 else
20646 if Is_Array_Type (Subtype_Mark_Id)
20647 or else Is_Concurrent_Type (Subtype_Mark_Id)
20648 or else Is_Access_Type (Subtype_Mark_Id)
20649 then
20650 Def_Id := Empty;
20652 -- For the other cases, we create a new unattached Itype,
20653 -- and set the indication to ensure it gets attached later.
20655 else
20656 Def_Id :=
20657 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
20658 end if;
20659 end if;
20661 -- If the kind of constraint is invalid for this kind of type,
20662 -- then give an error, and then pretend no constraint was given.
20664 if not Is_Valid_Constraint_Kind
20665 (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
20666 then
20667 Error_Msg_N
20668 ("incorrect constraint for this kind of type", Constraint (S));
20670 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
20672 -- Set Ekind of orphan itype, to prevent cascaded errors
20674 if Present (Def_Id) then
20675 Set_Ekind (Def_Id, Ekind (Any_Type));
20676 end if;
20678 -- Make recursive call, having got rid of the bogus constraint
20680 return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
20681 end if;
20683 -- Remaining processing depends on type. Select on Base_Type kind to
20684 -- ensure getting to the concrete type kind in the case of a private
20685 -- subtype (needed when only doing semantic analysis).
20687 case Ekind (Base_Type (Subtype_Mark_Id)) is
20688 when Access_Kind =>
20690 -- If this is a constraint on a class-wide type, discard it.
20691 -- There is currently no way to express a partial discriminant
20692 -- constraint on a type with unknown discriminants. This is
20693 -- a pathology that the ACATS wisely decides not to test.
20695 if Is_Class_Wide_Type (Designated_Type (Subtype_Mark_Id)) then
20696 if Comes_From_Source (S) then
20697 Error_Msg_N
20698 ("constraint on class-wide type ignored??",
20699 Constraint (S));
20700 end if;
20702 if Nkind (P) = N_Subtype_Declaration then
20703 Set_Subtype_Indication (P,
20704 New_Occurrence_Of (Subtype_Mark_Id, Sloc (S)));
20705 end if;
20707 return Subtype_Mark_Id;
20708 end if;
20710 Constrain_Access (Def_Id, S, Related_Nod);
20712 if Expander_Active
20713 and then Is_Itype (Designated_Type (Def_Id))
20714 and then Nkind (Related_Nod) = N_Subtype_Declaration
20715 and then not Is_Incomplete_Type (Designated_Type (Def_Id))
20716 then
20717 Build_Itype_Reference
20718 (Designated_Type (Def_Id), Related_Nod);
20719 end if;
20721 when Array_Kind =>
20722 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
20724 when Decimal_Fixed_Point_Kind =>
20725 Constrain_Decimal (Def_Id, S);
20727 when Enumeration_Kind =>
20728 Constrain_Enumeration (Def_Id, S);
20729 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
20731 when Ordinary_Fixed_Point_Kind =>
20732 Constrain_Ordinary_Fixed (Def_Id, S);
20734 when Float_Kind =>
20735 Constrain_Float (Def_Id, S);
20737 when Integer_Kind =>
20738 Constrain_Integer (Def_Id, S);
20739 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
20741 when E_Record_Type |
20742 E_Record_Subtype |
20743 Class_Wide_Kind |
20744 E_Incomplete_Type =>
20745 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
20747 if Ekind (Def_Id) = E_Incomplete_Type then
20748 Set_Private_Dependents (Def_Id, New_Elmt_List);
20749 end if;
20751 when Private_Kind =>
20752 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
20753 Set_Private_Dependents (Def_Id, New_Elmt_List);
20755 -- In case of an invalid constraint prevent further processing
20756 -- since the type constructed is missing expected fields.
20758 if Etype (Def_Id) = Any_Type then
20759 return Def_Id;
20760 end if;
20762 -- If the full view is that of a task with discriminants,
20763 -- we must constrain both the concurrent type and its
20764 -- corresponding record type. Otherwise we will just propagate
20765 -- the constraint to the full view, if available.
20767 if Present (Full_View (Subtype_Mark_Id))
20768 and then Has_Discriminants (Subtype_Mark_Id)
20769 and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
20770 then
20771 Full_View_Id :=
20772 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
20774 Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
20775 Constrain_Concurrent (Full_View_Id, S,
20776 Related_Nod, Related_Id, Suffix);
20777 Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
20778 Set_Full_View (Def_Id, Full_View_Id);
20780 -- Introduce an explicit reference to the private subtype,
20781 -- to prevent scope anomalies in gigi if first use appears
20782 -- in a nested context, e.g. a later function body.
20783 -- Should this be generated in other contexts than a full
20784 -- type declaration?
20786 if Is_Itype (Def_Id)
20787 and then
20788 Nkind (Parent (P)) = N_Full_Type_Declaration
20789 then
20790 Build_Itype_Reference (Def_Id, Parent (P));
20791 end if;
20793 else
20794 Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
20795 end if;
20797 when Concurrent_Kind =>
20798 Constrain_Concurrent (Def_Id, S,
20799 Related_Nod, Related_Id, Suffix);
20801 when others =>
20802 Error_Msg_N ("invalid subtype mark in subtype indication", S);
20803 end case;
20805 -- Size and Convention are always inherited from the base type
20807 Set_Size_Info (Def_Id, (Subtype_Mark_Id));
20808 Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
20810 return Def_Id;
20811 end if;
20812 end Process_Subtype;
20814 --------------------------------------------
20815 -- Propagate_Default_Init_Cond_Attributes --
20816 --------------------------------------------
20818 procedure Propagate_Default_Init_Cond_Attributes
20819 (From_Typ : Entity_Id;
20820 To_Typ : Entity_Id;
20821 Parent_To_Derivation : Boolean := False;
20822 Private_To_Full_View : Boolean := False)
20824 procedure Remove_Default_Init_Cond_Procedure (Typ : Entity_Id);
20825 -- Remove the default initial procedure (if any) from the rep chain of
20826 -- type Typ.
20828 ----------------------------------------
20829 -- Remove_Default_Init_Cond_Procedure --
20830 ----------------------------------------
20832 procedure Remove_Default_Init_Cond_Procedure (Typ : Entity_Id) is
20833 Found : Boolean := False;
20834 Prev : Entity_Id;
20835 Subp : Entity_Id;
20837 begin
20838 Prev := Typ;
20839 Subp := Subprograms_For_Type (Typ);
20840 while Present (Subp) loop
20841 if Is_Default_Init_Cond_Procedure (Subp) then
20842 Found := True;
20843 exit;
20844 end if;
20846 Prev := Subp;
20847 Subp := Subprograms_For_Type (Subp);
20848 end loop;
20850 if Found then
20851 Set_Subprograms_For_Type (Prev, Subprograms_For_Type (Subp));
20852 Set_Subprograms_For_Type (Subp, Empty);
20853 end if;
20854 end Remove_Default_Init_Cond_Procedure;
20856 -- Local variables
20858 Inherit_Procedure : Boolean := False;
20860 -- Start of processing for Propagate_Default_Init_Cond_Attributes
20862 begin
20863 if Has_Default_Init_Cond (From_Typ) then
20865 -- A derived type inherits the attributes from its parent type
20867 if Parent_To_Derivation then
20868 Set_Has_Inherited_Default_Init_Cond (To_Typ);
20870 -- A full view shares the attributes with its private view
20872 else
20873 Set_Has_Default_Init_Cond (To_Typ);
20874 end if;
20876 Inherit_Procedure := True;
20878 -- Due to the order of expansion, a derived private type is processed
20879 -- by two routines which both attempt to set the attributes related
20880 -- to pragma Default_Initial_Condition - Build_Derived_Type and then
20881 -- Process_Full_View.
20883 -- package Pack is
20884 -- type Parent_Typ is private
20885 -- with Default_Initial_Condition ...;
20886 -- private
20887 -- type Parent_Typ is ...;
20888 -- end Pack;
20890 -- with Pack; use Pack;
20891 -- package Pack_2 is
20892 -- type Deriv_Typ is private
20893 -- with Default_Initial_Condition ...;
20894 -- private
20895 -- type Deriv_Typ is new Parent_Typ;
20896 -- end Pack_2;
20898 -- When Build_Derived_Type operates, it sets the attributes on the
20899 -- full view without taking into account that the private view may
20900 -- define its own default initial condition procedure. This becomes
20901 -- apparent in Process_Full_View which must undo some of the work by
20902 -- Build_Derived_Type and propagate the attributes from the private
20903 -- to the full view.
20905 if Private_To_Full_View then
20906 Set_Has_Inherited_Default_Init_Cond (To_Typ, False);
20907 Remove_Default_Init_Cond_Procedure (To_Typ);
20908 end if;
20910 -- A type must inherit the default initial condition procedure from a
20911 -- parent type when the parent itself is inheriting the procedure or
20912 -- when it is defining one. This circuitry is also used when dealing
20913 -- with the private / full view of a type.
20915 elsif Has_Inherited_Default_Init_Cond (From_Typ)
20916 or (Parent_To_Derivation
20917 and Present (Get_Pragma
20918 (From_Typ, Pragma_Default_Initial_Condition)))
20919 then
20920 Set_Has_Inherited_Default_Init_Cond (To_Typ);
20921 Inherit_Procedure := True;
20922 end if;
20924 if Inherit_Procedure
20925 and then No (Default_Init_Cond_Procedure (To_Typ))
20926 then
20927 Set_Default_Init_Cond_Procedure
20928 (To_Typ, Default_Init_Cond_Procedure (From_Typ));
20929 end if;
20930 end Propagate_Default_Init_Cond_Attributes;
20932 -----------------------------
20933 -- Record_Type_Declaration --
20934 -----------------------------
20936 procedure Record_Type_Declaration
20937 (T : Entity_Id;
20938 N : Node_Id;
20939 Prev : Entity_Id)
20941 Def : constant Node_Id := Type_Definition (N);
20942 Is_Tagged : Boolean;
20943 Tag_Comp : Entity_Id;
20945 begin
20946 -- These flags must be initialized before calling Process_Discriminants
20947 -- because this routine makes use of them.
20949 Set_Ekind (T, E_Record_Type);
20950 Set_Etype (T, T);
20951 Init_Size_Align (T);
20952 Set_Interfaces (T, No_Elist);
20953 Set_Stored_Constraint (T, No_Elist);
20954 Set_Default_SSO (T);
20956 -- Normal case
20958 if Ada_Version < Ada_2005 or else not Interface_Present (Def) then
20959 if Limited_Present (Def) then
20960 Check_SPARK_05_Restriction ("limited is not allowed", N);
20961 end if;
20963 if Abstract_Present (Def) then
20964 Check_SPARK_05_Restriction ("abstract is not allowed", N);
20965 end if;
20967 -- The flag Is_Tagged_Type might have already been set by
20968 -- Find_Type_Name if it detected an error for declaration T. This
20969 -- arises in the case of private tagged types where the full view
20970 -- omits the word tagged.
20972 Is_Tagged :=
20973 Tagged_Present (Def)
20974 or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
20976 Set_Is_Limited_Record (T, Limited_Present (Def));
20978 if Is_Tagged then
20979 Set_Is_Tagged_Type (T, True);
20980 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
20981 end if;
20983 -- Type is abstract if full declaration carries keyword, or if
20984 -- previous partial view did.
20986 Set_Is_Abstract_Type (T, Is_Abstract_Type (T)
20987 or else Abstract_Present (Def));
20989 else
20990 Check_SPARK_05_Restriction ("interface is not allowed", N);
20992 Is_Tagged := True;
20993 Analyze_Interface_Declaration (T, Def);
20995 if Present (Discriminant_Specifications (N)) then
20996 Error_Msg_N
20997 ("interface types cannot have discriminants",
20998 Defining_Identifier
20999 (First (Discriminant_Specifications (N))));
21000 end if;
21001 end if;
21003 -- First pass: if there are self-referential access components,
21004 -- create the required anonymous access type declarations, and if
21005 -- need be an incomplete type declaration for T itself.
21007 Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
21009 if Ada_Version >= Ada_2005
21010 and then Present (Interface_List (Def))
21011 then
21012 Check_Interfaces (N, Def);
21014 declare
21015 Ifaces_List : Elist_Id;
21017 begin
21018 -- Ada 2005 (AI-251): Collect the list of progenitors that are not
21019 -- already in the parents.
21021 Collect_Interfaces
21022 (T => T,
21023 Ifaces_List => Ifaces_List,
21024 Exclude_Parents => True);
21026 Set_Interfaces (T, Ifaces_List);
21027 end;
21028 end if;
21030 -- Records constitute a scope for the component declarations within.
21031 -- The scope is created prior to the processing of these declarations.
21032 -- Discriminants are processed first, so that they are visible when
21033 -- processing the other components. The Ekind of the record type itself
21034 -- is set to E_Record_Type (subtypes appear as E_Record_Subtype).
21036 -- Enter record scope
21038 Push_Scope (T);
21040 -- If an incomplete or private type declaration was already given for
21041 -- the type, then this scope already exists, and the discriminants have
21042 -- been declared within. We must verify that the full declaration
21043 -- matches the incomplete one.
21045 Check_Or_Process_Discriminants (N, T, Prev);
21047 Set_Is_Constrained (T, not Has_Discriminants (T));
21048 Set_Has_Delayed_Freeze (T, True);
21050 -- For tagged types add a manually analyzed component corresponding
21051 -- to the component _tag, the corresponding piece of tree will be
21052 -- expanded as part of the freezing actions if it is not a CPP_Class.
21054 if Is_Tagged then
21056 -- Do not add the tag unless we are in expansion mode
21058 if Expander_Active then
21059 Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
21060 Enter_Name (Tag_Comp);
21062 Set_Ekind (Tag_Comp, E_Component);
21063 Set_Is_Tag (Tag_Comp);
21064 Set_Is_Aliased (Tag_Comp);
21065 Set_Etype (Tag_Comp, RTE (RE_Tag));
21066 Set_DT_Entry_Count (Tag_Comp, No_Uint);
21067 Set_Original_Record_Component (Tag_Comp, Tag_Comp);
21068 Init_Component_Location (Tag_Comp);
21070 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
21071 -- implemented interfaces.
21073 if Has_Interfaces (T) then
21074 Add_Interface_Tag_Components (N, T);
21075 end if;
21076 end if;
21078 Make_Class_Wide_Type (T);
21079 Set_Direct_Primitive_Operations (T, New_Elmt_List);
21080 end if;
21082 -- We must suppress range checks when processing record components in
21083 -- the presence of discriminants, since we don't want spurious checks to
21084 -- be generated during their analysis, but Suppress_Range_Checks flags
21085 -- must be reset the after processing the record definition.
21087 -- Note: this is the only use of Kill_Range_Checks, and is a bit odd,
21088 -- couldn't we just use the normal range check suppression method here.
21089 -- That would seem cleaner ???
21091 if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
21092 Set_Kill_Range_Checks (T, True);
21093 Record_Type_Definition (Def, Prev);
21094 Set_Kill_Range_Checks (T, False);
21095 else
21096 Record_Type_Definition (Def, Prev);
21097 end if;
21099 -- Exit from record scope
21101 End_Scope;
21103 -- Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
21104 -- the implemented interfaces and associate them an aliased entity.
21106 if Is_Tagged
21107 and then not Is_Empty_List (Interface_List (Def))
21108 then
21109 Derive_Progenitor_Subprograms (T, T);
21110 end if;
21112 Check_Function_Writable_Actuals (N);
21113 end Record_Type_Declaration;
21115 ----------------------------
21116 -- Record_Type_Definition --
21117 ----------------------------
21119 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
21120 Component : Entity_Id;
21121 Ctrl_Components : Boolean := False;
21122 Final_Storage_Only : Boolean;
21123 T : Entity_Id;
21125 begin
21126 if Ekind (Prev_T) = E_Incomplete_Type then
21127 T := Full_View (Prev_T);
21128 else
21129 T := Prev_T;
21130 end if;
21132 -- In SPARK, tagged types and type extensions may only be declared in
21133 -- the specification of library unit packages.
21135 if Present (Def) and then Is_Tagged_Type (T) then
21136 declare
21137 Typ : Node_Id;
21138 Ctxt : Node_Id;
21140 begin
21141 if Nkind (Parent (Def)) = N_Full_Type_Declaration then
21142 Typ := Parent (Def);
21143 else
21144 pragma Assert
21145 (Nkind (Parent (Def)) = N_Derived_Type_Definition);
21146 Typ := Parent (Parent (Def));
21147 end if;
21149 Ctxt := Parent (Typ);
21151 if Nkind (Ctxt) = N_Package_Body
21152 and then Nkind (Parent (Ctxt)) = N_Compilation_Unit
21153 then
21154 Check_SPARK_05_Restriction
21155 ("type should be defined in package specification", Typ);
21157 elsif Nkind (Ctxt) /= N_Package_Specification
21158 or else Nkind (Parent (Parent (Ctxt))) /= N_Compilation_Unit
21159 then
21160 Check_SPARK_05_Restriction
21161 ("type should be defined in library unit package", Typ);
21162 end if;
21163 end;
21164 end if;
21166 Final_Storage_Only := not Is_Controlled (T);
21168 -- Ada 2005: Check whether an explicit Limited is present in a derived
21169 -- type declaration.
21171 if Nkind (Parent (Def)) = N_Derived_Type_Definition
21172 and then Limited_Present (Parent (Def))
21173 then
21174 Set_Is_Limited_Record (T);
21175 end if;
21177 -- If the component list of a record type is defined by the reserved
21178 -- word null and there is no discriminant part, then the record type has
21179 -- no components and all records of the type are null records (RM 3.7)
21180 -- This procedure is also called to process the extension part of a
21181 -- record extension, in which case the current scope may have inherited
21182 -- components.
21184 if No (Def)
21185 or else No (Component_List (Def))
21186 or else Null_Present (Component_List (Def))
21187 then
21188 if not Is_Tagged_Type (T) then
21189 Check_SPARK_05_Restriction ("untagged record cannot be null", Def);
21190 end if;
21192 else
21193 Analyze_Declarations (Component_Items (Component_List (Def)));
21195 if Present (Variant_Part (Component_List (Def))) then
21196 Check_SPARK_05_Restriction ("variant part is not allowed", Def);
21197 Analyze (Variant_Part (Component_List (Def)));
21198 end if;
21199 end if;
21201 -- After completing the semantic analysis of the record definition,
21202 -- record components, both new and inherited, are accessible. Set their
21203 -- kind accordingly. Exclude malformed itypes from illegal declarations,
21204 -- whose Ekind may be void.
21206 Component := First_Entity (Current_Scope);
21207 while Present (Component) loop
21208 if Ekind (Component) = E_Void
21209 and then not Is_Itype (Component)
21210 then
21211 Set_Ekind (Component, E_Component);
21212 Init_Component_Location (Component);
21213 end if;
21215 if Has_Task (Etype (Component)) then
21216 Set_Has_Task (T);
21217 end if;
21219 if Has_Protected (Etype (Component)) then
21220 Set_Has_Protected (T);
21221 end if;
21223 if Ekind (Component) /= E_Component then
21224 null;
21226 -- Do not set Has_Controlled_Component on a class-wide equivalent
21227 -- type. See Make_CW_Equivalent_Type.
21229 elsif not Is_Class_Wide_Equivalent_Type (T)
21230 and then (Has_Controlled_Component (Etype (Component))
21231 or else (Chars (Component) /= Name_uParent
21232 and then Is_Controlled (Etype (Component))))
21233 then
21234 Set_Has_Controlled_Component (T, True);
21235 Final_Storage_Only :=
21236 Final_Storage_Only
21237 and then Finalize_Storage_Only (Etype (Component));
21238 Ctrl_Components := True;
21239 end if;
21241 Next_Entity (Component);
21242 end loop;
21244 -- A Type is Finalize_Storage_Only only if all its controlled components
21245 -- are also.
21247 if Ctrl_Components then
21248 Set_Finalize_Storage_Only (T, Final_Storage_Only);
21249 end if;
21251 -- Place reference to end record on the proper entity, which may
21252 -- be a partial view.
21254 if Present (Def) then
21255 Process_End_Label (Def, 'e', Prev_T);
21256 end if;
21257 end Record_Type_Definition;
21259 ------------------------
21260 -- Replace_Components --
21261 ------------------------
21263 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id) is
21264 function Process (N : Node_Id) return Traverse_Result;
21266 -------------
21267 -- Process --
21268 -------------
21270 function Process (N : Node_Id) return Traverse_Result is
21271 Comp : Entity_Id;
21273 begin
21274 if Nkind (N) = N_Discriminant_Specification then
21275 Comp := First_Discriminant (Typ);
21276 while Present (Comp) loop
21277 if Chars (Comp) = Chars (Defining_Identifier (N)) then
21278 Set_Defining_Identifier (N, Comp);
21279 exit;
21280 end if;
21282 Next_Discriminant (Comp);
21283 end loop;
21285 elsif Nkind (N) = N_Component_Declaration then
21286 Comp := First_Component (Typ);
21287 while Present (Comp) loop
21288 if Chars (Comp) = Chars (Defining_Identifier (N)) then
21289 Set_Defining_Identifier (N, Comp);
21290 exit;
21291 end if;
21293 Next_Component (Comp);
21294 end loop;
21295 end if;
21297 return OK;
21298 end Process;
21300 procedure Replace is new Traverse_Proc (Process);
21302 -- Start of processing for Replace_Components
21304 begin
21305 Replace (Decl);
21306 end Replace_Components;
21308 -------------------------------
21309 -- Set_Completion_Referenced --
21310 -------------------------------
21312 procedure Set_Completion_Referenced (E : Entity_Id) is
21313 begin
21314 -- If in main unit, mark entity that is a completion as referenced,
21315 -- warnings go on the partial view when needed.
21317 if In_Extended_Main_Source_Unit (E) then
21318 Set_Referenced (E);
21319 end if;
21320 end Set_Completion_Referenced;
21322 ---------------------
21323 -- Set_Default_SSO --
21324 ---------------------
21326 procedure Set_Default_SSO (T : Entity_Id) is
21327 begin
21328 case Opt.Default_SSO is
21329 when ' ' =>
21330 null;
21331 when 'L' =>
21332 Set_SSO_Set_Low_By_Default (T, True);
21333 when 'H' =>
21334 Set_SSO_Set_High_By_Default (T, True);
21335 when others =>
21336 raise Program_Error;
21337 end case;
21338 end Set_Default_SSO;
21340 ---------------------
21341 -- Set_Fixed_Range --
21342 ---------------------
21344 -- The range for fixed-point types is complicated by the fact that we
21345 -- do not know the exact end points at the time of the declaration. This
21346 -- is true for three reasons:
21348 -- A size clause may affect the fudging of the end-points.
21349 -- A small clause may affect the values of the end-points.
21350 -- We try to include the end-points if it does not affect the size.
21352 -- This means that the actual end-points must be established at the
21353 -- point when the type is frozen. Meanwhile, we first narrow the range
21354 -- as permitted (so that it will fit if necessary in a small specified
21355 -- size), and then build a range subtree with these narrowed bounds.
21356 -- Set_Fixed_Range constructs the range from real literal values, and
21357 -- sets the range as the Scalar_Range of the given fixed-point type entity.
21359 -- The parent of this range is set to point to the entity so that it is
21360 -- properly hooked into the tree (unlike normal Scalar_Range entries for
21361 -- other scalar types, which are just pointers to the range in the
21362 -- original tree, this would otherwise be an orphan).
21364 -- The tree is left unanalyzed. When the type is frozen, the processing
21365 -- in Freeze.Freeze_Fixed_Point_Type notices that the range is not
21366 -- analyzed, and uses this as an indication that it should complete
21367 -- work on the range (it will know the final small and size values).
21369 procedure Set_Fixed_Range
21370 (E : Entity_Id;
21371 Loc : Source_Ptr;
21372 Lo : Ureal;
21373 Hi : Ureal)
21375 S : constant Node_Id :=
21376 Make_Range (Loc,
21377 Low_Bound => Make_Real_Literal (Loc, Lo),
21378 High_Bound => Make_Real_Literal (Loc, Hi));
21379 begin
21380 Set_Scalar_Range (E, S);
21381 Set_Parent (S, E);
21383 -- Before the freeze point, the bounds of a fixed point are universal
21384 -- and carry the corresponding type.
21386 Set_Etype (Low_Bound (S), Universal_Real);
21387 Set_Etype (High_Bound (S), Universal_Real);
21388 end Set_Fixed_Range;
21390 ----------------------------------
21391 -- Set_Scalar_Range_For_Subtype --
21392 ----------------------------------
21394 procedure Set_Scalar_Range_For_Subtype
21395 (Def_Id : Entity_Id;
21396 R : Node_Id;
21397 Subt : Entity_Id)
21399 Kind : constant Entity_Kind := Ekind (Def_Id);
21401 begin
21402 -- Defend against previous error
21404 if Nkind (R) = N_Error then
21405 return;
21406 end if;
21408 Set_Scalar_Range (Def_Id, R);
21410 -- We need to link the range into the tree before resolving it so
21411 -- that types that are referenced, including importantly the subtype
21412 -- itself, are properly frozen (Freeze_Expression requires that the
21413 -- expression be properly linked into the tree). Of course if it is
21414 -- already linked in, then we do not disturb the current link.
21416 if No (Parent (R)) then
21417 Set_Parent (R, Def_Id);
21418 end if;
21420 -- Reset the kind of the subtype during analysis of the range, to
21421 -- catch possible premature use in the bounds themselves.
21423 Set_Ekind (Def_Id, E_Void);
21424 Process_Range_Expr_In_Decl (R, Subt, Subtyp => Def_Id);
21425 Set_Ekind (Def_Id, Kind);
21426 end Set_Scalar_Range_For_Subtype;
21428 --------------------------------------------------------
21429 -- Set_Stored_Constraint_From_Discriminant_Constraint --
21430 --------------------------------------------------------
21432 procedure Set_Stored_Constraint_From_Discriminant_Constraint
21433 (E : Entity_Id)
21435 begin
21436 -- Make sure set if encountered during Expand_To_Stored_Constraint
21438 Set_Stored_Constraint (E, No_Elist);
21440 -- Give it the right value
21442 if Is_Constrained (E) and then Has_Discriminants (E) then
21443 Set_Stored_Constraint (E,
21444 Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
21445 end if;
21446 end Set_Stored_Constraint_From_Discriminant_Constraint;
21448 -------------------------------------
21449 -- Signed_Integer_Type_Declaration --
21450 -------------------------------------
21452 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
21453 Implicit_Base : Entity_Id;
21454 Base_Typ : Entity_Id;
21455 Lo_Val : Uint;
21456 Hi_Val : Uint;
21457 Errs : Boolean := False;
21458 Lo : Node_Id;
21459 Hi : Node_Id;
21461 function Can_Derive_From (E : Entity_Id) return Boolean;
21462 -- Determine whether given bounds allow derivation from specified type
21464 procedure Check_Bound (Expr : Node_Id);
21465 -- Check bound to make sure it is integral and static. If not, post
21466 -- appropriate error message and set Errs flag
21468 ---------------------
21469 -- Can_Derive_From --
21470 ---------------------
21472 -- Note we check both bounds against both end values, to deal with
21473 -- strange types like ones with a range of 0 .. -12341234.
21475 function Can_Derive_From (E : Entity_Id) return Boolean is
21476 Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
21477 Hi : constant Uint := Expr_Value (Type_High_Bound (E));
21478 begin
21479 return Lo <= Lo_Val and then Lo_Val <= Hi
21480 and then
21481 Lo <= Hi_Val and then Hi_Val <= Hi;
21482 end Can_Derive_From;
21484 -----------------
21485 -- Check_Bound --
21486 -----------------
21488 procedure Check_Bound (Expr : Node_Id) is
21489 begin
21490 -- If a range constraint is used as an integer type definition, each
21491 -- bound of the range must be defined by a static expression of some
21492 -- integer type, but the two bounds need not have the same integer
21493 -- type (Negative bounds are allowed.) (RM 3.5.4)
21495 if not Is_Integer_Type (Etype (Expr)) then
21496 Error_Msg_N
21497 ("integer type definition bounds must be of integer type", Expr);
21498 Errs := True;
21500 elsif not Is_OK_Static_Expression (Expr) then
21501 Flag_Non_Static_Expr
21502 ("non-static expression used for integer type bound!", Expr);
21503 Errs := True;
21505 -- The bounds are folded into literals, and we set their type to be
21506 -- universal, to avoid typing difficulties: we cannot set the type
21507 -- of the literal to the new type, because this would be a forward
21508 -- reference for the back end, and if the original type is user-
21509 -- defined this can lead to spurious semantic errors (e.g. 2928-003).
21511 else
21512 if Is_Entity_Name (Expr) then
21513 Fold_Uint (Expr, Expr_Value (Expr), True);
21514 end if;
21516 Set_Etype (Expr, Universal_Integer);
21517 end if;
21518 end Check_Bound;
21520 -- Start of processing for Signed_Integer_Type_Declaration
21522 begin
21523 -- Create an anonymous base type
21525 Implicit_Base :=
21526 Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
21528 -- Analyze and check the bounds, they can be of any integer type
21530 Lo := Low_Bound (Def);
21531 Hi := High_Bound (Def);
21533 -- Arbitrarily use Integer as the type if either bound had an error
21535 if Hi = Error or else Lo = Error then
21536 Base_Typ := Any_Integer;
21537 Set_Error_Posted (T, True);
21539 -- Here both bounds are OK expressions
21541 else
21542 Analyze_And_Resolve (Lo, Any_Integer);
21543 Analyze_And_Resolve (Hi, Any_Integer);
21545 Check_Bound (Lo);
21546 Check_Bound (Hi);
21548 if Errs then
21549 Hi := Type_High_Bound (Standard_Long_Long_Integer);
21550 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
21551 end if;
21553 -- Find type to derive from
21555 Lo_Val := Expr_Value (Lo);
21556 Hi_Val := Expr_Value (Hi);
21558 if Can_Derive_From (Standard_Short_Short_Integer) then
21559 Base_Typ := Base_Type (Standard_Short_Short_Integer);
21561 elsif Can_Derive_From (Standard_Short_Integer) then
21562 Base_Typ := Base_Type (Standard_Short_Integer);
21564 elsif Can_Derive_From (Standard_Integer) then
21565 Base_Typ := Base_Type (Standard_Integer);
21567 elsif Can_Derive_From (Standard_Long_Integer) then
21568 Base_Typ := Base_Type (Standard_Long_Integer);
21570 elsif Can_Derive_From (Standard_Long_Long_Integer) then
21571 Check_Restriction (No_Long_Long_Integers, Def);
21572 Base_Typ := Base_Type (Standard_Long_Long_Integer);
21574 else
21575 Base_Typ := Base_Type (Standard_Long_Long_Integer);
21576 Error_Msg_N ("integer type definition bounds out of range", Def);
21577 Hi := Type_High_Bound (Standard_Long_Long_Integer);
21578 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
21579 end if;
21580 end if;
21582 -- Complete both implicit base and declared first subtype entities. The
21583 -- inheritance of the rep item chain ensures that SPARK-related pragmas
21584 -- are not clobbered when the signed integer type acts as a full view of
21585 -- a private type.
21587 Set_Etype (Implicit_Base, Base_Typ);
21588 Set_Size_Info (Implicit_Base, Base_Typ);
21589 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
21590 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
21591 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
21593 Set_Ekind (T, E_Signed_Integer_Subtype);
21594 Set_Etype (T, Implicit_Base);
21595 Set_Size_Info (T, Implicit_Base);
21596 Inherit_Rep_Item_Chain (T, Implicit_Base);
21597 Set_Scalar_Range (T, Def);
21598 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
21599 Set_Is_Constrained (T);
21600 end Signed_Integer_Type_Declaration;
21602 end Sem_Ch3;