PR tree-optimization/82929
[official-gcc.git] / gcc / ada / sem_ch3.adb
blob9dc3902803307de020d8f56b3cae1dadad690a21
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ C H 3 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2017, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Contracts; use Contracts;
30 with Debug; use Debug;
31 with Elists; use Elists;
32 with Einfo; use Einfo;
33 with Errout; use Errout;
34 with Eval_Fat; use Eval_Fat;
35 with Exp_Ch3; use Exp_Ch3;
36 with Exp_Ch9; use Exp_Ch9;
37 with Exp_Disp; use Exp_Disp;
38 with Exp_Dist; use Exp_Dist;
39 with Exp_Tss; use Exp_Tss;
40 with Exp_Util; use Exp_Util;
41 with Freeze; use Freeze;
42 with Ghost; use Ghost;
43 with Itypes; use Itypes;
44 with Layout; use Layout;
45 with Lib; use Lib;
46 with Lib.Xref; use Lib.Xref;
47 with Namet; use Namet;
48 with Nmake; use Nmake;
49 with Opt; use Opt;
50 with Restrict; use Restrict;
51 with Rident; use Rident;
52 with Rtsfind; use Rtsfind;
53 with Sem; use Sem;
54 with Sem_Aux; use Sem_Aux;
55 with Sem_Case; use Sem_Case;
56 with Sem_Cat; use Sem_Cat;
57 with Sem_Ch6; use Sem_Ch6;
58 with Sem_Ch7; use Sem_Ch7;
59 with Sem_Ch8; use Sem_Ch8;
60 with Sem_Ch13; use Sem_Ch13;
61 with Sem_Dim; use Sem_Dim;
62 with Sem_Disp; use Sem_Disp;
63 with Sem_Dist; use Sem_Dist;
64 with Sem_Elim; use Sem_Elim;
65 with Sem_Eval; use Sem_Eval;
66 with Sem_Mech; use Sem_Mech;
67 with Sem_Res; use Sem_Res;
68 with Sem_Smem; use Sem_Smem;
69 with Sem_Type; use Sem_Type;
70 with Sem_Util; use Sem_Util;
71 with Sem_Warn; use Sem_Warn;
72 with Stand; use Stand;
73 with Sinfo; use Sinfo;
74 with Sinput; use Sinput;
75 with Snames; use Snames;
76 with Targparm; use Targparm;
77 with Tbuild; use Tbuild;
78 with Ttypes; use Ttypes;
79 with Uintp; use Uintp;
80 with Urealp; use Urealp;
82 package body Sem_Ch3 is
84 -----------------------
85 -- Local Subprograms --
86 -----------------------
88 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id);
89 -- Ada 2005 (AI-251): Add the tag components corresponding to all the
90 -- abstract interface types implemented by a record type or a derived
91 -- record type.
93 procedure Build_Derived_Type
94 (N : Node_Id;
95 Parent_Type : Entity_Id;
96 Derived_Type : Entity_Id;
97 Is_Completion : Boolean;
98 Derive_Subps : Boolean := True);
99 -- Create and decorate a Derived_Type given the Parent_Type entity. N is
100 -- the N_Full_Type_Declaration node containing the derived type definition.
101 -- Parent_Type is the entity for the parent type in the derived type
102 -- definition and Derived_Type the actual derived type. Is_Completion must
103 -- be set to False if Derived_Type is the N_Defining_Identifier node in N
104 -- (i.e. Derived_Type = Defining_Identifier (N)). In this case N is not the
105 -- completion of a private type declaration. If Is_Completion is set to
106 -- True, N is the completion of a private type declaration and Derived_Type
107 -- is different from the defining identifier inside N (i.e. Derived_Type /=
108 -- Defining_Identifier (N)). Derive_Subps indicates whether the parent
109 -- subprograms should be derived. The only case where this parameter is
110 -- False is when Build_Derived_Type is recursively called to process an
111 -- implicit derived full type for a type derived from a private type (in
112 -- that case the subprograms must only be derived for the private view of
113 -- the type).
115 -- ??? These flags need a bit of re-examination and re-documentation:
116 -- ??? are they both necessary (both seem related to the recursion)?
118 procedure Build_Derived_Access_Type
119 (N : Node_Id;
120 Parent_Type : Entity_Id;
121 Derived_Type : Entity_Id);
122 -- Subsidiary procedure to Build_Derived_Type. For a derived access type,
123 -- create an implicit base if the parent type is constrained or if the
124 -- subtype indication has a constraint.
126 procedure Build_Derived_Array_Type
127 (N : Node_Id;
128 Parent_Type : Entity_Id;
129 Derived_Type : Entity_Id);
130 -- Subsidiary procedure to Build_Derived_Type. For a derived array type,
131 -- create an implicit base if the parent type is constrained or if the
132 -- subtype indication has a constraint.
134 procedure Build_Derived_Concurrent_Type
135 (N : Node_Id;
136 Parent_Type : Entity_Id;
137 Derived_Type : Entity_Id);
138 -- Subsidiary procedure to Build_Derived_Type. For a derived task or
139 -- protected type, inherit entries and protected subprograms, check
140 -- legality of discriminant constraints if any.
142 procedure Build_Derived_Enumeration_Type
143 (N : Node_Id;
144 Parent_Type : Entity_Id;
145 Derived_Type : Entity_Id);
146 -- Subsidiary procedure to Build_Derived_Type. For a derived enumeration
147 -- type, we must create a new list of literals. Types derived from
148 -- Character and [Wide_]Wide_Character are special-cased.
150 procedure Build_Derived_Numeric_Type
151 (N : Node_Id;
152 Parent_Type : Entity_Id;
153 Derived_Type : Entity_Id);
154 -- Subsidiary procedure to Build_Derived_Type. For numeric types, create
155 -- an anonymous base type, and propagate constraint to subtype if needed.
157 procedure Build_Derived_Private_Type
158 (N : Node_Id;
159 Parent_Type : Entity_Id;
160 Derived_Type : Entity_Id;
161 Is_Completion : Boolean;
162 Derive_Subps : Boolean := True);
163 -- Subsidiary procedure to Build_Derived_Type. This procedure is complex
164 -- because the parent may or may not have a completion, and the derivation
165 -- may itself be a completion.
167 procedure Build_Derived_Record_Type
168 (N : Node_Id;
169 Parent_Type : Entity_Id;
170 Derived_Type : Entity_Id;
171 Derive_Subps : Boolean := True);
172 -- Subsidiary procedure used for tagged and untagged record types
173 -- by Build_Derived_Type and Analyze_Private_Extension_Declaration.
174 -- All parameters are as in Build_Derived_Type except that N, in
175 -- addition to being an N_Full_Type_Declaration node, can also be an
176 -- N_Private_Extension_Declaration node. See the definition of this routine
177 -- for much more info. Derive_Subps indicates whether subprograms should be
178 -- derived from the parent type. The only case where Derive_Subps is False
179 -- is for an implicit derived full type for a type derived from a private
180 -- type (see Build_Derived_Type).
182 procedure Build_Discriminal (Discrim : Entity_Id);
183 -- Create the discriminal corresponding to discriminant Discrim, that is
184 -- the parameter corresponding to Discrim to be used in initialization
185 -- procedures for the type where Discrim is a discriminant. Discriminals
186 -- are not used during semantic analysis, and are not fully defined
187 -- entities until expansion. Thus they are not given a scope until
188 -- initialization procedures are built.
190 function Build_Discriminant_Constraints
191 (T : Entity_Id;
192 Def : Node_Id;
193 Derived_Def : Boolean := False) return Elist_Id;
194 -- Validate discriminant constraints and return the list of the constraints
195 -- in order of discriminant declarations, where T is the discriminated
196 -- unconstrained type. Def is the N_Subtype_Indication node where the
197 -- discriminants constraints for T are specified. Derived_Def is True
198 -- when building the discriminant constraints in a derived type definition
199 -- of the form "type D (...) is new T (xxx)". In this case T is the parent
200 -- type and Def is the constraint "(xxx)" on T and this routine sets the
201 -- Corresponding_Discriminant field of the discriminants in the derived
202 -- type D to point to the corresponding discriminants in the parent type T.
204 procedure Build_Discriminated_Subtype
205 (T : Entity_Id;
206 Def_Id : Entity_Id;
207 Elist : Elist_Id;
208 Related_Nod : Node_Id;
209 For_Access : Boolean := False);
210 -- Subsidiary procedure to Constrain_Discriminated_Type and to
211 -- Process_Incomplete_Dependents. Given
213 -- T (a possibly discriminated base type)
214 -- Def_Id (a very partially built subtype for T),
216 -- the call completes Def_Id to be the appropriate E_*_Subtype.
218 -- The Elist is the list of discriminant constraints if any (it is set
219 -- to No_Elist if T is not a discriminated type, and to an empty list if
220 -- T has discriminants but there are no discriminant constraints). The
221 -- Related_Nod is the same as Decl_Node in Create_Constrained_Components.
222 -- The For_Access says whether or not this subtype is really constraining
223 -- an access type. That is its sole purpose is the designated type of an
224 -- access type -- in which case a Private_Subtype Is_For_Access_Subtype
225 -- is built to avoid freezing T when the access subtype is frozen.
227 function Build_Scalar_Bound
228 (Bound : Node_Id;
229 Par_T : Entity_Id;
230 Der_T : Entity_Id) return Node_Id;
231 -- The bounds of a derived scalar type are conversions of the bounds of
232 -- the parent type. Optimize the representation if the bounds are literals.
233 -- Needs a more complete spec--what are the parameters exactly, and what
234 -- exactly is the returned value, and how is Bound affected???
236 procedure Build_Underlying_Full_View
237 (N : Node_Id;
238 Typ : Entity_Id;
239 Par : Entity_Id);
240 -- If the completion of a private type is itself derived from a private
241 -- type, or if the full view of a private subtype is itself private, the
242 -- back-end has no way to compute the actual size of this type. We build
243 -- an internal subtype declaration of the proper parent type to convey
244 -- this information. This extra mechanism is needed because a full
245 -- view cannot itself have a full view (it would get clobbered during
246 -- view exchanges).
248 procedure Check_Access_Discriminant_Requires_Limited
249 (D : Node_Id;
250 Loc : Node_Id);
251 -- Check the restriction that the type to which an access discriminant
252 -- belongs must be a concurrent type or a descendant of a type with
253 -- the reserved word 'limited' in its declaration.
255 procedure Check_Anonymous_Access_Components
256 (Typ_Decl : Node_Id;
257 Typ : Entity_Id;
258 Prev : Entity_Id;
259 Comp_List : Node_Id);
260 -- Ada 2005 AI-382: an access component in a record definition can refer to
261 -- the enclosing record, in which case it denotes the type itself, and not
262 -- the current instance of the type. We create an anonymous access type for
263 -- the component, and flag it as an access to a component, so accessibility
264 -- checks are properly performed on it. The declaration of the access type
265 -- is placed ahead of that of the record to prevent order-of-elaboration
266 -- circularity issues in Gigi. We create an incomplete type for the record
267 -- declaration, which is the designated type of the anonymous access.
269 procedure Check_Delta_Expression (E : Node_Id);
270 -- Check that the expression represented by E is suitable for use as a
271 -- delta expression, i.e. it is of real type and is static.
273 procedure Check_Digits_Expression (E : Node_Id);
274 -- Check that the expression represented by E is suitable for use as a
275 -- digits expression, i.e. it is of integer type, positive and static.
277 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id);
278 -- Validate the initialization of an object declaration. T is the required
279 -- type, and Exp is the initialization expression.
281 procedure Check_Interfaces (N : Node_Id; Def : Node_Id);
282 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
284 procedure Check_Or_Process_Discriminants
285 (N : Node_Id;
286 T : Entity_Id;
287 Prev : Entity_Id := Empty);
288 -- If N is the full declaration of the completion T of an incomplete or
289 -- private type, check its discriminants (which are already known to be
290 -- conformant with those of the partial view, see Find_Type_Name),
291 -- otherwise process them. Prev is the entity of the partial declaration,
292 -- if any.
294 procedure Check_Real_Bound (Bound : Node_Id);
295 -- Check given bound for being of real type and static. If not, post an
296 -- appropriate message, and rewrite the bound with the real literal zero.
298 procedure Constant_Redeclaration
299 (Id : Entity_Id;
300 N : Node_Id;
301 T : out Entity_Id);
302 -- Various checks on legality of full declaration of deferred constant.
303 -- Id is the entity for the redeclaration, N is the N_Object_Declaration,
304 -- node. The caller has not yet set any attributes of this entity.
306 function Contain_Interface
307 (Iface : Entity_Id;
308 Ifaces : Elist_Id) return Boolean;
309 -- Ada 2005: Determine whether Iface is present in the list Ifaces
311 procedure Convert_Scalar_Bounds
312 (N : Node_Id;
313 Parent_Type : Entity_Id;
314 Derived_Type : Entity_Id;
315 Loc : Source_Ptr);
316 -- For derived scalar types, convert the bounds in the type definition to
317 -- the derived type, and complete their analysis. Given a constraint of the
318 -- form ".. new T range Lo .. Hi", Lo and Hi are analyzed and resolved with
319 -- T'Base, the parent_type. The bounds of the derived type (the anonymous
320 -- base) are copies of Lo and Hi. Finally, the bounds of the derived
321 -- subtype are conversions of those bounds to the derived_type, so that
322 -- their typing is consistent.
324 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id);
325 -- Copies attributes from array base type T2 to array base type T1. Copies
326 -- only attributes that apply to base types, but not subtypes.
328 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id);
329 -- Copies attributes from array subtype T2 to array subtype T1. Copies
330 -- attributes that apply to both subtypes and base types.
332 procedure Create_Constrained_Components
333 (Subt : Entity_Id;
334 Decl_Node : Node_Id;
335 Typ : Entity_Id;
336 Constraints : Elist_Id);
337 -- Build the list of entities for a constrained discriminated record
338 -- subtype. If a component depends on a discriminant, replace its subtype
339 -- using the discriminant values in the discriminant constraint. Subt
340 -- is the defining identifier for the subtype whose list of constrained
341 -- entities we will create. Decl_Node is the type declaration node where
342 -- we will attach all the itypes created. Typ is the base discriminated
343 -- type for the subtype Subt. Constraints is the list of discriminant
344 -- constraints for Typ.
346 function Constrain_Component_Type
347 (Comp : Entity_Id;
348 Constrained_Typ : Entity_Id;
349 Related_Node : Node_Id;
350 Typ : Entity_Id;
351 Constraints : Elist_Id) return Entity_Id;
352 -- Given a discriminated base type Typ, a list of discriminant constraints,
353 -- Constraints, for Typ and a component Comp of Typ, create and return the
354 -- type corresponding to Etype (Comp) where all discriminant references
355 -- are replaced with the corresponding constraint. If Etype (Comp) contains
356 -- no discriminant references then it is returned as-is. Constrained_Typ
357 -- is the final constrained subtype to which the constrained component
358 -- belongs. Related_Node is the node where we attach all created itypes.
360 procedure Constrain_Access
361 (Def_Id : in out Entity_Id;
362 S : Node_Id;
363 Related_Nod : Node_Id);
364 -- Apply a list of constraints to an access type. If Def_Id is empty, it is
365 -- an anonymous type created for a subtype indication. In that case it is
366 -- created in the procedure and attached to Related_Nod.
368 procedure Constrain_Array
369 (Def_Id : in out Entity_Id;
370 SI : Node_Id;
371 Related_Nod : Node_Id;
372 Related_Id : Entity_Id;
373 Suffix : Character);
374 -- Apply a list of index constraints to an unconstrained array type. The
375 -- first parameter is the entity for the resulting subtype. A value of
376 -- Empty for Def_Id indicates that an implicit type must be created, but
377 -- creation is delayed (and must be done by this procedure) because other
378 -- subsidiary implicit types must be created first (which is why Def_Id
379 -- is an in/out parameter). The second parameter is a subtype indication
380 -- node for the constrained array to be created (e.g. something of the
381 -- form string (1 .. 10)). Related_Nod gives the place where this type
382 -- has to be inserted in the tree. The Related_Id and Suffix parameters
383 -- are used to build the associated Implicit type name.
385 procedure Constrain_Concurrent
386 (Def_Id : in out Entity_Id;
387 SI : Node_Id;
388 Related_Nod : Node_Id;
389 Related_Id : Entity_Id;
390 Suffix : Character);
391 -- Apply list of discriminant constraints to an unconstrained concurrent
392 -- type.
394 -- SI is the N_Subtype_Indication node containing the constraint and
395 -- the unconstrained type to constrain.
397 -- Def_Id is the entity for the resulting constrained subtype. A value
398 -- of Empty for Def_Id indicates that an implicit type must be created,
399 -- but creation is delayed (and must be done by this procedure) because
400 -- other subsidiary implicit types must be created first (which is why
401 -- Def_Id is an in/out parameter).
403 -- Related_Nod gives the place where this type has to be inserted
404 -- in the tree.
406 -- The last two arguments are used to create its external name if needed.
408 function Constrain_Corresponding_Record
409 (Prot_Subt : Entity_Id;
410 Corr_Rec : Entity_Id;
411 Related_Nod : Node_Id) return Entity_Id;
412 -- When constraining a protected type or task type with discriminants,
413 -- constrain the corresponding record with the same discriminant values.
415 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id);
416 -- Constrain a decimal fixed point type with a digits constraint and/or a
417 -- range constraint, and build E_Decimal_Fixed_Point_Subtype entity.
419 procedure Constrain_Discriminated_Type
420 (Def_Id : Entity_Id;
421 S : Node_Id;
422 Related_Nod : Node_Id;
423 For_Access : Boolean := False);
424 -- Process discriminant constraints of composite type. Verify that values
425 -- have been provided for all discriminants, that the original type is
426 -- unconstrained, and that the types of the supplied expressions match
427 -- the discriminant types. The first three parameters are like in routine
428 -- Constrain_Concurrent. See Build_Discriminated_Subtype for an explanation
429 -- of For_Access.
431 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id);
432 -- Constrain an enumeration type with a range constraint. This is identical
433 -- to Constrain_Integer, but for the Ekind of the resulting subtype.
435 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id);
436 -- Constrain a floating point type with either a digits constraint
437 -- and/or a range constraint, building a E_Floating_Point_Subtype.
439 procedure Constrain_Index
440 (Index : Node_Id;
441 S : Node_Id;
442 Related_Nod : Node_Id;
443 Related_Id : Entity_Id;
444 Suffix : Character;
445 Suffix_Index : Nat);
446 -- Process an index constraint S in a constrained array declaration. The
447 -- constraint can be a subtype name, or a range with or without an explicit
448 -- subtype mark. The index is the corresponding index of the unconstrained
449 -- array. The Related_Id and Suffix parameters are used to build the
450 -- associated Implicit type name.
452 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id);
453 -- Build subtype of a signed or modular integer type
455 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id);
456 -- Constrain an ordinary fixed point type with a range constraint, and
457 -- build an E_Ordinary_Fixed_Point_Subtype entity.
459 procedure Copy_And_Swap (Priv, Full : Entity_Id);
460 -- Copy the Priv entity into the entity of its full declaration then swap
461 -- the two entities in such a manner that the former private type is now
462 -- seen as a full type.
464 procedure Decimal_Fixed_Point_Type_Declaration
465 (T : Entity_Id;
466 Def : Node_Id);
467 -- Create a new decimal fixed point type, and apply the constraint to
468 -- obtain a subtype of this new type.
470 procedure Complete_Private_Subtype
471 (Priv : Entity_Id;
472 Full : Entity_Id;
473 Full_Base : Entity_Id;
474 Related_Nod : Node_Id);
475 -- Complete the implicit full view of a private subtype by setting the
476 -- appropriate semantic fields. If the full view of the parent is a record
477 -- type, build constrained components of subtype.
479 procedure Derive_Progenitor_Subprograms
480 (Parent_Type : Entity_Id;
481 Tagged_Type : Entity_Id);
482 -- Ada 2005 (AI-251): To complete type derivation, collect the primitive
483 -- operations of progenitors of Tagged_Type, and replace the subsidiary
484 -- subtypes with Tagged_Type, to build the specs of the inherited interface
485 -- primitives. The derived primitives are aliased to those of the
486 -- interface. This routine takes care also of transferring to the full view
487 -- subprograms associated with the partial view of Tagged_Type that cover
488 -- interface primitives.
490 procedure Derived_Standard_Character
491 (N : Node_Id;
492 Parent_Type : Entity_Id;
493 Derived_Type : Entity_Id);
494 -- Subsidiary procedure to Build_Derived_Enumeration_Type which handles
495 -- derivations from types Standard.Character and Standard.Wide_Character.
497 procedure Derived_Type_Declaration
498 (T : Entity_Id;
499 N : Node_Id;
500 Is_Completion : Boolean);
501 -- Process a derived type declaration. Build_Derived_Type is invoked
502 -- to process the actual derived type definition. Parameters N and
503 -- Is_Completion have the same meaning as in Build_Derived_Type.
504 -- T is the N_Defining_Identifier for the entity defined in the
505 -- N_Full_Type_Declaration node N, that is T is the derived type.
507 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id);
508 -- Insert each literal in symbol table, as an overloadable identifier. Each
509 -- enumeration type is mapped into a sequence of integers, and each literal
510 -- is defined as a constant with integer value. If any of the literals are
511 -- character literals, the type is a character type, which means that
512 -- strings are legal aggregates for arrays of components of the type.
514 function Expand_To_Stored_Constraint
515 (Typ : Entity_Id;
516 Constraint : Elist_Id) return Elist_Id;
517 -- Given a constraint (i.e. a list of expressions) on the discriminants of
518 -- Typ, expand it into a constraint on the stored discriminants and return
519 -- the new list of expressions constraining the stored discriminants.
521 function Find_Type_Of_Object
522 (Obj_Def : Node_Id;
523 Related_Nod : Node_Id) return Entity_Id;
524 -- Get type entity for object referenced by Obj_Def, attaching the implicit
525 -- types generated to Related_Nod.
527 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id);
528 -- Create a new float and apply the constraint to obtain subtype of it
530 function Has_Range_Constraint (N : Node_Id) return Boolean;
531 -- Given an N_Subtype_Indication node N, return True if a range constraint
532 -- is present, either directly, or as part of a digits or delta constraint.
533 -- In addition, a digits constraint in the decimal case returns True, since
534 -- it establishes a default range if no explicit range is present.
536 function Inherit_Components
537 (N : Node_Id;
538 Parent_Base : Entity_Id;
539 Derived_Base : Entity_Id;
540 Is_Tagged : Boolean;
541 Inherit_Discr : Boolean;
542 Discs : Elist_Id) return Elist_Id;
543 -- Called from Build_Derived_Record_Type to inherit the components of
544 -- Parent_Base (a base type) into the Derived_Base (the derived base type).
545 -- For more information on derived types and component inheritance please
546 -- consult the comment above the body of Build_Derived_Record_Type.
548 -- N is the original derived type declaration
550 -- Is_Tagged is set if we are dealing with tagged types
552 -- If Inherit_Discr is set, Derived_Base inherits its discriminants from
553 -- Parent_Base, otherwise no discriminants are inherited.
555 -- Discs gives the list of constraints that apply to Parent_Base in the
556 -- derived type declaration. If Discs is set to No_Elist, then we have
557 -- the following situation:
559 -- type Parent (D1..Dn : ..) is [tagged] record ...;
560 -- type Derived is new Parent [with ...];
562 -- which gets treated as
564 -- type Derived (D1..Dn : ..) is new Parent (D1,..,Dn) [with ...];
566 -- For untagged types the returned value is an association list. The list
567 -- starts from the association (Parent_Base => Derived_Base), and then it
568 -- contains a sequence of the associations of the form
570 -- (Old_Component => New_Component),
572 -- where Old_Component is the Entity_Id of a component in Parent_Base and
573 -- New_Component is the Entity_Id of the corresponding component in
574 -- Derived_Base. For untagged records, this association list is needed when
575 -- copying the record declaration for the derived base. In the tagged case
576 -- the value returned is irrelevant.
578 procedure Inherit_Predicate_Flags (Subt, Par : Entity_Id);
579 -- Propagate static and dynamic predicate flags from a parent to the
580 -- subtype in a subtype declaration with and without constraints.
582 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean;
583 -- Subsidiary to Check_Abstract_Overriding and Derive_Subprogram.
584 -- Determine whether subprogram Subp is a procedure subject to pragma
585 -- Extensions_Visible with value False and has at least one controlling
586 -- parameter of mode OUT.
588 function Is_Valid_Constraint_Kind
589 (T_Kind : Type_Kind;
590 Constraint_Kind : Node_Kind) return Boolean;
591 -- Returns True if it is legal to apply the given kind of constraint to the
592 -- given kind of type (index constraint to an array type, for example).
594 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id);
595 -- Create new modular type. Verify that modulus is in bounds
597 procedure New_Concatenation_Op (Typ : Entity_Id);
598 -- Create an abbreviated declaration for an operator in order to
599 -- materialize concatenation on array types.
601 procedure Ordinary_Fixed_Point_Type_Declaration
602 (T : Entity_Id;
603 Def : Node_Id);
604 -- Create a new ordinary fixed point type, and apply the constraint to
605 -- obtain subtype of it.
607 procedure Prepare_Private_Subtype_Completion
608 (Id : Entity_Id;
609 Related_Nod : Node_Id);
610 -- Id is a subtype of some private type. Creates the full declaration
611 -- associated with Id whenever possible, i.e. when the full declaration
612 -- of the base type is already known. Records each subtype into
613 -- Private_Dependents of the base type.
615 procedure Process_Incomplete_Dependents
616 (N : Node_Id;
617 Full_T : Entity_Id;
618 Inc_T : Entity_Id);
619 -- Process all entities that depend on an incomplete type. There include
620 -- subtypes, subprogram types that mention the incomplete type in their
621 -- profiles, and subprogram with access parameters that designate the
622 -- incomplete type.
624 -- Inc_T is the defining identifier of an incomplete type declaration, its
625 -- Ekind is E_Incomplete_Type.
627 -- N is the corresponding N_Full_Type_Declaration for Inc_T.
629 -- Full_T is N's defining identifier.
631 -- Subtypes of incomplete types with discriminants are completed when the
632 -- parent type is. This is simpler than private subtypes, because they can
633 -- only appear in the same scope, and there is no need to exchange views.
634 -- Similarly, access_to_subprogram types may have a parameter or a return
635 -- type that is an incomplete type, and that must be replaced with the
636 -- full type.
638 -- If the full type is tagged, subprogram with access parameters that
639 -- designated the incomplete may be primitive operations of the full type,
640 -- and have to be processed accordingly.
642 procedure Process_Real_Range_Specification (Def : Node_Id);
643 -- Given the type definition for a real type, this procedure processes and
644 -- checks the real range specification of this type definition if one is
645 -- present. If errors are found, error messages are posted, and the
646 -- Real_Range_Specification of Def is reset to Empty.
648 procedure Record_Type_Declaration
649 (T : Entity_Id;
650 N : Node_Id;
651 Prev : Entity_Id);
652 -- Process a record type declaration (for both untagged and tagged
653 -- records). Parameters T and N are exactly like in procedure
654 -- Derived_Type_Declaration, except that no flag Is_Completion is needed
655 -- for this routine. If this is the completion of an incomplete type
656 -- declaration, Prev is the entity of the incomplete declaration, used for
657 -- cross-referencing. Otherwise Prev = T.
659 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id);
660 -- This routine is used to process the actual record type definition (both
661 -- for untagged and tagged records). Def is a record type definition node.
662 -- This procedure analyzes the components in this record type definition.
663 -- Prev_T is the entity for the enclosing record type. It is provided so
664 -- that its Has_Task flag can be set if any of the component have Has_Task
665 -- set. If the declaration is the completion of an incomplete type
666 -- declaration, Prev_T is the original incomplete type, whose full view is
667 -- the record type.
669 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id);
670 -- Subsidiary to Build_Derived_Record_Type. For untagged records, we
671 -- build a copy of the declaration tree of the parent, and we create
672 -- independently the list of components for the derived type. Semantic
673 -- information uses the component entities, but record representation
674 -- clauses are validated on the declaration tree. This procedure replaces
675 -- discriminants and components in the declaration with those that have
676 -- been created by Inherit_Components.
678 procedure Set_Fixed_Range
679 (E : Entity_Id;
680 Loc : Source_Ptr;
681 Lo : Ureal;
682 Hi : Ureal);
683 -- Build a range node with the given bounds and set it as the Scalar_Range
684 -- of the given fixed-point type entity. Loc is the source location used
685 -- for the constructed range. See body for further details.
687 procedure Set_Scalar_Range_For_Subtype
688 (Def_Id : Entity_Id;
689 R : Node_Id;
690 Subt : Entity_Id);
691 -- This routine is used to set the scalar range field for a subtype given
692 -- Def_Id, the entity for the subtype, and R, the range expression for the
693 -- scalar range. Subt provides the parent subtype to be used to analyze,
694 -- resolve, and check the given range.
696 procedure Set_Default_SSO (T : Entity_Id);
697 -- T is the entity for an array or record being declared. This procedure
698 -- sets the flags SSO_Set_Low_By_Default/SSO_Set_High_By_Default according
699 -- to the setting of Opt.Default_SSO.
701 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id);
702 -- Create a new signed integer entity, and apply the constraint to obtain
703 -- the required first named subtype of this type.
705 procedure Set_Stored_Constraint_From_Discriminant_Constraint
706 (E : Entity_Id);
707 -- E is some record type. This routine computes E's Stored_Constraint
708 -- from its Discriminant_Constraint.
710 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id);
711 -- Check that an entity in a list of progenitors is an interface,
712 -- emit error otherwise.
714 -----------------------
715 -- Access_Definition --
716 -----------------------
718 function Access_Definition
719 (Related_Nod : Node_Id;
720 N : Node_Id) return Entity_Id
722 Anon_Type : Entity_Id;
723 Anon_Scope : Entity_Id;
724 Desig_Type : Entity_Id;
725 Enclosing_Prot_Type : Entity_Id := Empty;
727 begin
728 Check_SPARK_05_Restriction ("access type is not allowed", N);
730 if Is_Entry (Current_Scope)
731 and then Is_Task_Type (Etype (Scope (Current_Scope)))
732 then
733 Error_Msg_N ("task entries cannot have access parameters", N);
734 return Empty;
735 end if;
737 -- Ada 2005: For an object declaration the corresponding anonymous
738 -- type is declared in the current scope.
740 -- If the access definition is the return type of another access to
741 -- function, scope is the current one, because it is the one of the
742 -- current type declaration, except for the pathological case below.
744 if Nkind_In (Related_Nod, N_Object_Declaration,
745 N_Access_Function_Definition)
746 then
747 Anon_Scope := Current_Scope;
749 -- A pathological case: function returning access functions that
750 -- return access functions, etc. Each anonymous access type created
751 -- is in the enclosing scope of the outermost function.
753 declare
754 Par : Node_Id;
756 begin
757 Par := Related_Nod;
758 while Nkind_In (Par, N_Access_Function_Definition,
759 N_Access_Definition)
760 loop
761 Par := Parent (Par);
762 end loop;
764 if Nkind (Par) = N_Function_Specification then
765 Anon_Scope := Scope (Defining_Entity (Par));
766 end if;
767 end;
769 -- For the anonymous function result case, retrieve the scope of the
770 -- function specification's associated entity rather than using the
771 -- current scope. The current scope will be the function itself if the
772 -- formal part is currently being analyzed, but will be the parent scope
773 -- in the case of a parameterless function, and we always want to use
774 -- the function's parent scope. Finally, if the function is a child
775 -- unit, we must traverse the tree to retrieve the proper entity.
777 elsif Nkind (Related_Nod) = N_Function_Specification
778 and then Nkind (Parent (N)) /= N_Parameter_Specification
779 then
780 -- If the current scope is a protected type, the anonymous access
781 -- is associated with one of the protected operations, and must
782 -- be available in the scope that encloses the protected declaration.
783 -- Otherwise the type is in the scope enclosing the subprogram.
785 -- If the function has formals, The return type of a subprogram
786 -- declaration is analyzed in the scope of the subprogram (see
787 -- Process_Formals) and thus the protected type, if present, is
788 -- the scope of the current function scope.
790 if Ekind (Current_Scope) = E_Protected_Type then
791 Enclosing_Prot_Type := Current_Scope;
793 elsif Ekind (Current_Scope) = E_Function
794 and then Ekind (Scope (Current_Scope)) = E_Protected_Type
795 then
796 Enclosing_Prot_Type := Scope (Current_Scope);
797 end if;
799 if Present (Enclosing_Prot_Type) then
800 Anon_Scope := Scope (Enclosing_Prot_Type);
802 else
803 Anon_Scope := Scope (Defining_Entity (Related_Nod));
804 end if;
806 -- For an access type definition, if the current scope is a child
807 -- unit it is the scope of the type.
809 elsif Is_Compilation_Unit (Current_Scope) then
810 Anon_Scope := Current_Scope;
812 -- For access formals, access components, and access discriminants, the
813 -- scope is that of the enclosing declaration,
815 else
816 Anon_Scope := Scope (Current_Scope);
817 end if;
819 Anon_Type :=
820 Create_Itype
821 (E_Anonymous_Access_Type, Related_Nod, Scope_Id => Anon_Scope);
823 if All_Present (N)
824 and then Ada_Version >= Ada_2005
825 then
826 Error_Msg_N ("ALL is not permitted for anonymous access types", N);
827 end if;
829 -- Ada 2005 (AI-254): In case of anonymous access to subprograms call
830 -- the corresponding semantic routine
832 if Present (Access_To_Subprogram_Definition (N)) then
834 -- Compiler runtime units are compiled in Ada 2005 mode when building
835 -- the runtime library but must also be compilable in Ada 95 mode
836 -- (when bootstrapping the compiler).
838 Check_Compiler_Unit ("anonymous access to subprogram", N);
840 Access_Subprogram_Declaration
841 (T_Name => Anon_Type,
842 T_Def => Access_To_Subprogram_Definition (N));
844 if Ekind (Anon_Type) = E_Access_Protected_Subprogram_Type then
845 Set_Ekind
846 (Anon_Type, E_Anonymous_Access_Protected_Subprogram_Type);
847 else
848 Set_Ekind (Anon_Type, E_Anonymous_Access_Subprogram_Type);
849 end if;
851 Set_Can_Use_Internal_Rep
852 (Anon_Type, not Always_Compatible_Rep_On_Target);
854 -- If the anonymous access is associated with a protected operation,
855 -- create a reference to it after the enclosing protected definition
856 -- because the itype will be used in the subsequent bodies.
858 -- If the anonymous access itself is protected, a full type
859 -- declaratiton will be created for it, so that the equivalent
860 -- record type can be constructed. For further details, see
861 -- Replace_Anonymous_Access_To_Protected-Subprogram.
863 if Ekind (Current_Scope) = E_Protected_Type
864 and then not Protected_Present (Access_To_Subprogram_Definition (N))
865 then
866 Build_Itype_Reference (Anon_Type, Parent (Current_Scope));
867 end if;
869 return Anon_Type;
870 end if;
872 Find_Type (Subtype_Mark (N));
873 Desig_Type := Entity (Subtype_Mark (N));
875 Set_Directly_Designated_Type (Anon_Type, Desig_Type);
876 Set_Etype (Anon_Type, Anon_Type);
878 -- Make sure the anonymous access type has size and alignment fields
879 -- set, as required by gigi. This is necessary in the case of the
880 -- Task_Body_Procedure.
882 if not Has_Private_Component (Desig_Type) then
883 Layout_Type (Anon_Type);
884 end if;
886 -- Ada 2005 (AI-231): Ada 2005 semantics for anonymous access differs
887 -- from Ada 95 semantics. In Ada 2005, anonymous access must specify if
888 -- the null value is allowed. In Ada 95 the null value is never allowed.
890 if Ada_Version >= Ada_2005 then
891 Set_Can_Never_Be_Null (Anon_Type, Null_Exclusion_Present (N));
892 else
893 Set_Can_Never_Be_Null (Anon_Type, True);
894 end if;
896 -- The anonymous access type is as public as the discriminated type or
897 -- subprogram that defines it. It is imported (for back-end purposes)
898 -- if the designated type is.
900 Set_Is_Public (Anon_Type, Is_Public (Scope (Anon_Type)));
902 -- Ada 2005 (AI-231): Propagate the access-constant attribute
904 Set_Is_Access_Constant (Anon_Type, Constant_Present (N));
906 -- The context is either a subprogram declaration, object declaration,
907 -- or an access discriminant, in a private or a full type declaration.
908 -- In the case of a subprogram, if the designated type is incomplete,
909 -- the operation will be a primitive operation of the full type, to be
910 -- updated subsequently. If the type is imported through a limited_with
911 -- clause, the subprogram is not a primitive operation of the type
912 -- (which is declared elsewhere in some other scope).
914 if Ekind (Desig_Type) = E_Incomplete_Type
915 and then not From_Limited_With (Desig_Type)
916 and then Is_Overloadable (Current_Scope)
917 then
918 Append_Elmt (Current_Scope, Private_Dependents (Desig_Type));
919 Set_Has_Delayed_Freeze (Current_Scope);
920 end if;
922 -- Ada 2005: If the designated type is an interface that may contain
923 -- tasks, create a Master entity for the declaration. This must be done
924 -- before expansion of the full declaration, because the declaration may
925 -- include an expression that is an allocator, whose expansion needs the
926 -- proper Master for the created tasks.
928 if Nkind (Related_Nod) = N_Object_Declaration and then Expander_Active
929 then
930 if Is_Interface (Desig_Type) and then Is_Limited_Record (Desig_Type)
931 then
932 Build_Class_Wide_Master (Anon_Type);
934 -- Similarly, if the type is an anonymous access that designates
935 -- tasks, create a master entity for it in the current context.
937 elsif Has_Task (Desig_Type) and then Comes_From_Source (Related_Nod)
938 then
939 Build_Master_Entity (Defining_Identifier (Related_Nod));
940 Build_Master_Renaming (Anon_Type);
941 end if;
942 end if;
944 -- For a private component of a protected type, it is imperative that
945 -- the back-end elaborate the type immediately after the protected
946 -- declaration, because this type will be used in the declarations
947 -- created for the component within each protected body, so we must
948 -- create an itype reference for it now.
950 if Nkind (Parent (Related_Nod)) = N_Protected_Definition then
951 Build_Itype_Reference (Anon_Type, Parent (Parent (Related_Nod)));
953 -- Similarly, if the access definition is the return result of a
954 -- function, create an itype reference for it because it will be used
955 -- within the function body. For a regular function that is not a
956 -- compilation unit, insert reference after the declaration. For a
957 -- protected operation, insert it after the enclosing protected type
958 -- declaration. In either case, do not create a reference for a type
959 -- obtained through a limited_with clause, because this would introduce
960 -- semantic dependencies.
962 -- Similarly, do not create a reference if the designated type is a
963 -- generic formal, because no use of it will reach the backend.
965 elsif Nkind (Related_Nod) = N_Function_Specification
966 and then not From_Limited_With (Desig_Type)
967 and then not Is_Generic_Type (Desig_Type)
968 then
969 if Present (Enclosing_Prot_Type) then
970 Build_Itype_Reference (Anon_Type, Parent (Enclosing_Prot_Type));
972 elsif Is_List_Member (Parent (Related_Nod))
973 and then Nkind (Parent (N)) /= N_Parameter_Specification
974 then
975 Build_Itype_Reference (Anon_Type, Parent (Related_Nod));
976 end if;
978 -- Finally, create an itype reference for an object declaration of an
979 -- anonymous access type. This is strictly necessary only for deferred
980 -- constants, but in any case will avoid out-of-scope problems in the
981 -- back-end.
983 elsif Nkind (Related_Nod) = N_Object_Declaration then
984 Build_Itype_Reference (Anon_Type, Related_Nod);
985 end if;
987 return Anon_Type;
988 end Access_Definition;
990 -----------------------------------
991 -- Access_Subprogram_Declaration --
992 -----------------------------------
994 procedure Access_Subprogram_Declaration
995 (T_Name : Entity_Id;
996 T_Def : Node_Id)
998 procedure Check_For_Premature_Usage (Def : Node_Id);
999 -- Check that type T_Name is not used, directly or recursively, as a
1000 -- parameter or a return type in Def. Def is either a subtype, an
1001 -- access_definition, or an access_to_subprogram_definition.
1003 -------------------------------
1004 -- Check_For_Premature_Usage --
1005 -------------------------------
1007 procedure Check_For_Premature_Usage (Def : Node_Id) is
1008 Param : Node_Id;
1010 begin
1011 -- Check for a subtype mark
1013 if Nkind (Def) in N_Has_Etype then
1014 if Etype (Def) = T_Name then
1015 Error_Msg_N
1016 ("type& cannot be used before end of its declaration", Def);
1017 end if;
1019 -- If this is not a subtype, then this is an access_definition
1021 elsif Nkind (Def) = N_Access_Definition then
1022 if Present (Access_To_Subprogram_Definition (Def)) then
1023 Check_For_Premature_Usage
1024 (Access_To_Subprogram_Definition (Def));
1025 else
1026 Check_For_Premature_Usage (Subtype_Mark (Def));
1027 end if;
1029 -- The only cases left are N_Access_Function_Definition and
1030 -- N_Access_Procedure_Definition.
1032 else
1033 if Present (Parameter_Specifications (Def)) then
1034 Param := First (Parameter_Specifications (Def));
1035 while Present (Param) loop
1036 Check_For_Premature_Usage (Parameter_Type (Param));
1037 Param := Next (Param);
1038 end loop;
1039 end if;
1041 if Nkind (Def) = N_Access_Function_Definition then
1042 Check_For_Premature_Usage (Result_Definition (Def));
1043 end if;
1044 end if;
1045 end Check_For_Premature_Usage;
1047 -- Local variables
1049 Formals : constant List_Id := Parameter_Specifications (T_Def);
1050 Formal : Entity_Id;
1051 D_Ityp : Node_Id;
1052 Desig_Type : constant Entity_Id :=
1053 Create_Itype (E_Subprogram_Type, Parent (T_Def));
1055 -- Start of processing for Access_Subprogram_Declaration
1057 begin
1058 Check_SPARK_05_Restriction ("access type is not allowed", T_Def);
1060 -- Associate the Itype node with the inner full-type declaration or
1061 -- subprogram spec or entry body. This is required to handle nested
1062 -- anonymous declarations. For example:
1064 -- procedure P
1065 -- (X : access procedure
1066 -- (Y : access procedure
1067 -- (Z : access T)))
1069 D_Ityp := Associated_Node_For_Itype (Desig_Type);
1070 while not (Nkind_In (D_Ityp, N_Full_Type_Declaration,
1071 N_Private_Type_Declaration,
1072 N_Private_Extension_Declaration,
1073 N_Procedure_Specification,
1074 N_Function_Specification,
1075 N_Entry_Body)
1077 or else
1078 Nkind_In (D_Ityp, N_Object_Declaration,
1079 N_Object_Renaming_Declaration,
1080 N_Formal_Object_Declaration,
1081 N_Formal_Type_Declaration,
1082 N_Task_Type_Declaration,
1083 N_Protected_Type_Declaration))
1084 loop
1085 D_Ityp := Parent (D_Ityp);
1086 pragma Assert (D_Ityp /= Empty);
1087 end loop;
1089 Set_Associated_Node_For_Itype (Desig_Type, D_Ityp);
1091 if Nkind_In (D_Ityp, N_Procedure_Specification,
1092 N_Function_Specification)
1093 then
1094 Set_Scope (Desig_Type, Scope (Defining_Entity (D_Ityp)));
1096 elsif Nkind_In (D_Ityp, N_Full_Type_Declaration,
1097 N_Object_Declaration,
1098 N_Object_Renaming_Declaration,
1099 N_Formal_Type_Declaration)
1100 then
1101 Set_Scope (Desig_Type, Scope (Defining_Identifier (D_Ityp)));
1102 end if;
1104 if Nkind (T_Def) = N_Access_Function_Definition then
1105 if Nkind (Result_Definition (T_Def)) = N_Access_Definition then
1106 declare
1107 Acc : constant Node_Id := Result_Definition (T_Def);
1109 begin
1110 if Present (Access_To_Subprogram_Definition (Acc))
1111 and then
1112 Protected_Present (Access_To_Subprogram_Definition (Acc))
1113 then
1114 Set_Etype
1115 (Desig_Type,
1116 Replace_Anonymous_Access_To_Protected_Subprogram
1117 (T_Def));
1119 else
1120 Set_Etype
1121 (Desig_Type,
1122 Access_Definition (T_Def, Result_Definition (T_Def)));
1123 end if;
1124 end;
1126 else
1127 Analyze (Result_Definition (T_Def));
1129 declare
1130 Typ : constant Entity_Id := Entity (Result_Definition (T_Def));
1132 begin
1133 -- If a null exclusion is imposed on the result type, then
1134 -- create a null-excluding itype (an access subtype) and use
1135 -- it as the function's Etype.
1137 if Is_Access_Type (Typ)
1138 and then Null_Exclusion_In_Return_Present (T_Def)
1139 then
1140 Set_Etype (Desig_Type,
1141 Create_Null_Excluding_Itype
1142 (T => Typ,
1143 Related_Nod => T_Def,
1144 Scope_Id => Current_Scope));
1146 else
1147 if From_Limited_With (Typ) then
1149 -- AI05-151: Incomplete types are allowed in all basic
1150 -- declarations, including access to subprograms.
1152 if Ada_Version >= Ada_2012 then
1153 null;
1155 else
1156 Error_Msg_NE
1157 ("illegal use of incomplete type&",
1158 Result_Definition (T_Def), Typ);
1159 end if;
1161 elsif Ekind (Current_Scope) = E_Package
1162 and then In_Private_Part (Current_Scope)
1163 then
1164 if Ekind (Typ) = E_Incomplete_Type then
1165 Append_Elmt (Desig_Type, Private_Dependents (Typ));
1167 elsif Is_Class_Wide_Type (Typ)
1168 and then Ekind (Etype (Typ)) = E_Incomplete_Type
1169 then
1170 Append_Elmt
1171 (Desig_Type, Private_Dependents (Etype (Typ)));
1172 end if;
1173 end if;
1175 Set_Etype (Desig_Type, Typ);
1176 end if;
1177 end;
1178 end if;
1180 if not (Is_Type (Etype (Desig_Type))) then
1181 Error_Msg_N
1182 ("expect type in function specification",
1183 Result_Definition (T_Def));
1184 end if;
1186 else
1187 Set_Etype (Desig_Type, Standard_Void_Type);
1188 end if;
1190 if Present (Formals) then
1191 Push_Scope (Desig_Type);
1193 -- Some special tests here. These special tests can be removed
1194 -- if and when Itypes always have proper parent pointers to their
1195 -- declarations???
1197 -- Special test 1) Link defining_identifier of formals. Required by
1198 -- First_Formal to provide its functionality.
1200 declare
1201 F : Node_Id;
1203 begin
1204 F := First (Formals);
1206 -- In ASIS mode, the access_to_subprogram may be analyzed twice,
1207 -- when it is part of an unconstrained type and subtype expansion
1208 -- is disabled. To avoid back-end problems with shared profiles,
1209 -- use previous subprogram type as the designated type, and then
1210 -- remove scope added above.
1212 if ASIS_Mode and then Present (Scope (Defining_Identifier (F)))
1213 then
1214 Set_Etype (T_Name, T_Name);
1215 Init_Size_Align (T_Name);
1216 Set_Directly_Designated_Type (T_Name,
1217 Scope (Defining_Identifier (F)));
1218 End_Scope;
1219 return;
1220 end if;
1222 while Present (F) loop
1223 if No (Parent (Defining_Identifier (F))) then
1224 Set_Parent (Defining_Identifier (F), F);
1225 end if;
1227 Next (F);
1228 end loop;
1229 end;
1231 Process_Formals (Formals, Parent (T_Def));
1233 -- Special test 2) End_Scope requires that the parent pointer be set
1234 -- to something reasonable, but Itypes don't have parent pointers. So
1235 -- we set it and then unset it ???
1237 Set_Parent (Desig_Type, T_Name);
1238 End_Scope;
1239 Set_Parent (Desig_Type, Empty);
1240 end if;
1242 -- Check for premature usage of the type being defined
1244 Check_For_Premature_Usage (T_Def);
1246 -- The return type and/or any parameter type may be incomplete. Mark the
1247 -- subprogram_type as depending on the incomplete type, so that it can
1248 -- be updated when the full type declaration is seen. This only applies
1249 -- to incomplete types declared in some enclosing scope, not to limited
1250 -- views from other packages.
1252 -- Prior to Ada 2012, access to functions can only have in_parameters.
1254 if Present (Formals) then
1255 Formal := First_Formal (Desig_Type);
1256 while Present (Formal) loop
1257 if Ekind (Formal) /= E_In_Parameter
1258 and then Nkind (T_Def) = N_Access_Function_Definition
1259 and then Ada_Version < Ada_2012
1260 then
1261 Error_Msg_N ("functions can only have IN parameters", Formal);
1262 end if;
1264 if Ekind (Etype (Formal)) = E_Incomplete_Type
1265 and then In_Open_Scopes (Scope (Etype (Formal)))
1266 then
1267 Append_Elmt (Desig_Type, Private_Dependents (Etype (Formal)));
1268 Set_Has_Delayed_Freeze (Desig_Type);
1269 end if;
1271 Next_Formal (Formal);
1272 end loop;
1273 end if;
1275 -- Check whether an indirect call without actuals may be possible. This
1276 -- is used when resolving calls whose result is then indexed.
1278 May_Need_Actuals (Desig_Type);
1280 -- If the return type is incomplete, this is legal as long as the type
1281 -- is declared in the current scope and will be completed in it (rather
1282 -- than being part of limited view).
1284 if Ekind (Etype (Desig_Type)) = E_Incomplete_Type
1285 and then not Has_Delayed_Freeze (Desig_Type)
1286 and then In_Open_Scopes (Scope (Etype (Desig_Type)))
1287 then
1288 Append_Elmt (Desig_Type, Private_Dependents (Etype (Desig_Type)));
1289 Set_Has_Delayed_Freeze (Desig_Type);
1290 end if;
1292 Check_Delayed_Subprogram (Desig_Type);
1294 if Protected_Present (T_Def) then
1295 Set_Ekind (T_Name, E_Access_Protected_Subprogram_Type);
1296 Set_Convention (Desig_Type, Convention_Protected);
1297 else
1298 Set_Ekind (T_Name, E_Access_Subprogram_Type);
1299 end if;
1301 Set_Can_Use_Internal_Rep (T_Name, not Always_Compatible_Rep_On_Target);
1303 Set_Etype (T_Name, T_Name);
1304 Init_Size_Align (T_Name);
1305 Set_Directly_Designated_Type (T_Name, Desig_Type);
1307 Generate_Reference_To_Formals (T_Name);
1309 -- Ada 2005 (AI-231): Propagate the null-excluding attribute
1311 Set_Can_Never_Be_Null (T_Name, Null_Exclusion_Present (T_Def));
1313 Check_Restriction (No_Access_Subprograms, T_Def);
1314 end Access_Subprogram_Declaration;
1316 ----------------------------
1317 -- Access_Type_Declaration --
1318 ----------------------------
1320 procedure Access_Type_Declaration (T : Entity_Id; Def : Node_Id) is
1321 P : constant Node_Id := Parent (Def);
1322 S : constant Node_Id := Subtype_Indication (Def);
1324 Full_Desig : Entity_Id;
1326 begin
1327 Check_SPARK_05_Restriction ("access type is not allowed", Def);
1329 -- Check for permissible use of incomplete type
1331 if Nkind (S) /= N_Subtype_Indication then
1332 Analyze (S);
1334 if Present (Entity (S))
1335 and then Ekind (Root_Type (Entity (S))) = E_Incomplete_Type
1336 then
1337 Set_Directly_Designated_Type (T, Entity (S));
1339 -- If the designated type is a limited view, we cannot tell if
1340 -- the full view contains tasks, and there is no way to handle
1341 -- that full view in a client. We create a master entity for the
1342 -- scope, which will be used when a client determines that one
1343 -- is needed.
1345 if From_Limited_With (Entity (S))
1346 and then not Is_Class_Wide_Type (Entity (S))
1347 then
1348 Set_Ekind (T, E_Access_Type);
1349 Build_Master_Entity (T);
1350 Build_Master_Renaming (T);
1351 end if;
1353 else
1354 Set_Directly_Designated_Type (T, Process_Subtype (S, P, T, 'P'));
1355 end if;
1357 -- If the access definition is of the form: ACCESS NOT NULL ..
1358 -- the subtype indication must be of an access type. Create
1359 -- a null-excluding subtype of it.
1361 if Null_Excluding_Subtype (Def) then
1362 if not Is_Access_Type (Entity (S)) then
1363 Error_Msg_N ("null exclusion must apply to access type", Def);
1365 else
1366 declare
1367 Loc : constant Source_Ptr := Sloc (S);
1368 Decl : Node_Id;
1369 Nam : constant Entity_Id := Make_Temporary (Loc, 'S');
1371 begin
1372 Decl :=
1373 Make_Subtype_Declaration (Loc,
1374 Defining_Identifier => Nam,
1375 Subtype_Indication =>
1376 New_Occurrence_Of (Entity (S), Loc));
1377 Set_Null_Exclusion_Present (Decl);
1378 Insert_Before (Parent (Def), Decl);
1379 Analyze (Decl);
1380 Set_Entity (S, Nam);
1381 end;
1382 end if;
1383 end if;
1385 else
1386 Set_Directly_Designated_Type (T,
1387 Process_Subtype (S, P, T, 'P'));
1388 end if;
1390 if All_Present (Def) or Constant_Present (Def) then
1391 Set_Ekind (T, E_General_Access_Type);
1392 else
1393 Set_Ekind (T, E_Access_Type);
1394 end if;
1396 Full_Desig := Designated_Type (T);
1398 if Base_Type (Full_Desig) = T then
1399 Error_Msg_N ("access type cannot designate itself", S);
1401 -- In Ada 2005, the type may have a limited view through some unit in
1402 -- its own context, allowing the following circularity that cannot be
1403 -- detected earlier.
1405 elsif Is_Class_Wide_Type (Full_Desig) and then Etype (Full_Desig) = T
1406 then
1407 Error_Msg_N
1408 ("access type cannot designate its own class-wide type", S);
1410 -- Clean up indication of tagged status to prevent cascaded errors
1412 Set_Is_Tagged_Type (T, False);
1413 end if;
1415 Set_Etype (T, T);
1417 -- If the type has appeared already in a with_type clause, it is frozen
1418 -- and the pointer size is already set. Else, initialize.
1420 if not From_Limited_With (T) then
1421 Init_Size_Align (T);
1422 end if;
1424 -- Note that Has_Task is always false, since the access type itself
1425 -- is not a task type. See Einfo for more description on this point.
1426 -- Exactly the same consideration applies to Has_Controlled_Component
1427 -- and to Has_Protected.
1429 Set_Has_Task (T, False);
1430 Set_Has_Protected (T, False);
1431 Set_Has_Timing_Event (T, False);
1432 Set_Has_Controlled_Component (T, False);
1434 -- Initialize field Finalization_Master explicitly to Empty, to avoid
1435 -- problems where an incomplete view of this entity has been previously
1436 -- established by a limited with and an overlaid version of this field
1437 -- (Stored_Constraint) was initialized for the incomplete view.
1439 -- This reset is performed in most cases except where the access type
1440 -- has been created for the purposes of allocating or deallocating a
1441 -- build-in-place object. Such access types have explicitly set pools
1442 -- and finalization masters.
1444 if No (Associated_Storage_Pool (T)) then
1445 Set_Finalization_Master (T, Empty);
1446 end if;
1448 -- Ada 2005 (AI-231): Propagate the null-excluding and access-constant
1449 -- attributes
1451 Set_Can_Never_Be_Null (T, Null_Exclusion_Present (Def));
1452 Set_Is_Access_Constant (T, Constant_Present (Def));
1453 end Access_Type_Declaration;
1455 ----------------------------------
1456 -- Add_Interface_Tag_Components --
1457 ----------------------------------
1459 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id) is
1460 Loc : constant Source_Ptr := Sloc (N);
1461 L : List_Id;
1462 Last_Tag : Node_Id;
1464 procedure Add_Tag (Iface : Entity_Id);
1465 -- Add tag for one of the progenitor interfaces
1467 -------------
1468 -- Add_Tag --
1469 -------------
1471 procedure Add_Tag (Iface : Entity_Id) is
1472 Decl : Node_Id;
1473 Def : Node_Id;
1474 Tag : Entity_Id;
1475 Offset : Entity_Id;
1477 begin
1478 pragma Assert (Is_Tagged_Type (Iface) and then Is_Interface (Iface));
1480 -- This is a reasonable place to propagate predicates
1482 if Has_Predicates (Iface) then
1483 Set_Has_Predicates (Typ);
1484 end if;
1486 Def :=
1487 Make_Component_Definition (Loc,
1488 Aliased_Present => True,
1489 Subtype_Indication =>
1490 New_Occurrence_Of (RTE (RE_Interface_Tag), Loc));
1492 Tag := Make_Temporary (Loc, 'V');
1494 Decl :=
1495 Make_Component_Declaration (Loc,
1496 Defining_Identifier => Tag,
1497 Component_Definition => Def);
1499 Analyze_Component_Declaration (Decl);
1501 Set_Analyzed (Decl);
1502 Set_Ekind (Tag, E_Component);
1503 Set_Is_Tag (Tag);
1504 Set_Is_Aliased (Tag);
1505 Set_Related_Type (Tag, Iface);
1506 Init_Component_Location (Tag);
1508 pragma Assert (Is_Frozen (Iface));
1510 Set_DT_Entry_Count (Tag,
1511 DT_Entry_Count (First_Entity (Iface)));
1513 if No (Last_Tag) then
1514 Prepend (Decl, L);
1515 else
1516 Insert_After (Last_Tag, Decl);
1517 end if;
1519 Last_Tag := Decl;
1521 -- If the ancestor has discriminants we need to give special support
1522 -- to store the offset_to_top value of the secondary dispatch tables.
1523 -- For this purpose we add a supplementary component just after the
1524 -- field that contains the tag associated with each secondary DT.
1526 if Typ /= Etype (Typ) and then Has_Discriminants (Etype (Typ)) then
1527 Def :=
1528 Make_Component_Definition (Loc,
1529 Subtype_Indication =>
1530 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
1532 Offset := Make_Temporary (Loc, 'V');
1534 Decl :=
1535 Make_Component_Declaration (Loc,
1536 Defining_Identifier => Offset,
1537 Component_Definition => Def);
1539 Analyze_Component_Declaration (Decl);
1541 Set_Analyzed (Decl);
1542 Set_Ekind (Offset, E_Component);
1543 Set_Is_Aliased (Offset);
1544 Set_Related_Type (Offset, Iface);
1545 Init_Component_Location (Offset);
1546 Insert_After (Last_Tag, Decl);
1547 Last_Tag := Decl;
1548 end if;
1549 end Add_Tag;
1551 -- Local variables
1553 Elmt : Elmt_Id;
1554 Ext : Node_Id;
1555 Comp : Node_Id;
1557 -- Start of processing for Add_Interface_Tag_Components
1559 begin
1560 if not RTE_Available (RE_Interface_Tag) then
1561 Error_Msg
1562 ("(Ada 2005) interface types not supported by this run-time!",
1563 Sloc (N));
1564 return;
1565 end if;
1567 if Ekind (Typ) /= E_Record_Type
1568 or else (Is_Concurrent_Record_Type (Typ)
1569 and then Is_Empty_List (Abstract_Interface_List (Typ)))
1570 or else (not Is_Concurrent_Record_Type (Typ)
1571 and then No (Interfaces (Typ))
1572 and then Is_Empty_Elmt_List (Interfaces (Typ)))
1573 then
1574 return;
1575 end if;
1577 -- Find the current last tag
1579 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1580 Ext := Record_Extension_Part (Type_Definition (N));
1581 else
1582 pragma Assert (Nkind (Type_Definition (N)) = N_Record_Definition);
1583 Ext := Type_Definition (N);
1584 end if;
1586 Last_Tag := Empty;
1588 if not (Present (Component_List (Ext))) then
1589 Set_Null_Present (Ext, False);
1590 L := New_List;
1591 Set_Component_List (Ext,
1592 Make_Component_List (Loc,
1593 Component_Items => L,
1594 Null_Present => False));
1595 else
1596 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1597 L := Component_Items
1598 (Component_List
1599 (Record_Extension_Part
1600 (Type_Definition (N))));
1601 else
1602 L := Component_Items
1603 (Component_List
1604 (Type_Definition (N)));
1605 end if;
1607 -- Find the last tag component
1609 Comp := First (L);
1610 while Present (Comp) loop
1611 if Nkind (Comp) = N_Component_Declaration
1612 and then Is_Tag (Defining_Identifier (Comp))
1613 then
1614 Last_Tag := Comp;
1615 end if;
1617 Next (Comp);
1618 end loop;
1619 end if;
1621 -- At this point L references the list of components and Last_Tag
1622 -- references the current last tag (if any). Now we add the tag
1623 -- corresponding with all the interfaces that are not implemented
1624 -- by the parent.
1626 if Present (Interfaces (Typ)) then
1627 Elmt := First_Elmt (Interfaces (Typ));
1628 while Present (Elmt) loop
1629 Add_Tag (Node (Elmt));
1630 Next_Elmt (Elmt);
1631 end loop;
1632 end if;
1633 end Add_Interface_Tag_Components;
1635 -------------------------------------
1636 -- Add_Internal_Interface_Entities --
1637 -------------------------------------
1639 procedure Add_Internal_Interface_Entities (Tagged_Type : Entity_Id) is
1640 Elmt : Elmt_Id;
1641 Iface : Entity_Id;
1642 Iface_Elmt : Elmt_Id;
1643 Iface_Prim : Entity_Id;
1644 Ifaces_List : Elist_Id;
1645 New_Subp : Entity_Id := Empty;
1646 Prim : Entity_Id;
1647 Restore_Scope : Boolean := False;
1649 begin
1650 pragma Assert (Ada_Version >= Ada_2005
1651 and then Is_Record_Type (Tagged_Type)
1652 and then Is_Tagged_Type (Tagged_Type)
1653 and then Has_Interfaces (Tagged_Type)
1654 and then not Is_Interface (Tagged_Type));
1656 -- Ensure that the internal entities are added to the scope of the type
1658 if Scope (Tagged_Type) /= Current_Scope then
1659 Push_Scope (Scope (Tagged_Type));
1660 Restore_Scope := True;
1661 end if;
1663 Collect_Interfaces (Tagged_Type, Ifaces_List);
1665 Iface_Elmt := First_Elmt (Ifaces_List);
1666 while Present (Iface_Elmt) loop
1667 Iface := Node (Iface_Elmt);
1669 -- Originally we excluded here from this processing interfaces that
1670 -- are parents of Tagged_Type because their primitives are located
1671 -- in the primary dispatch table (and hence no auxiliary internal
1672 -- entities are required to handle secondary dispatch tables in such
1673 -- case). However, these auxiliary entities are also required to
1674 -- handle derivations of interfaces in formals of generics (see
1675 -- Derive_Subprograms).
1677 Elmt := First_Elmt (Primitive_Operations (Iface));
1678 while Present (Elmt) loop
1679 Iface_Prim := Node (Elmt);
1681 if not Is_Predefined_Dispatching_Operation (Iface_Prim) then
1682 Prim :=
1683 Find_Primitive_Covering_Interface
1684 (Tagged_Type => Tagged_Type,
1685 Iface_Prim => Iface_Prim);
1687 if No (Prim) and then Serious_Errors_Detected > 0 then
1688 goto Continue;
1689 end if;
1691 pragma Assert (Present (Prim));
1693 -- Ada 2012 (AI05-0197): If the name of the covering primitive
1694 -- differs from the name of the interface primitive then it is
1695 -- a private primitive inherited from a parent type. In such
1696 -- case, given that Tagged_Type covers the interface, the
1697 -- inherited private primitive becomes visible. For such
1698 -- purpose we add a new entity that renames the inherited
1699 -- private primitive.
1701 if Chars (Prim) /= Chars (Iface_Prim) then
1702 pragma Assert (Has_Suffix (Prim, 'P'));
1703 Derive_Subprogram
1704 (New_Subp => New_Subp,
1705 Parent_Subp => Iface_Prim,
1706 Derived_Type => Tagged_Type,
1707 Parent_Type => Iface);
1708 Set_Alias (New_Subp, Prim);
1709 Set_Is_Abstract_Subprogram
1710 (New_Subp, Is_Abstract_Subprogram (Prim));
1711 end if;
1713 Derive_Subprogram
1714 (New_Subp => New_Subp,
1715 Parent_Subp => Iface_Prim,
1716 Derived_Type => Tagged_Type,
1717 Parent_Type => Iface);
1719 declare
1720 Anc : Entity_Id;
1721 begin
1722 if Is_Inherited_Operation (Prim)
1723 and then Present (Alias (Prim))
1724 then
1725 Anc := Alias (Prim);
1726 else
1727 Anc := Overridden_Operation (Prim);
1728 end if;
1730 -- Apply legality checks in RM 6.1.1 (10-13) concerning
1731 -- nonconforming preconditions in both an ancestor and
1732 -- a progenitor operation.
1734 if Present (Anc)
1735 and then Has_Non_Trivial_Precondition (Anc)
1736 and then Has_Non_Trivial_Precondition (Iface_Prim)
1737 then
1738 if Is_Abstract_Subprogram (Prim)
1739 or else
1740 (Ekind (Prim) = E_Procedure
1741 and then Nkind (Parent (Prim)) =
1742 N_Procedure_Specification
1743 and then Null_Present (Parent (Prim)))
1744 then
1745 null;
1747 -- The inherited operation must be overridden
1749 elsif not Comes_From_Source (Prim) then
1750 Error_Msg_NE
1751 ("&inherits non-conforming preconditions and must "
1752 & "be overridden (RM 6.1.1 (10-16)",
1753 Parent (Tagged_Type), Prim);
1754 end if;
1755 end if;
1756 end;
1758 -- Ada 2005 (AI-251): Decorate internal entity Iface_Subp
1759 -- associated with interface types. These entities are
1760 -- only registered in the list of primitives of its
1761 -- corresponding tagged type because they are only used
1762 -- to fill the contents of the secondary dispatch tables.
1763 -- Therefore they are removed from the homonym chains.
1765 Set_Is_Hidden (New_Subp);
1766 Set_Is_Internal (New_Subp);
1767 Set_Alias (New_Subp, Prim);
1768 Set_Is_Abstract_Subprogram
1769 (New_Subp, Is_Abstract_Subprogram (Prim));
1770 Set_Interface_Alias (New_Subp, Iface_Prim);
1772 -- If the returned type is an interface then propagate it to
1773 -- the returned type. Needed by the thunk to generate the code
1774 -- which displaces "this" to reference the corresponding
1775 -- secondary dispatch table in the returned object.
1777 if Is_Interface (Etype (Iface_Prim)) then
1778 Set_Etype (New_Subp, Etype (Iface_Prim));
1779 end if;
1781 -- Internal entities associated with interface types are only
1782 -- registered in the list of primitives of the tagged type.
1783 -- They are only used to fill the contents of the secondary
1784 -- dispatch tables. Therefore they are not needed in the
1785 -- homonym chains.
1787 Remove_Homonym (New_Subp);
1789 -- Hidden entities associated with interfaces must have set
1790 -- the Has_Delay_Freeze attribute to ensure that, in case
1791 -- of locally defined tagged types (or compiling with static
1792 -- dispatch tables generation disabled) the corresponding
1793 -- entry of the secondary dispatch table is filled when such
1794 -- an entity is frozen. This is an expansion activity that must
1795 -- be suppressed for ASIS because it leads to gigi elaboration
1796 -- issues in annotate mode.
1798 if not ASIS_Mode then
1799 Set_Has_Delayed_Freeze (New_Subp);
1800 end if;
1801 end if;
1803 <<Continue>>
1804 Next_Elmt (Elmt);
1805 end loop;
1807 Next_Elmt (Iface_Elmt);
1808 end loop;
1810 if Restore_Scope then
1811 Pop_Scope;
1812 end if;
1813 end Add_Internal_Interface_Entities;
1815 -----------------------------------
1816 -- Analyze_Component_Declaration --
1817 -----------------------------------
1819 procedure Analyze_Component_Declaration (N : Node_Id) is
1820 Loc : constant Source_Ptr := Sloc (Component_Definition (N));
1821 Id : constant Entity_Id := Defining_Identifier (N);
1822 E : constant Node_Id := Expression (N);
1823 Typ : constant Node_Id :=
1824 Subtype_Indication (Component_Definition (N));
1825 T : Entity_Id;
1826 P : Entity_Id;
1828 function Contains_POC (Constr : Node_Id) return Boolean;
1829 -- Determines whether a constraint uses the discriminant of a record
1830 -- type thus becoming a per-object constraint (POC).
1832 function Is_Known_Limited (Typ : Entity_Id) return Boolean;
1833 -- Typ is the type of the current component, check whether this type is
1834 -- a limited type. Used to validate declaration against that of
1835 -- enclosing record.
1837 ------------------
1838 -- Contains_POC --
1839 ------------------
1841 function Contains_POC (Constr : Node_Id) return Boolean is
1842 begin
1843 -- Prevent cascaded errors
1845 if Error_Posted (Constr) then
1846 return False;
1847 end if;
1849 case Nkind (Constr) is
1850 when N_Attribute_Reference =>
1851 return Attribute_Name (Constr) = Name_Access
1852 and then Prefix (Constr) = Scope (Entity (Prefix (Constr)));
1854 when N_Discriminant_Association =>
1855 return Denotes_Discriminant (Expression (Constr));
1857 when N_Identifier =>
1858 return Denotes_Discriminant (Constr);
1860 when N_Index_Or_Discriminant_Constraint =>
1861 declare
1862 IDC : Node_Id;
1864 begin
1865 IDC := First (Constraints (Constr));
1866 while Present (IDC) loop
1868 -- One per-object constraint is sufficient
1870 if Contains_POC (IDC) then
1871 return True;
1872 end if;
1874 Next (IDC);
1875 end loop;
1877 return False;
1878 end;
1880 when N_Range =>
1881 return Denotes_Discriminant (Low_Bound (Constr))
1882 or else
1883 Denotes_Discriminant (High_Bound (Constr));
1885 when N_Range_Constraint =>
1886 return Denotes_Discriminant (Range_Expression (Constr));
1888 when others =>
1889 return False;
1890 end case;
1891 end Contains_POC;
1893 ----------------------
1894 -- Is_Known_Limited --
1895 ----------------------
1897 function Is_Known_Limited (Typ : Entity_Id) return Boolean is
1898 P : constant Entity_Id := Etype (Typ);
1899 R : constant Entity_Id := Root_Type (Typ);
1901 begin
1902 if Is_Limited_Record (Typ) then
1903 return True;
1905 -- If the root type is limited (and not a limited interface)
1906 -- so is the current type
1908 elsif Is_Limited_Record (R)
1909 and then (not Is_Interface (R) or else not Is_Limited_Interface (R))
1910 then
1911 return True;
1913 -- Else the type may have a limited interface progenitor, but a
1914 -- limited record parent.
1916 elsif R /= P and then Is_Limited_Record (P) then
1917 return True;
1919 else
1920 return False;
1921 end if;
1922 end Is_Known_Limited;
1924 -- Start of processing for Analyze_Component_Declaration
1926 begin
1927 Generate_Definition (Id);
1928 Enter_Name (Id);
1930 if Present (Typ) then
1931 T := Find_Type_Of_Object
1932 (Subtype_Indication (Component_Definition (N)), N);
1934 if not Nkind_In (Typ, N_Identifier, N_Expanded_Name) then
1935 Check_SPARK_05_Restriction ("subtype mark required", Typ);
1936 end if;
1938 -- Ada 2005 (AI-230): Access Definition case
1940 else
1941 pragma Assert (Present
1942 (Access_Definition (Component_Definition (N))));
1944 T := Access_Definition
1945 (Related_Nod => N,
1946 N => Access_Definition (Component_Definition (N)));
1947 Set_Is_Local_Anonymous_Access (T);
1949 -- Ada 2005 (AI-254)
1951 if Present (Access_To_Subprogram_Definition
1952 (Access_Definition (Component_Definition (N))))
1953 and then Protected_Present (Access_To_Subprogram_Definition
1954 (Access_Definition
1955 (Component_Definition (N))))
1956 then
1957 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1958 end if;
1959 end if;
1961 -- If the subtype is a constrained subtype of the enclosing record,
1962 -- (which must have a partial view) the back-end does not properly
1963 -- handle the recursion. Rewrite the component declaration with an
1964 -- explicit subtype indication, which is acceptable to Gigi. We can copy
1965 -- the tree directly because side effects have already been removed from
1966 -- discriminant constraints.
1968 if Ekind (T) = E_Access_Subtype
1969 and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
1970 and then Comes_From_Source (T)
1971 and then Nkind (Parent (T)) = N_Subtype_Declaration
1972 and then Etype (Directly_Designated_Type (T)) = Current_Scope
1973 then
1974 Rewrite
1975 (Subtype_Indication (Component_Definition (N)),
1976 New_Copy_Tree (Subtype_Indication (Parent (T))));
1977 T := Find_Type_Of_Object
1978 (Subtype_Indication (Component_Definition (N)), N);
1979 end if;
1981 -- If the component declaration includes a default expression, then we
1982 -- check that the component is not of a limited type (RM 3.7(5)),
1983 -- and do the special preanalysis of the expression (see section on
1984 -- "Handling of Default and Per-Object Expressions" in the spec of
1985 -- package Sem).
1987 if Present (E) then
1988 Check_SPARK_05_Restriction ("default expression is not allowed", E);
1989 Preanalyze_Default_Expression (E, T);
1990 Check_Initialization (T, E);
1992 if Ada_Version >= Ada_2005
1993 and then Ekind (T) = E_Anonymous_Access_Type
1994 and then Etype (E) /= Any_Type
1995 then
1996 -- Check RM 3.9.2(9): "if the expected type for an expression is
1997 -- an anonymous access-to-specific tagged type, then the object
1998 -- designated by the expression shall not be dynamically tagged
1999 -- unless it is a controlling operand in a call on a dispatching
2000 -- operation"
2002 if Is_Tagged_Type (Directly_Designated_Type (T))
2003 and then
2004 Ekind (Directly_Designated_Type (T)) /= E_Class_Wide_Type
2005 and then
2006 Ekind (Directly_Designated_Type (Etype (E))) =
2007 E_Class_Wide_Type
2008 then
2009 Error_Msg_N
2010 ("access to specific tagged type required (RM 3.9.2(9))", E);
2011 end if;
2013 -- (Ada 2005: AI-230): Accessibility check for anonymous
2014 -- components
2016 if Type_Access_Level (Etype (E)) >
2017 Deepest_Type_Access_Level (T)
2018 then
2019 Error_Msg_N
2020 ("expression has deeper access level than component " &
2021 "(RM 3.10.2 (12.2))", E);
2022 end if;
2024 -- The initialization expression is a reference to an access
2025 -- discriminant. The type of the discriminant is always deeper
2026 -- than any access type.
2028 if Ekind (Etype (E)) = E_Anonymous_Access_Type
2029 and then Is_Entity_Name (E)
2030 and then Ekind (Entity (E)) = E_In_Parameter
2031 and then Present (Discriminal_Link (Entity (E)))
2032 then
2033 Error_Msg_N
2034 ("discriminant has deeper accessibility level than target",
2036 end if;
2037 end if;
2038 end if;
2040 -- The parent type may be a private view with unknown discriminants,
2041 -- and thus unconstrained. Regular components must be constrained.
2043 if not Is_Definite_Subtype (T) and then Chars (Id) /= Name_uParent then
2044 if Is_Class_Wide_Type (T) then
2045 Error_Msg_N
2046 ("class-wide subtype with unknown discriminants" &
2047 " in component declaration",
2048 Subtype_Indication (Component_Definition (N)));
2049 else
2050 Error_Msg_N
2051 ("unconstrained subtype in component declaration",
2052 Subtype_Indication (Component_Definition (N)));
2053 end if;
2055 -- Components cannot be abstract, except for the special case of
2056 -- the _Parent field (case of extending an abstract tagged type)
2058 elsif Is_Abstract_Type (T) and then Chars (Id) /= Name_uParent then
2059 Error_Msg_N ("type of a component cannot be abstract", N);
2060 end if;
2062 Set_Etype (Id, T);
2063 Set_Is_Aliased (Id, Aliased_Present (Component_Definition (N)));
2065 -- The component declaration may have a per-object constraint, set
2066 -- the appropriate flag in the defining identifier of the subtype.
2068 if Present (Subtype_Indication (Component_Definition (N))) then
2069 declare
2070 Sindic : constant Node_Id :=
2071 Subtype_Indication (Component_Definition (N));
2072 begin
2073 if Nkind (Sindic) = N_Subtype_Indication
2074 and then Present (Constraint (Sindic))
2075 and then Contains_POC (Constraint (Sindic))
2076 then
2077 Set_Has_Per_Object_Constraint (Id);
2078 end if;
2079 end;
2080 end if;
2082 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
2083 -- out some static checks.
2085 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (T) then
2086 Null_Exclusion_Static_Checks (N);
2087 end if;
2089 -- If this component is private (or depends on a private type), flag the
2090 -- record type to indicate that some operations are not available.
2092 P := Private_Component (T);
2094 if Present (P) then
2096 -- Check for circular definitions
2098 if P = Any_Type then
2099 Set_Etype (Id, Any_Type);
2101 -- There is a gap in the visibility of operations only if the
2102 -- component type is not defined in the scope of the record type.
2104 elsif Scope (P) = Scope (Current_Scope) then
2105 null;
2107 elsif Is_Limited_Type (P) then
2108 Set_Is_Limited_Composite (Current_Scope);
2110 else
2111 Set_Is_Private_Composite (Current_Scope);
2112 end if;
2113 end if;
2115 if P /= Any_Type
2116 and then Is_Limited_Type (T)
2117 and then Chars (Id) /= Name_uParent
2118 and then Is_Tagged_Type (Current_Scope)
2119 then
2120 if Is_Derived_Type (Current_Scope)
2121 and then not Is_Known_Limited (Current_Scope)
2122 then
2123 Error_Msg_N
2124 ("extension of nonlimited type cannot have limited components",
2127 if Is_Interface (Root_Type (Current_Scope)) then
2128 Error_Msg_N
2129 ("\limitedness is not inherited from limited interface", N);
2130 Error_Msg_N ("\add LIMITED to type indication", N);
2131 end if;
2133 Explain_Limited_Type (T, N);
2134 Set_Etype (Id, Any_Type);
2135 Set_Is_Limited_Composite (Current_Scope, False);
2137 elsif not Is_Derived_Type (Current_Scope)
2138 and then not Is_Limited_Record (Current_Scope)
2139 and then not Is_Concurrent_Type (Current_Scope)
2140 then
2141 Error_Msg_N
2142 ("nonlimited tagged type cannot have limited components", N);
2143 Explain_Limited_Type (T, N);
2144 Set_Etype (Id, Any_Type);
2145 Set_Is_Limited_Composite (Current_Scope, False);
2146 end if;
2147 end if;
2149 -- If the component is an unconstrained task or protected type with
2150 -- discriminants, the component and the enclosing record are limited
2151 -- and the component is constrained by its default values. Compute
2152 -- its actual subtype, else it may be allocated the maximum size by
2153 -- the backend, and possibly overflow.
2155 if Is_Concurrent_Type (T)
2156 and then not Is_Constrained (T)
2157 and then Has_Discriminants (T)
2158 and then not Has_Discriminants (Current_Scope)
2159 then
2160 declare
2161 Act_T : constant Entity_Id := Build_Default_Subtype (T, N);
2163 begin
2164 Set_Etype (Id, Act_T);
2166 -- Rewrite component definition to use the constrained subtype
2168 Rewrite (Component_Definition (N),
2169 Make_Component_Definition (Loc,
2170 Subtype_Indication => New_Occurrence_Of (Act_T, Loc)));
2171 end;
2172 end if;
2174 Set_Original_Record_Component (Id, Id);
2176 if Has_Aspects (N) then
2177 Analyze_Aspect_Specifications (N, Id);
2178 end if;
2180 Analyze_Dimension (N);
2181 end Analyze_Component_Declaration;
2183 --------------------------
2184 -- Analyze_Declarations --
2185 --------------------------
2187 procedure Analyze_Declarations (L : List_Id) is
2188 Decl : Node_Id;
2190 procedure Adjust_Decl;
2191 -- Adjust Decl not to include implicit label declarations, since these
2192 -- have strange Sloc values that result in elaboration check problems.
2193 -- (They have the sloc of the label as found in the source, and that
2194 -- is ahead of the current declarative part).
2196 procedure Build_Assertion_Bodies (Decls : List_Id; Context : Node_Id);
2197 -- Create the subprogram bodies which verify the run-time semantics of
2198 -- the pragmas listed below for each elibigle type found in declarative
2199 -- list Decls. The pragmas are:
2201 -- Default_Initial_Condition
2202 -- Invariant
2203 -- Type_Invariant
2205 -- Context denotes the owner of the declarative list.
2207 procedure Check_Entry_Contracts;
2208 -- Perform a pre-analysis of the pre- and postconditions of an entry
2209 -- declaration. This must be done before full resolution and creation
2210 -- of the parameter block, etc. to catch illegal uses within the
2211 -- contract expression. Full analysis of the expression is done when
2212 -- the contract is processed.
2214 function Contains_Lib_Incomplete_Type (Pkg : Entity_Id) return Boolean;
2215 -- Check if a nested package has entities within it that rely on library
2216 -- level private types where the full view has not been completed for
2217 -- the purposes of checking if it is acceptable to freeze an expression
2218 -- function at the point of declaration.
2220 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id);
2221 -- Determine whether Body_Decl denotes the body of a late controlled
2222 -- primitive (either Initialize, Adjust or Finalize). If this is the
2223 -- case, add a proper spec if the body lacks one. The spec is inserted
2224 -- before Body_Decl and immediately analyzed.
2226 procedure Remove_Partial_Visible_Refinements (Spec_Id : Entity_Id);
2227 -- Spec_Id is the entity of a package that may define abstract states,
2228 -- and in the case of a child unit, whose ancestors may define abstract
2229 -- states. If the states have partial visible refinement, remove the
2230 -- partial visibility of each constituent at the end of the package
2231 -- spec and body declarations.
2233 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id);
2234 -- Spec_Id is the entity of a package that may define abstract states.
2235 -- If the states have visible refinement, remove the visibility of each
2236 -- constituent at the end of the package body declaration.
2238 procedure Resolve_Aspects;
2239 -- Utility to resolve the expressions of aspects at the end of a list of
2240 -- declarations, or before a declaration that freezes previous entities,
2241 -- such as in a subprogram body.
2243 -----------------
2244 -- Adjust_Decl --
2245 -----------------
2247 procedure Adjust_Decl is
2248 begin
2249 while Present (Prev (Decl))
2250 and then Nkind (Decl) = N_Implicit_Label_Declaration
2251 loop
2252 Prev (Decl);
2253 end loop;
2254 end Adjust_Decl;
2256 ----------------------------
2257 -- Build_Assertion_Bodies --
2258 ----------------------------
2260 procedure Build_Assertion_Bodies (Decls : List_Id; Context : Node_Id) is
2261 procedure Build_Assertion_Bodies_For_Type (Typ : Entity_Id);
2262 -- Create the subprogram bodies which verify the run-time semantics
2263 -- of the pragmas listed below for type Typ. The pragmas are:
2265 -- Default_Initial_Condition
2266 -- Invariant
2267 -- Type_Invariant
2269 -------------------------------------
2270 -- Build_Assertion_Bodies_For_Type --
2271 -------------------------------------
2273 procedure Build_Assertion_Bodies_For_Type (Typ : Entity_Id) is
2274 begin
2275 -- Preanalyze and resolve the Default_Initial_Condition assertion
2276 -- expression at the end of the declarations to catch any errors.
2278 if Has_DIC (Typ) then
2279 Build_DIC_Procedure_Body (Typ);
2280 end if;
2282 if Nkind (Context) = N_Package_Specification then
2284 -- Preanalyze and resolve the class-wide invariants of an
2285 -- interface at the end of whichever declarative part has the
2286 -- interface type. Note that an interface may be declared in
2287 -- any non-package declarative part, but reaching the end of
2288 -- such a declarative part will always freeze the type and
2289 -- generate the invariant procedure (see Freeze_Type).
2291 if Is_Interface (Typ) then
2293 -- Interfaces are treated as the partial view of a private
2294 -- type, in order to achieve uniformity with the general
2295 -- case. As a result, an interface receives only a "partial"
2296 -- invariant procedure, which is never called.
2298 if Has_Own_Invariants (Typ) then
2299 Build_Invariant_Procedure_Body
2300 (Typ => Typ,
2301 Partial_Invariant => True);
2302 end if;
2304 -- Preanalyze and resolve the invariants of a private type
2305 -- at the end of the visible declarations to catch potential
2306 -- errors. Inherited class-wide invariants are not included
2307 -- because they have already been resolved.
2309 elsif Decls = Visible_Declarations (Context)
2310 and then Ekind_In (Typ, E_Limited_Private_Type,
2311 E_Private_Type,
2312 E_Record_Type_With_Private)
2313 and then Has_Own_Invariants (Typ)
2314 then
2315 Build_Invariant_Procedure_Body
2316 (Typ => Typ,
2317 Partial_Invariant => True);
2319 -- Preanalyze and resolve the invariants of a private type's
2320 -- full view at the end of the private declarations to catch
2321 -- potential errors.
2323 elsif Decls = Private_Declarations (Context)
2324 and then not Is_Private_Type (Typ)
2325 and then Has_Private_Declaration (Typ)
2326 and then Has_Invariants (Typ)
2327 then
2328 Build_Invariant_Procedure_Body (Typ);
2329 end if;
2330 end if;
2331 end Build_Assertion_Bodies_For_Type;
2333 -- Local variables
2335 Decl : Node_Id;
2336 Decl_Id : Entity_Id;
2338 -- Start of processing for Build_Assertion_Bodies
2340 begin
2341 Decl := First (Decls);
2342 while Present (Decl) loop
2343 if Is_Declaration (Decl) then
2344 Decl_Id := Defining_Entity (Decl);
2346 if Is_Type (Decl_Id) then
2347 Build_Assertion_Bodies_For_Type (Decl_Id);
2348 end if;
2349 end if;
2351 Next (Decl);
2352 end loop;
2353 end Build_Assertion_Bodies;
2355 ---------------------------
2356 -- Check_Entry_Contracts --
2357 ---------------------------
2359 procedure Check_Entry_Contracts is
2360 ASN : Node_Id;
2361 Ent : Entity_Id;
2362 Exp : Node_Id;
2364 begin
2365 Ent := First_Entity (Current_Scope);
2366 while Present (Ent) loop
2368 -- This only concerns entries with pre/postconditions
2370 if Ekind (Ent) = E_Entry
2371 and then Present (Contract (Ent))
2372 and then Present (Pre_Post_Conditions (Contract (Ent)))
2373 then
2374 ASN := Pre_Post_Conditions (Contract (Ent));
2375 Push_Scope (Ent);
2376 Install_Formals (Ent);
2378 -- Pre/postconditions are rewritten as Check pragmas. Analysis
2379 -- is performed on a copy of the pragma expression, to prevent
2380 -- modifying the original expression.
2382 while Present (ASN) loop
2383 if Nkind (ASN) = N_Pragma then
2384 Exp :=
2385 New_Copy_Tree
2386 (Expression
2387 (First (Pragma_Argument_Associations (ASN))));
2388 Set_Parent (Exp, ASN);
2390 Preanalyze_Assert_Expression (Exp, Standard_Boolean);
2391 end if;
2393 ASN := Next_Pragma (ASN);
2394 end loop;
2396 End_Scope;
2397 end if;
2399 Next_Entity (Ent);
2400 end loop;
2401 end Check_Entry_Contracts;
2403 ----------------------------------
2404 -- Contains_Lib_Incomplete_Type --
2405 ----------------------------------
2407 function Contains_Lib_Incomplete_Type (Pkg : Entity_Id) return Boolean is
2408 Curr : Entity_Id;
2410 begin
2411 -- Avoid looking through scopes that do not meet the precondition of
2412 -- Pkg not being within a library unit spec.
2414 if not Is_Compilation_Unit (Pkg)
2415 and then not Is_Generic_Instance (Pkg)
2416 and then not In_Package_Body (Enclosing_Lib_Unit_Entity (Pkg))
2417 then
2418 -- Loop through all entities in the current scope to identify
2419 -- an entity that depends on a private type.
2421 Curr := First_Entity (Pkg);
2422 loop
2423 if Nkind (Curr) in N_Entity
2424 and then Depends_On_Private (Curr)
2425 then
2426 return True;
2427 end if;
2429 exit when Last_Entity (Current_Scope) = Curr;
2430 Curr := Next_Entity (Curr);
2431 end loop;
2432 end if;
2434 return False;
2435 end Contains_Lib_Incomplete_Type;
2437 --------------------------------------
2438 -- Handle_Late_Controlled_Primitive --
2439 --------------------------------------
2441 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id) is
2442 Body_Spec : constant Node_Id := Specification (Body_Decl);
2443 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2444 Loc : constant Source_Ptr := Sloc (Body_Id);
2445 Params : constant List_Id :=
2446 Parameter_Specifications (Body_Spec);
2447 Spec : Node_Id;
2448 Spec_Id : Entity_Id;
2449 Typ : Node_Id;
2451 begin
2452 -- Consider only procedure bodies whose name matches one of the three
2453 -- controlled primitives.
2455 if Nkind (Body_Spec) /= N_Procedure_Specification
2456 or else not Nam_In (Chars (Body_Id), Name_Adjust,
2457 Name_Finalize,
2458 Name_Initialize)
2459 then
2460 return;
2462 -- A controlled primitive must have exactly one formal which is not
2463 -- an anonymous access type.
2465 elsif List_Length (Params) /= 1 then
2466 return;
2467 end if;
2469 Typ := Parameter_Type (First (Params));
2471 if Nkind (Typ) = N_Access_Definition then
2472 return;
2473 end if;
2475 Find_Type (Typ);
2477 -- The type of the formal must be derived from [Limited_]Controlled
2479 if not Is_Controlled (Entity (Typ)) then
2480 return;
2481 end if;
2483 -- Check whether a specification exists for this body. We do not
2484 -- analyze the spec of the body in full, because it will be analyzed
2485 -- again when the body is properly analyzed, and we cannot create
2486 -- duplicate entries in the formals chain. We look for an explicit
2487 -- specification because the body may be an overriding operation and
2488 -- an inherited spec may be present.
2490 Spec_Id := Current_Entity (Body_Id);
2492 while Present (Spec_Id) loop
2493 if Ekind_In (Spec_Id, E_Procedure, E_Generic_Procedure)
2494 and then Scope (Spec_Id) = Current_Scope
2495 and then Present (First_Formal (Spec_Id))
2496 and then No (Next_Formal (First_Formal (Spec_Id)))
2497 and then Etype (First_Formal (Spec_Id)) = Entity (Typ)
2498 and then Comes_From_Source (Spec_Id)
2499 then
2500 return;
2501 end if;
2503 Spec_Id := Homonym (Spec_Id);
2504 end loop;
2506 -- At this point the body is known to be a late controlled primitive.
2507 -- Generate a matching spec and insert it before the body. Note the
2508 -- use of Copy_Separate_Tree - we want an entirely separate semantic
2509 -- tree in this case.
2511 Spec := Copy_Separate_Tree (Body_Spec);
2513 -- Ensure that the subprogram declaration does not inherit the null
2514 -- indicator from the body as we now have a proper spec/body pair.
2516 Set_Null_Present (Spec, False);
2518 -- Ensure that the freeze node is inserted after the declaration of
2519 -- the primitive since its expansion will freeze the primitive.
2521 Decl := Make_Subprogram_Declaration (Loc, Specification => Spec);
2523 Insert_Before_And_Analyze (Body_Decl, Decl);
2524 end Handle_Late_Controlled_Primitive;
2526 ----------------------------------------
2527 -- Remove_Partial_Visible_Refinements --
2528 ----------------------------------------
2530 procedure Remove_Partial_Visible_Refinements (Spec_Id : Entity_Id) is
2531 State_Elmt : Elmt_Id;
2532 begin
2533 if Present (Abstract_States (Spec_Id)) then
2534 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2535 while Present (State_Elmt) loop
2536 Set_Has_Partial_Visible_Refinement (Node (State_Elmt), False);
2537 Next_Elmt (State_Elmt);
2538 end loop;
2539 end if;
2541 -- For a child unit, also hide the partial state refinement from
2542 -- ancestor packages.
2544 if Is_Child_Unit (Spec_Id) then
2545 Remove_Partial_Visible_Refinements (Scope (Spec_Id));
2546 end if;
2547 end Remove_Partial_Visible_Refinements;
2549 --------------------------------
2550 -- Remove_Visible_Refinements --
2551 --------------------------------
2553 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id) is
2554 State_Elmt : Elmt_Id;
2555 begin
2556 if Present (Abstract_States (Spec_Id)) then
2557 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2558 while Present (State_Elmt) loop
2559 Set_Has_Visible_Refinement (Node (State_Elmt), False);
2560 Next_Elmt (State_Elmt);
2561 end loop;
2562 end if;
2563 end Remove_Visible_Refinements;
2565 ---------------------
2566 -- Resolve_Aspects --
2567 ---------------------
2569 procedure Resolve_Aspects is
2570 E : Entity_Id;
2572 begin
2573 E := First_Entity (Current_Scope);
2574 while Present (E) loop
2575 Resolve_Aspect_Expressions (E);
2576 Next_Entity (E);
2577 end loop;
2578 end Resolve_Aspects;
2580 -- Local variables
2582 Context : Node_Id := Empty;
2583 Freeze_From : Entity_Id := Empty;
2584 Next_Decl : Node_Id;
2586 Body_Seen : Boolean := False;
2587 -- Flag set when the first body [stub] is encountered
2589 -- Start of processing for Analyze_Declarations
2591 begin
2592 if Restriction_Check_Required (SPARK_05) then
2593 Check_Later_Vs_Basic_Declarations (L, During_Parsing => False);
2594 end if;
2596 Decl := First (L);
2597 while Present (Decl) loop
2599 -- Package spec cannot contain a package declaration in SPARK
2601 if Nkind (Decl) = N_Package_Declaration
2602 and then Nkind (Parent (L)) = N_Package_Specification
2603 then
2604 Check_SPARK_05_Restriction
2605 ("package specification cannot contain a package declaration",
2606 Decl);
2607 end if;
2609 -- Complete analysis of declaration
2611 Analyze (Decl);
2612 Next_Decl := Next (Decl);
2614 if No (Freeze_From) then
2615 Freeze_From := First_Entity (Current_Scope);
2616 end if;
2618 -- At the end of a declarative part, freeze remaining entities
2619 -- declared in it. The end of the visible declarations of package
2620 -- specification is not the end of a declarative part if private
2621 -- declarations are present. The end of a package declaration is a
2622 -- freezing point only if it a library package. A task definition or
2623 -- protected type definition is not a freeze point either. Finally,
2624 -- we do not freeze entities in generic scopes, because there is no
2625 -- code generated for them and freeze nodes will be generated for
2626 -- the instance.
2628 -- The end of a package instantiation is not a freeze point, but
2629 -- for now we make it one, because the generic body is inserted
2630 -- (currently) immediately after. Generic instantiations will not
2631 -- be a freeze point once delayed freezing of bodies is implemented.
2632 -- (This is needed in any case for early instantiations ???).
2634 if No (Next_Decl) then
2635 if Nkind (Parent (L)) = N_Component_List then
2636 null;
2638 elsif Nkind_In (Parent (L), N_Protected_Definition,
2639 N_Task_Definition)
2640 then
2641 Check_Entry_Contracts;
2643 elsif Nkind (Parent (L)) /= N_Package_Specification then
2644 if Nkind (Parent (L)) = N_Package_Body then
2645 Freeze_From := First_Entity (Current_Scope);
2646 end if;
2648 -- There may have been several freezing points previously,
2649 -- for example object declarations or subprogram bodies, but
2650 -- at the end of a declarative part we check freezing from
2651 -- the beginning, even though entities may already be frozen,
2652 -- in order to perform visibility checks on delayed aspects.
2654 Adjust_Decl;
2656 -- If the current scope is a generic subprogram body. Skip the
2657 -- generic formal parameters that are not frozen here.
2659 if Is_Subprogram (Current_Scope)
2660 and then Nkind (Unit_Declaration_Node (Current_Scope)) =
2661 N_Generic_Subprogram_Declaration
2662 and then Present (First_Entity (Current_Scope))
2663 then
2664 while Is_Generic_Formal (Freeze_From) loop
2665 Freeze_From := Next_Entity (Freeze_From);
2666 end loop;
2668 Freeze_All (Freeze_From, Decl);
2669 Freeze_From := Last_Entity (Current_Scope);
2671 else
2672 -- For declarations in a subprogram body there is no issue
2673 -- with name resolution in aspect specifications, but in
2674 -- ASIS mode we need to preanalyze aspect specifications
2675 -- that may otherwise only be analyzed during expansion
2676 -- (e.g. during generation of a related subprogram).
2678 if ASIS_Mode then
2679 Resolve_Aspects;
2680 end if;
2682 Freeze_All (First_Entity (Current_Scope), Decl);
2683 Freeze_From := Last_Entity (Current_Scope);
2684 end if;
2686 -- Current scope is a package specification
2688 elsif Scope (Current_Scope) /= Standard_Standard
2689 and then not Is_Child_Unit (Current_Scope)
2690 and then No (Generic_Parent (Parent (L)))
2691 then
2692 -- ARM rule 13.1.1(11/3): usage names in aspect definitions are
2693 -- resolved at the end of the immediately enclosing declaration
2694 -- list (AI05-0183-1).
2696 Resolve_Aspects;
2698 elsif L /= Visible_Declarations (Parent (L))
2699 or else No (Private_Declarations (Parent (L)))
2700 or else Is_Empty_List (Private_Declarations (Parent (L)))
2701 then
2702 Adjust_Decl;
2704 -- End of a package declaration
2706 -- In compilation mode the expansion of freeze node takes care
2707 -- of resolving expressions of all aspects in the list. In ASIS
2708 -- mode this must be done explicitly.
2710 if ASIS_Mode
2711 and then Scope (Current_Scope) = Standard_Standard
2712 then
2713 Resolve_Aspects;
2714 end if;
2716 -- This is a freeze point because it is the end of a
2717 -- compilation unit.
2719 Freeze_All (First_Entity (Current_Scope), Decl);
2720 Freeze_From := Last_Entity (Current_Scope);
2722 -- At the end of the visible declarations the expressions in
2723 -- aspects of all entities declared so far must be resolved.
2724 -- The entities themselves might be frozen later, and the
2725 -- generated pragmas and attribute definition clauses analyzed
2726 -- in full at that point, but name resolution must take place
2727 -- now.
2728 -- In addition to being the proper semantics, this is mandatory
2729 -- within generic units, because global name capture requires
2730 -- those expressions to be analyzed, given that the generated
2731 -- pragmas do not appear in the original generic tree.
2733 elsif Serious_Errors_Detected = 0 then
2734 Resolve_Aspects;
2735 end if;
2737 -- If next node is a body then freeze all types before the body.
2738 -- An exception occurs for some expander-generated bodies. If these
2739 -- are generated at places where in general language rules would not
2740 -- allow a freeze point, then we assume that the expander has
2741 -- explicitly checked that all required types are properly frozen,
2742 -- and we do not cause general freezing here. This special circuit
2743 -- is used when the encountered body is marked as having already
2744 -- been analyzed.
2746 -- In all other cases (bodies that come from source, and expander
2747 -- generated bodies that have not been analyzed yet), freeze all
2748 -- types now. Note that in the latter case, the expander must take
2749 -- care to attach the bodies at a proper place in the tree so as to
2750 -- not cause unwanted freezing at that point.
2752 -- It is also necessary to check for a case where both an expression
2753 -- function is used and the current scope depends on an incomplete
2754 -- private type from a library unit, otherwise premature freezing of
2755 -- the private type will occur.
2757 elsif not Analyzed (Next_Decl) and then Is_Body (Next_Decl)
2758 and then ((Nkind (Next_Decl) /= N_Subprogram_Body
2759 or else not Was_Expression_Function (Next_Decl))
2760 or else (not Is_Ignored_Ghost_Entity (Current_Scope)
2761 and then not Contains_Lib_Incomplete_Type
2762 (Current_Scope)))
2763 then
2764 -- When a controlled type is frozen, the expander generates stream
2765 -- and controlled-type support routines. If the freeze is caused
2766 -- by the stand-alone body of Initialize, Adjust, or Finalize, the
2767 -- expander will end up using the wrong version of these routines,
2768 -- as the body has not been processed yet. To remedy this, detect
2769 -- a late controlled primitive and create a proper spec for it.
2770 -- This ensures that the primitive will override its inherited
2771 -- counterpart before the freeze takes place.
2773 -- If the declaration we just processed is a body, do not attempt
2774 -- to examine Next_Decl as the late primitive idiom can only apply
2775 -- to the first encountered body.
2777 -- The spec of the late primitive is not generated in ASIS mode to
2778 -- ensure a consistent list of primitives that indicates the true
2779 -- semantic structure of the program (which is not relevant when
2780 -- generating executable code).
2782 -- ??? A cleaner approach may be possible and/or this solution
2783 -- could be extended to general-purpose late primitives, TBD.
2785 if not ASIS_Mode
2786 and then not Body_Seen
2787 and then not Is_Body (Decl)
2788 then
2789 Body_Seen := True;
2791 if Nkind (Next_Decl) = N_Subprogram_Body then
2792 Handle_Late_Controlled_Primitive (Next_Decl);
2793 end if;
2795 else
2796 -- In ASIS mode, if the next declaration is a body, complete
2797 -- the analysis of declarations so far.
2799 Resolve_Aspects;
2800 end if;
2802 Adjust_Decl;
2804 -- The generated body of an expression function does not freeze,
2805 -- unless it is a completion, in which case only the expression
2806 -- itself freezes. This is handled when the body itself is
2807 -- analyzed (see Freeze_Expr_Types, sem_ch6.adb).
2809 Freeze_All (Freeze_From, Decl);
2810 Freeze_From := Last_Entity (Current_Scope);
2811 end if;
2813 Decl := Next_Decl;
2814 end loop;
2816 -- Post-freezing actions
2818 if Present (L) then
2819 Context := Parent (L);
2821 -- Certain contract annocations have forward visibility semantics and
2822 -- must be analyzed after all declarative items have been processed.
2823 -- This timing ensures that entities referenced by such contracts are
2824 -- visible.
2826 -- Analyze the contract of an immediately enclosing package spec or
2827 -- body first because other contracts may depend on its information.
2829 if Nkind (Context) = N_Package_Body then
2830 Analyze_Package_Body_Contract (Defining_Entity (Context));
2832 elsif Nkind (Context) = N_Package_Specification then
2833 Analyze_Package_Contract (Defining_Entity (Context));
2834 end if;
2836 -- Analyze the contracts of various constructs in the declarative
2837 -- list.
2839 Analyze_Contracts (L);
2841 if Nkind (Context) = N_Package_Body then
2843 -- Ensure that all abstract states and objects declared in the
2844 -- state space of a package body are utilized as constituents.
2846 Check_Unused_Body_States (Defining_Entity (Context));
2848 -- State refinements are visible up to the end of the package body
2849 -- declarations. Hide the state refinements from visibility to
2850 -- restore the original state conditions.
2852 Remove_Visible_Refinements (Corresponding_Spec (Context));
2853 Remove_Partial_Visible_Refinements (Corresponding_Spec (Context));
2855 elsif Nkind (Context) = N_Package_Specification then
2857 -- Partial state refinements are visible up to the end of the
2858 -- package spec declarations. Hide the partial state refinements
2859 -- from visibility to restore the original state conditions.
2861 Remove_Partial_Visible_Refinements (Defining_Entity (Context));
2862 end if;
2864 -- Verify that all abstract states found in any package declared in
2865 -- the input declarative list have proper refinements. The check is
2866 -- performed only when the context denotes a block, entry, package,
2867 -- protected, subprogram, or task body (SPARK RM 7.2.2(3)).
2869 Check_State_Refinements (Context);
2871 -- Create the subprogram bodies which verify the run-time semantics
2872 -- of pragmas Default_Initial_Condition and [Type_]Invariant for all
2873 -- types within the current declarative list. This ensures that all
2874 -- assertion expressions are preanalyzed and resolved at the end of
2875 -- the declarative part. Note that the resolution happens even when
2876 -- freezing does not take place.
2878 Build_Assertion_Bodies (L, Context);
2879 end if;
2880 end Analyze_Declarations;
2882 -----------------------------------
2883 -- Analyze_Full_Type_Declaration --
2884 -----------------------------------
2886 procedure Analyze_Full_Type_Declaration (N : Node_Id) is
2887 Def : constant Node_Id := Type_Definition (N);
2888 Def_Id : constant Entity_Id := Defining_Identifier (N);
2889 T : Entity_Id;
2890 Prev : Entity_Id;
2892 Is_Remote : constant Boolean :=
2893 (Is_Remote_Types (Current_Scope)
2894 or else Is_Remote_Call_Interface (Current_Scope))
2895 and then not (In_Private_Part (Current_Scope)
2896 or else In_Package_Body (Current_Scope));
2898 procedure Check_Nonoverridable_Aspects;
2899 -- Apply the rule in RM 13.1.1(18.4/4) on iterator aspects that cannot
2900 -- be overridden, and can only be confirmed on derivation.
2902 procedure Check_Ops_From_Incomplete_Type;
2903 -- If there is a tagged incomplete partial view of the type, traverse
2904 -- the primitives of the incomplete view and change the type of any
2905 -- controlling formals and result to indicate the full view. The
2906 -- primitives will be added to the full type's primitive operations
2907 -- list later in Sem_Disp.Check_Operation_From_Incomplete_Type (which
2908 -- is called from Process_Incomplete_Dependents).
2910 ----------------------------------
2911 -- Check_Nonoverridable_Aspects --
2912 ----------------------------------
2914 procedure Check_Nonoverridable_Aspects is
2915 function Get_Aspect_Spec
2916 (Specs : List_Id;
2917 Aspect_Name : Name_Id) return Node_Id;
2918 -- Check whether a list of aspect specifications includes an entry
2919 -- for a specific aspect. The list is either that of a partial or
2920 -- a full view.
2922 ---------------------
2923 -- Get_Aspect_Spec --
2924 ---------------------
2926 function Get_Aspect_Spec
2927 (Specs : List_Id;
2928 Aspect_Name : Name_Id) return Node_Id
2930 Spec : Node_Id;
2932 begin
2933 Spec := First (Specs);
2934 while Present (Spec) loop
2935 if Chars (Identifier (Spec)) = Aspect_Name then
2936 return Spec;
2937 end if;
2938 Next (Spec);
2939 end loop;
2941 return Empty;
2942 end Get_Aspect_Spec;
2944 -- Local variables
2946 Prev_Aspects : constant List_Id :=
2947 Aspect_Specifications (Parent (Def_Id));
2948 Par_Type : Entity_Id;
2949 Prev_Aspect : Node_Id;
2951 -- Start of processing for Check_Nonoverridable_Aspects
2953 begin
2954 -- Get parent type of derived type. Note that Prev is the entity in
2955 -- the partial declaration, but its contents are now those of full
2956 -- view, while Def_Id reflects the partial view.
2958 if Is_Private_Type (Def_Id) then
2959 Par_Type := Etype (Full_View (Def_Id));
2960 else
2961 Par_Type := Etype (Def_Id);
2962 end if;
2964 -- If there is an inherited Implicit_Dereference, verify that it is
2965 -- made explicit in the partial view.
2967 if Has_Discriminants (Base_Type (Par_Type))
2968 and then Nkind (Parent (Prev)) = N_Full_Type_Declaration
2969 and then Present (Discriminant_Specifications (Parent (Prev)))
2970 and then Present (Get_Reference_Discriminant (Par_Type))
2971 then
2972 Prev_Aspect :=
2973 Get_Aspect_Spec (Prev_Aspects, Name_Implicit_Dereference);
2975 if No (Prev_Aspect)
2976 and then Present
2977 (Discriminant_Specifications
2978 (Original_Node (Parent (Prev))))
2979 then
2980 Error_Msg_N
2981 ("type does not inherit implicit dereference", Prev);
2983 else
2984 -- If one of the views has the aspect specified, verify that it
2985 -- is consistent with that of the parent.
2987 declare
2988 Par_Discr : constant Entity_Id :=
2989 Get_Reference_Discriminant (Par_Type);
2990 Cur_Discr : constant Entity_Id :=
2991 Get_Reference_Discriminant (Prev);
2993 begin
2994 if Corresponding_Discriminant (Cur_Discr) /= Par_Discr then
2995 Error_Msg_N ("aspect incosistent with that of parent", N);
2996 end if;
2998 -- Check that specification in partial view matches the
2999 -- inherited aspect. Compare names directly because aspect
3000 -- expression may not be analyzed.
3002 if Present (Prev_Aspect)
3003 and then Nkind (Expression (Prev_Aspect)) = N_Identifier
3004 and then Chars (Expression (Prev_Aspect)) /=
3005 Chars (Cur_Discr)
3006 then
3007 Error_Msg_N
3008 ("aspect incosistent with that of parent", N);
3009 end if;
3010 end;
3011 end if;
3012 end if;
3014 -- TBD : other nonoverridable aspects.
3015 end Check_Nonoverridable_Aspects;
3017 ------------------------------------
3018 -- Check_Ops_From_Incomplete_Type --
3019 ------------------------------------
3021 procedure Check_Ops_From_Incomplete_Type is
3022 Elmt : Elmt_Id;
3023 Formal : Entity_Id;
3024 Op : Entity_Id;
3026 begin
3027 if Prev /= T
3028 and then Ekind (Prev) = E_Incomplete_Type
3029 and then Is_Tagged_Type (Prev)
3030 and then Is_Tagged_Type (T)
3031 then
3032 Elmt := First_Elmt (Primitive_Operations (Prev));
3033 while Present (Elmt) loop
3034 Op := Node (Elmt);
3036 Formal := First_Formal (Op);
3037 while Present (Formal) loop
3038 if Etype (Formal) = Prev then
3039 Set_Etype (Formal, T);
3040 end if;
3042 Next_Formal (Formal);
3043 end loop;
3045 if Etype (Op) = Prev then
3046 Set_Etype (Op, T);
3047 end if;
3049 Next_Elmt (Elmt);
3050 end loop;
3051 end if;
3052 end Check_Ops_From_Incomplete_Type;
3054 -- Start of processing for Analyze_Full_Type_Declaration
3056 begin
3057 Prev := Find_Type_Name (N);
3059 -- The full view, if present, now points to the current type. If there
3060 -- is an incomplete partial view, set a link to it, to simplify the
3061 -- retrieval of primitive operations of the type.
3063 -- Ada 2005 (AI-50217): If the type was previously decorated when
3064 -- imported through a LIMITED WITH clause, it appears as incomplete
3065 -- but has no full view.
3067 if Ekind (Prev) = E_Incomplete_Type
3068 and then Present (Full_View (Prev))
3069 then
3070 T := Full_View (Prev);
3071 Set_Incomplete_View (N, Parent (Prev));
3072 else
3073 T := Prev;
3074 end if;
3076 Set_Is_Pure (T, Is_Pure (Current_Scope));
3078 -- We set the flag Is_First_Subtype here. It is needed to set the
3079 -- corresponding flag for the Implicit class-wide-type created
3080 -- during tagged types processing.
3082 Set_Is_First_Subtype (T, True);
3084 -- Only composite types other than array types are allowed to have
3085 -- discriminants.
3087 case Nkind (Def) is
3089 -- For derived types, the rule will be checked once we've figured
3090 -- out the parent type.
3092 when N_Derived_Type_Definition =>
3093 null;
3095 -- For record types, discriminants are allowed, unless we are in
3096 -- SPARK.
3098 when N_Record_Definition =>
3099 if Present (Discriminant_Specifications (N)) then
3100 Check_SPARK_05_Restriction
3101 ("discriminant type is not allowed",
3102 Defining_Identifier
3103 (First (Discriminant_Specifications (N))));
3104 end if;
3106 when others =>
3107 if Present (Discriminant_Specifications (N)) then
3108 Error_Msg_N
3109 ("elementary or array type cannot have discriminants",
3110 Defining_Identifier
3111 (First (Discriminant_Specifications (N))));
3112 end if;
3113 end case;
3115 -- Elaborate the type definition according to kind, and generate
3116 -- subsidiary (implicit) subtypes where needed. We skip this if it was
3117 -- already done (this happens during the reanalysis that follows a call
3118 -- to the high level optimizer).
3120 if not Analyzed (T) then
3121 Set_Analyzed (T);
3123 case Nkind (Def) is
3124 when N_Access_To_Subprogram_Definition =>
3125 Access_Subprogram_Declaration (T, Def);
3127 -- If this is a remote access to subprogram, we must create the
3128 -- equivalent fat pointer type, and related subprograms.
3130 if Is_Remote then
3131 Process_Remote_AST_Declaration (N);
3132 end if;
3134 -- Validate categorization rule against access type declaration
3135 -- usually a violation in Pure unit, Shared_Passive unit.
3137 Validate_Access_Type_Declaration (T, N);
3139 when N_Access_To_Object_Definition =>
3140 Access_Type_Declaration (T, Def);
3142 -- Validate categorization rule against access type declaration
3143 -- usually a violation in Pure unit, Shared_Passive unit.
3145 Validate_Access_Type_Declaration (T, N);
3147 -- If we are in a Remote_Call_Interface package and define a
3148 -- RACW, then calling stubs and specific stream attributes
3149 -- must be added.
3151 if Is_Remote
3152 and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
3153 then
3154 Add_RACW_Features (Def_Id);
3155 end if;
3157 when N_Array_Type_Definition =>
3158 Array_Type_Declaration (T, Def);
3160 when N_Derived_Type_Definition =>
3161 Derived_Type_Declaration (T, N, T /= Def_Id);
3163 -- Inherit predicates from parent, and protect against illegal
3164 -- derivations.
3166 if Is_Type (T) and then Has_Predicates (T) then
3167 Set_Has_Predicates (Def_Id);
3168 end if;
3170 when N_Enumeration_Type_Definition =>
3171 Enumeration_Type_Declaration (T, Def);
3173 when N_Floating_Point_Definition =>
3174 Floating_Point_Type_Declaration (T, Def);
3176 when N_Decimal_Fixed_Point_Definition =>
3177 Decimal_Fixed_Point_Type_Declaration (T, Def);
3179 when N_Ordinary_Fixed_Point_Definition =>
3180 Ordinary_Fixed_Point_Type_Declaration (T, Def);
3182 when N_Signed_Integer_Type_Definition =>
3183 Signed_Integer_Type_Declaration (T, Def);
3185 when N_Modular_Type_Definition =>
3186 Modular_Type_Declaration (T, Def);
3188 when N_Record_Definition =>
3189 Record_Type_Declaration (T, N, Prev);
3191 -- If declaration has a parse error, nothing to elaborate.
3193 when N_Error =>
3194 null;
3196 when others =>
3197 raise Program_Error;
3198 end case;
3199 end if;
3201 if Etype (T) = Any_Type then
3202 return;
3203 end if;
3205 -- Controlled type is not allowed in SPARK
3207 if Is_Visibly_Controlled (T) then
3208 Check_SPARK_05_Restriction ("controlled type is not allowed", N);
3209 end if;
3211 -- Some common processing for all types
3213 Set_Depends_On_Private (T, Has_Private_Component (T));
3214 Check_Ops_From_Incomplete_Type;
3216 -- Both the declared entity, and its anonymous base type if one was
3217 -- created, need freeze nodes allocated.
3219 declare
3220 B : constant Entity_Id := Base_Type (T);
3222 begin
3223 -- In the case where the base type differs from the first subtype, we
3224 -- pre-allocate a freeze node, and set the proper link to the first
3225 -- subtype. Freeze_Entity will use this preallocated freeze node when
3226 -- it freezes the entity.
3228 -- This does not apply if the base type is a generic type, whose
3229 -- declaration is independent of the current derived definition.
3231 if B /= T and then not Is_Generic_Type (B) then
3232 Ensure_Freeze_Node (B);
3233 Set_First_Subtype_Link (Freeze_Node (B), T);
3234 end if;
3236 -- A type that is imported through a limited_with clause cannot
3237 -- generate any code, and thus need not be frozen. However, an access
3238 -- type with an imported designated type needs a finalization list,
3239 -- which may be referenced in some other package that has non-limited
3240 -- visibility on the designated type. Thus we must create the
3241 -- finalization list at the point the access type is frozen, to
3242 -- prevent unsatisfied references at link time.
3244 if not From_Limited_With (T) or else Is_Access_Type (T) then
3245 Set_Has_Delayed_Freeze (T);
3246 end if;
3247 end;
3249 -- Case where T is the full declaration of some private type which has
3250 -- been swapped in Defining_Identifier (N).
3252 if T /= Def_Id and then Is_Private_Type (Def_Id) then
3253 Process_Full_View (N, T, Def_Id);
3255 -- Record the reference. The form of this is a little strange, since
3256 -- the full declaration has been swapped in. So the first parameter
3257 -- here represents the entity to which a reference is made which is
3258 -- the "real" entity, i.e. the one swapped in, and the second
3259 -- parameter provides the reference location.
3261 -- Also, we want to kill Has_Pragma_Unreferenced temporarily here
3262 -- since we don't want a complaint about the full type being an
3263 -- unwanted reference to the private type
3265 declare
3266 B : constant Boolean := Has_Pragma_Unreferenced (T);
3267 begin
3268 Set_Has_Pragma_Unreferenced (T, False);
3269 Generate_Reference (T, T, 'c');
3270 Set_Has_Pragma_Unreferenced (T, B);
3271 end;
3273 Set_Completion_Referenced (Def_Id);
3275 -- For completion of incomplete type, process incomplete dependents
3276 -- and always mark the full type as referenced (it is the incomplete
3277 -- type that we get for any real reference).
3279 elsif Ekind (Prev) = E_Incomplete_Type then
3280 Process_Incomplete_Dependents (N, T, Prev);
3281 Generate_Reference (Prev, Def_Id, 'c');
3282 Set_Completion_Referenced (Def_Id);
3284 -- If not private type or incomplete type completion, this is a real
3285 -- definition of a new entity, so record it.
3287 else
3288 Generate_Definition (Def_Id);
3289 end if;
3291 -- Propagate any pending access types whose finalization masters need to
3292 -- be fully initialized from the partial to the full view. Guard against
3293 -- an illegal full view that remains unanalyzed.
3295 if Is_Type (Def_Id) and then Is_Incomplete_Or_Private_Type (Prev) then
3296 Set_Pending_Access_Types (Def_Id, Pending_Access_Types (Prev));
3297 end if;
3299 if Chars (Scope (Def_Id)) = Name_System
3300 and then Chars (Def_Id) = Name_Address
3301 and then In_Predefined_Unit (N)
3302 then
3303 Set_Is_Descendant_Of_Address (Def_Id);
3304 Set_Is_Descendant_Of_Address (Base_Type (Def_Id));
3305 Set_Is_Descendant_Of_Address (Prev);
3306 end if;
3308 Set_Optimize_Alignment_Flags (Def_Id);
3309 Check_Eliminated (Def_Id);
3311 -- If the declaration is a completion and aspects are present, apply
3312 -- them to the entity for the type which is currently the partial
3313 -- view, but which is the one that will be frozen.
3315 if Has_Aspects (N) then
3317 -- In most cases the partial view is a private type, and both views
3318 -- appear in different declarative parts. In the unusual case where
3319 -- the partial view is incomplete, perform the analysis on the
3320 -- full view, to prevent freezing anomalies with the corresponding
3321 -- class-wide type, which otherwise might be frozen before the
3322 -- dispatch table is built.
3324 if Prev /= Def_Id
3325 and then Ekind (Prev) /= E_Incomplete_Type
3326 then
3327 Analyze_Aspect_Specifications (N, Prev);
3329 -- Normal case
3331 else
3332 Analyze_Aspect_Specifications (N, Def_Id);
3333 end if;
3334 end if;
3336 if Is_Derived_Type (Prev)
3337 and then Def_Id /= Prev
3338 then
3339 Check_Nonoverridable_Aspects;
3340 end if;
3341 end Analyze_Full_Type_Declaration;
3343 ----------------------------------
3344 -- Analyze_Incomplete_Type_Decl --
3345 ----------------------------------
3347 procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
3348 F : constant Boolean := Is_Pure (Current_Scope);
3349 T : Entity_Id;
3351 begin
3352 Check_SPARK_05_Restriction ("incomplete type is not allowed", N);
3354 Generate_Definition (Defining_Identifier (N));
3356 -- Process an incomplete declaration. The identifier must not have been
3357 -- declared already in the scope. However, an incomplete declaration may
3358 -- appear in the private part of a package, for a private type that has
3359 -- already been declared.
3361 -- In this case, the discriminants (if any) must match
3363 T := Find_Type_Name (N);
3365 Set_Ekind (T, E_Incomplete_Type);
3366 Init_Size_Align (T);
3367 Set_Is_First_Subtype (T, True);
3368 Set_Etype (T, T);
3370 -- Ada 2005 (AI-326): Minimum decoration to give support to tagged
3371 -- incomplete types.
3373 if Tagged_Present (N) then
3374 Set_Is_Tagged_Type (T, True);
3375 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
3376 Make_Class_Wide_Type (T);
3377 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3378 end if;
3380 Set_Stored_Constraint (T, No_Elist);
3382 if Present (Discriminant_Specifications (N)) then
3383 Push_Scope (T);
3384 Process_Discriminants (N);
3385 End_Scope;
3386 end if;
3388 -- If the type has discriminants, nontrivial subtypes may be declared
3389 -- before the full view of the type. The full views of those subtypes
3390 -- will be built after the full view of the type.
3392 Set_Private_Dependents (T, New_Elmt_List);
3393 Set_Is_Pure (T, F);
3394 end Analyze_Incomplete_Type_Decl;
3396 -----------------------------------
3397 -- Analyze_Interface_Declaration --
3398 -----------------------------------
3400 procedure Analyze_Interface_Declaration (T : Entity_Id; Def : Node_Id) is
3401 CW : constant Entity_Id := Class_Wide_Type (T);
3403 begin
3404 Set_Is_Tagged_Type (T);
3405 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
3407 Set_Is_Limited_Record (T, Limited_Present (Def)
3408 or else Task_Present (Def)
3409 or else Protected_Present (Def)
3410 or else Synchronized_Present (Def));
3412 -- Type is abstract if full declaration carries keyword, or if previous
3413 -- partial view did.
3415 Set_Is_Abstract_Type (T);
3416 Set_Is_Interface (T);
3418 -- Type is a limited interface if it includes the keyword limited, task,
3419 -- protected, or synchronized.
3421 Set_Is_Limited_Interface
3422 (T, Limited_Present (Def)
3423 or else Protected_Present (Def)
3424 or else Synchronized_Present (Def)
3425 or else Task_Present (Def));
3427 Set_Interfaces (T, New_Elmt_List);
3428 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3430 -- Complete the decoration of the class-wide entity if it was already
3431 -- built (i.e. during the creation of the limited view)
3433 if Present (CW) then
3434 Set_Is_Interface (CW);
3435 Set_Is_Limited_Interface (CW, Is_Limited_Interface (T));
3436 end if;
3438 -- Check runtime support for synchronized interfaces
3440 if (Is_Task_Interface (T)
3441 or else Is_Protected_Interface (T)
3442 or else Is_Synchronized_Interface (T))
3443 and then not RTE_Available (RE_Select_Specific_Data)
3444 then
3445 Error_Msg_CRT ("synchronized interfaces", T);
3446 end if;
3447 end Analyze_Interface_Declaration;
3449 -----------------------------
3450 -- Analyze_Itype_Reference --
3451 -----------------------------
3453 -- Nothing to do. This node is placed in the tree only for the benefit of
3454 -- back end processing, and has no effect on the semantic processing.
3456 procedure Analyze_Itype_Reference (N : Node_Id) is
3457 begin
3458 pragma Assert (Is_Itype (Itype (N)));
3459 null;
3460 end Analyze_Itype_Reference;
3462 --------------------------------
3463 -- Analyze_Number_Declaration --
3464 --------------------------------
3466 procedure Analyze_Number_Declaration (N : Node_Id) is
3467 E : constant Node_Id := Expression (N);
3468 Id : constant Entity_Id := Defining_Identifier (N);
3469 Index : Interp_Index;
3470 It : Interp;
3471 T : Entity_Id;
3473 begin
3474 Generate_Definition (Id);
3475 Enter_Name (Id);
3477 -- This is an optimization of a common case of an integer literal
3479 if Nkind (E) = N_Integer_Literal then
3480 Set_Is_Static_Expression (E, True);
3481 Set_Etype (E, Universal_Integer);
3483 Set_Etype (Id, Universal_Integer);
3484 Set_Ekind (Id, E_Named_Integer);
3485 Set_Is_Frozen (Id, True);
3486 return;
3487 end if;
3489 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3491 -- Process expression, replacing error by integer zero, to avoid
3492 -- cascaded errors or aborts further along in the processing
3494 -- Replace Error by integer zero, which seems least likely to cause
3495 -- cascaded errors.
3497 if E = Error then
3498 Rewrite (E, Make_Integer_Literal (Sloc (E), Uint_0));
3499 Set_Error_Posted (E);
3500 end if;
3502 Analyze (E);
3504 -- Verify that the expression is static and numeric. If
3505 -- the expression is overloaded, we apply the preference
3506 -- rule that favors root numeric types.
3508 if not Is_Overloaded (E) then
3509 T := Etype (E);
3510 if Has_Dynamic_Predicate_Aspect (T) then
3511 Error_Msg_N
3512 ("subtype has dynamic predicate, "
3513 & "not allowed in number declaration", N);
3514 end if;
3516 else
3517 T := Any_Type;
3519 Get_First_Interp (E, Index, It);
3520 while Present (It.Typ) loop
3521 if (Is_Integer_Type (It.Typ) or else Is_Real_Type (It.Typ))
3522 and then (Scope (Base_Type (It.Typ))) = Standard_Standard
3523 then
3524 if T = Any_Type then
3525 T := It.Typ;
3527 elsif It.Typ = Universal_Real
3528 or else
3529 It.Typ = Universal_Integer
3530 then
3531 -- Choose universal interpretation over any other
3533 T := It.Typ;
3534 exit;
3535 end if;
3536 end if;
3538 Get_Next_Interp (Index, It);
3539 end loop;
3540 end if;
3542 if Is_Integer_Type (T) then
3543 Resolve (E, T);
3544 Set_Etype (Id, Universal_Integer);
3545 Set_Ekind (Id, E_Named_Integer);
3547 elsif Is_Real_Type (T) then
3549 -- Because the real value is converted to universal_real, this is a
3550 -- legal context for a universal fixed expression.
3552 if T = Universal_Fixed then
3553 declare
3554 Loc : constant Source_Ptr := Sloc (N);
3555 Conv : constant Node_Id := Make_Type_Conversion (Loc,
3556 Subtype_Mark =>
3557 New_Occurrence_Of (Universal_Real, Loc),
3558 Expression => Relocate_Node (E));
3560 begin
3561 Rewrite (E, Conv);
3562 Analyze (E);
3563 end;
3565 elsif T = Any_Fixed then
3566 Error_Msg_N ("illegal context for mixed mode operation", E);
3568 -- Expression is of the form : universal_fixed * integer. Try to
3569 -- resolve as universal_real.
3571 T := Universal_Real;
3572 Set_Etype (E, T);
3573 end if;
3575 Resolve (E, T);
3576 Set_Etype (Id, Universal_Real);
3577 Set_Ekind (Id, E_Named_Real);
3579 else
3580 Wrong_Type (E, Any_Numeric);
3581 Resolve (E, T);
3583 Set_Etype (Id, T);
3584 Set_Ekind (Id, E_Constant);
3585 Set_Never_Set_In_Source (Id, True);
3586 Set_Is_True_Constant (Id, True);
3587 return;
3588 end if;
3590 if Nkind_In (E, N_Integer_Literal, N_Real_Literal) then
3591 Set_Etype (E, Etype (Id));
3592 end if;
3594 if not Is_OK_Static_Expression (E) then
3595 Flag_Non_Static_Expr
3596 ("non-static expression used in number declaration!", E);
3597 Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
3598 Set_Etype (E, Any_Type);
3599 end if;
3601 Analyze_Dimension (N);
3602 end Analyze_Number_Declaration;
3604 --------------------------------
3605 -- Analyze_Object_Declaration --
3606 --------------------------------
3608 -- WARNING: This routine manages Ghost regions. Return statements must be
3609 -- replaced by gotos which jump to the end of the routine and restore the
3610 -- Ghost mode.
3612 procedure Analyze_Object_Declaration (N : Node_Id) is
3613 Loc : constant Source_Ptr := Sloc (N);
3614 Id : constant Entity_Id := Defining_Identifier (N);
3615 Act_T : Entity_Id;
3616 T : Entity_Id;
3618 E : Node_Id := Expression (N);
3619 -- E is set to Expression (N) throughout this routine. When Expression
3620 -- (N) is modified, E is changed accordingly.
3622 Prev_Entity : Entity_Id := Empty;
3624 procedure Check_Dynamic_Object (Typ : Entity_Id);
3625 -- A library-level object with non-static discriminant constraints may
3626 -- require dynamic allocation. The declaration is illegal if the
3627 -- profile includes the restriction No_Implicit_Heap_Allocations.
3629 procedure Check_For_Null_Excluding_Components
3630 (Obj_Typ : Entity_Id;
3631 Obj_Decl : Node_Id);
3632 -- Verify that each null-excluding component of object declaration
3633 -- Obj_Decl carrying type Obj_Typ has explicit initialization. Emit
3634 -- a compile-time warning if this is not the case.
3636 function Count_Tasks (T : Entity_Id) return Uint;
3637 -- This function is called when a non-generic library level object of a
3638 -- task type is declared. Its function is to count the static number of
3639 -- tasks declared within the type (it is only called if Has_Task is set
3640 -- for T). As a side effect, if an array of tasks with non-static bounds
3641 -- or a variant record type is encountered, Check_Restriction is called
3642 -- indicating the count is unknown.
3644 function Delayed_Aspect_Present return Boolean;
3645 -- If the declaration has an expression that is an aggregate, and it
3646 -- has aspects that require delayed analysis, the resolution of the
3647 -- aggregate must be deferred to the freeze point of the objet. This
3648 -- special processing was created for address clauses, but it must
3649 -- also apply to Alignment. This must be done before the aspect
3650 -- specifications are analyzed because we must handle the aggregate
3651 -- before the analysis of the object declaration is complete.
3653 -- Any other relevant delayed aspects on object declarations ???
3655 --------------------------
3656 -- Check_Dynamic_Object --
3657 --------------------------
3659 procedure Check_Dynamic_Object (Typ : Entity_Id) is
3660 Comp : Entity_Id;
3661 Obj_Type : Entity_Id;
3663 begin
3664 Obj_Type := Typ;
3666 if Is_Private_Type (Obj_Type)
3667 and then Present (Full_View (Obj_Type))
3668 then
3669 Obj_Type := Full_View (Obj_Type);
3670 end if;
3672 if Known_Static_Esize (Obj_Type) then
3673 return;
3674 end if;
3676 if Restriction_Active (No_Implicit_Heap_Allocations)
3677 and then Expander_Active
3678 and then Has_Discriminants (Obj_Type)
3679 then
3680 Comp := First_Component (Obj_Type);
3681 while Present (Comp) loop
3682 if Known_Static_Esize (Etype (Comp))
3683 or else Size_Known_At_Compile_Time (Etype (Comp))
3684 then
3685 null;
3687 elsif not Discriminated_Size (Comp)
3688 and then Comes_From_Source (Comp)
3689 then
3690 Error_Msg_NE
3691 ("component& of non-static size will violate restriction "
3692 & "No_Implicit_Heap_Allocation?", N, Comp);
3694 elsif Is_Record_Type (Etype (Comp)) then
3695 Check_Dynamic_Object (Etype (Comp));
3696 end if;
3698 Next_Component (Comp);
3699 end loop;
3700 end if;
3701 end Check_Dynamic_Object;
3703 -----------------------------------------
3704 -- Check_For_Null_Excluding_Components --
3705 -----------------------------------------
3707 procedure Check_For_Null_Excluding_Components
3708 (Obj_Typ : Entity_Id;
3709 Obj_Decl : Node_Id)
3711 procedure Check_Component
3712 (Comp_Typ : Entity_Id;
3713 Comp_Decl : Node_Id := Empty;
3714 Array_Comp : Boolean := False);
3715 -- Apply a compile-time null-exclusion check on a component denoted
3716 -- by its declaration Comp_Decl and type Comp_Typ, and all of its
3717 -- subcomponents (if any).
3719 ---------------------
3720 -- Check_Component --
3721 ---------------------
3723 procedure Check_Component
3724 (Comp_Typ : Entity_Id;
3725 Comp_Decl : Node_Id := Empty;
3726 Array_Comp : Boolean := False)
3728 Comp : Entity_Id;
3729 T : Entity_Id;
3731 begin
3732 -- Do not consider internally-generated components or those that
3733 -- are already initialized.
3735 if Present (Comp_Decl)
3736 and then (not Comes_From_Source (Comp_Decl)
3737 or else Present (Expression (Comp_Decl)))
3738 then
3739 return;
3740 end if;
3742 if Is_Incomplete_Or_Private_Type (Comp_Typ)
3743 and then Present (Full_View (Comp_Typ))
3744 then
3745 T := Full_View (Comp_Typ);
3746 else
3747 T := Comp_Typ;
3748 end if;
3750 -- Verify a component of a null-excluding access type
3752 if Is_Access_Type (T)
3753 and then Can_Never_Be_Null (T)
3754 then
3755 if Comp_Decl = Obj_Decl then
3756 Null_Exclusion_Static_Checks
3757 (N => Obj_Decl,
3758 Comp => Empty,
3759 Array_Comp => Array_Comp);
3761 else
3762 Null_Exclusion_Static_Checks
3763 (N => Obj_Decl,
3764 Comp => Comp_Decl,
3765 Array_Comp => Array_Comp);
3766 end if;
3768 -- Check array components
3770 elsif Is_Array_Type (T) then
3772 -- There is no suitable component when the object is of an
3773 -- array type. However, a namable component may appear at some
3774 -- point during the recursive inspection, but not at the top
3775 -- level. At the top level just indicate array component case.
3777 if Comp_Decl = Obj_Decl then
3778 Check_Component (Component_Type (T), Array_Comp => True);
3779 else
3780 Check_Component (Component_Type (T), Comp_Decl);
3781 end if;
3783 -- Verify all components of type T
3785 -- Note: No checks are performed on types with discriminants due
3786 -- to complexities involving variants. ???
3788 elsif (Is_Concurrent_Type (T)
3789 or else Is_Incomplete_Or_Private_Type (T)
3790 or else Is_Record_Type (T))
3791 and then not Has_Discriminants (T)
3792 then
3793 Comp := First_Component (T);
3794 while Present (Comp) loop
3795 Check_Component (Etype (Comp), Parent (Comp));
3797 Comp := Next_Component (Comp);
3798 end loop;
3799 end if;
3800 end Check_Component;
3802 -- Start processing for Check_For_Null_Excluding_Components
3804 begin
3805 Check_Component (Obj_Typ, Obj_Decl);
3806 end Check_For_Null_Excluding_Components;
3808 -----------------
3809 -- Count_Tasks --
3810 -----------------
3812 function Count_Tasks (T : Entity_Id) return Uint is
3813 C : Entity_Id;
3814 X : Node_Id;
3815 V : Uint;
3817 begin
3818 if Is_Task_Type (T) then
3819 return Uint_1;
3821 elsif Is_Record_Type (T) then
3822 if Has_Discriminants (T) then
3823 Check_Restriction (Max_Tasks, N);
3824 return Uint_0;
3826 else
3827 V := Uint_0;
3828 C := First_Component (T);
3829 while Present (C) loop
3830 V := V + Count_Tasks (Etype (C));
3831 Next_Component (C);
3832 end loop;
3834 return V;
3835 end if;
3837 elsif Is_Array_Type (T) then
3838 X := First_Index (T);
3839 V := Count_Tasks (Component_Type (T));
3840 while Present (X) loop
3841 C := Etype (X);
3843 if not Is_OK_Static_Subtype (C) then
3844 Check_Restriction (Max_Tasks, N);
3845 return Uint_0;
3846 else
3847 V := V * (UI_Max (Uint_0,
3848 Expr_Value (Type_High_Bound (C)) -
3849 Expr_Value (Type_Low_Bound (C)) + Uint_1));
3850 end if;
3852 Next_Index (X);
3853 end loop;
3855 return V;
3857 else
3858 return Uint_0;
3859 end if;
3860 end Count_Tasks;
3862 ----------------------------
3863 -- Delayed_Aspect_Present --
3864 ----------------------------
3866 function Delayed_Aspect_Present return Boolean is
3867 A : Node_Id;
3868 A_Id : Aspect_Id;
3870 begin
3871 if Present (Aspect_Specifications (N)) then
3872 A := First (Aspect_Specifications (N));
3873 A_Id := Get_Aspect_Id (Chars (Identifier (A)));
3874 while Present (A) loop
3875 if A_Id = Aspect_Alignment or else A_Id = Aspect_Address then
3876 return True;
3877 end if;
3879 Next (A);
3880 end loop;
3881 end if;
3883 return False;
3884 end Delayed_Aspect_Present;
3886 -- Local variables
3888 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
3889 -- Save the Ghost mode to restore on exit
3891 Related_Id : Entity_Id;
3893 -- Start of processing for Analyze_Object_Declaration
3895 begin
3896 -- There are three kinds of implicit types generated by an
3897 -- object declaration:
3899 -- 1. Those generated by the original Object Definition
3901 -- 2. Those generated by the Expression
3903 -- 3. Those used to constrain the Object Definition with the
3904 -- expression constraints when the definition is unconstrained.
3906 -- They must be generated in this order to avoid order of elaboration
3907 -- issues. Thus the first step (after entering the name) is to analyze
3908 -- the object definition.
3910 if Constant_Present (N) then
3911 Prev_Entity := Current_Entity_In_Scope (Id);
3913 if Present (Prev_Entity)
3914 and then
3915 -- If the homograph is an implicit subprogram, it is overridden
3916 -- by the current declaration.
3918 ((Is_Overloadable (Prev_Entity)
3919 and then Is_Inherited_Operation (Prev_Entity))
3921 -- The current object is a discriminal generated for an entry
3922 -- family index. Even though the index is a constant, in this
3923 -- particular context there is no true constant redeclaration.
3924 -- Enter_Name will handle the visibility.
3926 or else
3927 (Is_Discriminal (Id)
3928 and then Ekind (Discriminal_Link (Id)) =
3929 E_Entry_Index_Parameter)
3931 -- The current object is the renaming for a generic declared
3932 -- within the instance.
3934 or else
3935 (Ekind (Prev_Entity) = E_Package
3936 and then Nkind (Parent (Prev_Entity)) =
3937 N_Package_Renaming_Declaration
3938 and then not Comes_From_Source (Prev_Entity)
3939 and then
3940 Is_Generic_Instance (Renamed_Entity (Prev_Entity)))
3942 -- The entity may be a homonym of a private component of the
3943 -- enclosing protected object, for which we create a local
3944 -- renaming declaration. The declaration is legal, even if
3945 -- useless when it just captures that component.
3947 or else
3948 (Ekind (Scope (Current_Scope)) = E_Protected_Type
3949 and then Nkind (Parent (Prev_Entity)) =
3950 N_Object_Renaming_Declaration))
3951 then
3952 Prev_Entity := Empty;
3953 end if;
3954 end if;
3956 if Present (Prev_Entity) then
3958 -- The object declaration is Ghost when it completes a deferred Ghost
3959 -- constant.
3961 Mark_And_Set_Ghost_Completion (N, Prev_Entity);
3963 Constant_Redeclaration (Id, N, T);
3965 Generate_Reference (Prev_Entity, Id, 'c');
3966 Set_Completion_Referenced (Id);
3968 if Error_Posted (N) then
3970 -- Type mismatch or illegal redeclaration; do not analyze
3971 -- expression to avoid cascaded errors.
3973 T := Find_Type_Of_Object (Object_Definition (N), N);
3974 Set_Etype (Id, T);
3975 Set_Ekind (Id, E_Variable);
3976 goto Leave;
3977 end if;
3979 -- In the normal case, enter identifier at the start to catch premature
3980 -- usage in the initialization expression.
3982 else
3983 Generate_Definition (Id);
3984 Enter_Name (Id);
3986 Mark_Coextensions (N, Object_Definition (N));
3988 T := Find_Type_Of_Object (Object_Definition (N), N);
3990 if Nkind (Object_Definition (N)) = N_Access_Definition
3991 and then Present
3992 (Access_To_Subprogram_Definition (Object_Definition (N)))
3993 and then Protected_Present
3994 (Access_To_Subprogram_Definition (Object_Definition (N)))
3995 then
3996 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
3997 end if;
3999 if Error_Posted (Id) then
4000 Set_Etype (Id, T);
4001 Set_Ekind (Id, E_Variable);
4002 goto Leave;
4003 end if;
4004 end if;
4006 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
4007 -- out some static checks.
4009 if Ada_Version >= Ada_2005 then
4011 -- In case of aggregates we must also take care of the correct
4012 -- initialization of nested aggregates bug this is done at the
4013 -- point of the analysis of the aggregate (see sem_aggr.adb) ???
4015 if Can_Never_Be_Null (T) then
4016 if Present (Expression (N))
4017 and then Nkind (Expression (N)) = N_Aggregate
4018 then
4019 null;
4021 else
4022 declare
4023 Save_Typ : constant Entity_Id := Etype (Id);
4024 begin
4025 Set_Etype (Id, T); -- Temp. decoration for static checks
4026 Null_Exclusion_Static_Checks (N);
4027 Set_Etype (Id, Save_Typ);
4028 end;
4029 end if;
4031 -- We might be dealing with an object of a composite type containing
4032 -- null-excluding components without an aggregate, so we must verify
4033 -- that such components have default initialization.
4035 else
4036 Check_For_Null_Excluding_Components (T, N);
4037 end if;
4038 end if;
4040 -- Object is marked pure if it is in a pure scope
4042 Set_Is_Pure (Id, Is_Pure (Current_Scope));
4044 -- If deferred constant, make sure context is appropriate. We detect
4045 -- a deferred constant as a constant declaration with no expression.
4046 -- A deferred constant can appear in a package body if its completion
4047 -- is by means of an interface pragma.
4049 if Constant_Present (N) and then No (E) then
4051 -- A deferred constant may appear in the declarative part of the
4052 -- following constructs:
4054 -- blocks
4055 -- entry bodies
4056 -- extended return statements
4057 -- package specs
4058 -- package bodies
4059 -- subprogram bodies
4060 -- task bodies
4062 -- When declared inside a package spec, a deferred constant must be
4063 -- completed by a full constant declaration or pragma Import. In all
4064 -- other cases, the only proper completion is pragma Import. Extended
4065 -- return statements are flagged as invalid contexts because they do
4066 -- not have a declarative part and so cannot accommodate the pragma.
4068 if Ekind (Current_Scope) = E_Return_Statement then
4069 Error_Msg_N
4070 ("invalid context for deferred constant declaration (RM 7.4)",
4072 Error_Msg_N
4073 ("\declaration requires an initialization expression",
4075 Set_Constant_Present (N, False);
4077 -- In Ada 83, deferred constant must be of private type
4079 elsif not Is_Private_Type (T) then
4080 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
4081 Error_Msg_N
4082 ("(Ada 83) deferred constant must be private type", N);
4083 end if;
4084 end if;
4086 -- If not a deferred constant, then the object declaration freezes
4087 -- its type, unless the object is of an anonymous type and has delayed
4088 -- aspects. In that case the type is frozen when the object itself is.
4090 else
4091 Check_Fully_Declared (T, N);
4093 if Has_Delayed_Aspects (Id)
4094 and then Is_Array_Type (T)
4095 and then Is_Itype (T)
4096 then
4097 Set_Has_Delayed_Freeze (T);
4098 else
4099 Freeze_Before (N, T);
4100 end if;
4101 end if;
4103 -- If the object was created by a constrained array definition, then
4104 -- set the link in both the anonymous base type and anonymous subtype
4105 -- that are built to represent the array type to point to the object.
4107 if Nkind (Object_Definition (Declaration_Node (Id))) =
4108 N_Constrained_Array_Definition
4109 then
4110 Set_Related_Array_Object (T, Id);
4111 Set_Related_Array_Object (Base_Type (T), Id);
4112 end if;
4114 -- Special checks for protected objects not at library level
4116 if Has_Protected (T) and then not Is_Library_Level_Entity (Id) then
4117 Check_Restriction (No_Local_Protected_Objects, Id);
4119 -- Protected objects with interrupt handlers must be at library level
4121 -- Ada 2005: This test is not needed (and the corresponding clause
4122 -- in the RM is removed) because accessibility checks are sufficient
4123 -- to make handlers not at the library level illegal.
4125 -- AI05-0303: The AI is in fact a binding interpretation, and thus
4126 -- applies to the '95 version of the language as well.
4128 if Is_Protected_Type (T)
4129 and then Has_Interrupt_Handler (T)
4130 and then Ada_Version < Ada_95
4131 then
4132 Error_Msg_N
4133 ("interrupt object can only be declared at library level", Id);
4134 end if;
4135 end if;
4137 -- Check for violation of No_Local_Timing_Events
4139 if Has_Timing_Event (T) and then not Is_Library_Level_Entity (Id) then
4140 Check_Restriction (No_Local_Timing_Events, Id);
4141 end if;
4143 -- The actual subtype of the object is the nominal subtype, unless
4144 -- the nominal one is unconstrained and obtained from the expression.
4146 Act_T := T;
4148 -- These checks should be performed before the initialization expression
4149 -- is considered, so that the Object_Definition node is still the same
4150 -- as in source code.
4152 -- In SPARK, the nominal subtype is always given by a subtype mark
4153 -- and must not be unconstrained. (The only exception to this is the
4154 -- acceptance of declarations of constants of type String.)
4156 if not Nkind_In (Object_Definition (N), N_Expanded_Name, N_Identifier)
4157 then
4158 Check_SPARK_05_Restriction
4159 ("subtype mark required", Object_Definition (N));
4161 elsif Is_Array_Type (T)
4162 and then not Is_Constrained (T)
4163 and then T /= Standard_String
4164 then
4165 Check_SPARK_05_Restriction
4166 ("subtype mark of constrained type expected",
4167 Object_Definition (N));
4168 end if;
4170 if Is_Library_Level_Entity (Id) then
4171 Check_Dynamic_Object (T);
4172 end if;
4174 -- There are no aliased objects in SPARK
4176 if Aliased_Present (N) then
4177 Check_SPARK_05_Restriction ("aliased object is not allowed", N);
4178 end if;
4180 -- Process initialization expression if present and not in error
4182 if Present (E) and then E /= Error then
4184 -- Generate an error in case of CPP class-wide object initialization.
4185 -- Required because otherwise the expansion of the class-wide
4186 -- assignment would try to use 'size to initialize the object
4187 -- (primitive that is not available in CPP tagged types).
4189 if Is_Class_Wide_Type (Act_T)
4190 and then
4191 (Is_CPP_Class (Root_Type (Etype (Act_T)))
4192 or else
4193 (Present (Full_View (Root_Type (Etype (Act_T))))
4194 and then
4195 Is_CPP_Class (Full_View (Root_Type (Etype (Act_T))))))
4196 then
4197 Error_Msg_N
4198 ("predefined assignment not available for 'C'P'P tagged types",
4200 end if;
4202 Mark_Coextensions (N, E);
4203 Analyze (E);
4205 -- In case of errors detected in the analysis of the expression,
4206 -- decorate it with the expected type to avoid cascaded errors
4208 if No (Etype (E)) then
4209 Set_Etype (E, T);
4210 end if;
4212 -- If an initialization expression is present, then we set the
4213 -- Is_True_Constant flag. It will be reset if this is a variable
4214 -- and it is indeed modified.
4216 Set_Is_True_Constant (Id, True);
4218 -- If we are analyzing a constant declaration, set its completion
4219 -- flag after analyzing and resolving the expression.
4221 if Constant_Present (N) then
4222 Set_Has_Completion (Id);
4223 end if;
4225 -- Set type and resolve (type may be overridden later on). Note:
4226 -- Ekind (Id) must still be E_Void at this point so that incorrect
4227 -- early usage within E is properly diagnosed.
4229 Set_Etype (Id, T);
4231 -- If the expression is an aggregate we must look ahead to detect
4232 -- the possible presence of an address clause, and defer resolution
4233 -- and expansion of the aggregate to the freeze point of the entity.
4235 -- This is not always legal because the aggregate may contain other
4236 -- references that need freezing, e.g. references to other entities
4237 -- with address clauses. In any case, when compiling with -gnatI the
4238 -- presence of the address clause must be ignored.
4240 if Comes_From_Source (N)
4241 and then Expander_Active
4242 and then Nkind (E) = N_Aggregate
4243 and then
4244 ((Present (Following_Address_Clause (N))
4245 and then not Ignore_Rep_Clauses)
4246 or else Delayed_Aspect_Present)
4247 then
4248 Set_Etype (E, T);
4250 else
4251 Resolve (E, T);
4252 end if;
4254 -- No further action needed if E is a call to an inlined function
4255 -- which returns an unconstrained type and it has been expanded into
4256 -- a procedure call. In that case N has been replaced by an object
4257 -- declaration without initializing expression and it has been
4258 -- analyzed (see Expand_Inlined_Call).
4260 if Back_End_Inlining
4261 and then Expander_Active
4262 and then Nkind (E) = N_Function_Call
4263 and then Nkind (Name (E)) in N_Has_Entity
4264 and then Is_Inlined (Entity (Name (E)))
4265 and then not Is_Constrained (Etype (E))
4266 and then Analyzed (N)
4267 and then No (Expression (N))
4268 then
4269 goto Leave;
4270 end if;
4272 -- If E is null and has been replaced by an N_Raise_Constraint_Error
4273 -- node (which was marked already-analyzed), we need to set the type
4274 -- to something other than Any_Access in order to keep gigi happy.
4276 if Etype (E) = Any_Access then
4277 Set_Etype (E, T);
4278 end if;
4280 -- If the object is an access to variable, the initialization
4281 -- expression cannot be an access to constant.
4283 if Is_Access_Type (T)
4284 and then not Is_Access_Constant (T)
4285 and then Is_Access_Type (Etype (E))
4286 and then Is_Access_Constant (Etype (E))
4287 then
4288 Error_Msg_N
4289 ("access to variable cannot be initialized with an "
4290 & "access-to-constant expression", E);
4291 end if;
4293 if not Assignment_OK (N) then
4294 Check_Initialization (T, E);
4295 end if;
4297 Check_Unset_Reference (E);
4299 -- If this is a variable, then set current value. If this is a
4300 -- declared constant of a scalar type with a static expression,
4301 -- indicate that it is always valid.
4303 if not Constant_Present (N) then
4304 if Compile_Time_Known_Value (E) then
4305 Set_Current_Value (Id, E);
4306 end if;
4308 elsif Is_Scalar_Type (T) and then Is_OK_Static_Expression (E) then
4309 Set_Is_Known_Valid (Id);
4310 end if;
4312 -- Deal with setting of null flags
4314 if Is_Access_Type (T) then
4315 if Known_Non_Null (E) then
4316 Set_Is_Known_Non_Null (Id, True);
4317 elsif Known_Null (E) and then not Can_Never_Be_Null (Id) then
4318 Set_Is_Known_Null (Id, True);
4319 end if;
4320 end if;
4322 -- Check incorrect use of dynamically tagged expressions
4324 if Is_Tagged_Type (T) then
4325 Check_Dynamically_Tagged_Expression
4326 (Expr => E,
4327 Typ => T,
4328 Related_Nod => N);
4329 end if;
4331 Apply_Scalar_Range_Check (E, T);
4332 Apply_Static_Length_Check (E, T);
4334 if Nkind (Original_Node (N)) = N_Object_Declaration
4335 and then Comes_From_Source (Original_Node (N))
4337 -- Only call test if needed
4339 and then Restriction_Check_Required (SPARK_05)
4340 and then not Is_SPARK_05_Initialization_Expr (Original_Node (E))
4341 then
4342 Check_SPARK_05_Restriction
4343 ("initialization expression is not appropriate", E);
4344 end if;
4346 -- A formal parameter of a specific tagged type whose related
4347 -- subprogram is subject to pragma Extensions_Visible with value
4348 -- "False" cannot be implicitly converted to a class-wide type by
4349 -- means of an initialization expression (SPARK RM 6.1.7(3)). Do
4350 -- not consider internally generated expressions.
4352 if Is_Class_Wide_Type (T)
4353 and then Comes_From_Source (E)
4354 and then Is_EVF_Expression (E)
4355 then
4356 Error_Msg_N
4357 ("formal parameter cannot be implicitly converted to "
4358 & "class-wide type when Extensions_Visible is False", E);
4359 end if;
4360 end if;
4362 -- If the No_Streams restriction is set, check that the type of the
4363 -- object is not, and does not contain, any subtype derived from
4364 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
4365 -- Has_Stream just for efficiency reasons. There is no point in
4366 -- spending time on a Has_Stream check if the restriction is not set.
4368 if Restriction_Check_Required (No_Streams) then
4369 if Has_Stream (T) then
4370 Check_Restriction (No_Streams, N);
4371 end if;
4372 end if;
4374 -- Deal with predicate check before we start to do major rewriting. It
4375 -- is OK to initialize and then check the initialized value, since the
4376 -- object goes out of scope if we get a predicate failure. Note that we
4377 -- do this in the analyzer and not the expander because the analyzer
4378 -- does some substantial rewriting in some cases.
4380 -- We need a predicate check if the type has predicates that are not
4381 -- ignored, and if either there is an initializing expression, or for
4382 -- default initialization when we have at least one case of an explicit
4383 -- default initial value and then this is not an internal declaration
4384 -- whose initialization comes later (as for an aggregate expansion).
4386 if not Suppress_Assignment_Checks (N)
4387 and then Present (Predicate_Function (T))
4388 and then not Predicates_Ignored (T)
4389 and then not No_Initialization (N)
4390 and then
4391 (Present (E)
4392 or else
4393 Is_Partially_Initialized_Type (T, Include_Implicit => False))
4394 then
4395 -- If the type has a static predicate and the expression is known at
4396 -- compile time, see if the expression satisfies the predicate.
4398 if Present (E) then
4399 Check_Expression_Against_Static_Predicate (E, T);
4400 end if;
4402 -- If the type is a null record and there is no explicit initial
4403 -- expression, no predicate check applies.
4405 if No (E) and then Is_Null_Record_Type (T) then
4406 null;
4408 -- Do not generate a predicate check if the initialization expression
4409 -- is a type conversion because the conversion has been subjected to
4410 -- the same check. This is a small optimization which avoid redundant
4411 -- checks.
4413 elsif Present (E) and then Nkind (E) = N_Type_Conversion then
4414 null;
4416 else
4417 Insert_After (N,
4418 Make_Predicate_Check (T, New_Occurrence_Of (Id, Loc)));
4419 end if;
4420 end if;
4422 -- Case of unconstrained type
4424 if not Is_Definite_Subtype (T) then
4426 -- In SPARK, a declaration of unconstrained type is allowed
4427 -- only for constants of type string.
4429 if Is_String_Type (T) and then not Constant_Present (N) then
4430 Check_SPARK_05_Restriction
4431 ("declaration of object of unconstrained type not allowed", N);
4432 end if;
4434 -- Nothing to do in deferred constant case
4436 if Constant_Present (N) and then No (E) then
4437 null;
4439 -- Case of no initialization present
4441 elsif No (E) then
4442 if No_Initialization (N) then
4443 null;
4445 elsif Is_Class_Wide_Type (T) then
4446 Error_Msg_N
4447 ("initialization required in class-wide declaration ", N);
4449 else
4450 Error_Msg_N
4451 ("unconstrained subtype not allowed (need initialization)",
4452 Object_Definition (N));
4454 if Is_Record_Type (T) and then Has_Discriminants (T) then
4455 Error_Msg_N
4456 ("\provide initial value or explicit discriminant values",
4457 Object_Definition (N));
4459 Error_Msg_NE
4460 ("\or give default discriminant values for type&",
4461 Object_Definition (N), T);
4463 elsif Is_Array_Type (T) then
4464 Error_Msg_N
4465 ("\provide initial value or explicit array bounds",
4466 Object_Definition (N));
4467 end if;
4468 end if;
4470 -- Case of initialization present but in error. Set initial
4471 -- expression as absent (but do not make above complaints)
4473 elsif E = Error then
4474 Set_Expression (N, Empty);
4475 E := Empty;
4477 -- Case of initialization present
4479 else
4480 -- Check restrictions in Ada 83
4482 if not Constant_Present (N) then
4484 -- Unconstrained variables not allowed in Ada 83 mode
4486 if Ada_Version = Ada_83
4487 and then Comes_From_Source (Object_Definition (N))
4488 then
4489 Error_Msg_N
4490 ("(Ada 83) unconstrained variable not allowed",
4491 Object_Definition (N));
4492 end if;
4493 end if;
4495 -- Now we constrain the variable from the initializing expression
4497 -- If the expression is an aggregate, it has been expanded into
4498 -- individual assignments. Retrieve the actual type from the
4499 -- expanded construct.
4501 if Is_Array_Type (T)
4502 and then No_Initialization (N)
4503 and then Nkind (Original_Node (E)) = N_Aggregate
4504 then
4505 Act_T := Etype (E);
4507 -- In case of class-wide interface object declarations we delay
4508 -- the generation of the equivalent record type declarations until
4509 -- its expansion because there are cases in they are not required.
4511 elsif Is_Interface (T) then
4512 null;
4514 -- In GNATprove mode, Expand_Subtype_From_Expr does nothing. Thus,
4515 -- we should prevent the generation of another Itype with the
4516 -- same name as the one already generated, or we end up with
4517 -- two identical types in GNATprove.
4519 elsif GNATprove_Mode then
4520 null;
4522 -- If the type is an unchecked union, no subtype can be built from
4523 -- the expression. Rewrite declaration as a renaming, which the
4524 -- back-end can handle properly. This is a rather unusual case,
4525 -- because most unchecked_union declarations have default values
4526 -- for discriminants and are thus not indefinite.
4528 elsif Is_Unchecked_Union (T) then
4529 if Constant_Present (N) or else Nkind (E) = N_Function_Call then
4530 Set_Ekind (Id, E_Constant);
4531 else
4532 Set_Ekind (Id, E_Variable);
4533 end if;
4535 Rewrite (N,
4536 Make_Object_Renaming_Declaration (Loc,
4537 Defining_Identifier => Id,
4538 Subtype_Mark => New_Occurrence_Of (T, Loc),
4539 Name => E));
4541 Set_Renamed_Object (Id, E);
4542 Freeze_Before (N, T);
4543 Set_Is_Frozen (Id);
4544 goto Leave;
4546 else
4547 -- Ensure that the generated subtype has a unique external name
4548 -- when the related object is public. This guarantees that the
4549 -- subtype and its bounds will not be affected by switches or
4550 -- pragmas that may offset the internal counter due to extra
4551 -- generated code.
4553 if Is_Public (Id) then
4554 Related_Id := Id;
4555 else
4556 Related_Id := Empty;
4557 end if;
4559 Expand_Subtype_From_Expr
4560 (N => N,
4561 Unc_Type => T,
4562 Subtype_Indic => Object_Definition (N),
4563 Exp => E,
4564 Related_Id => Related_Id);
4566 Act_T := Find_Type_Of_Object (Object_Definition (N), N);
4567 end if;
4569 Set_Is_Constr_Subt_For_U_Nominal (Act_T);
4571 if Aliased_Present (N) then
4572 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4573 end if;
4575 Freeze_Before (N, Act_T);
4576 Freeze_Before (N, T);
4577 end if;
4579 elsif Is_Array_Type (T)
4580 and then No_Initialization (N)
4581 and then (Nkind (Original_Node (E)) = N_Aggregate
4582 or else (Nkind (Original_Node (E)) = N_Qualified_Expression
4583 and then Nkind (Original_Node (Expression
4584 (Original_Node (E)))) = N_Aggregate))
4585 then
4586 if not Is_Entity_Name (Object_Definition (N)) then
4587 Act_T := Etype (E);
4588 Check_Compile_Time_Size (Act_T);
4590 if Aliased_Present (N) then
4591 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4592 end if;
4593 end if;
4595 -- When the given object definition and the aggregate are specified
4596 -- independently, and their lengths might differ do a length check.
4597 -- This cannot happen if the aggregate is of the form (others =>...)
4599 if not Is_Constrained (T) then
4600 null;
4602 elsif Nkind (E) = N_Raise_Constraint_Error then
4604 -- Aggregate is statically illegal. Place back in declaration
4606 Set_Expression (N, E);
4607 Set_No_Initialization (N, False);
4609 elsif T = Etype (E) then
4610 null;
4612 elsif Nkind (E) = N_Aggregate
4613 and then Present (Component_Associations (E))
4614 and then Present (Choice_List (First (Component_Associations (E))))
4615 and then
4616 Nkind (First (Choice_List (First (Component_Associations (E))))) =
4617 N_Others_Choice
4618 then
4619 null;
4621 else
4622 Apply_Length_Check (E, T);
4623 end if;
4625 -- If the type is limited unconstrained with defaulted discriminants and
4626 -- there is no expression, then the object is constrained by the
4627 -- defaults, so it is worthwhile building the corresponding subtype.
4629 elsif (Is_Limited_Record (T) or else Is_Concurrent_Type (T))
4630 and then not Is_Constrained (T)
4631 and then Has_Discriminants (T)
4632 then
4633 if No (E) then
4634 Act_T := Build_Default_Subtype (T, N);
4635 else
4636 -- Ada 2005: A limited object may be initialized by means of an
4637 -- aggregate. If the type has default discriminants it has an
4638 -- unconstrained nominal type, Its actual subtype will be obtained
4639 -- from the aggregate, and not from the default discriminants.
4641 Act_T := Etype (E);
4642 end if;
4644 Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
4646 elsif Nkind (E) = N_Function_Call
4647 and then Constant_Present (N)
4648 and then Has_Unconstrained_Elements (Etype (E))
4649 then
4650 -- The back-end has problems with constants of a discriminated type
4651 -- with defaults, if the initial value is a function call. We
4652 -- generate an intermediate temporary that will receive a reference
4653 -- to the result of the call. The initialization expression then
4654 -- becomes a dereference of that temporary.
4656 Remove_Side_Effects (E);
4658 -- If this is a constant declaration of an unconstrained type and
4659 -- the initialization is an aggregate, we can use the subtype of the
4660 -- aggregate for the declared entity because it is immutable.
4662 elsif not Is_Constrained (T)
4663 and then Has_Discriminants (T)
4664 and then Constant_Present (N)
4665 and then not Has_Unchecked_Union (T)
4666 and then Nkind (E) = N_Aggregate
4667 then
4668 Act_T := Etype (E);
4669 end if;
4671 -- Check No_Wide_Characters restriction
4673 Check_Wide_Character_Restriction (T, Object_Definition (N));
4675 -- Indicate this is not set in source. Certainly true for constants, and
4676 -- true for variables so far (will be reset for a variable if and when
4677 -- we encounter a modification in the source).
4679 Set_Never_Set_In_Source (Id);
4681 -- Now establish the proper kind and type of the object
4683 if Constant_Present (N) then
4684 Set_Ekind (Id, E_Constant);
4685 Set_Is_True_Constant (Id);
4687 else
4688 Set_Ekind (Id, E_Variable);
4690 -- A variable is set as shared passive if it appears in a shared
4691 -- passive package, and is at the outer level. This is not done for
4692 -- entities generated during expansion, because those are always
4693 -- manipulated locally.
4695 if Is_Shared_Passive (Current_Scope)
4696 and then Is_Library_Level_Entity (Id)
4697 and then Comes_From_Source (Id)
4698 then
4699 Set_Is_Shared_Passive (Id);
4700 Check_Shared_Var (Id, T, N);
4701 end if;
4703 -- Set Has_Initial_Value if initializing expression present. Note
4704 -- that if there is no initializing expression, we leave the state
4705 -- of this flag unchanged (usually it will be False, but notably in
4706 -- the case of exception choice variables, it will already be true).
4708 if Present (E) then
4709 Set_Has_Initial_Value (Id);
4710 end if;
4711 end if;
4713 -- Set the SPARK mode from the current context (may be overwritten later
4714 -- with explicit pragma).
4716 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
4717 Set_SPARK_Pragma_Inherited (Id);
4719 -- Preserve relevant elaboration-related attributes of the context which
4720 -- are no longer available or very expensive to recompute once analysis,
4721 -- resolution, and expansion are over.
4723 Mark_Elaboration_Attributes
4724 (N_Id => Id,
4725 Checks => True);
4727 -- Initialize alignment and size and capture alignment setting
4729 Init_Alignment (Id);
4730 Init_Esize (Id);
4731 Set_Optimize_Alignment_Flags (Id);
4733 -- Deal with aliased case
4735 if Aliased_Present (N) then
4736 Set_Is_Aliased (Id);
4738 -- If the object is aliased and the type is unconstrained with
4739 -- defaulted discriminants and there is no expression, then the
4740 -- object is constrained by the defaults, so it is worthwhile
4741 -- building the corresponding subtype.
4743 -- Ada 2005 (AI-363): If the aliased object is discriminated and
4744 -- unconstrained, then only establish an actual subtype if the
4745 -- nominal subtype is indefinite. In definite cases the object is
4746 -- unconstrained in Ada 2005.
4748 if No (E)
4749 and then Is_Record_Type (T)
4750 and then not Is_Constrained (T)
4751 and then Has_Discriminants (T)
4752 and then (Ada_Version < Ada_2005
4753 or else not Is_Definite_Subtype (T))
4754 then
4755 Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
4756 end if;
4757 end if;
4759 -- Now we can set the type of the object
4761 Set_Etype (Id, Act_T);
4763 -- Non-constant object is marked to be treated as volatile if type is
4764 -- volatile and we clear the Current_Value setting that may have been
4765 -- set above. Doing so for constants isn't required and might interfere
4766 -- with possible uses of the object as a static expression in contexts
4767 -- incompatible with volatility (e.g. as a case-statement alternative).
4769 if Ekind (Id) /= E_Constant and then Treat_As_Volatile (Etype (Id)) then
4770 Set_Treat_As_Volatile (Id);
4771 Set_Current_Value (Id, Empty);
4772 end if;
4774 -- Deal with controlled types
4776 if Has_Controlled_Component (Etype (Id))
4777 or else Is_Controlled (Etype (Id))
4778 then
4779 if not Is_Library_Level_Entity (Id) then
4780 Check_Restriction (No_Nested_Finalization, N);
4781 else
4782 Validate_Controlled_Object (Id);
4783 end if;
4784 end if;
4786 if Has_Task (Etype (Id)) then
4787 Check_Restriction (No_Tasking, N);
4789 -- Deal with counting max tasks
4791 -- Nothing to do if inside a generic
4793 if Inside_A_Generic then
4794 null;
4796 -- If library level entity, then count tasks
4798 elsif Is_Library_Level_Entity (Id) then
4799 Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
4801 -- If not library level entity, then indicate we don't know max
4802 -- tasks and also check task hierarchy restriction and blocking
4803 -- operation (since starting a task is definitely blocking).
4805 else
4806 Check_Restriction (Max_Tasks, N);
4807 Check_Restriction (No_Task_Hierarchy, N);
4808 Check_Potentially_Blocking_Operation (N);
4809 end if;
4811 -- A rather specialized test. If we see two tasks being declared
4812 -- of the same type in the same object declaration, and the task
4813 -- has an entry with an address clause, we know that program error
4814 -- will be raised at run time since we can't have two tasks with
4815 -- entries at the same address.
4817 if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
4818 declare
4819 E : Entity_Id;
4821 begin
4822 E := First_Entity (Etype (Id));
4823 while Present (E) loop
4824 if Ekind (E) = E_Entry
4825 and then Present (Get_Attribute_Definition_Clause
4826 (E, Attribute_Address))
4827 then
4828 Error_Msg_Warn := SPARK_Mode /= On;
4829 Error_Msg_N
4830 ("more than one task with same entry address<<", N);
4831 Error_Msg_N ("\Program_Error [<<", N);
4832 Insert_Action (N,
4833 Make_Raise_Program_Error (Loc,
4834 Reason => PE_Duplicated_Entry_Address));
4835 exit;
4836 end if;
4838 Next_Entity (E);
4839 end loop;
4840 end;
4841 end if;
4842 end if;
4844 -- Some simple constant-propagation: if the expression is a constant
4845 -- string initialized with a literal, share the literal. This avoids
4846 -- a run-time copy.
4848 if Present (E)
4849 and then Is_Entity_Name (E)
4850 and then Ekind (Entity (E)) = E_Constant
4851 and then Base_Type (Etype (E)) = Standard_String
4852 then
4853 declare
4854 Val : constant Node_Id := Constant_Value (Entity (E));
4855 begin
4856 if Present (Val) and then Nkind (Val) = N_String_Literal then
4857 Rewrite (E, New_Copy (Val));
4858 end if;
4859 end;
4860 end if;
4862 -- Another optimization: if the nominal subtype is unconstrained and
4863 -- the expression is a function call that returns an unconstrained
4864 -- type, rewrite the declaration as a renaming of the result of the
4865 -- call. The exceptions below are cases where the copy is expected,
4866 -- either by the back end (Aliased case) or by the semantics, as for
4867 -- initializing controlled types or copying tags for class-wide types.
4869 if Present (E)
4870 and then Nkind (E) = N_Explicit_Dereference
4871 and then Nkind (Original_Node (E)) = N_Function_Call
4872 and then not Is_Library_Level_Entity (Id)
4873 and then not Is_Constrained (Underlying_Type (T))
4874 and then not Is_Aliased (Id)
4875 and then not Is_Class_Wide_Type (T)
4876 and then not Is_Controlled (T)
4877 and then not Has_Controlled_Component (Base_Type (T))
4878 and then Expander_Active
4879 then
4880 Rewrite (N,
4881 Make_Object_Renaming_Declaration (Loc,
4882 Defining_Identifier => Id,
4883 Access_Definition => Empty,
4884 Subtype_Mark => New_Occurrence_Of
4885 (Base_Type (Etype (Id)), Loc),
4886 Name => E));
4888 Set_Renamed_Object (Id, E);
4890 -- Force generation of debugging information for the constant and for
4891 -- the renamed function call.
4893 Set_Debug_Info_Needed (Id);
4894 Set_Debug_Info_Needed (Entity (Prefix (E)));
4895 end if;
4897 if Present (Prev_Entity)
4898 and then Is_Frozen (Prev_Entity)
4899 and then not Error_Posted (Id)
4900 then
4901 Error_Msg_N ("full constant declaration appears too late", N);
4902 end if;
4904 Check_Eliminated (Id);
4906 -- Deal with setting In_Private_Part flag if in private part
4908 if Ekind (Scope (Id)) = E_Package
4909 and then In_Private_Part (Scope (Id))
4910 then
4911 Set_In_Private_Part (Id);
4912 end if;
4914 <<Leave>>
4915 -- Initialize the refined state of a variable here because this is a
4916 -- common destination for legal and illegal object declarations.
4918 if Ekind (Id) = E_Variable then
4919 Set_Encapsulating_State (Id, Empty);
4920 end if;
4922 if Has_Aspects (N) then
4923 Analyze_Aspect_Specifications (N, Id);
4924 end if;
4926 Analyze_Dimension (N);
4928 -- Verify whether the object declaration introduces an illegal hidden
4929 -- state within a package subject to a null abstract state.
4931 if Ekind (Id) = E_Variable then
4932 Check_No_Hidden_State (Id);
4933 end if;
4935 Restore_Ghost_Mode (Saved_GM);
4936 end Analyze_Object_Declaration;
4938 ---------------------------
4939 -- Analyze_Others_Choice --
4940 ---------------------------
4942 -- Nothing to do for the others choice node itself, the semantic analysis
4943 -- of the others choice will occur as part of the processing of the parent
4945 procedure Analyze_Others_Choice (N : Node_Id) is
4946 pragma Warnings (Off, N);
4947 begin
4948 null;
4949 end Analyze_Others_Choice;
4951 -------------------------------------------
4952 -- Analyze_Private_Extension_Declaration --
4953 -------------------------------------------
4955 procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
4956 Indic : constant Node_Id := Subtype_Indication (N);
4957 T : constant Entity_Id := Defining_Identifier (N);
4958 Iface : Entity_Id;
4959 Iface_Elmt : Elmt_Id;
4960 Parent_Base : Entity_Id;
4961 Parent_Type : Entity_Id;
4963 begin
4964 -- Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
4966 if Is_Non_Empty_List (Interface_List (N)) then
4967 declare
4968 Intf : Node_Id;
4969 T : Entity_Id;
4971 begin
4972 Intf := First (Interface_List (N));
4973 while Present (Intf) loop
4974 T := Find_Type_Of_Subtype_Indic (Intf);
4976 Diagnose_Interface (Intf, T);
4977 Next (Intf);
4978 end loop;
4979 end;
4980 end if;
4982 Generate_Definition (T);
4984 -- For other than Ada 2012, just enter the name in the current scope
4986 if Ada_Version < Ada_2012 then
4987 Enter_Name (T);
4989 -- Ada 2012 (AI05-0162): Enter the name in the current scope handling
4990 -- case of private type that completes an incomplete type.
4992 else
4993 declare
4994 Prev : Entity_Id;
4996 begin
4997 Prev := Find_Type_Name (N);
4999 pragma Assert (Prev = T
5000 or else (Ekind (Prev) = E_Incomplete_Type
5001 and then Present (Full_View (Prev))
5002 and then Full_View (Prev) = T));
5003 end;
5004 end if;
5006 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
5007 Parent_Base := Base_Type (Parent_Type);
5009 if Parent_Type = Any_Type or else Etype (Parent_Type) = Any_Type then
5010 Set_Ekind (T, Ekind (Parent_Type));
5011 Set_Etype (T, Any_Type);
5012 goto Leave;
5014 elsif not Is_Tagged_Type (Parent_Type) then
5015 Error_Msg_N
5016 ("parent of type extension must be a tagged type ", Indic);
5017 goto Leave;
5019 elsif Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
5020 Error_Msg_N ("premature derivation of incomplete type", Indic);
5021 goto Leave;
5023 elsif Is_Concurrent_Type (Parent_Type) then
5024 Error_Msg_N
5025 ("parent type of a private extension cannot be a synchronized "
5026 & "tagged type (RM 3.9.1 (3/1))", N);
5028 Set_Etype (T, Any_Type);
5029 Set_Ekind (T, E_Limited_Private_Type);
5030 Set_Private_Dependents (T, New_Elmt_List);
5031 Set_Error_Posted (T);
5032 goto Leave;
5033 end if;
5035 -- Perhaps the parent type should be changed to the class-wide type's
5036 -- specific type in this case to prevent cascading errors ???
5038 if Is_Class_Wide_Type (Parent_Type) then
5039 Error_Msg_N
5040 ("parent of type extension must not be a class-wide type", Indic);
5041 goto Leave;
5042 end if;
5044 if (not Is_Package_Or_Generic_Package (Current_Scope)
5045 and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
5046 or else In_Private_Part (Current_Scope)
5047 then
5048 Error_Msg_N ("invalid context for private extension", N);
5049 end if;
5051 -- Set common attributes
5053 Set_Is_Pure (T, Is_Pure (Current_Scope));
5054 Set_Scope (T, Current_Scope);
5055 Set_Ekind (T, E_Record_Type_With_Private);
5056 Init_Size_Align (T);
5057 Set_Default_SSO (T);
5058 Set_No_Reordering (T, No_Component_Reordering);
5060 Set_Etype (T, Parent_Base);
5061 Propagate_Concurrent_Flags (T, Parent_Base);
5063 Set_Convention (T, Convention (Parent_Type));
5064 Set_First_Rep_Item (T, First_Rep_Item (Parent_Type));
5065 Set_Is_First_Subtype (T);
5066 Make_Class_Wide_Type (T);
5068 if Unknown_Discriminants_Present (N) then
5069 Set_Discriminant_Constraint (T, No_Elist);
5070 end if;
5072 Build_Derived_Record_Type (N, Parent_Type, T);
5074 -- A private extension inherits the Default_Initial_Condition pragma
5075 -- coming from any parent type within the derivation chain.
5077 if Has_DIC (Parent_Type) then
5078 Set_Has_Inherited_DIC (T);
5079 end if;
5081 -- A private extension inherits any class-wide invariants coming from a
5082 -- parent type or an interface. Note that the invariant procedure of the
5083 -- parent type should not be inherited because the private extension may
5084 -- define invariants of its own.
5086 if Has_Inherited_Invariants (Parent_Type)
5087 or else Has_Inheritable_Invariants (Parent_Type)
5088 then
5089 Set_Has_Inherited_Invariants (T);
5091 elsif Present (Interfaces (T)) then
5092 Iface_Elmt := First_Elmt (Interfaces (T));
5093 while Present (Iface_Elmt) loop
5094 Iface := Node (Iface_Elmt);
5096 if Has_Inheritable_Invariants (Iface) then
5097 Set_Has_Inherited_Invariants (T);
5098 exit;
5099 end if;
5101 Next_Elmt (Iface_Elmt);
5102 end loop;
5103 end if;
5105 -- Ada 2005 (AI-443): Synchronized private extension or a rewritten
5106 -- synchronized formal derived type.
5108 if Ada_Version >= Ada_2005 and then Synchronized_Present (N) then
5109 Set_Is_Limited_Record (T);
5111 -- Formal derived type case
5113 if Is_Generic_Type (T) then
5115 -- The parent must be a tagged limited type or a synchronized
5116 -- interface.
5118 if (not Is_Tagged_Type (Parent_Type)
5119 or else not Is_Limited_Type (Parent_Type))
5120 and then
5121 (not Is_Interface (Parent_Type)
5122 or else not Is_Synchronized_Interface (Parent_Type))
5123 then
5124 Error_Msg_NE
5125 ("parent type of & must be tagged limited or synchronized",
5126 N, T);
5127 end if;
5129 -- The progenitors (if any) must be limited or synchronized
5130 -- interfaces.
5132 if Present (Interfaces (T)) then
5133 Iface_Elmt := First_Elmt (Interfaces (T));
5134 while Present (Iface_Elmt) loop
5135 Iface := Node (Iface_Elmt);
5137 if not Is_Limited_Interface (Iface)
5138 and then not Is_Synchronized_Interface (Iface)
5139 then
5140 Error_Msg_NE
5141 ("progenitor & must be limited or synchronized",
5142 N, Iface);
5143 end if;
5145 Next_Elmt (Iface_Elmt);
5146 end loop;
5147 end if;
5149 -- Regular derived extension, the parent must be a limited or
5150 -- synchronized interface.
5152 else
5153 if not Is_Interface (Parent_Type)
5154 or else (not Is_Limited_Interface (Parent_Type)
5155 and then not Is_Synchronized_Interface (Parent_Type))
5156 then
5157 Error_Msg_NE
5158 ("parent type of & must be limited interface", N, T);
5159 end if;
5160 end if;
5162 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
5163 -- extension with a synchronized parent must be explicitly declared
5164 -- synchronized, because the full view will be a synchronized type.
5165 -- This must be checked before the check for limited types below,
5166 -- to ensure that types declared limited are not allowed to extend
5167 -- synchronized interfaces.
5169 elsif Is_Interface (Parent_Type)
5170 and then Is_Synchronized_Interface (Parent_Type)
5171 and then not Synchronized_Present (N)
5172 then
5173 Error_Msg_NE
5174 ("private extension of& must be explicitly synchronized",
5175 N, Parent_Type);
5177 elsif Limited_Present (N) then
5178 Set_Is_Limited_Record (T);
5180 if not Is_Limited_Type (Parent_Type)
5181 and then
5182 (not Is_Interface (Parent_Type)
5183 or else not Is_Limited_Interface (Parent_Type))
5184 then
5185 Error_Msg_NE ("parent type& of limited extension must be limited",
5186 N, Parent_Type);
5187 end if;
5188 end if;
5190 -- Remember that its parent type has a private extension. Used to warn
5191 -- on public primitives of the parent type defined after its private
5192 -- extensions (see Check_Dispatching_Operation).
5194 Set_Has_Private_Extension (Parent_Type);
5196 <<Leave>>
5197 if Has_Aspects (N) then
5198 Analyze_Aspect_Specifications (N, T);
5199 end if;
5200 end Analyze_Private_Extension_Declaration;
5202 ---------------------------------
5203 -- Analyze_Subtype_Declaration --
5204 ---------------------------------
5206 procedure Analyze_Subtype_Declaration
5207 (N : Node_Id;
5208 Skip : Boolean := False)
5210 Id : constant Entity_Id := Defining_Identifier (N);
5211 R_Checks : Check_Result;
5212 T : Entity_Id;
5214 begin
5215 Generate_Definition (Id);
5216 Set_Is_Pure (Id, Is_Pure (Current_Scope));
5217 Init_Size_Align (Id);
5219 -- The following guard condition on Enter_Name is to handle cases where
5220 -- the defining identifier has already been entered into the scope but
5221 -- the declaration as a whole needs to be analyzed.
5223 -- This case in particular happens for derived enumeration types. The
5224 -- derived enumeration type is processed as an inserted enumeration type
5225 -- declaration followed by a rewritten subtype declaration. The defining
5226 -- identifier, however, is entered into the name scope very early in the
5227 -- processing of the original type declaration and therefore needs to be
5228 -- avoided here, when the created subtype declaration is analyzed. (See
5229 -- Build_Derived_Types)
5231 -- This also happens when the full view of a private type is derived
5232 -- type with constraints. In this case the entity has been introduced
5233 -- in the private declaration.
5235 -- Finally this happens in some complex cases when validity checks are
5236 -- enabled, where the same subtype declaration may be analyzed twice.
5237 -- This can happen if the subtype is created by the pre-analysis of
5238 -- an attribute tht gives the range of a loop statement, and the loop
5239 -- itself appears within an if_statement that will be rewritten during
5240 -- expansion.
5242 if Skip
5243 or else (Present (Etype (Id))
5244 and then (Is_Private_Type (Etype (Id))
5245 or else Is_Task_Type (Etype (Id))
5246 or else Is_Rewrite_Substitution (N)))
5247 then
5248 null;
5250 elsif Current_Entity (Id) = Id then
5251 null;
5253 else
5254 Enter_Name (Id);
5255 end if;
5257 T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
5259 -- Class-wide equivalent types of records with unknown discriminants
5260 -- involve the generation of an itype which serves as the private view
5261 -- of a constrained record subtype. In such cases the base type of the
5262 -- current subtype we are processing is the private itype. Use the full
5263 -- of the private itype when decorating various attributes.
5265 if Is_Itype (T)
5266 and then Is_Private_Type (T)
5267 and then Present (Full_View (T))
5268 then
5269 T := Full_View (T);
5270 end if;
5272 -- Inherit common attributes
5274 Set_Is_Volatile (Id, Is_Volatile (T));
5275 Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
5276 Set_Is_Generic_Type (Id, Is_Generic_Type (Base_Type (T)));
5277 Set_Convention (Id, Convention (T));
5279 -- If ancestor has predicates then so does the subtype, and in addition
5280 -- we must delay the freeze to properly arrange predicate inheritance.
5282 -- The Ancestor_Type test is really unpleasant, there seem to be cases
5283 -- in which T = ID, so the above tests and assignments do nothing???
5285 if Has_Predicates (T)
5286 or else (Present (Ancestor_Subtype (T))
5287 and then Has_Predicates (Ancestor_Subtype (T)))
5288 then
5289 Set_Has_Predicates (Id);
5290 Set_Has_Delayed_Freeze (Id);
5292 -- Generated subtypes inherit the predicate function from the parent
5293 -- (no aspects to examine on the generated declaration).
5295 if not Comes_From_Source (N) then
5296 Set_Ekind (Id, Ekind (T));
5298 if Present (Predicate_Function (T)) then
5299 Set_Predicate_Function (Id, Predicate_Function (T));
5301 elsif Present (Ancestor_Subtype (T))
5302 and then Has_Predicates (Ancestor_Subtype (T))
5303 and then Present (Predicate_Function (Ancestor_Subtype (T)))
5304 then
5305 Set_Predicate_Function (Id,
5306 Predicate_Function (Ancestor_Subtype (T)));
5307 end if;
5308 end if;
5309 end if;
5311 -- Subtype of Boolean cannot have a constraint in SPARK
5313 if Is_Boolean_Type (T)
5314 and then Nkind (Subtype_Indication (N)) = N_Subtype_Indication
5315 then
5316 Check_SPARK_05_Restriction
5317 ("subtype of Boolean cannot have constraint", N);
5318 end if;
5320 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5321 declare
5322 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
5323 One_Cstr : Node_Id;
5324 Low : Node_Id;
5325 High : Node_Id;
5327 begin
5328 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint then
5329 One_Cstr := First (Constraints (Cstr));
5330 while Present (One_Cstr) loop
5332 -- Index or discriminant constraint in SPARK must be a
5333 -- subtype mark.
5335 if not
5336 Nkind_In (One_Cstr, N_Identifier, N_Expanded_Name)
5337 then
5338 Check_SPARK_05_Restriction
5339 ("subtype mark required", One_Cstr);
5341 -- String subtype must have a lower bound of 1 in SPARK.
5342 -- Note that we do not need to test for the non-static case
5343 -- here, since that was already taken care of in
5344 -- Process_Range_Expr_In_Decl.
5346 elsif Base_Type (T) = Standard_String then
5347 Get_Index_Bounds (One_Cstr, Low, High);
5349 if Is_OK_Static_Expression (Low)
5350 and then Expr_Value (Low) /= 1
5351 then
5352 Check_SPARK_05_Restriction
5353 ("String subtype must have lower bound of 1", N);
5354 end if;
5355 end if;
5357 Next (One_Cstr);
5358 end loop;
5359 end if;
5360 end;
5361 end if;
5363 -- In the case where there is no constraint given in the subtype
5364 -- indication, Process_Subtype just returns the Subtype_Mark, so its
5365 -- semantic attributes must be established here.
5367 if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
5368 Set_Etype (Id, Base_Type (T));
5370 -- Subtype of unconstrained array without constraint is not allowed
5371 -- in SPARK.
5373 if Is_Array_Type (T) and then not Is_Constrained (T) then
5374 Check_SPARK_05_Restriction
5375 ("subtype of unconstrained array must have constraint", N);
5376 end if;
5378 case Ekind (T) is
5379 when Array_Kind =>
5380 Set_Ekind (Id, E_Array_Subtype);
5381 Copy_Array_Subtype_Attributes (Id, T);
5383 when Decimal_Fixed_Point_Kind =>
5384 Set_Ekind (Id, E_Decimal_Fixed_Point_Subtype);
5385 Set_Digits_Value (Id, Digits_Value (T));
5386 Set_Delta_Value (Id, Delta_Value (T));
5387 Set_Scale_Value (Id, Scale_Value (T));
5388 Set_Small_Value (Id, Small_Value (T));
5389 Set_Scalar_Range (Id, Scalar_Range (T));
5390 Set_Machine_Radix_10 (Id, Machine_Radix_10 (T));
5391 Set_Is_Constrained (Id, Is_Constrained (T));
5392 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5393 Set_RM_Size (Id, RM_Size (T));
5395 when Enumeration_Kind =>
5396 Set_Ekind (Id, E_Enumeration_Subtype);
5397 Set_First_Literal (Id, First_Literal (Base_Type (T)));
5398 Set_Scalar_Range (Id, Scalar_Range (T));
5399 Set_Is_Character_Type (Id, Is_Character_Type (T));
5400 Set_Is_Constrained (Id, Is_Constrained (T));
5401 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5402 Set_RM_Size (Id, RM_Size (T));
5403 Inherit_Predicate_Flags (Id, T);
5405 when Ordinary_Fixed_Point_Kind =>
5406 Set_Ekind (Id, E_Ordinary_Fixed_Point_Subtype);
5407 Set_Scalar_Range (Id, Scalar_Range (T));
5408 Set_Small_Value (Id, Small_Value (T));
5409 Set_Delta_Value (Id, Delta_Value (T));
5410 Set_Is_Constrained (Id, Is_Constrained (T));
5411 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5412 Set_RM_Size (Id, RM_Size (T));
5414 when Float_Kind =>
5415 Set_Ekind (Id, E_Floating_Point_Subtype);
5416 Set_Scalar_Range (Id, Scalar_Range (T));
5417 Set_Digits_Value (Id, Digits_Value (T));
5418 Set_Is_Constrained (Id, Is_Constrained (T));
5420 -- If the floating point type has dimensions, these will be
5421 -- inherited subsequently when Analyze_Dimensions is called.
5423 when Signed_Integer_Kind =>
5424 Set_Ekind (Id, E_Signed_Integer_Subtype);
5425 Set_Scalar_Range (Id, Scalar_Range (T));
5426 Set_Is_Constrained (Id, Is_Constrained (T));
5427 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5428 Set_RM_Size (Id, RM_Size (T));
5429 Inherit_Predicate_Flags (Id, T);
5431 when Modular_Integer_Kind =>
5432 Set_Ekind (Id, E_Modular_Integer_Subtype);
5433 Set_Scalar_Range (Id, Scalar_Range (T));
5434 Set_Is_Constrained (Id, Is_Constrained (T));
5435 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5436 Set_RM_Size (Id, RM_Size (T));
5437 Inherit_Predicate_Flags (Id, T);
5439 when Class_Wide_Kind =>
5440 Set_Ekind (Id, E_Class_Wide_Subtype);
5441 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5442 Set_Cloned_Subtype (Id, T);
5443 Set_Is_Tagged_Type (Id, True);
5444 Set_Has_Unknown_Discriminants
5445 (Id, True);
5446 Set_No_Tagged_Streams_Pragma
5447 (Id, No_Tagged_Streams_Pragma (T));
5449 if Ekind (T) = E_Class_Wide_Subtype then
5450 Set_Equivalent_Type (Id, Equivalent_Type (T));
5451 end if;
5453 when E_Record_Subtype
5454 | E_Record_Type
5456 Set_Ekind (Id, E_Record_Subtype);
5458 if Ekind (T) = E_Record_Subtype
5459 and then Present (Cloned_Subtype (T))
5460 then
5461 Set_Cloned_Subtype (Id, Cloned_Subtype (T));
5462 else
5463 Set_Cloned_Subtype (Id, T);
5464 end if;
5466 Set_First_Entity (Id, First_Entity (T));
5467 Set_Last_Entity (Id, Last_Entity (T));
5468 Set_Has_Discriminants (Id, Has_Discriminants (T));
5469 Set_Is_Constrained (Id, Is_Constrained (T));
5470 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5471 Set_Has_Implicit_Dereference
5472 (Id, Has_Implicit_Dereference (T));
5473 Set_Has_Unknown_Discriminants
5474 (Id, Has_Unknown_Discriminants (T));
5476 if Has_Discriminants (T) then
5477 Set_Discriminant_Constraint
5478 (Id, Discriminant_Constraint (T));
5479 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5481 elsif Has_Unknown_Discriminants (Id) then
5482 Set_Discriminant_Constraint (Id, No_Elist);
5483 end if;
5485 if Is_Tagged_Type (T) then
5486 Set_Is_Tagged_Type (Id, True);
5487 Set_No_Tagged_Streams_Pragma
5488 (Id, No_Tagged_Streams_Pragma (T));
5489 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5490 Set_Direct_Primitive_Operations
5491 (Id, Direct_Primitive_Operations (T));
5492 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5494 if Is_Interface (T) then
5495 Set_Is_Interface (Id);
5496 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
5497 end if;
5498 end if;
5500 when Private_Kind =>
5501 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
5502 Set_Has_Discriminants (Id, Has_Discriminants (T));
5503 Set_Is_Constrained (Id, Is_Constrained (T));
5504 Set_First_Entity (Id, First_Entity (T));
5505 Set_Last_Entity (Id, Last_Entity (T));
5506 Set_Private_Dependents (Id, New_Elmt_List);
5507 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5508 Set_Has_Implicit_Dereference
5509 (Id, Has_Implicit_Dereference (T));
5510 Set_Has_Unknown_Discriminants
5511 (Id, Has_Unknown_Discriminants (T));
5512 Set_Known_To_Have_Preelab_Init
5513 (Id, Known_To_Have_Preelab_Init (T));
5515 if Is_Tagged_Type (T) then
5516 Set_Is_Tagged_Type (Id);
5517 Set_No_Tagged_Streams_Pragma (Id,
5518 No_Tagged_Streams_Pragma (T));
5519 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5520 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5521 Set_Direct_Primitive_Operations (Id,
5522 Direct_Primitive_Operations (T));
5523 end if;
5525 -- In general the attributes of the subtype of a private type
5526 -- are the attributes of the partial view of parent. However,
5527 -- the full view may be a discriminated type, and the subtype
5528 -- must share the discriminant constraint to generate correct
5529 -- calls to initialization procedures.
5531 if Has_Discriminants (T) then
5532 Set_Discriminant_Constraint
5533 (Id, Discriminant_Constraint (T));
5534 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5536 elsif Present (Full_View (T))
5537 and then Has_Discriminants (Full_View (T))
5538 then
5539 Set_Discriminant_Constraint
5540 (Id, Discriminant_Constraint (Full_View (T)));
5541 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5543 -- This would seem semantically correct, but apparently
5544 -- generates spurious errors about missing components ???
5546 -- Set_Has_Discriminants (Id);
5547 end if;
5549 Prepare_Private_Subtype_Completion (Id, N);
5551 -- If this is the subtype of a constrained private type with
5552 -- discriminants that has got a full view and we also have
5553 -- built a completion just above, show that the completion
5554 -- is a clone of the full view to the back-end.
5556 if Has_Discriminants (T)
5557 and then not Has_Unknown_Discriminants (T)
5558 and then not Is_Empty_Elmt_List (Discriminant_Constraint (T))
5559 and then Present (Full_View (T))
5560 and then Present (Full_View (Id))
5561 then
5562 Set_Cloned_Subtype (Full_View (Id), Full_View (T));
5563 end if;
5565 when Access_Kind =>
5566 Set_Ekind (Id, E_Access_Subtype);
5567 Set_Is_Constrained (Id, Is_Constrained (T));
5568 Set_Is_Access_Constant
5569 (Id, Is_Access_Constant (T));
5570 Set_Directly_Designated_Type
5571 (Id, Designated_Type (T));
5572 Set_Can_Never_Be_Null (Id, Can_Never_Be_Null (T));
5574 -- A Pure library_item must not contain the declaration of a
5575 -- named access type, except within a subprogram, generic
5576 -- subprogram, task unit, or protected unit, or if it has
5577 -- a specified Storage_Size of zero (RM05-10.2.1(15.4-15.5)).
5579 if Comes_From_Source (Id)
5580 and then In_Pure_Unit
5581 and then not In_Subprogram_Task_Protected_Unit
5582 and then not No_Pool_Assigned (Id)
5583 then
5584 Error_Msg_N
5585 ("named access types not allowed in pure unit", N);
5586 end if;
5588 when Concurrent_Kind =>
5589 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
5590 Set_Corresponding_Record_Type (Id,
5591 Corresponding_Record_Type (T));
5592 Set_First_Entity (Id, First_Entity (T));
5593 Set_First_Private_Entity (Id, First_Private_Entity (T));
5594 Set_Has_Discriminants (Id, Has_Discriminants (T));
5595 Set_Is_Constrained (Id, Is_Constrained (T));
5596 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5597 Set_Last_Entity (Id, Last_Entity (T));
5599 if Is_Tagged_Type (T) then
5600 Set_No_Tagged_Streams_Pragma
5601 (Id, No_Tagged_Streams_Pragma (T));
5602 end if;
5604 if Has_Discriminants (T) then
5605 Set_Discriminant_Constraint
5606 (Id, Discriminant_Constraint (T));
5607 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5608 end if;
5610 when Incomplete_Kind =>
5611 if Ada_Version >= Ada_2005 then
5613 -- In Ada 2005 an incomplete type can be explicitly tagged:
5614 -- propagate indication. Note that we also have to include
5615 -- subtypes for Ada 2012 extended use of incomplete types.
5617 Set_Ekind (Id, E_Incomplete_Subtype);
5618 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5619 Set_Private_Dependents (Id, New_Elmt_List);
5621 if Is_Tagged_Type (Id) then
5622 Set_No_Tagged_Streams_Pragma
5623 (Id, No_Tagged_Streams_Pragma (T));
5624 Set_Direct_Primitive_Operations (Id, New_Elmt_List);
5625 end if;
5627 -- Ada 2005 (AI-412): Decorate an incomplete subtype of an
5628 -- incomplete type visible through a limited with clause.
5630 if From_Limited_With (T)
5631 and then Present (Non_Limited_View (T))
5632 then
5633 Set_From_Limited_With (Id);
5634 Set_Non_Limited_View (Id, Non_Limited_View (T));
5636 -- Ada 2005 (AI-412): Add the regular incomplete subtype
5637 -- to the private dependents of the original incomplete
5638 -- type for future transformation.
5640 else
5641 Append_Elmt (Id, Private_Dependents (T));
5642 end if;
5644 -- If the subtype name denotes an incomplete type an error
5645 -- was already reported by Process_Subtype.
5647 else
5648 Set_Etype (Id, Any_Type);
5649 end if;
5651 when others =>
5652 raise Program_Error;
5653 end case;
5654 end if;
5656 if Etype (Id) = Any_Type then
5657 goto Leave;
5658 end if;
5660 -- Some common processing on all types
5662 Set_Size_Info (Id, T);
5663 Set_First_Rep_Item (Id, First_Rep_Item (T));
5665 -- If the parent type is a generic actual, so is the subtype. This may
5666 -- happen in a nested instance. Why Comes_From_Source test???
5668 if not Comes_From_Source (N) then
5669 Set_Is_Generic_Actual_Type (Id, Is_Generic_Actual_Type (T));
5670 end if;
5672 -- If this is a subtype declaration for an actual in an instance,
5673 -- inherit static and dynamic predicates if any.
5675 -- If declaration has no aspect specifications, inherit predicate
5676 -- info as well. Unclear how to handle the case of both specified
5677 -- and inherited predicates ??? Other inherited aspects, such as
5678 -- invariants, should be OK, but the combination with later pragmas
5679 -- may also require special merging.
5681 if Has_Predicates (T)
5682 and then Present (Predicate_Function (T))
5683 and then
5684 ((In_Instance and then not Comes_From_Source (N))
5685 or else No (Aspect_Specifications (N)))
5686 then
5687 Set_Subprograms_For_Type (Id, Subprograms_For_Type (T));
5689 if Has_Static_Predicate (T) then
5690 Set_Has_Static_Predicate (Id);
5691 Set_Static_Discrete_Predicate (Id, Static_Discrete_Predicate (T));
5692 end if;
5693 end if;
5695 -- Remaining processing depends on characteristics of base type
5697 T := Etype (Id);
5699 Set_Is_Immediately_Visible (Id, True);
5700 Set_Depends_On_Private (Id, Has_Private_Component (T));
5701 Set_Is_Descendant_Of_Address (Id, Is_Descendant_Of_Address (T));
5703 if Is_Interface (T) then
5704 Set_Is_Interface (Id);
5705 end if;
5707 if Present (Generic_Parent_Type (N))
5708 and then
5709 (Nkind (Parent (Generic_Parent_Type (N))) /=
5710 N_Formal_Type_Declaration
5711 or else Nkind (Formal_Type_Definition
5712 (Parent (Generic_Parent_Type (N)))) /=
5713 N_Formal_Private_Type_Definition)
5714 then
5715 if Is_Tagged_Type (Id) then
5717 -- If this is a generic actual subtype for a synchronized type,
5718 -- the primitive operations are those of the corresponding record
5719 -- for which there is a separate subtype declaration.
5721 if Is_Concurrent_Type (Id) then
5722 null;
5723 elsif Is_Class_Wide_Type (Id) then
5724 Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
5725 else
5726 Derive_Subprograms (Generic_Parent_Type (N), Id, T);
5727 end if;
5729 elsif Scope (Etype (Id)) /= Standard_Standard then
5730 Derive_Subprograms (Generic_Parent_Type (N), Id);
5731 end if;
5732 end if;
5734 if Is_Private_Type (T) and then Present (Full_View (T)) then
5735 Conditional_Delay (Id, Full_View (T));
5737 -- The subtypes of components or subcomponents of protected types
5738 -- do not need freeze nodes, which would otherwise appear in the
5739 -- wrong scope (before the freeze node for the protected type). The
5740 -- proper subtypes are those of the subcomponents of the corresponding
5741 -- record.
5743 elsif Ekind (Scope (Id)) /= E_Protected_Type
5744 and then Present (Scope (Scope (Id))) -- error defense
5745 and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
5746 then
5747 Conditional_Delay (Id, T);
5748 end if;
5750 -- If we have a subtype of an incomplete type whose full type is a
5751 -- derived numeric type, we need to have a freeze node for the subtype.
5752 -- Otherwise gigi will complain while computing the (static) bounds of
5753 -- the subtype.
5755 if Is_Itype (T)
5756 and then Is_Elementary_Type (Id)
5757 and then Etype (Id) /= Id
5758 then
5759 declare
5760 Partial : constant Entity_Id :=
5761 Incomplete_Or_Partial_View (First_Subtype (Id));
5762 begin
5763 if Present (Partial)
5764 and then Ekind (Partial) = E_Incomplete_Type
5765 then
5766 Set_Has_Delayed_Freeze (Id);
5767 end if;
5768 end;
5769 end if;
5771 -- Check that Constraint_Error is raised for a scalar subtype indication
5772 -- when the lower or upper bound of a non-null range lies outside the
5773 -- range of the type mark.
5775 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5776 if Is_Scalar_Type (Etype (Id))
5777 and then Scalar_Range (Id) /=
5778 Scalar_Range
5779 (Etype (Subtype_Mark (Subtype_Indication (N))))
5780 then
5781 Apply_Range_Check
5782 (Scalar_Range (Id),
5783 Etype (Subtype_Mark (Subtype_Indication (N))));
5785 -- In the array case, check compatibility for each index
5787 elsif Is_Array_Type (Etype (Id)) and then Present (First_Index (Id))
5788 then
5789 -- This really should be a subprogram that finds the indications
5790 -- to check???
5792 declare
5793 Subt_Index : Node_Id := First_Index (Id);
5794 Target_Index : Node_Id :=
5795 First_Index (Etype
5796 (Subtype_Mark (Subtype_Indication (N))));
5797 Has_Dyn_Chk : Boolean := Has_Dynamic_Range_Check (N);
5799 begin
5800 while Present (Subt_Index) loop
5801 if ((Nkind (Subt_Index) = N_Identifier
5802 and then Ekind (Entity (Subt_Index)) in Scalar_Kind)
5803 or else Nkind (Subt_Index) = N_Subtype_Indication)
5804 and then
5805 Nkind (Scalar_Range (Etype (Subt_Index))) = N_Range
5806 then
5807 declare
5808 Target_Typ : constant Entity_Id :=
5809 Etype (Target_Index);
5810 begin
5811 R_Checks :=
5812 Get_Range_Checks
5813 (Scalar_Range (Etype (Subt_Index)),
5814 Target_Typ,
5815 Etype (Subt_Index),
5816 Defining_Identifier (N));
5818 -- Reset Has_Dynamic_Range_Check on the subtype to
5819 -- prevent elision of the index check due to a dynamic
5820 -- check generated for a preceding index (needed since
5821 -- Insert_Range_Checks tries to avoid generating
5822 -- redundant checks on a given declaration).
5824 Set_Has_Dynamic_Range_Check (N, False);
5826 Insert_Range_Checks
5827 (R_Checks,
5829 Target_Typ,
5830 Sloc (Defining_Identifier (N)));
5832 -- Record whether this index involved a dynamic check
5834 Has_Dyn_Chk :=
5835 Has_Dyn_Chk or else Has_Dynamic_Range_Check (N);
5836 end;
5837 end if;
5839 Next_Index (Subt_Index);
5840 Next_Index (Target_Index);
5841 end loop;
5843 -- Finally, mark whether the subtype involves dynamic checks
5845 Set_Has_Dynamic_Range_Check (N, Has_Dyn_Chk);
5846 end;
5847 end if;
5848 end if;
5850 Set_Optimize_Alignment_Flags (Id);
5851 Check_Eliminated (Id);
5853 <<Leave>>
5854 if Has_Aspects (N) then
5855 Analyze_Aspect_Specifications (N, Id);
5856 end if;
5858 Analyze_Dimension (N);
5860 -- Check No_Dynamic_Sized_Objects restriction, which disallows subtype
5861 -- indications on composite types where the constraints are dynamic.
5862 -- Note that object declarations and aggregates generate implicit
5863 -- subtype declarations, which this covers. One special case is that the
5864 -- implicitly generated "=" for discriminated types includes an
5865 -- offending subtype declaration, which is harmless, so we ignore it
5866 -- here.
5868 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5869 declare
5870 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
5871 begin
5872 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint
5873 and then not (Is_Internal (Id)
5874 and then Is_TSS (Scope (Id),
5875 TSS_Composite_Equality))
5876 and then not Within_Init_Proc
5877 and then not All_Composite_Constraints_Static (Cstr)
5878 then
5879 Check_Restriction (No_Dynamic_Sized_Objects, Cstr);
5880 end if;
5881 end;
5882 end if;
5883 end Analyze_Subtype_Declaration;
5885 --------------------------------
5886 -- Analyze_Subtype_Indication --
5887 --------------------------------
5889 procedure Analyze_Subtype_Indication (N : Node_Id) is
5890 T : constant Entity_Id := Subtype_Mark (N);
5891 R : constant Node_Id := Range_Expression (Constraint (N));
5893 begin
5894 Analyze (T);
5896 if R /= Error then
5897 Analyze (R);
5898 Set_Etype (N, Etype (R));
5899 Resolve (R, Entity (T));
5900 else
5901 Set_Error_Posted (R);
5902 Set_Error_Posted (T);
5903 end if;
5904 end Analyze_Subtype_Indication;
5906 --------------------------
5907 -- Analyze_Variant_Part --
5908 --------------------------
5910 procedure Analyze_Variant_Part (N : Node_Id) is
5911 Discr_Name : Node_Id;
5912 Discr_Type : Entity_Id;
5914 procedure Process_Variant (A : Node_Id);
5915 -- Analyze declarations for a single variant
5917 package Analyze_Variant_Choices is
5918 new Generic_Analyze_Choices (Process_Variant);
5919 use Analyze_Variant_Choices;
5921 ---------------------
5922 -- Process_Variant --
5923 ---------------------
5925 procedure Process_Variant (A : Node_Id) is
5926 CL : constant Node_Id := Component_List (A);
5927 begin
5928 if not Null_Present (CL) then
5929 Analyze_Declarations (Component_Items (CL));
5931 if Present (Variant_Part (CL)) then
5932 Analyze (Variant_Part (CL));
5933 end if;
5934 end if;
5935 end Process_Variant;
5937 -- Start of processing for Analyze_Variant_Part
5939 begin
5940 Discr_Name := Name (N);
5941 Analyze (Discr_Name);
5943 -- If Discr_Name bad, get out (prevent cascaded errors)
5945 if Etype (Discr_Name) = Any_Type then
5946 return;
5947 end if;
5949 -- Check invalid discriminant in variant part
5951 if Ekind (Entity (Discr_Name)) /= E_Discriminant then
5952 Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
5953 end if;
5955 Discr_Type := Etype (Entity (Discr_Name));
5957 if not Is_Discrete_Type (Discr_Type) then
5958 Error_Msg_N
5959 ("discriminant in a variant part must be of a discrete type",
5960 Name (N));
5961 return;
5962 end if;
5964 -- Now analyze the choices, which also analyzes the declarations that
5965 -- are associated with each choice.
5967 Analyze_Choices (Variants (N), Discr_Type);
5969 -- Note: we used to instantiate and call Check_Choices here to check
5970 -- that the choices covered the discriminant, but it's too early to do
5971 -- that because of statically predicated subtypes, whose analysis may
5972 -- be deferred to their freeze point which may be as late as the freeze
5973 -- point of the containing record. So this call is now to be found in
5974 -- Freeze_Record_Declaration.
5976 end Analyze_Variant_Part;
5978 ----------------------------
5979 -- Array_Type_Declaration --
5980 ----------------------------
5982 procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
5983 Component_Def : constant Node_Id := Component_Definition (Def);
5984 Component_Typ : constant Node_Id := Subtype_Indication (Component_Def);
5985 P : constant Node_Id := Parent (Def);
5986 Element_Type : Entity_Id;
5987 Implicit_Base : Entity_Id;
5988 Index : Node_Id;
5989 Nb_Index : Nat;
5990 Priv : Entity_Id;
5991 Related_Id : Entity_Id := Empty;
5993 begin
5994 if Nkind (Def) = N_Constrained_Array_Definition then
5995 Index := First (Discrete_Subtype_Definitions (Def));
5996 else
5997 Index := First (Subtype_Marks (Def));
5998 end if;
6000 -- Find proper names for the implicit types which may be public. In case
6001 -- of anonymous arrays we use the name of the first object of that type
6002 -- as prefix.
6004 if No (T) then
6005 Related_Id := Defining_Identifier (P);
6006 else
6007 Related_Id := T;
6008 end if;
6010 Nb_Index := 1;
6011 while Present (Index) loop
6012 Analyze (Index);
6014 -- Test for odd case of trying to index a type by the type itself
6016 if Is_Entity_Name (Index) and then Entity (Index) = T then
6017 Error_Msg_N ("type& cannot be indexed by itself", Index);
6018 Set_Entity (Index, Standard_Boolean);
6019 Set_Etype (Index, Standard_Boolean);
6020 end if;
6022 -- Check SPARK restriction requiring a subtype mark
6024 if not Nkind_In (Index, N_Identifier, N_Expanded_Name) then
6025 Check_SPARK_05_Restriction ("subtype mark required", Index);
6026 end if;
6028 -- Add a subtype declaration for each index of private array type
6029 -- declaration whose etype is also private. For example:
6031 -- package Pkg is
6032 -- type Index is private;
6033 -- private
6034 -- type Table is array (Index) of ...
6035 -- end;
6037 -- This is currently required by the expander for the internally
6038 -- generated equality subprogram of records with variant parts in
6039 -- which the etype of some component is such private type.
6041 if Ekind (Current_Scope) = E_Package
6042 and then In_Private_Part (Current_Scope)
6043 and then Has_Private_Declaration (Etype (Index))
6044 then
6045 declare
6046 Loc : constant Source_Ptr := Sloc (Def);
6047 Decl : Entity_Id;
6048 New_E : Entity_Id;
6050 begin
6051 New_E := Make_Temporary (Loc, 'T');
6052 Set_Is_Internal (New_E);
6054 Decl :=
6055 Make_Subtype_Declaration (Loc,
6056 Defining_Identifier => New_E,
6057 Subtype_Indication =>
6058 New_Occurrence_Of (Etype (Index), Loc));
6060 Insert_Before (Parent (Def), Decl);
6061 Analyze (Decl);
6062 Set_Etype (Index, New_E);
6064 -- If the index is a range or a subtype indication it carries
6065 -- no entity. Example:
6067 -- package Pkg is
6068 -- type T is private;
6069 -- private
6070 -- type T is new Natural;
6071 -- Table : array (T(1) .. T(10)) of Boolean;
6072 -- end Pkg;
6074 -- Otherwise the type of the reference is its entity.
6076 if Is_Entity_Name (Index) then
6077 Set_Entity (Index, New_E);
6078 end if;
6079 end;
6080 end if;
6082 Make_Index (Index, P, Related_Id, Nb_Index);
6084 -- Check error of subtype with predicate for index type
6086 Bad_Predicated_Subtype_Use
6087 ("subtype& has predicate, not allowed as index subtype",
6088 Index, Etype (Index));
6090 -- Move to next index
6092 Next_Index (Index);
6093 Nb_Index := Nb_Index + 1;
6094 end loop;
6096 -- Process subtype indication if one is present
6098 if Present (Component_Typ) then
6099 Element_Type := Process_Subtype (Component_Typ, P, Related_Id, 'C');
6101 Set_Etype (Component_Typ, Element_Type);
6103 if not Nkind_In (Component_Typ, N_Identifier, N_Expanded_Name) then
6104 Check_SPARK_05_Restriction
6105 ("subtype mark required", Component_Typ);
6106 end if;
6108 -- Ada 2005 (AI-230): Access Definition case
6110 else pragma Assert (Present (Access_Definition (Component_Def)));
6112 -- Indicate that the anonymous access type is created by the
6113 -- array type declaration.
6115 Element_Type := Access_Definition
6116 (Related_Nod => P,
6117 N => Access_Definition (Component_Def));
6118 Set_Is_Local_Anonymous_Access (Element_Type);
6120 -- Propagate the parent. This field is needed if we have to generate
6121 -- the master_id associated with an anonymous access to task type
6122 -- component (see Expand_N_Full_Type_Declaration.Build_Master)
6124 Set_Parent (Element_Type, Parent (T));
6126 -- Ada 2005 (AI-230): In case of components that are anonymous access
6127 -- types the level of accessibility depends on the enclosing type
6128 -- declaration
6130 Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
6132 -- Ada 2005 (AI-254)
6134 declare
6135 CD : constant Node_Id :=
6136 Access_To_Subprogram_Definition
6137 (Access_Definition (Component_Def));
6138 begin
6139 if Present (CD) and then Protected_Present (CD) then
6140 Element_Type :=
6141 Replace_Anonymous_Access_To_Protected_Subprogram (Def);
6142 end if;
6143 end;
6144 end if;
6146 -- Constrained array case
6148 if No (T) then
6149 T := Create_Itype (E_Void, P, Related_Id, 'T');
6150 end if;
6152 if Nkind (Def) = N_Constrained_Array_Definition then
6154 -- Establish Implicit_Base as unconstrained base type
6156 Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
6158 Set_Etype (Implicit_Base, Implicit_Base);
6159 Set_Scope (Implicit_Base, Current_Scope);
6160 Set_Has_Delayed_Freeze (Implicit_Base);
6161 Set_Default_SSO (Implicit_Base);
6163 -- The constrained array type is a subtype of the unconstrained one
6165 Set_Ekind (T, E_Array_Subtype);
6166 Init_Size_Align (T);
6167 Set_Etype (T, Implicit_Base);
6168 Set_Scope (T, Current_Scope);
6169 Set_Is_Constrained (T);
6170 Set_First_Index (T,
6171 First (Discrete_Subtype_Definitions (Def)));
6172 Set_Has_Delayed_Freeze (T);
6174 -- Complete setup of implicit base type
6176 Set_Component_Size (Implicit_Base, Uint_0);
6177 Set_Component_Type (Implicit_Base, Element_Type);
6178 Set_Finalize_Storage_Only
6179 (Implicit_Base,
6180 Finalize_Storage_Only (Element_Type));
6181 Set_First_Index (Implicit_Base, First_Index (T));
6182 Set_Has_Controlled_Component
6183 (Implicit_Base,
6184 Has_Controlled_Component (Element_Type)
6185 or else Is_Controlled (Element_Type));
6186 Set_Packed_Array_Impl_Type
6187 (Implicit_Base, Empty);
6189 Propagate_Concurrent_Flags (Implicit_Base, Element_Type);
6191 -- Unconstrained array case
6193 else
6194 Set_Ekind (T, E_Array_Type);
6195 Init_Size_Align (T);
6196 Set_Etype (T, T);
6197 Set_Scope (T, Current_Scope);
6198 Set_Component_Size (T, Uint_0);
6199 Set_Is_Constrained (T, False);
6200 Set_First_Index (T, First (Subtype_Marks (Def)));
6201 Set_Has_Delayed_Freeze (T, True);
6202 Propagate_Concurrent_Flags (T, Element_Type);
6203 Set_Has_Controlled_Component (T, Has_Controlled_Component
6204 (Element_Type)
6205 or else
6206 Is_Controlled (Element_Type));
6207 Set_Finalize_Storage_Only (T, Finalize_Storage_Only
6208 (Element_Type));
6209 Set_Default_SSO (T);
6210 end if;
6212 -- Common attributes for both cases
6214 Set_Component_Type (Base_Type (T), Element_Type);
6215 Set_Packed_Array_Impl_Type (T, Empty);
6217 if Aliased_Present (Component_Definition (Def)) then
6218 Check_SPARK_05_Restriction
6219 ("aliased is not allowed", Component_Definition (Def));
6220 Set_Has_Aliased_Components (Etype (T));
6221 end if;
6223 -- Ada 2005 (AI-231): Propagate the null-excluding attribute to the
6224 -- array type to ensure that objects of this type are initialized.
6226 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (Element_Type) then
6227 Set_Can_Never_Be_Null (T);
6229 if Null_Exclusion_Present (Component_Definition (Def))
6231 -- No need to check itypes because in their case this check was
6232 -- done at their point of creation
6234 and then not Is_Itype (Element_Type)
6235 then
6236 Error_Msg_N
6237 ("`NOT NULL` not allowed (null already excluded)",
6238 Subtype_Indication (Component_Definition (Def)));
6239 end if;
6240 end if;
6242 Priv := Private_Component (Element_Type);
6244 if Present (Priv) then
6246 -- Check for circular definitions
6248 if Priv = Any_Type then
6249 Set_Component_Type (Etype (T), Any_Type);
6251 -- There is a gap in the visibility of operations on the composite
6252 -- type only if the component type is defined in a different scope.
6254 elsif Scope (Priv) = Current_Scope then
6255 null;
6257 elsif Is_Limited_Type (Priv) then
6258 Set_Is_Limited_Composite (Etype (T));
6259 Set_Is_Limited_Composite (T);
6260 else
6261 Set_Is_Private_Composite (Etype (T));
6262 Set_Is_Private_Composite (T);
6263 end if;
6264 end if;
6266 -- A syntax error in the declaration itself may lead to an empty index
6267 -- list, in which case do a minimal patch.
6269 if No (First_Index (T)) then
6270 Error_Msg_N ("missing index definition in array type declaration", T);
6272 declare
6273 Indexes : constant List_Id :=
6274 New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
6275 begin
6276 Set_Discrete_Subtype_Definitions (Def, Indexes);
6277 Set_First_Index (T, First (Indexes));
6278 return;
6279 end;
6280 end if;
6282 -- Create a concatenation operator for the new type. Internal array
6283 -- types created for packed entities do not need such, they are
6284 -- compatible with the user-defined type.
6286 if Number_Dimensions (T) = 1
6287 and then not Is_Packed_Array_Impl_Type (T)
6288 then
6289 New_Concatenation_Op (T);
6290 end if;
6292 -- In the case of an unconstrained array the parser has already verified
6293 -- that all the indexes are unconstrained but we still need to make sure
6294 -- that the element type is constrained.
6296 if not Is_Definite_Subtype (Element_Type) then
6297 Error_Msg_N
6298 ("unconstrained element type in array declaration",
6299 Subtype_Indication (Component_Def));
6301 elsif Is_Abstract_Type (Element_Type) then
6302 Error_Msg_N
6303 ("the type of a component cannot be abstract",
6304 Subtype_Indication (Component_Def));
6305 end if;
6307 -- There may be an invariant declared for the component type, but
6308 -- the construction of the component invariant checking procedure
6309 -- takes place during expansion.
6310 end Array_Type_Declaration;
6312 ------------------------------------------------------
6313 -- Replace_Anonymous_Access_To_Protected_Subprogram --
6314 ------------------------------------------------------
6316 function Replace_Anonymous_Access_To_Protected_Subprogram
6317 (N : Node_Id) return Entity_Id
6319 Loc : constant Source_Ptr := Sloc (N);
6321 Curr_Scope : constant Scope_Stack_Entry :=
6322 Scope_Stack.Table (Scope_Stack.Last);
6324 Anon : constant Entity_Id := Make_Temporary (Loc, 'S');
6326 Acc : Node_Id;
6327 -- Access definition in declaration
6329 Comp : Node_Id;
6330 -- Object definition or formal definition with an access definition
6332 Decl : Node_Id;
6333 -- Declaration of anonymous access to subprogram type
6335 Spec : Node_Id;
6336 -- Original specification in access to subprogram
6338 P : Node_Id;
6340 begin
6341 Set_Is_Internal (Anon);
6343 case Nkind (N) is
6344 when N_Constrained_Array_Definition
6345 | N_Component_Declaration
6346 | N_Unconstrained_Array_Definition
6348 Comp := Component_Definition (N);
6349 Acc := Access_Definition (Comp);
6351 when N_Discriminant_Specification =>
6352 Comp := Discriminant_Type (N);
6353 Acc := Comp;
6355 when N_Parameter_Specification =>
6356 Comp := Parameter_Type (N);
6357 Acc := Comp;
6359 when N_Access_Function_Definition =>
6360 Comp := Result_Definition (N);
6361 Acc := Comp;
6363 when N_Object_Declaration =>
6364 Comp := Object_Definition (N);
6365 Acc := Comp;
6367 when N_Function_Specification =>
6368 Comp := Result_Definition (N);
6369 Acc := Comp;
6371 when others =>
6372 raise Program_Error;
6373 end case;
6375 Spec := Access_To_Subprogram_Definition (Acc);
6377 Decl :=
6378 Make_Full_Type_Declaration (Loc,
6379 Defining_Identifier => Anon,
6380 Type_Definition => Copy_Separate_Tree (Spec));
6382 Mark_Rewrite_Insertion (Decl);
6384 -- In ASIS mode, analyze the profile on the original node, because
6385 -- the separate copy does not provide enough links to recover the
6386 -- original tree. Analysis is limited to type annotations, within
6387 -- a temporary scope that serves as an anonymous subprogram to collect
6388 -- otherwise useless temporaries and itypes.
6390 if ASIS_Mode then
6391 declare
6392 Typ : constant Entity_Id := Make_Temporary (Loc, 'S');
6394 begin
6395 if Nkind (Spec) = N_Access_Function_Definition then
6396 Set_Ekind (Typ, E_Function);
6397 else
6398 Set_Ekind (Typ, E_Procedure);
6399 end if;
6401 Set_Parent (Typ, N);
6402 Set_Scope (Typ, Current_Scope);
6403 Push_Scope (Typ);
6405 -- Nothing to do if procedure is parameterless
6407 if Present (Parameter_Specifications (Spec)) then
6408 Process_Formals (Parameter_Specifications (Spec), Spec);
6409 end if;
6411 if Nkind (Spec) = N_Access_Function_Definition then
6412 declare
6413 Def : constant Node_Id := Result_Definition (Spec);
6415 begin
6416 -- The result might itself be an anonymous access type, so
6417 -- have to recurse.
6419 if Nkind (Def) = N_Access_Definition then
6420 if Present (Access_To_Subprogram_Definition (Def)) then
6421 Set_Etype
6422 (Def,
6423 Replace_Anonymous_Access_To_Protected_Subprogram
6424 (Spec));
6425 else
6426 Find_Type (Subtype_Mark (Def));
6427 end if;
6429 else
6430 Find_Type (Def);
6431 end if;
6432 end;
6433 end if;
6435 End_Scope;
6436 end;
6437 end if;
6439 -- Insert the new declaration in the nearest enclosing scope. If the
6440 -- parent is a body and N is its return type, the declaration belongs
6441 -- in the enclosing scope. Likewise if N is the type of a parameter.
6443 P := Parent (N);
6445 if Nkind (N) = N_Function_Specification
6446 and then Nkind (P) = N_Subprogram_Body
6447 then
6448 P := Parent (P);
6449 elsif Nkind (N) = N_Parameter_Specification
6450 and then Nkind (P) in N_Subprogram_Specification
6451 and then Nkind (Parent (P)) = N_Subprogram_Body
6452 then
6453 P := Parent (Parent (P));
6454 end if;
6456 while Present (P) and then not Has_Declarations (P) loop
6457 P := Parent (P);
6458 end loop;
6460 pragma Assert (Present (P));
6462 if Nkind (P) = N_Package_Specification then
6463 Prepend (Decl, Visible_Declarations (P));
6464 else
6465 Prepend (Decl, Declarations (P));
6466 end if;
6468 -- Replace the anonymous type with an occurrence of the new declaration.
6469 -- In all cases the rewritten node does not have the null-exclusion
6470 -- attribute because (if present) it was already inherited by the
6471 -- anonymous entity (Anon). Thus, in case of components we do not
6472 -- inherit this attribute.
6474 if Nkind (N) = N_Parameter_Specification then
6475 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6476 Set_Etype (Defining_Identifier (N), Anon);
6477 Set_Null_Exclusion_Present (N, False);
6479 elsif Nkind (N) = N_Object_Declaration then
6480 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6481 Set_Etype (Defining_Identifier (N), Anon);
6483 elsif Nkind (N) = N_Access_Function_Definition then
6484 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6486 elsif Nkind (N) = N_Function_Specification then
6487 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6488 Set_Etype (Defining_Unit_Name (N), Anon);
6490 else
6491 Rewrite (Comp,
6492 Make_Component_Definition (Loc,
6493 Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
6494 end if;
6496 Mark_Rewrite_Insertion (Comp);
6498 if Nkind_In (N, N_Object_Declaration, N_Access_Function_Definition)
6499 or else (Nkind (Parent (N)) = N_Full_Type_Declaration
6500 and then not Is_Type (Current_Scope))
6501 then
6503 -- Declaration can be analyzed in the current scope.
6505 Analyze (Decl);
6507 else
6508 -- Temporarily remove the current scope (record or subprogram) from
6509 -- the stack to add the new declarations to the enclosing scope.
6510 -- The anonymous entity is an Itype with the proper attributes.
6512 Scope_Stack.Decrement_Last;
6513 Analyze (Decl);
6514 Set_Is_Itype (Anon);
6515 Set_Associated_Node_For_Itype (Anon, N);
6516 Scope_Stack.Append (Curr_Scope);
6517 end if;
6519 Set_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
6520 Set_Can_Use_Internal_Rep (Anon, not Always_Compatible_Rep_On_Target);
6521 return Anon;
6522 end Replace_Anonymous_Access_To_Protected_Subprogram;
6524 -------------------------------
6525 -- Build_Derived_Access_Type --
6526 -------------------------------
6528 procedure Build_Derived_Access_Type
6529 (N : Node_Id;
6530 Parent_Type : Entity_Id;
6531 Derived_Type : Entity_Id)
6533 S : constant Node_Id := Subtype_Indication (Type_Definition (N));
6535 Desig_Type : Entity_Id;
6536 Discr : Entity_Id;
6537 Discr_Con_Elist : Elist_Id;
6538 Discr_Con_El : Elmt_Id;
6539 Subt : Entity_Id;
6541 begin
6542 -- Set the designated type so it is available in case this is an access
6543 -- to a self-referential type, e.g. a standard list type with a next
6544 -- pointer. Will be reset after subtype is built.
6546 Set_Directly_Designated_Type
6547 (Derived_Type, Designated_Type (Parent_Type));
6549 Subt := Process_Subtype (S, N);
6551 if Nkind (S) /= N_Subtype_Indication
6552 and then Subt /= Base_Type (Subt)
6553 then
6554 Set_Ekind (Derived_Type, E_Access_Subtype);
6555 end if;
6557 if Ekind (Derived_Type) = E_Access_Subtype then
6558 declare
6559 Pbase : constant Entity_Id := Base_Type (Parent_Type);
6560 Ibase : constant Entity_Id :=
6561 Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
6562 Svg_Chars : constant Name_Id := Chars (Ibase);
6563 Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
6565 begin
6566 Copy_Node (Pbase, Ibase);
6568 -- Restore Itype status after Copy_Node
6570 Set_Is_Itype (Ibase);
6571 Set_Associated_Node_For_Itype (Ibase, N);
6573 Set_Chars (Ibase, Svg_Chars);
6574 Set_Next_Entity (Ibase, Svg_Next_E);
6575 Set_Sloc (Ibase, Sloc (Derived_Type));
6576 Set_Scope (Ibase, Scope (Derived_Type));
6577 Set_Freeze_Node (Ibase, Empty);
6578 Set_Is_Frozen (Ibase, False);
6579 Set_Comes_From_Source (Ibase, False);
6580 Set_Is_First_Subtype (Ibase, False);
6582 Set_Etype (Ibase, Pbase);
6583 Set_Etype (Derived_Type, Ibase);
6584 end;
6585 end if;
6587 Set_Directly_Designated_Type
6588 (Derived_Type, Designated_Type (Subt));
6590 Set_Is_Constrained (Derived_Type, Is_Constrained (Subt));
6591 Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
6592 Set_Size_Info (Derived_Type, Parent_Type);
6593 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
6594 Set_Depends_On_Private (Derived_Type,
6595 Has_Private_Component (Derived_Type));
6596 Conditional_Delay (Derived_Type, Subt);
6598 -- Ada 2005 (AI-231): Set the null-exclusion attribute, and verify
6599 -- that it is not redundant.
6601 if Null_Exclusion_Present (Type_Definition (N)) then
6602 Set_Can_Never_Be_Null (Derived_Type);
6604 elsif Can_Never_Be_Null (Parent_Type) then
6605 Set_Can_Never_Be_Null (Derived_Type);
6606 end if;
6608 -- Note: we do not copy the Storage_Size_Variable, since we always go to
6609 -- the root type for this information.
6611 -- Apply range checks to discriminants for derived record case
6612 -- ??? THIS CODE SHOULD NOT BE HERE REALLY.
6614 Desig_Type := Designated_Type (Derived_Type);
6616 if Is_Composite_Type (Desig_Type)
6617 and then (not Is_Array_Type (Desig_Type))
6618 and then Has_Discriminants (Desig_Type)
6619 and then Base_Type (Desig_Type) /= Desig_Type
6620 then
6621 Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
6622 Discr_Con_El := First_Elmt (Discr_Con_Elist);
6624 Discr := First_Discriminant (Base_Type (Desig_Type));
6625 while Present (Discr_Con_El) loop
6626 Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
6627 Next_Elmt (Discr_Con_El);
6628 Next_Discriminant (Discr);
6629 end loop;
6630 end if;
6631 end Build_Derived_Access_Type;
6633 ------------------------------
6634 -- Build_Derived_Array_Type --
6635 ------------------------------
6637 procedure Build_Derived_Array_Type
6638 (N : Node_Id;
6639 Parent_Type : Entity_Id;
6640 Derived_Type : Entity_Id)
6642 Loc : constant Source_Ptr := Sloc (N);
6643 Tdef : constant Node_Id := Type_Definition (N);
6644 Indic : constant Node_Id := Subtype_Indication (Tdef);
6645 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
6646 Implicit_Base : Entity_Id := Empty;
6647 New_Indic : Node_Id;
6649 procedure Make_Implicit_Base;
6650 -- If the parent subtype is constrained, the derived type is a subtype
6651 -- of an implicit base type derived from the parent base.
6653 ------------------------
6654 -- Make_Implicit_Base --
6655 ------------------------
6657 procedure Make_Implicit_Base is
6658 begin
6659 Implicit_Base :=
6660 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
6662 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
6663 Set_Etype (Implicit_Base, Parent_Base);
6665 Copy_Array_Subtype_Attributes (Implicit_Base, Parent_Base);
6666 Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
6668 Set_Has_Delayed_Freeze (Implicit_Base, True);
6669 end Make_Implicit_Base;
6671 -- Start of processing for Build_Derived_Array_Type
6673 begin
6674 if not Is_Constrained (Parent_Type) then
6675 if Nkind (Indic) /= N_Subtype_Indication then
6676 Set_Ekind (Derived_Type, E_Array_Type);
6678 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6679 Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
6681 Set_Has_Delayed_Freeze (Derived_Type, True);
6683 else
6684 Make_Implicit_Base;
6685 Set_Etype (Derived_Type, Implicit_Base);
6687 New_Indic :=
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 => Constraint (Indic)));
6695 Rewrite (N, New_Indic);
6696 Analyze (N);
6697 end if;
6699 else
6700 if Nkind (Indic) /= N_Subtype_Indication then
6701 Make_Implicit_Base;
6703 Set_Ekind (Derived_Type, Ekind (Parent_Type));
6704 Set_Etype (Derived_Type, Implicit_Base);
6705 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6707 else
6708 Error_Msg_N ("illegal constraint on constrained type", Indic);
6709 end if;
6710 end if;
6712 -- If parent type is not a derived type itself, and is declared in
6713 -- closed scope (e.g. a subprogram), then we must explicitly introduce
6714 -- the new type's concatenation operator since Derive_Subprograms
6715 -- will not inherit the parent's operator. If the parent type is
6716 -- unconstrained, the operator is of the unconstrained base type.
6718 if Number_Dimensions (Parent_Type) = 1
6719 and then not Is_Limited_Type (Parent_Type)
6720 and then not Is_Derived_Type (Parent_Type)
6721 and then not Is_Package_Or_Generic_Package
6722 (Scope (Base_Type (Parent_Type)))
6723 then
6724 if not Is_Constrained (Parent_Type)
6725 and then Is_Constrained (Derived_Type)
6726 then
6727 New_Concatenation_Op (Implicit_Base);
6728 else
6729 New_Concatenation_Op (Derived_Type);
6730 end if;
6731 end if;
6732 end Build_Derived_Array_Type;
6734 -----------------------------------
6735 -- Build_Derived_Concurrent_Type --
6736 -----------------------------------
6738 procedure Build_Derived_Concurrent_Type
6739 (N : Node_Id;
6740 Parent_Type : Entity_Id;
6741 Derived_Type : Entity_Id)
6743 Loc : constant Source_Ptr := Sloc (N);
6745 Corr_Record : constant Entity_Id := Make_Temporary (Loc, 'C');
6746 Corr_Decl : Node_Id;
6747 Corr_Decl_Needed : Boolean;
6748 -- If the derived type has fewer discriminants than its parent, the
6749 -- corresponding record is also a derived type, in order to account for
6750 -- the bound discriminants. We create a full type declaration for it in
6751 -- this case.
6753 Constraint_Present : constant Boolean :=
6754 Nkind (Subtype_Indication (Type_Definition (N))) =
6755 N_Subtype_Indication;
6757 D_Constraint : Node_Id;
6758 New_Constraint : Elist_Id := No_Elist;
6759 Old_Disc : Entity_Id;
6760 New_Disc : Entity_Id;
6761 New_N : Node_Id;
6763 begin
6764 Set_Stored_Constraint (Derived_Type, No_Elist);
6765 Corr_Decl_Needed := False;
6766 Old_Disc := Empty;
6768 if Present (Discriminant_Specifications (N))
6769 and then Constraint_Present
6770 then
6771 Old_Disc := First_Discriminant (Parent_Type);
6772 New_Disc := First (Discriminant_Specifications (N));
6773 while Present (New_Disc) and then Present (Old_Disc) loop
6774 Next_Discriminant (Old_Disc);
6775 Next (New_Disc);
6776 end loop;
6777 end if;
6779 if Present (Old_Disc) and then Expander_Active then
6781 -- The new type has fewer discriminants, so we need to create a new
6782 -- corresponding record, which is derived from the corresponding
6783 -- record of the parent, and has a stored constraint that captures
6784 -- the values of the discriminant constraints. The corresponding
6785 -- record is needed only if expander is active and code generation is
6786 -- enabled.
6788 -- The type declaration for the derived corresponding record has the
6789 -- same discriminant part and constraints as the current declaration.
6790 -- Copy the unanalyzed tree to build declaration.
6792 Corr_Decl_Needed := True;
6793 New_N := Copy_Separate_Tree (N);
6795 Corr_Decl :=
6796 Make_Full_Type_Declaration (Loc,
6797 Defining_Identifier => Corr_Record,
6798 Discriminant_Specifications =>
6799 Discriminant_Specifications (New_N),
6800 Type_Definition =>
6801 Make_Derived_Type_Definition (Loc,
6802 Subtype_Indication =>
6803 Make_Subtype_Indication (Loc,
6804 Subtype_Mark =>
6805 New_Occurrence_Of
6806 (Corresponding_Record_Type (Parent_Type), Loc),
6807 Constraint =>
6808 Constraint
6809 (Subtype_Indication (Type_Definition (New_N))))));
6810 end if;
6812 -- Copy Storage_Size and Relative_Deadline variables if task case
6814 if Is_Task_Type (Parent_Type) then
6815 Set_Storage_Size_Variable (Derived_Type,
6816 Storage_Size_Variable (Parent_Type));
6817 Set_Relative_Deadline_Variable (Derived_Type,
6818 Relative_Deadline_Variable (Parent_Type));
6819 end if;
6821 if Present (Discriminant_Specifications (N)) then
6822 Push_Scope (Derived_Type);
6823 Check_Or_Process_Discriminants (N, Derived_Type);
6825 if Constraint_Present then
6826 New_Constraint :=
6827 Expand_To_Stored_Constraint
6828 (Parent_Type,
6829 Build_Discriminant_Constraints
6830 (Parent_Type,
6831 Subtype_Indication (Type_Definition (N)), True));
6832 end if;
6834 End_Scope;
6836 elsif Constraint_Present then
6838 -- Build constrained subtype, copying the constraint, and derive
6839 -- from it to create a derived constrained type.
6841 declare
6842 Loc : constant Source_Ptr := Sloc (N);
6843 Anon : constant Entity_Id :=
6844 Make_Defining_Identifier (Loc,
6845 Chars => New_External_Name (Chars (Derived_Type), 'T'));
6846 Decl : Node_Id;
6848 begin
6849 Decl :=
6850 Make_Subtype_Declaration (Loc,
6851 Defining_Identifier => Anon,
6852 Subtype_Indication =>
6853 New_Copy_Tree (Subtype_Indication (Type_Definition (N))));
6854 Insert_Before (N, Decl);
6855 Analyze (Decl);
6857 Rewrite (Subtype_Indication (Type_Definition (N)),
6858 New_Occurrence_Of (Anon, Loc));
6859 Set_Analyzed (Derived_Type, False);
6860 Analyze (N);
6861 return;
6862 end;
6863 end if;
6865 -- By default, operations and private data are inherited from parent.
6866 -- However, in the presence of bound discriminants, a new corresponding
6867 -- record will be created, see below.
6869 Set_Has_Discriminants
6870 (Derived_Type, Has_Discriminants (Parent_Type));
6871 Set_Corresponding_Record_Type
6872 (Derived_Type, Corresponding_Record_Type (Parent_Type));
6874 -- Is_Constrained is set according the parent subtype, but is set to
6875 -- False if the derived type is declared with new discriminants.
6877 Set_Is_Constrained
6878 (Derived_Type,
6879 (Is_Constrained (Parent_Type) or else Constraint_Present)
6880 and then not Present (Discriminant_Specifications (N)));
6882 if Constraint_Present then
6883 if not Has_Discriminants (Parent_Type) then
6884 Error_Msg_N ("untagged parent must have discriminants", N);
6886 elsif Present (Discriminant_Specifications (N)) then
6888 -- Verify that new discriminants are used to constrain old ones
6890 D_Constraint :=
6891 First
6892 (Constraints
6893 (Constraint (Subtype_Indication (Type_Definition (N)))));
6895 Old_Disc := First_Discriminant (Parent_Type);
6897 while Present (D_Constraint) loop
6898 if Nkind (D_Constraint) /= N_Discriminant_Association then
6900 -- Positional constraint. If it is a reference to a new
6901 -- discriminant, it constrains the corresponding old one.
6903 if Nkind (D_Constraint) = N_Identifier then
6904 New_Disc := First_Discriminant (Derived_Type);
6905 while Present (New_Disc) loop
6906 exit when Chars (New_Disc) = Chars (D_Constraint);
6907 Next_Discriminant (New_Disc);
6908 end loop;
6910 if Present (New_Disc) then
6911 Set_Corresponding_Discriminant (New_Disc, Old_Disc);
6912 end if;
6913 end if;
6915 Next_Discriminant (Old_Disc);
6917 -- if this is a named constraint, search by name for the old
6918 -- discriminants constrained by the new one.
6920 elsif Nkind (Expression (D_Constraint)) = N_Identifier then
6922 -- Find new discriminant with that name
6924 New_Disc := First_Discriminant (Derived_Type);
6925 while Present (New_Disc) loop
6926 exit when
6927 Chars (New_Disc) = Chars (Expression (D_Constraint));
6928 Next_Discriminant (New_Disc);
6929 end loop;
6931 if Present (New_Disc) then
6933 -- Verify that new discriminant renames some discriminant
6934 -- of the parent type, and associate the new discriminant
6935 -- with one or more old ones that it renames.
6937 declare
6938 Selector : Node_Id;
6940 begin
6941 Selector := First (Selector_Names (D_Constraint));
6942 while Present (Selector) loop
6943 Old_Disc := First_Discriminant (Parent_Type);
6944 while Present (Old_Disc) loop
6945 exit when Chars (Old_Disc) = Chars (Selector);
6946 Next_Discriminant (Old_Disc);
6947 end loop;
6949 if Present (Old_Disc) then
6950 Set_Corresponding_Discriminant
6951 (New_Disc, Old_Disc);
6952 end if;
6954 Next (Selector);
6955 end loop;
6956 end;
6957 end if;
6958 end if;
6960 Next (D_Constraint);
6961 end loop;
6963 New_Disc := First_Discriminant (Derived_Type);
6964 while Present (New_Disc) loop
6965 if No (Corresponding_Discriminant (New_Disc)) then
6966 Error_Msg_NE
6967 ("new discriminant& must constrain old one", N, New_Disc);
6969 elsif not
6970 Subtypes_Statically_Compatible
6971 (Etype (New_Disc),
6972 Etype (Corresponding_Discriminant (New_Disc)))
6973 then
6974 Error_Msg_NE
6975 ("& not statically compatible with parent discriminant",
6976 N, New_Disc);
6977 end if;
6979 Next_Discriminant (New_Disc);
6980 end loop;
6981 end if;
6983 elsif Present (Discriminant_Specifications (N)) then
6984 Error_Msg_N
6985 ("missing discriminant constraint in untagged derivation", N);
6986 end if;
6988 -- The entity chain of the derived type includes the new discriminants
6989 -- but shares operations with the parent.
6991 if Present (Discriminant_Specifications (N)) then
6992 Old_Disc := First_Discriminant (Parent_Type);
6993 while Present (Old_Disc) loop
6994 if No (Next_Entity (Old_Disc))
6995 or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
6996 then
6997 Set_Next_Entity
6998 (Last_Entity (Derived_Type), Next_Entity (Old_Disc));
6999 exit;
7000 end if;
7002 Next_Discriminant (Old_Disc);
7003 end loop;
7005 else
7006 Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
7007 if Has_Discriminants (Parent_Type) then
7008 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7009 Set_Discriminant_Constraint (
7010 Derived_Type, Discriminant_Constraint (Parent_Type));
7011 end if;
7012 end if;
7014 Set_Last_Entity (Derived_Type, Last_Entity (Parent_Type));
7016 Set_Has_Completion (Derived_Type);
7018 if Corr_Decl_Needed then
7019 Set_Stored_Constraint (Derived_Type, New_Constraint);
7020 Insert_After (N, Corr_Decl);
7021 Analyze (Corr_Decl);
7022 Set_Corresponding_Record_Type (Derived_Type, Corr_Record);
7023 end if;
7024 end Build_Derived_Concurrent_Type;
7026 ------------------------------------
7027 -- Build_Derived_Enumeration_Type --
7028 ------------------------------------
7030 procedure Build_Derived_Enumeration_Type
7031 (N : Node_Id;
7032 Parent_Type : Entity_Id;
7033 Derived_Type : Entity_Id)
7035 Loc : constant Source_Ptr := Sloc (N);
7036 Def : constant Node_Id := Type_Definition (N);
7037 Indic : constant Node_Id := Subtype_Indication (Def);
7038 Implicit_Base : Entity_Id;
7039 Literal : Entity_Id;
7040 New_Lit : Entity_Id;
7041 Literals_List : List_Id;
7042 Type_Decl : Node_Id;
7043 Hi, Lo : Node_Id;
7044 Rang_Expr : Node_Id;
7046 begin
7047 -- Since types Standard.Character and Standard.[Wide_]Wide_Character do
7048 -- not have explicit literals lists we need to process types derived
7049 -- from them specially. This is handled by Derived_Standard_Character.
7050 -- If the parent type is a generic type, there are no literals either,
7051 -- and we construct the same skeletal representation as for the generic
7052 -- parent type.
7054 if Is_Standard_Character_Type (Parent_Type) then
7055 Derived_Standard_Character (N, Parent_Type, Derived_Type);
7057 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
7058 declare
7059 Lo : Node_Id;
7060 Hi : Node_Id;
7062 begin
7063 if Nkind (Indic) /= N_Subtype_Indication then
7064 Lo :=
7065 Make_Attribute_Reference (Loc,
7066 Attribute_Name => Name_First,
7067 Prefix => New_Occurrence_Of (Derived_Type, Loc));
7068 Set_Etype (Lo, Derived_Type);
7070 Hi :=
7071 Make_Attribute_Reference (Loc,
7072 Attribute_Name => Name_Last,
7073 Prefix => New_Occurrence_Of (Derived_Type, Loc));
7074 Set_Etype (Hi, Derived_Type);
7076 Set_Scalar_Range (Derived_Type,
7077 Make_Range (Loc,
7078 Low_Bound => Lo,
7079 High_Bound => Hi));
7080 else
7082 -- Analyze subtype indication and verify compatibility
7083 -- with parent type.
7085 if Base_Type (Process_Subtype (Indic, N)) /=
7086 Base_Type (Parent_Type)
7087 then
7088 Error_Msg_N
7089 ("illegal constraint for formal discrete type", N);
7090 end if;
7091 end if;
7092 end;
7094 else
7095 -- If a constraint is present, analyze the bounds to catch
7096 -- premature usage of the derived literals.
7098 if Nkind (Indic) = N_Subtype_Indication
7099 and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
7100 then
7101 Analyze (Low_Bound (Range_Expression (Constraint (Indic))));
7102 Analyze (High_Bound (Range_Expression (Constraint (Indic))));
7103 end if;
7105 -- Introduce an implicit base type for the derived type even if there
7106 -- is no constraint attached to it, since this seems closer to the
7107 -- Ada semantics. Build a full type declaration tree for the derived
7108 -- type using the implicit base type as the defining identifier. The
7109 -- build a subtype declaration tree which applies the constraint (if
7110 -- any) have it replace the derived type declaration.
7112 Literal := First_Literal (Parent_Type);
7113 Literals_List := New_List;
7114 while Present (Literal)
7115 and then Ekind (Literal) = E_Enumeration_Literal
7116 loop
7117 -- Literals of the derived type have the same representation as
7118 -- those of the parent type, but this representation can be
7119 -- overridden by an explicit representation clause. Indicate
7120 -- that there is no explicit representation given yet. These
7121 -- derived literals are implicit operations of the new type,
7122 -- and can be overridden by explicit ones.
7124 if Nkind (Literal) = N_Defining_Character_Literal then
7125 New_Lit :=
7126 Make_Defining_Character_Literal (Loc, Chars (Literal));
7127 else
7128 New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
7129 end if;
7131 Set_Ekind (New_Lit, E_Enumeration_Literal);
7132 Set_Enumeration_Pos (New_Lit, Enumeration_Pos (Literal));
7133 Set_Enumeration_Rep (New_Lit, Enumeration_Rep (Literal));
7134 Set_Enumeration_Rep_Expr (New_Lit, Empty);
7135 Set_Alias (New_Lit, Literal);
7136 Set_Is_Known_Valid (New_Lit, True);
7138 Append (New_Lit, Literals_List);
7139 Next_Literal (Literal);
7140 end loop;
7142 Implicit_Base :=
7143 Make_Defining_Identifier (Sloc (Derived_Type),
7144 Chars => New_External_Name (Chars (Derived_Type), 'B'));
7146 -- Indicate the proper nature of the derived type. This must be done
7147 -- before analysis of the literals, to recognize cases when a literal
7148 -- may be hidden by a previous explicit function definition (cf.
7149 -- c83031a).
7151 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
7152 Set_Etype (Derived_Type, Implicit_Base);
7154 Type_Decl :=
7155 Make_Full_Type_Declaration (Loc,
7156 Defining_Identifier => Implicit_Base,
7157 Discriminant_Specifications => No_List,
7158 Type_Definition =>
7159 Make_Enumeration_Type_Definition (Loc, Literals_List));
7161 Mark_Rewrite_Insertion (Type_Decl);
7162 Insert_Before (N, Type_Decl);
7163 Analyze (Type_Decl);
7165 -- The anonymous base now has a full declaration, but this base
7166 -- is not a first subtype.
7168 Set_Is_First_Subtype (Implicit_Base, False);
7170 -- After the implicit base is analyzed its Etype needs to be changed
7171 -- to reflect the fact that it is derived from the parent type which
7172 -- was ignored during analysis. We also set the size at this point.
7174 Set_Etype (Implicit_Base, Parent_Type);
7176 Set_Size_Info (Implicit_Base, Parent_Type);
7177 Set_RM_Size (Implicit_Base, RM_Size (Parent_Type));
7178 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
7180 -- Copy other flags from parent type
7182 Set_Has_Non_Standard_Rep
7183 (Implicit_Base, Has_Non_Standard_Rep
7184 (Parent_Type));
7185 Set_Has_Pragma_Ordered
7186 (Implicit_Base, Has_Pragma_Ordered
7187 (Parent_Type));
7188 Set_Has_Delayed_Freeze (Implicit_Base);
7190 -- Process the subtype indication including a validation check on the
7191 -- constraint, if any. If a constraint is given, its bounds must be
7192 -- implicitly converted to the new type.
7194 if Nkind (Indic) = N_Subtype_Indication then
7195 declare
7196 R : constant Node_Id :=
7197 Range_Expression (Constraint (Indic));
7199 begin
7200 if Nkind (R) = N_Range then
7201 Hi := Build_Scalar_Bound
7202 (High_Bound (R), Parent_Type, Implicit_Base);
7203 Lo := Build_Scalar_Bound
7204 (Low_Bound (R), Parent_Type, Implicit_Base);
7206 else
7207 -- Constraint is a Range attribute. Replace with explicit
7208 -- mention of the bounds of the prefix, which must be a
7209 -- subtype.
7211 Analyze (Prefix (R));
7212 Hi :=
7213 Convert_To (Implicit_Base,
7214 Make_Attribute_Reference (Loc,
7215 Attribute_Name => Name_Last,
7216 Prefix =>
7217 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
7219 Lo :=
7220 Convert_To (Implicit_Base,
7221 Make_Attribute_Reference (Loc,
7222 Attribute_Name => Name_First,
7223 Prefix =>
7224 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
7225 end if;
7226 end;
7228 else
7229 Hi :=
7230 Build_Scalar_Bound
7231 (Type_High_Bound (Parent_Type),
7232 Parent_Type, Implicit_Base);
7233 Lo :=
7234 Build_Scalar_Bound
7235 (Type_Low_Bound (Parent_Type),
7236 Parent_Type, Implicit_Base);
7237 end if;
7239 Rang_Expr :=
7240 Make_Range (Loc,
7241 Low_Bound => Lo,
7242 High_Bound => Hi);
7244 -- If we constructed a default range for the case where no range
7245 -- was given, then the expressions in the range must not freeze
7246 -- since they do not correspond to expressions in the source.
7247 -- However, if the type inherits predicates the expressions will
7248 -- be elaborated earlier and must freeze.
7250 if Nkind (Indic) /= N_Subtype_Indication
7251 and then not Has_Predicates (Derived_Type)
7252 then
7253 Set_Must_Not_Freeze (Lo);
7254 Set_Must_Not_Freeze (Hi);
7255 Set_Must_Not_Freeze (Rang_Expr);
7256 end if;
7258 Rewrite (N,
7259 Make_Subtype_Declaration (Loc,
7260 Defining_Identifier => Derived_Type,
7261 Subtype_Indication =>
7262 Make_Subtype_Indication (Loc,
7263 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
7264 Constraint =>
7265 Make_Range_Constraint (Loc,
7266 Range_Expression => Rang_Expr))));
7268 Analyze (N);
7270 -- Propagate the aspects from the original type declaration to the
7271 -- declaration of the implicit base.
7273 Move_Aspects (From => Original_Node (N), To => Type_Decl);
7275 -- Apply a range check. Since this range expression doesn't have an
7276 -- Etype, we have to specifically pass the Source_Typ parameter. Is
7277 -- this right???
7279 if Nkind (Indic) = N_Subtype_Indication then
7280 Apply_Range_Check
7281 (Range_Expression (Constraint (Indic)), Parent_Type,
7282 Source_Typ => Entity (Subtype_Mark (Indic)));
7283 end if;
7284 end if;
7285 end Build_Derived_Enumeration_Type;
7287 --------------------------------
7288 -- Build_Derived_Numeric_Type --
7289 --------------------------------
7291 procedure Build_Derived_Numeric_Type
7292 (N : Node_Id;
7293 Parent_Type : Entity_Id;
7294 Derived_Type : Entity_Id)
7296 Loc : constant Source_Ptr := Sloc (N);
7297 Tdef : constant Node_Id := Type_Definition (N);
7298 Indic : constant Node_Id := Subtype_Indication (Tdef);
7299 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
7300 No_Constraint : constant Boolean := Nkind (Indic) /=
7301 N_Subtype_Indication;
7302 Implicit_Base : Entity_Id;
7304 Lo : Node_Id;
7305 Hi : Node_Id;
7307 begin
7308 -- Process the subtype indication including a validation check on
7309 -- the constraint if any.
7311 Discard_Node (Process_Subtype (Indic, N));
7313 -- Introduce an implicit base type for the derived type even if there
7314 -- is no constraint attached to it, since this seems closer to the Ada
7315 -- semantics.
7317 Implicit_Base :=
7318 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
7320 Set_Etype (Implicit_Base, Parent_Base);
7321 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
7322 Set_Size_Info (Implicit_Base, Parent_Base);
7323 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
7324 Set_Parent (Implicit_Base, Parent (Derived_Type));
7325 Set_Is_Known_Valid (Implicit_Base, Is_Known_Valid (Parent_Base));
7327 -- Set RM Size for discrete type or decimal fixed-point type
7328 -- Ordinary fixed-point is excluded, why???
7330 if Is_Discrete_Type (Parent_Base)
7331 or else Is_Decimal_Fixed_Point_Type (Parent_Base)
7332 then
7333 Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
7334 end if;
7336 Set_Has_Delayed_Freeze (Implicit_Base);
7338 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
7339 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
7341 Set_Scalar_Range (Implicit_Base,
7342 Make_Range (Loc,
7343 Low_Bound => Lo,
7344 High_Bound => Hi));
7346 if Has_Infinities (Parent_Base) then
7347 Set_Includes_Infinities (Scalar_Range (Implicit_Base));
7348 end if;
7350 -- The Derived_Type, which is the entity of the declaration, is a
7351 -- subtype of the implicit base. Its Ekind is a subtype, even in the
7352 -- absence of an explicit constraint.
7354 Set_Etype (Derived_Type, Implicit_Base);
7356 -- If we did not have a constraint, then the Ekind is set from the
7357 -- parent type (otherwise Process_Subtype has set the bounds)
7359 if No_Constraint then
7360 Set_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
7361 end if;
7363 -- If we did not have a range constraint, then set the range from the
7364 -- parent type. Otherwise, the Process_Subtype call has set the bounds.
7366 if No_Constraint or else not Has_Range_Constraint (Indic) then
7367 Set_Scalar_Range (Derived_Type,
7368 Make_Range (Loc,
7369 Low_Bound => New_Copy_Tree (Type_Low_Bound (Parent_Type)),
7370 High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
7371 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7373 if Has_Infinities (Parent_Type) then
7374 Set_Includes_Infinities (Scalar_Range (Derived_Type));
7375 end if;
7377 Set_Is_Known_Valid (Derived_Type, Is_Known_Valid (Parent_Type));
7378 end if;
7380 Set_Is_Descendant_Of_Address (Derived_Type,
7381 Is_Descendant_Of_Address (Parent_Type));
7382 Set_Is_Descendant_Of_Address (Implicit_Base,
7383 Is_Descendant_Of_Address (Parent_Type));
7385 -- Set remaining type-specific fields, depending on numeric type
7387 if Is_Modular_Integer_Type (Parent_Type) then
7388 Set_Modulus (Implicit_Base, Modulus (Parent_Base));
7390 Set_Non_Binary_Modulus
7391 (Implicit_Base, Non_Binary_Modulus (Parent_Base));
7393 Set_Is_Known_Valid
7394 (Implicit_Base, Is_Known_Valid (Parent_Base));
7396 elsif Is_Floating_Point_Type (Parent_Type) then
7398 -- Digits of base type is always copied from the digits value of
7399 -- the parent base type, but the digits of the derived type will
7400 -- already have been set if there was a constraint present.
7402 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
7403 Set_Float_Rep (Implicit_Base, Float_Rep (Parent_Base));
7405 if No_Constraint then
7406 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
7407 end if;
7409 elsif Is_Fixed_Point_Type (Parent_Type) then
7411 -- Small of base type and derived type are always copied from the
7412 -- parent base type, since smalls never change. The delta of the
7413 -- base type is also copied from the parent base type. However the
7414 -- delta of the derived type will have been set already if a
7415 -- constraint was present.
7417 Set_Small_Value (Derived_Type, Small_Value (Parent_Base));
7418 Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
7419 Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
7421 if No_Constraint then
7422 Set_Delta_Value (Derived_Type, Delta_Value (Parent_Type));
7423 end if;
7425 -- The scale and machine radix in the decimal case are always
7426 -- copied from the parent base type.
7428 if Is_Decimal_Fixed_Point_Type (Parent_Type) then
7429 Set_Scale_Value (Derived_Type, Scale_Value (Parent_Base));
7430 Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
7432 Set_Machine_Radix_10
7433 (Derived_Type, Machine_Radix_10 (Parent_Base));
7434 Set_Machine_Radix_10
7435 (Implicit_Base, Machine_Radix_10 (Parent_Base));
7437 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
7439 if No_Constraint then
7440 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
7442 else
7443 -- the analysis of the subtype_indication sets the
7444 -- digits value of the derived type.
7446 null;
7447 end if;
7448 end if;
7449 end if;
7451 if Is_Integer_Type (Parent_Type) then
7452 Set_Has_Shift_Operator
7453 (Implicit_Base, Has_Shift_Operator (Parent_Type));
7454 end if;
7456 -- The type of the bounds is that of the parent type, and they
7457 -- must be converted to the derived type.
7459 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
7461 -- The implicit_base should be frozen when the derived type is frozen,
7462 -- but note that it is used in the conversions of the bounds. For fixed
7463 -- types we delay the determination of the bounds until the proper
7464 -- freezing point. For other numeric types this is rejected by GCC, for
7465 -- reasons that are currently unclear (???), so we choose to freeze the
7466 -- implicit base now. In the case of integers and floating point types
7467 -- this is harmless because subsequent representation clauses cannot
7468 -- affect anything, but it is still baffling that we cannot use the
7469 -- same mechanism for all derived numeric types.
7471 -- There is a further complication: actually some representation
7472 -- clauses can affect the implicit base type. For example, attribute
7473 -- definition clauses for stream-oriented attributes need to set the
7474 -- corresponding TSS entries on the base type, and this normally
7475 -- cannot be done after the base type is frozen, so the circuitry in
7476 -- Sem_Ch13.New_Stream_Subprogram must account for this possibility
7477 -- and not use Set_TSS in this case.
7479 -- There are also consequences for the case of delayed representation
7480 -- aspects for some cases. For example, a Size aspect is delayed and
7481 -- should not be evaluated to the freeze point. This early freezing
7482 -- means that the size attribute evaluation happens too early???
7484 if Is_Fixed_Point_Type (Parent_Type) then
7485 Conditional_Delay (Implicit_Base, Parent_Type);
7486 else
7487 Freeze_Before (N, Implicit_Base);
7488 end if;
7489 end Build_Derived_Numeric_Type;
7491 --------------------------------
7492 -- Build_Derived_Private_Type --
7493 --------------------------------
7495 procedure Build_Derived_Private_Type
7496 (N : Node_Id;
7497 Parent_Type : Entity_Id;
7498 Derived_Type : Entity_Id;
7499 Is_Completion : Boolean;
7500 Derive_Subps : Boolean := True)
7502 Loc : constant Source_Ptr := Sloc (N);
7503 Par_Base : constant Entity_Id := Base_Type (Parent_Type);
7504 Par_Scope : constant Entity_Id := Scope (Par_Base);
7505 Full_N : constant Node_Id := New_Copy_Tree (N);
7506 Full_Der : Entity_Id := New_Copy (Derived_Type);
7507 Full_P : Entity_Id;
7509 procedure Build_Full_Derivation;
7510 -- Build full derivation, i.e. derive from the full view
7512 procedure Copy_And_Build;
7513 -- Copy derived type declaration, replace parent with its full view,
7514 -- and build derivation
7516 ---------------------------
7517 -- Build_Full_Derivation --
7518 ---------------------------
7520 procedure Build_Full_Derivation is
7521 begin
7522 -- If parent scope is not open, install the declarations
7524 if not In_Open_Scopes (Par_Scope) then
7525 Install_Private_Declarations (Par_Scope);
7526 Install_Visible_Declarations (Par_Scope);
7527 Copy_And_Build;
7528 Uninstall_Declarations (Par_Scope);
7530 -- If parent scope is open and in another unit, and parent has a
7531 -- completion, then the derivation is taking place in the visible
7532 -- part of a child unit. In that case retrieve the full view of
7533 -- the parent momentarily.
7535 elsif not In_Same_Source_Unit (N, Parent_Type) then
7536 Full_P := Full_View (Parent_Type);
7537 Exchange_Declarations (Parent_Type);
7538 Copy_And_Build;
7539 Exchange_Declarations (Full_P);
7541 -- Otherwise it is a local derivation
7543 else
7544 Copy_And_Build;
7545 end if;
7546 end Build_Full_Derivation;
7548 --------------------
7549 -- Copy_And_Build --
7550 --------------------
7552 procedure Copy_And_Build is
7553 Full_Parent : Entity_Id := Parent_Type;
7555 begin
7556 -- If the parent is itself derived from another private type,
7557 -- installing the private declarations has not affected its
7558 -- privacy status, so use its own full view explicitly.
7560 if Is_Private_Type (Full_Parent)
7561 and then Present (Full_View (Full_Parent))
7562 then
7563 Full_Parent := Full_View (Full_Parent);
7564 end if;
7566 -- And its underlying full view if necessary
7568 if Is_Private_Type (Full_Parent)
7569 and then Present (Underlying_Full_View (Full_Parent))
7570 then
7571 Full_Parent := Underlying_Full_View (Full_Parent);
7572 end if;
7574 -- For record, access and most enumeration types, derivation from
7575 -- the full view requires a fully-fledged declaration. In the other
7576 -- cases, just use an itype.
7578 if Ekind (Full_Parent) in Record_Kind
7579 or else Ekind (Full_Parent) in Access_Kind
7580 or else
7581 (Ekind (Full_Parent) in Enumeration_Kind
7582 and then not Is_Standard_Character_Type (Full_Parent)
7583 and then not Is_Generic_Type (Root_Type (Full_Parent)))
7584 then
7585 -- Copy and adjust declaration to provide a completion for what
7586 -- is originally a private declaration. Indicate that full view
7587 -- is internally generated.
7589 Set_Comes_From_Source (Full_N, False);
7590 Set_Comes_From_Source (Full_Der, False);
7591 Set_Parent (Full_Der, Full_N);
7592 Set_Defining_Identifier (Full_N, Full_Der);
7594 -- If there are no constraints, adjust the subtype mark
7596 if Nkind (Subtype_Indication (Type_Definition (Full_N))) /=
7597 N_Subtype_Indication
7598 then
7599 Set_Subtype_Indication
7600 (Type_Definition (Full_N),
7601 New_Occurrence_Of (Full_Parent, Sloc (Full_N)));
7602 end if;
7604 Insert_After (N, Full_N);
7606 -- Build full view of derived type from full view of parent which
7607 -- is now installed. Subprograms have been derived on the partial
7608 -- view, the completion does not derive them anew.
7610 if Ekind (Full_Parent) in Record_Kind then
7612 -- If parent type is tagged, the completion inherits the proper
7613 -- primitive operations.
7615 if Is_Tagged_Type (Parent_Type) then
7616 Build_Derived_Record_Type
7617 (Full_N, Full_Parent, Full_Der, Derive_Subps);
7618 else
7619 Build_Derived_Record_Type
7620 (Full_N, Full_Parent, Full_Der, Derive_Subps => False);
7621 end if;
7623 else
7624 Build_Derived_Type
7625 (Full_N, Full_Parent, Full_Der,
7626 Is_Completion => False, Derive_Subps => False);
7627 end if;
7629 -- The full declaration has been introduced into the tree and
7630 -- processed in the step above. It should not be analyzed again
7631 -- (when encountered later in the current list of declarations)
7632 -- to prevent spurious name conflicts. The full entity remains
7633 -- invisible.
7635 Set_Analyzed (Full_N);
7637 else
7638 Full_Der :=
7639 Make_Defining_Identifier (Sloc (Derived_Type),
7640 Chars => Chars (Derived_Type));
7641 Set_Is_Itype (Full_Der);
7642 Set_Associated_Node_For_Itype (Full_Der, N);
7643 Set_Parent (Full_Der, N);
7644 Build_Derived_Type
7645 (N, Full_Parent, Full_Der,
7646 Is_Completion => False, Derive_Subps => False);
7647 end if;
7649 Set_Has_Private_Declaration (Full_Der);
7650 Set_Has_Private_Declaration (Derived_Type);
7652 Set_Scope (Full_Der, Scope (Derived_Type));
7653 Set_Is_First_Subtype (Full_Der, Is_First_Subtype (Derived_Type));
7654 Set_Has_Size_Clause (Full_Der, False);
7655 Set_Has_Alignment_Clause (Full_Der, False);
7656 Set_Has_Delayed_Freeze (Full_Der);
7657 Set_Is_Frozen (Full_Der, False);
7658 Set_Freeze_Node (Full_Der, Empty);
7659 Set_Depends_On_Private (Full_Der, Has_Private_Component (Full_Der));
7660 Set_Is_Public (Full_Der, Is_Public (Derived_Type));
7662 -- The convention on the base type may be set in the private part
7663 -- and not propagated to the subtype until later, so we obtain the
7664 -- convention from the base type of the parent.
7666 Set_Convention (Full_Der, Convention (Base_Type (Full_Parent)));
7667 end Copy_And_Build;
7669 -- Start of processing for Build_Derived_Private_Type
7671 begin
7672 if Is_Tagged_Type (Parent_Type) then
7673 Full_P := Full_View (Parent_Type);
7675 -- A type extension of a type with unknown discriminants is an
7676 -- indefinite type that the back-end cannot handle directly.
7677 -- We treat it as a private type, and build a completion that is
7678 -- derived from the full view of the parent, and hopefully has
7679 -- known discriminants.
7681 -- If the full view of the parent type has an underlying record view,
7682 -- use it to generate the underlying record view of this derived type
7683 -- (required for chains of derivations with unknown discriminants).
7685 -- Minor optimization: we avoid the generation of useless underlying
7686 -- record view entities if the private type declaration has unknown
7687 -- discriminants but its corresponding full view has no
7688 -- discriminants.
7690 if Has_Unknown_Discriminants (Parent_Type)
7691 and then Present (Full_P)
7692 and then (Has_Discriminants (Full_P)
7693 or else Present (Underlying_Record_View (Full_P)))
7694 and then not In_Open_Scopes (Par_Scope)
7695 and then Expander_Active
7696 then
7697 declare
7698 Full_Der : constant Entity_Id := Make_Temporary (Loc, 'T');
7699 New_Ext : constant Node_Id :=
7700 Copy_Separate_Tree
7701 (Record_Extension_Part (Type_Definition (N)));
7702 Decl : Node_Id;
7704 begin
7705 Build_Derived_Record_Type
7706 (N, Parent_Type, Derived_Type, Derive_Subps);
7708 -- Build anonymous completion, as a derivation from the full
7709 -- view of the parent. This is not a completion in the usual
7710 -- sense, because the current type is not private.
7712 Decl :=
7713 Make_Full_Type_Declaration (Loc,
7714 Defining_Identifier => Full_Der,
7715 Type_Definition =>
7716 Make_Derived_Type_Definition (Loc,
7717 Subtype_Indication =>
7718 New_Copy_Tree
7719 (Subtype_Indication (Type_Definition (N))),
7720 Record_Extension_Part => New_Ext));
7722 -- If the parent type has an underlying record view, use it
7723 -- here to build the new underlying record view.
7725 if Present (Underlying_Record_View (Full_P)) then
7726 pragma Assert
7727 (Nkind (Subtype_Indication (Type_Definition (Decl)))
7728 = N_Identifier);
7729 Set_Entity (Subtype_Indication (Type_Definition (Decl)),
7730 Underlying_Record_View (Full_P));
7731 end if;
7733 Install_Private_Declarations (Par_Scope);
7734 Install_Visible_Declarations (Par_Scope);
7735 Insert_Before (N, Decl);
7737 -- Mark entity as an underlying record view before analysis,
7738 -- to avoid generating the list of its primitive operations
7739 -- (which is not really required for this entity) and thus
7740 -- prevent spurious errors associated with missing overriding
7741 -- of abstract primitives (overridden only for Derived_Type).
7743 Set_Ekind (Full_Der, E_Record_Type);
7744 Set_Is_Underlying_Record_View (Full_Der);
7745 Set_Default_SSO (Full_Der);
7746 Set_No_Reordering (Full_Der, No_Component_Reordering);
7748 Analyze (Decl);
7750 pragma Assert (Has_Discriminants (Full_Der)
7751 and then not Has_Unknown_Discriminants (Full_Der));
7753 Uninstall_Declarations (Par_Scope);
7755 -- Freeze the underlying record view, to prevent generation of
7756 -- useless dispatching information, which is simply shared with
7757 -- the real derived type.
7759 Set_Is_Frozen (Full_Der);
7761 -- If the derived type has access discriminants, create
7762 -- references to their anonymous types now, to prevent
7763 -- back-end problems when their first use is in generated
7764 -- bodies of primitives.
7766 declare
7767 E : Entity_Id;
7769 begin
7770 E := First_Entity (Full_Der);
7772 while Present (E) loop
7773 if Ekind (E) = E_Discriminant
7774 and then Ekind (Etype (E)) = E_Anonymous_Access_Type
7775 then
7776 Build_Itype_Reference (Etype (E), Decl);
7777 end if;
7779 Next_Entity (E);
7780 end loop;
7781 end;
7783 -- Set up links between real entity and underlying record view
7785 Set_Underlying_Record_View (Derived_Type, Base_Type (Full_Der));
7786 Set_Underlying_Record_View (Base_Type (Full_Der), Derived_Type);
7787 end;
7789 -- If discriminants are known, build derived record
7791 else
7792 Build_Derived_Record_Type
7793 (N, Parent_Type, Derived_Type, Derive_Subps);
7794 end if;
7796 return;
7798 elsif Has_Discriminants (Parent_Type) then
7800 -- Build partial view of derived type from partial view of parent.
7801 -- This must be done before building the full derivation because the
7802 -- second derivation will modify the discriminants of the first and
7803 -- the discriminants are chained with the rest of the components in
7804 -- the full derivation.
7806 Build_Derived_Record_Type
7807 (N, Parent_Type, Derived_Type, Derive_Subps);
7809 -- Build the full derivation if this is not the anonymous derived
7810 -- base type created by Build_Derived_Record_Type in the constrained
7811 -- case (see point 5. of its head comment) since we build it for the
7812 -- derived subtype. And skip it for protected types altogether, as
7813 -- gigi does not use these types directly.
7815 if Present (Full_View (Parent_Type))
7816 and then not Is_Itype (Derived_Type)
7817 and then not (Ekind (Full_View (Parent_Type)) in Protected_Kind)
7818 then
7819 declare
7820 Der_Base : constant Entity_Id := Base_Type (Derived_Type);
7821 Discr : Entity_Id;
7822 Last_Discr : Entity_Id;
7824 begin
7825 -- If this is not a completion, construct the implicit full
7826 -- view by deriving from the full view of the parent type.
7827 -- But if this is a completion, the derived private type
7828 -- being built is a full view and the full derivation can
7829 -- only be its underlying full view.
7831 Build_Full_Derivation;
7833 if not Is_Completion then
7834 Set_Full_View (Derived_Type, Full_Der);
7835 else
7836 Set_Underlying_Full_View (Derived_Type, Full_Der);
7837 Set_Is_Underlying_Full_View (Full_Der);
7838 end if;
7840 if not Is_Base_Type (Derived_Type) then
7841 Set_Full_View (Der_Base, Base_Type (Full_Der));
7842 end if;
7844 -- Copy the discriminant list from full view to the partial
7845 -- view (base type and its subtype). Gigi requires that the
7846 -- partial and full views have the same discriminants.
7848 -- Note that since the partial view points to discriminants
7849 -- in the full view, their scope will be that of the full
7850 -- view. This might cause some front end problems and need
7851 -- adjustment???
7853 Discr := First_Discriminant (Base_Type (Full_Der));
7854 Set_First_Entity (Der_Base, Discr);
7856 loop
7857 Last_Discr := Discr;
7858 Next_Discriminant (Discr);
7859 exit when No (Discr);
7860 end loop;
7862 Set_Last_Entity (Der_Base, Last_Discr);
7863 Set_First_Entity (Derived_Type, First_Entity (Der_Base));
7864 Set_Last_Entity (Derived_Type, Last_Entity (Der_Base));
7865 end;
7866 end if;
7868 elsif Present (Full_View (Parent_Type))
7869 and then Has_Discriminants (Full_View (Parent_Type))
7870 then
7871 if Has_Unknown_Discriminants (Parent_Type)
7872 and then Nkind (Subtype_Indication (Type_Definition (N))) =
7873 N_Subtype_Indication
7874 then
7875 Error_Msg_N
7876 ("cannot constrain type with unknown discriminants",
7877 Subtype_Indication (Type_Definition (N)));
7878 return;
7879 end if;
7881 -- If this is not a completion, construct the implicit full view by
7882 -- deriving from the full view of the parent type. But if this is a
7883 -- completion, the derived private type being built is a full view
7884 -- and the full derivation can only be its underlying full view.
7886 Build_Full_Derivation;
7888 if not Is_Completion then
7889 Set_Full_View (Derived_Type, Full_Der);
7890 else
7891 Set_Underlying_Full_View (Derived_Type, Full_Der);
7892 Set_Is_Underlying_Full_View (Full_Der);
7893 end if;
7895 -- In any case, the primitive operations are inherited from the
7896 -- parent type, not from the internal full view.
7898 Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
7900 if Derive_Subps then
7901 Derive_Subprograms (Parent_Type, Derived_Type);
7902 end if;
7904 Set_Stored_Constraint (Derived_Type, No_Elist);
7905 Set_Is_Constrained
7906 (Derived_Type, Is_Constrained (Full_View (Parent_Type)));
7908 else
7909 -- Untagged type, No discriminants on either view
7911 if Nkind (Subtype_Indication (Type_Definition (N))) =
7912 N_Subtype_Indication
7913 then
7914 Error_Msg_N
7915 ("illegal constraint on type without discriminants", N);
7916 end if;
7918 if Present (Discriminant_Specifications (N))
7919 and then Present (Full_View (Parent_Type))
7920 and then not Is_Tagged_Type (Full_View (Parent_Type))
7921 then
7922 Error_Msg_N ("cannot add discriminants to untagged type", N);
7923 end if;
7925 Set_Stored_Constraint (Derived_Type, No_Elist);
7926 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7928 Set_Is_Controlled_Active
7929 (Derived_Type, Is_Controlled_Active (Parent_Type));
7931 Set_Disable_Controlled
7932 (Derived_Type, Disable_Controlled (Parent_Type));
7934 Set_Has_Controlled_Component
7935 (Derived_Type, Has_Controlled_Component (Parent_Type));
7937 -- Direct controlled types do not inherit Finalize_Storage_Only flag
7939 if not Is_Controlled (Parent_Type) then
7940 Set_Finalize_Storage_Only
7941 (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
7942 end if;
7944 -- If this is not a completion, construct the implicit full view by
7945 -- deriving from the full view of the parent type.
7947 -- ??? If the parent is untagged private and its completion is
7948 -- tagged, this mechanism will not work because we cannot derive from
7949 -- the tagged full view unless we have an extension.
7951 if Present (Full_View (Parent_Type))
7952 and then not Is_Tagged_Type (Full_View (Parent_Type))
7953 and then not Is_Completion
7954 then
7955 Build_Full_Derivation;
7956 Set_Full_View (Derived_Type, Full_Der);
7957 end if;
7958 end if;
7960 Set_Has_Unknown_Discriminants (Derived_Type,
7961 Has_Unknown_Discriminants (Parent_Type));
7963 if Is_Private_Type (Derived_Type) then
7964 Set_Private_Dependents (Derived_Type, New_Elmt_List);
7965 end if;
7967 -- If the parent base type is in scope, add the derived type to its
7968 -- list of private dependents, because its full view may become
7969 -- visible subsequently (in a nested private part, a body, or in a
7970 -- further child unit).
7972 if Is_Private_Type (Par_Base) and then In_Open_Scopes (Par_Scope) then
7973 Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
7975 -- Check for unusual case where a type completed by a private
7976 -- derivation occurs within a package nested in a child unit, and
7977 -- the parent is declared in an ancestor.
7979 if Is_Child_Unit (Scope (Current_Scope))
7980 and then Is_Completion
7981 and then In_Private_Part (Current_Scope)
7982 and then Scope (Parent_Type) /= Current_Scope
7984 -- Note that if the parent has a completion in the private part,
7985 -- (which is itself a derivation from some other private type)
7986 -- it is that completion that is visible, there is no full view
7987 -- available, and no special processing is needed.
7989 and then Present (Full_View (Parent_Type))
7990 then
7991 -- In this case, the full view of the parent type will become
7992 -- visible in the body of the enclosing child, and only then will
7993 -- the current type be possibly non-private. Build an underlying
7994 -- full view that will be installed when the enclosing child body
7995 -- is compiled.
7997 if Present (Underlying_Full_View (Derived_Type)) then
7998 Full_Der := Underlying_Full_View (Derived_Type);
7999 else
8000 Build_Full_Derivation;
8001 Set_Underlying_Full_View (Derived_Type, Full_Der);
8002 Set_Is_Underlying_Full_View (Full_Der);
8003 end if;
8005 -- The full view will be used to swap entities on entry/exit to
8006 -- the body, and must appear in the entity list for the package.
8008 Append_Entity (Full_Der, Scope (Derived_Type));
8009 end if;
8010 end if;
8011 end Build_Derived_Private_Type;
8013 -------------------------------
8014 -- Build_Derived_Record_Type --
8015 -------------------------------
8017 -- 1. INTRODUCTION
8019 -- Ideally we would like to use the same model of type derivation for
8020 -- tagged and untagged record types. Unfortunately this is not quite
8021 -- possible because the semantics of representation clauses is different
8022 -- for tagged and untagged records under inheritance. Consider the
8023 -- following:
8025 -- type R (...) is [tagged] record ... end record;
8026 -- type T (...) is new R (...) [with ...];
8028 -- The representation clauses for T can specify a completely different
8029 -- record layout from R's. Hence the same component can be placed in two
8030 -- very different positions in objects of type T and R. If R and T are
8031 -- tagged types, representation clauses for T can only specify the layout
8032 -- of non inherited components, thus components that are common in R and T
8033 -- have the same position in objects of type R and T.
8035 -- This has two implications. The first is that the entire tree for R's
8036 -- declaration needs to be copied for T in the untagged case, so that T
8037 -- can be viewed as a record type of its own with its own representation
8038 -- clauses. The second implication is the way we handle discriminants.
8039 -- Specifically, in the untagged case we need a way to communicate to Gigi
8040 -- what are the real discriminants in the record, while for the semantics
8041 -- we need to consider those introduced by the user to rename the
8042 -- discriminants in the parent type. This is handled by introducing the
8043 -- notion of stored discriminants. See below for more.
8045 -- Fortunately the way regular components are inherited can be handled in
8046 -- the same way in tagged and untagged types.
8048 -- To complicate things a bit more the private view of a private extension
8049 -- cannot be handled in the same way as the full view (for one thing the
8050 -- semantic rules are somewhat different). We will explain what differs
8051 -- below.
8053 -- 2. DISCRIMINANTS UNDER INHERITANCE
8055 -- The semantic rules governing the discriminants of derived types are
8056 -- quite subtle.
8058 -- type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
8059 -- [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
8061 -- If parent type has discriminants, then the discriminants that are
8062 -- declared in the derived type are [3.4 (11)]:
8064 -- o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
8065 -- there is one;
8067 -- o Otherwise, each discriminant of the parent type (implicitly declared
8068 -- in the same order with the same specifications). In this case, the
8069 -- discriminants are said to be "inherited", or if unknown in the parent
8070 -- are also unknown in the derived type.
8072 -- Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
8074 -- o The parent subtype must be constrained;
8076 -- o If the parent type is not a tagged type, then each discriminant of
8077 -- the derived type must be used in the constraint defining a parent
8078 -- subtype. [Implementation note: This ensures that the new discriminant
8079 -- can share storage with an existing discriminant.]
8081 -- For the derived type each discriminant of the parent type is either
8082 -- inherited, constrained to equal some new discriminant of the derived
8083 -- type, or constrained to the value of an expression.
8085 -- When inherited or constrained to equal some new discriminant, the
8086 -- parent discriminant and the discriminant of the derived type are said
8087 -- to "correspond".
8089 -- If a discriminant of the parent type is constrained to a specific value
8090 -- in the derived type definition, then the discriminant is said to be
8091 -- "specified" by that derived type definition.
8093 -- 3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
8095 -- We have spoken about stored discriminants in point 1 (introduction)
8096 -- above. There are two sorts of stored discriminants: implicit and
8097 -- explicit. As long as the derived type inherits the same discriminants as
8098 -- the root record type, stored discriminants are the same as regular
8099 -- discriminants, and are said to be implicit. However, if any discriminant
8100 -- in the root type was renamed in the derived type, then the derived
8101 -- type will contain explicit stored discriminants. Explicit stored
8102 -- discriminants are discriminants in addition to the semantically visible
8103 -- discriminants defined for the derived type. Stored discriminants are
8104 -- used by Gigi to figure out what are the physical discriminants in
8105 -- objects of the derived type (see precise definition in einfo.ads).
8106 -- As an example, consider the following:
8108 -- type R (D1, D2, D3 : Int) is record ... end record;
8109 -- type T1 is new R;
8110 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
8111 -- type T3 is new T2;
8112 -- type T4 (Y : Int) is new T3 (Y, 99);
8114 -- The following table summarizes the discriminants and stored
8115 -- discriminants in R and T1 through T4:
8117 -- Type Discrim Stored Discrim Comment
8118 -- R (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in R
8119 -- T1 (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in T1
8120 -- T2 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T2
8121 -- T3 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T3
8122 -- T4 (Y) (D1, D2, D3) Girder discrims EXPLICIT in T4
8124 -- Field Corresponding_Discriminant (abbreviated CD below) allows us to
8125 -- find the corresponding discriminant in the parent type, while
8126 -- Original_Record_Component (abbreviated ORC below) the actual physical
8127 -- component that is renamed. Finally the field Is_Completely_Hidden
8128 -- (abbreviated ICH below) is set for all explicit stored discriminants
8129 -- (see einfo.ads for more info). For the above example this gives:
8131 -- Discrim CD ORC ICH
8132 -- ^^^^^^^ ^^ ^^^ ^^^
8133 -- D1 in R empty itself no
8134 -- D2 in R empty itself no
8135 -- D3 in R empty itself no
8137 -- D1 in T1 D1 in R itself no
8138 -- D2 in T1 D2 in R itself no
8139 -- D3 in T1 D3 in R itself no
8141 -- X1 in T2 D3 in T1 D3 in T2 no
8142 -- X2 in T2 D1 in T1 D1 in T2 no
8143 -- D1 in T2 empty itself yes
8144 -- D2 in T2 empty itself yes
8145 -- D3 in T2 empty itself yes
8147 -- X1 in T3 X1 in T2 D3 in T3 no
8148 -- X2 in T3 X2 in T2 D1 in T3 no
8149 -- D1 in T3 empty itself yes
8150 -- D2 in T3 empty itself yes
8151 -- D3 in T3 empty itself yes
8153 -- Y in T4 X1 in T3 D3 in T4 no
8154 -- D1 in T4 empty itself yes
8155 -- D2 in T4 empty itself yes
8156 -- D3 in T4 empty itself yes
8158 -- 4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
8160 -- Type derivation for tagged types is fairly straightforward. If no
8161 -- discriminants are specified by the derived type, these are inherited
8162 -- from the parent. No explicit stored discriminants are ever necessary.
8163 -- The only manipulation that is done to the tree is that of adding a
8164 -- _parent field with parent type and constrained to the same constraint
8165 -- specified for the parent in the derived type definition. For instance:
8167 -- type R (D1, D2, D3 : Int) is tagged record ... end record;
8168 -- type T1 is new R with null record;
8169 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
8171 -- are changed into:
8173 -- type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
8174 -- _parent : R (D1, D2, D3);
8175 -- end record;
8177 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
8178 -- _parent : T1 (X2, 88, X1);
8179 -- end record;
8181 -- The discriminants actually present in R, T1 and T2 as well as their CD,
8182 -- ORC and ICH fields are:
8184 -- Discrim CD ORC ICH
8185 -- ^^^^^^^ ^^ ^^^ ^^^
8186 -- D1 in R empty itself no
8187 -- D2 in R empty itself no
8188 -- D3 in R empty itself no
8190 -- D1 in T1 D1 in R D1 in R no
8191 -- D2 in T1 D2 in R D2 in R no
8192 -- D3 in T1 D3 in R D3 in R no
8194 -- X1 in T2 D3 in T1 D3 in R no
8195 -- X2 in T2 D1 in T1 D1 in R no
8197 -- 5. FIRST TRANSFORMATION FOR DERIVED RECORDS
8199 -- Regardless of whether we dealing with a tagged or untagged type
8200 -- we will transform all derived type declarations of the form
8202 -- type T is new R (...) [with ...];
8203 -- or
8204 -- subtype S is R (...);
8205 -- type T is new S [with ...];
8206 -- into
8207 -- type BT is new R [with ...];
8208 -- subtype T is BT (...);
8210 -- That is, the base derived type is constrained only if it has no
8211 -- discriminants. The reason for doing this is that GNAT's semantic model
8212 -- assumes that a base type with discriminants is unconstrained.
8214 -- Note that, strictly speaking, the above transformation is not always
8215 -- correct. Consider for instance the following excerpt from ACVC b34011a:
8217 -- procedure B34011A is
8218 -- type REC (D : integer := 0) is record
8219 -- I : Integer;
8220 -- end record;
8222 -- package P is
8223 -- type T6 is new Rec;
8224 -- function F return T6;
8225 -- end P;
8227 -- use P;
8228 -- package Q6 is
8229 -- type U is new T6 (Q6.F.I); -- ERROR: Q6.F.
8230 -- end Q6;
8232 -- The definition of Q6.U is illegal. However transforming Q6.U into
8234 -- type BaseU is new T6;
8235 -- subtype U is BaseU (Q6.F.I)
8237 -- turns U into a legal subtype, which is incorrect. To avoid this problem
8238 -- we always analyze the constraint (in this case (Q6.F.I)) before applying
8239 -- the transformation described above.
8241 -- There is another instance where the above transformation is incorrect.
8242 -- Consider:
8244 -- package Pack is
8245 -- type Base (D : Integer) is tagged null record;
8246 -- procedure P (X : Base);
8248 -- type Der is new Base (2) with null record;
8249 -- procedure P (X : Der);
8250 -- end Pack;
8252 -- Then the above transformation turns this into
8254 -- type Der_Base is new Base with null record;
8255 -- -- procedure P (X : Base) is implicitly inherited here
8256 -- -- as procedure P (X : Der_Base).
8258 -- subtype Der is Der_Base (2);
8259 -- procedure P (X : Der);
8260 -- -- The overriding of P (X : Der_Base) is illegal since we
8261 -- -- have a parameter conformance problem.
8263 -- To get around this problem, after having semantically processed Der_Base
8264 -- and the rewritten subtype declaration for Der, we copy Der_Base field
8265 -- Discriminant_Constraint from Der so that when parameter conformance is
8266 -- checked when P is overridden, no semantic errors are flagged.
8268 -- 6. SECOND TRANSFORMATION FOR DERIVED RECORDS
8270 -- Regardless of whether we are dealing with a tagged or untagged type
8271 -- we will transform all derived type declarations of the form
8273 -- type R (D1, .., Dn : ...) is [tagged] record ...;
8274 -- type T is new R [with ...];
8275 -- into
8276 -- type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
8278 -- The reason for such transformation is that it allows us to implement a
8279 -- very clean form of component inheritance as explained below.
8281 -- Note that this transformation is not achieved by direct tree rewriting
8282 -- and manipulation, but rather by redoing the semantic actions that the
8283 -- above transformation will entail. This is done directly in routine
8284 -- Inherit_Components.
8286 -- 7. TYPE DERIVATION AND COMPONENT INHERITANCE
8288 -- In both tagged and untagged derived types, regular non discriminant
8289 -- components are inherited in the derived type from the parent type. In
8290 -- the absence of discriminants component, inheritance is straightforward
8291 -- as components can simply be copied from the parent.
8293 -- If the parent has discriminants, inheriting components constrained with
8294 -- these discriminants requires caution. Consider the following example:
8296 -- type R (D1, D2 : Positive) is [tagged] record
8297 -- S : String (D1 .. D2);
8298 -- end record;
8300 -- type T1 is new R [with null record];
8301 -- type T2 (X : positive) is new R (1, X) [with null record];
8303 -- As explained in 6. above, T1 is rewritten as
8304 -- type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
8305 -- which makes the treatment for T1 and T2 identical.
8307 -- What we want when inheriting S, is that references to D1 and D2 in R are
8308 -- replaced with references to their correct constraints, i.e. D1 and D2 in
8309 -- T1 and 1 and X in T2. So all R's discriminant references are replaced
8310 -- with either discriminant references in the derived type or expressions.
8311 -- This replacement is achieved as follows: before inheriting R's
8312 -- components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
8313 -- created in the scope of T1 (resp. scope of T2) so that discriminants D1
8314 -- and D2 of T1 are visible (resp. discriminant X of T2 is visible).
8315 -- For T2, for instance, this has the effect of replacing String (D1 .. D2)
8316 -- by String (1 .. X).
8318 -- 8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
8320 -- We explain here the rules governing private type extensions relevant to
8321 -- type derivation. These rules are explained on the following example:
8323 -- type D [(...)] is new A [(...)] with private; <-- partial view
8324 -- type D [(...)] is new P [(...)] with null record; <-- full view
8326 -- Type A is called the ancestor subtype of the private extension.
8327 -- Type P is the parent type of the full view of the private extension. It
8328 -- must be A or a type derived from A.
8330 -- The rules concerning the discriminants of private type extensions are
8331 -- [7.3(10-13)]:
8333 -- o If a private extension inherits known discriminants from the ancestor
8334 -- subtype, then the full view must also inherit its discriminants from
8335 -- the ancestor subtype and the parent subtype of the full view must be
8336 -- constrained if and only if the ancestor subtype is constrained.
8338 -- o If a partial view has unknown discriminants, then the full view may
8339 -- define a definite or an indefinite subtype, with or without
8340 -- discriminants.
8342 -- o If a partial view has neither known nor unknown discriminants, then
8343 -- the full view must define a definite subtype.
8345 -- o If the ancestor subtype of a private extension has constrained
8346 -- discriminants, then the parent subtype of the full view must impose a
8347 -- statically matching constraint on those discriminants.
8349 -- This means that only the following forms of private extensions are
8350 -- allowed:
8352 -- type D is new A with private; <-- partial view
8353 -- type D is new P with null record; <-- full view
8355 -- If A has no discriminants than P has no discriminants, otherwise P must
8356 -- inherit A's discriminants.
8358 -- type D is new A (...) with private; <-- partial view
8359 -- type D is new P (:::) with null record; <-- full view
8361 -- P must inherit A's discriminants and (...) and (:::) must statically
8362 -- match.
8364 -- subtype A is R (...);
8365 -- type D is new A with private; <-- partial view
8366 -- type D is new P with null record; <-- full view
8368 -- P must have inherited R's discriminants and must be derived from A or
8369 -- any of its subtypes.
8371 -- type D (..) is new A with private; <-- partial view
8372 -- type D (..) is new P [(:::)] with null record; <-- full view
8374 -- No specific constraints on P's discriminants or constraint (:::).
8375 -- Note that A can be unconstrained, but the parent subtype P must either
8376 -- be constrained or (:::) must be present.
8378 -- type D (..) is new A [(...)] with private; <-- partial view
8379 -- type D (..) is new P [(:::)] with null record; <-- full view
8381 -- P's constraints on A's discriminants must statically match those
8382 -- imposed by (...).
8384 -- 9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
8386 -- The full view of a private extension is handled exactly as described
8387 -- above. The model chose for the private view of a private extension is
8388 -- the same for what concerns discriminants (i.e. they receive the same
8389 -- treatment as in the tagged case). However, the private view of the
8390 -- private extension always inherits the components of the parent base,
8391 -- without replacing any discriminant reference. Strictly speaking this is
8392 -- incorrect. However, Gigi never uses this view to generate code so this
8393 -- is a purely semantic issue. In theory, a set of transformations similar
8394 -- to those given in 5. and 6. above could be applied to private views of
8395 -- private extensions to have the same model of component inheritance as
8396 -- for non private extensions. However, this is not done because it would
8397 -- further complicate private type processing. Semantically speaking, this
8398 -- leaves us in an uncomfortable situation. As an example consider:
8400 -- package Pack is
8401 -- type R (D : integer) is tagged record
8402 -- S : String (1 .. D);
8403 -- end record;
8404 -- procedure P (X : R);
8405 -- type T is new R (1) with private;
8406 -- private
8407 -- type T is new R (1) with null record;
8408 -- end;
8410 -- This is transformed into:
8412 -- package Pack is
8413 -- type R (D : integer) is tagged record
8414 -- S : String (1 .. D);
8415 -- end record;
8416 -- procedure P (X : R);
8417 -- type T is new R (1) with private;
8418 -- private
8419 -- type BaseT is new R with null record;
8420 -- subtype T is BaseT (1);
8421 -- end;
8423 -- (strictly speaking the above is incorrect Ada)
8425 -- From the semantic standpoint the private view of private extension T
8426 -- should be flagged as constrained since one can clearly have
8428 -- Obj : T;
8430 -- in a unit withing Pack. However, when deriving subprograms for the
8431 -- private view of private extension T, T must be seen as unconstrained
8432 -- since T has discriminants (this is a constraint of the current
8433 -- subprogram derivation model). Thus, when processing the private view of
8434 -- a private extension such as T, we first mark T as unconstrained, we
8435 -- process it, we perform program derivation and just before returning from
8436 -- Build_Derived_Record_Type we mark T as constrained.
8438 -- ??? Are there are other uncomfortable cases that we will have to
8439 -- deal with.
8441 -- 10. RECORD_TYPE_WITH_PRIVATE complications
8443 -- Types that are derived from a visible record type and have a private
8444 -- extension present other peculiarities. They behave mostly like private
8445 -- types, but if they have primitive operations defined, these will not
8446 -- have the proper signatures for further inheritance, because other
8447 -- primitive operations will use the implicit base that we define for
8448 -- private derivations below. This affect subprogram inheritance (see
8449 -- Derive_Subprograms for details). We also derive the implicit base from
8450 -- the base type of the full view, so that the implicit base is a record
8451 -- type and not another private type, This avoids infinite loops.
8453 procedure Build_Derived_Record_Type
8454 (N : Node_Id;
8455 Parent_Type : Entity_Id;
8456 Derived_Type : Entity_Id;
8457 Derive_Subps : Boolean := True)
8459 Discriminant_Specs : constant Boolean :=
8460 Present (Discriminant_Specifications (N));
8461 Is_Tagged : constant Boolean := Is_Tagged_Type (Parent_Type);
8462 Loc : constant Source_Ptr := Sloc (N);
8463 Private_Extension : constant Boolean :=
8464 Nkind (N) = N_Private_Extension_Declaration;
8465 Assoc_List : Elist_Id;
8466 Constraint_Present : Boolean;
8467 Constrs : Elist_Id;
8468 Discrim : Entity_Id;
8469 Indic : Node_Id;
8470 Inherit_Discrims : Boolean := False;
8471 Last_Discrim : Entity_Id;
8472 New_Base : Entity_Id;
8473 New_Decl : Node_Id;
8474 New_Discrs : Elist_Id;
8475 New_Indic : Node_Id;
8476 Parent_Base : Entity_Id;
8477 Save_Etype : Entity_Id;
8478 Save_Discr_Constr : Elist_Id;
8479 Save_Next_Entity : Entity_Id;
8480 Type_Def : Node_Id;
8482 Discs : Elist_Id := New_Elmt_List;
8483 -- An empty Discs list means that there were no constraints in the
8484 -- subtype indication or that there was an error processing it.
8486 begin
8487 if Ekind (Parent_Type) = E_Record_Type_With_Private
8488 and then Present (Full_View (Parent_Type))
8489 and then Has_Discriminants (Parent_Type)
8490 then
8491 Parent_Base := Base_Type (Full_View (Parent_Type));
8492 else
8493 Parent_Base := Base_Type (Parent_Type);
8494 end if;
8496 -- AI05-0115 : if this is a derivation from a private type in some
8497 -- other scope that may lead to invisible components for the derived
8498 -- type, mark it accordingly.
8500 if Is_Private_Type (Parent_Type) then
8501 if Scope (Parent_Type) = Scope (Derived_Type) then
8502 null;
8504 elsif In_Open_Scopes (Scope (Parent_Type))
8505 and then In_Private_Part (Scope (Parent_Type))
8506 then
8507 null;
8509 else
8510 Set_Has_Private_Ancestor (Derived_Type);
8511 end if;
8513 else
8514 Set_Has_Private_Ancestor
8515 (Derived_Type, Has_Private_Ancestor (Parent_Type));
8516 end if;
8518 -- Before we start the previously documented transformations, here is
8519 -- little fix for size and alignment of tagged types. Normally when we
8520 -- derive type D from type P, we copy the size and alignment of P as the
8521 -- default for D, and in the absence of explicit representation clauses
8522 -- for D, the size and alignment are indeed the same as the parent.
8524 -- But this is wrong for tagged types, since fields may be added, and
8525 -- the default size may need to be larger, and the default alignment may
8526 -- need to be larger.
8528 -- We therefore reset the size and alignment fields in the tagged case.
8529 -- Note that the size and alignment will in any case be at least as
8530 -- large as the parent type (since the derived type has a copy of the
8531 -- parent type in the _parent field)
8533 -- The type is also marked as being tagged here, which is needed when
8534 -- processing components with a self-referential anonymous access type
8535 -- in the call to Check_Anonymous_Access_Components below. Note that
8536 -- this flag is also set later on for completeness.
8538 if Is_Tagged then
8539 Set_Is_Tagged_Type (Derived_Type);
8540 Init_Size_Align (Derived_Type);
8541 end if;
8543 -- STEP 0a: figure out what kind of derived type declaration we have
8545 if Private_Extension then
8546 Type_Def := N;
8547 Set_Ekind (Derived_Type, E_Record_Type_With_Private);
8548 Set_Default_SSO (Derived_Type);
8549 Set_No_Reordering (Derived_Type, No_Component_Reordering);
8551 else
8552 Type_Def := Type_Definition (N);
8554 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
8555 -- Parent_Base can be a private type or private extension. However,
8556 -- for tagged types with an extension the newly added fields are
8557 -- visible and hence the Derived_Type is always an E_Record_Type.
8558 -- (except that the parent may have its own private fields).
8559 -- For untagged types we preserve the Ekind of the Parent_Base.
8561 if Present (Record_Extension_Part (Type_Def)) then
8562 Set_Ekind (Derived_Type, E_Record_Type);
8563 Set_Default_SSO (Derived_Type);
8564 Set_No_Reordering (Derived_Type, No_Component_Reordering);
8566 -- Create internal access types for components with anonymous
8567 -- access types.
8569 if Ada_Version >= Ada_2005 then
8570 Check_Anonymous_Access_Components
8571 (N, Derived_Type, Derived_Type,
8572 Component_List (Record_Extension_Part (Type_Def)));
8573 end if;
8575 else
8576 Set_Ekind (Derived_Type, Ekind (Parent_Base));
8577 end if;
8578 end if;
8580 -- Indic can either be an N_Identifier if the subtype indication
8581 -- contains no constraint or an N_Subtype_Indication if the subtype
8582 -- indication has a constraint.
8584 Indic := Subtype_Indication (Type_Def);
8585 Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
8587 -- Check that the type has visible discriminants. The type may be
8588 -- a private type with unknown discriminants whose full view has
8589 -- discriminants which are invisible.
8591 if Constraint_Present then
8592 if not Has_Discriminants (Parent_Base)
8593 or else
8594 (Has_Unknown_Discriminants (Parent_Base)
8595 and then Is_Private_Type (Parent_Base))
8596 then
8597 Error_Msg_N
8598 ("invalid constraint: type has no discriminant",
8599 Constraint (Indic));
8601 Constraint_Present := False;
8602 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
8604 elsif Is_Constrained (Parent_Type) then
8605 Error_Msg_N
8606 ("invalid constraint: parent type is already constrained",
8607 Constraint (Indic));
8609 Constraint_Present := False;
8610 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
8611 end if;
8612 end if;
8614 -- STEP 0b: If needed, apply transformation given in point 5. above
8616 if not Private_Extension
8617 and then Has_Discriminants (Parent_Type)
8618 and then not Discriminant_Specs
8619 and then (Is_Constrained (Parent_Type) or else Constraint_Present)
8620 then
8621 -- First, we must analyze the constraint (see comment in point 5.)
8622 -- The constraint may come from the subtype indication of the full
8623 -- declaration.
8625 if Constraint_Present then
8626 New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
8628 -- If there is no explicit constraint, there might be one that is
8629 -- inherited from a constrained parent type. In that case verify that
8630 -- it conforms to the constraint in the partial view. In perverse
8631 -- cases the parent subtypes of the partial and full view can have
8632 -- different constraints.
8634 elsif Present (Stored_Constraint (Parent_Type)) then
8635 New_Discrs := Stored_Constraint (Parent_Type);
8637 else
8638 New_Discrs := No_Elist;
8639 end if;
8641 if Has_Discriminants (Derived_Type)
8642 and then Has_Private_Declaration (Derived_Type)
8643 and then Present (Discriminant_Constraint (Derived_Type))
8644 and then Present (New_Discrs)
8645 then
8646 -- Verify that constraints of the full view statically match
8647 -- those given in the partial view.
8649 declare
8650 C1, C2 : Elmt_Id;
8652 begin
8653 C1 := First_Elmt (New_Discrs);
8654 C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
8655 while Present (C1) and then Present (C2) loop
8656 if Fully_Conformant_Expressions (Node (C1), Node (C2))
8657 or else
8658 (Is_OK_Static_Expression (Node (C1))
8659 and then Is_OK_Static_Expression (Node (C2))
8660 and then
8661 Expr_Value (Node (C1)) = Expr_Value (Node (C2)))
8662 then
8663 null;
8665 else
8666 if Constraint_Present then
8667 Error_Msg_N
8668 ("constraint not conformant to previous declaration",
8669 Node (C1));
8670 else
8671 Error_Msg_N
8672 ("constraint of full view is incompatible "
8673 & "with partial view", N);
8674 end if;
8675 end if;
8677 Next_Elmt (C1);
8678 Next_Elmt (C2);
8679 end loop;
8680 end;
8681 end if;
8683 -- Insert and analyze the declaration for the unconstrained base type
8685 New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
8687 New_Decl :=
8688 Make_Full_Type_Declaration (Loc,
8689 Defining_Identifier => New_Base,
8690 Type_Definition =>
8691 Make_Derived_Type_Definition (Loc,
8692 Abstract_Present => Abstract_Present (Type_Def),
8693 Limited_Present => Limited_Present (Type_Def),
8694 Subtype_Indication =>
8695 New_Occurrence_Of (Parent_Base, Loc),
8696 Record_Extension_Part =>
8697 Relocate_Node (Record_Extension_Part (Type_Def)),
8698 Interface_List => Interface_List (Type_Def)));
8700 Set_Parent (New_Decl, Parent (N));
8701 Mark_Rewrite_Insertion (New_Decl);
8702 Insert_Before (N, New_Decl);
8704 -- In the extension case, make sure ancestor is frozen appropriately
8705 -- (see also non-discriminated case below).
8707 if Present (Record_Extension_Part (Type_Def))
8708 or else Is_Interface (Parent_Base)
8709 then
8710 Freeze_Before (New_Decl, Parent_Type);
8711 end if;
8713 -- Note that this call passes False for the Derive_Subps parameter
8714 -- because subprogram derivation is deferred until after creating
8715 -- the subtype (see below).
8717 Build_Derived_Type
8718 (New_Decl, Parent_Base, New_Base,
8719 Is_Completion => False, Derive_Subps => False);
8721 -- ??? This needs re-examination to determine whether the
8722 -- above call can simply be replaced by a call to Analyze.
8724 Set_Analyzed (New_Decl);
8726 -- Insert and analyze the declaration for the constrained subtype
8728 if Constraint_Present then
8729 New_Indic :=
8730 Make_Subtype_Indication (Loc,
8731 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
8732 Constraint => Relocate_Node (Constraint (Indic)));
8734 else
8735 declare
8736 Constr_List : constant List_Id := New_List;
8737 C : Elmt_Id;
8738 Expr : Node_Id;
8740 begin
8741 C := First_Elmt (Discriminant_Constraint (Parent_Type));
8742 while Present (C) loop
8743 Expr := Node (C);
8745 -- It is safe here to call New_Copy_Tree since we called
8746 -- Force_Evaluation on each constraint previously
8747 -- in Build_Discriminant_Constraints.
8749 Append (New_Copy_Tree (Expr), To => Constr_List);
8751 Next_Elmt (C);
8752 end loop;
8754 New_Indic :=
8755 Make_Subtype_Indication (Loc,
8756 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
8757 Constraint =>
8758 Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
8759 end;
8760 end if;
8762 Rewrite (N,
8763 Make_Subtype_Declaration (Loc,
8764 Defining_Identifier => Derived_Type,
8765 Subtype_Indication => New_Indic));
8767 Analyze (N);
8769 -- Derivation of subprograms must be delayed until the full subtype
8770 -- has been established, to ensure proper overriding of subprograms
8771 -- inherited by full types. If the derivations occurred as part of
8772 -- the call to Build_Derived_Type above, then the check for type
8773 -- conformance would fail because earlier primitive subprograms
8774 -- could still refer to the full type prior the change to the new
8775 -- subtype and hence would not match the new base type created here.
8776 -- Subprograms are not derived, however, when Derive_Subps is False
8777 -- (since otherwise there could be redundant derivations).
8779 if Derive_Subps then
8780 Derive_Subprograms (Parent_Type, Derived_Type);
8781 end if;
8783 -- For tagged types the Discriminant_Constraint of the new base itype
8784 -- is inherited from the first subtype so that no subtype conformance
8785 -- problem arise when the first subtype overrides primitive
8786 -- operations inherited by the implicit base type.
8788 if Is_Tagged then
8789 Set_Discriminant_Constraint
8790 (New_Base, Discriminant_Constraint (Derived_Type));
8791 end if;
8793 return;
8794 end if;
8796 -- If we get here Derived_Type will have no discriminants or it will be
8797 -- a discriminated unconstrained base type.
8799 -- STEP 1a: perform preliminary actions/checks for derived tagged types
8801 if Is_Tagged then
8803 -- The parent type is frozen for non-private extensions (RM 13.14(7))
8804 -- The declaration of a specific descendant of an interface type
8805 -- freezes the interface type (RM 13.14).
8807 if not Private_Extension or else Is_Interface (Parent_Base) then
8808 Freeze_Before (N, Parent_Type);
8809 end if;
8811 -- In Ada 2005 (AI-344), the restriction that a derived tagged type
8812 -- cannot be declared at a deeper level than its parent type is
8813 -- removed. The check on derivation within a generic body is also
8814 -- relaxed, but there's a restriction that a derived tagged type
8815 -- cannot be declared in a generic body if it's derived directly
8816 -- or indirectly from a formal type of that generic.
8818 if Ada_Version >= Ada_2005 then
8819 if Present (Enclosing_Generic_Body (Derived_Type)) then
8820 declare
8821 Ancestor_Type : Entity_Id;
8823 begin
8824 -- Check to see if any ancestor of the derived type is a
8825 -- formal type.
8827 Ancestor_Type := Parent_Type;
8828 while not Is_Generic_Type (Ancestor_Type)
8829 and then Etype (Ancestor_Type) /= Ancestor_Type
8830 loop
8831 Ancestor_Type := Etype (Ancestor_Type);
8832 end loop;
8834 -- If the derived type does have a formal type as an
8835 -- ancestor, then it's an error if the derived type is
8836 -- declared within the body of the generic unit that
8837 -- declares the formal type in its generic formal part. It's
8838 -- sufficient to check whether the ancestor type is declared
8839 -- inside the same generic body as the derived type (such as
8840 -- within a nested generic spec), in which case the
8841 -- derivation is legal. If the formal type is declared
8842 -- outside of that generic body, then it's guaranteed that
8843 -- the derived type is declared within the generic body of
8844 -- the generic unit declaring the formal type.
8846 if Is_Generic_Type (Ancestor_Type)
8847 and then Enclosing_Generic_Body (Ancestor_Type) /=
8848 Enclosing_Generic_Body (Derived_Type)
8849 then
8850 Error_Msg_NE
8851 ("parent type of& must not be descendant of formal type"
8852 & " of an enclosing generic body",
8853 Indic, Derived_Type);
8854 end if;
8855 end;
8856 end if;
8858 elsif Type_Access_Level (Derived_Type) /=
8859 Type_Access_Level (Parent_Type)
8860 and then not Is_Generic_Type (Derived_Type)
8861 then
8862 if Is_Controlled (Parent_Type) then
8863 Error_Msg_N
8864 ("controlled type must be declared at the library level",
8865 Indic);
8866 else
8867 Error_Msg_N
8868 ("type extension at deeper accessibility level than parent",
8869 Indic);
8870 end if;
8872 else
8873 declare
8874 GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
8875 begin
8876 if Present (GB)
8877 and then GB /= Enclosing_Generic_Body (Parent_Base)
8878 then
8879 Error_Msg_NE
8880 ("parent type of& must not be outside generic body"
8881 & " (RM 3.9.1(4))",
8882 Indic, Derived_Type);
8883 end if;
8884 end;
8885 end if;
8886 end if;
8888 -- Ada 2005 (AI-251)
8890 if Ada_Version >= Ada_2005 and then Is_Tagged then
8892 -- "The declaration of a specific descendant of an interface type
8893 -- freezes the interface type" (RM 13.14).
8895 declare
8896 Iface : Node_Id;
8897 begin
8898 if Is_Non_Empty_List (Interface_List (Type_Def)) then
8899 Iface := First (Interface_List (Type_Def));
8900 while Present (Iface) loop
8901 Freeze_Before (N, Etype (Iface));
8902 Next (Iface);
8903 end loop;
8904 end if;
8905 end;
8906 end if;
8908 -- STEP 1b : preliminary cleanup of the full view of private types
8910 -- If the type is already marked as having discriminants, then it's the
8911 -- completion of a private type or private extension and we need to
8912 -- retain the discriminants from the partial view if the current
8913 -- declaration has Discriminant_Specifications so that we can verify
8914 -- conformance. However, we must remove any existing components that
8915 -- were inherited from the parent (and attached in Copy_And_Swap)
8916 -- because the full type inherits all appropriate components anyway, and
8917 -- we do not want the partial view's components interfering.
8919 if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
8920 Discrim := First_Discriminant (Derived_Type);
8921 loop
8922 Last_Discrim := Discrim;
8923 Next_Discriminant (Discrim);
8924 exit when No (Discrim);
8925 end loop;
8927 Set_Last_Entity (Derived_Type, Last_Discrim);
8929 -- In all other cases wipe out the list of inherited components (even
8930 -- inherited discriminants), it will be properly rebuilt here.
8932 else
8933 Set_First_Entity (Derived_Type, Empty);
8934 Set_Last_Entity (Derived_Type, Empty);
8935 end if;
8937 -- STEP 1c: Initialize some flags for the Derived_Type
8939 -- The following flags must be initialized here so that
8940 -- Process_Discriminants can check that discriminants of tagged types do
8941 -- not have a default initial value and that access discriminants are
8942 -- only specified for limited records. For completeness, these flags are
8943 -- also initialized along with all the other flags below.
8945 -- AI-419: Limitedness is not inherited from an interface parent, so to
8946 -- be limited in that case the type must be explicitly declared as
8947 -- limited. However, task and protected interfaces are always limited.
8949 if Limited_Present (Type_Def) then
8950 Set_Is_Limited_Record (Derived_Type);
8952 elsif Is_Limited_Record (Parent_Type)
8953 or else (Present (Full_View (Parent_Type))
8954 and then Is_Limited_Record (Full_View (Parent_Type)))
8955 then
8956 if not Is_Interface (Parent_Type)
8957 or else Is_Synchronized_Interface (Parent_Type)
8958 or else Is_Protected_Interface (Parent_Type)
8959 or else Is_Task_Interface (Parent_Type)
8960 then
8961 Set_Is_Limited_Record (Derived_Type);
8962 end if;
8963 end if;
8965 -- STEP 2a: process discriminants of derived type if any
8967 Push_Scope (Derived_Type);
8969 if Discriminant_Specs then
8970 Set_Has_Unknown_Discriminants (Derived_Type, False);
8972 -- The following call initializes fields Has_Discriminants and
8973 -- Discriminant_Constraint, unless we are processing the completion
8974 -- of a private type declaration.
8976 Check_Or_Process_Discriminants (N, Derived_Type);
8978 -- For untagged types, the constraint on the Parent_Type must be
8979 -- present and is used to rename the discriminants.
8981 if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
8982 Error_Msg_N ("untagged parent must have discriminants", Indic);
8984 elsif not Is_Tagged and then not Constraint_Present then
8985 Error_Msg_N
8986 ("discriminant constraint needed for derived untagged records",
8987 Indic);
8989 -- Otherwise the parent subtype must be constrained unless we have a
8990 -- private extension.
8992 elsif not Constraint_Present
8993 and then not Private_Extension
8994 and then not Is_Constrained (Parent_Type)
8995 then
8996 Error_Msg_N
8997 ("unconstrained type not allowed in this context", Indic);
8999 elsif Constraint_Present then
9000 -- The following call sets the field Corresponding_Discriminant
9001 -- for the discriminants in the Derived_Type.
9003 Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
9005 -- For untagged types all new discriminants must rename
9006 -- discriminants in the parent. For private extensions new
9007 -- discriminants cannot rename old ones (implied by [7.3(13)]).
9009 Discrim := First_Discriminant (Derived_Type);
9010 while Present (Discrim) loop
9011 if not Is_Tagged
9012 and then No (Corresponding_Discriminant (Discrim))
9013 then
9014 Error_Msg_N
9015 ("new discriminants must constrain old ones", Discrim);
9017 elsif Private_Extension
9018 and then Present (Corresponding_Discriminant (Discrim))
9019 then
9020 Error_Msg_N
9021 ("only static constraints allowed for parent"
9022 & " discriminants in the partial view", Indic);
9023 exit;
9024 end if;
9026 -- If a new discriminant is used in the constraint, then its
9027 -- subtype must be statically compatible with the parent
9028 -- discriminant's subtype (3.7(15)).
9030 -- However, if the record contains an array constrained by
9031 -- the discriminant but with some different bound, the compiler
9032 -- tries to create a smaller range for the discriminant type.
9033 -- (See exp_ch3.Adjust_Discriminants). In this case, where
9034 -- the discriminant type is a scalar type, the check must use
9035 -- the original discriminant type in the parent declaration.
9037 declare
9038 Corr_Disc : constant Entity_Id :=
9039 Corresponding_Discriminant (Discrim);
9040 Disc_Type : constant Entity_Id := Etype (Discrim);
9041 Corr_Type : Entity_Id;
9043 begin
9044 if Present (Corr_Disc) then
9045 if Is_Scalar_Type (Disc_Type) then
9046 Corr_Type :=
9047 Entity (Discriminant_Type (Parent (Corr_Disc)));
9048 else
9049 Corr_Type := Etype (Corr_Disc);
9050 end if;
9052 if not
9053 Subtypes_Statically_Compatible (Disc_Type, Corr_Type)
9054 then
9055 Error_Msg_N
9056 ("subtype must be compatible "
9057 & "with parent discriminant",
9058 Discrim);
9059 end if;
9060 end if;
9061 end;
9063 Next_Discriminant (Discrim);
9064 end loop;
9066 -- Check whether the constraints of the full view statically
9067 -- match those imposed by the parent subtype [7.3(13)].
9069 if Present (Stored_Constraint (Derived_Type)) then
9070 declare
9071 C1, C2 : Elmt_Id;
9073 begin
9074 C1 := First_Elmt (Discs);
9075 C2 := First_Elmt (Stored_Constraint (Derived_Type));
9076 while Present (C1) and then Present (C2) loop
9077 if not
9078 Fully_Conformant_Expressions (Node (C1), Node (C2))
9079 then
9080 Error_Msg_N
9081 ("not conformant with previous declaration",
9082 Node (C1));
9083 end if;
9085 Next_Elmt (C1);
9086 Next_Elmt (C2);
9087 end loop;
9088 end;
9089 end if;
9090 end if;
9092 -- STEP 2b: No new discriminants, inherit discriminants if any
9094 else
9095 if Private_Extension then
9096 Set_Has_Unknown_Discriminants
9097 (Derived_Type,
9098 Has_Unknown_Discriminants (Parent_Type)
9099 or else Unknown_Discriminants_Present (N));
9101 -- The partial view of the parent may have unknown discriminants,
9102 -- but if the full view has discriminants and the parent type is
9103 -- in scope they must be inherited.
9105 elsif Has_Unknown_Discriminants (Parent_Type)
9106 and then
9107 (not Has_Discriminants (Parent_Type)
9108 or else not In_Open_Scopes (Scope (Parent_Type)))
9109 then
9110 Set_Has_Unknown_Discriminants (Derived_Type);
9111 end if;
9113 if not Has_Unknown_Discriminants (Derived_Type)
9114 and then not Has_Unknown_Discriminants (Parent_Base)
9115 and then Has_Discriminants (Parent_Type)
9116 then
9117 Inherit_Discrims := True;
9118 Set_Has_Discriminants
9119 (Derived_Type, True);
9120 Set_Discriminant_Constraint
9121 (Derived_Type, Discriminant_Constraint (Parent_Base));
9122 end if;
9124 -- The following test is true for private types (remember
9125 -- transformation 5. is not applied to those) and in an error
9126 -- situation.
9128 if Constraint_Present then
9129 Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
9130 end if;
9132 -- For now mark a new derived type as constrained only if it has no
9133 -- discriminants. At the end of Build_Derived_Record_Type we properly
9134 -- set this flag in the case of private extensions. See comments in
9135 -- point 9. just before body of Build_Derived_Record_Type.
9137 Set_Is_Constrained
9138 (Derived_Type,
9139 not (Inherit_Discrims
9140 or else Has_Unknown_Discriminants (Derived_Type)));
9141 end if;
9143 -- STEP 3: initialize fields of derived type
9145 Set_Is_Tagged_Type (Derived_Type, Is_Tagged);
9146 Set_Stored_Constraint (Derived_Type, No_Elist);
9148 -- Ada 2005 (AI-251): Private type-declarations can implement interfaces
9149 -- but cannot be interfaces
9151 if not Private_Extension
9152 and then Ekind (Derived_Type) /= E_Private_Type
9153 and then Ekind (Derived_Type) /= E_Limited_Private_Type
9154 then
9155 if Interface_Present (Type_Def) then
9156 Analyze_Interface_Declaration (Derived_Type, Type_Def);
9157 end if;
9159 Set_Interfaces (Derived_Type, No_Elist);
9160 end if;
9162 -- Fields inherited from the Parent_Type
9164 Set_Has_Specified_Layout
9165 (Derived_Type, Has_Specified_Layout (Parent_Type));
9166 Set_Is_Limited_Composite
9167 (Derived_Type, Is_Limited_Composite (Parent_Type));
9168 Set_Is_Private_Composite
9169 (Derived_Type, Is_Private_Composite (Parent_Type));
9171 if Is_Tagged_Type (Parent_Type) then
9172 Set_No_Tagged_Streams_Pragma
9173 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
9174 end if;
9176 -- Fields inherited from the Parent_Base
9178 Set_Has_Controlled_Component
9179 (Derived_Type, Has_Controlled_Component (Parent_Base));
9180 Set_Has_Non_Standard_Rep
9181 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
9182 Set_Has_Primitive_Operations
9183 (Derived_Type, Has_Primitive_Operations (Parent_Base));
9185 -- Set fields for private derived types
9187 if Is_Private_Type (Derived_Type) then
9188 Set_Depends_On_Private (Derived_Type, True);
9189 Set_Private_Dependents (Derived_Type, New_Elmt_List);
9190 end if;
9192 -- Inherit fields for non-private types. If this is the completion of a
9193 -- derivation from a private type, the parent itself is private and the
9194 -- attributes come from its full view, which must be present.
9196 if Is_Record_Type (Derived_Type) then
9197 declare
9198 Parent_Full : Entity_Id;
9200 begin
9201 if Is_Private_Type (Parent_Base)
9202 and then not Is_Record_Type (Parent_Base)
9203 then
9204 Parent_Full := Full_View (Parent_Base);
9205 else
9206 Parent_Full := Parent_Base;
9207 end if;
9209 Set_Component_Alignment
9210 (Derived_Type, Component_Alignment (Parent_Full));
9211 Set_C_Pass_By_Copy
9212 (Derived_Type, C_Pass_By_Copy (Parent_Full));
9213 Set_Has_Complex_Representation
9214 (Derived_Type, Has_Complex_Representation (Parent_Full));
9216 -- For untagged types, inherit the layout by default to avoid
9217 -- costly changes of representation for type conversions.
9219 if not Is_Tagged then
9220 Set_Is_Packed (Derived_Type, Is_Packed (Parent_Full));
9221 Set_No_Reordering (Derived_Type, No_Reordering (Parent_Full));
9222 end if;
9223 end;
9224 end if;
9226 -- Set fields for tagged types
9228 if Is_Tagged then
9229 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
9231 -- All tagged types defined in Ada.Finalization are controlled
9233 if Chars (Scope (Derived_Type)) = Name_Finalization
9234 and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
9235 and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
9236 then
9237 Set_Is_Controlled_Active (Derived_Type);
9238 else
9239 Set_Is_Controlled_Active
9240 (Derived_Type, Is_Controlled_Active (Parent_Base));
9241 end if;
9243 -- Minor optimization: there is no need to generate the class-wide
9244 -- entity associated with an underlying record view.
9246 if not Is_Underlying_Record_View (Derived_Type) then
9247 Make_Class_Wide_Type (Derived_Type);
9248 end if;
9250 Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
9252 if Has_Discriminants (Derived_Type)
9253 and then Constraint_Present
9254 then
9255 Set_Stored_Constraint
9256 (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
9257 end if;
9259 if Ada_Version >= Ada_2005 then
9260 declare
9261 Ifaces_List : Elist_Id;
9263 begin
9264 -- Checks rules 3.9.4 (13/2 and 14/2)
9266 if Comes_From_Source (Derived_Type)
9267 and then not Is_Private_Type (Derived_Type)
9268 and then Is_Interface (Parent_Type)
9269 and then not Is_Interface (Derived_Type)
9270 then
9271 if Is_Task_Interface (Parent_Type) then
9272 Error_Msg_N
9273 ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
9274 Derived_Type);
9276 elsif Is_Protected_Interface (Parent_Type) then
9277 Error_Msg_N
9278 ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
9279 Derived_Type);
9280 end if;
9281 end if;
9283 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
9285 Check_Interfaces (N, Type_Def);
9287 -- Ada 2005 (AI-251): Collect the list of progenitors that are
9288 -- not already in the parents.
9290 Collect_Interfaces
9291 (T => Derived_Type,
9292 Ifaces_List => Ifaces_List,
9293 Exclude_Parents => True);
9295 Set_Interfaces (Derived_Type, Ifaces_List);
9297 -- If the derived type is the anonymous type created for
9298 -- a declaration whose parent has a constraint, propagate
9299 -- the interface list to the source type. This must be done
9300 -- prior to the completion of the analysis of the source type
9301 -- because the components in the extension may contain current
9302 -- instances whose legality depends on some ancestor.
9304 if Is_Itype (Derived_Type) then
9305 declare
9306 Def : constant Node_Id :=
9307 Associated_Node_For_Itype (Derived_Type);
9308 begin
9309 if Present (Def)
9310 and then Nkind (Def) = N_Full_Type_Declaration
9311 then
9312 Set_Interfaces
9313 (Defining_Identifier (Def), Ifaces_List);
9314 end if;
9315 end;
9316 end if;
9318 -- A type extension is automatically Ghost when one of its
9319 -- progenitors is Ghost (SPARK RM 6.9(9)). This property is
9320 -- also inherited when the parent type is Ghost, but this is
9321 -- done in Build_Derived_Type as the mechanism also handles
9322 -- untagged derivations.
9324 if Implements_Ghost_Interface (Derived_Type) then
9325 Set_Is_Ghost_Entity (Derived_Type);
9326 end if;
9327 end;
9328 end if;
9329 end if;
9331 -- STEP 4: Inherit components from the parent base and constrain them.
9332 -- Apply the second transformation described in point 6. above.
9334 if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
9335 or else not Has_Discriminants (Parent_Type)
9336 or else not Is_Constrained (Parent_Type)
9337 then
9338 Constrs := Discs;
9339 else
9340 Constrs := Discriminant_Constraint (Parent_Type);
9341 end if;
9343 Assoc_List :=
9344 Inherit_Components
9345 (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
9347 -- STEP 5a: Copy the parent record declaration for untagged types
9349 Set_Has_Implicit_Dereference
9350 (Derived_Type, Has_Implicit_Dereference (Parent_Type));
9352 if not Is_Tagged then
9354 -- Discriminant_Constraint (Derived_Type) has been properly
9355 -- constructed. Save it and temporarily set it to Empty because we
9356 -- do not want the call to New_Copy_Tree below to mess this list.
9358 if Has_Discriminants (Derived_Type) then
9359 Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
9360 Set_Discriminant_Constraint (Derived_Type, No_Elist);
9361 else
9362 Save_Discr_Constr := No_Elist;
9363 end if;
9365 -- Save the Etype field of Derived_Type. It is correctly set now,
9366 -- but the call to New_Copy tree may remap it to point to itself,
9367 -- which is not what we want. Ditto for the Next_Entity field.
9369 Save_Etype := Etype (Derived_Type);
9370 Save_Next_Entity := Next_Entity (Derived_Type);
9372 -- Assoc_List maps all stored discriminants in the Parent_Base to
9373 -- stored discriminants in the Derived_Type. It is fundamental that
9374 -- no types or itypes with discriminants other than the stored
9375 -- discriminants appear in the entities declared inside
9376 -- Derived_Type, since the back end cannot deal with it.
9378 New_Decl :=
9379 New_Copy_Tree
9380 (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
9381 Copy_Dimensions_Of_Components (Derived_Type);
9383 -- Restore the fields saved prior to the New_Copy_Tree call
9384 -- and compute the stored constraint.
9386 Set_Etype (Derived_Type, Save_Etype);
9387 Set_Next_Entity (Derived_Type, Save_Next_Entity);
9389 if Has_Discriminants (Derived_Type) then
9390 Set_Discriminant_Constraint
9391 (Derived_Type, Save_Discr_Constr);
9392 Set_Stored_Constraint
9393 (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
9394 Replace_Components (Derived_Type, New_Decl);
9395 end if;
9397 -- Insert the new derived type declaration
9399 Rewrite (N, New_Decl);
9401 -- STEP 5b: Complete the processing for record extensions in generics
9403 -- There is no completion for record extensions declared in the
9404 -- parameter part of a generic, so we need to complete processing for
9405 -- these generic record extensions here. The Record_Type_Definition call
9406 -- will change the Ekind of the components from E_Void to E_Component.
9408 elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
9409 Record_Type_Definition (Empty, Derived_Type);
9411 -- STEP 5c: Process the record extension for non private tagged types
9413 elsif not Private_Extension then
9414 Expand_Record_Extension (Derived_Type, Type_Def);
9416 -- Note : previously in ASIS mode we set the Parent_Subtype of the
9417 -- derived type to propagate some semantic information. This led
9418 -- to other ASIS failures and has been removed.
9420 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
9421 -- implemented interfaces if we are in expansion mode
9423 if Expander_Active
9424 and then Has_Interfaces (Derived_Type)
9425 then
9426 Add_Interface_Tag_Components (N, Derived_Type);
9427 end if;
9429 -- Analyze the record extension
9431 Record_Type_Definition
9432 (Record_Extension_Part (Type_Def), Derived_Type);
9433 end if;
9435 End_Scope;
9437 -- Nothing else to do if there is an error in the derivation.
9438 -- An unusual case: the full view may be derived from a type in an
9439 -- instance, when the partial view was used illegally as an actual
9440 -- in that instance, leading to a circular definition.
9442 if Etype (Derived_Type) = Any_Type
9443 or else Etype (Parent_Type) = Derived_Type
9444 then
9445 return;
9446 end if;
9448 -- Set delayed freeze and then derive subprograms, we need to do
9449 -- this in this order so that derived subprograms inherit the
9450 -- derived freeze if necessary.
9452 Set_Has_Delayed_Freeze (Derived_Type);
9454 if Derive_Subps then
9455 Derive_Subprograms (Parent_Type, Derived_Type);
9456 end if;
9458 -- If we have a private extension which defines a constrained derived
9459 -- type mark as constrained here after we have derived subprograms. See
9460 -- comment on point 9. just above the body of Build_Derived_Record_Type.
9462 if Private_Extension and then Inherit_Discrims then
9463 if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
9464 Set_Is_Constrained (Derived_Type, True);
9465 Set_Discriminant_Constraint (Derived_Type, Discs);
9467 elsif Is_Constrained (Parent_Type) then
9468 Set_Is_Constrained
9469 (Derived_Type, True);
9470 Set_Discriminant_Constraint
9471 (Derived_Type, Discriminant_Constraint (Parent_Type));
9472 end if;
9473 end if;
9475 -- Update the class-wide type, which shares the now-completed entity
9476 -- list with its specific type. In case of underlying record views,
9477 -- we do not generate the corresponding class wide entity.
9479 if Is_Tagged
9480 and then not Is_Underlying_Record_View (Derived_Type)
9481 then
9482 Set_First_Entity
9483 (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
9484 Set_Last_Entity
9485 (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
9486 end if;
9488 Check_Function_Writable_Actuals (N);
9489 end Build_Derived_Record_Type;
9491 ------------------------
9492 -- Build_Derived_Type --
9493 ------------------------
9495 procedure Build_Derived_Type
9496 (N : Node_Id;
9497 Parent_Type : Entity_Id;
9498 Derived_Type : Entity_Id;
9499 Is_Completion : Boolean;
9500 Derive_Subps : Boolean := True)
9502 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
9504 begin
9505 -- Set common attributes
9507 Set_Scope (Derived_Type, Current_Scope);
9508 Set_Etype (Derived_Type, Parent_Base);
9509 Set_Ekind (Derived_Type, Ekind (Parent_Base));
9510 Propagate_Concurrent_Flags (Derived_Type, Parent_Base);
9512 Set_Size_Info (Derived_Type, Parent_Type);
9513 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
9515 Set_Is_Controlled_Active
9516 (Derived_Type, Is_Controlled_Active (Parent_Type));
9518 Set_Disable_Controlled (Derived_Type, Disable_Controlled (Parent_Type));
9519 Set_Is_Tagged_Type (Derived_Type, Is_Tagged_Type (Parent_Type));
9520 Set_Is_Volatile (Derived_Type, Is_Volatile (Parent_Type));
9522 if Is_Tagged_Type (Derived_Type) then
9523 Set_No_Tagged_Streams_Pragma
9524 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
9525 end if;
9527 -- If the parent has primitive routines, set the derived type link
9529 if Has_Primitive_Operations (Parent_Type) then
9530 Set_Derived_Type_Link (Parent_Base, Derived_Type);
9531 end if;
9533 -- If the parent type is a private subtype, the convention on the base
9534 -- type may be set in the private part, and not propagated to the
9535 -- subtype until later, so we obtain the convention from the base type.
9537 Set_Convention (Derived_Type, Convention (Parent_Base));
9539 -- Set SSO default for record or array type
9541 if (Is_Array_Type (Derived_Type) or else Is_Record_Type (Derived_Type))
9542 and then Is_Base_Type (Derived_Type)
9543 then
9544 Set_Default_SSO (Derived_Type);
9545 end if;
9547 -- A derived type inherits the Default_Initial_Condition pragma coming
9548 -- from any parent type within the derivation chain.
9550 if Has_DIC (Parent_Type) then
9551 Set_Has_Inherited_DIC (Derived_Type);
9552 end if;
9554 -- A derived type inherits any class-wide invariants coming from a
9555 -- parent type or an interface. Note that the invariant procedure of
9556 -- the parent type should not be inherited because the derived type may
9557 -- define invariants of its own.
9559 if not Is_Interface (Derived_Type) then
9560 if Has_Inherited_Invariants (Parent_Type)
9561 or else Has_Inheritable_Invariants (Parent_Type)
9562 then
9563 Set_Has_Inherited_Invariants (Derived_Type);
9565 elsif Is_Concurrent_Type (Derived_Type)
9566 or else Is_Tagged_Type (Derived_Type)
9567 then
9568 declare
9569 Iface : Entity_Id;
9570 Ifaces : Elist_Id;
9571 Iface_Elmt : Elmt_Id;
9573 begin
9574 Collect_Interfaces
9575 (T => Derived_Type,
9576 Ifaces_List => Ifaces,
9577 Exclude_Parents => True);
9579 if Present (Ifaces) then
9580 Iface_Elmt := First_Elmt (Ifaces);
9581 while Present (Iface_Elmt) loop
9582 Iface := Node (Iface_Elmt);
9584 if Has_Inheritable_Invariants (Iface) then
9585 Set_Has_Inherited_Invariants (Derived_Type);
9586 exit;
9587 end if;
9589 Next_Elmt (Iface_Elmt);
9590 end loop;
9591 end if;
9592 end;
9593 end if;
9594 end if;
9596 -- We similarly inherit predicates. Note that for scalar derived types
9597 -- the predicate is inherited from the first subtype, and not from its
9598 -- (anonymous) base type.
9600 if Has_Predicates (Parent_Type)
9601 or else Has_Predicates (First_Subtype (Parent_Type))
9602 then
9603 Set_Has_Predicates (Derived_Type);
9604 end if;
9606 -- The derived type inherits representation clauses from the parent
9607 -- type, and from any interfaces.
9609 Inherit_Rep_Item_Chain (Derived_Type, Parent_Type);
9611 declare
9612 Iface : Node_Id := First (Abstract_Interface_List (Derived_Type));
9613 begin
9614 while Present (Iface) loop
9615 Inherit_Rep_Item_Chain (Derived_Type, Entity (Iface));
9616 Next (Iface);
9617 end loop;
9618 end;
9620 -- If the parent type has delayed rep aspects, then mark the derived
9621 -- type as possibly inheriting a delayed rep aspect.
9623 if Has_Delayed_Rep_Aspects (Parent_Type) then
9624 Set_May_Inherit_Delayed_Rep_Aspects (Derived_Type);
9625 end if;
9627 -- A derived type becomes Ghost when its parent type is also Ghost
9628 -- (SPARK RM 6.9(9)). Note that the Ghost-related attributes are not
9629 -- directly inherited because the Ghost policy in effect may differ.
9631 if Is_Ghost_Entity (Parent_Type) then
9632 Set_Is_Ghost_Entity (Derived_Type);
9633 end if;
9635 -- Type dependent processing
9637 case Ekind (Parent_Type) is
9638 when Numeric_Kind =>
9639 Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
9641 when Array_Kind =>
9642 Build_Derived_Array_Type (N, Parent_Type, Derived_Type);
9644 when Class_Wide_Kind
9645 | E_Record_Subtype
9646 | E_Record_Type
9648 Build_Derived_Record_Type
9649 (N, Parent_Type, Derived_Type, Derive_Subps);
9650 return;
9652 when Enumeration_Kind =>
9653 Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
9655 when Access_Kind =>
9656 Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
9658 when Incomplete_Or_Private_Kind =>
9659 Build_Derived_Private_Type
9660 (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
9662 -- For discriminated types, the derivation includes deriving
9663 -- primitive operations. For others it is done below.
9665 if Is_Tagged_Type (Parent_Type)
9666 or else Has_Discriminants (Parent_Type)
9667 or else (Present (Full_View (Parent_Type))
9668 and then Has_Discriminants (Full_View (Parent_Type)))
9669 then
9670 return;
9671 end if;
9673 when Concurrent_Kind =>
9674 Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
9676 when others =>
9677 raise Program_Error;
9678 end case;
9680 -- Nothing more to do if some error occurred
9682 if Etype (Derived_Type) = Any_Type then
9683 return;
9684 end if;
9686 -- Set delayed freeze and then derive subprograms, we need to do this
9687 -- in this order so that derived subprograms inherit the derived freeze
9688 -- if necessary.
9690 Set_Has_Delayed_Freeze (Derived_Type);
9692 if Derive_Subps then
9693 Derive_Subprograms (Parent_Type, Derived_Type);
9694 end if;
9696 Set_Has_Primitive_Operations
9697 (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
9698 end Build_Derived_Type;
9700 -----------------------
9701 -- Build_Discriminal --
9702 -----------------------
9704 procedure Build_Discriminal (Discrim : Entity_Id) is
9705 D_Minal : Entity_Id;
9706 CR_Disc : Entity_Id;
9708 begin
9709 -- A discriminal has the same name as the discriminant
9711 D_Minal := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9713 Set_Ekind (D_Minal, E_In_Parameter);
9714 Set_Mechanism (D_Minal, Default_Mechanism);
9715 Set_Etype (D_Minal, Etype (Discrim));
9716 Set_Scope (D_Minal, Current_Scope);
9717 Set_Parent (D_Minal, Parent (Discrim));
9719 Set_Discriminal (Discrim, D_Minal);
9720 Set_Discriminal_Link (D_Minal, Discrim);
9722 -- For task types, build at once the discriminants of the corresponding
9723 -- record, which are needed if discriminants are used in entry defaults
9724 -- and in family bounds.
9726 if Is_Concurrent_Type (Current_Scope)
9727 or else
9728 Is_Limited_Type (Current_Scope)
9729 then
9730 CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9732 Set_Ekind (CR_Disc, E_In_Parameter);
9733 Set_Mechanism (CR_Disc, Default_Mechanism);
9734 Set_Etype (CR_Disc, Etype (Discrim));
9735 Set_Scope (CR_Disc, Current_Scope);
9736 Set_Discriminal_Link (CR_Disc, Discrim);
9737 Set_CR_Discriminant (Discrim, CR_Disc);
9738 end if;
9739 end Build_Discriminal;
9741 ------------------------------------
9742 -- Build_Discriminant_Constraints --
9743 ------------------------------------
9745 function Build_Discriminant_Constraints
9746 (T : Entity_Id;
9747 Def : Node_Id;
9748 Derived_Def : Boolean := False) return Elist_Id
9750 C : constant Node_Id := Constraint (Def);
9751 Nb_Discr : constant Nat := Number_Discriminants (T);
9753 Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
9754 -- Saves the expression corresponding to a given discriminant in T
9756 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
9757 -- Return the Position number within array Discr_Expr of a discriminant
9758 -- D within the discriminant list of the discriminated type T.
9760 procedure Process_Discriminant_Expression
9761 (Expr : Node_Id;
9762 D : Entity_Id);
9763 -- If this is a discriminant constraint on a partial view, do not
9764 -- generate an overflow check on the discriminant expression. The check
9765 -- will be generated when constraining the full view. Otherwise the
9766 -- backend creates duplicate symbols for the temporaries corresponding
9767 -- to the expressions to be checked, causing spurious assembler errors.
9769 ------------------
9770 -- Pos_Of_Discr --
9771 ------------------
9773 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
9774 Disc : Entity_Id;
9776 begin
9777 Disc := First_Discriminant (T);
9778 for J in Discr_Expr'Range loop
9779 if Disc = D then
9780 return J;
9781 end if;
9783 Next_Discriminant (Disc);
9784 end loop;
9786 -- Note: Since this function is called on discriminants that are
9787 -- known to belong to the discriminated type, falling through the
9788 -- loop with no match signals an internal compiler error.
9790 raise Program_Error;
9791 end Pos_Of_Discr;
9793 -------------------------------------
9794 -- Process_Discriminant_Expression --
9795 -------------------------------------
9797 procedure Process_Discriminant_Expression
9798 (Expr : Node_Id;
9799 D : Entity_Id)
9801 BDT : constant Entity_Id := Base_Type (Etype (D));
9803 begin
9804 -- If this is a discriminant constraint on a partial view, do
9805 -- not generate an overflow on the discriminant expression. The
9806 -- check will be generated when constraining the full view.
9808 if Is_Private_Type (T)
9809 and then Present (Full_View (T))
9810 then
9811 Analyze_And_Resolve (Expr, BDT, Suppress => Overflow_Check);
9812 else
9813 Analyze_And_Resolve (Expr, BDT);
9814 end if;
9815 end Process_Discriminant_Expression;
9817 -- Declarations local to Build_Discriminant_Constraints
9819 Discr : Entity_Id;
9820 E : Entity_Id;
9821 Elist : constant Elist_Id := New_Elmt_List;
9823 Constr : Node_Id;
9824 Expr : Node_Id;
9825 Id : Node_Id;
9826 Position : Nat;
9827 Found : Boolean;
9829 Discrim_Present : Boolean := False;
9831 -- Start of processing for Build_Discriminant_Constraints
9833 begin
9834 -- The following loop will process positional associations only.
9835 -- For a positional association, the (single) discriminant is
9836 -- implicitly specified by position, in textual order (RM 3.7.2).
9838 Discr := First_Discriminant (T);
9839 Constr := First (Constraints (C));
9840 for D in Discr_Expr'Range loop
9841 exit when Nkind (Constr) = N_Discriminant_Association;
9843 if No (Constr) then
9844 Error_Msg_N ("too few discriminants given in constraint", C);
9845 return New_Elmt_List;
9847 elsif Nkind (Constr) = N_Range
9848 or else (Nkind (Constr) = N_Attribute_Reference
9849 and then Attribute_Name (Constr) = Name_Range)
9850 then
9851 Error_Msg_N
9852 ("a range is not a valid discriminant constraint", Constr);
9853 Discr_Expr (D) := Error;
9855 else
9856 Process_Discriminant_Expression (Constr, Discr);
9857 Discr_Expr (D) := Constr;
9858 end if;
9860 Next_Discriminant (Discr);
9861 Next (Constr);
9862 end loop;
9864 if No (Discr) and then Present (Constr) then
9865 Error_Msg_N ("too many discriminants given in constraint", Constr);
9866 return New_Elmt_List;
9867 end if;
9869 -- Named associations can be given in any order, but if both positional
9870 -- and named associations are used in the same discriminant constraint,
9871 -- then positional associations must occur first, at their normal
9872 -- position. Hence once a named association is used, the rest of the
9873 -- discriminant constraint must use only named associations.
9875 while Present (Constr) loop
9877 -- Positional association forbidden after a named association
9879 if Nkind (Constr) /= N_Discriminant_Association then
9880 Error_Msg_N ("positional association follows named one", Constr);
9881 return New_Elmt_List;
9883 -- Otherwise it is a named association
9885 else
9886 -- E records the type of the discriminants in the named
9887 -- association. All the discriminants specified in the same name
9888 -- association must have the same type.
9890 E := Empty;
9892 -- Search the list of discriminants in T to see if the simple name
9893 -- given in the constraint matches any of them.
9895 Id := First (Selector_Names (Constr));
9896 while Present (Id) loop
9897 Found := False;
9899 -- If Original_Discriminant is present, we are processing a
9900 -- generic instantiation and this is an instance node. We need
9901 -- to find the name of the corresponding discriminant in the
9902 -- actual record type T and not the name of the discriminant in
9903 -- the generic formal. Example:
9905 -- generic
9906 -- type G (D : int) is private;
9907 -- package P is
9908 -- subtype W is G (D => 1);
9909 -- end package;
9910 -- type Rec (X : int) is record ... end record;
9911 -- package Q is new P (G => Rec);
9913 -- At the point of the instantiation, formal type G is Rec
9914 -- and therefore when reanalyzing "subtype W is G (D => 1);"
9915 -- which really looks like "subtype W is Rec (D => 1);" at
9916 -- the point of instantiation, we want to find the discriminant
9917 -- that corresponds to D in Rec, i.e. X.
9919 if Present (Original_Discriminant (Id))
9920 and then In_Instance
9921 then
9922 Discr := Find_Corresponding_Discriminant (Id, T);
9923 Found := True;
9925 else
9926 Discr := First_Discriminant (T);
9927 while Present (Discr) loop
9928 if Chars (Discr) = Chars (Id) then
9929 Found := True;
9930 exit;
9931 end if;
9933 Next_Discriminant (Discr);
9934 end loop;
9936 if not Found then
9937 Error_Msg_N ("& does not match any discriminant", Id);
9938 return New_Elmt_List;
9940 -- If the parent type is a generic formal, preserve the
9941 -- name of the discriminant for subsequent instances.
9942 -- see comment at the beginning of this if statement.
9944 elsif Is_Generic_Type (Root_Type (T)) then
9945 Set_Original_Discriminant (Id, Discr);
9946 end if;
9947 end if;
9949 Position := Pos_Of_Discr (T, Discr);
9951 if Present (Discr_Expr (Position)) then
9952 Error_Msg_N ("duplicate constraint for discriminant&", Id);
9954 else
9955 -- Each discriminant specified in the same named association
9956 -- must be associated with a separate copy of the
9957 -- corresponding expression.
9959 if Present (Next (Id)) then
9960 Expr := New_Copy_Tree (Expression (Constr));
9961 Set_Parent (Expr, Parent (Expression (Constr)));
9962 else
9963 Expr := Expression (Constr);
9964 end if;
9966 Discr_Expr (Position) := Expr;
9967 Process_Discriminant_Expression (Expr, Discr);
9968 end if;
9970 -- A discriminant association with more than one discriminant
9971 -- name is only allowed if the named discriminants are all of
9972 -- the same type (RM 3.7.1(8)).
9974 if E = Empty then
9975 E := Base_Type (Etype (Discr));
9977 elsif Base_Type (Etype (Discr)) /= E then
9978 Error_Msg_N
9979 ("all discriminants in an association " &
9980 "must have the same type", Id);
9981 end if;
9983 Next (Id);
9984 end loop;
9985 end if;
9987 Next (Constr);
9988 end loop;
9990 -- A discriminant constraint must provide exactly one value for each
9991 -- discriminant of the type (RM 3.7.1(8)).
9993 for J in Discr_Expr'Range loop
9994 if No (Discr_Expr (J)) then
9995 Error_Msg_N ("too few discriminants given in constraint", C);
9996 return New_Elmt_List;
9997 end if;
9998 end loop;
10000 -- Determine if there are discriminant expressions in the constraint
10002 for J in Discr_Expr'Range loop
10003 if Denotes_Discriminant
10004 (Discr_Expr (J), Check_Concurrent => True)
10005 then
10006 Discrim_Present := True;
10007 end if;
10008 end loop;
10010 -- Build an element list consisting of the expressions given in the
10011 -- discriminant constraint and apply the appropriate checks. The list
10012 -- is constructed after resolving any named discriminant associations
10013 -- and therefore the expressions appear in the textual order of the
10014 -- discriminants.
10016 Discr := First_Discriminant (T);
10017 for J in Discr_Expr'Range loop
10018 if Discr_Expr (J) /= Error then
10019 Append_Elmt (Discr_Expr (J), Elist);
10021 -- If any of the discriminant constraints is given by a
10022 -- discriminant and we are in a derived type declaration we
10023 -- have a discriminant renaming. Establish link between new
10024 -- and old discriminant. The new discriminant has an implicit
10025 -- dereference if the old one does.
10027 if Denotes_Discriminant (Discr_Expr (J)) then
10028 if Derived_Def then
10029 declare
10030 New_Discr : constant Entity_Id := Entity (Discr_Expr (J));
10032 begin
10033 Set_Corresponding_Discriminant (New_Discr, Discr);
10034 Set_Has_Implicit_Dereference (New_Discr,
10035 Has_Implicit_Dereference (Discr));
10036 end;
10037 end if;
10039 -- Force the evaluation of non-discriminant expressions.
10040 -- If we have found a discriminant in the constraint 3.4(26)
10041 -- and 3.8(18) demand that no range checks are performed are
10042 -- after evaluation. If the constraint is for a component
10043 -- definition that has a per-object constraint, expressions are
10044 -- evaluated but not checked either. In all other cases perform
10045 -- a range check.
10047 else
10048 if Discrim_Present then
10049 null;
10051 elsif Nkind (Parent (Parent (Def))) = N_Component_Declaration
10052 and then Has_Per_Object_Constraint
10053 (Defining_Identifier (Parent (Parent (Def))))
10054 then
10055 null;
10057 elsif Is_Access_Type (Etype (Discr)) then
10058 Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
10060 else
10061 Apply_Range_Check (Discr_Expr (J), Etype (Discr));
10062 end if;
10064 Force_Evaluation (Discr_Expr (J));
10065 end if;
10067 -- Check that the designated type of an access discriminant's
10068 -- expression is not a class-wide type unless the discriminant's
10069 -- designated type is also class-wide.
10071 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
10072 and then not Is_Class_Wide_Type
10073 (Designated_Type (Etype (Discr)))
10074 and then Etype (Discr_Expr (J)) /= Any_Type
10075 and then Is_Class_Wide_Type
10076 (Designated_Type (Etype (Discr_Expr (J))))
10077 then
10078 Wrong_Type (Discr_Expr (J), Etype (Discr));
10080 elsif Is_Access_Type (Etype (Discr))
10081 and then not Is_Access_Constant (Etype (Discr))
10082 and then Is_Access_Type (Etype (Discr_Expr (J)))
10083 and then Is_Access_Constant (Etype (Discr_Expr (J)))
10084 then
10085 Error_Msg_NE
10086 ("constraint for discriminant& must be access to variable",
10087 Def, Discr);
10088 end if;
10089 end if;
10091 Next_Discriminant (Discr);
10092 end loop;
10094 return Elist;
10095 end Build_Discriminant_Constraints;
10097 ---------------------------------
10098 -- Build_Discriminated_Subtype --
10099 ---------------------------------
10101 procedure Build_Discriminated_Subtype
10102 (T : Entity_Id;
10103 Def_Id : Entity_Id;
10104 Elist : Elist_Id;
10105 Related_Nod : Node_Id;
10106 For_Access : Boolean := False)
10108 Has_Discrs : constant Boolean := Has_Discriminants (T);
10109 Constrained : constant Boolean :=
10110 (Has_Discrs
10111 and then not Is_Empty_Elmt_List (Elist)
10112 and then not Is_Class_Wide_Type (T))
10113 or else Is_Constrained (T);
10115 begin
10116 if Ekind (T) = E_Record_Type then
10117 if For_Access then
10118 Set_Ekind (Def_Id, E_Private_Subtype);
10119 Set_Is_For_Access_Subtype (Def_Id, True);
10120 else
10121 Set_Ekind (Def_Id, E_Record_Subtype);
10122 end if;
10124 -- Inherit preelaboration flag from base, for types for which it
10125 -- may have been set: records, private types, protected types.
10127 Set_Known_To_Have_Preelab_Init
10128 (Def_Id, Known_To_Have_Preelab_Init (T));
10130 elsif Ekind (T) = E_Task_Type then
10131 Set_Ekind (Def_Id, E_Task_Subtype);
10133 elsif Ekind (T) = E_Protected_Type then
10134 Set_Ekind (Def_Id, E_Protected_Subtype);
10135 Set_Known_To_Have_Preelab_Init
10136 (Def_Id, Known_To_Have_Preelab_Init (T));
10138 elsif Is_Private_Type (T) then
10139 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
10140 Set_Known_To_Have_Preelab_Init
10141 (Def_Id, Known_To_Have_Preelab_Init (T));
10143 -- Private subtypes may have private dependents
10145 Set_Private_Dependents (Def_Id, New_Elmt_List);
10147 elsif Is_Class_Wide_Type (T) then
10148 Set_Ekind (Def_Id, E_Class_Wide_Subtype);
10150 else
10151 -- Incomplete type. Attach subtype to list of dependents, to be
10152 -- completed with full view of parent type, unless is it the
10153 -- designated subtype of a record component within an init_proc.
10154 -- This last case arises for a component of an access type whose
10155 -- designated type is incomplete (e.g. a Taft Amendment type).
10156 -- The designated subtype is within an inner scope, and needs no
10157 -- elaboration, because only the access type is needed in the
10158 -- initialization procedure.
10160 if Ekind (T) = E_Incomplete_Type then
10161 Set_Ekind (Def_Id, E_Incomplete_Subtype);
10162 else
10163 Set_Ekind (Def_Id, Ekind (T));
10164 end if;
10166 if For_Access and then Within_Init_Proc then
10167 null;
10168 else
10169 Append_Elmt (Def_Id, Private_Dependents (T));
10170 end if;
10171 end if;
10173 Set_Etype (Def_Id, T);
10174 Init_Size_Align (Def_Id);
10175 Set_Has_Discriminants (Def_Id, Has_Discrs);
10176 Set_Is_Constrained (Def_Id, Constrained);
10178 Set_First_Entity (Def_Id, First_Entity (T));
10179 Set_Last_Entity (Def_Id, Last_Entity (T));
10180 Set_Has_Implicit_Dereference
10181 (Def_Id, Has_Implicit_Dereference (T));
10182 Set_Has_Pragma_Unreferenced_Objects
10183 (Def_Id, Has_Pragma_Unreferenced_Objects (T));
10185 -- If the subtype is the completion of a private declaration, there may
10186 -- have been representation clauses for the partial view, and they must
10187 -- be preserved. Build_Derived_Type chains the inherited clauses with
10188 -- the ones appearing on the extension. If this comes from a subtype
10189 -- declaration, all clauses are inherited.
10191 if No (First_Rep_Item (Def_Id)) then
10192 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10193 end if;
10195 if Is_Tagged_Type (T) then
10196 Set_Is_Tagged_Type (Def_Id);
10197 Set_No_Tagged_Streams_Pragma (Def_Id, No_Tagged_Streams_Pragma (T));
10198 Make_Class_Wide_Type (Def_Id);
10199 end if;
10201 Set_Stored_Constraint (Def_Id, No_Elist);
10203 if Has_Discrs then
10204 Set_Discriminant_Constraint (Def_Id, Elist);
10205 Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
10206 end if;
10208 if Is_Tagged_Type (T) then
10210 -- Ada 2005 (AI-251): In case of concurrent types we inherit the
10211 -- concurrent record type (which has the list of primitive
10212 -- operations).
10214 if Ada_Version >= Ada_2005
10215 and then Is_Concurrent_Type (T)
10216 then
10217 Set_Corresponding_Record_Type (Def_Id,
10218 Corresponding_Record_Type (T));
10219 else
10220 Set_Direct_Primitive_Operations (Def_Id,
10221 Direct_Primitive_Operations (T));
10222 end if;
10224 Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
10225 end if;
10227 -- Subtypes introduced by component declarations do not need to be
10228 -- marked as delayed, and do not get freeze nodes, because the semantics
10229 -- verifies that the parents of the subtypes are frozen before the
10230 -- enclosing record is frozen.
10232 if not Is_Type (Scope (Def_Id)) then
10233 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
10235 if Is_Private_Type (T)
10236 and then Present (Full_View (T))
10237 then
10238 Conditional_Delay (Def_Id, Full_View (T));
10239 else
10240 Conditional_Delay (Def_Id, T);
10241 end if;
10242 end if;
10244 if Is_Record_Type (T) then
10245 Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
10247 if Has_Discrs
10248 and then not Is_Empty_Elmt_List (Elist)
10249 and then not For_Access
10250 then
10251 Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
10253 elsif not For_Access then
10254 Set_Cloned_Subtype (Def_Id, T);
10255 end if;
10256 end if;
10257 end Build_Discriminated_Subtype;
10259 ---------------------------
10260 -- Build_Itype_Reference --
10261 ---------------------------
10263 procedure Build_Itype_Reference
10264 (Ityp : Entity_Id;
10265 Nod : Node_Id)
10267 IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
10268 begin
10270 -- Itype references are only created for use by the back-end
10272 if Inside_A_Generic then
10273 return;
10274 else
10275 Set_Itype (IR, Ityp);
10277 -- If Nod is a library unit entity, then Insert_After won't work,
10278 -- because Nod is not a member of any list. Therefore, we use
10279 -- Add_Global_Declaration in this case. This can happen if we have a
10280 -- build-in-place library function.
10282 if (Nkind (Nod) in N_Entity and then Is_Compilation_Unit (Nod))
10283 or else
10284 (Nkind (Nod) = N_Defining_Program_Unit_Name
10285 and then Is_Compilation_Unit (Defining_Identifier (Nod)))
10286 then
10287 Add_Global_Declaration (IR);
10288 else
10289 Insert_After (Nod, IR);
10290 end if;
10291 end if;
10292 end Build_Itype_Reference;
10294 ------------------------
10295 -- Build_Scalar_Bound --
10296 ------------------------
10298 function Build_Scalar_Bound
10299 (Bound : Node_Id;
10300 Par_T : Entity_Id;
10301 Der_T : Entity_Id) return Node_Id
10303 New_Bound : Entity_Id;
10305 begin
10306 -- Note: not clear why this is needed, how can the original bound
10307 -- be unanalyzed at this point? and if it is, what business do we
10308 -- have messing around with it? and why is the base type of the
10309 -- parent type the right type for the resolution. It probably is
10310 -- not. It is OK for the new bound we are creating, but not for
10311 -- the old one??? Still if it never happens, no problem.
10313 Analyze_And_Resolve (Bound, Base_Type (Par_T));
10315 if Nkind_In (Bound, N_Integer_Literal, N_Real_Literal) then
10316 New_Bound := New_Copy (Bound);
10317 Set_Etype (New_Bound, Der_T);
10318 Set_Analyzed (New_Bound);
10320 elsif Is_Entity_Name (Bound) then
10321 New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
10323 -- The following is almost certainly wrong. What business do we have
10324 -- relocating a node (Bound) that is presumably still attached to
10325 -- the tree elsewhere???
10327 else
10328 New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
10329 end if;
10331 Set_Etype (New_Bound, Der_T);
10332 return New_Bound;
10333 end Build_Scalar_Bound;
10335 --------------------------------
10336 -- Build_Underlying_Full_View --
10337 --------------------------------
10339 procedure Build_Underlying_Full_View
10340 (N : Node_Id;
10341 Typ : Entity_Id;
10342 Par : Entity_Id)
10344 Loc : constant Source_Ptr := Sloc (N);
10345 Subt : constant Entity_Id :=
10346 Make_Defining_Identifier
10347 (Loc, New_External_Name (Chars (Typ), 'S'));
10349 Constr : Node_Id;
10350 Indic : Node_Id;
10351 C : Node_Id;
10352 Id : Node_Id;
10354 procedure Set_Discriminant_Name (Id : Node_Id);
10355 -- If the derived type has discriminants, they may rename discriminants
10356 -- of the parent. When building the full view of the parent, we need to
10357 -- recover the names of the original discriminants if the constraint is
10358 -- given by named associations.
10360 ---------------------------
10361 -- Set_Discriminant_Name --
10362 ---------------------------
10364 procedure Set_Discriminant_Name (Id : Node_Id) is
10365 Disc : Entity_Id;
10367 begin
10368 Set_Original_Discriminant (Id, Empty);
10370 if Has_Discriminants (Typ) then
10371 Disc := First_Discriminant (Typ);
10372 while Present (Disc) loop
10373 if Chars (Disc) = Chars (Id)
10374 and then Present (Corresponding_Discriminant (Disc))
10375 then
10376 Set_Chars (Id, Chars (Corresponding_Discriminant (Disc)));
10377 end if;
10378 Next_Discriminant (Disc);
10379 end loop;
10380 end if;
10381 end Set_Discriminant_Name;
10383 -- Start of processing for Build_Underlying_Full_View
10385 begin
10386 if Nkind (N) = N_Full_Type_Declaration then
10387 Constr := Constraint (Subtype_Indication (Type_Definition (N)));
10389 elsif Nkind (N) = N_Subtype_Declaration then
10390 Constr := New_Copy_Tree (Constraint (Subtype_Indication (N)));
10392 elsif Nkind (N) = N_Component_Declaration then
10393 Constr :=
10394 New_Copy_Tree
10395 (Constraint (Subtype_Indication (Component_Definition (N))));
10397 else
10398 raise Program_Error;
10399 end if;
10401 C := First (Constraints (Constr));
10402 while Present (C) loop
10403 if Nkind (C) = N_Discriminant_Association then
10404 Id := First (Selector_Names (C));
10405 while Present (Id) loop
10406 Set_Discriminant_Name (Id);
10407 Next (Id);
10408 end loop;
10409 end if;
10411 Next (C);
10412 end loop;
10414 Indic :=
10415 Make_Subtype_Declaration (Loc,
10416 Defining_Identifier => Subt,
10417 Subtype_Indication =>
10418 Make_Subtype_Indication (Loc,
10419 Subtype_Mark => New_Occurrence_Of (Par, Loc),
10420 Constraint => New_Copy_Tree (Constr)));
10422 -- If this is a component subtype for an outer itype, it is not
10423 -- a list member, so simply set the parent link for analysis: if
10424 -- the enclosing type does not need to be in a declarative list,
10425 -- neither do the components.
10427 if Is_List_Member (N)
10428 and then Nkind (N) /= N_Component_Declaration
10429 then
10430 Insert_Before (N, Indic);
10431 else
10432 Set_Parent (Indic, Parent (N));
10433 end if;
10435 Analyze (Indic);
10436 Set_Underlying_Full_View (Typ, Full_View (Subt));
10437 Set_Is_Underlying_Full_View (Full_View (Subt));
10438 end Build_Underlying_Full_View;
10440 -------------------------------
10441 -- Check_Abstract_Overriding --
10442 -------------------------------
10444 procedure Check_Abstract_Overriding (T : Entity_Id) is
10445 Alias_Subp : Entity_Id;
10446 Elmt : Elmt_Id;
10447 Op_List : Elist_Id;
10448 Subp : Entity_Id;
10449 Type_Def : Node_Id;
10451 procedure Check_Pragma_Implemented (Subp : Entity_Id);
10452 -- Ada 2012 (AI05-0030): Subprogram Subp overrides an interface routine
10453 -- which has pragma Implemented already set. Check whether Subp's entity
10454 -- kind conforms to the implementation kind of the overridden routine.
10456 procedure Check_Pragma_Implemented
10457 (Subp : Entity_Id;
10458 Iface_Subp : Entity_Id);
10459 -- Ada 2012 (AI05-0030): Subprogram Subp overrides interface routine
10460 -- Iface_Subp and both entities have pragma Implemented already set on
10461 -- them. Check whether the two implementation kinds are conforming.
10463 procedure Inherit_Pragma_Implemented
10464 (Subp : Entity_Id;
10465 Iface_Subp : Entity_Id);
10466 -- Ada 2012 (AI05-0030): Interface primitive Subp overrides interface
10467 -- subprogram Iface_Subp which has been marked by pragma Implemented.
10468 -- Propagate the implementation kind of Iface_Subp to Subp.
10470 ------------------------------
10471 -- Check_Pragma_Implemented --
10472 ------------------------------
10474 procedure Check_Pragma_Implemented (Subp : Entity_Id) is
10475 Iface_Alias : constant Entity_Id := Interface_Alias (Subp);
10476 Impl_Kind : constant Name_Id := Implementation_Kind (Iface_Alias);
10477 Subp_Alias : constant Entity_Id := Alias (Subp);
10478 Contr_Typ : Entity_Id;
10479 Impl_Subp : Entity_Id;
10481 begin
10482 -- Subp must have an alias since it is a hidden entity used to link
10483 -- an interface subprogram to its overriding counterpart.
10485 pragma Assert (Present (Subp_Alias));
10487 -- Handle aliases to synchronized wrappers
10489 Impl_Subp := Subp_Alias;
10491 if Is_Primitive_Wrapper (Impl_Subp) then
10492 Impl_Subp := Wrapped_Entity (Impl_Subp);
10493 end if;
10495 -- Extract the type of the controlling formal
10497 Contr_Typ := Etype (First_Formal (Subp_Alias));
10499 if Is_Concurrent_Record_Type (Contr_Typ) then
10500 Contr_Typ := Corresponding_Concurrent_Type (Contr_Typ);
10501 end if;
10503 -- An interface subprogram whose implementation kind is By_Entry must
10504 -- be implemented by an entry.
10506 if Impl_Kind = Name_By_Entry
10507 and then Ekind (Impl_Subp) /= E_Entry
10508 then
10509 Error_Msg_Node_2 := Iface_Alias;
10510 Error_Msg_NE
10511 ("type & must implement abstract subprogram & with an entry",
10512 Subp_Alias, Contr_Typ);
10514 elsif Impl_Kind = Name_By_Protected_Procedure then
10516 -- An interface subprogram whose implementation kind is By_
10517 -- Protected_Procedure cannot be implemented by a primitive
10518 -- procedure of a task type.
10520 if Ekind (Contr_Typ) /= E_Protected_Type then
10521 Error_Msg_Node_2 := Contr_Typ;
10522 Error_Msg_NE
10523 ("interface subprogram & cannot be implemented by a " &
10524 "primitive procedure of task type &", Subp_Alias,
10525 Iface_Alias);
10527 -- An interface subprogram whose implementation kind is By_
10528 -- Protected_Procedure must be implemented by a procedure.
10530 elsif Ekind (Impl_Subp) /= E_Procedure then
10531 Error_Msg_Node_2 := Iface_Alias;
10532 Error_Msg_NE
10533 ("type & must implement abstract subprogram & with a " &
10534 "procedure", Subp_Alias, Contr_Typ);
10536 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
10537 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
10538 then
10539 Error_Msg_Name_1 := Impl_Kind;
10540 Error_Msg_N
10541 ("overriding operation& must have synchronization%",
10542 Subp_Alias);
10543 end if;
10545 -- If primitive has Optional synchronization, overriding operation
10546 -- must match if it has an explicit synchronization..
10548 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
10549 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
10550 then
10551 Error_Msg_Name_1 := Impl_Kind;
10552 Error_Msg_N
10553 ("overriding operation& must have syncrhonization%",
10554 Subp_Alias);
10555 end if;
10556 end Check_Pragma_Implemented;
10558 ------------------------------
10559 -- Check_Pragma_Implemented --
10560 ------------------------------
10562 procedure Check_Pragma_Implemented
10563 (Subp : Entity_Id;
10564 Iface_Subp : Entity_Id)
10566 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
10567 Subp_Kind : constant Name_Id := Implementation_Kind (Subp);
10569 begin
10570 -- Ada 2012 (AI05-0030): The implementation kinds of an overridden
10571 -- and overriding subprogram are different. In general this is an
10572 -- error except when the implementation kind of the overridden
10573 -- subprograms is By_Any or Optional.
10575 if Iface_Kind /= Subp_Kind
10576 and then Iface_Kind /= Name_By_Any
10577 and then Iface_Kind /= Name_Optional
10578 then
10579 if Iface_Kind = Name_By_Entry then
10580 Error_Msg_N
10581 ("incompatible implementation kind, overridden subprogram " &
10582 "is marked By_Entry", Subp);
10583 else
10584 Error_Msg_N
10585 ("incompatible implementation kind, overridden subprogram " &
10586 "is marked By_Protected_Procedure", Subp);
10587 end if;
10588 end if;
10589 end Check_Pragma_Implemented;
10591 --------------------------------
10592 -- Inherit_Pragma_Implemented --
10593 --------------------------------
10595 procedure Inherit_Pragma_Implemented
10596 (Subp : Entity_Id;
10597 Iface_Subp : Entity_Id)
10599 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
10600 Loc : constant Source_Ptr := Sloc (Subp);
10601 Impl_Prag : Node_Id;
10603 begin
10604 -- Since the implementation kind is stored as a representation item
10605 -- rather than a flag, create a pragma node.
10607 Impl_Prag :=
10608 Make_Pragma (Loc,
10609 Chars => Name_Implemented,
10610 Pragma_Argument_Associations => New_List (
10611 Make_Pragma_Argument_Association (Loc,
10612 Expression => New_Occurrence_Of (Subp, Loc)),
10614 Make_Pragma_Argument_Association (Loc,
10615 Expression => Make_Identifier (Loc, Iface_Kind))));
10617 -- The pragma doesn't need to be analyzed because it is internally
10618 -- built. It is safe to directly register it as a rep item since we
10619 -- are only interested in the characters of the implementation kind.
10621 Record_Rep_Item (Subp, Impl_Prag);
10622 end Inherit_Pragma_Implemented;
10624 -- Start of processing for Check_Abstract_Overriding
10626 begin
10627 Op_List := Primitive_Operations (T);
10629 -- Loop to check primitive operations
10631 Elmt := First_Elmt (Op_List);
10632 while Present (Elmt) loop
10633 Subp := Node (Elmt);
10634 Alias_Subp := Alias (Subp);
10636 -- Inherited subprograms are identified by the fact that they do not
10637 -- come from source, and the associated source location is the
10638 -- location of the first subtype of the derived type.
10640 -- Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
10641 -- subprograms that "require overriding".
10643 -- Special exception, do not complain about failure to override the
10644 -- stream routines _Input and _Output, as well as the primitive
10645 -- operations used in dispatching selects since we always provide
10646 -- automatic overridings for these subprograms.
10648 -- The partial view of T may have been a private extension, for
10649 -- which inherited functions dispatching on result are abstract.
10650 -- If the full view is a null extension, there is no need for
10651 -- overriding in Ada 2005, but wrappers need to be built for them
10652 -- (see exp_ch3, Build_Controlling_Function_Wrappers).
10654 if Is_Null_Extension (T)
10655 and then Has_Controlling_Result (Subp)
10656 and then Ada_Version >= Ada_2005
10657 and then Present (Alias_Subp)
10658 and then not Comes_From_Source (Subp)
10659 and then not Is_Abstract_Subprogram (Alias_Subp)
10660 and then not Is_Access_Type (Etype (Subp))
10661 then
10662 null;
10664 -- Ada 2005 (AI-251): Internal entities of interfaces need no
10665 -- processing because this check is done with the aliased
10666 -- entity
10668 elsif Present (Interface_Alias (Subp)) then
10669 null;
10671 elsif (Is_Abstract_Subprogram (Subp)
10672 or else Requires_Overriding (Subp)
10673 or else
10674 (Has_Controlling_Result (Subp)
10675 and then Present (Alias_Subp)
10676 and then not Comes_From_Source (Subp)
10677 and then Sloc (Subp) = Sloc (First_Subtype (T))))
10678 and then not Is_TSS (Subp, TSS_Stream_Input)
10679 and then not Is_TSS (Subp, TSS_Stream_Output)
10680 and then not Is_Abstract_Type (T)
10681 and then not Is_Predefined_Interface_Primitive (Subp)
10683 -- Ada 2005 (AI-251): Do not consider hidden entities associated
10684 -- with abstract interface types because the check will be done
10685 -- with the aliased entity (otherwise we generate a duplicated
10686 -- error message).
10688 and then not Present (Interface_Alias (Subp))
10689 then
10690 if Present (Alias_Subp) then
10692 -- Only perform the check for a derived subprogram when the
10693 -- type has an explicit record extension. This avoids incorrect
10694 -- flagging of abstract subprograms for the case of a type
10695 -- without an extension that is derived from a formal type
10696 -- with a tagged actual (can occur within a private part).
10698 -- Ada 2005 (AI-391): In the case of an inherited function with
10699 -- a controlling result of the type, the rule does not apply if
10700 -- the type is a null extension (unless the parent function
10701 -- itself is abstract, in which case the function must still be
10702 -- be overridden). The expander will generate an overriding
10703 -- wrapper function calling the parent subprogram (see
10704 -- Exp_Ch3.Make_Controlling_Wrapper_Functions).
10706 Type_Def := Type_Definition (Parent (T));
10708 if Nkind (Type_Def) = N_Derived_Type_Definition
10709 and then Present (Record_Extension_Part (Type_Def))
10710 and then
10711 (Ada_Version < Ada_2005
10712 or else not Is_Null_Extension (T)
10713 or else Ekind (Subp) = E_Procedure
10714 or else not Has_Controlling_Result (Subp)
10715 or else Is_Abstract_Subprogram (Alias_Subp)
10716 or else Requires_Overriding (Subp)
10717 or else Is_Access_Type (Etype (Subp)))
10718 then
10719 -- Avoid reporting error in case of abstract predefined
10720 -- primitive inherited from interface type because the
10721 -- body of internally generated predefined primitives
10722 -- of tagged types are generated later by Freeze_Type
10724 if Is_Interface (Root_Type (T))
10725 and then Is_Abstract_Subprogram (Subp)
10726 and then Is_Predefined_Dispatching_Operation (Subp)
10727 and then not Comes_From_Source (Ultimate_Alias (Subp))
10728 then
10729 null;
10731 -- A null extension is not obliged to override an inherited
10732 -- procedure subject to pragma Extensions_Visible with value
10733 -- False and at least one controlling OUT parameter
10734 -- (SPARK RM 6.1.7(6)).
10736 elsif Is_Null_Extension (T)
10737 and then Is_EVF_Procedure (Subp)
10738 then
10739 null;
10741 else
10742 Error_Msg_NE
10743 ("type must be declared abstract or & overridden",
10744 T, Subp);
10746 -- Traverse the whole chain of aliased subprograms to
10747 -- complete the error notification. This is especially
10748 -- useful for traceability of the chain of entities when
10749 -- the subprogram corresponds with an interface
10750 -- subprogram (which may be defined in another package).
10752 if Present (Alias_Subp) then
10753 declare
10754 E : Entity_Id;
10756 begin
10757 E := Subp;
10758 while Present (Alias (E)) loop
10760 -- Avoid reporting redundant errors on entities
10761 -- inherited from interfaces
10763 if Sloc (E) /= Sloc (T) then
10764 Error_Msg_Sloc := Sloc (E);
10765 Error_Msg_NE
10766 ("\& has been inherited #", T, Subp);
10767 end if;
10769 E := Alias (E);
10770 end loop;
10772 Error_Msg_Sloc := Sloc (E);
10774 -- AI05-0068: report if there is an overriding
10775 -- non-abstract subprogram that is invisible.
10777 if Is_Hidden (E)
10778 and then not Is_Abstract_Subprogram (E)
10779 then
10780 Error_Msg_NE
10781 ("\& subprogram# is not visible",
10782 T, Subp);
10784 -- Clarify the case where a non-null extension must
10785 -- override inherited procedure subject to pragma
10786 -- Extensions_Visible with value False and at least
10787 -- one controlling OUT param.
10789 elsif Is_EVF_Procedure (E) then
10790 Error_Msg_NE
10791 ("\& # is subject to Extensions_Visible False",
10792 T, Subp);
10794 else
10795 Error_Msg_NE
10796 ("\& has been inherited from subprogram #",
10797 T, Subp);
10798 end if;
10799 end;
10800 end if;
10801 end if;
10803 -- Ada 2005 (AI-345): Protected or task type implementing
10804 -- abstract interfaces.
10806 elsif Is_Concurrent_Record_Type (T)
10807 and then Present (Interfaces (T))
10808 then
10809 -- There is no need to check here RM 9.4(11.9/3) since we
10810 -- are processing the corresponding record type and the
10811 -- mode of the overriding subprograms was verified by
10812 -- Check_Conformance when the corresponding concurrent
10813 -- type declaration was analyzed.
10815 Error_Msg_NE
10816 ("interface subprogram & must be overridden", T, Subp);
10818 -- Examine primitive operations of synchronized type to find
10819 -- homonyms that have the wrong profile.
10821 declare
10822 Prim : Entity_Id;
10824 begin
10825 Prim := First_Entity (Corresponding_Concurrent_Type (T));
10826 while Present (Prim) loop
10827 if Chars (Prim) = Chars (Subp) then
10828 Error_Msg_NE
10829 ("profile is not type conformant with prefixed "
10830 & "view profile of inherited operation&",
10831 Prim, Subp);
10832 end if;
10834 Next_Entity (Prim);
10835 end loop;
10836 end;
10837 end if;
10839 else
10840 Error_Msg_Node_2 := T;
10841 Error_Msg_N
10842 ("abstract subprogram& not allowed for type&", Subp);
10844 -- Also post unconditional warning on the type (unconditional
10845 -- so that if there are more than one of these cases, we get
10846 -- them all, and not just the first one).
10848 Error_Msg_Node_2 := Subp;
10849 Error_Msg_N ("nonabstract type& has abstract subprogram&!", T);
10850 end if;
10852 -- A subprogram subject to pragma Extensions_Visible with value
10853 -- "True" cannot override a subprogram subject to the same pragma
10854 -- with value "False" (SPARK RM 6.1.7(5)).
10856 elsif Extensions_Visible_Status (Subp) = Extensions_Visible_True
10857 and then Present (Overridden_Operation (Subp))
10858 and then Extensions_Visible_Status (Overridden_Operation (Subp)) =
10859 Extensions_Visible_False
10860 then
10861 Error_Msg_Sloc := Sloc (Overridden_Operation (Subp));
10862 Error_Msg_N
10863 ("subprogram & with Extensions_Visible True cannot override "
10864 & "subprogram # with Extensions_Visible False", Subp);
10865 end if;
10867 -- Ada 2012 (AI05-0030): Perform checks related to pragma Implemented
10869 -- Subp is an expander-generated procedure which maps an interface
10870 -- alias to a protected wrapper. The interface alias is flagged by
10871 -- pragma Implemented. Ensure that Subp is a procedure when the
10872 -- implementation kind is By_Protected_Procedure or an entry when
10873 -- By_Entry.
10875 if Ada_Version >= Ada_2012
10876 and then Is_Hidden (Subp)
10877 and then Present (Interface_Alias (Subp))
10878 and then Has_Rep_Pragma (Interface_Alias (Subp), Name_Implemented)
10879 then
10880 Check_Pragma_Implemented (Subp);
10881 end if;
10883 -- Subp is an interface primitive which overrides another interface
10884 -- primitive marked with pragma Implemented.
10886 if Ada_Version >= Ada_2012
10887 and then Present (Overridden_Operation (Subp))
10888 and then Has_Rep_Pragma
10889 (Overridden_Operation (Subp), Name_Implemented)
10890 then
10891 -- If the overriding routine is also marked by Implemented, check
10892 -- that the two implementation kinds are conforming.
10894 if Has_Rep_Pragma (Subp, Name_Implemented) then
10895 Check_Pragma_Implemented
10896 (Subp => Subp,
10897 Iface_Subp => Overridden_Operation (Subp));
10899 -- Otherwise the overriding routine inherits the implementation
10900 -- kind from the overridden subprogram.
10902 else
10903 Inherit_Pragma_Implemented
10904 (Subp => Subp,
10905 Iface_Subp => Overridden_Operation (Subp));
10906 end if;
10907 end if;
10909 -- If the operation is a wrapper for a synchronized primitive, it
10910 -- may be called indirectly through a dispatching select. We assume
10911 -- that it will be referenced elsewhere indirectly, and suppress
10912 -- warnings about an unused entity.
10914 if Is_Primitive_Wrapper (Subp)
10915 and then Present (Wrapped_Entity (Subp))
10916 then
10917 Set_Referenced (Wrapped_Entity (Subp));
10918 end if;
10920 Next_Elmt (Elmt);
10921 end loop;
10922 end Check_Abstract_Overriding;
10924 ------------------------------------------------
10925 -- Check_Access_Discriminant_Requires_Limited --
10926 ------------------------------------------------
10928 procedure Check_Access_Discriminant_Requires_Limited
10929 (D : Node_Id;
10930 Loc : Node_Id)
10932 begin
10933 -- A discriminant_specification for an access discriminant shall appear
10934 -- only in the declaration for a task or protected type, or for a type
10935 -- with the reserved word 'limited' in its definition or in one of its
10936 -- ancestors (RM 3.7(10)).
10938 -- AI-0063: The proper condition is that type must be immutably limited,
10939 -- or else be a partial view.
10941 if Nkind (Discriminant_Type (D)) = N_Access_Definition then
10942 if Is_Limited_View (Current_Scope)
10943 or else
10944 (Nkind (Parent (Current_Scope)) = N_Private_Type_Declaration
10945 and then Limited_Present (Parent (Current_Scope)))
10946 then
10947 null;
10949 else
10950 Error_Msg_N
10951 ("access discriminants allowed only for limited types", Loc);
10952 end if;
10953 end if;
10954 end Check_Access_Discriminant_Requires_Limited;
10956 -----------------------------------
10957 -- Check_Aliased_Component_Types --
10958 -----------------------------------
10960 procedure Check_Aliased_Component_Types (T : Entity_Id) is
10961 C : Entity_Id;
10963 begin
10964 -- ??? Also need to check components of record extensions, but not
10965 -- components of protected types (which are always limited).
10967 -- Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
10968 -- types to be unconstrained. This is safe because it is illegal to
10969 -- create access subtypes to such types with explicit discriminant
10970 -- constraints.
10972 if not Is_Limited_Type (T) then
10973 if Ekind (T) = E_Record_Type then
10974 C := First_Component (T);
10975 while Present (C) loop
10976 if Is_Aliased (C)
10977 and then Has_Discriminants (Etype (C))
10978 and then not Is_Constrained (Etype (C))
10979 and then not In_Instance_Body
10980 and then Ada_Version < Ada_2005
10981 then
10982 Error_Msg_N
10983 ("aliased component must be constrained (RM 3.6(11))",
10985 end if;
10987 Next_Component (C);
10988 end loop;
10990 elsif Ekind (T) = E_Array_Type then
10991 if Has_Aliased_Components (T)
10992 and then Has_Discriminants (Component_Type (T))
10993 and then not Is_Constrained (Component_Type (T))
10994 and then not In_Instance_Body
10995 and then Ada_Version < Ada_2005
10996 then
10997 Error_Msg_N
10998 ("aliased component type must be constrained (RM 3.6(11))",
11000 end if;
11001 end if;
11002 end if;
11003 end Check_Aliased_Component_Types;
11005 ---------------------------------------
11006 -- Check_Anonymous_Access_Components --
11007 ---------------------------------------
11009 procedure Check_Anonymous_Access_Components
11010 (Typ_Decl : Node_Id;
11011 Typ : Entity_Id;
11012 Prev : Entity_Id;
11013 Comp_List : Node_Id)
11015 Loc : constant Source_Ptr := Sloc (Typ_Decl);
11016 Anon_Access : Entity_Id;
11017 Acc_Def : Node_Id;
11018 Comp : Node_Id;
11019 Comp_Def : Node_Id;
11020 Decl : Node_Id;
11021 Type_Def : Node_Id;
11023 procedure Build_Incomplete_Type_Declaration;
11024 -- If the record type contains components that include an access to the
11025 -- current record, then create an incomplete type declaration for the
11026 -- record, to be used as the designated type of the anonymous access.
11027 -- This is done only once, and only if there is no previous partial
11028 -- view of the type.
11030 function Designates_T (Subt : Node_Id) return Boolean;
11031 -- Check whether a node designates the enclosing record type, or 'Class
11032 -- of that type
11034 function Mentions_T (Acc_Def : Node_Id) return Boolean;
11035 -- Check whether an access definition includes a reference to
11036 -- the enclosing record type. The reference can be a subtype mark
11037 -- in the access definition itself, a 'Class attribute reference, or
11038 -- recursively a reference appearing in a parameter specification
11039 -- or result definition of an access_to_subprogram definition.
11041 --------------------------------------
11042 -- Build_Incomplete_Type_Declaration --
11043 --------------------------------------
11045 procedure Build_Incomplete_Type_Declaration is
11046 Decl : Node_Id;
11047 Inc_T : Entity_Id;
11048 H : Entity_Id;
11050 -- Is_Tagged indicates whether the type is tagged. It is tagged if
11051 -- it's "is new ... with record" or else "is tagged record ...".
11053 Is_Tagged : constant Boolean :=
11054 (Nkind (Type_Definition (Typ_Decl)) = N_Derived_Type_Definition
11055 and then
11056 Present (Record_Extension_Part (Type_Definition (Typ_Decl))))
11057 or else
11058 (Nkind (Type_Definition (Typ_Decl)) = N_Record_Definition
11059 and then Tagged_Present (Type_Definition (Typ_Decl)));
11061 begin
11062 -- If there is a previous partial view, no need to create a new one
11063 -- If the partial view, given by Prev, is incomplete, If Prev is
11064 -- a private declaration, full declaration is flagged accordingly.
11066 if Prev /= Typ then
11067 if Is_Tagged then
11068 Make_Class_Wide_Type (Prev);
11069 Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
11070 Set_Etype (Class_Wide_Type (Typ), Typ);
11071 end if;
11073 return;
11075 elsif Has_Private_Declaration (Typ) then
11077 -- If we refer to T'Class inside T, and T is the completion of a
11078 -- private type, then make sure the class-wide type exists.
11080 if Is_Tagged then
11081 Make_Class_Wide_Type (Typ);
11082 end if;
11084 return;
11086 -- If there was a previous anonymous access type, the incomplete
11087 -- type declaration will have been created already.
11089 elsif Present (Current_Entity (Typ))
11090 and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
11091 and then Full_View (Current_Entity (Typ)) = Typ
11092 then
11093 if Is_Tagged
11094 and then Comes_From_Source (Current_Entity (Typ))
11095 and then not Is_Tagged_Type (Current_Entity (Typ))
11096 then
11097 Make_Class_Wide_Type (Typ);
11098 Error_Msg_N
11099 ("incomplete view of tagged type should be declared tagged??",
11100 Parent (Current_Entity (Typ)));
11101 end if;
11102 return;
11104 else
11105 Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
11106 Decl := Make_Incomplete_Type_Declaration (Loc, Inc_T);
11108 -- Type has already been inserted into the current scope. Remove
11109 -- it, and add incomplete declaration for type, so that subsequent
11110 -- anonymous access types can use it. The entity is unchained from
11111 -- the homonym list and from immediate visibility. After analysis,
11112 -- the entity in the incomplete declaration becomes immediately
11113 -- visible in the record declaration that follows.
11115 H := Current_Entity (Typ);
11117 if H = Typ then
11118 Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
11119 else
11120 while Present (H)
11121 and then Homonym (H) /= Typ
11122 loop
11123 H := Homonym (Typ);
11124 end loop;
11126 Set_Homonym (H, Homonym (Typ));
11127 end if;
11129 Insert_Before (Typ_Decl, Decl);
11130 Analyze (Decl);
11131 Set_Full_View (Inc_T, Typ);
11133 if Is_Tagged then
11135 -- Create a common class-wide type for both views, and set the
11136 -- Etype of the class-wide type to the full view.
11138 Make_Class_Wide_Type (Inc_T);
11139 Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
11140 Set_Etype (Class_Wide_Type (Typ), Typ);
11141 end if;
11142 end if;
11143 end Build_Incomplete_Type_Declaration;
11145 ------------------
11146 -- Designates_T --
11147 ------------------
11149 function Designates_T (Subt : Node_Id) return Boolean is
11150 Type_Id : constant Name_Id := Chars (Typ);
11152 function Names_T (Nam : Node_Id) return Boolean;
11153 -- The record type has not been introduced in the current scope
11154 -- yet, so we must examine the name of the type itself, either
11155 -- an identifier T, or an expanded name of the form P.T, where
11156 -- P denotes the current scope.
11158 -------------
11159 -- Names_T --
11160 -------------
11162 function Names_T (Nam : Node_Id) return Boolean is
11163 begin
11164 if Nkind (Nam) = N_Identifier then
11165 return Chars (Nam) = Type_Id;
11167 elsif Nkind (Nam) = N_Selected_Component then
11168 if Chars (Selector_Name (Nam)) = Type_Id then
11169 if Nkind (Prefix (Nam)) = N_Identifier then
11170 return Chars (Prefix (Nam)) = Chars (Current_Scope);
11172 elsif Nkind (Prefix (Nam)) = N_Selected_Component then
11173 return Chars (Selector_Name (Prefix (Nam))) =
11174 Chars (Current_Scope);
11175 else
11176 return False;
11177 end if;
11179 else
11180 return False;
11181 end if;
11183 else
11184 return False;
11185 end if;
11186 end Names_T;
11188 -- Start of processing for Designates_T
11190 begin
11191 if Nkind (Subt) = N_Identifier then
11192 return Chars (Subt) = Type_Id;
11194 -- Reference can be through an expanded name which has not been
11195 -- analyzed yet, and which designates enclosing scopes.
11197 elsif Nkind (Subt) = N_Selected_Component then
11198 if Names_T (Subt) then
11199 return True;
11201 -- Otherwise it must denote an entity that is already visible.
11202 -- The access definition may name a subtype of the enclosing
11203 -- type, if there is a previous incomplete declaration for it.
11205 else
11206 Find_Selected_Component (Subt);
11207 return
11208 Is_Entity_Name (Subt)
11209 and then Scope (Entity (Subt)) = Current_Scope
11210 and then
11211 (Chars (Base_Type (Entity (Subt))) = Type_Id
11212 or else
11213 (Is_Class_Wide_Type (Entity (Subt))
11214 and then
11215 Chars (Etype (Base_Type (Entity (Subt)))) =
11216 Type_Id));
11217 end if;
11219 -- A reference to the current type may appear as the prefix of
11220 -- a 'Class attribute.
11222 elsif Nkind (Subt) = N_Attribute_Reference
11223 and then Attribute_Name (Subt) = Name_Class
11224 then
11225 return Names_T (Prefix (Subt));
11227 else
11228 return False;
11229 end if;
11230 end Designates_T;
11232 ----------------
11233 -- Mentions_T --
11234 ----------------
11236 function Mentions_T (Acc_Def : Node_Id) return Boolean is
11237 Param_Spec : Node_Id;
11239 Acc_Subprg : constant Node_Id :=
11240 Access_To_Subprogram_Definition (Acc_Def);
11242 begin
11243 if No (Acc_Subprg) then
11244 return Designates_T (Subtype_Mark (Acc_Def));
11245 end if;
11247 -- Component is an access_to_subprogram: examine its formals,
11248 -- and result definition in the case of an access_to_function.
11250 Param_Spec := First (Parameter_Specifications (Acc_Subprg));
11251 while Present (Param_Spec) loop
11252 if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
11253 and then Mentions_T (Parameter_Type (Param_Spec))
11254 then
11255 return True;
11257 elsif Designates_T (Parameter_Type (Param_Spec)) then
11258 return True;
11259 end if;
11261 Next (Param_Spec);
11262 end loop;
11264 if Nkind (Acc_Subprg) = N_Access_Function_Definition then
11265 if Nkind (Result_Definition (Acc_Subprg)) =
11266 N_Access_Definition
11267 then
11268 return Mentions_T (Result_Definition (Acc_Subprg));
11269 else
11270 return Designates_T (Result_Definition (Acc_Subprg));
11271 end if;
11272 end if;
11274 return False;
11275 end Mentions_T;
11277 -- Start of processing for Check_Anonymous_Access_Components
11279 begin
11280 if No (Comp_List) then
11281 return;
11282 end if;
11284 Comp := First (Component_Items (Comp_List));
11285 while Present (Comp) loop
11286 if Nkind (Comp) = N_Component_Declaration
11287 and then Present
11288 (Access_Definition (Component_Definition (Comp)))
11289 and then
11290 Mentions_T (Access_Definition (Component_Definition (Comp)))
11291 then
11292 Comp_Def := Component_Definition (Comp);
11293 Acc_Def :=
11294 Access_To_Subprogram_Definition (Access_Definition (Comp_Def));
11296 Build_Incomplete_Type_Declaration;
11297 Anon_Access := Make_Temporary (Loc, 'S');
11299 -- Create a declaration for the anonymous access type: either
11300 -- an access_to_object or an access_to_subprogram.
11302 if Present (Acc_Def) then
11303 if Nkind (Acc_Def) = N_Access_Function_Definition then
11304 Type_Def :=
11305 Make_Access_Function_Definition (Loc,
11306 Parameter_Specifications =>
11307 Parameter_Specifications (Acc_Def),
11308 Result_Definition => Result_Definition (Acc_Def));
11309 else
11310 Type_Def :=
11311 Make_Access_Procedure_Definition (Loc,
11312 Parameter_Specifications =>
11313 Parameter_Specifications (Acc_Def));
11314 end if;
11316 else
11317 Type_Def :=
11318 Make_Access_To_Object_Definition (Loc,
11319 Subtype_Indication =>
11320 Relocate_Node
11321 (Subtype_Mark (Access_Definition (Comp_Def))));
11323 Set_Constant_Present
11324 (Type_Def, Constant_Present (Access_Definition (Comp_Def)));
11325 Set_All_Present
11326 (Type_Def, All_Present (Access_Definition (Comp_Def)));
11327 end if;
11329 Set_Null_Exclusion_Present
11330 (Type_Def,
11331 Null_Exclusion_Present (Access_Definition (Comp_Def)));
11333 Decl :=
11334 Make_Full_Type_Declaration (Loc,
11335 Defining_Identifier => Anon_Access,
11336 Type_Definition => Type_Def);
11338 Insert_Before (Typ_Decl, Decl);
11339 Analyze (Decl);
11341 -- If an access to subprogram, create the extra formals
11343 if Present (Acc_Def) then
11344 Create_Extra_Formals (Designated_Type (Anon_Access));
11346 -- If an access to object, preserve entity of designated type,
11347 -- for ASIS use, before rewriting the component definition.
11349 else
11350 declare
11351 Desig : Entity_Id;
11353 begin
11354 Desig := Entity (Subtype_Indication (Type_Def));
11356 -- If the access definition is to the current record,
11357 -- the visible entity at this point is an incomplete
11358 -- type. Retrieve the full view to simplify ASIS queries
11360 if Ekind (Desig) = E_Incomplete_Type then
11361 Desig := Full_View (Desig);
11362 end if;
11364 Set_Entity
11365 (Subtype_Mark (Access_Definition (Comp_Def)), Desig);
11366 end;
11367 end if;
11369 Rewrite (Comp_Def,
11370 Make_Component_Definition (Loc,
11371 Subtype_Indication =>
11372 New_Occurrence_Of (Anon_Access, Loc)));
11374 if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
11375 Set_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
11376 else
11377 Set_Ekind (Anon_Access, E_Anonymous_Access_Type);
11378 end if;
11380 Set_Is_Local_Anonymous_Access (Anon_Access);
11381 end if;
11383 Next (Comp);
11384 end loop;
11386 if Present (Variant_Part (Comp_List)) then
11387 declare
11388 V : Node_Id;
11389 begin
11390 V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
11391 while Present (V) loop
11392 Check_Anonymous_Access_Components
11393 (Typ_Decl, Typ, Prev, Component_List (V));
11394 Next_Non_Pragma (V);
11395 end loop;
11396 end;
11397 end if;
11398 end Check_Anonymous_Access_Components;
11400 ----------------------
11401 -- Check_Completion --
11402 ----------------------
11404 procedure Check_Completion (Body_Id : Node_Id := Empty) is
11405 E : Entity_Id;
11407 procedure Post_Error;
11408 -- Post error message for lack of completion for entity E
11410 ----------------
11411 -- Post_Error --
11412 ----------------
11414 procedure Post_Error is
11415 procedure Missing_Body;
11416 -- Output missing body message
11418 ------------------
11419 -- Missing_Body --
11420 ------------------
11422 procedure Missing_Body is
11423 begin
11424 -- Spec is in same unit, so we can post on spec
11426 if In_Same_Source_Unit (Body_Id, E) then
11427 Error_Msg_N ("missing body for &", E);
11429 -- Spec is in a separate unit, so we have to post on the body
11431 else
11432 Error_Msg_NE ("missing body for & declared#!", Body_Id, E);
11433 end if;
11434 end Missing_Body;
11436 -- Start of processing for Post_Error
11438 begin
11439 if not Comes_From_Source (E) then
11440 if Ekind_In (E, E_Task_Type, E_Protected_Type) then
11442 -- It may be an anonymous protected type created for a
11443 -- single variable. Post error on variable, if present.
11445 declare
11446 Var : Entity_Id;
11448 begin
11449 Var := First_Entity (Current_Scope);
11450 while Present (Var) loop
11451 exit when Etype (Var) = E
11452 and then Comes_From_Source (Var);
11454 Next_Entity (Var);
11455 end loop;
11457 if Present (Var) then
11458 E := Var;
11459 end if;
11460 end;
11461 end if;
11462 end if;
11464 -- If a generated entity has no completion, then either previous
11465 -- semantic errors have disabled the expansion phase, or else we had
11466 -- missing subunits, or else we are compiling without expansion,
11467 -- or else something is very wrong.
11469 if not Comes_From_Source (E) then
11470 pragma Assert
11471 (Serious_Errors_Detected > 0
11472 or else Configurable_Run_Time_Violations > 0
11473 or else Subunits_Missing
11474 or else not Expander_Active);
11475 return;
11477 -- Here for source entity
11479 else
11480 -- Here if no body to post the error message, so we post the error
11481 -- on the declaration that has no completion. This is not really
11482 -- the right place to post it, think about this later ???
11484 if No (Body_Id) then
11485 if Is_Type (E) then
11486 Error_Msg_NE
11487 ("missing full declaration for }", Parent (E), E);
11488 else
11489 Error_Msg_NE ("missing body for &", Parent (E), E);
11490 end if;
11492 -- Package body has no completion for a declaration that appears
11493 -- in the corresponding spec. Post error on the body, with a
11494 -- reference to the non-completed declaration.
11496 else
11497 Error_Msg_Sloc := Sloc (E);
11499 if Is_Type (E) then
11500 Error_Msg_NE ("missing full declaration for }!", Body_Id, E);
11502 elsif Is_Overloadable (E)
11503 and then Current_Entity_In_Scope (E) /= E
11504 then
11505 -- It may be that the completion is mistyped and appears as
11506 -- a distinct overloading of the entity.
11508 declare
11509 Candidate : constant Entity_Id :=
11510 Current_Entity_In_Scope (E);
11511 Decl : constant Node_Id :=
11512 Unit_Declaration_Node (Candidate);
11514 begin
11515 if Is_Overloadable (Candidate)
11516 and then Ekind (Candidate) = Ekind (E)
11517 and then Nkind (Decl) = N_Subprogram_Body
11518 and then Acts_As_Spec (Decl)
11519 then
11520 Check_Type_Conformant (Candidate, E);
11522 else
11523 Missing_Body;
11524 end if;
11525 end;
11527 else
11528 Missing_Body;
11529 end if;
11530 end if;
11531 end if;
11532 end Post_Error;
11534 -- Local variables
11536 Pack_Id : constant Entity_Id := Current_Scope;
11538 -- Start of processing for Check_Completion
11540 begin
11541 E := First_Entity (Pack_Id);
11542 while Present (E) loop
11543 if Is_Intrinsic_Subprogram (E) then
11544 null;
11546 -- The following situation requires special handling: a child unit
11547 -- that appears in the context clause of the body of its parent:
11549 -- procedure Parent.Child (...);
11551 -- with Parent.Child;
11552 -- package body Parent is
11554 -- Here Parent.Child appears as a local entity, but should not be
11555 -- flagged as requiring completion, because it is a compilation
11556 -- unit.
11558 -- Ignore missing completion for a subprogram that does not come from
11559 -- source (including the _Call primitive operation of RAS types,
11560 -- which has to have the flag Comes_From_Source for other purposes):
11561 -- we assume that the expander will provide the missing completion.
11562 -- In case of previous errors, other expansion actions that provide
11563 -- bodies for null procedures with not be invoked, so inhibit message
11564 -- in those cases.
11566 -- Note that E_Operator is not in the list that follows, because
11567 -- this kind is reserved for predefined operators, that are
11568 -- intrinsic and do not need completion.
11570 elsif Ekind_In (E, E_Function,
11571 E_Procedure,
11572 E_Generic_Function,
11573 E_Generic_Procedure)
11574 then
11575 if Has_Completion (E) then
11576 null;
11578 elsif Is_Subprogram (E) and then Is_Abstract_Subprogram (E) then
11579 null;
11581 elsif Is_Subprogram (E)
11582 and then (not Comes_From_Source (E)
11583 or else Chars (E) = Name_uCall)
11584 then
11585 null;
11587 elsif
11588 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
11589 then
11590 null;
11592 elsif Nkind (Parent (E)) = N_Procedure_Specification
11593 and then Null_Present (Parent (E))
11594 and then Serious_Errors_Detected > 0
11595 then
11596 null;
11598 else
11599 Post_Error;
11600 end if;
11602 elsif Is_Entry (E) then
11603 if not Has_Completion (E) and then
11604 (Ekind (Scope (E)) = E_Protected_Object
11605 or else Ekind (Scope (E)) = E_Protected_Type)
11606 then
11607 Post_Error;
11608 end if;
11610 elsif Is_Package_Or_Generic_Package (E) then
11611 if Unit_Requires_Body (E) then
11612 if not Has_Completion (E)
11613 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
11614 N_Compilation_Unit
11615 then
11616 Post_Error;
11617 end if;
11619 elsif not Is_Child_Unit (E) then
11620 May_Need_Implicit_Body (E);
11621 end if;
11623 -- A formal incomplete type (Ada 2012) does not require a completion;
11624 -- other incomplete type declarations do.
11626 elsif Ekind (E) = E_Incomplete_Type
11627 and then No (Underlying_Type (E))
11628 and then not Is_Generic_Type (E)
11629 then
11630 Post_Error;
11632 elsif Ekind_In (E, E_Task_Type, E_Protected_Type)
11633 and then not Has_Completion (E)
11634 then
11635 Post_Error;
11637 -- A single task declared in the current scope is a constant, verify
11638 -- that the body of its anonymous type is in the same scope. If the
11639 -- task is defined elsewhere, this may be a renaming declaration for
11640 -- which no completion is needed.
11642 elsif Ekind (E) = E_Constant
11643 and then Ekind (Etype (E)) = E_Task_Type
11644 and then not Has_Completion (Etype (E))
11645 and then Scope (Etype (E)) = Current_Scope
11646 then
11647 Post_Error;
11649 elsif Ekind (E) = E_Protected_Object
11650 and then not Has_Completion (Etype (E))
11651 then
11652 Post_Error;
11654 elsif Ekind (E) = E_Record_Type then
11655 if Is_Tagged_Type (E) then
11656 Check_Abstract_Overriding (E);
11657 Check_Conventions (E);
11658 end if;
11660 Check_Aliased_Component_Types (E);
11662 elsif Ekind (E) = E_Array_Type then
11663 Check_Aliased_Component_Types (E);
11665 end if;
11667 Next_Entity (E);
11668 end loop;
11669 end Check_Completion;
11671 ------------------------------------
11672 -- Check_CPP_Type_Has_No_Defaults --
11673 ------------------------------------
11675 procedure Check_CPP_Type_Has_No_Defaults (T : Entity_Id) is
11676 Tdef : constant Node_Id := Type_Definition (Declaration_Node (T));
11677 Clist : Node_Id;
11678 Comp : Node_Id;
11680 begin
11681 -- Obtain the component list
11683 if Nkind (Tdef) = N_Record_Definition then
11684 Clist := Component_List (Tdef);
11685 else pragma Assert (Nkind (Tdef) = N_Derived_Type_Definition);
11686 Clist := Component_List (Record_Extension_Part (Tdef));
11687 end if;
11689 -- Check all components to ensure no default expressions
11691 if Present (Clist) then
11692 Comp := First (Component_Items (Clist));
11693 while Present (Comp) loop
11694 if Present (Expression (Comp)) then
11695 Error_Msg_N
11696 ("component of imported 'C'P'P type cannot have "
11697 & "default expression", Expression (Comp));
11698 end if;
11700 Next (Comp);
11701 end loop;
11702 end if;
11703 end Check_CPP_Type_Has_No_Defaults;
11705 ----------------------------
11706 -- Check_Delta_Expression --
11707 ----------------------------
11709 procedure Check_Delta_Expression (E : Node_Id) is
11710 begin
11711 if not (Is_Real_Type (Etype (E))) then
11712 Wrong_Type (E, Any_Real);
11714 elsif not Is_OK_Static_Expression (E) then
11715 Flag_Non_Static_Expr
11716 ("non-static expression used for delta value!", E);
11718 elsif not UR_Is_Positive (Expr_Value_R (E)) then
11719 Error_Msg_N ("delta expression must be positive", E);
11721 else
11722 return;
11723 end if;
11725 -- If any of above errors occurred, then replace the incorrect
11726 -- expression by the real 0.1, which should prevent further errors.
11728 Rewrite (E,
11729 Make_Real_Literal (Sloc (E), Ureal_Tenth));
11730 Analyze_And_Resolve (E, Standard_Float);
11731 end Check_Delta_Expression;
11733 -----------------------------
11734 -- Check_Digits_Expression --
11735 -----------------------------
11737 procedure Check_Digits_Expression (E : Node_Id) is
11738 begin
11739 if not (Is_Integer_Type (Etype (E))) then
11740 Wrong_Type (E, Any_Integer);
11742 elsif not Is_OK_Static_Expression (E) then
11743 Flag_Non_Static_Expr
11744 ("non-static expression used for digits value!", E);
11746 elsif Expr_Value (E) <= 0 then
11747 Error_Msg_N ("digits value must be greater than zero", E);
11749 else
11750 return;
11751 end if;
11753 -- If any of above errors occurred, then replace the incorrect
11754 -- expression by the integer 1, which should prevent further errors.
11756 Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
11757 Analyze_And_Resolve (E, Standard_Integer);
11759 end Check_Digits_Expression;
11761 --------------------------
11762 -- Check_Initialization --
11763 --------------------------
11765 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
11766 begin
11767 -- Special processing for limited types
11769 if Is_Limited_Type (T)
11770 and then not In_Instance
11771 and then not In_Inlined_Body
11772 then
11773 if not OK_For_Limited_Init (T, Exp) then
11775 -- In GNAT mode, this is just a warning, to allow it to be evilly
11776 -- turned off. Otherwise it is a real error.
11778 if GNAT_Mode then
11779 Error_Msg_N
11780 ("??cannot initialize entities of limited type!", Exp);
11782 elsif Ada_Version < Ada_2005 then
11784 -- The side effect removal machinery may generate illegal Ada
11785 -- code to avoid the usage of access types and 'reference in
11786 -- SPARK mode. Since this is legal code with respect to theorem
11787 -- proving, do not emit the error.
11789 if GNATprove_Mode
11790 and then Nkind (Exp) = N_Function_Call
11791 and then Nkind (Parent (Exp)) = N_Object_Declaration
11792 and then not Comes_From_Source
11793 (Defining_Identifier (Parent (Exp)))
11794 then
11795 null;
11797 else
11798 Error_Msg_N
11799 ("cannot initialize entities of limited type", Exp);
11800 Explain_Limited_Type (T, Exp);
11801 end if;
11803 else
11804 -- Specialize error message according to kind of illegal
11805 -- initial expression.
11807 if Nkind (Exp) = N_Type_Conversion
11808 and then Nkind (Expression (Exp)) = N_Function_Call
11809 then
11810 -- No error for internally-generated object declarations,
11811 -- which can come from build-in-place assignment statements.
11813 if Nkind (Parent (Exp)) = N_Object_Declaration
11814 and then not Comes_From_Source
11815 (Defining_Identifier (Parent (Exp)))
11816 then
11817 null;
11819 else
11820 Error_Msg_N
11821 ("illegal context for call to function with limited "
11822 & "result", Exp);
11823 end if;
11825 else
11826 Error_Msg_N
11827 ("initialization of limited object requires aggregate or "
11828 & "function call", Exp);
11829 end if;
11830 end if;
11831 end if;
11832 end if;
11834 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag gets
11835 -- set unless we can be sure that no range check is required.
11837 if (GNATprove_Mode or not Expander_Active)
11838 and then Is_Scalar_Type (T)
11839 and then not Is_In_Range (Exp, T, Assume_Valid => True)
11840 then
11841 Set_Do_Range_Check (Exp);
11842 end if;
11843 end Check_Initialization;
11845 ----------------------
11846 -- Check_Interfaces --
11847 ----------------------
11849 procedure Check_Interfaces (N : Node_Id; Def : Node_Id) is
11850 Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
11852 Iface : Node_Id;
11853 Iface_Def : Node_Id;
11854 Iface_Typ : Entity_Id;
11855 Parent_Node : Node_Id;
11857 Is_Task : Boolean := False;
11858 -- Set True if parent type or any progenitor is a task interface
11860 Is_Protected : Boolean := False;
11861 -- Set True if parent type or any progenitor is a protected interface
11863 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
11864 -- Check that a progenitor is compatible with declaration. If an error
11865 -- message is output, it is posted on Error_Node.
11867 ------------------
11868 -- Check_Ifaces --
11869 ------------------
11871 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
11872 Iface_Id : constant Entity_Id :=
11873 Defining_Identifier (Parent (Iface_Def));
11874 Type_Def : Node_Id;
11876 begin
11877 if Nkind (N) = N_Private_Extension_Declaration then
11878 Type_Def := N;
11879 else
11880 Type_Def := Type_Definition (N);
11881 end if;
11883 if Is_Task_Interface (Iface_Id) then
11884 Is_Task := True;
11886 elsif Is_Protected_Interface (Iface_Id) then
11887 Is_Protected := True;
11888 end if;
11890 if Is_Synchronized_Interface (Iface_Id) then
11892 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
11893 -- extension derived from a synchronized interface must explicitly
11894 -- be declared synchronized, because the full view will be a
11895 -- synchronized type.
11897 if Nkind (N) = N_Private_Extension_Declaration then
11898 if not Synchronized_Present (N) then
11899 Error_Msg_NE
11900 ("private extension of& must be explicitly synchronized",
11901 N, Iface_Id);
11902 end if;
11904 -- However, by 3.9.4(16/2), a full type that is a record extension
11905 -- is never allowed to derive from a synchronized interface (note
11906 -- that interfaces must be excluded from this check, because those
11907 -- are represented by derived type definitions in some cases).
11909 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition
11910 and then not Interface_Present (Type_Definition (N))
11911 then
11912 Error_Msg_N ("record extension cannot derive from synchronized "
11913 & "interface", Error_Node);
11914 end if;
11915 end if;
11917 -- Check that the characteristics of the progenitor are compatible
11918 -- with the explicit qualifier in the declaration.
11919 -- The check only applies to qualifiers that come from source.
11920 -- Limited_Present also appears in the declaration of corresponding
11921 -- records, and the check does not apply to them.
11923 if Limited_Present (Type_Def)
11924 and then not
11925 Is_Concurrent_Record_Type (Defining_Identifier (N))
11926 then
11927 if Is_Limited_Interface (Parent_Type)
11928 and then not Is_Limited_Interface (Iface_Id)
11929 then
11930 Error_Msg_NE
11931 ("progenitor & must be limited interface",
11932 Error_Node, Iface_Id);
11934 elsif
11935 (Task_Present (Iface_Def)
11936 or else Protected_Present (Iface_Def)
11937 or else Synchronized_Present (Iface_Def))
11938 and then Nkind (N) /= N_Private_Extension_Declaration
11939 and then not Error_Posted (N)
11940 then
11941 Error_Msg_NE
11942 ("progenitor & must be limited interface",
11943 Error_Node, Iface_Id);
11944 end if;
11946 -- Protected interfaces can only inherit from limited, synchronized
11947 -- or protected interfaces.
11949 elsif Nkind (N) = N_Full_Type_Declaration
11950 and then Protected_Present (Type_Def)
11951 then
11952 if Limited_Present (Iface_Def)
11953 or else Synchronized_Present (Iface_Def)
11954 or else Protected_Present (Iface_Def)
11955 then
11956 null;
11958 elsif Task_Present (Iface_Def) then
11959 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
11960 & "from task interface", Error_Node);
11962 else
11963 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
11964 & "from non-limited interface", Error_Node);
11965 end if;
11967 -- Ada 2005 (AI-345): Synchronized interfaces can only inherit from
11968 -- limited and synchronized.
11970 elsif Synchronized_Present (Type_Def) then
11971 if Limited_Present (Iface_Def)
11972 or else Synchronized_Present (Iface_Def)
11973 then
11974 null;
11976 elsif Protected_Present (Iface_Def)
11977 and then Nkind (N) /= N_Private_Extension_Declaration
11978 then
11979 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11980 & "from protected interface", Error_Node);
11982 elsif Task_Present (Iface_Def)
11983 and then Nkind (N) /= N_Private_Extension_Declaration
11984 then
11985 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11986 & "from task interface", Error_Node);
11988 elsif not Is_Limited_Interface (Iface_Id) then
11989 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11990 & "from non-limited interface", Error_Node);
11991 end if;
11993 -- Ada 2005 (AI-345): Task interfaces can only inherit from limited,
11994 -- synchronized or task interfaces.
11996 elsif Nkind (N) = N_Full_Type_Declaration
11997 and then Task_Present (Type_Def)
11998 then
11999 if Limited_Present (Iface_Def)
12000 or else Synchronized_Present (Iface_Def)
12001 or else Task_Present (Iface_Def)
12002 then
12003 null;
12005 elsif Protected_Present (Iface_Def) then
12006 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
12007 & "protected interface", Error_Node);
12009 else
12010 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
12011 & "non-limited interface", Error_Node);
12012 end if;
12013 end if;
12014 end Check_Ifaces;
12016 -- Start of processing for Check_Interfaces
12018 begin
12019 if Is_Interface (Parent_Type) then
12020 if Is_Task_Interface (Parent_Type) then
12021 Is_Task := True;
12023 elsif Is_Protected_Interface (Parent_Type) then
12024 Is_Protected := True;
12025 end if;
12026 end if;
12028 if Nkind (N) = N_Private_Extension_Declaration then
12030 -- Check that progenitors are compatible with declaration
12032 Iface := First (Interface_List (Def));
12033 while Present (Iface) loop
12034 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
12036 Parent_Node := Parent (Base_Type (Iface_Typ));
12037 Iface_Def := Type_Definition (Parent_Node);
12039 if not Is_Interface (Iface_Typ) then
12040 Diagnose_Interface (Iface, Iface_Typ);
12041 else
12042 Check_Ifaces (Iface_Def, Iface);
12043 end if;
12045 Next (Iface);
12046 end loop;
12048 if Is_Task and Is_Protected then
12049 Error_Msg_N
12050 ("type cannot derive from task and protected interface", N);
12051 end if;
12053 return;
12054 end if;
12056 -- Full type declaration of derived type.
12057 -- Check compatibility with parent if it is interface type
12059 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
12060 and then Is_Interface (Parent_Type)
12061 then
12062 Parent_Node := Parent (Parent_Type);
12064 -- More detailed checks for interface varieties
12066 Check_Ifaces
12067 (Iface_Def => Type_Definition (Parent_Node),
12068 Error_Node => Subtype_Indication (Type_Definition (N)));
12069 end if;
12071 Iface := First (Interface_List (Def));
12072 while Present (Iface) loop
12073 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
12075 Parent_Node := Parent (Base_Type (Iface_Typ));
12076 Iface_Def := Type_Definition (Parent_Node);
12078 if not Is_Interface (Iface_Typ) then
12079 Diagnose_Interface (Iface, Iface_Typ);
12081 else
12082 -- "The declaration of a specific descendant of an interface
12083 -- type freezes the interface type" RM 13.14
12085 Freeze_Before (N, Iface_Typ);
12086 Check_Ifaces (Iface_Def, Error_Node => Iface);
12087 end if;
12089 Next (Iface);
12090 end loop;
12092 if Is_Task and Is_Protected then
12093 Error_Msg_N
12094 ("type cannot derive from task and protected interface", N);
12095 end if;
12096 end Check_Interfaces;
12098 ------------------------------------
12099 -- Check_Or_Process_Discriminants --
12100 ------------------------------------
12102 -- If an incomplete or private type declaration was already given for the
12103 -- type, the discriminants may have already been processed if they were
12104 -- present on the incomplete declaration. In this case a full conformance
12105 -- check has been performed in Find_Type_Name, and we then recheck here
12106 -- some properties that can't be checked on the partial view alone.
12107 -- Otherwise we call Process_Discriminants.
12109 procedure Check_Or_Process_Discriminants
12110 (N : Node_Id;
12111 T : Entity_Id;
12112 Prev : Entity_Id := Empty)
12114 begin
12115 if Has_Discriminants (T) then
12117 -- Discriminants are already set on T if they were already present
12118 -- on the partial view. Make them visible to component declarations.
12120 declare
12121 D : Entity_Id;
12122 -- Discriminant on T (full view) referencing expr on partial view
12124 Prev_D : Entity_Id;
12125 -- Entity of corresponding discriminant on partial view
12127 New_D : Node_Id;
12128 -- Discriminant specification for full view, expression is
12129 -- the syntactic copy on full view (which has been checked for
12130 -- conformance with partial view), only used here to post error
12131 -- message.
12133 begin
12134 D := First_Discriminant (T);
12135 New_D := First (Discriminant_Specifications (N));
12136 while Present (D) loop
12137 Prev_D := Current_Entity (D);
12138 Set_Current_Entity (D);
12139 Set_Is_Immediately_Visible (D);
12140 Set_Homonym (D, Prev_D);
12142 -- Handle the case where there is an untagged partial view and
12143 -- the full view is tagged: must disallow discriminants with
12144 -- defaults, unless compiling for Ada 2012, which allows a
12145 -- limited tagged type to have defaulted discriminants (see
12146 -- AI05-0214). However, suppress error here if it was already
12147 -- reported on the default expression of the partial view.
12149 if Is_Tagged_Type (T)
12150 and then Present (Expression (Parent (D)))
12151 and then (not Is_Limited_Type (Current_Scope)
12152 or else Ada_Version < Ada_2012)
12153 and then not Error_Posted (Expression (Parent (D)))
12154 then
12155 if Ada_Version >= Ada_2012 then
12156 Error_Msg_N
12157 ("discriminants of nonlimited tagged type cannot have "
12158 & "defaults",
12159 Expression (New_D));
12160 else
12161 Error_Msg_N
12162 ("discriminants of tagged type cannot have defaults",
12163 Expression (New_D));
12164 end if;
12165 end if;
12167 -- Ada 2005 (AI-230): Access discriminant allowed in
12168 -- non-limited record types.
12170 if Ada_Version < Ada_2005 then
12172 -- This restriction gets applied to the full type here. It
12173 -- has already been applied earlier to the partial view.
12175 Check_Access_Discriminant_Requires_Limited (Parent (D), N);
12176 end if;
12178 Next_Discriminant (D);
12179 Next (New_D);
12180 end loop;
12181 end;
12183 elsif Present (Discriminant_Specifications (N)) then
12184 Process_Discriminants (N, Prev);
12185 end if;
12186 end Check_Or_Process_Discriminants;
12188 ----------------------
12189 -- Check_Real_Bound --
12190 ----------------------
12192 procedure Check_Real_Bound (Bound : Node_Id) is
12193 begin
12194 if not Is_Real_Type (Etype (Bound)) then
12195 Error_Msg_N
12196 ("bound in real type definition must be of real type", Bound);
12198 elsif not Is_OK_Static_Expression (Bound) then
12199 Flag_Non_Static_Expr
12200 ("non-static expression used for real type bound!", Bound);
12202 else
12203 return;
12204 end if;
12206 Rewrite
12207 (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
12208 Analyze (Bound);
12209 Resolve (Bound, Standard_Float);
12210 end Check_Real_Bound;
12212 ------------------------------
12213 -- Complete_Private_Subtype --
12214 ------------------------------
12216 procedure Complete_Private_Subtype
12217 (Priv : Entity_Id;
12218 Full : Entity_Id;
12219 Full_Base : Entity_Id;
12220 Related_Nod : Node_Id)
12222 Save_Next_Entity : Entity_Id;
12223 Save_Homonym : Entity_Id;
12225 begin
12226 -- Set semantic attributes for (implicit) private subtype completion.
12227 -- If the full type has no discriminants, then it is a copy of the
12228 -- full view of the base. Otherwise, it is a subtype of the base with
12229 -- a possible discriminant constraint. Save and restore the original
12230 -- Next_Entity field of full to ensure that the calls to Copy_Node do
12231 -- not corrupt the entity chain.
12233 -- Note that the type of the full view is the same entity as the type
12234 -- of the partial view. In this fashion, the subtype has access to the
12235 -- correct view of the parent.
12237 Save_Next_Entity := Next_Entity (Full);
12238 Save_Homonym := Homonym (Priv);
12240 case Ekind (Full_Base) is
12241 when Class_Wide_Kind
12242 | Private_Kind
12243 | Protected_Kind
12244 | Task_Kind
12245 | E_Record_Subtype
12246 | E_Record_Type
12248 Copy_Node (Priv, Full);
12250 Set_Has_Discriminants
12251 (Full, Has_Discriminants (Full_Base));
12252 Set_Has_Unknown_Discriminants
12253 (Full, Has_Unknown_Discriminants (Full_Base));
12254 Set_First_Entity (Full, First_Entity (Full_Base));
12255 Set_Last_Entity (Full, Last_Entity (Full_Base));
12257 -- If the underlying base type is constrained, we know that the
12258 -- full view of the subtype is constrained as well (the converse
12259 -- is not necessarily true).
12261 if Is_Constrained (Full_Base) then
12262 Set_Is_Constrained (Full);
12263 end if;
12265 when others =>
12266 Copy_Node (Full_Base, Full);
12268 Set_Chars (Full, Chars (Priv));
12269 Conditional_Delay (Full, Priv);
12270 Set_Sloc (Full, Sloc (Priv));
12271 end case;
12273 Set_Next_Entity (Full, Save_Next_Entity);
12274 Set_Homonym (Full, Save_Homonym);
12275 Set_Associated_Node_For_Itype (Full, Related_Nod);
12277 -- Set common attributes for all subtypes: kind, convention, etc.
12279 Set_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
12280 Set_Convention (Full, Convention (Full_Base));
12282 -- The Etype of the full view is inconsistent. Gigi needs to see the
12283 -- structural full view, which is what the current scheme gives: the
12284 -- Etype of the full view is the etype of the full base. However, if the
12285 -- full base is a derived type, the full view then looks like a subtype
12286 -- of the parent, not a subtype of the full base. If instead we write:
12288 -- Set_Etype (Full, Full_Base);
12290 -- then we get inconsistencies in the front-end (confusion between
12291 -- views). Several outstanding bugs are related to this ???
12293 Set_Is_First_Subtype (Full, False);
12294 Set_Scope (Full, Scope (Priv));
12295 Set_Size_Info (Full, Full_Base);
12296 Set_RM_Size (Full, RM_Size (Full_Base));
12297 Set_Is_Itype (Full);
12299 -- A subtype of a private-type-without-discriminants, whose full-view
12300 -- has discriminants with default expressions, is not constrained.
12302 if not Has_Discriminants (Priv) then
12303 Set_Is_Constrained (Full, Is_Constrained (Full_Base));
12305 if Has_Discriminants (Full_Base) then
12306 Set_Discriminant_Constraint
12307 (Full, Discriminant_Constraint (Full_Base));
12309 -- The partial view may have been indefinite, the full view
12310 -- might not be.
12312 Set_Has_Unknown_Discriminants
12313 (Full, Has_Unknown_Discriminants (Full_Base));
12314 end if;
12315 end if;
12317 Set_First_Rep_Item (Full, First_Rep_Item (Full_Base));
12318 Set_Depends_On_Private (Full, Has_Private_Component (Full));
12320 -- Freeze the private subtype entity if its parent is delayed, and not
12321 -- already frozen. We skip this processing if the type is an anonymous
12322 -- subtype of a record component, or is the corresponding record of a
12323 -- protected type, since these are processed when the enclosing type
12324 -- is frozen. If the parent type is declared in a nested package then
12325 -- the freezing of the private and full views also happens later.
12327 if not Is_Type (Scope (Full)) then
12328 if Is_Itype (Priv)
12329 and then In_Same_Source_Unit (Full, Full_Base)
12330 and then Scope (Full_Base) /= Scope (Full)
12331 then
12332 Set_Has_Delayed_Freeze (Full);
12333 Set_Has_Delayed_Freeze (Priv);
12335 else
12336 Set_Has_Delayed_Freeze (Full,
12337 Has_Delayed_Freeze (Full_Base)
12338 and then not Is_Frozen (Full_Base));
12339 end if;
12340 end if;
12342 Set_Freeze_Node (Full, Empty);
12343 Set_Is_Frozen (Full, False);
12344 Set_Full_View (Priv, Full);
12346 if Has_Discriminants (Full) then
12347 Set_Stored_Constraint_From_Discriminant_Constraint (Full);
12348 Set_Stored_Constraint (Priv, Stored_Constraint (Full));
12350 if Has_Unknown_Discriminants (Full) then
12351 Set_Discriminant_Constraint (Full, No_Elist);
12352 end if;
12353 end if;
12355 if Ekind (Full_Base) = E_Record_Type
12356 and then Has_Discriminants (Full_Base)
12357 and then Has_Discriminants (Priv) -- might not, if errors
12358 and then not Has_Unknown_Discriminants (Priv)
12359 and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
12360 then
12361 Create_Constrained_Components
12362 (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
12364 -- If the full base is itself derived from private, build a congruent
12365 -- subtype of its underlying type, for use by the back end. For a
12366 -- constrained record component, the declaration cannot be placed on
12367 -- the component list, but it must nevertheless be built an analyzed, to
12368 -- supply enough information for Gigi to compute the size of component.
12370 elsif Ekind (Full_Base) in Private_Kind
12371 and then Is_Derived_Type (Full_Base)
12372 and then Has_Discriminants (Full_Base)
12373 and then (Ekind (Current_Scope) /= E_Record_Subtype)
12374 then
12375 if not Is_Itype (Priv)
12376 and then
12377 Nkind (Subtype_Indication (Parent (Priv))) = N_Subtype_Indication
12378 then
12379 Build_Underlying_Full_View
12380 (Parent (Priv), Full, Etype (Full_Base));
12382 elsif Nkind (Related_Nod) = N_Component_Declaration then
12383 Build_Underlying_Full_View (Related_Nod, Full, Etype (Full_Base));
12384 end if;
12386 elsif Is_Record_Type (Full_Base) then
12388 -- Show Full is simply a renaming of Full_Base
12390 Set_Cloned_Subtype (Full, Full_Base);
12391 end if;
12393 -- It is unsafe to share the bounds of a scalar type, because the Itype
12394 -- is elaborated on demand, and if a bound is non-static then different
12395 -- orders of elaboration in different units will lead to different
12396 -- external symbols.
12398 if Is_Scalar_Type (Full_Base) then
12399 Set_Scalar_Range (Full,
12400 Make_Range (Sloc (Related_Nod),
12401 Low_Bound =>
12402 Duplicate_Subexpr_No_Checks (Type_Low_Bound (Full_Base)),
12403 High_Bound =>
12404 Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
12406 -- This completion inherits the bounds of the full parent, but if
12407 -- the parent is an unconstrained floating point type, so is the
12408 -- completion.
12410 if Is_Floating_Point_Type (Full_Base) then
12411 Set_Includes_Infinities
12412 (Scalar_Range (Full), Has_Infinities (Full_Base));
12413 end if;
12414 end if;
12416 -- ??? It seems that a lot of fields are missing that should be copied
12417 -- from Full_Base to Full. Here are some that are introduced in a
12418 -- non-disruptive way but a cleanup is necessary.
12420 if Is_Tagged_Type (Full_Base) then
12421 Set_Is_Tagged_Type (Full);
12422 Set_Direct_Primitive_Operations
12423 (Full, Direct_Primitive_Operations (Full_Base));
12424 Set_No_Tagged_Streams_Pragma
12425 (Full, No_Tagged_Streams_Pragma (Full_Base));
12427 -- Inherit class_wide type of full_base in case the partial view was
12428 -- not tagged. Otherwise it has already been created when the private
12429 -- subtype was analyzed.
12431 if No (Class_Wide_Type (Full)) then
12432 Set_Class_Wide_Type (Full, Class_Wide_Type (Full_Base));
12433 end if;
12435 -- If this is a subtype of a protected or task type, constrain its
12436 -- corresponding record, unless this is a subtype without constraints,
12437 -- i.e. a simple renaming as with an actual subtype in an instance.
12439 elsif Is_Concurrent_Type (Full_Base) then
12440 if Has_Discriminants (Full)
12441 and then Present (Corresponding_Record_Type (Full_Base))
12442 and then
12443 not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
12444 then
12445 Set_Corresponding_Record_Type (Full,
12446 Constrain_Corresponding_Record
12447 (Full, Corresponding_Record_Type (Full_Base), Related_Nod));
12449 else
12450 Set_Corresponding_Record_Type (Full,
12451 Corresponding_Record_Type (Full_Base));
12452 end if;
12453 end if;
12455 -- Link rep item chain, and also setting of Has_Predicates from private
12456 -- subtype to full subtype, since we will need these on the full subtype
12457 -- to create the predicate function. Note that the full subtype may
12458 -- already have rep items, inherited from the full view of the base
12459 -- type, so we must be sure not to overwrite these entries.
12461 declare
12462 Append : Boolean;
12463 Item : Node_Id;
12464 Next_Item : Node_Id;
12465 Priv_Item : Node_Id;
12467 begin
12468 Item := First_Rep_Item (Full);
12469 Priv_Item := First_Rep_Item (Priv);
12471 -- If no existing rep items on full type, we can just link directly
12472 -- to the list of items on the private type, if any exist.. Same if
12473 -- the rep items are only those inherited from the base
12475 if (No (Item)
12476 or else Nkind (Item) /= N_Aspect_Specification
12477 or else Entity (Item) = Full_Base)
12478 and then Present (First_Rep_Item (Priv))
12479 then
12480 Set_First_Rep_Item (Full, Priv_Item);
12482 -- Otherwise, search to the end of items currently linked to the full
12483 -- subtype and append the private items to the end. However, if Priv
12484 -- and Full already have the same list of rep items, then the append
12485 -- is not done, as that would create a circularity.
12487 -- The partial view may have a predicate and the rep item lists of
12488 -- both views agree when inherited from the same ancestor. In that
12489 -- case, simply propagate the list from one view to the other.
12490 -- A more complex analysis needed here ???
12492 elsif Present (Priv_Item)
12493 and then Item = Next_Rep_Item (Priv_Item)
12494 then
12495 Set_First_Rep_Item (Full, Priv_Item);
12497 elsif Item /= Priv_Item then
12498 Append := True;
12499 loop
12500 Next_Item := Next_Rep_Item (Item);
12501 exit when No (Next_Item);
12502 Item := Next_Item;
12504 -- If the private view has aspect specifications, the full view
12505 -- inherits them. Since these aspects may already have been
12506 -- attached to the full view during derivation, do not append
12507 -- them if already present.
12509 if Item = First_Rep_Item (Priv) then
12510 Append := False;
12511 exit;
12512 end if;
12513 end loop;
12515 -- And link the private type items at the end of the chain
12517 if Append then
12518 Set_Next_Rep_Item (Item, First_Rep_Item (Priv));
12519 end if;
12520 end if;
12521 end;
12523 -- Make sure Has_Predicates is set on full type if it is set on the
12524 -- private type. Note that it may already be set on the full type and
12525 -- if so, we don't want to unset it. Similarly, propagate information
12526 -- about delayed aspects, because the corresponding pragmas must be
12527 -- analyzed when one of the views is frozen. This last step is needed
12528 -- in particular when the full type is a scalar type for which an
12529 -- anonymous base type is constructed.
12531 -- The predicate functions are generated either at the freeze point
12532 -- of the type or at the end of the visible part, and we must avoid
12533 -- generating them twice.
12535 if Has_Predicates (Priv) then
12536 Set_Has_Predicates (Full);
12538 if Present (Predicate_Function (Priv))
12539 and then No (Predicate_Function (Full))
12540 then
12541 Set_Predicate_Function (Full, Predicate_Function (Priv));
12542 end if;
12543 end if;
12545 if Has_Delayed_Aspects (Priv) then
12546 Set_Has_Delayed_Aspects (Full);
12547 end if;
12548 end Complete_Private_Subtype;
12550 ----------------------------
12551 -- Constant_Redeclaration --
12552 ----------------------------
12554 procedure Constant_Redeclaration
12555 (Id : Entity_Id;
12556 N : Node_Id;
12557 T : out Entity_Id)
12559 Prev : constant Entity_Id := Current_Entity_In_Scope (Id);
12560 Obj_Def : constant Node_Id := Object_Definition (N);
12561 New_T : Entity_Id;
12563 procedure Check_Possible_Deferred_Completion
12564 (Prev_Id : Entity_Id;
12565 Prev_Obj_Def : Node_Id;
12566 Curr_Obj_Def : Node_Id);
12567 -- Determine whether the two object definitions describe the partial
12568 -- and the full view of a constrained deferred constant. Generate
12569 -- a subtype for the full view and verify that it statically matches
12570 -- the subtype of the partial view.
12572 procedure Check_Recursive_Declaration (Typ : Entity_Id);
12573 -- If deferred constant is an access type initialized with an allocator,
12574 -- check whether there is an illegal recursion in the definition,
12575 -- through a default value of some record subcomponent. This is normally
12576 -- detected when generating init procs, but requires this additional
12577 -- mechanism when expansion is disabled.
12579 ----------------------------------------
12580 -- Check_Possible_Deferred_Completion --
12581 ----------------------------------------
12583 procedure Check_Possible_Deferred_Completion
12584 (Prev_Id : Entity_Id;
12585 Prev_Obj_Def : Node_Id;
12586 Curr_Obj_Def : Node_Id)
12588 begin
12589 if Nkind (Prev_Obj_Def) = N_Subtype_Indication
12590 and then Present (Constraint (Prev_Obj_Def))
12591 and then Nkind (Curr_Obj_Def) = N_Subtype_Indication
12592 and then Present (Constraint (Curr_Obj_Def))
12593 then
12594 declare
12595 Loc : constant Source_Ptr := Sloc (N);
12596 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
12597 Decl : constant Node_Id :=
12598 Make_Subtype_Declaration (Loc,
12599 Defining_Identifier => Def_Id,
12600 Subtype_Indication =>
12601 Relocate_Node (Curr_Obj_Def));
12603 begin
12604 Insert_Before_And_Analyze (N, Decl);
12605 Set_Etype (Id, Def_Id);
12607 if not Subtypes_Statically_Match (Etype (Prev_Id), Def_Id) then
12608 Error_Msg_Sloc := Sloc (Prev_Id);
12609 Error_Msg_N ("subtype does not statically match deferred "
12610 & "declaration #", N);
12611 end if;
12612 end;
12613 end if;
12614 end Check_Possible_Deferred_Completion;
12616 ---------------------------------
12617 -- Check_Recursive_Declaration --
12618 ---------------------------------
12620 procedure Check_Recursive_Declaration (Typ : Entity_Id) is
12621 Comp : Entity_Id;
12623 begin
12624 if Is_Record_Type (Typ) then
12625 Comp := First_Component (Typ);
12626 while Present (Comp) loop
12627 if Comes_From_Source (Comp) then
12628 if Present (Expression (Parent (Comp)))
12629 and then Is_Entity_Name (Expression (Parent (Comp)))
12630 and then Entity (Expression (Parent (Comp))) = Prev
12631 then
12632 Error_Msg_Sloc := Sloc (Parent (Comp));
12633 Error_Msg_NE
12634 ("illegal circularity with declaration for & #",
12635 N, Comp);
12636 return;
12638 elsif Is_Record_Type (Etype (Comp)) then
12639 Check_Recursive_Declaration (Etype (Comp));
12640 end if;
12641 end if;
12643 Next_Component (Comp);
12644 end loop;
12645 end if;
12646 end Check_Recursive_Declaration;
12648 -- Start of processing for Constant_Redeclaration
12650 begin
12651 if Nkind (Parent (Prev)) = N_Object_Declaration then
12652 if Nkind (Object_Definition
12653 (Parent (Prev))) = N_Subtype_Indication
12654 then
12655 -- Find type of new declaration. The constraints of the two
12656 -- views must match statically, but there is no point in
12657 -- creating an itype for the full view.
12659 if Nkind (Obj_Def) = N_Subtype_Indication then
12660 Find_Type (Subtype_Mark (Obj_Def));
12661 New_T := Entity (Subtype_Mark (Obj_Def));
12663 else
12664 Find_Type (Obj_Def);
12665 New_T := Entity (Obj_Def);
12666 end if;
12668 T := Etype (Prev);
12670 else
12671 -- The full view may impose a constraint, even if the partial
12672 -- view does not, so construct the subtype.
12674 New_T := Find_Type_Of_Object (Obj_Def, N);
12675 T := New_T;
12676 end if;
12678 else
12679 -- Current declaration is illegal, diagnosed below in Enter_Name
12681 T := Empty;
12682 New_T := Any_Type;
12683 end if;
12685 -- If previous full declaration or a renaming declaration exists, or if
12686 -- a homograph is present, let Enter_Name handle it, either with an
12687 -- error or with the removal of an overridden implicit subprogram.
12688 -- The previous one is a full declaration if it has an expression
12689 -- (which in the case of an aggregate is indicated by the Init flag).
12691 if Ekind (Prev) /= E_Constant
12692 or else Nkind (Parent (Prev)) = N_Object_Renaming_Declaration
12693 or else Present (Expression (Parent (Prev)))
12694 or else Has_Init_Expression (Parent (Prev))
12695 or else Present (Full_View (Prev))
12696 then
12697 Enter_Name (Id);
12699 -- Verify that types of both declarations match, or else that both types
12700 -- are anonymous access types whose designated subtypes statically match
12701 -- (as allowed in Ada 2005 by AI-385).
12703 elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
12704 and then
12705 (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
12706 or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
12707 or else Is_Access_Constant (Etype (New_T)) /=
12708 Is_Access_Constant (Etype (Prev))
12709 or else Can_Never_Be_Null (Etype (New_T)) /=
12710 Can_Never_Be_Null (Etype (Prev))
12711 or else Null_Exclusion_Present (Parent (Prev)) /=
12712 Null_Exclusion_Present (Parent (Id))
12713 or else not Subtypes_Statically_Match
12714 (Designated_Type (Etype (Prev)),
12715 Designated_Type (Etype (New_T))))
12716 then
12717 Error_Msg_Sloc := Sloc (Prev);
12718 Error_Msg_N ("type does not match declaration#", N);
12719 Set_Full_View (Prev, Id);
12720 Set_Etype (Id, Any_Type);
12722 -- A deferred constant whose type is an anonymous array is always
12723 -- illegal (unless imported). A detailed error message might be
12724 -- helpful for Ada beginners.
12726 if Nkind (Object_Definition (Parent (Prev)))
12727 = N_Constrained_Array_Definition
12728 and then Nkind (Object_Definition (N))
12729 = N_Constrained_Array_Definition
12730 then
12731 Error_Msg_N ("\each anonymous array is a distinct type", N);
12732 Error_Msg_N ("a deferred constant must have a named type",
12733 Object_Definition (Parent (Prev)));
12734 end if;
12736 elsif
12737 Null_Exclusion_Present (Parent (Prev))
12738 and then not Null_Exclusion_Present (N)
12739 then
12740 Error_Msg_Sloc := Sloc (Prev);
12741 Error_Msg_N ("null-exclusion does not match declaration#", N);
12742 Set_Full_View (Prev, Id);
12743 Set_Etype (Id, Any_Type);
12745 -- If so, process the full constant declaration
12747 else
12748 -- RM 7.4 (6): If the subtype defined by the subtype_indication in
12749 -- the deferred declaration is constrained, then the subtype defined
12750 -- by the subtype_indication in the full declaration shall match it
12751 -- statically.
12753 Check_Possible_Deferred_Completion
12754 (Prev_Id => Prev,
12755 Prev_Obj_Def => Object_Definition (Parent (Prev)),
12756 Curr_Obj_Def => Obj_Def);
12758 Set_Full_View (Prev, Id);
12759 Set_Is_Public (Id, Is_Public (Prev));
12760 Set_Is_Internal (Id);
12761 Append_Entity (Id, Current_Scope);
12763 -- Check ALIASED present if present before (RM 7.4(7))
12765 if Is_Aliased (Prev)
12766 and then not Aliased_Present (N)
12767 then
12768 Error_Msg_Sloc := Sloc (Prev);
12769 Error_Msg_N ("ALIASED required (see declaration #)", N);
12770 end if;
12772 -- Check that placement is in private part and that the incomplete
12773 -- declaration appeared in the visible part.
12775 if Ekind (Current_Scope) = E_Package
12776 and then not In_Private_Part (Current_Scope)
12777 then
12778 Error_Msg_Sloc := Sloc (Prev);
12779 Error_Msg_N
12780 ("full constant for declaration # must be in private part", N);
12782 elsif Ekind (Current_Scope) = E_Package
12783 and then
12784 List_Containing (Parent (Prev)) /=
12785 Visible_Declarations (Package_Specification (Current_Scope))
12786 then
12787 Error_Msg_N
12788 ("deferred constant must be declared in visible part",
12789 Parent (Prev));
12790 end if;
12792 if Is_Access_Type (T)
12793 and then Nkind (Expression (N)) = N_Allocator
12794 then
12795 Check_Recursive_Declaration (Designated_Type (T));
12796 end if;
12798 -- A deferred constant is a visible entity. If type has invariants,
12799 -- verify that the initial value satisfies them. This is not done in
12800 -- GNATprove mode, as GNATprove handles invariant checks itself.
12802 if Has_Invariants (T)
12803 and then Present (Invariant_Procedure (T))
12804 and then not GNATprove_Mode
12805 then
12806 Insert_After (N,
12807 Make_Invariant_Call (New_Occurrence_Of (Prev, Sloc (N))));
12808 end if;
12809 end if;
12810 end Constant_Redeclaration;
12812 ----------------------
12813 -- Constrain_Access --
12814 ----------------------
12816 procedure Constrain_Access
12817 (Def_Id : in out Entity_Id;
12818 S : Node_Id;
12819 Related_Nod : Node_Id)
12821 T : constant Entity_Id := Entity (Subtype_Mark (S));
12822 Desig_Type : constant Entity_Id := Designated_Type (T);
12823 Desig_Subtype : Entity_Id := Create_Itype (E_Void, Related_Nod);
12824 Constraint_OK : Boolean := True;
12826 begin
12827 if Is_Array_Type (Desig_Type) then
12828 Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
12830 elsif (Is_Record_Type (Desig_Type)
12831 or else Is_Incomplete_Or_Private_Type (Desig_Type))
12832 and then not Is_Constrained (Desig_Type)
12833 then
12834 -- ??? The following code is a temporary bypass to ignore a
12835 -- discriminant constraint on access type if it is constraining
12836 -- the current record. Avoid creating the implicit subtype of the
12837 -- record we are currently compiling since right now, we cannot
12838 -- handle these. For now, just return the access type itself.
12840 if Desig_Type = Current_Scope
12841 and then No (Def_Id)
12842 then
12843 Set_Ekind (Desig_Subtype, E_Record_Subtype);
12844 Def_Id := Entity (Subtype_Mark (S));
12846 -- This call added to ensure that the constraint is analyzed
12847 -- (needed for a B test). Note that we still return early from
12848 -- this procedure to avoid recursive processing. ???
12850 Constrain_Discriminated_Type
12851 (Desig_Subtype, S, Related_Nod, For_Access => True);
12852 return;
12853 end if;
12855 -- Enforce rule that the constraint is illegal if there is an
12856 -- unconstrained view of the designated type. This means that the
12857 -- partial view (either a private type declaration or a derivation
12858 -- from a private type) has no discriminants. (Defect Report
12859 -- 8652/0008, Technical Corrigendum 1, checked by ACATS B371001).
12861 -- Rule updated for Ada 2005: The private type is said to have
12862 -- a constrained partial view, given that objects of the type
12863 -- can be declared. Furthermore, the rule applies to all access
12864 -- types, unlike the rule concerning default discriminants (see
12865 -- RM 3.7.1(7/3))
12867 if (Ekind (T) = E_General_Access_Type or else Ada_Version >= Ada_2005)
12868 and then Has_Private_Declaration (Desig_Type)
12869 and then In_Open_Scopes (Scope (Desig_Type))
12870 and then Has_Discriminants (Desig_Type)
12871 then
12872 declare
12873 Pack : constant Node_Id :=
12874 Unit_Declaration_Node (Scope (Desig_Type));
12875 Decls : List_Id;
12876 Decl : Node_Id;
12878 begin
12879 if Nkind (Pack) = N_Package_Declaration then
12880 Decls := Visible_Declarations (Specification (Pack));
12881 Decl := First (Decls);
12882 while Present (Decl) loop
12883 if (Nkind (Decl) = N_Private_Type_Declaration
12884 and then Chars (Defining_Identifier (Decl)) =
12885 Chars (Desig_Type))
12887 or else
12888 (Nkind (Decl) = N_Full_Type_Declaration
12889 and then
12890 Chars (Defining_Identifier (Decl)) =
12891 Chars (Desig_Type)
12892 and then Is_Derived_Type (Desig_Type)
12893 and then
12894 Has_Private_Declaration (Etype (Desig_Type)))
12895 then
12896 if No (Discriminant_Specifications (Decl)) then
12897 Error_Msg_N
12898 ("cannot constrain access type if designated "
12899 & "type has constrained partial view", S);
12900 end if;
12902 exit;
12903 end if;
12905 Next (Decl);
12906 end loop;
12907 end if;
12908 end;
12909 end if;
12911 Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
12912 For_Access => True);
12914 elsif Is_Concurrent_Type (Desig_Type)
12915 and then not Is_Constrained (Desig_Type)
12916 then
12917 Constrain_Concurrent (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
12919 else
12920 Error_Msg_N ("invalid constraint on access type", S);
12922 -- We simply ignore an invalid constraint
12924 Desig_Subtype := Desig_Type;
12925 Constraint_OK := False;
12926 end if;
12928 if No (Def_Id) then
12929 Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
12930 else
12931 Set_Ekind (Def_Id, E_Access_Subtype);
12932 end if;
12934 if Constraint_OK then
12935 Set_Etype (Def_Id, Base_Type (T));
12937 if Is_Private_Type (Desig_Type) then
12938 Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
12939 end if;
12940 else
12941 Set_Etype (Def_Id, Any_Type);
12942 end if;
12944 Set_Size_Info (Def_Id, T);
12945 Set_Is_Constrained (Def_Id, Constraint_OK);
12946 Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
12947 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
12948 Set_Is_Access_Constant (Def_Id, Is_Access_Constant (T));
12950 Conditional_Delay (Def_Id, T);
12952 -- AI-363 : Subtypes of general access types whose designated types have
12953 -- default discriminants are disallowed. In instances, the rule has to
12954 -- be checked against the actual, of which T is the subtype. In a
12955 -- generic body, the rule is checked assuming that the actual type has
12956 -- defaulted discriminants.
12958 if Ada_Version >= Ada_2005 or else Warn_On_Ada_2005_Compatibility then
12959 if Ekind (Base_Type (T)) = E_General_Access_Type
12960 and then Has_Defaulted_Discriminants (Desig_Type)
12961 then
12962 if Ada_Version < Ada_2005 then
12963 Error_Msg_N
12964 ("access subtype of general access type would not " &
12965 "be allowed in Ada 2005?y?", S);
12966 else
12967 Error_Msg_N
12968 ("access subtype of general access type not allowed", S);
12969 end if;
12971 Error_Msg_N ("\discriminants have defaults", S);
12973 elsif Is_Access_Type (T)
12974 and then Is_Generic_Type (Desig_Type)
12975 and then Has_Discriminants (Desig_Type)
12976 and then In_Package_Body (Current_Scope)
12977 then
12978 if Ada_Version < Ada_2005 then
12979 Error_Msg_N
12980 ("access subtype would not be allowed in generic body "
12981 & "in Ada 2005?y?", S);
12982 else
12983 Error_Msg_N
12984 ("access subtype not allowed in generic body", S);
12985 end if;
12987 Error_Msg_N
12988 ("\designated type is a discriminated formal", S);
12989 end if;
12990 end if;
12991 end Constrain_Access;
12993 ---------------------
12994 -- Constrain_Array --
12995 ---------------------
12997 procedure Constrain_Array
12998 (Def_Id : in out Entity_Id;
12999 SI : Node_Id;
13000 Related_Nod : Node_Id;
13001 Related_Id : Entity_Id;
13002 Suffix : Character)
13004 C : constant Node_Id := Constraint (SI);
13005 Number_Of_Constraints : Nat := 0;
13006 Index : Node_Id;
13007 S, T : Entity_Id;
13008 Constraint_OK : Boolean := True;
13010 begin
13011 T := Entity (Subtype_Mark (SI));
13013 if Is_Access_Type (T) then
13014 T := Designated_Type (T);
13015 end if;
13017 -- If an index constraint follows a subtype mark in a subtype indication
13018 -- then the type or subtype denoted by the subtype mark must not already
13019 -- impose an index constraint. The subtype mark must denote either an
13020 -- unconstrained array type or an access type whose designated type
13021 -- is such an array type... (RM 3.6.1)
13023 if Is_Constrained (T) then
13024 Error_Msg_N ("array type is already constrained", Subtype_Mark (SI));
13025 Constraint_OK := False;
13027 else
13028 S := First (Constraints (C));
13029 while Present (S) loop
13030 Number_Of_Constraints := Number_Of_Constraints + 1;
13031 Next (S);
13032 end loop;
13034 -- In either case, the index constraint must provide a discrete
13035 -- range for each index of the array type and the type of each
13036 -- discrete range must be the same as that of the corresponding
13037 -- index. (RM 3.6.1)
13039 if Number_Of_Constraints /= Number_Dimensions (T) then
13040 Error_Msg_NE ("incorrect number of index constraints for }", C, T);
13041 Constraint_OK := False;
13043 else
13044 S := First (Constraints (C));
13045 Index := First_Index (T);
13046 Analyze (Index);
13048 -- Apply constraints to each index type
13050 for J in 1 .. Number_Of_Constraints loop
13051 Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
13052 Next (Index);
13053 Next (S);
13054 end loop;
13056 end if;
13057 end if;
13059 if No (Def_Id) then
13060 Def_Id :=
13061 Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
13062 Set_Parent (Def_Id, Related_Nod);
13064 else
13065 Set_Ekind (Def_Id, E_Array_Subtype);
13066 end if;
13068 Set_Size_Info (Def_Id, (T));
13069 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13070 Set_Etype (Def_Id, Base_Type (T));
13072 if Constraint_OK then
13073 Set_First_Index (Def_Id, First (Constraints (C)));
13074 else
13075 Set_First_Index (Def_Id, First_Index (T));
13076 end if;
13078 Set_Is_Constrained (Def_Id, True);
13079 Set_Is_Aliased (Def_Id, Is_Aliased (T));
13080 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13082 Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
13083 Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
13085 -- A subtype does not inherit the Packed_Array_Impl_Type of is parent.
13086 -- We need to initialize the attribute because if Def_Id is previously
13087 -- analyzed through a limited_with clause, it will have the attributes
13088 -- of an incomplete type, one of which is an Elist that overlaps the
13089 -- Packed_Array_Impl_Type field.
13091 Set_Packed_Array_Impl_Type (Def_Id, Empty);
13093 -- Build a freeze node if parent still needs one. Also make sure that
13094 -- the Depends_On_Private status is set because the subtype will need
13095 -- reprocessing at the time the base type does, and also we must set a
13096 -- conditional delay.
13098 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
13099 Conditional_Delay (Def_Id, T);
13100 end Constrain_Array;
13102 ------------------------------
13103 -- Constrain_Component_Type --
13104 ------------------------------
13106 function Constrain_Component_Type
13107 (Comp : Entity_Id;
13108 Constrained_Typ : Entity_Id;
13109 Related_Node : Node_Id;
13110 Typ : Entity_Id;
13111 Constraints : Elist_Id) return Entity_Id
13113 Loc : constant Source_Ptr := Sloc (Constrained_Typ);
13114 Compon_Type : constant Entity_Id := Etype (Comp);
13116 function Build_Constrained_Array_Type
13117 (Old_Type : Entity_Id) return Entity_Id;
13118 -- If Old_Type is an array type, one of whose indexes is constrained
13119 -- by a discriminant, build an Itype whose constraint replaces the
13120 -- discriminant with its value in the constraint.
13122 function Build_Constrained_Discriminated_Type
13123 (Old_Type : Entity_Id) return Entity_Id;
13124 -- Ditto for record components
13126 function Build_Constrained_Access_Type
13127 (Old_Type : Entity_Id) return Entity_Id;
13128 -- Ditto for access types. Makes use of previous two functions, to
13129 -- constrain designated type.
13131 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id;
13132 -- T is an array or discriminated type, C is a list of constraints
13133 -- that apply to T. This routine builds the constrained subtype.
13135 function Is_Discriminant (Expr : Node_Id) return Boolean;
13136 -- Returns True if Expr is a discriminant
13138 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id;
13139 -- Find the value of discriminant Discrim in Constraint
13141 -----------------------------------
13142 -- Build_Constrained_Access_Type --
13143 -----------------------------------
13145 function Build_Constrained_Access_Type
13146 (Old_Type : Entity_Id) return Entity_Id
13148 Desig_Type : constant Entity_Id := Designated_Type (Old_Type);
13149 Itype : Entity_Id;
13150 Desig_Subtype : Entity_Id;
13151 Scop : Entity_Id;
13153 begin
13154 -- if the original access type was not embedded in the enclosing
13155 -- type definition, there is no need to produce a new access
13156 -- subtype. In fact every access type with an explicit constraint
13157 -- generates an itype whose scope is the enclosing record.
13159 if not Is_Type (Scope (Old_Type)) then
13160 return Old_Type;
13162 elsif Is_Array_Type (Desig_Type) then
13163 Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
13165 elsif Has_Discriminants (Desig_Type) then
13167 -- This may be an access type to an enclosing record type for
13168 -- which we are constructing the constrained components. Return
13169 -- the enclosing record subtype. This is not always correct,
13170 -- but avoids infinite recursion. ???
13172 Desig_Subtype := Any_Type;
13174 for J in reverse 0 .. Scope_Stack.Last loop
13175 Scop := Scope_Stack.Table (J).Entity;
13177 if Is_Type (Scop)
13178 and then Base_Type (Scop) = Base_Type (Desig_Type)
13179 then
13180 Desig_Subtype := Scop;
13181 end if;
13183 exit when not Is_Type (Scop);
13184 end loop;
13186 if Desig_Subtype = Any_Type then
13187 Desig_Subtype :=
13188 Build_Constrained_Discriminated_Type (Desig_Type);
13189 end if;
13191 else
13192 return Old_Type;
13193 end if;
13195 if Desig_Subtype /= Desig_Type then
13197 -- The Related_Node better be here or else we won't be able
13198 -- to attach new itypes to a node in the tree.
13200 pragma Assert (Present (Related_Node));
13202 Itype := Create_Itype (E_Access_Subtype, Related_Node);
13204 Set_Etype (Itype, Base_Type (Old_Type));
13205 Set_Size_Info (Itype, (Old_Type));
13206 Set_Directly_Designated_Type (Itype, Desig_Subtype);
13207 Set_Depends_On_Private (Itype, Has_Private_Component
13208 (Old_Type));
13209 Set_Is_Access_Constant (Itype, Is_Access_Constant
13210 (Old_Type));
13212 -- The new itype needs freezing when it depends on a not frozen
13213 -- type and the enclosing subtype needs freezing.
13215 if Has_Delayed_Freeze (Constrained_Typ)
13216 and then not Is_Frozen (Constrained_Typ)
13217 then
13218 Conditional_Delay (Itype, Base_Type (Old_Type));
13219 end if;
13221 return Itype;
13223 else
13224 return Old_Type;
13225 end if;
13226 end Build_Constrained_Access_Type;
13228 ----------------------------------
13229 -- Build_Constrained_Array_Type --
13230 ----------------------------------
13232 function Build_Constrained_Array_Type
13233 (Old_Type : Entity_Id) return Entity_Id
13235 Lo_Expr : Node_Id;
13236 Hi_Expr : Node_Id;
13237 Old_Index : Node_Id;
13238 Range_Node : Node_Id;
13239 Constr_List : List_Id;
13241 Need_To_Create_Itype : Boolean := False;
13243 begin
13244 Old_Index := First_Index (Old_Type);
13245 while Present (Old_Index) loop
13246 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
13248 if Is_Discriminant (Lo_Expr)
13249 or else
13250 Is_Discriminant (Hi_Expr)
13251 then
13252 Need_To_Create_Itype := True;
13253 end if;
13255 Next_Index (Old_Index);
13256 end loop;
13258 if Need_To_Create_Itype then
13259 Constr_List := New_List;
13261 Old_Index := First_Index (Old_Type);
13262 while Present (Old_Index) loop
13263 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
13265 if Is_Discriminant (Lo_Expr) then
13266 Lo_Expr := Get_Discr_Value (Lo_Expr);
13267 end if;
13269 if Is_Discriminant (Hi_Expr) then
13270 Hi_Expr := Get_Discr_Value (Hi_Expr);
13271 end if;
13273 Range_Node :=
13274 Make_Range
13275 (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
13277 Append (Range_Node, To => Constr_List);
13279 Next_Index (Old_Index);
13280 end loop;
13282 return Build_Subtype (Old_Type, Constr_List);
13284 else
13285 return Old_Type;
13286 end if;
13287 end Build_Constrained_Array_Type;
13289 ------------------------------------------
13290 -- Build_Constrained_Discriminated_Type --
13291 ------------------------------------------
13293 function Build_Constrained_Discriminated_Type
13294 (Old_Type : Entity_Id) return Entity_Id
13296 Expr : Node_Id;
13297 Constr_List : List_Id;
13298 Old_Constraint : Elmt_Id;
13300 Need_To_Create_Itype : Boolean := False;
13302 begin
13303 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
13304 while Present (Old_Constraint) loop
13305 Expr := Node (Old_Constraint);
13307 if Is_Discriminant (Expr) then
13308 Need_To_Create_Itype := True;
13309 end if;
13311 Next_Elmt (Old_Constraint);
13312 end loop;
13314 if Need_To_Create_Itype then
13315 Constr_List := New_List;
13317 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
13318 while Present (Old_Constraint) loop
13319 Expr := Node (Old_Constraint);
13321 if Is_Discriminant (Expr) then
13322 Expr := Get_Discr_Value (Expr);
13323 end if;
13325 Append (New_Copy_Tree (Expr), To => Constr_List);
13327 Next_Elmt (Old_Constraint);
13328 end loop;
13330 return Build_Subtype (Old_Type, Constr_List);
13332 else
13333 return Old_Type;
13334 end if;
13335 end Build_Constrained_Discriminated_Type;
13337 -------------------
13338 -- Build_Subtype --
13339 -------------------
13341 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id is
13342 Indic : Node_Id;
13343 Subtyp_Decl : Node_Id;
13344 Def_Id : Entity_Id;
13345 Btyp : Entity_Id := Base_Type (T);
13347 begin
13348 -- The Related_Node better be here or else we won't be able to
13349 -- attach new itypes to a node in the tree.
13351 pragma Assert (Present (Related_Node));
13353 -- If the view of the component's type is incomplete or private
13354 -- with unknown discriminants, then the constraint must be applied
13355 -- to the full type.
13357 if Has_Unknown_Discriminants (Btyp)
13358 and then Present (Underlying_Type (Btyp))
13359 then
13360 Btyp := Underlying_Type (Btyp);
13361 end if;
13363 Indic :=
13364 Make_Subtype_Indication (Loc,
13365 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
13366 Constraint => Make_Index_Or_Discriminant_Constraint (Loc, C));
13368 Def_Id := Create_Itype (Ekind (T), Related_Node);
13370 Subtyp_Decl :=
13371 Make_Subtype_Declaration (Loc,
13372 Defining_Identifier => Def_Id,
13373 Subtype_Indication => Indic);
13375 Set_Parent (Subtyp_Decl, Parent (Related_Node));
13377 -- Itypes must be analyzed with checks off (see package Itypes)
13379 Analyze (Subtyp_Decl, Suppress => All_Checks);
13381 return Def_Id;
13382 end Build_Subtype;
13384 ---------------------
13385 -- Get_Discr_Value --
13386 ---------------------
13388 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id is
13389 D : Entity_Id;
13390 E : Elmt_Id;
13392 begin
13393 -- The discriminant may be declared for the type, in which case we
13394 -- find it by iterating over the list of discriminants. If the
13395 -- discriminant is inherited from a parent type, it appears as the
13396 -- corresponding discriminant of the current type. This will be the
13397 -- case when constraining an inherited component whose constraint is
13398 -- given by a discriminant of the parent.
13400 D := First_Discriminant (Typ);
13401 E := First_Elmt (Constraints);
13403 while Present (D) loop
13404 if D = Entity (Discrim)
13405 or else D = CR_Discriminant (Entity (Discrim))
13406 or else Corresponding_Discriminant (D) = Entity (Discrim)
13407 then
13408 return Node (E);
13409 end if;
13411 Next_Discriminant (D);
13412 Next_Elmt (E);
13413 end loop;
13415 -- The Corresponding_Discriminant mechanism is incomplete, because
13416 -- the correspondence between new and old discriminants is not one
13417 -- to one: one new discriminant can constrain several old ones. In
13418 -- that case, scan sequentially the stored_constraint, the list of
13419 -- discriminants of the parents, and the constraints.
13421 -- Previous code checked for the present of the Stored_Constraint
13422 -- list for the derived type, but did not use it at all. Should it
13423 -- be present when the component is a discriminated task type?
13425 if Is_Derived_Type (Typ)
13426 and then Scope (Entity (Discrim)) = Etype (Typ)
13427 then
13428 D := First_Discriminant (Etype (Typ));
13429 E := First_Elmt (Constraints);
13430 while Present (D) loop
13431 if D = Entity (Discrim) then
13432 return Node (E);
13433 end if;
13435 Next_Discriminant (D);
13436 Next_Elmt (E);
13437 end loop;
13438 end if;
13440 -- Something is wrong if we did not find the value
13442 raise Program_Error;
13443 end Get_Discr_Value;
13445 ---------------------
13446 -- Is_Discriminant --
13447 ---------------------
13449 function Is_Discriminant (Expr : Node_Id) return Boolean is
13450 Discrim_Scope : Entity_Id;
13452 begin
13453 if Denotes_Discriminant (Expr) then
13454 Discrim_Scope := Scope (Entity (Expr));
13456 -- Either we have a reference to one of Typ's discriminants,
13458 pragma Assert (Discrim_Scope = Typ
13460 -- or to the discriminants of the parent type, in the case
13461 -- of a derivation of a tagged type with variants.
13463 or else Discrim_Scope = Etype (Typ)
13464 or else Full_View (Discrim_Scope) = Etype (Typ)
13466 -- or same as above for the case where the discriminants
13467 -- were declared in Typ's private view.
13469 or else (Is_Private_Type (Discrim_Scope)
13470 and then Chars (Discrim_Scope) = Chars (Typ))
13472 -- or else we are deriving from the full view and the
13473 -- discriminant is declared in the private entity.
13475 or else (Is_Private_Type (Typ)
13476 and then Chars (Discrim_Scope) = Chars (Typ))
13478 -- Or we are constrained the corresponding record of a
13479 -- synchronized type that completes a private declaration.
13481 or else (Is_Concurrent_Record_Type (Typ)
13482 and then
13483 Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
13485 -- or we have a class-wide type, in which case make sure the
13486 -- discriminant found belongs to the root type.
13488 or else (Is_Class_Wide_Type (Typ)
13489 and then Etype (Typ) = Discrim_Scope));
13491 return True;
13492 end if;
13494 -- In all other cases we have something wrong
13496 return False;
13497 end Is_Discriminant;
13499 -- Start of processing for Constrain_Component_Type
13501 begin
13502 if Nkind (Parent (Comp)) = N_Component_Declaration
13503 and then Comes_From_Source (Parent (Comp))
13504 and then Comes_From_Source
13505 (Subtype_Indication (Component_Definition (Parent (Comp))))
13506 and then
13507 Is_Entity_Name
13508 (Subtype_Indication (Component_Definition (Parent (Comp))))
13509 then
13510 return Compon_Type;
13512 elsif Is_Array_Type (Compon_Type) then
13513 return Build_Constrained_Array_Type (Compon_Type);
13515 elsif Has_Discriminants (Compon_Type) then
13516 return Build_Constrained_Discriminated_Type (Compon_Type);
13518 elsif Is_Access_Type (Compon_Type) then
13519 return Build_Constrained_Access_Type (Compon_Type);
13521 else
13522 return Compon_Type;
13523 end if;
13524 end Constrain_Component_Type;
13526 --------------------------
13527 -- Constrain_Concurrent --
13528 --------------------------
13530 -- For concurrent types, the associated record value type carries the same
13531 -- discriminants, so when we constrain a concurrent type, we must constrain
13532 -- the corresponding record type as well.
13534 procedure Constrain_Concurrent
13535 (Def_Id : in out Entity_Id;
13536 SI : Node_Id;
13537 Related_Nod : Node_Id;
13538 Related_Id : Entity_Id;
13539 Suffix : Character)
13541 -- Retrieve Base_Type to ensure getting to the concurrent type in the
13542 -- case of a private subtype (needed when only doing semantic analysis).
13544 T_Ent : Entity_Id := Base_Type (Entity (Subtype_Mark (SI)));
13545 T_Val : Entity_Id;
13547 begin
13548 if Is_Access_Type (T_Ent) then
13549 T_Ent := Designated_Type (T_Ent);
13550 end if;
13552 T_Val := Corresponding_Record_Type (T_Ent);
13554 if Present (T_Val) then
13556 if No (Def_Id) then
13557 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
13559 -- Elaborate itype now, as it may be used in a subsequent
13560 -- synchronized operation in another scope.
13562 if Nkind (Related_Nod) = N_Full_Type_Declaration then
13563 Build_Itype_Reference (Def_Id, Related_Nod);
13564 end if;
13565 end if;
13567 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
13568 Set_First_Private_Entity (Def_Id, First_Private_Entity (T_Ent));
13570 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13571 Set_Corresponding_Record_Type (Def_Id,
13572 Constrain_Corresponding_Record (Def_Id, T_Val, Related_Nod));
13574 else
13575 -- If there is no associated record, expansion is disabled and this
13576 -- is a generic context. Create a subtype in any case, so that
13577 -- semantic analysis can proceed.
13579 if No (Def_Id) then
13580 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
13581 end if;
13583 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
13584 end if;
13585 end Constrain_Concurrent;
13587 ------------------------------------
13588 -- Constrain_Corresponding_Record --
13589 ------------------------------------
13591 function Constrain_Corresponding_Record
13592 (Prot_Subt : Entity_Id;
13593 Corr_Rec : Entity_Id;
13594 Related_Nod : Node_Id) return Entity_Id
13596 T_Sub : constant Entity_Id :=
13597 Create_Itype (E_Record_Subtype, Related_Nod, Corr_Rec, 'C');
13599 begin
13600 Set_Etype (T_Sub, Corr_Rec);
13601 Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
13602 Set_Is_Constrained (T_Sub, True);
13603 Set_First_Entity (T_Sub, First_Entity (Corr_Rec));
13604 Set_Last_Entity (T_Sub, Last_Entity (Corr_Rec));
13606 if Has_Discriminants (Prot_Subt) then -- False only if errors.
13607 Set_Discriminant_Constraint
13608 (T_Sub, Discriminant_Constraint (Prot_Subt));
13609 Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
13610 Create_Constrained_Components
13611 (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
13612 end if;
13614 Set_Depends_On_Private (T_Sub, Has_Private_Component (T_Sub));
13616 if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
13617 Conditional_Delay (T_Sub, Corr_Rec);
13619 else
13620 -- This is a component subtype: it will be frozen in the context of
13621 -- the enclosing record's init_proc, so that discriminant references
13622 -- are resolved to discriminals. (Note: we used to skip freezing
13623 -- altogether in that case, which caused errors downstream for
13624 -- components of a bit packed array type).
13626 Set_Has_Delayed_Freeze (T_Sub);
13627 end if;
13629 return T_Sub;
13630 end Constrain_Corresponding_Record;
13632 -----------------------
13633 -- Constrain_Decimal --
13634 -----------------------
13636 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id) is
13637 T : constant Entity_Id := Entity (Subtype_Mark (S));
13638 C : constant Node_Id := Constraint (S);
13639 Loc : constant Source_Ptr := Sloc (C);
13640 Range_Expr : Node_Id;
13641 Digits_Expr : Node_Id;
13642 Digits_Val : Uint;
13643 Bound_Val : Ureal;
13645 begin
13646 Set_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
13648 if Nkind (C) = N_Range_Constraint then
13649 Range_Expr := Range_Expression (C);
13650 Digits_Val := Digits_Value (T);
13652 else
13653 pragma Assert (Nkind (C) = N_Digits_Constraint);
13655 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
13657 Digits_Expr := Digits_Expression (C);
13658 Analyze_And_Resolve (Digits_Expr, Any_Integer);
13660 Check_Digits_Expression (Digits_Expr);
13661 Digits_Val := Expr_Value (Digits_Expr);
13663 if Digits_Val > Digits_Value (T) then
13664 Error_Msg_N
13665 ("digits expression is incompatible with subtype", C);
13666 Digits_Val := Digits_Value (T);
13667 end if;
13669 if Present (Range_Constraint (C)) then
13670 Range_Expr := Range_Expression (Range_Constraint (C));
13671 else
13672 Range_Expr := Empty;
13673 end if;
13674 end if;
13676 Set_Etype (Def_Id, Base_Type (T));
13677 Set_Size_Info (Def_Id, (T));
13678 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13679 Set_Delta_Value (Def_Id, Delta_Value (T));
13680 Set_Scale_Value (Def_Id, Scale_Value (T));
13681 Set_Small_Value (Def_Id, Small_Value (T));
13682 Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
13683 Set_Digits_Value (Def_Id, Digits_Val);
13685 -- Manufacture range from given digits value if no range present
13687 if No (Range_Expr) then
13688 Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
13689 Range_Expr :=
13690 Make_Range (Loc,
13691 Low_Bound =>
13692 Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
13693 High_Bound =>
13694 Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
13695 end if;
13697 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
13698 Set_Discrete_RM_Size (Def_Id);
13700 -- Unconditionally delay the freeze, since we cannot set size
13701 -- information in all cases correctly until the freeze point.
13703 Set_Has_Delayed_Freeze (Def_Id);
13704 end Constrain_Decimal;
13706 ----------------------------------
13707 -- Constrain_Discriminated_Type --
13708 ----------------------------------
13710 procedure Constrain_Discriminated_Type
13711 (Def_Id : Entity_Id;
13712 S : Node_Id;
13713 Related_Nod : Node_Id;
13714 For_Access : Boolean := False)
13716 E : Entity_Id := Entity (Subtype_Mark (S));
13717 T : Entity_Id;
13719 procedure Fixup_Bad_Constraint;
13720 -- Called after finding a bad constraint, and after having posted an
13721 -- appropriate error message. The goal is to leave type Def_Id in as
13722 -- reasonable state as possible.
13724 --------------------------
13725 -- Fixup_Bad_Constraint --
13726 --------------------------
13728 procedure Fixup_Bad_Constraint is
13729 begin
13730 -- Set a reasonable Ekind for the entity, including incomplete types.
13732 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
13734 -- Set Etype to the known type, to reduce chances of cascaded errors
13736 Set_Etype (Def_Id, E);
13737 Set_Error_Posted (Def_Id);
13738 end Fixup_Bad_Constraint;
13740 -- Local variables
13742 C : Node_Id;
13743 Constr : Elist_Id := New_Elmt_List;
13745 -- Start of processing for Constrain_Discriminated_Type
13747 begin
13748 C := Constraint (S);
13750 -- A discriminant constraint is only allowed in a subtype indication,
13751 -- after a subtype mark. This subtype mark must denote either a type
13752 -- with discriminants, or an access type whose designated type is a
13753 -- type with discriminants. A discriminant constraint specifies the
13754 -- values of these discriminants (RM 3.7.2(5)).
13756 T := Base_Type (Entity (Subtype_Mark (S)));
13758 if Is_Access_Type (T) then
13759 T := Designated_Type (T);
13760 end if;
13762 -- In an instance it may be necessary to retrieve the full view of a
13763 -- type with unknown discriminants, or a full view with defaulted
13764 -- discriminants. In other contexts the constraint is illegal.
13766 if In_Instance
13767 and then Is_Private_Type (T)
13768 and then Present (Full_View (T))
13769 and then
13770 (Has_Unknown_Discriminants (T)
13771 or else
13772 (not Has_Discriminants (T)
13773 and then Has_Discriminants (Full_View (T))
13774 and then Present (Discriminant_Default_Value
13775 (First_Discriminant (Full_View (T))))))
13776 then
13777 T := Full_View (T);
13778 E := Full_View (E);
13779 end if;
13781 -- Ada 2005 (AI-412): Constrained incomplete subtypes are illegal. Avoid
13782 -- generating an error for access-to-incomplete subtypes.
13784 if Ada_Version >= Ada_2005
13785 and then Ekind (T) = E_Incomplete_Type
13786 and then Nkind (Parent (S)) = N_Subtype_Declaration
13787 and then not Is_Itype (Def_Id)
13788 then
13789 -- A little sanity check: emit an error message if the type has
13790 -- discriminants to begin with. Type T may be a regular incomplete
13791 -- type or imported via a limited with clause.
13793 if Has_Discriminants (T)
13794 or else (From_Limited_With (T)
13795 and then Present (Non_Limited_View (T))
13796 and then Nkind (Parent (Non_Limited_View (T))) =
13797 N_Full_Type_Declaration
13798 and then Present (Discriminant_Specifications
13799 (Parent (Non_Limited_View (T)))))
13800 then
13801 Error_Msg_N
13802 ("(Ada 2005) incomplete subtype may not be constrained", C);
13803 else
13804 Error_Msg_N ("invalid constraint: type has no discriminant", C);
13805 end if;
13807 Fixup_Bad_Constraint;
13808 return;
13810 -- Check that the type has visible discriminants. The type may be
13811 -- a private type with unknown discriminants whose full view has
13812 -- discriminants which are invisible.
13814 elsif not Has_Discriminants (T)
13815 or else
13816 (Has_Unknown_Discriminants (T)
13817 and then Is_Private_Type (T))
13818 then
13819 Error_Msg_N ("invalid constraint: type has no discriminant", C);
13820 Fixup_Bad_Constraint;
13821 return;
13823 elsif Is_Constrained (E)
13824 or else (Ekind (E) = E_Class_Wide_Subtype
13825 and then Present (Discriminant_Constraint (E)))
13826 then
13827 Error_Msg_N ("type is already constrained", Subtype_Mark (S));
13828 Fixup_Bad_Constraint;
13829 return;
13830 end if;
13832 -- T may be an unconstrained subtype (e.g. a generic actual). Constraint
13833 -- applies to the base type.
13835 T := Base_Type (T);
13837 Constr := Build_Discriminant_Constraints (T, S);
13839 -- If the list returned was empty we had an error in building the
13840 -- discriminant constraint. We have also already signalled an error
13841 -- in the incomplete type case
13843 if Is_Empty_Elmt_List (Constr) then
13844 Fixup_Bad_Constraint;
13845 return;
13846 end if;
13848 Build_Discriminated_Subtype (T, Def_Id, Constr, Related_Nod, For_Access);
13849 end Constrain_Discriminated_Type;
13851 ---------------------------
13852 -- Constrain_Enumeration --
13853 ---------------------------
13855 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id) is
13856 T : constant Entity_Id := Entity (Subtype_Mark (S));
13857 C : constant Node_Id := Constraint (S);
13859 begin
13860 Set_Ekind (Def_Id, E_Enumeration_Subtype);
13862 Set_First_Literal (Def_Id, First_Literal (Base_Type (T)));
13864 Set_Etype (Def_Id, Base_Type (T));
13865 Set_Size_Info (Def_Id, (T));
13866 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13867 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
13869 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13871 Set_Discrete_RM_Size (Def_Id);
13872 end Constrain_Enumeration;
13874 ----------------------
13875 -- Constrain_Float --
13876 ----------------------
13878 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id) is
13879 T : constant Entity_Id := Entity (Subtype_Mark (S));
13880 C : Node_Id;
13881 D : Node_Id;
13882 Rais : Node_Id;
13884 begin
13885 Set_Ekind (Def_Id, E_Floating_Point_Subtype);
13887 Set_Etype (Def_Id, Base_Type (T));
13888 Set_Size_Info (Def_Id, (T));
13889 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13891 -- Process the constraint
13893 C := Constraint (S);
13895 -- Digits constraint present
13897 if Nkind (C) = N_Digits_Constraint then
13899 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
13900 Check_Restriction (No_Obsolescent_Features, C);
13902 if Warn_On_Obsolescent_Feature then
13903 Error_Msg_N
13904 ("subtype digits constraint is an " &
13905 "obsolescent feature (RM J.3(8))?j?", C);
13906 end if;
13908 D := Digits_Expression (C);
13909 Analyze_And_Resolve (D, Any_Integer);
13910 Check_Digits_Expression (D);
13911 Set_Digits_Value (Def_Id, Expr_Value (D));
13913 -- Check that digits value is in range. Obviously we can do this
13914 -- at compile time, but it is strictly a runtime check, and of
13915 -- course there is an ACVC test that checks this.
13917 if Digits_Value (Def_Id) > Digits_Value (T) then
13918 Error_Msg_Uint_1 := Digits_Value (T);
13919 Error_Msg_N ("??digits value is too large, maximum is ^", D);
13920 Rais :=
13921 Make_Raise_Constraint_Error (Sloc (D),
13922 Reason => CE_Range_Check_Failed);
13923 Insert_Action (Declaration_Node (Def_Id), Rais);
13924 end if;
13926 C := Range_Constraint (C);
13928 -- No digits constraint present
13930 else
13931 Set_Digits_Value (Def_Id, Digits_Value (T));
13932 end if;
13934 -- Range constraint present
13936 if Nkind (C) = N_Range_Constraint then
13937 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13939 -- No range constraint present
13941 else
13942 pragma Assert (No (C));
13943 Set_Scalar_Range (Def_Id, Scalar_Range (T));
13944 end if;
13946 Set_Is_Constrained (Def_Id);
13947 end Constrain_Float;
13949 ---------------------
13950 -- Constrain_Index --
13951 ---------------------
13953 procedure Constrain_Index
13954 (Index : Node_Id;
13955 S : Node_Id;
13956 Related_Nod : Node_Id;
13957 Related_Id : Entity_Id;
13958 Suffix : Character;
13959 Suffix_Index : Nat)
13961 Def_Id : Entity_Id;
13962 R : Node_Id := Empty;
13963 T : constant Entity_Id := Etype (Index);
13965 begin
13966 Def_Id :=
13967 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
13968 Set_Etype (Def_Id, Base_Type (T));
13970 if Nkind (S) = N_Range
13971 or else
13972 (Nkind (S) = N_Attribute_Reference
13973 and then Attribute_Name (S) = Name_Range)
13974 then
13975 -- A Range attribute will be transformed into N_Range by Resolve
13977 Analyze (S);
13978 Set_Etype (S, T);
13979 R := S;
13981 Process_Range_Expr_In_Decl (R, T);
13983 if not Error_Posted (S)
13984 and then
13985 (Nkind (S) /= N_Range
13986 or else not Covers (T, (Etype (Low_Bound (S))))
13987 or else not Covers (T, (Etype (High_Bound (S)))))
13988 then
13989 if Base_Type (T) /= Any_Type
13990 and then Etype (Low_Bound (S)) /= Any_Type
13991 and then Etype (High_Bound (S)) /= Any_Type
13992 then
13993 Error_Msg_N ("range expected", S);
13994 end if;
13995 end if;
13997 elsif Nkind (S) = N_Subtype_Indication then
13999 -- The parser has verified that this is a discrete indication
14001 Resolve_Discrete_Subtype_Indication (S, T);
14002 Bad_Predicated_Subtype_Use
14003 ("subtype& has predicate, not allowed in index constraint",
14004 S, Entity (Subtype_Mark (S)));
14006 R := Range_Expression (Constraint (S));
14008 -- Capture values of bounds and generate temporaries for them if
14009 -- needed, since checks may cause duplication of the expressions
14010 -- which must not be reevaluated.
14012 -- The forced evaluation removes side effects from expressions, which
14013 -- should occur also in GNATprove mode. Otherwise, we end up with
14014 -- unexpected insertions of actions at places where this is not
14015 -- supposed to occur, e.g. on default parameters of a call.
14017 if Expander_Active or GNATprove_Mode then
14018 Force_Evaluation
14019 (Low_Bound (R), Related_Id => Def_Id, Is_Low_Bound => True);
14020 Force_Evaluation
14021 (High_Bound (R), Related_Id => Def_Id, Is_High_Bound => True);
14022 end if;
14024 elsif Nkind (S) = N_Discriminant_Association then
14026 -- Syntactically valid in subtype indication
14028 Error_Msg_N ("invalid index constraint", S);
14029 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
14030 return;
14032 -- Subtype_Mark case, no anonymous subtypes to construct
14034 else
14035 Analyze (S);
14037 if Is_Entity_Name (S) then
14038 if not Is_Type (Entity (S)) then
14039 Error_Msg_N ("expect subtype mark for index constraint", S);
14041 elsif Base_Type (Entity (S)) /= Base_Type (T) then
14042 Wrong_Type (S, Base_Type (T));
14044 -- Check error of subtype with predicate in index constraint
14046 else
14047 Bad_Predicated_Subtype_Use
14048 ("subtype& has predicate, not allowed in index constraint",
14049 S, Entity (S));
14050 end if;
14052 return;
14054 else
14055 Error_Msg_N ("invalid index constraint", S);
14056 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
14057 return;
14058 end if;
14059 end if;
14061 -- Complete construction of the Itype
14063 if Is_Modular_Integer_Type (T) then
14064 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
14066 elsif Is_Integer_Type (T) then
14067 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
14069 else
14070 Set_Ekind (Def_Id, E_Enumeration_Subtype);
14071 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
14072 Set_First_Literal (Def_Id, First_Literal (T));
14073 end if;
14075 Set_Size_Info (Def_Id, (T));
14076 Set_RM_Size (Def_Id, RM_Size (T));
14077 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14079 Set_Scalar_Range (Def_Id, R);
14081 Set_Etype (S, Def_Id);
14082 Set_Discrete_RM_Size (Def_Id);
14083 end Constrain_Index;
14085 -----------------------
14086 -- Constrain_Integer --
14087 -----------------------
14089 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id) is
14090 T : constant Entity_Id := Entity (Subtype_Mark (S));
14091 C : constant Node_Id := Constraint (S);
14093 begin
14094 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14096 if Is_Modular_Integer_Type (T) then
14097 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
14098 else
14099 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
14100 end if;
14102 Set_Etype (Def_Id, Base_Type (T));
14103 Set_Size_Info (Def_Id, (T));
14104 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14105 Set_Discrete_RM_Size (Def_Id);
14106 end Constrain_Integer;
14108 ------------------------------
14109 -- Constrain_Ordinary_Fixed --
14110 ------------------------------
14112 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id) is
14113 T : constant Entity_Id := Entity (Subtype_Mark (S));
14114 C : Node_Id;
14115 D : Node_Id;
14116 Rais : Node_Id;
14118 begin
14119 Set_Ekind (Def_Id, E_Ordinary_Fixed_Point_Subtype);
14120 Set_Etype (Def_Id, Base_Type (T));
14121 Set_Size_Info (Def_Id, (T));
14122 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14123 Set_Small_Value (Def_Id, Small_Value (T));
14125 -- Process the constraint
14127 C := Constraint (S);
14129 -- Delta constraint present
14131 if Nkind (C) = N_Delta_Constraint then
14133 Check_SPARK_05_Restriction ("delta constraint is not allowed", S);
14134 Check_Restriction (No_Obsolescent_Features, C);
14136 if Warn_On_Obsolescent_Feature then
14137 Error_Msg_S
14138 ("subtype delta constraint is an " &
14139 "obsolescent feature (RM J.3(7))?j?");
14140 end if;
14142 D := Delta_Expression (C);
14143 Analyze_And_Resolve (D, Any_Real);
14144 Check_Delta_Expression (D);
14145 Set_Delta_Value (Def_Id, Expr_Value_R (D));
14147 -- Check that delta value is in range. Obviously we can do this
14148 -- at compile time, but it is strictly a runtime check, and of
14149 -- course there is an ACVC test that checks this.
14151 if Delta_Value (Def_Id) < Delta_Value (T) then
14152 Error_Msg_N ("??delta value is too small", D);
14153 Rais :=
14154 Make_Raise_Constraint_Error (Sloc (D),
14155 Reason => CE_Range_Check_Failed);
14156 Insert_Action (Declaration_Node (Def_Id), Rais);
14157 end if;
14159 C := Range_Constraint (C);
14161 -- No delta constraint present
14163 else
14164 Set_Delta_Value (Def_Id, Delta_Value (T));
14165 end if;
14167 -- Range constraint present
14169 if Nkind (C) = N_Range_Constraint then
14170 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14172 -- No range constraint present
14174 else
14175 pragma Assert (No (C));
14176 Set_Scalar_Range (Def_Id, Scalar_Range (T));
14177 end if;
14179 Set_Discrete_RM_Size (Def_Id);
14181 -- Unconditionally delay the freeze, since we cannot set size
14182 -- information in all cases correctly until the freeze point.
14184 Set_Has_Delayed_Freeze (Def_Id);
14185 end Constrain_Ordinary_Fixed;
14187 -----------------------
14188 -- Contain_Interface --
14189 -----------------------
14191 function Contain_Interface
14192 (Iface : Entity_Id;
14193 Ifaces : Elist_Id) return Boolean
14195 Iface_Elmt : Elmt_Id;
14197 begin
14198 if Present (Ifaces) then
14199 Iface_Elmt := First_Elmt (Ifaces);
14200 while Present (Iface_Elmt) loop
14201 if Node (Iface_Elmt) = Iface then
14202 return True;
14203 end if;
14205 Next_Elmt (Iface_Elmt);
14206 end loop;
14207 end if;
14209 return False;
14210 end Contain_Interface;
14212 ---------------------------
14213 -- Convert_Scalar_Bounds --
14214 ---------------------------
14216 procedure Convert_Scalar_Bounds
14217 (N : Node_Id;
14218 Parent_Type : Entity_Id;
14219 Derived_Type : Entity_Id;
14220 Loc : Source_Ptr)
14222 Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
14224 Lo : Node_Id;
14225 Hi : Node_Id;
14226 Rng : Node_Id;
14228 begin
14229 -- Defend against previous errors
14231 if No (Scalar_Range (Derived_Type)) then
14232 Check_Error_Detected;
14233 return;
14234 end if;
14236 Lo := Build_Scalar_Bound
14237 (Type_Low_Bound (Derived_Type),
14238 Parent_Type, Implicit_Base);
14240 Hi := Build_Scalar_Bound
14241 (Type_High_Bound (Derived_Type),
14242 Parent_Type, Implicit_Base);
14244 Rng :=
14245 Make_Range (Loc,
14246 Low_Bound => Lo,
14247 High_Bound => Hi);
14249 Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
14251 Set_Parent (Rng, N);
14252 Set_Scalar_Range (Derived_Type, Rng);
14254 -- Analyze the bounds
14256 Analyze_And_Resolve (Lo, Implicit_Base);
14257 Analyze_And_Resolve (Hi, Implicit_Base);
14259 -- Analyze the range itself, except that we do not analyze it if
14260 -- the bounds are real literals, and we have a fixed-point type.
14261 -- The reason for this is that we delay setting the bounds in this
14262 -- case till we know the final Small and Size values (see circuit
14263 -- in Freeze.Freeze_Fixed_Point_Type for further details).
14265 if Is_Fixed_Point_Type (Parent_Type)
14266 and then Nkind (Lo) = N_Real_Literal
14267 and then Nkind (Hi) = N_Real_Literal
14268 then
14269 return;
14271 -- Here we do the analysis of the range
14273 -- Note: we do this manually, since if we do a normal Analyze and
14274 -- Resolve call, there are problems with the conversions used for
14275 -- the derived type range.
14277 else
14278 Set_Etype (Rng, Implicit_Base);
14279 Set_Analyzed (Rng, True);
14280 end if;
14281 end Convert_Scalar_Bounds;
14283 -------------------
14284 -- Copy_And_Swap --
14285 -------------------
14287 procedure Copy_And_Swap (Priv, Full : Entity_Id) is
14288 begin
14289 -- Initialize new full declaration entity by copying the pertinent
14290 -- fields of the corresponding private declaration entity.
14292 -- We temporarily set Ekind to a value appropriate for a type to
14293 -- avoid assert failures in Einfo from checking for setting type
14294 -- attributes on something that is not a type. Ekind (Priv) is an
14295 -- appropriate choice, since it allowed the attributes to be set
14296 -- in the first place. This Ekind value will be modified later.
14298 Set_Ekind (Full, Ekind (Priv));
14300 -- Also set Etype temporarily to Any_Type, again, in the absence
14301 -- of errors, it will be properly reset, and if there are errors,
14302 -- then we want a value of Any_Type to remain.
14304 Set_Etype (Full, Any_Type);
14306 -- Now start copying attributes
14308 Set_Has_Discriminants (Full, Has_Discriminants (Priv));
14310 if Has_Discriminants (Full) then
14311 Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
14312 Set_Stored_Constraint (Full, Stored_Constraint (Priv));
14313 end if;
14315 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
14316 Set_Homonym (Full, Homonym (Priv));
14317 Set_Is_Immediately_Visible (Full, Is_Immediately_Visible (Priv));
14318 Set_Is_Public (Full, Is_Public (Priv));
14319 Set_Is_Pure (Full, Is_Pure (Priv));
14320 Set_Is_Tagged_Type (Full, Is_Tagged_Type (Priv));
14321 Set_Has_Pragma_Unmodified (Full, Has_Pragma_Unmodified (Priv));
14322 Set_Has_Pragma_Unreferenced (Full, Has_Pragma_Unreferenced (Priv));
14323 Set_Has_Pragma_Unreferenced_Objects
14324 (Full, Has_Pragma_Unreferenced_Objects
14325 (Priv));
14327 Conditional_Delay (Full, Priv);
14329 if Is_Tagged_Type (Full) then
14330 Set_Direct_Primitive_Operations
14331 (Full, Direct_Primitive_Operations (Priv));
14332 Set_No_Tagged_Streams_Pragma
14333 (Full, No_Tagged_Streams_Pragma (Priv));
14335 if Is_Base_Type (Priv) then
14336 Set_Class_Wide_Type (Full, Class_Wide_Type (Priv));
14337 end if;
14338 end if;
14340 Set_Is_Volatile (Full, Is_Volatile (Priv));
14341 Set_Treat_As_Volatile (Full, Treat_As_Volatile (Priv));
14342 Set_Scope (Full, Scope (Priv));
14343 Set_Next_Entity (Full, Next_Entity (Priv));
14344 Set_First_Entity (Full, First_Entity (Priv));
14345 Set_Last_Entity (Full, Last_Entity (Priv));
14347 -- If access types have been recorded for later handling, keep them in
14348 -- the full view so that they get handled when the full view freeze
14349 -- node is expanded.
14351 if Present (Freeze_Node (Priv))
14352 and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
14353 then
14354 Ensure_Freeze_Node (Full);
14355 Set_Access_Types_To_Process
14356 (Freeze_Node (Full),
14357 Access_Types_To_Process (Freeze_Node (Priv)));
14358 end if;
14360 -- Swap the two entities. Now Private is the full type entity and Full
14361 -- is the private one. They will be swapped back at the end of the
14362 -- private part. This swapping ensures that the entity that is visible
14363 -- in the private part is the full declaration.
14365 Exchange_Entities (Priv, Full);
14366 Append_Entity (Full, Scope (Full));
14367 end Copy_And_Swap;
14369 -------------------------------------
14370 -- Copy_Array_Base_Type_Attributes --
14371 -------------------------------------
14373 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
14374 begin
14375 Set_Component_Alignment (T1, Component_Alignment (T2));
14376 Set_Component_Type (T1, Component_Type (T2));
14377 Set_Component_Size (T1, Component_Size (T2));
14378 Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
14379 Set_Has_Non_Standard_Rep (T1, Has_Non_Standard_Rep (T2));
14380 Propagate_Concurrent_Flags (T1, T2);
14381 Set_Is_Packed (T1, Is_Packed (T2));
14382 Set_Has_Aliased_Components (T1, Has_Aliased_Components (T2));
14383 Set_Has_Atomic_Components (T1, Has_Atomic_Components (T2));
14384 Set_Has_Volatile_Components (T1, Has_Volatile_Components (T2));
14385 end Copy_Array_Base_Type_Attributes;
14387 -----------------------------------
14388 -- Copy_Array_Subtype_Attributes --
14389 -----------------------------------
14391 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
14392 begin
14393 Set_Size_Info (T1, T2);
14395 Set_First_Index (T1, First_Index (T2));
14396 Set_Is_Aliased (T1, Is_Aliased (T2));
14397 Set_Is_Volatile (T1, Is_Volatile (T2));
14398 Set_Treat_As_Volatile (T1, Treat_As_Volatile (T2));
14399 Set_Is_Constrained (T1, Is_Constrained (T2));
14400 Set_Depends_On_Private (T1, Has_Private_Component (T2));
14401 Inherit_Rep_Item_Chain (T1, T2);
14402 Set_Convention (T1, Convention (T2));
14403 Set_Is_Limited_Composite (T1, Is_Limited_Composite (T2));
14404 Set_Is_Private_Composite (T1, Is_Private_Composite (T2));
14405 Set_Packed_Array_Impl_Type (T1, Packed_Array_Impl_Type (T2));
14406 end Copy_Array_Subtype_Attributes;
14408 -----------------------------------
14409 -- Create_Constrained_Components --
14410 -----------------------------------
14412 procedure Create_Constrained_Components
14413 (Subt : Entity_Id;
14414 Decl_Node : Node_Id;
14415 Typ : Entity_Id;
14416 Constraints : Elist_Id)
14418 Loc : constant Source_Ptr := Sloc (Subt);
14419 Comp_List : constant Elist_Id := New_Elmt_List;
14420 Parent_Type : constant Entity_Id := Etype (Typ);
14421 Assoc_List : constant List_Id := New_List;
14422 Discr_Val : Elmt_Id;
14423 Errors : Boolean;
14424 New_C : Entity_Id;
14425 Old_C : Entity_Id;
14426 Is_Static : Boolean := True;
14428 procedure Collect_Fixed_Components (Typ : Entity_Id);
14429 -- Collect parent type components that do not appear in a variant part
14431 procedure Create_All_Components;
14432 -- Iterate over Comp_List to create the components of the subtype
14434 function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
14435 -- Creates a new component from Old_Compon, copying all the fields from
14436 -- it, including its Etype, inserts the new component in the Subt entity
14437 -- chain and returns the new component.
14439 function Is_Variant_Record (T : Entity_Id) return Boolean;
14440 -- If true, and discriminants are static, collect only components from
14441 -- variants selected by discriminant values.
14443 ------------------------------
14444 -- Collect_Fixed_Components --
14445 ------------------------------
14447 procedure Collect_Fixed_Components (Typ : Entity_Id) is
14448 begin
14449 -- Build association list for discriminants, and find components of the
14450 -- variant part selected by the values of the discriminants.
14452 Old_C := First_Discriminant (Typ);
14453 Discr_Val := First_Elmt (Constraints);
14454 while Present (Old_C) loop
14455 Append_To (Assoc_List,
14456 Make_Component_Association (Loc,
14457 Choices => New_List (New_Occurrence_Of (Old_C, Loc)),
14458 Expression => New_Copy (Node (Discr_Val))));
14460 Next_Elmt (Discr_Val);
14461 Next_Discriminant (Old_C);
14462 end loop;
14464 -- The tag and the possible parent component are unconditionally in
14465 -- the subtype.
14467 if Is_Tagged_Type (Typ) or else Has_Controlled_Component (Typ) then
14468 Old_C := First_Component (Typ);
14469 while Present (Old_C) loop
14470 if Nam_In (Chars (Old_C), Name_uTag, Name_uParent) then
14471 Append_Elmt (Old_C, Comp_List);
14472 end if;
14474 Next_Component (Old_C);
14475 end loop;
14476 end if;
14477 end Collect_Fixed_Components;
14479 ---------------------------
14480 -- Create_All_Components --
14481 ---------------------------
14483 procedure Create_All_Components is
14484 Comp : Elmt_Id;
14486 begin
14487 Comp := First_Elmt (Comp_List);
14488 while Present (Comp) loop
14489 Old_C := Node (Comp);
14490 New_C := Create_Component (Old_C);
14492 Set_Etype
14493 (New_C,
14494 Constrain_Component_Type
14495 (Old_C, Subt, Decl_Node, Typ, Constraints));
14496 Set_Is_Public (New_C, Is_Public (Subt));
14498 Next_Elmt (Comp);
14499 end loop;
14500 end Create_All_Components;
14502 ----------------------
14503 -- Create_Component --
14504 ----------------------
14506 function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
14507 New_Compon : constant Entity_Id := New_Copy (Old_Compon);
14509 begin
14510 if Ekind (Old_Compon) = E_Discriminant
14511 and then Is_Completely_Hidden (Old_Compon)
14512 then
14513 -- This is a shadow discriminant created for a discriminant of
14514 -- the parent type, which needs to be present in the subtype.
14515 -- Give the shadow discriminant an internal name that cannot
14516 -- conflict with that of visible components.
14518 Set_Chars (New_Compon, New_Internal_Name ('C'));
14519 end if;
14521 -- Set the parent so we have a proper link for freezing etc. This is
14522 -- not a real parent pointer, since of course our parent does not own
14523 -- up to us and reference us, we are an illegitimate child of the
14524 -- original parent.
14526 Set_Parent (New_Compon, Parent (Old_Compon));
14528 -- We do not want this node marked as Comes_From_Source, since
14529 -- otherwise it would get first class status and a separate cross-
14530 -- reference line would be generated. Illegitimate children do not
14531 -- rate such recognition.
14533 Set_Comes_From_Source (New_Compon, False);
14535 -- But it is a real entity, and a birth certificate must be properly
14536 -- registered by entering it into the entity list.
14538 Enter_Name (New_Compon);
14540 return New_Compon;
14541 end Create_Component;
14543 -----------------------
14544 -- Is_Variant_Record --
14545 -----------------------
14547 function Is_Variant_Record (T : Entity_Id) return Boolean is
14548 begin
14549 return Nkind (Parent (T)) = N_Full_Type_Declaration
14550 and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
14551 and then Present (Component_List (Type_Definition (Parent (T))))
14552 and then
14553 Present
14554 (Variant_Part (Component_List (Type_Definition (Parent (T)))));
14555 end Is_Variant_Record;
14557 -- Start of processing for Create_Constrained_Components
14559 begin
14560 pragma Assert (Subt /= Base_Type (Subt));
14561 pragma Assert (Typ = Base_Type (Typ));
14563 Set_First_Entity (Subt, Empty);
14564 Set_Last_Entity (Subt, Empty);
14566 -- Check whether constraint is fully static, in which case we can
14567 -- optimize the list of components.
14569 Discr_Val := First_Elmt (Constraints);
14570 while Present (Discr_Val) loop
14571 if not Is_OK_Static_Expression (Node (Discr_Val)) then
14572 Is_Static := False;
14573 exit;
14574 end if;
14576 Next_Elmt (Discr_Val);
14577 end loop;
14579 Set_Has_Static_Discriminants (Subt, Is_Static);
14581 Push_Scope (Subt);
14583 -- Inherit the discriminants of the parent type
14585 Add_Discriminants : declare
14586 Num_Disc : Nat;
14587 Num_Gird : Nat;
14589 begin
14590 Num_Disc := 0;
14591 Old_C := First_Discriminant (Typ);
14593 while Present (Old_C) loop
14594 Num_Disc := Num_Disc + 1;
14595 New_C := Create_Component (Old_C);
14596 Set_Is_Public (New_C, Is_Public (Subt));
14597 Next_Discriminant (Old_C);
14598 end loop;
14600 -- For an untagged derived subtype, the number of discriminants may
14601 -- be smaller than the number of inherited discriminants, because
14602 -- several of them may be renamed by a single new discriminant or
14603 -- constrained. In this case, add the hidden discriminants back into
14604 -- the subtype, because they need to be present if the optimizer of
14605 -- the GCC 4.x back-end decides to break apart assignments between
14606 -- objects using the parent view into member-wise assignments.
14608 Num_Gird := 0;
14610 if Is_Derived_Type (Typ)
14611 and then not Is_Tagged_Type (Typ)
14612 then
14613 Old_C := First_Stored_Discriminant (Typ);
14615 while Present (Old_C) loop
14616 Num_Gird := Num_Gird + 1;
14617 Next_Stored_Discriminant (Old_C);
14618 end loop;
14619 end if;
14621 if Num_Gird > Num_Disc then
14623 -- Find out multiple uses of new discriminants, and add hidden
14624 -- components for the extra renamed discriminants. We recognize
14625 -- multiple uses through the Corresponding_Discriminant of a
14626 -- new discriminant: if it constrains several old discriminants,
14627 -- this field points to the last one in the parent type. The
14628 -- stored discriminants of the derived type have the same name
14629 -- as those of the parent.
14631 declare
14632 Constr : Elmt_Id;
14633 New_Discr : Entity_Id;
14634 Old_Discr : Entity_Id;
14636 begin
14637 Constr := First_Elmt (Stored_Constraint (Typ));
14638 Old_Discr := First_Stored_Discriminant (Typ);
14639 while Present (Constr) loop
14640 if Is_Entity_Name (Node (Constr))
14641 and then Ekind (Entity (Node (Constr))) = E_Discriminant
14642 then
14643 New_Discr := Entity (Node (Constr));
14645 if Chars (Corresponding_Discriminant (New_Discr)) /=
14646 Chars (Old_Discr)
14647 then
14648 -- The new discriminant has been used to rename a
14649 -- subsequent old discriminant. Introduce a shadow
14650 -- component for the current old discriminant.
14652 New_C := Create_Component (Old_Discr);
14653 Set_Original_Record_Component (New_C, Old_Discr);
14654 end if;
14656 else
14657 -- The constraint has eliminated the old discriminant.
14658 -- Introduce a shadow component.
14660 New_C := Create_Component (Old_Discr);
14661 Set_Original_Record_Component (New_C, Old_Discr);
14662 end if;
14664 Next_Elmt (Constr);
14665 Next_Stored_Discriminant (Old_Discr);
14666 end loop;
14667 end;
14668 end if;
14669 end Add_Discriminants;
14671 if Is_Static
14672 and then Is_Variant_Record (Typ)
14673 then
14674 Collect_Fixed_Components (Typ);
14676 Gather_Components (
14677 Typ,
14678 Component_List (Type_Definition (Parent (Typ))),
14679 Governed_By => Assoc_List,
14680 Into => Comp_List,
14681 Report_Errors => Errors);
14682 pragma Assert (not Errors
14683 or else Serious_Errors_Detected > 0);
14685 Create_All_Components;
14687 -- If the subtype declaration is created for a tagged type derivation
14688 -- with constraints, we retrieve the record definition of the parent
14689 -- type to select the components of the proper variant.
14691 elsif Is_Static
14692 and then Is_Tagged_Type (Typ)
14693 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
14694 and then
14695 Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
14696 and then Is_Variant_Record (Parent_Type)
14697 then
14698 Collect_Fixed_Components (Typ);
14700 Gather_Components
14701 (Typ,
14702 Component_List (Type_Definition (Parent (Parent_Type))),
14703 Governed_By => Assoc_List,
14704 Into => Comp_List,
14705 Report_Errors => Errors);
14707 -- Note: previously there was a check at this point that no errors
14708 -- were detected. As a consequence of AI05-220 there may be an error
14709 -- if an inherited discriminant that controls a variant has a non-
14710 -- static constraint.
14712 -- If the tagged derivation has a type extension, collect all the
14713 -- new components therein.
14715 if Present (Record_Extension_Part (Type_Definition (Parent (Typ))))
14716 then
14717 Old_C := First_Component (Typ);
14718 while Present (Old_C) loop
14719 if Original_Record_Component (Old_C) = Old_C
14720 and then Chars (Old_C) /= Name_uTag
14721 and then Chars (Old_C) /= Name_uParent
14722 then
14723 Append_Elmt (Old_C, Comp_List);
14724 end if;
14726 Next_Component (Old_C);
14727 end loop;
14728 end if;
14730 Create_All_Components;
14732 else
14733 -- If discriminants are not static, or if this is a multi-level type
14734 -- extension, we have to include all components of the parent type.
14736 Old_C := First_Component (Typ);
14737 while Present (Old_C) loop
14738 New_C := Create_Component (Old_C);
14740 Set_Etype
14741 (New_C,
14742 Constrain_Component_Type
14743 (Old_C, Subt, Decl_Node, Typ, Constraints));
14744 Set_Is_Public (New_C, Is_Public (Subt));
14746 Next_Component (Old_C);
14747 end loop;
14748 end if;
14750 End_Scope;
14751 end Create_Constrained_Components;
14753 ------------------------------------------
14754 -- Decimal_Fixed_Point_Type_Declaration --
14755 ------------------------------------------
14757 procedure Decimal_Fixed_Point_Type_Declaration
14758 (T : Entity_Id;
14759 Def : Node_Id)
14761 Loc : constant Source_Ptr := Sloc (Def);
14762 Digs_Expr : constant Node_Id := Digits_Expression (Def);
14763 Delta_Expr : constant Node_Id := Delta_Expression (Def);
14764 Implicit_Base : Entity_Id;
14765 Digs_Val : Uint;
14766 Delta_Val : Ureal;
14767 Scale_Val : Uint;
14768 Bound_Val : Ureal;
14770 begin
14771 Check_SPARK_05_Restriction
14772 ("decimal fixed point type is not allowed", Def);
14773 Check_Restriction (No_Fixed_Point, Def);
14775 -- Create implicit base type
14777 Implicit_Base :=
14778 Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
14779 Set_Etype (Implicit_Base, Implicit_Base);
14781 -- Analyze and process delta expression
14783 Analyze_And_Resolve (Delta_Expr, Universal_Real);
14785 Check_Delta_Expression (Delta_Expr);
14786 Delta_Val := Expr_Value_R (Delta_Expr);
14788 -- Check delta is power of 10, and determine scale value from it
14790 declare
14791 Val : Ureal;
14793 begin
14794 Scale_Val := Uint_0;
14795 Val := Delta_Val;
14797 if Val < Ureal_1 then
14798 while Val < Ureal_1 loop
14799 Val := Val * Ureal_10;
14800 Scale_Val := Scale_Val + 1;
14801 end loop;
14803 if Scale_Val > 18 then
14804 Error_Msg_N ("scale exceeds maximum value of 18", Def);
14805 Scale_Val := UI_From_Int (+18);
14806 end if;
14808 else
14809 while Val > Ureal_1 loop
14810 Val := Val / Ureal_10;
14811 Scale_Val := Scale_Val - 1;
14812 end loop;
14814 if Scale_Val < -18 then
14815 Error_Msg_N ("scale is less than minimum value of -18", Def);
14816 Scale_Val := UI_From_Int (-18);
14817 end if;
14818 end if;
14820 if Val /= Ureal_1 then
14821 Error_Msg_N ("delta expression must be a power of 10", Def);
14822 Delta_Val := Ureal_10 ** (-Scale_Val);
14823 end if;
14824 end;
14826 -- Set delta, scale and small (small = delta for decimal type)
14828 Set_Delta_Value (Implicit_Base, Delta_Val);
14829 Set_Scale_Value (Implicit_Base, Scale_Val);
14830 Set_Small_Value (Implicit_Base, Delta_Val);
14832 -- Analyze and process digits expression
14834 Analyze_And_Resolve (Digs_Expr, Any_Integer);
14835 Check_Digits_Expression (Digs_Expr);
14836 Digs_Val := Expr_Value (Digs_Expr);
14838 if Digs_Val > 18 then
14839 Digs_Val := UI_From_Int (+18);
14840 Error_Msg_N ("digits value out of range, maximum is 18", Digs_Expr);
14841 end if;
14843 Set_Digits_Value (Implicit_Base, Digs_Val);
14844 Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
14846 -- Set range of base type from digits value for now. This will be
14847 -- expanded to represent the true underlying base range by Freeze.
14849 Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
14851 -- Note: We leave size as zero for now, size will be set at freeze
14852 -- time. We have to do this for ordinary fixed-point, because the size
14853 -- depends on the specified small, and we might as well do the same for
14854 -- decimal fixed-point.
14856 pragma Assert (Esize (Implicit_Base) = Uint_0);
14858 -- If there are bounds given in the declaration use them as the
14859 -- bounds of the first named subtype.
14861 if Present (Real_Range_Specification (Def)) then
14862 declare
14863 RRS : constant Node_Id := Real_Range_Specification (Def);
14864 Low : constant Node_Id := Low_Bound (RRS);
14865 High : constant Node_Id := High_Bound (RRS);
14866 Low_Val : Ureal;
14867 High_Val : Ureal;
14869 begin
14870 Analyze_And_Resolve (Low, Any_Real);
14871 Analyze_And_Resolve (High, Any_Real);
14872 Check_Real_Bound (Low);
14873 Check_Real_Bound (High);
14874 Low_Val := Expr_Value_R (Low);
14875 High_Val := Expr_Value_R (High);
14877 if Low_Val < (-Bound_Val) then
14878 Error_Msg_N
14879 ("range low bound too small for digits value", Low);
14880 Low_Val := -Bound_Val;
14881 end if;
14883 if High_Val > Bound_Val then
14884 Error_Msg_N
14885 ("range high bound too large for digits value", High);
14886 High_Val := Bound_Val;
14887 end if;
14889 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
14890 end;
14892 -- If no explicit range, use range that corresponds to given
14893 -- digits value. This will end up as the final range for the
14894 -- first subtype.
14896 else
14897 Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
14898 end if;
14900 -- Complete entity for first subtype. The inheritance of the rep item
14901 -- chain ensures that SPARK-related pragmas are not clobbered when the
14902 -- decimal fixed point type acts as a full view of a private type.
14904 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
14905 Set_Etype (T, Implicit_Base);
14906 Set_Size_Info (T, Implicit_Base);
14907 Inherit_Rep_Item_Chain (T, Implicit_Base);
14908 Set_Digits_Value (T, Digs_Val);
14909 Set_Delta_Value (T, Delta_Val);
14910 Set_Small_Value (T, Delta_Val);
14911 Set_Scale_Value (T, Scale_Val);
14912 Set_Is_Constrained (T);
14913 end Decimal_Fixed_Point_Type_Declaration;
14915 -----------------------------------
14916 -- Derive_Progenitor_Subprograms --
14917 -----------------------------------
14919 procedure Derive_Progenitor_Subprograms
14920 (Parent_Type : Entity_Id;
14921 Tagged_Type : Entity_Id)
14923 E : Entity_Id;
14924 Elmt : Elmt_Id;
14925 Iface : Entity_Id;
14926 Iface_Elmt : Elmt_Id;
14927 Iface_Subp : Entity_Id;
14928 New_Subp : Entity_Id := Empty;
14929 Prim_Elmt : Elmt_Id;
14930 Subp : Entity_Id;
14931 Typ : Entity_Id;
14933 begin
14934 pragma Assert (Ada_Version >= Ada_2005
14935 and then Is_Record_Type (Tagged_Type)
14936 and then Is_Tagged_Type (Tagged_Type)
14937 and then Has_Interfaces (Tagged_Type));
14939 -- Step 1: Transfer to the full-view primitives associated with the
14940 -- partial-view that cover interface primitives. Conceptually this
14941 -- work should be done later by Process_Full_View; done here to
14942 -- simplify its implementation at later stages. It can be safely
14943 -- done here because interfaces must be visible in the partial and
14944 -- private view (RM 7.3(7.3/2)).
14946 -- Small optimization: This work is only required if the parent may
14947 -- have entities whose Alias attribute reference an interface primitive.
14948 -- Such a situation may occur if the parent is an abstract type and the
14949 -- primitive has not been yet overridden or if the parent is a generic
14950 -- formal type covering interfaces.
14952 -- If the tagged type is not abstract, it cannot have abstract
14953 -- primitives (the only entities in the list of primitives of
14954 -- non-abstract tagged types that can reference abstract primitives
14955 -- through its Alias attribute are the internal entities that have
14956 -- attribute Interface_Alias, and these entities are generated later
14957 -- by Add_Internal_Interface_Entities).
14959 if In_Private_Part (Current_Scope)
14960 and then (Is_Abstract_Type (Parent_Type)
14961 or else
14962 Is_Generic_Type (Parent_Type))
14963 then
14964 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
14965 while Present (Elmt) loop
14966 Subp := Node (Elmt);
14968 -- At this stage it is not possible to have entities in the list
14969 -- of primitives that have attribute Interface_Alias.
14971 pragma Assert (No (Interface_Alias (Subp)));
14973 Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
14975 if Is_Interface (Typ) then
14976 E := Find_Primitive_Covering_Interface
14977 (Tagged_Type => Tagged_Type,
14978 Iface_Prim => Subp);
14980 if Present (E)
14981 and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
14982 then
14983 Replace_Elmt (Elmt, E);
14984 Remove_Homonym (Subp);
14985 end if;
14986 end if;
14988 Next_Elmt (Elmt);
14989 end loop;
14990 end if;
14992 -- Step 2: Add primitives of progenitors that are not implemented by
14993 -- parents of Tagged_Type.
14995 if Present (Interfaces (Base_Type (Tagged_Type))) then
14996 Iface_Elmt := First_Elmt (Interfaces (Base_Type (Tagged_Type)));
14997 while Present (Iface_Elmt) loop
14998 Iface := Node (Iface_Elmt);
15000 Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
15001 while Present (Prim_Elmt) loop
15002 Iface_Subp := Node (Prim_Elmt);
15004 -- Exclude derivation of predefined primitives except those
15005 -- that come from source, or are inherited from one that comes
15006 -- from source. Required to catch declarations of equality
15007 -- operators of interfaces. For example:
15009 -- type Iface is interface;
15010 -- function "=" (Left, Right : Iface) return Boolean;
15012 if not Is_Predefined_Dispatching_Operation (Iface_Subp)
15013 or else Comes_From_Source (Ultimate_Alias (Iface_Subp))
15014 then
15015 E := Find_Primitive_Covering_Interface
15016 (Tagged_Type => Tagged_Type,
15017 Iface_Prim => Iface_Subp);
15019 -- If not found we derive a new primitive leaving its alias
15020 -- attribute referencing the interface primitive.
15022 if No (E) then
15023 Derive_Subprogram
15024 (New_Subp, Iface_Subp, Tagged_Type, Iface);
15026 -- Ada 2012 (AI05-0197): If the covering primitive's name
15027 -- differs from the name of the interface primitive then it
15028 -- is a private primitive inherited from a parent type. In
15029 -- such case, given that Tagged_Type covers the interface,
15030 -- the inherited private primitive becomes visible. For such
15031 -- purpose we add a new entity that renames the inherited
15032 -- private primitive.
15034 elsif Chars (E) /= Chars (Iface_Subp) then
15035 pragma Assert (Has_Suffix (E, 'P'));
15036 Derive_Subprogram
15037 (New_Subp, Iface_Subp, Tagged_Type, Iface);
15038 Set_Alias (New_Subp, E);
15039 Set_Is_Abstract_Subprogram (New_Subp,
15040 Is_Abstract_Subprogram (E));
15042 -- Propagate to the full view interface entities associated
15043 -- with the partial view.
15045 elsif In_Private_Part (Current_Scope)
15046 and then Present (Alias (E))
15047 and then Alias (E) = Iface_Subp
15048 and then
15049 List_Containing (Parent (E)) /=
15050 Private_Declarations
15051 (Specification
15052 (Unit_Declaration_Node (Current_Scope)))
15053 then
15054 Append_Elmt (E, Primitive_Operations (Tagged_Type));
15055 end if;
15056 end if;
15058 Next_Elmt (Prim_Elmt);
15059 end loop;
15061 Next_Elmt (Iface_Elmt);
15062 end loop;
15063 end if;
15064 end Derive_Progenitor_Subprograms;
15066 -----------------------
15067 -- Derive_Subprogram --
15068 -----------------------
15070 procedure Derive_Subprogram
15071 (New_Subp : out Entity_Id;
15072 Parent_Subp : Entity_Id;
15073 Derived_Type : Entity_Id;
15074 Parent_Type : Entity_Id;
15075 Actual_Subp : Entity_Id := Empty)
15077 Formal : Entity_Id;
15078 -- Formal parameter of parent primitive operation
15080 Formal_Of_Actual : Entity_Id;
15081 -- Formal parameter of actual operation, when the derivation is to
15082 -- create a renaming for a primitive operation of an actual in an
15083 -- instantiation.
15085 New_Formal : Entity_Id;
15086 -- Formal of inherited operation
15088 Visible_Subp : Entity_Id := Parent_Subp;
15090 function Is_Private_Overriding return Boolean;
15091 -- If Subp is a private overriding of a visible operation, the inherited
15092 -- operation derives from the overridden op (even though its body is the
15093 -- overriding one) and the inherited operation is visible now. See
15094 -- sem_disp to see the full details of the handling of the overridden
15095 -- subprogram, which is removed from the list of primitive operations of
15096 -- the type. The overridden subprogram is saved locally in Visible_Subp,
15097 -- and used to diagnose abstract operations that need overriding in the
15098 -- derived type.
15100 procedure Replace_Type (Id, New_Id : Entity_Id);
15101 -- When the type is an anonymous access type, create a new access type
15102 -- designating the derived type.
15104 procedure Set_Derived_Name;
15105 -- This procedure sets the appropriate Chars name for New_Subp. This
15106 -- is normally just a copy of the parent name. An exception arises for
15107 -- type support subprograms, where the name is changed to reflect the
15108 -- name of the derived type, e.g. if type foo is derived from type bar,
15109 -- then a procedure barDA is derived with a name fooDA.
15111 ---------------------------
15112 -- Is_Private_Overriding --
15113 ---------------------------
15115 function Is_Private_Overriding return Boolean is
15116 Prev : Entity_Id;
15118 begin
15119 -- If the parent is not a dispatching operation there is no
15120 -- need to investigate overridings
15122 if not Is_Dispatching_Operation (Parent_Subp) then
15123 return False;
15124 end if;
15126 -- The visible operation that is overridden is a homonym of the
15127 -- parent subprogram. We scan the homonym chain to find the one
15128 -- whose alias is the subprogram we are deriving.
15130 Prev := Current_Entity (Parent_Subp);
15131 while Present (Prev) loop
15132 if Ekind (Prev) = Ekind (Parent_Subp)
15133 and then Alias (Prev) = Parent_Subp
15134 and then Scope (Parent_Subp) = Scope (Prev)
15135 and then not Is_Hidden (Prev)
15136 then
15137 Visible_Subp := Prev;
15138 return True;
15139 end if;
15141 Prev := Homonym (Prev);
15142 end loop;
15144 return False;
15145 end Is_Private_Overriding;
15147 ------------------
15148 -- Replace_Type --
15149 ------------------
15151 procedure Replace_Type (Id, New_Id : Entity_Id) is
15152 Id_Type : constant Entity_Id := Etype (Id);
15153 Acc_Type : Entity_Id;
15154 Par : constant Node_Id := Parent (Derived_Type);
15156 begin
15157 -- When the type is an anonymous access type, create a new access
15158 -- type designating the derived type. This itype must be elaborated
15159 -- at the point of the derivation, not on subsequent calls that may
15160 -- be out of the proper scope for Gigi, so we insert a reference to
15161 -- it after the derivation.
15163 if Ekind (Id_Type) = E_Anonymous_Access_Type then
15164 declare
15165 Desig_Typ : Entity_Id := Designated_Type (Id_Type);
15167 begin
15168 if Ekind (Desig_Typ) = E_Record_Type_With_Private
15169 and then Present (Full_View (Desig_Typ))
15170 and then not Is_Private_Type (Parent_Type)
15171 then
15172 Desig_Typ := Full_View (Desig_Typ);
15173 end if;
15175 if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
15177 -- Ada 2005 (AI-251): Handle also derivations of abstract
15178 -- interface primitives.
15180 or else (Is_Interface (Desig_Typ)
15181 and then not Is_Class_Wide_Type (Desig_Typ))
15182 then
15183 Acc_Type := New_Copy (Id_Type);
15184 Set_Etype (Acc_Type, Acc_Type);
15185 Set_Scope (Acc_Type, New_Subp);
15187 -- Set size of anonymous access type. If we have an access
15188 -- to an unconstrained array, this is a fat pointer, so it
15189 -- is sizes at twice addtress size.
15191 if Is_Array_Type (Desig_Typ)
15192 and then not Is_Constrained (Desig_Typ)
15193 then
15194 Init_Size (Acc_Type, 2 * System_Address_Size);
15196 -- Other cases use a thin pointer
15198 else
15199 Init_Size (Acc_Type, System_Address_Size);
15200 end if;
15202 -- Set remaining characterstics of anonymous access type
15204 Init_Alignment (Acc_Type);
15205 Set_Directly_Designated_Type (Acc_Type, Derived_Type);
15207 Set_Etype (New_Id, Acc_Type);
15208 Set_Scope (New_Id, New_Subp);
15210 -- Create a reference to it
15212 Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
15214 else
15215 Set_Etype (New_Id, Id_Type);
15216 end if;
15217 end;
15219 -- In Ada2012, a formal may have an incomplete type but the type
15220 -- derivation that inherits the primitive follows the full view.
15222 elsif Base_Type (Id_Type) = Base_Type (Parent_Type)
15223 or else
15224 (Ekind (Id_Type) = E_Record_Type_With_Private
15225 and then Present (Full_View (Id_Type))
15226 and then
15227 Base_Type (Full_View (Id_Type)) = Base_Type (Parent_Type))
15228 or else
15229 (Ada_Version >= Ada_2012
15230 and then Ekind (Id_Type) = E_Incomplete_Type
15231 and then Full_View (Id_Type) = Parent_Type)
15232 then
15233 -- Constraint checks on formals are generated during expansion,
15234 -- based on the signature of the original subprogram. The bounds
15235 -- of the derived type are not relevant, and thus we can use
15236 -- the base type for the formals. However, the return type may be
15237 -- used in a context that requires that the proper static bounds
15238 -- be used (a case statement, for example) and for those cases
15239 -- we must use the derived type (first subtype), not its base.
15241 -- If the derived_type_definition has no constraints, we know that
15242 -- the derived type has the same constraints as the first subtype
15243 -- of the parent, and we can also use it rather than its base,
15244 -- which can lead to more efficient code.
15246 if Etype (Id) = Parent_Type then
15247 if Is_Scalar_Type (Parent_Type)
15248 and then
15249 Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
15250 then
15251 Set_Etype (New_Id, Derived_Type);
15253 elsif Nkind (Par) = N_Full_Type_Declaration
15254 and then
15255 Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
15256 and then
15257 Is_Entity_Name
15258 (Subtype_Indication (Type_Definition (Par)))
15259 then
15260 Set_Etype (New_Id, Derived_Type);
15262 else
15263 Set_Etype (New_Id, Base_Type (Derived_Type));
15264 end if;
15266 else
15267 Set_Etype (New_Id, Base_Type (Derived_Type));
15268 end if;
15270 else
15271 Set_Etype (New_Id, Etype (Id));
15272 end if;
15273 end Replace_Type;
15275 ----------------------
15276 -- Set_Derived_Name --
15277 ----------------------
15279 procedure Set_Derived_Name is
15280 Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
15281 begin
15282 if Nm = TSS_Null then
15283 Set_Chars (New_Subp, Chars (Parent_Subp));
15284 else
15285 Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
15286 end if;
15287 end Set_Derived_Name;
15289 -- Start of processing for Derive_Subprogram
15291 begin
15292 New_Subp := New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
15293 Set_Ekind (New_Subp, Ekind (Parent_Subp));
15295 -- Check whether the inherited subprogram is a private operation that
15296 -- should be inherited but not yet made visible. Such subprograms can
15297 -- become visible at a later point (e.g., the private part of a public
15298 -- child unit) via Declare_Inherited_Private_Subprograms. If the
15299 -- following predicate is true, then this is not such a private
15300 -- operation and the subprogram simply inherits the name of the parent
15301 -- subprogram. Note the special check for the names of controlled
15302 -- operations, which are currently exempted from being inherited with
15303 -- a hidden name because they must be findable for generation of
15304 -- implicit run-time calls.
15306 if not Is_Hidden (Parent_Subp)
15307 or else Is_Internal (Parent_Subp)
15308 or else Is_Private_Overriding
15309 or else Is_Internal_Name (Chars (Parent_Subp))
15310 or else (Is_Controlled (Parent_Type)
15311 and then Nam_In (Chars (Parent_Subp), Name_Adjust,
15312 Name_Finalize,
15313 Name_Initialize))
15314 then
15315 Set_Derived_Name;
15317 -- An inherited dispatching equality will be overridden by an internally
15318 -- generated one, or by an explicit one, so preserve its name and thus
15319 -- its entry in the dispatch table. Otherwise, if Parent_Subp is a
15320 -- private operation it may become invisible if the full view has
15321 -- progenitors, and the dispatch table will be malformed.
15322 -- We check that the type is limited to handle the anomalous declaration
15323 -- of Limited_Controlled, which is derived from a non-limited type, and
15324 -- which is handled specially elsewhere as well.
15326 elsif Chars (Parent_Subp) = Name_Op_Eq
15327 and then Is_Dispatching_Operation (Parent_Subp)
15328 and then Etype (Parent_Subp) = Standard_Boolean
15329 and then not Is_Limited_Type (Etype (First_Formal (Parent_Subp)))
15330 and then
15331 Etype (First_Formal (Parent_Subp)) =
15332 Etype (Next_Formal (First_Formal (Parent_Subp)))
15333 then
15334 Set_Derived_Name;
15336 -- If parent is hidden, this can be a regular derivation if the
15337 -- parent is immediately visible in a non-instantiating context,
15338 -- or if we are in the private part of an instance. This test
15339 -- should still be refined ???
15341 -- The test for In_Instance_Not_Visible avoids inheriting the derived
15342 -- operation as a non-visible operation in cases where the parent
15343 -- subprogram might not be visible now, but was visible within the
15344 -- original generic, so it would be wrong to make the inherited
15345 -- subprogram non-visible now. (Not clear if this test is fully
15346 -- correct; are there any cases where we should declare the inherited
15347 -- operation as not visible to avoid it being overridden, e.g., when
15348 -- the parent type is a generic actual with private primitives ???)
15350 -- (they should be treated the same as other private inherited
15351 -- subprograms, but it's not clear how to do this cleanly). ???
15353 elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
15354 and then Is_Immediately_Visible (Parent_Subp)
15355 and then not In_Instance)
15356 or else In_Instance_Not_Visible
15357 then
15358 Set_Derived_Name;
15360 -- Ada 2005 (AI-251): Regular derivation if the parent subprogram
15361 -- overrides an interface primitive because interface primitives
15362 -- must be visible in the partial view of the parent (RM 7.3 (7.3/2))
15364 elsif Ada_Version >= Ada_2005
15365 and then Is_Dispatching_Operation (Parent_Subp)
15366 and then Present (Covered_Interface_Op (Parent_Subp))
15367 then
15368 Set_Derived_Name;
15370 -- Otherwise, the type is inheriting a private operation, so enter it
15371 -- with a special name so it can't be overridden.
15373 else
15374 Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
15375 end if;
15377 Set_Parent (New_Subp, Parent (Derived_Type));
15379 if Present (Actual_Subp) then
15380 Replace_Type (Actual_Subp, New_Subp);
15381 else
15382 Replace_Type (Parent_Subp, New_Subp);
15383 end if;
15385 Conditional_Delay (New_Subp, Parent_Subp);
15387 -- If we are creating a renaming for a primitive operation of an
15388 -- actual of a generic derived type, we must examine the signature
15389 -- of the actual primitive, not that of the generic formal, which for
15390 -- example may be an interface. However the name and initial value
15391 -- of the inherited operation are those of the formal primitive.
15393 Formal := First_Formal (Parent_Subp);
15395 if Present (Actual_Subp) then
15396 Formal_Of_Actual := First_Formal (Actual_Subp);
15397 else
15398 Formal_Of_Actual := Empty;
15399 end if;
15401 while Present (Formal) loop
15402 New_Formal := New_Copy (Formal);
15404 -- Normally we do not go copying parents, but in the case of
15405 -- formals, we need to link up to the declaration (which is the
15406 -- parameter specification), and it is fine to link up to the
15407 -- original formal's parameter specification in this case.
15409 Set_Parent (New_Formal, Parent (Formal));
15410 Append_Entity (New_Formal, New_Subp);
15412 if Present (Formal_Of_Actual) then
15413 Replace_Type (Formal_Of_Actual, New_Formal);
15414 Next_Formal (Formal_Of_Actual);
15415 else
15416 Replace_Type (Formal, New_Formal);
15417 end if;
15419 Next_Formal (Formal);
15420 end loop;
15422 -- If this derivation corresponds to a tagged generic actual, then
15423 -- primitive operations rename those of the actual. Otherwise the
15424 -- primitive operations rename those of the parent type, If the parent
15425 -- renames an intrinsic operator, so does the new subprogram. We except
15426 -- concatenation, which is always properly typed, and does not get
15427 -- expanded as other intrinsic operations.
15429 if No (Actual_Subp) then
15430 if Is_Intrinsic_Subprogram (Parent_Subp) then
15431 Set_Is_Intrinsic_Subprogram (New_Subp);
15433 if Present (Alias (Parent_Subp))
15434 and then Chars (Parent_Subp) /= Name_Op_Concat
15435 then
15436 Set_Alias (New_Subp, Alias (Parent_Subp));
15437 else
15438 Set_Alias (New_Subp, Parent_Subp);
15439 end if;
15441 else
15442 Set_Alias (New_Subp, Parent_Subp);
15443 end if;
15445 else
15446 Set_Alias (New_Subp, Actual_Subp);
15447 end if;
15449 -- Derived subprograms of a tagged type must inherit the convention
15450 -- of the parent subprogram (a requirement of AI-117). Derived
15451 -- subprograms of untagged types simply get convention Ada by default.
15453 -- If the derived type is a tagged generic formal type with unknown
15454 -- discriminants, its convention is intrinsic (RM 6.3.1 (8)).
15456 -- However, if the type is derived from a generic formal, the further
15457 -- inherited subprogram has the convention of the non-generic ancestor.
15458 -- Otherwise there would be no way to override the operation.
15459 -- (This is subject to forthcoming ARG discussions).
15461 if Is_Tagged_Type (Derived_Type) then
15462 if Is_Generic_Type (Derived_Type)
15463 and then Has_Unknown_Discriminants (Derived_Type)
15464 then
15465 Set_Convention (New_Subp, Convention_Intrinsic);
15467 else
15468 if Is_Generic_Type (Parent_Type)
15469 and then Has_Unknown_Discriminants (Parent_Type)
15470 then
15471 Set_Convention (New_Subp, Convention (Alias (Parent_Subp)));
15472 else
15473 Set_Convention (New_Subp, Convention (Parent_Subp));
15474 end if;
15475 end if;
15476 end if;
15478 -- Predefined controlled operations retain their name even if the parent
15479 -- is hidden (see above), but they are not primitive operations if the
15480 -- ancestor is not visible, for example if the parent is a private
15481 -- extension completed with a controlled extension. Note that a full
15482 -- type that is controlled can break privacy: the flag Is_Controlled is
15483 -- set on both views of the type.
15485 if Is_Controlled (Parent_Type)
15486 and then Nam_In (Chars (Parent_Subp), Name_Initialize,
15487 Name_Adjust,
15488 Name_Finalize)
15489 and then Is_Hidden (Parent_Subp)
15490 and then not Is_Visibly_Controlled (Parent_Type)
15491 then
15492 Set_Is_Hidden (New_Subp);
15493 end if;
15495 Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
15496 Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
15498 if Ekind (Parent_Subp) = E_Procedure then
15499 Set_Is_Valued_Procedure
15500 (New_Subp, Is_Valued_Procedure (Parent_Subp));
15501 else
15502 Set_Has_Controlling_Result
15503 (New_Subp, Has_Controlling_Result (Parent_Subp));
15504 end if;
15506 -- No_Return must be inherited properly. If this is overridden in the
15507 -- case of a dispatching operation, then a check is made in Sem_Disp
15508 -- that the overriding operation is also No_Return (no such check is
15509 -- required for the case of non-dispatching operation.
15511 Set_No_Return (New_Subp, No_Return (Parent_Subp));
15513 -- A derived function with a controlling result is abstract. If the
15514 -- Derived_Type is a nonabstract formal generic derived type, then
15515 -- inherited operations are not abstract: the required check is done at
15516 -- instantiation time. If the derivation is for a generic actual, the
15517 -- function is not abstract unless the actual is.
15519 if Is_Generic_Type (Derived_Type)
15520 and then not Is_Abstract_Type (Derived_Type)
15521 then
15522 null;
15524 -- Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
15525 -- properties of the subprogram, as defined in RM-3.9.3(4/2-6/2).
15527 -- A subprogram subject to pragma Extensions_Visible with value False
15528 -- requires overriding if the subprogram has at least one controlling
15529 -- OUT parameter (SPARK RM 6.1.7(6)).
15531 elsif Ada_Version >= Ada_2005
15532 and then (Is_Abstract_Subprogram (Alias (New_Subp))
15533 or else (Is_Tagged_Type (Derived_Type)
15534 and then Etype (New_Subp) = Derived_Type
15535 and then not Is_Null_Extension (Derived_Type))
15536 or else (Is_Tagged_Type (Derived_Type)
15537 and then Ekind (Etype (New_Subp)) =
15538 E_Anonymous_Access_Type
15539 and then Designated_Type (Etype (New_Subp)) =
15540 Derived_Type
15541 and then not Is_Null_Extension (Derived_Type))
15542 or else (Comes_From_Source (Alias (New_Subp))
15543 and then Is_EVF_Procedure (Alias (New_Subp))))
15544 and then No (Actual_Subp)
15545 then
15546 if not Is_Tagged_Type (Derived_Type)
15547 or else Is_Abstract_Type (Derived_Type)
15548 or else Is_Abstract_Subprogram (Alias (New_Subp))
15549 then
15550 Set_Is_Abstract_Subprogram (New_Subp);
15551 else
15552 Set_Requires_Overriding (New_Subp);
15553 end if;
15555 elsif Ada_Version < Ada_2005
15556 and then (Is_Abstract_Subprogram (Alias (New_Subp))
15557 or else (Is_Tagged_Type (Derived_Type)
15558 and then Etype (New_Subp) = Derived_Type
15559 and then No (Actual_Subp)))
15560 then
15561 Set_Is_Abstract_Subprogram (New_Subp);
15563 -- AI05-0097 : an inherited operation that dispatches on result is
15564 -- abstract if the derived type is abstract, even if the parent type
15565 -- is concrete and the derived type is a null extension.
15567 elsif Has_Controlling_Result (Alias (New_Subp))
15568 and then Is_Abstract_Type (Etype (New_Subp))
15569 then
15570 Set_Is_Abstract_Subprogram (New_Subp);
15572 -- Finally, if the parent type is abstract we must verify that all
15573 -- inherited operations are either non-abstract or overridden, or that
15574 -- the derived type itself is abstract (this check is performed at the
15575 -- end of a package declaration, in Check_Abstract_Overriding). A
15576 -- private overriding in the parent type will not be visible in the
15577 -- derivation if we are not in an inner package or in a child unit of
15578 -- the parent type, in which case the abstractness of the inherited
15579 -- operation is carried to the new subprogram.
15581 elsif Is_Abstract_Type (Parent_Type)
15582 and then not In_Open_Scopes (Scope (Parent_Type))
15583 and then Is_Private_Overriding
15584 and then Is_Abstract_Subprogram (Visible_Subp)
15585 then
15586 if No (Actual_Subp) then
15587 Set_Alias (New_Subp, Visible_Subp);
15588 Set_Is_Abstract_Subprogram (New_Subp, True);
15590 else
15591 -- If this is a derivation for an instance of a formal derived
15592 -- type, abstractness comes from the primitive operation of the
15593 -- actual, not from the operation inherited from the ancestor.
15595 Set_Is_Abstract_Subprogram
15596 (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
15597 end if;
15598 end if;
15600 New_Overloaded_Entity (New_Subp, Derived_Type);
15602 -- Ada RM 6.1.1 (15): If a subprogram inherits nonconforming class-wide
15603 -- preconditions and the derived type is abstract, the derived operation
15604 -- is abstract as well if parent subprogram is not abstract or null.
15606 if Is_Abstract_Type (Derived_Type)
15607 and then Has_Non_Trivial_Precondition (Parent_Subp)
15608 and then Present (Interfaces (Derived_Type))
15609 then
15611 -- Add useful attributes of subprogram before the freeze point,
15612 -- in case freezing is delayed or there are previous errors.
15614 Set_Is_Dispatching_Operation (New_Subp);
15616 declare
15617 Iface_Prim : constant Entity_Id := Covered_Interface_Op (New_Subp);
15619 begin
15620 if Present (Iface_Prim)
15621 and then Has_Non_Trivial_Precondition (Iface_Prim)
15622 then
15623 Set_Is_Abstract_Subprogram (New_Subp);
15624 end if;
15625 end;
15626 end if;
15628 -- Check for case of a derived subprogram for the instantiation of a
15629 -- formal derived tagged type, if so mark the subprogram as dispatching
15630 -- and inherit the dispatching attributes of the actual subprogram. The
15631 -- derived subprogram is effectively renaming of the actual subprogram,
15632 -- so it needs to have the same attributes as the actual.
15634 if Present (Actual_Subp)
15635 and then Is_Dispatching_Operation (Actual_Subp)
15636 then
15637 Set_Is_Dispatching_Operation (New_Subp);
15639 if Present (DTC_Entity (Actual_Subp)) then
15640 Set_DTC_Entity (New_Subp, DTC_Entity (Actual_Subp));
15641 Set_DT_Position_Value (New_Subp, DT_Position (Actual_Subp));
15642 end if;
15643 end if;
15645 -- Indicate that a derived subprogram does not require a body and that
15646 -- it does not require processing of default expressions.
15648 Set_Has_Completion (New_Subp);
15649 Set_Default_Expressions_Processed (New_Subp);
15651 if Ekind (New_Subp) = E_Function then
15652 Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
15653 end if;
15654 end Derive_Subprogram;
15656 ------------------------
15657 -- Derive_Subprograms --
15658 ------------------------
15660 procedure Derive_Subprograms
15661 (Parent_Type : Entity_Id;
15662 Derived_Type : Entity_Id;
15663 Generic_Actual : Entity_Id := Empty)
15665 Op_List : constant Elist_Id :=
15666 Collect_Primitive_Operations (Parent_Type);
15668 function Check_Derived_Type return Boolean;
15669 -- Check that all the entities derived from Parent_Type are found in
15670 -- the list of primitives of Derived_Type exactly in the same order.
15672 procedure Derive_Interface_Subprogram
15673 (New_Subp : out Entity_Id;
15674 Subp : Entity_Id;
15675 Actual_Subp : Entity_Id);
15676 -- Derive New_Subp from the ultimate alias of the parent subprogram Subp
15677 -- (which is an interface primitive). If Generic_Actual is present then
15678 -- Actual_Subp is the actual subprogram corresponding with the generic
15679 -- subprogram Subp.
15681 ------------------------
15682 -- Check_Derived_Type --
15683 ------------------------
15685 function Check_Derived_Type return Boolean is
15686 E : Entity_Id;
15687 Elmt : Elmt_Id;
15688 List : Elist_Id;
15689 New_Subp : Entity_Id;
15690 Op_Elmt : Elmt_Id;
15691 Subp : Entity_Id;
15693 begin
15694 -- Traverse list of entities in the current scope searching for
15695 -- an incomplete type whose full-view is derived type.
15697 E := First_Entity (Scope (Derived_Type));
15698 while Present (E) and then E /= Derived_Type loop
15699 if Ekind (E) = E_Incomplete_Type
15700 and then Present (Full_View (E))
15701 and then Full_View (E) = Derived_Type
15702 then
15703 -- Disable this test if Derived_Type completes an incomplete
15704 -- type because in such case more primitives can be added
15705 -- later to the list of primitives of Derived_Type by routine
15706 -- Process_Incomplete_Dependents
15708 return True;
15709 end if;
15711 E := Next_Entity (E);
15712 end loop;
15714 List := Collect_Primitive_Operations (Derived_Type);
15715 Elmt := First_Elmt (List);
15717 Op_Elmt := First_Elmt (Op_List);
15718 while Present (Op_Elmt) loop
15719 Subp := Node (Op_Elmt);
15720 New_Subp := Node (Elmt);
15722 -- At this early stage Derived_Type has no entities with attribute
15723 -- Interface_Alias. In addition, such primitives are always
15724 -- located at the end of the list of primitives of Parent_Type.
15725 -- Therefore, if found we can safely stop processing pending
15726 -- entities.
15728 exit when Present (Interface_Alias (Subp));
15730 -- Handle hidden entities
15732 if not Is_Predefined_Dispatching_Operation (Subp)
15733 and then Is_Hidden (Subp)
15734 then
15735 if Present (New_Subp)
15736 and then Primitive_Names_Match (Subp, New_Subp)
15737 then
15738 Next_Elmt (Elmt);
15739 end if;
15741 else
15742 if not Present (New_Subp)
15743 or else Ekind (Subp) /= Ekind (New_Subp)
15744 or else not Primitive_Names_Match (Subp, New_Subp)
15745 then
15746 return False;
15747 end if;
15749 Next_Elmt (Elmt);
15750 end if;
15752 Next_Elmt (Op_Elmt);
15753 end loop;
15755 return True;
15756 end Check_Derived_Type;
15758 ---------------------------------
15759 -- Derive_Interface_Subprogram --
15760 ---------------------------------
15762 procedure Derive_Interface_Subprogram
15763 (New_Subp : out Entity_Id;
15764 Subp : Entity_Id;
15765 Actual_Subp : Entity_Id)
15767 Iface_Subp : constant Entity_Id := Ultimate_Alias (Subp);
15768 Iface_Type : constant Entity_Id := Find_Dispatching_Type (Iface_Subp);
15770 begin
15771 pragma Assert (Is_Interface (Iface_Type));
15773 Derive_Subprogram
15774 (New_Subp => New_Subp,
15775 Parent_Subp => Iface_Subp,
15776 Derived_Type => Derived_Type,
15777 Parent_Type => Iface_Type,
15778 Actual_Subp => Actual_Subp);
15780 -- Given that this new interface entity corresponds with a primitive
15781 -- of the parent that was not overridden we must leave it associated
15782 -- with its parent primitive to ensure that it will share the same
15783 -- dispatch table slot when overridden. We must set the Alias to Subp
15784 -- (instead of Iface_Subp), and we must fix Is_Abstract_Subprogram
15785 -- (in case we inherited Subp from Iface_Type via a nonabstract
15786 -- generic formal type).
15788 if No (Actual_Subp) then
15789 Set_Alias (New_Subp, Subp);
15791 declare
15792 T : Entity_Id := Find_Dispatching_Type (Subp);
15793 begin
15794 while Etype (T) /= T loop
15795 if Is_Generic_Type (T) and then not Is_Abstract_Type (T) then
15796 Set_Is_Abstract_Subprogram (New_Subp, False);
15797 exit;
15798 end if;
15800 T := Etype (T);
15801 end loop;
15802 end;
15804 -- For instantiations this is not needed since the previous call to
15805 -- Derive_Subprogram leaves the entity well decorated.
15807 else
15808 pragma Assert (Alias (New_Subp) = Actual_Subp);
15809 null;
15810 end if;
15811 end Derive_Interface_Subprogram;
15813 -- Local variables
15815 Alias_Subp : Entity_Id;
15816 Act_List : Elist_Id;
15817 Act_Elmt : Elmt_Id;
15818 Act_Subp : Entity_Id := Empty;
15819 Elmt : Elmt_Id;
15820 Need_Search : Boolean := False;
15821 New_Subp : Entity_Id := Empty;
15822 Parent_Base : Entity_Id;
15823 Subp : Entity_Id;
15825 -- Start of processing for Derive_Subprograms
15827 begin
15828 if Ekind (Parent_Type) = E_Record_Type_With_Private
15829 and then Has_Discriminants (Parent_Type)
15830 and then Present (Full_View (Parent_Type))
15831 then
15832 Parent_Base := Full_View (Parent_Type);
15833 else
15834 Parent_Base := Parent_Type;
15835 end if;
15837 if Present (Generic_Actual) then
15838 Act_List := Collect_Primitive_Operations (Generic_Actual);
15839 Act_Elmt := First_Elmt (Act_List);
15840 else
15841 Act_List := No_Elist;
15842 Act_Elmt := No_Elmt;
15843 end if;
15845 -- Derive primitives inherited from the parent. Note that if the generic
15846 -- actual is present, this is not really a type derivation, it is a
15847 -- completion within an instance.
15849 -- Case 1: Derived_Type does not implement interfaces
15851 if not Is_Tagged_Type (Derived_Type)
15852 or else (not Has_Interfaces (Derived_Type)
15853 and then not (Present (Generic_Actual)
15854 and then Has_Interfaces (Generic_Actual)))
15855 then
15856 Elmt := First_Elmt (Op_List);
15857 while Present (Elmt) loop
15858 Subp := Node (Elmt);
15860 -- Literals are derived earlier in the process of building the
15861 -- derived type, and are skipped here.
15863 if Ekind (Subp) = E_Enumeration_Literal then
15864 null;
15866 -- The actual is a direct descendant and the common primitive
15867 -- operations appear in the same order.
15869 -- If the generic parent type is present, the derived type is an
15870 -- instance of a formal derived type, and within the instance its
15871 -- operations are those of the actual. We derive from the formal
15872 -- type but make the inherited operations aliases of the
15873 -- corresponding operations of the actual.
15875 else
15876 pragma Assert (No (Node (Act_Elmt))
15877 or else (Primitive_Names_Match (Subp, Node (Act_Elmt))
15878 and then
15879 Type_Conformant
15880 (Subp, Node (Act_Elmt),
15881 Skip_Controlling_Formals => True)));
15883 Derive_Subprogram
15884 (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
15886 if Present (Act_Elmt) then
15887 Next_Elmt (Act_Elmt);
15888 end if;
15889 end if;
15891 Next_Elmt (Elmt);
15892 end loop;
15894 -- Case 2: Derived_Type implements interfaces
15896 else
15897 -- If the parent type has no predefined primitives we remove
15898 -- predefined primitives from the list of primitives of generic
15899 -- actual to simplify the complexity of this algorithm.
15901 if Present (Generic_Actual) then
15902 declare
15903 Has_Predefined_Primitives : Boolean := False;
15905 begin
15906 -- Check if the parent type has predefined primitives
15908 Elmt := First_Elmt (Op_List);
15909 while Present (Elmt) loop
15910 Subp := Node (Elmt);
15912 if Is_Predefined_Dispatching_Operation (Subp)
15913 and then not Comes_From_Source (Ultimate_Alias (Subp))
15914 then
15915 Has_Predefined_Primitives := True;
15916 exit;
15917 end if;
15919 Next_Elmt (Elmt);
15920 end loop;
15922 -- Remove predefined primitives of Generic_Actual. We must use
15923 -- an auxiliary list because in case of tagged types the value
15924 -- returned by Collect_Primitive_Operations is the value stored
15925 -- in its Primitive_Operations attribute (and we don't want to
15926 -- modify its current contents).
15928 if not Has_Predefined_Primitives then
15929 declare
15930 Aux_List : constant Elist_Id := New_Elmt_List;
15932 begin
15933 Elmt := First_Elmt (Act_List);
15934 while Present (Elmt) loop
15935 Subp := Node (Elmt);
15937 if not Is_Predefined_Dispatching_Operation (Subp)
15938 or else Comes_From_Source (Subp)
15939 then
15940 Append_Elmt (Subp, Aux_List);
15941 end if;
15943 Next_Elmt (Elmt);
15944 end loop;
15946 Act_List := Aux_List;
15947 end;
15948 end if;
15950 Act_Elmt := First_Elmt (Act_List);
15951 Act_Subp := Node (Act_Elmt);
15952 end;
15953 end if;
15955 -- Stage 1: If the generic actual is not present we derive the
15956 -- primitives inherited from the parent type. If the generic parent
15957 -- type is present, the derived type is an instance of a formal
15958 -- derived type, and within the instance its operations are those of
15959 -- the actual. We derive from the formal type but make the inherited
15960 -- operations aliases of the corresponding operations of the actual.
15962 Elmt := First_Elmt (Op_List);
15963 while Present (Elmt) loop
15964 Subp := Node (Elmt);
15965 Alias_Subp := Ultimate_Alias (Subp);
15967 -- Do not derive internal entities of the parent that link
15968 -- interface primitives with their covering primitive. These
15969 -- entities will be added to this type when frozen.
15971 if Present (Interface_Alias (Subp)) then
15972 goto Continue;
15973 end if;
15975 -- If the generic actual is present find the corresponding
15976 -- operation in the generic actual. If the parent type is a
15977 -- direct ancestor of the derived type then, even if it is an
15978 -- interface, the operations are inherited from the primary
15979 -- dispatch table and are in the proper order. If we detect here
15980 -- that primitives are not in the same order we traverse the list
15981 -- of primitive operations of the actual to find the one that
15982 -- implements the interface primitive.
15984 if Need_Search
15985 or else
15986 (Present (Generic_Actual)
15987 and then Present (Act_Subp)
15988 and then not
15989 (Primitive_Names_Match (Subp, Act_Subp)
15990 and then
15991 Type_Conformant (Subp, Act_Subp,
15992 Skip_Controlling_Formals => True)))
15993 then
15994 pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual,
15995 Use_Full_View => True));
15997 -- Remember that we need searching for all pending primitives
15999 Need_Search := True;
16001 -- Handle entities associated with interface primitives
16003 if Present (Alias_Subp)
16004 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
16005 and then not Is_Predefined_Dispatching_Operation (Subp)
16006 then
16007 -- Search for the primitive in the homonym chain
16009 Act_Subp :=
16010 Find_Primitive_Covering_Interface
16011 (Tagged_Type => Generic_Actual,
16012 Iface_Prim => Alias_Subp);
16014 -- Previous search may not locate primitives covering
16015 -- interfaces defined in generics units or instantiations.
16016 -- (it fails if the covering primitive has formals whose
16017 -- type is also defined in generics or instantiations).
16018 -- In such case we search in the list of primitives of the
16019 -- generic actual for the internal entity that links the
16020 -- interface primitive and the covering primitive.
16022 if No (Act_Subp)
16023 and then Is_Generic_Type (Parent_Type)
16024 then
16025 -- This code has been designed to handle only generic
16026 -- formals that implement interfaces that are defined
16027 -- in a generic unit or instantiation. If this code is
16028 -- needed for other cases we must review it because
16029 -- (given that it relies on Original_Location to locate
16030 -- the primitive of Generic_Actual that covers the
16031 -- interface) it could leave linked through attribute
16032 -- Alias entities of unrelated instantiations).
16034 pragma Assert
16035 (Is_Generic_Unit
16036 (Scope (Find_Dispatching_Type (Alias_Subp)))
16037 or else
16038 Instantiation_Depth
16039 (Sloc (Find_Dispatching_Type (Alias_Subp))) > 0);
16041 declare
16042 Iface_Prim_Loc : constant Source_Ptr :=
16043 Original_Location (Sloc (Alias_Subp));
16045 Elmt : Elmt_Id;
16046 Prim : Entity_Id;
16048 begin
16049 Elmt :=
16050 First_Elmt (Primitive_Operations (Generic_Actual));
16052 Search : while Present (Elmt) loop
16053 Prim := Node (Elmt);
16055 if Present (Interface_Alias (Prim))
16056 and then Original_Location
16057 (Sloc (Interface_Alias (Prim))) =
16058 Iface_Prim_Loc
16059 then
16060 Act_Subp := Alias (Prim);
16061 exit Search;
16062 end if;
16064 Next_Elmt (Elmt);
16065 end loop Search;
16066 end;
16067 end if;
16069 pragma Assert (Present (Act_Subp)
16070 or else Is_Abstract_Type (Generic_Actual)
16071 or else Serious_Errors_Detected > 0);
16073 -- Handle predefined primitives plus the rest of user-defined
16074 -- primitives
16076 else
16077 Act_Elmt := First_Elmt (Act_List);
16078 while Present (Act_Elmt) loop
16079 Act_Subp := Node (Act_Elmt);
16081 exit when Primitive_Names_Match (Subp, Act_Subp)
16082 and then Type_Conformant
16083 (Subp, Act_Subp,
16084 Skip_Controlling_Formals => True)
16085 and then No (Interface_Alias (Act_Subp));
16087 Next_Elmt (Act_Elmt);
16088 end loop;
16090 if No (Act_Elmt) then
16091 Act_Subp := Empty;
16092 end if;
16093 end if;
16094 end if;
16096 -- Case 1: If the parent is a limited interface then it has the
16097 -- predefined primitives of synchronized interfaces. However, the
16098 -- actual type may be a non-limited type and hence it does not
16099 -- have such primitives.
16101 if Present (Generic_Actual)
16102 and then not Present (Act_Subp)
16103 and then Is_Limited_Interface (Parent_Base)
16104 and then Is_Predefined_Interface_Primitive (Subp)
16105 then
16106 null;
16108 -- Case 2: Inherit entities associated with interfaces that were
16109 -- not covered by the parent type. We exclude here null interface
16110 -- primitives because they do not need special management.
16112 -- We also exclude interface operations that are renamings. If the
16113 -- subprogram is an explicit renaming of an interface primitive,
16114 -- it is a regular primitive operation, and the presence of its
16115 -- alias is not relevant: it has to be derived like any other
16116 -- primitive.
16118 elsif Present (Alias (Subp))
16119 and then Nkind (Unit_Declaration_Node (Subp)) /=
16120 N_Subprogram_Renaming_Declaration
16121 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
16122 and then not
16123 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
16124 and then Null_Present (Parent (Alias_Subp)))
16125 then
16126 -- If this is an abstract private type then we transfer the
16127 -- derivation of the interface primitive from the partial view
16128 -- to the full view. This is safe because all the interfaces
16129 -- must be visible in the partial view. Done to avoid adding
16130 -- a new interface derivation to the private part of the
16131 -- enclosing package; otherwise this new derivation would be
16132 -- decorated as hidden when the analysis of the enclosing
16133 -- package completes.
16135 if Is_Abstract_Type (Derived_Type)
16136 and then In_Private_Part (Current_Scope)
16137 and then Has_Private_Declaration (Derived_Type)
16138 then
16139 declare
16140 Partial_View : Entity_Id;
16141 Elmt : Elmt_Id;
16142 Ent : Entity_Id;
16144 begin
16145 Partial_View := First_Entity (Current_Scope);
16146 loop
16147 exit when No (Partial_View)
16148 or else (Has_Private_Declaration (Partial_View)
16149 and then
16150 Full_View (Partial_View) = Derived_Type);
16152 Next_Entity (Partial_View);
16153 end loop;
16155 -- If the partial view was not found then the source code
16156 -- has errors and the derivation is not needed.
16158 if Present (Partial_View) then
16159 Elmt :=
16160 First_Elmt (Primitive_Operations (Partial_View));
16161 while Present (Elmt) loop
16162 Ent := Node (Elmt);
16164 if Present (Alias (Ent))
16165 and then Ultimate_Alias (Ent) = Alias (Subp)
16166 then
16167 Append_Elmt
16168 (Ent, Primitive_Operations (Derived_Type));
16169 exit;
16170 end if;
16172 Next_Elmt (Elmt);
16173 end loop;
16175 -- If the interface primitive was not found in the
16176 -- partial view then this interface primitive was
16177 -- overridden. We add a derivation to activate in
16178 -- Derive_Progenitor_Subprograms the machinery to
16179 -- search for it.
16181 if No (Elmt) then
16182 Derive_Interface_Subprogram
16183 (New_Subp => New_Subp,
16184 Subp => Subp,
16185 Actual_Subp => Act_Subp);
16186 end if;
16187 end if;
16188 end;
16189 else
16190 Derive_Interface_Subprogram
16191 (New_Subp => New_Subp,
16192 Subp => Subp,
16193 Actual_Subp => Act_Subp);
16194 end if;
16196 -- Case 3: Common derivation
16198 else
16199 Derive_Subprogram
16200 (New_Subp => New_Subp,
16201 Parent_Subp => Subp,
16202 Derived_Type => Derived_Type,
16203 Parent_Type => Parent_Base,
16204 Actual_Subp => Act_Subp);
16205 end if;
16207 -- No need to update Act_Elm if we must search for the
16208 -- corresponding operation in the generic actual
16210 if not Need_Search
16211 and then Present (Act_Elmt)
16212 then
16213 Next_Elmt (Act_Elmt);
16214 Act_Subp := Node (Act_Elmt);
16215 end if;
16217 <<Continue>>
16218 Next_Elmt (Elmt);
16219 end loop;
16221 -- Inherit additional operations from progenitors. If the derived
16222 -- type is a generic actual, there are not new primitive operations
16223 -- for the type because it has those of the actual, and therefore
16224 -- nothing needs to be done. The renamings generated above are not
16225 -- primitive operations, and their purpose is simply to make the
16226 -- proper operations visible within an instantiation.
16228 if No (Generic_Actual) then
16229 Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
16230 end if;
16231 end if;
16233 -- Final check: Direct descendants must have their primitives in the
16234 -- same order. We exclude from this test untagged types and instances
16235 -- of formal derived types. We skip this test if we have already
16236 -- reported serious errors in the sources.
16238 pragma Assert (not Is_Tagged_Type (Derived_Type)
16239 or else Present (Generic_Actual)
16240 or else Serious_Errors_Detected > 0
16241 or else Check_Derived_Type);
16242 end Derive_Subprograms;
16244 --------------------------------
16245 -- Derived_Standard_Character --
16246 --------------------------------
16248 procedure Derived_Standard_Character
16249 (N : Node_Id;
16250 Parent_Type : Entity_Id;
16251 Derived_Type : Entity_Id)
16253 Loc : constant Source_Ptr := Sloc (N);
16254 Def : constant Node_Id := Type_Definition (N);
16255 Indic : constant Node_Id := Subtype_Indication (Def);
16256 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
16257 Implicit_Base : constant Entity_Id :=
16258 Create_Itype
16259 (E_Enumeration_Type, N, Derived_Type, 'B');
16261 Lo : Node_Id;
16262 Hi : Node_Id;
16264 begin
16265 Discard_Node (Process_Subtype (Indic, N));
16267 Set_Etype (Implicit_Base, Parent_Base);
16268 Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
16269 Set_RM_Size (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
16271 Set_Is_Character_Type (Implicit_Base, True);
16272 Set_Has_Delayed_Freeze (Implicit_Base);
16274 -- The bounds of the implicit base are the bounds of the parent base.
16275 -- Note that their type is the parent base.
16277 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
16278 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
16280 Set_Scalar_Range (Implicit_Base,
16281 Make_Range (Loc,
16282 Low_Bound => Lo,
16283 High_Bound => Hi));
16285 Conditional_Delay (Derived_Type, Parent_Type);
16287 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
16288 Set_Etype (Derived_Type, Implicit_Base);
16289 Set_Size_Info (Derived_Type, Parent_Type);
16291 if Unknown_RM_Size (Derived_Type) then
16292 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
16293 end if;
16295 Set_Is_Character_Type (Derived_Type, True);
16297 if Nkind (Indic) /= N_Subtype_Indication then
16299 -- If no explicit constraint, the bounds are those
16300 -- of the parent type.
16302 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Type));
16303 Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
16304 Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
16305 end if;
16307 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
16309 -- Because the implicit base is used in the conversion of the bounds, we
16310 -- have to freeze it now. This is similar to what is done for numeric
16311 -- types, and it equally suspicious, but otherwise a non-static bound
16312 -- will have a reference to an unfrozen type, which is rejected by Gigi
16313 -- (???). This requires specific care for definition of stream
16314 -- attributes. For details, see comments at the end of
16315 -- Build_Derived_Numeric_Type.
16317 Freeze_Before (N, Implicit_Base);
16318 end Derived_Standard_Character;
16320 ------------------------------
16321 -- Derived_Type_Declaration --
16322 ------------------------------
16324 procedure Derived_Type_Declaration
16325 (T : Entity_Id;
16326 N : Node_Id;
16327 Is_Completion : Boolean)
16329 Parent_Type : Entity_Id;
16331 function Comes_From_Generic (Typ : Entity_Id) return Boolean;
16332 -- Check whether the parent type is a generic formal, or derives
16333 -- directly or indirectly from one.
16335 ------------------------
16336 -- Comes_From_Generic --
16337 ------------------------
16339 function Comes_From_Generic (Typ : Entity_Id) return Boolean is
16340 begin
16341 if Is_Generic_Type (Typ) then
16342 return True;
16344 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
16345 return True;
16347 elsif Is_Private_Type (Typ)
16348 and then Present (Full_View (Typ))
16349 and then Is_Generic_Type (Root_Type (Full_View (Typ)))
16350 then
16351 return True;
16353 elsif Is_Generic_Actual_Type (Typ) then
16354 return True;
16356 else
16357 return False;
16358 end if;
16359 end Comes_From_Generic;
16361 -- Local variables
16363 Def : constant Node_Id := Type_Definition (N);
16364 Iface_Def : Node_Id;
16365 Indic : constant Node_Id := Subtype_Indication (Def);
16366 Extension : constant Node_Id := Record_Extension_Part (Def);
16367 Parent_Node : Node_Id;
16368 Taggd : Boolean;
16370 -- Start of processing for Derived_Type_Declaration
16372 begin
16373 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
16375 if SPARK_Mode = On
16376 and then Is_Tagged_Type (Parent_Type)
16377 then
16378 declare
16379 Partial_View : constant Entity_Id :=
16380 Incomplete_Or_Partial_View (Parent_Type);
16382 begin
16383 -- If the partial view was not found then the parent type is not
16384 -- a private type. Otherwise check if the partial view is a tagged
16385 -- private type.
16387 if Present (Partial_View)
16388 and then Is_Private_Type (Partial_View)
16389 and then not Is_Tagged_Type (Partial_View)
16390 then
16391 Error_Msg_NE
16392 ("cannot derive from & declared as untagged private "
16393 & "(SPARK RM 3.4(1))", N, Partial_View);
16394 end if;
16395 end;
16396 end if;
16398 -- Ada 2005 (AI-251): In case of interface derivation check that the
16399 -- parent is also an interface.
16401 if Interface_Present (Def) then
16402 Check_SPARK_05_Restriction ("interface is not allowed", Def);
16404 if not Is_Interface (Parent_Type) then
16405 Diagnose_Interface (Indic, Parent_Type);
16407 else
16408 Parent_Node := Parent (Base_Type (Parent_Type));
16409 Iface_Def := Type_Definition (Parent_Node);
16411 -- Ada 2005 (AI-251): Limited interfaces can only inherit from
16412 -- other limited interfaces.
16414 if Limited_Present (Def) then
16415 if Limited_Present (Iface_Def) then
16416 null;
16418 elsif Protected_Present (Iface_Def) then
16419 Error_Msg_NE
16420 ("descendant of & must be declared as a protected "
16421 & "interface", N, Parent_Type);
16423 elsif Synchronized_Present (Iface_Def) then
16424 Error_Msg_NE
16425 ("descendant of & must be declared as a synchronized "
16426 & "interface", N, Parent_Type);
16428 elsif Task_Present (Iface_Def) then
16429 Error_Msg_NE
16430 ("descendant of & must be declared as a task interface",
16431 N, Parent_Type);
16433 else
16434 Error_Msg_N
16435 ("(Ada 2005) limited interface cannot inherit from "
16436 & "non-limited interface", Indic);
16437 end if;
16439 -- Ada 2005 (AI-345): Non-limited interfaces can only inherit
16440 -- from non-limited or limited interfaces.
16442 elsif not Protected_Present (Def)
16443 and then not Synchronized_Present (Def)
16444 and then not Task_Present (Def)
16445 then
16446 if Limited_Present (Iface_Def) then
16447 null;
16449 elsif Protected_Present (Iface_Def) then
16450 Error_Msg_NE
16451 ("descendant of & must be declared as a protected "
16452 & "interface", N, Parent_Type);
16454 elsif Synchronized_Present (Iface_Def) then
16455 Error_Msg_NE
16456 ("descendant of & must be declared as a synchronized "
16457 & "interface", N, Parent_Type);
16459 elsif Task_Present (Iface_Def) then
16460 Error_Msg_NE
16461 ("descendant of & must be declared as a task interface",
16462 N, Parent_Type);
16463 else
16464 null;
16465 end if;
16466 end if;
16467 end if;
16468 end if;
16470 if Is_Tagged_Type (Parent_Type)
16471 and then Is_Concurrent_Type (Parent_Type)
16472 and then not Is_Interface (Parent_Type)
16473 then
16474 Error_Msg_N
16475 ("parent type of a record extension cannot be a synchronized "
16476 & "tagged type (RM 3.9.1 (3/1))", N);
16477 Set_Etype (T, Any_Type);
16478 return;
16479 end if;
16481 -- Ada 2005 (AI-251): Decorate all the names in the list of ancestor
16482 -- interfaces
16484 if Is_Tagged_Type (Parent_Type)
16485 and then Is_Non_Empty_List (Interface_List (Def))
16486 then
16487 declare
16488 Intf : Node_Id;
16489 T : Entity_Id;
16491 begin
16492 Intf := First (Interface_List (Def));
16493 while Present (Intf) loop
16494 T := Find_Type_Of_Subtype_Indic (Intf);
16496 if not Is_Interface (T) then
16497 Diagnose_Interface (Intf, T);
16499 -- Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
16500 -- a limited type from having a nonlimited progenitor.
16502 elsif (Limited_Present (Def)
16503 or else (not Is_Interface (Parent_Type)
16504 and then Is_Limited_Type (Parent_Type)))
16505 and then not Is_Limited_Interface (T)
16506 then
16507 Error_Msg_NE
16508 ("progenitor interface& of limited type must be limited",
16509 N, T);
16510 end if;
16512 Next (Intf);
16513 end loop;
16514 end;
16515 end if;
16517 if Parent_Type = Any_Type
16518 or else Etype (Parent_Type) = Any_Type
16519 or else (Is_Class_Wide_Type (Parent_Type)
16520 and then Etype (Parent_Type) = T)
16521 then
16522 -- If Parent_Type is undefined or illegal, make new type into a
16523 -- subtype of Any_Type, and set a few attributes to prevent cascaded
16524 -- errors. If this is a self-definition, emit error now.
16526 if T = Parent_Type or else T = Etype (Parent_Type) then
16527 Error_Msg_N ("type cannot be used in its own definition", Indic);
16528 end if;
16530 Set_Ekind (T, Ekind (Parent_Type));
16531 Set_Etype (T, Any_Type);
16532 Set_Scalar_Range (T, Scalar_Range (Any_Type));
16534 if Is_Tagged_Type (T)
16535 and then Is_Record_Type (T)
16536 then
16537 Set_Direct_Primitive_Operations (T, New_Elmt_List);
16538 end if;
16540 return;
16541 end if;
16543 -- Ada 2005 (AI-251): The case in which the parent of the full-view is
16544 -- an interface is special because the list of interfaces in the full
16545 -- view can be given in any order. For example:
16547 -- type A is interface;
16548 -- type B is interface and A;
16549 -- type D is new B with private;
16550 -- private
16551 -- type D is new A and B with null record; -- 1 --
16553 -- In this case we perform the following transformation of -1-:
16555 -- type D is new B and A with null record;
16557 -- If the parent of the full-view covers the parent of the partial-view
16558 -- we have two possible cases:
16560 -- 1) They have the same parent
16561 -- 2) The parent of the full-view implements some further interfaces
16563 -- In both cases we do not need to perform the transformation. In the
16564 -- first case the source program is correct and the transformation is
16565 -- not needed; in the second case the source program does not fulfill
16566 -- the no-hidden interfaces rule (AI-396) and the error will be reported
16567 -- later.
16569 -- This transformation not only simplifies the rest of the analysis of
16570 -- this type declaration but also simplifies the correct generation of
16571 -- the object layout to the expander.
16573 if In_Private_Part (Current_Scope)
16574 and then Is_Interface (Parent_Type)
16575 then
16576 declare
16577 Iface : Node_Id;
16578 Partial_View : Entity_Id;
16579 Partial_View_Parent : Entity_Id;
16580 New_Iface : Node_Id;
16582 begin
16583 -- Look for the associated private type declaration
16585 Partial_View := Incomplete_Or_Partial_View (T);
16587 -- If the partial view was not found then the source code has
16588 -- errors and the transformation is not needed.
16590 if Present (Partial_View) then
16591 Partial_View_Parent := Etype (Partial_View);
16593 -- If the parent of the full-view covers the parent of the
16594 -- partial-view we have nothing else to do.
16596 if Interface_Present_In_Ancestor
16597 (Parent_Type, Partial_View_Parent)
16598 then
16599 null;
16601 -- Traverse the list of interfaces of the full-view to look
16602 -- for the parent of the partial-view and perform the tree
16603 -- transformation.
16605 else
16606 Iface := First (Interface_List (Def));
16607 while Present (Iface) loop
16608 if Etype (Iface) = Etype (Partial_View) then
16609 Rewrite (Subtype_Indication (Def),
16610 New_Copy (Subtype_Indication
16611 (Parent (Partial_View))));
16613 New_Iface :=
16614 Make_Identifier (Sloc (N), Chars (Parent_Type));
16615 Append (New_Iface, Interface_List (Def));
16617 -- Analyze the transformed code
16619 Derived_Type_Declaration (T, N, Is_Completion);
16620 return;
16621 end if;
16623 Next (Iface);
16624 end loop;
16625 end if;
16626 end if;
16627 end;
16628 end if;
16630 -- Only composite types other than array types are allowed to have
16631 -- discriminants.
16633 if Present (Discriminant_Specifications (N)) then
16634 if (Is_Elementary_Type (Parent_Type)
16635 or else
16636 Is_Array_Type (Parent_Type))
16637 and then not Error_Posted (N)
16638 then
16639 Error_Msg_N
16640 ("elementary or array type cannot have discriminants",
16641 Defining_Identifier (First (Discriminant_Specifications (N))));
16642 Set_Has_Discriminants (T, False);
16644 -- The type is allowed to have discriminants
16646 else
16647 Check_SPARK_05_Restriction ("discriminant type is not allowed", N);
16648 end if;
16649 end if;
16651 -- In Ada 83, a derived type defined in a package specification cannot
16652 -- be used for further derivation until the end of its visible part.
16653 -- Note that derivation in the private part of the package is allowed.
16655 if Ada_Version = Ada_83
16656 and then Is_Derived_Type (Parent_Type)
16657 and then In_Visible_Part (Scope (Parent_Type))
16658 then
16659 if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
16660 Error_Msg_N
16661 ("(Ada 83): premature use of type for derivation", Indic);
16662 end if;
16663 end if;
16665 -- Check for early use of incomplete or private type
16667 if Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
16668 Error_Msg_N ("premature derivation of incomplete type", Indic);
16669 return;
16671 elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
16672 and then not Comes_From_Generic (Parent_Type))
16673 or else Has_Private_Component (Parent_Type)
16674 then
16675 -- The ancestor type of a formal type can be incomplete, in which
16676 -- case only the operations of the partial view are available in the
16677 -- generic. Subsequent checks may be required when the full view is
16678 -- analyzed to verify that a derivation from a tagged type has an
16679 -- extension.
16681 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
16682 null;
16684 elsif No (Underlying_Type (Parent_Type))
16685 or else Has_Private_Component (Parent_Type)
16686 then
16687 Error_Msg_N
16688 ("premature derivation of derived or private type", Indic);
16690 -- Flag the type itself as being in error, this prevents some
16691 -- nasty problems with subsequent uses of the malformed type.
16693 Set_Error_Posted (T);
16695 -- Check that within the immediate scope of an untagged partial
16696 -- view it's illegal to derive from the partial view if the
16697 -- full view is tagged. (7.3(7))
16699 -- We verify that the Parent_Type is a partial view by checking
16700 -- that it is not a Full_Type_Declaration (i.e. a private type or
16701 -- private extension declaration), to distinguish a partial view
16702 -- from a derivation from a private type which also appears as
16703 -- E_Private_Type. If the parent base type is not declared in an
16704 -- enclosing scope there is no need to check.
16706 elsif Present (Full_View (Parent_Type))
16707 and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
16708 and then not Is_Tagged_Type (Parent_Type)
16709 and then Is_Tagged_Type (Full_View (Parent_Type))
16710 and then In_Open_Scopes (Scope (Base_Type (Parent_Type)))
16711 then
16712 Error_Msg_N
16713 ("premature derivation from type with tagged full view",
16714 Indic);
16715 end if;
16716 end if;
16718 -- Check that form of derivation is appropriate
16720 Taggd := Is_Tagged_Type (Parent_Type);
16722 -- Set the parent type to the class-wide type's specific type in this
16723 -- case to prevent cascading errors
16725 if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
16726 Error_Msg_N ("parent type must not be a class-wide type", Indic);
16727 Set_Etype (T, Etype (Parent_Type));
16728 return;
16729 end if;
16731 if Present (Extension) and then not Taggd then
16732 Error_Msg_N
16733 ("type derived from untagged type cannot have extension", Indic);
16735 elsif No (Extension) and then Taggd then
16737 -- If this declaration is within a private part (or body) of a
16738 -- generic instantiation then the derivation is allowed (the parent
16739 -- type can only appear tagged in this case if it's a generic actual
16740 -- type, since it would otherwise have been rejected in the analysis
16741 -- of the generic template).
16743 if not Is_Generic_Actual_Type (Parent_Type)
16744 or else In_Visible_Part (Scope (Parent_Type))
16745 then
16746 if Is_Class_Wide_Type (Parent_Type) then
16747 Error_Msg_N
16748 ("parent type must not be a class-wide type", Indic);
16750 -- Use specific type to prevent cascaded errors.
16752 Parent_Type := Etype (Parent_Type);
16754 else
16755 Error_Msg_N
16756 ("type derived from tagged type must have extension", Indic);
16757 end if;
16758 end if;
16759 end if;
16761 -- AI-443: Synchronized formal derived types require a private
16762 -- extension. There is no point in checking the ancestor type or
16763 -- the progenitors since the construct is wrong to begin with.
16765 if Ada_Version >= Ada_2005
16766 and then Is_Generic_Type (T)
16767 and then Present (Original_Node (N))
16768 then
16769 declare
16770 Decl : constant Node_Id := Original_Node (N);
16772 begin
16773 if Nkind (Decl) = N_Formal_Type_Declaration
16774 and then Nkind (Formal_Type_Definition (Decl)) =
16775 N_Formal_Derived_Type_Definition
16776 and then Synchronized_Present (Formal_Type_Definition (Decl))
16777 and then No (Extension)
16779 -- Avoid emitting a duplicate error message
16781 and then not Error_Posted (Indic)
16782 then
16783 Error_Msg_N
16784 ("synchronized derived type must have extension", N);
16785 end if;
16786 end;
16787 end if;
16789 if Null_Exclusion_Present (Def)
16790 and then not Is_Access_Type (Parent_Type)
16791 then
16792 Error_Msg_N ("null exclusion can only apply to an access type", N);
16793 end if;
16795 -- Avoid deriving parent primitives of underlying record views
16797 Build_Derived_Type (N, Parent_Type, T, Is_Completion,
16798 Derive_Subps => not Is_Underlying_Record_View (T));
16800 -- AI-419: The parent type of an explicitly limited derived type must
16801 -- be a limited type or a limited interface.
16803 if Limited_Present (Def) then
16804 Set_Is_Limited_Record (T);
16806 if Is_Interface (T) then
16807 Set_Is_Limited_Interface (T);
16808 end if;
16810 if not Is_Limited_Type (Parent_Type)
16811 and then
16812 (not Is_Interface (Parent_Type)
16813 or else not Is_Limited_Interface (Parent_Type))
16814 then
16815 -- AI05-0096: a derivation in the private part of an instance is
16816 -- legal if the generic formal is untagged limited, and the actual
16817 -- is non-limited.
16819 if Is_Generic_Actual_Type (Parent_Type)
16820 and then In_Private_Part (Current_Scope)
16821 and then
16822 not Is_Tagged_Type
16823 (Generic_Parent_Type (Parent (Parent_Type)))
16824 then
16825 null;
16827 else
16828 Error_Msg_NE
16829 ("parent type& of limited type must be limited",
16830 N, Parent_Type);
16831 end if;
16832 end if;
16833 end if;
16835 -- In SPARK, there are no derived type definitions other than type
16836 -- extensions of tagged record types.
16838 if No (Extension) then
16839 Check_SPARK_05_Restriction
16840 ("derived type is not allowed", Original_Node (N));
16841 end if;
16842 end Derived_Type_Declaration;
16844 ------------------------
16845 -- Diagnose_Interface --
16846 ------------------------
16848 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id) is
16849 begin
16850 if not Is_Interface (E) and then E /= Any_Type then
16851 Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
16852 end if;
16853 end Diagnose_Interface;
16855 ----------------------------------
16856 -- Enumeration_Type_Declaration --
16857 ----------------------------------
16859 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
16860 Ev : Uint;
16861 L : Node_Id;
16862 R_Node : Node_Id;
16863 B_Node : Node_Id;
16865 begin
16866 -- Create identifier node representing lower bound
16868 B_Node := New_Node (N_Identifier, Sloc (Def));
16869 L := First (Literals (Def));
16870 Set_Chars (B_Node, Chars (L));
16871 Set_Entity (B_Node, L);
16872 Set_Etype (B_Node, T);
16873 Set_Is_Static_Expression (B_Node, True);
16875 R_Node := New_Node (N_Range, Sloc (Def));
16876 Set_Low_Bound (R_Node, B_Node);
16878 Set_Ekind (T, E_Enumeration_Type);
16879 Set_First_Literal (T, L);
16880 Set_Etype (T, T);
16881 Set_Is_Constrained (T);
16883 Ev := Uint_0;
16885 -- Loop through literals of enumeration type setting pos and rep values
16886 -- except that if the Ekind is already set, then it means the literal
16887 -- was already constructed (case of a derived type declaration and we
16888 -- should not disturb the Pos and Rep values.
16890 while Present (L) loop
16891 if Ekind (L) /= E_Enumeration_Literal then
16892 Set_Ekind (L, E_Enumeration_Literal);
16893 Set_Enumeration_Pos (L, Ev);
16894 Set_Enumeration_Rep (L, Ev);
16895 Set_Is_Known_Valid (L, True);
16896 end if;
16898 Set_Etype (L, T);
16899 New_Overloaded_Entity (L);
16900 Generate_Definition (L);
16901 Set_Convention (L, Convention_Intrinsic);
16903 -- Case of character literal
16905 if Nkind (L) = N_Defining_Character_Literal then
16906 Set_Is_Character_Type (T, True);
16908 -- Check violation of No_Wide_Characters
16910 if Restriction_Check_Required (No_Wide_Characters) then
16911 Get_Name_String (Chars (L));
16913 if Name_Len >= 3 and then Name_Buffer (1 .. 2) = "QW" then
16914 Check_Restriction (No_Wide_Characters, L);
16915 end if;
16916 end if;
16917 end if;
16919 Ev := Ev + 1;
16920 Next (L);
16921 end loop;
16923 -- Now create a node representing upper bound
16925 B_Node := New_Node (N_Identifier, Sloc (Def));
16926 Set_Chars (B_Node, Chars (Last (Literals (Def))));
16927 Set_Entity (B_Node, Last (Literals (Def)));
16928 Set_Etype (B_Node, T);
16929 Set_Is_Static_Expression (B_Node, True);
16931 Set_High_Bound (R_Node, B_Node);
16933 -- Initialize various fields of the type. Some of this information
16934 -- may be overwritten later through rep.clauses.
16936 Set_Scalar_Range (T, R_Node);
16937 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
16938 Set_Enum_Esize (T);
16939 Set_Enum_Pos_To_Rep (T, Empty);
16941 -- Set Discard_Names if configuration pragma set, or if there is
16942 -- a parameterless pragma in the current declarative region
16944 if Global_Discard_Names or else Discard_Names (Scope (T)) then
16945 Set_Discard_Names (T);
16946 end if;
16948 -- Process end label if there is one
16950 if Present (Def) then
16951 Process_End_Label (Def, 'e', T);
16952 end if;
16953 end Enumeration_Type_Declaration;
16955 ---------------------------------
16956 -- Expand_To_Stored_Constraint --
16957 ---------------------------------
16959 function Expand_To_Stored_Constraint
16960 (Typ : Entity_Id;
16961 Constraint : Elist_Id) return Elist_Id
16963 Explicitly_Discriminated_Type : Entity_Id;
16964 Expansion : Elist_Id;
16965 Discriminant : Entity_Id;
16967 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
16968 -- Find the nearest type that actually specifies discriminants
16970 ---------------------------------
16971 -- Type_With_Explicit_Discrims --
16972 ---------------------------------
16974 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
16975 Typ : constant E := Base_Type (Id);
16977 begin
16978 if Ekind (Typ) in Incomplete_Or_Private_Kind then
16979 if Present (Full_View (Typ)) then
16980 return Type_With_Explicit_Discrims (Full_View (Typ));
16981 end if;
16983 else
16984 if Has_Discriminants (Typ) then
16985 return Typ;
16986 end if;
16987 end if;
16989 if Etype (Typ) = Typ then
16990 return Empty;
16991 elsif Has_Discriminants (Typ) then
16992 return Typ;
16993 else
16994 return Type_With_Explicit_Discrims (Etype (Typ));
16995 end if;
16997 end Type_With_Explicit_Discrims;
16999 -- Start of processing for Expand_To_Stored_Constraint
17001 begin
17002 if No (Constraint) or else Is_Empty_Elmt_List (Constraint) then
17003 return No_Elist;
17004 end if;
17006 Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
17008 if No (Explicitly_Discriminated_Type) then
17009 return No_Elist;
17010 end if;
17012 Expansion := New_Elmt_List;
17014 Discriminant :=
17015 First_Stored_Discriminant (Explicitly_Discriminated_Type);
17016 while Present (Discriminant) loop
17017 Append_Elmt
17018 (Get_Discriminant_Value
17019 (Discriminant, Explicitly_Discriminated_Type, Constraint),
17020 To => Expansion);
17021 Next_Stored_Discriminant (Discriminant);
17022 end loop;
17024 return Expansion;
17025 end Expand_To_Stored_Constraint;
17027 ---------------------------
17028 -- Find_Hidden_Interface --
17029 ---------------------------
17031 function Find_Hidden_Interface
17032 (Src : Elist_Id;
17033 Dest : Elist_Id) return Entity_Id
17035 Iface : Entity_Id;
17036 Iface_Elmt : Elmt_Id;
17038 begin
17039 if Present (Src) and then Present (Dest) then
17040 Iface_Elmt := First_Elmt (Src);
17041 while Present (Iface_Elmt) loop
17042 Iface := Node (Iface_Elmt);
17044 if Is_Interface (Iface)
17045 and then not Contain_Interface (Iface, Dest)
17046 then
17047 return Iface;
17048 end if;
17050 Next_Elmt (Iface_Elmt);
17051 end loop;
17052 end if;
17054 return Empty;
17055 end Find_Hidden_Interface;
17057 --------------------
17058 -- Find_Type_Name --
17059 --------------------
17061 function Find_Type_Name (N : Node_Id) return Entity_Id is
17062 Id : constant Entity_Id := Defining_Identifier (N);
17063 New_Id : Entity_Id;
17064 Prev : Entity_Id;
17065 Prev_Par : Node_Id;
17067 procedure Check_Duplicate_Aspects;
17068 -- Check that aspects specified in a completion have not been specified
17069 -- already in the partial view.
17071 procedure Tag_Mismatch;
17072 -- Diagnose a tagged partial view whose full view is untagged. We post
17073 -- the message on the full view, with a reference to the previous
17074 -- partial view. The partial view can be private or incomplete, and
17075 -- these are handled in a different manner, so we determine the position
17076 -- of the error message from the respective slocs of both.
17078 -----------------------------
17079 -- Check_Duplicate_Aspects --
17080 -----------------------------
17082 procedure Check_Duplicate_Aspects is
17083 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id;
17084 -- Return the corresponding aspect of the partial view which matches
17085 -- the aspect id of Asp. Return Empty is no such aspect exists.
17087 -----------------------------
17088 -- Get_Partial_View_Aspect --
17089 -----------------------------
17091 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id is
17092 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp);
17093 Prev_Asps : constant List_Id := Aspect_Specifications (Prev_Par);
17094 Prev_Asp : Node_Id;
17096 begin
17097 if Present (Prev_Asps) then
17098 Prev_Asp := First (Prev_Asps);
17099 while Present (Prev_Asp) loop
17100 if Get_Aspect_Id (Prev_Asp) = Asp_Id then
17101 return Prev_Asp;
17102 end if;
17104 Next (Prev_Asp);
17105 end loop;
17106 end if;
17108 return Empty;
17109 end Get_Partial_View_Aspect;
17111 -- Local variables
17113 Full_Asps : constant List_Id := Aspect_Specifications (N);
17114 Full_Asp : Node_Id;
17115 Part_Asp : Node_Id;
17117 -- Start of processing for Check_Duplicate_Aspects
17119 begin
17120 if Present (Full_Asps) then
17121 Full_Asp := First (Full_Asps);
17122 while Present (Full_Asp) loop
17123 Part_Asp := Get_Partial_View_Aspect (Full_Asp);
17125 -- An aspect and its class-wide counterpart are two distinct
17126 -- aspects and may apply to both views of an entity.
17128 if Present (Part_Asp)
17129 and then Class_Present (Part_Asp) = Class_Present (Full_Asp)
17130 then
17131 Error_Msg_N
17132 ("aspect already specified in private declaration",
17133 Full_Asp);
17135 Remove (Full_Asp);
17136 return;
17137 end if;
17139 if Has_Discriminants (Prev)
17140 and then not Has_Unknown_Discriminants (Prev)
17141 and then Get_Aspect_Id (Full_Asp) =
17142 Aspect_Implicit_Dereference
17143 then
17144 Error_Msg_N
17145 ("cannot specify aspect if partial view has known "
17146 & "discriminants", Full_Asp);
17147 end if;
17149 Next (Full_Asp);
17150 end loop;
17151 end if;
17152 end Check_Duplicate_Aspects;
17154 ------------------
17155 -- Tag_Mismatch --
17156 ------------------
17158 procedure Tag_Mismatch is
17159 begin
17160 if Sloc (Prev) < Sloc (Id) then
17161 if Ada_Version >= Ada_2012
17162 and then Nkind (N) = N_Private_Type_Declaration
17163 then
17164 Error_Msg_NE
17165 ("declaration of private } must be a tagged type ", Id, Prev);
17166 else
17167 Error_Msg_NE
17168 ("full declaration of } must be a tagged type ", Id, Prev);
17169 end if;
17171 else
17172 if Ada_Version >= Ada_2012
17173 and then Nkind (N) = N_Private_Type_Declaration
17174 then
17175 Error_Msg_NE
17176 ("declaration of private } must be a tagged type ", Prev, Id);
17177 else
17178 Error_Msg_NE
17179 ("full declaration of } must be a tagged type ", Prev, Id);
17180 end if;
17181 end if;
17182 end Tag_Mismatch;
17184 -- Start of processing for Find_Type_Name
17186 begin
17187 -- Find incomplete declaration, if one was given
17189 Prev := Current_Entity_In_Scope (Id);
17191 -- New type declaration
17193 if No (Prev) then
17194 Enter_Name (Id);
17195 return Id;
17197 -- Previous declaration exists
17199 else
17200 Prev_Par := Parent (Prev);
17202 -- Error if not incomplete/private case except if previous
17203 -- declaration is implicit, etc. Enter_Name will emit error if
17204 -- appropriate.
17206 if not Is_Incomplete_Or_Private_Type (Prev) then
17207 Enter_Name (Id);
17208 New_Id := Id;
17210 -- Check invalid completion of private or incomplete type
17212 elsif not Nkind_In (N, N_Full_Type_Declaration,
17213 N_Task_Type_Declaration,
17214 N_Protected_Type_Declaration)
17215 and then
17216 (Ada_Version < Ada_2012
17217 or else not Is_Incomplete_Type (Prev)
17218 or else not Nkind_In (N, N_Private_Type_Declaration,
17219 N_Private_Extension_Declaration))
17220 then
17221 -- Completion must be a full type declarations (RM 7.3(4))
17223 Error_Msg_Sloc := Sloc (Prev);
17224 Error_Msg_NE ("invalid completion of }", Id, Prev);
17226 -- Set scope of Id to avoid cascaded errors. Entity is never
17227 -- examined again, except when saving globals in generics.
17229 Set_Scope (Id, Current_Scope);
17230 New_Id := Id;
17232 -- If this is a repeated incomplete declaration, no further
17233 -- checks are possible.
17235 if Nkind (N) = N_Incomplete_Type_Declaration then
17236 return Prev;
17237 end if;
17239 -- Case of full declaration of incomplete type
17241 elsif Ekind (Prev) = E_Incomplete_Type
17242 and then (Ada_Version < Ada_2012
17243 or else No (Full_View (Prev))
17244 or else not Is_Private_Type (Full_View (Prev)))
17245 then
17246 -- Indicate that the incomplete declaration has a matching full
17247 -- declaration. The defining occurrence of the incomplete
17248 -- declaration remains the visible one, and the procedure
17249 -- Get_Full_View dereferences it whenever the type is used.
17251 if Present (Full_View (Prev)) then
17252 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
17253 end if;
17255 Set_Full_View (Prev, Id);
17256 Append_Entity (Id, Current_Scope);
17257 Set_Is_Public (Id, Is_Public (Prev));
17258 Set_Is_Internal (Id);
17259 New_Id := Prev;
17261 -- If the incomplete view is tagged, a class_wide type has been
17262 -- created already. Use it for the private type as well, in order
17263 -- to prevent multiple incompatible class-wide types that may be
17264 -- created for self-referential anonymous access components.
17266 if Is_Tagged_Type (Prev)
17267 and then Present (Class_Wide_Type (Prev))
17268 then
17269 Set_Ekind (Id, Ekind (Prev)); -- will be reset later
17270 Set_Class_Wide_Type (Id, Class_Wide_Type (Prev));
17272 -- Type of the class-wide type is the current Id. Previously
17273 -- this was not done for private declarations because of order-
17274 -- of-elaboration issues in the back end, but gigi now handles
17275 -- this properly.
17277 Set_Etype (Class_Wide_Type (Id), Id);
17278 end if;
17280 -- Case of full declaration of private type
17282 else
17283 -- If the private type was a completion of an incomplete type then
17284 -- update Prev to reference the private type
17286 if Ada_Version >= Ada_2012
17287 and then Ekind (Prev) = E_Incomplete_Type
17288 and then Present (Full_View (Prev))
17289 and then Is_Private_Type (Full_View (Prev))
17290 then
17291 Prev := Full_View (Prev);
17292 Prev_Par := Parent (Prev);
17293 end if;
17295 if Nkind (N) = N_Full_Type_Declaration
17296 and then Nkind_In
17297 (Type_Definition (N), N_Record_Definition,
17298 N_Derived_Type_Definition)
17299 and then Interface_Present (Type_Definition (N))
17300 then
17301 Error_Msg_N
17302 ("completion of private type cannot be an interface", N);
17303 end if;
17305 if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
17306 if Etype (Prev) /= Prev then
17308 -- Prev is a private subtype or a derived type, and needs
17309 -- no completion.
17311 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
17312 New_Id := Id;
17314 elsif Ekind (Prev) = E_Private_Type
17315 and then Nkind_In (N, N_Task_Type_Declaration,
17316 N_Protected_Type_Declaration)
17317 then
17318 Error_Msg_N
17319 ("completion of nonlimited type cannot be limited", N);
17321 elsif Ekind (Prev) = E_Record_Type_With_Private
17322 and then Nkind_In (N, N_Task_Type_Declaration,
17323 N_Protected_Type_Declaration)
17324 then
17325 if not Is_Limited_Record (Prev) then
17326 Error_Msg_N
17327 ("completion of nonlimited type cannot be limited", N);
17329 elsif No (Interface_List (N)) then
17330 Error_Msg_N
17331 ("completion of tagged private type must be tagged",
17333 end if;
17334 end if;
17336 -- Ada 2005 (AI-251): Private extension declaration of a task
17337 -- type or a protected type. This case arises when covering
17338 -- interface types.
17340 elsif Nkind_In (N, N_Task_Type_Declaration,
17341 N_Protected_Type_Declaration)
17342 then
17343 null;
17345 elsif Nkind (N) /= N_Full_Type_Declaration
17346 or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
17347 then
17348 Error_Msg_N
17349 ("full view of private extension must be an extension", N);
17351 elsif not (Abstract_Present (Parent (Prev)))
17352 and then Abstract_Present (Type_Definition (N))
17353 then
17354 Error_Msg_N
17355 ("full view of non-abstract extension cannot be abstract", N);
17356 end if;
17358 if not In_Private_Part (Current_Scope) then
17359 Error_Msg_N
17360 ("declaration of full view must appear in private part", N);
17361 end if;
17363 if Ada_Version >= Ada_2012 then
17364 Check_Duplicate_Aspects;
17365 end if;
17367 Copy_And_Swap (Prev, Id);
17368 Set_Has_Private_Declaration (Prev);
17369 Set_Has_Private_Declaration (Id);
17371 -- AI12-0133: Indicate whether we have a partial view with
17372 -- unknown discriminants, in which case initialization of objects
17373 -- of the type do not receive an invariant check.
17375 Set_Partial_View_Has_Unknown_Discr
17376 (Prev, Has_Unknown_Discriminants (Id));
17378 -- Preserve aspect and iterator flags that may have been set on
17379 -- the partial view.
17381 Set_Has_Delayed_Aspects (Prev, Has_Delayed_Aspects (Id));
17382 Set_Has_Implicit_Dereference (Prev, Has_Implicit_Dereference (Id));
17384 -- If no error, propagate freeze_node from private to full view.
17385 -- It may have been generated for an early operational item.
17387 if Present (Freeze_Node (Id))
17388 and then Serious_Errors_Detected = 0
17389 and then No (Full_View (Id))
17390 then
17391 Set_Freeze_Node (Prev, Freeze_Node (Id));
17392 Set_Freeze_Node (Id, Empty);
17393 Set_First_Rep_Item (Prev, First_Rep_Item (Id));
17394 end if;
17396 Set_Full_View (Id, Prev);
17397 New_Id := Prev;
17398 end if;
17400 -- Verify that full declaration conforms to partial one
17402 if Is_Incomplete_Or_Private_Type (Prev)
17403 and then Present (Discriminant_Specifications (Prev_Par))
17404 then
17405 if Present (Discriminant_Specifications (N)) then
17406 if Ekind (Prev) = E_Incomplete_Type then
17407 Check_Discriminant_Conformance (N, Prev, Prev);
17408 else
17409 Check_Discriminant_Conformance (N, Prev, Id);
17410 end if;
17412 else
17413 Error_Msg_N
17414 ("missing discriminants in full type declaration", N);
17416 -- To avoid cascaded errors on subsequent use, share the
17417 -- discriminants of the partial view.
17419 Set_Discriminant_Specifications (N,
17420 Discriminant_Specifications (Prev_Par));
17421 end if;
17422 end if;
17424 -- A prior untagged partial view can have an associated class-wide
17425 -- type due to use of the class attribute, and in this case the full
17426 -- type must also be tagged. This Ada 95 usage is deprecated in favor
17427 -- of incomplete tagged declarations, but we check for it.
17429 if Is_Type (Prev)
17430 and then (Is_Tagged_Type (Prev)
17431 or else Present (Class_Wide_Type (Prev)))
17432 then
17433 -- Ada 2012 (AI05-0162): A private type may be the completion of
17434 -- an incomplete type.
17436 if Ada_Version >= Ada_2012
17437 and then Is_Incomplete_Type (Prev)
17438 and then Nkind_In (N, N_Private_Type_Declaration,
17439 N_Private_Extension_Declaration)
17440 then
17441 -- No need to check private extensions since they are tagged
17443 if Nkind (N) = N_Private_Type_Declaration
17444 and then not Tagged_Present (N)
17445 then
17446 Tag_Mismatch;
17447 end if;
17449 -- The full declaration is either a tagged type (including
17450 -- a synchronized type that implements interfaces) or a
17451 -- type extension, otherwise this is an error.
17453 elsif Nkind_In (N, N_Task_Type_Declaration,
17454 N_Protected_Type_Declaration)
17455 then
17456 if No (Interface_List (N)) and then not Error_Posted (N) then
17457 Tag_Mismatch;
17458 end if;
17460 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
17462 -- Indicate that the previous declaration (tagged incomplete
17463 -- or private declaration) requires the same on the full one.
17465 if not Tagged_Present (Type_Definition (N)) then
17466 Tag_Mismatch;
17467 Set_Is_Tagged_Type (Id);
17468 end if;
17470 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
17471 if No (Record_Extension_Part (Type_Definition (N))) then
17472 Error_Msg_NE
17473 ("full declaration of } must be a record extension",
17474 Prev, Id);
17476 -- Set some attributes to produce a usable full view
17478 Set_Is_Tagged_Type (Id);
17479 end if;
17481 else
17482 Tag_Mismatch;
17483 end if;
17484 end if;
17486 if Present (Prev)
17487 and then Nkind (Parent (Prev)) = N_Incomplete_Type_Declaration
17488 and then Present (Premature_Use (Parent (Prev)))
17489 then
17490 Error_Msg_Sloc := Sloc (N);
17491 Error_Msg_N
17492 ("\full declaration #", Premature_Use (Parent (Prev)));
17493 end if;
17495 return New_Id;
17496 end if;
17497 end Find_Type_Name;
17499 -------------------------
17500 -- Find_Type_Of_Object --
17501 -------------------------
17503 function Find_Type_Of_Object
17504 (Obj_Def : Node_Id;
17505 Related_Nod : Node_Id) return Entity_Id
17507 Def_Kind : constant Node_Kind := Nkind (Obj_Def);
17508 P : Node_Id := Parent (Obj_Def);
17509 T : Entity_Id;
17510 Nam : Name_Id;
17512 begin
17513 -- If the parent is a component_definition node we climb to the
17514 -- component_declaration node
17516 if Nkind (P) = N_Component_Definition then
17517 P := Parent (P);
17518 end if;
17520 -- Case of an anonymous array subtype
17522 if Nkind_In (Def_Kind, N_Constrained_Array_Definition,
17523 N_Unconstrained_Array_Definition)
17524 then
17525 T := Empty;
17526 Array_Type_Declaration (T, Obj_Def);
17528 -- Create an explicit subtype whenever possible
17530 elsif Nkind (P) /= N_Component_Declaration
17531 and then Def_Kind = N_Subtype_Indication
17532 then
17533 -- Base name of subtype on object name, which will be unique in
17534 -- the current scope.
17536 -- If this is a duplicate declaration, return base type, to avoid
17537 -- generating duplicate anonymous types.
17539 if Error_Posted (P) then
17540 Analyze (Subtype_Mark (Obj_Def));
17541 return Entity (Subtype_Mark (Obj_Def));
17542 end if;
17544 Nam :=
17545 New_External_Name
17546 (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
17548 T := Make_Defining_Identifier (Sloc (P), Nam);
17550 Insert_Action (Obj_Def,
17551 Make_Subtype_Declaration (Sloc (P),
17552 Defining_Identifier => T,
17553 Subtype_Indication => Relocate_Node (Obj_Def)));
17555 -- This subtype may need freezing, and this will not be done
17556 -- automatically if the object declaration is not in declarative
17557 -- part. Since this is an object declaration, the type cannot always
17558 -- be frozen here. Deferred constants do not freeze their type
17559 -- (which often enough will be private).
17561 if Nkind (P) = N_Object_Declaration
17562 and then Constant_Present (P)
17563 and then No (Expression (P))
17564 then
17565 null;
17567 -- Here we freeze the base type of object type to catch premature use
17568 -- of discriminated private type without a full view.
17570 else
17571 Insert_Actions (Obj_Def, Freeze_Entity (Base_Type (T), P));
17572 end if;
17574 -- Ada 2005 AI-406: the object definition in an object declaration
17575 -- can be an access definition.
17577 elsif Def_Kind = N_Access_Definition then
17578 T := Access_Definition (Related_Nod, Obj_Def);
17580 Set_Is_Local_Anonymous_Access
17582 V => (Ada_Version < Ada_2012)
17583 or else (Nkind (P) /= N_Object_Declaration)
17584 or else Is_Library_Level_Entity (Defining_Identifier (P)));
17586 -- Otherwise, the object definition is just a subtype_mark
17588 else
17589 T := Process_Subtype (Obj_Def, Related_Nod);
17591 -- If expansion is disabled an object definition that is an aggregate
17592 -- will not get expanded and may lead to scoping problems in the back
17593 -- end, if the object is referenced in an inner scope. In that case
17594 -- create an itype reference for the object definition now. This
17595 -- may be redundant in some cases, but harmless.
17597 if Is_Itype (T)
17598 and then Nkind (Related_Nod) = N_Object_Declaration
17599 and then ASIS_Mode
17600 then
17601 Build_Itype_Reference (T, Related_Nod);
17602 end if;
17603 end if;
17605 return T;
17606 end Find_Type_Of_Object;
17608 --------------------------------
17609 -- Find_Type_Of_Subtype_Indic --
17610 --------------------------------
17612 function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
17613 Typ : Entity_Id;
17615 begin
17616 -- Case of subtype mark with a constraint
17618 if Nkind (S) = N_Subtype_Indication then
17619 Find_Type (Subtype_Mark (S));
17620 Typ := Entity (Subtype_Mark (S));
17622 if not
17623 Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
17624 then
17625 Error_Msg_N
17626 ("incorrect constraint for this kind of type", Constraint (S));
17627 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
17628 end if;
17630 -- Otherwise we have a subtype mark without a constraint
17632 elsif Error_Posted (S) then
17633 Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
17634 return Any_Type;
17636 else
17637 Find_Type (S);
17638 Typ := Entity (S);
17639 end if;
17641 -- Check No_Wide_Characters restriction
17643 Check_Wide_Character_Restriction (Typ, S);
17645 return Typ;
17646 end Find_Type_Of_Subtype_Indic;
17648 -------------------------------------
17649 -- Floating_Point_Type_Declaration --
17650 -------------------------------------
17652 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
17653 Digs : constant Node_Id := Digits_Expression (Def);
17654 Max_Digs_Val : constant Uint := Digits_Value (Standard_Long_Long_Float);
17655 Digs_Val : Uint;
17656 Base_Typ : Entity_Id;
17657 Implicit_Base : Entity_Id;
17658 Bound : Node_Id;
17660 function Can_Derive_From (E : Entity_Id) return Boolean;
17661 -- Find if given digits value, and possibly a specified range, allows
17662 -- derivation from specified type
17664 function Find_Base_Type return Entity_Id;
17665 -- Find a predefined base type that Def can derive from, or generate
17666 -- an error and substitute Long_Long_Float if none exists.
17668 ---------------------
17669 -- Can_Derive_From --
17670 ---------------------
17672 function Can_Derive_From (E : Entity_Id) return Boolean is
17673 Spec : constant Entity_Id := Real_Range_Specification (Def);
17675 begin
17676 -- Check specified "digits" constraint
17678 if Digs_Val > Digits_Value (E) then
17679 return False;
17680 end if;
17682 -- Check for matching range, if specified
17684 if Present (Spec) then
17685 if Expr_Value_R (Type_Low_Bound (E)) >
17686 Expr_Value_R (Low_Bound (Spec))
17687 then
17688 return False;
17689 end if;
17691 if Expr_Value_R (Type_High_Bound (E)) <
17692 Expr_Value_R (High_Bound (Spec))
17693 then
17694 return False;
17695 end if;
17696 end if;
17698 return True;
17699 end Can_Derive_From;
17701 --------------------
17702 -- Find_Base_Type --
17703 --------------------
17705 function Find_Base_Type return Entity_Id is
17706 Choice : Elmt_Id := First_Elmt (Predefined_Float_Types);
17708 begin
17709 -- Iterate over the predefined types in order, returning the first
17710 -- one that Def can derive from.
17712 while Present (Choice) loop
17713 if Can_Derive_From (Node (Choice)) then
17714 return Node (Choice);
17715 end if;
17717 Next_Elmt (Choice);
17718 end loop;
17720 -- If we can't derive from any existing type, use Long_Long_Float
17721 -- and give appropriate message explaining the problem.
17723 if Digs_Val > Max_Digs_Val then
17724 -- It might be the case that there is a type with the requested
17725 -- range, just not the combination of digits and range.
17727 Error_Msg_N
17728 ("no predefined type has requested range and precision",
17729 Real_Range_Specification (Def));
17731 else
17732 Error_Msg_N
17733 ("range too large for any predefined type",
17734 Real_Range_Specification (Def));
17735 end if;
17737 return Standard_Long_Long_Float;
17738 end Find_Base_Type;
17740 -- Start of processing for Floating_Point_Type_Declaration
17742 begin
17743 Check_Restriction (No_Floating_Point, Def);
17745 -- Create an implicit base type
17747 Implicit_Base :=
17748 Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
17750 -- Analyze and verify digits value
17752 Analyze_And_Resolve (Digs, Any_Integer);
17753 Check_Digits_Expression (Digs);
17754 Digs_Val := Expr_Value (Digs);
17756 -- Process possible range spec and find correct type to derive from
17758 Process_Real_Range_Specification (Def);
17760 -- Check that requested number of digits is not too high.
17762 if Digs_Val > Max_Digs_Val then
17764 -- The check for Max_Base_Digits may be somewhat expensive, as it
17765 -- requires reading System, so only do it when necessary.
17767 declare
17768 Max_Base_Digits : constant Uint :=
17769 Expr_Value
17770 (Expression
17771 (Parent (RTE (RE_Max_Base_Digits))));
17773 begin
17774 if Digs_Val > Max_Base_Digits then
17775 Error_Msg_Uint_1 := Max_Base_Digits;
17776 Error_Msg_N ("digits value out of range, maximum is ^", Digs);
17778 elsif No (Real_Range_Specification (Def)) then
17779 Error_Msg_Uint_1 := Max_Digs_Val;
17780 Error_Msg_N ("types with more than ^ digits need range spec "
17781 & "(RM 3.5.7(6))", Digs);
17782 end if;
17783 end;
17784 end if;
17786 -- Find a suitable type to derive from or complain and use a substitute
17788 Base_Typ := Find_Base_Type;
17790 -- If there are bounds given in the declaration use them as the bounds
17791 -- of the type, otherwise use the bounds of the predefined base type
17792 -- that was chosen based on the Digits value.
17794 if Present (Real_Range_Specification (Def)) then
17795 Set_Scalar_Range (T, Real_Range_Specification (Def));
17796 Set_Is_Constrained (T);
17798 -- The bounds of this range must be converted to machine numbers
17799 -- in accordance with RM 4.9(38).
17801 Bound := Type_Low_Bound (T);
17803 if Nkind (Bound) = N_Real_Literal then
17804 Set_Realval
17805 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
17806 Set_Is_Machine_Number (Bound);
17807 end if;
17809 Bound := Type_High_Bound (T);
17811 if Nkind (Bound) = N_Real_Literal then
17812 Set_Realval
17813 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
17814 Set_Is_Machine_Number (Bound);
17815 end if;
17817 else
17818 Set_Scalar_Range (T, Scalar_Range (Base_Typ));
17819 end if;
17821 -- Complete definition of implicit base and declared first subtype. The
17822 -- inheritance of the rep item chain ensures that SPARK-related pragmas
17823 -- are not clobbered when the floating point type acts as a full view of
17824 -- a private type.
17826 Set_Etype (Implicit_Base, Base_Typ);
17827 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
17828 Set_Size_Info (Implicit_Base, Base_Typ);
17829 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
17830 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
17831 Set_Digits_Value (Implicit_Base, Digits_Value (Base_Typ));
17832 Set_Float_Rep (Implicit_Base, Float_Rep (Base_Typ));
17834 Set_Ekind (T, E_Floating_Point_Subtype);
17835 Set_Etype (T, Implicit_Base);
17836 Set_Size_Info (T, Implicit_Base);
17837 Set_RM_Size (T, RM_Size (Implicit_Base));
17838 Inherit_Rep_Item_Chain (T, Implicit_Base);
17839 Set_Digits_Value (T, Digs_Val);
17840 end Floating_Point_Type_Declaration;
17842 ----------------------------
17843 -- Get_Discriminant_Value --
17844 ----------------------------
17846 -- This is the situation:
17848 -- There is a non-derived type
17850 -- type T0 (Dx, Dy, Dz...)
17852 -- There are zero or more levels of derivation, with each derivation
17853 -- either purely inheriting the discriminants, or defining its own.
17855 -- type Ti is new Ti-1
17856 -- or
17857 -- type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
17858 -- or
17859 -- subtype Ti is ...
17861 -- The subtype issue is avoided by the use of Original_Record_Component,
17862 -- and the fact that derived subtypes also derive the constraints.
17864 -- This chain leads back from
17866 -- Typ_For_Constraint
17868 -- Typ_For_Constraint has discriminants, and the value for each
17869 -- discriminant is given by its corresponding Elmt of Constraints.
17871 -- Discriminant is some discriminant in this hierarchy
17873 -- We need to return its value
17875 -- We do this by recursively searching each level, and looking for
17876 -- Discriminant. Once we get to the bottom, we start backing up
17877 -- returning the value for it which may in turn be a discriminant
17878 -- further up, so on the backup we continue the substitution.
17880 function Get_Discriminant_Value
17881 (Discriminant : Entity_Id;
17882 Typ_For_Constraint : Entity_Id;
17883 Constraint : Elist_Id) return Node_Id
17885 function Root_Corresponding_Discriminant
17886 (Discr : Entity_Id) return Entity_Id;
17887 -- Given a discriminant, traverse the chain of inherited discriminants
17888 -- and return the topmost discriminant.
17890 function Search_Derivation_Levels
17891 (Ti : Entity_Id;
17892 Discrim_Values : Elist_Id;
17893 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
17894 -- This is the routine that performs the recursive search of levels
17895 -- as described above.
17897 -------------------------------------
17898 -- Root_Corresponding_Discriminant --
17899 -------------------------------------
17901 function Root_Corresponding_Discriminant
17902 (Discr : Entity_Id) return Entity_Id
17904 D : Entity_Id;
17906 begin
17907 D := Discr;
17908 while Present (Corresponding_Discriminant (D)) loop
17909 D := Corresponding_Discriminant (D);
17910 end loop;
17912 return D;
17913 end Root_Corresponding_Discriminant;
17915 ------------------------------
17916 -- Search_Derivation_Levels --
17917 ------------------------------
17919 function Search_Derivation_Levels
17920 (Ti : Entity_Id;
17921 Discrim_Values : Elist_Id;
17922 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
17924 Assoc : Elmt_Id;
17925 Disc : Entity_Id;
17926 Result : Node_Or_Entity_Id;
17927 Result_Entity : Node_Id;
17929 begin
17930 -- If inappropriate type, return Error, this happens only in
17931 -- cascaded error situations, and we want to avoid a blow up.
17933 if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
17934 return Error;
17935 end if;
17937 -- Look deeper if possible. Use Stored_Constraints only for
17938 -- untagged types. For tagged types use the given constraint.
17939 -- This asymmetry needs explanation???
17941 if not Stored_Discrim_Values
17942 and then Present (Stored_Constraint (Ti))
17943 and then not Is_Tagged_Type (Ti)
17944 then
17945 Result :=
17946 Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
17947 else
17948 declare
17949 Td : constant Entity_Id := Etype (Ti);
17951 begin
17952 if Td = Ti then
17953 Result := Discriminant;
17955 else
17956 if Present (Stored_Constraint (Ti)) then
17957 Result :=
17958 Search_Derivation_Levels
17959 (Td, Stored_Constraint (Ti), True);
17960 else
17961 Result :=
17962 Search_Derivation_Levels
17963 (Td, Discrim_Values, Stored_Discrim_Values);
17964 end if;
17965 end if;
17966 end;
17967 end if;
17969 -- Extra underlying places to search, if not found above. For
17970 -- concurrent types, the relevant discriminant appears in the
17971 -- corresponding record. For a type derived from a private type
17972 -- without discriminant, the full view inherits the discriminants
17973 -- of the full view of the parent.
17975 if Result = Discriminant then
17976 if Is_Concurrent_Type (Ti)
17977 and then Present (Corresponding_Record_Type (Ti))
17978 then
17979 Result :=
17980 Search_Derivation_Levels (
17981 Corresponding_Record_Type (Ti),
17982 Discrim_Values,
17983 Stored_Discrim_Values);
17985 elsif Is_Private_Type (Ti)
17986 and then not Has_Discriminants (Ti)
17987 and then Present (Full_View (Ti))
17988 and then Etype (Full_View (Ti)) /= Ti
17989 then
17990 Result :=
17991 Search_Derivation_Levels (
17992 Full_View (Ti),
17993 Discrim_Values,
17994 Stored_Discrim_Values);
17995 end if;
17996 end if;
17998 -- If Result is not a (reference to a) discriminant, return it,
17999 -- otherwise set Result_Entity to the discriminant.
18001 if Nkind (Result) = N_Defining_Identifier then
18002 pragma Assert (Result = Discriminant);
18003 Result_Entity := Result;
18005 else
18006 if not Denotes_Discriminant (Result) then
18007 return Result;
18008 end if;
18010 Result_Entity := Entity (Result);
18011 end if;
18013 -- See if this level of derivation actually has discriminants because
18014 -- tagged derivations can add them, hence the lower levels need not
18015 -- have any.
18017 if not Has_Discriminants (Ti) then
18018 return Result;
18019 end if;
18021 -- Scan Ti's discriminants for Result_Entity, and return its
18022 -- corresponding value, if any.
18024 Result_Entity := Original_Record_Component (Result_Entity);
18026 Assoc := First_Elmt (Discrim_Values);
18028 if Stored_Discrim_Values then
18029 Disc := First_Stored_Discriminant (Ti);
18030 else
18031 Disc := First_Discriminant (Ti);
18032 end if;
18034 while Present (Disc) loop
18036 -- If no further associations return the discriminant, value will
18037 -- be found on the second pass.
18039 if No (Assoc) then
18040 return Result;
18041 end if;
18043 if Original_Record_Component (Disc) = Result_Entity then
18044 return Node (Assoc);
18045 end if;
18047 Next_Elmt (Assoc);
18049 if Stored_Discrim_Values then
18050 Next_Stored_Discriminant (Disc);
18051 else
18052 Next_Discriminant (Disc);
18053 end if;
18054 end loop;
18056 -- Could not find it
18058 return Result;
18059 end Search_Derivation_Levels;
18061 -- Local Variables
18063 Result : Node_Or_Entity_Id;
18065 -- Start of processing for Get_Discriminant_Value
18067 begin
18068 -- ??? This routine is a gigantic mess and will be deleted. For the
18069 -- time being just test for the trivial case before calling recurse.
18071 -- We are now celebrating the 20th anniversary of this comment!
18073 if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
18074 declare
18075 D : Entity_Id;
18076 E : Elmt_Id;
18078 begin
18079 D := First_Discriminant (Typ_For_Constraint);
18080 E := First_Elmt (Constraint);
18081 while Present (D) loop
18082 if Chars (D) = Chars (Discriminant) then
18083 return Node (E);
18084 end if;
18086 Next_Discriminant (D);
18087 Next_Elmt (E);
18088 end loop;
18089 end;
18090 end if;
18092 Result := Search_Derivation_Levels
18093 (Typ_For_Constraint, Constraint, False);
18095 -- ??? hack to disappear when this routine is gone
18097 if Nkind (Result) = N_Defining_Identifier then
18098 declare
18099 D : Entity_Id;
18100 E : Elmt_Id;
18102 begin
18103 D := First_Discriminant (Typ_For_Constraint);
18104 E := First_Elmt (Constraint);
18105 while Present (D) loop
18106 if Root_Corresponding_Discriminant (D) = Discriminant then
18107 return Node (E);
18108 end if;
18110 Next_Discriminant (D);
18111 Next_Elmt (E);
18112 end loop;
18113 end;
18114 end if;
18116 pragma Assert (Nkind (Result) /= N_Defining_Identifier);
18117 return Result;
18118 end Get_Discriminant_Value;
18120 --------------------------
18121 -- Has_Range_Constraint --
18122 --------------------------
18124 function Has_Range_Constraint (N : Node_Id) return Boolean is
18125 C : constant Node_Id := Constraint (N);
18127 begin
18128 if Nkind (C) = N_Range_Constraint then
18129 return True;
18131 elsif Nkind (C) = N_Digits_Constraint then
18132 return
18133 Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
18134 or else Present (Range_Constraint (C));
18136 elsif Nkind (C) = N_Delta_Constraint then
18137 return Present (Range_Constraint (C));
18139 else
18140 return False;
18141 end if;
18142 end Has_Range_Constraint;
18144 ------------------------
18145 -- Inherit_Components --
18146 ------------------------
18148 function Inherit_Components
18149 (N : Node_Id;
18150 Parent_Base : Entity_Id;
18151 Derived_Base : Entity_Id;
18152 Is_Tagged : Boolean;
18153 Inherit_Discr : Boolean;
18154 Discs : Elist_Id) return Elist_Id
18156 Assoc_List : constant Elist_Id := New_Elmt_List;
18158 procedure Inherit_Component
18159 (Old_C : Entity_Id;
18160 Plain_Discrim : Boolean := False;
18161 Stored_Discrim : Boolean := False);
18162 -- Inherits component Old_C from Parent_Base to the Derived_Base. If
18163 -- Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
18164 -- True, Old_C is a stored discriminant. If they are both false then
18165 -- Old_C is a regular component.
18167 -----------------------
18168 -- Inherit_Component --
18169 -----------------------
18171 procedure Inherit_Component
18172 (Old_C : Entity_Id;
18173 Plain_Discrim : Boolean := False;
18174 Stored_Discrim : Boolean := False)
18176 procedure Set_Anonymous_Type (Id : Entity_Id);
18177 -- Id denotes the entity of an access discriminant or anonymous
18178 -- access component. Set the type of Id to either the same type of
18179 -- Old_C or create a new one depending on whether the parent and
18180 -- the child types are in the same scope.
18182 ------------------------
18183 -- Set_Anonymous_Type --
18184 ------------------------
18186 procedure Set_Anonymous_Type (Id : Entity_Id) is
18187 Old_Typ : constant Entity_Id := Etype (Old_C);
18189 begin
18190 if Scope (Parent_Base) = Scope (Derived_Base) then
18191 Set_Etype (Id, Old_Typ);
18193 -- The parent and the derived type are in two different scopes.
18194 -- Reuse the type of the original discriminant / component by
18195 -- copying it in order to preserve all attributes.
18197 else
18198 declare
18199 Typ : constant Entity_Id := New_Copy (Old_Typ);
18201 begin
18202 Set_Etype (Id, Typ);
18204 -- Since we do not generate component declarations for
18205 -- inherited components, associate the itype with the
18206 -- derived type.
18208 Set_Associated_Node_For_Itype (Typ, Parent (Derived_Base));
18209 Set_Scope (Typ, Derived_Base);
18210 end;
18211 end if;
18212 end Set_Anonymous_Type;
18214 -- Local variables and constants
18216 New_C : constant Entity_Id := New_Copy (Old_C);
18218 Corr_Discrim : Entity_Id;
18219 Discrim : Entity_Id;
18221 -- Start of processing for Inherit_Component
18223 begin
18224 pragma Assert (not Is_Tagged or not Stored_Discrim);
18226 Set_Parent (New_C, Parent (Old_C));
18228 -- Regular discriminants and components must be inserted in the scope
18229 -- of the Derived_Base. Do it here.
18231 if not Stored_Discrim then
18232 Enter_Name (New_C);
18233 end if;
18235 -- For tagged types the Original_Record_Component must point to
18236 -- whatever this field was pointing to in the parent type. This has
18237 -- already been achieved by the call to New_Copy above.
18239 if not Is_Tagged then
18240 Set_Original_Record_Component (New_C, New_C);
18241 Set_Corresponding_Record_Component (New_C, Old_C);
18242 end if;
18244 -- Set the proper type of an access discriminant
18246 if Ekind (New_C) = E_Discriminant
18247 and then Ekind (Etype (New_C)) = E_Anonymous_Access_Type
18248 then
18249 Set_Anonymous_Type (New_C);
18250 end if;
18252 -- If we have inherited a component then see if its Etype contains
18253 -- references to Parent_Base discriminants. In this case, replace
18254 -- these references with the constraints given in Discs. We do not
18255 -- do this for the partial view of private types because this is
18256 -- not needed (only the components of the full view will be used
18257 -- for code generation) and cause problem. We also avoid this
18258 -- transformation in some error situations.
18260 if Ekind (New_C) = E_Component then
18262 -- Set the proper type of an anonymous access component
18264 if Ekind (Etype (New_C)) = E_Anonymous_Access_Type then
18265 Set_Anonymous_Type (New_C);
18267 elsif (Is_Private_Type (Derived_Base)
18268 and then not Is_Generic_Type (Derived_Base))
18269 or else (Is_Empty_Elmt_List (Discs)
18270 and then not Expander_Active)
18271 then
18272 Set_Etype (New_C, Etype (Old_C));
18274 else
18275 -- The current component introduces a circularity of the
18276 -- following kind:
18278 -- limited with Pack_2;
18279 -- package Pack_1 is
18280 -- type T_1 is tagged record
18281 -- Comp : access Pack_2.T_2;
18282 -- ...
18283 -- end record;
18284 -- end Pack_1;
18286 -- with Pack_1;
18287 -- package Pack_2 is
18288 -- type T_2 is new Pack_1.T_1 with ...;
18289 -- end Pack_2;
18291 Set_Etype
18292 (New_C,
18293 Constrain_Component_Type
18294 (Old_C, Derived_Base, N, Parent_Base, Discs));
18295 end if;
18296 end if;
18298 -- In derived tagged types it is illegal to reference a non
18299 -- discriminant component in the parent type. To catch this, mark
18300 -- these components with an Ekind of E_Void. This will be reset in
18301 -- Record_Type_Definition after processing the record extension of
18302 -- the derived type.
18304 -- If the declaration is a private extension, there is no further
18305 -- record extension to process, and the components retain their
18306 -- current kind, because they are visible at this point.
18308 if Is_Tagged and then Ekind (New_C) = E_Component
18309 and then Nkind (N) /= N_Private_Extension_Declaration
18310 then
18311 Set_Ekind (New_C, E_Void);
18312 end if;
18314 if Plain_Discrim then
18315 Set_Corresponding_Discriminant (New_C, Old_C);
18316 Build_Discriminal (New_C);
18318 -- If we are explicitly inheriting a stored discriminant it will be
18319 -- completely hidden.
18321 elsif Stored_Discrim then
18322 Set_Corresponding_Discriminant (New_C, Empty);
18323 Set_Discriminal (New_C, Empty);
18324 Set_Is_Completely_Hidden (New_C);
18326 -- Set the Original_Record_Component of each discriminant in the
18327 -- derived base to point to the corresponding stored that we just
18328 -- created.
18330 Discrim := First_Discriminant (Derived_Base);
18331 while Present (Discrim) loop
18332 Corr_Discrim := Corresponding_Discriminant (Discrim);
18334 -- Corr_Discrim could be missing in an error situation
18336 if Present (Corr_Discrim)
18337 and then Original_Record_Component (Corr_Discrim) = Old_C
18338 then
18339 Set_Original_Record_Component (Discrim, New_C);
18340 Set_Corresponding_Record_Component (Discrim, Empty);
18341 end if;
18343 Next_Discriminant (Discrim);
18344 end loop;
18346 Append_Entity (New_C, Derived_Base);
18347 end if;
18349 if not Is_Tagged then
18350 Append_Elmt (Old_C, Assoc_List);
18351 Append_Elmt (New_C, Assoc_List);
18352 end if;
18353 end Inherit_Component;
18355 -- Variables local to Inherit_Component
18357 Loc : constant Source_Ptr := Sloc (N);
18359 Parent_Discrim : Entity_Id;
18360 Stored_Discrim : Entity_Id;
18361 D : Entity_Id;
18362 Component : Entity_Id;
18364 -- Start of processing for Inherit_Components
18366 begin
18367 if not Is_Tagged then
18368 Append_Elmt (Parent_Base, Assoc_List);
18369 Append_Elmt (Derived_Base, Assoc_List);
18370 end if;
18372 -- Inherit parent discriminants if needed
18374 if Inherit_Discr then
18375 Parent_Discrim := First_Discriminant (Parent_Base);
18376 while Present (Parent_Discrim) loop
18377 Inherit_Component (Parent_Discrim, Plain_Discrim => True);
18378 Next_Discriminant (Parent_Discrim);
18379 end loop;
18380 end if;
18382 -- Create explicit stored discrims for untagged types when necessary
18384 if not Has_Unknown_Discriminants (Derived_Base)
18385 and then Has_Discriminants (Parent_Base)
18386 and then not Is_Tagged
18387 and then
18388 (not Inherit_Discr
18389 or else First_Discriminant (Parent_Base) /=
18390 First_Stored_Discriminant (Parent_Base))
18391 then
18392 Stored_Discrim := First_Stored_Discriminant (Parent_Base);
18393 while Present (Stored_Discrim) loop
18394 Inherit_Component (Stored_Discrim, Stored_Discrim => True);
18395 Next_Stored_Discriminant (Stored_Discrim);
18396 end loop;
18397 end if;
18399 -- See if we can apply the second transformation for derived types, as
18400 -- explained in point 6. in the comments above Build_Derived_Record_Type
18401 -- This is achieved by appending Derived_Base discriminants into Discs,
18402 -- which has the side effect of returning a non empty Discs list to the
18403 -- caller of Inherit_Components, which is what we want. This must be
18404 -- done for private derived types if there are explicit stored
18405 -- discriminants, to ensure that we can retrieve the values of the
18406 -- constraints provided in the ancestors.
18408 if Inherit_Discr
18409 and then Is_Empty_Elmt_List (Discs)
18410 and then Present (First_Discriminant (Derived_Base))
18411 and then
18412 (not Is_Private_Type (Derived_Base)
18413 or else Is_Completely_Hidden
18414 (First_Stored_Discriminant (Derived_Base))
18415 or else Is_Generic_Type (Derived_Base))
18416 then
18417 D := First_Discriminant (Derived_Base);
18418 while Present (D) loop
18419 Append_Elmt (New_Occurrence_Of (D, Loc), Discs);
18420 Next_Discriminant (D);
18421 end loop;
18422 end if;
18424 -- Finally, inherit non-discriminant components unless they are not
18425 -- visible because defined or inherited from the full view of the
18426 -- parent. Don't inherit the _parent field of the parent type.
18428 Component := First_Entity (Parent_Base);
18429 while Present (Component) loop
18431 -- Ada 2005 (AI-251): Do not inherit components associated with
18432 -- secondary tags of the parent.
18434 if Ekind (Component) = E_Component
18435 and then Present (Related_Type (Component))
18436 then
18437 null;
18439 elsif Ekind (Component) /= E_Component
18440 or else Chars (Component) = Name_uParent
18441 then
18442 null;
18444 -- If the derived type is within the parent type's declarative
18445 -- region, then the components can still be inherited even though
18446 -- they aren't visible at this point. This can occur for cases
18447 -- such as within public child units where the components must
18448 -- become visible upon entering the child unit's private part.
18450 elsif not Is_Visible_Component (Component)
18451 and then not In_Open_Scopes (Scope (Parent_Base))
18452 then
18453 null;
18455 elsif Ekind_In (Derived_Base, E_Private_Type,
18456 E_Limited_Private_Type)
18457 then
18458 null;
18460 else
18461 Inherit_Component (Component);
18462 end if;
18464 Next_Entity (Component);
18465 end loop;
18467 -- For tagged derived types, inherited discriminants cannot be used in
18468 -- component declarations of the record extension part. To achieve this
18469 -- we mark the inherited discriminants as not visible.
18471 if Is_Tagged and then Inherit_Discr then
18472 D := First_Discriminant (Derived_Base);
18473 while Present (D) loop
18474 Set_Is_Immediately_Visible (D, False);
18475 Next_Discriminant (D);
18476 end loop;
18477 end if;
18479 return Assoc_List;
18480 end Inherit_Components;
18482 -----------------------------
18483 -- Inherit_Predicate_Flags --
18484 -----------------------------
18486 procedure Inherit_Predicate_Flags (Subt, Par : Entity_Id) is
18487 begin
18488 Set_Has_Predicates (Subt, Has_Predicates (Par));
18489 Set_Has_Static_Predicate_Aspect
18490 (Subt, Has_Static_Predicate_Aspect (Par));
18491 Set_Has_Dynamic_Predicate_Aspect
18492 (Subt, Has_Dynamic_Predicate_Aspect (Par));
18494 -- A named subtype does not inherit the predicate function of its
18495 -- parent but an itype declared for a loop index needs the discrete
18496 -- predicate information of its parent to execute the loop properly.
18498 if Is_Itype (Subt) and then Present (Predicate_Function (Par)) then
18499 Set_Subprograms_For_Type (Subt, Subprograms_For_Type (Par));
18501 if Has_Static_Predicate (Par) then
18502 Set_Static_Discrete_Predicate
18503 (Subt, Static_Discrete_Predicate (Par));
18504 end if;
18505 end if;
18506 end Inherit_Predicate_Flags;
18508 ----------------------
18509 -- Is_EVF_Procedure --
18510 ----------------------
18512 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean is
18513 Formal : Entity_Id;
18515 begin
18516 -- Examine the formals of an Extensions_Visible False procedure looking
18517 -- for a controlling OUT parameter.
18519 if Ekind (Subp) = E_Procedure
18520 and then Extensions_Visible_Status (Subp) = Extensions_Visible_False
18521 then
18522 Formal := First_Formal (Subp);
18523 while Present (Formal) loop
18524 if Ekind (Formal) = E_Out_Parameter
18525 and then Is_Controlling_Formal (Formal)
18526 then
18527 return True;
18528 end if;
18530 Next_Formal (Formal);
18531 end loop;
18532 end if;
18534 return False;
18535 end Is_EVF_Procedure;
18537 -----------------------
18538 -- Is_Null_Extension --
18539 -----------------------
18541 function Is_Null_Extension (T : Entity_Id) return Boolean is
18542 Type_Decl : constant Node_Id := Parent (Base_Type (T));
18543 Comp_List : Node_Id;
18544 Comp : Node_Id;
18546 begin
18547 if Nkind (Type_Decl) /= N_Full_Type_Declaration
18548 or else not Is_Tagged_Type (T)
18549 or else Nkind (Type_Definition (Type_Decl)) /=
18550 N_Derived_Type_Definition
18551 or else No (Record_Extension_Part (Type_Definition (Type_Decl)))
18552 then
18553 return False;
18554 end if;
18556 Comp_List :=
18557 Component_List (Record_Extension_Part (Type_Definition (Type_Decl)));
18559 if Present (Discriminant_Specifications (Type_Decl)) then
18560 return False;
18562 elsif Present (Comp_List)
18563 and then Is_Non_Empty_List (Component_Items (Comp_List))
18564 then
18565 Comp := First (Component_Items (Comp_List));
18567 -- Only user-defined components are relevant. The component list
18568 -- may also contain a parent component and internal components
18569 -- corresponding to secondary tags, but these do not determine
18570 -- whether this is a null extension.
18572 while Present (Comp) loop
18573 if Comes_From_Source (Comp) then
18574 return False;
18575 end if;
18577 Next (Comp);
18578 end loop;
18580 return True;
18582 else
18583 return True;
18584 end if;
18585 end Is_Null_Extension;
18587 ------------------------------
18588 -- Is_Valid_Constraint_Kind --
18589 ------------------------------
18591 function Is_Valid_Constraint_Kind
18592 (T_Kind : Type_Kind;
18593 Constraint_Kind : Node_Kind) return Boolean
18595 begin
18596 case T_Kind is
18597 when Enumeration_Kind
18598 | Integer_Kind
18600 return Constraint_Kind = N_Range_Constraint;
18602 when Decimal_Fixed_Point_Kind =>
18603 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
18604 N_Range_Constraint);
18606 when Ordinary_Fixed_Point_Kind =>
18607 return Nkind_In (Constraint_Kind, N_Delta_Constraint,
18608 N_Range_Constraint);
18610 when Float_Kind =>
18611 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
18612 N_Range_Constraint);
18614 when Access_Kind
18615 | Array_Kind
18616 | Class_Wide_Kind
18617 | Concurrent_Kind
18618 | Private_Kind
18619 | E_Incomplete_Type
18620 | E_Record_Subtype
18621 | E_Record_Type
18623 return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
18625 when others =>
18626 return True; -- Error will be detected later
18627 end case;
18628 end Is_Valid_Constraint_Kind;
18630 --------------------------
18631 -- Is_Visible_Component --
18632 --------------------------
18634 function Is_Visible_Component
18635 (C : Entity_Id;
18636 N : Node_Id := Empty) return Boolean
18638 Original_Comp : Entity_Id := Empty;
18639 Original_Type : Entity_Id;
18640 Type_Scope : Entity_Id;
18642 function Is_Local_Type (Typ : Entity_Id) return Boolean;
18643 -- Check whether parent type of inherited component is declared locally,
18644 -- possibly within a nested package or instance. The current scope is
18645 -- the derived record itself.
18647 -------------------
18648 -- Is_Local_Type --
18649 -------------------
18651 function Is_Local_Type (Typ : Entity_Id) return Boolean is
18652 Scop : Entity_Id;
18654 begin
18655 Scop := Scope (Typ);
18656 while Present (Scop)
18657 and then Scop /= Standard_Standard
18658 loop
18659 if Scop = Scope (Current_Scope) then
18660 return True;
18661 end if;
18663 Scop := Scope (Scop);
18664 end loop;
18666 return False;
18667 end Is_Local_Type;
18669 -- Start of processing for Is_Visible_Component
18671 begin
18672 if Ekind_In (C, E_Component, E_Discriminant) then
18673 Original_Comp := Original_Record_Component (C);
18674 end if;
18676 if No (Original_Comp) then
18678 -- Premature usage, or previous error
18680 return False;
18682 else
18683 Original_Type := Scope (Original_Comp);
18684 Type_Scope := Scope (Base_Type (Scope (C)));
18685 end if;
18687 -- This test only concerns tagged types
18689 if not Is_Tagged_Type (Original_Type) then
18690 return True;
18692 -- If it is _Parent or _Tag, there is no visibility issue
18694 elsif not Comes_From_Source (Original_Comp) then
18695 return True;
18697 -- Discriminants are visible unless the (private) type has unknown
18698 -- discriminants. If the discriminant reference is inserted for a
18699 -- discriminant check on a full view it is also visible.
18701 elsif Ekind (Original_Comp) = E_Discriminant
18702 and then
18703 (not Has_Unknown_Discriminants (Original_Type)
18704 or else (Present (N)
18705 and then Nkind (N) = N_Selected_Component
18706 and then Nkind (Prefix (N)) = N_Type_Conversion
18707 and then not Comes_From_Source (Prefix (N))))
18708 then
18709 return True;
18711 -- In the body of an instantiation, check the visibility of a component
18712 -- in case it has a homograph that is a primitive operation of a private
18713 -- type which was not visible in the generic unit.
18715 -- Should Is_Prefixed_Call be propagated from template to instance???
18717 elsif In_Instance_Body then
18718 if not Is_Tagged_Type (Original_Type)
18719 or else not Is_Private_Type (Original_Type)
18720 then
18721 return True;
18723 else
18724 declare
18725 Subp_Elmt : Elmt_Id;
18727 begin
18728 Subp_Elmt := First_Elmt (Primitive_Operations (Original_Type));
18729 while Present (Subp_Elmt) loop
18731 -- The component is hidden by a primitive operation
18733 if Chars (Node (Subp_Elmt)) = Chars (C) then
18734 return False;
18735 end if;
18737 Next_Elmt (Subp_Elmt);
18738 end loop;
18740 return True;
18741 end;
18742 end if;
18744 -- If the component has been declared in an ancestor which is currently
18745 -- a private type, then it is not visible. The same applies if the
18746 -- component's containing type is not in an open scope and the original
18747 -- component's enclosing type is a visible full view of a private type
18748 -- (which can occur in cases where an attempt is being made to reference
18749 -- a component in a sibling package that is inherited from a visible
18750 -- component of a type in an ancestor package; the component in the
18751 -- sibling package should not be visible even though the component it
18752 -- inherited from is visible). This does not apply however in the case
18753 -- where the scope of the type is a private child unit, or when the
18754 -- parent comes from a local package in which the ancestor is currently
18755 -- visible. The latter suppression of visibility is needed for cases
18756 -- that are tested in B730006.
18758 elsif Is_Private_Type (Original_Type)
18759 or else
18760 (not Is_Private_Descendant (Type_Scope)
18761 and then not In_Open_Scopes (Type_Scope)
18762 and then Has_Private_Declaration (Original_Type))
18763 then
18764 -- If the type derives from an entity in a formal package, there
18765 -- are no additional visible components.
18767 if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
18768 N_Formal_Package_Declaration
18769 then
18770 return False;
18772 -- if we are not in the private part of the current package, there
18773 -- are no additional visible components.
18775 elsif Ekind (Scope (Current_Scope)) = E_Package
18776 and then not In_Private_Part (Scope (Current_Scope))
18777 then
18778 return False;
18779 else
18780 return
18781 Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
18782 and then In_Open_Scopes (Scope (Original_Type))
18783 and then Is_Local_Type (Type_Scope);
18784 end if;
18786 -- There is another weird way in which a component may be invisible when
18787 -- the private and the full view are not derived from the same ancestor.
18788 -- Here is an example :
18790 -- type A1 is tagged record F1 : integer; end record;
18791 -- type A2 is new A1 with record F2 : integer; end record;
18792 -- type T is new A1 with private;
18793 -- private
18794 -- type T is new A2 with null record;
18796 -- In this case, the full view of T inherits F1 and F2 but the private
18797 -- view inherits only F1
18799 else
18800 declare
18801 Ancestor : Entity_Id := Scope (C);
18803 begin
18804 loop
18805 if Ancestor = Original_Type then
18806 return True;
18808 -- The ancestor may have a partial view of the original type,
18809 -- but if the full view is in scope, as in a child body, the
18810 -- component is visible.
18812 elsif In_Private_Part (Scope (Original_Type))
18813 and then Full_View (Ancestor) = Original_Type
18814 then
18815 return True;
18817 elsif Ancestor = Etype (Ancestor) then
18819 -- No further ancestors to examine
18821 return False;
18822 end if;
18824 Ancestor := Etype (Ancestor);
18825 end loop;
18826 end;
18827 end if;
18828 end Is_Visible_Component;
18830 --------------------------
18831 -- Make_Class_Wide_Type --
18832 --------------------------
18834 procedure Make_Class_Wide_Type (T : Entity_Id) is
18835 CW_Type : Entity_Id;
18836 CW_Name : Name_Id;
18837 Next_E : Entity_Id;
18839 begin
18840 if Present (Class_Wide_Type (T)) then
18842 -- The class-wide type is a partially decorated entity created for a
18843 -- unanalyzed tagged type referenced through a limited with clause.
18844 -- When the tagged type is analyzed, its class-wide type needs to be
18845 -- redecorated. Note that we reuse the entity created by Decorate_
18846 -- Tagged_Type in order to preserve all links.
18848 if Materialize_Entity (Class_Wide_Type (T)) then
18849 CW_Type := Class_Wide_Type (T);
18850 Set_Materialize_Entity (CW_Type, False);
18852 -- The class wide type can have been defined by the partial view, in
18853 -- which case everything is already done.
18855 else
18856 return;
18857 end if;
18859 -- Default case, we need to create a new class-wide type
18861 else
18862 CW_Type :=
18863 New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
18864 end if;
18866 -- Inherit root type characteristics
18868 CW_Name := Chars (CW_Type);
18869 Next_E := Next_Entity (CW_Type);
18870 Copy_Node (T, CW_Type);
18871 Set_Comes_From_Source (CW_Type, False);
18872 Set_Chars (CW_Type, CW_Name);
18873 Set_Parent (CW_Type, Parent (T));
18874 Set_Next_Entity (CW_Type, Next_E);
18876 -- Ensure we have a new freeze node for the class-wide type. The partial
18877 -- view may have freeze action of its own, requiring a proper freeze
18878 -- node, and the same freeze node cannot be shared between the two
18879 -- types.
18881 Set_Has_Delayed_Freeze (CW_Type);
18882 Set_Freeze_Node (CW_Type, Empty);
18884 -- Customize the class-wide type: It has no prim. op., it cannot be
18885 -- abstract, its Etype points back to the specific root type, and it
18886 -- cannot have any invariants.
18888 Set_Ekind (CW_Type, E_Class_Wide_Type);
18889 Set_Is_Tagged_Type (CW_Type, True);
18890 Set_Direct_Primitive_Operations (CW_Type, New_Elmt_List);
18891 Set_Is_Abstract_Type (CW_Type, False);
18892 Set_Is_Constrained (CW_Type, False);
18893 Set_Is_First_Subtype (CW_Type, Is_First_Subtype (T));
18894 Set_Default_SSO (CW_Type);
18895 Set_Has_Inheritable_Invariants (CW_Type, False);
18896 Set_Has_Inherited_Invariants (CW_Type, False);
18897 Set_Has_Own_Invariants (CW_Type, False);
18899 if Ekind (T) = E_Class_Wide_Subtype then
18900 Set_Etype (CW_Type, Etype (Base_Type (T)));
18901 else
18902 Set_Etype (CW_Type, T);
18903 end if;
18905 Set_No_Tagged_Streams_Pragma (CW_Type, No_Tagged_Streams);
18907 -- If this is the class_wide type of a constrained subtype, it does
18908 -- not have discriminants.
18910 Set_Has_Discriminants (CW_Type,
18911 Has_Discriminants (T) and then not Is_Constrained (T));
18913 Set_Has_Unknown_Discriminants (CW_Type, True);
18914 Set_Class_Wide_Type (T, CW_Type);
18915 Set_Equivalent_Type (CW_Type, Empty);
18917 -- The class-wide type of a class-wide type is itself (RM 3.9(14))
18919 Set_Class_Wide_Type (CW_Type, CW_Type);
18920 end Make_Class_Wide_Type;
18922 ----------------
18923 -- Make_Index --
18924 ----------------
18926 procedure Make_Index
18927 (N : Node_Id;
18928 Related_Nod : Node_Id;
18929 Related_Id : Entity_Id := Empty;
18930 Suffix_Index : Nat := 1;
18931 In_Iter_Schm : Boolean := False)
18933 R : Node_Id;
18934 T : Entity_Id;
18935 Def_Id : Entity_Id := Empty;
18936 Found : Boolean := False;
18938 begin
18939 -- For a discrete range used in a constrained array definition and
18940 -- defined by a range, an implicit conversion to the predefined type
18941 -- INTEGER is assumed if each bound is either a numeric literal, a named
18942 -- number, or an attribute, and the type of both bounds (prior to the
18943 -- implicit conversion) is the type universal_integer. Otherwise, both
18944 -- bounds must be of the same discrete type, other than universal
18945 -- integer; this type must be determinable independently of the
18946 -- context, but using the fact that the type must be discrete and that
18947 -- both bounds must have the same type.
18949 -- Character literals also have a universal type in the absence of
18950 -- of additional context, and are resolved to Standard_Character.
18952 if Nkind (N) = N_Range then
18954 -- The index is given by a range constraint. The bounds are known
18955 -- to be of a consistent type.
18957 if not Is_Overloaded (N) then
18958 T := Etype (N);
18960 -- For universal bounds, choose the specific predefined type
18962 if T = Universal_Integer then
18963 T := Standard_Integer;
18965 elsif T = Any_Character then
18966 Ambiguous_Character (Low_Bound (N));
18968 T := Standard_Character;
18969 end if;
18971 -- The node may be overloaded because some user-defined operators
18972 -- are available, but if a universal interpretation exists it is
18973 -- also the selected one.
18975 elsif Universal_Interpretation (N) = Universal_Integer then
18976 T := Standard_Integer;
18978 else
18979 T := Any_Type;
18981 declare
18982 Ind : Interp_Index;
18983 It : Interp;
18985 begin
18986 Get_First_Interp (N, Ind, It);
18987 while Present (It.Typ) loop
18988 if Is_Discrete_Type (It.Typ) then
18990 if Found
18991 and then not Covers (It.Typ, T)
18992 and then not Covers (T, It.Typ)
18993 then
18994 Error_Msg_N ("ambiguous bounds in discrete range", N);
18995 exit;
18996 else
18997 T := It.Typ;
18998 Found := True;
18999 end if;
19000 end if;
19002 Get_Next_Interp (Ind, It);
19003 end loop;
19005 if T = Any_Type then
19006 Error_Msg_N ("discrete type required for range", N);
19007 Set_Etype (N, Any_Type);
19008 return;
19010 elsif T = Universal_Integer then
19011 T := Standard_Integer;
19012 end if;
19013 end;
19014 end if;
19016 if not Is_Discrete_Type (T) then
19017 Error_Msg_N ("discrete type required for range", N);
19018 Set_Etype (N, Any_Type);
19019 return;
19020 end if;
19022 if Nkind (Low_Bound (N)) = N_Attribute_Reference
19023 and then Attribute_Name (Low_Bound (N)) = Name_First
19024 and then Is_Entity_Name (Prefix (Low_Bound (N)))
19025 and then Is_Type (Entity (Prefix (Low_Bound (N))))
19026 and then Is_Discrete_Type (Entity (Prefix (Low_Bound (N))))
19027 then
19028 -- The type of the index will be the type of the prefix, as long
19029 -- as the upper bound is 'Last of the same type.
19031 Def_Id := Entity (Prefix (Low_Bound (N)));
19033 if Nkind (High_Bound (N)) /= N_Attribute_Reference
19034 or else Attribute_Name (High_Bound (N)) /= Name_Last
19035 or else not Is_Entity_Name (Prefix (High_Bound (N)))
19036 or else Entity (Prefix (High_Bound (N))) /= Def_Id
19037 then
19038 Def_Id := Empty;
19039 end if;
19040 end if;
19042 R := N;
19043 Process_Range_Expr_In_Decl (R, T, In_Iter_Schm => In_Iter_Schm);
19045 elsif Nkind (N) = N_Subtype_Indication then
19047 -- The index is given by a subtype with a range constraint
19049 T := Base_Type (Entity (Subtype_Mark (N)));
19051 if not Is_Discrete_Type (T) then
19052 Error_Msg_N ("discrete type required for range", N);
19053 Set_Etype (N, Any_Type);
19054 return;
19055 end if;
19057 R := Range_Expression (Constraint (N));
19059 Resolve (R, T);
19060 Process_Range_Expr_In_Decl
19061 (R, Entity (Subtype_Mark (N)), In_Iter_Schm => In_Iter_Schm);
19063 elsif Nkind (N) = N_Attribute_Reference then
19065 -- Catch beginner's error (use of attribute other than 'Range)
19067 if Attribute_Name (N) /= Name_Range then
19068 Error_Msg_N ("expect attribute ''Range", N);
19069 Set_Etype (N, Any_Type);
19070 return;
19071 end if;
19073 -- If the node denotes the range of a type mark, that is also the
19074 -- resulting type, and we do not need to create an Itype for it.
19076 if Is_Entity_Name (Prefix (N))
19077 and then Comes_From_Source (N)
19078 and then Is_Type (Entity (Prefix (N)))
19079 and then Is_Discrete_Type (Entity (Prefix (N)))
19080 then
19081 Def_Id := Entity (Prefix (N));
19082 end if;
19084 Analyze_And_Resolve (N);
19085 T := Etype (N);
19086 R := N;
19088 -- If none of the above, must be a subtype. We convert this to a
19089 -- range attribute reference because in the case of declared first
19090 -- named subtypes, the types in the range reference can be different
19091 -- from the type of the entity. A range attribute normalizes the
19092 -- reference and obtains the correct types for the bounds.
19094 -- This transformation is in the nature of an expansion, is only
19095 -- done if expansion is active. In particular, it is not done on
19096 -- formal generic types, because we need to retain the name of the
19097 -- original index for instantiation purposes.
19099 else
19100 if not Is_Entity_Name (N) or else not Is_Type (Entity (N)) then
19101 Error_Msg_N ("invalid subtype mark in discrete range ", N);
19102 Set_Etype (N, Any_Integer);
19103 return;
19105 else
19106 -- The type mark may be that of an incomplete type. It is only
19107 -- now that we can get the full view, previous analysis does
19108 -- not look specifically for a type mark.
19110 Set_Entity (N, Get_Full_View (Entity (N)));
19111 Set_Etype (N, Entity (N));
19112 Def_Id := Entity (N);
19114 if not Is_Discrete_Type (Def_Id) then
19115 Error_Msg_N ("discrete type required for index", N);
19116 Set_Etype (N, Any_Type);
19117 return;
19118 end if;
19119 end if;
19121 if Expander_Active then
19122 Rewrite (N,
19123 Make_Attribute_Reference (Sloc (N),
19124 Attribute_Name => Name_Range,
19125 Prefix => Relocate_Node (N)));
19127 -- The original was a subtype mark that does not freeze. This
19128 -- means that the rewritten version must not freeze either.
19130 Set_Must_Not_Freeze (N);
19131 Set_Must_Not_Freeze (Prefix (N));
19132 Analyze_And_Resolve (N);
19133 T := Etype (N);
19134 R := N;
19136 -- If expander is inactive, type is legal, nothing else to construct
19138 else
19139 return;
19140 end if;
19141 end if;
19143 if not Is_Discrete_Type (T) then
19144 Error_Msg_N ("discrete type required for range", N);
19145 Set_Etype (N, Any_Type);
19146 return;
19148 elsif T = Any_Type then
19149 Set_Etype (N, Any_Type);
19150 return;
19151 end if;
19153 -- We will now create the appropriate Itype to describe the range, but
19154 -- first a check. If we originally had a subtype, then we just label
19155 -- the range with this subtype. Not only is there no need to construct
19156 -- a new subtype, but it is wrong to do so for two reasons:
19158 -- 1. A legality concern, if we have a subtype, it must not freeze,
19159 -- and the Itype would cause freezing incorrectly
19161 -- 2. An efficiency concern, if we created an Itype, it would not be
19162 -- recognized as the same type for the purposes of eliminating
19163 -- checks in some circumstances.
19165 -- We signal this case by setting the subtype entity in Def_Id
19167 if No (Def_Id) then
19168 Def_Id :=
19169 Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
19170 Set_Etype (Def_Id, Base_Type (T));
19172 if Is_Signed_Integer_Type (T) then
19173 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
19175 elsif Is_Modular_Integer_Type (T) then
19176 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
19178 else
19179 Set_Ekind (Def_Id, E_Enumeration_Subtype);
19180 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
19181 Set_First_Literal (Def_Id, First_Literal (T));
19182 end if;
19184 Set_Size_Info (Def_Id, (T));
19185 Set_RM_Size (Def_Id, RM_Size (T));
19186 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
19188 Set_Scalar_Range (Def_Id, R);
19189 Conditional_Delay (Def_Id, T);
19191 if Nkind (N) = N_Subtype_Indication then
19192 Inherit_Predicate_Flags (Def_Id, Entity (Subtype_Mark (N)));
19193 end if;
19195 -- In the subtype indication case, if the immediate parent of the
19196 -- new subtype is non-static, then the subtype we create is non-
19197 -- static, even if its bounds are static.
19199 if Nkind (N) = N_Subtype_Indication
19200 and then not Is_OK_Static_Subtype (Entity (Subtype_Mark (N)))
19201 then
19202 Set_Is_Non_Static_Subtype (Def_Id);
19203 end if;
19204 end if;
19206 -- Final step is to label the index with this constructed type
19208 Set_Etype (N, Def_Id);
19209 end Make_Index;
19211 ------------------------------
19212 -- Modular_Type_Declaration --
19213 ------------------------------
19215 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
19216 Mod_Expr : constant Node_Id := Expression (Def);
19217 M_Val : Uint;
19219 procedure Set_Modular_Size (Bits : Int);
19220 -- Sets RM_Size to Bits, and Esize to normal word size above this
19222 ----------------------
19223 -- Set_Modular_Size --
19224 ----------------------
19226 procedure Set_Modular_Size (Bits : Int) is
19227 begin
19228 Set_RM_Size (T, UI_From_Int (Bits));
19230 if Bits <= 8 then
19231 Init_Esize (T, 8);
19233 elsif Bits <= 16 then
19234 Init_Esize (T, 16);
19236 elsif Bits <= 32 then
19237 Init_Esize (T, 32);
19239 else
19240 Init_Esize (T, System_Max_Binary_Modulus_Power);
19241 end if;
19243 if not Non_Binary_Modulus (T) and then Esize (T) = RM_Size (T) then
19244 Set_Is_Known_Valid (T);
19245 end if;
19246 end Set_Modular_Size;
19248 -- Start of processing for Modular_Type_Declaration
19250 begin
19251 -- If the mod expression is (exactly) 2 * literal, where literal is
19252 -- 64 or less,then almost certainly the * was meant to be **. Warn.
19254 if Warn_On_Suspicious_Modulus_Value
19255 and then Nkind (Mod_Expr) = N_Op_Multiply
19256 and then Nkind (Left_Opnd (Mod_Expr)) = N_Integer_Literal
19257 and then Intval (Left_Opnd (Mod_Expr)) = Uint_2
19258 and then Nkind (Right_Opnd (Mod_Expr)) = N_Integer_Literal
19259 and then Intval (Right_Opnd (Mod_Expr)) <= Uint_64
19260 then
19261 Error_Msg_N
19262 ("suspicious MOD value, was '*'* intended'??M?", Mod_Expr);
19263 end if;
19265 -- Proceed with analysis of mod expression
19267 Analyze_And_Resolve (Mod_Expr, Any_Integer);
19268 Set_Etype (T, T);
19269 Set_Ekind (T, E_Modular_Integer_Type);
19270 Init_Alignment (T);
19271 Set_Is_Constrained (T);
19273 if not Is_OK_Static_Expression (Mod_Expr) then
19274 Flag_Non_Static_Expr
19275 ("non-static expression used for modular type bound!", Mod_Expr);
19276 M_Val := 2 ** System_Max_Binary_Modulus_Power;
19277 else
19278 M_Val := Expr_Value (Mod_Expr);
19279 end if;
19281 if M_Val < 1 then
19282 Error_Msg_N ("modulus value must be positive", Mod_Expr);
19283 M_Val := 2 ** System_Max_Binary_Modulus_Power;
19284 end if;
19286 if M_Val > 2 ** Standard_Long_Integer_Size then
19287 Check_Restriction (No_Long_Long_Integers, Mod_Expr);
19288 end if;
19290 Set_Modulus (T, M_Val);
19292 -- Create bounds for the modular type based on the modulus given in
19293 -- the type declaration and then analyze and resolve those bounds.
19295 Set_Scalar_Range (T,
19296 Make_Range (Sloc (Mod_Expr),
19297 Low_Bound => Make_Integer_Literal (Sloc (Mod_Expr), 0),
19298 High_Bound => Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
19300 -- Properly analyze the literals for the range. We do this manually
19301 -- because we can't go calling Resolve, since we are resolving these
19302 -- bounds with the type, and this type is certainly not complete yet.
19304 Set_Etype (Low_Bound (Scalar_Range (T)), T);
19305 Set_Etype (High_Bound (Scalar_Range (T)), T);
19306 Set_Is_Static_Expression (Low_Bound (Scalar_Range (T)));
19307 Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
19309 -- Loop through powers of two to find number of bits required
19311 for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
19313 -- Binary case
19315 if M_Val = 2 ** Bits then
19316 Set_Modular_Size (Bits);
19317 return;
19319 -- Nonbinary case
19321 elsif M_Val < 2 ** Bits then
19322 Check_SPARK_05_Restriction ("modulus should be a power of 2", T);
19323 Set_Non_Binary_Modulus (T);
19325 if Bits > System_Max_Nonbinary_Modulus_Power then
19326 Error_Msg_Uint_1 :=
19327 UI_From_Int (System_Max_Nonbinary_Modulus_Power);
19328 Error_Msg_F
19329 ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
19330 Set_Modular_Size (System_Max_Binary_Modulus_Power);
19331 return;
19333 else
19334 -- In the nonbinary case, set size as per RM 13.3(55)
19336 Set_Modular_Size (Bits);
19337 return;
19338 end if;
19339 end if;
19341 end loop;
19343 -- If we fall through, then the size exceed System.Max_Binary_Modulus
19344 -- so we just signal an error and set the maximum size.
19346 Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
19347 Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
19349 Set_Modular_Size (System_Max_Binary_Modulus_Power);
19350 Init_Alignment (T);
19352 end Modular_Type_Declaration;
19354 --------------------------
19355 -- New_Concatenation_Op --
19356 --------------------------
19358 procedure New_Concatenation_Op (Typ : Entity_Id) is
19359 Loc : constant Source_Ptr := Sloc (Typ);
19360 Op : Entity_Id;
19362 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
19363 -- Create abbreviated declaration for the formal of a predefined
19364 -- Operator 'Op' of type 'Typ'
19366 --------------------
19367 -- Make_Op_Formal --
19368 --------------------
19370 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
19371 Formal : Entity_Id;
19372 begin
19373 Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
19374 Set_Etype (Formal, Typ);
19375 Set_Mechanism (Formal, Default_Mechanism);
19376 return Formal;
19377 end Make_Op_Formal;
19379 -- Start of processing for New_Concatenation_Op
19381 begin
19382 Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
19384 Set_Ekind (Op, E_Operator);
19385 Set_Scope (Op, Current_Scope);
19386 Set_Etype (Op, Typ);
19387 Set_Homonym (Op, Get_Name_Entity_Id (Name_Op_Concat));
19388 Set_Is_Immediately_Visible (Op);
19389 Set_Is_Intrinsic_Subprogram (Op);
19390 Set_Has_Completion (Op);
19391 Append_Entity (Op, Current_Scope);
19393 Set_Name_Entity_Id (Name_Op_Concat, Op);
19395 Append_Entity (Make_Op_Formal (Typ, Op), Op);
19396 Append_Entity (Make_Op_Formal (Typ, Op), Op);
19397 end New_Concatenation_Op;
19399 -------------------------
19400 -- OK_For_Limited_Init --
19401 -------------------------
19403 -- ???Check all calls of this, and compare the conditions under which it's
19404 -- called.
19406 function OK_For_Limited_Init
19407 (Typ : Entity_Id;
19408 Exp : Node_Id) return Boolean
19410 begin
19411 return Is_CPP_Constructor_Call (Exp)
19412 or else (Ada_Version >= Ada_2005
19413 and then not Debug_Flag_Dot_L
19414 and then OK_For_Limited_Init_In_05 (Typ, Exp));
19415 end OK_For_Limited_Init;
19417 -------------------------------
19418 -- OK_For_Limited_Init_In_05 --
19419 -------------------------------
19421 function OK_For_Limited_Init_In_05
19422 (Typ : Entity_Id;
19423 Exp : Node_Id) return Boolean
19425 begin
19426 -- An object of a limited interface type can be initialized with any
19427 -- expression of a nonlimited descendant type. However this does not
19428 -- apply if this is a view conversion of some other expression. This
19429 -- is checked below.
19431 if Is_Class_Wide_Type (Typ)
19432 and then Is_Limited_Interface (Typ)
19433 and then not Is_Limited_Type (Etype (Exp))
19434 and then Nkind (Exp) /= N_Type_Conversion
19435 then
19436 return True;
19437 end if;
19439 -- Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
19440 -- case of limited aggregates (including extension aggregates), and
19441 -- function calls. The function call may have been given in prefixed
19442 -- notation, in which case the original node is an indexed component.
19443 -- If the function is parameterless, the original node was an explicit
19444 -- dereference. The function may also be parameterless, in which case
19445 -- the source node is just an identifier.
19447 -- A branch of a conditional expression may have been removed if the
19448 -- condition is statically known. This happens during expansion, and
19449 -- thus will not happen if previous errors were encountered. The check
19450 -- will have been performed on the chosen branch, which replaces the
19451 -- original conditional expression.
19453 if No (Exp) then
19454 return True;
19455 end if;
19457 case Nkind (Original_Node (Exp)) is
19458 when N_Aggregate
19459 | N_Extension_Aggregate
19460 | N_Function_Call
19461 | N_Op
19463 return True;
19465 when N_Identifier =>
19466 return Present (Entity (Original_Node (Exp)))
19467 and then Ekind (Entity (Original_Node (Exp))) = E_Function;
19469 when N_Qualified_Expression =>
19470 return
19471 OK_For_Limited_Init_In_05
19472 (Typ, Expression (Original_Node (Exp)));
19474 -- Ada 2005 (AI-251): If a class-wide interface object is initialized
19475 -- with a function call, the expander has rewritten the call into an
19476 -- N_Type_Conversion node to force displacement of the pointer to
19477 -- reference the component containing the secondary dispatch table.
19478 -- Otherwise a type conversion is not a legal context.
19479 -- A return statement for a build-in-place function returning a
19480 -- synchronized type also introduces an unchecked conversion.
19482 when N_Type_Conversion
19483 | N_Unchecked_Type_Conversion
19485 return not Comes_From_Source (Exp)
19486 and then
19487 OK_For_Limited_Init_In_05
19488 (Typ, Expression (Original_Node (Exp)));
19490 when N_Explicit_Dereference
19491 | N_Indexed_Component
19492 | N_Selected_Component
19494 return Nkind (Exp) = N_Function_Call;
19496 -- A use of 'Input is a function call, hence allowed. Normally the
19497 -- attribute will be changed to a call, but the attribute by itself
19498 -- can occur with -gnatc.
19500 when N_Attribute_Reference =>
19501 return Attribute_Name (Original_Node (Exp)) = Name_Input;
19503 -- "return raise ..." is OK
19505 when N_Raise_Expression =>
19506 return True;
19508 -- For a case expression, all dependent expressions must be legal
19510 when N_Case_Expression =>
19511 declare
19512 Alt : Node_Id;
19514 begin
19515 Alt := First (Alternatives (Original_Node (Exp)));
19516 while Present (Alt) loop
19517 if not OK_For_Limited_Init_In_05 (Typ, Expression (Alt)) then
19518 return False;
19519 end if;
19521 Next (Alt);
19522 end loop;
19524 return True;
19525 end;
19527 -- For an if expression, all dependent expressions must be legal
19529 when N_If_Expression =>
19530 declare
19531 Then_Expr : constant Node_Id :=
19532 Next (First (Expressions (Original_Node (Exp))));
19533 Else_Expr : constant Node_Id := Next (Then_Expr);
19534 begin
19535 return OK_For_Limited_Init_In_05 (Typ, Then_Expr)
19536 and then
19537 OK_For_Limited_Init_In_05 (Typ, Else_Expr);
19538 end;
19540 when others =>
19541 return False;
19542 end case;
19543 end OK_For_Limited_Init_In_05;
19545 -------------------------------------------
19546 -- Ordinary_Fixed_Point_Type_Declaration --
19547 -------------------------------------------
19549 procedure Ordinary_Fixed_Point_Type_Declaration
19550 (T : Entity_Id;
19551 Def : Node_Id)
19553 Loc : constant Source_Ptr := Sloc (Def);
19554 Delta_Expr : constant Node_Id := Delta_Expression (Def);
19555 RRS : constant Node_Id := Real_Range_Specification (Def);
19556 Implicit_Base : Entity_Id;
19557 Delta_Val : Ureal;
19558 Small_Val : Ureal;
19559 Low_Val : Ureal;
19560 High_Val : Ureal;
19562 begin
19563 Check_Restriction (No_Fixed_Point, Def);
19565 -- Create implicit base type
19567 Implicit_Base :=
19568 Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
19569 Set_Etype (Implicit_Base, Implicit_Base);
19571 -- Analyze and process delta expression
19573 Analyze_And_Resolve (Delta_Expr, Any_Real);
19575 Check_Delta_Expression (Delta_Expr);
19576 Delta_Val := Expr_Value_R (Delta_Expr);
19578 Set_Delta_Value (Implicit_Base, Delta_Val);
19580 -- Compute default small from given delta, which is the largest power
19581 -- of two that does not exceed the given delta value.
19583 declare
19584 Tmp : Ureal;
19585 Scale : Int;
19587 begin
19588 Tmp := Ureal_1;
19589 Scale := 0;
19591 if Delta_Val < Ureal_1 then
19592 while Delta_Val < Tmp loop
19593 Tmp := Tmp / Ureal_2;
19594 Scale := Scale + 1;
19595 end loop;
19597 else
19598 loop
19599 Tmp := Tmp * Ureal_2;
19600 exit when Tmp > Delta_Val;
19601 Scale := Scale - 1;
19602 end loop;
19603 end if;
19605 Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
19606 end;
19608 Set_Small_Value (Implicit_Base, Small_Val);
19610 -- If no range was given, set a dummy range
19612 if RRS <= Empty_Or_Error then
19613 Low_Val := -Small_Val;
19614 High_Val := Small_Val;
19616 -- Otherwise analyze and process given range
19618 else
19619 declare
19620 Low : constant Node_Id := Low_Bound (RRS);
19621 High : constant Node_Id := High_Bound (RRS);
19623 begin
19624 Analyze_And_Resolve (Low, Any_Real);
19625 Analyze_And_Resolve (High, Any_Real);
19626 Check_Real_Bound (Low);
19627 Check_Real_Bound (High);
19629 -- Obtain and set the range
19631 Low_Val := Expr_Value_R (Low);
19632 High_Val := Expr_Value_R (High);
19634 if Low_Val > High_Val then
19635 Error_Msg_NE ("??fixed point type& has null range", Def, T);
19636 end if;
19637 end;
19638 end if;
19640 -- The range for both the implicit base and the declared first subtype
19641 -- cannot be set yet, so we use the special routine Set_Fixed_Range to
19642 -- set a temporary range in place. Note that the bounds of the base
19643 -- type will be widened to be symmetrical and to fill the available
19644 -- bits when the type is frozen.
19646 -- We could do this with all discrete types, and probably should, but
19647 -- we absolutely have to do it for fixed-point, since the end-points
19648 -- of the range and the size are determined by the small value, which
19649 -- could be reset before the freeze point.
19651 Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
19652 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
19654 -- Complete definition of first subtype. The inheritance of the rep item
19655 -- chain ensures that SPARK-related pragmas are not clobbered when the
19656 -- ordinary fixed point type acts as a full view of a private type.
19658 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
19659 Set_Etype (T, Implicit_Base);
19660 Init_Size_Align (T);
19661 Inherit_Rep_Item_Chain (T, Implicit_Base);
19662 Set_Small_Value (T, Small_Val);
19663 Set_Delta_Value (T, Delta_Val);
19664 Set_Is_Constrained (T);
19665 end Ordinary_Fixed_Point_Type_Declaration;
19667 ----------------------------------
19668 -- Preanalyze_Assert_Expression --
19669 ----------------------------------
19671 procedure Preanalyze_Assert_Expression (N : Node_Id; T : Entity_Id) is
19672 begin
19673 In_Assertion_Expr := In_Assertion_Expr + 1;
19674 Preanalyze_Spec_Expression (N, T);
19675 In_Assertion_Expr := In_Assertion_Expr - 1;
19676 end Preanalyze_Assert_Expression;
19678 -----------------------------------
19679 -- Preanalyze_Default_Expression --
19680 -----------------------------------
19682 procedure Preanalyze_Default_Expression (N : Node_Id; T : Entity_Id) is
19683 Save_In_Default_Expr : constant Boolean := In_Default_Expr;
19684 begin
19685 In_Default_Expr := True;
19686 Preanalyze_Spec_Expression (N, T);
19687 In_Default_Expr := Save_In_Default_Expr;
19688 end Preanalyze_Default_Expression;
19690 --------------------------------
19691 -- Preanalyze_Spec_Expression --
19692 --------------------------------
19694 procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
19695 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
19696 begin
19697 In_Spec_Expression := True;
19698 Preanalyze_And_Resolve (N, T);
19699 In_Spec_Expression := Save_In_Spec_Expression;
19700 end Preanalyze_Spec_Expression;
19702 ----------------------------------------
19703 -- Prepare_Private_Subtype_Completion --
19704 ----------------------------------------
19706 procedure Prepare_Private_Subtype_Completion
19707 (Id : Entity_Id;
19708 Related_Nod : Node_Id)
19710 Id_B : constant Entity_Id := Base_Type (Id);
19711 Full_B : Entity_Id := Full_View (Id_B);
19712 Full : Entity_Id;
19714 begin
19715 if Present (Full_B) then
19717 -- Get to the underlying full view if necessary
19719 if Is_Private_Type (Full_B)
19720 and then Present (Underlying_Full_View (Full_B))
19721 then
19722 Full_B := Underlying_Full_View (Full_B);
19723 end if;
19725 -- The Base_Type is already completed, we can complete the subtype
19726 -- now. We have to create a new entity with the same name, Thus we
19727 -- can't use Create_Itype.
19729 Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
19730 Set_Is_Itype (Full);
19731 Set_Associated_Node_For_Itype (Full, Related_Nod);
19732 Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
19733 end if;
19735 -- The parent subtype may be private, but the base might not, in some
19736 -- nested instances. In that case, the subtype does not need to be
19737 -- exchanged. It would still be nice to make private subtypes and their
19738 -- bases consistent at all times ???
19740 if Is_Private_Type (Id_B) then
19741 Append_Elmt (Id, Private_Dependents (Id_B));
19742 end if;
19743 end Prepare_Private_Subtype_Completion;
19745 ---------------------------
19746 -- Process_Discriminants --
19747 ---------------------------
19749 procedure Process_Discriminants
19750 (N : Node_Id;
19751 Prev : Entity_Id := Empty)
19753 Elist : constant Elist_Id := New_Elmt_List;
19754 Id : Node_Id;
19755 Discr : Node_Id;
19756 Discr_Number : Uint;
19757 Discr_Type : Entity_Id;
19758 Default_Present : Boolean := False;
19759 Default_Not_Present : Boolean := False;
19761 begin
19762 -- A composite type other than an array type can have discriminants.
19763 -- On entry, the current scope is the composite type.
19765 -- The discriminants are initially entered into the scope of the type
19766 -- via Enter_Name with the default Ekind of E_Void to prevent premature
19767 -- use, as explained at the end of this procedure.
19769 Discr := First (Discriminant_Specifications (N));
19770 while Present (Discr) loop
19771 Enter_Name (Defining_Identifier (Discr));
19773 -- For navigation purposes we add a reference to the discriminant
19774 -- in the entity for the type. If the current declaration is a
19775 -- completion, place references on the partial view. Otherwise the
19776 -- type is the current scope.
19778 if Present (Prev) then
19780 -- The references go on the partial view, if present. If the
19781 -- partial view has discriminants, the references have been
19782 -- generated already.
19784 if not Has_Discriminants (Prev) then
19785 Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
19786 end if;
19787 else
19788 Generate_Reference
19789 (Current_Scope, Defining_Identifier (Discr), 'd');
19790 end if;
19792 if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
19793 Discr_Type := Access_Definition (Discr, Discriminant_Type (Discr));
19795 -- Ada 2005 (AI-254)
19797 if Present (Access_To_Subprogram_Definition
19798 (Discriminant_Type (Discr)))
19799 and then Protected_Present (Access_To_Subprogram_Definition
19800 (Discriminant_Type (Discr)))
19801 then
19802 Discr_Type :=
19803 Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
19804 end if;
19806 else
19807 Find_Type (Discriminant_Type (Discr));
19808 Discr_Type := Etype (Discriminant_Type (Discr));
19810 if Error_Posted (Discriminant_Type (Discr)) then
19811 Discr_Type := Any_Type;
19812 end if;
19813 end if;
19815 -- Handling of discriminants that are access types
19817 if Is_Access_Type (Discr_Type) then
19819 -- Ada 2005 (AI-230): Access discriminant allowed in non-
19820 -- limited record types
19822 if Ada_Version < Ada_2005 then
19823 Check_Access_Discriminant_Requires_Limited
19824 (Discr, Discriminant_Type (Discr));
19825 end if;
19827 if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
19828 Error_Msg_N
19829 ("(Ada 83) access discriminant not allowed", Discr);
19830 end if;
19832 -- If not access type, must be a discrete type
19834 elsif not Is_Discrete_Type (Discr_Type) then
19835 Error_Msg_N
19836 ("discriminants must have a discrete or access type",
19837 Discriminant_Type (Discr));
19838 end if;
19840 Set_Etype (Defining_Identifier (Discr), Discr_Type);
19842 -- If a discriminant specification includes the assignment compound
19843 -- delimiter followed by an expression, the expression is the default
19844 -- expression of the discriminant; the default expression must be of
19845 -- the type of the discriminant. (RM 3.7.1) Since this expression is
19846 -- a default expression, we do the special preanalysis, since this
19847 -- expression does not freeze (see section "Handling of Default and
19848 -- Per-Object Expressions" in spec of package Sem).
19850 if Present (Expression (Discr)) then
19851 Preanalyze_Spec_Expression (Expression (Discr), Discr_Type);
19853 -- Legaity checks
19855 if Nkind (N) = N_Formal_Type_Declaration then
19856 Error_Msg_N
19857 ("discriminant defaults not allowed for formal type",
19858 Expression (Discr));
19860 -- Flag an error for a tagged type with defaulted discriminants,
19861 -- excluding limited tagged types when compiling for Ada 2012
19862 -- (see AI05-0214).
19864 elsif Is_Tagged_Type (Current_Scope)
19865 and then (not Is_Limited_Type (Current_Scope)
19866 or else Ada_Version < Ada_2012)
19867 and then Comes_From_Source (N)
19868 then
19869 -- Note: see similar test in Check_Or_Process_Discriminants, to
19870 -- handle the (illegal) case of the completion of an untagged
19871 -- view with discriminants with defaults by a tagged full view.
19872 -- We skip the check if Discr does not come from source, to
19873 -- account for the case of an untagged derived type providing
19874 -- defaults for a renamed discriminant from a private untagged
19875 -- ancestor with a tagged full view (ACATS B460006).
19877 if Ada_Version >= Ada_2012 then
19878 Error_Msg_N
19879 ("discriminants of nonlimited tagged type cannot have"
19880 & " defaults",
19881 Expression (Discr));
19882 else
19883 Error_Msg_N
19884 ("discriminants of tagged type cannot have defaults",
19885 Expression (Discr));
19886 end if;
19888 else
19889 Default_Present := True;
19890 Append_Elmt (Expression (Discr), Elist);
19892 -- Tag the defining identifiers for the discriminants with
19893 -- their corresponding default expressions from the tree.
19895 Set_Discriminant_Default_Value
19896 (Defining_Identifier (Discr), Expression (Discr));
19897 end if;
19899 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag
19900 -- gets set unless we can be sure that no range check is required.
19902 if (GNATprove_Mode or not Expander_Active)
19903 and then not
19904 Is_In_Range
19905 (Expression (Discr), Discr_Type, Assume_Valid => True)
19906 then
19907 Set_Do_Range_Check (Expression (Discr));
19908 end if;
19910 -- No default discriminant value given
19912 else
19913 Default_Not_Present := True;
19914 end if;
19916 -- Ada 2005 (AI-231): Create an Itype that is a duplicate of
19917 -- Discr_Type but with the null-exclusion attribute
19919 if Ada_Version >= Ada_2005 then
19921 -- Ada 2005 (AI-231): Static checks
19923 if Can_Never_Be_Null (Discr_Type) then
19924 Null_Exclusion_Static_Checks (Discr);
19926 elsif Is_Access_Type (Discr_Type)
19927 and then Null_Exclusion_Present (Discr)
19929 -- No need to check itypes because in their case this check
19930 -- was done at their point of creation
19932 and then not Is_Itype (Discr_Type)
19933 then
19934 if Can_Never_Be_Null (Discr_Type) then
19935 Error_Msg_NE
19936 ("`NOT NULL` not allowed (& already excludes null)",
19937 Discr,
19938 Discr_Type);
19939 end if;
19941 Set_Etype (Defining_Identifier (Discr),
19942 Create_Null_Excluding_Itype
19943 (T => Discr_Type,
19944 Related_Nod => Discr));
19946 -- Check for improper null exclusion if the type is otherwise
19947 -- legal for a discriminant.
19949 elsif Null_Exclusion_Present (Discr)
19950 and then Is_Discrete_Type (Discr_Type)
19951 then
19952 Error_Msg_N
19953 ("null exclusion can only apply to an access type", Discr);
19954 end if;
19956 -- Ada 2005 (AI-402): access discriminants of nonlimited types
19957 -- can't have defaults. Synchronized types, or types that are
19958 -- explicitly limited are fine, but special tests apply to derived
19959 -- types in generics: in a generic body we have to assume the
19960 -- worst, and therefore defaults are not allowed if the parent is
19961 -- a generic formal private type (see ACATS B370001).
19963 if Is_Access_Type (Discr_Type) and then Default_Present then
19964 if Ekind (Discr_Type) /= E_Anonymous_Access_Type
19965 or else Is_Limited_Record (Current_Scope)
19966 or else Is_Concurrent_Type (Current_Scope)
19967 or else Is_Concurrent_Record_Type (Current_Scope)
19968 or else Ekind (Current_Scope) = E_Limited_Private_Type
19969 then
19970 if not Is_Derived_Type (Current_Scope)
19971 or else not Is_Generic_Type (Etype (Current_Scope))
19972 or else not In_Package_Body (Scope (Etype (Current_Scope)))
19973 or else Limited_Present
19974 (Type_Definition (Parent (Current_Scope)))
19975 then
19976 null;
19978 else
19979 Error_Msg_N
19980 ("access discriminants of nonlimited types cannot "
19981 & "have defaults", Expression (Discr));
19982 end if;
19984 elsif Present (Expression (Discr)) then
19985 Error_Msg_N
19986 ("(Ada 2005) access discriminants of nonlimited types "
19987 & "cannot have defaults", Expression (Discr));
19988 end if;
19989 end if;
19990 end if;
19992 -- A discriminant cannot be effectively volatile (SPARK RM 7.1.3(6)).
19993 -- This check is relevant only when SPARK_Mode is on as it is not a
19994 -- standard Ada legality rule.
19996 if SPARK_Mode = On
19997 and then Is_Effectively_Volatile (Defining_Identifier (Discr))
19998 then
19999 Error_Msg_N ("discriminant cannot be volatile", Discr);
20000 end if;
20002 Next (Discr);
20003 end loop;
20005 -- An element list consisting of the default expressions of the
20006 -- discriminants is constructed in the above loop and used to set
20007 -- the Discriminant_Constraint attribute for the type. If an object
20008 -- is declared of this (record or task) type without any explicit
20009 -- discriminant constraint given, this element list will form the
20010 -- actual parameters for the corresponding initialization procedure
20011 -- for the type.
20013 Set_Discriminant_Constraint (Current_Scope, Elist);
20014 Set_Stored_Constraint (Current_Scope, No_Elist);
20016 -- Default expressions must be provided either for all or for none
20017 -- of the discriminants of a discriminant part. (RM 3.7.1)
20019 if Default_Present and then Default_Not_Present then
20020 Error_Msg_N
20021 ("incomplete specification of defaults for discriminants", N);
20022 end if;
20024 -- The use of the name of a discriminant is not allowed in default
20025 -- expressions of a discriminant part if the specification of the
20026 -- discriminant is itself given in the discriminant part. (RM 3.7.1)
20028 -- To detect this, the discriminant names are entered initially with an
20029 -- Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
20030 -- attempt to use a void entity (for example in an expression that is
20031 -- type-checked) produces the error message: premature usage. Now after
20032 -- completing the semantic analysis of the discriminant part, we can set
20033 -- the Ekind of all the discriminants appropriately.
20035 Discr := First (Discriminant_Specifications (N));
20036 Discr_Number := Uint_1;
20037 while Present (Discr) loop
20038 Id := Defining_Identifier (Discr);
20039 Set_Ekind (Id, E_Discriminant);
20040 Init_Component_Location (Id);
20041 Init_Esize (Id);
20042 Set_Discriminant_Number (Id, Discr_Number);
20044 -- Make sure this is always set, even in illegal programs
20046 Set_Corresponding_Discriminant (Id, Empty);
20048 -- Initialize the Original_Record_Component to the entity itself.
20049 -- Inherit_Components will propagate the right value to
20050 -- discriminants in derived record types.
20052 Set_Original_Record_Component (Id, Id);
20054 -- Create the discriminal for the discriminant
20056 Build_Discriminal (Id);
20058 Next (Discr);
20059 Discr_Number := Discr_Number + 1;
20060 end loop;
20062 Set_Has_Discriminants (Current_Scope);
20063 end Process_Discriminants;
20065 -----------------------
20066 -- Process_Full_View --
20067 -----------------------
20069 -- WARNING: This routine manages Ghost regions. Return statements must be
20070 -- replaced by gotos which jump to the end of the routine and restore the
20071 -- Ghost mode.
20073 procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
20074 procedure Collect_Implemented_Interfaces
20075 (Typ : Entity_Id;
20076 Ifaces : Elist_Id);
20077 -- Ada 2005: Gather all the interfaces that Typ directly or
20078 -- inherently implements. Duplicate entries are not added to
20079 -- the list Ifaces.
20081 ------------------------------------
20082 -- Collect_Implemented_Interfaces --
20083 ------------------------------------
20085 procedure Collect_Implemented_Interfaces
20086 (Typ : Entity_Id;
20087 Ifaces : Elist_Id)
20089 Iface : Entity_Id;
20090 Iface_Elmt : Elmt_Id;
20092 begin
20093 -- Abstract interfaces are only associated with tagged record types
20095 if not Is_Tagged_Type (Typ) or else not Is_Record_Type (Typ) then
20096 return;
20097 end if;
20099 -- Recursively climb to the ancestors
20101 if Etype (Typ) /= Typ
20103 -- Protect the frontend against wrong cyclic declarations like:
20105 -- type B is new A with private;
20106 -- type C is new A with private;
20107 -- private
20108 -- type B is new C with null record;
20109 -- type C is new B with null record;
20111 and then Etype (Typ) /= Priv_T
20112 and then Etype (Typ) /= Full_T
20113 then
20114 -- Keep separate the management of private type declarations
20116 if Ekind (Typ) = E_Record_Type_With_Private then
20118 -- Handle the following illegal usage:
20119 -- type Private_Type is tagged private;
20120 -- private
20121 -- type Private_Type is new Type_Implementing_Iface;
20123 if Present (Full_View (Typ))
20124 and then Etype (Typ) /= Full_View (Typ)
20125 then
20126 if Is_Interface (Etype (Typ)) then
20127 Append_Unique_Elmt (Etype (Typ), Ifaces);
20128 end if;
20130 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
20131 end if;
20133 -- Non-private types
20135 else
20136 if Is_Interface (Etype (Typ)) then
20137 Append_Unique_Elmt (Etype (Typ), Ifaces);
20138 end if;
20140 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
20141 end if;
20142 end if;
20144 -- Handle entities in the list of abstract interfaces
20146 if Present (Interfaces (Typ)) then
20147 Iface_Elmt := First_Elmt (Interfaces (Typ));
20148 while Present (Iface_Elmt) loop
20149 Iface := Node (Iface_Elmt);
20151 pragma Assert (Is_Interface (Iface));
20153 if not Contain_Interface (Iface, Ifaces) then
20154 Append_Elmt (Iface, Ifaces);
20155 Collect_Implemented_Interfaces (Iface, Ifaces);
20156 end if;
20158 Next_Elmt (Iface_Elmt);
20159 end loop;
20160 end if;
20161 end Collect_Implemented_Interfaces;
20163 -- Local variables
20165 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
20167 Full_Indic : Node_Id;
20168 Full_Parent : Entity_Id;
20169 Priv_Parent : Entity_Id;
20171 -- Start of processing for Process_Full_View
20173 begin
20174 Mark_And_Set_Ghost_Completion (N, Priv_T);
20176 -- First some sanity checks that must be done after semantic
20177 -- decoration of the full view and thus cannot be placed with other
20178 -- similar checks in Find_Type_Name
20180 if not Is_Limited_Type (Priv_T)
20181 and then (Is_Limited_Type (Full_T)
20182 or else Is_Limited_Composite (Full_T))
20183 then
20184 if In_Instance then
20185 null;
20186 else
20187 Error_Msg_N
20188 ("completion of nonlimited type cannot be limited", Full_T);
20189 Explain_Limited_Type (Full_T, Full_T);
20190 end if;
20192 elsif Is_Abstract_Type (Full_T)
20193 and then not Is_Abstract_Type (Priv_T)
20194 then
20195 Error_Msg_N
20196 ("completion of nonabstract type cannot be abstract", Full_T);
20198 elsif Is_Tagged_Type (Priv_T)
20199 and then Is_Limited_Type (Priv_T)
20200 and then not Is_Limited_Type (Full_T)
20201 then
20202 -- If pragma CPP_Class was applied to the private declaration
20203 -- propagate the limitedness to the full-view
20205 if Is_CPP_Class (Priv_T) then
20206 Set_Is_Limited_Record (Full_T);
20208 -- GNAT allow its own definition of Limited_Controlled to disobey
20209 -- this rule in order in ease the implementation. This test is safe
20210 -- because Root_Controlled is defined in a child of System that
20211 -- normal programs are not supposed to use.
20213 elsif Is_RTE (Etype (Full_T), RE_Root_Controlled) then
20214 Set_Is_Limited_Composite (Full_T);
20215 else
20216 Error_Msg_N
20217 ("completion of limited tagged type must be limited", Full_T);
20218 end if;
20220 elsif Is_Generic_Type (Priv_T) then
20221 Error_Msg_N ("generic type cannot have a completion", Full_T);
20222 end if;
20224 -- Check that ancestor interfaces of private and full views are
20225 -- consistent. We omit this check for synchronized types because
20226 -- they are performed on the corresponding record type when frozen.
20228 if Ada_Version >= Ada_2005
20229 and then Is_Tagged_Type (Priv_T)
20230 and then Is_Tagged_Type (Full_T)
20231 and then not Is_Concurrent_Type (Full_T)
20232 then
20233 declare
20234 Iface : Entity_Id;
20235 Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
20236 Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
20238 begin
20239 Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
20240 Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
20242 -- Ada 2005 (AI-251): The partial view shall be a descendant of
20243 -- an interface type if and only if the full type is descendant
20244 -- of the interface type (AARM 7.3 (7.3/2)).
20246 Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
20248 if Present (Iface) then
20249 Error_Msg_NE
20250 ("interface in partial view& not implemented by full type "
20251 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
20252 end if;
20254 Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
20256 if Present (Iface) then
20257 Error_Msg_NE
20258 ("interface & not implemented by partial view "
20259 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
20260 end if;
20261 end;
20262 end if;
20264 if Is_Tagged_Type (Priv_T)
20265 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
20266 and then Is_Derived_Type (Full_T)
20267 then
20268 Priv_Parent := Etype (Priv_T);
20270 -- The full view of a private extension may have been transformed
20271 -- into an unconstrained derived type declaration and a subtype
20272 -- declaration (see build_derived_record_type for details).
20274 if Nkind (N) = N_Subtype_Declaration then
20275 Full_Indic := Subtype_Indication (N);
20276 Full_Parent := Etype (Base_Type (Full_T));
20277 else
20278 Full_Indic := Subtype_Indication (Type_Definition (N));
20279 Full_Parent := Etype (Full_T);
20280 end if;
20282 -- Check that the parent type of the full type is a descendant of
20283 -- the ancestor subtype given in the private extension. If either
20284 -- entity has an Etype equal to Any_Type then we had some previous
20285 -- error situation [7.3(8)].
20287 if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
20288 goto Leave;
20290 -- Ada 2005 (AI-251): Interfaces in the full type can be given in
20291 -- any order. Therefore we don't have to check that its parent must
20292 -- be a descendant of the parent of the private type declaration.
20294 elsif Is_Interface (Priv_Parent)
20295 and then Is_Interface (Full_Parent)
20296 then
20297 null;
20299 -- Ada 2005 (AI-251): If the parent of the private type declaration
20300 -- is an interface there is no need to check that it is an ancestor
20301 -- of the associated full type declaration. The required tests for
20302 -- this case are performed by Build_Derived_Record_Type.
20304 elsif not Is_Interface (Base_Type (Priv_Parent))
20305 and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
20306 then
20307 Error_Msg_N
20308 ("parent of full type must descend from parent of private "
20309 & "extension", Full_Indic);
20311 -- First check a formal restriction, and then proceed with checking
20312 -- Ada rules. Since the formal restriction is not a serious error, we
20313 -- don't prevent further error detection for this check, hence the
20314 -- ELSE.
20316 else
20317 -- In formal mode, when completing a private extension the type
20318 -- named in the private part must be exactly the same as that
20319 -- named in the visible part.
20321 if Priv_Parent /= Full_Parent then
20322 Error_Msg_Name_1 := Chars (Priv_Parent);
20323 Check_SPARK_05_Restriction ("% expected", Full_Indic);
20324 end if;
20326 -- Check the rules of 7.3(10): if the private extension inherits
20327 -- known discriminants, then the full type must also inherit those
20328 -- discriminants from the same (ancestor) type, and the parent
20329 -- subtype of the full type must be constrained if and only if
20330 -- the ancestor subtype of the private extension is constrained.
20332 if No (Discriminant_Specifications (Parent (Priv_T)))
20333 and then not Has_Unknown_Discriminants (Priv_T)
20334 and then Has_Discriminants (Base_Type (Priv_Parent))
20335 then
20336 declare
20337 Priv_Indic : constant Node_Id :=
20338 Subtype_Indication (Parent (Priv_T));
20340 Priv_Constr : constant Boolean :=
20341 Is_Constrained (Priv_Parent)
20342 or else
20343 Nkind (Priv_Indic) = N_Subtype_Indication
20344 or else
20345 Is_Constrained (Entity (Priv_Indic));
20347 Full_Constr : constant Boolean :=
20348 Is_Constrained (Full_Parent)
20349 or else
20350 Nkind (Full_Indic) = N_Subtype_Indication
20351 or else
20352 Is_Constrained (Entity (Full_Indic));
20354 Priv_Discr : Entity_Id;
20355 Full_Discr : Entity_Id;
20357 begin
20358 Priv_Discr := First_Discriminant (Priv_Parent);
20359 Full_Discr := First_Discriminant (Full_Parent);
20360 while Present (Priv_Discr) and then Present (Full_Discr) loop
20361 if Original_Record_Component (Priv_Discr) =
20362 Original_Record_Component (Full_Discr)
20363 or else
20364 Corresponding_Discriminant (Priv_Discr) =
20365 Corresponding_Discriminant (Full_Discr)
20366 then
20367 null;
20368 else
20369 exit;
20370 end if;
20372 Next_Discriminant (Priv_Discr);
20373 Next_Discriminant (Full_Discr);
20374 end loop;
20376 if Present (Priv_Discr) or else Present (Full_Discr) then
20377 Error_Msg_N
20378 ("full view must inherit discriminants of the parent "
20379 & "type used in the private extension", Full_Indic);
20381 elsif Priv_Constr and then not Full_Constr then
20382 Error_Msg_N
20383 ("parent subtype of full type must be constrained",
20384 Full_Indic);
20386 elsif Full_Constr and then not Priv_Constr then
20387 Error_Msg_N
20388 ("parent subtype of full type must be unconstrained",
20389 Full_Indic);
20390 end if;
20391 end;
20393 -- Check the rules of 7.3(12): if a partial view has neither
20394 -- known or unknown discriminants, then the full type
20395 -- declaration shall define a definite subtype.
20397 elsif not Has_Unknown_Discriminants (Priv_T)
20398 and then not Has_Discriminants (Priv_T)
20399 and then not Is_Constrained (Full_T)
20400 then
20401 Error_Msg_N
20402 ("full view must define a constrained type if partial view "
20403 & "has no discriminants", Full_T);
20404 end if;
20406 -- ??????? Do we implement the following properly ?????
20407 -- If the ancestor subtype of a private extension has constrained
20408 -- discriminants, then the parent subtype of the full view shall
20409 -- impose a statically matching constraint on those discriminants
20410 -- [7.3(13)].
20411 end if;
20413 else
20414 -- For untagged types, verify that a type without discriminants is
20415 -- not completed with an unconstrained type. A separate error message
20416 -- is produced if the full type has defaulted discriminants.
20418 if Is_Definite_Subtype (Priv_T)
20419 and then not Is_Definite_Subtype (Full_T)
20420 then
20421 Error_Msg_Sloc := Sloc (Parent (Priv_T));
20422 Error_Msg_NE
20423 ("full view of& not compatible with declaration#",
20424 Full_T, Priv_T);
20426 if not Is_Tagged_Type (Full_T) then
20427 Error_Msg_N
20428 ("\one is constrained, the other unconstrained", Full_T);
20429 end if;
20430 end if;
20431 end if;
20433 -- AI-419: verify that the use of "limited" is consistent
20435 declare
20436 Orig_Decl : constant Node_Id := Original_Node (N);
20438 begin
20439 if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
20440 and then Nkind (Orig_Decl) = N_Full_Type_Declaration
20441 and then Nkind
20442 (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
20443 then
20444 if not Limited_Present (Parent (Priv_T))
20445 and then not Synchronized_Present (Parent (Priv_T))
20446 and then Limited_Present (Type_Definition (Orig_Decl))
20447 then
20448 Error_Msg_N
20449 ("full view of non-limited extension cannot be limited", N);
20451 -- Conversely, if the partial view carries the limited keyword,
20452 -- the full view must as well, even if it may be redundant.
20454 elsif Limited_Present (Parent (Priv_T))
20455 and then not Limited_Present (Type_Definition (Orig_Decl))
20456 then
20457 Error_Msg_N
20458 ("full view of limited extension must be explicitly limited",
20460 end if;
20461 end if;
20462 end;
20464 -- Ada 2005 (AI-443): A synchronized private extension must be
20465 -- completed by a task or protected type.
20467 if Ada_Version >= Ada_2005
20468 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
20469 and then Synchronized_Present (Parent (Priv_T))
20470 and then not Is_Concurrent_Type (Full_T)
20471 then
20472 Error_Msg_N ("full view of synchronized extension must " &
20473 "be synchronized type", N);
20474 end if;
20476 -- Ada 2005 AI-363: if the full view has discriminants with
20477 -- defaults, it is illegal to declare constrained access subtypes
20478 -- whose designated type is the current type. This allows objects
20479 -- of the type that are declared in the heap to be unconstrained.
20481 if not Has_Unknown_Discriminants (Priv_T)
20482 and then not Has_Discriminants (Priv_T)
20483 and then Has_Discriminants (Full_T)
20484 and then
20485 Present (Discriminant_Default_Value (First_Discriminant (Full_T)))
20486 then
20487 Set_Has_Constrained_Partial_View (Full_T);
20488 Set_Has_Constrained_Partial_View (Priv_T);
20489 end if;
20491 -- Create a full declaration for all its subtypes recorded in
20492 -- Private_Dependents and swap them similarly to the base type. These
20493 -- are subtypes that have been define before the full declaration of
20494 -- the private type. We also swap the entry in Private_Dependents list
20495 -- so we can properly restore the private view on exit from the scope.
20497 declare
20498 Priv_Elmt : Elmt_Id;
20499 Priv_Scop : Entity_Id;
20500 Priv : Entity_Id;
20501 Full : Entity_Id;
20503 begin
20504 Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
20505 while Present (Priv_Elmt) loop
20506 Priv := Node (Priv_Elmt);
20507 Priv_Scop := Scope (Priv);
20509 if Ekind_In (Priv, E_Private_Subtype,
20510 E_Limited_Private_Subtype,
20511 E_Record_Subtype_With_Private)
20512 then
20513 Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
20514 Set_Is_Itype (Full);
20515 Set_Parent (Full, Parent (Priv));
20516 Set_Associated_Node_For_Itype (Full, N);
20518 -- Now we need to complete the private subtype, but since the
20519 -- base type has already been swapped, we must also swap the
20520 -- subtypes (and thus, reverse the arguments in the call to
20521 -- Complete_Private_Subtype). Also note that we may need to
20522 -- re-establish the scope of the private subtype.
20524 Copy_And_Swap (Priv, Full);
20526 if not In_Open_Scopes (Priv_Scop) then
20527 Push_Scope (Priv_Scop);
20529 else
20530 -- Reset Priv_Scop to Empty to indicate no scope was pushed
20532 Priv_Scop := Empty;
20533 end if;
20535 Complete_Private_Subtype (Full, Priv, Full_T, N);
20537 if Present (Priv_Scop) then
20538 Pop_Scope;
20539 end if;
20541 Replace_Elmt (Priv_Elmt, Full);
20542 end if;
20544 Next_Elmt (Priv_Elmt);
20545 end loop;
20546 end;
20548 -- If the private view was tagged, copy the new primitive operations
20549 -- from the private view to the full view.
20551 if Is_Tagged_Type (Full_T) then
20552 declare
20553 Disp_Typ : Entity_Id;
20554 Full_List : Elist_Id;
20555 Prim : Entity_Id;
20556 Prim_Elmt : Elmt_Id;
20557 Priv_List : Elist_Id;
20559 function Contains
20560 (E : Entity_Id;
20561 L : Elist_Id) return Boolean;
20562 -- Determine whether list L contains element E
20564 --------------
20565 -- Contains --
20566 --------------
20568 function Contains
20569 (E : Entity_Id;
20570 L : Elist_Id) return Boolean
20572 List_Elmt : Elmt_Id;
20574 begin
20575 List_Elmt := First_Elmt (L);
20576 while Present (List_Elmt) loop
20577 if Node (List_Elmt) = E then
20578 return True;
20579 end if;
20581 Next_Elmt (List_Elmt);
20582 end loop;
20584 return False;
20585 end Contains;
20587 -- Start of processing
20589 begin
20590 if Is_Tagged_Type (Priv_T) then
20591 Priv_List := Primitive_Operations (Priv_T);
20592 Prim_Elmt := First_Elmt (Priv_List);
20594 -- In the case of a concurrent type completing a private tagged
20595 -- type, primitives may have been declared in between the two
20596 -- views. These subprograms need to be wrapped the same way
20597 -- entries and protected procedures are handled because they
20598 -- cannot be directly shared by the two views.
20600 if Is_Concurrent_Type (Full_T) then
20601 declare
20602 Conc_Typ : constant Entity_Id :=
20603 Corresponding_Record_Type (Full_T);
20604 Curr_Nod : Node_Id := Parent (Conc_Typ);
20605 Wrap_Spec : Node_Id;
20607 begin
20608 while Present (Prim_Elmt) loop
20609 Prim := Node (Prim_Elmt);
20611 if Comes_From_Source (Prim)
20612 and then not Is_Abstract_Subprogram (Prim)
20613 then
20614 Wrap_Spec :=
20615 Make_Subprogram_Declaration (Sloc (Prim),
20616 Specification =>
20617 Build_Wrapper_Spec
20618 (Subp_Id => Prim,
20619 Obj_Typ => Conc_Typ,
20620 Formals =>
20621 Parameter_Specifications
20622 (Parent (Prim))));
20624 Insert_After (Curr_Nod, Wrap_Spec);
20625 Curr_Nod := Wrap_Spec;
20627 Analyze (Wrap_Spec);
20629 -- Remove the wrapper from visibility to avoid
20630 -- spurious conflict with the wrapped entity.
20632 Set_Is_Immediately_Visible
20633 (Defining_Entity (Specification (Wrap_Spec)),
20634 False);
20635 end if;
20637 Next_Elmt (Prim_Elmt);
20638 end loop;
20640 goto Leave;
20641 end;
20643 -- For non-concurrent types, transfer explicit primitives, but
20644 -- omit those inherited from the parent of the private view
20645 -- since they will be re-inherited later on.
20647 else
20648 Full_List := Primitive_Operations (Full_T);
20650 while Present (Prim_Elmt) loop
20651 Prim := Node (Prim_Elmt);
20653 if Comes_From_Source (Prim)
20654 and then not Contains (Prim, Full_List)
20655 then
20656 Append_Elmt (Prim, Full_List);
20657 end if;
20659 Next_Elmt (Prim_Elmt);
20660 end loop;
20661 end if;
20663 -- Untagged private view
20665 else
20666 Full_List := Primitive_Operations (Full_T);
20668 -- In this case the partial view is untagged, so here we locate
20669 -- all of the earlier primitives that need to be treated as
20670 -- dispatching (those that appear between the two views). Note
20671 -- that these additional operations must all be new operations
20672 -- (any earlier operations that override inherited operations
20673 -- of the full view will already have been inserted in the
20674 -- primitives list, marked by Check_Operation_From_Private_View
20675 -- as dispatching. Note that implicit "/=" operators are
20676 -- excluded from being added to the primitives list since they
20677 -- shouldn't be treated as dispatching (tagged "/=" is handled
20678 -- specially).
20680 Prim := Next_Entity (Full_T);
20681 while Present (Prim) and then Prim /= Priv_T loop
20682 if Ekind_In (Prim, E_Procedure, E_Function) then
20683 Disp_Typ := Find_Dispatching_Type (Prim);
20685 if Disp_Typ = Full_T
20686 and then (Chars (Prim) /= Name_Op_Ne
20687 or else Comes_From_Source (Prim))
20688 then
20689 Check_Controlling_Formals (Full_T, Prim);
20691 if not Is_Dispatching_Operation (Prim) then
20692 Append_Elmt (Prim, Full_List);
20693 Set_Is_Dispatching_Operation (Prim, True);
20694 Set_DT_Position_Value (Prim, No_Uint);
20695 end if;
20697 elsif Is_Dispatching_Operation (Prim)
20698 and then Disp_Typ /= Full_T
20699 then
20701 -- Verify that it is not otherwise controlled by a
20702 -- formal or a return value of type T.
20704 Check_Controlling_Formals (Disp_Typ, Prim);
20705 end if;
20706 end if;
20708 Next_Entity (Prim);
20709 end loop;
20710 end if;
20712 -- For the tagged case, the two views can share the same primitive
20713 -- operations list and the same class-wide type. Update attributes
20714 -- of the class-wide type which depend on the full declaration.
20716 if Is_Tagged_Type (Priv_T) then
20717 Set_Direct_Primitive_Operations (Priv_T, Full_List);
20718 Set_Class_Wide_Type
20719 (Base_Type (Full_T), Class_Wide_Type (Priv_T));
20721 Propagate_Concurrent_Flags (Class_Wide_Type (Priv_T), Full_T);
20722 end if;
20723 end;
20724 end if;
20726 -- Ada 2005 AI 161: Check preelaborable initialization consistency
20728 if Known_To_Have_Preelab_Init (Priv_T) then
20730 -- Case where there is a pragma Preelaborable_Initialization. We
20731 -- always allow this in predefined units, which is cheating a bit,
20732 -- but it means we don't have to struggle to meet the requirements in
20733 -- the RM for having Preelaborable Initialization. Otherwise we
20734 -- require that the type meets the RM rules. But we can't check that
20735 -- yet, because of the rule about overriding Initialize, so we simply
20736 -- set a flag that will be checked at freeze time.
20738 if not In_Predefined_Unit (Full_T) then
20739 Set_Must_Have_Preelab_Init (Full_T);
20740 end if;
20741 end if;
20743 -- If pragma CPP_Class was applied to the private type declaration,
20744 -- propagate it now to the full type declaration.
20746 if Is_CPP_Class (Priv_T) then
20747 Set_Is_CPP_Class (Full_T);
20748 Set_Convention (Full_T, Convention_CPP);
20750 -- Check that components of imported CPP types do not have default
20751 -- expressions.
20753 Check_CPP_Type_Has_No_Defaults (Full_T);
20754 end if;
20756 -- If the private view has user specified stream attributes, then so has
20757 -- the full view.
20759 -- Why the test, how could these flags be already set in Full_T ???
20761 if Has_Specified_Stream_Read (Priv_T) then
20762 Set_Has_Specified_Stream_Read (Full_T);
20763 end if;
20765 if Has_Specified_Stream_Write (Priv_T) then
20766 Set_Has_Specified_Stream_Write (Full_T);
20767 end if;
20769 if Has_Specified_Stream_Input (Priv_T) then
20770 Set_Has_Specified_Stream_Input (Full_T);
20771 end if;
20773 if Has_Specified_Stream_Output (Priv_T) then
20774 Set_Has_Specified_Stream_Output (Full_T);
20775 end if;
20777 -- Propagate Default_Initial_Condition-related attributes from the
20778 -- partial view to the full view and its base type.
20780 Propagate_DIC_Attributes (Full_T, From_Typ => Priv_T);
20781 Propagate_DIC_Attributes (Base_Type (Full_T), From_Typ => Priv_T);
20783 -- Propagate invariant-related attributes from the partial view to the
20784 -- full view and its base type.
20786 Propagate_Invariant_Attributes (Full_T, From_Typ => Priv_T);
20787 Propagate_Invariant_Attributes (Base_Type (Full_T), From_Typ => Priv_T);
20789 -- AI12-0041: Detect an attempt to inherit a class-wide type invariant
20790 -- in the full view without advertising the inheritance in the partial
20791 -- view. This can only occur when the partial view has no parent type
20792 -- and the full view has an interface as a parent. Any other scenarios
20793 -- are illegal because implemented interfaces must match between the
20794 -- two views.
20796 if Is_Tagged_Type (Priv_T) and then Is_Tagged_Type (Full_T) then
20797 declare
20798 Full_Par : constant Entity_Id := Etype (Full_T);
20799 Priv_Par : constant Entity_Id := Etype (Priv_T);
20801 begin
20802 if not Is_Interface (Priv_Par)
20803 and then Is_Interface (Full_Par)
20804 and then Has_Inheritable_Invariants (Full_Par)
20805 then
20806 Error_Msg_N
20807 ("hidden inheritance of class-wide type invariants not "
20808 & "allowed", N);
20809 end if;
20810 end;
20811 end if;
20813 -- Propagate predicates to full type, and predicate function if already
20814 -- defined. It is not clear that this can actually happen? the partial
20815 -- view cannot be frozen yet, and the predicate function has not been
20816 -- built. Still it is a cheap check and seems safer to make it.
20818 if Has_Predicates (Priv_T) then
20819 Set_Has_Predicates (Full_T);
20821 if Present (Predicate_Function (Priv_T)) then
20822 Set_Predicate_Function (Full_T, Predicate_Function (Priv_T));
20823 end if;
20824 end if;
20826 <<Leave>>
20827 Restore_Ghost_Mode (Saved_GM);
20828 end Process_Full_View;
20830 -----------------------------------
20831 -- Process_Incomplete_Dependents --
20832 -----------------------------------
20834 procedure Process_Incomplete_Dependents
20835 (N : Node_Id;
20836 Full_T : Entity_Id;
20837 Inc_T : Entity_Id)
20839 Inc_Elmt : Elmt_Id;
20840 Priv_Dep : Entity_Id;
20841 New_Subt : Entity_Id;
20843 Disc_Constraint : Elist_Id;
20845 begin
20846 if No (Private_Dependents (Inc_T)) then
20847 return;
20848 end if;
20850 -- Itypes that may be generated by the completion of an incomplete
20851 -- subtype are not used by the back-end and not attached to the tree.
20852 -- They are created only for constraint-checking purposes.
20854 Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
20855 while Present (Inc_Elmt) loop
20856 Priv_Dep := Node (Inc_Elmt);
20858 if Ekind (Priv_Dep) = E_Subprogram_Type then
20860 -- An Access_To_Subprogram type may have a return type or a
20861 -- parameter type that is incomplete. Replace with the full view.
20863 if Etype (Priv_Dep) = Inc_T then
20864 Set_Etype (Priv_Dep, Full_T);
20865 end if;
20867 declare
20868 Formal : Entity_Id;
20870 begin
20871 Formal := First_Formal (Priv_Dep);
20872 while Present (Formal) loop
20873 if Etype (Formal) = Inc_T then
20874 Set_Etype (Formal, Full_T);
20875 end if;
20877 Next_Formal (Formal);
20878 end loop;
20879 end;
20881 elsif Is_Overloadable (Priv_Dep) then
20883 -- If a subprogram in the incomplete dependents list is primitive
20884 -- for a tagged full type then mark it as a dispatching operation,
20885 -- check whether it overrides an inherited subprogram, and check
20886 -- restrictions on its controlling formals. Note that a protected
20887 -- operation is never dispatching: only its wrapper operation
20888 -- (which has convention Ada) is.
20890 if Is_Tagged_Type (Full_T)
20891 and then Is_Primitive (Priv_Dep)
20892 and then Convention (Priv_Dep) /= Convention_Protected
20893 then
20894 Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
20895 Set_Is_Dispatching_Operation (Priv_Dep);
20896 Check_Controlling_Formals (Full_T, Priv_Dep);
20897 end if;
20899 elsif Ekind (Priv_Dep) = E_Subprogram_Body then
20901 -- Can happen during processing of a body before the completion
20902 -- of a TA type. Ignore, because spec is also on dependent list.
20904 return;
20906 -- Ada 2005 (AI-412): Transform a regular incomplete subtype into a
20907 -- corresponding subtype of the full view.
20909 elsif Ekind (Priv_Dep) = E_Incomplete_Subtype
20910 and then Comes_From_Source (Priv_Dep)
20911 then
20912 Set_Subtype_Indication
20913 (Parent (Priv_Dep), New_Occurrence_Of (Full_T, Sloc (Priv_Dep)));
20914 Set_Etype (Priv_Dep, Full_T);
20915 Set_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
20916 Set_Analyzed (Parent (Priv_Dep), False);
20918 -- Reanalyze the declaration, suppressing the call to Enter_Name
20919 -- to avoid duplicate names.
20921 Analyze_Subtype_Declaration
20922 (N => Parent (Priv_Dep),
20923 Skip => True);
20925 -- Dependent is a subtype
20927 else
20928 -- We build a new subtype indication using the full view of the
20929 -- incomplete parent. The discriminant constraints have been
20930 -- elaborated already at the point of the subtype declaration.
20932 New_Subt := Create_Itype (E_Void, N);
20934 if Has_Discriminants (Full_T) then
20935 Disc_Constraint := Discriminant_Constraint (Priv_Dep);
20936 else
20937 Disc_Constraint := No_Elist;
20938 end if;
20940 Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
20941 Set_Full_View (Priv_Dep, New_Subt);
20942 end if;
20944 Next_Elmt (Inc_Elmt);
20945 end loop;
20946 end Process_Incomplete_Dependents;
20948 --------------------------------
20949 -- Process_Range_Expr_In_Decl --
20950 --------------------------------
20952 procedure Process_Range_Expr_In_Decl
20953 (R : Node_Id;
20954 T : Entity_Id;
20955 Subtyp : Entity_Id := Empty;
20956 Check_List : List_Id := Empty_List;
20957 R_Check_Off : Boolean := False;
20958 In_Iter_Schm : Boolean := False)
20960 Lo, Hi : Node_Id;
20961 R_Checks : Check_Result;
20962 Insert_Node : Node_Id;
20963 Def_Id : Entity_Id;
20965 begin
20966 Analyze_And_Resolve (R, Base_Type (T));
20968 if Nkind (R) = N_Range then
20970 -- In SPARK, all ranges should be static, with the exception of the
20971 -- discrete type definition of a loop parameter specification.
20973 if not In_Iter_Schm
20974 and then not Is_OK_Static_Range (R)
20975 then
20976 Check_SPARK_05_Restriction ("range should be static", R);
20977 end if;
20979 Lo := Low_Bound (R);
20980 Hi := High_Bound (R);
20982 -- Validity checks on the range of a quantified expression are
20983 -- delayed until the construct is transformed into a loop.
20985 if Nkind (Parent (R)) = N_Loop_Parameter_Specification
20986 and then Nkind (Parent (Parent (R))) = N_Quantified_Expression
20987 then
20988 null;
20990 -- We need to ensure validity of the bounds here, because if we
20991 -- go ahead and do the expansion, then the expanded code will get
20992 -- analyzed with range checks suppressed and we miss the check.
20994 -- WARNING: The capture of the range bounds with xxx_FIRST/_LAST and
20995 -- the temporaries generated by routine Remove_Side_Effects by means
20996 -- of validity checks must use the same names. When a range appears
20997 -- in the parent of a generic, the range is processed with checks
20998 -- disabled as part of the generic context and with checks enabled
20999 -- for code generation purposes. This leads to link issues as the
21000 -- generic contains references to xxx_FIRST/_LAST, but the inlined
21001 -- template sees the temporaries generated by Remove_Side_Effects.
21003 else
21004 Validity_Check_Range (R, Subtyp);
21005 end if;
21007 -- If there were errors in the declaration, try and patch up some
21008 -- common mistakes in the bounds. The cases handled are literals
21009 -- which are Integer where the expected type is Real and vice versa.
21010 -- These corrections allow the compilation process to proceed further
21011 -- along since some basic assumptions of the format of the bounds
21012 -- are guaranteed.
21014 if Etype (R) = Any_Type then
21015 if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
21016 Rewrite (Lo,
21017 Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
21019 elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
21020 Rewrite (Hi,
21021 Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
21023 elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
21024 Rewrite (Lo,
21025 Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
21027 elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
21028 Rewrite (Hi,
21029 Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
21030 end if;
21032 Set_Etype (Lo, T);
21033 Set_Etype (Hi, T);
21034 end if;
21036 -- If the bounds of the range have been mistakenly given as string
21037 -- literals (perhaps in place of character literals), then an error
21038 -- has already been reported, but we rewrite the string literal as a
21039 -- bound of the range's type to avoid blowups in later processing
21040 -- that looks at static values.
21042 if Nkind (Lo) = N_String_Literal then
21043 Rewrite (Lo,
21044 Make_Attribute_Reference (Sloc (Lo),
21045 Prefix => New_Occurrence_Of (T, Sloc (Lo)),
21046 Attribute_Name => Name_First));
21047 Analyze_And_Resolve (Lo);
21048 end if;
21050 if Nkind (Hi) = N_String_Literal then
21051 Rewrite (Hi,
21052 Make_Attribute_Reference (Sloc (Hi),
21053 Prefix => New_Occurrence_Of (T, Sloc (Hi)),
21054 Attribute_Name => Name_First));
21055 Analyze_And_Resolve (Hi);
21056 end if;
21058 -- If bounds aren't scalar at this point then exit, avoiding
21059 -- problems with further processing of the range in this procedure.
21061 if not Is_Scalar_Type (Etype (Lo)) then
21062 return;
21063 end if;
21065 -- Resolve (actually Sem_Eval) has checked that the bounds are in
21066 -- then range of the base type. Here we check whether the bounds
21067 -- are in the range of the subtype itself. Note that if the bounds
21068 -- represent the null range the Constraint_Error exception should
21069 -- not be raised.
21071 -- ??? The following code should be cleaned up as follows
21073 -- 1. The Is_Null_Range (Lo, Hi) test should disappear since it
21074 -- is done in the call to Range_Check (R, T); below
21076 -- 2. The use of R_Check_Off should be investigated and possibly
21077 -- removed, this would clean up things a bit.
21079 if Is_Null_Range (Lo, Hi) then
21080 null;
21082 else
21083 -- Capture values of bounds and generate temporaries for them
21084 -- if needed, before applying checks, since checks may cause
21085 -- duplication of the expression without forcing evaluation.
21087 -- The forced evaluation removes side effects from expressions,
21088 -- which should occur also in GNATprove mode. Otherwise, we end up
21089 -- with unexpected insertions of actions at places where this is
21090 -- not supposed to occur, e.g. on default parameters of a call.
21092 if Expander_Active or GNATprove_Mode then
21094 -- Call Force_Evaluation to create declarations as needed to
21095 -- deal with side effects, and also create typ_FIRST/LAST
21096 -- entities for bounds if we have a subtype name.
21098 -- Note: we do this transformation even if expansion is not
21099 -- active if we are in GNATprove_Mode since the transformation
21100 -- is in general required to ensure that the resulting tree has
21101 -- proper Ada semantics.
21103 Force_Evaluation
21104 (Lo, Related_Id => Subtyp, Is_Low_Bound => True);
21105 Force_Evaluation
21106 (Hi, Related_Id => Subtyp, Is_High_Bound => True);
21107 end if;
21109 -- We use a flag here instead of suppressing checks on the type
21110 -- because the type we check against isn't necessarily the place
21111 -- where we put the check.
21113 if not R_Check_Off then
21114 R_Checks := Get_Range_Checks (R, T);
21116 -- Look up tree to find an appropriate insertion point. We
21117 -- can't just use insert_actions because later processing
21118 -- depends on the insertion node. Prior to Ada 2012 the
21119 -- insertion point could only be a declaration or a loop, but
21120 -- quantified expressions can appear within any context in an
21121 -- expression, and the insertion point can be any statement,
21122 -- pragma, or declaration.
21124 Insert_Node := Parent (R);
21125 while Present (Insert_Node) loop
21126 exit when
21127 Nkind (Insert_Node) in N_Declaration
21128 and then
21129 not Nkind_In
21130 (Insert_Node, N_Component_Declaration,
21131 N_Loop_Parameter_Specification,
21132 N_Function_Specification,
21133 N_Procedure_Specification);
21135 exit when Nkind (Insert_Node) in N_Later_Decl_Item
21136 or else Nkind (Insert_Node) in
21137 N_Statement_Other_Than_Procedure_Call
21138 or else Nkind_In (Insert_Node, N_Procedure_Call_Statement,
21139 N_Pragma);
21141 Insert_Node := Parent (Insert_Node);
21142 end loop;
21144 -- Why would Type_Decl not be present??? Without this test,
21145 -- short regression tests fail.
21147 if Present (Insert_Node) then
21149 -- Case of loop statement. Verify that the range is part
21150 -- of the subtype indication of the iteration scheme.
21152 if Nkind (Insert_Node) = N_Loop_Statement then
21153 declare
21154 Indic : Node_Id;
21156 begin
21157 Indic := Parent (R);
21158 while Present (Indic)
21159 and then Nkind (Indic) /= N_Subtype_Indication
21160 loop
21161 Indic := Parent (Indic);
21162 end loop;
21164 if Present (Indic) then
21165 Def_Id := Etype (Subtype_Mark (Indic));
21167 Insert_Range_Checks
21168 (R_Checks,
21169 Insert_Node,
21170 Def_Id,
21171 Sloc (Insert_Node),
21173 Do_Before => True);
21174 end if;
21175 end;
21177 -- Insertion before a declaration. If the declaration
21178 -- includes discriminants, the list of applicable checks
21179 -- is given by the caller.
21181 elsif Nkind (Insert_Node) in N_Declaration then
21182 Def_Id := Defining_Identifier (Insert_Node);
21184 if (Ekind (Def_Id) = E_Record_Type
21185 and then Depends_On_Discriminant (R))
21186 or else
21187 (Ekind (Def_Id) = E_Protected_Type
21188 and then Has_Discriminants (Def_Id))
21189 then
21190 Append_Range_Checks
21191 (R_Checks,
21192 Check_List, Def_Id, Sloc (Insert_Node), R);
21194 else
21195 Insert_Range_Checks
21196 (R_Checks,
21197 Insert_Node, Def_Id, Sloc (Insert_Node), R);
21199 end if;
21201 -- Insertion before a statement. Range appears in the
21202 -- context of a quantified expression. Insertion will
21203 -- take place when expression is expanded.
21205 else
21206 null;
21207 end if;
21208 end if;
21209 end if;
21210 end if;
21212 -- Case of other than an explicit N_Range node
21214 -- The forced evaluation removes side effects from expressions, which
21215 -- should occur also in GNATprove mode. Otherwise, we end up with
21216 -- unexpected insertions of actions at places where this is not
21217 -- supposed to occur, e.g. on default parameters of a call.
21219 elsif Expander_Active or GNATprove_Mode then
21220 Get_Index_Bounds (R, Lo, Hi);
21221 Force_Evaluation (Lo);
21222 Force_Evaluation (Hi);
21223 end if;
21224 end Process_Range_Expr_In_Decl;
21226 --------------------------------------
21227 -- Process_Real_Range_Specification --
21228 --------------------------------------
21230 procedure Process_Real_Range_Specification (Def : Node_Id) is
21231 Spec : constant Node_Id := Real_Range_Specification (Def);
21232 Lo : Node_Id;
21233 Hi : Node_Id;
21234 Err : Boolean := False;
21236 procedure Analyze_Bound (N : Node_Id);
21237 -- Analyze and check one bound
21239 -------------------
21240 -- Analyze_Bound --
21241 -------------------
21243 procedure Analyze_Bound (N : Node_Id) is
21244 begin
21245 Analyze_And_Resolve (N, Any_Real);
21247 if not Is_OK_Static_Expression (N) then
21248 Flag_Non_Static_Expr
21249 ("bound in real type definition is not static!", N);
21250 Err := True;
21251 end if;
21252 end Analyze_Bound;
21254 -- Start of processing for Process_Real_Range_Specification
21256 begin
21257 if Present (Spec) then
21258 Lo := Low_Bound (Spec);
21259 Hi := High_Bound (Spec);
21260 Analyze_Bound (Lo);
21261 Analyze_Bound (Hi);
21263 -- If error, clear away junk range specification
21265 if Err then
21266 Set_Real_Range_Specification (Def, Empty);
21267 end if;
21268 end if;
21269 end Process_Real_Range_Specification;
21271 ---------------------
21272 -- Process_Subtype --
21273 ---------------------
21275 function Process_Subtype
21276 (S : Node_Id;
21277 Related_Nod : Node_Id;
21278 Related_Id : Entity_Id := Empty;
21279 Suffix : Character := ' ') return Entity_Id
21281 P : Node_Id;
21282 Def_Id : Entity_Id;
21283 Error_Node : Node_Id;
21284 Full_View_Id : Entity_Id;
21285 Subtype_Mark_Id : Entity_Id;
21287 May_Have_Null_Exclusion : Boolean;
21289 procedure Check_Incomplete (T : Node_Id);
21290 -- Called to verify that an incomplete type is not used prematurely
21292 ----------------------
21293 -- Check_Incomplete --
21294 ----------------------
21296 procedure Check_Incomplete (T : Node_Id) is
21297 begin
21298 -- Ada 2005 (AI-412): Incomplete subtypes are legal
21300 if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
21301 and then
21302 not (Ada_Version >= Ada_2005
21303 and then
21304 (Nkind (Parent (T)) = N_Subtype_Declaration
21305 or else (Nkind (Parent (T)) = N_Subtype_Indication
21306 and then Nkind (Parent (Parent (T))) =
21307 N_Subtype_Declaration)))
21308 then
21309 Error_Msg_N ("invalid use of type before its full declaration", T);
21310 end if;
21311 end Check_Incomplete;
21313 -- Start of processing for Process_Subtype
21315 begin
21316 -- Case of no constraints present
21318 if Nkind (S) /= N_Subtype_Indication then
21319 Find_Type (S);
21320 Check_Incomplete (S);
21321 P := Parent (S);
21323 -- Ada 2005 (AI-231): Static check
21325 if Ada_Version >= Ada_2005
21326 and then Present (P)
21327 and then Null_Exclusion_Present (P)
21328 and then Nkind (P) /= N_Access_To_Object_Definition
21329 and then not Is_Access_Type (Entity (S))
21330 then
21331 Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
21332 end if;
21334 -- The following is ugly, can't we have a range or even a flag???
21336 May_Have_Null_Exclusion :=
21337 Nkind_In (P, N_Access_Definition,
21338 N_Access_Function_Definition,
21339 N_Access_Procedure_Definition,
21340 N_Access_To_Object_Definition,
21341 N_Allocator,
21342 N_Component_Definition)
21343 or else
21344 Nkind_In (P, N_Derived_Type_Definition,
21345 N_Discriminant_Specification,
21346 N_Formal_Object_Declaration,
21347 N_Object_Declaration,
21348 N_Object_Renaming_Declaration,
21349 N_Parameter_Specification,
21350 N_Subtype_Declaration);
21352 -- Create an Itype that is a duplicate of Entity (S) but with the
21353 -- null-exclusion attribute.
21355 if May_Have_Null_Exclusion
21356 and then Is_Access_Type (Entity (S))
21357 and then Null_Exclusion_Present (P)
21359 -- No need to check the case of an access to object definition.
21360 -- It is correct to define double not-null pointers.
21362 -- Example:
21363 -- type Not_Null_Int_Ptr is not null access Integer;
21364 -- type Acc is not null access Not_Null_Int_Ptr;
21366 and then Nkind (P) /= N_Access_To_Object_Definition
21367 then
21368 if Can_Never_Be_Null (Entity (S)) then
21369 case Nkind (Related_Nod) is
21370 when N_Full_Type_Declaration =>
21371 if Nkind (Type_Definition (Related_Nod))
21372 in N_Array_Type_Definition
21373 then
21374 Error_Node :=
21375 Subtype_Indication
21376 (Component_Definition
21377 (Type_Definition (Related_Nod)));
21378 else
21379 Error_Node :=
21380 Subtype_Indication (Type_Definition (Related_Nod));
21381 end if;
21383 when N_Subtype_Declaration =>
21384 Error_Node := Subtype_Indication (Related_Nod);
21386 when N_Object_Declaration =>
21387 Error_Node := Object_Definition (Related_Nod);
21389 when N_Component_Declaration =>
21390 Error_Node :=
21391 Subtype_Indication (Component_Definition (Related_Nod));
21393 when N_Allocator =>
21394 Error_Node := Expression (Related_Nod);
21396 when others =>
21397 pragma Assert (False);
21398 Error_Node := Related_Nod;
21399 end case;
21401 Error_Msg_NE
21402 ("`NOT NULL` not allowed (& already excludes null)",
21403 Error_Node,
21404 Entity (S));
21405 end if;
21407 Set_Etype (S,
21408 Create_Null_Excluding_Itype
21409 (T => Entity (S),
21410 Related_Nod => P));
21411 Set_Entity (S, Etype (S));
21412 end if;
21414 return Entity (S);
21416 -- Case of constraint present, so that we have an N_Subtype_Indication
21417 -- node (this node is created only if constraints are present).
21419 else
21420 Find_Type (Subtype_Mark (S));
21422 if Nkind (Parent (S)) /= N_Access_To_Object_Definition
21423 and then not
21424 (Nkind (Parent (S)) = N_Subtype_Declaration
21425 and then Is_Itype (Defining_Identifier (Parent (S))))
21426 then
21427 Check_Incomplete (Subtype_Mark (S));
21428 end if;
21430 P := Parent (S);
21431 Subtype_Mark_Id := Entity (Subtype_Mark (S));
21433 -- Explicit subtype declaration case
21435 if Nkind (P) = N_Subtype_Declaration then
21436 Def_Id := Defining_Identifier (P);
21438 -- Explicit derived type definition case
21440 elsif Nkind (P) = N_Derived_Type_Definition then
21441 Def_Id := Defining_Identifier (Parent (P));
21443 -- Implicit case, the Def_Id must be created as an implicit type.
21444 -- The one exception arises in the case of concurrent types, array
21445 -- and access types, where other subsidiary implicit types may be
21446 -- created and must appear before the main implicit type. In these
21447 -- cases we leave Def_Id set to Empty as a signal that Create_Itype
21448 -- has not yet been called to create Def_Id.
21450 else
21451 if Is_Array_Type (Subtype_Mark_Id)
21452 or else Is_Concurrent_Type (Subtype_Mark_Id)
21453 or else Is_Access_Type (Subtype_Mark_Id)
21454 then
21455 Def_Id := Empty;
21457 -- For the other cases, we create a new unattached Itype,
21458 -- and set the indication to ensure it gets attached later.
21460 else
21461 Def_Id :=
21462 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
21463 end if;
21464 end if;
21466 -- If the kind of constraint is invalid for this kind of type,
21467 -- then give an error, and then pretend no constraint was given.
21469 if not Is_Valid_Constraint_Kind
21470 (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
21471 then
21472 Error_Msg_N
21473 ("incorrect constraint for this kind of type", Constraint (S));
21475 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
21477 -- Set Ekind of orphan itype, to prevent cascaded errors
21479 if Present (Def_Id) then
21480 Set_Ekind (Def_Id, Ekind (Any_Type));
21481 end if;
21483 -- Make recursive call, having got rid of the bogus constraint
21485 return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
21486 end if;
21488 -- Remaining processing depends on type. Select on Base_Type kind to
21489 -- ensure getting to the concrete type kind in the case of a private
21490 -- subtype (needed when only doing semantic analysis).
21492 case Ekind (Base_Type (Subtype_Mark_Id)) is
21493 when Access_Kind =>
21495 -- If this is a constraint on a class-wide type, discard it.
21496 -- There is currently no way to express a partial discriminant
21497 -- constraint on a type with unknown discriminants. This is
21498 -- a pathology that the ACATS wisely decides not to test.
21500 if Is_Class_Wide_Type (Designated_Type (Subtype_Mark_Id)) then
21501 if Comes_From_Source (S) then
21502 Error_Msg_N
21503 ("constraint on class-wide type ignored??",
21504 Constraint (S));
21505 end if;
21507 if Nkind (P) = N_Subtype_Declaration then
21508 Set_Subtype_Indication (P,
21509 New_Occurrence_Of (Subtype_Mark_Id, Sloc (S)));
21510 end if;
21512 return Subtype_Mark_Id;
21513 end if;
21515 Constrain_Access (Def_Id, S, Related_Nod);
21517 if Expander_Active
21518 and then Is_Itype (Designated_Type (Def_Id))
21519 and then Nkind (Related_Nod) = N_Subtype_Declaration
21520 and then not Is_Incomplete_Type (Designated_Type (Def_Id))
21521 then
21522 Build_Itype_Reference
21523 (Designated_Type (Def_Id), Related_Nod);
21524 end if;
21526 when Array_Kind =>
21527 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
21529 when Decimal_Fixed_Point_Kind =>
21530 Constrain_Decimal (Def_Id, S);
21532 when Enumeration_Kind =>
21533 Constrain_Enumeration (Def_Id, S);
21534 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
21536 when Ordinary_Fixed_Point_Kind =>
21537 Constrain_Ordinary_Fixed (Def_Id, S);
21539 when Float_Kind =>
21540 Constrain_Float (Def_Id, S);
21542 when Integer_Kind =>
21543 Constrain_Integer (Def_Id, S);
21544 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
21546 when Class_Wide_Kind
21547 | E_Incomplete_Type
21548 | E_Record_Subtype
21549 | E_Record_Type
21551 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
21553 if Ekind (Def_Id) = E_Incomplete_Type then
21554 Set_Private_Dependents (Def_Id, New_Elmt_List);
21555 end if;
21557 when Private_Kind =>
21558 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
21560 -- The base type may be private but Def_Id may be a full view
21561 -- in an instance.
21563 if Is_Private_Type (Def_Id) then
21564 Set_Private_Dependents (Def_Id, New_Elmt_List);
21565 end if;
21567 -- In case of an invalid constraint prevent further processing
21568 -- since the type constructed is missing expected fields.
21570 if Etype (Def_Id) = Any_Type then
21571 return Def_Id;
21572 end if;
21574 -- If the full view is that of a task with discriminants,
21575 -- we must constrain both the concurrent type and its
21576 -- corresponding record type. Otherwise we will just propagate
21577 -- the constraint to the full view, if available.
21579 if Present (Full_View (Subtype_Mark_Id))
21580 and then Has_Discriminants (Subtype_Mark_Id)
21581 and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
21582 then
21583 Full_View_Id :=
21584 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
21586 Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
21587 Constrain_Concurrent (Full_View_Id, S,
21588 Related_Nod, Related_Id, Suffix);
21589 Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
21590 Set_Full_View (Def_Id, Full_View_Id);
21592 -- Introduce an explicit reference to the private subtype,
21593 -- to prevent scope anomalies in gigi if first use appears
21594 -- in a nested context, e.g. a later function body.
21595 -- Should this be generated in other contexts than a full
21596 -- type declaration?
21598 if Is_Itype (Def_Id)
21599 and then
21600 Nkind (Parent (P)) = N_Full_Type_Declaration
21601 then
21602 Build_Itype_Reference (Def_Id, Parent (P));
21603 end if;
21605 else
21606 Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
21607 end if;
21609 when Concurrent_Kind =>
21610 Constrain_Concurrent (Def_Id, S,
21611 Related_Nod, Related_Id, Suffix);
21613 when others =>
21614 Error_Msg_N ("invalid subtype mark in subtype indication", S);
21615 end case;
21617 -- Size, Alignment, Representation aspects and Convention are always
21618 -- inherited from the base type.
21620 Set_Size_Info (Def_Id, (Subtype_Mark_Id));
21621 Set_Rep_Info (Def_Id, (Subtype_Mark_Id));
21622 Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
21624 return Def_Id;
21625 end if;
21626 end Process_Subtype;
21628 -----------------------------
21629 -- Record_Type_Declaration --
21630 -----------------------------
21632 procedure Record_Type_Declaration
21633 (T : Entity_Id;
21634 N : Node_Id;
21635 Prev : Entity_Id)
21637 Def : constant Node_Id := Type_Definition (N);
21638 Is_Tagged : Boolean;
21639 Tag_Comp : Entity_Id;
21641 begin
21642 -- These flags must be initialized before calling Process_Discriminants
21643 -- because this routine makes use of them.
21645 Set_Ekind (T, E_Record_Type);
21646 Set_Etype (T, T);
21647 Init_Size_Align (T);
21648 Set_Interfaces (T, No_Elist);
21649 Set_Stored_Constraint (T, No_Elist);
21650 Set_Default_SSO (T);
21651 Set_No_Reordering (T, No_Component_Reordering);
21653 -- Normal case
21655 if Ada_Version < Ada_2005 or else not Interface_Present (Def) then
21656 if Limited_Present (Def) then
21657 Check_SPARK_05_Restriction ("limited is not allowed", N);
21658 end if;
21660 if Abstract_Present (Def) then
21661 Check_SPARK_05_Restriction ("abstract is not allowed", N);
21662 end if;
21664 -- The flag Is_Tagged_Type might have already been set by
21665 -- Find_Type_Name if it detected an error for declaration T. This
21666 -- arises in the case of private tagged types where the full view
21667 -- omits the word tagged.
21669 Is_Tagged :=
21670 Tagged_Present (Def)
21671 or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
21673 Set_Is_Limited_Record (T, Limited_Present (Def));
21675 if Is_Tagged then
21676 Set_Is_Tagged_Type (T, True);
21677 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
21678 end if;
21680 -- Type is abstract if full declaration carries keyword, or if
21681 -- previous partial view did.
21683 Set_Is_Abstract_Type (T, Is_Abstract_Type (T)
21684 or else Abstract_Present (Def));
21686 else
21687 Check_SPARK_05_Restriction ("interface is not allowed", N);
21689 Is_Tagged := True;
21690 Analyze_Interface_Declaration (T, Def);
21692 if Present (Discriminant_Specifications (N)) then
21693 Error_Msg_N
21694 ("interface types cannot have discriminants",
21695 Defining_Identifier
21696 (First (Discriminant_Specifications (N))));
21697 end if;
21698 end if;
21700 -- First pass: if there are self-referential access components,
21701 -- create the required anonymous access type declarations, and if
21702 -- need be an incomplete type declaration for T itself.
21704 Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
21706 if Ada_Version >= Ada_2005
21707 and then Present (Interface_List (Def))
21708 then
21709 Check_Interfaces (N, Def);
21711 declare
21712 Ifaces_List : Elist_Id;
21714 begin
21715 -- Ada 2005 (AI-251): Collect the list of progenitors that are not
21716 -- already in the parents.
21718 Collect_Interfaces
21719 (T => T,
21720 Ifaces_List => Ifaces_List,
21721 Exclude_Parents => True);
21723 Set_Interfaces (T, Ifaces_List);
21724 end;
21725 end if;
21727 -- Records constitute a scope for the component declarations within.
21728 -- The scope is created prior to the processing of these declarations.
21729 -- Discriminants are processed first, so that they are visible when
21730 -- processing the other components. The Ekind of the record type itself
21731 -- is set to E_Record_Type (subtypes appear as E_Record_Subtype).
21733 -- Enter record scope
21735 Push_Scope (T);
21737 -- If an incomplete or private type declaration was already given for
21738 -- the type, then this scope already exists, and the discriminants have
21739 -- been declared within. We must verify that the full declaration
21740 -- matches the incomplete one.
21742 Check_Or_Process_Discriminants (N, T, Prev);
21744 Set_Is_Constrained (T, not Has_Discriminants (T));
21745 Set_Has_Delayed_Freeze (T, True);
21747 -- For tagged types add a manually analyzed component corresponding
21748 -- to the component _tag, the corresponding piece of tree will be
21749 -- expanded as part of the freezing actions if it is not a CPP_Class.
21751 if Is_Tagged then
21753 -- Do not add the tag unless we are in expansion mode
21755 if Expander_Active then
21756 Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
21757 Enter_Name (Tag_Comp);
21759 Set_Ekind (Tag_Comp, E_Component);
21760 Set_Is_Tag (Tag_Comp);
21761 Set_Is_Aliased (Tag_Comp);
21762 Set_Etype (Tag_Comp, RTE (RE_Tag));
21763 Set_DT_Entry_Count (Tag_Comp, No_Uint);
21764 Set_Original_Record_Component (Tag_Comp, Tag_Comp);
21765 Init_Component_Location (Tag_Comp);
21767 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
21768 -- implemented interfaces.
21770 if Has_Interfaces (T) then
21771 Add_Interface_Tag_Components (N, T);
21772 end if;
21773 end if;
21775 Make_Class_Wide_Type (T);
21776 Set_Direct_Primitive_Operations (T, New_Elmt_List);
21777 end if;
21779 -- We must suppress range checks when processing record components in
21780 -- the presence of discriminants, since we don't want spurious checks to
21781 -- be generated during their analysis, but Suppress_Range_Checks flags
21782 -- must be reset the after processing the record definition.
21784 -- Note: this is the only use of Kill_Range_Checks, and is a bit odd,
21785 -- couldn't we just use the normal range check suppression method here.
21786 -- That would seem cleaner ???
21788 if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
21789 Set_Kill_Range_Checks (T, True);
21790 Record_Type_Definition (Def, Prev);
21791 Set_Kill_Range_Checks (T, False);
21792 else
21793 Record_Type_Definition (Def, Prev);
21794 end if;
21796 -- Exit from record scope
21798 End_Scope;
21800 -- Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
21801 -- the implemented interfaces and associate them an aliased entity.
21803 if Is_Tagged
21804 and then not Is_Empty_List (Interface_List (Def))
21805 then
21806 Derive_Progenitor_Subprograms (T, T);
21807 end if;
21809 Check_Function_Writable_Actuals (N);
21810 end Record_Type_Declaration;
21812 ----------------------------
21813 -- Record_Type_Definition --
21814 ----------------------------
21816 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
21817 Component : Entity_Id;
21818 Ctrl_Components : Boolean := False;
21819 Final_Storage_Only : Boolean;
21820 T : Entity_Id;
21822 begin
21823 if Ekind (Prev_T) = E_Incomplete_Type then
21824 T := Full_View (Prev_T);
21825 else
21826 T := Prev_T;
21827 end if;
21829 -- In SPARK, tagged types and type extensions may only be declared in
21830 -- the specification of library unit packages.
21832 if Present (Def) and then Is_Tagged_Type (T) then
21833 declare
21834 Typ : Node_Id;
21835 Ctxt : Node_Id;
21837 begin
21838 if Nkind (Parent (Def)) = N_Full_Type_Declaration then
21839 Typ := Parent (Def);
21840 else
21841 pragma Assert
21842 (Nkind (Parent (Def)) = N_Derived_Type_Definition);
21843 Typ := Parent (Parent (Def));
21844 end if;
21846 Ctxt := Parent (Typ);
21848 if Nkind (Ctxt) = N_Package_Body
21849 and then Nkind (Parent (Ctxt)) = N_Compilation_Unit
21850 then
21851 Check_SPARK_05_Restriction
21852 ("type should be defined in package specification", Typ);
21854 elsif Nkind (Ctxt) /= N_Package_Specification
21855 or else Nkind (Parent (Parent (Ctxt))) /= N_Compilation_Unit
21856 then
21857 Check_SPARK_05_Restriction
21858 ("type should be defined in library unit package", Typ);
21859 end if;
21860 end;
21861 end if;
21863 Final_Storage_Only := not Is_Controlled (T);
21865 -- Ada 2005: Check whether an explicit Limited is present in a derived
21866 -- type declaration.
21868 if Nkind (Parent (Def)) = N_Derived_Type_Definition
21869 and then Limited_Present (Parent (Def))
21870 then
21871 Set_Is_Limited_Record (T);
21872 end if;
21874 -- If the component list of a record type is defined by the reserved
21875 -- word null and there is no discriminant part, then the record type has
21876 -- no components and all records of the type are null records (RM 3.7)
21877 -- This procedure is also called to process the extension part of a
21878 -- record extension, in which case the current scope may have inherited
21879 -- components.
21881 if No (Def)
21882 or else No (Component_List (Def))
21883 or else Null_Present (Component_List (Def))
21884 then
21885 if not Is_Tagged_Type (T) then
21886 Check_SPARK_05_Restriction ("untagged record cannot be null", Def);
21887 end if;
21889 else
21890 Analyze_Declarations (Component_Items (Component_List (Def)));
21892 if Present (Variant_Part (Component_List (Def))) then
21893 Check_SPARK_05_Restriction ("variant part is not allowed", Def);
21894 Analyze (Variant_Part (Component_List (Def)));
21895 end if;
21896 end if;
21898 -- After completing the semantic analysis of the record definition,
21899 -- record components, both new and inherited, are accessible. Set their
21900 -- kind accordingly. Exclude malformed itypes from illegal declarations,
21901 -- whose Ekind may be void.
21903 Component := First_Entity (Current_Scope);
21904 while Present (Component) loop
21905 if Ekind (Component) = E_Void
21906 and then not Is_Itype (Component)
21907 then
21908 Set_Ekind (Component, E_Component);
21909 Init_Component_Location (Component);
21910 end if;
21912 Propagate_Concurrent_Flags (T, Etype (Component));
21914 if Ekind (Component) /= E_Component then
21915 null;
21917 -- Do not set Has_Controlled_Component on a class-wide equivalent
21918 -- type. See Make_CW_Equivalent_Type.
21920 elsif not Is_Class_Wide_Equivalent_Type (T)
21921 and then (Has_Controlled_Component (Etype (Component))
21922 or else (Chars (Component) /= Name_uParent
21923 and then Is_Controlled (Etype (Component))))
21924 then
21925 Set_Has_Controlled_Component (T, True);
21926 Final_Storage_Only :=
21927 Final_Storage_Only
21928 and then Finalize_Storage_Only (Etype (Component));
21929 Ctrl_Components := True;
21930 end if;
21932 Next_Entity (Component);
21933 end loop;
21935 -- A Type is Finalize_Storage_Only only if all its controlled components
21936 -- are also.
21938 if Ctrl_Components then
21939 Set_Finalize_Storage_Only (T, Final_Storage_Only);
21940 end if;
21942 -- Place reference to end record on the proper entity, which may
21943 -- be a partial view.
21945 if Present (Def) then
21946 Process_End_Label (Def, 'e', Prev_T);
21947 end if;
21948 end Record_Type_Definition;
21950 ------------------------
21951 -- Replace_Components --
21952 ------------------------
21954 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id) is
21955 function Process (N : Node_Id) return Traverse_Result;
21957 -------------
21958 -- Process --
21959 -------------
21961 function Process (N : Node_Id) return Traverse_Result is
21962 Comp : Entity_Id;
21964 begin
21965 if Nkind (N) = N_Discriminant_Specification then
21966 Comp := First_Discriminant (Typ);
21967 while Present (Comp) loop
21968 if Chars (Comp) = Chars (Defining_Identifier (N)) then
21969 Set_Defining_Identifier (N, Comp);
21970 exit;
21971 end if;
21973 Next_Discriminant (Comp);
21974 end loop;
21976 elsif Nkind (N) = N_Variant_Part then
21977 Comp := First_Discriminant (Typ);
21978 while Present (Comp) loop
21979 if Chars (Comp) = Chars (Name (N)) then
21980 Set_Entity (Name (N), Comp);
21981 exit;
21982 end if;
21984 Next_Discriminant (Comp);
21985 end loop;
21987 elsif Nkind (N) = N_Component_Declaration then
21988 Comp := First_Component (Typ);
21989 while Present (Comp) loop
21990 if Chars (Comp) = Chars (Defining_Identifier (N)) then
21991 Set_Defining_Identifier (N, Comp);
21992 exit;
21993 end if;
21995 Next_Component (Comp);
21996 end loop;
21997 end if;
21999 return OK;
22000 end Process;
22002 procedure Replace is new Traverse_Proc (Process);
22004 -- Start of processing for Replace_Components
22006 begin
22007 Replace (Decl);
22008 end Replace_Components;
22010 -------------------------------
22011 -- Set_Completion_Referenced --
22012 -------------------------------
22014 procedure Set_Completion_Referenced (E : Entity_Id) is
22015 begin
22016 -- If in main unit, mark entity that is a completion as referenced,
22017 -- warnings go on the partial view when needed.
22019 if In_Extended_Main_Source_Unit (E) then
22020 Set_Referenced (E);
22021 end if;
22022 end Set_Completion_Referenced;
22024 ---------------------
22025 -- Set_Default_SSO --
22026 ---------------------
22028 procedure Set_Default_SSO (T : Entity_Id) is
22029 begin
22030 case Opt.Default_SSO is
22031 when ' ' =>
22032 null;
22033 when 'L' =>
22034 Set_SSO_Set_Low_By_Default (T, True);
22035 when 'H' =>
22036 Set_SSO_Set_High_By_Default (T, True);
22037 when others =>
22038 raise Program_Error;
22039 end case;
22040 end Set_Default_SSO;
22042 ---------------------
22043 -- Set_Fixed_Range --
22044 ---------------------
22046 -- The range for fixed-point types is complicated by the fact that we
22047 -- do not know the exact end points at the time of the declaration. This
22048 -- is true for three reasons:
22050 -- A size clause may affect the fudging of the end-points.
22051 -- A small clause may affect the values of the end-points.
22052 -- We try to include the end-points if it does not affect the size.
22054 -- This means that the actual end-points must be established at the
22055 -- point when the type is frozen. Meanwhile, we first narrow the range
22056 -- as permitted (so that it will fit if necessary in a small specified
22057 -- size), and then build a range subtree with these narrowed bounds.
22058 -- Set_Fixed_Range constructs the range from real literal values, and
22059 -- sets the range as the Scalar_Range of the given fixed-point type entity.
22061 -- The parent of this range is set to point to the entity so that it is
22062 -- properly hooked into the tree (unlike normal Scalar_Range entries for
22063 -- other scalar types, which are just pointers to the range in the
22064 -- original tree, this would otherwise be an orphan).
22066 -- The tree is left unanalyzed. When the type is frozen, the processing
22067 -- in Freeze.Freeze_Fixed_Point_Type notices that the range is not
22068 -- analyzed, and uses this as an indication that it should complete
22069 -- work on the range (it will know the final small and size values).
22071 procedure Set_Fixed_Range
22072 (E : Entity_Id;
22073 Loc : Source_Ptr;
22074 Lo : Ureal;
22075 Hi : Ureal)
22077 S : constant Node_Id :=
22078 Make_Range (Loc,
22079 Low_Bound => Make_Real_Literal (Loc, Lo),
22080 High_Bound => Make_Real_Literal (Loc, Hi));
22081 begin
22082 Set_Scalar_Range (E, S);
22083 Set_Parent (S, E);
22085 -- Before the freeze point, the bounds of a fixed point are universal
22086 -- and carry the corresponding type.
22088 Set_Etype (Low_Bound (S), Universal_Real);
22089 Set_Etype (High_Bound (S), Universal_Real);
22090 end Set_Fixed_Range;
22092 ----------------------------------
22093 -- Set_Scalar_Range_For_Subtype --
22094 ----------------------------------
22096 procedure Set_Scalar_Range_For_Subtype
22097 (Def_Id : Entity_Id;
22098 R : Node_Id;
22099 Subt : Entity_Id)
22101 Kind : constant Entity_Kind := Ekind (Def_Id);
22103 begin
22104 -- Defend against previous error
22106 if Nkind (R) = N_Error then
22107 return;
22108 end if;
22110 Set_Scalar_Range (Def_Id, R);
22112 -- We need to link the range into the tree before resolving it so
22113 -- that types that are referenced, including importantly the subtype
22114 -- itself, are properly frozen (Freeze_Expression requires that the
22115 -- expression be properly linked into the tree). Of course if it is
22116 -- already linked in, then we do not disturb the current link.
22118 if No (Parent (R)) then
22119 Set_Parent (R, Def_Id);
22120 end if;
22122 -- Reset the kind of the subtype during analysis of the range, to
22123 -- catch possible premature use in the bounds themselves.
22125 Set_Ekind (Def_Id, E_Void);
22126 Process_Range_Expr_In_Decl (R, Subt, Subtyp => Def_Id);
22127 Set_Ekind (Def_Id, Kind);
22128 end Set_Scalar_Range_For_Subtype;
22130 --------------------------------------------------------
22131 -- Set_Stored_Constraint_From_Discriminant_Constraint --
22132 --------------------------------------------------------
22134 procedure Set_Stored_Constraint_From_Discriminant_Constraint
22135 (E : Entity_Id)
22137 begin
22138 -- Make sure set if encountered during Expand_To_Stored_Constraint
22140 Set_Stored_Constraint (E, No_Elist);
22142 -- Give it the right value
22144 if Is_Constrained (E) and then Has_Discriminants (E) then
22145 Set_Stored_Constraint (E,
22146 Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
22147 end if;
22148 end Set_Stored_Constraint_From_Discriminant_Constraint;
22150 -------------------------------------
22151 -- Signed_Integer_Type_Declaration --
22152 -------------------------------------
22154 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
22155 Implicit_Base : Entity_Id;
22156 Base_Typ : Entity_Id;
22157 Lo_Val : Uint;
22158 Hi_Val : Uint;
22159 Errs : Boolean := False;
22160 Lo : Node_Id;
22161 Hi : Node_Id;
22163 function Can_Derive_From (E : Entity_Id) return Boolean;
22164 -- Determine whether given bounds allow derivation from specified type
22166 procedure Check_Bound (Expr : Node_Id);
22167 -- Check bound to make sure it is integral and static. If not, post
22168 -- appropriate error message and set Errs flag
22170 ---------------------
22171 -- Can_Derive_From --
22172 ---------------------
22174 -- Note we check both bounds against both end values, to deal with
22175 -- strange types like ones with a range of 0 .. -12341234.
22177 function Can_Derive_From (E : Entity_Id) return Boolean is
22178 Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
22179 Hi : constant Uint := Expr_Value (Type_High_Bound (E));
22180 begin
22181 return Lo <= Lo_Val and then Lo_Val <= Hi
22182 and then
22183 Lo <= Hi_Val and then Hi_Val <= Hi;
22184 end Can_Derive_From;
22186 -----------------
22187 -- Check_Bound --
22188 -----------------
22190 procedure Check_Bound (Expr : Node_Id) is
22191 begin
22192 -- If a range constraint is used as an integer type definition, each
22193 -- bound of the range must be defined by a static expression of some
22194 -- integer type, but the two bounds need not have the same integer
22195 -- type (Negative bounds are allowed.) (RM 3.5.4)
22197 if not Is_Integer_Type (Etype (Expr)) then
22198 Error_Msg_N
22199 ("integer type definition bounds must be of integer type", Expr);
22200 Errs := True;
22202 elsif not Is_OK_Static_Expression (Expr) then
22203 Flag_Non_Static_Expr
22204 ("non-static expression used for integer type bound!", Expr);
22205 Errs := True;
22207 -- The bounds are folded into literals, and we set their type to be
22208 -- universal, to avoid typing difficulties: we cannot set the type
22209 -- of the literal to the new type, because this would be a forward
22210 -- reference for the back end, and if the original type is user-
22211 -- defined this can lead to spurious semantic errors (e.g. 2928-003).
22213 else
22214 if Is_Entity_Name (Expr) then
22215 Fold_Uint (Expr, Expr_Value (Expr), True);
22216 end if;
22218 Set_Etype (Expr, Universal_Integer);
22219 end if;
22220 end Check_Bound;
22222 -- Start of processing for Signed_Integer_Type_Declaration
22224 begin
22225 -- Create an anonymous base type
22227 Implicit_Base :=
22228 Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
22230 -- Analyze and check the bounds, they can be of any integer type
22232 Lo := Low_Bound (Def);
22233 Hi := High_Bound (Def);
22235 -- Arbitrarily use Integer as the type if either bound had an error
22237 if Hi = Error or else Lo = Error then
22238 Base_Typ := Any_Integer;
22239 Set_Error_Posted (T, True);
22241 -- Here both bounds are OK expressions
22243 else
22244 Analyze_And_Resolve (Lo, Any_Integer);
22245 Analyze_And_Resolve (Hi, Any_Integer);
22247 Check_Bound (Lo);
22248 Check_Bound (Hi);
22250 if Errs then
22251 Hi := Type_High_Bound (Standard_Long_Long_Integer);
22252 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
22253 end if;
22255 -- Find type to derive from
22257 Lo_Val := Expr_Value (Lo);
22258 Hi_Val := Expr_Value (Hi);
22260 if Can_Derive_From (Standard_Short_Short_Integer) then
22261 Base_Typ := Base_Type (Standard_Short_Short_Integer);
22263 elsif Can_Derive_From (Standard_Short_Integer) then
22264 Base_Typ := Base_Type (Standard_Short_Integer);
22266 elsif Can_Derive_From (Standard_Integer) then
22267 Base_Typ := Base_Type (Standard_Integer);
22269 elsif Can_Derive_From (Standard_Long_Integer) then
22270 Base_Typ := Base_Type (Standard_Long_Integer);
22272 elsif Can_Derive_From (Standard_Long_Long_Integer) then
22273 Check_Restriction (No_Long_Long_Integers, Def);
22274 Base_Typ := Base_Type (Standard_Long_Long_Integer);
22276 else
22277 Base_Typ := Base_Type (Standard_Long_Long_Integer);
22278 Error_Msg_N ("integer type definition bounds out of range", Def);
22279 Hi := Type_High_Bound (Standard_Long_Long_Integer);
22280 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
22281 end if;
22282 end if;
22284 -- Complete both implicit base and declared first subtype entities. The
22285 -- inheritance of the rep item chain ensures that SPARK-related pragmas
22286 -- are not clobbered when the signed integer type acts as a full view of
22287 -- a private type.
22289 Set_Etype (Implicit_Base, Base_Typ);
22290 Set_Size_Info (Implicit_Base, Base_Typ);
22291 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
22292 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
22293 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
22295 Set_Ekind (T, E_Signed_Integer_Subtype);
22296 Set_Etype (T, Implicit_Base);
22297 Set_Size_Info (T, Implicit_Base);
22298 Inherit_Rep_Item_Chain (T, Implicit_Base);
22299 Set_Scalar_Range (T, Def);
22300 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
22301 Set_Is_Constrained (T);
22302 end Signed_Integer_Type_Declaration;
22304 end Sem_Ch3;