[multiple changes]
[gcc.git] / gcc / ada / sem_ch3.adb
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ C H 3 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2015, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
25
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Debug; use Debug;
30 with Elists; use Elists;
31 with Einfo; use Einfo;
32 with Errout; use Errout;
33 with Eval_Fat; use Eval_Fat;
34 with Exp_Ch3; use Exp_Ch3;
35 with Exp_Ch9; use Exp_Ch9;
36 with Exp_Disp; use Exp_Disp;
37 with Exp_Dist; use Exp_Dist;
38 with Exp_Tss; use Exp_Tss;
39 with Exp_Util; use Exp_Util;
40 with Fname; use Fname;
41 with Freeze; use Freeze;
42 with Ghost; use Ghost;
43 with Itypes; use Itypes;
44 with Layout; use Layout;
45 with Lib; use Lib;
46 with Lib.Xref; use Lib.Xref;
47 with Namet; use Namet;
48 with Nmake; use Nmake;
49 with Opt; use Opt;
50 with Restrict; use Restrict;
51 with Rident; use Rident;
52 with Rtsfind; use Rtsfind;
53 with Sem; use Sem;
54 with Sem_Aux; use Sem_Aux;
55 with Sem_Case; use Sem_Case;
56 with Sem_Cat; use Sem_Cat;
57 with Sem_Ch6; use Sem_Ch6;
58 with Sem_Ch7; use Sem_Ch7;
59 with Sem_Ch8; use Sem_Ch8;
60 with Sem_Ch10; use Sem_Ch10;
61 with Sem_Ch12; use Sem_Ch12;
62 with Sem_Ch13; use Sem_Ch13;
63 with Sem_Dim; use Sem_Dim;
64 with Sem_Disp; use Sem_Disp;
65 with Sem_Dist; use Sem_Dist;
66 with Sem_Elim; use Sem_Elim;
67 with Sem_Eval; use Sem_Eval;
68 with Sem_Mech; use Sem_Mech;
69 with Sem_Prag; use Sem_Prag;
70 with Sem_Res; use Sem_Res;
71 with Sem_Smem; use Sem_Smem;
72 with Sem_Type; use Sem_Type;
73 with Sem_Util; use Sem_Util;
74 with Sem_Warn; use Sem_Warn;
75 with Stand; use Stand;
76 with Sinfo; use Sinfo;
77 with Sinput; use Sinput;
78 with Snames; use Snames;
79 with Targparm; use Targparm;
80 with Tbuild; use Tbuild;
81 with Ttypes; use Ttypes;
82 with Uintp; use Uintp;
83 with Urealp; use Urealp;
84
85 package body Sem_Ch3 is
86
87 -----------------------
88 -- Local Subprograms --
89 -----------------------
90
91 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id);
92 -- Ada 2005 (AI-251): Add the tag components corresponding to all the
93 -- abstract interface types implemented by a record type or a derived
94 -- record type.
95
96 procedure Analyze_Object_Contract (Obj_Id : Entity_Id);
97 -- Analyze all delayed pragmas chained on the contract of object Obj_Id as
98 -- if they appeared at the end of the declarative region. The pragmas to be
99 -- considered are:
100 -- Async_Readers
101 -- Async_Writers
102 -- Effective_Reads
103 -- Effective_Writes
104 -- Part_Of
105
106 procedure Build_Derived_Type
107 (N : Node_Id;
108 Parent_Type : Entity_Id;
109 Derived_Type : Entity_Id;
110 Is_Completion : Boolean;
111 Derive_Subps : Boolean := True);
112 -- Create and decorate a Derived_Type given the Parent_Type entity. N is
113 -- the N_Full_Type_Declaration node containing the derived type definition.
114 -- Parent_Type is the entity for the parent type in the derived type
115 -- definition and Derived_Type the actual derived type. Is_Completion must
116 -- be set to False if Derived_Type is the N_Defining_Identifier node in N
117 -- (i.e. Derived_Type = Defining_Identifier (N)). In this case N is not the
118 -- completion of a private type declaration. If Is_Completion is set to
119 -- True, N is the completion of a private type declaration and Derived_Type
120 -- is different from the defining identifier inside N (i.e. Derived_Type /=
121 -- Defining_Identifier (N)). Derive_Subps indicates whether the parent
122 -- subprograms should be derived. The only case where this parameter is
123 -- False is when Build_Derived_Type is recursively called to process an
124 -- implicit derived full type for a type derived from a private type (in
125 -- that case the subprograms must only be derived for the private view of
126 -- the type).
127 --
128 -- ??? These flags need a bit of re-examination and re-documentation:
129 -- ??? are they both necessary (both seem related to the recursion)?
130
131 procedure Build_Derived_Access_Type
132 (N : Node_Id;
133 Parent_Type : Entity_Id;
134 Derived_Type : Entity_Id);
135 -- Subsidiary procedure to Build_Derived_Type. For a derived access type,
136 -- create an implicit base if the parent type is constrained or if the
137 -- subtype indication has a constraint.
138
139 procedure Build_Derived_Array_Type
140 (N : Node_Id;
141 Parent_Type : Entity_Id;
142 Derived_Type : Entity_Id);
143 -- Subsidiary procedure to Build_Derived_Type. For a derived array type,
144 -- create an implicit base if the parent type is constrained or if the
145 -- subtype indication has a constraint.
146
147 procedure Build_Derived_Concurrent_Type
148 (N : Node_Id;
149 Parent_Type : Entity_Id;
150 Derived_Type : Entity_Id);
151 -- Subsidiary procedure to Build_Derived_Type. For a derived task or
152 -- protected type, inherit entries and protected subprograms, check
153 -- legality of discriminant constraints if any.
154
155 procedure Build_Derived_Enumeration_Type
156 (N : Node_Id;
157 Parent_Type : Entity_Id;
158 Derived_Type : Entity_Id);
159 -- Subsidiary procedure to Build_Derived_Type. For a derived enumeration
160 -- type, we must create a new list of literals. Types derived from
161 -- Character and [Wide_]Wide_Character are special-cased.
162
163 procedure Build_Derived_Numeric_Type
164 (N : Node_Id;
165 Parent_Type : Entity_Id;
166 Derived_Type : Entity_Id);
167 -- Subsidiary procedure to Build_Derived_Type. For numeric types, create
168 -- an anonymous base type, and propagate constraint to subtype if needed.
169
170 procedure Build_Derived_Private_Type
171 (N : Node_Id;
172 Parent_Type : Entity_Id;
173 Derived_Type : Entity_Id;
174 Is_Completion : Boolean;
175 Derive_Subps : Boolean := True);
176 -- Subsidiary procedure to Build_Derived_Type. This procedure is complex
177 -- because the parent may or may not have a completion, and the derivation
178 -- may itself be a completion.
179
180 procedure Build_Derived_Record_Type
181 (N : Node_Id;
182 Parent_Type : Entity_Id;
183 Derived_Type : Entity_Id;
184 Derive_Subps : Boolean := True);
185 -- Subsidiary procedure used for tagged and untagged record types
186 -- by Build_Derived_Type and Analyze_Private_Extension_Declaration.
187 -- All parameters are as in Build_Derived_Type except that N, in
188 -- addition to being an N_Full_Type_Declaration node, can also be an
189 -- N_Private_Extension_Declaration node. See the definition of this routine
190 -- for much more info. Derive_Subps indicates whether subprograms should be
191 -- derived from the parent type. The only case where Derive_Subps is False
192 -- is for an implicit derived full type for a type derived from a private
193 -- type (see Build_Derived_Type).
194
195 procedure Build_Discriminal (Discrim : Entity_Id);
196 -- Create the discriminal corresponding to discriminant Discrim, that is
197 -- the parameter corresponding to Discrim to be used in initialization
198 -- procedures for the type where Discrim is a discriminant. Discriminals
199 -- are not used during semantic analysis, and are not fully defined
200 -- entities until expansion. Thus they are not given a scope until
201 -- initialization procedures are built.
202
203 function Build_Discriminant_Constraints
204 (T : Entity_Id;
205 Def : Node_Id;
206 Derived_Def : Boolean := False) return Elist_Id;
207 -- Validate discriminant constraints and return the list of the constraints
208 -- in order of discriminant declarations, where T is the discriminated
209 -- unconstrained type. Def is the N_Subtype_Indication node where the
210 -- discriminants constraints for T are specified. Derived_Def is True
211 -- when building the discriminant constraints in a derived type definition
212 -- of the form "type D (...) is new T (xxx)". In this case T is the parent
213 -- type and Def is the constraint "(xxx)" on T and this routine sets the
214 -- Corresponding_Discriminant field of the discriminants in the derived
215 -- type D to point to the corresponding discriminants in the parent type T.
216
217 procedure Build_Discriminated_Subtype
218 (T : Entity_Id;
219 Def_Id : Entity_Id;
220 Elist : Elist_Id;
221 Related_Nod : Node_Id;
222 For_Access : Boolean := False);
223 -- Subsidiary procedure to Constrain_Discriminated_Type and to
224 -- Process_Incomplete_Dependents. Given
225 --
226 -- T (a possibly discriminated base type)
227 -- Def_Id (a very partially built subtype for T),
228 --
229 -- the call completes Def_Id to be the appropriate E_*_Subtype.
230 --
231 -- The Elist is the list of discriminant constraints if any (it is set
232 -- to No_Elist if T is not a discriminated type, and to an empty list if
233 -- T has discriminants but there are no discriminant constraints). The
234 -- Related_Nod is the same as Decl_Node in Create_Constrained_Components.
235 -- The For_Access says whether or not this subtype is really constraining
236 -- an access type. That is its sole purpose is the designated type of an
237 -- access type -- in which case a Private_Subtype Is_For_Access_Subtype
238 -- is built to avoid freezing T when the access subtype is frozen.
239
240 function Build_Scalar_Bound
241 (Bound : Node_Id;
242 Par_T : Entity_Id;
243 Der_T : Entity_Id) return Node_Id;
244 -- The bounds of a derived scalar type are conversions of the bounds of
245 -- the parent type. Optimize the representation if the bounds are literals.
246 -- Needs a more complete spec--what are the parameters exactly, and what
247 -- exactly is the returned value, and how is Bound affected???
248
249 procedure Build_Underlying_Full_View
250 (N : Node_Id;
251 Typ : Entity_Id;
252 Par : Entity_Id);
253 -- If the completion of a private type is itself derived from a private
254 -- type, or if the full view of a private subtype is itself private, the
255 -- back-end has no way to compute the actual size of this type. We build
256 -- an internal subtype declaration of the proper parent type to convey
257 -- this information. This extra mechanism is needed because a full
258 -- view cannot itself have a full view (it would get clobbered during
259 -- view exchanges).
260
261 procedure Check_Access_Discriminant_Requires_Limited
262 (D : Node_Id;
263 Loc : Node_Id);
264 -- Check the restriction that the type to which an access discriminant
265 -- belongs must be a concurrent type or a descendant of a type with
266 -- the reserved word 'limited' in its declaration.
267
268 procedure Check_Anonymous_Access_Components
269 (Typ_Decl : Node_Id;
270 Typ : Entity_Id;
271 Prev : Entity_Id;
272 Comp_List : Node_Id);
273 -- Ada 2005 AI-382: an access component in a record definition can refer to
274 -- the enclosing record, in which case it denotes the type itself, and not
275 -- the current instance of the type. We create an anonymous access type for
276 -- the component, and flag it as an access to a component, so accessibility
277 -- checks are properly performed on it. The declaration of the access type
278 -- is placed ahead of that of the record to prevent order-of-elaboration
279 -- circularity issues in Gigi. We create an incomplete type for the record
280 -- declaration, which is the designated type of the anonymous access.
281
282 procedure Check_Delta_Expression (E : Node_Id);
283 -- Check that the expression represented by E is suitable for use as a
284 -- delta expression, i.e. it is of real type and is static.
285
286 procedure Check_Digits_Expression (E : Node_Id);
287 -- Check that the expression represented by E is suitable for use as a
288 -- digits expression, i.e. it is of integer type, positive and static.
289
290 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id);
291 -- Validate the initialization of an object declaration. T is the required
292 -- type, and Exp is the initialization expression.
293
294 procedure Check_Interfaces (N : Node_Id; Def : Node_Id);
295 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
296
297 procedure Check_Or_Process_Discriminants
298 (N : Node_Id;
299 T : Entity_Id;
300 Prev : Entity_Id := Empty);
301 -- If N is the full declaration of the completion T of an incomplete or
302 -- private type, check its discriminants (which are already known to be
303 -- conformant with those of the partial view, see Find_Type_Name),
304 -- otherwise process them. Prev is the entity of the partial declaration,
305 -- if any.
306
307 procedure Check_Real_Bound (Bound : Node_Id);
308 -- Check given bound for being of real type and static. If not, post an
309 -- appropriate message, and rewrite the bound with the real literal zero.
310
311 procedure Constant_Redeclaration
312 (Id : Entity_Id;
313 N : Node_Id;
314 T : out Entity_Id);
315 -- Various checks on legality of full declaration of deferred constant.
316 -- Id is the entity for the redeclaration, N is the N_Object_Declaration,
317 -- node. The caller has not yet set any attributes of this entity.
318
319 function Contain_Interface
320 (Iface : Entity_Id;
321 Ifaces : Elist_Id) return Boolean;
322 -- Ada 2005: Determine whether Iface is present in the list Ifaces
323
324 procedure Convert_Scalar_Bounds
325 (N : Node_Id;
326 Parent_Type : Entity_Id;
327 Derived_Type : Entity_Id;
328 Loc : Source_Ptr);
329 -- For derived scalar types, convert the bounds in the type definition to
330 -- the derived type, and complete their analysis. Given a constraint of the
331 -- form ".. new T range Lo .. Hi", Lo and Hi are analyzed and resolved with
332 -- T'Base, the parent_type. The bounds of the derived type (the anonymous
333 -- base) are copies of Lo and Hi. Finally, the bounds of the derived
334 -- subtype are conversions of those bounds to the derived_type, so that
335 -- their typing is consistent.
336
337 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id);
338 -- Copies attributes from array base type T2 to array base type T1. Copies
339 -- only attributes that apply to base types, but not subtypes.
340
341 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id);
342 -- Copies attributes from array subtype T2 to array subtype T1. Copies
343 -- attributes that apply to both subtypes and base types.
344
345 procedure Create_Constrained_Components
346 (Subt : Entity_Id;
347 Decl_Node : Node_Id;
348 Typ : Entity_Id;
349 Constraints : Elist_Id);
350 -- Build the list of entities for a constrained discriminated record
351 -- subtype. If a component depends on a discriminant, replace its subtype
352 -- using the discriminant values in the discriminant constraint. Subt
353 -- is the defining identifier for the subtype whose list of constrained
354 -- entities we will create. Decl_Node is the type declaration node where
355 -- we will attach all the itypes created. Typ is the base discriminated
356 -- type for the subtype Subt. Constraints is the list of discriminant
357 -- constraints for Typ.
358
359 function Constrain_Component_Type
360 (Comp : Entity_Id;
361 Constrained_Typ : Entity_Id;
362 Related_Node : Node_Id;
363 Typ : Entity_Id;
364 Constraints : Elist_Id) return Entity_Id;
365 -- Given a discriminated base type Typ, a list of discriminant constraints,
366 -- Constraints, for Typ and a component Comp of Typ, create and return the
367 -- type corresponding to Etype (Comp) where all discriminant references
368 -- are replaced with the corresponding constraint. If Etype (Comp) contains
369 -- no discriminant references then it is returned as-is. Constrained_Typ
370 -- is the final constrained subtype to which the constrained component
371 -- belongs. Related_Node is the node where we attach all created itypes.
372
373 procedure Constrain_Access
374 (Def_Id : in out Entity_Id;
375 S : Node_Id;
376 Related_Nod : Node_Id);
377 -- Apply a list of constraints to an access type. If Def_Id is empty, it is
378 -- an anonymous type created for a subtype indication. In that case it is
379 -- created in the procedure and attached to Related_Nod.
380
381 procedure Constrain_Array
382 (Def_Id : in out Entity_Id;
383 SI : Node_Id;
384 Related_Nod : Node_Id;
385 Related_Id : Entity_Id;
386 Suffix : Character);
387 -- Apply a list of index constraints to an unconstrained array type. The
388 -- first parameter is the entity for the resulting subtype. A value of
389 -- Empty for Def_Id indicates that an implicit type must be created, but
390 -- creation is delayed (and must be done by this procedure) because other
391 -- subsidiary implicit types must be created first (which is why Def_Id
392 -- is an in/out parameter). The second parameter is a subtype indication
393 -- node for the constrained array to be created (e.g. something of the
394 -- form string (1 .. 10)). Related_Nod gives the place where this type
395 -- has to be inserted in the tree. The Related_Id and Suffix parameters
396 -- are used to build the associated Implicit type name.
397
398 procedure Constrain_Concurrent
399 (Def_Id : in out Entity_Id;
400 SI : Node_Id;
401 Related_Nod : Node_Id;
402 Related_Id : Entity_Id;
403 Suffix : Character);
404 -- Apply list of discriminant constraints to an unconstrained concurrent
405 -- type.
406 --
407 -- SI is the N_Subtype_Indication node containing the constraint and
408 -- the unconstrained type to constrain.
409 --
410 -- Def_Id is the entity for the resulting constrained subtype. A value
411 -- of Empty for Def_Id indicates that an implicit type must be created,
412 -- but creation is delayed (and must be done by this procedure) because
413 -- other subsidiary implicit types must be created first (which is why
414 -- Def_Id is an in/out parameter).
415 --
416 -- Related_Nod gives the place where this type has to be inserted
417 -- in the tree.
418 --
419 -- The last two arguments are used to create its external name if needed.
420
421 function Constrain_Corresponding_Record
422 (Prot_Subt : Entity_Id;
423 Corr_Rec : Entity_Id;
424 Related_Nod : Node_Id) return Entity_Id;
425 -- When constraining a protected type or task type with discriminants,
426 -- constrain the corresponding record with the same discriminant values.
427
428 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id);
429 -- Constrain a decimal fixed point type with a digits constraint and/or a
430 -- range constraint, and build E_Decimal_Fixed_Point_Subtype entity.
431
432 procedure Constrain_Discriminated_Type
433 (Def_Id : Entity_Id;
434 S : Node_Id;
435 Related_Nod : Node_Id;
436 For_Access : Boolean := False);
437 -- Process discriminant constraints of composite type. Verify that values
438 -- have been provided for all discriminants, that the original type is
439 -- unconstrained, and that the types of the supplied expressions match
440 -- the discriminant types. The first three parameters are like in routine
441 -- Constrain_Concurrent. See Build_Discriminated_Subtype for an explanation
442 -- of For_Access.
443
444 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id);
445 -- Constrain an enumeration type with a range constraint. This is identical
446 -- to Constrain_Integer, but for the Ekind of the resulting subtype.
447
448 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id);
449 -- Constrain a floating point type with either a digits constraint
450 -- and/or a range constraint, building a E_Floating_Point_Subtype.
451
452 procedure Constrain_Index
453 (Index : Node_Id;
454 S : Node_Id;
455 Related_Nod : Node_Id;
456 Related_Id : Entity_Id;
457 Suffix : Character;
458 Suffix_Index : Nat);
459 -- Process an index constraint S in a constrained array declaration. The
460 -- constraint can be a subtype name, or a range with or without an explicit
461 -- subtype mark. The index is the corresponding index of the unconstrained
462 -- array. The Related_Id and Suffix parameters are used to build the
463 -- associated Implicit type name.
464
465 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id);
466 -- Build subtype of a signed or modular integer type
467
468 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id);
469 -- Constrain an ordinary fixed point type with a range constraint, and
470 -- build an E_Ordinary_Fixed_Point_Subtype entity.
471
472 procedure Copy_And_Swap (Priv, Full : Entity_Id);
473 -- Copy the Priv entity into the entity of its full declaration then swap
474 -- the two entities in such a manner that the former private type is now
475 -- seen as a full type.
476
477 procedure Decimal_Fixed_Point_Type_Declaration
478 (T : Entity_Id;
479 Def : Node_Id);
480 -- Create a new decimal fixed point type, and apply the constraint to
481 -- obtain a subtype of this new type.
482
483 procedure Complete_Private_Subtype
484 (Priv : Entity_Id;
485 Full : Entity_Id;
486 Full_Base : Entity_Id;
487 Related_Nod : Node_Id);
488 -- Complete the implicit full view of a private subtype by setting the
489 -- appropriate semantic fields. If the full view of the parent is a record
490 -- type, build constrained components of subtype.
491
492 procedure Derive_Progenitor_Subprograms
493 (Parent_Type : Entity_Id;
494 Tagged_Type : Entity_Id);
495 -- Ada 2005 (AI-251): To complete type derivation, collect the primitive
496 -- operations of progenitors of Tagged_Type, and replace the subsidiary
497 -- subtypes with Tagged_Type, to build the specs of the inherited interface
498 -- primitives. The derived primitives are aliased to those of the
499 -- interface. This routine takes care also of transferring to the full view
500 -- subprograms associated with the partial view of Tagged_Type that cover
501 -- interface primitives.
502
503 procedure Derived_Standard_Character
504 (N : Node_Id;
505 Parent_Type : Entity_Id;
506 Derived_Type : Entity_Id);
507 -- Subsidiary procedure to Build_Derived_Enumeration_Type which handles
508 -- derivations from types Standard.Character and Standard.Wide_Character.
509
510 procedure Derived_Type_Declaration
511 (T : Entity_Id;
512 N : Node_Id;
513 Is_Completion : Boolean);
514 -- Process a derived type declaration. Build_Derived_Type is invoked
515 -- to process the actual derived type definition. Parameters N and
516 -- Is_Completion have the same meaning as in Build_Derived_Type.
517 -- T is the N_Defining_Identifier for the entity defined in the
518 -- N_Full_Type_Declaration node N, that is T is the derived type.
519
520 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id);
521 -- Insert each literal in symbol table, as an overloadable identifier. Each
522 -- enumeration type is mapped into a sequence of integers, and each literal
523 -- is defined as a constant with integer value. If any of the literals are
524 -- character literals, the type is a character type, which means that
525 -- strings are legal aggregates for arrays of components of the type.
526
527 function Expand_To_Stored_Constraint
528 (Typ : Entity_Id;
529 Constraint : Elist_Id) return Elist_Id;
530 -- Given a constraint (i.e. a list of expressions) on the discriminants of
531 -- Typ, expand it into a constraint on the stored discriminants and return
532 -- the new list of expressions constraining the stored discriminants.
533
534 function Find_Type_Of_Object
535 (Obj_Def : Node_Id;
536 Related_Nod : Node_Id) return Entity_Id;
537 -- Get type entity for object referenced by Obj_Def, attaching the implicit
538 -- types generated to Related_Nod.
539
540 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id);
541 -- Create a new float and apply the constraint to obtain subtype of it
542
543 function Has_Range_Constraint (N : Node_Id) return Boolean;
544 -- Given an N_Subtype_Indication node N, return True if a range constraint
545 -- is present, either directly, or as part of a digits or delta constraint.
546 -- In addition, a digits constraint in the decimal case returns True, since
547 -- it establishes a default range if no explicit range is present.
548
549 function Inherit_Components
550 (N : Node_Id;
551 Parent_Base : Entity_Id;
552 Derived_Base : Entity_Id;
553 Is_Tagged : Boolean;
554 Inherit_Discr : Boolean;
555 Discs : Elist_Id) return Elist_Id;
556 -- Called from Build_Derived_Record_Type to inherit the components of
557 -- Parent_Base (a base type) into the Derived_Base (the derived base type).
558 -- For more information on derived types and component inheritance please
559 -- consult the comment above the body of Build_Derived_Record_Type.
560 --
561 -- N is the original derived type declaration
562 --
563 -- Is_Tagged is set if we are dealing with tagged types
564 --
565 -- If Inherit_Discr is set, Derived_Base inherits its discriminants from
566 -- Parent_Base, otherwise no discriminants are inherited.
567 --
568 -- Discs gives the list of constraints that apply to Parent_Base in the
569 -- derived type declaration. If Discs is set to No_Elist, then we have
570 -- the following situation:
571 --
572 -- type Parent (D1..Dn : ..) is [tagged] record ...;
573 -- type Derived is new Parent [with ...];
574 --
575 -- which gets treated as
576 --
577 -- type Derived (D1..Dn : ..) is new Parent (D1,..,Dn) [with ...];
578 --
579 -- For untagged types the returned value is an association list. The list
580 -- starts from the association (Parent_Base => Derived_Base), and then it
581 -- contains a sequence of the associations of the form
582 --
583 -- (Old_Component => New_Component),
584 --
585 -- where Old_Component is the Entity_Id of a component in Parent_Base and
586 -- New_Component is the Entity_Id of the corresponding component in
587 -- Derived_Base. For untagged records, this association list is needed when
588 -- copying the record declaration for the derived base. In the tagged case
589 -- the value returned is irrelevant.
590
591 procedure Inherit_Predicate_Flags (Subt, Par : Entity_Id);
592 -- Propagate static and dynamic predicate flags from a parent to the
593 -- subtype in a subtype declaration with and without constraints.
594
595 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean;
596 -- Subsidiary to Check_Abstract_Overriding and Derive_Subprogram.
597 -- Determine whether subprogram Subp is a procedure subject to pragma
598 -- Extensions_Visible with value False and has at least one controlling
599 -- parameter of mode OUT.
600
601 function Is_Valid_Constraint_Kind
602 (T_Kind : Type_Kind;
603 Constraint_Kind : Node_Kind) return Boolean;
604 -- Returns True if it is legal to apply the given kind of constraint to the
605 -- given kind of type (index constraint to an array type, for example).
606
607 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id);
608 -- Create new modular type. Verify that modulus is in bounds
609
610 procedure New_Concatenation_Op (Typ : Entity_Id);
611 -- Create an abbreviated declaration for an operator in order to
612 -- materialize concatenation on array types.
613
614 procedure Ordinary_Fixed_Point_Type_Declaration
615 (T : Entity_Id;
616 Def : Node_Id);
617 -- Create a new ordinary fixed point type, and apply the constraint to
618 -- obtain subtype of it.
619
620 procedure Prepare_Private_Subtype_Completion
621 (Id : Entity_Id;
622 Related_Nod : Node_Id);
623 -- Id is a subtype of some private type. Creates the full declaration
624 -- associated with Id whenever possible, i.e. when the full declaration
625 -- of the base type is already known. Records each subtype into
626 -- Private_Dependents of the base type.
627
628 procedure Process_Incomplete_Dependents
629 (N : Node_Id;
630 Full_T : Entity_Id;
631 Inc_T : Entity_Id);
632 -- Process all entities that depend on an incomplete type. There include
633 -- subtypes, subprogram types that mention the incomplete type in their
634 -- profiles, and subprogram with access parameters that designate the
635 -- incomplete type.
636
637 -- Inc_T is the defining identifier of an incomplete type declaration, its
638 -- Ekind is E_Incomplete_Type.
639 --
640 -- N is the corresponding N_Full_Type_Declaration for Inc_T.
641 --
642 -- Full_T is N's defining identifier.
643 --
644 -- Subtypes of incomplete types with discriminants are completed when the
645 -- parent type is. This is simpler than private subtypes, because they can
646 -- only appear in the same scope, and there is no need to exchange views.
647 -- Similarly, access_to_subprogram types may have a parameter or a return
648 -- type that is an incomplete type, and that must be replaced with the
649 -- full type.
650 --
651 -- If the full type is tagged, subprogram with access parameters that
652 -- designated the incomplete may be primitive operations of the full type,
653 -- and have to be processed accordingly.
654
655 procedure Process_Real_Range_Specification (Def : Node_Id);
656 -- Given the type definition for a real type, this procedure processes and
657 -- checks the real range specification of this type definition if one is
658 -- present. If errors are found, error messages are posted, and the
659 -- Real_Range_Specification of Def is reset to Empty.
660
661 procedure Propagate_Default_Init_Cond_Attributes
662 (From_Typ : Entity_Id;
663 To_Typ : Entity_Id;
664 Parent_To_Derivation : Boolean := False;
665 Private_To_Full_View : Boolean := False);
666 -- Subsidiary to routines Build_Derived_Type and Process_Full_View. Inherit
667 -- all attributes related to pragma Default_Initial_Condition from From_Typ
668 -- to To_Typ. Flag Parent_To_Derivation should be set when the context is
669 -- the creation of a derived type. Flag Private_To_Full_View should be set
670 -- when processing both views of a private type.
671
672 procedure Record_Type_Declaration
673 (T : Entity_Id;
674 N : Node_Id;
675 Prev : Entity_Id);
676 -- Process a record type declaration (for both untagged and tagged
677 -- records). Parameters T and N are exactly like in procedure
678 -- Derived_Type_Declaration, except that no flag Is_Completion is needed
679 -- for this routine. If this is the completion of an incomplete type
680 -- declaration, Prev is the entity of the incomplete declaration, used for
681 -- cross-referencing. Otherwise Prev = T.
682
683 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id);
684 -- This routine is used to process the actual record type definition (both
685 -- for untagged and tagged records). Def is a record type definition node.
686 -- This procedure analyzes the components in this record type definition.
687 -- Prev_T is the entity for the enclosing record type. It is provided so
688 -- that its Has_Task flag can be set if any of the component have Has_Task
689 -- set. If the declaration is the completion of an incomplete type
690 -- declaration, Prev_T is the original incomplete type, whose full view is
691 -- the record type.
692
693 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id);
694 -- Subsidiary to Build_Derived_Record_Type. For untagged records, we
695 -- build a copy of the declaration tree of the parent, and we create
696 -- independently the list of components for the derived type. Semantic
697 -- information uses the component entities, but record representation
698 -- clauses are validated on the declaration tree. This procedure replaces
699 -- discriminants and components in the declaration with those that have
700 -- been created by Inherit_Components.
701
702 procedure Set_Fixed_Range
703 (E : Entity_Id;
704 Loc : Source_Ptr;
705 Lo : Ureal;
706 Hi : Ureal);
707 -- Build a range node with the given bounds and set it as the Scalar_Range
708 -- of the given fixed-point type entity. Loc is the source location used
709 -- for the constructed range. See body for further details.
710
711 procedure Set_Scalar_Range_For_Subtype
712 (Def_Id : Entity_Id;
713 R : Node_Id;
714 Subt : Entity_Id);
715 -- This routine is used to set the scalar range field for a subtype given
716 -- Def_Id, the entity for the subtype, and R, the range expression for the
717 -- scalar range. Subt provides the parent subtype to be used to analyze,
718 -- resolve, and check the given range.
719
720 procedure Set_Default_SSO (T : Entity_Id);
721 -- T is the entity for an array or record being declared. This procedure
722 -- sets the flags SSO_Set_Low_By_Default/SSO_Set_High_By_Default according
723 -- to the setting of Opt.Default_SSO.
724
725 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id);
726 -- Create a new signed integer entity, and apply the constraint to obtain
727 -- the required first named subtype of this type.
728
729 procedure Set_Stored_Constraint_From_Discriminant_Constraint
730 (E : Entity_Id);
731 -- E is some record type. This routine computes E's Stored_Constraint
732 -- from its Discriminant_Constraint.
733
734 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id);
735 -- Check that an entity in a list of progenitors is an interface,
736 -- emit error otherwise.
737
738 -----------------------
739 -- Access_Definition --
740 -----------------------
741
742 function Access_Definition
743 (Related_Nod : Node_Id;
744 N : Node_Id) return Entity_Id
745 is
746 Anon_Type : Entity_Id;
747 Anon_Scope : Entity_Id;
748 Desig_Type : Entity_Id;
749 Enclosing_Prot_Type : Entity_Id := Empty;
750
751 begin
752 Check_SPARK_05_Restriction ("access type is not allowed", N);
753
754 if Is_Entry (Current_Scope)
755 and then Is_Task_Type (Etype (Scope (Current_Scope)))
756 then
757 Error_Msg_N ("task entries cannot have access parameters", N);
758 return Empty;
759 end if;
760
761 -- Ada 2005: For an object declaration the corresponding anonymous
762 -- type is declared in the current scope.
763
764 -- If the access definition is the return type of another access to
765 -- function, scope is the current one, because it is the one of the
766 -- current type declaration, except for the pathological case below.
767
768 if Nkind_In (Related_Nod, N_Object_Declaration,
769 N_Access_Function_Definition)
770 then
771 Anon_Scope := Current_Scope;
772
773 -- A pathological case: function returning access functions that
774 -- return access functions, etc. Each anonymous access type created
775 -- is in the enclosing scope of the outermost function.
776
777 declare
778 Par : Node_Id;
779
780 begin
781 Par := Related_Nod;
782 while Nkind_In (Par, N_Access_Function_Definition,
783 N_Access_Definition)
784 loop
785 Par := Parent (Par);
786 end loop;
787
788 if Nkind (Par) = N_Function_Specification then
789 Anon_Scope := Scope (Defining_Entity (Par));
790 end if;
791 end;
792
793 -- For the anonymous function result case, retrieve the scope of the
794 -- function specification's associated entity rather than using the
795 -- current scope. The current scope will be the function itself if the
796 -- formal part is currently being analyzed, but will be the parent scope
797 -- in the case of a parameterless function, and we always want to use
798 -- the function's parent scope. Finally, if the function is a child
799 -- unit, we must traverse the tree to retrieve the proper entity.
800
801 elsif Nkind (Related_Nod) = N_Function_Specification
802 and then Nkind (Parent (N)) /= N_Parameter_Specification
803 then
804 -- If the current scope is a protected type, the anonymous access
805 -- is associated with one of the protected operations, and must
806 -- be available in the scope that encloses the protected declaration.
807 -- Otherwise the type is in the scope enclosing the subprogram.
808
809 -- If the function has formals, The return type of a subprogram
810 -- declaration is analyzed in the scope of the subprogram (see
811 -- Process_Formals) and thus the protected type, if present, is
812 -- the scope of the current function scope.
813
814 if Ekind (Current_Scope) = E_Protected_Type then
815 Enclosing_Prot_Type := Current_Scope;
816
817 elsif Ekind (Current_Scope) = E_Function
818 and then Ekind (Scope (Current_Scope)) = E_Protected_Type
819 then
820 Enclosing_Prot_Type := Scope (Current_Scope);
821 end if;
822
823 if Present (Enclosing_Prot_Type) then
824 Anon_Scope := Scope (Enclosing_Prot_Type);
825
826 else
827 Anon_Scope := Scope (Defining_Entity (Related_Nod));
828 end if;
829
830 -- For an access type definition, if the current scope is a child
831 -- unit it is the scope of the type.
832
833 elsif Is_Compilation_Unit (Current_Scope) then
834 Anon_Scope := Current_Scope;
835
836 -- For access formals, access components, and access discriminants, the
837 -- scope is that of the enclosing declaration,
838
839 else
840 Anon_Scope := Scope (Current_Scope);
841 end if;
842
843 Anon_Type :=
844 Create_Itype
845 (E_Anonymous_Access_Type, Related_Nod, Scope_Id => Anon_Scope);
846
847 if All_Present (N)
848 and then Ada_Version >= Ada_2005
849 then
850 Error_Msg_N ("ALL is not permitted for anonymous access types", N);
851 end if;
852
853 -- Ada 2005 (AI-254): In case of anonymous access to subprograms call
854 -- the corresponding semantic routine
855
856 if Present (Access_To_Subprogram_Definition (N)) then
857
858 -- Compiler runtime units are compiled in Ada 2005 mode when building
859 -- the runtime library but must also be compilable in Ada 95 mode
860 -- (when bootstrapping the compiler).
861
862 Check_Compiler_Unit ("anonymous access to subprogram", N);
863
864 Access_Subprogram_Declaration
865 (T_Name => Anon_Type,
866 T_Def => Access_To_Subprogram_Definition (N));
867
868 if Ekind (Anon_Type) = E_Access_Protected_Subprogram_Type then
869 Set_Ekind
870 (Anon_Type, E_Anonymous_Access_Protected_Subprogram_Type);
871 else
872 Set_Ekind (Anon_Type, E_Anonymous_Access_Subprogram_Type);
873 end if;
874
875 Set_Can_Use_Internal_Rep
876 (Anon_Type, not Always_Compatible_Rep_On_Target);
877
878 -- If the anonymous access is associated with a protected operation,
879 -- create a reference to it after the enclosing protected definition
880 -- because the itype will be used in the subsequent bodies.
881
882 -- If the anonymous access itself is protected, a full type
883 -- declaratiton will be created for it, so that the equivalent
884 -- record type can be constructed. For further details, see
885 -- Replace_Anonymous_Access_To_Protected-Subprogram.
886
887 if Ekind (Current_Scope) = E_Protected_Type
888 and then not Protected_Present (Access_To_Subprogram_Definition (N))
889 then
890 Build_Itype_Reference (Anon_Type, Parent (Current_Scope));
891 end if;
892
893 return Anon_Type;
894 end if;
895
896 Find_Type (Subtype_Mark (N));
897 Desig_Type := Entity (Subtype_Mark (N));
898
899 Set_Directly_Designated_Type (Anon_Type, Desig_Type);
900 Set_Etype (Anon_Type, Anon_Type);
901
902 -- Make sure the anonymous access type has size and alignment fields
903 -- set, as required by gigi. This is necessary in the case of the
904 -- Task_Body_Procedure.
905
906 if not Has_Private_Component (Desig_Type) then
907 Layout_Type (Anon_Type);
908 end if;
909
910 -- Ada 2005 (AI-231): Ada 2005 semantics for anonymous access differs
911 -- from Ada 95 semantics. In Ada 2005, anonymous access must specify if
912 -- the null value is allowed. In Ada 95 the null value is never allowed.
913
914 if Ada_Version >= Ada_2005 then
915 Set_Can_Never_Be_Null (Anon_Type, Null_Exclusion_Present (N));
916 else
917 Set_Can_Never_Be_Null (Anon_Type, True);
918 end if;
919
920 -- The anonymous access type is as public as the discriminated type or
921 -- subprogram that defines it. It is imported (for back-end purposes)
922 -- if the designated type is.
923
924 Set_Is_Public (Anon_Type, Is_Public (Scope (Anon_Type)));
925
926 -- Ada 2005 (AI-231): Propagate the access-constant attribute
927
928 Set_Is_Access_Constant (Anon_Type, Constant_Present (N));
929
930 -- The context is either a subprogram declaration, object declaration,
931 -- or an access discriminant, in a private or a full type declaration.
932 -- In the case of a subprogram, if the designated type is incomplete,
933 -- the operation will be a primitive operation of the full type, to be
934 -- updated subsequently. If the type is imported through a limited_with
935 -- clause, the subprogram is not a primitive operation of the type
936 -- (which is declared elsewhere in some other scope).
937
938 if Ekind (Desig_Type) = E_Incomplete_Type
939 and then not From_Limited_With (Desig_Type)
940 and then Is_Overloadable (Current_Scope)
941 then
942 Append_Elmt (Current_Scope, Private_Dependents (Desig_Type));
943 Set_Has_Delayed_Freeze (Current_Scope);
944 end if;
945
946 -- Ada 2005: If the designated type is an interface that may contain
947 -- tasks, create a Master entity for the declaration. This must be done
948 -- before expansion of the full declaration, because the declaration may
949 -- include an expression that is an allocator, whose expansion needs the
950 -- proper Master for the created tasks.
951
952 if Nkind (Related_Nod) = N_Object_Declaration and then Expander_Active
953 then
954 if Is_Interface (Desig_Type) and then Is_Limited_Record (Desig_Type)
955 then
956 Build_Class_Wide_Master (Anon_Type);
957
958 -- Similarly, if the type is an anonymous access that designates
959 -- tasks, create a master entity for it in the current context.
960
961 elsif Has_Task (Desig_Type) and then Comes_From_Source (Related_Nod)
962 then
963 Build_Master_Entity (Defining_Identifier (Related_Nod));
964 Build_Master_Renaming (Anon_Type);
965 end if;
966 end if;
967
968 -- For a private component of a protected type, it is imperative that
969 -- the back-end elaborate the type immediately after the protected
970 -- declaration, because this type will be used in the declarations
971 -- created for the component within each protected body, so we must
972 -- create an itype reference for it now.
973
974 if Nkind (Parent (Related_Nod)) = N_Protected_Definition then
975 Build_Itype_Reference (Anon_Type, Parent (Parent (Related_Nod)));
976
977 -- Similarly, if the access definition is the return result of a
978 -- function, create an itype reference for it because it will be used
979 -- within the function body. For a regular function that is not a
980 -- compilation unit, insert reference after the declaration. For a
981 -- protected operation, insert it after the enclosing protected type
982 -- declaration. In either case, do not create a reference for a type
983 -- obtained through a limited_with clause, because this would introduce
984 -- semantic dependencies.
985
986 -- Similarly, do not create a reference if the designated type is a
987 -- generic formal, because no use of it will reach the backend.
988
989 elsif Nkind (Related_Nod) = N_Function_Specification
990 and then not From_Limited_With (Desig_Type)
991 and then not Is_Generic_Type (Desig_Type)
992 then
993 if Present (Enclosing_Prot_Type) then
994 Build_Itype_Reference (Anon_Type, Parent (Enclosing_Prot_Type));
995
996 elsif Is_List_Member (Parent (Related_Nod))
997 and then Nkind (Parent (N)) /= N_Parameter_Specification
998 then
999 Build_Itype_Reference (Anon_Type, Parent (Related_Nod));
1000 end if;
1001
1002 -- Finally, create an itype reference for an object declaration of an
1003 -- anonymous access type. This is strictly necessary only for deferred
1004 -- constants, but in any case will avoid out-of-scope problems in the
1005 -- back-end.
1006
1007 elsif Nkind (Related_Nod) = N_Object_Declaration then
1008 Build_Itype_Reference (Anon_Type, Related_Nod);
1009 end if;
1010
1011 return Anon_Type;
1012 end Access_Definition;
1013
1014 -----------------------------------
1015 -- Access_Subprogram_Declaration --
1016 -----------------------------------
1017
1018 procedure Access_Subprogram_Declaration
1019 (T_Name : Entity_Id;
1020 T_Def : Node_Id)
1021 is
1022 procedure Check_For_Premature_Usage (Def : Node_Id);
1023 -- Check that type T_Name is not used, directly or recursively, as a
1024 -- parameter or a return type in Def. Def is either a subtype, an
1025 -- access_definition, or an access_to_subprogram_definition.
1026
1027 -------------------------------
1028 -- Check_For_Premature_Usage --
1029 -------------------------------
1030
1031 procedure Check_For_Premature_Usage (Def : Node_Id) is
1032 Param : Node_Id;
1033
1034 begin
1035 -- Check for a subtype mark
1036
1037 if Nkind (Def) in N_Has_Etype then
1038 if Etype (Def) = T_Name then
1039 Error_Msg_N
1040 ("type& cannot be used before end of its declaration", Def);
1041 end if;
1042
1043 -- If this is not a subtype, then this is an access_definition
1044
1045 elsif Nkind (Def) = N_Access_Definition then
1046 if Present (Access_To_Subprogram_Definition (Def)) then
1047 Check_For_Premature_Usage
1048 (Access_To_Subprogram_Definition (Def));
1049 else
1050 Check_For_Premature_Usage (Subtype_Mark (Def));
1051 end if;
1052
1053 -- The only cases left are N_Access_Function_Definition and
1054 -- N_Access_Procedure_Definition.
1055
1056 else
1057 if Present (Parameter_Specifications (Def)) then
1058 Param := First (Parameter_Specifications (Def));
1059 while Present (Param) loop
1060 Check_For_Premature_Usage (Parameter_Type (Param));
1061 Param := Next (Param);
1062 end loop;
1063 end if;
1064
1065 if Nkind (Def) = N_Access_Function_Definition then
1066 Check_For_Premature_Usage (Result_Definition (Def));
1067 end if;
1068 end if;
1069 end Check_For_Premature_Usage;
1070
1071 -- Local variables
1072
1073 Formals : constant List_Id := Parameter_Specifications (T_Def);
1074 Formal : Entity_Id;
1075 D_Ityp : Node_Id;
1076 Desig_Type : constant Entity_Id :=
1077 Create_Itype (E_Subprogram_Type, Parent (T_Def));
1078
1079 -- Start of processing for Access_Subprogram_Declaration
1080
1081 begin
1082 Check_SPARK_05_Restriction ("access type is not allowed", T_Def);
1083
1084 -- Associate the Itype node with the inner full-type declaration or
1085 -- subprogram spec or entry body. This is required to handle nested
1086 -- anonymous declarations. For example:
1087
1088 -- procedure P
1089 -- (X : access procedure
1090 -- (Y : access procedure
1091 -- (Z : access T)))
1092
1093 D_Ityp := Associated_Node_For_Itype (Desig_Type);
1094 while not (Nkind_In (D_Ityp, N_Full_Type_Declaration,
1095 N_Private_Type_Declaration,
1096 N_Private_Extension_Declaration,
1097 N_Procedure_Specification,
1098 N_Function_Specification,
1099 N_Entry_Body)
1100
1101 or else
1102 Nkind_In (D_Ityp, N_Object_Declaration,
1103 N_Object_Renaming_Declaration,
1104 N_Formal_Object_Declaration,
1105 N_Formal_Type_Declaration,
1106 N_Task_Type_Declaration,
1107 N_Protected_Type_Declaration))
1108 loop
1109 D_Ityp := Parent (D_Ityp);
1110 pragma Assert (D_Ityp /= Empty);
1111 end loop;
1112
1113 Set_Associated_Node_For_Itype (Desig_Type, D_Ityp);
1114
1115 if Nkind_In (D_Ityp, N_Procedure_Specification,
1116 N_Function_Specification)
1117 then
1118 Set_Scope (Desig_Type, Scope (Defining_Entity (D_Ityp)));
1119
1120 elsif Nkind_In (D_Ityp, N_Full_Type_Declaration,
1121 N_Object_Declaration,
1122 N_Object_Renaming_Declaration,
1123 N_Formal_Type_Declaration)
1124 then
1125 Set_Scope (Desig_Type, Scope (Defining_Identifier (D_Ityp)));
1126 end if;
1127
1128 if Nkind (T_Def) = N_Access_Function_Definition then
1129 if Nkind (Result_Definition (T_Def)) = N_Access_Definition then
1130 declare
1131 Acc : constant Node_Id := Result_Definition (T_Def);
1132
1133 begin
1134 if Present (Access_To_Subprogram_Definition (Acc))
1135 and then
1136 Protected_Present (Access_To_Subprogram_Definition (Acc))
1137 then
1138 Set_Etype
1139 (Desig_Type,
1140 Replace_Anonymous_Access_To_Protected_Subprogram
1141 (T_Def));
1142
1143 else
1144 Set_Etype
1145 (Desig_Type,
1146 Access_Definition (T_Def, Result_Definition (T_Def)));
1147 end if;
1148 end;
1149
1150 else
1151 Analyze (Result_Definition (T_Def));
1152
1153 declare
1154 Typ : constant Entity_Id := Entity (Result_Definition (T_Def));
1155
1156 begin
1157 -- If a null exclusion is imposed on the result type, then
1158 -- create a null-excluding itype (an access subtype) and use
1159 -- it as the function's Etype.
1160
1161 if Is_Access_Type (Typ)
1162 and then Null_Exclusion_In_Return_Present (T_Def)
1163 then
1164 Set_Etype (Desig_Type,
1165 Create_Null_Excluding_Itype
1166 (T => Typ,
1167 Related_Nod => T_Def,
1168 Scope_Id => Current_Scope));
1169
1170 else
1171 if From_Limited_With (Typ) then
1172
1173 -- AI05-151: Incomplete types are allowed in all basic
1174 -- declarations, including access to subprograms.
1175
1176 if Ada_Version >= Ada_2012 then
1177 null;
1178
1179 else
1180 Error_Msg_NE
1181 ("illegal use of incomplete type&",
1182 Result_Definition (T_Def), Typ);
1183 end if;
1184
1185 elsif Ekind (Current_Scope) = E_Package
1186 and then In_Private_Part (Current_Scope)
1187 then
1188 if Ekind (Typ) = E_Incomplete_Type then
1189 Append_Elmt (Desig_Type, Private_Dependents (Typ));
1190
1191 elsif Is_Class_Wide_Type (Typ)
1192 and then Ekind (Etype (Typ)) = E_Incomplete_Type
1193 then
1194 Append_Elmt
1195 (Desig_Type, Private_Dependents (Etype (Typ)));
1196 end if;
1197 end if;
1198
1199 Set_Etype (Desig_Type, Typ);
1200 end if;
1201 end;
1202 end if;
1203
1204 if not (Is_Type (Etype (Desig_Type))) then
1205 Error_Msg_N
1206 ("expect type in function specification",
1207 Result_Definition (T_Def));
1208 end if;
1209
1210 else
1211 Set_Etype (Desig_Type, Standard_Void_Type);
1212 end if;
1213
1214 if Present (Formals) then
1215 Push_Scope (Desig_Type);
1216
1217 -- Some special tests here. These special tests can be removed
1218 -- if and when Itypes always have proper parent pointers to their
1219 -- declarations???
1220
1221 -- Special test 1) Link defining_identifier of formals. Required by
1222 -- First_Formal to provide its functionality.
1223
1224 declare
1225 F : Node_Id;
1226
1227 begin
1228 F := First (Formals);
1229
1230 -- In ASIS mode, the access_to_subprogram may be analyzed twice,
1231 -- when it is part of an unconstrained type and subtype expansion
1232 -- is disabled. To avoid back-end problems with shared profiles,
1233 -- use previous subprogram type as the designated type, and then
1234 -- remove scope added above.
1235
1236 if ASIS_Mode and then Present (Scope (Defining_Identifier (F)))
1237 then
1238 Set_Etype (T_Name, T_Name);
1239 Init_Size_Align (T_Name);
1240 Set_Directly_Designated_Type (T_Name,
1241 Scope (Defining_Identifier (F)));
1242 End_Scope;
1243 return;
1244 end if;
1245
1246 while Present (F) loop
1247 if No (Parent (Defining_Identifier (F))) then
1248 Set_Parent (Defining_Identifier (F), F);
1249 end if;
1250
1251 Next (F);
1252 end loop;
1253 end;
1254
1255 Process_Formals (Formals, Parent (T_Def));
1256
1257 -- Special test 2) End_Scope requires that the parent pointer be set
1258 -- to something reasonable, but Itypes don't have parent pointers. So
1259 -- we set it and then unset it ???
1260
1261 Set_Parent (Desig_Type, T_Name);
1262 End_Scope;
1263 Set_Parent (Desig_Type, Empty);
1264 end if;
1265
1266 -- Check for premature usage of the type being defined
1267
1268 Check_For_Premature_Usage (T_Def);
1269
1270 -- The return type and/or any parameter type may be incomplete. Mark the
1271 -- subprogram_type as depending on the incomplete type, so that it can
1272 -- be updated when the full type declaration is seen. This only applies
1273 -- to incomplete types declared in some enclosing scope, not to limited
1274 -- views from other packages.
1275
1276 -- Prior to Ada 2012, access to functions can only have in_parameters.
1277
1278 if Present (Formals) then
1279 Formal := First_Formal (Desig_Type);
1280 while Present (Formal) loop
1281 if Ekind (Formal) /= E_In_Parameter
1282 and then Nkind (T_Def) = N_Access_Function_Definition
1283 and then Ada_Version < Ada_2012
1284 then
1285 Error_Msg_N ("functions can only have IN parameters", Formal);
1286 end if;
1287
1288 if Ekind (Etype (Formal)) = E_Incomplete_Type
1289 and then In_Open_Scopes (Scope (Etype (Formal)))
1290 then
1291 Append_Elmt (Desig_Type, Private_Dependents (Etype (Formal)));
1292 Set_Has_Delayed_Freeze (Desig_Type);
1293 end if;
1294
1295 Next_Formal (Formal);
1296 end loop;
1297 end if;
1298
1299 -- Check whether an indirect call without actuals may be possible. This
1300 -- is used when resolving calls whose result is then indexed.
1301
1302 May_Need_Actuals (Desig_Type);
1303
1304 -- If the return type is incomplete, this is legal as long as the type
1305 -- is declared in the current scope and will be completed in it (rather
1306 -- than being part of limited view).
1307
1308 if Ekind (Etype (Desig_Type)) = E_Incomplete_Type
1309 and then not Has_Delayed_Freeze (Desig_Type)
1310 and then In_Open_Scopes (Scope (Etype (Desig_Type)))
1311 then
1312 Append_Elmt (Desig_Type, Private_Dependents (Etype (Desig_Type)));
1313 Set_Has_Delayed_Freeze (Desig_Type);
1314 end if;
1315
1316 Check_Delayed_Subprogram (Desig_Type);
1317
1318 if Protected_Present (T_Def) then
1319 Set_Ekind (T_Name, E_Access_Protected_Subprogram_Type);
1320 Set_Convention (Desig_Type, Convention_Protected);
1321 else
1322 Set_Ekind (T_Name, E_Access_Subprogram_Type);
1323 end if;
1324
1325 Set_Can_Use_Internal_Rep (T_Name, not Always_Compatible_Rep_On_Target);
1326
1327 Set_Etype (T_Name, T_Name);
1328 Init_Size_Align (T_Name);
1329 Set_Directly_Designated_Type (T_Name, Desig_Type);
1330
1331 Generate_Reference_To_Formals (T_Name);
1332
1333 -- Ada 2005 (AI-231): Propagate the null-excluding attribute
1334
1335 Set_Can_Never_Be_Null (T_Name, Null_Exclusion_Present (T_Def));
1336
1337 Check_Restriction (No_Access_Subprograms, T_Def);
1338 end Access_Subprogram_Declaration;
1339
1340 ----------------------------
1341 -- Access_Type_Declaration --
1342 ----------------------------
1343
1344 procedure Access_Type_Declaration (T : Entity_Id; Def : Node_Id) is
1345 P : constant Node_Id := Parent (Def);
1346 S : constant Node_Id := Subtype_Indication (Def);
1347
1348 Full_Desig : Entity_Id;
1349
1350 begin
1351 Check_SPARK_05_Restriction ("access type is not allowed", Def);
1352
1353 -- Check for permissible use of incomplete type
1354
1355 if Nkind (S) /= N_Subtype_Indication then
1356 Analyze (S);
1357
1358 if Ekind (Root_Type (Entity (S))) = E_Incomplete_Type then
1359 Set_Directly_Designated_Type (T, Entity (S));
1360
1361 -- If the designated type is a limited view, we cannot tell if
1362 -- the full view contains tasks, and there is no way to handle
1363 -- that full view in a client. We create a master entity for the
1364 -- scope, which will be used when a client determines that one
1365 -- is needed.
1366
1367 if From_Limited_With (Entity (S))
1368 and then not Is_Class_Wide_Type (Entity (S))
1369 then
1370 Set_Ekind (T, E_Access_Type);
1371 Build_Master_Entity (T);
1372 Build_Master_Renaming (T);
1373 end if;
1374
1375 else
1376 Set_Directly_Designated_Type (T, Process_Subtype (S, P, T, 'P'));
1377 end if;
1378
1379 -- If the access definition is of the form: ACCESS NOT NULL ..
1380 -- the subtype indication must be of an access type. Create
1381 -- a null-excluding subtype of it.
1382
1383 if Null_Excluding_Subtype (Def) then
1384 if not Is_Access_Type (Entity (S)) then
1385 Error_Msg_N ("null exclusion must apply to access type", Def);
1386
1387 else
1388 declare
1389 Loc : constant Source_Ptr := Sloc (S);
1390 Decl : Node_Id;
1391 Nam : constant Entity_Id := Make_Temporary (Loc, 'S');
1392
1393 begin
1394 Decl :=
1395 Make_Subtype_Declaration (Loc,
1396 Defining_Identifier => Nam,
1397 Subtype_Indication =>
1398 New_Occurrence_Of (Entity (S), Loc));
1399 Set_Null_Exclusion_Present (Decl);
1400 Insert_Before (Parent (Def), Decl);
1401 Analyze (Decl);
1402 Set_Entity (S, Nam);
1403 end;
1404 end if;
1405 end if;
1406
1407 else
1408 Set_Directly_Designated_Type (T,
1409 Process_Subtype (S, P, T, 'P'));
1410 end if;
1411
1412 if All_Present (Def) or Constant_Present (Def) then
1413 Set_Ekind (T, E_General_Access_Type);
1414 else
1415 Set_Ekind (T, E_Access_Type);
1416 end if;
1417
1418 Full_Desig := Designated_Type (T);
1419
1420 if Base_Type (Full_Desig) = T then
1421 Error_Msg_N ("access type cannot designate itself", S);
1422
1423 -- In Ada 2005, the type may have a limited view through some unit in
1424 -- its own context, allowing the following circularity that cannot be
1425 -- detected earlier.
1426
1427 elsif Is_Class_Wide_Type (Full_Desig) and then Etype (Full_Desig) = T
1428 then
1429 Error_Msg_N
1430 ("access type cannot designate its own classwide type", S);
1431
1432 -- Clean up indication of tagged status to prevent cascaded errors
1433
1434 Set_Is_Tagged_Type (T, False);
1435 end if;
1436
1437 Set_Etype (T, T);
1438
1439 -- If the type has appeared already in a with_type clause, it is frozen
1440 -- and the pointer size is already set. Else, initialize.
1441
1442 if not From_Limited_With (T) then
1443 Init_Size_Align (T);
1444 end if;
1445
1446 -- Note that Has_Task is always false, since the access type itself
1447 -- is not a task type. See Einfo for more description on this point.
1448 -- Exactly the same consideration applies to Has_Controlled_Component
1449 -- and to Has_Protected.
1450
1451 Set_Has_Task (T, False);
1452 Set_Has_Controlled_Component (T, False);
1453 Set_Has_Protected (T, False);
1454
1455 -- Initialize field Finalization_Master explicitly to Empty, to avoid
1456 -- problems where an incomplete view of this entity has been previously
1457 -- established by a limited with and an overlaid version of this field
1458 -- (Stored_Constraint) was initialized for the incomplete view.
1459
1460 -- This reset is performed in most cases except where the access type
1461 -- has been created for the purposes of allocating or deallocating a
1462 -- build-in-place object. Such access types have explicitly set pools
1463 -- and finalization masters.
1464
1465 if No (Associated_Storage_Pool (T)) then
1466 Set_Finalization_Master (T, Empty);
1467 end if;
1468
1469 -- Ada 2005 (AI-231): Propagate the null-excluding and access-constant
1470 -- attributes
1471
1472 Set_Can_Never_Be_Null (T, Null_Exclusion_Present (Def));
1473 Set_Is_Access_Constant (T, Constant_Present (Def));
1474 end Access_Type_Declaration;
1475
1476 ----------------------------------
1477 -- Add_Interface_Tag_Components --
1478 ----------------------------------
1479
1480 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id) is
1481 Loc : constant Source_Ptr := Sloc (N);
1482 L : List_Id;
1483 Last_Tag : Node_Id;
1484
1485 procedure Add_Tag (Iface : Entity_Id);
1486 -- Add tag for one of the progenitor interfaces
1487
1488 -------------
1489 -- Add_Tag --
1490 -------------
1491
1492 procedure Add_Tag (Iface : Entity_Id) is
1493 Decl : Node_Id;
1494 Def : Node_Id;
1495 Tag : Entity_Id;
1496 Offset : Entity_Id;
1497
1498 begin
1499 pragma Assert (Is_Tagged_Type (Iface) and then Is_Interface (Iface));
1500
1501 -- This is a reasonable place to propagate predicates
1502
1503 if Has_Predicates (Iface) then
1504 Set_Has_Predicates (Typ);
1505 end if;
1506
1507 Def :=
1508 Make_Component_Definition (Loc,
1509 Aliased_Present => True,
1510 Subtype_Indication =>
1511 New_Occurrence_Of (RTE (RE_Interface_Tag), Loc));
1512
1513 Tag := Make_Temporary (Loc, 'V');
1514
1515 Decl :=
1516 Make_Component_Declaration (Loc,
1517 Defining_Identifier => Tag,
1518 Component_Definition => Def);
1519
1520 Analyze_Component_Declaration (Decl);
1521
1522 Set_Analyzed (Decl);
1523 Set_Ekind (Tag, E_Component);
1524 Set_Is_Tag (Tag);
1525 Set_Is_Aliased (Tag);
1526 Set_Related_Type (Tag, Iface);
1527 Init_Component_Location (Tag);
1528
1529 pragma Assert (Is_Frozen (Iface));
1530
1531 Set_DT_Entry_Count (Tag,
1532 DT_Entry_Count (First_Entity (Iface)));
1533
1534 if No (Last_Tag) then
1535 Prepend (Decl, L);
1536 else
1537 Insert_After (Last_Tag, Decl);
1538 end if;
1539
1540 Last_Tag := Decl;
1541
1542 -- If the ancestor has discriminants we need to give special support
1543 -- to store the offset_to_top value of the secondary dispatch tables.
1544 -- For this purpose we add a supplementary component just after the
1545 -- field that contains the tag associated with each secondary DT.
1546
1547 if Typ /= Etype (Typ) and then Has_Discriminants (Etype (Typ)) then
1548 Def :=
1549 Make_Component_Definition (Loc,
1550 Subtype_Indication =>
1551 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
1552
1553 Offset := Make_Temporary (Loc, 'V');
1554
1555 Decl :=
1556 Make_Component_Declaration (Loc,
1557 Defining_Identifier => Offset,
1558 Component_Definition => Def);
1559
1560 Analyze_Component_Declaration (Decl);
1561
1562 Set_Analyzed (Decl);
1563 Set_Ekind (Offset, E_Component);
1564 Set_Is_Aliased (Offset);
1565 Set_Related_Type (Offset, Iface);
1566 Init_Component_Location (Offset);
1567 Insert_After (Last_Tag, Decl);
1568 Last_Tag := Decl;
1569 end if;
1570 end Add_Tag;
1571
1572 -- Local variables
1573
1574 Elmt : Elmt_Id;
1575 Ext : Node_Id;
1576 Comp : Node_Id;
1577
1578 -- Start of processing for Add_Interface_Tag_Components
1579
1580 begin
1581 if not RTE_Available (RE_Interface_Tag) then
1582 Error_Msg
1583 ("(Ada 2005) interface types not supported by this run-time!",
1584 Sloc (N));
1585 return;
1586 end if;
1587
1588 if Ekind (Typ) /= E_Record_Type
1589 or else (Is_Concurrent_Record_Type (Typ)
1590 and then Is_Empty_List (Abstract_Interface_List (Typ)))
1591 or else (not Is_Concurrent_Record_Type (Typ)
1592 and then No (Interfaces (Typ))
1593 and then Is_Empty_Elmt_List (Interfaces (Typ)))
1594 then
1595 return;
1596 end if;
1597
1598 -- Find the current last tag
1599
1600 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1601 Ext := Record_Extension_Part (Type_Definition (N));
1602 else
1603 pragma Assert (Nkind (Type_Definition (N)) = N_Record_Definition);
1604 Ext := Type_Definition (N);
1605 end if;
1606
1607 Last_Tag := Empty;
1608
1609 if not (Present (Component_List (Ext))) then
1610 Set_Null_Present (Ext, False);
1611 L := New_List;
1612 Set_Component_List (Ext,
1613 Make_Component_List (Loc,
1614 Component_Items => L,
1615 Null_Present => False));
1616 else
1617 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1618 L := Component_Items
1619 (Component_List
1620 (Record_Extension_Part
1621 (Type_Definition (N))));
1622 else
1623 L := Component_Items
1624 (Component_List
1625 (Type_Definition (N)));
1626 end if;
1627
1628 -- Find the last tag component
1629
1630 Comp := First (L);
1631 while Present (Comp) loop
1632 if Nkind (Comp) = N_Component_Declaration
1633 and then Is_Tag (Defining_Identifier (Comp))
1634 then
1635 Last_Tag := Comp;
1636 end if;
1637
1638 Next (Comp);
1639 end loop;
1640 end if;
1641
1642 -- At this point L references the list of components and Last_Tag
1643 -- references the current last tag (if any). Now we add the tag
1644 -- corresponding with all the interfaces that are not implemented
1645 -- by the parent.
1646
1647 if Present (Interfaces (Typ)) then
1648 Elmt := First_Elmt (Interfaces (Typ));
1649 while Present (Elmt) loop
1650 Add_Tag (Node (Elmt));
1651 Next_Elmt (Elmt);
1652 end loop;
1653 end if;
1654 end Add_Interface_Tag_Components;
1655
1656 -------------------------------------
1657 -- Add_Internal_Interface_Entities --
1658 -------------------------------------
1659
1660 procedure Add_Internal_Interface_Entities (Tagged_Type : Entity_Id) is
1661 Elmt : Elmt_Id;
1662 Iface : Entity_Id;
1663 Iface_Elmt : Elmt_Id;
1664 Iface_Prim : Entity_Id;
1665 Ifaces_List : Elist_Id;
1666 New_Subp : Entity_Id := Empty;
1667 Prim : Entity_Id;
1668 Restore_Scope : Boolean := False;
1669
1670 begin
1671 pragma Assert (Ada_Version >= Ada_2005
1672 and then Is_Record_Type (Tagged_Type)
1673 and then Is_Tagged_Type (Tagged_Type)
1674 and then Has_Interfaces (Tagged_Type)
1675 and then not Is_Interface (Tagged_Type));
1676
1677 -- Ensure that the internal entities are added to the scope of the type
1678
1679 if Scope (Tagged_Type) /= Current_Scope then
1680 Push_Scope (Scope (Tagged_Type));
1681 Restore_Scope := True;
1682 end if;
1683
1684 Collect_Interfaces (Tagged_Type, Ifaces_List);
1685
1686 Iface_Elmt := First_Elmt (Ifaces_List);
1687 while Present (Iface_Elmt) loop
1688 Iface := Node (Iface_Elmt);
1689
1690 -- Originally we excluded here from this processing interfaces that
1691 -- are parents of Tagged_Type because their primitives are located
1692 -- in the primary dispatch table (and hence no auxiliary internal
1693 -- entities are required to handle secondary dispatch tables in such
1694 -- case). However, these auxiliary entities are also required to
1695 -- handle derivations of interfaces in formals of generics (see
1696 -- Derive_Subprograms).
1697
1698 Elmt := First_Elmt (Primitive_Operations (Iface));
1699 while Present (Elmt) loop
1700 Iface_Prim := Node (Elmt);
1701
1702 if not Is_Predefined_Dispatching_Operation (Iface_Prim) then
1703 Prim :=
1704 Find_Primitive_Covering_Interface
1705 (Tagged_Type => Tagged_Type,
1706 Iface_Prim => Iface_Prim);
1707
1708 if No (Prim) and then Serious_Errors_Detected > 0 then
1709 goto Continue;
1710 end if;
1711
1712 pragma Assert (Present (Prim));
1713
1714 -- Ada 2012 (AI05-0197): If the name of the covering primitive
1715 -- differs from the name of the interface primitive then it is
1716 -- a private primitive inherited from a parent type. In such
1717 -- case, given that Tagged_Type covers the interface, the
1718 -- inherited private primitive becomes visible. For such
1719 -- purpose we add a new entity that renames the inherited
1720 -- private primitive.
1721
1722 if Chars (Prim) /= Chars (Iface_Prim) then
1723 pragma Assert (Has_Suffix (Prim, 'P'));
1724 Derive_Subprogram
1725 (New_Subp => New_Subp,
1726 Parent_Subp => Iface_Prim,
1727 Derived_Type => Tagged_Type,
1728 Parent_Type => Iface);
1729 Set_Alias (New_Subp, Prim);
1730 Set_Is_Abstract_Subprogram
1731 (New_Subp, Is_Abstract_Subprogram (Prim));
1732 end if;
1733
1734 Derive_Subprogram
1735 (New_Subp => New_Subp,
1736 Parent_Subp => Iface_Prim,
1737 Derived_Type => Tagged_Type,
1738 Parent_Type => Iface);
1739
1740 -- Ada 2005 (AI-251): Decorate internal entity Iface_Subp
1741 -- associated with interface types. These entities are
1742 -- only registered in the list of primitives of its
1743 -- corresponding tagged type because they are only used
1744 -- to fill the contents of the secondary dispatch tables.
1745 -- Therefore they are removed from the homonym chains.
1746
1747 Set_Is_Hidden (New_Subp);
1748 Set_Is_Internal (New_Subp);
1749 Set_Alias (New_Subp, Prim);
1750 Set_Is_Abstract_Subprogram
1751 (New_Subp, Is_Abstract_Subprogram (Prim));
1752 Set_Interface_Alias (New_Subp, Iface_Prim);
1753
1754 -- If the returned type is an interface then propagate it to
1755 -- the returned type. Needed by the thunk to generate the code
1756 -- which displaces "this" to reference the corresponding
1757 -- secondary dispatch table in the returned object.
1758
1759 if Is_Interface (Etype (Iface_Prim)) then
1760 Set_Etype (New_Subp, Etype (Iface_Prim));
1761 end if;
1762
1763 -- Internal entities associated with interface types are only
1764 -- registered in the list of primitives of the tagged type.
1765 -- They are only used to fill the contents of the secondary
1766 -- dispatch tables. Therefore they are not needed in the
1767 -- homonym chains.
1768
1769 Remove_Homonym (New_Subp);
1770
1771 -- Hidden entities associated with interfaces must have set
1772 -- the Has_Delay_Freeze attribute to ensure that, in case
1773 -- of locally defined tagged types (or compiling with static
1774 -- dispatch tables generation disabled) the corresponding
1775 -- entry of the secondary dispatch table is filled when such
1776 -- an entity is frozen. This is an expansion activity that must
1777 -- be suppressed for ASIS because it leads to gigi elaboration
1778 -- issues in annotate mode.
1779
1780 if not ASIS_Mode then
1781 Set_Has_Delayed_Freeze (New_Subp);
1782 end if;
1783 end if;
1784
1785 <<Continue>>
1786 Next_Elmt (Elmt);
1787 end loop;
1788
1789 Next_Elmt (Iface_Elmt);
1790 end loop;
1791
1792 if Restore_Scope then
1793 Pop_Scope;
1794 end if;
1795 end Add_Internal_Interface_Entities;
1796
1797 -----------------------------------
1798 -- Analyze_Component_Declaration --
1799 -----------------------------------
1800
1801 procedure Analyze_Component_Declaration (N : Node_Id) is
1802 Loc : constant Source_Ptr := Sloc (Component_Definition (N));
1803 Id : constant Entity_Id := Defining_Identifier (N);
1804 E : constant Node_Id := Expression (N);
1805 Typ : constant Node_Id :=
1806 Subtype_Indication (Component_Definition (N));
1807 T : Entity_Id;
1808 P : Entity_Id;
1809
1810 function Contains_POC (Constr : Node_Id) return Boolean;
1811 -- Determines whether a constraint uses the discriminant of a record
1812 -- type thus becoming a per-object constraint (POC).
1813
1814 function Is_Known_Limited (Typ : Entity_Id) return Boolean;
1815 -- Typ is the type of the current component, check whether this type is
1816 -- a limited type. Used to validate declaration against that of
1817 -- enclosing record.
1818
1819 ------------------
1820 -- Contains_POC --
1821 ------------------
1822
1823 function Contains_POC (Constr : Node_Id) return Boolean is
1824 begin
1825 -- Prevent cascaded errors
1826
1827 if Error_Posted (Constr) then
1828 return False;
1829 end if;
1830
1831 case Nkind (Constr) is
1832 when N_Attribute_Reference =>
1833 return Attribute_Name (Constr) = Name_Access
1834 and then Prefix (Constr) = Scope (Entity (Prefix (Constr)));
1835
1836 when N_Discriminant_Association =>
1837 return Denotes_Discriminant (Expression (Constr));
1838
1839 when N_Identifier =>
1840 return Denotes_Discriminant (Constr);
1841
1842 when N_Index_Or_Discriminant_Constraint =>
1843 declare
1844 IDC : Node_Id;
1845
1846 begin
1847 IDC := First (Constraints (Constr));
1848 while Present (IDC) loop
1849
1850 -- One per-object constraint is sufficient
1851
1852 if Contains_POC (IDC) then
1853 return True;
1854 end if;
1855
1856 Next (IDC);
1857 end loop;
1858
1859 return False;
1860 end;
1861
1862 when N_Range =>
1863 return Denotes_Discriminant (Low_Bound (Constr))
1864 or else
1865 Denotes_Discriminant (High_Bound (Constr));
1866
1867 when N_Range_Constraint =>
1868 return Denotes_Discriminant (Range_Expression (Constr));
1869
1870 when others =>
1871 return False;
1872
1873 end case;
1874 end Contains_POC;
1875
1876 ----------------------
1877 -- Is_Known_Limited --
1878 ----------------------
1879
1880 function Is_Known_Limited (Typ : Entity_Id) return Boolean is
1881 P : constant Entity_Id := Etype (Typ);
1882 R : constant Entity_Id := Root_Type (Typ);
1883
1884 begin
1885 if Is_Limited_Record (Typ) then
1886 return True;
1887
1888 -- If the root type is limited (and not a limited interface)
1889 -- so is the current type
1890
1891 elsif Is_Limited_Record (R)
1892 and then (not Is_Interface (R) or else not Is_Limited_Interface (R))
1893 then
1894 return True;
1895
1896 -- Else the type may have a limited interface progenitor, but a
1897 -- limited record parent.
1898
1899 elsif R /= P and then Is_Limited_Record (P) then
1900 return True;
1901
1902 else
1903 return False;
1904 end if;
1905 end Is_Known_Limited;
1906
1907 -- Start of processing for Analyze_Component_Declaration
1908
1909 begin
1910 Generate_Definition (Id);
1911 Enter_Name (Id);
1912
1913 if Present (Typ) then
1914 T := Find_Type_Of_Object
1915 (Subtype_Indication (Component_Definition (N)), N);
1916
1917 if not Nkind_In (Typ, N_Identifier, N_Expanded_Name) then
1918 Check_SPARK_05_Restriction ("subtype mark required", Typ);
1919 end if;
1920
1921 -- Ada 2005 (AI-230): Access Definition case
1922
1923 else
1924 pragma Assert (Present
1925 (Access_Definition (Component_Definition (N))));
1926
1927 T := Access_Definition
1928 (Related_Nod => N,
1929 N => Access_Definition (Component_Definition (N)));
1930 Set_Is_Local_Anonymous_Access (T);
1931
1932 -- Ada 2005 (AI-254)
1933
1934 if Present (Access_To_Subprogram_Definition
1935 (Access_Definition (Component_Definition (N))))
1936 and then Protected_Present (Access_To_Subprogram_Definition
1937 (Access_Definition
1938 (Component_Definition (N))))
1939 then
1940 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1941 end if;
1942 end if;
1943
1944 -- If the subtype is a constrained subtype of the enclosing record,
1945 -- (which must have a partial view) the back-end does not properly
1946 -- handle the recursion. Rewrite the component declaration with an
1947 -- explicit subtype indication, which is acceptable to Gigi. We can copy
1948 -- the tree directly because side effects have already been removed from
1949 -- discriminant constraints.
1950
1951 if Ekind (T) = E_Access_Subtype
1952 and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
1953 and then Comes_From_Source (T)
1954 and then Nkind (Parent (T)) = N_Subtype_Declaration
1955 and then Etype (Directly_Designated_Type (T)) = Current_Scope
1956 then
1957 Rewrite
1958 (Subtype_Indication (Component_Definition (N)),
1959 New_Copy_Tree (Subtype_Indication (Parent (T))));
1960 T := Find_Type_Of_Object
1961 (Subtype_Indication (Component_Definition (N)), N);
1962 end if;
1963
1964 -- If the component declaration includes a default expression, then we
1965 -- check that the component is not of a limited type (RM 3.7(5)),
1966 -- and do the special preanalysis of the expression (see section on
1967 -- "Handling of Default and Per-Object Expressions" in the spec of
1968 -- package Sem).
1969
1970 if Present (E) then
1971 Check_SPARK_05_Restriction ("default expression is not allowed", E);
1972 Preanalyze_Default_Expression (E, T);
1973 Check_Initialization (T, E);
1974
1975 if Ada_Version >= Ada_2005
1976 and then Ekind (T) = E_Anonymous_Access_Type
1977 and then Etype (E) /= Any_Type
1978 then
1979 -- Check RM 3.9.2(9): "if the expected type for an expression is
1980 -- an anonymous access-to-specific tagged type, then the object
1981 -- designated by the expression shall not be dynamically tagged
1982 -- unless it is a controlling operand in a call on a dispatching
1983 -- operation"
1984
1985 if Is_Tagged_Type (Directly_Designated_Type (T))
1986 and then
1987 Ekind (Directly_Designated_Type (T)) /= E_Class_Wide_Type
1988 and then
1989 Ekind (Directly_Designated_Type (Etype (E))) =
1990 E_Class_Wide_Type
1991 then
1992 Error_Msg_N
1993 ("access to specific tagged type required (RM 3.9.2(9))", E);
1994 end if;
1995
1996 -- (Ada 2005: AI-230): Accessibility check for anonymous
1997 -- components
1998
1999 if Type_Access_Level (Etype (E)) >
2000 Deepest_Type_Access_Level (T)
2001 then
2002 Error_Msg_N
2003 ("expression has deeper access level than component " &
2004 "(RM 3.10.2 (12.2))", E);
2005 end if;
2006
2007 -- The initialization expression is a reference to an access
2008 -- discriminant. The type of the discriminant is always deeper
2009 -- than any access type.
2010
2011 if Ekind (Etype (E)) = E_Anonymous_Access_Type
2012 and then Is_Entity_Name (E)
2013 and then Ekind (Entity (E)) = E_In_Parameter
2014 and then Present (Discriminal_Link (Entity (E)))
2015 then
2016 Error_Msg_N
2017 ("discriminant has deeper accessibility level than target",
2018 E);
2019 end if;
2020 end if;
2021 end if;
2022
2023 -- The parent type may be a private view with unknown discriminants,
2024 -- and thus unconstrained. Regular components must be constrained.
2025
2026 if Is_Indefinite_Subtype (T) and then Chars (Id) /= Name_uParent then
2027 if Is_Class_Wide_Type (T) then
2028 Error_Msg_N
2029 ("class-wide subtype with unknown discriminants" &
2030 " in component declaration",
2031 Subtype_Indication (Component_Definition (N)));
2032 else
2033 Error_Msg_N
2034 ("unconstrained subtype in component declaration",
2035 Subtype_Indication (Component_Definition (N)));
2036 end if;
2037
2038 -- Components cannot be abstract, except for the special case of
2039 -- the _Parent field (case of extending an abstract tagged type)
2040
2041 elsif Is_Abstract_Type (T) and then Chars (Id) /= Name_uParent then
2042 Error_Msg_N ("type of a component cannot be abstract", N);
2043 end if;
2044
2045 Set_Etype (Id, T);
2046 Set_Is_Aliased (Id, Aliased_Present (Component_Definition (N)));
2047
2048 -- The component declaration may have a per-object constraint, set
2049 -- the appropriate flag in the defining identifier of the subtype.
2050
2051 if Present (Subtype_Indication (Component_Definition (N))) then
2052 declare
2053 Sindic : constant Node_Id :=
2054 Subtype_Indication (Component_Definition (N));
2055 begin
2056 if Nkind (Sindic) = N_Subtype_Indication
2057 and then Present (Constraint (Sindic))
2058 and then Contains_POC (Constraint (Sindic))
2059 then
2060 Set_Has_Per_Object_Constraint (Id);
2061 end if;
2062 end;
2063 end if;
2064
2065 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
2066 -- out some static checks.
2067
2068 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (T) then
2069 Null_Exclusion_Static_Checks (N);
2070 end if;
2071
2072 -- If this component is private (or depends on a private type), flag the
2073 -- record type to indicate that some operations are not available.
2074
2075 P := Private_Component (T);
2076
2077 if Present (P) then
2078
2079 -- Check for circular definitions
2080
2081 if P = Any_Type then
2082 Set_Etype (Id, Any_Type);
2083
2084 -- There is a gap in the visibility of operations only if the
2085 -- component type is not defined in the scope of the record type.
2086
2087 elsif Scope (P) = Scope (Current_Scope) then
2088 null;
2089
2090 elsif Is_Limited_Type (P) then
2091 Set_Is_Limited_Composite (Current_Scope);
2092
2093 else
2094 Set_Is_Private_Composite (Current_Scope);
2095 end if;
2096 end if;
2097
2098 if P /= Any_Type
2099 and then Is_Limited_Type (T)
2100 and then Chars (Id) /= Name_uParent
2101 and then Is_Tagged_Type (Current_Scope)
2102 then
2103 if Is_Derived_Type (Current_Scope)
2104 and then not Is_Known_Limited (Current_Scope)
2105 then
2106 Error_Msg_N
2107 ("extension of nonlimited type cannot have limited components",
2108 N);
2109
2110 if Is_Interface (Root_Type (Current_Scope)) then
2111 Error_Msg_N
2112 ("\limitedness is not inherited from limited interface", N);
2113 Error_Msg_N ("\add LIMITED to type indication", N);
2114 end if;
2115
2116 Explain_Limited_Type (T, N);
2117 Set_Etype (Id, Any_Type);
2118 Set_Is_Limited_Composite (Current_Scope, False);
2119
2120 elsif not Is_Derived_Type (Current_Scope)
2121 and then not Is_Limited_Record (Current_Scope)
2122 and then not Is_Concurrent_Type (Current_Scope)
2123 then
2124 Error_Msg_N
2125 ("nonlimited tagged type cannot have limited components", N);
2126 Explain_Limited_Type (T, N);
2127 Set_Etype (Id, Any_Type);
2128 Set_Is_Limited_Composite (Current_Scope, False);
2129 end if;
2130 end if;
2131
2132 -- If the component is an unconstrained task or protected type with
2133 -- discriminants, the component and the enclosing record are limited
2134 -- and the component is constrained by its default values. Compute
2135 -- its actual subtype, else it may be allocated the maximum size by
2136 -- the backend, and possibly overflow.
2137
2138 if Is_Concurrent_Type (T)
2139 and then not Is_Constrained (T)
2140 and then Has_Discriminants (T)
2141 and then not Has_Discriminants (Current_Scope)
2142 then
2143 declare
2144 Act_T : constant Entity_Id := Build_Default_Subtype (T, N);
2145
2146 begin
2147 Set_Etype (Id, Act_T);
2148
2149 -- Rewrite component definition to use the constrained subtype
2150
2151 Rewrite (Component_Definition (N),
2152 Make_Component_Definition (Loc,
2153 Subtype_Indication => New_Occurrence_Of (Act_T, Loc)));
2154 end;
2155 end if;
2156
2157 Set_Original_Record_Component (Id, Id);
2158
2159 if Has_Aspects (N) then
2160 Analyze_Aspect_Specifications (N, Id);
2161 end if;
2162
2163 Analyze_Dimension (N);
2164 end Analyze_Component_Declaration;
2165
2166 --------------------------
2167 -- Analyze_Declarations --
2168 --------------------------
2169
2170 procedure Analyze_Declarations (L : List_Id) is
2171 Decl : Node_Id;
2172
2173 procedure Adjust_Decl;
2174 -- Adjust Decl not to include implicit label declarations, since these
2175 -- have strange Sloc values that result in elaboration check problems.
2176 -- (They have the sloc of the label as found in the source, and that
2177 -- is ahead of the current declarative part).
2178
2179 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id);
2180 -- Determine whether Body_Decl denotes the body of a late controlled
2181 -- primitive (either Initialize, Adjust or Finalize). If this is the
2182 -- case, add a proper spec if the body lacks one. The spec is inserted
2183 -- before Body_Decl and immedately analyzed.
2184
2185 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id);
2186 -- Spec_Id is the entity of a package that may define abstract states.
2187 -- If the states have visible refinement, remove the visibility of each
2188 -- constituent at the end of the package body declarations.
2189
2190 -----------------
2191 -- Adjust_Decl --
2192 -----------------
2193
2194 procedure Adjust_Decl is
2195 begin
2196 while Present (Prev (Decl))
2197 and then Nkind (Decl) = N_Implicit_Label_Declaration
2198 loop
2199 Prev (Decl);
2200 end loop;
2201 end Adjust_Decl;
2202
2203 --------------------------------------
2204 -- Handle_Late_Controlled_Primitive --
2205 --------------------------------------
2206
2207 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id) is
2208 Body_Spec : constant Node_Id := Specification (Body_Decl);
2209 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2210 Loc : constant Source_Ptr := Sloc (Body_Id);
2211 Params : constant List_Id :=
2212 Parameter_Specifications (Body_Spec);
2213 Spec : Node_Id;
2214 Spec_Id : Entity_Id;
2215 Typ : Node_Id;
2216
2217 begin
2218 -- Consider only procedure bodies whose name matches one of the three
2219 -- controlled primitives.
2220
2221 if Nkind (Body_Spec) /= N_Procedure_Specification
2222 or else not Nam_In (Chars (Body_Id), Name_Adjust,
2223 Name_Finalize,
2224 Name_Initialize)
2225 then
2226 return;
2227
2228 -- A controlled primitive must have exactly one formal which is not
2229 -- an anonymous access type.
2230
2231 elsif List_Length (Params) /= 1 then
2232 return;
2233 end if;
2234
2235 Typ := Parameter_Type (First (Params));
2236
2237 if Nkind (Typ) = N_Access_Definition then
2238 return;
2239 end if;
2240
2241 Find_Type (Typ);
2242
2243 -- The type of the formal must be derived from [Limited_]Controlled
2244
2245 if not Is_Controlled (Entity (Typ)) then
2246 return;
2247 end if;
2248
2249 -- Check whether a specification exists for this body. We do not
2250 -- analyze the spec of the body in full, because it will be analyzed
2251 -- again when the body is properly analyzed, and we cannot create
2252 -- duplicate entries in the formals chain. We look for an explicit
2253 -- specification because the body may be an overriding operation and
2254 -- an inherited spec may be present.
2255
2256 Spec_Id := Current_Entity (Body_Id);
2257
2258 while Present (Spec_Id) loop
2259 if Ekind_In (Spec_Id, E_Procedure, E_Generic_Procedure)
2260 and then Scope (Spec_Id) = Current_Scope
2261 and then Present (First_Formal (Spec_Id))
2262 and then No (Next_Formal (First_Formal (Spec_Id)))
2263 and then Etype (First_Formal (Spec_Id)) = Entity (Typ)
2264 and then Comes_From_Source (Spec_Id)
2265 then
2266 return;
2267 end if;
2268
2269 Spec_Id := Homonym (Spec_Id);
2270 end loop;
2271
2272 -- At this point the body is known to be a late controlled primitive.
2273 -- Generate a matching spec and insert it before the body. Note the
2274 -- use of Copy_Separate_Tree - we want an entirely separate semantic
2275 -- tree in this case.
2276
2277 Spec := Copy_Separate_Tree (Body_Spec);
2278
2279 -- Ensure that the subprogram declaration does not inherit the null
2280 -- indicator from the body as we now have a proper spec/body pair.
2281
2282 Set_Null_Present (Spec, False);
2283
2284 Insert_Before_And_Analyze (Body_Decl,
2285 Make_Subprogram_Declaration (Loc, Specification => Spec));
2286 end Handle_Late_Controlled_Primitive;
2287
2288 --------------------------------
2289 -- Remove_Visible_Refinements --
2290 --------------------------------
2291
2292 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id) is
2293 State_Elmt : Elmt_Id;
2294 begin
2295 if Present (Abstract_States (Spec_Id)) then
2296 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2297 while Present (State_Elmt) loop
2298 Set_Has_Visible_Refinement (Node (State_Elmt), False);
2299 Next_Elmt (State_Elmt);
2300 end loop;
2301 end if;
2302 end Remove_Visible_Refinements;
2303
2304 -- Local variables
2305
2306 Context : Node_Id := Empty;
2307 Freeze_From : Entity_Id := Empty;
2308 Next_Decl : Node_Id;
2309 Pack_Decl : Node_Id := Empty;
2310
2311 Body_Seen : Boolean := False;
2312 -- Flag set when the first body [stub] is encountered
2313
2314 -- Start of processing for Analyze_Declarations
2315
2316 begin
2317 if Restriction_Check_Required (SPARK_05) then
2318 Check_Later_Vs_Basic_Declarations (L, During_Parsing => False);
2319 end if;
2320
2321 Decl := First (L);
2322 while Present (Decl) loop
2323
2324 -- Package spec cannot contain a package declaration in SPARK
2325
2326 if Nkind (Decl) = N_Package_Declaration
2327 and then Nkind (Parent (L)) = N_Package_Specification
2328 then
2329 Check_SPARK_05_Restriction
2330 ("package specification cannot contain a package declaration",
2331 Decl);
2332 end if;
2333
2334 -- Complete analysis of declaration
2335
2336 Analyze (Decl);
2337 Next_Decl := Next (Decl);
2338
2339 if No (Freeze_From) then
2340 Freeze_From := First_Entity (Current_Scope);
2341 end if;
2342
2343 -- At the end of a declarative part, freeze remaining entities
2344 -- declared in it. The end of the visible declarations of package
2345 -- specification is not the end of a declarative part if private
2346 -- declarations are present. The end of a package declaration is a
2347 -- freezing point only if it a library package. A task definition or
2348 -- protected type definition is not a freeze point either. Finally,
2349 -- we do not freeze entities in generic scopes, because there is no
2350 -- code generated for them and freeze nodes will be generated for
2351 -- the instance.
2352
2353 -- The end of a package instantiation is not a freeze point, but
2354 -- for now we make it one, because the generic body is inserted
2355 -- (currently) immediately after. Generic instantiations will not
2356 -- be a freeze point once delayed freezing of bodies is implemented.
2357 -- (This is needed in any case for early instantiations ???).
2358
2359 if No (Next_Decl) then
2360 if Nkind_In (Parent (L), N_Component_List,
2361 N_Task_Definition,
2362 N_Protected_Definition)
2363 then
2364 null;
2365
2366 elsif Nkind (Parent (L)) /= N_Package_Specification then
2367 if Nkind (Parent (L)) = N_Package_Body then
2368 Freeze_From := First_Entity (Current_Scope);
2369 end if;
2370
2371 -- There may have been several freezing points previously,
2372 -- for example object declarations or subprogram bodies, but
2373 -- at the end of a declarative part we check freezing from
2374 -- the beginning, even though entities may already be frozen,
2375 -- in order to perform visibility checks on delayed aspects.
2376
2377 Adjust_Decl;
2378 Freeze_All (First_Entity (Current_Scope), Decl);
2379 Freeze_From := Last_Entity (Current_Scope);
2380
2381 elsif Scope (Current_Scope) /= Standard_Standard
2382 and then not Is_Child_Unit (Current_Scope)
2383 and then No (Generic_Parent (Parent (L)))
2384 then
2385 null;
2386
2387 elsif L /= Visible_Declarations (Parent (L))
2388 or else No (Private_Declarations (Parent (L)))
2389 or else Is_Empty_List (Private_Declarations (Parent (L)))
2390 then
2391 Adjust_Decl;
2392 Freeze_All (First_Entity (Current_Scope), Decl);
2393 Freeze_From := Last_Entity (Current_Scope);
2394 end if;
2395
2396 -- If next node is a body then freeze all types before the body.
2397 -- An exception occurs for some expander-generated bodies. If these
2398 -- are generated at places where in general language rules would not
2399 -- allow a freeze point, then we assume that the expander has
2400 -- explicitly checked that all required types are properly frozen,
2401 -- and we do not cause general freezing here. This special circuit
2402 -- is used when the encountered body is marked as having already
2403 -- been analyzed.
2404
2405 -- In all other cases (bodies that come from source, and expander
2406 -- generated bodies that have not been analyzed yet), freeze all
2407 -- types now. Note that in the latter case, the expander must take
2408 -- care to attach the bodies at a proper place in the tree so as to
2409 -- not cause unwanted freezing at that point.
2410
2411 elsif not Analyzed (Next_Decl) and then Is_Body (Next_Decl) then
2412
2413 -- When a controlled type is frozen, the expander generates stream
2414 -- and controlled type support routines. If the freeze is caused
2415 -- by the stand alone body of Initialize, Adjust and Finalize, the
2416 -- expander will end up using the wrong version of these routines
2417 -- as the body has not been processed yet. To remedy this, detect
2418 -- a late controlled primitive and create a proper spec for it.
2419 -- This ensures that the primitive will override its inherited
2420 -- counterpart before the freeze takes place.
2421
2422 -- If the declaration we just processed is a body, do not attempt
2423 -- to examine Next_Decl as the late primitive idiom can only apply
2424 -- to the first encountered body.
2425
2426 -- The spec of the late primitive is not generated in ASIS mode to
2427 -- ensure a consistent list of primitives that indicates the true
2428 -- semantic structure of the program (which is not relevant when
2429 -- generating executable code.
2430
2431 -- ??? a cleaner approach may be possible and/or this solution
2432 -- could be extended to general-purpose late primitives, TBD.
2433
2434 if not ASIS_Mode and then not Body_Seen and then not Is_Body (Decl)
2435 then
2436 Body_Seen := True;
2437
2438 if Nkind (Next_Decl) = N_Subprogram_Body then
2439 Handle_Late_Controlled_Primitive (Next_Decl);
2440 end if;
2441 end if;
2442
2443 Adjust_Decl;
2444 Freeze_All (Freeze_From, Decl);
2445 Freeze_From := Last_Entity (Current_Scope);
2446 end if;
2447
2448 Decl := Next_Decl;
2449 end loop;
2450
2451 -- Analyze the contracts of packages and their bodies
2452
2453 if Present (L) then
2454 Context := Parent (L);
2455
2456 if Nkind (Context) = N_Package_Specification then
2457 Pack_Decl := Parent (Context);
2458
2459 -- When a package has private declarations, its contract must be
2460 -- analyzed at the end of the said declarations. This way both the
2461 -- analysis and freeze actions are properly synchronized in case
2462 -- of private type use within the contract.
2463
2464 if L = Private_Declarations (Context) then
2465 Analyze_Package_Contract (Defining_Entity (Context));
2466
2467 -- Build the bodies of the default initial condition procedures
2468 -- for all types subject to pragma Default_Initial_Condition.
2469 -- From a purely Ada stand point, this is a freezing activity,
2470 -- however freezing is not available under GNATprove_Mode. To
2471 -- accomodate both scenarios, the bodies are build at the end
2472 -- of private declaration analysis.
2473
2474 Build_Default_Init_Cond_Procedure_Bodies (L);
2475
2476 -- Otherwise the contract is analyzed at the end of the visible
2477 -- declarations.
2478
2479 elsif L = Visible_Declarations (Context)
2480 and then No (Private_Declarations (Context))
2481 then
2482 Analyze_Package_Contract (Defining_Entity (Context));
2483 end if;
2484
2485 elsif Nkind (Context) = N_Package_Body then
2486 Pack_Decl := Context;
2487 Analyze_Package_Body_Contract (Defining_Entity (Context));
2488 end if;
2489
2490 -- Analyze the contracts of all subprogram declarations, subprogram
2491 -- bodies and variables now due to the delayed visibility needs of
2492 -- of their aspects and pragmas. Capture global references in generic
2493 -- subprograms or bodies.
2494
2495 Decl := First (L);
2496 while Present (Decl) loop
2497 if Nkind (Decl) = N_Object_Declaration then
2498 Analyze_Object_Contract (Defining_Entity (Decl));
2499
2500 elsif Nkind_In (Decl, N_Abstract_Subprogram_Declaration,
2501 N_Generic_Subprogram_Declaration,
2502 N_Subprogram_Declaration)
2503 then
2504 Analyze_Subprogram_Contract (Defining_Entity (Decl));
2505
2506 elsif Nkind (Decl) = N_Subprogram_Body then
2507 Analyze_Subprogram_Body_Contract (Defining_Entity (Decl));
2508
2509 elsif Nkind (Decl) = N_Subprogram_Body_Stub then
2510 Analyze_Subprogram_Body_Stub_Contract (Defining_Entity (Decl));
2511 end if;
2512
2513 -- Capture all global references in a generic subprogram or a body
2514 -- [stub] now that the contract has been analyzed.
2515
2516 if Nkind_In (Decl, N_Generic_Subprogram_Declaration,
2517 N_Subprogram_Body,
2518 N_Subprogram_Body_Stub)
2519 and then Is_Generic_Declaration_Or_Body (Decl)
2520 then
2521 Save_Global_References_In_Contract
2522 (Templ => Original_Node (Decl),
2523 Gen_Id => Corresponding_Spec_Of (Decl));
2524 end if;
2525
2526 Next (Decl);
2527 end loop;
2528
2529 -- The owner of the declarations is a package [body]
2530
2531 if Present (Pack_Decl) then
2532
2533 -- Capture all global references in a generic package or a body
2534 -- after all nested generic subprograms and bodies were subjected
2535 -- to the same processing.
2536
2537 if Is_Generic_Declaration_Or_Body (Pack_Decl) then
2538 Save_Global_References_In_Contract
2539 (Templ => Original_Node (Pack_Decl),
2540 Gen_Id => Corresponding_Spec_Of (Pack_Decl));
2541 end if;
2542
2543 -- State refinements are visible upto the end the of the package
2544 -- body declarations. Hide the state refinements from visibility
2545 -- to restore the original state conditions.
2546
2547 if Nkind (Pack_Decl) = N_Package_Body then
2548 Remove_Visible_Refinements (Corresponding_Spec (Pack_Decl));
2549 end if;
2550 end if;
2551 end if;
2552 end Analyze_Declarations;
2553
2554 -----------------------------------
2555 -- Analyze_Full_Type_Declaration --
2556 -----------------------------------
2557
2558 procedure Analyze_Full_Type_Declaration (N : Node_Id) is
2559 Def : constant Node_Id := Type_Definition (N);
2560 Def_Id : constant Entity_Id := Defining_Identifier (N);
2561 T : Entity_Id;
2562 Prev : Entity_Id;
2563
2564 Is_Remote : constant Boolean :=
2565 (Is_Remote_Types (Current_Scope)
2566 or else Is_Remote_Call_Interface (Current_Scope))
2567 and then not (In_Private_Part (Current_Scope)
2568 or else In_Package_Body (Current_Scope));
2569
2570 procedure Check_Ops_From_Incomplete_Type;
2571 -- If there is a tagged incomplete partial view of the type, traverse
2572 -- the primitives of the incomplete view and change the type of any
2573 -- controlling formals and result to indicate the full view. The
2574 -- primitives will be added to the full type's primitive operations
2575 -- list later in Sem_Disp.Check_Operation_From_Incomplete_Type (which
2576 -- is called from Process_Incomplete_Dependents).
2577
2578 ------------------------------------
2579 -- Check_Ops_From_Incomplete_Type --
2580 ------------------------------------
2581
2582 procedure Check_Ops_From_Incomplete_Type is
2583 Elmt : Elmt_Id;
2584 Formal : Entity_Id;
2585 Op : Entity_Id;
2586
2587 begin
2588 if Prev /= T
2589 and then Ekind (Prev) = E_Incomplete_Type
2590 and then Is_Tagged_Type (Prev)
2591 and then Is_Tagged_Type (T)
2592 then
2593 Elmt := First_Elmt (Primitive_Operations (Prev));
2594 while Present (Elmt) loop
2595 Op := Node (Elmt);
2596
2597 Formal := First_Formal (Op);
2598 while Present (Formal) loop
2599 if Etype (Formal) = Prev then
2600 Set_Etype (Formal, T);
2601 end if;
2602
2603 Next_Formal (Formal);
2604 end loop;
2605
2606 if Etype (Op) = Prev then
2607 Set_Etype (Op, T);
2608 end if;
2609
2610 Next_Elmt (Elmt);
2611 end loop;
2612 end if;
2613 end Check_Ops_From_Incomplete_Type;
2614
2615 -- Start of processing for Analyze_Full_Type_Declaration
2616
2617 begin
2618 Prev := Find_Type_Name (N);
2619
2620 -- The type declaration may be subject to pragma Ghost with policy
2621 -- Ignore. Set the mode now to ensure that any nodes generated during
2622 -- analysis and expansion are properly flagged as ignored Ghost.
2623
2624 Set_Ghost_Mode (N, Prev);
2625
2626 -- The full view, if present, now points to the current type. If there
2627 -- is an incomplete partial view, set a link to it, to simplify the
2628 -- retrieval of primitive operations of the type.
2629
2630 -- Ada 2005 (AI-50217): If the type was previously decorated when
2631 -- imported through a LIMITED WITH clause, it appears as incomplete
2632 -- but has no full view.
2633
2634 if Ekind (Prev) = E_Incomplete_Type
2635 and then Present (Full_View (Prev))
2636 then
2637 T := Full_View (Prev);
2638 Set_Incomplete_View (N, Parent (Prev));
2639 else
2640 T := Prev;
2641 end if;
2642
2643 Set_Is_Pure (T, Is_Pure (Current_Scope));
2644
2645 -- We set the flag Is_First_Subtype here. It is needed to set the
2646 -- corresponding flag for the Implicit class-wide-type created
2647 -- during tagged types processing.
2648
2649 Set_Is_First_Subtype (T, True);
2650
2651 -- Only composite types other than array types are allowed to have
2652 -- discriminants.
2653
2654 case Nkind (Def) is
2655
2656 -- For derived types, the rule will be checked once we've figured
2657 -- out the parent type.
2658
2659 when N_Derived_Type_Definition =>
2660 null;
2661
2662 -- For record types, discriminants are allowed, unless we are in
2663 -- SPARK.
2664
2665 when N_Record_Definition =>
2666 if Present (Discriminant_Specifications (N)) then
2667 Check_SPARK_05_Restriction
2668 ("discriminant type is not allowed",
2669 Defining_Identifier
2670 (First (Discriminant_Specifications (N))));
2671 end if;
2672
2673 when others =>
2674 if Present (Discriminant_Specifications (N)) then
2675 Error_Msg_N
2676 ("elementary or array type cannot have discriminants",
2677 Defining_Identifier
2678 (First (Discriminant_Specifications (N))));
2679 end if;
2680 end case;
2681
2682 -- Elaborate the type definition according to kind, and generate
2683 -- subsidiary (implicit) subtypes where needed. We skip this if it was
2684 -- already done (this happens during the reanalysis that follows a call
2685 -- to the high level optimizer).
2686
2687 if not Analyzed (T) then
2688 Set_Analyzed (T);
2689
2690 case Nkind (Def) is
2691 when N_Access_To_Subprogram_Definition =>
2692 Access_Subprogram_Declaration (T, Def);
2693
2694 -- If this is a remote access to subprogram, we must create the
2695 -- equivalent fat pointer type, and related subprograms.
2696
2697 if Is_Remote then
2698 Process_Remote_AST_Declaration (N);
2699 end if;
2700
2701 -- Validate categorization rule against access type declaration
2702 -- usually a violation in Pure unit, Shared_Passive unit.
2703
2704 Validate_Access_Type_Declaration (T, N);
2705
2706 when N_Access_To_Object_Definition =>
2707 Access_Type_Declaration (T, Def);
2708
2709 -- Validate categorization rule against access type declaration
2710 -- usually a violation in Pure unit, Shared_Passive unit.
2711
2712 Validate_Access_Type_Declaration (T, N);
2713
2714 -- If we are in a Remote_Call_Interface package and define a
2715 -- RACW, then calling stubs and specific stream attributes
2716 -- must be added.
2717
2718 if Is_Remote
2719 and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
2720 then
2721 Add_RACW_Features (Def_Id);
2722 end if;
2723
2724 when N_Array_Type_Definition =>
2725 Array_Type_Declaration (T, Def);
2726
2727 when N_Derived_Type_Definition =>
2728 Derived_Type_Declaration (T, N, T /= Def_Id);
2729
2730 when N_Enumeration_Type_Definition =>
2731 Enumeration_Type_Declaration (T, Def);
2732
2733 when N_Floating_Point_Definition =>
2734 Floating_Point_Type_Declaration (T, Def);
2735
2736 when N_Decimal_Fixed_Point_Definition =>
2737 Decimal_Fixed_Point_Type_Declaration (T, Def);
2738
2739 when N_Ordinary_Fixed_Point_Definition =>
2740 Ordinary_Fixed_Point_Type_Declaration (T, Def);
2741
2742 when N_Signed_Integer_Type_Definition =>
2743 Signed_Integer_Type_Declaration (T, Def);
2744
2745 when N_Modular_Type_Definition =>
2746 Modular_Type_Declaration (T, Def);
2747
2748 when N_Record_Definition =>
2749 Record_Type_Declaration (T, N, Prev);
2750
2751 -- If declaration has a parse error, nothing to elaborate.
2752
2753 when N_Error =>
2754 null;
2755
2756 when others =>
2757 raise Program_Error;
2758
2759 end case;
2760 end if;
2761
2762 if Etype (T) = Any_Type then
2763 return;
2764 end if;
2765
2766 -- Controlled type is not allowed in SPARK
2767
2768 if Is_Visibly_Controlled (T) then
2769 Check_SPARK_05_Restriction ("controlled type is not allowed", N);
2770 end if;
2771
2772 -- A type declared within a Ghost region is automatically Ghost
2773 -- (SPARK RM 6.9(2)).
2774
2775 if Comes_From_Source (T) and then Ghost_Mode > None then
2776 Set_Is_Ghost_Entity (T);
2777 end if;
2778
2779 -- Some common processing for all types
2780
2781 Set_Depends_On_Private (T, Has_Private_Component (T));
2782 Check_Ops_From_Incomplete_Type;
2783
2784 -- Both the declared entity, and its anonymous base type if one was
2785 -- created, need freeze nodes allocated.
2786
2787 declare
2788 B : constant Entity_Id := Base_Type (T);
2789
2790 begin
2791 -- In the case where the base type differs from the first subtype, we
2792 -- pre-allocate a freeze node, and set the proper link to the first
2793 -- subtype. Freeze_Entity will use this preallocated freeze node when
2794 -- it freezes the entity.
2795
2796 -- This does not apply if the base type is a generic type, whose
2797 -- declaration is independent of the current derived definition.
2798
2799 if B /= T and then not Is_Generic_Type (B) then
2800 Ensure_Freeze_Node (B);
2801 Set_First_Subtype_Link (Freeze_Node (B), T);
2802 end if;
2803
2804 -- A type that is imported through a limited_with clause cannot
2805 -- generate any code, and thus need not be frozen. However, an access
2806 -- type with an imported designated type needs a finalization list,
2807 -- which may be referenced in some other package that has non-limited
2808 -- visibility on the designated type. Thus we must create the
2809 -- finalization list at the point the access type is frozen, to
2810 -- prevent unsatisfied references at link time.
2811
2812 if not From_Limited_With (T) or else Is_Access_Type (T) then
2813 Set_Has_Delayed_Freeze (T);
2814 end if;
2815 end;
2816
2817 -- Case where T is the full declaration of some private type which has
2818 -- been swapped in Defining_Identifier (N).
2819
2820 if T /= Def_Id and then Is_Private_Type (Def_Id) then
2821 Process_Full_View (N, T, Def_Id);
2822
2823 -- Record the reference. The form of this is a little strange, since
2824 -- the full declaration has been swapped in. So the first parameter
2825 -- here represents the entity to which a reference is made which is
2826 -- the "real" entity, i.e. the one swapped in, and the second
2827 -- parameter provides the reference location.
2828
2829 -- Also, we want to kill Has_Pragma_Unreferenced temporarily here
2830 -- since we don't want a complaint about the full type being an
2831 -- unwanted reference to the private type
2832
2833 declare
2834 B : constant Boolean := Has_Pragma_Unreferenced (T);
2835 begin
2836 Set_Has_Pragma_Unreferenced (T, False);
2837 Generate_Reference (T, T, 'c');
2838 Set_Has_Pragma_Unreferenced (T, B);
2839 end;
2840
2841 Set_Completion_Referenced (Def_Id);
2842
2843 -- For completion of incomplete type, process incomplete dependents
2844 -- and always mark the full type as referenced (it is the incomplete
2845 -- type that we get for any real reference).
2846
2847 elsif Ekind (Prev) = E_Incomplete_Type then
2848 Process_Incomplete_Dependents (N, T, Prev);
2849 Generate_Reference (Prev, Def_Id, 'c');
2850 Set_Completion_Referenced (Def_Id);
2851
2852 -- If not private type or incomplete type completion, this is a real
2853 -- definition of a new entity, so record it.
2854
2855 else
2856 Generate_Definition (Def_Id);
2857 end if;
2858
2859 -- Propagate any pending access types whose finalization masters need to
2860 -- be fully initialized from the partial to the full view. Guard against
2861 -- an illegal full view that remains unanalyzed.
2862
2863 if Is_Type (Def_Id) and then Is_Incomplete_Or_Private_Type (Prev) then
2864 Set_Pending_Access_Types (Def_Id, Pending_Access_Types (Prev));
2865 end if;
2866
2867 if Chars (Scope (Def_Id)) = Name_System
2868 and then Chars (Def_Id) = Name_Address
2869 and then Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (N)))
2870 then
2871 Set_Is_Descendent_Of_Address (Def_Id);
2872 Set_Is_Descendent_Of_Address (Base_Type (Def_Id));
2873 Set_Is_Descendent_Of_Address (Prev);
2874 end if;
2875
2876 Set_Optimize_Alignment_Flags (Def_Id);
2877 Check_Eliminated (Def_Id);
2878
2879 -- If the declaration is a completion and aspects are present, apply
2880 -- them to the entity for the type which is currently the partial
2881 -- view, but which is the one that will be frozen.
2882
2883 if Has_Aspects (N) then
2884
2885 -- In most cases the partial view is a private type, and both views
2886 -- appear in different declarative parts. In the unusual case where
2887 -- the partial view is incomplete, perform the analysis on the
2888 -- full view, to prevent freezing anomalies with the corresponding
2889 -- class-wide type, which otherwise might be frozen before the
2890 -- dispatch table is built.
2891
2892 if Prev /= Def_Id
2893 and then Ekind (Prev) /= E_Incomplete_Type
2894 then
2895 Analyze_Aspect_Specifications (N, Prev);
2896
2897 -- Normal case
2898
2899 else
2900 Analyze_Aspect_Specifications (N, Def_Id);
2901 end if;
2902 end if;
2903 end Analyze_Full_Type_Declaration;
2904
2905 ----------------------------------
2906 -- Analyze_Incomplete_Type_Decl --
2907 ----------------------------------
2908
2909 procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
2910 F : constant Boolean := Is_Pure (Current_Scope);
2911 T : Entity_Id;
2912
2913 begin
2914 Check_SPARK_05_Restriction ("incomplete type is not allowed", N);
2915
2916 Generate_Definition (Defining_Identifier (N));
2917
2918 -- Process an incomplete declaration. The identifier must not have been
2919 -- declared already in the scope. However, an incomplete declaration may
2920 -- appear in the private part of a package, for a private type that has
2921 -- already been declared.
2922
2923 -- In this case, the discriminants (if any) must match
2924
2925 T := Find_Type_Name (N);
2926
2927 Set_Ekind (T, E_Incomplete_Type);
2928 Init_Size_Align (T);
2929 Set_Is_First_Subtype (T, True);
2930 Set_Etype (T, T);
2931
2932 -- An incomplete type declared within a Ghost region is automatically
2933 -- Ghost (SPARK RM 6.9(2)).
2934
2935 if Ghost_Mode > None then
2936 Set_Is_Ghost_Entity (T);
2937 end if;
2938
2939 -- Ada 2005 (AI-326): Minimum decoration to give support to tagged
2940 -- incomplete types.
2941
2942 if Tagged_Present (N) then
2943 Set_Is_Tagged_Type (T, True);
2944 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
2945 Make_Class_Wide_Type (T);
2946 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2947 end if;
2948
2949 Push_Scope (T);
2950
2951 Set_Stored_Constraint (T, No_Elist);
2952
2953 if Present (Discriminant_Specifications (N)) then
2954 Process_Discriminants (N);
2955 end if;
2956
2957 End_Scope;
2958
2959 -- If the type has discriminants, non-trivial subtypes may be
2960 -- declared before the full view of the type. The full views of those
2961 -- subtypes will be built after the full view of the type.
2962
2963 Set_Private_Dependents (T, New_Elmt_List);
2964 Set_Is_Pure (T, F);
2965 end Analyze_Incomplete_Type_Decl;
2966
2967 -----------------------------------
2968 -- Analyze_Interface_Declaration --
2969 -----------------------------------
2970
2971 procedure Analyze_Interface_Declaration (T : Entity_Id; Def : Node_Id) is
2972 CW : constant Entity_Id := Class_Wide_Type (T);
2973
2974 begin
2975 Set_Is_Tagged_Type (T);
2976 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
2977
2978 Set_Is_Limited_Record (T, Limited_Present (Def)
2979 or else Task_Present (Def)
2980 or else Protected_Present (Def)
2981 or else Synchronized_Present (Def));
2982
2983 -- Type is abstract if full declaration carries keyword, or if previous
2984 -- partial view did.
2985
2986 Set_Is_Abstract_Type (T);
2987 Set_Is_Interface (T);
2988
2989 -- Type is a limited interface if it includes the keyword limited, task,
2990 -- protected, or synchronized.
2991
2992 Set_Is_Limited_Interface
2993 (T, Limited_Present (Def)
2994 or else Protected_Present (Def)
2995 or else Synchronized_Present (Def)
2996 or else Task_Present (Def));
2997
2998 Set_Interfaces (T, New_Elmt_List);
2999 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3000
3001 -- Complete the decoration of the class-wide entity if it was already
3002 -- built (i.e. during the creation of the limited view)
3003
3004 if Present (CW) then
3005 Set_Is_Interface (CW);
3006 Set_Is_Limited_Interface (CW, Is_Limited_Interface (T));
3007 end if;
3008
3009 -- Check runtime support for synchronized interfaces
3010
3011 if VM_Target = No_VM
3012 and then (Is_Task_Interface (T)
3013 or else Is_Protected_Interface (T)
3014 or else Is_Synchronized_Interface (T))
3015 and then not RTE_Available (RE_Select_Specific_Data)
3016 then
3017 Error_Msg_CRT ("synchronized interfaces", T);
3018 end if;
3019 end Analyze_Interface_Declaration;
3020
3021 -----------------------------
3022 -- Analyze_Itype_Reference --
3023 -----------------------------
3024
3025 -- Nothing to do. This node is placed in the tree only for the benefit of
3026 -- back end processing, and has no effect on the semantic processing.
3027
3028 procedure Analyze_Itype_Reference (N : Node_Id) is
3029 begin
3030 pragma Assert (Is_Itype (Itype (N)));
3031 null;
3032 end Analyze_Itype_Reference;
3033
3034 --------------------------------
3035 -- Analyze_Number_Declaration --
3036 --------------------------------
3037
3038 procedure Analyze_Number_Declaration (N : Node_Id) is
3039 Id : constant Entity_Id := Defining_Identifier (N);
3040 E : constant Node_Id := Expression (N);
3041 T : Entity_Id;
3042 Index : Interp_Index;
3043 It : Interp;
3044
3045 begin
3046 -- The number declaration may be subject to pragma Ghost with policy
3047 -- Ignore. Set the mode now to ensure that any nodes generated during
3048 -- analysis and expansion are properly flagged as ignored Ghost.
3049
3050 Set_Ghost_Mode (N);
3051
3052 Generate_Definition (Id);
3053 Enter_Name (Id);
3054
3055 -- A number declared within a Ghost region is automatically Ghost
3056 -- (SPARK RM 6.9(2)).
3057
3058 if Ghost_Mode > None then
3059 Set_Is_Ghost_Entity (Id);
3060 end if;
3061
3062 -- This is an optimization of a common case of an integer literal
3063
3064 if Nkind (E) = N_Integer_Literal then
3065 Set_Is_Static_Expression (E, True);
3066 Set_Etype (E, Universal_Integer);
3067
3068 Set_Etype (Id, Universal_Integer);
3069 Set_Ekind (Id, E_Named_Integer);
3070 Set_Is_Frozen (Id, True);
3071 return;
3072 end if;
3073
3074 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3075
3076 -- Process expression, replacing error by integer zero, to avoid
3077 -- cascaded errors or aborts further along in the processing
3078
3079 -- Replace Error by integer zero, which seems least likely to cause
3080 -- cascaded errors.
3081
3082 if E = Error then
3083 Rewrite (E, Make_Integer_Literal (Sloc (E), Uint_0));
3084 Set_Error_Posted (E);
3085 end if;
3086
3087 Analyze (E);
3088
3089 -- Verify that the expression is static and numeric. If
3090 -- the expression is overloaded, we apply the preference
3091 -- rule that favors root numeric types.
3092
3093 if not Is_Overloaded (E) then
3094 T := Etype (E);
3095 if Has_Dynamic_Predicate_Aspect (T) then
3096 Error_Msg_N
3097 ("subtype has dynamic predicate, "
3098 & "not allowed in number declaration", N);
3099 end if;
3100
3101 else
3102 T := Any_Type;
3103
3104 Get_First_Interp (E, Index, It);
3105 while Present (It.Typ) loop
3106 if (Is_Integer_Type (It.Typ) or else Is_Real_Type (It.Typ))
3107 and then (Scope (Base_Type (It.Typ))) = Standard_Standard
3108 then
3109 if T = Any_Type then
3110 T := It.Typ;
3111
3112 elsif It.Typ = Universal_Real
3113 or else
3114 It.Typ = Universal_Integer
3115 then
3116 -- Choose universal interpretation over any other
3117
3118 T := It.Typ;
3119 exit;
3120 end if;
3121 end if;
3122
3123 Get_Next_Interp (Index, It);
3124 end loop;
3125 end if;
3126
3127 if Is_Integer_Type (T) then
3128 Resolve (E, T);
3129 Set_Etype (Id, Universal_Integer);
3130 Set_Ekind (Id, E_Named_Integer);
3131
3132 elsif Is_Real_Type (T) then
3133
3134 -- Because the real value is converted to universal_real, this is a
3135 -- legal context for a universal fixed expression.
3136
3137 if T = Universal_Fixed then
3138 declare
3139 Loc : constant Source_Ptr := Sloc (N);
3140 Conv : constant Node_Id := Make_Type_Conversion (Loc,
3141 Subtype_Mark =>
3142 New_Occurrence_Of (Universal_Real, Loc),
3143 Expression => Relocate_Node (E));
3144
3145 begin
3146 Rewrite (E, Conv);
3147 Analyze (E);
3148 end;
3149
3150 elsif T = Any_Fixed then
3151 Error_Msg_N ("illegal context for mixed mode operation", E);
3152
3153 -- Expression is of the form : universal_fixed * integer. Try to
3154 -- resolve as universal_real.
3155
3156 T := Universal_Real;
3157 Set_Etype (E, T);
3158 end if;
3159
3160 Resolve (E, T);
3161 Set_Etype (Id, Universal_Real);
3162 Set_Ekind (Id, E_Named_Real);
3163
3164 else
3165 Wrong_Type (E, Any_Numeric);
3166 Resolve (E, T);
3167
3168 Set_Etype (Id, T);
3169 Set_Ekind (Id, E_Constant);
3170 Set_Never_Set_In_Source (Id, True);
3171 Set_Is_True_Constant (Id, True);
3172 return;
3173 end if;
3174
3175 if Nkind_In (E, N_Integer_Literal, N_Real_Literal) then
3176 Set_Etype (E, Etype (Id));
3177 end if;
3178
3179 if not Is_OK_Static_Expression (E) then
3180 Flag_Non_Static_Expr
3181 ("non-static expression used in number declaration!", E);
3182 Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
3183 Set_Etype (E, Any_Type);
3184 end if;
3185 end Analyze_Number_Declaration;
3186
3187 -----------------------------
3188 -- Analyze_Object_Contract --
3189 -----------------------------
3190
3191 procedure Analyze_Object_Contract (Obj_Id : Entity_Id) is
3192 Obj_Typ : constant Entity_Id := Etype (Obj_Id);
3193 AR_Val : Boolean := False;
3194 AW_Val : Boolean := False;
3195 ER_Val : Boolean := False;
3196 EW_Val : Boolean := False;
3197 Prag : Node_Id;
3198 Seen : Boolean := False;
3199
3200 begin
3201 -- The loop parameter in an element iterator over a formal container
3202 -- is declared with an object declaration but no contracts apply.
3203
3204 if Ekind (Obj_Id) = E_Loop_Parameter then
3205 return;
3206 end if;
3207
3208 if Ekind (Obj_Id) = E_Constant then
3209
3210 -- A constant cannot be effectively volatile. This check is only
3211 -- relevant with SPARK_Mode on as it is not a standard Ada legality
3212 -- rule. Do not flag internally-generated constants that map generic
3213 -- formals to actuals in instantiations (SPARK RM 7.1.3(6)).
3214
3215 if SPARK_Mode = On
3216 and then Is_Effectively_Volatile (Obj_Id)
3217 and then No (Corresponding_Generic_Association (Parent (Obj_Id)))
3218
3219 -- Don't give this for internally generated entities (such as the
3220 -- FIRST and LAST temporaries generated for bounds).
3221
3222 and then Comes_From_Source (Obj_Id)
3223 then
3224 Error_Msg_N ("constant cannot be volatile", Obj_Id);
3225 end if;
3226
3227 else pragma Assert (Ekind (Obj_Id) = E_Variable);
3228
3229 -- The following checks are only relevant when SPARK_Mode is on as
3230 -- they are not standard Ada legality rules. Internally generated
3231 -- temporaries are ignored.
3232
3233 if SPARK_Mode = On and then Comes_From_Source (Obj_Id) then
3234 if Is_Effectively_Volatile (Obj_Id) then
3235
3236 -- The declaration of an effectively volatile object must
3237 -- appear at the library level (SPARK RM 7.1.3(7), C.6(6)).
3238
3239 if not Is_Library_Level_Entity (Obj_Id) then
3240 Error_Msg_N
3241 ("volatile variable & must be declared at library level",
3242 Obj_Id);
3243
3244 -- An object of a discriminated type cannot be effectively
3245 -- volatile (SPARK RM C.6(4)).
3246
3247 elsif Has_Discriminants (Obj_Typ) then
3248 Error_Msg_N
3249 ("discriminated object & cannot be volatile", Obj_Id);
3250
3251 -- An object of a tagged type cannot be effectively volatile
3252 -- (SPARK RM C.6(5)).
3253
3254 elsif Is_Tagged_Type (Obj_Typ) then
3255 Error_Msg_N ("tagged object & cannot be volatile", Obj_Id);
3256 end if;
3257
3258 -- The object is not effectively volatile
3259
3260 else
3261 -- A non-effectively volatile object cannot have effectively
3262 -- volatile components (SPARK RM 7.1.3(7)).
3263
3264 if not Is_Effectively_Volatile (Obj_Id)
3265 and then Has_Volatile_Component (Obj_Typ)
3266 then
3267 Error_Msg_N
3268 ("non-volatile object & cannot have volatile components",
3269 Obj_Id);
3270 end if;
3271 end if;
3272 end if;
3273
3274 if Is_Ghost_Entity (Obj_Id) then
3275
3276 -- A Ghost object cannot be effectively volatile (SPARK RM 6.9(8))
3277
3278 if Is_Effectively_Volatile (Obj_Id) then
3279 Error_Msg_N ("ghost variable & cannot be volatile", Obj_Id);
3280
3281 -- A Ghost object cannot be imported or exported (SPARK RM 6.9(8))
3282
3283 elsif Is_Imported (Obj_Id) then
3284 Error_Msg_N ("ghost object & cannot be imported", Obj_Id);
3285
3286 elsif Is_Exported (Obj_Id) then
3287 Error_Msg_N ("ghost object & cannot be exported", Obj_Id);
3288 end if;
3289 end if;
3290
3291 -- Analyze all external properties
3292
3293 Prag := Get_Pragma (Obj_Id, Pragma_Async_Readers);
3294
3295 if Present (Prag) then
3296 Analyze_External_Property_In_Decl_Part (Prag, AR_Val);
3297 Seen := True;
3298 end if;
3299
3300 Prag := Get_Pragma (Obj_Id, Pragma_Async_Writers);
3301
3302 if Present (Prag) then
3303 Analyze_External_Property_In_Decl_Part (Prag, AW_Val);
3304 Seen := True;
3305 end if;
3306
3307 Prag := Get_Pragma (Obj_Id, Pragma_Effective_Reads);
3308
3309 if Present (Prag) then
3310 Analyze_External_Property_In_Decl_Part (Prag, ER_Val);
3311 Seen := True;
3312 end if;
3313
3314 Prag := Get_Pragma (Obj_Id, Pragma_Effective_Writes);
3315
3316 if Present (Prag) then
3317 Analyze_External_Property_In_Decl_Part (Prag, EW_Val);
3318 Seen := True;
3319 end if;
3320
3321 -- Verify the mutual interaction of the various external properties
3322
3323 if Seen then
3324 Check_External_Properties (Obj_Id, AR_Val, AW_Val, ER_Val, EW_Val);
3325 end if;
3326
3327 -- Check whether the lack of indicator Part_Of agrees with the
3328 -- placement of the variable with respect to the state space.
3329
3330 Prag := Get_Pragma (Obj_Id, Pragma_Part_Of);
3331
3332 if No (Prag) then
3333 Check_Missing_Part_Of (Obj_Id);
3334 end if;
3335 end if;
3336
3337 -- A ghost object cannot be imported or exported (SPARK RM 6.9(8))
3338
3339 if Is_Ghost_Entity (Obj_Id) then
3340 if Is_Exported (Obj_Id) then
3341 Error_Msg_N ("ghost object & cannot be exported", Obj_Id);
3342
3343 elsif Is_Imported (Obj_Id) then
3344 Error_Msg_N ("ghost object & cannot be imported", Obj_Id);
3345 end if;
3346 end if;
3347 end Analyze_Object_Contract;
3348
3349 --------------------------------
3350 -- Analyze_Object_Declaration --
3351 --------------------------------
3352
3353 procedure Analyze_Object_Declaration (N : Node_Id) is
3354 Loc : constant Source_Ptr := Sloc (N);
3355 Id : constant Entity_Id := Defining_Identifier (N);
3356 T : Entity_Id;
3357 Act_T : Entity_Id;
3358
3359 E : Node_Id := Expression (N);
3360 -- E is set to Expression (N) throughout this routine. When
3361 -- Expression (N) is modified, E is changed accordingly.
3362
3363 Prev_Entity : Entity_Id := Empty;
3364
3365 function Count_Tasks (T : Entity_Id) return Uint;
3366 -- This function is called when a non-generic library level object of a
3367 -- task type is declared. Its function is to count the static number of
3368 -- tasks declared within the type (it is only called if Has_Tasks is set
3369 -- for T). As a side effect, if an array of tasks with non-static bounds
3370 -- or a variant record type is encountered, Check_Restrictions is called
3371 -- indicating the count is unknown.
3372
3373 function Delayed_Aspect_Present return Boolean;
3374 -- If the declaration has an expression that is an aggregate, and it
3375 -- has aspects that require delayed analysis, the resolution of the
3376 -- aggregate must be deferred to the freeze point of the objet. This
3377 -- special processing was created for address clauses, but it must
3378 -- also apply to Alignment. This must be done before the aspect
3379 -- specifications are analyzed because we must handle the aggregate
3380 -- before the analysis of the object declaration is complete.
3381
3382 -- Any other relevant delayed aspects on object declarations ???
3383
3384 -----------------
3385 -- Count_Tasks --
3386 -----------------
3387
3388 function Count_Tasks (T : Entity_Id) return Uint is
3389 C : Entity_Id;
3390 X : Node_Id;
3391 V : Uint;
3392
3393 begin
3394 if Is_Task_Type (T) then
3395 return Uint_1;
3396
3397 elsif Is_Record_Type (T) then
3398 if Has_Discriminants (T) then
3399 Check_Restriction (Max_Tasks, N);
3400 return Uint_0;
3401
3402 else
3403 V := Uint_0;
3404 C := First_Component (T);
3405 while Present (C) loop
3406 V := V + Count_Tasks (Etype (C));
3407 Next_Component (C);
3408 end loop;
3409
3410 return V;
3411 end if;
3412
3413 elsif Is_Array_Type (T) then
3414 X := First_Index (T);
3415 V := Count_Tasks (Component_Type (T));
3416 while Present (X) loop
3417 C := Etype (X);
3418
3419 if not Is_OK_Static_Subtype (C) then
3420 Check_Restriction (Max_Tasks, N);
3421 return Uint_0;
3422 else
3423 V := V * (UI_Max (Uint_0,
3424 Expr_Value (Type_High_Bound (C)) -
3425 Expr_Value (Type_Low_Bound (C)) + Uint_1));
3426 end if;
3427
3428 Next_Index (X);
3429 end loop;
3430
3431 return V;
3432
3433 else
3434 return Uint_0;
3435 end if;
3436 end Count_Tasks;
3437
3438 ----------------------------
3439 -- Delayed_Aspect_Present --
3440 ----------------------------
3441
3442 function Delayed_Aspect_Present return Boolean is
3443 A : Node_Id;
3444 A_Id : Aspect_Id;
3445
3446 begin
3447 if Present (Aspect_Specifications (N)) then
3448 A := First (Aspect_Specifications (N));
3449 A_Id := Get_Aspect_Id (Chars (Identifier (A)));
3450 while Present (A) loop
3451 if A_Id = Aspect_Alignment or else A_Id = Aspect_Address then
3452 return True;
3453 end if;
3454
3455 Next (A);
3456 end loop;
3457 end if;
3458
3459 return False;
3460 end Delayed_Aspect_Present;
3461
3462 -- Start of processing for Analyze_Object_Declaration
3463
3464 begin
3465 -- There are three kinds of implicit types generated by an
3466 -- object declaration:
3467
3468 -- 1. Those generated by the original Object Definition
3469
3470 -- 2. Those generated by the Expression
3471
3472 -- 3. Those used to constrain the Object Definition with the
3473 -- expression constraints when the definition is unconstrained.
3474
3475 -- They must be generated in this order to avoid order of elaboration
3476 -- issues. Thus the first step (after entering the name) is to analyze
3477 -- the object definition.
3478
3479 if Constant_Present (N) then
3480 Prev_Entity := Current_Entity_In_Scope (Id);
3481
3482 if Present (Prev_Entity)
3483 and then
3484 -- If the homograph is an implicit subprogram, it is overridden
3485 -- by the current declaration.
3486
3487 ((Is_Overloadable (Prev_Entity)
3488 and then Is_Inherited_Operation (Prev_Entity))
3489
3490 -- The current object is a discriminal generated for an entry
3491 -- family index. Even though the index is a constant, in this
3492 -- particular context there is no true constant redeclaration.
3493 -- Enter_Name will handle the visibility.
3494
3495 or else
3496 (Is_Discriminal (Id)
3497 and then Ekind (Discriminal_Link (Id)) =
3498 E_Entry_Index_Parameter)
3499
3500 -- The current object is the renaming for a generic declared
3501 -- within the instance.
3502
3503 or else
3504 (Ekind (Prev_Entity) = E_Package
3505 and then Nkind (Parent (Prev_Entity)) =
3506 N_Package_Renaming_Declaration
3507 and then not Comes_From_Source (Prev_Entity)
3508 and then
3509 Is_Generic_Instance (Renamed_Entity (Prev_Entity))))
3510 then
3511 Prev_Entity := Empty;
3512 end if;
3513 end if;
3514
3515 -- The object declaration may be subject to pragma Ghost with policy
3516 -- Ignore. Set the mode now to ensure that any nodes generated during
3517 -- analysis and expansion are properly flagged as ignored Ghost.
3518
3519 Set_Ghost_Mode (N, Prev_Entity);
3520
3521 if Present (Prev_Entity) then
3522 Constant_Redeclaration (Id, N, T);
3523
3524 Generate_Reference (Prev_Entity, Id, 'c');
3525 Set_Completion_Referenced (Id);
3526
3527 if Error_Posted (N) then
3528
3529 -- Type mismatch or illegal redeclaration, Do not analyze
3530 -- expression to avoid cascaded errors.
3531
3532 T := Find_Type_Of_Object (Object_Definition (N), N);
3533 Set_Etype (Id, T);
3534 Set_Ekind (Id, E_Variable);
3535 goto Leave;
3536 end if;
3537
3538 -- In the normal case, enter identifier at the start to catch premature
3539 -- usage in the initialization expression.
3540
3541 else
3542 Generate_Definition (Id);
3543 Enter_Name (Id);
3544
3545 Mark_Coextensions (N, Object_Definition (N));
3546
3547 T := Find_Type_Of_Object (Object_Definition (N), N);
3548
3549 if Nkind (Object_Definition (N)) = N_Access_Definition
3550 and then Present
3551 (Access_To_Subprogram_Definition (Object_Definition (N)))
3552 and then Protected_Present
3553 (Access_To_Subprogram_Definition (Object_Definition (N)))
3554 then
3555 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
3556 end if;
3557
3558 if Error_Posted (Id) then
3559 Set_Etype (Id, T);
3560 Set_Ekind (Id, E_Variable);
3561 goto Leave;
3562 end if;
3563 end if;
3564
3565 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
3566 -- out some static checks
3567
3568 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (T) then
3569
3570 -- In case of aggregates we must also take care of the correct
3571 -- initialization of nested aggregates bug this is done at the
3572 -- point of the analysis of the aggregate (see sem_aggr.adb).
3573
3574 if Present (Expression (N))
3575 and then Nkind (Expression (N)) = N_Aggregate
3576 then
3577 null;
3578
3579 else
3580 declare
3581 Save_Typ : constant Entity_Id := Etype (Id);
3582 begin
3583 Set_Etype (Id, T); -- Temp. decoration for static checks
3584 Null_Exclusion_Static_Checks (N);
3585 Set_Etype (Id, Save_Typ);
3586 end;
3587 end if;
3588 end if;
3589
3590 -- Object is marked pure if it is in a pure scope
3591
3592 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3593
3594 -- If deferred constant, make sure context is appropriate. We detect
3595 -- a deferred constant as a constant declaration with no expression.
3596 -- A deferred constant can appear in a package body if its completion
3597 -- is by means of an interface pragma.
3598
3599 if Constant_Present (N) and then No (E) then
3600
3601 -- A deferred constant may appear in the declarative part of the
3602 -- following constructs:
3603
3604 -- blocks
3605 -- entry bodies
3606 -- extended return statements
3607 -- package specs
3608 -- package bodies
3609 -- subprogram bodies
3610 -- task bodies
3611
3612 -- When declared inside a package spec, a deferred constant must be
3613 -- completed by a full constant declaration or pragma Import. In all
3614 -- other cases, the only proper completion is pragma Import. Extended
3615 -- return statements are flagged as invalid contexts because they do
3616 -- not have a declarative part and so cannot accommodate the pragma.
3617
3618 if Ekind (Current_Scope) = E_Return_Statement then
3619 Error_Msg_N
3620 ("invalid context for deferred constant declaration (RM 7.4)",
3621 N);
3622 Error_Msg_N
3623 ("\declaration requires an initialization expression",
3624 N);
3625 Set_Constant_Present (N, False);
3626
3627 -- In Ada 83, deferred constant must be of private type
3628
3629 elsif not Is_Private_Type (T) then
3630 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
3631 Error_Msg_N
3632 ("(Ada 83) deferred constant must be private type", N);
3633 end if;
3634 end if;
3635
3636 -- If not a deferred constant, then the object declaration freezes
3637 -- its type, unless the object is of an anonymous type and has delayed
3638 -- aspects. In that case the type is frozen when the object itself is.
3639
3640 else
3641 Check_Fully_Declared (T, N);
3642
3643 if Has_Delayed_Aspects (Id)
3644 and then Is_Array_Type (T)
3645 and then Is_Itype (T)
3646 then
3647 Set_Has_Delayed_Freeze (T);
3648 else
3649 Freeze_Before (N, T);
3650 end if;
3651 end if;
3652
3653 -- If the object was created by a constrained array definition, then
3654 -- set the link in both the anonymous base type and anonymous subtype
3655 -- that are built to represent the array type to point to the object.
3656
3657 if Nkind (Object_Definition (Declaration_Node (Id))) =
3658 N_Constrained_Array_Definition
3659 then
3660 Set_Related_Array_Object (T, Id);
3661 Set_Related_Array_Object (Base_Type (T), Id);
3662 end if;
3663
3664 -- Special checks for protected objects not at library level
3665
3666 if Is_Protected_Type (T)
3667 and then not Is_Library_Level_Entity (Id)
3668 then
3669 Check_Restriction (No_Local_Protected_Objects, Id);
3670
3671 -- Protected objects with interrupt handlers must be at library level
3672
3673 -- Ada 2005: This test is not needed (and the corresponding clause
3674 -- in the RM is removed) because accessibility checks are sufficient
3675 -- to make handlers not at the library level illegal.
3676
3677 -- AI05-0303: The AI is in fact a binding interpretation, and thus
3678 -- applies to the '95 version of the language as well.
3679
3680 if Has_Interrupt_Handler (T) and then Ada_Version < Ada_95 then
3681 Error_Msg_N
3682 ("interrupt object can only be declared at library level", Id);
3683 end if;
3684 end if;
3685
3686 -- The actual subtype of the object is the nominal subtype, unless
3687 -- the nominal one is unconstrained and obtained from the expression.
3688
3689 Act_T := T;
3690
3691 -- These checks should be performed before the initialization expression
3692 -- is considered, so that the Object_Definition node is still the same
3693 -- as in source code.
3694
3695 -- In SPARK, the nominal subtype is always given by a subtype mark
3696 -- and must not be unconstrained. (The only exception to this is the
3697 -- acceptance of declarations of constants of type String.)
3698
3699 if not Nkind_In (Object_Definition (N), N_Expanded_Name, N_Identifier)
3700 then
3701 Check_SPARK_05_Restriction
3702 ("subtype mark required", Object_Definition (N));
3703
3704 elsif Is_Array_Type (T)
3705 and then not Is_Constrained (T)
3706 and then T /= Standard_String
3707 then
3708 Check_SPARK_05_Restriction
3709 ("subtype mark of constrained type expected",
3710 Object_Definition (N));
3711 end if;
3712
3713 -- There are no aliased objects in SPARK
3714
3715 if Aliased_Present (N) then
3716 Check_SPARK_05_Restriction ("aliased object is not allowed", N);
3717 end if;
3718
3719 -- Process initialization expression if present and not in error
3720
3721 if Present (E) and then E /= Error then
3722
3723 -- Generate an error in case of CPP class-wide object initialization.
3724 -- Required because otherwise the expansion of the class-wide
3725 -- assignment would try to use 'size to initialize the object
3726 -- (primitive that is not available in CPP tagged types).
3727
3728 if Is_Class_Wide_Type (Act_T)
3729 and then
3730 (Is_CPP_Class (Root_Type (Etype (Act_T)))
3731 or else
3732 (Present (Full_View (Root_Type (Etype (Act_T))))
3733 and then
3734 Is_CPP_Class (Full_View (Root_Type (Etype (Act_T))))))
3735 then
3736 Error_Msg_N
3737 ("predefined assignment not available for 'C'P'P tagged types",
3738 E);
3739 end if;
3740
3741 Mark_Coextensions (N, E);
3742 Analyze (E);
3743
3744 -- In case of errors detected in the analysis of the expression,
3745 -- decorate it with the expected type to avoid cascaded errors
3746
3747 if No (Etype (E)) then
3748 Set_Etype (E, T);
3749 end if;
3750
3751 -- If an initialization expression is present, then we set the
3752 -- Is_True_Constant flag. It will be reset if this is a variable
3753 -- and it is indeed modified.
3754
3755 Set_Is_True_Constant (Id, True);
3756
3757 -- If we are analyzing a constant declaration, set its completion
3758 -- flag after analyzing and resolving the expression.
3759
3760 if Constant_Present (N) then
3761 Set_Has_Completion (Id);
3762 end if;
3763
3764 -- Set type and resolve (type may be overridden later on). Note:
3765 -- Ekind (Id) must still be E_Void at this point so that incorrect
3766 -- early usage within E is properly diagnosed.
3767
3768 Set_Etype (Id, T);
3769
3770 -- If the expression is an aggregate we must look ahead to detect
3771 -- the possible presence of an address clause, and defer resolution
3772 -- and expansion of the aggregate to the freeze point of the entity.
3773
3774 if Comes_From_Source (N)
3775 and then Expander_Active
3776 and then Nkind (E) = N_Aggregate
3777 and then (Present (Following_Address_Clause (N))
3778 or else Delayed_Aspect_Present)
3779 then
3780 Set_Etype (E, T);
3781
3782 else
3783 Resolve (E, T);
3784 end if;
3785
3786 -- No further action needed if E is a call to an inlined function
3787 -- which returns an unconstrained type and it has been expanded into
3788 -- a procedure call. In that case N has been replaced by an object
3789 -- declaration without initializing expression and it has been
3790 -- analyzed (see Expand_Inlined_Call).
3791
3792 if Back_End_Inlining
3793 and then Expander_Active
3794 and then Nkind (E) = N_Function_Call
3795 and then Nkind (Name (E)) in N_Has_Entity
3796 and then Is_Inlined (Entity (Name (E)))
3797 and then not Is_Constrained (Etype (E))
3798 and then Analyzed (N)
3799 and then No (Expression (N))
3800 then
3801 return;
3802 end if;
3803
3804 -- If E is null and has been replaced by an N_Raise_Constraint_Error
3805 -- node (which was marked already-analyzed), we need to set the type
3806 -- to something other than Any_Access in order to keep gigi happy.
3807
3808 if Etype (E) = Any_Access then
3809 Set_Etype (E, T);
3810 end if;
3811
3812 -- If the object is an access to variable, the initialization
3813 -- expression cannot be an access to constant.
3814
3815 if Is_Access_Type (T)
3816 and then not Is_Access_Constant (T)
3817 and then Is_Access_Type (Etype (E))
3818 and then Is_Access_Constant (Etype (E))
3819 then
3820 Error_Msg_N
3821 ("access to variable cannot be initialized with an "
3822 & "access-to-constant expression", E);
3823 end if;
3824
3825 if not Assignment_OK (N) then
3826 Check_Initialization (T, E);
3827 end if;
3828
3829 Check_Unset_Reference (E);
3830
3831 -- If this is a variable, then set current value. If this is a
3832 -- declared constant of a scalar type with a static expression,
3833 -- indicate that it is always valid.
3834
3835 if not Constant_Present (N) then
3836 if Compile_Time_Known_Value (E) then
3837 Set_Current_Value (Id, E);
3838 end if;
3839
3840 elsif Is_Scalar_Type (T) and then Is_OK_Static_Expression (E) then
3841 Set_Is_Known_Valid (Id);
3842 end if;
3843
3844 -- Deal with setting of null flags
3845
3846 if Is_Access_Type (T) then
3847 if Known_Non_Null (E) then
3848 Set_Is_Known_Non_Null (Id, True);
3849 elsif Known_Null (E) and then not Can_Never_Be_Null (Id) then
3850 Set_Is_Known_Null (Id, True);
3851 end if;
3852 end if;
3853
3854 -- Check incorrect use of dynamically tagged expressions
3855
3856 if Is_Tagged_Type (T) then
3857 Check_Dynamically_Tagged_Expression
3858 (Expr => E,
3859 Typ => T,
3860 Related_Nod => N);
3861 end if;
3862
3863 Apply_Scalar_Range_Check (E, T);
3864 Apply_Static_Length_Check (E, T);
3865
3866 if Nkind (Original_Node (N)) = N_Object_Declaration
3867 and then Comes_From_Source (Original_Node (N))
3868
3869 -- Only call test if needed
3870
3871 and then Restriction_Check_Required (SPARK_05)
3872 and then not Is_SPARK_05_Initialization_Expr (Original_Node (E))
3873 then
3874 Check_SPARK_05_Restriction
3875 ("initialization expression is not appropriate", E);
3876 end if;
3877
3878 -- A formal parameter of a specific tagged type whose related
3879 -- subprogram is subject to pragma Extensions_Visible with value
3880 -- "False" cannot be implicitly converted to a class-wide type by
3881 -- means of an initialization expression (SPARK RM 6.1.7(3)).
3882
3883 if Is_Class_Wide_Type (T) and then Is_EVF_Expression (E) then
3884 Error_Msg_N
3885 ("formal parameter with Extensions_Visible False cannot be "
3886 & "implicitly converted to class-wide type", E);
3887 end if;
3888 end if;
3889
3890 -- If the No_Streams restriction is set, check that the type of the
3891 -- object is not, and does not contain, any subtype derived from
3892 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
3893 -- Has_Stream just for efficiency reasons. There is no point in
3894 -- spending time on a Has_Stream check if the restriction is not set.
3895
3896 if Restriction_Check_Required (No_Streams) then
3897 if Has_Stream (T) then
3898 Check_Restriction (No_Streams, N);
3899 end if;
3900 end if;
3901
3902 -- Deal with predicate check before we start to do major rewriting. It
3903 -- is OK to initialize and then check the initialized value, since the
3904 -- object goes out of scope if we get a predicate failure. Note that we
3905 -- do this in the analyzer and not the expander because the analyzer
3906 -- does some substantial rewriting in some cases.
3907
3908 -- We need a predicate check if the type has predicates, and if either
3909 -- there is an initializing expression, or for default initialization
3910 -- when we have at least one case of an explicit default initial value
3911 -- and then this is not an internal declaration whose initialization
3912 -- comes later (as for an aggregate expansion).
3913
3914 if not Suppress_Assignment_Checks (N)
3915 and then Present (Predicate_Function (T))
3916 and then not No_Initialization (N)
3917 and then
3918 (Present (E)
3919 or else
3920 Is_Partially_Initialized_Type (T, Include_Implicit => False))
3921 then
3922 -- If the type has a static predicate and the expression is known at
3923 -- compile time, see if the expression satisfies the predicate.
3924
3925 if Present (E) then
3926 Check_Expression_Against_Static_Predicate (E, T);
3927 end if;
3928
3929 Insert_After (N,
3930 Make_Predicate_Check (T, New_Occurrence_Of (Id, Loc)));
3931 end if;
3932
3933 -- Case of unconstrained type
3934
3935 if Is_Indefinite_Subtype (T) then
3936
3937 -- In SPARK, a declaration of unconstrained type is allowed
3938 -- only for constants of type string.
3939
3940 if Is_String_Type (T) and then not Constant_Present (N) then
3941 Check_SPARK_05_Restriction
3942 ("declaration of object of unconstrained type not allowed", N);
3943 end if;
3944
3945 -- Nothing to do in deferred constant case
3946
3947 if Constant_Present (N) and then No (E) then
3948 null;
3949
3950 -- Case of no initialization present
3951
3952 elsif No (E) then
3953 if No_Initialization (N) then
3954 null;
3955
3956 elsif Is_Class_Wide_Type (T) then
3957 Error_Msg_N
3958 ("initialization required in class-wide declaration ", N);
3959
3960 else
3961 Error_Msg_N
3962 ("unconstrained subtype not allowed (need initialization)",
3963 Object_Definition (N));
3964
3965 if Is_Record_Type (T) and then Has_Discriminants (T) then
3966 Error_Msg_N
3967 ("\provide initial value or explicit discriminant values",
3968 Object_Definition (N));
3969
3970 Error_Msg_NE
3971 ("\or give default discriminant values for type&",
3972 Object_Definition (N), T);
3973
3974 elsif Is_Array_Type (T) then
3975 Error_Msg_N
3976 ("\provide initial value or explicit array bounds",
3977 Object_Definition (N));
3978 end if;
3979 end if;
3980
3981 -- Case of initialization present but in error. Set initial
3982 -- expression as absent (but do not make above complaints)
3983
3984 elsif E = Error then
3985 Set_Expression (N, Empty);
3986 E := Empty;
3987
3988 -- Case of initialization present
3989
3990 else
3991 -- Check restrictions in Ada 83
3992
3993 if not Constant_Present (N) then
3994
3995 -- Unconstrained variables not allowed in Ada 83 mode
3996
3997 if Ada_Version = Ada_83
3998 and then Comes_From_Source (Object_Definition (N))
3999 then
4000 Error_Msg_N
4001 ("(Ada 83) unconstrained variable not allowed",
4002 Object_Definition (N));
4003 end if;
4004 end if;
4005
4006 -- Now we constrain the variable from the initializing expression
4007
4008 -- If the expression is an aggregate, it has been expanded into
4009 -- individual assignments. Retrieve the actual type from the
4010 -- expanded construct.
4011
4012 if Is_Array_Type (T)
4013 and then No_Initialization (N)
4014 and then Nkind (Original_Node (E)) = N_Aggregate
4015 then
4016 Act_T := Etype (E);
4017
4018 -- In case of class-wide interface object declarations we delay
4019 -- the generation of the equivalent record type declarations until
4020 -- its expansion because there are cases in they are not required.
4021
4022 elsif Is_Interface (T) then
4023 null;
4024
4025 -- In GNATprove mode, Expand_Subtype_From_Expr does nothing. Thus,
4026 -- we should prevent the generation of another Itype with the
4027 -- same name as the one already generated, or we end up with
4028 -- two identical types in GNATprove.
4029
4030 elsif GNATprove_Mode then
4031 null;
4032
4033 -- If the type is an unchecked union, no subtype can be built from
4034 -- the expression. Rewrite declaration as a renaming, which the
4035 -- back-end can handle properly. This is a rather unusual case,
4036 -- because most unchecked_union declarations have default values
4037 -- for discriminants and are thus not indefinite.
4038
4039 elsif Is_Unchecked_Union (T) then
4040 if Constant_Present (N) or else Nkind (E) = N_Function_Call then
4041 Set_Ekind (Id, E_Constant);
4042 else
4043 Set_Ekind (Id, E_Variable);
4044 end if;
4045
4046 -- An object declared within a Ghost region is automatically
4047 -- Ghost (SPARK RM 6.9(2)).
4048
4049 if Comes_From_Source (Id) and then Ghost_Mode > None then
4050 Set_Is_Ghost_Entity (Id);
4051
4052 -- The Ghost policy in effect at the point of declaration
4053 -- and at the point of completion must match
4054 -- (SPARK RM 6.9(15)).
4055
4056 if Present (Prev_Entity)
4057 and then Is_Ghost_Entity (Prev_Entity)
4058 then
4059 Check_Ghost_Completion (Prev_Entity, Id);
4060 end if;
4061 end if;
4062
4063 Rewrite (N,
4064 Make_Object_Renaming_Declaration (Loc,
4065 Defining_Identifier => Id,
4066 Subtype_Mark => New_Occurrence_Of (T, Loc),
4067 Name => E));
4068
4069 Set_Renamed_Object (Id, E);
4070 Freeze_Before (N, T);
4071 Set_Is_Frozen (Id);
4072 return;
4073
4074 else
4075 Expand_Subtype_From_Expr (N, T, Object_Definition (N), E);
4076 Act_T := Find_Type_Of_Object (Object_Definition (N), N);
4077 end if;
4078
4079 Set_Is_Constr_Subt_For_U_Nominal (Act_T);
4080
4081 if Aliased_Present (N) then
4082 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4083 end if;
4084
4085 Freeze_Before (N, Act_T);
4086 Freeze_Before (N, T);
4087 end if;
4088
4089 elsif Is_Array_Type (T)
4090 and then No_Initialization (N)
4091 and then Nkind (Original_Node (E)) = N_Aggregate
4092 then
4093 if not Is_Entity_Name (Object_Definition (N)) then
4094 Act_T := Etype (E);
4095 Check_Compile_Time_Size (Act_T);
4096
4097 if Aliased_Present (N) then
4098 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4099 end if;
4100 end if;
4101
4102 -- When the given object definition and the aggregate are specified
4103 -- independently, and their lengths might differ do a length check.
4104 -- This cannot happen if the aggregate is of the form (others =>...)
4105
4106 if not Is_Constrained (T) then
4107 null;
4108
4109 elsif Nkind (E) = N_Raise_Constraint_Error then
4110
4111 -- Aggregate is statically illegal. Place back in declaration
4112
4113 Set_Expression (N, E);
4114 Set_No_Initialization (N, False);
4115
4116 elsif T = Etype (E) then
4117 null;
4118
4119 elsif Nkind (E) = N_Aggregate
4120 and then Present (Component_Associations (E))
4121 and then Present (Choices (First (Component_Associations (E))))
4122 and then Nkind (First
4123 (Choices (First (Component_Associations (E))))) = N_Others_Choice
4124 then
4125 null;
4126
4127 else
4128 Apply_Length_Check (E, T);
4129 end if;
4130
4131 -- If the type is limited unconstrained with defaulted discriminants and
4132 -- there is no expression, then the object is constrained by the
4133 -- defaults, so it is worthwhile building the corresponding subtype.
4134
4135 elsif (Is_Limited_Record (T) or else Is_Concurrent_Type (T))
4136 and then not Is_Constrained (T)
4137 and then Has_Discriminants (T)
4138 then
4139 if No (E) then
4140 Act_T := Build_Default_Subtype (T, N);
4141 else
4142 -- Ada 2005: A limited object may be initialized by means of an
4143 -- aggregate. If the type has default discriminants it has an
4144 -- unconstrained nominal type, Its actual subtype will be obtained
4145 -- from the aggregate, and not from the default discriminants.
4146
4147 Act_T := Etype (E);
4148 end if;
4149
4150 Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
4151
4152 elsif Nkind (E) = N_Function_Call
4153 and then Constant_Present (N)
4154 and then Has_Unconstrained_Elements (Etype (E))
4155 then
4156 -- The back-end has problems with constants of a discriminated type
4157 -- with defaults, if the initial value is a function call. We
4158 -- generate an intermediate temporary that will receive a reference
4159 -- to the result of the call. The initialization expression then
4160 -- becomes a dereference of that temporary.
4161
4162 Remove_Side_Effects (E);
4163
4164 -- If this is a constant declaration of an unconstrained type and
4165 -- the initialization is an aggregate, we can use the subtype of the
4166 -- aggregate for the declared entity because it is immutable.
4167
4168 elsif not Is_Constrained (T)
4169 and then Has_Discriminants (T)
4170 and then Constant_Present (N)
4171 and then not Has_Unchecked_Union (T)
4172 and then Nkind (E) = N_Aggregate
4173 then
4174 Act_T := Etype (E);
4175 end if;
4176
4177 -- Check No_Wide_Characters restriction
4178
4179 Check_Wide_Character_Restriction (T, Object_Definition (N));
4180
4181 -- Indicate this is not set in source. Certainly true for constants, and
4182 -- true for variables so far (will be reset for a variable if and when
4183 -- we encounter a modification in the source).
4184
4185 Set_Never_Set_In_Source (Id);
4186
4187 -- Now establish the proper kind and type of the object
4188
4189 if Constant_Present (N) then
4190 Set_Ekind (Id, E_Constant);
4191 Set_Is_True_Constant (Id);
4192
4193 else
4194 Set_Ekind (Id, E_Variable);
4195
4196 -- A variable is set as shared passive if it appears in a shared
4197 -- passive package, and is at the outer level. This is not done for
4198 -- entities generated during expansion, because those are always
4199 -- manipulated locally.
4200
4201 if Is_Shared_Passive (Current_Scope)
4202 and then Is_Library_Level_Entity (Id)
4203 and then Comes_From_Source (Id)
4204 then
4205 Set_Is_Shared_Passive (Id);
4206 Check_Shared_Var (Id, T, N);
4207 end if;
4208
4209 -- Set Has_Initial_Value if initializing expression present. Note
4210 -- that if there is no initializing expression, we leave the state
4211 -- of this flag unchanged (usually it will be False, but notably in
4212 -- the case of exception choice variables, it will already be true).
4213
4214 if Present (E) then
4215 Set_Has_Initial_Value (Id);
4216 end if;
4217 end if;
4218
4219 -- Initialize alignment and size and capture alignment setting
4220
4221 Init_Alignment (Id);
4222 Init_Esize (Id);
4223 Set_Optimize_Alignment_Flags (Id);
4224
4225 -- An object declared within a Ghost region is automatically Ghost
4226 -- (SPARK RM 6.9(2)).
4227
4228 if Comes_From_Source (Id)
4229 and then (Ghost_Mode > None
4230 or else (Present (Prev_Entity)
4231 and then Is_Ghost_Entity (Prev_Entity)))
4232 then
4233 Set_Is_Ghost_Entity (Id);
4234
4235 -- The Ghost policy in effect at the point of declaration and at the
4236 -- point of completion must match (SPARK RM 6.9(16)).
4237
4238 if Present (Prev_Entity) and then Is_Ghost_Entity (Prev_Entity) then
4239 Check_Ghost_Completion (Prev_Entity, Id);
4240 end if;
4241 end if;
4242
4243 -- Deal with aliased case
4244
4245 if Aliased_Present (N) then
4246 Set_Is_Aliased (Id);
4247
4248 -- If the object is aliased and the type is unconstrained with
4249 -- defaulted discriminants and there is no expression, then the
4250 -- object is constrained by the defaults, so it is worthwhile
4251 -- building the corresponding subtype.
4252
4253 -- Ada 2005 (AI-363): If the aliased object is discriminated and
4254 -- unconstrained, then only establish an actual subtype if the
4255 -- nominal subtype is indefinite. In definite cases the object is
4256 -- unconstrained in Ada 2005.
4257
4258 if No (E)
4259 and then Is_Record_Type (T)
4260 and then not Is_Constrained (T)
4261 and then Has_Discriminants (T)
4262 and then (Ada_Version < Ada_2005 or else Is_Indefinite_Subtype (T))
4263 then
4264 Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
4265 end if;
4266 end if;
4267
4268 -- Now we can set the type of the object
4269
4270 Set_Etype (Id, Act_T);
4271
4272 -- Non-constant object is marked to be treated as volatile if type is
4273 -- volatile and we clear the Current_Value setting that may have been
4274 -- set above. Doing so for constants isn't required and might interfere
4275 -- with possible uses of the object as a static expression in contexts
4276 -- incompatible with volatility (e.g. as a case-statement alternative).
4277
4278 if Ekind (Id) /= E_Constant and then Treat_As_Volatile (Etype (Id)) then
4279 Set_Treat_As_Volatile (Id);
4280 Set_Current_Value (Id, Empty);
4281 end if;
4282
4283 -- Deal with controlled types
4284
4285 if Has_Controlled_Component (Etype (Id))
4286 or else Is_Controlled (Etype (Id))
4287 then
4288 if not Is_Library_Level_Entity (Id) then
4289 Check_Restriction (No_Nested_Finalization, N);
4290 else
4291 Validate_Controlled_Object (Id);
4292 end if;
4293 end if;
4294
4295 if Has_Task (Etype (Id)) then
4296 Check_Restriction (No_Tasking, N);
4297
4298 -- Deal with counting max tasks
4299
4300 -- Nothing to do if inside a generic
4301
4302 if Inside_A_Generic then
4303 null;
4304
4305 -- If library level entity, then count tasks
4306
4307 elsif Is_Library_Level_Entity (Id) then
4308 Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
4309
4310 -- If not library level entity, then indicate we don't know max
4311 -- tasks and also check task hierarchy restriction and blocking
4312 -- operation (since starting a task is definitely blocking).
4313
4314 else
4315 Check_Restriction (Max_Tasks, N);
4316 Check_Restriction (No_Task_Hierarchy, N);
4317 Check_Potentially_Blocking_Operation (N);
4318 end if;
4319
4320 -- A rather specialized test. If we see two tasks being declared
4321 -- of the same type in the same object declaration, and the task
4322 -- has an entry with an address clause, we know that program error
4323 -- will be raised at run time since we can't have two tasks with
4324 -- entries at the same address.
4325
4326 if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
4327 declare
4328 E : Entity_Id;
4329
4330 begin
4331 E := First_Entity (Etype (Id));
4332 while Present (E) loop
4333 if Ekind (E) = E_Entry
4334 and then Present (Get_Attribute_Definition_Clause
4335 (E, Attribute_Address))
4336 then
4337 Error_Msg_Warn := SPARK_Mode /= On;
4338 Error_Msg_N
4339 ("more than one task with same entry address<<", N);
4340 Error_Msg_N ("\Program_Error [<<", N);
4341 Insert_Action (N,
4342 Make_Raise_Program_Error (Loc,
4343 Reason => PE_Duplicated_Entry_Address));
4344 exit;
4345 end if;
4346
4347 Next_Entity (E);
4348 end loop;
4349 end;
4350 end if;
4351 end if;
4352
4353 -- Some simple constant-propagation: if the expression is a constant
4354 -- string initialized with a literal, share the literal. This avoids
4355 -- a run-time copy.
4356
4357 if Present (E)
4358 and then Is_Entity_Name (E)
4359 and then Ekind (Entity (E)) = E_Constant
4360 and then Base_Type (Etype (E)) = Standard_String
4361 then
4362 declare
4363 Val : constant Node_Id := Constant_Value (Entity (E));
4364 begin
4365 if Present (Val) and then Nkind (Val) = N_String_Literal then
4366 Rewrite (E, New_Copy (Val));
4367 end if;
4368 end;
4369 end if;
4370
4371 -- Another optimization: if the nominal subtype is unconstrained and
4372 -- the expression is a function call that returns an unconstrained
4373 -- type, rewrite the declaration as a renaming of the result of the
4374 -- call. The exceptions below are cases where the copy is expected,
4375 -- either by the back end (Aliased case) or by the semantics, as for
4376 -- initializing controlled types or copying tags for classwide types.
4377
4378 if Present (E)
4379 and then Nkind (E) = N_Explicit_Dereference
4380 and then Nkind (Original_Node (E)) = N_Function_Call
4381 and then not Is_Library_Level_Entity (Id)
4382 and then not Is_Constrained (Underlying_Type (T))
4383 and then not Is_Aliased (Id)
4384 and then not Is_Class_Wide_Type (T)
4385 and then not Is_Controlled (T)
4386 and then not Has_Controlled_Component (Base_Type (T))
4387 and then Expander_Active
4388 then
4389 Rewrite (N,
4390 Make_Object_Renaming_Declaration (Loc,
4391 Defining_Identifier => Id,
4392 Access_Definition => Empty,
4393 Subtype_Mark => New_Occurrence_Of
4394 (Base_Type (Etype (Id)), Loc),
4395 Name => E));
4396
4397 Set_Renamed_Object (Id, E);
4398
4399 -- Force generation of debugging information for the constant and for
4400 -- the renamed function call.
4401
4402 Set_Debug_Info_Needed (Id);
4403 Set_Debug_Info_Needed (Entity (Prefix (E)));
4404 end if;
4405
4406 if Present (Prev_Entity)
4407 and then Is_Frozen (Prev_Entity)
4408 and then not Error_Posted (Id)
4409 then
4410 Error_Msg_N ("full constant declaration appears too late", N);
4411 end if;
4412
4413 Check_Eliminated (Id);
4414
4415 -- Deal with setting In_Private_Part flag if in private part
4416
4417 if Ekind (Scope (Id)) = E_Package and then In_Private_Part (Scope (Id))
4418 then
4419 Set_In_Private_Part (Id);
4420 end if;
4421
4422 -- Check for violation of No_Local_Timing_Events
4423
4424 if Restriction_Check_Required (No_Local_Timing_Events)
4425 and then not Is_Library_Level_Entity (Id)
4426 and then Is_RTE (Etype (Id), RE_Timing_Event)
4427 then
4428 Check_Restriction (No_Local_Timing_Events, N);
4429 end if;
4430
4431 <<Leave>>
4432 -- Initialize the refined state of a variable here because this is a
4433 -- common destination for legal and illegal object declarations.
4434
4435 if Ekind (Id) = E_Variable then
4436 Set_Encapsulating_State (Id, Empty);
4437 end if;
4438
4439 if Has_Aspects (N) then
4440 Analyze_Aspect_Specifications (N, Id);
4441 end if;
4442
4443 Analyze_Dimension (N);
4444
4445 -- Verify whether the object declaration introduces an illegal hidden
4446 -- state within a package subject to a null abstract state.
4447
4448 if Ekind (Id) = E_Variable then
4449 Check_No_Hidden_State (Id);
4450 end if;
4451 end Analyze_Object_Declaration;
4452
4453 ---------------------------
4454 -- Analyze_Others_Choice --
4455 ---------------------------
4456
4457 -- Nothing to do for the others choice node itself, the semantic analysis
4458 -- of the others choice will occur as part of the processing of the parent
4459
4460 procedure Analyze_Others_Choice (N : Node_Id) is
4461 pragma Warnings (Off, N);
4462 begin
4463 null;
4464 end Analyze_Others_Choice;
4465
4466 -------------------------------------------
4467 -- Analyze_Private_Extension_Declaration --
4468 -------------------------------------------
4469
4470 procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
4471 T : constant Entity_Id := Defining_Identifier (N);
4472 Indic : constant Node_Id := Subtype_Indication (N);
4473 Parent_Type : Entity_Id;
4474 Parent_Base : Entity_Id;
4475
4476 begin
4477 -- The private extension declaration may be subject to pragma Ghost with
4478 -- policy Ignore. Set the mode now to ensure that any nodes generated
4479 -- during analysis and expansion are properly flagged as ignored Ghost.
4480
4481 Set_Ghost_Mode (N);
4482
4483 -- Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
4484
4485 if Is_Non_Empty_List (Interface_List (N)) then
4486 declare
4487 Intf : Node_Id;
4488 T : Entity_Id;
4489
4490 begin
4491 Intf := First (Interface_List (N));
4492 while Present (Intf) loop
4493 T := Find_Type_Of_Subtype_Indic (Intf);
4494
4495 Diagnose_Interface (Intf, T);
4496 Next (Intf);
4497 end loop;
4498 end;
4499 end if;
4500
4501 Generate_Definition (T);
4502
4503 -- For other than Ada 2012, just enter the name in the current scope
4504
4505 if Ada_Version < Ada_2012 then
4506 Enter_Name (T);
4507
4508 -- Ada 2012 (AI05-0162): Enter the name in the current scope handling
4509 -- case of private type that completes an incomplete type.
4510
4511 else
4512 declare
4513 Prev : Entity_Id;
4514
4515 begin
4516 Prev := Find_Type_Name (N);
4517
4518 pragma Assert (Prev = T
4519 or else (Ekind (Prev) = E_Incomplete_Type
4520 and then Present (Full_View (Prev))
4521 and then Full_View (Prev) = T));
4522 end;
4523 end if;
4524
4525 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
4526 Parent_Base := Base_Type (Parent_Type);
4527
4528 if Parent_Type = Any_Type or else Etype (Parent_Type) = Any_Type then
4529 Set_Ekind (T, Ekind (Parent_Type));
4530 Set_Etype (T, Any_Type);
4531 goto Leave;
4532
4533 elsif not Is_Tagged_Type (Parent_Type) then
4534 Error_Msg_N
4535 ("parent of type extension must be a tagged type ", Indic);
4536 goto Leave;
4537
4538 elsif Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
4539 Error_Msg_N ("premature derivation of incomplete type", Indic);
4540 goto Leave;
4541
4542 elsif Is_Concurrent_Type (Parent_Type) then
4543 Error_Msg_N
4544 ("parent type of a private extension cannot be "
4545 & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
4546
4547 Set_Etype (T, Any_Type);
4548 Set_Ekind (T, E_Limited_Private_Type);
4549 Set_Private_Dependents (T, New_Elmt_List);
4550 Set_Error_Posted (T);
4551 goto Leave;
4552 end if;
4553
4554 -- Perhaps the parent type should be changed to the class-wide type's
4555 -- specific type in this case to prevent cascading errors ???
4556
4557 if Is_Class_Wide_Type (Parent_Type) then
4558 Error_Msg_N
4559 ("parent of type extension must not be a class-wide type", Indic);
4560 goto Leave;
4561 end if;
4562
4563 if (not Is_Package_Or_Generic_Package (Current_Scope)
4564 and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
4565 or else In_Private_Part (Current_Scope)
4566
4567 then
4568 Error_Msg_N ("invalid context for private extension", N);
4569 end if;
4570
4571 -- Set common attributes
4572
4573 Set_Is_Pure (T, Is_Pure (Current_Scope));
4574 Set_Scope (T, Current_Scope);
4575 Set_Ekind (T, E_Record_Type_With_Private);
4576 Init_Size_Align (T);
4577 Set_Default_SSO (T);
4578
4579 Set_Etype (T, Parent_Base);
4580 Set_Has_Task (T, Has_Task (Parent_Base));
4581 Set_Has_Protected (T, Has_Task (Parent_Base));
4582
4583 Set_Convention (T, Convention (Parent_Type));
4584 Set_First_Rep_Item (T, First_Rep_Item (Parent_Type));
4585 Set_Is_First_Subtype (T);
4586 Make_Class_Wide_Type (T);
4587
4588 if Unknown_Discriminants_Present (N) then
4589 Set_Discriminant_Constraint (T, No_Elist);
4590 end if;
4591
4592 Build_Derived_Record_Type (N, Parent_Type, T);
4593
4594 -- Propagate inherited invariant information. The new type has
4595 -- invariants, if the parent type has inheritable invariants,
4596 -- and these invariants can in turn be inherited.
4597
4598 if Has_Inheritable_Invariants (Parent_Type) then
4599 Set_Has_Inheritable_Invariants (T);
4600 Set_Has_Invariants (T);
4601 end if;
4602
4603 -- Ada 2005 (AI-443): Synchronized private extension or a rewritten
4604 -- synchronized formal derived type.
4605
4606 if Ada_Version >= Ada_2005 and then Synchronized_Present (N) then
4607 Set_Is_Limited_Record (T);
4608
4609 -- Formal derived type case
4610
4611 if Is_Generic_Type (T) then
4612
4613 -- The parent must be a tagged limited type or a synchronized
4614 -- interface.
4615
4616 if (not Is_Tagged_Type (Parent_Type)
4617 or else not Is_Limited_Type (Parent_Type))
4618 and then
4619 (not Is_Interface (Parent_Type)
4620 or else not Is_Synchronized_Interface (Parent_Type))
4621 then
4622 Error_Msg_NE ("parent type of & must be tagged limited " &
4623 "or synchronized", N, T);
4624 end if;
4625
4626 -- The progenitors (if any) must be limited or synchronized
4627 -- interfaces.
4628
4629 if Present (Interfaces (T)) then
4630 declare
4631 Iface : Entity_Id;
4632 Iface_Elmt : Elmt_Id;
4633
4634 begin
4635 Iface_Elmt := First_Elmt (Interfaces (T));
4636 while Present (Iface_Elmt) loop
4637 Iface := Node (Iface_Elmt);
4638
4639 if not Is_Limited_Interface (Iface)
4640 and then not Is_Synchronized_Interface (Iface)
4641 then
4642 Error_Msg_NE ("progenitor & must be limited " &
4643 "or synchronized", N, Iface);
4644 end if;
4645
4646 Next_Elmt (Iface_Elmt);
4647 end loop;
4648 end;
4649 end if;
4650
4651 -- Regular derived extension, the parent must be a limited or
4652 -- synchronized interface.
4653
4654 else
4655 if not Is_Interface (Parent_Type)
4656 or else (not Is_Limited_Interface (Parent_Type)
4657 and then not Is_Synchronized_Interface (Parent_Type))
4658 then
4659 Error_Msg_NE
4660 ("parent type of & must be limited interface", N, T);
4661 end if;
4662 end if;
4663
4664 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
4665 -- extension with a synchronized parent must be explicitly declared
4666 -- synchronized, because the full view will be a synchronized type.
4667 -- This must be checked before the check for limited types below,
4668 -- to ensure that types declared limited are not allowed to extend
4669 -- synchronized interfaces.
4670
4671 elsif Is_Interface (Parent_Type)
4672 and then Is_Synchronized_Interface (Parent_Type)
4673 and then not Synchronized_Present (N)
4674 then
4675 Error_Msg_NE
4676 ("private extension of& must be explicitly synchronized",
4677 N, Parent_Type);
4678
4679 elsif Limited_Present (N) then
4680 Set_Is_Limited_Record (T);
4681
4682 if not Is_Limited_Type (Parent_Type)
4683 and then
4684 (not Is_Interface (Parent_Type)
4685 or else not Is_Limited_Interface (Parent_Type))
4686 then
4687 Error_Msg_NE ("parent type& of limited extension must be limited",
4688 N, Parent_Type);
4689 end if;
4690 end if;
4691
4692 <<Leave>>
4693 if Has_Aspects (N) then
4694 Analyze_Aspect_Specifications (N, T);
4695 end if;
4696 end Analyze_Private_Extension_Declaration;
4697
4698 ---------------------------------
4699 -- Analyze_Subtype_Declaration --
4700 ---------------------------------
4701
4702 procedure Analyze_Subtype_Declaration
4703 (N : Node_Id;
4704 Skip : Boolean := False)
4705 is
4706 Id : constant Entity_Id := Defining_Identifier (N);
4707 T : Entity_Id;
4708 R_Checks : Check_Result;
4709
4710 begin
4711 -- The subtype declaration may be subject to pragma Ghost with policy
4712 -- Ignore. Set the mode now to ensure that any nodes generated during
4713 -- analysis and expansion are properly flagged as ignored Ghost.
4714
4715 Set_Ghost_Mode (N);
4716
4717 Generate_Definition (Id);
4718 Set_Is_Pure (Id, Is_Pure (Current_Scope));
4719 Init_Size_Align (Id);
4720
4721 -- The following guard condition on Enter_Name is to handle cases where
4722 -- the defining identifier has already been entered into the scope but
4723 -- the declaration as a whole needs to be analyzed.
4724
4725 -- This case in particular happens for derived enumeration types. The
4726 -- derived enumeration type is processed as an inserted enumeration type
4727 -- declaration followed by a rewritten subtype declaration. The defining
4728 -- identifier, however, is entered into the name scope very early in the
4729 -- processing of the original type declaration and therefore needs to be
4730 -- avoided here, when the created subtype declaration is analyzed. (See
4731 -- Build_Derived_Types)
4732
4733 -- This also happens when the full view of a private type is derived
4734 -- type with constraints. In this case the entity has been introduced
4735 -- in the private declaration.
4736
4737 -- Finally this happens in some complex cases when validity checks are
4738 -- enabled, where the same subtype declaration may be analyzed twice.
4739 -- This can happen if the subtype is created by the pre-analysis of
4740 -- an attribute tht gives the range of a loop statement, and the loop
4741 -- itself appears within an if_statement that will be rewritten during
4742 -- expansion.
4743
4744 if Skip
4745 or else (Present (Etype (Id))
4746 and then (Is_Private_Type (Etype (Id))
4747 or else Is_Task_Type (Etype (Id))
4748 or else Is_Rewrite_Substitution (N)))
4749 then
4750 null;
4751
4752 elsif Current_Entity (Id) = Id then
4753 null;
4754
4755 else
4756 Enter_Name (Id);
4757 end if;
4758
4759 T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
4760
4761 -- Class-wide equivalent types of records with unknown discriminants
4762 -- involve the generation of an itype which serves as the private view
4763 -- of a constrained record subtype. In such cases the base type of the
4764 -- current subtype we are processing is the private itype. Use the full
4765 -- of the private itype when decorating various attributes.
4766
4767 if Is_Itype (T)
4768 and then Is_Private_Type (T)
4769 and then Present (Full_View (T))
4770 then
4771 T := Full_View (T);
4772 end if;
4773
4774 -- Inherit common attributes
4775
4776 Set_Is_Volatile (Id, Is_Volatile (T));
4777 Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
4778 Set_Is_Generic_Type (Id, Is_Generic_Type (Base_Type (T)));
4779 Set_Convention (Id, Convention (T));
4780
4781 -- If ancestor has predicates then so does the subtype, and in addition
4782 -- we must delay the freeze to properly arrange predicate inheritance.
4783
4784 -- The Ancestor_Type test is really unpleasant, there seem to be cases
4785 -- in which T = ID, so the above tests and assignments do nothing???
4786
4787 if Has_Predicates (T)
4788 or else (Present (Ancestor_Subtype (T))
4789 and then Has_Predicates (Ancestor_Subtype (T)))
4790 then
4791 Set_Has_Predicates (Id);
4792 Set_Has_Delayed_Freeze (Id);
4793 end if;
4794
4795 -- Subtype of Boolean cannot have a constraint in SPARK
4796
4797 if Is_Boolean_Type (T)
4798 and then Nkind (Subtype_Indication (N)) = N_Subtype_Indication
4799 then
4800 Check_SPARK_05_Restriction
4801 ("subtype of Boolean cannot have constraint", N);
4802 end if;
4803
4804 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
4805 declare
4806 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
4807 One_Cstr : Node_Id;
4808 Low : Node_Id;
4809 High : Node_Id;
4810
4811 begin
4812 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint then
4813 One_Cstr := First (Constraints (Cstr));
4814 while Present (One_Cstr) loop
4815
4816 -- Index or discriminant constraint in SPARK must be a
4817 -- subtype mark.
4818
4819 if not
4820 Nkind_In (One_Cstr, N_Identifier, N_Expanded_Name)
4821 then
4822 Check_SPARK_05_Restriction
4823 ("subtype mark required", One_Cstr);
4824
4825 -- String subtype must have a lower bound of 1 in SPARK.
4826 -- Note that we do not need to test for the non-static case
4827 -- here, since that was already taken care of in
4828 -- Process_Range_Expr_In_Decl.
4829
4830 elsif Base_Type (T) = Standard_String then
4831 Get_Index_Bounds (One_Cstr, Low, High);
4832
4833 if Is_OK_Static_Expression (Low)
4834 and then Expr_Value (Low) /= 1
4835 then
4836 Check_SPARK_05_Restriction
4837 ("String subtype must have lower bound of 1", N);
4838 end if;
4839 end if;
4840
4841 Next (One_Cstr);
4842 end loop;
4843 end if;
4844 end;
4845 end if;
4846
4847 -- In the case where there is no constraint given in the subtype
4848 -- indication, Process_Subtype just returns the Subtype_Mark, so its
4849 -- semantic attributes must be established here.
4850
4851 if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
4852 Set_Etype (Id, Base_Type (T));
4853
4854 -- Subtype of unconstrained array without constraint is not allowed
4855 -- in SPARK.
4856
4857 if Is_Array_Type (T) and then not Is_Constrained (T) then
4858 Check_SPARK_05_Restriction
4859 ("subtype of unconstrained array must have constraint", N);
4860 end if;
4861
4862 case Ekind (T) is
4863 when Array_Kind =>
4864 Set_Ekind (Id, E_Array_Subtype);
4865 Copy_Array_Subtype_Attributes (Id, T);
4866
4867 when Decimal_Fixed_Point_Kind =>
4868 Set_Ekind (Id, E_Decimal_Fixed_Point_Subtype);
4869 Set_Digits_Value (Id, Digits_Value (T));
4870 Set_Delta_Value (Id, Delta_Value (T));
4871 Set_Scale_Value (Id, Scale_Value (T));
4872 Set_Small_Value (Id, Small_Value (T));
4873 Set_Scalar_Range (Id, Scalar_Range (T));
4874 Set_Machine_Radix_10 (Id, Machine_Radix_10 (T));
4875 Set_Is_Constrained (Id, Is_Constrained (T));
4876 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4877 Set_RM_Size (Id, RM_Size (T));
4878
4879 when Enumeration_Kind =>
4880 Set_Ekind (Id, E_Enumeration_Subtype);
4881 Set_First_Literal (Id, First_Literal (Base_Type (T)));
4882 Set_Scalar_Range (Id, Scalar_Range (T));
4883 Set_Is_Character_Type (Id, Is_Character_Type (T));
4884 Set_Is_Constrained (Id, Is_Constrained (T));
4885 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4886 Set_RM_Size (Id, RM_Size (T));
4887 Inherit_Predicate_Flags (Id, T);
4888
4889 when Ordinary_Fixed_Point_Kind =>
4890 Set_Ekind (Id, E_Ordinary_Fixed_Point_Subtype);
4891 Set_Scalar_Range (Id, Scalar_Range (T));
4892 Set_Small_Value (Id, Small_Value (T));
4893 Set_Delta_Value (Id, Delta_Value (T));
4894 Set_Is_Constrained (Id, Is_Constrained (T));
4895 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4896 Set_RM_Size (Id, RM_Size (T));
4897
4898 when Float_Kind =>
4899 Set_Ekind (Id, E_Floating_Point_Subtype);
4900 Set_Scalar_Range (Id, Scalar_Range (T));
4901 Set_Digits_Value (Id, Digits_Value (T));
4902 Set_Is_Constrained (Id, Is_Constrained (T));
4903
4904 when Signed_Integer_Kind =>
4905 Set_Ekind (Id, E_Signed_Integer_Subtype);
4906 Set_Scalar_Range (Id, Scalar_Range (T));
4907 Set_Is_Constrained (Id, Is_Constrained (T));
4908 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4909 Set_RM_Size (Id, RM_Size (T));
4910 Inherit_Predicate_Flags (Id, T);
4911
4912 when Modular_Integer_Kind =>
4913 Set_Ekind (Id, E_Modular_Integer_Subtype);
4914 Set_Scalar_Range (Id, Scalar_Range (T));
4915 Set_Is_Constrained (Id, Is_Constrained (T));
4916 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4917 Set_RM_Size (Id, RM_Size (T));
4918 Inherit_Predicate_Flags (Id, T);
4919
4920 when Class_Wide_Kind =>
4921 Set_Ekind (Id, E_Class_Wide_Subtype);
4922 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
4923 Set_Cloned_Subtype (Id, T);
4924 Set_Is_Tagged_Type (Id, True);
4925 Set_Has_Unknown_Discriminants
4926 (Id, True);
4927 Set_No_Tagged_Streams_Pragma
4928 (Id, No_Tagged_Streams_Pragma (T));
4929
4930 if Ekind (T) = E_Class_Wide_Subtype then
4931 Set_Equivalent_Type (Id, Equivalent_Type (T));
4932 end if;
4933
4934 when E_Record_Type | E_Record_Subtype =>
4935 Set_Ekind (Id, E_Record_Subtype);
4936
4937 if Ekind (T) = E_Record_Subtype
4938 and then Present (Cloned_Subtype (T))
4939 then
4940 Set_Cloned_Subtype (Id, Cloned_Subtype (T));
4941 else
4942 Set_Cloned_Subtype (Id, T);
4943 end if;
4944
4945 Set_First_Entity (Id, First_Entity (T));
4946 Set_Last_Entity (Id, Last_Entity (T));
4947 Set_Has_Discriminants (Id, Has_Discriminants (T));
4948 Set_Is_Constrained (Id, Is_Constrained (T));
4949 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
4950 Set_Has_Implicit_Dereference
4951 (Id, Has_Implicit_Dereference (T));
4952 Set_Has_Unknown_Discriminants
4953 (Id, Has_Unknown_Discriminants (T));
4954
4955 if Has_Discriminants (T) then
4956 Set_Discriminant_Constraint
4957 (Id, Discriminant_Constraint (T));
4958 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
4959
4960 elsif Has_Unknown_Discriminants (Id) then
4961 Set_Discriminant_Constraint (Id, No_Elist);
4962 end if;
4963
4964 if Is_Tagged_Type (T) then
4965 Set_Is_Tagged_Type (Id, True);
4966 Set_No_Tagged_Streams_Pragma
4967 (Id, No_Tagged_Streams_Pragma (T));
4968 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
4969 Set_Direct_Primitive_Operations
4970 (Id, Direct_Primitive_Operations (T));
4971 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
4972
4973 if Is_Interface (T) then
4974 Set_Is_Interface (Id);
4975 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
4976 end if;
4977 end if;
4978
4979 when Private_Kind =>
4980 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
4981 Set_Has_Discriminants (Id, Has_Discriminants (T));
4982 Set_Is_Constrained (Id, Is_Constrained (T));
4983 Set_First_Entity (Id, First_Entity (T));
4984 Set_Last_Entity (Id, Last_Entity (T));
4985 Set_Private_Dependents (Id, New_Elmt_List);
4986 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
4987 Set_Has_Implicit_Dereference
4988 (Id, Has_Implicit_Dereference (T));
4989 Set_Has_Unknown_Discriminants
4990 (Id, Has_Unknown_Discriminants (T));
4991 Set_Known_To_Have_Preelab_Init
4992 (Id, Known_To_Have_Preelab_Init (T));
4993
4994 if Is_Tagged_Type (T) then
4995 Set_Is_Tagged_Type (Id);
4996 Set_No_Tagged_Streams_Pragma (Id,
4997 No_Tagged_Streams_Pragma (T));
4998 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
4999 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5000 Set_Direct_Primitive_Operations (Id,
5001 Direct_Primitive_Operations (T));
5002 end if;
5003
5004 -- In general the attributes of the subtype of a private type
5005 -- are the attributes of the partial view of parent. However,
5006 -- the full view may be a discriminated type, and the subtype
5007 -- must share the discriminant constraint to generate correct
5008 -- calls to initialization procedures.
5009
5010 if Has_Discriminants (T) then
5011 Set_Discriminant_Constraint
5012 (Id, Discriminant_Constraint (T));
5013 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5014
5015 elsif Present (Full_View (T))
5016 and then Has_Discriminants (Full_View (T))
5017 then
5018 Set_Discriminant_Constraint
5019 (Id, Discriminant_Constraint (Full_View (T)));
5020 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5021
5022 -- This would seem semantically correct, but apparently
5023 -- generates spurious errors about missing components ???
5024
5025 -- Set_Has_Discriminants (Id);
5026 end if;
5027
5028 Prepare_Private_Subtype_Completion (Id, N);
5029
5030 -- If this is the subtype of a constrained private type with
5031 -- discriminants that has got a full view and we also have
5032 -- built a completion just above, show that the completion
5033 -- is a clone of the full view to the back-end.
5034
5035 if Has_Discriminants (T)
5036 and then not Has_Unknown_Discriminants (T)
5037 and then not Is_Empty_Elmt_List (Discriminant_Constraint (T))
5038 and then Present (Full_View (T))
5039 and then Present (Full_View (Id))
5040 then
5041 Set_Cloned_Subtype (Full_View (Id), Full_View (T));
5042 end if;
5043
5044 when Access_Kind =>
5045 Set_Ekind (Id, E_Access_Subtype);
5046 Set_Is_Constrained (Id, Is_Constrained (T));
5047 Set_Is_Access_Constant
5048 (Id, Is_Access_Constant (T));
5049 Set_Directly_Designated_Type
5050 (Id, Designated_Type (T));
5051 Set_Can_Never_Be_Null (Id, Can_Never_Be_Null (T));
5052
5053 -- A Pure library_item must not contain the declaration of a
5054 -- named access type, except within a subprogram, generic
5055 -- subprogram, task unit, or protected unit, or if it has
5056 -- a specified Storage_Size of zero (RM05-10.2.1(15.4-15.5)).
5057
5058 if Comes_From_Source (Id)
5059 and then In_Pure_Unit
5060 and then not In_Subprogram_Task_Protected_Unit
5061 and then not No_Pool_Assigned (Id)
5062 then
5063 Error_Msg_N
5064 ("named access types not allowed in pure unit", N);
5065 end if;
5066
5067 when Concurrent_Kind =>
5068 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
5069 Set_Corresponding_Record_Type (Id,
5070 Corresponding_Record_Type (T));
5071 Set_First_Entity (Id, First_Entity (T));
5072 Set_First_Private_Entity (Id, First_Private_Entity (T));
5073 Set_Has_Discriminants (Id, Has_Discriminants (T));
5074 Set_Is_Constrained (Id, Is_Constrained (T));
5075 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5076 Set_Last_Entity (Id, Last_Entity (T));
5077
5078 if Is_Tagged_Type (T) then
5079 Set_No_Tagged_Streams_Pragma
5080 (Id, No_Tagged_Streams_Pragma (T));
5081 end if;
5082
5083 if Has_Discriminants (T) then
5084 Set_Discriminant_Constraint
5085 (Id, Discriminant_Constraint (T));
5086 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5087 end if;
5088
5089 when Incomplete_Kind =>
5090 if Ada_Version >= Ada_2005 then
5091
5092 -- In Ada 2005 an incomplete type can be explicitly tagged:
5093 -- propagate indication. Note that we also have to include
5094 -- subtypes for Ada 2012 extended use of incomplete types.
5095
5096 Set_Ekind (Id, E_Incomplete_Subtype);
5097 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5098 Set_Private_Dependents (Id, New_Elmt_List);
5099
5100 if Is_Tagged_Type (Id) then
5101 Set_No_Tagged_Streams_Pragma
5102 (Id, No_Tagged_Streams_Pragma (T));
5103 Set_Direct_Primitive_Operations (Id, New_Elmt_List);
5104 end if;
5105
5106 -- Ada 2005 (AI-412): Decorate an incomplete subtype of an
5107 -- incomplete type visible through a limited with clause.
5108
5109 if From_Limited_With (T)
5110 and then Present (Non_Limited_View (T))
5111 then
5112 Set_From_Limited_With (Id);
5113 Set_Non_Limited_View (Id, Non_Limited_View (T));
5114
5115 -- Ada 2005 (AI-412): Add the regular incomplete subtype
5116 -- to the private dependents of the original incomplete
5117 -- type for future transformation.
5118
5119 else
5120 Append_Elmt (Id, Private_Dependents (T));
5121 end if;
5122
5123 -- If the subtype name denotes an incomplete type an error
5124 -- was already reported by Process_Subtype.
5125
5126 else
5127 Set_Etype (Id, Any_Type);
5128 end if;
5129
5130 when others =>
5131 raise Program_Error;
5132 end case;
5133 end if;
5134
5135 if Etype (Id) = Any_Type then
5136 goto Leave;
5137 end if;
5138
5139 -- Some common processing on all types
5140
5141 Set_Size_Info (Id, T);
5142 Set_First_Rep_Item (Id, First_Rep_Item (T));
5143
5144 -- If the parent type is a generic actual, so is the subtype. This may
5145 -- happen in a nested instance. Why Comes_From_Source test???
5146
5147 if not Comes_From_Source (N) then
5148 Set_Is_Generic_Actual_Type (Id, Is_Generic_Actual_Type (T));
5149 end if;
5150
5151 T := Etype (Id);
5152
5153 Set_Is_Immediately_Visible (Id, True);
5154 Set_Depends_On_Private (Id, Has_Private_Component (T));
5155 Set_Is_Descendent_Of_Address (Id, Is_Descendent_Of_Address (T));
5156
5157 if Is_Interface (T) then
5158 Set_Is_Interface (Id);
5159 end if;
5160
5161 if Present (Generic_Parent_Type (N))
5162 and then
5163 (Nkind (Parent (Generic_Parent_Type (N))) /=
5164 N_Formal_Type_Declaration
5165 or else Nkind (Formal_Type_Definition
5166 (Parent (Generic_Parent_Type (N)))) /=
5167 N_Formal_Private_Type_Definition)
5168 then
5169 if Is_Tagged_Type (Id) then
5170
5171 -- If this is a generic actual subtype for a synchronized type,
5172 -- the primitive operations are those of the corresponding record
5173 -- for which there is a separate subtype declaration.
5174
5175 if Is_Concurrent_Type (Id) then
5176 null;
5177 elsif Is_Class_Wide_Type (Id) then
5178 Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
5179 else
5180 Derive_Subprograms (Generic_Parent_Type (N), Id, T);
5181 end if;
5182
5183 elsif Scope (Etype (Id)) /= Standard_Standard then
5184 Derive_Subprograms (Generic_Parent_Type (N), Id);
5185 end if;
5186 end if;
5187
5188 if Is_Private_Type (T) and then Present (Full_View (T)) then
5189 Conditional_Delay (Id, Full_View (T));
5190
5191 -- The subtypes of components or subcomponents of protected types
5192 -- do not need freeze nodes, which would otherwise appear in the
5193 -- wrong scope (before the freeze node for the protected type). The
5194 -- proper subtypes are those of the subcomponents of the corresponding
5195 -- record.
5196
5197 elsif Ekind (Scope (Id)) /= E_Protected_Type
5198 and then Present (Scope (Scope (Id))) -- error defense
5199 and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
5200 then
5201 Conditional_Delay (Id, T);
5202 end if;
5203
5204 -- Check that Constraint_Error is raised for a scalar subtype indication
5205 -- when the lower or upper bound of a non-null range lies outside the
5206 -- range of the type mark.
5207
5208 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5209 if Is_Scalar_Type (Etype (Id))
5210 and then Scalar_Range (Id) /=
5211 Scalar_Range (Etype (Subtype_Mark
5212 (Subtype_Indication (N))))
5213 then
5214 Apply_Range_Check
5215 (Scalar_Range (Id),
5216 Etype (Subtype_Mark (Subtype_Indication (N))));
5217
5218 -- In the array case, check compatibility for each index
5219
5220 elsif Is_Array_Type (Etype (Id)) and then Present (First_Index (Id))
5221 then
5222 -- This really should be a subprogram that finds the indications
5223 -- to check???
5224
5225 declare
5226 Subt_Index : Node_Id := First_Index (Id);
5227 Target_Index : Node_Id :=
5228 First_Index (Etype
5229 (Subtype_Mark (Subtype_Indication (N))));
5230 Has_Dyn_Chk : Boolean := Has_Dynamic_Range_Check (N);
5231
5232 begin
5233 while Present (Subt_Index) loop
5234 if ((Nkind (Subt_Index) = N_Identifier
5235 and then Ekind (Entity (Subt_Index)) in Scalar_Kind)
5236 or else Nkind (Subt_Index) = N_Subtype_Indication)
5237 and then
5238 Nkind (Scalar_Range (Etype (Subt_Index))) = N_Range
5239 then
5240 declare
5241 Target_Typ : constant Entity_Id :=
5242 Etype (Target_Index);
5243 begin
5244 R_Checks :=
5245 Get_Range_Checks
5246 (Scalar_Range (Etype (Subt_Index)),
5247 Target_Typ,
5248 Etype (Subt_Index),
5249 Defining_Identifier (N));
5250
5251 -- Reset Has_Dynamic_Range_Check on the subtype to
5252 -- prevent elision of the index check due to a dynamic
5253 -- check generated for a preceding index (needed since
5254 -- Insert_Range_Checks tries to avoid generating
5255 -- redundant checks on a given declaration).
5256
5257 Set_Has_Dynamic_Range_Check (N, False);
5258
5259 Insert_Range_Checks
5260 (R_Checks,
5261 N,
5262 Target_Typ,
5263 Sloc (Defining_Identifier (N)));
5264
5265 -- Record whether this index involved a dynamic check
5266
5267 Has_Dyn_Chk :=
5268 Has_Dyn_Chk or else Has_Dynamic_Range_Check (N);
5269 end;
5270 end if;
5271
5272 Next_Index (Subt_Index);
5273 Next_Index (Target_Index);
5274 end loop;
5275
5276 -- Finally, mark whether the subtype involves dynamic checks
5277
5278 Set_Has_Dynamic_Range_Check (N, Has_Dyn_Chk);
5279 end;
5280 end if;
5281 end if;
5282
5283 -- A type invariant applies to any subtype in its scope, in particular
5284 -- to a generic actual.
5285
5286 if Has_Invariants (T) and then In_Open_Scopes (Scope (T)) then
5287 Set_Has_Invariants (Id);
5288 Set_Invariant_Procedure (Id, Invariant_Procedure (T));
5289 end if;
5290
5291 -- Make sure that generic actual types are properly frozen. The subtype
5292 -- is marked as a generic actual type when the enclosing instance is
5293 -- analyzed, so here we identify the subtype from the tree structure.
5294
5295 if Expander_Active
5296 and then Is_Generic_Actual_Type (Id)
5297 and then In_Instance
5298 and then not Comes_From_Source (N)
5299 and then Nkind (Subtype_Indication (N)) /= N_Subtype_Indication
5300 and then Is_Frozen (T)
5301 then
5302 Freeze_Before (N, Id);
5303 end if;
5304
5305 Set_Optimize_Alignment_Flags (Id);
5306 Check_Eliminated (Id);
5307
5308 <<Leave>>
5309 if Has_Aspects (N) then
5310 Analyze_Aspect_Specifications (N, Id);
5311 end if;
5312
5313 Analyze_Dimension (N);
5314 end Analyze_Subtype_Declaration;
5315
5316 --------------------------------
5317 -- Analyze_Subtype_Indication --
5318 --------------------------------
5319
5320 procedure Analyze_Subtype_Indication (N : Node_Id) is
5321 T : constant Entity_Id := Subtype_Mark (N);
5322 R : constant Node_Id := Range_Expression (Constraint (N));
5323
5324 begin
5325 Analyze (T);
5326
5327 if R /= Error then
5328 Analyze (R);
5329 Set_Etype (N, Etype (R));
5330 Resolve (R, Entity (T));
5331 else
5332 Set_Error_Posted (R);
5333 Set_Error_Posted (T);
5334 end if;
5335 end Analyze_Subtype_Indication;
5336
5337 --------------------------
5338 -- Analyze_Variant_Part --
5339 --------------------------
5340
5341 procedure Analyze_Variant_Part (N : Node_Id) is
5342 Discr_Name : Node_Id;
5343 Discr_Type : Entity_Id;
5344
5345 procedure Process_Variant (A : Node_Id);
5346 -- Analyze declarations for a single variant
5347
5348 package Analyze_Variant_Choices is
5349 new Generic_Analyze_Choices (Process_Variant);
5350 use Analyze_Variant_Choices;
5351
5352 ---------------------
5353 -- Process_Variant --
5354 ---------------------
5355
5356 procedure Process_Variant (A : Node_Id) is
5357 CL : constant Node_Id := Component_List (A);
5358 begin
5359 if not Null_Present (CL) then
5360 Analyze_Declarations (Component_Items (CL));
5361
5362 if Present (Variant_Part (CL)) then
5363 Analyze (Variant_Part (CL));
5364 end if;
5365 end if;
5366 end Process_Variant;
5367
5368 -- Start of processing for Analyze_Variant_Part
5369
5370 begin
5371 Discr_Name := Name (N);
5372 Analyze (Discr_Name);
5373
5374 -- If Discr_Name bad, get out (prevent cascaded errors)
5375
5376 if Etype (Discr_Name) = Any_Type then
5377 return;
5378 end if;
5379
5380 -- Check invalid discriminant in variant part
5381
5382 if Ekind (Entity (Discr_Name)) /= E_Discriminant then
5383 Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
5384 end if;
5385
5386 Discr_Type := Etype (Entity (Discr_Name));
5387
5388 if not Is_Discrete_Type (Discr_Type) then
5389 Error_Msg_N
5390 ("discriminant in a variant part must be of a discrete type",
5391 Name (N));
5392 return;
5393 end if;
5394
5395 -- Now analyze the choices, which also analyzes the declarations that
5396 -- are associated with each choice.
5397
5398 Analyze_Choices (Variants (N), Discr_Type);
5399
5400 -- Note: we used to instantiate and call Check_Choices here to check
5401 -- that the choices covered the discriminant, but it's too early to do
5402 -- that because of statically predicated subtypes, whose analysis may
5403 -- be deferred to their freeze point which may be as late as the freeze
5404 -- point of the containing record. So this call is now to be found in
5405 -- Freeze_Record_Declaration.
5406
5407 end Analyze_Variant_Part;
5408
5409 ----------------------------
5410 -- Array_Type_Declaration --
5411 ----------------------------
5412
5413 procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
5414 Component_Def : constant Node_Id := Component_Definition (Def);
5415 Component_Typ : constant Node_Id := Subtype_Indication (Component_Def);
5416 Element_Type : Entity_Id;
5417 Implicit_Base : Entity_Id;
5418 Index : Node_Id;
5419 Related_Id : Entity_Id := Empty;
5420 Nb_Index : Nat;
5421 P : constant Node_Id := Parent (Def);
5422 Priv : Entity_Id;
5423
5424 begin
5425 if Nkind (Def) = N_Constrained_Array_Definition then
5426 Index := First (Discrete_Subtype_Definitions (Def));
5427 else
5428 Index := First (Subtype_Marks (Def));
5429 end if;
5430
5431 -- Find proper names for the implicit types which may be public. In case
5432 -- of anonymous arrays we use the name of the first object of that type
5433 -- as prefix.
5434
5435 if No (T) then
5436 Related_Id := Defining_Identifier (P);
5437 else
5438 Related_Id := T;
5439 end if;
5440
5441 Nb_Index := 1;
5442 while Present (Index) loop
5443 Analyze (Index);
5444
5445 -- Test for odd case of trying to index a type by the type itself
5446
5447 if Is_Entity_Name (Index) and then Entity (Index) = T then
5448 Error_Msg_N ("type& cannot be indexed by itself", Index);
5449 Set_Entity (Index, Standard_Boolean);
5450 Set_Etype (Index, Standard_Boolean);
5451 end if;
5452
5453 -- Check SPARK restriction requiring a subtype mark
5454
5455 if not Nkind_In (Index, N_Identifier, N_Expanded_Name) then
5456 Check_SPARK_05_Restriction ("subtype mark required", Index);
5457 end if;
5458
5459 -- Add a subtype declaration for each index of private array type
5460 -- declaration whose etype is also private. For example:
5461
5462 -- package Pkg is
5463 -- type Index is private;
5464 -- private
5465 -- type Table is array (Index) of ...
5466 -- end;
5467
5468 -- This is currently required by the expander for the internally
5469 -- generated equality subprogram of records with variant parts in
5470 -- which the etype of some component is such private type.
5471
5472 if Ekind (Current_Scope) = E_Package
5473 and then In_Private_Part (Current_Scope)
5474 and then Has_Private_Declaration (Etype (Index))
5475 then
5476 declare
5477 Loc : constant Source_Ptr := Sloc (Def);
5478 New_E : Entity_Id;
5479 Decl : Entity_Id;
5480
5481 begin
5482 New_E := Make_Temporary (Loc, 'T');
5483 Set_Is_Internal (New_E);
5484
5485 Decl :=
5486 Make_Subtype_Declaration (Loc,
5487 Defining_Identifier => New_E,
5488 Subtype_Indication =>
5489 New_Occurrence_Of (Etype (Index), Loc));
5490
5491 Insert_Before (Parent (Def), Decl);
5492 Analyze (Decl);
5493 Set_Etype (Index, New_E);
5494
5495 -- If the index is a range the Entity attribute is not
5496 -- available. Example:
5497
5498 -- package Pkg is
5499 -- type T is private;
5500 -- private
5501 -- type T is new Natural;
5502 -- Table : array (T(1) .. T(10)) of Boolean;
5503 -- end Pkg;
5504
5505 if Nkind (Index) /= N_Range then
5506 Set_Entity (Index, New_E);
5507 end if;
5508 end;
5509 end if;
5510
5511 Make_Index (Index, P, Related_Id, Nb_Index);
5512
5513 -- Check error of subtype with predicate for index type
5514
5515 Bad_Predicated_Subtype_Use
5516 ("subtype& has predicate, not allowed as index subtype",
5517 Index, Etype (Index));
5518
5519 -- Move to next index
5520
5521 Next_Index (Index);
5522 Nb_Index := Nb_Index + 1;
5523 end loop;
5524
5525 -- Process subtype indication if one is present
5526
5527 if Present (Component_Typ) then
5528 Element_Type := Process_Subtype (Component_Typ, P, Related_Id, 'C');
5529
5530 Set_Etype (Component_Typ, Element_Type);
5531
5532 if not Nkind_In (Component_Typ, N_Identifier, N_Expanded_Name) then
5533 Check_SPARK_05_Restriction
5534 ("subtype mark required", Component_Typ);
5535 end if;
5536
5537 -- Ada 2005 (AI-230): Access Definition case
5538
5539 else pragma Assert (Present (Access_Definition (Component_Def)));
5540
5541 -- Indicate that the anonymous access type is created by the
5542 -- array type declaration.
5543
5544 Element_Type := Access_Definition
5545 (Related_Nod => P,
5546 N => Access_Definition (Component_Def));
5547 Set_Is_Local_Anonymous_Access (Element_Type);
5548
5549 -- Propagate the parent. This field is needed if we have to generate
5550 -- the master_id associated with an anonymous access to task type
5551 -- component (see Expand_N_Full_Type_Declaration.Build_Master)
5552
5553 Set_Parent (Element_Type, Parent (T));
5554
5555 -- Ada 2005 (AI-230): In case of components that are anonymous access
5556 -- types the level of accessibility depends on the enclosing type
5557 -- declaration
5558
5559 Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
5560
5561 -- Ada 2005 (AI-254)
5562
5563 declare
5564 CD : constant Node_Id :=
5565 Access_To_Subprogram_Definition
5566 (Access_Definition (Component_Def));
5567 begin
5568 if Present (CD) and then Protected_Present (CD) then
5569 Element_Type :=
5570 Replace_Anonymous_Access_To_Protected_Subprogram (Def);
5571 end if;
5572 end;
5573 end if;
5574
5575 -- Constrained array case
5576
5577 if No (T) then
5578 T := Create_Itype (E_Void, P, Related_Id, 'T');
5579 end if;
5580
5581 if Nkind (Def) = N_Constrained_Array_Definition then
5582
5583 -- Establish Implicit_Base as unconstrained base type
5584
5585 Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
5586
5587 Set_Etype (Implicit_Base, Implicit_Base);
5588 Set_Scope (Implicit_Base, Current_Scope);
5589 Set_Has_Delayed_Freeze (Implicit_Base);
5590 Set_Default_SSO (Implicit_Base);
5591
5592 -- The constrained array type is a subtype of the unconstrained one
5593
5594 Set_Ekind (T, E_Array_Subtype);
5595 Init_Size_Align (T);
5596 Set_Etype (T, Implicit_Base);
5597 Set_Scope (T, Current_Scope);
5598 Set_Is_Constrained (T);
5599 Set_First_Index (T,
5600 First (Discrete_Subtype_Definitions (Def)));
5601 Set_Has_Delayed_Freeze (T);
5602
5603 -- Complete setup of implicit base type
5604
5605 Set_First_Index (Implicit_Base, First_Index (T));
5606 Set_Component_Type (Implicit_Base, Element_Type);
5607 Set_Has_Task (Implicit_Base, Has_Task (Element_Type));
5608 Set_Has_Protected (Implicit_Base, Has_Protected (Element_Type));
5609 Set_Component_Size (Implicit_Base, Uint_0);
5610 Set_Packed_Array_Impl_Type (Implicit_Base, Empty);
5611 Set_Has_Controlled_Component (Implicit_Base,
5612 Has_Controlled_Component (Element_Type)
5613 or else Is_Controlled (Element_Type));
5614 Set_Finalize_Storage_Only (Implicit_Base,
5615 Finalize_Storage_Only (Element_Type));
5616
5617 -- Inherit the "ghostness" from the constrained array type
5618
5619 if Is_Ghost_Entity (T) or else Ghost_Mode > None then
5620 Set_Is_Ghost_Entity (Implicit_Base);
5621 end if;
5622
5623 -- Unconstrained array case
5624
5625 else
5626 Set_Ekind (T, E_Array_Type);
5627 Init_Size_Align (T);
5628 Set_Etype (T, T);
5629 Set_Scope (T, Current_Scope);
5630 Set_Component_Size (T, Uint_0);
5631 Set_Is_Constrained (T, False);
5632 Set_First_Index (T, First (Subtype_Marks (Def)));
5633 Set_Has_Delayed_Freeze (T, True);
5634 Set_Has_Task (T, Has_Task (Element_Type));
5635 Set_Has_Protected (T, Has_Protected (Element_Type));
5636 Set_Has_Controlled_Component (T, Has_Controlled_Component
5637 (Element_Type)
5638 or else
5639 Is_Controlled (Element_Type));
5640 Set_Finalize_Storage_Only (T, Finalize_Storage_Only
5641 (Element_Type));
5642 Set_Default_SSO (T);
5643 end if;
5644
5645 -- Common attributes for both cases
5646
5647 Set_Component_Type (Base_Type (T), Element_Type);
5648 Set_Packed_Array_Impl_Type (T, Empty);
5649
5650 if Aliased_Present (Component_Definition (Def)) then
5651 Check_SPARK_05_Restriction
5652 ("aliased is not allowed", Component_Definition (Def));
5653 Set_Has_Aliased_Components (Etype (T));
5654 end if;
5655
5656 -- Ada 2005 (AI-231): Propagate the null-excluding attribute to the
5657 -- array type to ensure that objects of this type are initialized.
5658
5659 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (Element_Type) then
5660 Set_Can_Never_Be_Null (T);
5661
5662 if Null_Exclusion_Present (Component_Definition (Def))
5663
5664 -- No need to check itypes because in their case this check was
5665 -- done at their point of creation
5666
5667 and then not Is_Itype (Element_Type)
5668 then
5669 Error_Msg_N
5670 ("`NOT NULL` not allowed (null already excluded)",
5671 Subtype_Indication (Component_Definition (Def)));
5672 end if;
5673 end if;
5674
5675 Priv := Private_Component (Element_Type);
5676
5677 if Present (Priv) then
5678
5679 -- Check for circular definitions
5680
5681 if Priv = Any_Type then
5682 Set_Component_Type (Etype (T), Any_Type);
5683
5684 -- There is a gap in the visibility of operations on the composite
5685 -- type only if the component type is defined in a different scope.
5686
5687 elsif Scope (Priv) = Current_Scope then
5688 null;
5689
5690 elsif Is_Limited_Type (Priv) then
5691 Set_Is_Limited_Composite (Etype (T));
5692 Set_Is_Limited_Composite (T);
5693 else
5694 Set_Is_Private_Composite (Etype (T));
5695 Set_Is_Private_Composite (T);
5696 end if;
5697 end if;
5698
5699 -- A syntax error in the declaration itself may lead to an empty index
5700 -- list, in which case do a minimal patch.
5701
5702 if No (First_Index (T)) then
5703 Error_Msg_N ("missing index definition in array type declaration", T);
5704
5705 declare
5706 Indexes : constant List_Id :=
5707 New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
5708 begin
5709 Set_Discrete_Subtype_Definitions (Def, Indexes);
5710 Set_First_Index (T, First (Indexes));
5711 return;
5712 end;
5713 end if;
5714
5715 -- Create a concatenation operator for the new type. Internal array
5716 -- types created for packed entities do not need such, they are
5717 -- compatible with the user-defined type.
5718
5719 if Number_Dimensions (T) = 1
5720 and then not Is_Packed_Array_Impl_Type (T)
5721 then
5722 New_Concatenation_Op (T);
5723 end if;
5724
5725 -- In the case of an unconstrained array the parser has already verified
5726 -- that all the indexes are unconstrained but we still need to make sure
5727 -- that the element type is constrained.
5728
5729 if Is_Indefinite_Subtype (Element_Type) then
5730 Error_Msg_N
5731 ("unconstrained element type in array declaration",
5732 Subtype_Indication (Component_Def));
5733
5734 elsif Is_Abstract_Type (Element_Type) then
5735 Error_Msg_N
5736 ("the type of a component cannot be abstract",
5737 Subtype_Indication (Component_Def));
5738 end if;
5739
5740 -- There may be an invariant declared for the component type, but
5741 -- the construction of the component invariant checking procedure
5742 -- takes place during expansion.
5743 end Array_Type_Declaration;
5744
5745 ------------------------------------------------------
5746 -- Replace_Anonymous_Access_To_Protected_Subprogram --
5747 ------------------------------------------------------
5748
5749 function Replace_Anonymous_Access_To_Protected_Subprogram
5750 (N : Node_Id) return Entity_Id
5751 is
5752 Loc : constant Source_Ptr := Sloc (N);
5753
5754 Curr_Scope : constant Scope_Stack_Entry :=
5755 Scope_Stack.Table (Scope_Stack.Last);
5756
5757 Anon : constant Entity_Id := Make_Temporary (Loc, 'S');
5758
5759 Acc : Node_Id;
5760 -- Access definition in declaration
5761
5762 Comp : Node_Id;
5763 -- Object definition or formal definition with an access definition
5764
5765 Decl : Node_Id;
5766 -- Declaration of anonymous access to subprogram type
5767
5768 Spec : Node_Id;
5769 -- Original specification in access to subprogram
5770
5771 P : Node_Id;
5772
5773 begin
5774 Set_Is_Internal (Anon);
5775
5776 case Nkind (N) is
5777 when N_Component_Declaration |
5778 N_Unconstrained_Array_Definition |
5779 N_Constrained_Array_Definition =>
5780 Comp := Component_Definition (N);
5781 Acc := Access_Definition (Comp);
5782
5783 when N_Discriminant_Specification =>
5784 Comp := Discriminant_Type (N);
5785 Acc := Comp;
5786
5787 when N_Parameter_Specification =>
5788 Comp := Parameter_Type (N);
5789 Acc := Comp;
5790
5791 when N_Access_Function_Definition =>
5792 Comp := Result_Definition (N);
5793 Acc := Comp;
5794
5795 when N_Object_Declaration =>
5796 Comp := Object_Definition (N);
5797 Acc := Comp;
5798
5799 when N_Function_Specification =>
5800 Comp := Result_Definition (N);
5801 Acc := Comp;
5802
5803 when others =>
5804 raise Program_Error;
5805 end case;
5806
5807 Spec := Access_To_Subprogram_Definition (Acc);
5808
5809 Decl :=
5810 Make_Full_Type_Declaration (Loc,
5811 Defining_Identifier => Anon,
5812 Type_Definition => Copy_Separate_Tree (Spec));
5813
5814 Mark_Rewrite_Insertion (Decl);
5815
5816 -- In ASIS mode, analyze the profile on the original node, because
5817 -- the separate copy does not provide enough links to recover the
5818 -- original tree. Analysis is limited to type annotations, within
5819 -- a temporary scope that serves as an anonymous subprogram to collect
5820 -- otherwise useless temporaries and itypes.
5821
5822 if ASIS_Mode then
5823 declare
5824 Typ : constant Entity_Id := Make_Temporary (Loc, 'S');
5825
5826 begin
5827 if Nkind (Spec) = N_Access_Function_Definition then
5828 Set_Ekind (Typ, E_Function);
5829 else
5830 Set_Ekind (Typ, E_Procedure);
5831 end if;
5832
5833 Set_Parent (Typ, N);
5834 Set_Scope (Typ, Current_Scope);
5835 Push_Scope (Typ);
5836
5837 -- Nothing to do if procedure is parameterless
5838
5839 if Present (Parameter_Specifications (Spec)) then
5840 Process_Formals (Parameter_Specifications (Spec), Spec);
5841 end if;
5842
5843 if Nkind (Spec) = N_Access_Function_Definition then
5844 declare
5845 Def : constant Node_Id := Result_Definition (Spec);
5846
5847 begin
5848 -- The result might itself be an anonymous access type, so
5849 -- have to recurse.
5850
5851 if Nkind (Def) = N_Access_Definition then
5852 if Present (Access_To_Subprogram_Definition (Def)) then
5853 Set_Etype
5854 (Def,
5855 Replace_Anonymous_Access_To_Protected_Subprogram
5856 (Spec));
5857 else
5858 Find_Type (Subtype_Mark (Def));
5859 end if;
5860
5861 else
5862 Find_Type (Def);
5863 end if;
5864 end;
5865 end if;
5866
5867 End_Scope;
5868 end;
5869 end if;
5870
5871 -- Insert the new declaration in the nearest enclosing scope. If the
5872 -- node is a body and N is its return type, the declaration belongs in
5873 -- the enclosing scope.
5874
5875 P := Parent (N);
5876
5877 if Nkind (P) = N_Subprogram_Body
5878 and then Nkind (N) = N_Function_Specification
5879 then
5880 P := Parent (P);
5881 end if;
5882
5883 while Present (P) and then not Has_Declarations (P) loop
5884 P := Parent (P);
5885 end loop;
5886
5887 pragma Assert (Present (P));
5888
5889 if Nkind (P) = N_Package_Specification then
5890 Prepend (Decl, Visible_Declarations (P));
5891 else
5892 Prepend (Decl, Declarations (P));
5893 end if;
5894
5895 -- Replace the anonymous type with an occurrence of the new declaration.
5896 -- In all cases the rewritten node does not have the null-exclusion
5897 -- attribute because (if present) it was already inherited by the
5898 -- anonymous entity (Anon). Thus, in case of components we do not
5899 -- inherit this attribute.
5900
5901 if Nkind (N) = N_Parameter_Specification then
5902 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5903 Set_Etype (Defining_Identifier (N), Anon);
5904 Set_Null_Exclusion_Present (N, False);
5905
5906 elsif Nkind (N) = N_Object_Declaration then
5907 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5908 Set_Etype (Defining_Identifier (N), Anon);
5909
5910 elsif Nkind (N) = N_Access_Function_Definition then
5911 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5912
5913 elsif Nkind (N) = N_Function_Specification then
5914 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5915 Set_Etype (Defining_Unit_Name (N), Anon);
5916
5917 else
5918 Rewrite (Comp,
5919 Make_Component_Definition (Loc,
5920 Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
5921 end if;
5922
5923 Mark_Rewrite_Insertion (Comp);
5924
5925 if Nkind_In (N, N_Object_Declaration, N_Access_Function_Definition) then
5926 Analyze (Decl);
5927
5928 else
5929 -- Temporarily remove the current scope (record or subprogram) from
5930 -- the stack to add the new declarations to the enclosing scope.
5931
5932 Scope_Stack.Decrement_Last;
5933 Analyze (Decl);
5934 Set_Is_Itype (Anon);
5935 Scope_Stack.Append (Curr_Scope);
5936 end if;
5937
5938 Set_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
5939 Set_Can_Use_Internal_Rep (Anon, not Always_Compatible_Rep_On_Target);
5940 return Anon;
5941 end Replace_Anonymous_Access_To_Protected_Subprogram;
5942
5943 -------------------------------
5944 -- Build_Derived_Access_Type --
5945 -------------------------------
5946
5947 procedure Build_Derived_Access_Type
5948 (N : Node_Id;
5949 Parent_Type : Entity_Id;
5950 Derived_Type : Entity_Id)
5951 is
5952 S : constant Node_Id := Subtype_Indication (Type_Definition (N));
5953
5954 Desig_Type : Entity_Id;
5955 Discr : Entity_Id;
5956 Discr_Con_Elist : Elist_Id;
5957 Discr_Con_El : Elmt_Id;
5958 Subt : Entity_Id;
5959
5960 begin
5961 -- Set the designated type so it is available in case this is an access
5962 -- to a self-referential type, e.g. a standard list type with a next
5963 -- pointer. Will be reset after subtype is built.
5964
5965 Set_Directly_Designated_Type
5966 (Derived_Type, Designated_Type (Parent_Type));
5967
5968 Subt := Process_Subtype (S, N);
5969
5970 if Nkind (S) /= N_Subtype_Indication
5971 and then Subt /= Base_Type (Subt)
5972 then
5973 Set_Ekind (Derived_Type, E_Access_Subtype);
5974 end if;
5975
5976 if Ekind (Derived_Type) = E_Access_Subtype then
5977 declare
5978 Pbase : constant Entity_Id := Base_Type (Parent_Type);
5979 Ibase : constant Entity_Id :=
5980 Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
5981 Svg_Chars : constant Name_Id := Chars (Ibase);
5982 Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
5983
5984 begin
5985 Copy_Node (Pbase, Ibase);
5986
5987 Set_Chars (Ibase, Svg_Chars);
5988 Set_Next_Entity (Ibase, Svg_Next_E);
5989 Set_Sloc (Ibase, Sloc (Derived_Type));
5990 Set_Scope (Ibase, Scope (Derived_Type));
5991 Set_Freeze_Node (Ibase, Empty);
5992 Set_Is_Frozen (Ibase, False);
5993 Set_Comes_From_Source (Ibase, False);
5994 Set_Is_First_Subtype (Ibase, False);
5995
5996 Set_Etype (Ibase, Pbase);
5997 Set_Etype (Derived_Type, Ibase);
5998 end;
5999 end if;
6000
6001 Set_Directly_Designated_Type
6002 (Derived_Type, Designated_Type (Subt));
6003
6004 Set_Is_Constrained (Derived_Type, Is_Constrained (Subt));
6005 Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
6006 Set_Size_Info (Derived_Type, Parent_Type);
6007 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
6008 Set_Depends_On_Private (Derived_Type,
6009 Has_Private_Component (Derived_Type));
6010 Conditional_Delay (Derived_Type, Subt);
6011
6012 -- Ada 2005 (AI-231): Set the null-exclusion attribute, and verify
6013 -- that it is not redundant.
6014
6015 if Null_Exclusion_Present (Type_Definition (N)) then
6016 Set_Can_Never_Be_Null (Derived_Type);
6017
6018 -- What is with the "AND THEN FALSE" here ???
6019
6020 if Can_Never_Be_Null (Parent_Type)
6021 and then False
6022 then
6023 Error_Msg_NE
6024 ("`NOT NULL` not allowed (& already excludes null)",
6025 N, Parent_Type);
6026 end if;
6027
6028 elsif Can_Never_Be_Null (Parent_Type) then
6029 Set_Can_Never_Be_Null (Derived_Type);
6030 end if;
6031
6032 -- Note: we do not copy the Storage_Size_Variable, since we always go to
6033 -- the root type for this information.
6034
6035 -- Apply range checks to discriminants for derived record case
6036 -- ??? THIS CODE SHOULD NOT BE HERE REALLY.
6037
6038 Desig_Type := Designated_Type (Derived_Type);
6039 if Is_Composite_Type (Desig_Type)
6040 and then (not Is_Array_Type (Desig_Type))
6041 and then Has_Discriminants (Desig_Type)
6042 and then Base_Type (Desig_Type) /= Desig_Type
6043 then
6044 Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
6045 Discr_Con_El := First_Elmt (Discr_Con_Elist);
6046
6047 Discr := First_Discriminant (Base_Type (Desig_Type));
6048 while Present (Discr_Con_El) loop
6049 Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
6050 Next_Elmt (Discr_Con_El);
6051 Next_Discriminant (Discr);
6052 end loop;
6053 end if;
6054 end Build_Derived_Access_Type;
6055
6056 ------------------------------
6057 -- Build_Derived_Array_Type --
6058 ------------------------------
6059
6060 procedure Build_Derived_Array_Type
6061 (N : Node_Id;
6062 Parent_Type : Entity_Id;
6063 Derived_Type : Entity_Id)
6064 is
6065 Loc : constant Source_Ptr := Sloc (N);
6066 Tdef : constant Node_Id := Type_Definition (N);
6067 Indic : constant Node_Id := Subtype_Indication (Tdef);
6068 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
6069 Implicit_Base : Entity_Id;
6070 New_Indic : Node_Id;
6071
6072 procedure Make_Implicit_Base;
6073 -- If the parent subtype is constrained, the derived type is a subtype
6074 -- of an implicit base type derived from the parent base.
6075
6076 ------------------------
6077 -- Make_Implicit_Base --
6078 ------------------------
6079
6080 procedure Make_Implicit_Base is
6081 begin
6082 Implicit_Base :=
6083 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
6084
6085 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
6086 Set_Etype (Implicit_Base, Parent_Base);
6087
6088 Copy_Array_Subtype_Attributes (Implicit_Base, Parent_Base);
6089 Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
6090
6091 Set_Has_Delayed_Freeze (Implicit_Base, True);
6092
6093 -- Inherit the "ghostness" from the parent base type
6094
6095 if Is_Ghost_Entity (Parent_Base) or else Ghost_Mode > None then
6096 Set_Is_Ghost_Entity (Implicit_Base);
6097 end if;
6098 end Make_Implicit_Base;
6099
6100 -- Start of processing for Build_Derived_Array_Type
6101
6102 begin
6103 if not Is_Constrained (Parent_Type) then
6104 if Nkind (Indic) /= N_Subtype_Indication then
6105 Set_Ekind (Derived_Type, E_Array_Type);
6106
6107 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6108 Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
6109
6110 Set_Has_Delayed_Freeze (Derived_Type, True);
6111
6112 else
6113 Make_Implicit_Base;
6114 Set_Etype (Derived_Type, Implicit_Base);
6115
6116 New_Indic :=
6117 Make_Subtype_Declaration (Loc,
6118 Defining_Identifier => Derived_Type,
6119 Subtype_Indication =>
6120 Make_Subtype_Indication (Loc,
6121 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
6122 Constraint => Constraint (Indic)));
6123
6124 Rewrite (N, New_Indic);
6125 Analyze (N);
6126 end if;
6127
6128 else
6129 if Nkind (Indic) /= N_Subtype_Indication then
6130 Make_Implicit_Base;
6131
6132 Set_Ekind (Derived_Type, Ekind (Parent_Type));
6133 Set_Etype (Derived_Type, Implicit_Base);
6134 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6135
6136 else
6137 Error_Msg_N ("illegal constraint on constrained type", Indic);
6138 end if;
6139 end if;
6140
6141 -- If parent type is not a derived type itself, and is declared in
6142 -- closed scope (e.g. a subprogram), then we must explicitly introduce
6143 -- the new type's concatenation operator since Derive_Subprograms
6144 -- will not inherit the parent's operator. If the parent type is
6145 -- unconstrained, the operator is of the unconstrained base type.
6146
6147 if Number_Dimensions (Parent_Type) = 1
6148 and then not Is_Limited_Type (Parent_Type)
6149 and then not Is_Derived_Type (Parent_Type)
6150 and then not Is_Package_Or_Generic_Package
6151 (Scope (Base_Type (Parent_Type)))
6152 then
6153 if not Is_Constrained (Parent_Type)
6154 and then Is_Constrained (Derived_Type)
6155 then
6156 New_Concatenation_Op (Implicit_Base);
6157 else
6158 New_Concatenation_Op (Derived_Type);
6159 end if;
6160 end if;
6161 end Build_Derived_Array_Type;
6162
6163 -----------------------------------
6164 -- Build_Derived_Concurrent_Type --
6165 -----------------------------------
6166
6167 procedure Build_Derived_Concurrent_Type
6168 (N : Node_Id;
6169 Parent_Type : Entity_Id;
6170 Derived_Type : Entity_Id)
6171 is
6172 Loc : constant Source_Ptr := Sloc (N);
6173
6174 Corr_Record : constant Entity_Id := Make_Temporary (Loc, 'C');
6175 Corr_Decl : Node_Id;
6176 Corr_Decl_Needed : Boolean;
6177 -- If the derived type has fewer discriminants than its parent, the
6178 -- corresponding record is also a derived type, in order to account for
6179 -- the bound discriminants. We create a full type declaration for it in
6180 -- this case.
6181
6182 Constraint_Present : constant Boolean :=
6183 Nkind (Subtype_Indication (Type_Definition (N))) =
6184 N_Subtype_Indication;
6185
6186 D_Constraint : Node_Id;
6187 New_Constraint : Elist_Id;
6188 Old_Disc : Entity_Id;
6189 New_Disc : Entity_Id;
6190 New_N : Node_Id;
6191
6192 begin
6193 Set_Stored_Constraint (Derived_Type, No_Elist);
6194 Corr_Decl_Needed := False;
6195 Old_Disc := Empty;
6196
6197 if Present (Discriminant_Specifications (N))
6198 and then Constraint_Present
6199 then
6200 Old_Disc := First_Discriminant (Parent_Type);
6201 New_Disc := First (Discriminant_Specifications (N));
6202 while Present (New_Disc) and then Present (Old_Disc) loop
6203 Next_Discriminant (Old_Disc);
6204 Next (New_Disc);
6205 end loop;
6206 end if;
6207
6208 if Present (Old_Disc) and then Expander_Active then
6209
6210 -- The new type has fewer discriminants, so we need to create a new
6211 -- corresponding record, which is derived from the corresponding
6212 -- record of the parent, and has a stored constraint that captures
6213 -- the values of the discriminant constraints. The corresponding
6214 -- record is needed only if expander is active and code generation is
6215 -- enabled.
6216
6217 -- The type declaration for the derived corresponding record has the
6218 -- same discriminant part and constraints as the current declaration.
6219 -- Copy the unanalyzed tree to build declaration.
6220
6221 Corr_Decl_Needed := True;
6222 New_N := Copy_Separate_Tree (N);
6223
6224 Corr_Decl :=
6225 Make_Full_Type_Declaration (Loc,
6226 Defining_Identifier => Corr_Record,
6227 Discriminant_Specifications =>
6228 Discriminant_Specifications (New_N),
6229 Type_Definition =>
6230 Make_Derived_Type_Definition (Loc,
6231 Subtype_Indication =>
6232 Make_Subtype_Indication (Loc,
6233 Subtype_Mark =>
6234 New_Occurrence_Of
6235 (Corresponding_Record_Type (Parent_Type), Loc),
6236 Constraint =>
6237 Constraint
6238 (Subtype_Indication (Type_Definition (New_N))))));
6239 end if;
6240
6241 -- Copy Storage_Size and Relative_Deadline variables if task case
6242
6243 if Is_Task_Type (Parent_Type) then
6244 Set_Storage_Size_Variable (Derived_Type,
6245 Storage_Size_Variable (Parent_Type));
6246 Set_Relative_Deadline_Variable (Derived_Type,
6247 Relative_Deadline_Variable (Parent_Type));
6248 end if;
6249
6250 if Present (Discriminant_Specifications (N)) then
6251 Push_Scope (Derived_Type);
6252 Check_Or_Process_Discriminants (N, Derived_Type);
6253
6254 if Constraint_Present then
6255 New_Constraint :=
6256 Expand_To_Stored_Constraint
6257 (Parent_Type,
6258 Build_Discriminant_Constraints
6259 (Parent_Type,
6260 Subtype_Indication (Type_Definition (N)), True));
6261 end if;
6262
6263 End_Scope;
6264
6265 elsif Constraint_Present then
6266
6267 -- Build constrained subtype, copying the constraint, and derive
6268 -- from it to create a derived constrained type.
6269
6270 declare
6271 Loc : constant Source_Ptr := Sloc (N);
6272 Anon : constant Entity_Id :=
6273 Make_Defining_Identifier (Loc,
6274 Chars => New_External_Name (Chars (Derived_Type), 'T'));
6275 Decl : Node_Id;
6276
6277 begin
6278 Decl :=
6279 Make_Subtype_Declaration (Loc,
6280 Defining_Identifier => Anon,
6281 Subtype_Indication =>
6282 New_Copy_Tree (Subtype_Indication (Type_Definition (N))));
6283 Insert_Before (N, Decl);
6284 Analyze (Decl);
6285
6286 Rewrite (Subtype_Indication (Type_Definition (N)),
6287 New_Occurrence_Of (Anon, Loc));
6288 Set_Analyzed (Derived_Type, False);
6289 Analyze (N);
6290 return;
6291 end;
6292 end if;
6293
6294 -- By default, operations and private data are inherited from parent.
6295 -- However, in the presence of bound discriminants, a new corresponding
6296 -- record will be created, see below.
6297
6298 Set_Has_Discriminants
6299 (Derived_Type, Has_Discriminants (Parent_Type));
6300 Set_Corresponding_Record_Type
6301 (Derived_Type, Corresponding_Record_Type (Parent_Type));
6302
6303 -- Is_Constrained is set according the parent subtype, but is set to
6304 -- False if the derived type is declared with new discriminants.
6305
6306 Set_Is_Constrained
6307 (Derived_Type,
6308 (Is_Constrained (Parent_Type) or else Constraint_Present)
6309 and then not Present (Discriminant_Specifications (N)));
6310
6311 if Constraint_Present then
6312 if not Has_Discriminants (Parent_Type) then
6313 Error_Msg_N ("untagged parent must have discriminants", N);
6314
6315 elsif Present (Discriminant_Specifications (N)) then
6316
6317 -- Verify that new discriminants are used to constrain old ones
6318
6319 D_Constraint :=
6320 First
6321 (Constraints
6322 (Constraint (Subtype_Indication (Type_Definition (N)))));
6323
6324 Old_Disc := First_Discriminant (Parent_Type);
6325
6326 while Present (D_Constraint) loop
6327 if Nkind (D_Constraint) /= N_Discriminant_Association then
6328
6329 -- Positional constraint. If it is a reference to a new
6330 -- discriminant, it constrains the corresponding old one.
6331
6332 if Nkind (D_Constraint) = N_Identifier then
6333 New_Disc := First_Discriminant (Derived_Type);
6334 while Present (New_Disc) loop
6335 exit when Chars (New_Disc) = Chars (D_Constraint);
6336 Next_Discriminant (New_Disc);
6337 end loop;
6338
6339 if Present (New_Disc) then
6340 Set_Corresponding_Discriminant (New_Disc, Old_Disc);
6341 end if;
6342 end if;
6343
6344 Next_Discriminant (Old_Disc);
6345
6346 -- if this is a named constraint, search by name for the old
6347 -- discriminants constrained by the new one.
6348
6349 elsif Nkind (Expression (D_Constraint)) = N_Identifier then
6350
6351 -- Find new discriminant with that name
6352
6353 New_Disc := First_Discriminant (Derived_Type);
6354 while Present (New_Disc) loop
6355 exit when
6356 Chars (New_Disc) = Chars (Expression (D_Constraint));
6357 Next_Discriminant (New_Disc);
6358 end loop;
6359
6360 if Present (New_Disc) then
6361
6362 -- Verify that new discriminant renames some discriminant
6363 -- of the parent type, and associate the new discriminant
6364 -- with one or more old ones that it renames.
6365
6366 declare
6367 Selector : Node_Id;
6368
6369 begin
6370 Selector := First (Selector_Names (D_Constraint));
6371 while Present (Selector) loop
6372 Old_Disc := First_Discriminant (Parent_Type);
6373 while Present (Old_Disc) loop
6374 exit when Chars (Old_Disc) = Chars (Selector);
6375 Next_Discriminant (Old_Disc);
6376 end loop;
6377
6378 if Present (Old_Disc) then
6379 Set_Corresponding_Discriminant
6380 (New_Disc, Old_Disc);
6381 end if;
6382
6383 Next (Selector);
6384 end loop;
6385 end;
6386 end if;
6387 end if;
6388
6389 Next (D_Constraint);
6390 end loop;
6391
6392 New_Disc := First_Discriminant (Derived_Type);
6393 while Present (New_Disc) loop
6394 if No (Corresponding_Discriminant (New_Disc)) then
6395 Error_Msg_NE
6396 ("new discriminant& must constrain old one", N, New_Disc);
6397
6398 elsif not
6399 Subtypes_Statically_Compatible
6400 (Etype (New_Disc),
6401 Etype (Corresponding_Discriminant (New_Disc)))
6402 then
6403 Error_Msg_NE
6404 ("& not statically compatible with parent discriminant",
6405 N, New_Disc);
6406 end if;
6407
6408 Next_Discriminant (New_Disc);
6409 end loop;
6410 end if;
6411
6412 elsif Present (Discriminant_Specifications (N)) then
6413 Error_Msg_N
6414 ("missing discriminant constraint in untagged derivation", N);
6415 end if;
6416
6417 -- The entity chain of the derived type includes the new discriminants
6418 -- but shares operations with the parent.
6419
6420 if Present (Discriminant_Specifications (N)) then
6421 Old_Disc := First_Discriminant (Parent_Type);
6422 while Present (Old_Disc) loop
6423 if No (Next_Entity (Old_Disc))
6424 or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
6425 then
6426 Set_Next_Entity
6427 (Last_Entity (Derived_Type), Next_Entity (Old_Disc));
6428 exit;
6429 end if;
6430
6431 Next_Discriminant (Old_Disc);
6432 end loop;
6433
6434 else
6435 Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
6436 if Has_Discriminants (Parent_Type) then
6437 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
6438 Set_Discriminant_Constraint (
6439 Derived_Type, Discriminant_Constraint (Parent_Type));
6440 end if;
6441 end if;
6442
6443 Set_Last_Entity (Derived_Type, Last_Entity (Parent_Type));
6444
6445 Set_Has_Completion (Derived_Type);
6446
6447 if Corr_Decl_Needed then
6448 Set_Stored_Constraint (Derived_Type, New_Constraint);
6449 Insert_After (N, Corr_Decl);
6450 Analyze (Corr_Decl);
6451 Set_Corresponding_Record_Type (Derived_Type, Corr_Record);
6452 end if;
6453 end Build_Derived_Concurrent_Type;
6454
6455 ------------------------------------
6456 -- Build_Derived_Enumeration_Type --
6457 ------------------------------------
6458
6459 procedure Build_Derived_Enumeration_Type
6460 (N : Node_Id;
6461 Parent_Type : Entity_Id;
6462 Derived_Type : Entity_Id)
6463 is
6464 Loc : constant Source_Ptr := Sloc (N);
6465 Def : constant Node_Id := Type_Definition (N);
6466 Indic : constant Node_Id := Subtype_Indication (Def);
6467 Implicit_Base : Entity_Id;
6468 Literal : Entity_Id;
6469 New_Lit : Entity_Id;
6470 Literals_List : List_Id;
6471 Type_Decl : Node_Id;
6472 Hi, Lo : Node_Id;
6473 Rang_Expr : Node_Id;
6474
6475 begin
6476 -- Since types Standard.Character and Standard.[Wide_]Wide_Character do
6477 -- not have explicit literals lists we need to process types derived
6478 -- from them specially. This is handled by Derived_Standard_Character.
6479 -- If the parent type is a generic type, there are no literals either,
6480 -- and we construct the same skeletal representation as for the generic
6481 -- parent type.
6482
6483 if Is_Standard_Character_Type (Parent_Type) then
6484 Derived_Standard_Character (N, Parent_Type, Derived_Type);
6485
6486 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
6487 declare
6488 Lo : Node_Id;
6489 Hi : Node_Id;
6490
6491 begin
6492 if Nkind (Indic) /= N_Subtype_Indication then
6493 Lo :=
6494 Make_Attribute_Reference (Loc,
6495 Attribute_Name => Name_First,
6496 Prefix => New_Occurrence_Of (Derived_Type, Loc));
6497 Set_Etype (Lo, Derived_Type);
6498
6499 Hi :=
6500 Make_Attribute_Reference (Loc,
6501 Attribute_Name => Name_Last,
6502 Prefix => New_Occurrence_Of (Derived_Type, Loc));
6503 Set_Etype (Hi, Derived_Type);
6504
6505 Set_Scalar_Range (Derived_Type,
6506 Make_Range (Loc,
6507 Low_Bound => Lo,
6508 High_Bound => Hi));
6509 else
6510
6511 -- Analyze subtype indication and verify compatibility
6512 -- with parent type.
6513
6514 if Base_Type (Process_Subtype (Indic, N)) /=
6515 Base_Type (Parent_Type)
6516 then
6517 Error_Msg_N
6518 ("illegal constraint for formal discrete type", N);
6519 end if;
6520 end if;
6521 end;
6522
6523 else
6524 -- If a constraint is present, analyze the bounds to catch
6525 -- premature usage of the derived literals.
6526
6527 if Nkind (Indic) = N_Subtype_Indication
6528 and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
6529 then
6530 Analyze (Low_Bound (Range_Expression (Constraint (Indic))));
6531 Analyze (High_Bound (Range_Expression (Constraint (Indic))));
6532 end if;
6533
6534 -- Introduce an implicit base type for the derived type even if there
6535 -- is no constraint attached to it, since this seems closer to the
6536 -- Ada semantics. Build a full type declaration tree for the derived
6537 -- type using the implicit base type as the defining identifier. The
6538 -- build a subtype declaration tree which applies the constraint (if
6539 -- any) have it replace the derived type declaration.
6540
6541 Literal := First_Literal (Parent_Type);
6542 Literals_List := New_List;
6543 while Present (Literal)
6544 and then Ekind (Literal) = E_Enumeration_Literal
6545 loop
6546 -- Literals of the derived type have the same representation as
6547 -- those of the parent type, but this representation can be
6548 -- overridden by an explicit representation clause. Indicate
6549 -- that there is no explicit representation given yet. These
6550 -- derived literals are implicit operations of the new type,
6551 -- and can be overridden by explicit ones.
6552
6553 if Nkind (Literal) = N_Defining_Character_Literal then
6554 New_Lit :=
6555 Make_Defining_Character_Literal (Loc, Chars (Literal));
6556 else
6557 New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
6558 end if;
6559
6560 Set_Ekind (New_Lit, E_Enumeration_Literal);
6561 Set_Enumeration_Pos (New_Lit, Enumeration_Pos (Literal));
6562 Set_Enumeration_Rep (New_Lit, Enumeration_Rep (Literal));
6563 Set_Enumeration_Rep_Expr (New_Lit, Empty);
6564 Set_Alias (New_Lit, Literal);
6565 Set_Is_Known_Valid (New_Lit, True);
6566
6567 Append (New_Lit, Literals_List);
6568 Next_Literal (Literal);
6569 end loop;
6570
6571 Implicit_Base :=
6572 Make_Defining_Identifier (Sloc (Derived_Type),
6573 Chars => New_External_Name (Chars (Derived_Type), 'B'));
6574
6575 -- Indicate the proper nature of the derived type. This must be done
6576 -- before analysis of the literals, to recognize cases when a literal
6577 -- may be hidden by a previous explicit function definition (cf.
6578 -- c83031a).
6579
6580 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
6581 Set_Etype (Derived_Type, Implicit_Base);
6582
6583 Type_Decl :=
6584 Make_Full_Type_Declaration (Loc,
6585 Defining_Identifier => Implicit_Base,
6586 Discriminant_Specifications => No_List,
6587 Type_Definition =>
6588 Make_Enumeration_Type_Definition (Loc, Literals_List));
6589
6590 Mark_Rewrite_Insertion (Type_Decl);
6591 Insert_Before (N, Type_Decl);
6592 Analyze (Type_Decl);
6593
6594 -- The anonymous base now has a full declaration, but this base
6595 -- is not a first subtype.
6596
6597 Set_Is_First_Subtype (Implicit_Base, False);
6598
6599 -- After the implicit base is analyzed its Etype needs to be changed
6600 -- to reflect the fact that it is derived from the parent type which
6601 -- was ignored during analysis. We also set the size at this point.
6602
6603 Set_Etype (Implicit_Base, Parent_Type);
6604
6605 Set_Size_Info (Implicit_Base, Parent_Type);
6606 Set_RM_Size (Implicit_Base, RM_Size (Parent_Type));
6607 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
6608
6609 -- Copy other flags from parent type
6610
6611 Set_Has_Non_Standard_Rep
6612 (Implicit_Base, Has_Non_Standard_Rep
6613 (Parent_Type));
6614 Set_Has_Pragma_Ordered
6615 (Implicit_Base, Has_Pragma_Ordered
6616 (Parent_Type));
6617 Set_Has_Delayed_Freeze (Implicit_Base);
6618
6619 -- Process the subtype indication including a validation check on the
6620 -- constraint, if any. If a constraint is given, its bounds must be
6621 -- implicitly converted to the new type.
6622
6623 if Nkind (Indic) = N_Subtype_Indication then
6624 declare
6625 R : constant Node_Id :=
6626 Range_Expression (Constraint (Indic));
6627
6628 begin
6629 if Nkind (R) = N_Range then
6630 Hi := Build_Scalar_Bound
6631 (High_Bound (R), Parent_Type, Implicit_Base);
6632 Lo := Build_Scalar_Bound
6633 (Low_Bound (R), Parent_Type, Implicit_Base);
6634
6635 else
6636 -- Constraint is a Range attribute. Replace with explicit
6637 -- mention of the bounds of the prefix, which must be a
6638 -- subtype.
6639
6640 Analyze (Prefix (R));
6641 Hi :=
6642 Convert_To (Implicit_Base,
6643 Make_Attribute_Reference (Loc,
6644 Attribute_Name => Name_Last,
6645 Prefix =>
6646 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
6647
6648 Lo :=
6649 Convert_To (Implicit_Base,
6650 Make_Attribute_Reference (Loc,
6651 Attribute_Name => Name_First,
6652 Prefix =>
6653 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
6654 end if;
6655 end;
6656
6657 else
6658 Hi :=
6659 Build_Scalar_Bound
6660 (Type_High_Bound (Parent_Type),
6661 Parent_Type, Implicit_Base);
6662 Lo :=
6663 Build_Scalar_Bound
6664 (Type_Low_Bound (Parent_Type),
6665 Parent_Type, Implicit_Base);
6666 end if;
6667
6668 Rang_Expr :=
6669 Make_Range (Loc,
6670 Low_Bound => Lo,
6671 High_Bound => Hi);
6672
6673 -- If we constructed a default range for the case where no range
6674 -- was given, then the expressions in the range must not freeze
6675 -- since they do not correspond to expressions in the source.
6676
6677 if Nkind (Indic) /= N_Subtype_Indication then
6678 Set_Must_Not_Freeze (Lo);
6679 Set_Must_Not_Freeze (Hi);
6680 Set_Must_Not_Freeze (Rang_Expr);
6681 end if;
6682
6683 Rewrite (N,
6684 Make_Subtype_Declaration (Loc,
6685 Defining_Identifier => Derived_Type,
6686 Subtype_Indication =>
6687 Make_Subtype_Indication (Loc,
6688 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
6689 Constraint =>
6690 Make_Range_Constraint (Loc,
6691 Range_Expression => Rang_Expr))));
6692
6693 Analyze (N);
6694
6695 -- Propagate the aspects from the original type declaration to the
6696 -- declaration of the implicit base.
6697
6698 Move_Aspects (From => Original_Node (N), To => Type_Decl);
6699
6700 -- Apply a range check. Since this range expression doesn't have an
6701 -- Etype, we have to specifically pass the Source_Typ parameter. Is
6702 -- this right???
6703
6704 if Nkind (Indic) = N_Subtype_Indication then
6705 Apply_Range_Check
6706 (Range_Expression (Constraint (Indic)), Parent_Type,
6707 Source_Typ => Entity (Subtype_Mark (Indic)));
6708 end if;
6709 end if;
6710 end Build_Derived_Enumeration_Type;
6711
6712 --------------------------------
6713 -- Build_Derived_Numeric_Type --
6714 --------------------------------
6715
6716 procedure Build_Derived_Numeric_Type
6717 (N : Node_Id;
6718 Parent_Type : Entity_Id;
6719 Derived_Type : Entity_Id)
6720 is
6721 Loc : constant Source_Ptr := Sloc (N);
6722 Tdef : constant Node_Id := Type_Definition (N);
6723 Indic : constant Node_Id := Subtype_Indication (Tdef);
6724 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
6725 No_Constraint : constant Boolean := Nkind (Indic) /=
6726 N_Subtype_Indication;
6727 Implicit_Base : Entity_Id;
6728
6729 Lo : Node_Id;
6730 Hi : Node_Id;
6731
6732 begin
6733 -- Process the subtype indication including a validation check on
6734 -- the constraint if any.
6735
6736 Discard_Node (Process_Subtype (Indic, N));
6737
6738 -- Introduce an implicit base type for the derived type even if there
6739 -- is no constraint attached to it, since this seems closer to the Ada
6740 -- semantics.
6741
6742 Implicit_Base :=
6743 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
6744
6745 Set_Etype (Implicit_Base, Parent_Base);
6746 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
6747 Set_Size_Info (Implicit_Base, Parent_Base);
6748 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
6749 Set_Parent (Implicit_Base, Parent (Derived_Type));
6750 Set_Is_Known_Valid (Implicit_Base, Is_Known_Valid (Parent_Base));
6751
6752 -- Set RM Size for discrete type or decimal fixed-point type
6753 -- Ordinary fixed-point is excluded, why???
6754
6755 if Is_Discrete_Type (Parent_Base)
6756 or else Is_Decimal_Fixed_Point_Type (Parent_Base)
6757 then
6758 Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
6759 end if;
6760
6761 Set_Has_Delayed_Freeze (Implicit_Base);
6762
6763 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
6764 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
6765
6766 Set_Scalar_Range (Implicit_Base,
6767 Make_Range (Loc,
6768 Low_Bound => Lo,
6769 High_Bound => Hi));
6770
6771 if Has_Infinities (Parent_Base) then
6772 Set_Includes_Infinities (Scalar_Range (Implicit_Base));
6773 end if;
6774
6775 -- The Derived_Type, which is the entity of the declaration, is a
6776 -- subtype of the implicit base. Its Ekind is a subtype, even in the
6777 -- absence of an explicit constraint.
6778
6779 Set_Etype (Derived_Type, Implicit_Base);
6780
6781 -- If we did not have a constraint, then the Ekind is set from the
6782 -- parent type (otherwise Process_Subtype has set the bounds)
6783
6784 if No_Constraint then
6785 Set_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
6786 end if;
6787
6788 -- If we did not have a range constraint, then set the range from the
6789 -- parent type. Otherwise, the Process_Subtype call has set the bounds.
6790
6791 if No_Constraint or else not Has_Range_Constraint (Indic) then
6792 Set_Scalar_Range (Derived_Type,
6793 Make_Range (Loc,
6794 Low_Bound => New_Copy_Tree (Type_Low_Bound (Parent_Type)),
6795 High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
6796 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
6797
6798 if Has_Infinities (Parent_Type) then
6799 Set_Includes_Infinities (Scalar_Range (Derived_Type));
6800 end if;
6801
6802 Set_Is_Known_Valid (Derived_Type, Is_Known_Valid (Parent_Type));
6803 end if;
6804
6805 Set_Is_Descendent_Of_Address (Derived_Type,
6806 Is_Descendent_Of_Address (Parent_Type));
6807 Set_Is_Descendent_Of_Address (Implicit_Base,
6808 Is_Descendent_Of_Address (Parent_Type));
6809
6810 -- Set remaining type-specific fields, depending on numeric type
6811
6812 if Is_Modular_Integer_Type (Parent_Type) then
6813 Set_Modulus (Implicit_Base, Modulus (Parent_Base));
6814
6815 Set_Non_Binary_Modulus
6816 (Implicit_Base, Non_Binary_Modulus (Parent_Base));
6817
6818 Set_Is_Known_Valid
6819 (Implicit_Base, Is_Known_Valid (Parent_Base));
6820
6821 elsif Is_Floating_Point_Type (Parent_Type) then
6822
6823 -- Digits of base type is always copied from the digits value of
6824 -- the parent base type, but the digits of the derived type will
6825 -- already have been set if there was a constraint present.
6826
6827 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
6828 Set_Float_Rep (Implicit_Base, Float_Rep (Parent_Base));
6829
6830 if No_Constraint then
6831 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
6832 end if;
6833
6834 elsif Is_Fixed_Point_Type (Parent_Type) then
6835
6836 -- Small of base type and derived type are always copied from the
6837 -- parent base type, since smalls never change. The delta of the
6838 -- base type is also copied from the parent base type. However the
6839 -- delta of the derived type will have been set already if a
6840 -- constraint was present.
6841
6842 Set_Small_Value (Derived_Type, Small_Value (Parent_Base));
6843 Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
6844 Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
6845
6846 if No_Constraint then
6847 Set_Delta_Value (Derived_Type, Delta_Value (Parent_Type));
6848 end if;
6849
6850 -- The scale and machine radix in the decimal case are always
6851 -- copied from the parent base type.
6852
6853 if Is_Decimal_Fixed_Point_Type (Parent_Type) then
6854 Set_Scale_Value (Derived_Type, Scale_Value (Parent_Base));
6855 Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
6856
6857 Set_Machine_Radix_10
6858 (Derived_Type, Machine_Radix_10 (Parent_Base));
6859 Set_Machine_Radix_10
6860 (Implicit_Base, Machine_Radix_10 (Parent_Base));
6861
6862 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
6863
6864 if No_Constraint then
6865 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
6866
6867 else
6868 -- the analysis of the subtype_indication sets the
6869 -- digits value of the derived type.
6870
6871 null;
6872 end if;
6873 end if;
6874 end if;
6875
6876 if Is_Integer_Type (Parent_Type) then
6877 Set_Has_Shift_Operator
6878 (Implicit_Base, Has_Shift_Operator (Parent_Type));
6879 end if;
6880
6881 -- The type of the bounds is that of the parent type, and they
6882 -- must be converted to the derived type.
6883
6884 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
6885
6886 -- The implicit_base should be frozen when the derived type is frozen,
6887 -- but note that it is used in the conversions of the bounds. For fixed
6888 -- types we delay the determination of the bounds until the proper
6889 -- freezing point. For other numeric types this is rejected by GCC, for
6890 -- reasons that are currently unclear (???), so we choose to freeze the
6891 -- implicit base now. In the case of integers and floating point types
6892 -- this is harmless because subsequent representation clauses cannot
6893 -- affect anything, but it is still baffling that we cannot use the
6894 -- same mechanism for all derived numeric types.
6895
6896 -- There is a further complication: actually some representation
6897 -- clauses can affect the implicit base type. For example, attribute
6898 -- definition clauses for stream-oriented attributes need to set the
6899 -- corresponding TSS entries on the base type, and this normally
6900 -- cannot be done after the base type is frozen, so the circuitry in
6901 -- Sem_Ch13.New_Stream_Subprogram must account for this possibility
6902 -- and not use Set_TSS in this case.
6903
6904 -- There are also consequences for the case of delayed representation
6905 -- aspects for some cases. For example, a Size aspect is delayed and
6906 -- should not be evaluated to the freeze point. This early freezing
6907 -- means that the size attribute evaluation happens too early???
6908
6909 if Is_Fixed_Point_Type (Parent_Type) then
6910 Conditional_Delay (Implicit_Base, Parent_Type);
6911 else
6912 Freeze_Before (N, Implicit_Base);
6913 end if;
6914 end Build_Derived_Numeric_Type;
6915
6916 --------------------------------
6917 -- Build_Derived_Private_Type --
6918 --------------------------------
6919
6920 procedure Build_Derived_Private_Type
6921 (N : Node_Id;
6922 Parent_Type : Entity_Id;
6923 Derived_Type : Entity_Id;
6924 Is_Completion : Boolean;
6925 Derive_Subps : Boolean := True)
6926 is
6927 Loc : constant Source_Ptr := Sloc (N);
6928 Par_Base : constant Entity_Id := Base_Type (Parent_Type);
6929 Par_Scope : constant Entity_Id := Scope (Par_Base);
6930 Full_N : constant Node_Id := New_Copy_Tree (N);
6931 Full_Der : Entity_Id := New_Copy (Derived_Type);
6932 Full_P : Entity_Id;
6933
6934 procedure Build_Full_Derivation;
6935 -- Build full derivation, i.e. derive from the full view
6936
6937 procedure Copy_And_Build;
6938 -- Copy derived type declaration, replace parent with its full view,
6939 -- and build derivation
6940
6941 ---------------------------
6942 -- Build_Full_Derivation --
6943 ---------------------------
6944
6945 procedure Build_Full_Derivation is
6946 begin
6947 -- If parent scope is not open, install the declarations
6948
6949 if not In_Open_Scopes (Par_Scope) then
6950 Install_Private_Declarations (Par_Scope);
6951 Install_Visible_Declarations (Par_Scope);
6952 Copy_And_Build;
6953 Uninstall_Declarations (Par_Scope);
6954
6955 -- If parent scope is open and in another unit, and parent has a
6956 -- completion, then the derivation is taking place in the visible
6957 -- part of a child unit. In that case retrieve the full view of
6958 -- the parent momentarily.
6959
6960 elsif not In_Same_Source_Unit (N, Parent_Type) then
6961 Full_P := Full_View (Parent_Type);
6962 Exchange_Declarations (Parent_Type);
6963 Copy_And_Build;
6964 Exchange_Declarations (Full_P);
6965
6966 -- Otherwise it is a local derivation
6967
6968 else
6969 Copy_And_Build;
6970 end if;
6971 end Build_Full_Derivation;
6972
6973 --------------------
6974 -- Copy_And_Build --
6975 --------------------
6976
6977 procedure Copy_And_Build is
6978 Full_Parent : Entity_Id := Parent_Type;
6979
6980 begin
6981 -- If the parent is itself derived from another private type,
6982 -- installing the private declarations has not affected its
6983 -- privacy status, so use its own full view explicitly.
6984
6985 if Is_Private_Type (Full_Parent)
6986 and then Present (Full_View (Full_Parent))
6987 then
6988 Full_Parent := Full_View (Full_Parent);
6989 end if;
6990
6991 -- And its underlying full view if necessary
6992
6993 if Is_Private_Type (Full_Parent)
6994 and then Present (Underlying_Full_View (Full_Parent))
6995 then
6996 Full_Parent := Underlying_Full_View (Full_Parent);
6997 end if;
6998
6999 -- For record, access and most enumeration types, derivation from
7000 -- the full view requires a fully-fledged declaration. In the other
7001 -- cases, just use an itype.
7002
7003 if Ekind (Full_Parent) in Record_Kind
7004 or else Ekind (Full_Parent) in Access_Kind
7005 or else
7006 (Ekind (Full_Parent) in Enumeration_Kind
7007 and then not Is_Standard_Character_Type (Full_Parent)
7008 and then not Is_Generic_Type (Root_Type (Full_Parent)))
7009 then
7010 -- Copy and adjust declaration to provide a completion for what
7011 -- is originally a private declaration. Indicate that full view
7012 -- is internally generated.
7013
7014 Set_Comes_From_Source (Full_N, False);
7015 Set_Comes_From_Source (Full_Der, False);
7016 Set_Parent (Full_Der, Full_N);
7017 Set_Defining_Identifier (Full_N, Full_Der);
7018
7019 -- If there are no constraints, adjust the subtype mark
7020
7021 if Nkind (Subtype_Indication (Type_Definition (Full_N))) /=
7022 N_Subtype_Indication
7023 then
7024 Set_Subtype_Indication
7025 (Type_Definition (Full_N),
7026 New_Occurrence_Of (Full_Parent, Sloc (Full_N)));
7027 end if;
7028
7029 Insert_After (N, Full_N);
7030
7031 -- Build full view of derived type from full view of parent which
7032 -- is now installed. Subprograms have been derived on the partial
7033 -- view, the completion does not derive them anew.
7034
7035 if Ekind (Full_Parent) in Record_Kind then
7036
7037 -- If parent type is tagged, the completion inherits the proper
7038 -- primitive operations.
7039
7040 if Is_Tagged_Type (Parent_Type) then
7041 Build_Derived_Record_Type
7042 (Full_N, Full_Parent, Full_Der, Derive_Subps);
7043 else
7044 Build_Derived_Record_Type
7045 (Full_N, Full_Parent, Full_Der, Derive_Subps => False);
7046 end if;
7047
7048 else
7049 Build_Derived_Type
7050 (Full_N, Full_Parent, Full_Der,
7051 Is_Completion => False, Derive_Subps => False);
7052 end if;
7053
7054 -- The full declaration has been introduced into the tree and
7055 -- processed in the step above. It should not be analyzed again
7056 -- (when encountered later in the current list of declarations)
7057 -- to prevent spurious name conflicts. The full entity remains
7058 -- invisible.
7059
7060 Set_Analyzed (Full_N);
7061
7062 else
7063 Full_Der :=
7064 Make_Defining_Identifier (Sloc (Derived_Type),
7065 Chars => Chars (Derived_Type));
7066 Set_Is_Itype (Full_Der);
7067 Set_Associated_Node_For_Itype (Full_Der, N);
7068 Set_Parent (Full_Der, N);
7069 Build_Derived_Type
7070 (N, Full_Parent, Full_Der,
7071 Is_Completion => False, Derive_Subps => False);
7072 end if;
7073
7074 Set_Has_Private_Declaration (Full_Der);
7075 Set_Has_Private_Declaration (Derived_Type);
7076
7077 Set_Scope (Full_Der, Scope (Derived_Type));
7078 Set_Is_First_Subtype (Full_Der, Is_First_Subtype (Derived_Type));
7079 Set_Has_Size_Clause (Full_Der, False);
7080 Set_Has_Alignment_Clause (Full_Der, False);
7081 Set_Has_Delayed_Freeze (Full_Der);
7082 Set_Is_Frozen (Full_Der, False);
7083 Set_Freeze_Node (Full_Der, Empty);
7084 Set_Depends_On_Private (Full_Der, Has_Private_Component (Full_Der));
7085 Set_Is_Public (Full_Der, Is_Public (Derived_Type));
7086
7087 -- The convention on the base type may be set in the private part
7088 -- and not propagated to the subtype until later, so we obtain the
7089 -- convention from the base type of the parent.
7090
7091 Set_Convention (Full_Der, Convention (Base_Type (Full_Parent)));
7092 end Copy_And_Build;
7093
7094 -- Start of processing for Build_Derived_Private_Type
7095
7096 begin
7097 if Is_Tagged_Type (Parent_Type) then
7098 Full_P := Full_View (Parent_Type);
7099
7100 -- A type extension of a type with unknown discriminants is an
7101 -- indefinite type that the back-end cannot handle directly.
7102 -- We treat it as a private type, and build a completion that is
7103 -- derived from the full view of the parent, and hopefully has
7104 -- known discriminants.
7105
7106 -- If the full view of the parent type has an underlying record view,
7107 -- use it to generate the underlying record view of this derived type
7108 -- (required for chains of derivations with unknown discriminants).
7109
7110 -- Minor optimization: we avoid the generation of useless underlying
7111 -- record view entities if the private type declaration has unknown
7112 -- discriminants but its corresponding full view has no
7113 -- discriminants.
7114
7115 if Has_Unknown_Discriminants (Parent_Type)
7116 and then Present (Full_P)
7117 and then (Has_Discriminants (Full_P)
7118 or else Present (Underlying_Record_View (Full_P)))
7119 and then not In_Open_Scopes (Par_Scope)
7120 and then Expander_Active
7121 then
7122 declare
7123 Full_Der : constant Entity_Id := Make_Temporary (Loc, 'T');
7124 New_Ext : constant Node_Id :=
7125 Copy_Separate_Tree
7126 (Record_Extension_Part (Type_Definition (N)));
7127 Decl : Node_Id;
7128
7129 begin
7130 Build_Derived_Record_Type
7131 (N, Parent_Type, Derived_Type, Derive_Subps);
7132
7133 -- Build anonymous completion, as a derivation from the full
7134 -- view of the parent. This is not a completion in the usual
7135 -- sense, because the current type is not private.
7136
7137 Decl :=
7138 Make_Full_Type_Declaration (Loc,
7139 Defining_Identifier => Full_Der,
7140 Type_Definition =>
7141 Make_Derived_Type_Definition (Loc,
7142 Subtype_Indication =>
7143 New_Copy_Tree
7144 (Subtype_Indication (Type_Definition (N))),
7145 Record_Extension_Part => New_Ext));
7146
7147 -- If the parent type has an underlying record view, use it
7148 -- here to build the new underlying record view.
7149
7150 if Present (Underlying_Record_View (Full_P)) then
7151 pragma Assert
7152 (Nkind (Subtype_Indication (Type_Definition (Decl)))
7153 = N_Identifier);
7154 Set_Entity (Subtype_Indication (Type_Definition (Decl)),
7155 Underlying_Record_View (Full_P));
7156 end if;
7157
7158 Install_Private_Declarations (Par_Scope);
7159 Install_Visible_Declarations (Par_Scope);
7160 Insert_Before (N, Decl);
7161
7162 -- Mark entity as an underlying record view before analysis,
7163 -- to avoid generating the list of its primitive operations
7164 -- (which is not really required for this entity) and thus
7165 -- prevent spurious errors associated with missing overriding
7166 -- of abstract primitives (overridden only for Derived_Type).
7167
7168 Set_Ekind (Full_Der, E_Record_Type);
7169 Set_Is_Underlying_Record_View (Full_Der);
7170 Set_Default_SSO (Full_Der);
7171
7172 Analyze (Decl);
7173
7174 pragma Assert (Has_Discriminants (Full_Der)
7175 and then not Has_Unknown_Discriminants (Full_Der));
7176
7177 Uninstall_Declarations (Par_Scope);
7178
7179 -- Freeze the underlying record view, to prevent generation of
7180 -- useless dispatching information, which is simply shared with
7181 -- the real derived type.
7182
7183 Set_Is_Frozen (Full_Der);
7184
7185 -- If the derived type has access discriminants, create
7186 -- references to their anonymous types now, to prevent
7187 -- back-end problems when their first use is in generated
7188 -- bodies of primitives.
7189
7190 declare
7191 E : Entity_Id;
7192
7193 begin
7194 E := First_Entity (Full_Der);
7195
7196 while Present (E) loop
7197 if Ekind (E) = E_Discriminant
7198 and then Ekind (Etype (E)) = E_Anonymous_Access_Type
7199 then
7200 Build_Itype_Reference (Etype (E), Decl);
7201 end if;
7202
7203 Next_Entity (E);
7204 end loop;
7205 end;
7206
7207 -- Set up links between real entity and underlying record view
7208
7209 Set_Underlying_Record_View (Derived_Type, Base_Type (Full_Der));
7210 Set_Underlying_Record_View (Base_Type (Full_Der), Derived_Type);
7211 end;
7212
7213 -- If discriminants are known, build derived record
7214
7215 else
7216 Build_Derived_Record_Type
7217 (N, Parent_Type, Derived_Type, Derive_Subps);
7218 end if;
7219
7220 return;
7221
7222 elsif Has_Discriminants (Parent_Type) then
7223
7224 -- Build partial view of derived type from partial view of parent.
7225 -- This must be done before building the full derivation because the
7226 -- second derivation will modify the discriminants of the first and
7227 -- the discriminants are chained with the rest of the components in
7228 -- the full derivation.
7229
7230 Build_Derived_Record_Type
7231 (N, Parent_Type, Derived_Type, Derive_Subps);
7232
7233 -- Build the full derivation if this is not the anonymous derived
7234 -- base type created by Build_Derived_Record_Type in the constrained
7235 -- case (see point 5. of its head comment) since we build it for the
7236 -- derived subtype. And skip it for protected types altogether, as
7237 -- gigi does not use these types directly.
7238
7239 if Present (Full_View (Parent_Type))
7240 and then not Is_Itype (Derived_Type)
7241 and then not (Ekind (Full_View (Parent_Type)) in Protected_Kind)
7242 then
7243 declare
7244 Der_Base : constant Entity_Id := Base_Type (Derived_Type);
7245 Discr : Entity_Id;
7246 Last_Discr : Entity_Id;
7247
7248 begin
7249 -- If this is not a completion, construct the implicit full
7250 -- view by deriving from the full view of the parent type.
7251 -- But if this is a completion, the derived private type
7252 -- being built is a full view and the full derivation can
7253 -- only be its underlying full view.
7254
7255 Build_Full_Derivation;
7256
7257 if not Is_Completion then
7258 Set_Full_View (Derived_Type, Full_Der);
7259 else
7260 Set_Underlying_Full_View (Derived_Type, Full_Der);
7261 end if;
7262
7263 if not Is_Base_Type (Derived_Type) then
7264 Set_Full_View (Der_Base, Base_Type (Full_Der));
7265 end if;
7266
7267 -- Copy the discriminant list from full view to the partial
7268 -- view (base type and its subtype). Gigi requires that the
7269 -- partial and full views have the same discriminants.
7270
7271 -- Note that since the partial view points to discriminants
7272 -- in the full view, their scope will be that of the full
7273 -- view. This might cause some front end problems and need
7274 -- adjustment???
7275
7276 Discr := First_Discriminant (Base_Type (Full_Der));
7277 Set_First_Entity (Der_Base, Discr);
7278
7279 loop
7280 Last_Discr := Discr;
7281 Next_Discriminant (Discr);
7282 exit when No (Discr);
7283 end loop;
7284
7285 Set_Last_Entity (Der_Base, Last_Discr);
7286 Set_First_Entity (Derived_Type, First_Entity (Der_Base));
7287 Set_Last_Entity (Derived_Type, Last_Entity (Der_Base));
7288
7289 Set_Stored_Constraint
7290 (Full_Der, Stored_Constraint (Derived_Type));
7291 end;
7292 end if;
7293
7294 elsif Present (Full_View (Parent_Type))
7295 and then Has_Discriminants (Full_View (Parent_Type))
7296 then
7297 if Has_Unknown_Discriminants (Parent_Type)
7298 and then Nkind (Subtype_Indication (Type_Definition (N))) =
7299 N_Subtype_Indication
7300 then
7301 Error_Msg_N
7302 ("cannot constrain type with unknown discriminants",
7303 Subtype_Indication (Type_Definition (N)));
7304 return;
7305 end if;
7306
7307 -- If this is not a completion, construct the implicit full view by
7308 -- deriving from the full view of the parent type. But if this is a
7309 -- completion, the derived private type being built is a full view
7310 -- and the full derivation can only be its underlying full view.
7311
7312 Build_Full_Derivation;
7313
7314 if not Is_Completion then
7315 Set_Full_View (Derived_Type, Full_Der);
7316 else
7317 Set_Underlying_Full_View (Derived_Type, Full_Der);
7318 end if;
7319
7320 -- In any case, the primitive operations are inherited from the
7321 -- parent type, not from the internal full view.
7322
7323 Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
7324
7325 if Derive_Subps then
7326 Derive_Subprograms (Parent_Type, Derived_Type);
7327 end if;
7328
7329 Set_Stored_Constraint (Derived_Type, No_Elist);
7330 Set_Is_Constrained
7331 (Derived_Type, Is_Constrained (Full_View (Parent_Type)));
7332
7333 else
7334 -- Untagged type, No discriminants on either view
7335
7336 if Nkind (Subtype_Indication (Type_Definition (N))) =
7337 N_Subtype_Indication
7338 then
7339 Error_Msg_N
7340 ("illegal constraint on type without discriminants", N);
7341 end if;
7342
7343 if Present (Discriminant_Specifications (N))
7344 and then Present (Full_View (Parent_Type))
7345 and then not Is_Tagged_Type (Full_View (Parent_Type))
7346 then
7347 Error_Msg_N ("cannot add discriminants to untagged type", N);
7348 end if;
7349
7350 Set_Stored_Constraint (Derived_Type, No_Elist);
7351 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7352 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
7353 Set_Has_Controlled_Component
7354 (Derived_Type, Has_Controlled_Component
7355 (Parent_Type));
7356
7357 -- Direct controlled types do not inherit Finalize_Storage_Only flag
7358
7359 if not Is_Controlled (Parent_Type) then
7360 Set_Finalize_Storage_Only
7361 (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
7362 end if;
7363
7364 -- If this is not a completion, construct the implicit full view by
7365 -- deriving from the full view of the parent type.
7366
7367 -- ??? If the parent is untagged private and its completion is
7368 -- tagged, this mechanism will not work because we cannot derive from
7369 -- the tagged full view unless we have an extension.
7370
7371 if Present (Full_View (Parent_Type))
7372 and then not Is_Tagged_Type (Full_View (Parent_Type))
7373 and then not Is_Completion
7374 then
7375 Build_Full_Derivation;
7376 Set_Full_View (Derived_Type, Full_Der);
7377 end if;
7378 end if;
7379
7380 Set_Has_Unknown_Discriminants (Derived_Type,
7381 Has_Unknown_Discriminants (Parent_Type));
7382
7383 if Is_Private_Type (Derived_Type) then
7384 Set_Private_Dependents (Derived_Type, New_Elmt_List);
7385 end if;
7386
7387 -- If the parent base type is in scope, add the derived type to its
7388 -- list of private dependents, because its full view may become
7389 -- visible subsequently (in a nested private part, a body, or in a
7390 -- further child unit).
7391
7392 if Is_Private_Type (Par_Base) and then In_Open_Scopes (Par_Scope) then
7393 Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
7394
7395 -- Check for unusual case where a type completed by a private
7396 -- derivation occurs within a package nested in a child unit, and
7397 -- the parent is declared in an ancestor.
7398
7399 if Is_Child_Unit (Scope (Current_Scope))
7400 and then Is_Completion
7401 and then In_Private_Part (Current_Scope)
7402 and then Scope (Parent_Type) /= Current_Scope
7403
7404 -- Note that if the parent has a completion in the private part,
7405 -- (which is itself a derivation from some other private type)
7406 -- it is that completion that is visible, there is no full view
7407 -- available, and no special processing is needed.
7408
7409 and then Present (Full_View (Parent_Type))
7410 then
7411 -- In this case, the full view of the parent type will become
7412 -- visible in the body of the enclosing child, and only then will
7413 -- the current type be possibly non-private. Build an underlying
7414 -- full view that will be installed when the enclosing child body
7415 -- is compiled.
7416
7417 if Present (Underlying_Full_View (Derived_Type)) then
7418 Full_Der := Underlying_Full_View (Derived_Type);
7419 else
7420 Build_Full_Derivation;
7421 Set_Underlying_Full_View (Derived_Type, Full_Der);
7422 end if;
7423
7424 -- The full view will be used to swap entities on entry/exit to
7425 -- the body, and must appear in the entity list for the package.
7426
7427 Append_Entity (Full_Der, Scope (Derived_Type));
7428 end if;
7429 end if;
7430 end Build_Derived_Private_Type;
7431
7432 -------------------------------
7433 -- Build_Derived_Record_Type --
7434 -------------------------------
7435
7436 -- 1. INTRODUCTION
7437
7438 -- Ideally we would like to use the same model of type derivation for
7439 -- tagged and untagged record types. Unfortunately this is not quite
7440 -- possible because the semantics of representation clauses is different
7441 -- for tagged and untagged records under inheritance. Consider the
7442 -- following:
7443
7444 -- type R (...) is [tagged] record ... end record;
7445 -- type T (...) is new R (...) [with ...];
7446
7447 -- The representation clauses for T can specify a completely different
7448 -- record layout from R's. Hence the same component can be placed in two
7449 -- very different positions in objects of type T and R. If R and T are
7450 -- tagged types, representation clauses for T can only specify the layout
7451 -- of non inherited components, thus components that are common in R and T
7452 -- have the same position in objects of type R and T.
7453
7454 -- This has two implications. The first is that the entire tree for R's
7455 -- declaration needs to be copied for T in the untagged case, so that T
7456 -- can be viewed as a record type of its own with its own representation
7457 -- clauses. The second implication is the way we handle discriminants.
7458 -- Specifically, in the untagged case we need a way to communicate to Gigi
7459 -- what are the real discriminants in the record, while for the semantics
7460 -- we need to consider those introduced by the user to rename the
7461 -- discriminants in the parent type. This is handled by introducing the
7462 -- notion of stored discriminants. See below for more.
7463
7464 -- Fortunately the way regular components are inherited can be handled in
7465 -- the same way in tagged and untagged types.
7466
7467 -- To complicate things a bit more the private view of a private extension
7468 -- cannot be handled in the same way as the full view (for one thing the
7469 -- semantic rules are somewhat different). We will explain what differs
7470 -- below.
7471
7472 -- 2. DISCRIMINANTS UNDER INHERITANCE
7473
7474 -- The semantic rules governing the discriminants of derived types are
7475 -- quite subtle.
7476
7477 -- type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
7478 -- [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
7479
7480 -- If parent type has discriminants, then the discriminants that are
7481 -- declared in the derived type are [3.4 (11)]:
7482
7483 -- o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
7484 -- there is one;
7485
7486 -- o Otherwise, each discriminant of the parent type (implicitly declared
7487 -- in the same order with the same specifications). In this case, the
7488 -- discriminants are said to be "inherited", or if unknown in the parent
7489 -- are also unknown in the derived type.
7490
7491 -- Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
7492
7493 -- o The parent subtype must be constrained;
7494
7495 -- o If the parent type is not a tagged type, then each discriminant of
7496 -- the derived type must be used in the constraint defining a parent
7497 -- subtype. [Implementation note: This ensures that the new discriminant
7498 -- can share storage with an existing discriminant.]
7499
7500 -- For the derived type each discriminant of the parent type is either
7501 -- inherited, constrained to equal some new discriminant of the derived
7502 -- type, or constrained to the value of an expression.
7503
7504 -- When inherited or constrained to equal some new discriminant, the
7505 -- parent discriminant and the discriminant of the derived type are said
7506 -- to "correspond".
7507
7508 -- If a discriminant of the parent type is constrained to a specific value
7509 -- in the derived type definition, then the discriminant is said to be
7510 -- "specified" by that derived type definition.
7511
7512 -- 3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
7513
7514 -- We have spoken about stored discriminants in point 1 (introduction)
7515 -- above. There are two sort of stored discriminants: implicit and
7516 -- explicit. As long as the derived type inherits the same discriminants as
7517 -- the root record type, stored discriminants are the same as regular
7518 -- discriminants, and are said to be implicit. However, if any discriminant
7519 -- in the root type was renamed in the derived type, then the derived
7520 -- type will contain explicit stored discriminants. Explicit stored
7521 -- discriminants are discriminants in addition to the semantically visible
7522 -- discriminants defined for the derived type. Stored discriminants are
7523 -- used by Gigi to figure out what are the physical discriminants in
7524 -- objects of the derived type (see precise definition in einfo.ads).
7525 -- As an example, consider the following:
7526
7527 -- type R (D1, D2, D3 : Int) is record ... end record;
7528 -- type T1 is new R;
7529 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
7530 -- type T3 is new T2;
7531 -- type T4 (Y : Int) is new T3 (Y, 99);
7532
7533 -- The following table summarizes the discriminants and stored
7534 -- discriminants in R and T1 through T4.
7535
7536 -- Type Discrim Stored Discrim Comment
7537 -- R (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in R
7538 -- T1 (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in T1
7539 -- T2 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T2
7540 -- T3 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T3
7541 -- T4 (Y) (D1, D2, D3) Girder discrims EXPLICIT in T4
7542
7543 -- Field Corresponding_Discriminant (abbreviated CD below) allows us to
7544 -- find the corresponding discriminant in the parent type, while
7545 -- Original_Record_Component (abbreviated ORC below), the actual physical
7546 -- component that is renamed. Finally the field Is_Completely_Hidden
7547 -- (abbreviated ICH below) is set for all explicit stored discriminants
7548 -- (see einfo.ads for more info). For the above example this gives:
7549
7550 -- Discrim CD ORC ICH
7551 -- ^^^^^^^ ^^ ^^^ ^^^
7552 -- D1 in R empty itself no
7553 -- D2 in R empty itself no
7554 -- D3 in R empty itself no
7555
7556 -- D1 in T1 D1 in R itself no
7557 -- D2 in T1 D2 in R itself no
7558 -- D3 in T1 D3 in R itself no
7559
7560 -- X1 in T2 D3 in T1 D3 in T2 no
7561 -- X2 in T2 D1 in T1 D1 in T2 no
7562 -- D1 in T2 empty itself yes
7563 -- D2 in T2 empty itself yes
7564 -- D3 in T2 empty itself yes
7565
7566 -- X1 in T3 X1 in T2 D3 in T3 no
7567 -- X2 in T3 X2 in T2 D1 in T3 no
7568 -- D1 in T3 empty itself yes
7569 -- D2 in T3 empty itself yes
7570 -- D3 in T3 empty itself yes
7571
7572 -- Y in T4 X1 in T3 D3 in T3 no
7573 -- D1 in T3 empty itself yes
7574 -- D2 in T3 empty itself yes
7575 -- D3 in T3 empty itself yes
7576
7577 -- 4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
7578
7579 -- Type derivation for tagged types is fairly straightforward. If no
7580 -- discriminants are specified by the derived type, these are inherited
7581 -- from the parent. No explicit stored discriminants are ever necessary.
7582 -- The only manipulation that is done to the tree is that of adding a
7583 -- _parent field with parent type and constrained to the same constraint
7584 -- specified for the parent in the derived type definition. For instance:
7585
7586 -- type R (D1, D2, D3 : Int) is tagged record ... end record;
7587 -- type T1 is new R with null record;
7588 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
7589
7590 -- are changed into:
7591
7592 -- type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
7593 -- _parent : R (D1, D2, D3);
7594 -- end record;
7595
7596 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
7597 -- _parent : T1 (X2, 88, X1);
7598 -- end record;
7599
7600 -- The discriminants actually present in R, T1 and T2 as well as their CD,
7601 -- ORC and ICH fields are:
7602
7603 -- Discrim CD ORC ICH
7604 -- ^^^^^^^ ^^ ^^^ ^^^
7605 -- D1 in R empty itself no
7606 -- D2 in R empty itself no
7607 -- D3 in R empty itself no
7608
7609 -- D1 in T1 D1 in R D1 in R no
7610 -- D2 in T1 D2 in R D2 in R no
7611 -- D3 in T1 D3 in R D3 in R no
7612
7613 -- X1 in T2 D3 in T1 D3 in R no
7614 -- X2 in T2 D1 in T1 D1 in R no
7615
7616 -- 5. FIRST TRANSFORMATION FOR DERIVED RECORDS
7617 --
7618 -- Regardless of whether we dealing with a tagged or untagged type
7619 -- we will transform all derived type declarations of the form
7620 --
7621 -- type T is new R (...) [with ...];
7622 -- or
7623 -- subtype S is R (...);
7624 -- type T is new S [with ...];
7625 -- into
7626 -- type BT is new R [with ...];
7627 -- subtype T is BT (...);
7628 --
7629 -- That is, the base derived type is constrained only if it has no
7630 -- discriminants. The reason for doing this is that GNAT's semantic model
7631 -- assumes that a base type with discriminants is unconstrained.
7632 --
7633 -- Note that, strictly speaking, the above transformation is not always
7634 -- correct. Consider for instance the following excerpt from ACVC b34011a:
7635 --
7636 -- procedure B34011A is
7637 -- type REC (D : integer := 0) is record
7638 -- I : Integer;
7639 -- end record;
7640
7641 -- package P is
7642 -- type T6 is new Rec;
7643 -- function F return T6;
7644 -- end P;
7645
7646 -- use P;
7647 -- package Q6 is
7648 -- type U is new T6 (Q6.F.I); -- ERROR: Q6.F.
7649 -- end Q6;
7650 --
7651 -- The definition of Q6.U is illegal. However transforming Q6.U into
7652
7653 -- type BaseU is new T6;
7654 -- subtype U is BaseU (Q6.F.I)
7655
7656 -- turns U into a legal subtype, which is incorrect. To avoid this problem
7657 -- we always analyze the constraint (in this case (Q6.F.I)) before applying
7658 -- the transformation described above.
7659
7660 -- There is another instance where the above transformation is incorrect.
7661 -- Consider:
7662
7663 -- package Pack is
7664 -- type Base (D : Integer) is tagged null record;
7665 -- procedure P (X : Base);
7666
7667 -- type Der is new Base (2) with null record;
7668 -- procedure P (X : Der);
7669 -- end Pack;
7670
7671 -- Then the above transformation turns this into
7672
7673 -- type Der_Base is new Base with null record;
7674 -- -- procedure P (X : Base) is implicitly inherited here
7675 -- -- as procedure P (X : Der_Base).
7676
7677 -- subtype Der is Der_Base (2);
7678 -- procedure P (X : Der);
7679 -- -- The overriding of P (X : Der_Base) is illegal since we
7680 -- -- have a parameter conformance problem.
7681
7682 -- To get around this problem, after having semantically processed Der_Base
7683 -- and the rewritten subtype declaration for Der, we copy Der_Base field
7684 -- Discriminant_Constraint from Der so that when parameter conformance is
7685 -- checked when P is overridden, no semantic errors are flagged.
7686
7687 -- 6. SECOND TRANSFORMATION FOR DERIVED RECORDS
7688
7689 -- Regardless of whether we are dealing with a tagged or untagged type
7690 -- we will transform all derived type declarations of the form
7691
7692 -- type R (D1, .., Dn : ...) is [tagged] record ...;
7693 -- type T is new R [with ...];
7694 -- into
7695 -- type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
7696
7697 -- The reason for such transformation is that it allows us to implement a
7698 -- very clean form of component inheritance as explained below.
7699
7700 -- Note that this transformation is not achieved by direct tree rewriting
7701 -- and manipulation, but rather by redoing the semantic actions that the
7702 -- above transformation will entail. This is done directly in routine
7703 -- Inherit_Components.
7704
7705 -- 7. TYPE DERIVATION AND COMPONENT INHERITANCE
7706
7707 -- In both tagged and untagged derived types, regular non discriminant
7708 -- components are inherited in the derived type from the parent type. In
7709 -- the absence of discriminants component, inheritance is straightforward
7710 -- as components can simply be copied from the parent.
7711
7712 -- If the parent has discriminants, inheriting components constrained with
7713 -- these discriminants requires caution. Consider the following example:
7714
7715 -- type R (D1, D2 : Positive) is [tagged] record
7716 -- S : String (D1 .. D2);
7717 -- end record;
7718
7719 -- type T1 is new R [with null record];
7720 -- type T2 (X : positive) is new R (1, X) [with null record];
7721
7722 -- As explained in 6. above, T1 is rewritten as
7723 -- type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
7724 -- which makes the treatment for T1 and T2 identical.
7725
7726 -- What we want when inheriting S, is that references to D1 and D2 in R are
7727 -- replaced with references to their correct constraints, i.e. D1 and D2 in
7728 -- T1 and 1 and X in T2. So all R's discriminant references are replaced
7729 -- with either discriminant references in the derived type or expressions.
7730 -- This replacement is achieved as follows: before inheriting R's
7731 -- components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
7732 -- created in the scope of T1 (resp. scope of T2) so that discriminants D1
7733 -- and D2 of T1 are visible (resp. discriminant X of T2 is visible).
7734 -- For T2, for instance, this has the effect of replacing String (D1 .. D2)
7735 -- by String (1 .. X).
7736
7737 -- 8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
7738
7739 -- We explain here the rules governing private type extensions relevant to
7740 -- type derivation. These rules are explained on the following example:
7741
7742 -- type D [(...)] is new A [(...)] with private; <-- partial view
7743 -- type D [(...)] is new P [(...)] with null record; <-- full view
7744
7745 -- Type A is called the ancestor subtype of the private extension.
7746 -- Type P is the parent type of the full view of the private extension. It
7747 -- must be A or a type derived from A.
7748
7749 -- The rules concerning the discriminants of private type extensions are
7750 -- [7.3(10-13)]:
7751
7752 -- o If a private extension inherits known discriminants from the ancestor
7753 -- subtype, then the full view must also inherit its discriminants from
7754 -- the ancestor subtype and the parent subtype of the full view must be
7755 -- constrained if and only if the ancestor subtype is constrained.
7756
7757 -- o If a partial view has unknown discriminants, then the full view may
7758 -- define a definite or an indefinite subtype, with or without
7759 -- discriminants.
7760
7761 -- o If a partial view has neither known nor unknown discriminants, then
7762 -- the full view must define a definite subtype.
7763
7764 -- o If the ancestor subtype of a private extension has constrained
7765 -- discriminants, then the parent subtype of the full view must impose a
7766 -- statically matching constraint on those discriminants.
7767
7768 -- This means that only the following forms of private extensions are
7769 -- allowed:
7770
7771 -- type D is new A with private; <-- partial view
7772 -- type D is new P with null record; <-- full view
7773
7774 -- If A has no discriminants than P has no discriminants, otherwise P must
7775 -- inherit A's discriminants.
7776
7777 -- type D is new A (...) with private; <-- partial view
7778 -- type D is new P (:::) with null record; <-- full view
7779
7780 -- P must inherit A's discriminants and (...) and (:::) must statically
7781 -- match.
7782
7783 -- subtype A is R (...);
7784 -- type D is new A with private; <-- partial view
7785 -- type D is new P with null record; <-- full view
7786
7787 -- P must have inherited R's discriminants and must be derived from A or
7788 -- any of its subtypes.
7789
7790 -- type D (..) is new A with private; <-- partial view
7791 -- type D (..) is new P [(:::)] with null record; <-- full view
7792
7793 -- No specific constraints on P's discriminants or constraint (:::).
7794 -- Note that A can be unconstrained, but the parent subtype P must either
7795 -- be constrained or (:::) must be present.
7796
7797 -- type D (..) is new A [(...)] with private; <-- partial view
7798 -- type D (..) is new P [(:::)] with null record; <-- full view
7799
7800 -- P's constraints on A's discriminants must statically match those
7801 -- imposed by (...).
7802
7803 -- 9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
7804
7805 -- The full view of a private extension is handled exactly as described
7806 -- above. The model chose for the private view of a private extension is
7807 -- the same for what concerns discriminants (i.e. they receive the same
7808 -- treatment as in the tagged case). However, the private view of the
7809 -- private extension always inherits the components of the parent base,
7810 -- without replacing any discriminant reference. Strictly speaking this is
7811 -- incorrect. However, Gigi never uses this view to generate code so this
7812 -- is a purely semantic issue. In theory, a set of transformations similar
7813 -- to those given in 5. and 6. above could be applied to private views of
7814 -- private extensions to have the same model of component inheritance as
7815 -- for non private extensions. However, this is not done because it would
7816 -- further complicate private type processing. Semantically speaking, this
7817 -- leaves us in an uncomfortable situation. As an example consider:
7818
7819 -- package Pack is
7820 -- type R (D : integer) is tagged record
7821 -- S : String (1 .. D);
7822 -- end record;
7823 -- procedure P (X : R);
7824 -- type T is new R (1) with private;
7825 -- private
7826 -- type T is new R (1) with null record;
7827 -- end;
7828
7829 -- This is transformed into:
7830
7831 -- package Pack is
7832 -- type R (D : integer) is tagged record
7833 -- S : String (1 .. D);
7834 -- end record;
7835 -- procedure P (X : R);
7836 -- type T is new R (1) with private;
7837 -- private
7838 -- type BaseT is new R with null record;
7839 -- subtype T is BaseT (1);
7840 -- end;
7841
7842 -- (strictly speaking the above is incorrect Ada)
7843
7844 -- From the semantic standpoint the private view of private extension T
7845 -- should be flagged as constrained since one can clearly have
7846 --
7847 -- Obj : T;
7848 --
7849 -- in a unit withing Pack. However, when deriving subprograms for the
7850 -- private view of private extension T, T must be seen as unconstrained
7851 -- since T has discriminants (this is a constraint of the current
7852 -- subprogram derivation model). Thus, when processing the private view of
7853 -- a private extension such as T, we first mark T as unconstrained, we
7854 -- process it, we perform program derivation and just before returning from
7855 -- Build_Derived_Record_Type we mark T as constrained.
7856
7857 -- ??? Are there are other uncomfortable cases that we will have to
7858 -- deal with.
7859
7860 -- 10. RECORD_TYPE_WITH_PRIVATE complications
7861
7862 -- Types that are derived from a visible record type and have a private
7863 -- extension present other peculiarities. They behave mostly like private
7864 -- types, but if they have primitive operations defined, these will not
7865 -- have the proper signatures for further inheritance, because other
7866 -- primitive operations will use the implicit base that we define for
7867 -- private derivations below. This affect subprogram inheritance (see
7868 -- Derive_Subprograms for details). We also derive the implicit base from
7869 -- the base type of the full view, so that the implicit base is a record
7870 -- type and not another private type, This avoids infinite loops.
7871
7872 procedure Build_Derived_Record_Type
7873 (N : Node_Id;
7874 Parent_Type : Entity_Id;
7875 Derived_Type : Entity_Id;
7876 Derive_Subps : Boolean := True)
7877 is
7878 Discriminant_Specs : constant Boolean :=
7879 Present (Discriminant_Specifications (N));
7880 Is_Tagged : constant Boolean := Is_Tagged_Type (Parent_Type);
7881 Loc : constant Source_Ptr := Sloc (N);
7882 Private_Extension : constant Boolean :=
7883 Nkind (N) = N_Private_Extension_Declaration;
7884 Assoc_List : Elist_Id;
7885 Constraint_Present : Boolean;
7886 Constrs : Elist_Id;
7887 Discrim : Entity_Id;
7888 Indic : Node_Id;
7889 Inherit_Discrims : Boolean := False;
7890 Last_Discrim : Entity_Id;
7891 New_Base : Entity_Id;
7892 New_Decl : Node_Id;
7893 New_Discrs : Elist_Id;
7894 New_Indic : Node_Id;
7895 Parent_Base : Entity_Id;
7896 Save_Etype : Entity_Id;
7897 Save_Discr_Constr : Elist_Id;
7898 Save_Next_Entity : Entity_Id;
7899 Type_Def : Node_Id;
7900
7901 Discs : Elist_Id := New_Elmt_List;
7902 -- An empty Discs list means that there were no constraints in the
7903 -- subtype indication or that there was an error processing it.
7904
7905 begin
7906 if Ekind (Parent_Type) = E_Record_Type_With_Private
7907 and then Present (Full_View (Parent_Type))
7908 and then Has_Discriminants (Parent_Type)
7909 then
7910 Parent_Base := Base_Type (Full_View (Parent_Type));
7911 else
7912 Parent_Base := Base_Type (Parent_Type);
7913 end if;
7914
7915 -- AI05-0115 : if this is a derivation from a private type in some
7916 -- other scope that may lead to invisible components for the derived
7917 -- type, mark it accordingly.
7918
7919 if Is_Private_Type (Parent_Type) then
7920 if Scope (Parent_Type) = Scope (Derived_Type) then
7921 null;
7922
7923 elsif In_Open_Scopes (Scope (Parent_Type))
7924 and then In_Private_Part (Scope (Parent_Type))
7925 then
7926 null;
7927
7928 else
7929 Set_Has_Private_Ancestor (Derived_Type);
7930 end if;
7931
7932 else
7933 Set_Has_Private_Ancestor
7934 (Derived_Type, Has_Private_Ancestor (Parent_Type));
7935 end if;
7936
7937 -- Before we start the previously documented transformations, here is
7938 -- little fix for size and alignment of tagged types. Normally when we
7939 -- derive type D from type P, we copy the size and alignment of P as the
7940 -- default for D, and in the absence of explicit representation clauses
7941 -- for D, the size and alignment are indeed the same as the parent.
7942
7943 -- But this is wrong for tagged types, since fields may be added, and
7944 -- the default size may need to be larger, and the default alignment may
7945 -- need to be larger.
7946
7947 -- We therefore reset the size and alignment fields in the tagged case.
7948 -- Note that the size and alignment will in any case be at least as
7949 -- large as the parent type (since the derived type has a copy of the
7950 -- parent type in the _parent field)
7951
7952 -- The type is also marked as being tagged here, which is needed when
7953 -- processing components with a self-referential anonymous access type
7954 -- in the call to Check_Anonymous_Access_Components below. Note that
7955 -- this flag is also set later on for completeness.
7956
7957 if Is_Tagged then
7958 Set_Is_Tagged_Type (Derived_Type);
7959 Init_Size_Align (Derived_Type);
7960 end if;
7961
7962 -- STEP 0a: figure out what kind of derived type declaration we have
7963
7964 if Private_Extension then
7965 Type_Def := N;
7966 Set_Ekind (Derived_Type, E_Record_Type_With_Private);
7967 Set_Default_SSO (Derived_Type);
7968
7969 else
7970 Type_Def := Type_Definition (N);
7971
7972 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
7973 -- Parent_Base can be a private type or private extension. However,
7974 -- for tagged types with an extension the newly added fields are
7975 -- visible and hence the Derived_Type is always an E_Record_Type.
7976 -- (except that the parent may have its own private fields).
7977 -- For untagged types we preserve the Ekind of the Parent_Base.
7978
7979 if Present (Record_Extension_Part (Type_Def)) then
7980 Set_Ekind (Derived_Type, E_Record_Type);
7981 Set_Default_SSO (Derived_Type);
7982
7983 -- Create internal access types for components with anonymous
7984 -- access types.
7985
7986 if Ada_Version >= Ada_2005 then
7987 Check_Anonymous_Access_Components
7988 (N, Derived_Type, Derived_Type,
7989 Component_List (Record_Extension_Part (Type_Def)));
7990 end if;
7991
7992 else
7993 Set_Ekind (Derived_Type, Ekind (Parent_Base));
7994 end if;
7995 end if;
7996
7997 -- Indic can either be an N_Identifier if the subtype indication
7998 -- contains no constraint or an N_Subtype_Indication if the subtype
7999 -- indication has a constraint.
8000
8001 Indic := Subtype_Indication (Type_Def);
8002 Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
8003
8004 -- Check that the type has visible discriminants. The type may be
8005 -- a private type with unknown discriminants whose full view has
8006 -- discriminants which are invisible.
8007
8008 if Constraint_Present then
8009 if not Has_Discriminants (Parent_Base)
8010 or else
8011 (Has_Unknown_Discriminants (Parent_Base)
8012 and then Is_Private_Type (Parent_Base))
8013 then
8014 Error_Msg_N
8015 ("invalid constraint: type has no discriminant",
8016 Constraint (Indic));
8017
8018 Constraint_Present := False;
8019 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
8020
8021 elsif Is_Constrained (Parent_Type) then
8022 Error_Msg_N
8023 ("invalid constraint: parent type is already constrained",
8024 Constraint (Indic));
8025
8026 Constraint_Present := False;
8027 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
8028 end if;
8029 end if;
8030
8031 -- STEP 0b: If needed, apply transformation given in point 5. above
8032
8033 if not Private_Extension
8034 and then Has_Discriminants (Parent_Type)
8035 and then not Discriminant_Specs
8036 and then (Is_Constrained (Parent_Type) or else Constraint_Present)
8037 then
8038 -- First, we must analyze the constraint (see comment in point 5.)
8039 -- The constraint may come from the subtype indication of the full
8040 -- declaration.
8041
8042 if Constraint_Present then
8043 New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
8044
8045 -- If there is no explicit constraint, there might be one that is
8046 -- inherited from a constrained parent type. In that case verify that
8047 -- it conforms to the constraint in the partial view. In perverse
8048 -- cases the parent subtypes of the partial and full view can have
8049 -- different constraints.
8050
8051 elsif Present (Stored_Constraint (Parent_Type)) then
8052 New_Discrs := Stored_Constraint (Parent_Type);
8053
8054 else
8055 New_Discrs := No_Elist;
8056 end if;
8057
8058 if Has_Discriminants (Derived_Type)
8059 and then Has_Private_Declaration (Derived_Type)
8060 and then Present (Discriminant_Constraint (Derived_Type))
8061 and then Present (New_Discrs)
8062 then
8063 -- Verify that constraints of the full view statically match
8064 -- those given in the partial view.
8065
8066 declare
8067 C1, C2 : Elmt_Id;
8068
8069 begin
8070 C1 := First_Elmt (New_Discrs);
8071 C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
8072 while Present (C1) and then Present (C2) loop
8073 if Fully_Conformant_Expressions (Node (C1), Node (C2))
8074 or else
8075 (Is_OK_Static_Expression (Node (C1))
8076 and then Is_OK_Static_Expression (Node (C2))
8077 and then
8078 Expr_Value (Node (C1)) = Expr_Value (Node (C2)))
8079 then
8080 null;
8081
8082 else
8083 if Constraint_Present then
8084 Error_Msg_N
8085 ("constraint not conformant to previous declaration",
8086 Node (C1));
8087 else
8088 Error_Msg_N
8089 ("constraint of full view is incompatible "
8090 & "with partial view", N);
8091 end if;
8092 end if;
8093
8094 Next_Elmt (C1);
8095 Next_Elmt (C2);
8096 end loop;
8097 end;
8098 end if;
8099
8100 -- Insert and analyze the declaration for the unconstrained base type
8101
8102 New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
8103
8104 New_Decl :=
8105 Make_Full_Type_Declaration (Loc,
8106 Defining_Identifier => New_Base,
8107 Type_Definition =>
8108 Make_Derived_Type_Definition (Loc,
8109 Abstract_Present => Abstract_Present (Type_Def),
8110 Limited_Present => Limited_Present (Type_Def),
8111 Subtype_Indication =>
8112 New_Occurrence_Of (Parent_Base, Loc),
8113 Record_Extension_Part =>
8114 Relocate_Node (Record_Extension_Part (Type_Def)),
8115 Interface_List => Interface_List (Type_Def)));
8116
8117 Set_Parent (New_Decl, Parent (N));
8118 Mark_Rewrite_Insertion (New_Decl);
8119 Insert_Before (N, New_Decl);
8120
8121 -- In the extension case, make sure ancestor is frozen appropriately
8122 -- (see also non-discriminated case below).
8123
8124 if Present (Record_Extension_Part (Type_Def))
8125 or else Is_Interface (Parent_Base)
8126 then
8127 Freeze_Before (New_Decl, Parent_Type);
8128 end if;
8129
8130 -- Note that this call passes False for the Derive_Subps parameter
8131 -- because subprogram derivation is deferred until after creating
8132 -- the subtype (see below).
8133
8134 Build_Derived_Type
8135 (New_Decl, Parent_Base, New_Base,
8136 Is_Completion => False, Derive_Subps => False);
8137
8138 -- ??? This needs re-examination to determine whether the
8139 -- above call can simply be replaced by a call to Analyze.
8140
8141 Set_Analyzed (New_Decl);
8142
8143 -- Insert and analyze the declaration for the constrained subtype
8144
8145 if Constraint_Present then
8146 New_Indic :=
8147 Make_Subtype_Indication (Loc,
8148 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
8149 Constraint => Relocate_Node (Constraint (Indic)));
8150
8151 else
8152 declare
8153 Constr_List : constant List_Id := New_List;
8154 C : Elmt_Id;
8155 Expr : Node_Id;
8156
8157 begin
8158 C := First_Elmt (Discriminant_Constraint (Parent_Type));
8159 while Present (C) loop
8160 Expr := Node (C);
8161
8162 -- It is safe here to call New_Copy_Tree since we called
8163 -- Force_Evaluation on each constraint previously
8164 -- in Build_Discriminant_Constraints.
8165
8166 Append (New_Copy_Tree (Expr), To => Constr_List);
8167
8168 Next_Elmt (C);
8169 end loop;
8170
8171 New_Indic :=
8172 Make_Subtype_Indication (Loc,
8173 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
8174 Constraint =>
8175 Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
8176 end;
8177 end if;
8178
8179 Rewrite (N,
8180 Make_Subtype_Declaration (Loc,
8181 Defining_Identifier => Derived_Type,
8182 Subtype_Indication => New_Indic));
8183
8184 Analyze (N);
8185
8186 -- Derivation of subprograms must be delayed until the full subtype
8187 -- has been established, to ensure proper overriding of subprograms
8188 -- inherited by full types. If the derivations occurred as part of
8189 -- the call to Build_Derived_Type above, then the check for type
8190 -- conformance would fail because earlier primitive subprograms
8191 -- could still refer to the full type prior the change to the new
8192 -- subtype and hence would not match the new base type created here.
8193 -- Subprograms are not derived, however, when Derive_Subps is False
8194 -- (since otherwise there could be redundant derivations).
8195
8196 if Derive_Subps then
8197 Derive_Subprograms (Parent_Type, Derived_Type);
8198 end if;
8199
8200 -- For tagged types the Discriminant_Constraint of the new base itype
8201 -- is inherited from the first subtype so that no subtype conformance
8202 -- problem arise when the first subtype overrides primitive
8203 -- operations inherited by the implicit base type.
8204
8205 if Is_Tagged then
8206 Set_Discriminant_Constraint
8207 (New_Base, Discriminant_Constraint (Derived_Type));
8208 end if;
8209
8210 return;
8211 end if;
8212
8213 -- If we get here Derived_Type will have no discriminants or it will be
8214 -- a discriminated unconstrained base type.
8215
8216 -- STEP 1a: perform preliminary actions/checks for derived tagged types
8217
8218 if Is_Tagged then
8219
8220 -- The parent type is frozen for non-private extensions (RM 13.14(7))
8221 -- The declaration of a specific descendant of an interface type
8222 -- freezes the interface type (RM 13.14).
8223
8224 if not Private_Extension or else Is_Interface (Parent_Base) then
8225 Freeze_Before (N, Parent_Type);
8226 end if;
8227
8228 -- In Ada 2005 (AI-344), the restriction that a derived tagged type
8229 -- cannot be declared at a deeper level than its parent type is
8230 -- removed. The check on derivation within a generic body is also
8231 -- relaxed, but there's a restriction that a derived tagged type
8232 -- cannot be declared in a generic body if it's derived directly
8233 -- or indirectly from a formal type of that generic.
8234
8235 if Ada_Version >= Ada_2005 then
8236 if Present (Enclosing_Generic_Body (Derived_Type)) then
8237 declare
8238 Ancestor_Type : Entity_Id;
8239
8240 begin
8241 -- Check to see if any ancestor of the derived type is a
8242 -- formal type.
8243
8244 Ancestor_Type := Parent_Type;
8245 while not Is_Generic_Type (Ancestor_Type)
8246 and then Etype (Ancestor_Type) /= Ancestor_Type
8247 loop
8248 Ancestor_Type := Etype (Ancestor_Type);
8249 end loop;
8250
8251 -- If the derived type does have a formal type as an
8252 -- ancestor, then it's an error if the derived type is
8253 -- declared within the body of the generic unit that
8254 -- declares the formal type in its generic formal part. It's
8255 -- sufficient to check whether the ancestor type is declared
8256 -- inside the same generic body as the derived type (such as
8257 -- within a nested generic spec), in which case the
8258 -- derivation is legal. If the formal type is declared
8259 -- outside of that generic body, then it's guaranteed that
8260 -- the derived type is declared within the generic body of
8261 -- the generic unit declaring the formal type.
8262
8263 if Is_Generic_Type (Ancestor_Type)
8264 and then Enclosing_Generic_Body (Ancestor_Type) /=
8265 Enclosing_Generic_Body (Derived_Type)
8266 then
8267 Error_Msg_NE
8268 ("parent type of& must not be descendant of formal type"
8269 & " of an enclosing generic body",
8270 Indic, Derived_Type);
8271 end if;
8272 end;
8273 end if;
8274
8275 elsif Type_Access_Level (Derived_Type) /=
8276 Type_Access_Level (Parent_Type)
8277 and then not Is_Generic_Type (Derived_Type)
8278 then
8279 if Is_Controlled (Parent_Type) then
8280 Error_Msg_N
8281 ("controlled type must be declared at the library level",
8282 Indic);
8283 else
8284 Error_Msg_N
8285 ("type extension at deeper accessibility level than parent",
8286 Indic);
8287 end if;
8288
8289 else
8290 declare
8291 GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
8292 begin
8293 if Present (GB)
8294 and then GB /= Enclosing_Generic_Body (Parent_Base)
8295 then
8296 Error_Msg_NE
8297 ("parent type of& must not be outside generic body"
8298 & " (RM 3.9.1(4))",
8299 Indic, Derived_Type);
8300 end if;
8301 end;
8302 end if;
8303 end if;
8304
8305 -- Ada 2005 (AI-251)
8306
8307 if Ada_Version >= Ada_2005 and then Is_Tagged then
8308
8309 -- "The declaration of a specific descendant of an interface type
8310 -- freezes the interface type" (RM 13.14).
8311
8312 declare
8313 Iface : Node_Id;
8314 begin
8315 if Is_Non_Empty_List (Interface_List (Type_Def)) then
8316 Iface := First (Interface_List (Type_Def));
8317 while Present (Iface) loop
8318 Freeze_Before (N, Etype (Iface));
8319 Next (Iface);
8320 end loop;
8321 end if;
8322 end;
8323 end if;
8324
8325 -- STEP 1b : preliminary cleanup of the full view of private types
8326
8327 -- If the type is already marked as having discriminants, then it's the
8328 -- completion of a private type or private extension and we need to
8329 -- retain the discriminants from the partial view if the current
8330 -- declaration has Discriminant_Specifications so that we can verify
8331 -- conformance. However, we must remove any existing components that
8332 -- were inherited from the parent (and attached in Copy_And_Swap)
8333 -- because the full type inherits all appropriate components anyway, and
8334 -- we do not want the partial view's components interfering.
8335
8336 if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
8337 Discrim := First_Discriminant (Derived_Type);
8338 loop
8339 Last_Discrim := Discrim;
8340 Next_Discriminant (Discrim);
8341 exit when No (Discrim);
8342 end loop;
8343
8344 Set_Last_Entity (Derived_Type, Last_Discrim);
8345
8346 -- In all other cases wipe out the list of inherited components (even
8347 -- inherited discriminants), it will be properly rebuilt here.
8348
8349 else
8350 Set_First_Entity (Derived_Type, Empty);
8351 Set_Last_Entity (Derived_Type, Empty);
8352 end if;
8353
8354 -- STEP 1c: Initialize some flags for the Derived_Type
8355
8356 -- The following flags must be initialized here so that
8357 -- Process_Discriminants can check that discriminants of tagged types do
8358 -- not have a default initial value and that access discriminants are
8359 -- only specified for limited records. For completeness, these flags are
8360 -- also initialized along with all the other flags below.
8361
8362 -- AI-419: Limitedness is not inherited from an interface parent, so to
8363 -- be limited in that case the type must be explicitly declared as
8364 -- limited. However, task and protected interfaces are always limited.
8365
8366 if Limited_Present (Type_Def) then
8367 Set_Is_Limited_Record (Derived_Type);
8368
8369 elsif Is_Limited_Record (Parent_Type)
8370 or else (Present (Full_View (Parent_Type))
8371 and then Is_Limited_Record (Full_View (Parent_Type)))
8372 then
8373 if not Is_Interface (Parent_Type)
8374 or else Is_Synchronized_Interface (Parent_Type)
8375 or else Is_Protected_Interface (Parent_Type)
8376 or else Is_Task_Interface (Parent_Type)
8377 then
8378 Set_Is_Limited_Record (Derived_Type);
8379 end if;
8380 end if;
8381
8382 -- STEP 2a: process discriminants of derived type if any
8383
8384 Push_Scope (Derived_Type);
8385
8386 if Discriminant_Specs then
8387 Set_Has_Unknown_Discriminants (Derived_Type, False);
8388
8389 -- The following call initializes fields Has_Discriminants and
8390 -- Discriminant_Constraint, unless we are processing the completion
8391 -- of a private type declaration.
8392
8393 Check_Or_Process_Discriminants (N, Derived_Type);
8394
8395 -- For untagged types, the constraint on the Parent_Type must be
8396 -- present and is used to rename the discriminants.
8397
8398 if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
8399 Error_Msg_N ("untagged parent must have discriminants", Indic);
8400
8401 elsif not Is_Tagged and then not Constraint_Present then
8402 Error_Msg_N
8403 ("discriminant constraint needed for derived untagged records",
8404 Indic);
8405
8406 -- Otherwise the parent subtype must be constrained unless we have a
8407 -- private extension.
8408
8409 elsif not Constraint_Present
8410 and then not Private_Extension
8411 and then not Is_Constrained (Parent_Type)
8412 then
8413 Error_Msg_N
8414 ("unconstrained type not allowed in this context", Indic);
8415
8416 elsif Constraint_Present then
8417 -- The following call sets the field Corresponding_Discriminant
8418 -- for the discriminants in the Derived_Type.
8419
8420 Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
8421
8422 -- For untagged types all new discriminants must rename
8423 -- discriminants in the parent. For private extensions new
8424 -- discriminants cannot rename old ones (implied by [7.3(13)]).
8425
8426 Discrim := First_Discriminant (Derived_Type);
8427 while Present (Discrim) loop
8428 if not Is_Tagged
8429 and then No (Corresponding_Discriminant (Discrim))
8430 then
8431 Error_Msg_N
8432 ("new discriminants must constrain old ones", Discrim);
8433
8434 elsif Private_Extension
8435 and then Present (Corresponding_Discriminant (Discrim))
8436 then
8437 Error_Msg_N
8438 ("only static constraints allowed for parent"
8439 & " discriminants in the partial view", Indic);
8440 exit;
8441 end if;
8442
8443 -- If a new discriminant is used in the constraint, then its
8444 -- subtype must be statically compatible with the parent
8445 -- discriminant's subtype (3.7(15)).
8446
8447 -- However, if the record contains an array constrained by
8448 -- the discriminant but with some different bound, the compiler
8449 -- attemps to create a smaller range for the discriminant type.
8450 -- (See exp_ch3.Adjust_Discriminants). In this case, where
8451 -- the discriminant type is a scalar type, the check must use
8452 -- the original discriminant type in the parent declaration.
8453
8454 declare
8455 Corr_Disc : constant Entity_Id :=
8456 Corresponding_Discriminant (Discrim);
8457 Disc_Type : constant Entity_Id := Etype (Discrim);
8458 Corr_Type : Entity_Id;
8459
8460 begin
8461 if Present (Corr_Disc) then
8462 if Is_Scalar_Type (Disc_Type) then
8463 Corr_Type :=
8464 Entity (Discriminant_Type (Parent (Corr_Disc)));
8465 else
8466 Corr_Type := Etype (Corr_Disc);
8467 end if;
8468
8469 if not
8470 Subtypes_Statically_Compatible (Disc_Type, Corr_Type)
8471 then
8472 Error_Msg_N
8473 ("subtype must be compatible "
8474 & "with parent discriminant",
8475 Discrim);
8476 end if;
8477 end if;
8478 end;
8479
8480 Next_Discriminant (Discrim);
8481 end loop;
8482
8483 -- Check whether the constraints of the full view statically
8484 -- match those imposed by the parent subtype [7.3(13)].
8485
8486 if Present (Stored_Constraint (Derived_Type)) then
8487 declare
8488 C1, C2 : Elmt_Id;
8489
8490 begin
8491 C1 := First_Elmt (Discs);
8492 C2 := First_Elmt (Stored_Constraint (Derived_Type));
8493 while Present (C1) and then Present (C2) loop
8494 if not
8495 Fully_Conformant_Expressions (Node (C1), Node (C2))
8496 then
8497 Error_Msg_N
8498 ("not conformant with previous declaration",
8499 Node (C1));
8500 end if;
8501
8502 Next_Elmt (C1);
8503 Next_Elmt (C2);
8504 end loop;
8505 end;
8506 end if;
8507 end if;
8508
8509 -- STEP 2b: No new discriminants, inherit discriminants if any
8510
8511 else
8512 if Private_Extension then
8513 Set_Has_Unknown_Discriminants
8514 (Derived_Type,
8515 Has_Unknown_Discriminants (Parent_Type)
8516 or else Unknown_Discriminants_Present (N));
8517
8518 -- The partial view of the parent may have unknown discriminants,
8519 -- but if the full view has discriminants and the parent type is
8520 -- in scope they must be inherited.
8521
8522 elsif Has_Unknown_Discriminants (Parent_Type)
8523 and then
8524 (not Has_Discriminants (Parent_Type)
8525 or else not In_Open_Scopes (Scope (Parent_Type)))
8526 then
8527 Set_Has_Unknown_Discriminants (Derived_Type);
8528 end if;
8529
8530 if not Has_Unknown_Discriminants (Derived_Type)
8531 and then not Has_Unknown_Discriminants (Parent_Base)
8532 and then Has_Discriminants (Parent_Type)
8533 then
8534 Inherit_Discrims := True;
8535 Set_Has_Discriminants
8536 (Derived_Type, True);
8537 Set_Discriminant_Constraint
8538 (Derived_Type, Discriminant_Constraint (Parent_Base));
8539 end if;
8540
8541 -- The following test is true for private types (remember
8542 -- transformation 5. is not applied to those) and in an error
8543 -- situation.
8544
8545 if Constraint_Present then
8546 Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
8547 end if;
8548
8549 -- For now mark a new derived type as constrained only if it has no
8550 -- discriminants. At the end of Build_Derived_Record_Type we properly
8551 -- set this flag in the case of private extensions. See comments in
8552 -- point 9. just before body of Build_Derived_Record_Type.
8553
8554 Set_Is_Constrained
8555 (Derived_Type,
8556 not (Inherit_Discrims
8557 or else Has_Unknown_Discriminants (Derived_Type)));
8558 end if;
8559
8560 -- STEP 3: initialize fields of derived type
8561
8562 Set_Is_Tagged_Type (Derived_Type, Is_Tagged);
8563 Set_Stored_Constraint (Derived_Type, No_Elist);
8564
8565 -- Ada 2005 (AI-251): Private type-declarations can implement interfaces
8566 -- but cannot be interfaces
8567
8568 if not Private_Extension
8569 and then Ekind (Derived_Type) /= E_Private_Type
8570 and then Ekind (Derived_Type) /= E_Limited_Private_Type
8571 then
8572 if Interface_Present (Type_Def) then
8573 Analyze_Interface_Declaration (Derived_Type, Type_Def);
8574 end if;
8575
8576 Set_Interfaces (Derived_Type, No_Elist);
8577 end if;
8578
8579 -- Fields inherited from the Parent_Type
8580
8581 Set_Has_Specified_Layout
8582 (Derived_Type, Has_Specified_Layout (Parent_Type));
8583 Set_Is_Limited_Composite
8584 (Derived_Type, Is_Limited_Composite (Parent_Type));
8585 Set_Is_Private_Composite
8586 (Derived_Type, Is_Private_Composite (Parent_Type));
8587
8588 if Is_Tagged_Type (Parent_Type) then
8589 Set_No_Tagged_Streams_Pragma
8590 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
8591 end if;
8592
8593 -- Fields inherited from the Parent_Base
8594
8595 Set_Has_Controlled_Component
8596 (Derived_Type, Has_Controlled_Component (Parent_Base));
8597 Set_Has_Non_Standard_Rep
8598 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
8599 Set_Has_Primitive_Operations
8600 (Derived_Type, Has_Primitive_Operations (Parent_Base));
8601
8602 -- Fields inherited from the Parent_Base in the non-private case
8603
8604 if Ekind (Derived_Type) = E_Record_Type then
8605 Set_Has_Complex_Representation
8606 (Derived_Type, Has_Complex_Representation (Parent_Base));
8607 end if;
8608
8609 -- Fields inherited from the Parent_Base for record types
8610
8611 if Is_Record_Type (Derived_Type) then
8612 declare
8613 Parent_Full : Entity_Id;
8614
8615 begin
8616 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
8617 -- Parent_Base can be a private type or private extension. Go
8618 -- to the full view here to get the E_Record_Type specific flags.
8619
8620 if Present (Full_View (Parent_Base)) then
8621 Parent_Full := Full_View (Parent_Base);
8622 else
8623 Parent_Full := Parent_Base;
8624 end if;
8625
8626 Set_OK_To_Reorder_Components
8627 (Derived_Type, OK_To_Reorder_Components (Parent_Full));
8628 end;
8629 end if;
8630
8631 -- Set fields for private derived types
8632
8633 if Is_Private_Type (Derived_Type) then
8634 Set_Depends_On_Private (Derived_Type, True);
8635 Set_Private_Dependents (Derived_Type, New_Elmt_List);
8636
8637 -- Inherit fields from non private record types. If this is the
8638 -- completion of a derivation from a private type, the parent itself
8639 -- is private, and the attributes come from its full view, which must
8640 -- be present.
8641
8642 else
8643 if Is_Private_Type (Parent_Base)
8644 and then not Is_Record_Type (Parent_Base)
8645 then
8646 Set_Component_Alignment
8647 (Derived_Type, Component_Alignment (Full_View (Parent_Base)));
8648 Set_C_Pass_By_Copy
8649 (Derived_Type, C_Pass_By_Copy (Full_View (Parent_Base)));
8650 else
8651 Set_Component_Alignment
8652 (Derived_Type, Component_Alignment (Parent_Base));
8653 Set_C_Pass_By_Copy
8654 (Derived_Type, C_Pass_By_Copy (Parent_Base));
8655 end if;
8656 end if;
8657
8658 -- Set fields for tagged types
8659
8660 if Is_Tagged then
8661 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
8662
8663 -- All tagged types defined in Ada.Finalization are controlled
8664
8665 if Chars (Scope (Derived_Type)) = Name_Finalization
8666 and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
8667 and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
8668 then
8669 Set_Is_Controlled (Derived_Type);
8670 else
8671 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Base));
8672 end if;
8673
8674 -- Minor optimization: there is no need to generate the class-wide
8675 -- entity associated with an underlying record view.
8676
8677 if not Is_Underlying_Record_View (Derived_Type) then
8678 Make_Class_Wide_Type (Derived_Type);
8679 end if;
8680
8681 Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
8682
8683 if Has_Discriminants (Derived_Type)
8684 and then Constraint_Present
8685 then
8686 Set_Stored_Constraint
8687 (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
8688 end if;
8689
8690 if Ada_Version >= Ada_2005 then
8691 declare
8692 Ifaces_List : Elist_Id;
8693
8694 begin
8695 -- Checks rules 3.9.4 (13/2 and 14/2)
8696
8697 if Comes_From_Source (Derived_Type)
8698 and then not Is_Private_Type (Derived_Type)
8699 and then Is_Interface (Parent_Type)
8700 and then not Is_Interface (Derived_Type)
8701 then
8702 if Is_Task_Interface (Parent_Type) then
8703 Error_Msg_N
8704 ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
8705 Derived_Type);
8706
8707 elsif Is_Protected_Interface (Parent_Type) then
8708 Error_Msg_N
8709 ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
8710 Derived_Type);
8711 end if;
8712 end if;
8713
8714 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
8715
8716 Check_Interfaces (N, Type_Def);
8717
8718 -- Ada 2005 (AI-251): Collect the list of progenitors that are
8719 -- not already in the parents.
8720
8721 Collect_Interfaces
8722 (T => Derived_Type,
8723 Ifaces_List => Ifaces_List,
8724 Exclude_Parents => True);
8725
8726 Set_Interfaces (Derived_Type, Ifaces_List);
8727
8728 -- If the derived type is the anonymous type created for
8729 -- a declaration whose parent has a constraint, propagate
8730 -- the interface list to the source type. This must be done
8731 -- prior to the completion of the analysis of the source type
8732 -- because the components in the extension may contain current
8733 -- instances whose legality depends on some ancestor.
8734
8735 if Is_Itype (Derived_Type) then
8736 declare
8737 Def : constant Node_Id :=
8738 Associated_Node_For_Itype (Derived_Type);
8739 begin
8740 if Present (Def)
8741 and then Nkind (Def) = N_Full_Type_Declaration
8742 then
8743 Set_Interfaces
8744 (Defining_Identifier (Def), Ifaces_List);
8745 end if;
8746 end;
8747 end if;
8748
8749 -- Propagate inherited invariant information of parents
8750 -- and progenitors
8751
8752 if Ada_Version >= Ada_2012
8753 and then not Is_Interface (Derived_Type)
8754 then
8755 if Has_Inheritable_Invariants (Parent_Type) then
8756 Set_Has_Invariants (Derived_Type);
8757 Set_Has_Inheritable_Invariants (Derived_Type);
8758
8759 elsif not Is_Empty_Elmt_List (Ifaces_List) then
8760 declare
8761 AI : Elmt_Id;
8762
8763 begin
8764 AI := First_Elmt (Ifaces_List);
8765 while Present (AI) loop
8766 if Has_Inheritable_Invariants (Node (AI)) then
8767 Set_Has_Invariants (Derived_Type);
8768 Set_Has_Inheritable_Invariants (Derived_Type);
8769
8770 exit;
8771 end if;
8772
8773 Next_Elmt (AI);
8774 end loop;
8775 end;
8776 end if;
8777 end if;
8778
8779 -- A type extension is automatically Ghost when one of its
8780 -- progenitors is Ghost (SPARK RM 6.9(9)). This property is
8781 -- also inherited when the parent type is Ghost, but this is
8782 -- done in Build_Derived_Type as the mechanism also handles
8783 -- untagged derivations.
8784
8785 if Implements_Ghost_Interface (Derived_Type) then
8786 Set_Is_Ghost_Entity (Derived_Type);
8787 end if;
8788 end;
8789 end if;
8790
8791 else
8792 Set_Is_Packed (Derived_Type, Is_Packed (Parent_Base));
8793 Set_Has_Non_Standard_Rep
8794 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
8795 end if;
8796
8797 -- STEP 4: Inherit components from the parent base and constrain them.
8798 -- Apply the second transformation described in point 6. above.
8799
8800 if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
8801 or else not Has_Discriminants (Parent_Type)
8802 or else not Is_Constrained (Parent_Type)
8803 then
8804 Constrs := Discs;
8805 else
8806 Constrs := Discriminant_Constraint (Parent_Type);
8807 end if;
8808
8809 Assoc_List :=
8810 Inherit_Components
8811 (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
8812
8813 -- STEP 5a: Copy the parent record declaration for untagged types
8814
8815 if not Is_Tagged then
8816
8817 -- Discriminant_Constraint (Derived_Type) has been properly
8818 -- constructed. Save it and temporarily set it to Empty because we
8819 -- do not want the call to New_Copy_Tree below to mess this list.
8820
8821 if Has_Discriminants (Derived_Type) then
8822 Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
8823 Set_Discriminant_Constraint (Derived_Type, No_Elist);
8824 else
8825 Save_Discr_Constr := No_Elist;
8826 end if;
8827
8828 -- Save the Etype field of Derived_Type. It is correctly set now,
8829 -- but the call to New_Copy tree may remap it to point to itself,
8830 -- which is not what we want. Ditto for the Next_Entity field.
8831
8832 Save_Etype := Etype (Derived_Type);
8833 Save_Next_Entity := Next_Entity (Derived_Type);
8834
8835 -- Assoc_List maps all stored discriminants in the Parent_Base to
8836 -- stored discriminants in the Derived_Type. It is fundamental that
8837 -- no types or itypes with discriminants other than the stored
8838 -- discriminants appear in the entities declared inside
8839 -- Derived_Type, since the back end cannot deal with it.
8840
8841 New_Decl :=
8842 New_Copy_Tree
8843 (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
8844
8845 -- Restore the fields saved prior to the New_Copy_Tree call
8846 -- and compute the stored constraint.
8847
8848 Set_Etype (Derived_Type, Save_Etype);
8849 Set_Next_Entity (Derived_Type, Save_Next_Entity);
8850
8851 if Has_Discriminants (Derived_Type) then
8852 Set_Discriminant_Constraint
8853 (Derived_Type, Save_Discr_Constr);
8854 Set_Stored_Constraint
8855 (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
8856 Replace_Components (Derived_Type, New_Decl);
8857 Set_Has_Implicit_Dereference
8858 (Derived_Type, Has_Implicit_Dereference (Parent_Type));
8859 end if;
8860
8861 -- Insert the new derived type declaration
8862
8863 Rewrite (N, New_Decl);
8864
8865 -- STEP 5b: Complete the processing for record extensions in generics
8866
8867 -- There is no completion for record extensions declared in the
8868 -- parameter part of a generic, so we need to complete processing for
8869 -- these generic record extensions here. The Record_Type_Definition call
8870 -- will change the Ekind of the components from E_Void to E_Component.
8871
8872 elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
8873 Record_Type_Definition (Empty, Derived_Type);
8874
8875 -- STEP 5c: Process the record extension for non private tagged types
8876
8877 elsif not Private_Extension then
8878 Expand_Record_Extension (Derived_Type, Type_Def);
8879
8880 -- Note : previously in ASIS mode we set the Parent_Subtype of the
8881 -- derived type to propagate some semantic information. This led
8882 -- to other ASIS failures and has been removed.
8883
8884 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
8885 -- implemented interfaces if we are in expansion mode
8886
8887 if Expander_Active
8888 and then Has_Interfaces (Derived_Type)
8889 then
8890 Add_Interface_Tag_Components (N, Derived_Type);
8891 end if;
8892
8893 -- Analyze the record extension
8894
8895 Record_Type_Definition
8896 (Record_Extension_Part (Type_Def), Derived_Type);
8897 end if;
8898
8899 End_Scope;
8900
8901 -- Nothing else to do if there is an error in the derivation.
8902 -- An unusual case: the full view may be derived from a type in an
8903 -- instance, when the partial view was used illegally as an actual
8904 -- in that instance, leading to a circular definition.
8905
8906 if Etype (Derived_Type) = Any_Type
8907 or else Etype (Parent_Type) = Derived_Type
8908 then
8909 return;
8910 end if;
8911
8912 -- Set delayed freeze and then derive subprograms, we need to do
8913 -- this in this order so that derived subprograms inherit the
8914 -- derived freeze if necessary.
8915
8916 Set_Has_Delayed_Freeze (Derived_Type);
8917
8918 if Derive_Subps then
8919 Derive_Subprograms (Parent_Type, Derived_Type);
8920 end if;
8921
8922 -- If we have a private extension which defines a constrained derived
8923 -- type mark as constrained here after we have derived subprograms. See
8924 -- comment on point 9. just above the body of Build_Derived_Record_Type.
8925
8926 if Private_Extension and then Inherit_Discrims then
8927 if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
8928 Set_Is_Constrained (Derived_Type, True);
8929 Set_Discriminant_Constraint (Derived_Type, Discs);
8930
8931 elsif Is_Constrained (Parent_Type) then
8932 Set_Is_Constrained
8933 (Derived_Type, True);
8934 Set_Discriminant_Constraint
8935 (Derived_Type, Discriminant_Constraint (Parent_Type));
8936 end if;
8937 end if;
8938
8939 -- Update the class-wide type, which shares the now-completed entity
8940 -- list with its specific type. In case of underlying record views,
8941 -- we do not generate the corresponding class wide entity.
8942
8943 if Is_Tagged
8944 and then not Is_Underlying_Record_View (Derived_Type)
8945 then
8946 Set_First_Entity
8947 (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
8948 Set_Last_Entity
8949 (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
8950 end if;
8951
8952 Check_Function_Writable_Actuals (N);
8953 end Build_Derived_Record_Type;
8954
8955 ------------------------
8956 -- Build_Derived_Type --
8957 ------------------------
8958
8959 procedure Build_Derived_Type
8960 (N : Node_Id;
8961 Parent_Type : Entity_Id;
8962 Derived_Type : Entity_Id;
8963 Is_Completion : Boolean;
8964 Derive_Subps : Boolean := True)
8965 is
8966 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
8967
8968 begin
8969 -- Set common attributes
8970
8971 Set_Scope (Derived_Type, Current_Scope);
8972
8973 Set_Etype (Derived_Type, Parent_Base);
8974 Set_Ekind (Derived_Type, Ekind (Parent_Base));
8975 Set_Has_Task (Derived_Type, Has_Task (Parent_Base));
8976 Set_Has_Protected (Derived_Type, Has_Protected (Parent_Base));
8977
8978 Set_Size_Info (Derived_Type, Parent_Type);
8979 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
8980 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
8981 Set_Is_Tagged_Type (Derived_Type, Is_Tagged_Type (Parent_Type));
8982 Set_Is_Volatile (Derived_Type, Is_Volatile (Parent_Type));
8983
8984 if Is_Tagged_Type (Derived_Type) then
8985 Set_No_Tagged_Streams_Pragma
8986 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
8987 end if;
8988
8989 -- If the parent has primitive routines, set the derived type link
8990
8991 if Has_Primitive_Operations (Parent_Type) then
8992 Set_Derived_Type_Link (Parent_Base, Derived_Type);
8993 end if;
8994
8995 -- If the parent type is a private subtype, the convention on the base
8996 -- type may be set in the private part, and not propagated to the
8997 -- subtype until later, so we obtain the convention from the base type.
8998
8999 Set_Convention (Derived_Type, Convention (Parent_Base));
9000
9001 -- Set SSO default for record or array type
9002
9003 if (Is_Array_Type (Derived_Type) or else Is_Record_Type (Derived_Type))
9004 and then Is_Base_Type (Derived_Type)
9005 then
9006 Set_Default_SSO (Derived_Type);
9007 end if;
9008
9009 -- Propagate invariant information. The new type has invariants if
9010 -- they are inherited from the parent type, and these invariants can
9011 -- be further inherited, so both flags are set.
9012
9013 -- We similarly inherit predicates
9014
9015 if Has_Predicates (Parent_Type) then
9016 Set_Has_Predicates (Derived_Type);
9017 end if;
9018
9019 -- The derived type inherits the representation clauses of the parent
9020
9021 Inherit_Rep_Item_Chain (Derived_Type, Parent_Type);
9022
9023 -- Propagate the attributes related to pragma Default_Initial_Condition
9024 -- from the parent type to the private extension. A derived type always
9025 -- inherits the default initial condition flag from the parent type. If
9026 -- the derived type carries its own Default_Initial_Condition pragma,
9027 -- the flag is later reset in Analyze_Pragma. Note that both flags are
9028 -- mutually exclusive.
9029
9030 Propagate_Default_Init_Cond_Attributes
9031 (From_Typ => Parent_Type,
9032 To_Typ => Derived_Type,
9033 Parent_To_Derivation => True);
9034
9035 -- If the parent type has delayed rep aspects, then mark the derived
9036 -- type as possibly inheriting a delayed rep aspect.
9037
9038 if Has_Delayed_Rep_Aspects (Parent_Type) then
9039 Set_May_Inherit_Delayed_Rep_Aspects (Derived_Type);
9040 end if;
9041
9042 -- Propagate the attributes related to pragma Ghost from the parent type
9043 -- to the derived type or type extension (SPARK RM 6.9(9)).
9044
9045 if Is_Ghost_Entity (Parent_Type) then
9046 Set_Is_Ghost_Entity (Derived_Type);
9047 end if;
9048
9049 -- Type dependent processing
9050
9051 case Ekind (Parent_Type) is
9052 when Numeric_Kind =>
9053 Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
9054
9055 when Array_Kind =>
9056 Build_Derived_Array_Type (N, Parent_Type, Derived_Type);
9057
9058 when E_Record_Type
9059 | E_Record_Subtype
9060 | Class_Wide_Kind =>
9061 Build_Derived_Record_Type
9062 (N, Parent_Type, Derived_Type, Derive_Subps);
9063 return;
9064
9065 when Enumeration_Kind =>
9066 Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
9067
9068 when Access_Kind =>
9069 Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
9070
9071 when Incomplete_Or_Private_Kind =>
9072 Build_Derived_Private_Type
9073 (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
9074
9075 -- For discriminated types, the derivation includes deriving
9076 -- primitive operations. For others it is done below.
9077
9078 if Is_Tagged_Type (Parent_Type)
9079 or else Has_Discriminants (Parent_Type)
9080 or else (Present (Full_View (Parent_Type))
9081 and then Has_Discriminants (Full_View (Parent_Type)))
9082 then
9083 return;
9084 end if;
9085
9086 when Concurrent_Kind =>
9087 Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
9088
9089 when others =>
9090 raise Program_Error;
9091 end case;
9092
9093 -- Nothing more to do if some error occurred
9094
9095 if Etype (Derived_Type) = Any_Type then
9096 return;
9097 end if;
9098
9099 -- Set delayed freeze and then derive subprograms, we need to do this
9100 -- in this order so that derived subprograms inherit the derived freeze
9101 -- if necessary.
9102
9103 Set_Has_Delayed_Freeze (Derived_Type);
9104
9105 if Derive_Subps then
9106 Derive_Subprograms (Parent_Type, Derived_Type);
9107 end if;
9108
9109 Set_Has_Primitive_Operations
9110 (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
9111 end Build_Derived_Type;
9112
9113 -----------------------
9114 -- Build_Discriminal --
9115 -----------------------
9116
9117 procedure Build_Discriminal (Discrim : Entity_Id) is
9118 D_Minal : Entity_Id;
9119 CR_Disc : Entity_Id;
9120
9121 begin
9122 -- A discriminal has the same name as the discriminant
9123
9124 D_Minal := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9125
9126 Set_Ekind (D_Minal, E_In_Parameter);
9127 Set_Mechanism (D_Minal, Default_Mechanism);
9128 Set_Etype (D_Minal, Etype (Discrim));
9129 Set_Scope (D_Minal, Current_Scope);
9130
9131 Set_Discriminal (Discrim, D_Minal);
9132 Set_Discriminal_Link (D_Minal, Discrim);
9133
9134 -- For task types, build at once the discriminants of the corresponding
9135 -- record, which are needed if discriminants are used in entry defaults
9136 -- and in family bounds.
9137
9138 if Is_Concurrent_Type (Current_Scope)
9139 or else
9140 Is_Limited_Type (Current_Scope)
9141 then
9142 CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9143
9144 Set_Ekind (CR_Disc, E_In_Parameter);
9145 Set_Mechanism (CR_Disc, Default_Mechanism);
9146 Set_Etype (CR_Disc, Etype (Discrim));
9147 Set_Scope (CR_Disc, Current_Scope);
9148 Set_Discriminal_Link (CR_Disc, Discrim);
9149 Set_CR_Discriminant (Discrim, CR_Disc);
9150 end if;
9151 end Build_Discriminal;
9152
9153 ------------------------------------
9154 -- Build_Discriminant_Constraints --
9155 ------------------------------------
9156
9157 function Build_Discriminant_Constraints
9158 (T : Entity_Id;
9159 Def : Node_Id;
9160 Derived_Def : Boolean := False) return Elist_Id
9161 is
9162 C : constant Node_Id := Constraint (Def);
9163 Nb_Discr : constant Nat := Number_Discriminants (T);
9164
9165 Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
9166 -- Saves the expression corresponding to a given discriminant in T
9167
9168 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
9169 -- Return the Position number within array Discr_Expr of a discriminant
9170 -- D within the discriminant list of the discriminated type T.
9171
9172 procedure Process_Discriminant_Expression
9173 (Expr : Node_Id;
9174 D : Entity_Id);
9175 -- If this is a discriminant constraint on a partial view, do not
9176 -- generate an overflow check on the discriminant expression. The check
9177 -- will be generated when constraining the full view. Otherwise the
9178 -- backend creates duplicate symbols for the temporaries corresponding
9179 -- to the expressions to be checked, causing spurious assembler errors.
9180
9181 ------------------
9182 -- Pos_Of_Discr --
9183 ------------------
9184
9185 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
9186 Disc : Entity_Id;
9187
9188 begin
9189 Disc := First_Discriminant (T);
9190 for J in Discr_Expr'Range loop
9191 if Disc = D then
9192 return J;
9193 end if;
9194
9195 Next_Discriminant (Disc);
9196 end loop;
9197
9198 -- Note: Since this function is called on discriminants that are
9199 -- known to belong to the discriminated type, falling through the
9200 -- loop with no match signals an internal compiler error.
9201
9202 raise Program_Error;
9203 end Pos_Of_Discr;
9204
9205 -------------------------------------
9206 -- Process_Discriminant_Expression --
9207 -------------------------------------
9208
9209 procedure Process_Discriminant_Expression
9210 (Expr : Node_Id;
9211 D : Entity_Id)
9212 is
9213 BDT : constant Entity_Id := Base_Type (Etype (D));
9214
9215 begin
9216 -- If this is a discriminant constraint on a partial view, do
9217 -- not generate an overflow on the discriminant expression. The
9218 -- check will be generated when constraining the full view.
9219
9220 if Is_Private_Type (T)
9221 and then Present (Full_View (T))
9222 then
9223 Analyze_And_Resolve (Expr, BDT, Suppress => Overflow_Check);
9224 else
9225 Analyze_And_Resolve (Expr, BDT);
9226 end if;
9227 end Process_Discriminant_Expression;
9228
9229 -- Declarations local to Build_Discriminant_Constraints
9230
9231 Discr : Entity_Id;
9232 E : Entity_Id;
9233 Elist : constant Elist_Id := New_Elmt_List;
9234
9235 Constr : Node_Id;
9236 Expr : Node_Id;
9237 Id : Node_Id;
9238 Position : Nat;
9239 Found : Boolean;
9240
9241 Discrim_Present : Boolean := False;
9242
9243 -- Start of processing for Build_Discriminant_Constraints
9244
9245 begin
9246 -- The following loop will process positional associations only.
9247 -- For a positional association, the (single) discriminant is
9248 -- implicitly specified by position, in textual order (RM 3.7.2).
9249
9250 Discr := First_Discriminant (T);
9251 Constr := First (Constraints (C));
9252 for D in Discr_Expr'Range loop
9253 exit when Nkind (Constr) = N_Discriminant_Association;
9254
9255 if No (Constr) then
9256 Error_Msg_N ("too few discriminants given in constraint", C);
9257 return New_Elmt_List;
9258
9259 elsif Nkind (Constr) = N_Range
9260 or else (Nkind (Constr) = N_Attribute_Reference
9261 and then Attribute_Name (Constr) = Name_Range)
9262 then
9263 Error_Msg_N
9264 ("a range is not a valid discriminant constraint", Constr);
9265 Discr_Expr (D) := Error;
9266
9267 else
9268 Process_Discriminant_Expression (Constr, Discr);
9269 Discr_Expr (D) := Constr;
9270 end if;
9271
9272 Next_Discriminant (Discr);
9273 Next (Constr);
9274 end loop;
9275
9276 if No (Discr) and then Present (Constr) then
9277 Error_Msg_N ("too many discriminants given in constraint", Constr);
9278 return New_Elmt_List;
9279 end if;
9280
9281 -- Named associations can be given in any order, but if both positional
9282 -- and named associations are used in the same discriminant constraint,
9283 -- then positional associations must occur first, at their normal
9284 -- position. Hence once a named association is used, the rest of the
9285 -- discriminant constraint must use only named associations.
9286
9287 while Present (Constr) loop
9288
9289 -- Positional association forbidden after a named association
9290
9291 if Nkind (Constr) /= N_Discriminant_Association then
9292 Error_Msg_N ("positional association follows named one", Constr);
9293 return New_Elmt_List;
9294
9295 -- Otherwise it is a named association
9296
9297 else
9298 -- E records the type of the discriminants in the named
9299 -- association. All the discriminants specified in the same name
9300 -- association must have the same type.
9301
9302 E := Empty;
9303
9304 -- Search the list of discriminants in T to see if the simple name
9305 -- given in the constraint matches any of them.
9306
9307 Id := First (Selector_Names (Constr));
9308 while Present (Id) loop
9309 Found := False;
9310
9311 -- If Original_Discriminant is present, we are processing a
9312 -- generic instantiation and this is an instance node. We need
9313 -- to find the name of the corresponding discriminant in the
9314 -- actual record type T and not the name of the discriminant in
9315 -- the generic formal. Example:
9316
9317 -- generic
9318 -- type G (D : int) is private;
9319 -- package P is
9320 -- subtype W is G (D => 1);
9321 -- end package;
9322 -- type Rec (X : int) is record ... end record;
9323 -- package Q is new P (G => Rec);
9324
9325 -- At the point of the instantiation, formal type G is Rec
9326 -- and therefore when reanalyzing "subtype W is G (D => 1);"
9327 -- which really looks like "subtype W is Rec (D => 1);" at
9328 -- the point of instantiation, we want to find the discriminant
9329 -- that corresponds to D in Rec, i.e. X.
9330
9331 if Present (Original_Discriminant (Id))
9332 and then In_Instance
9333 then
9334 Discr := Find_Corresponding_Discriminant (Id, T);
9335 Found := True;
9336
9337 else
9338 Discr := First_Discriminant (T);
9339 while Present (Discr) loop
9340 if Chars (Discr) = Chars (Id) then
9341 Found := True;
9342 exit;
9343 end if;
9344
9345 Next_Discriminant (Discr);
9346 end loop;
9347
9348 if not Found then
9349 Error_Msg_N ("& does not match any discriminant", Id);
9350 return New_Elmt_List;
9351
9352 -- If the parent type is a generic formal, preserve the
9353 -- name of the discriminant for subsequent instances.
9354 -- see comment at the beginning of this if statement.
9355
9356 elsif Is_Generic_Type (Root_Type (T)) then
9357 Set_Original_Discriminant (Id, Discr);
9358 end if;
9359 end if;
9360
9361 Position := Pos_Of_Discr (T, Discr);
9362
9363 if Present (Discr_Expr (Position)) then
9364 Error_Msg_N ("duplicate constraint for discriminant&", Id);
9365
9366 else
9367 -- Each discriminant specified in the same named association
9368 -- must be associated with a separate copy of the
9369 -- corresponding expression.
9370
9371 if Present (Next (Id)) then
9372 Expr := New_Copy_Tree (Expression (Constr));
9373 Set_Parent (Expr, Parent (Expression (Constr)));
9374 else
9375 Expr := Expression (Constr);
9376 end if;
9377
9378 Discr_Expr (Position) := Expr;
9379 Process_Discriminant_Expression (Expr, Discr);
9380 end if;
9381
9382 -- A discriminant association with more than one discriminant
9383 -- name is only allowed if the named discriminants are all of
9384 -- the same type (RM 3.7.1(8)).
9385
9386 if E = Empty then
9387 E := Base_Type (Etype (Discr));
9388
9389 elsif Base_Type (Etype (Discr)) /= E then
9390 Error_Msg_N
9391 ("all discriminants in an association " &
9392 "must have the same type", Id);
9393 end if;
9394
9395 Next (Id);
9396 end loop;
9397 end if;
9398
9399 Next (Constr);
9400 end loop;
9401
9402 -- A discriminant constraint must provide exactly one value for each
9403 -- discriminant of the type (RM 3.7.1(8)).
9404
9405 for J in Discr_Expr'Range loop
9406 if No (Discr_Expr (J)) then
9407 Error_Msg_N ("too few discriminants given in constraint", C);
9408 return New_Elmt_List;
9409 end if;
9410 end loop;
9411
9412 -- Determine if there are discriminant expressions in the constraint
9413
9414 for J in Discr_Expr'Range loop
9415 if Denotes_Discriminant
9416 (Discr_Expr (J), Check_Concurrent => True)
9417 then
9418 Discrim_Present := True;
9419 end if;
9420 end loop;
9421
9422 -- Build an element list consisting of the expressions given in the
9423 -- discriminant constraint and apply the appropriate checks. The list
9424 -- is constructed after resolving any named discriminant associations
9425 -- and therefore the expressions appear in the textual order of the
9426 -- discriminants.
9427
9428 Discr := First_Discriminant (T);
9429 for J in Discr_Expr'Range loop
9430 if Discr_Expr (J) /= Error then
9431 Append_Elmt (Discr_Expr (J), Elist);
9432
9433 -- If any of the discriminant constraints is given by a
9434 -- discriminant and we are in a derived type declaration we
9435 -- have a discriminant renaming. Establish link between new
9436 -- and old discriminant.
9437
9438 if Denotes_Discriminant (Discr_Expr (J)) then
9439 if Derived_Def then
9440 Set_Corresponding_Discriminant
9441 (Entity (Discr_Expr (J)), Discr);
9442 end if;
9443
9444 -- Force the evaluation of non-discriminant expressions.
9445 -- If we have found a discriminant in the constraint 3.4(26)
9446 -- and 3.8(18) demand that no range checks are performed are
9447 -- after evaluation. If the constraint is for a component
9448 -- definition that has a per-object constraint, expressions are
9449 -- evaluated but not checked either. In all other cases perform
9450 -- a range check.
9451
9452 else
9453 if Discrim_Present then
9454 null;
9455
9456 elsif Nkind (Parent (Parent (Def))) = N_Component_Declaration
9457 and then
9458 Has_Per_Object_Constraint
9459 (Defining_Identifier (Parent (Parent (Def))))
9460 then
9461 null;
9462
9463 elsif Is_Access_Type (Etype (Discr)) then
9464 Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
9465
9466 else
9467 Apply_Range_Check (Discr_Expr (J), Etype (Discr));
9468 end if;
9469
9470 Force_Evaluation (Discr_Expr (J));
9471 end if;
9472
9473 -- Check that the designated type of an access discriminant's
9474 -- expression is not a class-wide type unless the discriminant's
9475 -- designated type is also class-wide.
9476
9477 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
9478 and then not Is_Class_Wide_Type
9479 (Designated_Type (Etype (Discr)))
9480 and then Etype (Discr_Expr (J)) /= Any_Type
9481 and then Is_Class_Wide_Type
9482 (Designated_Type (Etype (Discr_Expr (J))))
9483 then
9484 Wrong_Type (Discr_Expr (J), Etype (Discr));
9485
9486 elsif Is_Access_Type (Etype (Discr))
9487 and then not Is_Access_Constant (Etype (Discr))
9488 and then Is_Access_Type (Etype (Discr_Expr (J)))
9489 and then Is_Access_Constant (Etype (Discr_Expr (J)))
9490 then
9491 Error_Msg_NE
9492 ("constraint for discriminant& must be access to variable",
9493 Def, Discr);
9494 end if;
9495 end if;
9496
9497 Next_Discriminant (Discr);
9498 end loop;
9499
9500 return Elist;
9501 end Build_Discriminant_Constraints;
9502
9503 ---------------------------------
9504 -- Build_Discriminated_Subtype --
9505 ---------------------------------
9506
9507 procedure Build_Discriminated_Subtype
9508 (T : Entity_Id;
9509 Def_Id : Entity_Id;
9510 Elist : Elist_Id;
9511 Related_Nod : Node_Id;
9512 For_Access : Boolean := False)
9513 is
9514 Has_Discrs : constant Boolean := Has_Discriminants (T);
9515 Constrained : constant Boolean :=
9516 (Has_Discrs
9517 and then not Is_Empty_Elmt_List (Elist)
9518 and then not Is_Class_Wide_Type (T))
9519 or else Is_Constrained (T);
9520
9521 begin
9522 if Ekind (T) = E_Record_Type then
9523 if For_Access then
9524 Set_Ekind (Def_Id, E_Private_Subtype);
9525 Set_Is_For_Access_Subtype (Def_Id, True);
9526 else
9527 Set_Ekind (Def_Id, E_Record_Subtype);
9528 end if;
9529
9530 -- Inherit preelaboration flag from base, for types for which it
9531 -- may have been set: records, private types, protected types.
9532
9533 Set_Known_To_Have_Preelab_Init
9534 (Def_Id, Known_To_Have_Preelab_Init (T));
9535
9536 elsif Ekind (T) = E_Task_Type then
9537 Set_Ekind (Def_Id, E_Task_Subtype);
9538
9539 elsif Ekind (T) = E_Protected_Type then
9540 Set_Ekind (Def_Id, E_Protected_Subtype);
9541 Set_Known_To_Have_Preelab_Init
9542 (Def_Id, Known_To_Have_Preelab_Init (T));
9543
9544 elsif Is_Private_Type (T) then
9545 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
9546 Set_Known_To_Have_Preelab_Init
9547 (Def_Id, Known_To_Have_Preelab_Init (T));
9548
9549 -- Private subtypes may have private dependents
9550
9551 Set_Private_Dependents (Def_Id, New_Elmt_List);
9552
9553 elsif Is_Class_Wide_Type (T) then
9554 Set_Ekind (Def_Id, E_Class_Wide_Subtype);
9555
9556 else
9557 -- Incomplete type. Attach subtype to list of dependents, to be
9558 -- completed with full view of parent type, unless is it the
9559 -- designated subtype of a record component within an init_proc.
9560 -- This last case arises for a component of an access type whose
9561 -- designated type is incomplete (e.g. a Taft Amendment type).
9562 -- The designated subtype is within an inner scope, and needs no
9563 -- elaboration, because only the access type is needed in the
9564 -- initialization procedure.
9565
9566 Set_Ekind (Def_Id, Ekind (T));
9567
9568 if For_Access and then Within_Init_Proc then
9569 null;
9570 else
9571 Append_Elmt (Def_Id, Private_Dependents (T));
9572 end if;
9573 end if;
9574
9575 Set_Etype (Def_Id, T);
9576 Init_Size_Align (Def_Id);
9577 Set_Has_Discriminants (Def_Id, Has_Discrs);
9578 Set_Is_Constrained (Def_Id, Constrained);
9579
9580 Set_First_Entity (Def_Id, First_Entity (T));
9581 Set_Last_Entity (Def_Id, Last_Entity (T));
9582 Set_Has_Implicit_Dereference
9583 (Def_Id, Has_Implicit_Dereference (T));
9584
9585 -- If the subtype is the completion of a private declaration, there may
9586 -- have been representation clauses for the partial view, and they must
9587 -- be preserved. Build_Derived_Type chains the inherited clauses with
9588 -- the ones appearing on the extension. If this comes from a subtype
9589 -- declaration, all clauses are inherited.
9590
9591 if No (First_Rep_Item (Def_Id)) then
9592 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
9593 end if;
9594
9595 if Is_Tagged_Type (T) then
9596 Set_Is_Tagged_Type (Def_Id);
9597 Set_No_Tagged_Streams_Pragma (Def_Id, No_Tagged_Streams_Pragma (T));
9598 Make_Class_Wide_Type (Def_Id);
9599 end if;
9600
9601 Set_Stored_Constraint (Def_Id, No_Elist);
9602
9603 if Has_Discrs then
9604 Set_Discriminant_Constraint (Def_Id, Elist);
9605 Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
9606 end if;
9607
9608 if Is_Tagged_Type (T) then
9609
9610 -- Ada 2005 (AI-251): In case of concurrent types we inherit the
9611 -- concurrent record type (which has the list of primitive
9612 -- operations).
9613
9614 if Ada_Version >= Ada_2005
9615 and then Is_Concurrent_Type (T)
9616 then
9617 Set_Corresponding_Record_Type (Def_Id,
9618 Corresponding_Record_Type (T));
9619 else
9620 Set_Direct_Primitive_Operations (Def_Id,
9621 Direct_Primitive_Operations (T));
9622 end if;
9623
9624 Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
9625 end if;
9626
9627 -- Subtypes introduced by component declarations do not need to be
9628 -- marked as delayed, and do not get freeze nodes, because the semantics
9629 -- verifies that the parents of the subtypes are frozen before the
9630 -- enclosing record is frozen.
9631
9632 if not Is_Type (Scope (Def_Id)) then
9633 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
9634
9635 if Is_Private_Type (T)
9636 and then Present (Full_View (T))
9637 then
9638 Conditional_Delay (Def_Id, Full_View (T));
9639 else
9640 Conditional_Delay (Def_Id, T);
9641 end if;
9642 end if;
9643
9644 if Is_Record_Type (T) then
9645 Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
9646
9647 if Has_Discrs
9648 and then not Is_Empty_Elmt_List (Elist)
9649 and then not For_Access
9650 then
9651 Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
9652 elsif not For_Access then
9653 Set_Cloned_Subtype (Def_Id, T);
9654 end if;
9655 end if;
9656 end Build_Discriminated_Subtype;
9657
9658 ---------------------------
9659 -- Build_Itype_Reference --
9660 ---------------------------
9661
9662 procedure Build_Itype_Reference
9663 (Ityp : Entity_Id;
9664 Nod : Node_Id)
9665 is
9666 IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
9667 begin
9668
9669 -- Itype references are only created for use by the back-end
9670
9671 if Inside_A_Generic then
9672 return;
9673 else
9674 Set_Itype (IR, Ityp);
9675 Insert_After (Nod, IR);
9676 end if;
9677 end Build_Itype_Reference;
9678
9679 ------------------------
9680 -- Build_Scalar_Bound --
9681 ------------------------
9682
9683 function Build_Scalar_Bound
9684 (Bound : Node_Id;
9685 Par_T : Entity_Id;
9686 Der_T : Entity_Id) return Node_Id
9687 is
9688 New_Bound : Entity_Id;
9689
9690 begin
9691 -- Note: not clear why this is needed, how can the original bound
9692 -- be unanalyzed at this point? and if it is, what business do we
9693 -- have messing around with it? and why is the base type of the
9694 -- parent type the right type for the resolution. It probably is
9695 -- not. It is OK for the new bound we are creating, but not for
9696 -- the old one??? Still if it never happens, no problem.
9697
9698 Analyze_And_Resolve (Bound, Base_Type (Par_T));
9699
9700 if Nkind_In (Bound, N_Integer_Literal, N_Real_Literal) then
9701 New_Bound := New_Copy (Bound);
9702 Set_Etype (New_Bound, Der_T);
9703 Set_Analyzed (New_Bound);
9704
9705 elsif Is_Entity_Name (Bound) then
9706 New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
9707
9708 -- The following is almost certainly wrong. What business do we have
9709 -- relocating a node (Bound) that is presumably still attached to
9710 -- the tree elsewhere???
9711
9712 else
9713 New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
9714 end if;
9715
9716 Set_Etype (New_Bound, Der_T);
9717 return New_Bound;
9718 end Build_Scalar_Bound;
9719
9720 --------------------------------
9721 -- Build_Underlying_Full_View --
9722 --------------------------------
9723
9724 procedure Build_Underlying_Full_View
9725 (N : Node_Id;
9726 Typ : Entity_Id;
9727 Par : Entity_Id)
9728 is
9729 Loc : constant Source_Ptr := Sloc (N);
9730 Subt : constant Entity_Id :=
9731 Make_Defining_Identifier
9732 (Loc, New_External_Name (Chars (Typ), 'S'));
9733
9734 Constr : Node_Id;
9735 Indic : Node_Id;
9736 C : Node_Id;
9737 Id : Node_Id;
9738
9739 procedure Set_Discriminant_Name (Id : Node_Id);
9740 -- If the derived type has discriminants, they may rename discriminants
9741 -- of the parent. When building the full view of the parent, we need to
9742 -- recover the names of the original discriminants if the constraint is
9743 -- given by named associations.
9744
9745 ---------------------------
9746 -- Set_Discriminant_Name --
9747 ---------------------------
9748
9749 procedure Set_Discriminant_Name (Id : Node_Id) is
9750 Disc : Entity_Id;
9751
9752 begin
9753 Set_Original_Discriminant (Id, Empty);
9754
9755 if Has_Discriminants (Typ) then
9756 Disc := First_Discriminant (Typ);
9757 while Present (Disc) loop
9758 if Chars (Disc) = Chars (Id)
9759 and then Present (Corresponding_Discriminant (Disc))
9760 then
9761 Set_Chars (Id, Chars (Corresponding_Discriminant (Disc)));
9762 end if;
9763 Next_Discriminant (Disc);
9764 end loop;
9765 end if;
9766 end Set_Discriminant_Name;
9767
9768 -- Start of processing for Build_Underlying_Full_View
9769
9770 begin
9771 if Nkind (N) = N_Full_Type_Declaration then
9772 Constr := Constraint (Subtype_Indication (Type_Definition (N)));
9773
9774 elsif Nkind (N) = N_Subtype_Declaration then
9775 Constr := New_Copy_Tree (Constraint (Subtype_Indication (N)));
9776
9777 elsif Nkind (N) = N_Component_Declaration then
9778 Constr :=
9779 New_Copy_Tree
9780 (Constraint (Subtype_Indication (Component_Definition (N))));
9781
9782 else
9783 raise Program_Error;
9784 end if;
9785
9786 C := First (Constraints (Constr));
9787 while Present (C) loop
9788 if Nkind (C) = N_Discriminant_Association then
9789 Id := First (Selector_Names (C));
9790 while Present (Id) loop
9791 Set_Discriminant_Name (Id);
9792 Next (Id);
9793 end loop;
9794 end if;
9795
9796 Next (C);
9797 end loop;
9798
9799 Indic :=
9800 Make_Subtype_Declaration (Loc,
9801 Defining_Identifier => Subt,
9802 Subtype_Indication =>
9803 Make_Subtype_Indication (Loc,
9804 Subtype_Mark => New_Occurrence_Of (Par, Loc),
9805 Constraint => New_Copy_Tree (Constr)));
9806
9807 -- If this is a component subtype for an outer itype, it is not
9808 -- a list member, so simply set the parent link for analysis: if
9809 -- the enclosing type does not need to be in a declarative list,
9810 -- neither do the components.
9811
9812 if Is_List_Member (N)
9813 and then Nkind (N) /= N_Component_Declaration
9814 then
9815 Insert_Before (N, Indic);
9816 else
9817 Set_Parent (Indic, Parent (N));
9818 end if;
9819
9820 Analyze (Indic);
9821 Set_Underlying_Full_View (Typ, Full_View (Subt));
9822 end Build_Underlying_Full_View;
9823
9824 -------------------------------
9825 -- Check_Abstract_Overriding --
9826 -------------------------------
9827
9828 procedure Check_Abstract_Overriding (T : Entity_Id) is
9829 Alias_Subp : Entity_Id;
9830 Elmt : Elmt_Id;
9831 Op_List : Elist_Id;
9832 Subp : Entity_Id;
9833 Type_Def : Node_Id;
9834
9835 procedure Check_Pragma_Implemented (Subp : Entity_Id);
9836 -- Ada 2012 (AI05-0030): Subprogram Subp overrides an interface routine
9837 -- which has pragma Implemented already set. Check whether Subp's entity
9838 -- kind conforms to the implementation kind of the overridden routine.
9839
9840 procedure Check_Pragma_Implemented
9841 (Subp : Entity_Id;
9842 Iface_Subp : Entity_Id);
9843 -- Ada 2012 (AI05-0030): Subprogram Subp overrides interface routine
9844 -- Iface_Subp and both entities have pragma Implemented already set on
9845 -- them. Check whether the two implementation kinds are conforming.
9846
9847 procedure Inherit_Pragma_Implemented
9848 (Subp : Entity_Id;
9849 Iface_Subp : Entity_Id);
9850 -- Ada 2012 (AI05-0030): Interface primitive Subp overrides interface
9851 -- subprogram Iface_Subp which has been marked by pragma Implemented.
9852 -- Propagate the implementation kind of Iface_Subp to Subp.
9853
9854 ------------------------------
9855 -- Check_Pragma_Implemented --
9856 ------------------------------
9857
9858 procedure Check_Pragma_Implemented (Subp : Entity_Id) is
9859 Iface_Alias : constant Entity_Id := Interface_Alias (Subp);
9860 Impl_Kind : constant Name_Id := Implementation_Kind (Iface_Alias);
9861 Subp_Alias : constant Entity_Id := Alias (Subp);
9862 Contr_Typ : Entity_Id;
9863 Impl_Subp : Entity_Id;
9864
9865 begin
9866 -- Subp must have an alias since it is a hidden entity used to link
9867 -- an interface subprogram to its overriding counterpart.
9868
9869 pragma Assert (Present (Subp_Alias));
9870
9871 -- Handle aliases to synchronized wrappers
9872
9873 Impl_Subp := Subp_Alias;
9874
9875 if Is_Primitive_Wrapper (Impl_Subp) then
9876 Impl_Subp := Wrapped_Entity (Impl_Subp);
9877 end if;
9878
9879 -- Extract the type of the controlling formal
9880
9881 Contr_Typ := Etype (First_Formal (Subp_Alias));
9882
9883 if Is_Concurrent_Record_Type (Contr_Typ) then
9884 Contr_Typ := Corresponding_Concurrent_Type (Contr_Typ);
9885 end if;
9886
9887 -- An interface subprogram whose implementation kind is By_Entry must
9888 -- be implemented by an entry.
9889
9890 if Impl_Kind = Name_By_Entry
9891 and then Ekind (Impl_Subp) /= E_Entry
9892 then
9893 Error_Msg_Node_2 := Iface_Alias;
9894 Error_Msg_NE
9895 ("type & must implement abstract subprogram & with an entry",
9896 Subp_Alias, Contr_Typ);
9897
9898 elsif Impl_Kind = Name_By_Protected_Procedure then
9899
9900 -- An interface subprogram whose implementation kind is By_
9901 -- Protected_Procedure cannot be implemented by a primitive
9902 -- procedure of a task type.
9903
9904 if Ekind (Contr_Typ) /= E_Protected_Type then
9905 Error_Msg_Node_2 := Contr_Typ;
9906 Error_Msg_NE
9907 ("interface subprogram & cannot be implemented by a " &
9908 "primitive procedure of task type &", Subp_Alias,
9909 Iface_Alias);
9910
9911 -- An interface subprogram whose implementation kind is By_
9912 -- Protected_Procedure must be implemented by a procedure.
9913
9914 elsif Ekind (Impl_Subp) /= E_Procedure then
9915 Error_Msg_Node_2 := Iface_Alias;
9916 Error_Msg_NE
9917 ("type & must implement abstract subprogram & with a " &
9918 "procedure", Subp_Alias, Contr_Typ);
9919
9920 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
9921 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
9922 then
9923 Error_Msg_Name_1 := Impl_Kind;
9924 Error_Msg_N
9925 ("overriding operation& must have synchronization%",
9926 Subp_Alias);
9927 end if;
9928
9929 -- If primitive has Optional synchronization, overriding operation
9930 -- must match if it has an explicit synchronization..
9931
9932 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
9933 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
9934 then
9935 Error_Msg_Name_1 := Impl_Kind;
9936 Error_Msg_N
9937 ("overriding operation& must have syncrhonization%",
9938 Subp_Alias);
9939 end if;
9940 end Check_Pragma_Implemented;
9941
9942 ------------------------------
9943 -- Check_Pragma_Implemented --
9944 ------------------------------
9945
9946 procedure Check_Pragma_Implemented
9947 (Subp : Entity_Id;
9948 Iface_Subp : Entity_Id)
9949 is
9950 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
9951 Subp_Kind : constant Name_Id := Implementation_Kind (Subp);
9952
9953 begin
9954 -- Ada 2012 (AI05-0030): The implementation kinds of an overridden
9955 -- and overriding subprogram are different. In general this is an
9956 -- error except when the implementation kind of the overridden
9957 -- subprograms is By_Any or Optional.
9958
9959 if Iface_Kind /= Subp_Kind
9960 and then Iface_Kind /= Name_By_Any
9961 and then Iface_Kind /= Name_Optional
9962 then
9963 if Iface_Kind = Name_By_Entry then
9964 Error_Msg_N
9965 ("incompatible implementation kind, overridden subprogram " &
9966 "is marked By_Entry", Subp);
9967 else
9968 Error_Msg_N
9969 ("incompatible implementation kind, overridden subprogram " &
9970 "is marked By_Protected_Procedure", Subp);
9971 end if;
9972 end if;
9973 end Check_Pragma_Implemented;
9974
9975 --------------------------------
9976 -- Inherit_Pragma_Implemented --
9977 --------------------------------
9978
9979 procedure Inherit_Pragma_Implemented
9980 (Subp : Entity_Id;
9981 Iface_Subp : Entity_Id)
9982 is
9983 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
9984 Loc : constant Source_Ptr := Sloc (Subp);
9985 Impl_Prag : Node_Id;
9986
9987 begin
9988 -- Since the implementation kind is stored as a representation item
9989 -- rather than a flag, create a pragma node.
9990
9991 Impl_Prag :=
9992 Make_Pragma (Loc,
9993 Chars => Name_Implemented,
9994 Pragma_Argument_Associations => New_List (
9995 Make_Pragma_Argument_Association (Loc,
9996 Expression => New_Occurrence_Of (Subp, Loc)),
9997
9998 Make_Pragma_Argument_Association (Loc,
9999 Expression => Make_Identifier (Loc, Iface_Kind))));
10000
10001 -- The pragma doesn't need to be analyzed because it is internally
10002 -- built. It is safe to directly register it as a rep item since we
10003 -- are only interested in the characters of the implementation kind.
10004
10005 Record_Rep_Item (Subp, Impl_Prag);
10006 end Inherit_Pragma_Implemented;
10007
10008 -- Start of processing for Check_Abstract_Overriding
10009
10010 begin
10011 Op_List := Primitive_Operations (T);
10012
10013 -- Loop to check primitive operations
10014
10015 Elmt := First_Elmt (Op_List);
10016 while Present (Elmt) loop
10017 Subp := Node (Elmt);
10018 Alias_Subp := Alias (Subp);
10019
10020 -- Inherited subprograms are identified by the fact that they do not
10021 -- come from source, and the associated source location is the
10022 -- location of the first subtype of the derived type.
10023
10024 -- Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
10025 -- subprograms that "require overriding".
10026
10027 -- Special exception, do not complain about failure to override the
10028 -- stream routines _Input and _Output, as well as the primitive
10029 -- operations used in dispatching selects since we always provide
10030 -- automatic overridings for these subprograms.
10031
10032 -- Also ignore this rule for convention CIL since .NET libraries
10033 -- do bizarre things with interfaces???
10034
10035 -- The partial view of T may have been a private extension, for
10036 -- which inherited functions dispatching on result are abstract.
10037 -- If the full view is a null extension, there is no need for
10038 -- overriding in Ada 2005, but wrappers need to be built for them
10039 -- (see exp_ch3, Build_Controlling_Function_Wrappers).
10040
10041 if Is_Null_Extension (T)
10042 and then Has_Controlling_Result (Subp)
10043 and then Ada_Version >= Ada_2005
10044 and then Present (Alias_Subp)
10045 and then not Comes_From_Source (Subp)
10046 and then not Is_Abstract_Subprogram (Alias_Subp)
10047 and then not Is_Access_Type (Etype (Subp))
10048 then
10049 null;
10050
10051 -- Ada 2005 (AI-251): Internal entities of interfaces need no
10052 -- processing because this check is done with the aliased
10053 -- entity
10054
10055 elsif Present (Interface_Alias (Subp)) then
10056 null;
10057
10058 elsif (Is_Abstract_Subprogram (Subp)
10059 or else Requires_Overriding (Subp)
10060 or else
10061 (Has_Controlling_Result (Subp)
10062 and then Present (Alias_Subp)
10063 and then not Comes_From_Source (Subp)
10064 and then Sloc (Subp) = Sloc (First_Subtype (T))))
10065 and then not Is_TSS (Subp, TSS_Stream_Input)
10066 and then not Is_TSS (Subp, TSS_Stream_Output)
10067 and then not Is_Abstract_Type (T)
10068 and then Convention (T) /= Convention_CIL
10069 and then not Is_Predefined_Interface_Primitive (Subp)
10070
10071 -- Ada 2005 (AI-251): Do not consider hidden entities associated
10072 -- with abstract interface types because the check will be done
10073 -- with the aliased entity (otherwise we generate a duplicated
10074 -- error message).
10075
10076 and then not Present (Interface_Alias (Subp))
10077 then
10078 if Present (Alias_Subp) then
10079
10080 -- Only perform the check for a derived subprogram when the
10081 -- type has an explicit record extension. This avoids incorrect
10082 -- flagging of abstract subprograms for the case of a type
10083 -- without an extension that is derived from a formal type
10084 -- with a tagged actual (can occur within a private part).
10085
10086 -- Ada 2005 (AI-391): In the case of an inherited function with
10087 -- a controlling result of the type, the rule does not apply if
10088 -- the type is a null extension (unless the parent function
10089 -- itself is abstract, in which case the function must still be
10090 -- be overridden). The expander will generate an overriding
10091 -- wrapper function calling the parent subprogram (see
10092 -- Exp_Ch3.Make_Controlling_Wrapper_Functions).
10093
10094 Type_Def := Type_Definition (Parent (T));
10095
10096 if Nkind (Type_Def) = N_Derived_Type_Definition
10097 and then Present (Record_Extension_Part (Type_Def))
10098 and then
10099 (Ada_Version < Ada_2005
10100 or else not Is_Null_Extension (T)
10101 or else Ekind (Subp) = E_Procedure
10102 or else not Has_Controlling_Result (Subp)
10103 or else Is_Abstract_Subprogram (Alias_Subp)
10104 or else Requires_Overriding (Subp)
10105 or else Is_Access_Type (Etype (Subp)))
10106 then
10107 -- Avoid reporting error in case of abstract predefined
10108 -- primitive inherited from interface type because the
10109 -- body of internally generated predefined primitives
10110 -- of tagged types are generated later by Freeze_Type
10111
10112 if Is_Interface (Root_Type (T))
10113 and then Is_Abstract_Subprogram (Subp)
10114 and then Is_Predefined_Dispatching_Operation (Subp)
10115 and then not Comes_From_Source (Ultimate_Alias (Subp))
10116 then
10117 null;
10118
10119 -- A null extension is not obliged to override an inherited
10120 -- procedure subject to pragma Extensions_Visible with value
10121 -- False and at least one controlling OUT parameter
10122 -- (SPARK RM 6.1.7(6)).
10123
10124 elsif Is_Null_Extension (T)
10125 and then Is_EVF_Procedure (Subp)
10126 then
10127 null;
10128
10129 else
10130 Error_Msg_NE
10131 ("type must be declared abstract or & overridden",
10132 T, Subp);
10133
10134 -- Traverse the whole chain of aliased subprograms to
10135 -- complete the error notification. This is especially
10136 -- useful for traceability of the chain of entities when
10137 -- the subprogram corresponds with an interface
10138 -- subprogram (which may be defined in another package).
10139
10140 if Present (Alias_Subp) then
10141 declare
10142 E : Entity_Id;
10143
10144 begin
10145 E := Subp;
10146 while Present (Alias (E)) loop
10147
10148 -- Avoid reporting redundant errors on entities
10149 -- inherited from interfaces
10150
10151 if Sloc (E) /= Sloc (T) then
10152 Error_Msg_Sloc := Sloc (E);
10153 Error_Msg_NE
10154 ("\& has been inherited #", T, Subp);
10155 end if;
10156
10157 E := Alias (E);
10158 end loop;
10159
10160 Error_Msg_Sloc := Sloc (E);
10161
10162 -- AI05-0068: report if there is an overriding
10163 -- non-abstract subprogram that is invisible.
10164
10165 if Is_Hidden (E)
10166 and then not Is_Abstract_Subprogram (E)
10167 then
10168 Error_Msg_NE
10169 ("\& subprogram# is not visible",
10170 T, Subp);
10171
10172 -- Clarify the case where a non-null extension must
10173 -- override inherited procedure subject to pragma
10174 -- Extensions_Visible with value False and at least
10175 -- one controlling OUT param.
10176
10177 elsif Is_EVF_Procedure (E) then
10178 Error_Msg_NE
10179 ("\& # is subject to Extensions_Visible False",
10180 T, Subp);
10181
10182 else
10183 Error_Msg_NE
10184 ("\& has been inherited from subprogram #",
10185 T, Subp);
10186 end if;
10187 end;
10188 end if;
10189 end if;
10190
10191 -- Ada 2005 (AI-345): Protected or task type implementing
10192 -- abstract interfaces.
10193
10194 elsif Is_Concurrent_Record_Type (T)
10195 and then Present (Interfaces (T))
10196 then
10197 -- There is no need to check here RM 9.4(11.9/3) since we
10198 -- are processing the corresponding record type and the
10199 -- mode of the overriding subprograms was verified by
10200 -- Check_Conformance when the corresponding concurrent
10201 -- type declaration was analyzed.
10202
10203 Error_Msg_NE
10204 ("interface subprogram & must be overridden", T, Subp);
10205
10206 -- Examine primitive operations of synchronized type to find
10207 -- homonyms that have the wrong profile.
10208
10209 declare
10210 Prim : Entity_Id;
10211
10212 begin
10213 Prim := First_Entity (Corresponding_Concurrent_Type (T));
10214 while Present (Prim) loop
10215 if Chars (Prim) = Chars (Subp) then
10216 Error_Msg_NE
10217 ("profile is not type conformant with prefixed "
10218 & "view profile of inherited operation&",
10219 Prim, Subp);
10220 end if;
10221
10222 Next_Entity (Prim);
10223 end loop;
10224 end;
10225 end if;
10226
10227 else
10228 Error_Msg_Node_2 := T;
10229 Error_Msg_N
10230 ("abstract subprogram& not allowed for type&", Subp);
10231
10232 -- Also post unconditional warning on the type (unconditional
10233 -- so that if there are more than one of these cases, we get
10234 -- them all, and not just the first one).
10235
10236 Error_Msg_Node_2 := Subp;
10237 Error_Msg_N ("nonabstract type& has abstract subprogram&!", T);
10238 end if;
10239
10240 -- A subprogram subject to pragma Extensions_Visible with value
10241 -- "True" cannot override a subprogram subject to the same pragma
10242 -- with value "False" (SPARK RM 6.1.7(5)).
10243
10244 elsif Extensions_Visible_Status (Subp) = Extensions_Visible_True
10245 and then Present (Overridden_Operation (Subp))
10246 and then Extensions_Visible_Status (Overridden_Operation (Subp)) =
10247 Extensions_Visible_False
10248 then
10249 Error_Msg_Sloc := Sloc (Overridden_Operation (Subp));
10250 Error_Msg_N
10251 ("subprogram & with Extensions_Visible True cannot override "
10252 & "subprogram # with Extensions_Visible False", Subp);
10253 end if;
10254
10255 -- Ada 2012 (AI05-0030): Perform checks related to pragma Implemented
10256
10257 -- Subp is an expander-generated procedure which maps an interface
10258 -- alias to a protected wrapper. The interface alias is flagged by
10259 -- pragma Implemented. Ensure that Subp is a procedure when the
10260 -- implementation kind is By_Protected_Procedure or an entry when
10261 -- By_Entry.
10262
10263 if Ada_Version >= Ada_2012
10264 and then Is_Hidden (Subp)
10265 and then Present (Interface_Alias (Subp))
10266 and then Has_Rep_Pragma (Interface_Alias (Subp), Name_Implemented)
10267 then
10268 Check_Pragma_Implemented (Subp);
10269 end if;
10270
10271 -- Subp is an interface primitive which overrides another interface
10272 -- primitive marked with pragma Implemented.
10273
10274 if Ada_Version >= Ada_2012
10275 and then Present (Overridden_Operation (Subp))
10276 and then Has_Rep_Pragma
10277 (Overridden_Operation (Subp), Name_Implemented)
10278 then
10279 -- If the overriding routine is also marked by Implemented, check
10280 -- that the two implementation kinds are conforming.
10281
10282 if Has_Rep_Pragma (Subp, Name_Implemented) then
10283 Check_Pragma_Implemented
10284 (Subp => Subp,
10285 Iface_Subp => Overridden_Operation (Subp));
10286
10287 -- Otherwise the overriding routine inherits the implementation
10288 -- kind from the overridden subprogram.
10289
10290 else
10291 Inherit_Pragma_Implemented
10292 (Subp => Subp,
10293 Iface_Subp => Overridden_Operation (Subp));
10294 end if;
10295 end if;
10296
10297 -- If the operation is a wrapper for a synchronized primitive, it
10298 -- may be called indirectly through a dispatching select. We assume
10299 -- that it will be referenced elsewhere indirectly, and suppress
10300 -- warnings about an unused entity.
10301
10302 if Is_Primitive_Wrapper (Subp)
10303 and then Present (Wrapped_Entity (Subp))
10304 then
10305 Set_Referenced (Wrapped_Entity (Subp));
10306 end if;
10307
10308 Next_Elmt (Elmt);
10309 end loop;
10310 end Check_Abstract_Overriding;
10311
10312 ------------------------------------------------
10313 -- Check_Access_Discriminant_Requires_Limited --
10314 ------------------------------------------------
10315
10316 procedure Check_Access_Discriminant_Requires_Limited
10317 (D : Node_Id;
10318 Loc : Node_Id)
10319 is
10320 begin
10321 -- A discriminant_specification for an access discriminant shall appear
10322 -- only in the declaration for a task or protected type, or for a type
10323 -- with the reserved word 'limited' in its definition or in one of its
10324 -- ancestors (RM 3.7(10)).
10325
10326 -- AI-0063: The proper condition is that type must be immutably limited,
10327 -- or else be a partial view.
10328
10329 if Nkind (Discriminant_Type (D)) = N_Access_Definition then
10330 if Is_Limited_View (Current_Scope)
10331 or else
10332 (Nkind (Parent (Current_Scope)) = N_Private_Type_Declaration
10333 and then Limited_Present (Parent (Current_Scope)))
10334 then
10335 null;
10336
10337 else
10338 Error_Msg_N
10339 ("access discriminants allowed only for limited types", Loc);
10340 end if;
10341 end if;
10342 end Check_Access_Discriminant_Requires_Limited;
10343
10344 -----------------------------------
10345 -- Check_Aliased_Component_Types --
10346 -----------------------------------
10347
10348 procedure Check_Aliased_Component_Types (T : Entity_Id) is
10349 C : Entity_Id;
10350
10351 begin
10352 -- ??? Also need to check components of record extensions, but not
10353 -- components of protected types (which are always limited).
10354
10355 -- Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
10356 -- types to be unconstrained. This is safe because it is illegal to
10357 -- create access subtypes to such types with explicit discriminant
10358 -- constraints.
10359
10360 if not Is_Limited_Type (T) then
10361 if Ekind (T) = E_Record_Type then
10362 C := First_Component (T);
10363 while Present (C) loop
10364 if Is_Aliased (C)
10365 and then Has_Discriminants (Etype (C))
10366 and then not Is_Constrained (Etype (C))
10367 and then not In_Instance_Body
10368 and then Ada_Version < Ada_2005
10369 then
10370 Error_Msg_N
10371 ("aliased component must be constrained (RM 3.6(11))",
10372 C);
10373 end if;
10374
10375 Next_Component (C);
10376 end loop;
10377
10378 elsif Ekind (T) = E_Array_Type then
10379 if Has_Aliased_Components (T)
10380 and then Has_Discriminants (Component_Type (T))
10381 and then not Is_Constrained (Component_Type (T))
10382 and then not In_Instance_Body
10383 and then Ada_Version < Ada_2005
10384 then
10385 Error_Msg_N
10386 ("aliased component type must be constrained (RM 3.6(11))",
10387 T);
10388 end if;
10389 end if;
10390 end if;
10391 end Check_Aliased_Component_Types;
10392
10393 ---------------------------------------
10394 -- Check_Anonymous_Access_Components --
10395 ---------------------------------------
10396
10397 procedure Check_Anonymous_Access_Components
10398 (Typ_Decl : Node_Id;
10399 Typ : Entity_Id;
10400 Prev : Entity_Id;
10401 Comp_List : Node_Id)
10402 is
10403 Loc : constant Source_Ptr := Sloc (Typ_Decl);
10404 Anon_Access : Entity_Id;
10405 Acc_Def : Node_Id;
10406 Comp : Node_Id;
10407 Comp_Def : Node_Id;
10408 Decl : Node_Id;
10409 Type_Def : Node_Id;
10410
10411 procedure Build_Incomplete_Type_Declaration;
10412 -- If the record type contains components that include an access to the
10413 -- current record, then create an incomplete type declaration for the
10414 -- record, to be used as the designated type of the anonymous access.
10415 -- This is done only once, and only if there is no previous partial
10416 -- view of the type.
10417
10418 function Designates_T (Subt : Node_Id) return Boolean;
10419 -- Check whether a node designates the enclosing record type, or 'Class
10420 -- of that type
10421
10422 function Mentions_T (Acc_Def : Node_Id) return Boolean;
10423 -- Check whether an access definition includes a reference to
10424 -- the enclosing record type. The reference can be a subtype mark
10425 -- in the access definition itself, a 'Class attribute reference, or
10426 -- recursively a reference appearing in a parameter specification
10427 -- or result definition of an access_to_subprogram definition.
10428
10429 --------------------------------------
10430 -- Build_Incomplete_Type_Declaration --
10431 --------------------------------------
10432
10433 procedure Build_Incomplete_Type_Declaration is
10434 Decl : Node_Id;
10435 Inc_T : Entity_Id;
10436 H : Entity_Id;
10437
10438 -- Is_Tagged indicates whether the type is tagged. It is tagged if
10439 -- it's "is new ... with record" or else "is tagged record ...".
10440
10441 Is_Tagged : constant Boolean :=
10442 (Nkind (Type_Definition (Typ_Decl)) = N_Derived_Type_Definition
10443 and then
10444 Present (Record_Extension_Part (Type_Definition (Typ_Decl))))
10445 or else
10446 (Nkind (Type_Definition (Typ_Decl)) = N_Record_Definition
10447 and then Tagged_Present (Type_Definition (Typ_Decl)));
10448
10449 begin
10450 -- If there is a previous partial view, no need to create a new one
10451 -- If the partial view, given by Prev, is incomplete, If Prev is
10452 -- a private declaration, full declaration is flagged accordingly.
10453
10454 if Prev /= Typ then
10455 if Is_Tagged then
10456 Make_Class_Wide_Type (Prev);
10457 Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
10458 Set_Etype (Class_Wide_Type (Typ), Typ);
10459 end if;
10460
10461 return;
10462
10463 elsif Has_Private_Declaration (Typ) then
10464
10465 -- If we refer to T'Class inside T, and T is the completion of a
10466 -- private type, then make sure the class-wide type exists.
10467
10468 if Is_Tagged then
10469 Make_Class_Wide_Type (Typ);
10470 end if;
10471
10472 return;
10473
10474 -- If there was a previous anonymous access type, the incomplete
10475 -- type declaration will have been created already.
10476
10477 elsif Present (Current_Entity (Typ))
10478 and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
10479 and then Full_View (Current_Entity (Typ)) = Typ
10480 then
10481 if Is_Tagged
10482 and then Comes_From_Source (Current_Entity (Typ))
10483 and then not Is_Tagged_Type (Current_Entity (Typ))
10484 then
10485 Make_Class_Wide_Type (Typ);
10486 Error_Msg_N
10487 ("incomplete view of tagged type should be declared tagged??",
10488 Parent (Current_Entity (Typ)));
10489 end if;
10490 return;
10491
10492 else
10493 Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
10494 Decl := Make_Incomplete_Type_Declaration (Loc, Inc_T);
10495
10496 -- Type has already been inserted into the current scope. Remove
10497 -- it, and add incomplete declaration for type, so that subsequent
10498 -- anonymous access types can use it. The entity is unchained from
10499 -- the homonym list and from immediate visibility. After analysis,
10500 -- the entity in the incomplete declaration becomes immediately
10501 -- visible in the record declaration that follows.
10502
10503 H := Current_Entity (Typ);
10504
10505 if H = Typ then
10506 Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
10507 else
10508 while Present (H)
10509 and then Homonym (H) /= Typ
10510 loop
10511 H := Homonym (Typ);
10512 end loop;
10513
10514 Set_Homonym (H, Homonym (Typ));
10515 end if;
10516
10517 Insert_Before (Typ_Decl, Decl);
10518 Analyze (Decl);
10519 Set_Full_View (Inc_T, Typ);
10520
10521 if Is_Tagged then
10522
10523 -- Create a common class-wide type for both views, and set the
10524 -- Etype of the class-wide type to the full view.
10525
10526 Make_Class_Wide_Type (Inc_T);
10527 Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
10528 Set_Etype (Class_Wide_Type (Typ), Typ);
10529 end if;
10530 end if;
10531 end Build_Incomplete_Type_Declaration;
10532
10533 ------------------
10534 -- Designates_T --
10535 ------------------
10536
10537 function Designates_T (Subt : Node_Id) return Boolean is
10538 Type_Id : constant Name_Id := Chars (Typ);
10539
10540 function Names_T (Nam : Node_Id) return Boolean;
10541 -- The record type has not been introduced in the current scope
10542 -- yet, so we must examine the name of the type itself, either
10543 -- an identifier T, or an expanded name of the form P.T, where
10544 -- P denotes the current scope.
10545
10546 -------------
10547 -- Names_T --
10548 -------------
10549
10550 function Names_T (Nam : Node_Id) return Boolean is
10551 begin
10552 if Nkind (Nam) = N_Identifier then
10553 return Chars (Nam) = Type_Id;
10554
10555 elsif Nkind (Nam) = N_Selected_Component then
10556 if Chars (Selector_Name (Nam)) = Type_Id then
10557 if Nkind (Prefix (Nam)) = N_Identifier then
10558 return Chars (Prefix (Nam)) = Chars (Current_Scope);
10559
10560 elsif Nkind (Prefix (Nam)) = N_Selected_Component then
10561 return Chars (Selector_Name (Prefix (Nam))) =
10562 Chars (Current_Scope);
10563 else
10564 return False;
10565 end if;
10566
10567 else
10568 return False;
10569 end if;
10570
10571 else
10572 return False;
10573 end if;
10574 end Names_T;
10575
10576 -- Start of processing for Designates_T
10577
10578 begin
10579 if Nkind (Subt) = N_Identifier then
10580 return Chars (Subt) = Type_Id;
10581
10582 -- Reference can be through an expanded name which has not been
10583 -- analyzed yet, and which designates enclosing scopes.
10584
10585 elsif Nkind (Subt) = N_Selected_Component then
10586 if Names_T (Subt) then
10587 return True;
10588
10589 -- Otherwise it must denote an entity that is already visible.
10590 -- The access definition may name a subtype of the enclosing
10591 -- type, if there is a previous incomplete declaration for it.
10592
10593 else
10594 Find_Selected_Component (Subt);
10595 return
10596 Is_Entity_Name (Subt)
10597 and then Scope (Entity (Subt)) = Current_Scope
10598 and then
10599 (Chars (Base_Type (Entity (Subt))) = Type_Id
10600 or else
10601 (Is_Class_Wide_Type (Entity (Subt))
10602 and then
10603 Chars (Etype (Base_Type (Entity (Subt)))) =
10604 Type_Id));
10605 end if;
10606
10607 -- A reference to the current type may appear as the prefix of
10608 -- a 'Class attribute.
10609
10610 elsif Nkind (Subt) = N_Attribute_Reference
10611 and then Attribute_Name (Subt) = Name_Class
10612 then
10613 return Names_T (Prefix (Subt));
10614
10615 else
10616 return False;
10617 end if;
10618 end Designates_T;
10619
10620 ----------------
10621 -- Mentions_T --
10622 ----------------
10623
10624 function Mentions_T (Acc_Def : Node_Id) return Boolean is
10625 Param_Spec : Node_Id;
10626
10627 Acc_Subprg : constant Node_Id :=
10628 Access_To_Subprogram_Definition (Acc_Def);
10629
10630 begin
10631 if No (Acc_Subprg) then
10632 return Designates_T (Subtype_Mark (Acc_Def));
10633 end if;
10634
10635 -- Component is an access_to_subprogram: examine its formals,
10636 -- and result definition in the case of an access_to_function.
10637
10638 Param_Spec := First (Parameter_Specifications (Acc_Subprg));
10639 while Present (Param_Spec) loop
10640 if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
10641 and then Mentions_T (Parameter_Type (Param_Spec))
10642 then
10643 return True;
10644
10645 elsif Designates_T (Parameter_Type (Param_Spec)) then
10646 return True;
10647 end if;
10648
10649 Next (Param_Spec);
10650 end loop;
10651
10652 if Nkind (Acc_Subprg) = N_Access_Function_Definition then
10653 if Nkind (Result_Definition (Acc_Subprg)) =
10654 N_Access_Definition
10655 then
10656 return Mentions_T (Result_Definition (Acc_Subprg));
10657 else
10658 return Designates_T (Result_Definition (Acc_Subprg));
10659 end if;
10660 end if;
10661
10662 return False;
10663 end Mentions_T;
10664
10665 -- Start of processing for Check_Anonymous_Access_Components
10666
10667 begin
10668 if No (Comp_List) then
10669 return;
10670 end if;
10671
10672 Comp := First (Component_Items (Comp_List));
10673 while Present (Comp) loop
10674 if Nkind (Comp) = N_Component_Declaration
10675 and then Present
10676 (Access_Definition (Component_Definition (Comp)))
10677 and then
10678 Mentions_T (Access_Definition (Component_Definition (Comp)))
10679 then
10680 Comp_Def := Component_Definition (Comp);
10681 Acc_Def :=
10682 Access_To_Subprogram_Definition (Access_Definition (Comp_Def));
10683
10684 Build_Incomplete_Type_Declaration;
10685 Anon_Access := Make_Temporary (Loc, 'S');
10686
10687 -- Create a declaration for the anonymous access type: either
10688 -- an access_to_object or an access_to_subprogram.
10689
10690 if Present (Acc_Def) then
10691 if Nkind (Acc_Def) = N_Access_Function_Definition then
10692 Type_Def :=
10693 Make_Access_Function_Definition (Loc,
10694 Parameter_Specifications =>
10695 Parameter_Specifications (Acc_Def),
10696 Result_Definition => Result_Definition (Acc_Def));
10697 else
10698 Type_Def :=
10699 Make_Access_Procedure_Definition (Loc,
10700 Parameter_Specifications =>
10701 Parameter_Specifications (Acc_Def));
10702 end if;
10703
10704 else
10705 Type_Def :=
10706 Make_Access_To_Object_Definition (Loc,
10707 Subtype_Indication =>
10708 Relocate_Node
10709 (Subtype_Mark (Access_Definition (Comp_Def))));
10710
10711 Set_Constant_Present
10712 (Type_Def, Constant_Present (Access_Definition (Comp_Def)));
10713 Set_All_Present
10714 (Type_Def, All_Present (Access_Definition (Comp_Def)));
10715 end if;
10716
10717 Set_Null_Exclusion_Present
10718 (Type_Def,
10719 Null_Exclusion_Present (Access_Definition (Comp_Def)));
10720
10721 Decl :=
10722 Make_Full_Type_Declaration (Loc,
10723 Defining_Identifier => Anon_Access,
10724 Type_Definition => Type_Def);
10725
10726 Insert_Before (Typ_Decl, Decl);
10727 Analyze (Decl);
10728
10729 -- If an access to subprogram, create the extra formals
10730
10731 if Present (Acc_Def) then
10732 Create_Extra_Formals (Designated_Type (Anon_Access));
10733
10734 -- If an access to object, preserve entity of designated type,
10735 -- for ASIS use, before rewriting the component definition.
10736
10737 else
10738 declare
10739 Desig : Entity_Id;
10740
10741 begin
10742 Desig := Entity (Subtype_Indication (Type_Def));
10743
10744 -- If the access definition is to the current record,
10745 -- the visible entity at this point is an incomplete
10746 -- type. Retrieve the full view to simplify ASIS queries
10747
10748 if Ekind (Desig) = E_Incomplete_Type then
10749 Desig := Full_View (Desig);
10750 end if;
10751
10752 Set_Entity
10753 (Subtype_Mark (Access_Definition (Comp_Def)), Desig);
10754 end;
10755 end if;
10756
10757 Rewrite (Comp_Def,
10758 Make_Component_Definition (Loc,
10759 Subtype_Indication =>
10760 New_Occurrence_Of (Anon_Access, Loc)));
10761
10762 if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
10763 Set_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
10764 else
10765 Set_Ekind (Anon_Access, E_Anonymous_Access_Type);
10766 end if;
10767
10768 Set_Is_Local_Anonymous_Access (Anon_Access);
10769 end if;
10770
10771 Next (Comp);
10772 end loop;
10773
10774 if Present (Variant_Part (Comp_List)) then
10775 declare
10776 V : Node_Id;
10777 begin
10778 V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
10779 while Present (V) loop
10780 Check_Anonymous_Access_Components
10781 (Typ_Decl, Typ, Prev, Component_List (V));
10782 Next_Non_Pragma (V);
10783 end loop;
10784 end;
10785 end if;
10786 end Check_Anonymous_Access_Components;
10787
10788 ----------------------
10789 -- Check_Completion --
10790 ----------------------
10791
10792 procedure Check_Completion (Body_Id : Node_Id := Empty) is
10793 E : Entity_Id;
10794
10795 procedure Post_Error;
10796 -- Post error message for lack of completion for entity E
10797
10798 ----------------
10799 -- Post_Error --
10800 ----------------
10801
10802 procedure Post_Error is
10803
10804 procedure Missing_Body;
10805 -- Output missing body message
10806
10807 ------------------
10808 -- Missing_Body --
10809 ------------------
10810
10811 procedure Missing_Body is
10812 begin
10813 -- Spec is in same unit, so we can post on spec
10814
10815 if In_Same_Source_Unit (Body_Id, E) then
10816 Error_Msg_N ("missing body for &", E);
10817
10818 -- Spec is in a separate unit, so we have to post on the body
10819
10820 else
10821 Error_Msg_NE ("missing body for & declared#!", Body_Id, E);
10822 end if;
10823 end Missing_Body;
10824
10825 -- Start of processing for Post_Error
10826
10827 begin
10828 if not Comes_From_Source (E) then
10829
10830 if Ekind_In (E, E_Task_Type, E_Protected_Type) then
10831
10832 -- It may be an anonymous protected type created for a
10833 -- single variable. Post error on variable, if present.
10834
10835 declare
10836 Var : Entity_Id;
10837
10838 begin
10839 Var := First_Entity (Current_Scope);
10840 while Present (Var) loop
10841 exit when Etype (Var) = E
10842 and then Comes_From_Source (Var);
10843
10844 Next_Entity (Var);
10845 end loop;
10846
10847 if Present (Var) then
10848 E := Var;
10849 end if;
10850 end;
10851 end if;
10852 end if;
10853
10854 -- If a generated entity has no completion, then either previous
10855 -- semantic errors have disabled the expansion phase, or else we had
10856 -- missing subunits, or else we are compiling without expansion,
10857 -- or else something is very wrong.
10858
10859 if not Comes_From_Source (E) then
10860 pragma Assert
10861 (Serious_Errors_Detected > 0
10862 or else Configurable_Run_Time_Violations > 0
10863 or else Subunits_Missing
10864 or else not Expander_Active);
10865 return;
10866
10867 -- Here for source entity
10868
10869 else
10870 -- Here if no body to post the error message, so we post the error
10871 -- on the declaration that has no completion. This is not really
10872 -- the right place to post it, think about this later ???
10873
10874 if No (Body_Id) then
10875 if Is_Type (E) then
10876 Error_Msg_NE
10877 ("missing full declaration for }", Parent (E), E);
10878 else
10879 Error_Msg_NE ("missing body for &", Parent (E), E);
10880 end if;
10881
10882 -- Package body has no completion for a declaration that appears
10883 -- in the corresponding spec. Post error on the body, with a
10884 -- reference to the non-completed declaration.
10885
10886 else
10887 Error_Msg_Sloc := Sloc (E);
10888
10889 if Is_Type (E) then
10890 Error_Msg_NE ("missing full declaration for }!", Body_Id, E);
10891
10892 elsif Is_Overloadable (E)
10893 and then Current_Entity_In_Scope (E) /= E
10894 then
10895 -- It may be that the completion is mistyped and appears as
10896 -- a distinct overloading of the entity.
10897
10898 declare
10899 Candidate : constant Entity_Id :=
10900 Current_Entity_In_Scope (E);
10901 Decl : constant Node_Id :=
10902 Unit_Declaration_Node (Candidate);
10903
10904 begin
10905 if Is_Overloadable (Candidate)
10906 and then Ekind (Candidate) = Ekind (E)
10907 and then Nkind (Decl) = N_Subprogram_Body
10908 and then Acts_As_Spec (Decl)
10909 then
10910 Check_Type_Conformant (Candidate, E);
10911
10912 else
10913 Missing_Body;
10914 end if;
10915 end;
10916
10917 else
10918 Missing_Body;
10919 end if;
10920 end if;
10921 end if;
10922 end Post_Error;
10923
10924 -- Local variables
10925
10926 Pack_Id : constant Entity_Id := Current_Scope;
10927
10928 -- Start of processing for Check_Completion
10929
10930 begin
10931 E := First_Entity (Pack_Id);
10932 while Present (E) loop
10933 if Is_Intrinsic_Subprogram (E) then
10934 null;
10935
10936 -- A Ghost entity declared in a non-Ghost package does not force the
10937 -- need for a body (SPARK RM 6.9(11)).
10938
10939 elsif not Is_Ghost_Entity (Pack_Id) and then Is_Ghost_Entity (E) then
10940 null;
10941
10942 -- The following situation requires special handling: a child unit
10943 -- that appears in the context clause of the body of its parent:
10944
10945 -- procedure Parent.Child (...);
10946
10947 -- with Parent.Child;
10948 -- package body Parent is
10949
10950 -- Here Parent.Child appears as a local entity, but should not be
10951 -- flagged as requiring completion, because it is a compilation
10952 -- unit.
10953
10954 -- Ignore missing completion for a subprogram that does not come from
10955 -- source (including the _Call primitive operation of RAS types,
10956 -- which has to have the flag Comes_From_Source for other purposes):
10957 -- we assume that the expander will provide the missing completion.
10958 -- In case of previous errors, other expansion actions that provide
10959 -- bodies for null procedures with not be invoked, so inhibit message
10960 -- in those cases.
10961
10962 -- Note that E_Operator is not in the list that follows, because
10963 -- this kind is reserved for predefined operators, that are
10964 -- intrinsic and do not need completion.
10965
10966 elsif Ekind_In (E, E_Function,
10967 E_Procedure,
10968 E_Generic_Function,
10969 E_Generic_Procedure)
10970 then
10971 if Has_Completion (E) then
10972 null;
10973
10974 elsif Is_Subprogram (E) and then Is_Abstract_Subprogram (E) then
10975 null;
10976
10977 elsif Is_Subprogram (E)
10978 and then (not Comes_From_Source (E)
10979 or else Chars (E) = Name_uCall)
10980 then
10981 null;
10982
10983 elsif
10984 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
10985 then
10986 null;
10987
10988 elsif Nkind (Parent (E)) = N_Procedure_Specification
10989 and then Null_Present (Parent (E))
10990 and then Serious_Errors_Detected > 0
10991 then
10992 null;
10993
10994 else
10995 Post_Error;
10996 end if;
10997
10998 elsif Is_Entry (E) then
10999 if not Has_Completion (E) and then
11000 (Ekind (Scope (E)) = E_Protected_Object
11001 or else Ekind (Scope (E)) = E_Protected_Type)
11002 then
11003 Post_Error;
11004 end if;
11005
11006 elsif Is_Package_Or_Generic_Package (E) then
11007 if Unit_Requires_Body (E) then
11008 if not Has_Completion (E)
11009 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
11010 N_Compilation_Unit
11011 then
11012 Post_Error;
11013 end if;
11014
11015 elsif not Is_Child_Unit (E) then
11016 May_Need_Implicit_Body (E);
11017 end if;
11018
11019 -- A formal incomplete type (Ada 2012) does not require a completion;
11020 -- other incomplete type declarations do.
11021
11022 elsif Ekind (E) = E_Incomplete_Type
11023 and then No (Underlying_Type (E))
11024 and then not Is_Generic_Type (E)
11025 then
11026 Post_Error;
11027
11028 elsif Ekind_In (E, E_Task_Type, E_Protected_Type)
11029 and then not Has_Completion (E)
11030 then
11031 Post_Error;
11032
11033 -- A single task declared in the current scope is a constant, verify
11034 -- that the body of its anonymous type is in the same scope. If the
11035 -- task is defined elsewhere, this may be a renaming declaration for
11036 -- which no completion is needed.
11037
11038 elsif Ekind (E) = E_Constant
11039 and then Ekind (Etype (E)) = E_Task_Type
11040 and then not Has_Completion (Etype (E))
11041 and then Scope (Etype (E)) = Current_Scope
11042 then
11043 Post_Error;
11044
11045 elsif Ekind (E) = E_Protected_Object
11046 and then not Has_Completion (Etype (E))
11047 then
11048 Post_Error;
11049
11050 elsif Ekind (E) = E_Record_Type then
11051 if Is_Tagged_Type (E) then
11052 Check_Abstract_Overriding (E);
11053 Check_Conventions (E);
11054 end if;
11055
11056 Check_Aliased_Component_Types (E);
11057
11058 elsif Ekind (E) = E_Array_Type then
11059 Check_Aliased_Component_Types (E);
11060
11061 end if;
11062
11063 Next_Entity (E);
11064 end loop;
11065 end Check_Completion;
11066
11067 ------------------------------------
11068 -- Check_CPP_Type_Has_No_Defaults --
11069 ------------------------------------
11070
11071 procedure Check_CPP_Type_Has_No_Defaults (T : Entity_Id) is
11072 Tdef : constant Node_Id := Type_Definition (Declaration_Node (T));
11073 Clist : Node_Id;
11074 Comp : Node_Id;
11075
11076 begin
11077 -- Obtain the component list
11078
11079 if Nkind (Tdef) = N_Record_Definition then
11080 Clist := Component_List (Tdef);
11081 else pragma Assert (Nkind (Tdef) = N_Derived_Type_Definition);
11082 Clist := Component_List (Record_Extension_Part (Tdef));
11083 end if;
11084
11085 -- Check all components to ensure no default expressions
11086
11087 if Present (Clist) then
11088 Comp := First (Component_Items (Clist));
11089 while Present (Comp) loop
11090 if Present (Expression (Comp)) then
11091 Error_Msg_N
11092 ("component of imported 'C'P'P type cannot have "
11093 & "default expression", Expression (Comp));
11094 end if;
11095
11096 Next (Comp);
11097 end loop;
11098 end if;
11099 end Check_CPP_Type_Has_No_Defaults;
11100
11101 ----------------------------
11102 -- Check_Delta_Expression --
11103 ----------------------------
11104
11105 procedure Check_Delta_Expression (E : Node_Id) is
11106 begin
11107 if not (Is_Real_Type (Etype (E))) then
11108 Wrong_Type (E, Any_Real);
11109
11110 elsif not Is_OK_Static_Expression (E) then
11111 Flag_Non_Static_Expr
11112 ("non-static expression used for delta value!", E);
11113
11114 elsif not UR_Is_Positive (Expr_Value_R (E)) then
11115 Error_Msg_N ("delta expression must be positive", E);
11116
11117 else
11118 return;
11119 end if;
11120
11121 -- If any of above errors occurred, then replace the incorrect
11122 -- expression by the real 0.1, which should prevent further errors.
11123
11124 Rewrite (E,
11125 Make_Real_Literal (Sloc (E), Ureal_Tenth));
11126 Analyze_And_Resolve (E, Standard_Float);
11127 end Check_Delta_Expression;
11128
11129 -----------------------------
11130 -- Check_Digits_Expression --
11131 -----------------------------
11132
11133 procedure Check_Digits_Expression (E : Node_Id) is
11134 begin
11135 if not (Is_Integer_Type (Etype (E))) then
11136 Wrong_Type (E, Any_Integer);
11137
11138 elsif not Is_OK_Static_Expression (E) then
11139 Flag_Non_Static_Expr
11140 ("non-static expression used for digits value!", E);
11141
11142 elsif Expr_Value (E) <= 0 then
11143 Error_Msg_N ("digits value must be greater than zero", E);
11144
11145 else
11146 return;
11147 end if;
11148
11149 -- If any of above errors occurred, then replace the incorrect
11150 -- expression by the integer 1, which should prevent further errors.
11151
11152 Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
11153 Analyze_And_Resolve (E, Standard_Integer);
11154
11155 end Check_Digits_Expression;
11156
11157 --------------------------
11158 -- Check_Initialization --
11159 --------------------------
11160
11161 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
11162 begin
11163 -- Special processing for limited types
11164
11165 if Is_Limited_Type (T)
11166 and then not In_Instance
11167 and then not In_Inlined_Body
11168 then
11169 if not OK_For_Limited_Init (T, Exp) then
11170
11171 -- In GNAT mode, this is just a warning, to allow it to be evilly
11172 -- turned off. Otherwise it is a real error.
11173
11174 if GNAT_Mode then
11175 Error_Msg_N
11176 ("??cannot initialize entities of limited type!", Exp);
11177
11178 elsif Ada_Version < Ada_2005 then
11179
11180 -- The side effect removal machinery may generate illegal Ada
11181 -- code to avoid the usage of access types and 'reference in
11182 -- SPARK mode. Since this is legal code with respect to theorem
11183 -- proving, do not emit the error.
11184
11185 if GNATprove_Mode
11186 and then Nkind (Exp) = N_Function_Call
11187 and then Nkind (Parent (Exp)) = N_Object_Declaration
11188 and then not Comes_From_Source
11189 (Defining_Identifier (Parent (Exp)))
11190 then
11191 null;
11192
11193 else
11194 Error_Msg_N
11195 ("cannot initialize entities of limited type", Exp);
11196 Explain_Limited_Type (T, Exp);
11197 end if;
11198
11199 else
11200 -- Specialize error message according to kind of illegal
11201 -- initial expression.
11202
11203 if Nkind (Exp) = N_Type_Conversion
11204 and then Nkind (Expression (Exp)) = N_Function_Call
11205 then
11206 Error_Msg_N
11207 ("illegal context for call"
11208 & " to function with limited result", Exp);
11209
11210 else
11211 Error_Msg_N
11212 ("initialization of limited object requires aggregate "
11213 & "or function call", Exp);
11214 end if;
11215 end if;
11216 end if;
11217 end if;
11218
11219 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag gets
11220 -- set unless we can be sure that no range check is required.
11221
11222 if (GNATprove_Mode or not Expander_Active)
11223 and then Is_Scalar_Type (T)
11224 and then not Is_In_Range (Exp, T, Assume_Valid => True)
11225 then
11226 Set_Do_Range_Check (Exp);
11227 end if;
11228 end Check_Initialization;
11229
11230 ----------------------
11231 -- Check_Interfaces --
11232 ----------------------
11233
11234 procedure Check_Interfaces (N : Node_Id; Def : Node_Id) is
11235 Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
11236
11237 Iface : Node_Id;
11238 Iface_Def : Node_Id;
11239 Iface_Typ : Entity_Id;
11240 Parent_Node : Node_Id;
11241
11242 Is_Task : Boolean := False;
11243 -- Set True if parent type or any progenitor is a task interface
11244
11245 Is_Protected : Boolean := False;
11246 -- Set True if parent type or any progenitor is a protected interface
11247
11248 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
11249 -- Check that a progenitor is compatible with declaration. If an error
11250 -- message is output, it is posted on Error_Node.
11251
11252 ------------------
11253 -- Check_Ifaces --
11254 ------------------
11255
11256 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
11257 Iface_Id : constant Entity_Id :=
11258 Defining_Identifier (Parent (Iface_Def));
11259 Type_Def : Node_Id;
11260
11261 begin
11262 if Nkind (N) = N_Private_Extension_Declaration then
11263 Type_Def := N;
11264 else
11265 Type_Def := Type_Definition (N);
11266 end if;
11267
11268 if Is_Task_Interface (Iface_Id) then
11269 Is_Task := True;
11270
11271 elsif Is_Protected_Interface (Iface_Id) then
11272 Is_Protected := True;
11273 end if;
11274
11275 if Is_Synchronized_Interface (Iface_Id) then
11276
11277 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
11278 -- extension derived from a synchronized interface must explicitly
11279 -- be declared synchronized, because the full view will be a
11280 -- synchronized type.
11281
11282 if Nkind (N) = N_Private_Extension_Declaration then
11283 if not Synchronized_Present (N) then
11284 Error_Msg_NE
11285 ("private extension of& must be explicitly synchronized",
11286 N, Iface_Id);
11287 end if;
11288
11289 -- However, by 3.9.4(16/2), a full type that is a record extension
11290 -- is never allowed to derive from a synchronized interface (note
11291 -- that interfaces must be excluded from this check, because those
11292 -- are represented by derived type definitions in some cases).
11293
11294 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition
11295 and then not Interface_Present (Type_Definition (N))
11296 then
11297 Error_Msg_N ("record extension cannot derive from synchronized "
11298 & "interface", Error_Node);
11299 end if;
11300 end if;
11301
11302 -- Check that the characteristics of the progenitor are compatible
11303 -- with the explicit qualifier in the declaration.
11304 -- The check only applies to qualifiers that come from source.
11305 -- Limited_Present also appears in the declaration of corresponding
11306 -- records, and the check does not apply to them.
11307
11308 if Limited_Present (Type_Def)
11309 and then not
11310 Is_Concurrent_Record_Type (Defining_Identifier (N))
11311 then
11312 if Is_Limited_Interface (Parent_Type)
11313 and then not Is_Limited_Interface (Iface_Id)
11314 then
11315 Error_Msg_NE
11316 ("progenitor & must be limited interface",
11317 Error_Node, Iface_Id);
11318
11319 elsif
11320 (Task_Present (Iface_Def)
11321 or else Protected_Present (Iface_Def)
11322 or else Synchronized_Present (Iface_Def))
11323 and then Nkind (N) /= N_Private_Extension_Declaration
11324 and then not Error_Posted (N)
11325 then
11326 Error_Msg_NE
11327 ("progenitor & must be limited interface",
11328 Error_Node, Iface_Id);
11329 end if;
11330
11331 -- Protected interfaces can only inherit from limited, synchronized
11332 -- or protected interfaces.
11333
11334 elsif Nkind (N) = N_Full_Type_Declaration
11335 and then Protected_Present (Type_Def)
11336 then
11337 if Limited_Present (Iface_Def)
11338 or else Synchronized_Present (Iface_Def)
11339 or else Protected_Present (Iface_Def)
11340 then
11341 null;
11342
11343 elsif Task_Present (Iface_Def) then
11344 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
11345 & "from task interface", Error_Node);
11346
11347 else
11348 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
11349 & "from non-limited interface", Error_Node);
11350 end if;
11351
11352 -- Ada 2005 (AI-345): Synchronized interfaces can only inherit from
11353 -- limited and synchronized.
11354
11355 elsif Synchronized_Present (Type_Def) then
11356 if Limited_Present (Iface_Def)
11357 or else Synchronized_Present (Iface_Def)
11358 then
11359 null;
11360
11361 elsif Protected_Present (Iface_Def)
11362 and then Nkind (N) /= N_Private_Extension_Declaration
11363 then
11364 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11365 & "from protected interface", Error_Node);
11366
11367 elsif Task_Present (Iface_Def)
11368 and then Nkind (N) /= N_Private_Extension_Declaration
11369 then
11370 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11371 & "from task interface", Error_Node);
11372
11373 elsif not Is_Limited_Interface (Iface_Id) then
11374 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11375 & "from non-limited interface", Error_Node);
11376 end if;
11377
11378 -- Ada 2005 (AI-345): Task interfaces can only inherit from limited,
11379 -- synchronized or task interfaces.
11380
11381 elsif Nkind (N) = N_Full_Type_Declaration
11382 and then Task_Present (Type_Def)
11383 then
11384 if Limited_Present (Iface_Def)
11385 or else Synchronized_Present (Iface_Def)
11386 or else Task_Present (Iface_Def)
11387 then
11388 null;
11389
11390 elsif Protected_Present (Iface_Def) then
11391 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
11392 & "protected interface", Error_Node);
11393
11394 else
11395 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
11396 & "non-limited interface", Error_Node);
11397 end if;
11398 end if;
11399 end Check_Ifaces;
11400
11401 -- Start of processing for Check_Interfaces
11402
11403 begin
11404 if Is_Interface (Parent_Type) then
11405 if Is_Task_Interface (Parent_Type) then
11406 Is_Task := True;
11407
11408 elsif Is_Protected_Interface (Parent_Type) then
11409 Is_Protected := True;
11410 end if;
11411 end if;
11412
11413 if Nkind (N) = N_Private_Extension_Declaration then
11414
11415 -- Check that progenitors are compatible with declaration
11416
11417 Iface := First (Interface_List (Def));
11418 while Present (Iface) loop
11419 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
11420
11421 Parent_Node := Parent (Base_Type (Iface_Typ));
11422 Iface_Def := Type_Definition (Parent_Node);
11423
11424 if not Is_Interface (Iface_Typ) then
11425 Diagnose_Interface (Iface, Iface_Typ);
11426 else
11427 Check_Ifaces (Iface_Def, Iface);
11428 end if;
11429
11430 Next (Iface);
11431 end loop;
11432
11433 if Is_Task and Is_Protected then
11434 Error_Msg_N
11435 ("type cannot derive from task and protected interface", N);
11436 end if;
11437
11438 return;
11439 end if;
11440
11441 -- Full type declaration of derived type.
11442 -- Check compatibility with parent if it is interface type
11443
11444 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
11445 and then Is_Interface (Parent_Type)
11446 then
11447 Parent_Node := Parent (Parent_Type);
11448
11449 -- More detailed checks for interface varieties
11450
11451 Check_Ifaces
11452 (Iface_Def => Type_Definition (Parent_Node),
11453 Error_Node => Subtype_Indication (Type_Definition (N)));
11454 end if;
11455
11456 Iface := First (Interface_List (Def));
11457 while Present (Iface) loop
11458 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
11459
11460 Parent_Node := Parent (Base_Type (Iface_Typ));
11461 Iface_Def := Type_Definition (Parent_Node);
11462
11463 if not Is_Interface (Iface_Typ) then
11464 Diagnose_Interface (Iface, Iface_Typ);
11465
11466 else
11467 -- "The declaration of a specific descendant of an interface
11468 -- type freezes the interface type" RM 13.14
11469
11470 Freeze_Before (N, Iface_Typ);
11471 Check_Ifaces (Iface_Def, Error_Node => Iface);
11472 end if;
11473
11474 Next (Iface);
11475 end loop;
11476
11477 if Is_Task and Is_Protected then
11478 Error_Msg_N
11479 ("type cannot derive from task and protected interface", N);
11480 end if;
11481 end Check_Interfaces;
11482
11483 ------------------------------------
11484 -- Check_Or_Process_Discriminants --
11485 ------------------------------------
11486
11487 -- If an incomplete or private type declaration was already given for the
11488 -- type, the discriminants may have already been processed if they were
11489 -- present on the incomplete declaration. In this case a full conformance
11490 -- check has been performed in Find_Type_Name, and we then recheck here
11491 -- some properties that can't be checked on the partial view alone.
11492 -- Otherwise we call Process_Discriminants.
11493
11494 procedure Check_Or_Process_Discriminants
11495 (N : Node_Id;
11496 T : Entity_Id;
11497 Prev : Entity_Id := Empty)
11498 is
11499 begin
11500 if Has_Discriminants (T) then
11501
11502 -- Discriminants are already set on T if they were already present
11503 -- on the partial view. Make them visible to component declarations.
11504
11505 declare
11506 D : Entity_Id;
11507 -- Discriminant on T (full view) referencing expr on partial view
11508
11509 Prev_D : Entity_Id;
11510 -- Entity of corresponding discriminant on partial view
11511
11512 New_D : Node_Id;
11513 -- Discriminant specification for full view, expression is
11514 -- the syntactic copy on full view (which has been checked for
11515 -- conformance with partial view), only used here to post error
11516 -- message.
11517
11518 begin
11519 D := First_Discriminant (T);
11520 New_D := First (Discriminant_Specifications (N));
11521 while Present (D) loop
11522 Prev_D := Current_Entity (D);
11523 Set_Current_Entity (D);
11524 Set_Is_Immediately_Visible (D);
11525 Set_Homonym (D, Prev_D);
11526
11527 -- Handle the case where there is an untagged partial view and
11528 -- the full view is tagged: must disallow discriminants with
11529 -- defaults, unless compiling for Ada 2012, which allows a
11530 -- limited tagged type to have defaulted discriminants (see
11531 -- AI05-0214). However, suppress error here if it was already
11532 -- reported on the default expression of the partial view.
11533
11534 if Is_Tagged_Type (T)
11535 and then Present (Expression (Parent (D)))
11536 and then (not Is_Limited_Type (Current_Scope)
11537 or else Ada_Version < Ada_2012)
11538 and then not Error_Posted (Expression (Parent (D)))
11539 then
11540 if Ada_Version >= Ada_2012 then
11541 Error_Msg_N
11542 ("discriminants of nonlimited tagged type cannot have "
11543 & "defaults",
11544 Expression (New_D));
11545 else
11546 Error_Msg_N
11547 ("discriminants of tagged type cannot have defaults",
11548 Expression (New_D));
11549 end if;
11550 end if;
11551
11552 -- Ada 2005 (AI-230): Access discriminant allowed in
11553 -- non-limited record types.
11554
11555 if Ada_Version < Ada_2005 then
11556
11557 -- This restriction gets applied to the full type here. It
11558 -- has already been applied earlier to the partial view.
11559
11560 Check_Access_Discriminant_Requires_Limited (Parent (D), N);
11561 end if;
11562
11563 Next_Discriminant (D);
11564 Next (New_D);
11565 end loop;
11566 end;
11567
11568 elsif Present (Discriminant_Specifications (N)) then
11569 Process_Discriminants (N, Prev);
11570 end if;
11571 end Check_Or_Process_Discriminants;
11572
11573 ----------------------
11574 -- Check_Real_Bound --
11575 ----------------------
11576
11577 procedure Check_Real_Bound (Bound : Node_Id) is
11578 begin
11579 if not Is_Real_Type (Etype (Bound)) then
11580 Error_Msg_N
11581 ("bound in real type definition must be of real type", Bound);
11582
11583 elsif not Is_OK_Static_Expression (Bound) then
11584 Flag_Non_Static_Expr
11585 ("non-static expression used for real type bound!", Bound);
11586
11587 else
11588 return;
11589 end if;
11590
11591 Rewrite
11592 (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
11593 Analyze (Bound);
11594 Resolve (Bound, Standard_Float);
11595 end Check_Real_Bound;
11596
11597 ------------------------------
11598 -- Complete_Private_Subtype --
11599 ------------------------------
11600
11601 procedure Complete_Private_Subtype
11602 (Priv : Entity_Id;
11603 Full : Entity_Id;
11604 Full_Base : Entity_Id;
11605 Related_Nod : Node_Id)
11606 is
11607 Save_Next_Entity : Entity_Id;
11608 Save_Homonym : Entity_Id;
11609
11610 begin
11611 -- Set semantic attributes for (implicit) private subtype completion.
11612 -- If the full type has no discriminants, then it is a copy of the
11613 -- full view of the base. Otherwise, it is a subtype of the base with
11614 -- a possible discriminant constraint. Save and restore the original
11615 -- Next_Entity field of full to ensure that the calls to Copy_Node do
11616 -- not corrupt the entity chain.
11617
11618 -- Note that the type of the full view is the same entity as the type
11619 -- of the partial view. In this fashion, the subtype has access to the
11620 -- correct view of the parent.
11621
11622 Save_Next_Entity := Next_Entity (Full);
11623 Save_Homonym := Homonym (Priv);
11624
11625 case Ekind (Full_Base) is
11626 when E_Record_Type |
11627 E_Record_Subtype |
11628 Class_Wide_Kind |
11629 Private_Kind |
11630 Task_Kind |
11631 Protected_Kind =>
11632 Copy_Node (Priv, Full);
11633
11634 Set_Has_Discriminants
11635 (Full, Has_Discriminants (Full_Base));
11636 Set_Has_Unknown_Discriminants
11637 (Full, Has_Unknown_Discriminants (Full_Base));
11638 Set_First_Entity (Full, First_Entity (Full_Base));
11639 Set_Last_Entity (Full, Last_Entity (Full_Base));
11640
11641 -- If the underlying base type is constrained, we know that the
11642 -- full view of the subtype is constrained as well (the converse
11643 -- is not necessarily true).
11644
11645 if Is_Constrained (Full_Base) then
11646 Set_Is_Constrained (Full);
11647 end if;
11648
11649 when others =>
11650 Copy_Node (Full_Base, Full);
11651
11652 Set_Chars (Full, Chars (Priv));
11653 Conditional_Delay (Full, Priv);
11654 Set_Sloc (Full, Sloc (Priv));
11655 end case;
11656
11657 Set_Next_Entity (Full, Save_Next_Entity);
11658 Set_Homonym (Full, Save_Homonym);
11659 Set_Associated_Node_For_Itype (Full, Related_Nod);
11660
11661 -- Set common attributes for all subtypes: kind, convention, etc.
11662
11663 Set_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
11664 Set_Convention (Full, Convention (Full_Base));
11665
11666 -- The Etype of the full view is inconsistent. Gigi needs to see the
11667 -- structural full view, which is what the current scheme gives: the
11668 -- Etype of the full view is the etype of the full base. However, if the
11669 -- full base is a derived type, the full view then looks like a subtype
11670 -- of the parent, not a subtype of the full base. If instead we write:
11671
11672 -- Set_Etype (Full, Full_Base);
11673
11674 -- then we get inconsistencies in the front-end (confusion between
11675 -- views). Several outstanding bugs are related to this ???
11676
11677 Set_Is_First_Subtype (Full, False);
11678 Set_Scope (Full, Scope (Priv));
11679 Set_Size_Info (Full, Full_Base);
11680 Set_RM_Size (Full, RM_Size (Full_Base));
11681 Set_Is_Itype (Full);
11682
11683 -- A subtype of a private-type-without-discriminants, whose full-view
11684 -- has discriminants with default expressions, is not constrained.
11685
11686 if not Has_Discriminants (Priv) then
11687 Set_Is_Constrained (Full, Is_Constrained (Full_Base));
11688
11689 if Has_Discriminants (Full_Base) then
11690 Set_Discriminant_Constraint
11691 (Full, Discriminant_Constraint (Full_Base));
11692
11693 -- The partial view may have been indefinite, the full view
11694 -- might not be.
11695
11696 Set_Has_Unknown_Discriminants
11697 (Full, Has_Unknown_Discriminants (Full_Base));
11698 end if;
11699 end if;
11700
11701 Set_First_Rep_Item (Full, First_Rep_Item (Full_Base));
11702 Set_Depends_On_Private (Full, Has_Private_Component (Full));
11703
11704 -- Freeze the private subtype entity if its parent is delayed, and not
11705 -- already frozen. We skip this processing if the type is an anonymous
11706 -- subtype of a record component, or is the corresponding record of a
11707 -- protected type, since these are processed when the enclosing type
11708 -- is frozen.
11709
11710 if not Is_Type (Scope (Full)) then
11711 Set_Has_Delayed_Freeze (Full,
11712 Has_Delayed_Freeze (Full_Base)
11713 and then (not Is_Frozen (Full_Base)));
11714 end if;
11715
11716 Set_Freeze_Node (Full, Empty);
11717 Set_Is_Frozen (Full, False);
11718 Set_Full_View (Priv, Full);
11719
11720 if Has_Discriminants (Full) then
11721 Set_Stored_Constraint_From_Discriminant_Constraint (Full);
11722 Set_Stored_Constraint (Priv, Stored_Constraint (Full));
11723
11724 if Has_Unknown_Discriminants (Full) then
11725 Set_Discriminant_Constraint (Full, No_Elist);
11726 end if;
11727 end if;
11728
11729 if Ekind (Full_Base) = E_Record_Type
11730 and then Has_Discriminants (Full_Base)
11731 and then Has_Discriminants (Priv) -- might not, if errors
11732 and then not Has_Unknown_Discriminants (Priv)
11733 and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
11734 then
11735 Create_Constrained_Components
11736 (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
11737
11738 -- If the full base is itself derived from private, build a congruent
11739 -- subtype of its underlying type, for use by the back end. For a
11740 -- constrained record component, the declaration cannot be placed on
11741 -- the component list, but it must nevertheless be built an analyzed, to
11742 -- supply enough information for Gigi to compute the size of component.
11743
11744 elsif Ekind (Full_Base) in Private_Kind
11745 and then Is_Derived_Type (Full_Base)
11746 and then Has_Discriminants (Full_Base)
11747 and then (Ekind (Current_Scope) /= E_Record_Subtype)
11748 then
11749 if not Is_Itype (Priv)
11750 and then
11751 Nkind (Subtype_Indication (Parent (Priv))) = N_Subtype_Indication
11752 then
11753 Build_Underlying_Full_View
11754 (Parent (Priv), Full, Etype (Full_Base));
11755
11756 elsif Nkind (Related_Nod) = N_Component_Declaration then
11757 Build_Underlying_Full_View (Related_Nod, Full, Etype (Full_Base));
11758 end if;
11759
11760 elsif Is_Record_Type (Full_Base) then
11761
11762 -- Show Full is simply a renaming of Full_Base
11763
11764 Set_Cloned_Subtype (Full, Full_Base);
11765 end if;
11766
11767 -- It is unsafe to share the bounds of a scalar type, because the Itype
11768 -- is elaborated on demand, and if a bound is non-static then different
11769 -- orders of elaboration in different units will lead to different
11770 -- external symbols.
11771
11772 if Is_Scalar_Type (Full_Base) then
11773 Set_Scalar_Range (Full,
11774 Make_Range (Sloc (Related_Nod),
11775 Low_Bound =>
11776 Duplicate_Subexpr_No_Checks (Type_Low_Bound (Full_Base)),
11777 High_Bound =>
11778 Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
11779
11780 -- This completion inherits the bounds of the full parent, but if
11781 -- the parent is an unconstrained floating point type, so is the
11782 -- completion.
11783
11784 if Is_Floating_Point_Type (Full_Base) then
11785 Set_Includes_Infinities
11786 (Scalar_Range (Full), Has_Infinities (Full_Base));
11787 end if;
11788 end if;
11789
11790 -- ??? It seems that a lot of fields are missing that should be copied
11791 -- from Full_Base to Full. Here are some that are introduced in a
11792 -- non-disruptive way but a cleanup is necessary.
11793
11794 if Is_Tagged_Type (Full_Base) then
11795 Set_Is_Tagged_Type (Full);
11796 Set_Direct_Primitive_Operations
11797 (Full, Direct_Primitive_Operations (Full_Base));
11798 Set_No_Tagged_Streams_Pragma
11799 (Full, No_Tagged_Streams_Pragma (Full_Base));
11800
11801 -- Inherit class_wide type of full_base in case the partial view was
11802 -- not tagged. Otherwise it has already been created when the private
11803 -- subtype was analyzed.
11804
11805 if No (Class_Wide_Type (Full)) then
11806 Set_Class_Wide_Type (Full, Class_Wide_Type (Full_Base));
11807 end if;
11808
11809 -- If this is a subtype of a protected or task type, constrain its
11810 -- corresponding record, unless this is a subtype without constraints,
11811 -- i.e. a simple renaming as with an actual subtype in an instance.
11812
11813 elsif Is_Concurrent_Type (Full_Base) then
11814 if Has_Discriminants (Full)
11815 and then Present (Corresponding_Record_Type (Full_Base))
11816 and then
11817 not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
11818 then
11819 Set_Corresponding_Record_Type (Full,
11820 Constrain_Corresponding_Record
11821 (Full, Corresponding_Record_Type (Full_Base), Related_Nod));
11822
11823 else
11824 Set_Corresponding_Record_Type (Full,
11825 Corresponding_Record_Type (Full_Base));
11826 end if;
11827 end if;
11828
11829 -- Link rep item chain, and also setting of Has_Predicates from private
11830 -- subtype to full subtype, since we will need these on the full subtype
11831 -- to create the predicate function. Note that the full subtype may
11832 -- already have rep items, inherited from the full view of the base
11833 -- type, so we must be sure not to overwrite these entries.
11834
11835 declare
11836 Append : Boolean;
11837 Item : Node_Id;
11838 Next_Item : Node_Id;
11839
11840 begin
11841 Item := First_Rep_Item (Full);
11842
11843 -- If no existing rep items on full type, we can just link directly
11844 -- to the list of items on the private type, if any exist.. Same if
11845 -- the rep items are only those inherited from the base
11846
11847 if (No (Item)
11848 or else Nkind (Item) /= N_Aspect_Specification
11849 or else Entity (Item) = Full_Base)
11850 and then Present (First_Rep_Item (Priv))
11851 then
11852 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
11853
11854 -- Otherwise, search to the end of items currently linked to the full
11855 -- subtype and append the private items to the end. However, if Priv
11856 -- and Full already have the same list of rep items, then the append
11857 -- is not done, as that would create a circularity.
11858
11859 elsif Item /= First_Rep_Item (Priv) then
11860 Append := True;
11861 loop
11862 Next_Item := Next_Rep_Item (Item);
11863 exit when No (Next_Item);
11864 Item := Next_Item;
11865
11866 -- If the private view has aspect specifications, the full view
11867 -- inherits them. Since these aspects may already have been
11868 -- attached to the full view during derivation, do not append
11869 -- them if already present.
11870
11871 if Item = First_Rep_Item (Priv) then
11872 Append := False;
11873 exit;
11874 end if;
11875 end loop;
11876
11877 -- And link the private type items at the end of the chain
11878
11879 if Append then
11880 Set_Next_Rep_Item (Item, First_Rep_Item (Priv));
11881 end if;
11882 end if;
11883 end;
11884
11885 -- Make sure Has_Predicates is set on full type if it is set on the
11886 -- private type. Note that it may already be set on the full type and
11887 -- if so, we don't want to unset it. Similarly, propagate information
11888 -- about delayed aspects, because the corresponding pragmas must be
11889 -- analyzed when one of the views is frozen. This last step is needed
11890 -- in particular when the full type is a scalar type for which an
11891 -- anonymous base type is constructed.
11892
11893 if Has_Predicates (Priv) then
11894 Set_Has_Predicates (Full);
11895 end if;
11896
11897 if Has_Delayed_Aspects (Priv) then
11898 Set_Has_Delayed_Aspects (Full);
11899 end if;
11900 end Complete_Private_Subtype;
11901
11902 ----------------------------
11903 -- Constant_Redeclaration --
11904 ----------------------------
11905
11906 procedure Constant_Redeclaration
11907 (Id : Entity_Id;
11908 N : Node_Id;
11909 T : out Entity_Id)
11910 is
11911 Prev : constant Entity_Id := Current_Entity_In_Scope (Id);
11912 Obj_Def : constant Node_Id := Object_Definition (N);
11913 New_T : Entity_Id;
11914
11915 procedure Check_Possible_Deferred_Completion
11916 (Prev_Id : Entity_Id;
11917 Prev_Obj_Def : Node_Id;
11918 Curr_Obj_Def : Node_Id);
11919 -- Determine whether the two object definitions describe the partial
11920 -- and the full view of a constrained deferred constant. Generate
11921 -- a subtype for the full view and verify that it statically matches
11922 -- the subtype of the partial view.
11923
11924 procedure Check_Recursive_Declaration (Typ : Entity_Id);
11925 -- If deferred constant is an access type initialized with an allocator,
11926 -- check whether there is an illegal recursion in the definition,
11927 -- through a default value of some record subcomponent. This is normally
11928 -- detected when generating init procs, but requires this additional
11929 -- mechanism when expansion is disabled.
11930
11931 ----------------------------------------
11932 -- Check_Possible_Deferred_Completion --
11933 ----------------------------------------
11934
11935 procedure Check_Possible_Deferred_Completion
11936 (Prev_Id : Entity_Id;
11937 Prev_Obj_Def : Node_Id;
11938 Curr_Obj_Def : Node_Id)
11939 is
11940 begin
11941 if Nkind (Prev_Obj_Def) = N_Subtype_Indication
11942 and then Present (Constraint (Prev_Obj_Def))
11943 and then Nkind (Curr_Obj_Def) = N_Subtype_Indication
11944 and then Present (Constraint (Curr_Obj_Def))
11945 then
11946 declare
11947 Loc : constant Source_Ptr := Sloc (N);
11948 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
11949 Decl : constant Node_Id :=
11950 Make_Subtype_Declaration (Loc,
11951 Defining_Identifier => Def_Id,
11952 Subtype_Indication =>
11953 Relocate_Node (Curr_Obj_Def));
11954
11955 begin
11956 Insert_Before_And_Analyze (N, Decl);
11957 Set_Etype (Id, Def_Id);
11958
11959 if not Subtypes_Statically_Match (Etype (Prev_Id), Def_Id) then
11960 Error_Msg_Sloc := Sloc (Prev_Id);
11961 Error_Msg_N ("subtype does not statically match deferred "
11962 & "declaration #", N);
11963 end if;
11964 end;
11965 end if;
11966 end Check_Possible_Deferred_Completion;
11967
11968 ---------------------------------
11969 -- Check_Recursive_Declaration --
11970 ---------------------------------
11971
11972 procedure Check_Recursive_Declaration (Typ : Entity_Id) is
11973 Comp : Entity_Id;
11974
11975 begin
11976 if Is_Record_Type (Typ) then
11977 Comp := First_Component (Typ);
11978 while Present (Comp) loop
11979 if Comes_From_Source (Comp) then
11980 if Present (Expression (Parent (Comp)))
11981 and then Is_Entity_Name (Expression (Parent (Comp)))
11982 and then Entity (Expression (Parent (Comp))) = Prev
11983 then
11984 Error_Msg_Sloc := Sloc (Parent (Comp));
11985 Error_Msg_NE
11986 ("illegal circularity with declaration for & #",
11987 N, Comp);
11988 return;
11989
11990 elsif Is_Record_Type (Etype (Comp)) then
11991 Check_Recursive_Declaration (Etype (Comp));
11992 end if;
11993 end if;
11994
11995 Next_Component (Comp);
11996 end loop;
11997 end if;
11998 end Check_Recursive_Declaration;
11999
12000 -- Start of processing for Constant_Redeclaration
12001
12002 begin
12003 if Nkind (Parent (Prev)) = N_Object_Declaration then
12004 if Nkind (Object_Definition
12005 (Parent (Prev))) = N_Subtype_Indication
12006 then
12007 -- Find type of new declaration. The constraints of the two
12008 -- views must match statically, but there is no point in
12009 -- creating an itype for the full view.
12010
12011 if Nkind (Obj_Def) = N_Subtype_Indication then
12012 Find_Type (Subtype_Mark (Obj_Def));
12013 New_T := Entity (Subtype_Mark (Obj_Def));
12014
12015 else
12016 Find_Type (Obj_Def);
12017 New_T := Entity (Obj_Def);
12018 end if;
12019
12020 T := Etype (Prev);
12021
12022 else
12023 -- The full view may impose a constraint, even if the partial
12024 -- view does not, so construct the subtype.
12025
12026 New_T := Find_Type_Of_Object (Obj_Def, N);
12027 T := New_T;
12028 end if;
12029
12030 else
12031 -- Current declaration is illegal, diagnosed below in Enter_Name
12032
12033 T := Empty;
12034 New_T := Any_Type;
12035 end if;
12036
12037 -- If previous full declaration or a renaming declaration exists, or if
12038 -- a homograph is present, let Enter_Name handle it, either with an
12039 -- error or with the removal of an overridden implicit subprogram.
12040 -- The previous one is a full declaration if it has an expression
12041 -- (which in the case of an aggregate is indicated by the Init flag).
12042
12043 if Ekind (Prev) /= E_Constant
12044 or else Nkind (Parent (Prev)) = N_Object_Renaming_Declaration
12045 or else Present (Expression (Parent (Prev)))
12046 or else Has_Init_Expression (Parent (Prev))
12047 or else Present (Full_View (Prev))
12048 then
12049 Enter_Name (Id);
12050
12051 -- Verify that types of both declarations match, or else that both types
12052 -- are anonymous access types whose designated subtypes statically match
12053 -- (as allowed in Ada 2005 by AI-385).
12054
12055 elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
12056 and then
12057 (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
12058 or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
12059 or else Is_Access_Constant (Etype (New_T)) /=
12060 Is_Access_Constant (Etype (Prev))
12061 or else Can_Never_Be_Null (Etype (New_T)) /=
12062 Can_Never_Be_Null (Etype (Prev))
12063 or else Null_Exclusion_Present (Parent (Prev)) /=
12064 Null_Exclusion_Present (Parent (Id))
12065 or else not Subtypes_Statically_Match
12066 (Designated_Type (Etype (Prev)),
12067 Designated_Type (Etype (New_T))))
12068 then
12069 Error_Msg_Sloc := Sloc (Prev);
12070 Error_Msg_N ("type does not match declaration#", N);
12071 Set_Full_View (Prev, Id);
12072 Set_Etype (Id, Any_Type);
12073
12074 -- A deferred constant whose type is an anonymous array is always
12075 -- illegal (unless imported). A detailed error message might be
12076 -- helpful for Ada beginners.
12077
12078 if Nkind (Object_Definition (Parent (Prev)))
12079 = N_Constrained_Array_Definition
12080 and then Nkind (Object_Definition (N))
12081 = N_Constrained_Array_Definition
12082 then
12083 Error_Msg_N ("\each anonymous array is a distinct type", N);
12084 Error_Msg_N ("a deferred constant must have a named type",
12085 Object_Definition (Parent (Prev)));
12086 end if;
12087
12088 elsif
12089 Null_Exclusion_Present (Parent (Prev))
12090 and then not Null_Exclusion_Present (N)
12091 then
12092 Error_Msg_Sloc := Sloc (Prev);
12093 Error_Msg_N ("null-exclusion does not match declaration#", N);
12094 Set_Full_View (Prev, Id);
12095 Set_Etype (Id, Any_Type);
12096
12097 -- If so, process the full constant declaration
12098
12099 else
12100 -- RM 7.4 (6): If the subtype defined by the subtype_indication in
12101 -- the deferred declaration is constrained, then the subtype defined
12102 -- by the subtype_indication in the full declaration shall match it
12103 -- statically.
12104
12105 Check_Possible_Deferred_Completion
12106 (Prev_Id => Prev,
12107 Prev_Obj_Def => Object_Definition (Parent (Prev)),
12108 Curr_Obj_Def => Obj_Def);
12109
12110 Set_Full_View (Prev, Id);
12111 Set_Is_Public (Id, Is_Public (Prev));
12112 Set_Is_Internal (Id);
12113 Append_Entity (Id, Current_Scope);
12114
12115 -- Check ALIASED present if present before (RM 7.4(7))
12116
12117 if Is_Aliased (Prev)
12118 and then not Aliased_Present (N)
12119 then
12120 Error_Msg_Sloc := Sloc (Prev);
12121 Error_Msg_N ("ALIASED required (see declaration #)", N);
12122 end if;
12123
12124 -- Check that placement is in private part and that the incomplete
12125 -- declaration appeared in the visible part.
12126
12127 if Ekind (Current_Scope) = E_Package
12128 and then not In_Private_Part (Current_Scope)
12129 then
12130 Error_Msg_Sloc := Sloc (Prev);
12131 Error_Msg_N
12132 ("full constant for declaration # must be in private part", N);
12133
12134 elsif Ekind (Current_Scope) = E_Package
12135 and then
12136 List_Containing (Parent (Prev)) /=
12137 Visible_Declarations (Package_Specification (Current_Scope))
12138 then
12139 Error_Msg_N
12140 ("deferred constant must be declared in visible part",
12141 Parent (Prev));
12142 end if;
12143
12144 if Is_Access_Type (T)
12145 and then Nkind (Expression (N)) = N_Allocator
12146 then
12147 Check_Recursive_Declaration (Designated_Type (T));
12148 end if;
12149
12150 -- A deferred constant is a visible entity. If type has invariants,
12151 -- verify that the initial value satisfies them.
12152
12153 if Has_Invariants (T) and then Present (Invariant_Procedure (T)) then
12154 Insert_After (N,
12155 Make_Invariant_Call (New_Occurrence_Of (Prev, Sloc (N))));
12156 end if;
12157 end if;
12158 end Constant_Redeclaration;
12159
12160 ----------------------
12161 -- Constrain_Access --
12162 ----------------------
12163
12164 procedure Constrain_Access
12165 (Def_Id : in out Entity_Id;
12166 S : Node_Id;
12167 Related_Nod : Node_Id)
12168 is
12169 T : constant Entity_Id := Entity (Subtype_Mark (S));
12170 Desig_Type : constant Entity_Id := Designated_Type (T);
12171 Desig_Subtype : Entity_Id := Create_Itype (E_Void, Related_Nod);
12172 Constraint_OK : Boolean := True;
12173
12174 begin
12175 if Is_Array_Type (Desig_Type) then
12176 Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
12177
12178 elsif (Is_Record_Type (Desig_Type)
12179 or else Is_Incomplete_Or_Private_Type (Desig_Type))
12180 and then not Is_Constrained (Desig_Type)
12181 then
12182 -- ??? The following code is a temporary bypass to ignore a
12183 -- discriminant constraint on access type if it is constraining
12184 -- the current record. Avoid creating the implicit subtype of the
12185 -- record we are currently compiling since right now, we cannot
12186 -- handle these. For now, just return the access type itself.
12187
12188 if Desig_Type = Current_Scope
12189 and then No (Def_Id)
12190 then
12191 Set_Ekind (Desig_Subtype, E_Record_Subtype);
12192 Def_Id := Entity (Subtype_Mark (S));
12193
12194 -- This call added to ensure that the constraint is analyzed
12195 -- (needed for a B test). Note that we still return early from
12196 -- this procedure to avoid recursive processing. ???
12197
12198 Constrain_Discriminated_Type
12199 (Desig_Subtype, S, Related_Nod, For_Access => True);
12200 return;
12201 end if;
12202
12203 -- Enforce rule that the constraint is illegal if there is an
12204 -- unconstrained view of the designated type. This means that the
12205 -- partial view (either a private type declaration or a derivation
12206 -- from a private type) has no discriminants. (Defect Report
12207 -- 8652/0008, Technical Corrigendum 1, checked by ACATS B371001).
12208
12209 -- Rule updated for Ada 2005: The private type is said to have
12210 -- a constrained partial view, given that objects of the type
12211 -- can be declared. Furthermore, the rule applies to all access
12212 -- types, unlike the rule concerning default discriminants (see
12213 -- RM 3.7.1(7/3))
12214
12215 if (Ekind (T) = E_General_Access_Type or else Ada_Version >= Ada_2005)
12216 and then Has_Private_Declaration (Desig_Type)
12217 and then In_Open_Scopes (Scope (Desig_Type))
12218 and then Has_Discriminants (Desig_Type)
12219 then
12220 declare
12221 Pack : constant Node_Id :=
12222 Unit_Declaration_Node (Scope (Desig_Type));
12223 Decls : List_Id;
12224 Decl : Node_Id;
12225
12226 begin
12227 if Nkind (Pack) = N_Package_Declaration then
12228 Decls := Visible_Declarations (Specification (Pack));
12229 Decl := First (Decls);
12230 while Present (Decl) loop
12231 if (Nkind (Decl) = N_Private_Type_Declaration
12232 and then Chars (Defining_Identifier (Decl)) =
12233 Chars (Desig_Type))
12234
12235 or else
12236 (Nkind (Decl) = N_Full_Type_Declaration
12237 and then
12238 Chars (Defining_Identifier (Decl)) =
12239 Chars (Desig_Type)
12240 and then Is_Derived_Type (Desig_Type)
12241 and then
12242 Has_Private_Declaration (Etype (Desig_Type)))
12243 then
12244 if No (Discriminant_Specifications (Decl)) then
12245 Error_Msg_N
12246 ("cannot constrain access type if designated "
12247 & "type has constrained partial view", S);
12248 end if;
12249
12250 exit;
12251 end if;
12252
12253 Next (Decl);
12254 end loop;
12255 end if;
12256 end;
12257 end if;
12258
12259 Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
12260 For_Access => True);
12261
12262 elsif Is_Concurrent_Type (Desig_Type)
12263 and then not Is_Constrained (Desig_Type)
12264 then
12265 Constrain_Concurrent (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
12266
12267 else
12268 Error_Msg_N ("invalid constraint on access type", S);
12269
12270 -- We simply ignore an invalid constraint
12271
12272 Desig_Subtype := Desig_Type;
12273 Constraint_OK := False;
12274 end if;
12275
12276 if No (Def_Id) then
12277 Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
12278 else
12279 Set_Ekind (Def_Id, E_Access_Subtype);
12280 end if;
12281
12282 if Constraint_OK then
12283 Set_Etype (Def_Id, Base_Type (T));
12284
12285 if Is_Private_Type (Desig_Type) then
12286 Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
12287 end if;
12288 else
12289 Set_Etype (Def_Id, Any_Type);
12290 end if;
12291
12292 Set_Size_Info (Def_Id, T);
12293 Set_Is_Constrained (Def_Id, Constraint_OK);
12294 Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
12295 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
12296 Set_Is_Access_Constant (Def_Id, Is_Access_Constant (T));
12297
12298 Conditional_Delay (Def_Id, T);
12299
12300 -- AI-363 : Subtypes of general access types whose designated types have
12301 -- default discriminants are disallowed. In instances, the rule has to
12302 -- be checked against the actual, of which T is the subtype. In a
12303 -- generic body, the rule is checked assuming that the actual type has
12304 -- defaulted discriminants.
12305
12306 if Ada_Version >= Ada_2005 or else Warn_On_Ada_2005_Compatibility then
12307 if Ekind (Base_Type (T)) = E_General_Access_Type
12308 and then Has_Defaulted_Discriminants (Desig_Type)
12309 then
12310 if Ada_Version < Ada_2005 then
12311 Error_Msg_N
12312 ("access subtype of general access type would not " &
12313 "be allowed in Ada 2005?y?", S);
12314 else
12315 Error_Msg_N
12316 ("access subtype of general access type not allowed", S);
12317 end if;
12318
12319 Error_Msg_N ("\discriminants have defaults", S);
12320
12321 elsif Is_Access_Type (T)
12322 and then Is_Generic_Type (Desig_Type)
12323 and then Has_Discriminants (Desig_Type)
12324 and then In_Package_Body (Current_Scope)
12325 then
12326 if Ada_Version < Ada_2005 then
12327 Error_Msg_N
12328 ("access subtype would not be allowed in generic body "
12329 & "in Ada 2005?y?", S);
12330 else
12331 Error_Msg_N
12332 ("access subtype not allowed in generic body", S);
12333 end if;
12334
12335 Error_Msg_N
12336 ("\designated type is a discriminated formal", S);
12337 end if;
12338 end if;
12339 end Constrain_Access;
12340
12341 ---------------------
12342 -- Constrain_Array --
12343 ---------------------
12344
12345 procedure Constrain_Array
12346 (Def_Id : in out Entity_Id;
12347 SI : Node_Id;
12348 Related_Nod : Node_Id;
12349 Related_Id : Entity_Id;
12350 Suffix : Character)
12351 is
12352 C : constant Node_Id := Constraint (SI);
12353 Number_Of_Constraints : Nat := 0;
12354 Index : Node_Id;
12355 S, T : Entity_Id;
12356 Constraint_OK : Boolean := True;
12357
12358 begin
12359 T := Entity (Subtype_Mark (SI));
12360
12361 if Is_Access_Type (T) then
12362 T := Designated_Type (T);
12363 end if;
12364
12365 -- If an index constraint follows a subtype mark in a subtype indication
12366 -- then the type or subtype denoted by the subtype mark must not already
12367 -- impose an index constraint. The subtype mark must denote either an
12368 -- unconstrained array type or an access type whose designated type
12369 -- is such an array type... (RM 3.6.1)
12370
12371 if Is_Constrained (T) then
12372 Error_Msg_N ("array type is already constrained", Subtype_Mark (SI));
12373 Constraint_OK := False;
12374
12375 else
12376 S := First (Constraints (C));
12377 while Present (S) loop
12378 Number_Of_Constraints := Number_Of_Constraints + 1;
12379 Next (S);
12380 end loop;
12381
12382 -- In either case, the index constraint must provide a discrete
12383 -- range for each index of the array type and the type of each
12384 -- discrete range must be the same as that of the corresponding
12385 -- index. (RM 3.6.1)
12386
12387 if Number_Of_Constraints /= Number_Dimensions (T) then
12388 Error_Msg_NE ("incorrect number of index constraints for }", C, T);
12389 Constraint_OK := False;
12390
12391 else
12392 S := First (Constraints (C));
12393 Index := First_Index (T);
12394 Analyze (Index);
12395
12396 -- Apply constraints to each index type
12397
12398 for J in 1 .. Number_Of_Constraints loop
12399 Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
12400 Next (Index);
12401 Next (S);
12402 end loop;
12403
12404 end if;
12405 end if;
12406
12407 if No (Def_Id) then
12408 Def_Id :=
12409 Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
12410 Set_Parent (Def_Id, Related_Nod);
12411
12412 else
12413 Set_Ekind (Def_Id, E_Array_Subtype);
12414 end if;
12415
12416 Set_Size_Info (Def_Id, (T));
12417 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
12418 Set_Etype (Def_Id, Base_Type (T));
12419
12420 if Constraint_OK then
12421 Set_First_Index (Def_Id, First (Constraints (C)));
12422 else
12423 Set_First_Index (Def_Id, First_Index (T));
12424 end if;
12425
12426 Set_Is_Constrained (Def_Id, True);
12427 Set_Is_Aliased (Def_Id, Is_Aliased (T));
12428 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
12429
12430 Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
12431 Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
12432
12433 -- A subtype does not inherit the Packed_Array_Impl_Type of is parent.
12434 -- We need to initialize the attribute because if Def_Id is previously
12435 -- analyzed through a limited_with clause, it will have the attributes
12436 -- of an incomplete type, one of which is an Elist that overlaps the
12437 -- Packed_Array_Impl_Type field.
12438
12439 Set_Packed_Array_Impl_Type (Def_Id, Empty);
12440
12441 -- Build a freeze node if parent still needs one. Also make sure that
12442 -- the Depends_On_Private status is set because the subtype will need
12443 -- reprocessing at the time the base type does, and also we must set a
12444 -- conditional delay.
12445
12446 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
12447 Conditional_Delay (Def_Id, T);
12448 end Constrain_Array;
12449
12450 ------------------------------
12451 -- Constrain_Component_Type --
12452 ------------------------------
12453
12454 function Constrain_Component_Type
12455 (Comp : Entity_Id;
12456 Constrained_Typ : Entity_Id;
12457 Related_Node : Node_Id;
12458 Typ : Entity_Id;
12459 Constraints : Elist_Id) return Entity_Id
12460 is
12461 Loc : constant Source_Ptr := Sloc (Constrained_Typ);
12462 Compon_Type : constant Entity_Id := Etype (Comp);
12463
12464 function Build_Constrained_Array_Type
12465 (Old_Type : Entity_Id) return Entity_Id;
12466 -- If Old_Type is an array type, one of whose indexes is constrained
12467 -- by a discriminant, build an Itype whose constraint replaces the
12468 -- discriminant with its value in the constraint.
12469
12470 function Build_Constrained_Discriminated_Type
12471 (Old_Type : Entity_Id) return Entity_Id;
12472 -- Ditto for record components
12473
12474 function Build_Constrained_Access_Type
12475 (Old_Type : Entity_Id) return Entity_Id;
12476 -- Ditto for access types. Makes use of previous two functions, to
12477 -- constrain designated type.
12478
12479 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id;
12480 -- T is an array or discriminated type, C is a list of constraints
12481 -- that apply to T. This routine builds the constrained subtype.
12482
12483 function Is_Discriminant (Expr : Node_Id) return Boolean;
12484 -- Returns True if Expr is a discriminant
12485
12486 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id;
12487 -- Find the value of discriminant Discrim in Constraint
12488
12489 -----------------------------------
12490 -- Build_Constrained_Access_Type --
12491 -----------------------------------
12492
12493 function Build_Constrained_Access_Type
12494 (Old_Type : Entity_Id) return Entity_Id
12495 is
12496 Desig_Type : constant Entity_Id := Designated_Type (Old_Type);
12497 Itype : Entity_Id;
12498 Desig_Subtype : Entity_Id;
12499 Scop : Entity_Id;
12500
12501 begin
12502 -- if the original access type was not embedded in the enclosing
12503 -- type definition, there is no need to produce a new access
12504 -- subtype. In fact every access type with an explicit constraint
12505 -- generates an itype whose scope is the enclosing record.
12506
12507 if not Is_Type (Scope (Old_Type)) then
12508 return Old_Type;
12509
12510 elsif Is_Array_Type (Desig_Type) then
12511 Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
12512
12513 elsif Has_Discriminants (Desig_Type) then
12514
12515 -- This may be an access type to an enclosing record type for
12516 -- which we are constructing the constrained components. Return
12517 -- the enclosing record subtype. This is not always correct,
12518 -- but avoids infinite recursion. ???
12519
12520 Desig_Subtype := Any_Type;
12521
12522 for J in reverse 0 .. Scope_Stack.Last loop
12523 Scop := Scope_Stack.Table (J).Entity;
12524
12525 if Is_Type (Scop)
12526 and then Base_Type (Scop) = Base_Type (Desig_Type)
12527 then
12528 Desig_Subtype := Scop;
12529 end if;
12530
12531 exit when not Is_Type (Scop);
12532 end loop;
12533
12534 if Desig_Subtype = Any_Type then
12535 Desig_Subtype :=
12536 Build_Constrained_Discriminated_Type (Desig_Type);
12537 end if;
12538
12539 else
12540 return Old_Type;
12541 end if;
12542
12543 if Desig_Subtype /= Desig_Type then
12544
12545 -- The Related_Node better be here or else we won't be able
12546 -- to attach new itypes to a node in the tree.
12547
12548 pragma Assert (Present (Related_Node));
12549
12550 Itype := Create_Itype (E_Access_Subtype, Related_Node);
12551
12552 Set_Etype (Itype, Base_Type (Old_Type));
12553 Set_Size_Info (Itype, (Old_Type));
12554 Set_Directly_Designated_Type (Itype, Desig_Subtype);
12555 Set_Depends_On_Private (Itype, Has_Private_Component
12556 (Old_Type));
12557 Set_Is_Access_Constant (Itype, Is_Access_Constant
12558 (Old_Type));
12559
12560 -- The new itype needs freezing when it depends on a not frozen
12561 -- type and the enclosing subtype needs freezing.
12562
12563 if Has_Delayed_Freeze (Constrained_Typ)
12564 and then not Is_Frozen (Constrained_Typ)
12565 then
12566 Conditional_Delay (Itype, Base_Type (Old_Type));
12567 end if;
12568
12569 return Itype;
12570
12571 else
12572 return Old_Type;
12573 end if;
12574 end Build_Constrained_Access_Type;
12575
12576 ----------------------------------
12577 -- Build_Constrained_Array_Type --
12578 ----------------------------------
12579
12580 function Build_Constrained_Array_Type
12581 (Old_Type : Entity_Id) return Entity_Id
12582 is
12583 Lo_Expr : Node_Id;
12584 Hi_Expr : Node_Id;
12585 Old_Index : Node_Id;
12586 Range_Node : Node_Id;
12587 Constr_List : List_Id;
12588
12589 Need_To_Create_Itype : Boolean := False;
12590
12591 begin
12592 Old_Index := First_Index (Old_Type);
12593 while Present (Old_Index) loop
12594 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
12595
12596 if Is_Discriminant (Lo_Expr)
12597 or else
12598 Is_Discriminant (Hi_Expr)
12599 then
12600 Need_To_Create_Itype := True;
12601 end if;
12602
12603 Next_Index (Old_Index);
12604 end loop;
12605
12606 if Need_To_Create_Itype then
12607 Constr_List := New_List;
12608
12609 Old_Index := First_Index (Old_Type);
12610 while Present (Old_Index) loop
12611 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
12612
12613 if Is_Discriminant (Lo_Expr) then
12614 Lo_Expr := Get_Discr_Value (Lo_Expr);
12615 end if;
12616
12617 if Is_Discriminant (Hi_Expr) then
12618 Hi_Expr := Get_Discr_Value (Hi_Expr);
12619 end if;
12620
12621 Range_Node :=
12622 Make_Range
12623 (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
12624
12625 Append (Range_Node, To => Constr_List);
12626
12627 Next_Index (Old_Index);
12628 end loop;
12629
12630 return Build_Subtype (Old_Type, Constr_List);
12631
12632 else
12633 return Old_Type;
12634 end if;
12635 end Build_Constrained_Array_Type;
12636
12637 ------------------------------------------
12638 -- Build_Constrained_Discriminated_Type --
12639 ------------------------------------------
12640
12641 function Build_Constrained_Discriminated_Type
12642 (Old_Type : Entity_Id) return Entity_Id
12643 is
12644 Expr : Node_Id;
12645 Constr_List : List_Id;
12646 Old_Constraint : Elmt_Id;
12647
12648 Need_To_Create_Itype : Boolean := False;
12649
12650 begin
12651 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
12652 while Present (Old_Constraint) loop
12653 Expr := Node (Old_Constraint);
12654
12655 if Is_Discriminant (Expr) then
12656 Need_To_Create_Itype := True;
12657 end if;
12658
12659 Next_Elmt (Old_Constraint);
12660 end loop;
12661
12662 if Need_To_Create_Itype then
12663 Constr_List := New_List;
12664
12665 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
12666 while Present (Old_Constraint) loop
12667 Expr := Node (Old_Constraint);
12668
12669 if Is_Discriminant (Expr) then
12670 Expr := Get_Discr_Value (Expr);
12671 end if;
12672
12673 Append (New_Copy_Tree (Expr), To => Constr_List);
12674
12675 Next_Elmt (Old_Constraint);
12676 end loop;
12677
12678 return Build_Subtype (Old_Type, Constr_List);
12679
12680 else
12681 return Old_Type;
12682 end if;
12683 end Build_Constrained_Discriminated_Type;
12684
12685 -------------------
12686 -- Build_Subtype --
12687 -------------------
12688
12689 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id is
12690 Indic : Node_Id;
12691 Subtyp_Decl : Node_Id;
12692 Def_Id : Entity_Id;
12693 Btyp : Entity_Id := Base_Type (T);
12694
12695 begin
12696 -- The Related_Node better be here or else we won't be able to
12697 -- attach new itypes to a node in the tree.
12698
12699 pragma Assert (Present (Related_Node));
12700
12701 -- If the view of the component's type is incomplete or private
12702 -- with unknown discriminants, then the constraint must be applied
12703 -- to the full type.
12704
12705 if Has_Unknown_Discriminants (Btyp)
12706 and then Present (Underlying_Type (Btyp))
12707 then
12708 Btyp := Underlying_Type (Btyp);
12709 end if;
12710
12711 Indic :=
12712 Make_Subtype_Indication (Loc,
12713 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
12714 Constraint => Make_Index_Or_Discriminant_Constraint (Loc, C));
12715
12716 Def_Id := Create_Itype (Ekind (T), Related_Node);
12717
12718 Subtyp_Decl :=
12719 Make_Subtype_Declaration (Loc,
12720 Defining_Identifier => Def_Id,
12721 Subtype_Indication => Indic);
12722
12723 Set_Parent (Subtyp_Decl, Parent (Related_Node));
12724
12725 -- Itypes must be analyzed with checks off (see package Itypes)
12726
12727 Analyze (Subtyp_Decl, Suppress => All_Checks);
12728
12729 return Def_Id;
12730 end Build_Subtype;
12731
12732 ---------------------
12733 -- Get_Discr_Value --
12734 ---------------------
12735
12736 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id is
12737 D : Entity_Id;
12738 E : Elmt_Id;
12739
12740 begin
12741 -- The discriminant may be declared for the type, in which case we
12742 -- find it by iterating over the list of discriminants. If the
12743 -- discriminant is inherited from a parent type, it appears as the
12744 -- corresponding discriminant of the current type. This will be the
12745 -- case when constraining an inherited component whose constraint is
12746 -- given by a discriminant of the parent.
12747
12748 D := First_Discriminant (Typ);
12749 E := First_Elmt (Constraints);
12750
12751 while Present (D) loop
12752 if D = Entity (Discrim)
12753 or else D = CR_Discriminant (Entity (Discrim))
12754 or else Corresponding_Discriminant (D) = Entity (Discrim)
12755 then
12756 return Node (E);
12757 end if;
12758
12759 Next_Discriminant (D);
12760 Next_Elmt (E);
12761 end loop;
12762
12763 -- The Corresponding_Discriminant mechanism is incomplete, because
12764 -- the correspondence between new and old discriminants is not one
12765 -- to one: one new discriminant can constrain several old ones. In
12766 -- that case, scan sequentially the stored_constraint, the list of
12767 -- discriminants of the parents, and the constraints.
12768
12769 -- Previous code checked for the present of the Stored_Constraint
12770 -- list for the derived type, but did not use it at all. Should it
12771 -- be present when the component is a discriminated task type?
12772
12773 if Is_Derived_Type (Typ)
12774 and then Scope (Entity (Discrim)) = Etype (Typ)
12775 then
12776 D := First_Discriminant (Etype (Typ));
12777 E := First_Elmt (Constraints);
12778 while Present (D) loop
12779 if D = Entity (Discrim) then
12780 return Node (E);
12781 end if;
12782
12783 Next_Discriminant (D);
12784 Next_Elmt (E);
12785 end loop;
12786 end if;
12787
12788 -- Something is wrong if we did not find the value
12789
12790 raise Program_Error;
12791 end Get_Discr_Value;
12792
12793 ---------------------
12794 -- Is_Discriminant --
12795 ---------------------
12796
12797 function Is_Discriminant (Expr : Node_Id) return Boolean is
12798 Discrim_Scope : Entity_Id;
12799
12800 begin
12801 if Denotes_Discriminant (Expr) then
12802 Discrim_Scope := Scope (Entity (Expr));
12803
12804 -- Either we have a reference to one of Typ's discriminants,
12805
12806 pragma Assert (Discrim_Scope = Typ
12807
12808 -- or to the discriminants of the parent type, in the case
12809 -- of a derivation of a tagged type with variants.
12810
12811 or else Discrim_Scope = Etype (Typ)
12812 or else Full_View (Discrim_Scope) = Etype (Typ)
12813
12814 -- or same as above for the case where the discriminants
12815 -- were declared in Typ's private view.
12816
12817 or else (Is_Private_Type (Discrim_Scope)
12818 and then Chars (Discrim_Scope) = Chars (Typ))
12819
12820 -- or else we are deriving from the full view and the
12821 -- discriminant is declared in the private entity.
12822
12823 or else (Is_Private_Type (Typ)
12824 and then Chars (Discrim_Scope) = Chars (Typ))
12825
12826 -- Or we are constrained the corresponding record of a
12827 -- synchronized type that completes a private declaration.
12828
12829 or else (Is_Concurrent_Record_Type (Typ)
12830 and then
12831 Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
12832
12833 -- or we have a class-wide type, in which case make sure the
12834 -- discriminant found belongs to the root type.
12835
12836 or else (Is_Class_Wide_Type (Typ)
12837 and then Etype (Typ) = Discrim_Scope));
12838
12839 return True;
12840 end if;
12841
12842 -- In all other cases we have something wrong
12843
12844 return False;
12845 end Is_Discriminant;
12846
12847 -- Start of processing for Constrain_Component_Type
12848
12849 begin
12850 if Nkind (Parent (Comp)) = N_Component_Declaration
12851 and then Comes_From_Source (Parent (Comp))
12852 and then Comes_From_Source
12853 (Subtype_Indication (Component_Definition (Parent (Comp))))
12854 and then
12855 Is_Entity_Name
12856 (Subtype_Indication (Component_Definition (Parent (Comp))))
12857 then
12858 return Compon_Type;
12859
12860 elsif Is_Array_Type (Compon_Type) then
12861 return Build_Constrained_Array_Type (Compon_Type);
12862
12863 elsif Has_Discriminants (Compon_Type) then
12864 return Build_Constrained_Discriminated_Type (Compon_Type);
12865
12866 elsif Is_Access_Type (Compon_Type) then
12867 return Build_Constrained_Access_Type (Compon_Type);
12868
12869 else
12870 return Compon_Type;
12871 end if;
12872 end Constrain_Component_Type;
12873
12874 --------------------------
12875 -- Constrain_Concurrent --
12876 --------------------------
12877
12878 -- For concurrent types, the associated record value type carries the same
12879 -- discriminants, so when we constrain a concurrent type, we must constrain
12880 -- the corresponding record type as well.
12881
12882 procedure Constrain_Concurrent
12883 (Def_Id : in out Entity_Id;
12884 SI : Node_Id;
12885 Related_Nod : Node_Id;
12886 Related_Id : Entity_Id;
12887 Suffix : Character)
12888 is
12889 -- Retrieve Base_Type to ensure getting to the concurrent type in the
12890 -- case of a private subtype (needed when only doing semantic analysis).
12891
12892 T_Ent : Entity_Id := Base_Type (Entity (Subtype_Mark (SI)));
12893 T_Val : Entity_Id;
12894
12895 begin
12896 if Is_Access_Type (T_Ent) then
12897 T_Ent := Designated_Type (T_Ent);
12898 end if;
12899
12900 T_Val := Corresponding_Record_Type (T_Ent);
12901
12902 if Present (T_Val) then
12903
12904 if No (Def_Id) then
12905 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
12906 end if;
12907
12908 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
12909
12910 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
12911 Set_Corresponding_Record_Type (Def_Id,
12912 Constrain_Corresponding_Record (Def_Id, T_Val, Related_Nod));
12913
12914 else
12915 -- If there is no associated record, expansion is disabled and this
12916 -- is a generic context. Create a subtype in any case, so that
12917 -- semantic analysis can proceed.
12918
12919 if No (Def_Id) then
12920 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
12921 end if;
12922
12923 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
12924 end if;
12925 end Constrain_Concurrent;
12926
12927 ------------------------------------
12928 -- Constrain_Corresponding_Record --
12929 ------------------------------------
12930
12931 function Constrain_Corresponding_Record
12932 (Prot_Subt : Entity_Id;
12933 Corr_Rec : Entity_Id;
12934 Related_Nod : Node_Id) return Entity_Id
12935 is
12936 T_Sub : constant Entity_Id :=
12937 Create_Itype (E_Record_Subtype, Related_Nod, Corr_Rec, 'C');
12938
12939 begin
12940 Set_Etype (T_Sub, Corr_Rec);
12941 Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
12942 Set_Is_Constrained (T_Sub, True);
12943 Set_First_Entity (T_Sub, First_Entity (Corr_Rec));
12944 Set_Last_Entity (T_Sub, Last_Entity (Corr_Rec));
12945
12946 if Has_Discriminants (Prot_Subt) then -- False only if errors.
12947 Set_Discriminant_Constraint
12948 (T_Sub, Discriminant_Constraint (Prot_Subt));
12949 Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
12950 Create_Constrained_Components
12951 (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
12952 end if;
12953
12954 Set_Depends_On_Private (T_Sub, Has_Private_Component (T_Sub));
12955
12956 if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
12957 Conditional_Delay (T_Sub, Corr_Rec);
12958
12959 else
12960 -- This is a component subtype: it will be frozen in the context of
12961 -- the enclosing record's init_proc, so that discriminant references
12962 -- are resolved to discriminals. (Note: we used to skip freezing
12963 -- altogether in that case, which caused errors downstream for
12964 -- components of a bit packed array type).
12965
12966 Set_Has_Delayed_Freeze (T_Sub);
12967 end if;
12968
12969 return T_Sub;
12970 end Constrain_Corresponding_Record;
12971
12972 -----------------------
12973 -- Constrain_Decimal --
12974 -----------------------
12975
12976 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id) is
12977 T : constant Entity_Id := Entity (Subtype_Mark (S));
12978 C : constant Node_Id := Constraint (S);
12979 Loc : constant Source_Ptr := Sloc (C);
12980 Range_Expr : Node_Id;
12981 Digits_Expr : Node_Id;
12982 Digits_Val : Uint;
12983 Bound_Val : Ureal;
12984
12985 begin
12986 Set_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
12987
12988 if Nkind (C) = N_Range_Constraint then
12989 Range_Expr := Range_Expression (C);
12990 Digits_Val := Digits_Value (T);
12991
12992 else
12993 pragma Assert (Nkind (C) = N_Digits_Constraint);
12994
12995 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
12996
12997 Digits_Expr := Digits_Expression (C);
12998 Analyze_And_Resolve (Digits_Expr, Any_Integer);
12999
13000 Check_Digits_Expression (Digits_Expr);
13001 Digits_Val := Expr_Value (Digits_Expr);
13002
13003 if Digits_Val > Digits_Value (T) then
13004 Error_Msg_N
13005 ("digits expression is incompatible with subtype", C);
13006 Digits_Val := Digits_Value (T);
13007 end if;
13008
13009 if Present (Range_Constraint (C)) then
13010 Range_Expr := Range_Expression (Range_Constraint (C));
13011 else
13012 Range_Expr := Empty;
13013 end if;
13014 end if;
13015
13016 Set_Etype (Def_Id, Base_Type (T));
13017 Set_Size_Info (Def_Id, (T));
13018 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13019 Set_Delta_Value (Def_Id, Delta_Value (T));
13020 Set_Scale_Value (Def_Id, Scale_Value (T));
13021 Set_Small_Value (Def_Id, Small_Value (T));
13022 Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
13023 Set_Digits_Value (Def_Id, Digits_Val);
13024
13025 -- Manufacture range from given digits value if no range present
13026
13027 if No (Range_Expr) then
13028 Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
13029 Range_Expr :=
13030 Make_Range (Loc,
13031 Low_Bound =>
13032 Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
13033 High_Bound =>
13034 Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
13035 end if;
13036
13037 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
13038 Set_Discrete_RM_Size (Def_Id);
13039
13040 -- Unconditionally delay the freeze, since we cannot set size
13041 -- information in all cases correctly until the freeze point.
13042
13043 Set_Has_Delayed_Freeze (Def_Id);
13044 end Constrain_Decimal;
13045
13046 ----------------------------------
13047 -- Constrain_Discriminated_Type --
13048 ----------------------------------
13049
13050 procedure Constrain_Discriminated_Type
13051 (Def_Id : Entity_Id;
13052 S : Node_Id;
13053 Related_Nod : Node_Id;
13054 For_Access : Boolean := False)
13055 is
13056 E : constant Entity_Id := Entity (Subtype_Mark (S));
13057 T : Entity_Id;
13058 C : Node_Id;
13059 Elist : Elist_Id := New_Elmt_List;
13060
13061 procedure Fixup_Bad_Constraint;
13062 -- This is called after finding a bad constraint, and after having
13063 -- posted an appropriate error message. The mission is to leave the
13064 -- entity T in as reasonable state as possible.
13065
13066 --------------------------
13067 -- Fixup_Bad_Constraint --
13068 --------------------------
13069
13070 procedure Fixup_Bad_Constraint is
13071 begin
13072 -- Set a reasonable Ekind for the entity. For an incomplete type,
13073 -- we can't do much, but for other types, we can set the proper
13074 -- corresponding subtype kind.
13075
13076 if Ekind (T) = E_Incomplete_Type then
13077 Set_Ekind (Def_Id, Ekind (T));
13078 else
13079 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
13080 end if;
13081
13082 -- Set Etype to the known type, to reduce chances of cascaded errors
13083
13084 Set_Etype (Def_Id, E);
13085 Set_Error_Posted (Def_Id);
13086 end Fixup_Bad_Constraint;
13087
13088 -- Start of processing for Constrain_Discriminated_Type
13089
13090 begin
13091 C := Constraint (S);
13092
13093 -- A discriminant constraint is only allowed in a subtype indication,
13094 -- after a subtype mark. This subtype mark must denote either a type
13095 -- with discriminants, or an access type whose designated type is a
13096 -- type with discriminants. A discriminant constraint specifies the
13097 -- values of these discriminants (RM 3.7.2(5)).
13098
13099 T := Base_Type (Entity (Subtype_Mark (S)));
13100
13101 if Is_Access_Type (T) then
13102 T := Designated_Type (T);
13103 end if;
13104
13105 -- Ada 2005 (AI-412): Constrained incomplete subtypes are illegal.
13106 -- Avoid generating an error for access-to-incomplete subtypes.
13107
13108 if Ada_Version >= Ada_2005
13109 and then Ekind (T) = E_Incomplete_Type
13110 and then Nkind (Parent (S)) = N_Subtype_Declaration
13111 and then not Is_Itype (Def_Id)
13112 then
13113 -- A little sanity check, emit an error message if the type
13114 -- has discriminants to begin with. Type T may be a regular
13115 -- incomplete type or imported via a limited with clause.
13116
13117 if Has_Discriminants (T)
13118 or else (From_Limited_With (T)
13119 and then Present (Non_Limited_View (T))
13120 and then Nkind (Parent (Non_Limited_View (T))) =
13121 N_Full_Type_Declaration
13122 and then Present (Discriminant_Specifications
13123 (Parent (Non_Limited_View (T)))))
13124 then
13125 Error_Msg_N
13126 ("(Ada 2005) incomplete subtype may not be constrained", C);
13127 else
13128 Error_Msg_N ("invalid constraint: type has no discriminant", C);
13129 end if;
13130
13131 Fixup_Bad_Constraint;
13132 return;
13133
13134 -- Check that the type has visible discriminants. The type may be
13135 -- a private type with unknown discriminants whose full view has
13136 -- discriminants which are invisible.
13137
13138 elsif not Has_Discriminants (T)
13139 or else
13140 (Has_Unknown_Discriminants (T)
13141 and then Is_Private_Type (T))
13142 then
13143 Error_Msg_N ("invalid constraint: type has no discriminant", C);
13144 Fixup_Bad_Constraint;
13145 return;
13146
13147 elsif Is_Constrained (E)
13148 or else (Ekind (E) = E_Class_Wide_Subtype
13149 and then Present (Discriminant_Constraint (E)))
13150 then
13151 Error_Msg_N ("type is already constrained", Subtype_Mark (S));
13152 Fixup_Bad_Constraint;
13153 return;
13154 end if;
13155
13156 -- T may be an unconstrained subtype (e.g. a generic actual).
13157 -- Constraint applies to the base type.
13158
13159 T := Base_Type (T);
13160
13161 Elist := Build_Discriminant_Constraints (T, S);
13162
13163 -- If the list returned was empty we had an error in building the
13164 -- discriminant constraint. We have also already signalled an error
13165 -- in the incomplete type case
13166
13167 if Is_Empty_Elmt_List (Elist) then
13168 Fixup_Bad_Constraint;
13169 return;
13170 end if;
13171
13172 Build_Discriminated_Subtype (T, Def_Id, Elist, Related_Nod, For_Access);
13173 end Constrain_Discriminated_Type;
13174
13175 ---------------------------
13176 -- Constrain_Enumeration --
13177 ---------------------------
13178
13179 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id) is
13180 T : constant Entity_Id := Entity (Subtype_Mark (S));
13181 C : constant Node_Id := Constraint (S);
13182
13183 begin
13184 Set_Ekind (Def_Id, E_Enumeration_Subtype);
13185
13186 Set_First_Literal (Def_Id, First_Literal (Base_Type (T)));
13187
13188 Set_Etype (Def_Id, Base_Type (T));
13189 Set_Size_Info (Def_Id, (T));
13190 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13191 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
13192
13193 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13194
13195 Set_Discrete_RM_Size (Def_Id);
13196 end Constrain_Enumeration;
13197
13198 ----------------------
13199 -- Constrain_Float --
13200 ----------------------
13201
13202 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id) is
13203 T : constant Entity_Id := Entity (Subtype_Mark (S));
13204 C : Node_Id;
13205 D : Node_Id;
13206 Rais : Node_Id;
13207
13208 begin
13209 Set_Ekind (Def_Id, E_Floating_Point_Subtype);
13210
13211 Set_Etype (Def_Id, Base_Type (T));
13212 Set_Size_Info (Def_Id, (T));
13213 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13214
13215 -- Process the constraint
13216
13217 C := Constraint (S);
13218
13219 -- Digits constraint present
13220
13221 if Nkind (C) = N_Digits_Constraint then
13222
13223 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
13224 Check_Restriction (No_Obsolescent_Features, C);
13225
13226 if Warn_On_Obsolescent_Feature then
13227 Error_Msg_N
13228 ("subtype digits constraint is an " &
13229 "obsolescent feature (RM J.3(8))?j?", C);
13230 end if;
13231
13232 D := Digits_Expression (C);
13233 Analyze_And_Resolve (D, Any_Integer);
13234 Check_Digits_Expression (D);
13235 Set_Digits_Value (Def_Id, Expr_Value (D));
13236
13237 -- Check that digits value is in range. Obviously we can do this
13238 -- at compile time, but it is strictly a runtime check, and of
13239 -- course there is an ACVC test that checks this.
13240
13241 if Digits_Value (Def_Id) > Digits_Value (T) then
13242 Error_Msg_Uint_1 := Digits_Value (T);
13243 Error_Msg_N ("??digits value is too large, maximum is ^", D);
13244 Rais :=
13245 Make_Raise_Constraint_Error (Sloc (D),
13246 Reason => CE_Range_Check_Failed);
13247 Insert_Action (Declaration_Node (Def_Id), Rais);
13248 end if;
13249
13250 C := Range_Constraint (C);
13251
13252 -- No digits constraint present
13253
13254 else
13255 Set_Digits_Value (Def_Id, Digits_Value (T));
13256 end if;
13257
13258 -- Range constraint present
13259
13260 if Nkind (C) = N_Range_Constraint then
13261 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13262
13263 -- No range constraint present
13264
13265 else
13266 pragma Assert (No (C));
13267 Set_Scalar_Range (Def_Id, Scalar_Range (T));
13268 end if;
13269
13270 Set_Is_Constrained (Def_Id);
13271 end Constrain_Float;
13272
13273 ---------------------
13274 -- Constrain_Index --
13275 ---------------------
13276
13277 procedure Constrain_Index
13278 (Index : Node_Id;
13279 S : Node_Id;
13280 Related_Nod : Node_Id;
13281 Related_Id : Entity_Id;
13282 Suffix : Character;
13283 Suffix_Index : Nat)
13284 is
13285 Def_Id : Entity_Id;
13286 R : Node_Id := Empty;
13287 T : constant Entity_Id := Etype (Index);
13288
13289 begin
13290 Def_Id :=
13291 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
13292 Set_Etype (Def_Id, Base_Type (T));
13293
13294 if Nkind (S) = N_Range
13295 or else
13296 (Nkind (S) = N_Attribute_Reference
13297 and then Attribute_Name (S) = Name_Range)
13298 then
13299 -- A Range attribute will be transformed into N_Range by Resolve
13300
13301 Analyze (S);
13302 Set_Etype (S, T);
13303 R := S;
13304
13305 Process_Range_Expr_In_Decl (R, T);
13306
13307 if not Error_Posted (S)
13308 and then
13309 (Nkind (S) /= N_Range
13310 or else not Covers (T, (Etype (Low_Bound (S))))
13311 or else not Covers (T, (Etype (High_Bound (S)))))
13312 then
13313 if Base_Type (T) /= Any_Type
13314 and then Etype (Low_Bound (S)) /= Any_Type
13315 and then Etype (High_Bound (S)) /= Any_Type
13316 then
13317 Error_Msg_N ("range expected", S);
13318 end if;
13319 end if;
13320
13321 elsif Nkind (S) = N_Subtype_Indication then
13322
13323 -- The parser has verified that this is a discrete indication
13324
13325 Resolve_Discrete_Subtype_Indication (S, T);
13326 Bad_Predicated_Subtype_Use
13327 ("subtype& has predicate, not allowed in index constraint",
13328 S, Entity (Subtype_Mark (S)));
13329
13330 R := Range_Expression (Constraint (S));
13331
13332 -- Capture values of bounds and generate temporaries for them if
13333 -- needed, since checks may cause duplication of the expressions
13334 -- which must not be reevaluated.
13335
13336 -- The forced evaluation removes side effects from expressions, which
13337 -- should occur also in GNATprove mode. Otherwise, we end up with
13338 -- unexpected insertions of actions at places where this is not
13339 -- supposed to occur, e.g. on default parameters of a call.
13340
13341 if Expander_Active or GNATprove_Mode then
13342 Force_Evaluation
13343 (Low_Bound (R), Related_Id => Def_Id, Is_Low_Bound => True);
13344 Force_Evaluation
13345 (High_Bound (R), Related_Id => Def_Id, Is_High_Bound => True);
13346 end if;
13347
13348 elsif Nkind (S) = N_Discriminant_Association then
13349
13350 -- Syntactically valid in subtype indication
13351
13352 Error_Msg_N ("invalid index constraint", S);
13353 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
13354 return;
13355
13356 -- Subtype_Mark case, no anonymous subtypes to construct
13357
13358 else
13359 Analyze (S);
13360
13361 if Is_Entity_Name (S) then
13362 if not Is_Type (Entity (S)) then
13363 Error_Msg_N ("expect subtype mark for index constraint", S);
13364
13365 elsif Base_Type (Entity (S)) /= Base_Type (T) then
13366 Wrong_Type (S, Base_Type (T));
13367
13368 -- Check error of subtype with predicate in index constraint
13369
13370 else
13371 Bad_Predicated_Subtype_Use
13372 ("subtype& has predicate, not allowed in index constraint",
13373 S, Entity (S));
13374 end if;
13375
13376 return;
13377
13378 else
13379 Error_Msg_N ("invalid index constraint", S);
13380 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
13381 return;
13382 end if;
13383 end if;
13384
13385 -- Complete construction of the Itype
13386
13387 if Is_Modular_Integer_Type (T) then
13388 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
13389
13390 elsif Is_Integer_Type (T) then
13391 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
13392
13393 else
13394 Set_Ekind (Def_Id, E_Enumeration_Subtype);
13395 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
13396 Set_First_Literal (Def_Id, First_Literal (T));
13397 end if;
13398
13399 Set_Size_Info (Def_Id, (T));
13400 Set_RM_Size (Def_Id, RM_Size (T));
13401 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13402
13403 Set_Scalar_Range (Def_Id, R);
13404
13405 Set_Etype (S, Def_Id);
13406 Set_Discrete_RM_Size (Def_Id);
13407 end Constrain_Index;
13408
13409 -----------------------
13410 -- Constrain_Integer --
13411 -----------------------
13412
13413 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id) is
13414 T : constant Entity_Id := Entity (Subtype_Mark (S));
13415 C : constant Node_Id := Constraint (S);
13416
13417 begin
13418 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13419
13420 if Is_Modular_Integer_Type (T) then
13421 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
13422 else
13423 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
13424 end if;
13425
13426 Set_Etype (Def_Id, Base_Type (T));
13427 Set_Size_Info (Def_Id, (T));
13428 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13429 Set_Discrete_RM_Size (Def_Id);
13430 end Constrain_Integer;
13431
13432 ------------------------------
13433 -- Constrain_Ordinary_Fixed --
13434 ------------------------------
13435
13436 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id) is
13437 T : constant Entity_Id := Entity (Subtype_Mark (S));
13438 C : Node_Id;
13439 D : Node_Id;
13440 Rais : Node_Id;
13441
13442 begin
13443 Set_Ekind (Def_Id, E_Ordinary_Fixed_Point_Subtype);
13444 Set_Etype (Def_Id, Base_Type (T));
13445 Set_Size_Info (Def_Id, (T));
13446 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13447 Set_Small_Value (Def_Id, Small_Value (T));
13448
13449 -- Process the constraint
13450
13451 C := Constraint (S);
13452
13453 -- Delta constraint present
13454
13455 if Nkind (C) = N_Delta_Constraint then
13456
13457 Check_SPARK_05_Restriction ("delta constraint is not allowed", S);
13458 Check_Restriction (No_Obsolescent_Features, C);
13459
13460 if Warn_On_Obsolescent_Feature then
13461 Error_Msg_S
13462 ("subtype delta constraint is an " &
13463 "obsolescent feature (RM J.3(7))?j?");
13464 end if;
13465
13466 D := Delta_Expression (C);
13467 Analyze_And_Resolve (D, Any_Real);
13468 Check_Delta_Expression (D);
13469 Set_Delta_Value (Def_Id, Expr_Value_R (D));
13470
13471 -- Check that delta value is in range. Obviously we can do this
13472 -- at compile time, but it is strictly a runtime check, and of
13473 -- course there is an ACVC test that checks this.
13474
13475 if Delta_Value (Def_Id) < Delta_Value (T) then
13476 Error_Msg_N ("??delta value is too small", D);
13477 Rais :=
13478 Make_Raise_Constraint_Error (Sloc (D),
13479 Reason => CE_Range_Check_Failed);
13480 Insert_Action (Declaration_Node (Def_Id), Rais);
13481 end if;
13482
13483 C := Range_Constraint (C);
13484
13485 -- No delta constraint present
13486
13487 else
13488 Set_Delta_Value (Def_Id, Delta_Value (T));
13489 end if;
13490
13491 -- Range constraint present
13492
13493 if Nkind (C) = N_Range_Constraint then
13494 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13495
13496 -- No range constraint present
13497
13498 else
13499 pragma Assert (No (C));
13500 Set_Scalar_Range (Def_Id, Scalar_Range (T));
13501 end if;
13502
13503 Set_Discrete_RM_Size (Def_Id);
13504
13505 -- Unconditionally delay the freeze, since we cannot set size
13506 -- information in all cases correctly until the freeze point.
13507
13508 Set_Has_Delayed_Freeze (Def_Id);
13509 end Constrain_Ordinary_Fixed;
13510
13511 -----------------------
13512 -- Contain_Interface --
13513 -----------------------
13514
13515 function Contain_Interface
13516 (Iface : Entity_Id;
13517 Ifaces : Elist_Id) return Boolean
13518 is
13519 Iface_Elmt : Elmt_Id;
13520
13521 begin
13522 if Present (Ifaces) then
13523 Iface_Elmt := First_Elmt (Ifaces);
13524 while Present (Iface_Elmt) loop
13525 if Node (Iface_Elmt) = Iface then
13526 return True;
13527 end if;
13528
13529 Next_Elmt (Iface_Elmt);
13530 end loop;
13531 end if;
13532
13533 return False;
13534 end Contain_Interface;
13535
13536 ---------------------------
13537 -- Convert_Scalar_Bounds --
13538 ---------------------------
13539
13540 procedure Convert_Scalar_Bounds
13541 (N : Node_Id;
13542 Parent_Type : Entity_Id;
13543 Derived_Type : Entity_Id;
13544 Loc : Source_Ptr)
13545 is
13546 Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
13547
13548 Lo : Node_Id;
13549 Hi : Node_Id;
13550 Rng : Node_Id;
13551
13552 begin
13553 -- Defend against previous errors
13554
13555 if No (Scalar_Range (Derived_Type)) then
13556 Check_Error_Detected;
13557 return;
13558 end if;
13559
13560 Lo := Build_Scalar_Bound
13561 (Type_Low_Bound (Derived_Type),
13562 Parent_Type, Implicit_Base);
13563
13564 Hi := Build_Scalar_Bound
13565 (Type_High_Bound (Derived_Type),
13566 Parent_Type, Implicit_Base);
13567
13568 Rng :=
13569 Make_Range (Loc,
13570 Low_Bound => Lo,
13571 High_Bound => Hi);
13572
13573 Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
13574
13575 Set_Parent (Rng, N);
13576 Set_Scalar_Range (Derived_Type, Rng);
13577
13578 -- Analyze the bounds
13579
13580 Analyze_And_Resolve (Lo, Implicit_Base);
13581 Analyze_And_Resolve (Hi, Implicit_Base);
13582
13583 -- Analyze the range itself, except that we do not analyze it if
13584 -- the bounds are real literals, and we have a fixed-point type.
13585 -- The reason for this is that we delay setting the bounds in this
13586 -- case till we know the final Small and Size values (see circuit
13587 -- in Freeze.Freeze_Fixed_Point_Type for further details).
13588
13589 if Is_Fixed_Point_Type (Parent_Type)
13590 and then Nkind (Lo) = N_Real_Literal
13591 and then Nkind (Hi) = N_Real_Literal
13592 then
13593 return;
13594
13595 -- Here we do the analysis of the range
13596
13597 -- Note: we do this manually, since if we do a normal Analyze and
13598 -- Resolve call, there are problems with the conversions used for
13599 -- the derived type range.
13600
13601 else
13602 Set_Etype (Rng, Implicit_Base);
13603 Set_Analyzed (Rng, True);
13604 end if;
13605 end Convert_Scalar_Bounds;
13606
13607 -------------------
13608 -- Copy_And_Swap --
13609 -------------------
13610
13611 procedure Copy_And_Swap (Priv, Full : Entity_Id) is
13612 begin
13613 -- Initialize new full declaration entity by copying the pertinent
13614 -- fields of the corresponding private declaration entity.
13615
13616 -- We temporarily set Ekind to a value appropriate for a type to
13617 -- avoid assert failures in Einfo from checking for setting type
13618 -- attributes on something that is not a type. Ekind (Priv) is an
13619 -- appropriate choice, since it allowed the attributes to be set
13620 -- in the first place. This Ekind value will be modified later.
13621
13622 Set_Ekind (Full, Ekind (Priv));
13623
13624 -- Also set Etype temporarily to Any_Type, again, in the absence
13625 -- of errors, it will be properly reset, and if there are errors,
13626 -- then we want a value of Any_Type to remain.
13627
13628 Set_Etype (Full, Any_Type);
13629
13630 -- Now start copying attributes
13631
13632 Set_Has_Discriminants (Full, Has_Discriminants (Priv));
13633
13634 if Has_Discriminants (Full) then
13635 Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
13636 Set_Stored_Constraint (Full, Stored_Constraint (Priv));
13637 end if;
13638
13639 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
13640 Set_Homonym (Full, Homonym (Priv));
13641 Set_Is_Immediately_Visible (Full, Is_Immediately_Visible (Priv));
13642 Set_Is_Public (Full, Is_Public (Priv));
13643 Set_Is_Pure (Full, Is_Pure (Priv));
13644 Set_Is_Tagged_Type (Full, Is_Tagged_Type (Priv));
13645 Set_Has_Pragma_Unmodified (Full, Has_Pragma_Unmodified (Priv));
13646 Set_Has_Pragma_Unreferenced (Full, Has_Pragma_Unreferenced (Priv));
13647 Set_Has_Pragma_Unreferenced_Objects
13648 (Full, Has_Pragma_Unreferenced_Objects
13649 (Priv));
13650
13651 Conditional_Delay (Full, Priv);
13652
13653 if Is_Tagged_Type (Full) then
13654 Set_Direct_Primitive_Operations
13655 (Full, Direct_Primitive_Operations (Priv));
13656 Set_No_Tagged_Streams_Pragma
13657 (Full, No_Tagged_Streams_Pragma (Priv));
13658
13659 if Is_Base_Type (Priv) then
13660 Set_Class_Wide_Type (Full, Class_Wide_Type (Priv));
13661 end if;
13662 end if;
13663
13664 Set_Is_Volatile (Full, Is_Volatile (Priv));
13665 Set_Treat_As_Volatile (Full, Treat_As_Volatile (Priv));
13666 Set_Scope (Full, Scope (Priv));
13667 Set_Next_Entity (Full, Next_Entity (Priv));
13668 Set_First_Entity (Full, First_Entity (Priv));
13669 Set_Last_Entity (Full, Last_Entity (Priv));
13670
13671 -- If access types have been recorded for later handling, keep them in
13672 -- the full view so that they get handled when the full view freeze
13673 -- node is expanded.
13674
13675 if Present (Freeze_Node (Priv))
13676 and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
13677 then
13678 Ensure_Freeze_Node (Full);
13679 Set_Access_Types_To_Process
13680 (Freeze_Node (Full),
13681 Access_Types_To_Process (Freeze_Node (Priv)));
13682 end if;
13683
13684 -- Swap the two entities. Now Private is the full type entity and Full
13685 -- is the private one. They will be swapped back at the end of the
13686 -- private part. This swapping ensures that the entity that is visible
13687 -- in the private part is the full declaration.
13688
13689 Exchange_Entities (Priv, Full);
13690 Append_Entity (Full, Scope (Full));
13691 end Copy_And_Swap;
13692
13693 -------------------------------------
13694 -- Copy_Array_Base_Type_Attributes --
13695 -------------------------------------
13696
13697 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
13698 begin
13699 Set_Component_Alignment (T1, Component_Alignment (T2));
13700 Set_Component_Type (T1, Component_Type (T2));
13701 Set_Component_Size (T1, Component_Size (T2));
13702 Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
13703 Set_Has_Non_Standard_Rep (T1, Has_Non_Standard_Rep (T2));
13704 Set_Has_Protected (T1, Has_Protected (T2));
13705 Set_Has_Task (T1, Has_Task (T2));
13706 Set_Is_Packed (T1, Is_Packed (T2));
13707 Set_Has_Aliased_Components (T1, Has_Aliased_Components (T2));
13708 Set_Has_Atomic_Components (T1, Has_Atomic_Components (T2));
13709 Set_Has_Volatile_Components (T1, Has_Volatile_Components (T2));
13710 end Copy_Array_Base_Type_Attributes;
13711
13712 -----------------------------------
13713 -- Copy_Array_Subtype_Attributes --
13714 -----------------------------------
13715
13716 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
13717 begin
13718 Set_Size_Info (T1, T2);
13719
13720 Set_First_Index (T1, First_Index (T2));
13721 Set_Is_Aliased (T1, Is_Aliased (T2));
13722 Set_Is_Volatile (T1, Is_Volatile (T2));
13723 Set_Treat_As_Volatile (T1, Treat_As_Volatile (T2));
13724 Set_Is_Constrained (T1, Is_Constrained (T2));
13725 Set_Depends_On_Private (T1, Has_Private_Component (T2));
13726 Inherit_Rep_Item_Chain (T1, T2);
13727 Set_Convention (T1, Convention (T2));
13728 Set_Is_Limited_Composite (T1, Is_Limited_Composite (T2));
13729 Set_Is_Private_Composite (T1, Is_Private_Composite (T2));
13730 Set_Packed_Array_Impl_Type (T1, Packed_Array_Impl_Type (T2));
13731 end Copy_Array_Subtype_Attributes;
13732
13733 -----------------------------------
13734 -- Create_Constrained_Components --
13735 -----------------------------------
13736
13737 procedure Create_Constrained_Components
13738 (Subt : Entity_Id;
13739 Decl_Node : Node_Id;
13740 Typ : Entity_Id;
13741 Constraints : Elist_Id)
13742 is
13743 Loc : constant Source_Ptr := Sloc (Subt);
13744 Comp_List : constant Elist_Id := New_Elmt_List;
13745 Parent_Type : constant Entity_Id := Etype (Typ);
13746 Assoc_List : constant List_Id := New_List;
13747 Discr_Val : Elmt_Id;
13748 Errors : Boolean;
13749 New_C : Entity_Id;
13750 Old_C : Entity_Id;
13751 Is_Static : Boolean := True;
13752
13753 procedure Collect_Fixed_Components (Typ : Entity_Id);
13754 -- Collect parent type components that do not appear in a variant part
13755
13756 procedure Create_All_Components;
13757 -- Iterate over Comp_List to create the components of the subtype
13758
13759 function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
13760 -- Creates a new component from Old_Compon, copying all the fields from
13761 -- it, including its Etype, inserts the new component in the Subt entity
13762 -- chain and returns the new component.
13763
13764 function Is_Variant_Record (T : Entity_Id) return Boolean;
13765 -- If true, and discriminants are static, collect only components from
13766 -- variants selected by discriminant values.
13767
13768 ------------------------------
13769 -- Collect_Fixed_Components --
13770 ------------------------------
13771
13772 procedure Collect_Fixed_Components (Typ : Entity_Id) is
13773 begin
13774 -- Build association list for discriminants, and find components of the
13775 -- variant part selected by the values of the discriminants.
13776
13777 Old_C := First_Discriminant (Typ);
13778 Discr_Val := First_Elmt (Constraints);
13779 while Present (Old_C) loop
13780 Append_To (Assoc_List,
13781 Make_Component_Association (Loc,
13782 Choices => New_List (New_Occurrence_Of (Old_C, Loc)),
13783 Expression => New_Copy (Node (Discr_Val))));
13784
13785 Next_Elmt (Discr_Val);
13786 Next_Discriminant (Old_C);
13787 end loop;
13788
13789 -- The tag and the possible parent component are unconditionally in
13790 -- the subtype.
13791
13792 if Is_Tagged_Type (Typ) or else Has_Controlled_Component (Typ) then
13793 Old_C := First_Component (Typ);
13794 while Present (Old_C) loop
13795 if Nam_In (Chars (Old_C), Name_uTag, Name_uParent) then
13796 Append_Elmt (Old_C, Comp_List);
13797 end if;
13798
13799 Next_Component (Old_C);
13800 end loop;
13801 end if;
13802 end Collect_Fixed_Components;
13803
13804 ---------------------------
13805 -- Create_All_Components --
13806 ---------------------------
13807
13808 procedure Create_All_Components is
13809 Comp : Elmt_Id;
13810
13811 begin
13812 Comp := First_Elmt (Comp_List);
13813 while Present (Comp) loop
13814 Old_C := Node (Comp);
13815 New_C := Create_Component (Old_C);
13816
13817 Set_Etype
13818 (New_C,
13819 Constrain_Component_Type
13820 (Old_C, Subt, Decl_Node, Typ, Constraints));
13821 Set_Is_Public (New_C, Is_Public (Subt));
13822
13823 Next_Elmt (Comp);
13824 end loop;
13825 end Create_All_Components;
13826
13827 ----------------------
13828 -- Create_Component --
13829 ----------------------
13830
13831 function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
13832 New_Compon : constant Entity_Id := New_Copy (Old_Compon);
13833
13834 begin
13835 if Ekind (Old_Compon) = E_Discriminant
13836 and then Is_Completely_Hidden (Old_Compon)
13837 then
13838 -- This is a shadow discriminant created for a discriminant of
13839 -- the parent type, which needs to be present in the subtype.
13840 -- Give the shadow discriminant an internal name that cannot
13841 -- conflict with that of visible components.
13842
13843 Set_Chars (New_Compon, New_Internal_Name ('C'));
13844 end if;
13845
13846 -- Set the parent so we have a proper link for freezing etc. This is
13847 -- not a real parent pointer, since of course our parent does not own
13848 -- up to us and reference us, we are an illegitimate child of the
13849 -- original parent.
13850
13851 Set_Parent (New_Compon, Parent (Old_Compon));
13852
13853 -- If the old component's Esize was already determined and is a
13854 -- static value, then the new component simply inherits it. Otherwise
13855 -- the old component's size may require run-time determination, but
13856 -- the new component's size still might be statically determinable
13857 -- (if, for example it has a static constraint). In that case we want
13858 -- Layout_Type to recompute the component's size, so we reset its
13859 -- size and positional fields.
13860
13861 if Frontend_Layout_On_Target
13862 and then not Known_Static_Esize (Old_Compon)
13863 then
13864 Set_Esize (New_Compon, Uint_0);
13865 Init_Normalized_First_Bit (New_Compon);
13866 Init_Normalized_Position (New_Compon);
13867 Init_Normalized_Position_Max (New_Compon);
13868 end if;
13869
13870 -- We do not want this node marked as Comes_From_Source, since
13871 -- otherwise it would get first class status and a separate cross-
13872 -- reference line would be generated. Illegitimate children do not
13873 -- rate such recognition.
13874
13875 Set_Comes_From_Source (New_Compon, False);
13876
13877 -- But it is a real entity, and a birth certificate must be properly
13878 -- registered by entering it into the entity list.
13879
13880 Enter_Name (New_Compon);
13881
13882 return New_Compon;
13883 end Create_Component;
13884
13885 -----------------------
13886 -- Is_Variant_Record --
13887 -----------------------
13888
13889 function Is_Variant_Record (T : Entity_Id) return Boolean is
13890 begin
13891 return Nkind (Parent (T)) = N_Full_Type_Declaration
13892 and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
13893 and then Present (Component_List (Type_Definition (Parent (T))))
13894 and then
13895 Present
13896 (Variant_Part (Component_List (Type_Definition (Parent (T)))));
13897 end Is_Variant_Record;
13898
13899 -- Start of processing for Create_Constrained_Components
13900
13901 begin
13902 pragma Assert (Subt /= Base_Type (Subt));
13903 pragma Assert (Typ = Base_Type (Typ));
13904
13905 Set_First_Entity (Subt, Empty);
13906 Set_Last_Entity (Subt, Empty);
13907
13908 -- Check whether constraint is fully static, in which case we can
13909 -- optimize the list of components.
13910
13911 Discr_Val := First_Elmt (Constraints);
13912 while Present (Discr_Val) loop
13913 if not Is_OK_Static_Expression (Node (Discr_Val)) then
13914 Is_Static := False;
13915 exit;
13916 end if;
13917
13918 Next_Elmt (Discr_Val);
13919 end loop;
13920
13921 Set_Has_Static_Discriminants (Subt, Is_Static);
13922
13923 Push_Scope (Subt);
13924
13925 -- Inherit the discriminants of the parent type
13926
13927 Add_Discriminants : declare
13928 Num_Disc : Int;
13929 Num_Gird : Int;
13930
13931 begin
13932 Num_Disc := 0;
13933 Old_C := First_Discriminant (Typ);
13934
13935 while Present (Old_C) loop
13936 Num_Disc := Num_Disc + 1;
13937 New_C := Create_Component (Old_C);
13938 Set_Is_Public (New_C, Is_Public (Subt));
13939 Next_Discriminant (Old_C);
13940 end loop;
13941
13942 -- For an untagged derived subtype, the number of discriminants may
13943 -- be smaller than the number of inherited discriminants, because
13944 -- several of them may be renamed by a single new discriminant or
13945 -- constrained. In this case, add the hidden discriminants back into
13946 -- the subtype, because they need to be present if the optimizer of
13947 -- the GCC 4.x back-end decides to break apart assignments between
13948 -- objects using the parent view into member-wise assignments.
13949
13950 Num_Gird := 0;
13951
13952 if Is_Derived_Type (Typ)
13953 and then not Is_Tagged_Type (Typ)
13954 then
13955 Old_C := First_Stored_Discriminant (Typ);
13956
13957 while Present (Old_C) loop
13958 Num_Gird := Num_Gird + 1;
13959 Next_Stored_Discriminant (Old_C);
13960 end loop;
13961 end if;
13962
13963 if Num_Gird > Num_Disc then
13964
13965 -- Find out multiple uses of new discriminants, and add hidden
13966 -- components for the extra renamed discriminants. We recognize
13967 -- multiple uses through the Corresponding_Discriminant of a
13968 -- new discriminant: if it constrains several old discriminants,
13969 -- this field points to the last one in the parent type. The
13970 -- stored discriminants of the derived type have the same name
13971 -- as those of the parent.
13972
13973 declare
13974 Constr : Elmt_Id;
13975 New_Discr : Entity_Id;
13976 Old_Discr : Entity_Id;
13977
13978 begin
13979 Constr := First_Elmt (Stored_Constraint (Typ));
13980 Old_Discr := First_Stored_Discriminant (Typ);
13981 while Present (Constr) loop
13982 if Is_Entity_Name (Node (Constr))
13983 and then Ekind (Entity (Node (Constr))) = E_Discriminant
13984 then
13985 New_Discr := Entity (Node (Constr));
13986
13987 if Chars (Corresponding_Discriminant (New_Discr)) /=
13988 Chars (Old_Discr)
13989 then
13990 -- The new discriminant has been used to rename a
13991 -- subsequent old discriminant. Introduce a shadow
13992 -- component for the current old discriminant.
13993
13994 New_C := Create_Component (Old_Discr);
13995 Set_Original_Record_Component (New_C, Old_Discr);
13996 end if;
13997
13998 else
13999 -- The constraint has eliminated the old discriminant.
14000 -- Introduce a shadow component.
14001
14002 New_C := Create_Component (Old_Discr);
14003 Set_Original_Record_Component (New_C, Old_Discr);
14004 end if;
14005
14006 Next_Elmt (Constr);
14007 Next_Stored_Discriminant (Old_Discr);
14008 end loop;
14009 end;
14010 end if;
14011 end Add_Discriminants;
14012
14013 if Is_Static
14014 and then Is_Variant_Record (Typ)
14015 then
14016 Collect_Fixed_Components (Typ);
14017
14018 Gather_Components (
14019 Typ,
14020 Component_List (Type_Definition (Parent (Typ))),
14021 Governed_By => Assoc_List,
14022 Into => Comp_List,
14023 Report_Errors => Errors);
14024 pragma Assert (not Errors);
14025
14026 Create_All_Components;
14027
14028 -- If the subtype declaration is created for a tagged type derivation
14029 -- with constraints, we retrieve the record definition of the parent
14030 -- type to select the components of the proper variant.
14031
14032 elsif Is_Static
14033 and then Is_Tagged_Type (Typ)
14034 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
14035 and then
14036 Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
14037 and then Is_Variant_Record (Parent_Type)
14038 then
14039 Collect_Fixed_Components (Typ);
14040
14041 Gather_Components
14042 (Typ,
14043 Component_List (Type_Definition (Parent (Parent_Type))),
14044 Governed_By => Assoc_List,
14045 Into => Comp_List,
14046 Report_Errors => Errors);
14047
14048 -- Note: previously there was a check at this point that no errors
14049 -- were detected. As a consequence of AI05-220 there may be an error
14050 -- if an inherited discriminant that controls a variant has a non-
14051 -- static constraint.
14052
14053 -- If the tagged derivation has a type extension, collect all the
14054 -- new components therein.
14055
14056 if Present (Record_Extension_Part (Type_Definition (Parent (Typ))))
14057 then
14058 Old_C := First_Component (Typ);
14059 while Present (Old_C) loop
14060 if Original_Record_Component (Old_C) = Old_C
14061 and then Chars (Old_C) /= Name_uTag
14062 and then Chars (Old_C) /= Name_uParent
14063 then
14064 Append_Elmt (Old_C, Comp_List);
14065 end if;
14066
14067 Next_Component (Old_C);
14068 end loop;
14069 end if;
14070
14071 Create_All_Components;
14072
14073 else
14074 -- If discriminants are not static, or if this is a multi-level type
14075 -- extension, we have to include all components of the parent type.
14076
14077 Old_C := First_Component (Typ);
14078 while Present (Old_C) loop
14079 New_C := Create_Component (Old_C);
14080
14081 Set_Etype
14082 (New_C,
14083 Constrain_Component_Type
14084 (Old_C, Subt, Decl_Node, Typ, Constraints));
14085 Set_Is_Public (New_C, Is_Public (Subt));
14086
14087 Next_Component (Old_C);
14088 end loop;
14089 end if;
14090
14091 End_Scope;
14092 end Create_Constrained_Components;
14093
14094 ------------------------------------------
14095 -- Decimal_Fixed_Point_Type_Declaration --
14096 ------------------------------------------
14097
14098 procedure Decimal_Fixed_Point_Type_Declaration
14099 (T : Entity_Id;
14100 Def : Node_Id)
14101 is
14102 Loc : constant Source_Ptr := Sloc (Def);
14103 Digs_Expr : constant Node_Id := Digits_Expression (Def);
14104 Delta_Expr : constant Node_Id := Delta_Expression (Def);
14105 Implicit_Base : Entity_Id;
14106 Digs_Val : Uint;
14107 Delta_Val : Ureal;
14108 Scale_Val : Uint;
14109 Bound_Val : Ureal;
14110
14111 begin
14112 Check_SPARK_05_Restriction
14113 ("decimal fixed point type is not allowed", Def);
14114 Check_Restriction (No_Fixed_Point, Def);
14115
14116 -- Create implicit base type
14117
14118 Implicit_Base :=
14119 Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
14120 Set_Etype (Implicit_Base, Implicit_Base);
14121
14122 -- Analyze and process delta expression
14123
14124 Analyze_And_Resolve (Delta_Expr, Universal_Real);
14125
14126 Check_Delta_Expression (Delta_Expr);
14127 Delta_Val := Expr_Value_R (Delta_Expr);
14128
14129 -- Check delta is power of 10, and determine scale value from it
14130
14131 declare
14132 Val : Ureal;
14133
14134 begin
14135 Scale_Val := Uint_0;
14136 Val := Delta_Val;
14137
14138 if Val < Ureal_1 then
14139 while Val < Ureal_1 loop
14140 Val := Val * Ureal_10;
14141 Scale_Val := Scale_Val + 1;
14142 end loop;
14143
14144 if Scale_Val > 18 then
14145 Error_Msg_N ("scale exceeds maximum value of 18", Def);
14146 Scale_Val := UI_From_Int (+18);
14147 end if;
14148
14149 else
14150 while Val > Ureal_1 loop
14151 Val := Val / Ureal_10;
14152 Scale_Val := Scale_Val - 1;
14153 end loop;
14154
14155 if Scale_Val < -18 then
14156 Error_Msg_N ("scale is less than minimum value of -18", Def);
14157 Scale_Val := UI_From_Int (-18);
14158 end if;
14159 end if;
14160
14161 if Val /= Ureal_1 then
14162 Error_Msg_N ("delta expression must be a power of 10", Def);
14163 Delta_Val := Ureal_10 ** (-Scale_Val);
14164 end if;
14165 end;
14166
14167 -- Set delta, scale and small (small = delta for decimal type)
14168
14169 Set_Delta_Value (Implicit_Base, Delta_Val);
14170 Set_Scale_Value (Implicit_Base, Scale_Val);
14171 Set_Small_Value (Implicit_Base, Delta_Val);
14172
14173 -- Analyze and process digits expression
14174
14175 Analyze_And_Resolve (Digs_Expr, Any_Integer);
14176 Check_Digits_Expression (Digs_Expr);
14177 Digs_Val := Expr_Value (Digs_Expr);
14178
14179 if Digs_Val > 18 then
14180 Digs_Val := UI_From_Int (+18);
14181 Error_Msg_N ("digits value out of range, maximum is 18", Digs_Expr);
14182 end if;
14183
14184 Set_Digits_Value (Implicit_Base, Digs_Val);
14185 Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
14186
14187 -- Set range of base type from digits value for now. This will be
14188 -- expanded to represent the true underlying base range by Freeze.
14189
14190 Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
14191
14192 -- Note: We leave size as zero for now, size will be set at freeze
14193 -- time. We have to do this for ordinary fixed-point, because the size
14194 -- depends on the specified small, and we might as well do the same for
14195 -- decimal fixed-point.
14196
14197 pragma Assert (Esize (Implicit_Base) = Uint_0);
14198
14199 -- If there are bounds given in the declaration use them as the
14200 -- bounds of the first named subtype.
14201
14202 if Present (Real_Range_Specification (Def)) then
14203 declare
14204 RRS : constant Node_Id := Real_Range_Specification (Def);
14205 Low : constant Node_Id := Low_Bound (RRS);
14206 High : constant Node_Id := High_Bound (RRS);
14207 Low_Val : Ureal;
14208 High_Val : Ureal;
14209
14210 begin
14211 Analyze_And_Resolve (Low, Any_Real);
14212 Analyze_And_Resolve (High, Any_Real);
14213 Check_Real_Bound (Low);
14214 Check_Real_Bound (High);
14215 Low_Val := Expr_Value_R (Low);
14216 High_Val := Expr_Value_R (High);
14217
14218 if Low_Val < (-Bound_Val) then
14219 Error_Msg_N
14220 ("range low bound too small for digits value", Low);
14221 Low_Val := -Bound_Val;
14222 end if;
14223
14224 if High_Val > Bound_Val then
14225 Error_Msg_N
14226 ("range high bound too large for digits value", High);
14227 High_Val := Bound_Val;
14228 end if;
14229
14230 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
14231 end;
14232
14233 -- If no explicit range, use range that corresponds to given
14234 -- digits value. This will end up as the final range for the
14235 -- first subtype.
14236
14237 else
14238 Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
14239 end if;
14240
14241 -- Complete entity for first subtype. The inheritance of the rep item
14242 -- chain ensures that SPARK-related pragmas are not clobbered when the
14243 -- decimal fixed point type acts as a full view of a private type.
14244
14245 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
14246 Set_Etype (T, Implicit_Base);
14247 Set_Size_Info (T, Implicit_Base);
14248 Inherit_Rep_Item_Chain (T, Implicit_Base);
14249 Set_Digits_Value (T, Digs_Val);
14250 Set_Delta_Value (T, Delta_Val);
14251 Set_Small_Value (T, Delta_Val);
14252 Set_Scale_Value (T, Scale_Val);
14253 Set_Is_Constrained (T);
14254 end Decimal_Fixed_Point_Type_Declaration;
14255
14256 -----------------------------------
14257 -- Derive_Progenitor_Subprograms --
14258 -----------------------------------
14259
14260 procedure Derive_Progenitor_Subprograms
14261 (Parent_Type : Entity_Id;
14262 Tagged_Type : Entity_Id)
14263 is
14264 E : Entity_Id;
14265 Elmt : Elmt_Id;
14266 Iface : Entity_Id;
14267 Iface_Elmt : Elmt_Id;
14268 Iface_Subp : Entity_Id;
14269 New_Subp : Entity_Id := Empty;
14270 Prim_Elmt : Elmt_Id;
14271 Subp : Entity_Id;
14272 Typ : Entity_Id;
14273
14274 begin
14275 pragma Assert (Ada_Version >= Ada_2005
14276 and then Is_Record_Type (Tagged_Type)
14277 and then Is_Tagged_Type (Tagged_Type)
14278 and then Has_Interfaces (Tagged_Type));
14279
14280 -- Step 1: Transfer to the full-view primitives associated with the
14281 -- partial-view that cover interface primitives. Conceptually this
14282 -- work should be done later by Process_Full_View; done here to
14283 -- simplify its implementation at later stages. It can be safely
14284 -- done here because interfaces must be visible in the partial and
14285 -- private view (RM 7.3(7.3/2)).
14286
14287 -- Small optimization: This work is only required if the parent may
14288 -- have entities whose Alias attribute reference an interface primitive.
14289 -- Such a situation may occur if the parent is an abstract type and the
14290 -- primitive has not been yet overridden or if the parent is a generic
14291 -- formal type covering interfaces.
14292
14293 -- If the tagged type is not abstract, it cannot have abstract
14294 -- primitives (the only entities in the list of primitives of
14295 -- non-abstract tagged types that can reference abstract primitives
14296 -- through its Alias attribute are the internal entities that have
14297 -- attribute Interface_Alias, and these entities are generated later
14298 -- by Add_Internal_Interface_Entities).
14299
14300 if In_Private_Part (Current_Scope)
14301 and then (Is_Abstract_Type (Parent_Type)
14302 or else
14303 Is_Generic_Type (Parent_Type))
14304 then
14305 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
14306 while Present (Elmt) loop
14307 Subp := Node (Elmt);
14308
14309 -- At this stage it is not possible to have entities in the list
14310 -- of primitives that have attribute Interface_Alias.
14311
14312 pragma Assert (No (Interface_Alias (Subp)));
14313
14314 Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
14315
14316 if Is_Interface (Typ) then
14317 E := Find_Primitive_Covering_Interface
14318 (Tagged_Type => Tagged_Type,
14319 Iface_Prim => Subp);
14320
14321 if Present (E)
14322 and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
14323 then
14324 Replace_Elmt (Elmt, E);
14325 Remove_Homonym (Subp);
14326 end if;
14327 end if;
14328
14329 Next_Elmt (Elmt);
14330 end loop;
14331 end if;
14332
14333 -- Step 2: Add primitives of progenitors that are not implemented by
14334 -- parents of Tagged_Type.
14335
14336 if Present (Interfaces (Base_Type (Tagged_Type))) then
14337 Iface_Elmt := First_Elmt (Interfaces (Base_Type (Tagged_Type)));
14338 while Present (Iface_Elmt) loop
14339 Iface := Node (Iface_Elmt);
14340
14341 Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
14342 while Present (Prim_Elmt) loop
14343 Iface_Subp := Node (Prim_Elmt);
14344
14345 -- Exclude derivation of predefined primitives except those
14346 -- that come from source, or are inherited from one that comes
14347 -- from source. Required to catch declarations of equality
14348 -- operators of interfaces. For example:
14349
14350 -- type Iface is interface;
14351 -- function "=" (Left, Right : Iface) return Boolean;
14352
14353 if not Is_Predefined_Dispatching_Operation (Iface_Subp)
14354 or else Comes_From_Source (Ultimate_Alias (Iface_Subp))
14355 then
14356 E := Find_Primitive_Covering_Interface
14357 (Tagged_Type => Tagged_Type,
14358 Iface_Prim => Iface_Subp);
14359
14360 -- If not found we derive a new primitive leaving its alias
14361 -- attribute referencing the interface primitive.
14362
14363 if No (E) then
14364 Derive_Subprogram
14365 (New_Subp, Iface_Subp, Tagged_Type, Iface);
14366
14367 -- Ada 2012 (AI05-0197): If the covering primitive's name
14368 -- differs from the name of the interface primitive then it
14369 -- is a private primitive inherited from a parent type. In
14370 -- such case, given that Tagged_Type covers the interface,
14371 -- the inherited private primitive becomes visible. For such
14372 -- purpose we add a new entity that renames the inherited
14373 -- private primitive.
14374
14375 elsif Chars (E) /= Chars (Iface_Subp) then
14376 pragma Assert (Has_Suffix (E, 'P'));
14377 Derive_Subprogram
14378 (New_Subp, Iface_Subp, Tagged_Type, Iface);
14379 Set_Alias (New_Subp, E);
14380 Set_Is_Abstract_Subprogram (New_Subp,
14381 Is_Abstract_Subprogram (E));
14382
14383 -- Propagate to the full view interface entities associated
14384 -- with the partial view.
14385
14386 elsif In_Private_Part (Current_Scope)
14387 and then Present (Alias (E))
14388 and then Alias (E) = Iface_Subp
14389 and then
14390 List_Containing (Parent (E)) /=
14391 Private_Declarations
14392 (Specification
14393 (Unit_Declaration_Node (Current_Scope)))
14394 then
14395 Append_Elmt (E, Primitive_Operations (Tagged_Type));
14396 end if;
14397 end if;
14398
14399 Next_Elmt (Prim_Elmt);
14400 end loop;
14401
14402 Next_Elmt (Iface_Elmt);
14403 end loop;
14404 end if;
14405 end Derive_Progenitor_Subprograms;
14406
14407 -----------------------
14408 -- Derive_Subprogram --
14409 -----------------------
14410
14411 procedure Derive_Subprogram
14412 (New_Subp : in out Entity_Id;
14413 Parent_Subp : Entity_Id;
14414 Derived_Type : Entity_Id;
14415 Parent_Type : Entity_Id;
14416 Actual_Subp : Entity_Id := Empty)
14417 is
14418 Formal : Entity_Id;
14419 -- Formal parameter of parent primitive operation
14420
14421 Formal_Of_Actual : Entity_Id;
14422 -- Formal parameter of actual operation, when the derivation is to
14423 -- create a renaming for a primitive operation of an actual in an
14424 -- instantiation.
14425
14426 New_Formal : Entity_Id;
14427 -- Formal of inherited operation
14428
14429 Visible_Subp : Entity_Id := Parent_Subp;
14430
14431 function Is_Private_Overriding return Boolean;
14432 -- If Subp is a private overriding of a visible operation, the inherited
14433 -- operation derives from the overridden op (even though its body is the
14434 -- overriding one) and the inherited operation is visible now. See
14435 -- sem_disp to see the full details of the handling of the overridden
14436 -- subprogram, which is removed from the list of primitive operations of
14437 -- the type. The overridden subprogram is saved locally in Visible_Subp,
14438 -- and used to diagnose abstract operations that need overriding in the
14439 -- derived type.
14440
14441 procedure Replace_Type (Id, New_Id : Entity_Id);
14442 -- When the type is an anonymous access type, create a new access type
14443 -- designating the derived type.
14444
14445 procedure Set_Derived_Name;
14446 -- This procedure sets the appropriate Chars name for New_Subp. This
14447 -- is normally just a copy of the parent name. An exception arises for
14448 -- type support subprograms, where the name is changed to reflect the
14449 -- name of the derived type, e.g. if type foo is derived from type bar,
14450 -- then a procedure barDA is derived with a name fooDA.
14451
14452 ---------------------------
14453 -- Is_Private_Overriding --
14454 ---------------------------
14455
14456 function Is_Private_Overriding return Boolean is
14457 Prev : Entity_Id;
14458
14459 begin
14460 -- If the parent is not a dispatching operation there is no
14461 -- need to investigate overridings
14462
14463 if not Is_Dispatching_Operation (Parent_Subp) then
14464 return False;
14465 end if;
14466
14467 -- The visible operation that is overridden is a homonym of the
14468 -- parent subprogram. We scan the homonym chain to find the one
14469 -- whose alias is the subprogram we are deriving.
14470
14471 Prev := Current_Entity (Parent_Subp);
14472 while Present (Prev) loop
14473 if Ekind (Prev) = Ekind (Parent_Subp)
14474 and then Alias (Prev) = Parent_Subp
14475 and then Scope (Parent_Subp) = Scope (Prev)
14476 and then not Is_Hidden (Prev)
14477 then
14478 Visible_Subp := Prev;
14479 return True;
14480 end if;
14481
14482 Prev := Homonym (Prev);
14483 end loop;
14484
14485 return False;
14486 end Is_Private_Overriding;
14487
14488 ------------------
14489 -- Replace_Type --
14490 ------------------
14491
14492 procedure Replace_Type (Id, New_Id : Entity_Id) is
14493 Id_Type : constant Entity_Id := Etype (Id);
14494 Acc_Type : Entity_Id;
14495 Par : constant Node_Id := Parent (Derived_Type);
14496
14497 begin
14498 -- When the type is an anonymous access type, create a new access
14499 -- type designating the derived type. This itype must be elaborated
14500 -- at the point of the derivation, not on subsequent calls that may
14501 -- be out of the proper scope for Gigi, so we insert a reference to
14502 -- it after the derivation.
14503
14504 if Ekind (Id_Type) = E_Anonymous_Access_Type then
14505 declare
14506 Desig_Typ : Entity_Id := Designated_Type (Id_Type);
14507
14508 begin
14509 if Ekind (Desig_Typ) = E_Record_Type_With_Private
14510 and then Present (Full_View (Desig_Typ))
14511 and then not Is_Private_Type (Parent_Type)
14512 then
14513 Desig_Typ := Full_View (Desig_Typ);
14514 end if;
14515
14516 if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
14517
14518 -- Ada 2005 (AI-251): Handle also derivations of abstract
14519 -- interface primitives.
14520
14521 or else (Is_Interface (Desig_Typ)
14522 and then not Is_Class_Wide_Type (Desig_Typ))
14523 then
14524 Acc_Type := New_Copy (Id_Type);
14525 Set_Etype (Acc_Type, Acc_Type);
14526 Set_Scope (Acc_Type, New_Subp);
14527
14528 -- Set size of anonymous access type. If we have an access
14529 -- to an unconstrained array, this is a fat pointer, so it
14530 -- is sizes at twice addtress size.
14531
14532 if Is_Array_Type (Desig_Typ)
14533 and then not Is_Constrained (Desig_Typ)
14534 then
14535 Init_Size (Acc_Type, 2 * System_Address_Size);
14536
14537 -- Other cases use a thin pointer
14538
14539 else
14540 Init_Size (Acc_Type, System_Address_Size);
14541 end if;
14542
14543 -- Set remaining characterstics of anonymous access type
14544
14545 Init_Alignment (Acc_Type);
14546 Set_Directly_Designated_Type (Acc_Type, Derived_Type);
14547
14548 Set_Etype (New_Id, Acc_Type);
14549 Set_Scope (New_Id, New_Subp);
14550
14551 -- Create a reference to it
14552
14553 Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
14554
14555 else
14556 Set_Etype (New_Id, Id_Type);
14557 end if;
14558 end;
14559
14560 -- In Ada2012, a formal may have an incomplete type but the type
14561 -- derivation that inherits the primitive follows the full view.
14562
14563 elsif Base_Type (Id_Type) = Base_Type (Parent_Type)
14564 or else
14565 (Ekind (Id_Type) = E_Record_Type_With_Private
14566 and then Present (Full_View (Id_Type))
14567 and then
14568 Base_Type (Full_View (Id_Type)) = Base_Type (Parent_Type))
14569 or else
14570 (Ada_Version >= Ada_2012
14571 and then Ekind (Id_Type) = E_Incomplete_Type
14572 and then Full_View (Id_Type) = Parent_Type)
14573 then
14574 -- Constraint checks on formals are generated during expansion,
14575 -- based on the signature of the original subprogram. The bounds
14576 -- of the derived type are not relevant, and thus we can use
14577 -- the base type for the formals. However, the return type may be
14578 -- used in a context that requires that the proper static bounds
14579 -- be used (a case statement, for example) and for those cases
14580 -- we must use the derived type (first subtype), not its base.
14581
14582 -- If the derived_type_definition has no constraints, we know that
14583 -- the derived type has the same constraints as the first subtype
14584 -- of the parent, and we can also use it rather than its base,
14585 -- which can lead to more efficient code.
14586
14587 if Etype (Id) = Parent_Type then
14588 if Is_Scalar_Type (Parent_Type)
14589 and then
14590 Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
14591 then
14592 Set_Etype (New_Id, Derived_Type);
14593
14594 elsif Nkind (Par) = N_Full_Type_Declaration
14595 and then
14596 Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
14597 and then
14598 Is_Entity_Name
14599 (Subtype_Indication (Type_Definition (Par)))
14600 then
14601 Set_Etype (New_Id, Derived_Type);
14602
14603 else
14604 Set_Etype (New_Id, Base_Type (Derived_Type));
14605 end if;
14606
14607 else
14608 Set_Etype (New_Id, Base_Type (Derived_Type));
14609 end if;
14610
14611 else
14612 Set_Etype (New_Id, Etype (Id));
14613 end if;
14614 end Replace_Type;
14615
14616 ----------------------
14617 -- Set_Derived_Name --
14618 ----------------------
14619
14620 procedure Set_Derived_Name is
14621 Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
14622 begin
14623 if Nm = TSS_Null then
14624 Set_Chars (New_Subp, Chars (Parent_Subp));
14625 else
14626 Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
14627 end if;
14628 end Set_Derived_Name;
14629
14630 -- Start of processing for Derive_Subprogram
14631
14632 begin
14633 New_Subp := New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
14634 Set_Ekind (New_Subp, Ekind (Parent_Subp));
14635
14636 -- Check whether the inherited subprogram is a private operation that
14637 -- should be inherited but not yet made visible. Such subprograms can
14638 -- become visible at a later point (e.g., the private part of a public
14639 -- child unit) via Declare_Inherited_Private_Subprograms. If the
14640 -- following predicate is true, then this is not such a private
14641 -- operation and the subprogram simply inherits the name of the parent
14642 -- subprogram. Note the special check for the names of controlled
14643 -- operations, which are currently exempted from being inherited with
14644 -- a hidden name because they must be findable for generation of
14645 -- implicit run-time calls.
14646
14647 if not Is_Hidden (Parent_Subp)
14648 or else Is_Internal (Parent_Subp)
14649 or else Is_Private_Overriding
14650 or else Is_Internal_Name (Chars (Parent_Subp))
14651 or else Nam_In (Chars (Parent_Subp), Name_Initialize,
14652 Name_Adjust,
14653 Name_Finalize)
14654 then
14655 Set_Derived_Name;
14656
14657 -- An inherited dispatching equality will be overridden by an internally
14658 -- generated one, or by an explicit one, so preserve its name and thus
14659 -- its entry in the dispatch table. Otherwise, if Parent_Subp is a
14660 -- private operation it may become invisible if the full view has
14661 -- progenitors, and the dispatch table will be malformed.
14662 -- We check that the type is limited to handle the anomalous declaration
14663 -- of Limited_Controlled, which is derived from a non-limited type, and
14664 -- which is handled specially elsewhere as well.
14665
14666 elsif Chars (Parent_Subp) = Name_Op_Eq
14667 and then Is_Dispatching_Operation (Parent_Subp)
14668 and then Etype (Parent_Subp) = Standard_Boolean
14669 and then not Is_Limited_Type (Etype (First_Formal (Parent_Subp)))
14670 and then
14671 Etype (First_Formal (Parent_Subp)) =
14672 Etype (Next_Formal (First_Formal (Parent_Subp)))
14673 then
14674 Set_Derived_Name;
14675
14676 -- If parent is hidden, this can be a regular derivation if the
14677 -- parent is immediately visible in a non-instantiating context,
14678 -- or if we are in the private part of an instance. This test
14679 -- should still be refined ???
14680
14681 -- The test for In_Instance_Not_Visible avoids inheriting the derived
14682 -- operation as a non-visible operation in cases where the parent
14683 -- subprogram might not be visible now, but was visible within the
14684 -- original generic, so it would be wrong to make the inherited
14685 -- subprogram non-visible now. (Not clear if this test is fully
14686 -- correct; are there any cases where we should declare the inherited
14687 -- operation as not visible to avoid it being overridden, e.g., when
14688 -- the parent type is a generic actual with private primitives ???)
14689
14690 -- (they should be treated the same as other private inherited
14691 -- subprograms, but it's not clear how to do this cleanly). ???
14692
14693 elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
14694 and then Is_Immediately_Visible (Parent_Subp)
14695 and then not In_Instance)
14696 or else In_Instance_Not_Visible
14697 then
14698 Set_Derived_Name;
14699
14700 -- Ada 2005 (AI-251): Regular derivation if the parent subprogram
14701 -- overrides an interface primitive because interface primitives
14702 -- must be visible in the partial view of the parent (RM 7.3 (7.3/2))
14703
14704 elsif Ada_Version >= Ada_2005
14705 and then Is_Dispatching_Operation (Parent_Subp)
14706 and then Covers_Some_Interface (Parent_Subp)
14707 then
14708 Set_Derived_Name;
14709
14710 -- Otherwise, the type is inheriting a private operation, so enter
14711 -- it with a special name so it can't be overridden.
14712
14713 else
14714 Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
14715 end if;
14716
14717 Set_Parent (New_Subp, Parent (Derived_Type));
14718
14719 if Present (Actual_Subp) then
14720 Replace_Type (Actual_Subp, New_Subp);
14721 else
14722 Replace_Type (Parent_Subp, New_Subp);
14723 end if;
14724
14725 Conditional_Delay (New_Subp, Parent_Subp);
14726
14727 -- If we are creating a renaming for a primitive operation of an
14728 -- actual of a generic derived type, we must examine the signature
14729 -- of the actual primitive, not that of the generic formal, which for
14730 -- example may be an interface. However the name and initial value
14731 -- of the inherited operation are those of the formal primitive.
14732
14733 Formal := First_Formal (Parent_Subp);
14734
14735 if Present (Actual_Subp) then
14736 Formal_Of_Actual := First_Formal (Actual_Subp);
14737 else
14738 Formal_Of_Actual := Empty;
14739 end if;
14740
14741 while Present (Formal) loop
14742 New_Formal := New_Copy (Formal);
14743
14744 -- Normally we do not go copying parents, but in the case of
14745 -- formals, we need to link up to the declaration (which is the
14746 -- parameter specification), and it is fine to link up to the
14747 -- original formal's parameter specification in this case.
14748
14749 Set_Parent (New_Formal, Parent (Formal));
14750 Append_Entity (New_Formal, New_Subp);
14751
14752 if Present (Formal_Of_Actual) then
14753 Replace_Type (Formal_Of_Actual, New_Formal);
14754 Next_Formal (Formal_Of_Actual);
14755 else
14756 Replace_Type (Formal, New_Formal);
14757 end if;
14758
14759 Next_Formal (Formal);
14760 end loop;
14761
14762 -- If this derivation corresponds to a tagged generic actual, then
14763 -- primitive operations rename those of the actual. Otherwise the
14764 -- primitive operations rename those of the parent type, If the parent
14765 -- renames an intrinsic operator, so does the new subprogram. We except
14766 -- concatenation, which is always properly typed, and does not get
14767 -- expanded as other intrinsic operations.
14768
14769 if No (Actual_Subp) then
14770 if Is_Intrinsic_Subprogram (Parent_Subp) then
14771 Set_Is_Intrinsic_Subprogram (New_Subp);
14772
14773 if Present (Alias (Parent_Subp))
14774 and then Chars (Parent_Subp) /= Name_Op_Concat
14775 then
14776 Set_Alias (New_Subp, Alias (Parent_Subp));
14777 else
14778 Set_Alias (New_Subp, Parent_Subp);
14779 end if;
14780
14781 else
14782 Set_Alias (New_Subp, Parent_Subp);
14783 end if;
14784
14785 else
14786 Set_Alias (New_Subp, Actual_Subp);
14787 end if;
14788
14789 -- Inherit the "ghostness" from the parent subprogram
14790
14791 if Is_Ghost_Entity (Alias (New_Subp)) then
14792 Set_Is_Ghost_Entity (New_Subp);
14793 end if;
14794
14795 -- Derived subprograms of a tagged type must inherit the convention
14796 -- of the parent subprogram (a requirement of AI-117). Derived
14797 -- subprograms of untagged types simply get convention Ada by default.
14798
14799 -- If the derived type is a tagged generic formal type with unknown
14800 -- discriminants, its convention is intrinsic (RM 6.3.1 (8)).
14801
14802 -- However, if the type is derived from a generic formal, the further
14803 -- inherited subprogram has the convention of the non-generic ancestor.
14804 -- Otherwise there would be no way to override the operation.
14805 -- (This is subject to forthcoming ARG discussions).
14806
14807 if Is_Tagged_Type (Derived_Type) then
14808 if Is_Generic_Type (Derived_Type)
14809 and then Has_Unknown_Discriminants (Derived_Type)
14810 then
14811 Set_Convention (New_Subp, Convention_Intrinsic);
14812
14813 else
14814 if Is_Generic_Type (Parent_Type)
14815 and then Has_Unknown_Discriminants (Parent_Type)
14816 then
14817 Set_Convention (New_Subp, Convention (Alias (Parent_Subp)));
14818 else
14819 Set_Convention (New_Subp, Convention (Parent_Subp));
14820 end if;
14821 end if;
14822 end if;
14823
14824 -- Predefined controlled operations retain their name even if the parent
14825 -- is hidden (see above), but they are not primitive operations if the
14826 -- ancestor is not visible, for example if the parent is a private
14827 -- extension completed with a controlled extension. Note that a full
14828 -- type that is controlled can break privacy: the flag Is_Controlled is
14829 -- set on both views of the type.
14830
14831 if Is_Controlled (Parent_Type)
14832 and then Nam_In (Chars (Parent_Subp), Name_Initialize,
14833 Name_Adjust,
14834 Name_Finalize)
14835 and then Is_Hidden (Parent_Subp)
14836 and then not Is_Visibly_Controlled (Parent_Type)
14837 then
14838 Set_Is_Hidden (New_Subp);
14839 end if;
14840
14841 Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
14842 Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
14843
14844 if Ekind (Parent_Subp) = E_Procedure then
14845 Set_Is_Valued_Procedure
14846 (New_Subp, Is_Valued_Procedure (Parent_Subp));
14847 else
14848 Set_Has_Controlling_Result
14849 (New_Subp, Has_Controlling_Result (Parent_Subp));
14850 end if;
14851
14852 -- No_Return must be inherited properly. If this is overridden in the
14853 -- case of a dispatching operation, then a check is made in Sem_Disp
14854 -- that the overriding operation is also No_Return (no such check is
14855 -- required for the case of non-dispatching operation.
14856
14857 Set_No_Return (New_Subp, No_Return (Parent_Subp));
14858
14859 -- A derived function with a controlling result is abstract. If the
14860 -- Derived_Type is a nonabstract formal generic derived type, then
14861 -- inherited operations are not abstract: the required check is done at
14862 -- instantiation time. If the derivation is for a generic actual, the
14863 -- function is not abstract unless the actual is.
14864
14865 if Is_Generic_Type (Derived_Type)
14866 and then not Is_Abstract_Type (Derived_Type)
14867 then
14868 null;
14869
14870 -- Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
14871 -- properties of the subprogram, as defined in RM-3.9.3(4/2-6/2).
14872
14873 -- A subprogram subject to pragma Extensions_Visible with value False
14874 -- requires overriding if the subprogram has at least one controlling
14875 -- OUT parameter (SPARK RM 6.1.7(6)).
14876
14877 elsif Ada_Version >= Ada_2005
14878 and then (Is_Abstract_Subprogram (Alias (New_Subp))
14879 or else (Is_Tagged_Type (Derived_Type)
14880 and then Etype (New_Subp) = Derived_Type
14881 and then not Is_Null_Extension (Derived_Type))
14882 or else (Is_Tagged_Type (Derived_Type)
14883 and then Ekind (Etype (New_Subp)) =
14884 E_Anonymous_Access_Type
14885 and then Designated_Type (Etype (New_Subp)) =
14886 Derived_Type
14887 and then not Is_Null_Extension (Derived_Type))
14888 or else (Comes_From_Source (Alias (New_Subp))
14889 and then Is_EVF_Procedure (Alias (New_Subp))))
14890 and then No (Actual_Subp)
14891 then
14892 if not Is_Tagged_Type (Derived_Type)
14893 or else Is_Abstract_Type (Derived_Type)
14894 or else Is_Abstract_Subprogram (Alias (New_Subp))
14895 then
14896 Set_Is_Abstract_Subprogram (New_Subp);
14897 else
14898 Set_Requires_Overriding (New_Subp);
14899 end if;
14900
14901 elsif Ada_Version < Ada_2005
14902 and then (Is_Abstract_Subprogram (Alias (New_Subp))
14903 or else (Is_Tagged_Type (Derived_Type)
14904 and then Etype (New_Subp) = Derived_Type
14905 and then No (Actual_Subp)))
14906 then
14907 Set_Is_Abstract_Subprogram (New_Subp);
14908
14909 -- AI05-0097 : an inherited operation that dispatches on result is
14910 -- abstract if the derived type is abstract, even if the parent type
14911 -- is concrete and the derived type is a null extension.
14912
14913 elsif Has_Controlling_Result (Alias (New_Subp))
14914 and then Is_Abstract_Type (Etype (New_Subp))
14915 then
14916 Set_Is_Abstract_Subprogram (New_Subp);
14917
14918 -- Finally, if the parent type is abstract we must verify that all
14919 -- inherited operations are either non-abstract or overridden, or that
14920 -- the derived type itself is abstract (this check is performed at the
14921 -- end of a package declaration, in Check_Abstract_Overriding). A
14922 -- private overriding in the parent type will not be visible in the
14923 -- derivation if we are not in an inner package or in a child unit of
14924 -- the parent type, in which case the abstractness of the inherited
14925 -- operation is carried to the new subprogram.
14926
14927 elsif Is_Abstract_Type (Parent_Type)
14928 and then not In_Open_Scopes (Scope (Parent_Type))
14929 and then Is_Private_Overriding
14930 and then Is_Abstract_Subprogram (Visible_Subp)
14931 then
14932 if No (Actual_Subp) then
14933 Set_Alias (New_Subp, Visible_Subp);
14934 Set_Is_Abstract_Subprogram (New_Subp, True);
14935
14936 else
14937 -- If this is a derivation for an instance of a formal derived
14938 -- type, abstractness comes from the primitive operation of the
14939 -- actual, not from the operation inherited from the ancestor.
14940
14941 Set_Is_Abstract_Subprogram
14942 (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
14943 end if;
14944 end if;
14945
14946 New_Overloaded_Entity (New_Subp, Derived_Type);
14947
14948 -- Check for case of a derived subprogram for the instantiation of a
14949 -- formal derived tagged type, if so mark the subprogram as dispatching
14950 -- and inherit the dispatching attributes of the actual subprogram. The
14951 -- derived subprogram is effectively renaming of the actual subprogram,
14952 -- so it needs to have the same attributes as the actual.
14953
14954 if Present (Actual_Subp)
14955 and then Is_Dispatching_Operation (Actual_Subp)
14956 then
14957 Set_Is_Dispatching_Operation (New_Subp);
14958
14959 if Present (DTC_Entity (Actual_Subp)) then
14960 Set_DTC_Entity (New_Subp, DTC_Entity (Actual_Subp));
14961 Set_DT_Position_Value (New_Subp, DT_Position (Actual_Subp));
14962 end if;
14963 end if;
14964
14965 -- Indicate that a derived subprogram does not require a body and that
14966 -- it does not require processing of default expressions.
14967
14968 Set_Has_Completion (New_Subp);
14969 Set_Default_Expressions_Processed (New_Subp);
14970
14971 if Ekind (New_Subp) = E_Function then
14972 Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
14973 end if;
14974 end Derive_Subprogram;
14975
14976 ------------------------
14977 -- Derive_Subprograms --
14978 ------------------------
14979
14980 procedure Derive_Subprograms
14981 (Parent_Type : Entity_Id;
14982 Derived_Type : Entity_Id;
14983 Generic_Actual : Entity_Id := Empty)
14984 is
14985 Op_List : constant Elist_Id :=
14986 Collect_Primitive_Operations (Parent_Type);
14987
14988 function Check_Derived_Type return Boolean;
14989 -- Check that all the entities derived from Parent_Type are found in
14990 -- the list of primitives of Derived_Type exactly in the same order.
14991
14992 procedure Derive_Interface_Subprogram
14993 (New_Subp : in out Entity_Id;
14994 Subp : Entity_Id;
14995 Actual_Subp : Entity_Id);
14996 -- Derive New_Subp from the ultimate alias of the parent subprogram Subp
14997 -- (which is an interface primitive). If Generic_Actual is present then
14998 -- Actual_Subp is the actual subprogram corresponding with the generic
14999 -- subprogram Subp.
15000
15001 function Check_Derived_Type return Boolean is
15002 E : Entity_Id;
15003 Elmt : Elmt_Id;
15004 List : Elist_Id;
15005 New_Subp : Entity_Id;
15006 Op_Elmt : Elmt_Id;
15007 Subp : Entity_Id;
15008
15009 begin
15010 -- Traverse list of entities in the current scope searching for
15011 -- an incomplete type whose full-view is derived type
15012
15013 E := First_Entity (Scope (Derived_Type));
15014 while Present (E) and then E /= Derived_Type loop
15015 if Ekind (E) = E_Incomplete_Type
15016 and then Present (Full_View (E))
15017 and then Full_View (E) = Derived_Type
15018 then
15019 -- Disable this test if Derived_Type completes an incomplete
15020 -- type because in such case more primitives can be added
15021 -- later to the list of primitives of Derived_Type by routine
15022 -- Process_Incomplete_Dependents
15023
15024 return True;
15025 end if;
15026
15027 E := Next_Entity (E);
15028 end loop;
15029
15030 List := Collect_Primitive_Operations (Derived_Type);
15031 Elmt := First_Elmt (List);
15032
15033 Op_Elmt := First_Elmt (Op_List);
15034 while Present (Op_Elmt) loop
15035 Subp := Node (Op_Elmt);
15036 New_Subp := Node (Elmt);
15037
15038 -- At this early stage Derived_Type has no entities with attribute
15039 -- Interface_Alias. In addition, such primitives are always
15040 -- located at the end of the list of primitives of Parent_Type.
15041 -- Therefore, if found we can safely stop processing pending
15042 -- entities.
15043
15044 exit when Present (Interface_Alias (Subp));
15045
15046 -- Handle hidden entities
15047
15048 if not Is_Predefined_Dispatching_Operation (Subp)
15049 and then Is_Hidden (Subp)
15050 then
15051 if Present (New_Subp)
15052 and then Primitive_Names_Match (Subp, New_Subp)
15053 then
15054 Next_Elmt (Elmt);
15055 end if;
15056
15057 else
15058 if not Present (New_Subp)
15059 or else Ekind (Subp) /= Ekind (New_Subp)
15060 or else not Primitive_Names_Match (Subp, New_Subp)
15061 then
15062 return False;
15063 end if;
15064
15065 Next_Elmt (Elmt);
15066 end if;
15067
15068 Next_Elmt (Op_Elmt);
15069 end loop;
15070
15071 return True;
15072 end Check_Derived_Type;
15073
15074 ---------------------------------
15075 -- Derive_Interface_Subprogram --
15076 ---------------------------------
15077
15078 procedure Derive_Interface_Subprogram
15079 (New_Subp : in out Entity_Id;
15080 Subp : Entity_Id;
15081 Actual_Subp : Entity_Id)
15082 is
15083 Iface_Subp : constant Entity_Id := Ultimate_Alias (Subp);
15084 Iface_Type : constant Entity_Id := Find_Dispatching_Type (Iface_Subp);
15085
15086 begin
15087 pragma Assert (Is_Interface (Iface_Type));
15088
15089 Derive_Subprogram
15090 (New_Subp => New_Subp,
15091 Parent_Subp => Iface_Subp,
15092 Derived_Type => Derived_Type,
15093 Parent_Type => Iface_Type,
15094 Actual_Subp => Actual_Subp);
15095
15096 -- Given that this new interface entity corresponds with a primitive
15097 -- of the parent that was not overridden we must leave it associated
15098 -- with its parent primitive to ensure that it will share the same
15099 -- dispatch table slot when overridden.
15100
15101 if No (Actual_Subp) then
15102 Set_Alias (New_Subp, Subp);
15103
15104 -- For instantiations this is not needed since the previous call to
15105 -- Derive_Subprogram leaves the entity well decorated.
15106
15107 else
15108 pragma Assert (Alias (New_Subp) = Actual_Subp);
15109 null;
15110 end if;
15111 end Derive_Interface_Subprogram;
15112
15113 -- Local variables
15114
15115 Alias_Subp : Entity_Id;
15116 Act_List : Elist_Id;
15117 Act_Elmt : Elmt_Id;
15118 Act_Subp : Entity_Id := Empty;
15119 Elmt : Elmt_Id;
15120 Need_Search : Boolean := False;
15121 New_Subp : Entity_Id := Empty;
15122 Parent_Base : Entity_Id;
15123 Subp : Entity_Id;
15124
15125 -- Start of processing for Derive_Subprograms
15126
15127 begin
15128 if Ekind (Parent_Type) = E_Record_Type_With_Private
15129 and then Has_Discriminants (Parent_Type)
15130 and then Present (Full_View (Parent_Type))
15131 then
15132 Parent_Base := Full_View (Parent_Type);
15133 else
15134 Parent_Base := Parent_Type;
15135 end if;
15136
15137 if Present (Generic_Actual) then
15138 Act_List := Collect_Primitive_Operations (Generic_Actual);
15139 Act_Elmt := First_Elmt (Act_List);
15140 else
15141 Act_List := No_Elist;
15142 Act_Elmt := No_Elmt;
15143 end if;
15144
15145 -- Derive primitives inherited from the parent. Note that if the generic
15146 -- actual is present, this is not really a type derivation, it is a
15147 -- completion within an instance.
15148
15149 -- Case 1: Derived_Type does not implement interfaces
15150
15151 if not Is_Tagged_Type (Derived_Type)
15152 or else (not Has_Interfaces (Derived_Type)
15153 and then not (Present (Generic_Actual)
15154 and then Has_Interfaces (Generic_Actual)))
15155 then
15156 Elmt := First_Elmt (Op_List);
15157 while Present (Elmt) loop
15158 Subp := Node (Elmt);
15159
15160 -- Literals are derived earlier in the process of building the
15161 -- derived type, and are skipped here.
15162
15163 if Ekind (Subp) = E_Enumeration_Literal then
15164 null;
15165
15166 -- The actual is a direct descendant and the common primitive
15167 -- operations appear in the same order.
15168
15169 -- If the generic parent type is present, the derived type is an
15170 -- instance of a formal derived type, and within the instance its
15171 -- operations are those of the actual. We derive from the formal
15172 -- type but make the inherited operations aliases of the
15173 -- corresponding operations of the actual.
15174
15175 else
15176 pragma Assert (No (Node (Act_Elmt))
15177 or else (Primitive_Names_Match (Subp, Node (Act_Elmt))
15178 and then
15179 Type_Conformant
15180 (Subp, Node (Act_Elmt),
15181 Skip_Controlling_Formals => True)));
15182
15183 Derive_Subprogram
15184 (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
15185
15186 if Present (Act_Elmt) then
15187 Next_Elmt (Act_Elmt);
15188 end if;
15189 end if;
15190
15191 Next_Elmt (Elmt);
15192 end loop;
15193
15194 -- Case 2: Derived_Type implements interfaces
15195
15196 else
15197 -- If the parent type has no predefined primitives we remove
15198 -- predefined primitives from the list of primitives of generic
15199 -- actual to simplify the complexity of this algorithm.
15200
15201 if Present (Generic_Actual) then
15202 declare
15203 Has_Predefined_Primitives : Boolean := False;
15204
15205 begin
15206 -- Check if the parent type has predefined primitives
15207
15208 Elmt := First_Elmt (Op_List);
15209 while Present (Elmt) loop
15210 Subp := Node (Elmt);
15211
15212 if Is_Predefined_Dispatching_Operation (Subp)
15213 and then not Comes_From_Source (Ultimate_Alias (Subp))
15214 then
15215 Has_Predefined_Primitives := True;
15216 exit;
15217 end if;
15218
15219 Next_Elmt (Elmt);
15220 end loop;
15221
15222 -- Remove predefined primitives of Generic_Actual. We must use
15223 -- an auxiliary list because in case of tagged types the value
15224 -- returned by Collect_Primitive_Operations is the value stored
15225 -- in its Primitive_Operations attribute (and we don't want to
15226 -- modify its current contents).
15227
15228 if not Has_Predefined_Primitives then
15229 declare
15230 Aux_List : constant Elist_Id := New_Elmt_List;
15231
15232 begin
15233 Elmt := First_Elmt (Act_List);
15234 while Present (Elmt) loop
15235 Subp := Node (Elmt);
15236
15237 if not Is_Predefined_Dispatching_Operation (Subp)
15238 or else Comes_From_Source (Subp)
15239 then
15240 Append_Elmt (Subp, Aux_List);
15241 end if;
15242
15243 Next_Elmt (Elmt);
15244 end loop;
15245
15246 Act_List := Aux_List;
15247 end;
15248 end if;
15249
15250 Act_Elmt := First_Elmt (Act_List);
15251 Act_Subp := Node (Act_Elmt);
15252 end;
15253 end if;
15254
15255 -- Stage 1: If the generic actual is not present we derive the
15256 -- primitives inherited from the parent type. If the generic parent
15257 -- type is present, the derived type is an instance of a formal
15258 -- derived type, and within the instance its operations are those of
15259 -- the actual. We derive from the formal type but make the inherited
15260 -- operations aliases of the corresponding operations of the actual.
15261
15262 Elmt := First_Elmt (Op_List);
15263 while Present (Elmt) loop
15264 Subp := Node (Elmt);
15265 Alias_Subp := Ultimate_Alias (Subp);
15266
15267 -- Do not derive internal entities of the parent that link
15268 -- interface primitives with their covering primitive. These
15269 -- entities will be added to this type when frozen.
15270
15271 if Present (Interface_Alias (Subp)) then
15272 goto Continue;
15273 end if;
15274
15275 -- If the generic actual is present find the corresponding
15276 -- operation in the generic actual. If the parent type is a
15277 -- direct ancestor of the derived type then, even if it is an
15278 -- interface, the operations are inherited from the primary
15279 -- dispatch table and are in the proper order. If we detect here
15280 -- that primitives are not in the same order we traverse the list
15281 -- of primitive operations of the actual to find the one that
15282 -- implements the interface primitive.
15283
15284 if Need_Search
15285 or else
15286 (Present (Generic_Actual)
15287 and then Present (Act_Subp)
15288 and then not
15289 (Primitive_Names_Match (Subp, Act_Subp)
15290 and then
15291 Type_Conformant (Subp, Act_Subp,
15292 Skip_Controlling_Formals => True)))
15293 then
15294 pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual,
15295 Use_Full_View => True));
15296
15297 -- Remember that we need searching for all pending primitives
15298
15299 Need_Search := True;
15300
15301 -- Handle entities associated with interface primitives
15302
15303 if Present (Alias_Subp)
15304 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
15305 and then not Is_Predefined_Dispatching_Operation (Subp)
15306 then
15307 -- Search for the primitive in the homonym chain
15308
15309 Act_Subp :=
15310 Find_Primitive_Covering_Interface
15311 (Tagged_Type => Generic_Actual,
15312 Iface_Prim => Alias_Subp);
15313
15314 -- Previous search may not locate primitives covering
15315 -- interfaces defined in generics units or instantiations.
15316 -- (it fails if the covering primitive has formals whose
15317 -- type is also defined in generics or instantiations).
15318 -- In such case we search in the list of primitives of the
15319 -- generic actual for the internal entity that links the
15320 -- interface primitive and the covering primitive.
15321
15322 if No (Act_Subp)
15323 and then Is_Generic_Type (Parent_Type)
15324 then
15325 -- This code has been designed to handle only generic
15326 -- formals that implement interfaces that are defined
15327 -- in a generic unit or instantiation. If this code is
15328 -- needed for other cases we must review it because
15329 -- (given that it relies on Original_Location to locate
15330 -- the primitive of Generic_Actual that covers the
15331 -- interface) it could leave linked through attribute
15332 -- Alias entities of unrelated instantiations).
15333
15334 pragma Assert
15335 (Is_Generic_Unit
15336 (Scope (Find_Dispatching_Type (Alias_Subp)))
15337 or else
15338 Instantiation_Depth
15339 (Sloc (Find_Dispatching_Type (Alias_Subp))) > 0);
15340
15341 declare
15342 Iface_Prim_Loc : constant Source_Ptr :=
15343 Original_Location (Sloc (Alias_Subp));
15344
15345 Elmt : Elmt_Id;
15346 Prim : Entity_Id;
15347
15348 begin
15349 Elmt :=
15350 First_Elmt (Primitive_Operations (Generic_Actual));
15351
15352 Search : while Present (Elmt) loop
15353 Prim := Node (Elmt);
15354
15355 if Present (Interface_Alias (Prim))
15356 and then Original_Location
15357 (Sloc (Interface_Alias (Prim))) =
15358 Iface_Prim_Loc
15359 then
15360 Act_Subp := Alias (Prim);
15361 exit Search;
15362 end if;
15363
15364 Next_Elmt (Elmt);
15365 end loop Search;
15366 end;
15367 end if;
15368
15369 pragma Assert (Present (Act_Subp)
15370 or else Is_Abstract_Type (Generic_Actual)
15371 or else Serious_Errors_Detected > 0);
15372
15373 -- Handle predefined primitives plus the rest of user-defined
15374 -- primitives
15375
15376 else
15377 Act_Elmt := First_Elmt (Act_List);
15378 while Present (Act_Elmt) loop
15379 Act_Subp := Node (Act_Elmt);
15380
15381 exit when Primitive_Names_Match (Subp, Act_Subp)
15382 and then Type_Conformant
15383 (Subp, Act_Subp,
15384 Skip_Controlling_Formals => True)
15385 and then No (Interface_Alias (Act_Subp));
15386
15387 Next_Elmt (Act_Elmt);
15388 end loop;
15389
15390 if No (Act_Elmt) then
15391 Act_Subp := Empty;
15392 end if;
15393 end if;
15394 end if;
15395
15396 -- Case 1: If the parent is a limited interface then it has the
15397 -- predefined primitives of synchronized interfaces. However, the
15398 -- actual type may be a non-limited type and hence it does not
15399 -- have such primitives.
15400
15401 if Present (Generic_Actual)
15402 and then not Present (Act_Subp)
15403 and then Is_Limited_Interface (Parent_Base)
15404 and then Is_Predefined_Interface_Primitive (Subp)
15405 then
15406 null;
15407
15408 -- Case 2: Inherit entities associated with interfaces that were
15409 -- not covered by the parent type. We exclude here null interface
15410 -- primitives because they do not need special management.
15411
15412 -- We also exclude interface operations that are renamings. If the
15413 -- subprogram is an explicit renaming of an interface primitive,
15414 -- it is a regular primitive operation, and the presence of its
15415 -- alias is not relevant: it has to be derived like any other
15416 -- primitive.
15417
15418 elsif Present (Alias (Subp))
15419 and then Nkind (Unit_Declaration_Node (Subp)) /=
15420 N_Subprogram_Renaming_Declaration
15421 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
15422 and then not
15423 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
15424 and then Null_Present (Parent (Alias_Subp)))
15425 then
15426 -- If this is an abstract private type then we transfer the
15427 -- derivation of the interface primitive from the partial view
15428 -- to the full view. This is safe because all the interfaces
15429 -- must be visible in the partial view. Done to avoid adding
15430 -- a new interface derivation to the private part of the
15431 -- enclosing package; otherwise this new derivation would be
15432 -- decorated as hidden when the analysis of the enclosing
15433 -- package completes.
15434
15435 if Is_Abstract_Type (Derived_Type)
15436 and then In_Private_Part (Current_Scope)
15437 and then Has_Private_Declaration (Derived_Type)
15438 then
15439 declare
15440 Partial_View : Entity_Id;
15441 Elmt : Elmt_Id;
15442 Ent : Entity_Id;
15443
15444 begin
15445 Partial_View := First_Entity (Current_Scope);
15446 loop
15447 exit when No (Partial_View)
15448 or else (Has_Private_Declaration (Partial_View)
15449 and then
15450 Full_View (Partial_View) = Derived_Type);
15451
15452 Next_Entity (Partial_View);
15453 end loop;
15454
15455 -- If the partial view was not found then the source code
15456 -- has errors and the derivation is not needed.
15457
15458 if Present (Partial_View) then
15459 Elmt :=
15460 First_Elmt (Primitive_Operations (Partial_View));
15461 while Present (Elmt) loop
15462 Ent := Node (Elmt);
15463
15464 if Present (Alias (Ent))
15465 and then Ultimate_Alias (Ent) = Alias (Subp)
15466 then
15467 Append_Elmt
15468 (Ent, Primitive_Operations (Derived_Type));
15469 exit;
15470 end if;
15471
15472 Next_Elmt (Elmt);
15473 end loop;
15474
15475 -- If the interface primitive was not found in the
15476 -- partial view then this interface primitive was
15477 -- overridden. We add a derivation to activate in
15478 -- Derive_Progenitor_Subprograms the machinery to
15479 -- search for it.
15480
15481 if No (Elmt) then
15482 Derive_Interface_Subprogram
15483 (New_Subp => New_Subp,
15484 Subp => Subp,
15485 Actual_Subp => Act_Subp);
15486 end if;
15487 end if;
15488 end;
15489 else
15490 Derive_Interface_Subprogram
15491 (New_Subp => New_Subp,
15492 Subp => Subp,
15493 Actual_Subp => Act_Subp);
15494 end if;
15495
15496 -- Case 3: Common derivation
15497
15498 else
15499 Derive_Subprogram
15500 (New_Subp => New_Subp,
15501 Parent_Subp => Subp,
15502 Derived_Type => Derived_Type,
15503 Parent_Type => Parent_Base,
15504 Actual_Subp => Act_Subp);
15505 end if;
15506
15507 -- No need to update Act_Elm if we must search for the
15508 -- corresponding operation in the generic actual
15509
15510 if not Need_Search
15511 and then Present (Act_Elmt)
15512 then
15513 Next_Elmt (Act_Elmt);
15514 Act_Subp := Node (Act_Elmt);
15515 end if;
15516
15517 <<Continue>>
15518 Next_Elmt (Elmt);
15519 end loop;
15520
15521 -- Inherit additional operations from progenitors. If the derived
15522 -- type is a generic actual, there are not new primitive operations
15523 -- for the type because it has those of the actual, and therefore
15524 -- nothing needs to be done. The renamings generated above are not
15525 -- primitive operations, and their purpose is simply to make the
15526 -- proper operations visible within an instantiation.
15527
15528 if No (Generic_Actual) then
15529 Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
15530 end if;
15531 end if;
15532
15533 -- Final check: Direct descendants must have their primitives in the
15534 -- same order. We exclude from this test untagged types and instances
15535 -- of formal derived types. We skip this test if we have already
15536 -- reported serious errors in the sources.
15537
15538 pragma Assert (not Is_Tagged_Type (Derived_Type)
15539 or else Present (Generic_Actual)
15540 or else Serious_Errors_Detected > 0
15541 or else Check_Derived_Type);
15542 end Derive_Subprograms;
15543
15544 --------------------------------
15545 -- Derived_Standard_Character --
15546 --------------------------------
15547
15548 procedure Derived_Standard_Character
15549 (N : Node_Id;
15550 Parent_Type : Entity_Id;
15551 Derived_Type : Entity_Id)
15552 is
15553 Loc : constant Source_Ptr := Sloc (N);
15554 Def : constant Node_Id := Type_Definition (N);
15555 Indic : constant Node_Id := Subtype_Indication (Def);
15556 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
15557 Implicit_Base : constant Entity_Id :=
15558 Create_Itype
15559 (E_Enumeration_Type, N, Derived_Type, 'B');
15560
15561 Lo : Node_Id;
15562 Hi : Node_Id;
15563
15564 begin
15565 Discard_Node (Process_Subtype (Indic, N));
15566
15567 Set_Etype (Implicit_Base, Parent_Base);
15568 Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
15569 Set_RM_Size (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
15570
15571 Set_Is_Character_Type (Implicit_Base, True);
15572 Set_Has_Delayed_Freeze (Implicit_Base);
15573
15574 -- The bounds of the implicit base are the bounds of the parent base.
15575 -- Note that their type is the parent base.
15576
15577 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
15578 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
15579
15580 Set_Scalar_Range (Implicit_Base,
15581 Make_Range (Loc,
15582 Low_Bound => Lo,
15583 High_Bound => Hi));
15584
15585 Conditional_Delay (Derived_Type, Parent_Type);
15586
15587 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
15588 Set_Etype (Derived_Type, Implicit_Base);
15589 Set_Size_Info (Derived_Type, Parent_Type);
15590
15591 if Unknown_RM_Size (Derived_Type) then
15592 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
15593 end if;
15594
15595 Set_Is_Character_Type (Derived_Type, True);
15596
15597 if Nkind (Indic) /= N_Subtype_Indication then
15598
15599 -- If no explicit constraint, the bounds are those
15600 -- of the parent type.
15601
15602 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Type));
15603 Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
15604 Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
15605 end if;
15606
15607 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
15608
15609 -- Because the implicit base is used in the conversion of the bounds, we
15610 -- have to freeze it now. This is similar to what is done for numeric
15611 -- types, and it equally suspicious, but otherwise a non-static bound
15612 -- will have a reference to an unfrozen type, which is rejected by Gigi
15613 -- (???). This requires specific care for definition of stream
15614 -- attributes. For details, see comments at the end of
15615 -- Build_Derived_Numeric_Type.
15616
15617 Freeze_Before (N, Implicit_Base);
15618 end Derived_Standard_Character;
15619
15620 ------------------------------
15621 -- Derived_Type_Declaration --
15622 ------------------------------
15623
15624 procedure Derived_Type_Declaration
15625 (T : Entity_Id;
15626 N : Node_Id;
15627 Is_Completion : Boolean)
15628 is
15629 Parent_Type : Entity_Id;
15630
15631 function Comes_From_Generic (Typ : Entity_Id) return Boolean;
15632 -- Check whether the parent type is a generic formal, or derives
15633 -- directly or indirectly from one.
15634
15635 ------------------------
15636 -- Comes_From_Generic --
15637 ------------------------
15638
15639 function Comes_From_Generic (Typ : Entity_Id) return Boolean is
15640 begin
15641 if Is_Generic_Type (Typ) then
15642 return True;
15643
15644 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
15645 return True;
15646
15647 elsif Is_Private_Type (Typ)
15648 and then Present (Full_View (Typ))
15649 and then Is_Generic_Type (Root_Type (Full_View (Typ)))
15650 then
15651 return True;
15652
15653 elsif Is_Generic_Actual_Type (Typ) then
15654 return True;
15655
15656 else
15657 return False;
15658 end if;
15659 end Comes_From_Generic;
15660
15661 -- Local variables
15662
15663 Def : constant Node_Id := Type_Definition (N);
15664 Iface_Def : Node_Id;
15665 Indic : constant Node_Id := Subtype_Indication (Def);
15666 Extension : constant Node_Id := Record_Extension_Part (Def);
15667 Parent_Node : Node_Id;
15668 Taggd : Boolean;
15669
15670 -- Start of processing for Derived_Type_Declaration
15671
15672 begin
15673 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
15674
15675 -- Ada 2005 (AI-251): In case of interface derivation check that the
15676 -- parent is also an interface.
15677
15678 if Interface_Present (Def) then
15679 Check_SPARK_05_Restriction ("interface is not allowed", Def);
15680
15681 if not Is_Interface (Parent_Type) then
15682 Diagnose_Interface (Indic, Parent_Type);
15683
15684 else
15685 Parent_Node := Parent (Base_Type (Parent_Type));
15686 Iface_Def := Type_Definition (Parent_Node);
15687
15688 -- Ada 2005 (AI-251): Limited interfaces can only inherit from
15689 -- other limited interfaces.
15690
15691 if Limited_Present (Def) then
15692 if Limited_Present (Iface_Def) then
15693 null;
15694
15695 elsif Protected_Present (Iface_Def) then
15696 Error_Msg_NE
15697 ("descendant of& must be declared"
15698 & " as a protected interface",
15699 N, Parent_Type);
15700
15701 elsif Synchronized_Present (Iface_Def) then
15702 Error_Msg_NE
15703 ("descendant of& must be declared"
15704 & " as a synchronized interface",
15705 N, Parent_Type);
15706
15707 elsif Task_Present (Iface_Def) then
15708 Error_Msg_NE
15709 ("descendant of& must be declared as a task interface",
15710 N, Parent_Type);
15711
15712 else
15713 Error_Msg_N
15714 ("(Ada 2005) limited interface cannot "
15715 & "inherit from non-limited interface", Indic);
15716 end if;
15717
15718 -- Ada 2005 (AI-345): Non-limited interfaces can only inherit
15719 -- from non-limited or limited interfaces.
15720
15721 elsif not Protected_Present (Def)
15722 and then not Synchronized_Present (Def)
15723 and then not Task_Present (Def)
15724 then
15725 if Limited_Present (Iface_Def) then
15726 null;
15727
15728 elsif Protected_Present (Iface_Def) then
15729 Error_Msg_NE
15730 ("descendant of& must be declared"
15731 & " as a protected interface",
15732 N, Parent_Type);
15733
15734 elsif Synchronized_Present (Iface_Def) then
15735 Error_Msg_NE
15736 ("descendant of& must be declared"
15737 & " as a synchronized interface",
15738 N, Parent_Type);
15739
15740 elsif Task_Present (Iface_Def) then
15741 Error_Msg_NE
15742 ("descendant of& must be declared as a task interface",
15743 N, Parent_Type);
15744 else
15745 null;
15746 end if;
15747 end if;
15748 end if;
15749 end if;
15750
15751 if Is_Tagged_Type (Parent_Type)
15752 and then Is_Concurrent_Type (Parent_Type)
15753 and then not Is_Interface (Parent_Type)
15754 then
15755 Error_Msg_N
15756 ("parent type of a record extension cannot be "
15757 & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
15758 Set_Etype (T, Any_Type);
15759 return;
15760 end if;
15761
15762 -- Ada 2005 (AI-251): Decorate all the names in the list of ancestor
15763 -- interfaces
15764
15765 if Is_Tagged_Type (Parent_Type)
15766 and then Is_Non_Empty_List (Interface_List (Def))
15767 then
15768 declare
15769 Intf : Node_Id;
15770 T : Entity_Id;
15771
15772 begin
15773 Intf := First (Interface_List (Def));
15774 while Present (Intf) loop
15775 T := Find_Type_Of_Subtype_Indic (Intf);
15776
15777 if not Is_Interface (T) then
15778 Diagnose_Interface (Intf, T);
15779
15780 -- Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
15781 -- a limited type from having a nonlimited progenitor.
15782
15783 elsif (Limited_Present (Def)
15784 or else (not Is_Interface (Parent_Type)
15785 and then Is_Limited_Type (Parent_Type)))
15786 and then not Is_Limited_Interface (T)
15787 then
15788 Error_Msg_NE
15789 ("progenitor interface& of limited type must be limited",
15790 N, T);
15791 end if;
15792
15793 Next (Intf);
15794 end loop;
15795 end;
15796 end if;
15797
15798 if Parent_Type = Any_Type
15799 or else Etype (Parent_Type) = Any_Type
15800 or else (Is_Class_Wide_Type (Parent_Type)
15801 and then Etype (Parent_Type) = T)
15802 then
15803 -- If Parent_Type is undefined or illegal, make new type into a
15804 -- subtype of Any_Type, and set a few attributes to prevent cascaded
15805 -- errors. If this is a self-definition, emit error now.
15806
15807 if T = Parent_Type or else T = Etype (Parent_Type) then
15808 Error_Msg_N ("type cannot be used in its own definition", Indic);
15809 end if;
15810
15811 Set_Ekind (T, Ekind (Parent_Type));
15812 Set_Etype (T, Any_Type);
15813 Set_Scalar_Range (T, Scalar_Range (Any_Type));
15814
15815 if Is_Tagged_Type (T)
15816 and then Is_Record_Type (T)
15817 then
15818 Set_Direct_Primitive_Operations (T, New_Elmt_List);
15819 end if;
15820
15821 return;
15822 end if;
15823
15824 -- Ada 2005 (AI-251): The case in which the parent of the full-view is
15825 -- an interface is special because the list of interfaces in the full
15826 -- view can be given in any order. For example:
15827
15828 -- type A is interface;
15829 -- type B is interface and A;
15830 -- type D is new B with private;
15831 -- private
15832 -- type D is new A and B with null record; -- 1 --
15833
15834 -- In this case we perform the following transformation of -1-:
15835
15836 -- type D is new B and A with null record;
15837
15838 -- If the parent of the full-view covers the parent of the partial-view
15839 -- we have two possible cases:
15840
15841 -- 1) They have the same parent
15842 -- 2) The parent of the full-view implements some further interfaces
15843
15844 -- In both cases we do not need to perform the transformation. In the
15845 -- first case the source program is correct and the transformation is
15846 -- not needed; in the second case the source program does not fulfill
15847 -- the no-hidden interfaces rule (AI-396) and the error will be reported
15848 -- later.
15849
15850 -- This transformation not only simplifies the rest of the analysis of
15851 -- this type declaration but also simplifies the correct generation of
15852 -- the object layout to the expander.
15853
15854 if In_Private_Part (Current_Scope)
15855 and then Is_Interface (Parent_Type)
15856 then
15857 declare
15858 Iface : Node_Id;
15859 Partial_View : Entity_Id;
15860 Partial_View_Parent : Entity_Id;
15861 New_Iface : Node_Id;
15862
15863 begin
15864 -- Look for the associated private type declaration
15865
15866 Partial_View := First_Entity (Current_Scope);
15867 loop
15868 exit when No (Partial_View)
15869 or else (Has_Private_Declaration (Partial_View)
15870 and then Full_View (Partial_View) = T);
15871
15872 Next_Entity (Partial_View);
15873 end loop;
15874
15875 -- If the partial view was not found then the source code has
15876 -- errors and the transformation is not needed.
15877
15878 if Present (Partial_View) then
15879 Partial_View_Parent := Etype (Partial_View);
15880
15881 -- If the parent of the full-view covers the parent of the
15882 -- partial-view we have nothing else to do.
15883
15884 if Interface_Present_In_Ancestor
15885 (Parent_Type, Partial_View_Parent)
15886 then
15887 null;
15888
15889 -- Traverse the list of interfaces of the full-view to look
15890 -- for the parent of the partial-view and perform the tree
15891 -- transformation.
15892
15893 else
15894 Iface := First (Interface_List (Def));
15895 while Present (Iface) loop
15896 if Etype (Iface) = Etype (Partial_View) then
15897 Rewrite (Subtype_Indication (Def),
15898 New_Copy (Subtype_Indication
15899 (Parent (Partial_View))));
15900
15901 New_Iface :=
15902 Make_Identifier (Sloc (N), Chars (Parent_Type));
15903 Append (New_Iface, Interface_List (Def));
15904
15905 -- Analyze the transformed code
15906
15907 Derived_Type_Declaration (T, N, Is_Completion);
15908 return;
15909 end if;
15910
15911 Next (Iface);
15912 end loop;
15913 end if;
15914 end if;
15915 end;
15916 end if;
15917
15918 -- Only composite types other than array types are allowed to have
15919 -- discriminants.
15920
15921 if Present (Discriminant_Specifications (N)) then
15922 if (Is_Elementary_Type (Parent_Type)
15923 or else
15924 Is_Array_Type (Parent_Type))
15925 and then not Error_Posted (N)
15926 then
15927 Error_Msg_N
15928 ("elementary or array type cannot have discriminants",
15929 Defining_Identifier (First (Discriminant_Specifications (N))));
15930 Set_Has_Discriminants (T, False);
15931
15932 -- The type is allowed to have discriminants
15933
15934 else
15935 Check_SPARK_05_Restriction ("discriminant type is not allowed", N);
15936 end if;
15937 end if;
15938
15939 -- In Ada 83, a derived type defined in a package specification cannot
15940 -- be used for further derivation until the end of its visible part.
15941 -- Note that derivation in the private part of the package is allowed.
15942
15943 if Ada_Version = Ada_83
15944 and then Is_Derived_Type (Parent_Type)
15945 and then In_Visible_Part (Scope (Parent_Type))
15946 then
15947 if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
15948 Error_Msg_N
15949 ("(Ada 83): premature use of type for derivation", Indic);
15950 end if;
15951 end if;
15952
15953 -- Check for early use of incomplete or private type
15954
15955 if Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
15956 Error_Msg_N ("premature derivation of incomplete type", Indic);
15957 return;
15958
15959 elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
15960 and then not Comes_From_Generic (Parent_Type))
15961 or else Has_Private_Component (Parent_Type)
15962 then
15963 -- The ancestor type of a formal type can be incomplete, in which
15964 -- case only the operations of the partial view are available in the
15965 -- generic. Subsequent checks may be required when the full view is
15966 -- analyzed to verify that a derivation from a tagged type has an
15967 -- extension.
15968
15969 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
15970 null;
15971
15972 elsif No (Underlying_Type (Parent_Type))
15973 or else Has_Private_Component (Parent_Type)
15974 then
15975 Error_Msg_N
15976 ("premature derivation of derived or private type", Indic);
15977
15978 -- Flag the type itself as being in error, this prevents some
15979 -- nasty problems with subsequent uses of the malformed type.
15980
15981 Set_Error_Posted (T);
15982
15983 -- Check that within the immediate scope of an untagged partial
15984 -- view it's illegal to derive from the partial view if the
15985 -- full view is tagged. (7.3(7))
15986
15987 -- We verify that the Parent_Type is a partial view by checking
15988 -- that it is not a Full_Type_Declaration (i.e. a private type or
15989 -- private extension declaration), to distinguish a partial view
15990 -- from a derivation from a private type which also appears as
15991 -- E_Private_Type. If the parent base type is not declared in an
15992 -- enclosing scope there is no need to check.
15993
15994 elsif Present (Full_View (Parent_Type))
15995 and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
15996 and then not Is_Tagged_Type (Parent_Type)
15997 and then Is_Tagged_Type (Full_View (Parent_Type))
15998 and then In_Open_Scopes (Scope (Base_Type (Parent_Type)))
15999 then
16000 Error_Msg_N
16001 ("premature derivation from type with tagged full view",
16002 Indic);
16003 end if;
16004 end if;
16005
16006 -- Check that form of derivation is appropriate
16007
16008 Taggd := Is_Tagged_Type (Parent_Type);
16009
16010 -- Set the parent type to the class-wide type's specific type in this
16011 -- case to prevent cascading errors
16012
16013 if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
16014 Error_Msg_N ("parent type must not be a class-wide type", Indic);
16015 Set_Etype (T, Etype (Parent_Type));
16016 return;
16017 end if;
16018
16019 if Present (Extension) and then not Taggd then
16020 Error_Msg_N
16021 ("type derived from untagged type cannot have extension", Indic);
16022
16023 elsif No (Extension) and then Taggd then
16024
16025 -- If this declaration is within a private part (or body) of a
16026 -- generic instantiation then the derivation is allowed (the parent
16027 -- type can only appear tagged in this case if it's a generic actual
16028 -- type, since it would otherwise have been rejected in the analysis
16029 -- of the generic template).
16030
16031 if not Is_Generic_Actual_Type (Parent_Type)
16032 or else In_Visible_Part (Scope (Parent_Type))
16033 then
16034 if Is_Class_Wide_Type (Parent_Type) then
16035 Error_Msg_N
16036 ("parent type must not be a class-wide type", Indic);
16037
16038 -- Use specific type to prevent cascaded errors.
16039
16040 Parent_Type := Etype (Parent_Type);
16041
16042 else
16043 Error_Msg_N
16044 ("type derived from tagged type must have extension", Indic);
16045 end if;
16046 end if;
16047 end if;
16048
16049 -- AI-443: Synchronized formal derived types require a private
16050 -- extension. There is no point in checking the ancestor type or
16051 -- the progenitors since the construct is wrong to begin with.
16052
16053 if Ada_Version >= Ada_2005
16054 and then Is_Generic_Type (T)
16055 and then Present (Original_Node (N))
16056 then
16057 declare
16058 Decl : constant Node_Id := Original_Node (N);
16059
16060 begin
16061 if Nkind (Decl) = N_Formal_Type_Declaration
16062 and then Nkind (Formal_Type_Definition (Decl)) =
16063 N_Formal_Derived_Type_Definition
16064 and then Synchronized_Present (Formal_Type_Definition (Decl))
16065 and then No (Extension)
16066
16067 -- Avoid emitting a duplicate error message
16068
16069 and then not Error_Posted (Indic)
16070 then
16071 Error_Msg_N
16072 ("synchronized derived type must have extension", N);
16073 end if;
16074 end;
16075 end if;
16076
16077 if Null_Exclusion_Present (Def)
16078 and then not Is_Access_Type (Parent_Type)
16079 then
16080 Error_Msg_N ("null exclusion can only apply to an access type", N);
16081 end if;
16082
16083 -- Avoid deriving parent primitives of underlying record views
16084
16085 Build_Derived_Type (N, Parent_Type, T, Is_Completion,
16086 Derive_Subps => not Is_Underlying_Record_View (T));
16087
16088 -- AI-419: The parent type of an explicitly limited derived type must
16089 -- be a limited type or a limited interface.
16090
16091 if Limited_Present (Def) then
16092 Set_Is_Limited_Record (T);
16093
16094 if Is_Interface (T) then
16095 Set_Is_Limited_Interface (T);
16096 end if;
16097
16098 if not Is_Limited_Type (Parent_Type)
16099 and then
16100 (not Is_Interface (Parent_Type)
16101 or else not Is_Limited_Interface (Parent_Type))
16102 then
16103 -- AI05-0096: a derivation in the private part of an instance is
16104 -- legal if the generic formal is untagged limited, and the actual
16105 -- is non-limited.
16106
16107 if Is_Generic_Actual_Type (Parent_Type)
16108 and then In_Private_Part (Current_Scope)
16109 and then
16110 not Is_Tagged_Type
16111 (Generic_Parent_Type (Parent (Parent_Type)))
16112 then
16113 null;
16114
16115 else
16116 Error_Msg_NE
16117 ("parent type& of limited type must be limited",
16118 N, Parent_Type);
16119 end if;
16120 end if;
16121 end if;
16122
16123 -- In SPARK, there are no derived type definitions other than type
16124 -- extensions of tagged record types.
16125
16126 if No (Extension) then
16127 Check_SPARK_05_Restriction
16128 ("derived type is not allowed", Original_Node (N));
16129 end if;
16130 end Derived_Type_Declaration;
16131
16132 ------------------------
16133 -- Diagnose_Interface --
16134 ------------------------
16135
16136 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id) is
16137 begin
16138 if not Is_Interface (E) and then E /= Any_Type then
16139 Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
16140 end if;
16141 end Diagnose_Interface;
16142
16143 ----------------------------------
16144 -- Enumeration_Type_Declaration --
16145 ----------------------------------
16146
16147 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
16148 Ev : Uint;
16149 L : Node_Id;
16150 R_Node : Node_Id;
16151 B_Node : Node_Id;
16152
16153 begin
16154 -- Create identifier node representing lower bound
16155
16156 B_Node := New_Node (N_Identifier, Sloc (Def));
16157 L := First (Literals (Def));
16158 Set_Chars (B_Node, Chars (L));
16159 Set_Entity (B_Node, L);
16160 Set_Etype (B_Node, T);
16161 Set_Is_Static_Expression (B_Node, True);
16162
16163 R_Node := New_Node (N_Range, Sloc (Def));
16164 Set_Low_Bound (R_Node, B_Node);
16165
16166 Set_Ekind (T, E_Enumeration_Type);
16167 Set_First_Literal (T, L);
16168 Set_Etype (T, T);
16169 Set_Is_Constrained (T);
16170
16171 Ev := Uint_0;
16172
16173 -- Loop through literals of enumeration type setting pos and rep values
16174 -- except that if the Ekind is already set, then it means the literal
16175 -- was already constructed (case of a derived type declaration and we
16176 -- should not disturb the Pos and Rep values.
16177
16178 while Present (L) loop
16179 if Ekind (L) /= E_Enumeration_Literal then
16180 Set_Ekind (L, E_Enumeration_Literal);
16181 Set_Enumeration_Pos (L, Ev);
16182 Set_Enumeration_Rep (L, Ev);
16183 Set_Is_Known_Valid (L, True);
16184 end if;
16185
16186 Set_Etype (L, T);
16187 New_Overloaded_Entity (L);
16188 Generate_Definition (L);
16189 Set_Convention (L, Convention_Intrinsic);
16190
16191 -- Case of character literal
16192
16193 if Nkind (L) = N_Defining_Character_Literal then
16194 Set_Is_Character_Type (T, True);
16195
16196 -- Check violation of No_Wide_Characters
16197
16198 if Restriction_Check_Required (No_Wide_Characters) then
16199 Get_Name_String (Chars (L));
16200
16201 if Name_Len >= 3 and then Name_Buffer (1 .. 2) = "QW" then
16202 Check_Restriction (No_Wide_Characters, L);
16203 end if;
16204 end if;
16205 end if;
16206
16207 Ev := Ev + 1;
16208 Next (L);
16209 end loop;
16210
16211 -- Now create a node representing upper bound
16212
16213 B_Node := New_Node (N_Identifier, Sloc (Def));
16214 Set_Chars (B_Node, Chars (Last (Literals (Def))));
16215 Set_Entity (B_Node, Last (Literals (Def)));
16216 Set_Etype (B_Node, T);
16217 Set_Is_Static_Expression (B_Node, True);
16218
16219 Set_High_Bound (R_Node, B_Node);
16220
16221 -- Initialize various fields of the type. Some of this information
16222 -- may be overwritten later through rep.clauses.
16223
16224 Set_Scalar_Range (T, R_Node);
16225 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
16226 Set_Enum_Esize (T);
16227 Set_Enum_Pos_To_Rep (T, Empty);
16228
16229 -- Set Discard_Names if configuration pragma set, or if there is
16230 -- a parameterless pragma in the current declarative region
16231
16232 if Global_Discard_Names or else Discard_Names (Scope (T)) then
16233 Set_Discard_Names (T);
16234 end if;
16235
16236 -- Process end label if there is one
16237
16238 if Present (Def) then
16239 Process_End_Label (Def, 'e', T);
16240 end if;
16241 end Enumeration_Type_Declaration;
16242
16243 ---------------------------------
16244 -- Expand_To_Stored_Constraint --
16245 ---------------------------------
16246
16247 function Expand_To_Stored_Constraint
16248 (Typ : Entity_Id;
16249 Constraint : Elist_Id) return Elist_Id
16250 is
16251 Explicitly_Discriminated_Type : Entity_Id;
16252 Expansion : Elist_Id;
16253 Discriminant : Entity_Id;
16254
16255 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
16256 -- Find the nearest type that actually specifies discriminants
16257
16258 ---------------------------------
16259 -- Type_With_Explicit_Discrims --
16260 ---------------------------------
16261
16262 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
16263 Typ : constant E := Base_Type (Id);
16264
16265 begin
16266 if Ekind (Typ) in Incomplete_Or_Private_Kind then
16267 if Present (Full_View (Typ)) then
16268 return Type_With_Explicit_Discrims (Full_View (Typ));
16269 end if;
16270
16271 else
16272 if Has_Discriminants (Typ) then
16273 return Typ;
16274 end if;
16275 end if;
16276
16277 if Etype (Typ) = Typ then
16278 return Empty;
16279 elsif Has_Discriminants (Typ) then
16280 return Typ;
16281 else
16282 return Type_With_Explicit_Discrims (Etype (Typ));
16283 end if;
16284
16285 end Type_With_Explicit_Discrims;
16286
16287 -- Start of processing for Expand_To_Stored_Constraint
16288
16289 begin
16290 if No (Constraint) or else Is_Empty_Elmt_List (Constraint) then
16291 return No_Elist;
16292 end if;
16293
16294 Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
16295
16296 if No (Explicitly_Discriminated_Type) then
16297 return No_Elist;
16298 end if;
16299
16300 Expansion := New_Elmt_List;
16301
16302 Discriminant :=
16303 First_Stored_Discriminant (Explicitly_Discriminated_Type);
16304 while Present (Discriminant) loop
16305 Append_Elmt
16306 (Get_Discriminant_Value
16307 (Discriminant, Explicitly_Discriminated_Type, Constraint),
16308 To => Expansion);
16309 Next_Stored_Discriminant (Discriminant);
16310 end loop;
16311
16312 return Expansion;
16313 end Expand_To_Stored_Constraint;
16314
16315 ---------------------------
16316 -- Find_Hidden_Interface --
16317 ---------------------------
16318
16319 function Find_Hidden_Interface
16320 (Src : Elist_Id;
16321 Dest : Elist_Id) return Entity_Id
16322 is
16323 Iface : Entity_Id;
16324 Iface_Elmt : Elmt_Id;
16325
16326 begin
16327 if Present (Src) and then Present (Dest) then
16328 Iface_Elmt := First_Elmt (Src);
16329 while Present (Iface_Elmt) loop
16330 Iface := Node (Iface_Elmt);
16331
16332 if Is_Interface (Iface)
16333 and then not Contain_Interface (Iface, Dest)
16334 then
16335 return Iface;
16336 end if;
16337
16338 Next_Elmt (Iface_Elmt);
16339 end loop;
16340 end if;
16341
16342 return Empty;
16343 end Find_Hidden_Interface;
16344
16345 --------------------
16346 -- Find_Type_Name --
16347 --------------------
16348
16349 function Find_Type_Name (N : Node_Id) return Entity_Id is
16350 Id : constant Entity_Id := Defining_Identifier (N);
16351 Prev : Entity_Id;
16352 New_Id : Entity_Id;
16353 Prev_Par : Node_Id;
16354
16355 procedure Check_Duplicate_Aspects;
16356 -- Check that aspects specified in a completion have not been specified
16357 -- already in the partial view. Type_Invariant and others can be
16358 -- specified on either view but never on both.
16359
16360 procedure Tag_Mismatch;
16361 -- Diagnose a tagged partial view whose full view is untagged.
16362 -- We post the message on the full view, with a reference to
16363 -- the previous partial view. The partial view can be private
16364 -- or incomplete, and these are handled in a different manner,
16365 -- so we determine the position of the error message from the
16366 -- respective slocs of both.
16367
16368 -----------------------------
16369 -- Check_Duplicate_Aspects --
16370 -----------------------------
16371 procedure Check_Duplicate_Aspects is
16372 Prev_Aspects : constant List_Id := Aspect_Specifications (Prev_Par);
16373 Full_Aspects : constant List_Id := Aspect_Specifications (N);
16374 F_Spec, P_Spec : Node_Id;
16375
16376 begin
16377 if Present (Prev_Aspects) and then Present (Full_Aspects) then
16378 F_Spec := First (Full_Aspects);
16379 while Present (F_Spec) loop
16380 P_Spec := First (Prev_Aspects);
16381 while Present (P_Spec) loop
16382 if Chars (Identifier (P_Spec)) = Chars (Identifier (F_Spec))
16383 then
16384 Error_Msg_N
16385 ("aspect already specified in private declaration",
16386 F_Spec);
16387 Remove (F_Spec);
16388 return;
16389 end if;
16390
16391 Next (P_Spec);
16392 end loop;
16393
16394 Next (F_Spec);
16395 end loop;
16396 end if;
16397 end Check_Duplicate_Aspects;
16398
16399 ------------------
16400 -- Tag_Mismatch --
16401 ------------------
16402
16403 procedure Tag_Mismatch is
16404 begin
16405 if Sloc (Prev) < Sloc (Id) then
16406 if Ada_Version >= Ada_2012
16407 and then Nkind (N) = N_Private_Type_Declaration
16408 then
16409 Error_Msg_NE
16410 ("declaration of private } must be a tagged type ", Id, Prev);
16411 else
16412 Error_Msg_NE
16413 ("full declaration of } must be a tagged type ", Id, Prev);
16414 end if;
16415
16416 else
16417 if Ada_Version >= Ada_2012
16418 and then Nkind (N) = N_Private_Type_Declaration
16419 then
16420 Error_Msg_NE
16421 ("declaration of private } must be a tagged type ", Prev, Id);
16422 else
16423 Error_Msg_NE
16424 ("full declaration of } must be a tagged type ", Prev, Id);
16425 end if;
16426 end if;
16427 end Tag_Mismatch;
16428
16429 -- Start of processing for Find_Type_Name
16430
16431 begin
16432 -- Find incomplete declaration, if one was given
16433
16434 Prev := Current_Entity_In_Scope (Id);
16435
16436 -- New type declaration
16437
16438 if No (Prev) then
16439 Enter_Name (Id);
16440 return Id;
16441
16442 -- Previous declaration exists
16443
16444 else
16445 Prev_Par := Parent (Prev);
16446
16447 -- Error if not incomplete/private case except if previous
16448 -- declaration is implicit, etc. Enter_Name will emit error if
16449 -- appropriate.
16450
16451 if not Is_Incomplete_Or_Private_Type (Prev) then
16452 Enter_Name (Id);
16453 New_Id := Id;
16454
16455 -- Check invalid completion of private or incomplete type
16456
16457 elsif not Nkind_In (N, N_Full_Type_Declaration,
16458 N_Task_Type_Declaration,
16459 N_Protected_Type_Declaration)
16460 and then
16461 (Ada_Version < Ada_2012
16462 or else not Is_Incomplete_Type (Prev)
16463 or else not Nkind_In (N, N_Private_Type_Declaration,
16464 N_Private_Extension_Declaration))
16465 then
16466 -- Completion must be a full type declarations (RM 7.3(4))
16467
16468 Error_Msg_Sloc := Sloc (Prev);
16469 Error_Msg_NE ("invalid completion of }", Id, Prev);
16470
16471 -- Set scope of Id to avoid cascaded errors. Entity is never
16472 -- examined again, except when saving globals in generics.
16473
16474 Set_Scope (Id, Current_Scope);
16475 New_Id := Id;
16476
16477 -- If this is a repeated incomplete declaration, no further
16478 -- checks are possible.
16479
16480 if Nkind (N) = N_Incomplete_Type_Declaration then
16481 return Prev;
16482 end if;
16483
16484 -- Case of full declaration of incomplete type
16485
16486 elsif Ekind (Prev) = E_Incomplete_Type
16487 and then (Ada_Version < Ada_2012
16488 or else No (Full_View (Prev))
16489 or else not Is_Private_Type (Full_View (Prev)))
16490 then
16491 -- Indicate that the incomplete declaration has a matching full
16492 -- declaration. The defining occurrence of the incomplete
16493 -- declaration remains the visible one, and the procedure
16494 -- Get_Full_View dereferences it whenever the type is used.
16495
16496 if Present (Full_View (Prev)) then
16497 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
16498 end if;
16499
16500 Set_Full_View (Prev, Id);
16501 Append_Entity (Id, Current_Scope);
16502 Set_Is_Public (Id, Is_Public (Prev));
16503 Set_Is_Internal (Id);
16504 New_Id := Prev;
16505
16506 -- If the incomplete view is tagged, a class_wide type has been
16507 -- created already. Use it for the private type as well, in order
16508 -- to prevent multiple incompatible class-wide types that may be
16509 -- created for self-referential anonymous access components.
16510
16511 if Is_Tagged_Type (Prev)
16512 and then Present (Class_Wide_Type (Prev))
16513 then
16514 Set_Ekind (Id, Ekind (Prev)); -- will be reset later
16515 Set_Class_Wide_Type (Id, Class_Wide_Type (Prev));
16516
16517 -- The type of the classwide type is the current Id. Previously
16518 -- this was not done for private declarations because of order-
16519 -- of elaboration issues in the back-end, but gigi now handles
16520 -- this properly.
16521
16522 Set_Etype (Class_Wide_Type (Id), Id);
16523 end if;
16524
16525 -- Case of full declaration of private type
16526
16527 else
16528 -- If the private type was a completion of an incomplete type then
16529 -- update Prev to reference the private type
16530
16531 if Ada_Version >= Ada_2012
16532 and then Ekind (Prev) = E_Incomplete_Type
16533 and then Present (Full_View (Prev))
16534 and then Is_Private_Type (Full_View (Prev))
16535 then
16536 Prev := Full_View (Prev);
16537 Prev_Par := Parent (Prev);
16538 end if;
16539
16540 if Nkind (N) = N_Full_Type_Declaration
16541 and then Nkind_In
16542 (Type_Definition (N), N_Record_Definition,
16543 N_Derived_Type_Definition)
16544 and then Interface_Present (Type_Definition (N))
16545 then
16546 Error_Msg_N
16547 ("completion of private type cannot be an interface", N);
16548 end if;
16549
16550 if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
16551 if Etype (Prev) /= Prev then
16552
16553 -- Prev is a private subtype or a derived type, and needs
16554 -- no completion.
16555
16556 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
16557 New_Id := Id;
16558
16559 elsif Ekind (Prev) = E_Private_Type
16560 and then Nkind_In (N, N_Task_Type_Declaration,
16561 N_Protected_Type_Declaration)
16562 then
16563 Error_Msg_N
16564 ("completion of nonlimited type cannot be limited", N);
16565
16566 elsif Ekind (Prev) = E_Record_Type_With_Private
16567 and then Nkind_In (N, N_Task_Type_Declaration,
16568 N_Protected_Type_Declaration)
16569 then
16570 if not Is_Limited_Record (Prev) then
16571 Error_Msg_N
16572 ("completion of nonlimited type cannot be limited", N);
16573
16574 elsif No (Interface_List (N)) then
16575 Error_Msg_N
16576 ("completion of tagged private type must be tagged",
16577 N);
16578 end if;
16579 end if;
16580
16581 -- Ada 2005 (AI-251): Private extension declaration of a task
16582 -- type or a protected type. This case arises when covering
16583 -- interface types.
16584
16585 elsif Nkind_In (N, N_Task_Type_Declaration,
16586 N_Protected_Type_Declaration)
16587 then
16588 null;
16589
16590 elsif Nkind (N) /= N_Full_Type_Declaration
16591 or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
16592 then
16593 Error_Msg_N
16594 ("full view of private extension must be an extension", N);
16595
16596 elsif not (Abstract_Present (Parent (Prev)))
16597 and then Abstract_Present (Type_Definition (N))
16598 then
16599 Error_Msg_N
16600 ("full view of non-abstract extension cannot be abstract", N);
16601 end if;
16602
16603 if not In_Private_Part (Current_Scope) then
16604 Error_Msg_N
16605 ("declaration of full view must appear in private part", N);
16606 end if;
16607
16608 if Ada_Version >= Ada_2012 then
16609 Check_Duplicate_Aspects;
16610 end if;
16611
16612 Copy_And_Swap (Prev, Id);
16613 Set_Has_Private_Declaration (Prev);
16614 Set_Has_Private_Declaration (Id);
16615
16616 -- AI12-0133: Indicate whether we have a partial view with
16617 -- unknown discriminants, in which case initialization of objects
16618 -- of the type do not receive an invariant check.
16619
16620 Set_Partial_View_Has_Unknown_Discr
16621 (Prev, Has_Unknown_Discriminants (Id));
16622
16623 -- Preserve aspect and iterator flags that may have been set on
16624 -- the partial view.
16625
16626 Set_Has_Delayed_Aspects (Prev, Has_Delayed_Aspects (Id));
16627 Set_Has_Implicit_Dereference (Prev, Has_Implicit_Dereference (Id));
16628
16629 -- If no error, propagate freeze_node from private to full view.
16630 -- It may have been generated for an early operational item.
16631
16632 if Present (Freeze_Node (Id))
16633 and then Serious_Errors_Detected = 0
16634 and then No (Full_View (Id))
16635 then
16636 Set_Freeze_Node (Prev, Freeze_Node (Id));
16637 Set_Freeze_Node (Id, Empty);
16638 Set_First_Rep_Item (Prev, First_Rep_Item (Id));
16639 end if;
16640
16641 Set_Full_View (Id, Prev);
16642 New_Id := Prev;
16643 end if;
16644
16645 -- Verify that full declaration conforms to partial one
16646
16647 if Is_Incomplete_Or_Private_Type (Prev)
16648 and then Present (Discriminant_Specifications (Prev_Par))
16649 then
16650 if Present (Discriminant_Specifications (N)) then
16651 if Ekind (Prev) = E_Incomplete_Type then
16652 Check_Discriminant_Conformance (N, Prev, Prev);
16653 else
16654 Check_Discriminant_Conformance (N, Prev, Id);
16655 end if;
16656
16657 else
16658 Error_Msg_N
16659 ("missing discriminants in full type declaration", N);
16660
16661 -- To avoid cascaded errors on subsequent use, share the
16662 -- discriminants of the partial view.
16663
16664 Set_Discriminant_Specifications (N,
16665 Discriminant_Specifications (Prev_Par));
16666 end if;
16667 end if;
16668
16669 -- A prior untagged partial view can have an associated class-wide
16670 -- type due to use of the class attribute, and in this case the full
16671 -- type must also be tagged. This Ada 95 usage is deprecated in favor
16672 -- of incomplete tagged declarations, but we check for it.
16673
16674 if Is_Type (Prev)
16675 and then (Is_Tagged_Type (Prev)
16676 or else Present (Class_Wide_Type (Prev)))
16677 then
16678 -- Ada 2012 (AI05-0162): A private type may be the completion of
16679 -- an incomplete type.
16680
16681 if Ada_Version >= Ada_2012
16682 and then Is_Incomplete_Type (Prev)
16683 and then Nkind_In (N, N_Private_Type_Declaration,
16684 N_Private_Extension_Declaration)
16685 then
16686 -- No need to check private extensions since they are tagged
16687
16688 if Nkind (N) = N_Private_Type_Declaration
16689 and then not Tagged_Present (N)
16690 then
16691 Tag_Mismatch;
16692 end if;
16693
16694 -- The full declaration is either a tagged type (including
16695 -- a synchronized type that implements interfaces) or a
16696 -- type extension, otherwise this is an error.
16697
16698 elsif Nkind_In (N, N_Task_Type_Declaration,
16699 N_Protected_Type_Declaration)
16700 then
16701 if No (Interface_List (N)) and then not Error_Posted (N) then
16702 Tag_Mismatch;
16703 end if;
16704
16705 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
16706
16707 -- Indicate that the previous declaration (tagged incomplete
16708 -- or private declaration) requires the same on the full one.
16709
16710 if not Tagged_Present (Type_Definition (N)) then
16711 Tag_Mismatch;
16712 Set_Is_Tagged_Type (Id);
16713 end if;
16714
16715 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
16716 if No (Record_Extension_Part (Type_Definition (N))) then
16717 Error_Msg_NE
16718 ("full declaration of } must be a record extension",
16719 Prev, Id);
16720
16721 -- Set some attributes to produce a usable full view
16722
16723 Set_Is_Tagged_Type (Id);
16724 end if;
16725
16726 else
16727 Tag_Mismatch;
16728 end if;
16729 end if;
16730
16731 if Present (Prev)
16732 and then Nkind (Parent (Prev)) = N_Incomplete_Type_Declaration
16733 and then Present (Premature_Use (Parent (Prev)))
16734 then
16735 Error_Msg_Sloc := Sloc (N);
16736 Error_Msg_N
16737 ("\full declaration #", Premature_Use (Parent (Prev)));
16738 end if;
16739
16740 return New_Id;
16741 end if;
16742 end Find_Type_Name;
16743
16744 -------------------------
16745 -- Find_Type_Of_Object --
16746 -------------------------
16747
16748 function Find_Type_Of_Object
16749 (Obj_Def : Node_Id;
16750 Related_Nod : Node_Id) return Entity_Id
16751 is
16752 Def_Kind : constant Node_Kind := Nkind (Obj_Def);
16753 P : Node_Id := Parent (Obj_Def);
16754 T : Entity_Id;
16755 Nam : Name_Id;
16756
16757 begin
16758 -- If the parent is a component_definition node we climb to the
16759 -- component_declaration node
16760
16761 if Nkind (P) = N_Component_Definition then
16762 P := Parent (P);
16763 end if;
16764
16765 -- Case of an anonymous array subtype
16766
16767 if Nkind_In (Def_Kind, N_Constrained_Array_Definition,
16768 N_Unconstrained_Array_Definition)
16769 then
16770 T := Empty;
16771 Array_Type_Declaration (T, Obj_Def);
16772
16773 -- Create an explicit subtype whenever possible
16774
16775 elsif Nkind (P) /= N_Component_Declaration
16776 and then Def_Kind = N_Subtype_Indication
16777 then
16778 -- Base name of subtype on object name, which will be unique in
16779 -- the current scope.
16780
16781 -- If this is a duplicate declaration, return base type, to avoid
16782 -- generating duplicate anonymous types.
16783
16784 if Error_Posted (P) then
16785 Analyze (Subtype_Mark (Obj_Def));
16786 return Entity (Subtype_Mark (Obj_Def));
16787 end if;
16788
16789 Nam :=
16790 New_External_Name
16791 (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
16792
16793 T := Make_Defining_Identifier (Sloc (P), Nam);
16794
16795 Insert_Action (Obj_Def,
16796 Make_Subtype_Declaration (Sloc (P),
16797 Defining_Identifier => T,
16798 Subtype_Indication => Relocate_Node (Obj_Def)));
16799
16800 -- This subtype may need freezing, and this will not be done
16801 -- automatically if the object declaration is not in declarative
16802 -- part. Since this is an object declaration, the type cannot always
16803 -- be frozen here. Deferred constants do not freeze their type
16804 -- (which often enough will be private).
16805
16806 if Nkind (P) = N_Object_Declaration
16807 and then Constant_Present (P)
16808 and then No (Expression (P))
16809 then
16810 null;
16811
16812 -- Here we freeze the base type of object type to catch premature use
16813 -- of discriminated private type without a full view.
16814
16815 else
16816 Insert_Actions (Obj_Def, Freeze_Entity (Base_Type (T), P));
16817 end if;
16818
16819 -- Ada 2005 AI-406: the object definition in an object declaration
16820 -- can be an access definition.
16821
16822 elsif Def_Kind = N_Access_Definition then
16823 T := Access_Definition (Related_Nod, Obj_Def);
16824
16825 Set_Is_Local_Anonymous_Access
16826 (T,
16827 V => (Ada_Version < Ada_2012)
16828 or else (Nkind (P) /= N_Object_Declaration)
16829 or else Is_Library_Level_Entity (Defining_Identifier (P)));
16830
16831 -- Otherwise, the object definition is just a subtype_mark
16832
16833 else
16834 T := Process_Subtype (Obj_Def, Related_Nod);
16835
16836 -- If expansion is disabled an object definition that is an aggregate
16837 -- will not get expanded and may lead to scoping problems in the back
16838 -- end, if the object is referenced in an inner scope. In that case
16839 -- create an itype reference for the object definition now. This
16840 -- may be redundant in some cases, but harmless.
16841
16842 if Is_Itype (T)
16843 and then Nkind (Related_Nod) = N_Object_Declaration
16844 and then ASIS_Mode
16845 then
16846 Build_Itype_Reference (T, Related_Nod);
16847 end if;
16848 end if;
16849
16850 return T;
16851 end Find_Type_Of_Object;
16852
16853 --------------------------------
16854 -- Find_Type_Of_Subtype_Indic --
16855 --------------------------------
16856
16857 function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
16858 Typ : Entity_Id;
16859
16860 begin
16861 -- Case of subtype mark with a constraint
16862
16863 if Nkind (S) = N_Subtype_Indication then
16864 Find_Type (Subtype_Mark (S));
16865 Typ := Entity (Subtype_Mark (S));
16866
16867 if not
16868 Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
16869 then
16870 Error_Msg_N
16871 ("incorrect constraint for this kind of type", Constraint (S));
16872 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
16873 end if;
16874
16875 -- Otherwise we have a subtype mark without a constraint
16876
16877 elsif Error_Posted (S) then
16878 Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
16879 return Any_Type;
16880
16881 else
16882 Find_Type (S);
16883 Typ := Entity (S);
16884 end if;
16885
16886 -- Check No_Wide_Characters restriction
16887
16888 Check_Wide_Character_Restriction (Typ, S);
16889
16890 return Typ;
16891 end Find_Type_Of_Subtype_Indic;
16892
16893 -------------------------------------
16894 -- Floating_Point_Type_Declaration --
16895 -------------------------------------
16896
16897 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
16898 Digs : constant Node_Id := Digits_Expression (Def);
16899 Max_Digs_Val : constant Uint := Digits_Value (Standard_Long_Long_Float);
16900 Digs_Val : Uint;
16901 Base_Typ : Entity_Id;
16902 Implicit_Base : Entity_Id;
16903 Bound : Node_Id;
16904
16905 function Can_Derive_From (E : Entity_Id) return Boolean;
16906 -- Find if given digits value, and possibly a specified range, allows
16907 -- derivation from specified type
16908
16909 function Find_Base_Type return Entity_Id;
16910 -- Find a predefined base type that Def can derive from, or generate
16911 -- an error and substitute Long_Long_Float if none exists.
16912
16913 ---------------------
16914 -- Can_Derive_From --
16915 ---------------------
16916
16917 function Can_Derive_From (E : Entity_Id) return Boolean is
16918 Spec : constant Entity_Id := Real_Range_Specification (Def);
16919
16920 begin
16921 -- Check specified "digits" constraint
16922
16923 if Digs_Val > Digits_Value (E) then
16924 return False;
16925 end if;
16926
16927 -- Check for matching range, if specified
16928
16929 if Present (Spec) then
16930 if Expr_Value_R (Type_Low_Bound (E)) >
16931 Expr_Value_R (Low_Bound (Spec))
16932 then
16933 return False;
16934 end if;
16935
16936 if Expr_Value_R (Type_High_Bound (E)) <
16937 Expr_Value_R (High_Bound (Spec))
16938 then
16939 return False;
16940 end if;
16941 end if;
16942
16943 return True;
16944 end Can_Derive_From;
16945
16946 --------------------
16947 -- Find_Base_Type --
16948 --------------------
16949
16950 function Find_Base_Type return Entity_Id is
16951 Choice : Elmt_Id := First_Elmt (Predefined_Float_Types);
16952
16953 begin
16954 -- Iterate over the predefined types in order, returning the first
16955 -- one that Def can derive from.
16956
16957 while Present (Choice) loop
16958 if Can_Derive_From (Node (Choice)) then
16959 return Node (Choice);
16960 end if;
16961
16962 Next_Elmt (Choice);
16963 end loop;
16964
16965 -- If we can't derive from any existing type, use Long_Long_Float
16966 -- and give appropriate message explaining the problem.
16967
16968 if Digs_Val > Max_Digs_Val then
16969 -- It might be the case that there is a type with the requested
16970 -- range, just not the combination of digits and range.
16971
16972 Error_Msg_N
16973 ("no predefined type has requested range and precision",
16974 Real_Range_Specification (Def));
16975
16976 else
16977 Error_Msg_N
16978 ("range too large for any predefined type",
16979 Real_Range_Specification (Def));
16980 end if;
16981
16982 return Standard_Long_Long_Float;
16983 end Find_Base_Type;
16984
16985 -- Start of processing for Floating_Point_Type_Declaration
16986
16987 begin
16988 Check_Restriction (No_Floating_Point, Def);
16989
16990 -- Create an implicit base type
16991
16992 Implicit_Base :=
16993 Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
16994
16995 -- Analyze and verify digits value
16996
16997 Analyze_And_Resolve (Digs, Any_Integer);
16998 Check_Digits_Expression (Digs);
16999 Digs_Val := Expr_Value (Digs);
17000
17001 -- Process possible range spec and find correct type to derive from
17002
17003 Process_Real_Range_Specification (Def);
17004
17005 -- Check that requested number of digits is not too high.
17006
17007 if Digs_Val > Max_Digs_Val then
17008
17009 -- The check for Max_Base_Digits may be somewhat expensive, as it
17010 -- requires reading System, so only do it when necessary.
17011
17012 declare
17013 Max_Base_Digits : constant Uint :=
17014 Expr_Value
17015 (Expression
17016 (Parent (RTE (RE_Max_Base_Digits))));
17017
17018 begin
17019 if Digs_Val > Max_Base_Digits then
17020 Error_Msg_Uint_1 := Max_Base_Digits;
17021 Error_Msg_N ("digits value out of range, maximum is ^", Digs);
17022
17023 elsif No (Real_Range_Specification (Def)) then
17024 Error_Msg_Uint_1 := Max_Digs_Val;
17025 Error_Msg_N ("types with more than ^ digits need range spec "
17026 & "(RM 3.5.7(6))", Digs);
17027 end if;
17028 end;
17029 end if;
17030
17031 -- Find a suitable type to derive from or complain and use a substitute
17032
17033 Base_Typ := Find_Base_Type;
17034
17035 -- If there are bounds given in the declaration use them as the bounds
17036 -- of the type, otherwise use the bounds of the predefined base type
17037 -- that was chosen based on the Digits value.
17038
17039 if Present (Real_Range_Specification (Def)) then
17040 Set_Scalar_Range (T, Real_Range_Specification (Def));
17041 Set_Is_Constrained (T);
17042
17043 -- The bounds of this range must be converted to machine numbers
17044 -- in accordance with RM 4.9(38).
17045
17046 Bound := Type_Low_Bound (T);
17047
17048 if Nkind (Bound) = N_Real_Literal then
17049 Set_Realval
17050 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
17051 Set_Is_Machine_Number (Bound);
17052 end if;
17053
17054 Bound := Type_High_Bound (T);
17055
17056 if Nkind (Bound) = N_Real_Literal then
17057 Set_Realval
17058 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
17059 Set_Is_Machine_Number (Bound);
17060 end if;
17061
17062 else
17063 Set_Scalar_Range (T, Scalar_Range (Base_Typ));
17064 end if;
17065
17066 -- Complete definition of implicit base and declared first subtype. The
17067 -- inheritance of the rep item chain ensures that SPARK-related pragmas
17068 -- are not clobbered when the floating point type acts as a full view of
17069 -- a private type.
17070
17071 Set_Etype (Implicit_Base, Base_Typ);
17072 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
17073 Set_Size_Info (Implicit_Base, Base_Typ);
17074 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
17075 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
17076 Set_Digits_Value (Implicit_Base, Digits_Value (Base_Typ));
17077 Set_Float_Rep (Implicit_Base, Float_Rep (Base_Typ));
17078
17079 Set_Ekind (T, E_Floating_Point_Subtype);
17080 Set_Etype (T, Implicit_Base);
17081 Set_Size_Info (T, Implicit_Base);
17082 Set_RM_Size (T, RM_Size (Implicit_Base));
17083 Inherit_Rep_Item_Chain (T, Implicit_Base);
17084 Set_Digits_Value (T, Digs_Val);
17085 end Floating_Point_Type_Declaration;
17086
17087 ----------------------------
17088 -- Get_Discriminant_Value --
17089 ----------------------------
17090
17091 -- This is the situation:
17092
17093 -- There is a non-derived type
17094
17095 -- type T0 (Dx, Dy, Dz...)
17096
17097 -- There are zero or more levels of derivation, with each derivation
17098 -- either purely inheriting the discriminants, or defining its own.
17099
17100 -- type Ti is new Ti-1
17101 -- or
17102 -- type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
17103 -- or
17104 -- subtype Ti is ...
17105
17106 -- The subtype issue is avoided by the use of Original_Record_Component,
17107 -- and the fact that derived subtypes also derive the constraints.
17108
17109 -- This chain leads back from
17110
17111 -- Typ_For_Constraint
17112
17113 -- Typ_For_Constraint has discriminants, and the value for each
17114 -- discriminant is given by its corresponding Elmt of Constraints.
17115
17116 -- Discriminant is some discriminant in this hierarchy
17117
17118 -- We need to return its value
17119
17120 -- We do this by recursively searching each level, and looking for
17121 -- Discriminant. Once we get to the bottom, we start backing up
17122 -- returning the value for it which may in turn be a discriminant
17123 -- further up, so on the backup we continue the substitution.
17124
17125 function Get_Discriminant_Value
17126 (Discriminant : Entity_Id;
17127 Typ_For_Constraint : Entity_Id;
17128 Constraint : Elist_Id) return Node_Id
17129 is
17130 function Root_Corresponding_Discriminant
17131 (Discr : Entity_Id) return Entity_Id;
17132 -- Given a discriminant, traverse the chain of inherited discriminants
17133 -- and return the topmost discriminant.
17134
17135 function Search_Derivation_Levels
17136 (Ti : Entity_Id;
17137 Discrim_Values : Elist_Id;
17138 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
17139 -- This is the routine that performs the recursive search of levels
17140 -- as described above.
17141
17142 -------------------------------------
17143 -- Root_Corresponding_Discriminant --
17144 -------------------------------------
17145
17146 function Root_Corresponding_Discriminant
17147 (Discr : Entity_Id) return Entity_Id
17148 is
17149 D : Entity_Id;
17150
17151 begin
17152 D := Discr;
17153 while Present (Corresponding_Discriminant (D)) loop
17154 D := Corresponding_Discriminant (D);
17155 end loop;
17156
17157 return D;
17158 end Root_Corresponding_Discriminant;
17159
17160 ------------------------------
17161 -- Search_Derivation_Levels --
17162 ------------------------------
17163
17164 function Search_Derivation_Levels
17165 (Ti : Entity_Id;
17166 Discrim_Values : Elist_Id;
17167 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
17168 is
17169 Assoc : Elmt_Id;
17170 Disc : Entity_Id;
17171 Result : Node_Or_Entity_Id;
17172 Result_Entity : Node_Id;
17173
17174 begin
17175 -- If inappropriate type, return Error, this happens only in
17176 -- cascaded error situations, and we want to avoid a blow up.
17177
17178 if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
17179 return Error;
17180 end if;
17181
17182 -- Look deeper if possible. Use Stored_Constraints only for
17183 -- untagged types. For tagged types use the given constraint.
17184 -- This asymmetry needs explanation???
17185
17186 if not Stored_Discrim_Values
17187 and then Present (Stored_Constraint (Ti))
17188 and then not Is_Tagged_Type (Ti)
17189 then
17190 Result :=
17191 Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
17192 else
17193 declare
17194 Td : constant Entity_Id := Etype (Ti);
17195
17196 begin
17197 if Td = Ti then
17198 Result := Discriminant;
17199
17200 else
17201 if Present (Stored_Constraint (Ti)) then
17202 Result :=
17203 Search_Derivation_Levels
17204 (Td, Stored_Constraint (Ti), True);
17205 else
17206 Result :=
17207 Search_Derivation_Levels
17208 (Td, Discrim_Values, Stored_Discrim_Values);
17209 end if;
17210 end if;
17211 end;
17212 end if;
17213
17214 -- Extra underlying places to search, if not found above. For
17215 -- concurrent types, the relevant discriminant appears in the
17216 -- corresponding record. For a type derived from a private type
17217 -- without discriminant, the full view inherits the discriminants
17218 -- of the full view of the parent.
17219
17220 if Result = Discriminant then
17221 if Is_Concurrent_Type (Ti)
17222 and then Present (Corresponding_Record_Type (Ti))
17223 then
17224 Result :=
17225 Search_Derivation_Levels (
17226 Corresponding_Record_Type (Ti),
17227 Discrim_Values,
17228 Stored_Discrim_Values);
17229
17230 elsif Is_Private_Type (Ti)
17231 and then not Has_Discriminants (Ti)
17232 and then Present (Full_View (Ti))
17233 and then Etype (Full_View (Ti)) /= Ti
17234 then
17235 Result :=
17236 Search_Derivation_Levels (
17237 Full_View (Ti),
17238 Discrim_Values,
17239 Stored_Discrim_Values);
17240 end if;
17241 end if;
17242
17243 -- If Result is not a (reference to a) discriminant, return it,
17244 -- otherwise set Result_Entity to the discriminant.
17245
17246 if Nkind (Result) = N_Defining_Identifier then
17247 pragma Assert (Result = Discriminant);
17248 Result_Entity := Result;
17249
17250 else
17251 if not Denotes_Discriminant (Result) then
17252 return Result;
17253 end if;
17254
17255 Result_Entity := Entity (Result);
17256 end if;
17257
17258 -- See if this level of derivation actually has discriminants because
17259 -- tagged derivations can add them, hence the lower levels need not
17260 -- have any.
17261
17262 if not Has_Discriminants (Ti) then
17263 return Result;
17264 end if;
17265
17266 -- Scan Ti's discriminants for Result_Entity, and return its
17267 -- corresponding value, if any.
17268
17269 Result_Entity := Original_Record_Component (Result_Entity);
17270
17271 Assoc := First_Elmt (Discrim_Values);
17272
17273 if Stored_Discrim_Values then
17274 Disc := First_Stored_Discriminant (Ti);
17275 else
17276 Disc := First_Discriminant (Ti);
17277 end if;
17278
17279 while Present (Disc) loop
17280 pragma Assert (Present (Assoc));
17281
17282 if Original_Record_Component (Disc) = Result_Entity then
17283 return Node (Assoc);
17284 end if;
17285
17286 Next_Elmt (Assoc);
17287
17288 if Stored_Discrim_Values then
17289 Next_Stored_Discriminant (Disc);
17290 else
17291 Next_Discriminant (Disc);
17292 end if;
17293 end loop;
17294
17295 -- Could not find it
17296
17297 return Result;
17298 end Search_Derivation_Levels;
17299
17300 -- Local Variables
17301
17302 Result : Node_Or_Entity_Id;
17303
17304 -- Start of processing for Get_Discriminant_Value
17305
17306 begin
17307 -- ??? This routine is a gigantic mess and will be deleted. For the
17308 -- time being just test for the trivial case before calling recurse.
17309
17310 if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
17311 declare
17312 D : Entity_Id;
17313 E : Elmt_Id;
17314
17315 begin
17316 D := First_Discriminant (Typ_For_Constraint);
17317 E := First_Elmt (Constraint);
17318 while Present (D) loop
17319 if Chars (D) = Chars (Discriminant) then
17320 return Node (E);
17321 end if;
17322
17323 Next_Discriminant (D);
17324 Next_Elmt (E);
17325 end loop;
17326 end;
17327 end if;
17328
17329 Result := Search_Derivation_Levels
17330 (Typ_For_Constraint, Constraint, False);
17331
17332 -- ??? hack to disappear when this routine is gone
17333
17334 if Nkind (Result) = N_Defining_Identifier then
17335 declare
17336 D : Entity_Id;
17337 E : Elmt_Id;
17338
17339 begin
17340 D := First_Discriminant (Typ_For_Constraint);
17341 E := First_Elmt (Constraint);
17342 while Present (D) loop
17343 if Root_Corresponding_Discriminant (D) = Discriminant then
17344 return Node (E);
17345 end if;
17346
17347 Next_Discriminant (D);
17348 Next_Elmt (E);
17349 end loop;
17350 end;
17351 end if;
17352
17353 pragma Assert (Nkind (Result) /= N_Defining_Identifier);
17354 return Result;
17355 end Get_Discriminant_Value;
17356
17357 --------------------------
17358 -- Has_Range_Constraint --
17359 --------------------------
17360
17361 function Has_Range_Constraint (N : Node_Id) return Boolean is
17362 C : constant Node_Id := Constraint (N);
17363
17364 begin
17365 if Nkind (C) = N_Range_Constraint then
17366 return True;
17367
17368 elsif Nkind (C) = N_Digits_Constraint then
17369 return
17370 Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
17371 or else Present (Range_Constraint (C));
17372
17373 elsif Nkind (C) = N_Delta_Constraint then
17374 return Present (Range_Constraint (C));
17375
17376 else
17377 return False;
17378 end if;
17379 end Has_Range_Constraint;
17380
17381 ------------------------
17382 -- Inherit_Components --
17383 ------------------------
17384
17385 function Inherit_Components
17386 (N : Node_Id;
17387 Parent_Base : Entity_Id;
17388 Derived_Base : Entity_Id;
17389 Is_Tagged : Boolean;
17390 Inherit_Discr : Boolean;
17391 Discs : Elist_Id) return Elist_Id
17392 is
17393 Assoc_List : constant Elist_Id := New_Elmt_List;
17394
17395 procedure Inherit_Component
17396 (Old_C : Entity_Id;
17397 Plain_Discrim : Boolean := False;
17398 Stored_Discrim : Boolean := False);
17399 -- Inherits component Old_C from Parent_Base to the Derived_Base. If
17400 -- Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
17401 -- True, Old_C is a stored discriminant. If they are both false then
17402 -- Old_C is a regular component.
17403
17404 -----------------------
17405 -- Inherit_Component --
17406 -----------------------
17407
17408 procedure Inherit_Component
17409 (Old_C : Entity_Id;
17410 Plain_Discrim : Boolean := False;
17411 Stored_Discrim : Boolean := False)
17412 is
17413 procedure Set_Anonymous_Type (Id : Entity_Id);
17414 -- Id denotes the entity of an access discriminant or anonymous
17415 -- access component. Set the type of Id to either the same type of
17416 -- Old_C or create a new one depending on whether the parent and
17417 -- the child types are in the same scope.
17418
17419 ------------------------
17420 -- Set_Anonymous_Type --
17421 ------------------------
17422
17423 procedure Set_Anonymous_Type (Id : Entity_Id) is
17424 Old_Typ : constant Entity_Id := Etype (Old_C);
17425
17426 begin
17427 if Scope (Parent_Base) = Scope (Derived_Base) then
17428 Set_Etype (Id, Old_Typ);
17429
17430 -- The parent and the derived type are in two different scopes.
17431 -- Reuse the type of the original discriminant / component by
17432 -- copying it in order to preserve all attributes.
17433
17434 else
17435 declare
17436 Typ : constant Entity_Id := New_Copy (Old_Typ);
17437
17438 begin
17439 Set_Etype (Id, Typ);
17440
17441 -- Since we do not generate component declarations for
17442 -- inherited components, associate the itype with the
17443 -- derived type.
17444
17445 Set_Associated_Node_For_Itype (Typ, Parent (Derived_Base));
17446 Set_Scope (Typ, Derived_Base);
17447 end;
17448 end if;
17449 end Set_Anonymous_Type;
17450
17451 -- Local variables and constants
17452
17453 New_C : constant Entity_Id := New_Copy (Old_C);
17454
17455 Corr_Discrim : Entity_Id;
17456 Discrim : Entity_Id;
17457
17458 -- Start of processing for Inherit_Component
17459
17460 begin
17461 pragma Assert (not Is_Tagged or not Stored_Discrim);
17462
17463 Set_Parent (New_C, Parent (Old_C));
17464
17465 -- Regular discriminants and components must be inserted in the scope
17466 -- of the Derived_Base. Do it here.
17467
17468 if not Stored_Discrim then
17469 Enter_Name (New_C);
17470 end if;
17471
17472 -- For tagged types the Original_Record_Component must point to
17473 -- whatever this field was pointing to in the parent type. This has
17474 -- already been achieved by the call to New_Copy above.
17475
17476 if not Is_Tagged then
17477 Set_Original_Record_Component (New_C, New_C);
17478 end if;
17479
17480 -- Set the proper type of an access discriminant
17481
17482 if Ekind (New_C) = E_Discriminant
17483 and then Ekind (Etype (New_C)) = E_Anonymous_Access_Type
17484 then
17485 Set_Anonymous_Type (New_C);
17486 end if;
17487
17488 -- If we have inherited a component then see if its Etype contains
17489 -- references to Parent_Base discriminants. In this case, replace
17490 -- these references with the constraints given in Discs. We do not
17491 -- do this for the partial view of private types because this is
17492 -- not needed (only the components of the full view will be used
17493 -- for code generation) and cause problem. We also avoid this
17494 -- transformation in some error situations.
17495
17496 if Ekind (New_C) = E_Component then
17497
17498 -- Set the proper type of an anonymous access component
17499
17500 if Ekind (Etype (New_C)) = E_Anonymous_Access_Type then
17501 Set_Anonymous_Type (New_C);
17502
17503 elsif (Is_Private_Type (Derived_Base)
17504 and then not Is_Generic_Type (Derived_Base))
17505 or else (Is_Empty_Elmt_List (Discs)
17506 and then not Expander_Active)
17507 then
17508 Set_Etype (New_C, Etype (Old_C));
17509
17510 else
17511 -- The current component introduces a circularity of the
17512 -- following kind:
17513
17514 -- limited with Pack_2;
17515 -- package Pack_1 is
17516 -- type T_1 is tagged record
17517 -- Comp : access Pack_2.T_2;
17518 -- ...
17519 -- end record;
17520 -- end Pack_1;
17521
17522 -- with Pack_1;
17523 -- package Pack_2 is
17524 -- type T_2 is new Pack_1.T_1 with ...;
17525 -- end Pack_2;
17526
17527 Set_Etype
17528 (New_C,
17529 Constrain_Component_Type
17530 (Old_C, Derived_Base, N, Parent_Base, Discs));
17531 end if;
17532 end if;
17533
17534 -- In derived tagged types it is illegal to reference a non
17535 -- discriminant component in the parent type. To catch this, mark
17536 -- these components with an Ekind of E_Void. This will be reset in
17537 -- Record_Type_Definition after processing the record extension of
17538 -- the derived type.
17539
17540 -- If the declaration is a private extension, there is no further
17541 -- record extension to process, and the components retain their
17542 -- current kind, because they are visible at this point.
17543
17544 if Is_Tagged and then Ekind (New_C) = E_Component
17545 and then Nkind (N) /= N_Private_Extension_Declaration
17546 then
17547 Set_Ekind (New_C, E_Void);
17548 end if;
17549
17550 if Plain_Discrim then
17551 Set_Corresponding_Discriminant (New_C, Old_C);
17552 Build_Discriminal (New_C);
17553
17554 -- If we are explicitly inheriting a stored discriminant it will be
17555 -- completely hidden.
17556
17557 elsif Stored_Discrim then
17558 Set_Corresponding_Discriminant (New_C, Empty);
17559 Set_Discriminal (New_C, Empty);
17560 Set_Is_Completely_Hidden (New_C);
17561
17562 -- Set the Original_Record_Component of each discriminant in the
17563 -- derived base to point to the corresponding stored that we just
17564 -- created.
17565
17566 Discrim := First_Discriminant (Derived_Base);
17567 while Present (Discrim) loop
17568 Corr_Discrim := Corresponding_Discriminant (Discrim);
17569
17570 -- Corr_Discrim could be missing in an error situation
17571
17572 if Present (Corr_Discrim)
17573 and then Original_Record_Component (Corr_Discrim) = Old_C
17574 then
17575 Set_Original_Record_Component (Discrim, New_C);
17576 end if;
17577
17578 Next_Discriminant (Discrim);
17579 end loop;
17580
17581 Append_Entity (New_C, Derived_Base);
17582 end if;
17583
17584 if not Is_Tagged then
17585 Append_Elmt (Old_C, Assoc_List);
17586 Append_Elmt (New_C, Assoc_List);
17587 end if;
17588 end Inherit_Component;
17589
17590 -- Variables local to Inherit_Component
17591
17592 Loc : constant Source_Ptr := Sloc (N);
17593
17594 Parent_Discrim : Entity_Id;
17595 Stored_Discrim : Entity_Id;
17596 D : Entity_Id;
17597 Component : Entity_Id;
17598
17599 -- Start of processing for Inherit_Components
17600
17601 begin
17602 if not Is_Tagged then
17603 Append_Elmt (Parent_Base, Assoc_List);
17604 Append_Elmt (Derived_Base, Assoc_List);
17605 end if;
17606
17607 -- Inherit parent discriminants if needed
17608
17609 if Inherit_Discr then
17610 Parent_Discrim := First_Discriminant (Parent_Base);
17611 while Present (Parent_Discrim) loop
17612 Inherit_Component (Parent_Discrim, Plain_Discrim => True);
17613 Next_Discriminant (Parent_Discrim);
17614 end loop;
17615 end if;
17616
17617 -- Create explicit stored discrims for untagged types when necessary
17618
17619 if not Has_Unknown_Discriminants (Derived_Base)
17620 and then Has_Discriminants (Parent_Base)
17621 and then not Is_Tagged
17622 and then
17623 (not Inherit_Discr
17624 or else First_Discriminant (Parent_Base) /=
17625 First_Stored_Discriminant (Parent_Base))
17626 then
17627 Stored_Discrim := First_Stored_Discriminant (Parent_Base);
17628 while Present (Stored_Discrim) loop
17629 Inherit_Component (Stored_Discrim, Stored_Discrim => True);
17630 Next_Stored_Discriminant (Stored_Discrim);
17631 end loop;
17632 end if;
17633
17634 -- See if we can apply the second transformation for derived types, as
17635 -- explained in point 6. in the comments above Build_Derived_Record_Type
17636 -- This is achieved by appending Derived_Base discriminants into Discs,
17637 -- which has the side effect of returning a non empty Discs list to the
17638 -- caller of Inherit_Components, which is what we want. This must be
17639 -- done for private derived types if there are explicit stored
17640 -- discriminants, to ensure that we can retrieve the values of the
17641 -- constraints provided in the ancestors.
17642
17643 if Inherit_Discr
17644 and then Is_Empty_Elmt_List (Discs)
17645 and then Present (First_Discriminant (Derived_Base))
17646 and then
17647 (not Is_Private_Type (Derived_Base)
17648 or else Is_Completely_Hidden
17649 (First_Stored_Discriminant (Derived_Base))
17650 or else Is_Generic_Type (Derived_Base))
17651 then
17652 D := First_Discriminant (Derived_Base);
17653 while Present (D) loop
17654 Append_Elmt (New_Occurrence_Of (D, Loc), Discs);
17655 Next_Discriminant (D);
17656 end loop;
17657 end if;
17658
17659 -- Finally, inherit non-discriminant components unless they are not
17660 -- visible because defined or inherited from the full view of the
17661 -- parent. Don't inherit the _parent field of the parent type.
17662
17663 Component := First_Entity (Parent_Base);
17664 while Present (Component) loop
17665
17666 -- Ada 2005 (AI-251): Do not inherit components associated with
17667 -- secondary tags of the parent.
17668
17669 if Ekind (Component) = E_Component
17670 and then Present (Related_Type (Component))
17671 then
17672 null;
17673
17674 elsif Ekind (Component) /= E_Component
17675 or else Chars (Component) = Name_uParent
17676 then
17677 null;
17678
17679 -- If the derived type is within the parent type's declarative
17680 -- region, then the components can still be inherited even though
17681 -- they aren't visible at this point. This can occur for cases
17682 -- such as within public child units where the components must
17683 -- become visible upon entering the child unit's private part.
17684
17685 elsif not Is_Visible_Component (Component)
17686 and then not In_Open_Scopes (Scope (Parent_Base))
17687 then
17688 null;
17689
17690 elsif Ekind_In (Derived_Base, E_Private_Type,
17691 E_Limited_Private_Type)
17692 then
17693 null;
17694
17695 else
17696 Inherit_Component (Component);
17697 end if;
17698
17699 Next_Entity (Component);
17700 end loop;
17701
17702 -- For tagged derived types, inherited discriminants cannot be used in
17703 -- component declarations of the record extension part. To achieve this
17704 -- we mark the inherited discriminants as not visible.
17705
17706 if Is_Tagged and then Inherit_Discr then
17707 D := First_Discriminant (Derived_Base);
17708 while Present (D) loop
17709 Set_Is_Immediately_Visible (D, False);
17710 Next_Discriminant (D);
17711 end loop;
17712 end if;
17713
17714 return Assoc_List;
17715 end Inherit_Components;
17716
17717 -----------------------------
17718 -- Inherit_Predicate_Flags --
17719 -----------------------------
17720
17721 procedure Inherit_Predicate_Flags (Subt, Par : Entity_Id) is
17722 begin
17723 Set_Has_Predicates (Subt, Has_Predicates (Par));
17724 Set_Has_Static_Predicate_Aspect
17725 (Subt, Has_Static_Predicate_Aspect (Par));
17726 Set_Has_Dynamic_Predicate_Aspect
17727 (Subt, Has_Dynamic_Predicate_Aspect (Par));
17728 end Inherit_Predicate_Flags;
17729
17730 ----------------------
17731 -- Is_EVF_Procedure --
17732 ----------------------
17733
17734 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean is
17735 Formal : Entity_Id;
17736
17737 begin
17738 -- Examine the formals of an Extensions_Visible False procedure looking
17739 -- for a controlling OUT parameter.
17740
17741 if Ekind (Subp) = E_Procedure
17742 and then Extensions_Visible_Status (Subp) = Extensions_Visible_False
17743 then
17744 Formal := First_Formal (Subp);
17745 while Present (Formal) loop
17746 if Ekind (Formal) = E_Out_Parameter
17747 and then Is_Controlling_Formal (Formal)
17748 then
17749 return True;
17750 end if;
17751
17752 Next_Formal (Formal);
17753 end loop;
17754 end if;
17755
17756 return False;
17757 end Is_EVF_Procedure;
17758
17759 -----------------------
17760 -- Is_Null_Extension --
17761 -----------------------
17762
17763 function Is_Null_Extension (T : Entity_Id) return Boolean is
17764 Type_Decl : constant Node_Id := Parent (Base_Type (T));
17765 Comp_List : Node_Id;
17766 Comp : Node_Id;
17767
17768 begin
17769 if Nkind (Type_Decl) /= N_Full_Type_Declaration
17770 or else not Is_Tagged_Type (T)
17771 or else Nkind (Type_Definition (Type_Decl)) /=
17772 N_Derived_Type_Definition
17773 or else No (Record_Extension_Part (Type_Definition (Type_Decl)))
17774 then
17775 return False;
17776 end if;
17777
17778 Comp_List :=
17779 Component_List (Record_Extension_Part (Type_Definition (Type_Decl)));
17780
17781 if Present (Discriminant_Specifications (Type_Decl)) then
17782 return False;
17783
17784 elsif Present (Comp_List)
17785 and then Is_Non_Empty_List (Component_Items (Comp_List))
17786 then
17787 Comp := First (Component_Items (Comp_List));
17788
17789 -- Only user-defined components are relevant. The component list
17790 -- may also contain a parent component and internal components
17791 -- corresponding to secondary tags, but these do not determine
17792 -- whether this is a null extension.
17793
17794 while Present (Comp) loop
17795 if Comes_From_Source (Comp) then
17796 return False;
17797 end if;
17798
17799 Next (Comp);
17800 end loop;
17801
17802 return True;
17803
17804 else
17805 return True;
17806 end if;
17807 end Is_Null_Extension;
17808
17809 ------------------------------
17810 -- Is_Valid_Constraint_Kind --
17811 ------------------------------
17812
17813 function Is_Valid_Constraint_Kind
17814 (T_Kind : Type_Kind;
17815 Constraint_Kind : Node_Kind) return Boolean
17816 is
17817 begin
17818 case T_Kind is
17819 when Enumeration_Kind |
17820 Integer_Kind =>
17821 return Constraint_Kind = N_Range_Constraint;
17822
17823 when Decimal_Fixed_Point_Kind =>
17824 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
17825 N_Range_Constraint);
17826
17827 when Ordinary_Fixed_Point_Kind =>
17828 return Nkind_In (Constraint_Kind, N_Delta_Constraint,
17829 N_Range_Constraint);
17830
17831 when Float_Kind =>
17832 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
17833 N_Range_Constraint);
17834
17835 when Access_Kind |
17836 Array_Kind |
17837 E_Record_Type |
17838 E_Record_Subtype |
17839 Class_Wide_Kind |
17840 E_Incomplete_Type |
17841 Private_Kind |
17842 Concurrent_Kind =>
17843 return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
17844
17845 when others =>
17846 return True; -- Error will be detected later
17847 end case;
17848 end Is_Valid_Constraint_Kind;
17849
17850 --------------------------
17851 -- Is_Visible_Component --
17852 --------------------------
17853
17854 function Is_Visible_Component
17855 (C : Entity_Id;
17856 N : Node_Id := Empty) return Boolean
17857 is
17858 Original_Comp : Entity_Id := Empty;
17859 Original_Scope : Entity_Id;
17860 Type_Scope : Entity_Id;
17861
17862 function Is_Local_Type (Typ : Entity_Id) return Boolean;
17863 -- Check whether parent type of inherited component is declared locally,
17864 -- possibly within a nested package or instance. The current scope is
17865 -- the derived record itself.
17866
17867 -------------------
17868 -- Is_Local_Type --
17869 -------------------
17870
17871 function Is_Local_Type (Typ : Entity_Id) return Boolean is
17872 Scop : Entity_Id;
17873
17874 begin
17875 Scop := Scope (Typ);
17876 while Present (Scop)
17877 and then Scop /= Standard_Standard
17878 loop
17879 if Scop = Scope (Current_Scope) then
17880 return True;
17881 end if;
17882
17883 Scop := Scope (Scop);
17884 end loop;
17885
17886 return False;
17887 end Is_Local_Type;
17888
17889 -- Start of processing for Is_Visible_Component
17890
17891 begin
17892 if Ekind_In (C, E_Component, E_Discriminant) then
17893 Original_Comp := Original_Record_Component (C);
17894 end if;
17895
17896 if No (Original_Comp) then
17897
17898 -- Premature usage, or previous error
17899
17900 return False;
17901
17902 else
17903 Original_Scope := Scope (Original_Comp);
17904 Type_Scope := Scope (Base_Type (Scope (C)));
17905 end if;
17906
17907 -- This test only concerns tagged types
17908
17909 if not Is_Tagged_Type (Original_Scope) then
17910 return True;
17911
17912 -- If it is _Parent or _Tag, there is no visibility issue
17913
17914 elsif not Comes_From_Source (Original_Comp) then
17915 return True;
17916
17917 -- Discriminants are visible unless the (private) type has unknown
17918 -- discriminants. If the discriminant reference is inserted for a
17919 -- discriminant check on a full view it is also visible.
17920
17921 elsif Ekind (Original_Comp) = E_Discriminant
17922 and then
17923 (not Has_Unknown_Discriminants (Original_Scope)
17924 or else (Present (N)
17925 and then Nkind (N) = N_Selected_Component
17926 and then Nkind (Prefix (N)) = N_Type_Conversion
17927 and then not Comes_From_Source (Prefix (N))))
17928 then
17929 return True;
17930
17931 -- In the body of an instantiation, no need to check for the visibility
17932 -- of a component.
17933
17934 elsif In_Instance_Body then
17935 return True;
17936
17937 -- If the component has been declared in an ancestor which is currently
17938 -- a private type, then it is not visible. The same applies if the
17939 -- component's containing type is not in an open scope and the original
17940 -- component's enclosing type is a visible full view of a private type
17941 -- (which can occur in cases where an attempt is being made to reference
17942 -- a component in a sibling package that is inherited from a visible
17943 -- component of a type in an ancestor package; the component in the
17944 -- sibling package should not be visible even though the component it
17945 -- inherited from is visible). This does not apply however in the case
17946 -- where the scope of the type is a private child unit, or when the
17947 -- parent comes from a local package in which the ancestor is currently
17948 -- visible. The latter suppression of visibility is needed for cases
17949 -- that are tested in B730006.
17950
17951 elsif Is_Private_Type (Original_Scope)
17952 or else
17953 (not Is_Private_Descendant (Type_Scope)
17954 and then not In_Open_Scopes (Type_Scope)
17955 and then Has_Private_Declaration (Original_Scope))
17956 then
17957 -- If the type derives from an entity in a formal package, there
17958 -- are no additional visible components.
17959
17960 if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
17961 N_Formal_Package_Declaration
17962 then
17963 return False;
17964
17965 -- if we are not in the private part of the current package, there
17966 -- are no additional visible components.
17967
17968 elsif Ekind (Scope (Current_Scope)) = E_Package
17969 and then not In_Private_Part (Scope (Current_Scope))
17970 then
17971 return False;
17972 else
17973 return
17974 Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
17975 and then In_Open_Scopes (Scope (Original_Scope))
17976 and then Is_Local_Type (Type_Scope);
17977 end if;
17978
17979 -- There is another weird way in which a component may be invisible when
17980 -- the private and the full view are not derived from the same ancestor.
17981 -- Here is an example :
17982
17983 -- type A1 is tagged record F1 : integer; end record;
17984 -- type A2 is new A1 with record F2 : integer; end record;
17985 -- type T is new A1 with private;
17986 -- private
17987 -- type T is new A2 with null record;
17988
17989 -- In this case, the full view of T inherits F1 and F2 but the private
17990 -- view inherits only F1
17991
17992 else
17993 declare
17994 Ancestor : Entity_Id := Scope (C);
17995
17996 begin
17997 loop
17998 if Ancestor = Original_Scope then
17999 return True;
18000 elsif Ancestor = Etype (Ancestor) then
18001 return False;
18002 end if;
18003
18004 Ancestor := Etype (Ancestor);
18005 end loop;
18006 end;
18007 end if;
18008 end Is_Visible_Component;
18009
18010 --------------------------
18011 -- Make_Class_Wide_Type --
18012 --------------------------
18013
18014 procedure Make_Class_Wide_Type (T : Entity_Id) is
18015 CW_Type : Entity_Id;
18016 CW_Name : Name_Id;
18017 Next_E : Entity_Id;
18018
18019 begin
18020 if Present (Class_Wide_Type (T)) then
18021
18022 -- The class-wide type is a partially decorated entity created for a
18023 -- unanalyzed tagged type referenced through a limited with clause.
18024 -- When the tagged type is analyzed, its class-wide type needs to be
18025 -- redecorated. Note that we reuse the entity created by Decorate_
18026 -- Tagged_Type in order to preserve all links.
18027
18028 if Materialize_Entity (Class_Wide_Type (T)) then
18029 CW_Type := Class_Wide_Type (T);
18030 Set_Materialize_Entity (CW_Type, False);
18031
18032 -- The class wide type can have been defined by the partial view, in
18033 -- which case everything is already done.
18034
18035 else
18036 return;
18037 end if;
18038
18039 -- Default case, we need to create a new class-wide type
18040
18041 else
18042 CW_Type :=
18043 New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
18044 end if;
18045
18046 -- Inherit root type characteristics
18047
18048 CW_Name := Chars (CW_Type);
18049 Next_E := Next_Entity (CW_Type);
18050 Copy_Node (T, CW_Type);
18051 Set_Comes_From_Source (CW_Type, False);
18052 Set_Chars (CW_Type, CW_Name);
18053 Set_Parent (CW_Type, Parent (T));
18054 Set_Next_Entity (CW_Type, Next_E);
18055
18056 -- Ensure we have a new freeze node for the class-wide type. The partial
18057 -- view may have freeze action of its own, requiring a proper freeze
18058 -- node, and the same freeze node cannot be shared between the two
18059 -- types.
18060
18061 Set_Has_Delayed_Freeze (CW_Type);
18062 Set_Freeze_Node (CW_Type, Empty);
18063
18064 -- Customize the class-wide type: It has no prim. op., it cannot be
18065 -- abstract and its Etype points back to the specific root type.
18066
18067 Set_Ekind (CW_Type, E_Class_Wide_Type);
18068 Set_Is_Tagged_Type (CW_Type, True);
18069 Set_Direct_Primitive_Operations (CW_Type, New_Elmt_List);
18070 Set_Is_Abstract_Type (CW_Type, False);
18071 Set_Is_Constrained (CW_Type, False);
18072 Set_Is_First_Subtype (CW_Type, Is_First_Subtype (T));
18073 Set_Default_SSO (CW_Type);
18074
18075 if Ekind (T) = E_Class_Wide_Subtype then
18076 Set_Etype (CW_Type, Etype (Base_Type (T)));
18077 else
18078 Set_Etype (CW_Type, T);
18079 end if;
18080
18081 Set_No_Tagged_Streams_Pragma (CW_Type, No_Tagged_Streams);
18082
18083 -- If this is the class_wide type of a constrained subtype, it does
18084 -- not have discriminants.
18085
18086 Set_Has_Discriminants (CW_Type,
18087 Has_Discriminants (T) and then not Is_Constrained (T));
18088
18089 Set_Has_Unknown_Discriminants (CW_Type, True);
18090 Set_Class_Wide_Type (T, CW_Type);
18091 Set_Equivalent_Type (CW_Type, Empty);
18092
18093 -- The class-wide type of a class-wide type is itself (RM 3.9(14))
18094
18095 Set_Class_Wide_Type (CW_Type, CW_Type);
18096 end Make_Class_Wide_Type;
18097
18098 ----------------
18099 -- Make_Index --
18100 ----------------
18101
18102 procedure Make_Index
18103 (N : Node_Id;
18104 Related_Nod : Node_Id;
18105 Related_Id : Entity_Id := Empty;
18106 Suffix_Index : Nat := 1;
18107 In_Iter_Schm : Boolean := False)
18108 is
18109 R : Node_Id;
18110 T : Entity_Id;
18111 Def_Id : Entity_Id := Empty;
18112 Found : Boolean := False;
18113
18114 begin
18115 -- For a discrete range used in a constrained array definition and
18116 -- defined by a range, an implicit conversion to the predefined type
18117 -- INTEGER is assumed if each bound is either a numeric literal, a named
18118 -- number, or an attribute, and the type of both bounds (prior to the
18119 -- implicit conversion) is the type universal_integer. Otherwise, both
18120 -- bounds must be of the same discrete type, other than universal
18121 -- integer; this type must be determinable independently of the
18122 -- context, but using the fact that the type must be discrete and that
18123 -- both bounds must have the same type.
18124
18125 -- Character literals also have a universal type in the absence of
18126 -- of additional context, and are resolved to Standard_Character.
18127
18128 if Nkind (N) = N_Range then
18129
18130 -- The index is given by a range constraint. The bounds are known
18131 -- to be of a consistent type.
18132
18133 if not Is_Overloaded (N) then
18134 T := Etype (N);
18135
18136 -- For universal bounds, choose the specific predefined type
18137
18138 if T = Universal_Integer then
18139 T := Standard_Integer;
18140
18141 elsif T = Any_Character then
18142 Ambiguous_Character (Low_Bound (N));
18143
18144 T := Standard_Character;
18145 end if;
18146
18147 -- The node may be overloaded because some user-defined operators
18148 -- are available, but if a universal interpretation exists it is
18149 -- also the selected one.
18150
18151 elsif Universal_Interpretation (N) = Universal_Integer then
18152 T := Standard_Integer;
18153
18154 else
18155 T := Any_Type;
18156
18157 declare
18158 Ind : Interp_Index;
18159 It : Interp;
18160
18161 begin
18162 Get_First_Interp (N, Ind, It);
18163 while Present (It.Typ) loop
18164 if Is_Discrete_Type (It.Typ) then
18165
18166 if Found
18167 and then not Covers (It.Typ, T)
18168 and then not Covers (T, It.Typ)
18169 then
18170 Error_Msg_N ("ambiguous bounds in discrete range", N);
18171 exit;
18172 else
18173 T := It.Typ;
18174 Found := True;
18175 end if;
18176 end if;
18177
18178 Get_Next_Interp (Ind, It);
18179 end loop;
18180
18181 if T = Any_Type then
18182 Error_Msg_N ("discrete type required for range", N);
18183 Set_Etype (N, Any_Type);
18184 return;
18185
18186 elsif T = Universal_Integer then
18187 T := Standard_Integer;
18188 end if;
18189 end;
18190 end if;
18191
18192 if not Is_Discrete_Type (T) then
18193 Error_Msg_N ("discrete type required for range", N);
18194 Set_Etype (N, Any_Type);
18195 return;
18196 end if;
18197
18198 if Nkind (Low_Bound (N)) = N_Attribute_Reference
18199 and then Attribute_Name (Low_Bound (N)) = Name_First
18200 and then Is_Entity_Name (Prefix (Low_Bound (N)))
18201 and then Is_Type (Entity (Prefix (Low_Bound (N))))
18202 and then Is_Discrete_Type (Entity (Prefix (Low_Bound (N))))
18203 then
18204 -- The type of the index will be the type of the prefix, as long
18205 -- as the upper bound is 'Last of the same type.
18206
18207 Def_Id := Entity (Prefix (Low_Bound (N)));
18208
18209 if Nkind (High_Bound (N)) /= N_Attribute_Reference
18210 or else Attribute_Name (High_Bound (N)) /= Name_Last
18211 or else not Is_Entity_Name (Prefix (High_Bound (N)))
18212 or else Entity (Prefix (High_Bound (N))) /= Def_Id
18213 then
18214 Def_Id := Empty;
18215 end if;
18216 end if;
18217
18218 R := N;
18219 Process_Range_Expr_In_Decl (R, T, In_Iter_Schm => In_Iter_Schm);
18220
18221 elsif Nkind (N) = N_Subtype_Indication then
18222
18223 -- The index is given by a subtype with a range constraint
18224
18225 T := Base_Type (Entity (Subtype_Mark (N)));
18226
18227 if not Is_Discrete_Type (T) then
18228 Error_Msg_N ("discrete type required for range", N);
18229 Set_Etype (N, Any_Type);
18230 return;
18231 end if;
18232
18233 R := Range_Expression (Constraint (N));
18234
18235 Resolve (R, T);
18236 Process_Range_Expr_In_Decl
18237 (R, Entity (Subtype_Mark (N)), In_Iter_Schm => In_Iter_Schm);
18238
18239 elsif Nkind (N) = N_Attribute_Reference then
18240
18241 -- Catch beginner's error (use of attribute other than 'Range)
18242
18243 if Attribute_Name (N) /= Name_Range then
18244 Error_Msg_N ("expect attribute ''Range", N);
18245 Set_Etype (N, Any_Type);
18246 return;
18247 end if;
18248
18249 -- If the node denotes the range of a type mark, that is also the
18250 -- resulting type, and we do not need to create an Itype for it.
18251
18252 if Is_Entity_Name (Prefix (N))
18253 and then Comes_From_Source (N)
18254 and then Is_Type (Entity (Prefix (N)))
18255 and then Is_Discrete_Type (Entity (Prefix (N)))
18256 then
18257 Def_Id := Entity (Prefix (N));
18258 end if;
18259
18260 Analyze_And_Resolve (N);
18261 T := Etype (N);
18262 R := N;
18263
18264 -- If none of the above, must be a subtype. We convert this to a
18265 -- range attribute reference because in the case of declared first
18266 -- named subtypes, the types in the range reference can be different
18267 -- from the type of the entity. A range attribute normalizes the
18268 -- reference and obtains the correct types for the bounds.
18269
18270 -- This transformation is in the nature of an expansion, is only
18271 -- done if expansion is active. In particular, it is not done on
18272 -- formal generic types, because we need to retain the name of the
18273 -- original index for instantiation purposes.
18274
18275 else
18276 if not Is_Entity_Name (N) or else not Is_Type (Entity (N)) then
18277 Error_Msg_N ("invalid subtype mark in discrete range ", N);
18278 Set_Etype (N, Any_Integer);
18279 return;
18280
18281 else
18282 -- The type mark may be that of an incomplete type. It is only
18283 -- now that we can get the full view, previous analysis does
18284 -- not look specifically for a type mark.
18285
18286 Set_Entity (N, Get_Full_View (Entity (N)));
18287 Set_Etype (N, Entity (N));
18288 Def_Id := Entity (N);
18289
18290 if not Is_Discrete_Type (Def_Id) then
18291 Error_Msg_N ("discrete type required for index", N);
18292 Set_Etype (N, Any_Type);
18293 return;
18294 end if;
18295 end if;
18296
18297 if Expander_Active then
18298 Rewrite (N,
18299 Make_Attribute_Reference (Sloc (N),
18300 Attribute_Name => Name_Range,
18301 Prefix => Relocate_Node (N)));
18302
18303 -- The original was a subtype mark that does not freeze. This
18304 -- means that the rewritten version must not freeze either.
18305
18306 Set_Must_Not_Freeze (N);
18307 Set_Must_Not_Freeze (Prefix (N));
18308 Analyze_And_Resolve (N);
18309 T := Etype (N);
18310 R := N;
18311
18312 -- If expander is inactive, type is legal, nothing else to construct
18313
18314 else
18315 return;
18316 end if;
18317 end if;
18318
18319 if not Is_Discrete_Type (T) then
18320 Error_Msg_N ("discrete type required for range", N);
18321 Set_Etype (N, Any_Type);
18322 return;
18323
18324 elsif T = Any_Type then
18325 Set_Etype (N, Any_Type);
18326 return;
18327 end if;
18328
18329 -- We will now create the appropriate Itype to describe the range, but
18330 -- first a check. If we originally had a subtype, then we just label
18331 -- the range with this subtype. Not only is there no need to construct
18332 -- a new subtype, but it is wrong to do so for two reasons:
18333
18334 -- 1. A legality concern, if we have a subtype, it must not freeze,
18335 -- and the Itype would cause freezing incorrectly
18336
18337 -- 2. An efficiency concern, if we created an Itype, it would not be
18338 -- recognized as the same type for the purposes of eliminating
18339 -- checks in some circumstances.
18340
18341 -- We signal this case by setting the subtype entity in Def_Id
18342
18343 if No (Def_Id) then
18344 Def_Id :=
18345 Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
18346 Set_Etype (Def_Id, Base_Type (T));
18347
18348 if Is_Signed_Integer_Type (T) then
18349 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
18350
18351 elsif Is_Modular_Integer_Type (T) then
18352 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
18353
18354 else
18355 Set_Ekind (Def_Id, E_Enumeration_Subtype);
18356 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
18357 Set_First_Literal (Def_Id, First_Literal (T));
18358 end if;
18359
18360 Set_Size_Info (Def_Id, (T));
18361 Set_RM_Size (Def_Id, RM_Size (T));
18362 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
18363
18364 Set_Scalar_Range (Def_Id, R);
18365 Conditional_Delay (Def_Id, T);
18366
18367 if Nkind (N) = N_Subtype_Indication then
18368 Inherit_Predicate_Flags (Def_Id, Entity (Subtype_Mark (N)));
18369 end if;
18370
18371 -- In the subtype indication case, if the immediate parent of the
18372 -- new subtype is non-static, then the subtype we create is non-
18373 -- static, even if its bounds are static.
18374
18375 if Nkind (N) = N_Subtype_Indication
18376 and then not Is_OK_Static_Subtype (Entity (Subtype_Mark (N)))
18377 then
18378 Set_Is_Non_Static_Subtype (Def_Id);
18379 end if;
18380 end if;
18381
18382 -- Final step is to label the index with this constructed type
18383
18384 Set_Etype (N, Def_Id);
18385 end Make_Index;
18386
18387 ------------------------------
18388 -- Modular_Type_Declaration --
18389 ------------------------------
18390
18391 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
18392 Mod_Expr : constant Node_Id := Expression (Def);
18393 M_Val : Uint;
18394
18395 procedure Set_Modular_Size (Bits : Int);
18396 -- Sets RM_Size to Bits, and Esize to normal word size above this
18397
18398 ----------------------
18399 -- Set_Modular_Size --
18400 ----------------------
18401
18402 procedure Set_Modular_Size (Bits : Int) is
18403 begin
18404 Set_RM_Size (T, UI_From_Int (Bits));
18405
18406 if Bits <= 8 then
18407 Init_Esize (T, 8);
18408
18409 elsif Bits <= 16 then
18410 Init_Esize (T, 16);
18411
18412 elsif Bits <= 32 then
18413 Init_Esize (T, 32);
18414
18415 else
18416 Init_Esize (T, System_Max_Binary_Modulus_Power);
18417 end if;
18418
18419 if not Non_Binary_Modulus (T) and then Esize (T) = RM_Size (T) then
18420 Set_Is_Known_Valid (T);
18421 end if;
18422 end Set_Modular_Size;
18423
18424 -- Start of processing for Modular_Type_Declaration
18425
18426 begin
18427 -- If the mod expression is (exactly) 2 * literal, where literal is
18428 -- 64 or less,then almost certainly the * was meant to be **. Warn.
18429
18430 if Warn_On_Suspicious_Modulus_Value
18431 and then Nkind (Mod_Expr) = N_Op_Multiply
18432 and then Nkind (Left_Opnd (Mod_Expr)) = N_Integer_Literal
18433 and then Intval (Left_Opnd (Mod_Expr)) = Uint_2
18434 and then Nkind (Right_Opnd (Mod_Expr)) = N_Integer_Literal
18435 and then Intval (Right_Opnd (Mod_Expr)) <= Uint_64
18436 then
18437 Error_Msg_N
18438 ("suspicious MOD value, was '*'* intended'??M?", Mod_Expr);
18439 end if;
18440
18441 -- Proceed with analysis of mod expression
18442
18443 Analyze_And_Resolve (Mod_Expr, Any_Integer);
18444 Set_Etype (T, T);
18445 Set_Ekind (T, E_Modular_Integer_Type);
18446 Init_Alignment (T);
18447 Set_Is_Constrained (T);
18448
18449 if not Is_OK_Static_Expression (Mod_Expr) then
18450 Flag_Non_Static_Expr
18451 ("non-static expression used for modular type bound!", Mod_Expr);
18452 M_Val := 2 ** System_Max_Binary_Modulus_Power;
18453 else
18454 M_Val := Expr_Value (Mod_Expr);
18455 end if;
18456
18457 if M_Val < 1 then
18458 Error_Msg_N ("modulus value must be positive", Mod_Expr);
18459 M_Val := 2 ** System_Max_Binary_Modulus_Power;
18460 end if;
18461
18462 if M_Val > 2 ** Standard_Long_Integer_Size then
18463 Check_Restriction (No_Long_Long_Integers, Mod_Expr);
18464 end if;
18465
18466 Set_Modulus (T, M_Val);
18467
18468 -- Create bounds for the modular type based on the modulus given in
18469 -- the type declaration and then analyze and resolve those bounds.
18470
18471 Set_Scalar_Range (T,
18472 Make_Range (Sloc (Mod_Expr),
18473 Low_Bound => Make_Integer_Literal (Sloc (Mod_Expr), 0),
18474 High_Bound => Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
18475
18476 -- Properly analyze the literals for the range. We do this manually
18477 -- because we can't go calling Resolve, since we are resolving these
18478 -- bounds with the type, and this type is certainly not complete yet.
18479
18480 Set_Etype (Low_Bound (Scalar_Range (T)), T);
18481 Set_Etype (High_Bound (Scalar_Range (T)), T);
18482 Set_Is_Static_Expression (Low_Bound (Scalar_Range (T)));
18483 Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
18484
18485 -- Loop through powers of two to find number of bits required
18486
18487 for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
18488
18489 -- Binary case
18490
18491 if M_Val = 2 ** Bits then
18492 Set_Modular_Size (Bits);
18493 return;
18494
18495 -- Non-binary case
18496
18497 elsif M_Val < 2 ** Bits then
18498 Check_SPARK_05_Restriction ("modulus should be a power of 2", T);
18499 Set_Non_Binary_Modulus (T);
18500
18501 if Bits > System_Max_Nonbinary_Modulus_Power then
18502 Error_Msg_Uint_1 :=
18503 UI_From_Int (System_Max_Nonbinary_Modulus_Power);
18504 Error_Msg_F
18505 ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
18506 Set_Modular_Size (System_Max_Binary_Modulus_Power);
18507 return;
18508
18509 else
18510 -- In the non-binary case, set size as per RM 13.3(55)
18511
18512 Set_Modular_Size (Bits);
18513 return;
18514 end if;
18515 end if;
18516
18517 end loop;
18518
18519 -- If we fall through, then the size exceed System.Max_Binary_Modulus
18520 -- so we just signal an error and set the maximum size.
18521
18522 Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
18523 Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
18524
18525 Set_Modular_Size (System_Max_Binary_Modulus_Power);
18526 Init_Alignment (T);
18527
18528 end Modular_Type_Declaration;
18529
18530 --------------------------
18531 -- New_Concatenation_Op --
18532 --------------------------
18533
18534 procedure New_Concatenation_Op (Typ : Entity_Id) is
18535 Loc : constant Source_Ptr := Sloc (Typ);
18536 Op : Entity_Id;
18537
18538 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
18539 -- Create abbreviated declaration for the formal of a predefined
18540 -- Operator 'Op' of type 'Typ'
18541
18542 --------------------
18543 -- Make_Op_Formal --
18544 --------------------
18545
18546 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
18547 Formal : Entity_Id;
18548 begin
18549 Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
18550 Set_Etype (Formal, Typ);
18551 Set_Mechanism (Formal, Default_Mechanism);
18552 return Formal;
18553 end Make_Op_Formal;
18554
18555 -- Start of processing for New_Concatenation_Op
18556
18557 begin
18558 Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
18559
18560 Set_Ekind (Op, E_Operator);
18561 Set_Scope (Op, Current_Scope);
18562 Set_Etype (Op, Typ);
18563 Set_Homonym (Op, Get_Name_Entity_Id (Name_Op_Concat));
18564 Set_Is_Immediately_Visible (Op);
18565 Set_Is_Intrinsic_Subprogram (Op);
18566 Set_Has_Completion (Op);
18567 Append_Entity (Op, Current_Scope);
18568
18569 Set_Name_Entity_Id (Name_Op_Concat, Op);
18570
18571 Append_Entity (Make_Op_Formal (Typ, Op), Op);
18572 Append_Entity (Make_Op_Formal (Typ, Op), Op);
18573 end New_Concatenation_Op;
18574
18575 -------------------------
18576 -- OK_For_Limited_Init --
18577 -------------------------
18578
18579 -- ???Check all calls of this, and compare the conditions under which it's
18580 -- called.
18581
18582 function OK_For_Limited_Init
18583 (Typ : Entity_Id;
18584 Exp : Node_Id) return Boolean
18585 is
18586 begin
18587 return Is_CPP_Constructor_Call (Exp)
18588 or else (Ada_Version >= Ada_2005
18589 and then not Debug_Flag_Dot_L
18590 and then OK_For_Limited_Init_In_05 (Typ, Exp));
18591 end OK_For_Limited_Init;
18592
18593 -------------------------------
18594 -- OK_For_Limited_Init_In_05 --
18595 -------------------------------
18596
18597 function OK_For_Limited_Init_In_05
18598 (Typ : Entity_Id;
18599 Exp : Node_Id) return Boolean
18600 is
18601 begin
18602 -- An object of a limited interface type can be initialized with any
18603 -- expression of a nonlimited descendant type.
18604
18605 if Is_Class_Wide_Type (Typ)
18606 and then Is_Limited_Interface (Typ)
18607 and then not Is_Limited_Type (Etype (Exp))
18608 then
18609 return True;
18610 end if;
18611
18612 -- Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
18613 -- case of limited aggregates (including extension aggregates), and
18614 -- function calls. The function call may have been given in prefixed
18615 -- notation, in which case the original node is an indexed component.
18616 -- If the function is parameterless, the original node was an explicit
18617 -- dereference. The function may also be parameterless, in which case
18618 -- the source node is just an identifier.
18619
18620 case Nkind (Original_Node (Exp)) is
18621 when N_Aggregate | N_Extension_Aggregate | N_Function_Call | N_Op =>
18622 return True;
18623
18624 when N_Identifier =>
18625 return Present (Entity (Original_Node (Exp)))
18626 and then Ekind (Entity (Original_Node (Exp))) = E_Function;
18627
18628 when N_Qualified_Expression =>
18629 return
18630 OK_For_Limited_Init_In_05
18631 (Typ, Expression (Original_Node (Exp)));
18632
18633 -- Ada 2005 (AI-251): If a class-wide interface object is initialized
18634 -- with a function call, the expander has rewritten the call into an
18635 -- N_Type_Conversion node to force displacement of the pointer to
18636 -- reference the component containing the secondary dispatch table.
18637 -- Otherwise a type conversion is not a legal context.
18638 -- A return statement for a build-in-place function returning a
18639 -- synchronized type also introduces an unchecked conversion.
18640
18641 when N_Type_Conversion |
18642 N_Unchecked_Type_Conversion =>
18643 return not Comes_From_Source (Exp)
18644 and then
18645 OK_For_Limited_Init_In_05
18646 (Typ, Expression (Original_Node (Exp)));
18647
18648 when N_Indexed_Component |
18649 N_Selected_Component |
18650 N_Explicit_Dereference =>
18651 return Nkind (Exp) = N_Function_Call;
18652
18653 -- A use of 'Input is a function call, hence allowed. Normally the
18654 -- attribute will be changed to a call, but the attribute by itself
18655 -- can occur with -gnatc.
18656
18657 when N_Attribute_Reference =>
18658 return Attribute_Name (Original_Node (Exp)) = Name_Input;
18659
18660 -- For a case expression, all dependent expressions must be legal
18661
18662 when N_Case_Expression =>
18663 declare
18664 Alt : Node_Id;
18665
18666 begin
18667 Alt := First (Alternatives (Original_Node (Exp)));
18668 while Present (Alt) loop
18669 if not OK_For_Limited_Init_In_05 (Typ, Expression (Alt)) then
18670 return False;
18671 end if;
18672
18673 Next (Alt);
18674 end loop;
18675
18676 return True;
18677 end;
18678
18679 -- For an if expression, all dependent expressions must be legal
18680
18681 when N_If_Expression =>
18682 declare
18683 Then_Expr : constant Node_Id :=
18684 Next (First (Expressions (Original_Node (Exp))));
18685 Else_Expr : constant Node_Id := Next (Then_Expr);
18686 begin
18687 return OK_For_Limited_Init_In_05 (Typ, Then_Expr)
18688 and then
18689 OK_For_Limited_Init_In_05 (Typ, Else_Expr);
18690 end;
18691
18692 when others =>
18693 return False;
18694 end case;
18695 end OK_For_Limited_Init_In_05;
18696
18697 -------------------------------------------
18698 -- Ordinary_Fixed_Point_Type_Declaration --
18699 -------------------------------------------
18700
18701 procedure Ordinary_Fixed_Point_Type_Declaration
18702 (T : Entity_Id;
18703 Def : Node_Id)
18704 is
18705 Loc : constant Source_Ptr := Sloc (Def);
18706 Delta_Expr : constant Node_Id := Delta_Expression (Def);
18707 RRS : constant Node_Id := Real_Range_Specification (Def);
18708 Implicit_Base : Entity_Id;
18709 Delta_Val : Ureal;
18710 Small_Val : Ureal;
18711 Low_Val : Ureal;
18712 High_Val : Ureal;
18713
18714 begin
18715 Check_Restriction (No_Fixed_Point, Def);
18716
18717 -- Create implicit base type
18718
18719 Implicit_Base :=
18720 Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
18721 Set_Etype (Implicit_Base, Implicit_Base);
18722
18723 -- Analyze and process delta expression
18724
18725 Analyze_And_Resolve (Delta_Expr, Any_Real);
18726
18727 Check_Delta_Expression (Delta_Expr);
18728 Delta_Val := Expr_Value_R (Delta_Expr);
18729
18730 Set_Delta_Value (Implicit_Base, Delta_Val);
18731
18732 -- Compute default small from given delta, which is the largest power
18733 -- of two that does not exceed the given delta value.
18734
18735 declare
18736 Tmp : Ureal;
18737 Scale : Int;
18738
18739 begin
18740 Tmp := Ureal_1;
18741 Scale := 0;
18742
18743 if Delta_Val < Ureal_1 then
18744 while Delta_Val < Tmp loop
18745 Tmp := Tmp / Ureal_2;
18746 Scale := Scale + 1;
18747 end loop;
18748
18749 else
18750 loop
18751 Tmp := Tmp * Ureal_2;
18752 exit when Tmp > Delta_Val;
18753 Scale := Scale - 1;
18754 end loop;
18755 end if;
18756
18757 Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
18758 end;
18759
18760 Set_Small_Value (Implicit_Base, Small_Val);
18761
18762 -- If no range was given, set a dummy range
18763
18764 if RRS <= Empty_Or_Error then
18765 Low_Val := -Small_Val;
18766 High_Val := Small_Val;
18767
18768 -- Otherwise analyze and process given range
18769
18770 else
18771 declare
18772 Low : constant Node_Id := Low_Bound (RRS);
18773 High : constant Node_Id := High_Bound (RRS);
18774
18775 begin
18776 Analyze_And_Resolve (Low, Any_Real);
18777 Analyze_And_Resolve (High, Any_Real);
18778 Check_Real_Bound (Low);
18779 Check_Real_Bound (High);
18780
18781 -- Obtain and set the range
18782
18783 Low_Val := Expr_Value_R (Low);
18784 High_Val := Expr_Value_R (High);
18785
18786 if Low_Val > High_Val then
18787 Error_Msg_NE ("??fixed point type& has null range", Def, T);
18788 end if;
18789 end;
18790 end if;
18791
18792 -- The range for both the implicit base and the declared first subtype
18793 -- cannot be set yet, so we use the special routine Set_Fixed_Range to
18794 -- set a temporary range in place. Note that the bounds of the base
18795 -- type will be widened to be symmetrical and to fill the available
18796 -- bits when the type is frozen.
18797
18798 -- We could do this with all discrete types, and probably should, but
18799 -- we absolutely have to do it for fixed-point, since the end-points
18800 -- of the range and the size are determined by the small value, which
18801 -- could be reset before the freeze point.
18802
18803 Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
18804 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
18805
18806 -- Complete definition of first subtype. The inheritance of the rep item
18807 -- chain ensures that SPARK-related pragmas are not clobbered when the
18808 -- ordinary fixed point type acts as a full view of a private type.
18809
18810 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
18811 Set_Etype (T, Implicit_Base);
18812 Init_Size_Align (T);
18813 Inherit_Rep_Item_Chain (T, Implicit_Base);
18814 Set_Small_Value (T, Small_Val);
18815 Set_Delta_Value (T, Delta_Val);
18816 Set_Is_Constrained (T);
18817 end Ordinary_Fixed_Point_Type_Declaration;
18818
18819 ----------------------------------
18820 -- Preanalyze_Assert_Expression --
18821 ----------------------------------
18822
18823 procedure Preanalyze_Assert_Expression (N : Node_Id; T : Entity_Id) is
18824 begin
18825 In_Assertion_Expr := In_Assertion_Expr + 1;
18826 Preanalyze_Spec_Expression (N, T);
18827 In_Assertion_Expr := In_Assertion_Expr - 1;
18828 end Preanalyze_Assert_Expression;
18829
18830 -----------------------------------
18831 -- Preanalyze_Default_Expression --
18832 -----------------------------------
18833
18834 procedure Preanalyze_Default_Expression (N : Node_Id; T : Entity_Id) is
18835 Save_In_Default_Expr : constant Boolean := In_Default_Expr;
18836 begin
18837 In_Default_Expr := True;
18838 Preanalyze_Spec_Expression (N, T);
18839 In_Default_Expr := Save_In_Default_Expr;
18840 end Preanalyze_Default_Expression;
18841
18842 --------------------------------
18843 -- Preanalyze_Spec_Expression --
18844 --------------------------------
18845
18846 procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
18847 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
18848 begin
18849 In_Spec_Expression := True;
18850 Preanalyze_And_Resolve (N, T);
18851 In_Spec_Expression := Save_In_Spec_Expression;
18852 end Preanalyze_Spec_Expression;
18853
18854 ----------------------------------------
18855 -- Prepare_Private_Subtype_Completion --
18856 ----------------------------------------
18857
18858 procedure Prepare_Private_Subtype_Completion
18859 (Id : Entity_Id;
18860 Related_Nod : Node_Id)
18861 is
18862 Id_B : constant Entity_Id := Base_Type (Id);
18863 Full_B : Entity_Id := Full_View (Id_B);
18864 Full : Entity_Id;
18865
18866 begin
18867 if Present (Full_B) then
18868
18869 -- Get to the underlying full view if necessary
18870
18871 if Is_Private_Type (Full_B)
18872 and then Present (Underlying_Full_View (Full_B))
18873 then
18874 Full_B := Underlying_Full_View (Full_B);
18875 end if;
18876
18877 -- The Base_Type is already completed, we can complete the subtype
18878 -- now. We have to create a new entity with the same name, Thus we
18879 -- can't use Create_Itype.
18880
18881 Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
18882 Set_Is_Itype (Full);
18883 Set_Associated_Node_For_Itype (Full, Related_Nod);
18884 Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
18885 end if;
18886
18887 -- The parent subtype may be private, but the base might not, in some
18888 -- nested instances. In that case, the subtype does not need to be
18889 -- exchanged. It would still be nice to make private subtypes and their
18890 -- bases consistent at all times ???
18891
18892 if Is_Private_Type (Id_B) then
18893 Append_Elmt (Id, Private_Dependents (Id_B));
18894 end if;
18895 end Prepare_Private_Subtype_Completion;
18896
18897 ---------------------------
18898 -- Process_Discriminants --
18899 ---------------------------
18900
18901 procedure Process_Discriminants
18902 (N : Node_Id;
18903 Prev : Entity_Id := Empty)
18904 is
18905 Elist : constant Elist_Id := New_Elmt_List;
18906 Id : Node_Id;
18907 Discr : Node_Id;
18908 Discr_Number : Uint;
18909 Discr_Type : Entity_Id;
18910 Default_Present : Boolean := False;
18911 Default_Not_Present : Boolean := False;
18912
18913 begin
18914 -- A composite type other than an array type can have discriminants.
18915 -- On entry, the current scope is the composite type.
18916
18917 -- The discriminants are initially entered into the scope of the type
18918 -- via Enter_Name with the default Ekind of E_Void to prevent premature
18919 -- use, as explained at the end of this procedure.
18920
18921 Discr := First (Discriminant_Specifications (N));
18922 while Present (Discr) loop
18923 Enter_Name (Defining_Identifier (Discr));
18924
18925 -- For navigation purposes we add a reference to the discriminant
18926 -- in the entity for the type. If the current declaration is a
18927 -- completion, place references on the partial view. Otherwise the
18928 -- type is the current scope.
18929
18930 if Present (Prev) then
18931
18932 -- The references go on the partial view, if present. If the
18933 -- partial view has discriminants, the references have been
18934 -- generated already.
18935
18936 if not Has_Discriminants (Prev) then
18937 Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
18938 end if;
18939 else
18940 Generate_Reference
18941 (Current_Scope, Defining_Identifier (Discr), 'd');
18942 end if;
18943
18944 if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
18945 Discr_Type := Access_Definition (Discr, Discriminant_Type (Discr));
18946
18947 -- Ada 2005 (AI-254)
18948
18949 if Present (Access_To_Subprogram_Definition
18950 (Discriminant_Type (Discr)))
18951 and then Protected_Present (Access_To_Subprogram_Definition
18952 (Discriminant_Type (Discr)))
18953 then
18954 Discr_Type :=
18955 Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
18956 end if;
18957
18958 else
18959 Find_Type (Discriminant_Type (Discr));
18960 Discr_Type := Etype (Discriminant_Type (Discr));
18961
18962 if Error_Posted (Discriminant_Type (Discr)) then
18963 Discr_Type := Any_Type;
18964 end if;
18965 end if;
18966
18967 -- Handling of discriminants that are access types
18968
18969 if Is_Access_Type (Discr_Type) then
18970
18971 -- Ada 2005 (AI-230): Access discriminant allowed in non-
18972 -- limited record types
18973
18974 if Ada_Version < Ada_2005 then
18975 Check_Access_Discriminant_Requires_Limited
18976 (Discr, Discriminant_Type (Discr));
18977 end if;
18978
18979 if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
18980 Error_Msg_N
18981 ("(Ada 83) access discriminant not allowed", Discr);
18982 end if;
18983
18984 -- If not access type, must be a discrete type
18985
18986 elsif not Is_Discrete_Type (Discr_Type) then
18987 Error_Msg_N
18988 ("discriminants must have a discrete or access type",
18989 Discriminant_Type (Discr));
18990 end if;
18991
18992 Set_Etype (Defining_Identifier (Discr), Discr_Type);
18993
18994 -- If a discriminant specification includes the assignment compound
18995 -- delimiter followed by an expression, the expression is the default
18996 -- expression of the discriminant; the default expression must be of
18997 -- the type of the discriminant. (RM 3.7.1) Since this expression is
18998 -- a default expression, we do the special preanalysis, since this
18999 -- expression does not freeze (see section "Handling of Default and
19000 -- Per-Object Expressions" in spec of package Sem).
19001
19002 if Present (Expression (Discr)) then
19003 Preanalyze_Spec_Expression (Expression (Discr), Discr_Type);
19004
19005 -- Legaity checks
19006
19007 if Nkind (N) = N_Formal_Type_Declaration then
19008 Error_Msg_N
19009 ("discriminant defaults not allowed for formal type",
19010 Expression (Discr));
19011
19012 -- Flag an error for a tagged type with defaulted discriminants,
19013 -- excluding limited tagged types when compiling for Ada 2012
19014 -- (see AI05-0214).
19015
19016 elsif Is_Tagged_Type (Current_Scope)
19017 and then (not Is_Limited_Type (Current_Scope)
19018 or else Ada_Version < Ada_2012)
19019 and then Comes_From_Source (N)
19020 then
19021 -- Note: see similar test in Check_Or_Process_Discriminants, to
19022 -- handle the (illegal) case of the completion of an untagged
19023 -- view with discriminants with defaults by a tagged full view.
19024 -- We skip the check if Discr does not come from source, to
19025 -- account for the case of an untagged derived type providing
19026 -- defaults for a renamed discriminant from a private untagged
19027 -- ancestor with a tagged full view (ACATS B460006).
19028
19029 if Ada_Version >= Ada_2012 then
19030 Error_Msg_N
19031 ("discriminants of nonlimited tagged type cannot have"
19032 & " defaults",
19033 Expression (Discr));
19034 else
19035 Error_Msg_N
19036 ("discriminants of tagged type cannot have defaults",
19037 Expression (Discr));
19038 end if;
19039
19040 else
19041 Default_Present := True;
19042 Append_Elmt (Expression (Discr), Elist);
19043
19044 -- Tag the defining identifiers for the discriminants with
19045 -- their corresponding default expressions from the tree.
19046
19047 Set_Discriminant_Default_Value
19048 (Defining_Identifier (Discr), Expression (Discr));
19049 end if;
19050
19051 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag
19052 -- gets set unless we can be sure that no range check is required.
19053
19054 if (GNATprove_Mode or not Expander_Active)
19055 and then not
19056 Is_In_Range
19057 (Expression (Discr), Discr_Type, Assume_Valid => True)
19058 then
19059 Set_Do_Range_Check (Expression (Discr));
19060 end if;
19061
19062 -- No default discriminant value given
19063
19064 else
19065 Default_Not_Present := True;
19066 end if;
19067
19068 -- Ada 2005 (AI-231): Create an Itype that is a duplicate of
19069 -- Discr_Type but with the null-exclusion attribute
19070
19071 if Ada_Version >= Ada_2005 then
19072
19073 -- Ada 2005 (AI-231): Static checks
19074
19075 if Can_Never_Be_Null (Discr_Type) then
19076 Null_Exclusion_Static_Checks (Discr);
19077
19078 elsif Is_Access_Type (Discr_Type)
19079 and then Null_Exclusion_Present (Discr)
19080
19081 -- No need to check itypes because in their case this check
19082 -- was done at their point of creation
19083
19084 and then not Is_Itype (Discr_Type)
19085 then
19086 if Can_Never_Be_Null (Discr_Type) then
19087 Error_Msg_NE
19088 ("`NOT NULL` not allowed (& already excludes null)",
19089 Discr,
19090 Discr_Type);
19091 end if;
19092
19093 Set_Etype (Defining_Identifier (Discr),
19094 Create_Null_Excluding_Itype
19095 (T => Discr_Type,
19096 Related_Nod => Discr));
19097
19098 -- Check for improper null exclusion if the type is otherwise
19099 -- legal for a discriminant.
19100
19101 elsif Null_Exclusion_Present (Discr)
19102 and then Is_Discrete_Type (Discr_Type)
19103 then
19104 Error_Msg_N
19105 ("null exclusion can only apply to an access type", Discr);
19106 end if;
19107
19108 -- Ada 2005 (AI-402): access discriminants of nonlimited types
19109 -- can't have defaults. Synchronized types, or types that are
19110 -- explicitly limited are fine, but special tests apply to derived
19111 -- types in generics: in a generic body we have to assume the
19112 -- worst, and therefore defaults are not allowed if the parent is
19113 -- a generic formal private type (see ACATS B370001).
19114
19115 if Is_Access_Type (Discr_Type) and then Default_Present then
19116 if Ekind (Discr_Type) /= E_Anonymous_Access_Type
19117 or else Is_Limited_Record (Current_Scope)
19118 or else Is_Concurrent_Type (Current_Scope)
19119 or else Is_Concurrent_Record_Type (Current_Scope)
19120 or else Ekind (Current_Scope) = E_Limited_Private_Type
19121 then
19122 if not Is_Derived_Type (Current_Scope)
19123 or else not Is_Generic_Type (Etype (Current_Scope))
19124 or else not In_Package_Body (Scope (Etype (Current_Scope)))
19125 or else Limited_Present
19126 (Type_Definition (Parent (Current_Scope)))
19127 then
19128 null;
19129
19130 else
19131 Error_Msg_N
19132 ("access discriminants of nonlimited types cannot "
19133 & "have defaults", Expression (Discr));
19134 end if;
19135
19136 elsif Present (Expression (Discr)) then
19137 Error_Msg_N
19138 ("(Ada 2005) access discriminants of nonlimited types "
19139 & "cannot have defaults", Expression (Discr));
19140 end if;
19141 end if;
19142 end if;
19143
19144 -- A discriminant cannot be effectively volatile. This check is only
19145 -- relevant when SPARK_Mode is on as it is not standard Ada legality
19146 -- rule (SPARK RM 7.1.3(6)).
19147
19148 if SPARK_Mode = On
19149 and then Is_Effectively_Volatile (Defining_Identifier (Discr))
19150 then
19151 Error_Msg_N ("discriminant cannot be volatile", Discr);
19152 end if;
19153
19154 Next (Discr);
19155 end loop;
19156
19157 -- An element list consisting of the default expressions of the
19158 -- discriminants is constructed in the above loop and used to set
19159 -- the Discriminant_Constraint attribute for the type. If an object
19160 -- is declared of this (record or task) type without any explicit
19161 -- discriminant constraint given, this element list will form the
19162 -- actual parameters for the corresponding initialization procedure
19163 -- for the type.
19164
19165 Set_Discriminant_Constraint (Current_Scope, Elist);
19166 Set_Stored_Constraint (Current_Scope, No_Elist);
19167
19168 -- Default expressions must be provided either for all or for none
19169 -- of the discriminants of a discriminant part. (RM 3.7.1)
19170
19171 if Default_Present and then Default_Not_Present then
19172 Error_Msg_N
19173 ("incomplete specification of defaults for discriminants", N);
19174 end if;
19175
19176 -- The use of the name of a discriminant is not allowed in default
19177 -- expressions of a discriminant part if the specification of the
19178 -- discriminant is itself given in the discriminant part. (RM 3.7.1)
19179
19180 -- To detect this, the discriminant names are entered initially with an
19181 -- Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
19182 -- attempt to use a void entity (for example in an expression that is
19183 -- type-checked) produces the error message: premature usage. Now after
19184 -- completing the semantic analysis of the discriminant part, we can set
19185 -- the Ekind of all the discriminants appropriately.
19186
19187 Discr := First (Discriminant_Specifications (N));
19188 Discr_Number := Uint_1;
19189 while Present (Discr) loop
19190 Id := Defining_Identifier (Discr);
19191 Set_Ekind (Id, E_Discriminant);
19192 Init_Component_Location (Id);
19193 Init_Esize (Id);
19194 Set_Discriminant_Number (Id, Discr_Number);
19195
19196 -- Make sure this is always set, even in illegal programs
19197
19198 Set_Corresponding_Discriminant (Id, Empty);
19199
19200 -- Initialize the Original_Record_Component to the entity itself.
19201 -- Inherit_Components will propagate the right value to
19202 -- discriminants in derived record types.
19203
19204 Set_Original_Record_Component (Id, Id);
19205
19206 -- Create the discriminal for the discriminant
19207
19208 Build_Discriminal (Id);
19209
19210 Next (Discr);
19211 Discr_Number := Discr_Number + 1;
19212 end loop;
19213
19214 Set_Has_Discriminants (Current_Scope);
19215 end Process_Discriminants;
19216
19217 -----------------------
19218 -- Process_Full_View --
19219 -----------------------
19220
19221 procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
19222 procedure Collect_Implemented_Interfaces
19223 (Typ : Entity_Id;
19224 Ifaces : Elist_Id);
19225 -- Ada 2005: Gather all the interfaces that Typ directly or
19226 -- inherently implements. Duplicate entries are not added to
19227 -- the list Ifaces.
19228
19229 ------------------------------------
19230 -- Collect_Implemented_Interfaces --
19231 ------------------------------------
19232
19233 procedure Collect_Implemented_Interfaces
19234 (Typ : Entity_Id;
19235 Ifaces : Elist_Id)
19236 is
19237 Iface : Entity_Id;
19238 Iface_Elmt : Elmt_Id;
19239
19240 begin
19241 -- Abstract interfaces are only associated with tagged record types
19242
19243 if not Is_Tagged_Type (Typ) or else not Is_Record_Type (Typ) then
19244 return;
19245 end if;
19246
19247 -- Recursively climb to the ancestors
19248
19249 if Etype (Typ) /= Typ
19250
19251 -- Protect the frontend against wrong cyclic declarations like:
19252
19253 -- type B is new A with private;
19254 -- type C is new A with private;
19255 -- private
19256 -- type B is new C with null record;
19257 -- type C is new B with null record;
19258
19259 and then Etype (Typ) /= Priv_T
19260 and then Etype (Typ) /= Full_T
19261 then
19262 -- Keep separate the management of private type declarations
19263
19264 if Ekind (Typ) = E_Record_Type_With_Private then
19265
19266 -- Handle the following illegal usage:
19267 -- type Private_Type is tagged private;
19268 -- private
19269 -- type Private_Type is new Type_Implementing_Iface;
19270
19271 if Present (Full_View (Typ))
19272 and then Etype (Typ) /= Full_View (Typ)
19273 then
19274 if Is_Interface (Etype (Typ)) then
19275 Append_Unique_Elmt (Etype (Typ), Ifaces);
19276 end if;
19277
19278 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
19279 end if;
19280
19281 -- Non-private types
19282
19283 else
19284 if Is_Interface (Etype (Typ)) then
19285 Append_Unique_Elmt (Etype (Typ), Ifaces);
19286 end if;
19287
19288 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
19289 end if;
19290 end if;
19291
19292 -- Handle entities in the list of abstract interfaces
19293
19294 if Present (Interfaces (Typ)) then
19295 Iface_Elmt := First_Elmt (Interfaces (Typ));
19296 while Present (Iface_Elmt) loop
19297 Iface := Node (Iface_Elmt);
19298
19299 pragma Assert (Is_Interface (Iface));
19300
19301 if not Contain_Interface (Iface, Ifaces) then
19302 Append_Elmt (Iface, Ifaces);
19303 Collect_Implemented_Interfaces (Iface, Ifaces);
19304 end if;
19305
19306 Next_Elmt (Iface_Elmt);
19307 end loop;
19308 end if;
19309 end Collect_Implemented_Interfaces;
19310
19311 -- Local variables
19312
19313 Full_Indic : Node_Id;
19314 Full_Parent : Entity_Id;
19315 Priv_Parent : Entity_Id;
19316
19317 -- Start of processing for Process_Full_View
19318
19319 begin
19320 -- First some sanity checks that must be done after semantic
19321 -- decoration of the full view and thus cannot be placed with other
19322 -- similar checks in Find_Type_Name
19323
19324 if not Is_Limited_Type (Priv_T)
19325 and then (Is_Limited_Type (Full_T)
19326 or else Is_Limited_Composite (Full_T))
19327 then
19328 if In_Instance then
19329 null;
19330 else
19331 Error_Msg_N
19332 ("completion of nonlimited type cannot be limited", Full_T);
19333 Explain_Limited_Type (Full_T, Full_T);
19334 end if;
19335
19336 elsif Is_Abstract_Type (Full_T)
19337 and then not Is_Abstract_Type (Priv_T)
19338 then
19339 Error_Msg_N
19340 ("completion of nonabstract type cannot be abstract", Full_T);
19341
19342 elsif Is_Tagged_Type (Priv_T)
19343 and then Is_Limited_Type (Priv_T)
19344 and then not Is_Limited_Type (Full_T)
19345 then
19346 -- If pragma CPP_Class was applied to the private declaration
19347 -- propagate the limitedness to the full-view
19348
19349 if Is_CPP_Class (Priv_T) then
19350 Set_Is_Limited_Record (Full_T);
19351
19352 -- GNAT allow its own definition of Limited_Controlled to disobey
19353 -- this rule in order in ease the implementation. This test is safe
19354 -- because Root_Controlled is defined in a child of System that
19355 -- normal programs are not supposed to use.
19356
19357 elsif Is_RTE (Etype (Full_T), RE_Root_Controlled) then
19358 Set_Is_Limited_Composite (Full_T);
19359 else
19360 Error_Msg_N
19361 ("completion of limited tagged type must be limited", Full_T);
19362 end if;
19363
19364 elsif Is_Generic_Type (Priv_T) then
19365 Error_Msg_N ("generic type cannot have a completion", Full_T);
19366 end if;
19367
19368 -- Check that ancestor interfaces of private and full views are
19369 -- consistent. We omit this check for synchronized types because
19370 -- they are performed on the corresponding record type when frozen.
19371
19372 if Ada_Version >= Ada_2005
19373 and then Is_Tagged_Type (Priv_T)
19374 and then Is_Tagged_Type (Full_T)
19375 and then not Is_Concurrent_Type (Full_T)
19376 then
19377 declare
19378 Iface : Entity_Id;
19379 Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
19380 Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
19381
19382 begin
19383 Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
19384 Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
19385
19386 -- Ada 2005 (AI-251): The partial view shall be a descendant of
19387 -- an interface type if and only if the full type is descendant
19388 -- of the interface type (AARM 7.3 (7.3/2)).
19389
19390 Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
19391
19392 if Present (Iface) then
19393 Error_Msg_NE
19394 ("interface in partial view& not implemented by full type "
19395 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
19396 end if;
19397
19398 Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
19399
19400 if Present (Iface) then
19401 Error_Msg_NE
19402 ("interface & not implemented by partial view "
19403 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
19404 end if;
19405 end;
19406 end if;
19407
19408 if Is_Tagged_Type (Priv_T)
19409 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
19410 and then Is_Derived_Type (Full_T)
19411 then
19412 Priv_Parent := Etype (Priv_T);
19413
19414 -- The full view of a private extension may have been transformed
19415 -- into an unconstrained derived type declaration and a subtype
19416 -- declaration (see build_derived_record_type for details).
19417
19418 if Nkind (N) = N_Subtype_Declaration then
19419 Full_Indic := Subtype_Indication (N);
19420 Full_Parent := Etype (Base_Type (Full_T));
19421 else
19422 Full_Indic := Subtype_Indication (Type_Definition (N));
19423 Full_Parent := Etype (Full_T);
19424 end if;
19425
19426 -- Check that the parent type of the full type is a descendant of
19427 -- the ancestor subtype given in the private extension. If either
19428 -- entity has an Etype equal to Any_Type then we had some previous
19429 -- error situation [7.3(8)].
19430
19431 if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
19432 return;
19433
19434 -- Ada 2005 (AI-251): Interfaces in the full type can be given in
19435 -- any order. Therefore we don't have to check that its parent must
19436 -- be a descendant of the parent of the private type declaration.
19437
19438 elsif Is_Interface (Priv_Parent)
19439 and then Is_Interface (Full_Parent)
19440 then
19441 null;
19442
19443 -- Ada 2005 (AI-251): If the parent of the private type declaration
19444 -- is an interface there is no need to check that it is an ancestor
19445 -- of the associated full type declaration. The required tests for
19446 -- this case are performed by Build_Derived_Record_Type.
19447
19448 elsif not Is_Interface (Base_Type (Priv_Parent))
19449 and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
19450 then
19451 Error_Msg_N
19452 ("parent of full type must descend from parent"
19453 & " of private extension", Full_Indic);
19454
19455 -- First check a formal restriction, and then proceed with checking
19456 -- Ada rules. Since the formal restriction is not a serious error, we
19457 -- don't prevent further error detection for this check, hence the
19458 -- ELSE.
19459
19460 else
19461 -- In formal mode, when completing a private extension the type
19462 -- named in the private part must be exactly the same as that
19463 -- named in the visible part.
19464
19465 if Priv_Parent /= Full_Parent then
19466 Error_Msg_Name_1 := Chars (Priv_Parent);
19467 Check_SPARK_05_Restriction ("% expected", Full_Indic);
19468 end if;
19469
19470 -- Check the rules of 7.3(10): if the private extension inherits
19471 -- known discriminants, then the full type must also inherit those
19472 -- discriminants from the same (ancestor) type, and the parent
19473 -- subtype of the full type must be constrained if and only if
19474 -- the ancestor subtype of the private extension is constrained.
19475
19476 if No (Discriminant_Specifications (Parent (Priv_T)))
19477 and then not Has_Unknown_Discriminants (Priv_T)
19478 and then Has_Discriminants (Base_Type (Priv_Parent))
19479 then
19480 declare
19481 Priv_Indic : constant Node_Id :=
19482 Subtype_Indication (Parent (Priv_T));
19483
19484 Priv_Constr : constant Boolean :=
19485 Is_Constrained (Priv_Parent)
19486 or else
19487 Nkind (Priv_Indic) = N_Subtype_Indication
19488 or else
19489 Is_Constrained (Entity (Priv_Indic));
19490
19491 Full_Constr : constant Boolean :=
19492 Is_Constrained (Full_Parent)
19493 or else
19494 Nkind (Full_Indic) = N_Subtype_Indication
19495 or else
19496 Is_Constrained (Entity (Full_Indic));
19497
19498 Priv_Discr : Entity_Id;
19499 Full_Discr : Entity_Id;
19500
19501 begin
19502 Priv_Discr := First_Discriminant (Priv_Parent);
19503 Full_Discr := First_Discriminant (Full_Parent);
19504 while Present (Priv_Discr) and then Present (Full_Discr) loop
19505 if Original_Record_Component (Priv_Discr) =
19506 Original_Record_Component (Full_Discr)
19507 or else
19508 Corresponding_Discriminant (Priv_Discr) =
19509 Corresponding_Discriminant (Full_Discr)
19510 then
19511 null;
19512 else
19513 exit;
19514 end if;
19515
19516 Next_Discriminant (Priv_Discr);
19517 Next_Discriminant (Full_Discr);
19518 end loop;
19519
19520 if Present (Priv_Discr) or else Present (Full_Discr) then
19521 Error_Msg_N
19522 ("full view must inherit discriminants of the parent"
19523 & " type used in the private extension", Full_Indic);
19524
19525 elsif Priv_Constr and then not Full_Constr then
19526 Error_Msg_N
19527 ("parent subtype of full type must be constrained",
19528 Full_Indic);
19529
19530 elsif Full_Constr and then not Priv_Constr then
19531 Error_Msg_N
19532 ("parent subtype of full type must be unconstrained",
19533 Full_Indic);
19534 end if;
19535 end;
19536
19537 -- Check the rules of 7.3(12): if a partial view has neither
19538 -- known or unknown discriminants, then the full type
19539 -- declaration shall define a definite subtype.
19540
19541 elsif not Has_Unknown_Discriminants (Priv_T)
19542 and then not Has_Discriminants (Priv_T)
19543 and then not Is_Constrained (Full_T)
19544 then
19545 Error_Msg_N
19546 ("full view must define a constrained type if partial view"
19547 & " has no discriminants", Full_T);
19548 end if;
19549
19550 -- ??????? Do we implement the following properly ?????
19551 -- If the ancestor subtype of a private extension has constrained
19552 -- discriminants, then the parent subtype of the full view shall
19553 -- impose a statically matching constraint on those discriminants
19554 -- [7.3(13)].
19555 end if;
19556
19557 else
19558 -- For untagged types, verify that a type without discriminants is
19559 -- not completed with an unconstrained type. A separate error message
19560 -- is produced if the full type has defaulted discriminants.
19561
19562 if not Is_Indefinite_Subtype (Priv_T)
19563 and then Is_Indefinite_Subtype (Full_T)
19564 then
19565 Error_Msg_Sloc := Sloc (Parent (Priv_T));
19566 Error_Msg_NE
19567 ("full view of& not compatible with declaration#",
19568 Full_T, Priv_T);
19569
19570 if not Is_Tagged_Type (Full_T) then
19571 Error_Msg_N
19572 ("\one is constrained, the other unconstrained", Full_T);
19573 end if;
19574 end if;
19575 end if;
19576
19577 -- AI-419: verify that the use of "limited" is consistent
19578
19579 declare
19580 Orig_Decl : constant Node_Id := Original_Node (N);
19581
19582 begin
19583 if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
19584 and then Nkind (Orig_Decl) = N_Full_Type_Declaration
19585 and then Nkind
19586 (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
19587 then
19588 if not Limited_Present (Parent (Priv_T))
19589 and then not Synchronized_Present (Parent (Priv_T))
19590 and then Limited_Present (Type_Definition (Orig_Decl))
19591 then
19592 Error_Msg_N
19593 ("full view of non-limited extension cannot be limited", N);
19594
19595 -- Conversely, if the partial view carries the limited keyword,
19596 -- the full view must as well, even if it may be redundant.
19597
19598 elsif Limited_Present (Parent (Priv_T))
19599 and then not Limited_Present (Type_Definition (Orig_Decl))
19600 then
19601 Error_Msg_N
19602 ("full view of limited extension must be explicitly limited",
19603 N);
19604 end if;
19605 end if;
19606 end;
19607
19608 -- Ada 2005 (AI-443): A synchronized private extension must be
19609 -- completed by a task or protected type.
19610
19611 if Ada_Version >= Ada_2005
19612 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
19613 and then Synchronized_Present (Parent (Priv_T))
19614 and then not Is_Concurrent_Type (Full_T)
19615 then
19616 Error_Msg_N ("full view of synchronized extension must " &
19617 "be synchronized type", N);
19618 end if;
19619
19620 -- Ada 2005 AI-363: if the full view has discriminants with
19621 -- defaults, it is illegal to declare constrained access subtypes
19622 -- whose designated type is the current type. This allows objects
19623 -- of the type that are declared in the heap to be unconstrained.
19624
19625 if not Has_Unknown_Discriminants (Priv_T)
19626 and then not Has_Discriminants (Priv_T)
19627 and then Has_Discriminants (Full_T)
19628 and then
19629 Present (Discriminant_Default_Value (First_Discriminant (Full_T)))
19630 then
19631 Set_Has_Constrained_Partial_View (Full_T);
19632 Set_Has_Constrained_Partial_View (Priv_T);
19633 end if;
19634
19635 -- Create a full declaration for all its subtypes recorded in
19636 -- Private_Dependents and swap them similarly to the base type. These
19637 -- are subtypes that have been define before the full declaration of
19638 -- the private type. We also swap the entry in Private_Dependents list
19639 -- so we can properly restore the private view on exit from the scope.
19640
19641 declare
19642 Priv_Elmt : Elmt_Id;
19643 Priv_Scop : Entity_Id;
19644 Priv : Entity_Id;
19645 Full : Entity_Id;
19646
19647 begin
19648 Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
19649 while Present (Priv_Elmt) loop
19650 Priv := Node (Priv_Elmt);
19651 Priv_Scop := Scope (Priv);
19652
19653 if Ekind_In (Priv, E_Private_Subtype,
19654 E_Limited_Private_Subtype,
19655 E_Record_Subtype_With_Private)
19656 then
19657 Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
19658 Set_Is_Itype (Full);
19659 Set_Parent (Full, Parent (Priv));
19660 Set_Associated_Node_For_Itype (Full, N);
19661
19662 -- Now we need to complete the private subtype, but since the
19663 -- base type has already been swapped, we must also swap the
19664 -- subtypes (and thus, reverse the arguments in the call to
19665 -- Complete_Private_Subtype). Also note that we may need to
19666 -- re-establish the scope of the private subtype.
19667
19668 Copy_And_Swap (Priv, Full);
19669
19670 if not In_Open_Scopes (Priv_Scop) then
19671 Push_Scope (Priv_Scop);
19672
19673 else
19674 -- Reset Priv_Scop to Empty to indicate no scope was pushed
19675
19676 Priv_Scop := Empty;
19677 end if;
19678
19679 Complete_Private_Subtype (Full, Priv, Full_T, N);
19680
19681 if Present (Priv_Scop) then
19682 Pop_Scope;
19683 end if;
19684
19685 Replace_Elmt (Priv_Elmt, Full);
19686 end if;
19687
19688 Next_Elmt (Priv_Elmt);
19689 end loop;
19690 end;
19691
19692 -- If the private view was tagged, copy the new primitive operations
19693 -- from the private view to the full view.
19694
19695 if Is_Tagged_Type (Full_T) then
19696 declare
19697 Disp_Typ : Entity_Id;
19698 Full_List : Elist_Id;
19699 Prim : Entity_Id;
19700 Prim_Elmt : Elmt_Id;
19701 Priv_List : Elist_Id;
19702
19703 function Contains
19704 (E : Entity_Id;
19705 L : Elist_Id) return Boolean;
19706 -- Determine whether list L contains element E
19707
19708 --------------
19709 -- Contains --
19710 --------------
19711
19712 function Contains
19713 (E : Entity_Id;
19714 L : Elist_Id) return Boolean
19715 is
19716 List_Elmt : Elmt_Id;
19717
19718 begin
19719 List_Elmt := First_Elmt (L);
19720 while Present (List_Elmt) loop
19721 if Node (List_Elmt) = E then
19722 return True;
19723 end if;
19724
19725 Next_Elmt (List_Elmt);
19726 end loop;
19727
19728 return False;
19729 end Contains;
19730
19731 -- Start of processing
19732
19733 begin
19734 if Is_Tagged_Type (Priv_T) then
19735 Priv_List := Primitive_Operations (Priv_T);
19736 Prim_Elmt := First_Elmt (Priv_List);
19737
19738 -- In the case of a concurrent type completing a private tagged
19739 -- type, primitives may have been declared in between the two
19740 -- views. These subprograms need to be wrapped the same way
19741 -- entries and protected procedures are handled because they
19742 -- cannot be directly shared by the two views.
19743
19744 if Is_Concurrent_Type (Full_T) then
19745 declare
19746 Conc_Typ : constant Entity_Id :=
19747 Corresponding_Record_Type (Full_T);
19748 Curr_Nod : Node_Id := Parent (Conc_Typ);
19749 Wrap_Spec : Node_Id;
19750
19751 begin
19752 while Present (Prim_Elmt) loop
19753 Prim := Node (Prim_Elmt);
19754
19755 if Comes_From_Source (Prim)
19756 and then not Is_Abstract_Subprogram (Prim)
19757 then
19758 Wrap_Spec :=
19759 Make_Subprogram_Declaration (Sloc (Prim),
19760 Specification =>
19761 Build_Wrapper_Spec
19762 (Subp_Id => Prim,
19763 Obj_Typ => Conc_Typ,
19764 Formals =>
19765 Parameter_Specifications (
19766 Parent (Prim))));
19767
19768 Insert_After (Curr_Nod, Wrap_Spec);
19769 Curr_Nod := Wrap_Spec;
19770
19771 Analyze (Wrap_Spec);
19772 end if;
19773
19774 Next_Elmt (Prim_Elmt);
19775 end loop;
19776
19777 return;
19778 end;
19779
19780 -- For non-concurrent types, transfer explicit primitives, but
19781 -- omit those inherited from the parent of the private view
19782 -- since they will be re-inherited later on.
19783
19784 else
19785 Full_List := Primitive_Operations (Full_T);
19786
19787 while Present (Prim_Elmt) loop
19788 Prim := Node (Prim_Elmt);
19789
19790 if Comes_From_Source (Prim)
19791 and then not Contains (Prim, Full_List)
19792 then
19793 Append_Elmt (Prim, Full_List);
19794 end if;
19795
19796 Next_Elmt (Prim_Elmt);
19797 end loop;
19798 end if;
19799
19800 -- Untagged private view
19801
19802 else
19803 Full_List := Primitive_Operations (Full_T);
19804
19805 -- In this case the partial view is untagged, so here we locate
19806 -- all of the earlier primitives that need to be treated as
19807 -- dispatching (those that appear between the two views). Note
19808 -- that these additional operations must all be new operations
19809 -- (any earlier operations that override inherited operations
19810 -- of the full view will already have been inserted in the
19811 -- primitives list, marked by Check_Operation_From_Private_View
19812 -- as dispatching. Note that implicit "/=" operators are
19813 -- excluded from being added to the primitives list since they
19814 -- shouldn't be treated as dispatching (tagged "/=" is handled
19815 -- specially).
19816
19817 Prim := Next_Entity (Full_T);
19818 while Present (Prim) and then Prim /= Priv_T loop
19819 if Ekind_In (Prim, E_Procedure, E_Function) then
19820 Disp_Typ := Find_Dispatching_Type (Prim);
19821
19822 if Disp_Typ = Full_T
19823 and then (Chars (Prim) /= Name_Op_Ne
19824 or else Comes_From_Source (Prim))
19825 then
19826 Check_Controlling_Formals (Full_T, Prim);
19827
19828 if not Is_Dispatching_Operation (Prim) then
19829 Append_Elmt (Prim, Full_List);
19830 Set_Is_Dispatching_Operation (Prim, True);
19831 Set_DT_Position_Value (Prim, No_Uint);
19832 end if;
19833
19834 elsif Is_Dispatching_Operation (Prim)
19835 and then Disp_Typ /= Full_T
19836 then
19837
19838 -- Verify that it is not otherwise controlled by a
19839 -- formal or a return value of type T.
19840
19841 Check_Controlling_Formals (Disp_Typ, Prim);
19842 end if;
19843 end if;
19844
19845 Next_Entity (Prim);
19846 end loop;
19847 end if;
19848
19849 -- For the tagged case, the two views can share the same primitive
19850 -- operations list and the same class-wide type. Update attributes
19851 -- of the class-wide type which depend on the full declaration.
19852
19853 if Is_Tagged_Type (Priv_T) then
19854 Set_Direct_Primitive_Operations (Priv_T, Full_List);
19855 Set_Class_Wide_Type
19856 (Base_Type (Full_T), Class_Wide_Type (Priv_T));
19857
19858 Set_Has_Task (Class_Wide_Type (Priv_T), Has_Task (Full_T));
19859 Set_Has_Protected
19860 (Class_Wide_Type (Priv_T), Has_Protected (Full_T));
19861 end if;
19862 end;
19863 end if;
19864
19865 -- Ada 2005 AI 161: Check preelaborable initialization consistency
19866
19867 if Known_To_Have_Preelab_Init (Priv_T) then
19868
19869 -- Case where there is a pragma Preelaborable_Initialization. We
19870 -- always allow this in predefined units, which is cheating a bit,
19871 -- but it means we don't have to struggle to meet the requirements in
19872 -- the RM for having Preelaborable Initialization. Otherwise we
19873 -- require that the type meets the RM rules. But we can't check that
19874 -- yet, because of the rule about overriding Initialize, so we simply
19875 -- set a flag that will be checked at freeze time.
19876
19877 if not In_Predefined_Unit (Full_T) then
19878 Set_Must_Have_Preelab_Init (Full_T);
19879 end if;
19880 end if;
19881
19882 -- If pragma CPP_Class was applied to the private type declaration,
19883 -- propagate it now to the full type declaration.
19884
19885 if Is_CPP_Class (Priv_T) then
19886 Set_Is_CPP_Class (Full_T);
19887 Set_Convention (Full_T, Convention_CPP);
19888
19889 -- Check that components of imported CPP types do not have default
19890 -- expressions.
19891
19892 Check_CPP_Type_Has_No_Defaults (Full_T);
19893 end if;
19894
19895 -- If the private view has user specified stream attributes, then so has
19896 -- the full view.
19897
19898 -- Why the test, how could these flags be already set in Full_T ???
19899
19900 if Has_Specified_Stream_Read (Priv_T) then
19901 Set_Has_Specified_Stream_Read (Full_T);
19902 end if;
19903
19904 if Has_Specified_Stream_Write (Priv_T) then
19905 Set_Has_Specified_Stream_Write (Full_T);
19906 end if;
19907
19908 if Has_Specified_Stream_Input (Priv_T) then
19909 Set_Has_Specified_Stream_Input (Full_T);
19910 end if;
19911
19912 if Has_Specified_Stream_Output (Priv_T) then
19913 Set_Has_Specified_Stream_Output (Full_T);
19914 end if;
19915
19916 -- Propagate the attributes related to pragma Default_Initial_Condition
19917 -- from the private to the full view. Note that both flags are mutually
19918 -- exclusive.
19919
19920 if Has_Default_Init_Cond (Priv_T)
19921 or else Has_Inherited_Default_Init_Cond (Priv_T)
19922 then
19923 Propagate_Default_Init_Cond_Attributes
19924 (From_Typ => Priv_T,
19925 To_Typ => Full_T,
19926 Private_To_Full_View => True);
19927
19928 -- In the case where the full view is derived from another private type,
19929 -- the attributes related to pragma Default_Initial_Condition must be
19930 -- propagated from the full to the private view to maintain consistency
19931 -- of views.
19932
19933 -- package Pack is
19934 -- type Parent_Typ is private
19935 -- with Default_Initial_Condition ...;
19936 -- private
19937 -- type Parent_Typ is ...;
19938 -- end Pack;
19939
19940 -- with Pack; use Pack;
19941 -- package Pack_2 is
19942 -- type Deriv_Typ is private; -- must inherit
19943 -- private
19944 -- type Deriv_Typ is new Parent_Typ; -- must inherit
19945 -- end Pack_2;
19946
19947 elsif Has_Default_Init_Cond (Full_T)
19948 or else Has_Inherited_Default_Init_Cond (Full_T)
19949 then
19950 Propagate_Default_Init_Cond_Attributes
19951 (From_Typ => Full_T,
19952 To_Typ => Priv_T,
19953 Private_To_Full_View => True);
19954 end if;
19955
19956 -- Propagate the attributes related to pragma Ghost from the private to
19957 -- the full view.
19958
19959 if Is_Ghost_Entity (Priv_T) then
19960 Set_Is_Ghost_Entity (Full_T);
19961
19962 -- The Ghost policy in effect at the point of declaration and at the
19963 -- point of completion must match (SPARK RM 6.9(15)).
19964
19965 Check_Ghost_Completion (Priv_T, Full_T);
19966
19967 -- In the case where the private view of a tagged type lacks a parent
19968 -- type and is subject to pragma Ghost, ensure that the parent type
19969 -- specified by the full view is also Ghost (SPARK RM 6.9(9)).
19970
19971 if Is_Derived_Type (Full_T) then
19972 Check_Ghost_Derivation (Full_T);
19973 end if;
19974 end if;
19975
19976 -- Propagate invariants to full type
19977
19978 if Has_Invariants (Priv_T) then
19979 Set_Has_Invariants (Full_T);
19980 Set_Invariant_Procedure (Full_T, Invariant_Procedure (Priv_T));
19981 end if;
19982
19983 if Has_Inheritable_Invariants (Priv_T) then
19984 Set_Has_Inheritable_Invariants (Full_T);
19985 end if;
19986
19987 -- Check hidden inheritance of class-wide type invariants
19988
19989 if Ada_Version >= Ada_2012
19990 and then not Has_Inheritable_Invariants (Full_T)
19991 and then In_Private_Part (Current_Scope)
19992 and then Has_Interfaces (Full_T)
19993 then
19994 declare
19995 Ifaces : Elist_Id;
19996 AI : Elmt_Id;
19997
19998 begin
19999 Collect_Interfaces (Full_T, Ifaces, Exclude_Parents => True);
20000
20001 AI := First_Elmt (Ifaces);
20002 while Present (AI) loop
20003 if Has_Inheritable_Invariants (Node (AI)) then
20004 Error_Msg_N
20005 ("hidden inheritance of class-wide type invariants " &
20006 "not allowed", N);
20007 exit;
20008 end if;
20009
20010 Next_Elmt (AI);
20011 end loop;
20012 end;
20013 end if;
20014
20015 -- Propagate predicates to full type, and predicate function if already
20016 -- defined. It is not clear that this can actually happen? the partial
20017 -- view cannot be frozen yet, and the predicate function has not been
20018 -- built. Still it is a cheap check and seems safer to make it.
20019
20020 if Has_Predicates (Priv_T) then
20021 if Present (Predicate_Function (Priv_T)) then
20022 Set_Predicate_Function (Full_T, Predicate_Function (Priv_T));
20023 end if;
20024
20025 Set_Has_Predicates (Full_T);
20026 end if;
20027 end Process_Full_View;
20028
20029 -----------------------------------
20030 -- Process_Incomplete_Dependents --
20031 -----------------------------------
20032
20033 procedure Process_Incomplete_Dependents
20034 (N : Node_Id;
20035 Full_T : Entity_Id;
20036 Inc_T : Entity_Id)
20037 is
20038 Inc_Elmt : Elmt_Id;
20039 Priv_Dep : Entity_Id;
20040 New_Subt : Entity_Id;
20041
20042 Disc_Constraint : Elist_Id;
20043
20044 begin
20045 if No (Private_Dependents (Inc_T)) then
20046 return;
20047 end if;
20048
20049 -- Itypes that may be generated by the completion of an incomplete
20050 -- subtype are not used by the back-end and not attached to the tree.
20051 -- They are created only for constraint-checking purposes.
20052
20053 Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
20054 while Present (Inc_Elmt) loop
20055 Priv_Dep := Node (Inc_Elmt);
20056
20057 if Ekind (Priv_Dep) = E_Subprogram_Type then
20058
20059 -- An Access_To_Subprogram type may have a return type or a
20060 -- parameter type that is incomplete. Replace with the full view.
20061
20062 if Etype (Priv_Dep) = Inc_T then
20063 Set_Etype (Priv_Dep, Full_T);
20064 end if;
20065
20066 declare
20067 Formal : Entity_Id;
20068
20069 begin
20070 Formal := First_Formal (Priv_Dep);
20071 while Present (Formal) loop
20072 if Etype (Formal) = Inc_T then
20073 Set_Etype (Formal, Full_T);
20074 end if;
20075
20076 Next_Formal (Formal);
20077 end loop;
20078 end;
20079
20080 elsif Is_Overloadable (Priv_Dep) then
20081
20082 -- If a subprogram in the incomplete dependents list is primitive
20083 -- for a tagged full type then mark it as a dispatching operation,
20084 -- check whether it overrides an inherited subprogram, and check
20085 -- restrictions on its controlling formals. Note that a protected
20086 -- operation is never dispatching: only its wrapper operation
20087 -- (which has convention Ada) is.
20088
20089 if Is_Tagged_Type (Full_T)
20090 and then Is_Primitive (Priv_Dep)
20091 and then Convention (Priv_Dep) /= Convention_Protected
20092 then
20093 Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
20094 Set_Is_Dispatching_Operation (Priv_Dep);
20095 Check_Controlling_Formals (Full_T, Priv_Dep);
20096 end if;
20097
20098 elsif Ekind (Priv_Dep) = E_Subprogram_Body then
20099
20100 -- Can happen during processing of a body before the completion
20101 -- of a TA type. Ignore, because spec is also on dependent list.
20102
20103 return;
20104
20105 -- Ada 2005 (AI-412): Transform a regular incomplete subtype into a
20106 -- corresponding subtype of the full view.
20107
20108 elsif Ekind (Priv_Dep) = E_Incomplete_Subtype then
20109 Set_Subtype_Indication
20110 (Parent (Priv_Dep), New_Occurrence_Of (Full_T, Sloc (Priv_Dep)));
20111 Set_Etype (Priv_Dep, Full_T);
20112 Set_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
20113 Set_Analyzed (Parent (Priv_Dep), False);
20114
20115 -- Reanalyze the declaration, suppressing the call to
20116 -- Enter_Name to avoid duplicate names.
20117
20118 Analyze_Subtype_Declaration
20119 (N => Parent (Priv_Dep),
20120 Skip => True);
20121
20122 -- Dependent is a subtype
20123
20124 else
20125 -- We build a new subtype indication using the full view of the
20126 -- incomplete parent. The discriminant constraints have been
20127 -- elaborated already at the point of the subtype declaration.
20128
20129 New_Subt := Create_Itype (E_Void, N);
20130
20131 if Has_Discriminants (Full_T) then
20132 Disc_Constraint := Discriminant_Constraint (Priv_Dep);
20133 else
20134 Disc_Constraint := No_Elist;
20135 end if;
20136
20137 Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
20138 Set_Full_View (Priv_Dep, New_Subt);
20139 end if;
20140
20141 Next_Elmt (Inc_Elmt);
20142 end loop;
20143 end Process_Incomplete_Dependents;
20144
20145 --------------------------------
20146 -- Process_Range_Expr_In_Decl --
20147 --------------------------------
20148
20149 procedure Process_Range_Expr_In_Decl
20150 (R : Node_Id;
20151 T : Entity_Id;
20152 Subtyp : Entity_Id := Empty;
20153 Check_List : List_Id := Empty_List;
20154 R_Check_Off : Boolean := False;
20155 In_Iter_Schm : Boolean := False)
20156 is
20157 Lo, Hi : Node_Id;
20158 R_Checks : Check_Result;
20159 Insert_Node : Node_Id;
20160 Def_Id : Entity_Id;
20161
20162 begin
20163 Analyze_And_Resolve (R, Base_Type (T));
20164
20165 if Nkind (R) = N_Range then
20166
20167 -- In SPARK, all ranges should be static, with the exception of the
20168 -- discrete type definition of a loop parameter specification.
20169
20170 if not In_Iter_Schm
20171 and then not Is_OK_Static_Range (R)
20172 then
20173 Check_SPARK_05_Restriction ("range should be static", R);
20174 end if;
20175
20176 Lo := Low_Bound (R);
20177 Hi := High_Bound (R);
20178
20179 -- Validity checks on the range of a quantified expression are
20180 -- delayed until the construct is transformed into a loop.
20181
20182 if Nkind (Parent (R)) = N_Loop_Parameter_Specification
20183 and then Nkind (Parent (Parent (R))) = N_Quantified_Expression
20184 then
20185 null;
20186
20187 -- We need to ensure validity of the bounds here, because if we
20188 -- go ahead and do the expansion, then the expanded code will get
20189 -- analyzed with range checks suppressed and we miss the check.
20190
20191 -- WARNING: The capture of the range bounds with xxx_FIRST/_LAST and
20192 -- the temporaries generated by routine Remove_Side_Effects by means
20193 -- of validity checks must use the same names. When a range appears
20194 -- in the parent of a generic, the range is processed with checks
20195 -- disabled as part of the generic context and with checks enabled
20196 -- for code generation purposes. This leads to link issues as the
20197 -- generic contains references to xxx_FIRST/_LAST, but the inlined
20198 -- template sees the temporaries generated by Remove_Side_Effects.
20199
20200 else
20201 Validity_Check_Range (R, Subtyp);
20202 end if;
20203
20204 -- If there were errors in the declaration, try and patch up some
20205 -- common mistakes in the bounds. The cases handled are literals
20206 -- which are Integer where the expected type is Real and vice versa.
20207 -- These corrections allow the compilation process to proceed further
20208 -- along since some basic assumptions of the format of the bounds
20209 -- are guaranteed.
20210
20211 if Etype (R) = Any_Type then
20212 if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
20213 Rewrite (Lo,
20214 Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
20215
20216 elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
20217 Rewrite (Hi,
20218 Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
20219
20220 elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
20221 Rewrite (Lo,
20222 Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
20223
20224 elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
20225 Rewrite (Hi,
20226 Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
20227 end if;
20228
20229 Set_Etype (Lo, T);
20230 Set_Etype (Hi, T);
20231 end if;
20232
20233 -- If the bounds of the range have been mistakenly given as string
20234 -- literals (perhaps in place of character literals), then an error
20235 -- has already been reported, but we rewrite the string literal as a
20236 -- bound of the range's type to avoid blowups in later processing
20237 -- that looks at static values.
20238
20239 if Nkind (Lo) = N_String_Literal then
20240 Rewrite (Lo,
20241 Make_Attribute_Reference (Sloc (Lo),
20242 Prefix => New_Occurrence_Of (T, Sloc (Lo)),
20243 Attribute_Name => Name_First));
20244 Analyze_And_Resolve (Lo);
20245 end if;
20246
20247 if Nkind (Hi) = N_String_Literal then
20248 Rewrite (Hi,
20249 Make_Attribute_Reference (Sloc (Hi),
20250 Prefix => New_Occurrence_Of (T, Sloc (Hi)),
20251 Attribute_Name => Name_First));
20252 Analyze_And_Resolve (Hi);
20253 end if;
20254
20255 -- If bounds aren't scalar at this point then exit, avoiding
20256 -- problems with further processing of the range in this procedure.
20257
20258 if not Is_Scalar_Type (Etype (Lo)) then
20259 return;
20260 end if;
20261
20262 -- Resolve (actually Sem_Eval) has checked that the bounds are in
20263 -- then range of the base type. Here we check whether the bounds
20264 -- are in the range of the subtype itself. Note that if the bounds
20265 -- represent the null range the Constraint_Error exception should
20266 -- not be raised.
20267
20268 -- ??? The following code should be cleaned up as follows
20269
20270 -- 1. The Is_Null_Range (Lo, Hi) test should disappear since it
20271 -- is done in the call to Range_Check (R, T); below
20272
20273 -- 2. The use of R_Check_Off should be investigated and possibly
20274 -- removed, this would clean up things a bit.
20275
20276 if Is_Null_Range (Lo, Hi) then
20277 null;
20278
20279 else
20280 -- Capture values of bounds and generate temporaries for them
20281 -- if needed, before applying checks, since checks may cause
20282 -- duplication of the expression without forcing evaluation.
20283
20284 -- The forced evaluation removes side effects from expressions,
20285 -- which should occur also in GNATprove mode. Otherwise, we end up
20286 -- with unexpected insertions of actions at places where this is
20287 -- not supposed to occur, e.g. on default parameters of a call.
20288
20289 if Expander_Active or GNATprove_Mode then
20290
20291 -- Call Force_Evaluation to create declarations as needed to
20292 -- deal with side effects, and also create typ_FIRST/LAST
20293 -- entities for bounds if we have a subtype name.
20294
20295 -- Note: we do this transformation even if expansion is not
20296 -- active if we are in GNATprove_Mode since the transformation
20297 -- is in general required to ensure that the resulting tree has
20298 -- proper Ada semantics.
20299
20300 Force_Evaluation
20301 (Lo, Related_Id => Subtyp, Is_Low_Bound => True);
20302 Force_Evaluation
20303 (Hi, Related_Id => Subtyp, Is_High_Bound => True);
20304 end if;
20305
20306 -- We use a flag here instead of suppressing checks on the type
20307 -- because the type we check against isn't necessarily the place
20308 -- where we put the check.
20309
20310 if not R_Check_Off then
20311 R_Checks := Get_Range_Checks (R, T);
20312
20313 -- Look up tree to find an appropriate insertion point. We
20314 -- can't just use insert_actions because later processing
20315 -- depends on the insertion node. Prior to Ada 2012 the
20316 -- insertion point could only be a declaration or a loop, but
20317 -- quantified expressions can appear within any context in an
20318 -- expression, and the insertion point can be any statement,
20319 -- pragma, or declaration.
20320
20321 Insert_Node := Parent (R);
20322 while Present (Insert_Node) loop
20323 exit when
20324 Nkind (Insert_Node) in N_Declaration
20325 and then
20326 not Nkind_In
20327 (Insert_Node, N_Component_Declaration,
20328 N_Loop_Parameter_Specification,
20329 N_Function_Specification,
20330 N_Procedure_Specification);
20331
20332 exit when Nkind (Insert_Node) in N_Later_Decl_Item
20333 or else Nkind (Insert_Node) in
20334 N_Statement_Other_Than_Procedure_Call
20335 or else Nkind_In (Insert_Node, N_Procedure_Call_Statement,
20336 N_Pragma);
20337
20338 Insert_Node := Parent (Insert_Node);
20339 end loop;
20340
20341 -- Why would Type_Decl not be present??? Without this test,
20342 -- short regression tests fail.
20343
20344 if Present (Insert_Node) then
20345
20346 -- Case of loop statement. Verify that the range is part
20347 -- of the subtype indication of the iteration scheme.
20348
20349 if Nkind (Insert_Node) = N_Loop_Statement then
20350 declare
20351 Indic : Node_Id;
20352
20353 begin
20354 Indic := Parent (R);
20355 while Present (Indic)
20356 and then Nkind (Indic) /= N_Subtype_Indication
20357 loop
20358 Indic := Parent (Indic);
20359 end loop;
20360
20361 if Present (Indic) then
20362 Def_Id := Etype (Subtype_Mark (Indic));
20363
20364 Insert_Range_Checks
20365 (R_Checks,
20366 Insert_Node,
20367 Def_Id,
20368 Sloc (Insert_Node),
20369 R,
20370 Do_Before => True);
20371 end if;
20372 end;
20373
20374 -- Insertion before a declaration. If the declaration
20375 -- includes discriminants, the list of applicable checks
20376 -- is given by the caller.
20377
20378 elsif Nkind (Insert_Node) in N_Declaration then
20379 Def_Id := Defining_Identifier (Insert_Node);
20380
20381 if (Ekind (Def_Id) = E_Record_Type
20382 and then Depends_On_Discriminant (R))
20383 or else
20384 (Ekind (Def_Id) = E_Protected_Type
20385 and then Has_Discriminants (Def_Id))
20386 then
20387 Append_Range_Checks
20388 (R_Checks,
20389 Check_List, Def_Id, Sloc (Insert_Node), R);
20390
20391 else
20392 Insert_Range_Checks
20393 (R_Checks,
20394 Insert_Node, Def_Id, Sloc (Insert_Node), R);
20395
20396 end if;
20397
20398 -- Insertion before a statement. Range appears in the
20399 -- context of a quantified expression. Insertion will
20400 -- take place when expression is expanded.
20401
20402 else
20403 null;
20404 end if;
20405 end if;
20406 end if;
20407 end if;
20408
20409 -- Case of other than an explicit N_Range node
20410
20411 -- The forced evaluation removes side effects from expressions, which
20412 -- should occur also in GNATprove mode. Otherwise, we end up with
20413 -- unexpected insertions of actions at places where this is not
20414 -- supposed to occur, e.g. on default parameters of a call.
20415
20416 elsif Expander_Active or GNATprove_Mode then
20417 Get_Index_Bounds (R, Lo, Hi);
20418 Force_Evaluation (Lo);
20419 Force_Evaluation (Hi);
20420 end if;
20421 end Process_Range_Expr_In_Decl;
20422
20423 --------------------------------------
20424 -- Process_Real_Range_Specification --
20425 --------------------------------------
20426
20427 procedure Process_Real_Range_Specification (Def : Node_Id) is
20428 Spec : constant Node_Id := Real_Range_Specification (Def);
20429 Lo : Node_Id;
20430 Hi : Node_Id;
20431 Err : Boolean := False;
20432
20433 procedure Analyze_Bound (N : Node_Id);
20434 -- Analyze and check one bound
20435
20436 -------------------
20437 -- Analyze_Bound --
20438 -------------------
20439
20440 procedure Analyze_Bound (N : Node_Id) is
20441 begin
20442 Analyze_And_Resolve (N, Any_Real);
20443
20444 if not Is_OK_Static_Expression (N) then
20445 Flag_Non_Static_Expr
20446 ("bound in real type definition is not static!", N);
20447 Err := True;
20448 end if;
20449 end Analyze_Bound;
20450
20451 -- Start of processing for Process_Real_Range_Specification
20452
20453 begin
20454 if Present (Spec) then
20455 Lo := Low_Bound (Spec);
20456 Hi := High_Bound (Spec);
20457 Analyze_Bound (Lo);
20458 Analyze_Bound (Hi);
20459
20460 -- If error, clear away junk range specification
20461
20462 if Err then
20463 Set_Real_Range_Specification (Def, Empty);
20464 end if;
20465 end if;
20466 end Process_Real_Range_Specification;
20467
20468 ---------------------
20469 -- Process_Subtype --
20470 ---------------------
20471
20472 function Process_Subtype
20473 (S : Node_Id;
20474 Related_Nod : Node_Id;
20475 Related_Id : Entity_Id := Empty;
20476 Suffix : Character := ' ') return Entity_Id
20477 is
20478 P : Node_Id;
20479 Def_Id : Entity_Id;
20480 Error_Node : Node_Id;
20481 Full_View_Id : Entity_Id;
20482 Subtype_Mark_Id : Entity_Id;
20483
20484 May_Have_Null_Exclusion : Boolean;
20485
20486 procedure Check_Incomplete (T : Entity_Id);
20487 -- Called to verify that an incomplete type is not used prematurely
20488
20489 ----------------------
20490 -- Check_Incomplete --
20491 ----------------------
20492
20493 procedure Check_Incomplete (T : Entity_Id) is
20494 begin
20495 -- Ada 2005 (AI-412): Incomplete subtypes are legal
20496
20497 if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
20498 and then
20499 not (Ada_Version >= Ada_2005
20500 and then
20501 (Nkind (Parent (T)) = N_Subtype_Declaration
20502 or else (Nkind (Parent (T)) = N_Subtype_Indication
20503 and then Nkind (Parent (Parent (T))) =
20504 N_Subtype_Declaration)))
20505 then
20506 Error_Msg_N ("invalid use of type before its full declaration", T);
20507 end if;
20508 end Check_Incomplete;
20509
20510 -- Start of processing for Process_Subtype
20511
20512 begin
20513 -- Case of no constraints present
20514
20515 if Nkind (S) /= N_Subtype_Indication then
20516 Find_Type (S);
20517 Check_Incomplete (S);
20518 P := Parent (S);
20519
20520 -- Ada 2005 (AI-231): Static check
20521
20522 if Ada_Version >= Ada_2005
20523 and then Present (P)
20524 and then Null_Exclusion_Present (P)
20525 and then Nkind (P) /= N_Access_To_Object_Definition
20526 and then not Is_Access_Type (Entity (S))
20527 then
20528 Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
20529 end if;
20530
20531 -- The following is ugly, can't we have a range or even a flag???
20532
20533 May_Have_Null_Exclusion :=
20534 Nkind_In (P, N_Access_Definition,
20535 N_Access_Function_Definition,
20536 N_Access_Procedure_Definition,
20537 N_Access_To_Object_Definition,
20538 N_Allocator,
20539 N_Component_Definition)
20540 or else
20541 Nkind_In (P, N_Derived_Type_Definition,
20542 N_Discriminant_Specification,
20543 N_Formal_Object_Declaration,
20544 N_Object_Declaration,
20545 N_Object_Renaming_Declaration,
20546 N_Parameter_Specification,
20547 N_Subtype_Declaration);
20548
20549 -- Create an Itype that is a duplicate of Entity (S) but with the
20550 -- null-exclusion attribute.
20551
20552 if May_Have_Null_Exclusion
20553 and then Is_Access_Type (Entity (S))
20554 and then Null_Exclusion_Present (P)
20555
20556 -- No need to check the case of an access to object definition.
20557 -- It is correct to define double not-null pointers.
20558
20559 -- Example:
20560 -- type Not_Null_Int_Ptr is not null access Integer;
20561 -- type Acc is not null access Not_Null_Int_Ptr;
20562
20563 and then Nkind (P) /= N_Access_To_Object_Definition
20564 then
20565 if Can_Never_Be_Null (Entity (S)) then
20566 case Nkind (Related_Nod) is
20567 when N_Full_Type_Declaration =>
20568 if Nkind (Type_Definition (Related_Nod))
20569 in N_Array_Type_Definition
20570 then
20571 Error_Node :=
20572 Subtype_Indication
20573 (Component_Definition
20574 (Type_Definition (Related_Nod)));
20575 else
20576 Error_Node :=
20577 Subtype_Indication (Type_Definition (Related_Nod));
20578 end if;
20579
20580 when N_Subtype_Declaration =>
20581 Error_Node := Subtype_Indication (Related_Nod);
20582
20583 when N_Object_Declaration =>
20584 Error_Node := Object_Definition (Related_Nod);
20585
20586 when N_Component_Declaration =>
20587 Error_Node :=
20588 Subtype_Indication (Component_Definition (Related_Nod));
20589
20590 when N_Allocator =>
20591 Error_Node := Expression (Related_Nod);
20592
20593 when others =>
20594 pragma Assert (False);
20595 Error_Node := Related_Nod;
20596 end case;
20597
20598 Error_Msg_NE
20599 ("`NOT NULL` not allowed (& already excludes null)",
20600 Error_Node,
20601 Entity (S));
20602 end if;
20603
20604 Set_Etype (S,
20605 Create_Null_Excluding_Itype
20606 (T => Entity (S),
20607 Related_Nod => P));
20608 Set_Entity (S, Etype (S));
20609 end if;
20610
20611 return Entity (S);
20612
20613 -- Case of constraint present, so that we have an N_Subtype_Indication
20614 -- node (this node is created only if constraints are present).
20615
20616 else
20617 Find_Type (Subtype_Mark (S));
20618
20619 if Nkind (Parent (S)) /= N_Access_To_Object_Definition
20620 and then not
20621 (Nkind (Parent (S)) = N_Subtype_Declaration
20622 and then Is_Itype (Defining_Identifier (Parent (S))))
20623 then
20624 Check_Incomplete (Subtype_Mark (S));
20625 end if;
20626
20627 P := Parent (S);
20628 Subtype_Mark_Id := Entity (Subtype_Mark (S));
20629
20630 -- Explicit subtype declaration case
20631
20632 if Nkind (P) = N_Subtype_Declaration then
20633 Def_Id := Defining_Identifier (P);
20634
20635 -- Explicit derived type definition case
20636
20637 elsif Nkind (P) = N_Derived_Type_Definition then
20638 Def_Id := Defining_Identifier (Parent (P));
20639
20640 -- Implicit case, the Def_Id must be created as an implicit type.
20641 -- The one exception arises in the case of concurrent types, array
20642 -- and access types, where other subsidiary implicit types may be
20643 -- created and must appear before the main implicit type. In these
20644 -- cases we leave Def_Id set to Empty as a signal that Create_Itype
20645 -- has not yet been called to create Def_Id.
20646
20647 else
20648 if Is_Array_Type (Subtype_Mark_Id)
20649 or else Is_Concurrent_Type (Subtype_Mark_Id)
20650 or else Is_Access_Type (Subtype_Mark_Id)
20651 then
20652 Def_Id := Empty;
20653
20654 -- For the other cases, we create a new unattached Itype,
20655 -- and set the indication to ensure it gets attached later.
20656
20657 else
20658 Def_Id :=
20659 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
20660 end if;
20661 end if;
20662
20663 -- If the kind of constraint is invalid for this kind of type,
20664 -- then give an error, and then pretend no constraint was given.
20665
20666 if not Is_Valid_Constraint_Kind
20667 (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
20668 then
20669 Error_Msg_N
20670 ("incorrect constraint for this kind of type", Constraint (S));
20671
20672 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
20673
20674 -- Set Ekind of orphan itype, to prevent cascaded errors
20675
20676 if Present (Def_Id) then
20677 Set_Ekind (Def_Id, Ekind (Any_Type));
20678 end if;
20679
20680 -- Make recursive call, having got rid of the bogus constraint
20681
20682 return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
20683 end if;
20684
20685 -- Remaining processing depends on type. Select on Base_Type kind to
20686 -- ensure getting to the concrete type kind in the case of a private
20687 -- subtype (needed when only doing semantic analysis).
20688
20689 case Ekind (Base_Type (Subtype_Mark_Id)) is
20690 when Access_Kind =>
20691
20692 -- If this is a constraint on a class-wide type, discard it.
20693 -- There is currently no way to express a partial discriminant
20694 -- constraint on a type with unknown discriminants. This is
20695 -- a pathology that the ACATS wisely decides not to test.
20696
20697 if Is_Class_Wide_Type (Designated_Type (Subtype_Mark_Id)) then
20698 if Comes_From_Source (S) then
20699 Error_Msg_N
20700 ("constraint on class-wide type ignored??",
20701 Constraint (S));
20702 end if;
20703
20704 if Nkind (P) = N_Subtype_Declaration then
20705 Set_Subtype_Indication (P,
20706 New_Occurrence_Of (Subtype_Mark_Id, Sloc (S)));
20707 end if;
20708
20709 return Subtype_Mark_Id;
20710 end if;
20711
20712 Constrain_Access (Def_Id, S, Related_Nod);
20713
20714 if Expander_Active
20715 and then Is_Itype (Designated_Type (Def_Id))
20716 and then Nkind (Related_Nod) = N_Subtype_Declaration
20717 and then not Is_Incomplete_Type (Designated_Type (Def_Id))
20718 then
20719 Build_Itype_Reference
20720 (Designated_Type (Def_Id), Related_Nod);
20721 end if;
20722
20723 when Array_Kind =>
20724 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
20725
20726 when Decimal_Fixed_Point_Kind =>
20727 Constrain_Decimal (Def_Id, S);
20728
20729 when Enumeration_Kind =>
20730 Constrain_Enumeration (Def_Id, S);
20731 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
20732
20733 when Ordinary_Fixed_Point_Kind =>
20734 Constrain_Ordinary_Fixed (Def_Id, S);
20735
20736 when Float_Kind =>
20737 Constrain_Float (Def_Id, S);
20738
20739 when Integer_Kind =>
20740 Constrain_Integer (Def_Id, S);
20741 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
20742
20743 when E_Record_Type |
20744 E_Record_Subtype |
20745 Class_Wide_Kind |
20746 E_Incomplete_Type =>
20747 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
20748
20749 if Ekind (Def_Id) = E_Incomplete_Type then
20750 Set_Private_Dependents (Def_Id, New_Elmt_List);
20751 end if;
20752
20753 when Private_Kind =>
20754 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
20755 Set_Private_Dependents (Def_Id, New_Elmt_List);
20756
20757 -- In case of an invalid constraint prevent further processing
20758 -- since the type constructed is missing expected fields.
20759
20760 if Etype (Def_Id) = Any_Type then
20761 return Def_Id;
20762 end if;
20763
20764 -- If the full view is that of a task with discriminants,
20765 -- we must constrain both the concurrent type and its
20766 -- corresponding record type. Otherwise we will just propagate
20767 -- the constraint to the full view, if available.
20768
20769 if Present (Full_View (Subtype_Mark_Id))
20770 and then Has_Discriminants (Subtype_Mark_Id)
20771 and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
20772 then
20773 Full_View_Id :=
20774 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
20775
20776 Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
20777 Constrain_Concurrent (Full_View_Id, S,
20778 Related_Nod, Related_Id, Suffix);
20779 Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
20780 Set_Full_View (Def_Id, Full_View_Id);
20781
20782 -- Introduce an explicit reference to the private subtype,
20783 -- to prevent scope anomalies in gigi if first use appears
20784 -- in a nested context, e.g. a later function body.
20785 -- Should this be generated in other contexts than a full
20786 -- type declaration?
20787
20788 if Is_Itype (Def_Id)
20789 and then
20790 Nkind (Parent (P)) = N_Full_Type_Declaration
20791 then
20792 Build_Itype_Reference (Def_Id, Parent (P));
20793 end if;
20794
20795 else
20796 Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
20797 end if;
20798
20799 when Concurrent_Kind =>
20800 Constrain_Concurrent (Def_Id, S,
20801 Related_Nod, Related_Id, Suffix);
20802
20803 when others =>
20804 Error_Msg_N ("invalid subtype mark in subtype indication", S);
20805 end case;
20806
20807 -- Size and Convention are always inherited from the base type
20808
20809 Set_Size_Info (Def_Id, (Subtype_Mark_Id));
20810 Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
20811
20812 return Def_Id;
20813 end if;
20814 end Process_Subtype;
20815
20816 --------------------------------------------
20817 -- Propagate_Default_Init_Cond_Attributes --
20818 --------------------------------------------
20819
20820 procedure Propagate_Default_Init_Cond_Attributes
20821 (From_Typ : Entity_Id;
20822 To_Typ : Entity_Id;
20823 Parent_To_Derivation : Boolean := False;
20824 Private_To_Full_View : Boolean := False)
20825 is
20826 procedure Remove_Default_Init_Cond_Procedure (Typ : Entity_Id);
20827 -- Remove the default initial procedure (if any) from the rep chain of
20828 -- type Typ.
20829
20830 ----------------------------------------
20831 -- Remove_Default_Init_Cond_Procedure --
20832 ----------------------------------------
20833
20834 procedure Remove_Default_Init_Cond_Procedure (Typ : Entity_Id) is
20835 Found : Boolean := False;
20836 Prev : Entity_Id;
20837 Subp : Entity_Id;
20838
20839 begin
20840 Prev := Typ;
20841 Subp := Subprograms_For_Type (Typ);
20842 while Present (Subp) loop
20843 if Is_Default_Init_Cond_Procedure (Subp) then
20844 Found := True;
20845 exit;
20846 end if;
20847
20848 Prev := Subp;
20849 Subp := Subprograms_For_Type (Subp);
20850 end loop;
20851
20852 if Found then
20853 Set_Subprograms_For_Type (Prev, Subprograms_For_Type (Subp));
20854 Set_Subprograms_For_Type (Subp, Empty);
20855 end if;
20856 end Remove_Default_Init_Cond_Procedure;
20857
20858 -- Local variables
20859
20860 Inherit_Procedure : Boolean := False;
20861
20862 -- Start of processing for Propagate_Default_Init_Cond_Attributes
20863
20864 begin
20865 if Has_Default_Init_Cond (From_Typ) then
20866
20867 -- A derived type inherits the attributes from its parent type
20868
20869 if Parent_To_Derivation then
20870 Set_Has_Inherited_Default_Init_Cond (To_Typ);
20871
20872 -- A full view shares the attributes with its private view
20873
20874 else
20875 Set_Has_Default_Init_Cond (To_Typ);
20876 end if;
20877
20878 Inherit_Procedure := True;
20879
20880 -- Due to the order of expansion, a derived private type is processed
20881 -- by two routines which both attempt to set the attributes related
20882 -- to pragma Default_Initial_Condition - Build_Derived_Type and then
20883 -- Process_Full_View.
20884
20885 -- package Pack is
20886 -- type Parent_Typ is private
20887 -- with Default_Initial_Condition ...;
20888 -- private
20889 -- type Parent_Typ is ...;
20890 -- end Pack;
20891
20892 -- with Pack; use Pack;
20893 -- package Pack_2 is
20894 -- type Deriv_Typ is private
20895 -- with Default_Initial_Condition ...;
20896 -- private
20897 -- type Deriv_Typ is new Parent_Typ;
20898 -- end Pack_2;
20899
20900 -- When Build_Derived_Type operates, it sets the attributes on the
20901 -- full view without taking into account that the private view may
20902 -- define its own default initial condition procedure. This becomes
20903 -- apparent in Process_Full_View which must undo some of the work by
20904 -- Build_Derived_Type and propagate the attributes from the private
20905 -- to the full view.
20906
20907 if Private_To_Full_View then
20908 Set_Has_Inherited_Default_Init_Cond (To_Typ, False);
20909 Remove_Default_Init_Cond_Procedure (To_Typ);
20910 end if;
20911
20912 -- A type must inherit the default initial condition procedure from a
20913 -- parent type when the parent itself is inheriting the procedure or
20914 -- when it is defining one. This circuitry is also used when dealing
20915 -- with the private / full view of a type.
20916
20917 elsif Has_Inherited_Default_Init_Cond (From_Typ)
20918 or (Parent_To_Derivation
20919 and Present (Get_Pragma
20920 (From_Typ, Pragma_Default_Initial_Condition)))
20921 then
20922 Set_Has_Inherited_Default_Init_Cond (To_Typ);
20923 Inherit_Procedure := True;
20924 end if;
20925
20926 if Inherit_Procedure
20927 and then No (Default_Init_Cond_Procedure (To_Typ))
20928 then
20929 Set_Default_Init_Cond_Procedure
20930 (To_Typ, Default_Init_Cond_Procedure (From_Typ));
20931 end if;
20932 end Propagate_Default_Init_Cond_Attributes;
20933
20934 -----------------------------
20935 -- Record_Type_Declaration --
20936 -----------------------------
20937
20938 procedure Record_Type_Declaration
20939 (T : Entity_Id;
20940 N : Node_Id;
20941 Prev : Entity_Id)
20942 is
20943 Def : constant Node_Id := Type_Definition (N);
20944 Is_Tagged : Boolean;
20945 Tag_Comp : Entity_Id;
20946
20947 begin
20948 -- These flags must be initialized before calling Process_Discriminants
20949 -- because this routine makes use of them.
20950
20951 Set_Ekind (T, E_Record_Type);
20952 Set_Etype (T, T);
20953 Init_Size_Align (T);
20954 Set_Interfaces (T, No_Elist);
20955 Set_Stored_Constraint (T, No_Elist);
20956 Set_Default_SSO (T);
20957
20958 -- Normal case
20959
20960 if Ada_Version < Ada_2005 or else not Interface_Present (Def) then
20961 if Limited_Present (Def) then
20962 Check_SPARK_05_Restriction ("limited is not allowed", N);
20963 end if;
20964
20965 if Abstract_Present (Def) then
20966 Check_SPARK_05_Restriction ("abstract is not allowed", N);
20967 end if;
20968
20969 -- The flag Is_Tagged_Type might have already been set by
20970 -- Find_Type_Name if it detected an error for declaration T. This
20971 -- arises in the case of private tagged types where the full view
20972 -- omits the word tagged.
20973
20974 Is_Tagged :=
20975 Tagged_Present (Def)
20976 or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
20977
20978 Set_Is_Limited_Record (T, Limited_Present (Def));
20979
20980 if Is_Tagged then
20981 Set_Is_Tagged_Type (T, True);
20982 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
20983 end if;
20984
20985 -- Type is abstract if full declaration carries keyword, or if
20986 -- previous partial view did.
20987
20988 Set_Is_Abstract_Type (T, Is_Abstract_Type (T)
20989 or else Abstract_Present (Def));
20990
20991 else
20992 Check_SPARK_05_Restriction ("interface is not allowed", N);
20993
20994 Is_Tagged := True;
20995 Analyze_Interface_Declaration (T, Def);
20996
20997 if Present (Discriminant_Specifications (N)) then
20998 Error_Msg_N
20999 ("interface types cannot have discriminants",
21000 Defining_Identifier
21001 (First (Discriminant_Specifications (N))));
21002 end if;
21003 end if;
21004
21005 -- First pass: if there are self-referential access components,
21006 -- create the required anonymous access type declarations, and if
21007 -- need be an incomplete type declaration for T itself.
21008
21009 Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
21010
21011 if Ada_Version >= Ada_2005
21012 and then Present (Interface_List (Def))
21013 then
21014 Check_Interfaces (N, Def);
21015
21016 declare
21017 Ifaces_List : Elist_Id;
21018
21019 begin
21020 -- Ada 2005 (AI-251): Collect the list of progenitors that are not
21021 -- already in the parents.
21022
21023 Collect_Interfaces
21024 (T => T,
21025 Ifaces_List => Ifaces_List,
21026 Exclude_Parents => True);
21027
21028 Set_Interfaces (T, Ifaces_List);
21029 end;
21030 end if;
21031
21032 -- Records constitute a scope for the component declarations within.
21033 -- The scope is created prior to the processing of these declarations.
21034 -- Discriminants are processed first, so that they are visible when
21035 -- processing the other components. The Ekind of the record type itself
21036 -- is set to E_Record_Type (subtypes appear as E_Record_Subtype).
21037
21038 -- Enter record scope
21039
21040 Push_Scope (T);
21041
21042 -- If an incomplete or private type declaration was already given for
21043 -- the type, then this scope already exists, and the discriminants have
21044 -- been declared within. We must verify that the full declaration
21045 -- matches the incomplete one.
21046
21047 Check_Or_Process_Discriminants (N, T, Prev);
21048
21049 Set_Is_Constrained (T, not Has_Discriminants (T));
21050 Set_Has_Delayed_Freeze (T, True);
21051
21052 -- For tagged types add a manually analyzed component corresponding
21053 -- to the component _tag, the corresponding piece of tree will be
21054 -- expanded as part of the freezing actions if it is not a CPP_Class.
21055
21056 if Is_Tagged then
21057
21058 -- Do not add the tag unless we are in expansion mode
21059
21060 if Expander_Active then
21061 Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
21062 Enter_Name (Tag_Comp);
21063
21064 Set_Ekind (Tag_Comp, E_Component);
21065 Set_Is_Tag (Tag_Comp);
21066 Set_Is_Aliased (Tag_Comp);
21067 Set_Etype (Tag_Comp, RTE (RE_Tag));
21068 Set_DT_Entry_Count (Tag_Comp, No_Uint);
21069 Set_Original_Record_Component (Tag_Comp, Tag_Comp);
21070 Init_Component_Location (Tag_Comp);
21071
21072 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
21073 -- implemented interfaces.
21074
21075 if Has_Interfaces (T) then
21076 Add_Interface_Tag_Components (N, T);
21077 end if;
21078 end if;
21079
21080 Make_Class_Wide_Type (T);
21081 Set_Direct_Primitive_Operations (T, New_Elmt_List);
21082 end if;
21083
21084 -- We must suppress range checks when processing record components in
21085 -- the presence of discriminants, since we don't want spurious checks to
21086 -- be generated during their analysis, but Suppress_Range_Checks flags
21087 -- must be reset the after processing the record definition.
21088
21089 -- Note: this is the only use of Kill_Range_Checks, and is a bit odd,
21090 -- couldn't we just use the normal range check suppression method here.
21091 -- That would seem cleaner ???
21092
21093 if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
21094 Set_Kill_Range_Checks (T, True);
21095 Record_Type_Definition (Def, Prev);
21096 Set_Kill_Range_Checks (T, False);
21097 else
21098 Record_Type_Definition (Def, Prev);
21099 end if;
21100
21101 -- Exit from record scope
21102
21103 End_Scope;
21104
21105 -- Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
21106 -- the implemented interfaces and associate them an aliased entity.
21107
21108 if Is_Tagged
21109 and then not Is_Empty_List (Interface_List (Def))
21110 then
21111 Derive_Progenitor_Subprograms (T, T);
21112 end if;
21113
21114 Check_Function_Writable_Actuals (N);
21115 end Record_Type_Declaration;
21116
21117 ----------------------------
21118 -- Record_Type_Definition --
21119 ----------------------------
21120
21121 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
21122 Component : Entity_Id;
21123 Ctrl_Components : Boolean := False;
21124 Final_Storage_Only : Boolean;
21125 T : Entity_Id;
21126
21127 begin
21128 if Ekind (Prev_T) = E_Incomplete_Type then
21129 T := Full_View (Prev_T);
21130 else
21131 T := Prev_T;
21132 end if;
21133
21134 -- In SPARK, tagged types and type extensions may only be declared in
21135 -- the specification of library unit packages.
21136
21137 if Present (Def) and then Is_Tagged_Type (T) then
21138 declare
21139 Typ : Node_Id;
21140 Ctxt : Node_Id;
21141
21142 begin
21143 if Nkind (Parent (Def)) = N_Full_Type_Declaration then
21144 Typ := Parent (Def);
21145 else
21146 pragma Assert
21147 (Nkind (Parent (Def)) = N_Derived_Type_Definition);
21148 Typ := Parent (Parent (Def));
21149 end if;
21150
21151 Ctxt := Parent (Typ);
21152
21153 if Nkind (Ctxt) = N_Package_Body
21154 and then Nkind (Parent (Ctxt)) = N_Compilation_Unit
21155 then
21156 Check_SPARK_05_Restriction
21157 ("type should be defined in package specification", Typ);
21158
21159 elsif Nkind (Ctxt) /= N_Package_Specification
21160 or else Nkind (Parent (Parent (Ctxt))) /= N_Compilation_Unit
21161 then
21162 Check_SPARK_05_Restriction
21163 ("type should be defined in library unit package", Typ);
21164 end if;
21165 end;
21166 end if;
21167
21168 Final_Storage_Only := not Is_Controlled (T);
21169
21170 -- Ada 2005: Check whether an explicit Limited is present in a derived
21171 -- type declaration.
21172
21173 if Nkind (Parent (Def)) = N_Derived_Type_Definition
21174 and then Limited_Present (Parent (Def))
21175 then
21176 Set_Is_Limited_Record (T);
21177 end if;
21178
21179 -- If the component list of a record type is defined by the reserved
21180 -- word null and there is no discriminant part, then the record type has
21181 -- no components and all records of the type are null records (RM 3.7)
21182 -- This procedure is also called to process the extension part of a
21183 -- record extension, in which case the current scope may have inherited
21184 -- components.
21185
21186 if No (Def)
21187 or else No (Component_List (Def))
21188 or else Null_Present (Component_List (Def))
21189 then
21190 if not Is_Tagged_Type (T) then
21191 Check_SPARK_05_Restriction ("untagged record cannot be null", Def);
21192 end if;
21193
21194 else
21195 Analyze_Declarations (Component_Items (Component_List (Def)));
21196
21197 if Present (Variant_Part (Component_List (Def))) then
21198 Check_SPARK_05_Restriction ("variant part is not allowed", Def);
21199 Analyze (Variant_Part (Component_List (Def)));
21200 end if;
21201 end if;
21202
21203 -- After completing the semantic analysis of the record definition,
21204 -- record components, both new and inherited, are accessible. Set their
21205 -- kind accordingly. Exclude malformed itypes from illegal declarations,
21206 -- whose Ekind may be void.
21207
21208 Component := First_Entity (Current_Scope);
21209 while Present (Component) loop
21210 if Ekind (Component) = E_Void
21211 and then not Is_Itype (Component)
21212 then
21213 Set_Ekind (Component, E_Component);
21214 Init_Component_Location (Component);
21215 end if;
21216
21217 if Has_Task (Etype (Component)) then
21218 Set_Has_Task (T);
21219 end if;
21220
21221 if Has_Protected (Etype (Component)) then
21222 Set_Has_Protected (T);
21223 end if;
21224
21225 if Ekind (Component) /= E_Component then
21226 null;
21227
21228 -- Do not set Has_Controlled_Component on a class-wide equivalent
21229 -- type. See Make_CW_Equivalent_Type.
21230
21231 elsif not Is_Class_Wide_Equivalent_Type (T)
21232 and then (Has_Controlled_Component (Etype (Component))
21233 or else (Chars (Component) /= Name_uParent
21234 and then Is_Controlled (Etype (Component))))
21235 then
21236 Set_Has_Controlled_Component (T, True);
21237 Final_Storage_Only :=
21238 Final_Storage_Only
21239 and then Finalize_Storage_Only (Etype (Component));
21240 Ctrl_Components := True;
21241 end if;
21242
21243 Next_Entity (Component);
21244 end loop;
21245
21246 -- A Type is Finalize_Storage_Only only if all its controlled components
21247 -- are also.
21248
21249 if Ctrl_Components then
21250 Set_Finalize_Storage_Only (T, Final_Storage_Only);
21251 end if;
21252
21253 -- Place reference to end record on the proper entity, which may
21254 -- be a partial view.
21255
21256 if Present (Def) then
21257 Process_End_Label (Def, 'e', Prev_T);
21258 end if;
21259 end Record_Type_Definition;
21260
21261 ------------------------
21262 -- Replace_Components --
21263 ------------------------
21264
21265 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id) is
21266 function Process (N : Node_Id) return Traverse_Result;
21267
21268 -------------
21269 -- Process --
21270 -------------
21271
21272 function Process (N : Node_Id) return Traverse_Result is
21273 Comp : Entity_Id;
21274
21275 begin
21276 if Nkind (N) = N_Discriminant_Specification then
21277 Comp := First_Discriminant (Typ);
21278 while Present (Comp) loop
21279 if Chars (Comp) = Chars (Defining_Identifier (N)) then
21280 Set_Defining_Identifier (N, Comp);
21281 exit;
21282 end if;
21283
21284 Next_Discriminant (Comp);
21285 end loop;
21286
21287 elsif Nkind (N) = N_Component_Declaration then
21288 Comp := First_Component (Typ);
21289 while Present (Comp) loop
21290 if Chars (Comp) = Chars (Defining_Identifier (N)) then
21291 Set_Defining_Identifier (N, Comp);
21292 exit;
21293 end if;
21294
21295 Next_Component (Comp);
21296 end loop;
21297 end if;
21298
21299 return OK;
21300 end Process;
21301
21302 procedure Replace is new Traverse_Proc (Process);
21303
21304 -- Start of processing for Replace_Components
21305
21306 begin
21307 Replace (Decl);
21308 end Replace_Components;
21309
21310 -------------------------------
21311 -- Set_Completion_Referenced --
21312 -------------------------------
21313
21314 procedure Set_Completion_Referenced (E : Entity_Id) is
21315 begin
21316 -- If in main unit, mark entity that is a completion as referenced,
21317 -- warnings go on the partial view when needed.
21318
21319 if In_Extended_Main_Source_Unit (E) then
21320 Set_Referenced (E);
21321 end if;
21322 end Set_Completion_Referenced;
21323
21324 ---------------------
21325 -- Set_Default_SSO --
21326 ---------------------
21327
21328 procedure Set_Default_SSO (T : Entity_Id) is
21329 begin
21330 case Opt.Default_SSO is
21331 when ' ' =>
21332 null;
21333 when 'L' =>
21334 Set_SSO_Set_Low_By_Default (T, True);
21335 when 'H' =>
21336 Set_SSO_Set_High_By_Default (T, True);
21337 when others =>
21338 raise Program_Error;
21339 end case;
21340 end Set_Default_SSO;
21341
21342 ---------------------
21343 -- Set_Fixed_Range --
21344 ---------------------
21345
21346 -- The range for fixed-point types is complicated by the fact that we
21347 -- do not know the exact end points at the time of the declaration. This
21348 -- is true for three reasons:
21349
21350 -- A size clause may affect the fudging of the end-points.
21351 -- A small clause may affect the values of the end-points.
21352 -- We try to include the end-points if it does not affect the size.
21353
21354 -- This means that the actual end-points must be established at the
21355 -- point when the type is frozen. Meanwhile, we first narrow the range
21356 -- as permitted (so that it will fit if necessary in a small specified
21357 -- size), and then build a range subtree with these narrowed bounds.
21358 -- Set_Fixed_Range constructs the range from real literal values, and
21359 -- sets the range as the Scalar_Range of the given fixed-point type entity.
21360
21361 -- The parent of this range is set to point to the entity so that it is
21362 -- properly hooked into the tree (unlike normal Scalar_Range entries for
21363 -- other scalar types, which are just pointers to the range in the
21364 -- original tree, this would otherwise be an orphan).
21365
21366 -- The tree is left unanalyzed. When the type is frozen, the processing
21367 -- in Freeze.Freeze_Fixed_Point_Type notices that the range is not
21368 -- analyzed, and uses this as an indication that it should complete
21369 -- work on the range (it will know the final small and size values).
21370
21371 procedure Set_Fixed_Range
21372 (E : Entity_Id;
21373 Loc : Source_Ptr;
21374 Lo : Ureal;
21375 Hi : Ureal)
21376 is
21377 S : constant Node_Id :=
21378 Make_Range (Loc,
21379 Low_Bound => Make_Real_Literal (Loc, Lo),
21380 High_Bound => Make_Real_Literal (Loc, Hi));
21381 begin
21382 Set_Scalar_Range (E, S);
21383 Set_Parent (S, E);
21384
21385 -- Before the freeze point, the bounds of a fixed point are universal
21386 -- and carry the corresponding type.
21387
21388 Set_Etype (Low_Bound (S), Universal_Real);
21389 Set_Etype (High_Bound (S), Universal_Real);
21390 end Set_Fixed_Range;
21391
21392 ----------------------------------
21393 -- Set_Scalar_Range_For_Subtype --
21394 ----------------------------------
21395
21396 procedure Set_Scalar_Range_For_Subtype
21397 (Def_Id : Entity_Id;
21398 R : Node_Id;
21399 Subt : Entity_Id)
21400 is
21401 Kind : constant Entity_Kind := Ekind (Def_Id);
21402
21403 begin
21404 -- Defend against previous error
21405
21406 if Nkind (R) = N_Error then
21407 return;
21408 end if;
21409
21410 Set_Scalar_Range (Def_Id, R);
21411
21412 -- We need to link the range into the tree before resolving it so
21413 -- that types that are referenced, including importantly the subtype
21414 -- itself, are properly frozen (Freeze_Expression requires that the
21415 -- expression be properly linked into the tree). Of course if it is
21416 -- already linked in, then we do not disturb the current link.
21417
21418 if No (Parent (R)) then
21419 Set_Parent (R, Def_Id);
21420 end if;
21421
21422 -- Reset the kind of the subtype during analysis of the range, to
21423 -- catch possible premature use in the bounds themselves.
21424
21425 Set_Ekind (Def_Id, E_Void);
21426 Process_Range_Expr_In_Decl (R, Subt, Subtyp => Def_Id);
21427 Set_Ekind (Def_Id, Kind);
21428 end Set_Scalar_Range_For_Subtype;
21429
21430 --------------------------------------------------------
21431 -- Set_Stored_Constraint_From_Discriminant_Constraint --
21432 --------------------------------------------------------
21433
21434 procedure Set_Stored_Constraint_From_Discriminant_Constraint
21435 (E : Entity_Id)
21436 is
21437 begin
21438 -- Make sure set if encountered during Expand_To_Stored_Constraint
21439
21440 Set_Stored_Constraint (E, No_Elist);
21441
21442 -- Give it the right value
21443
21444 if Is_Constrained (E) and then Has_Discriminants (E) then
21445 Set_Stored_Constraint (E,
21446 Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
21447 end if;
21448 end Set_Stored_Constraint_From_Discriminant_Constraint;
21449
21450 -------------------------------------
21451 -- Signed_Integer_Type_Declaration --
21452 -------------------------------------
21453
21454 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
21455 Implicit_Base : Entity_Id;
21456 Base_Typ : Entity_Id;
21457 Lo_Val : Uint;
21458 Hi_Val : Uint;
21459 Errs : Boolean := False;
21460 Lo : Node_Id;
21461 Hi : Node_Id;
21462
21463 function Can_Derive_From (E : Entity_Id) return Boolean;
21464 -- Determine whether given bounds allow derivation from specified type
21465
21466 procedure Check_Bound (Expr : Node_Id);
21467 -- Check bound to make sure it is integral and static. If not, post
21468 -- appropriate error message and set Errs flag
21469
21470 ---------------------
21471 -- Can_Derive_From --
21472 ---------------------
21473
21474 -- Note we check both bounds against both end values, to deal with
21475 -- strange types like ones with a range of 0 .. -12341234.
21476
21477 function Can_Derive_From (E : Entity_Id) return Boolean is
21478 Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
21479 Hi : constant Uint := Expr_Value (Type_High_Bound (E));
21480 begin
21481 return Lo <= Lo_Val and then Lo_Val <= Hi
21482 and then
21483 Lo <= Hi_Val and then Hi_Val <= Hi;
21484 end Can_Derive_From;
21485
21486 -----------------
21487 -- Check_Bound --
21488 -----------------
21489
21490 procedure Check_Bound (Expr : Node_Id) is
21491 begin
21492 -- If a range constraint is used as an integer type definition, each
21493 -- bound of the range must be defined by a static expression of some
21494 -- integer type, but the two bounds need not have the same integer
21495 -- type (Negative bounds are allowed.) (RM 3.5.4)
21496
21497 if not Is_Integer_Type (Etype (Expr)) then
21498 Error_Msg_N
21499 ("integer type definition bounds must be of integer type", Expr);
21500 Errs := True;
21501
21502 elsif not Is_OK_Static_Expression (Expr) then
21503 Flag_Non_Static_Expr
21504 ("non-static expression used for integer type bound!", Expr);
21505 Errs := True;
21506
21507 -- The bounds are folded into literals, and we set their type to be
21508 -- universal, to avoid typing difficulties: we cannot set the type
21509 -- of the literal to the new type, because this would be a forward
21510 -- reference for the back end, and if the original type is user-
21511 -- defined this can lead to spurious semantic errors (e.g. 2928-003).
21512
21513 else
21514 if Is_Entity_Name (Expr) then
21515 Fold_Uint (Expr, Expr_Value (Expr), True);
21516 end if;
21517
21518 Set_Etype (Expr, Universal_Integer);
21519 end if;
21520 end Check_Bound;
21521
21522 -- Start of processing for Signed_Integer_Type_Declaration
21523
21524 begin
21525 -- Create an anonymous base type
21526
21527 Implicit_Base :=
21528 Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
21529
21530 -- Analyze and check the bounds, they can be of any integer type
21531
21532 Lo := Low_Bound (Def);
21533 Hi := High_Bound (Def);
21534
21535 -- Arbitrarily use Integer as the type if either bound had an error
21536
21537 if Hi = Error or else Lo = Error then
21538 Base_Typ := Any_Integer;
21539 Set_Error_Posted (T, True);
21540
21541 -- Here both bounds are OK expressions
21542
21543 else
21544 Analyze_And_Resolve (Lo, Any_Integer);
21545 Analyze_And_Resolve (Hi, Any_Integer);
21546
21547 Check_Bound (Lo);
21548 Check_Bound (Hi);
21549
21550 if Errs then
21551 Hi := Type_High_Bound (Standard_Long_Long_Integer);
21552 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
21553 end if;
21554
21555 -- Find type to derive from
21556
21557 Lo_Val := Expr_Value (Lo);
21558 Hi_Val := Expr_Value (Hi);
21559
21560 if Can_Derive_From (Standard_Short_Short_Integer) then
21561 Base_Typ := Base_Type (Standard_Short_Short_Integer);
21562
21563 elsif Can_Derive_From (Standard_Short_Integer) then
21564 Base_Typ := Base_Type (Standard_Short_Integer);
21565
21566 elsif Can_Derive_From (Standard_Integer) then
21567 Base_Typ := Base_Type (Standard_Integer);
21568
21569 elsif Can_Derive_From (Standard_Long_Integer) then
21570 Base_Typ := Base_Type (Standard_Long_Integer);
21571
21572 elsif Can_Derive_From (Standard_Long_Long_Integer) then
21573 Check_Restriction (No_Long_Long_Integers, Def);
21574 Base_Typ := Base_Type (Standard_Long_Long_Integer);
21575
21576 else
21577 Base_Typ := Base_Type (Standard_Long_Long_Integer);
21578 Error_Msg_N ("integer type definition bounds out of range", Def);
21579 Hi := Type_High_Bound (Standard_Long_Long_Integer);
21580 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
21581 end if;
21582 end if;
21583
21584 -- Complete both implicit base and declared first subtype entities. The
21585 -- inheritance of the rep item chain ensures that SPARK-related pragmas
21586 -- are not clobbered when the signed integer type acts as a full view of
21587 -- a private type.
21588
21589 Set_Etype (Implicit_Base, Base_Typ);
21590 Set_Size_Info (Implicit_Base, Base_Typ);
21591 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
21592 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
21593 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
21594
21595 Set_Ekind (T, E_Signed_Integer_Subtype);
21596 Set_Etype (T, Implicit_Base);
21597 Set_Size_Info (T, Implicit_Base);
21598 Inherit_Rep_Item_Chain (T, Implicit_Base);
21599 Set_Scalar_Range (T, Def);
21600 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
21601 Set_Is_Constrained (T);
21602 end Signed_Integer_Type_Declaration;
21603
21604 end Sem_Ch3;