[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-2017, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
25
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Contracts; use Contracts;
30 with Debug; use Debug;
31 with Elists; use Elists;
32 with Einfo; use Einfo;
33 with Errout; use Errout;
34 with Eval_Fat; use Eval_Fat;
35 with Exp_Ch3; use Exp_Ch3;
36 with Exp_Ch9; use Exp_Ch9;
37 with Exp_Disp; use Exp_Disp;
38 with Exp_Dist; use Exp_Dist;
39 with Exp_Tss; use Exp_Tss;
40 with Exp_Util; use Exp_Util;
41 with Fname; use Fname;
42 with Freeze; use Freeze;
43 with Ghost; use Ghost;
44 with Itypes; use Itypes;
45 with Layout; use Layout;
46 with Lib; use Lib;
47 with Lib.Xref; use Lib.Xref;
48 with Namet; use Namet;
49 with Nmake; use Nmake;
50 with Opt; use Opt;
51 with Restrict; use Restrict;
52 with Rident; use Rident;
53 with Rtsfind; use Rtsfind;
54 with Sem; use Sem;
55 with Sem_Aux; use Sem_Aux;
56 with Sem_Case; use Sem_Case;
57 with Sem_Cat; use Sem_Cat;
58 with Sem_Ch6; use Sem_Ch6;
59 with Sem_Ch7; use Sem_Ch7;
60 with Sem_Ch8; use Sem_Ch8;
61 with Sem_Ch13; use Sem_Ch13;
62 with Sem_Dim; use Sem_Dim;
63 with Sem_Disp; use Sem_Disp;
64 with Sem_Dist; use Sem_Dist;
65 with Sem_Elim; use Sem_Elim;
66 with Sem_Eval; use Sem_Eval;
67 with Sem_Mech; use Sem_Mech;
68 with Sem_Res; use Sem_Res;
69 with Sem_Smem; use Sem_Smem;
70 with Sem_Type; use Sem_Type;
71 with Sem_Util; use Sem_Util;
72 with Sem_Warn; use Sem_Warn;
73 with Stand; use Stand;
74 with Sinfo; use Sinfo;
75 with Sinput; use Sinput;
76 with Snames; use Snames;
77 with Targparm; use Targparm;
78 with Tbuild; use Tbuild;
79 with Ttypes; use Ttypes;
80 with Uintp; use Uintp;
81 with Urealp; use Urealp;
82
83 package body Sem_Ch3 is
84
85 -----------------------
86 -- Local Subprograms --
87 -----------------------
88
89 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id);
90 -- Ada 2005 (AI-251): Add the tag components corresponding to all the
91 -- abstract interface types implemented by a record type or a derived
92 -- record type.
93
94 procedure Build_Derived_Type
95 (N : Node_Id;
96 Parent_Type : Entity_Id;
97 Derived_Type : Entity_Id;
98 Is_Completion : Boolean;
99 Derive_Subps : Boolean := True);
100 -- Create and decorate a Derived_Type given the Parent_Type entity. N is
101 -- the N_Full_Type_Declaration node containing the derived type definition.
102 -- Parent_Type is the entity for the parent type in the derived type
103 -- definition and Derived_Type the actual derived type. Is_Completion must
104 -- be set to False if Derived_Type is the N_Defining_Identifier node in N
105 -- (i.e. Derived_Type = Defining_Identifier (N)). In this case N is not the
106 -- completion of a private type declaration. If Is_Completion is set to
107 -- True, N is the completion of a private type declaration and Derived_Type
108 -- is different from the defining identifier inside N (i.e. Derived_Type /=
109 -- Defining_Identifier (N)). Derive_Subps indicates whether the parent
110 -- subprograms should be derived. The only case where this parameter is
111 -- False is when Build_Derived_Type is recursively called to process an
112 -- implicit derived full type for a type derived from a private type (in
113 -- that case the subprograms must only be derived for the private view of
114 -- the type).
115 --
116 -- ??? These flags need a bit of re-examination and re-documentation:
117 -- ??? are they both necessary (both seem related to the recursion)?
118
119 procedure Build_Derived_Access_Type
120 (N : Node_Id;
121 Parent_Type : Entity_Id;
122 Derived_Type : Entity_Id);
123 -- Subsidiary procedure to Build_Derived_Type. For a derived access type,
124 -- create an implicit base if the parent type is constrained or if the
125 -- subtype indication has a constraint.
126
127 procedure Build_Derived_Array_Type
128 (N : Node_Id;
129 Parent_Type : Entity_Id;
130 Derived_Type : Entity_Id);
131 -- Subsidiary procedure to Build_Derived_Type. For a derived array type,
132 -- create an implicit base if the parent type is constrained or if the
133 -- subtype indication has a constraint.
134
135 procedure Build_Derived_Concurrent_Type
136 (N : Node_Id;
137 Parent_Type : Entity_Id;
138 Derived_Type : Entity_Id);
139 -- Subsidiary procedure to Build_Derived_Type. For a derived task or
140 -- protected type, inherit entries and protected subprograms, check
141 -- legality of discriminant constraints if any.
142
143 procedure Build_Derived_Enumeration_Type
144 (N : Node_Id;
145 Parent_Type : Entity_Id;
146 Derived_Type : Entity_Id);
147 -- Subsidiary procedure to Build_Derived_Type. For a derived enumeration
148 -- type, we must create a new list of literals. Types derived from
149 -- Character and [Wide_]Wide_Character are special-cased.
150
151 procedure Build_Derived_Numeric_Type
152 (N : Node_Id;
153 Parent_Type : Entity_Id;
154 Derived_Type : Entity_Id);
155 -- Subsidiary procedure to Build_Derived_Type. For numeric types, create
156 -- an anonymous base type, and propagate constraint to subtype if needed.
157
158 procedure Build_Derived_Private_Type
159 (N : Node_Id;
160 Parent_Type : Entity_Id;
161 Derived_Type : Entity_Id;
162 Is_Completion : Boolean;
163 Derive_Subps : Boolean := True);
164 -- Subsidiary procedure to Build_Derived_Type. This procedure is complex
165 -- because the parent may or may not have a completion, and the derivation
166 -- may itself be a completion.
167
168 procedure Build_Derived_Record_Type
169 (N : Node_Id;
170 Parent_Type : Entity_Id;
171 Derived_Type : Entity_Id;
172 Derive_Subps : Boolean := True);
173 -- Subsidiary procedure used for tagged and untagged record types
174 -- by Build_Derived_Type and Analyze_Private_Extension_Declaration.
175 -- All parameters are as in Build_Derived_Type except that N, in
176 -- addition to being an N_Full_Type_Declaration node, can also be an
177 -- N_Private_Extension_Declaration node. See the definition of this routine
178 -- for much more info. Derive_Subps indicates whether subprograms should be
179 -- derived from the parent type. The only case where Derive_Subps is False
180 -- is for an implicit derived full type for a type derived from a private
181 -- type (see Build_Derived_Type).
182
183 procedure Build_Discriminal (Discrim : Entity_Id);
184 -- Create the discriminal corresponding to discriminant Discrim, that is
185 -- the parameter corresponding to Discrim to be used in initialization
186 -- procedures for the type where Discrim is a discriminant. Discriminals
187 -- are not used during semantic analysis, and are not fully defined
188 -- entities until expansion. Thus they are not given a scope until
189 -- initialization procedures are built.
190
191 function Build_Discriminant_Constraints
192 (T : Entity_Id;
193 Def : Node_Id;
194 Derived_Def : Boolean := False) return Elist_Id;
195 -- Validate discriminant constraints and return the list of the constraints
196 -- in order of discriminant declarations, where T is the discriminated
197 -- unconstrained type. Def is the N_Subtype_Indication node where the
198 -- discriminants constraints for T are specified. Derived_Def is True
199 -- when building the discriminant constraints in a derived type definition
200 -- of the form "type D (...) is new T (xxx)". In this case T is the parent
201 -- type and Def is the constraint "(xxx)" on T and this routine sets the
202 -- Corresponding_Discriminant field of the discriminants in the derived
203 -- type D to point to the corresponding discriminants in the parent type T.
204
205 procedure Build_Discriminated_Subtype
206 (T : Entity_Id;
207 Def_Id : Entity_Id;
208 Elist : Elist_Id;
209 Related_Nod : Node_Id;
210 For_Access : Boolean := False);
211 -- Subsidiary procedure to Constrain_Discriminated_Type and to
212 -- Process_Incomplete_Dependents. Given
213 --
214 -- T (a possibly discriminated base type)
215 -- Def_Id (a very partially built subtype for T),
216 --
217 -- the call completes Def_Id to be the appropriate E_*_Subtype.
218 --
219 -- The Elist is the list of discriminant constraints if any (it is set
220 -- to No_Elist if T is not a discriminated type, and to an empty list if
221 -- T has discriminants but there are no discriminant constraints). The
222 -- Related_Nod is the same as Decl_Node in Create_Constrained_Components.
223 -- The For_Access says whether or not this subtype is really constraining
224 -- an access type. That is its sole purpose is the designated type of an
225 -- access type -- in which case a Private_Subtype Is_For_Access_Subtype
226 -- is built to avoid freezing T when the access subtype is frozen.
227
228 function Build_Scalar_Bound
229 (Bound : Node_Id;
230 Par_T : Entity_Id;
231 Der_T : Entity_Id) return Node_Id;
232 -- The bounds of a derived scalar type are conversions of the bounds of
233 -- the parent type. Optimize the representation if the bounds are literals.
234 -- Needs a more complete spec--what are the parameters exactly, and what
235 -- exactly is the returned value, and how is Bound affected???
236
237 procedure Build_Underlying_Full_View
238 (N : Node_Id;
239 Typ : Entity_Id;
240 Par : Entity_Id);
241 -- If the completion of a private type is itself derived from a private
242 -- type, or if the full view of a private subtype is itself private, the
243 -- back-end has no way to compute the actual size of this type. We build
244 -- an internal subtype declaration of the proper parent type to convey
245 -- this information. This extra mechanism is needed because a full
246 -- view cannot itself have a full view (it would get clobbered during
247 -- view exchanges).
248
249 procedure Check_Access_Discriminant_Requires_Limited
250 (D : Node_Id;
251 Loc : Node_Id);
252 -- Check the restriction that the type to which an access discriminant
253 -- belongs must be a concurrent type or a descendant of a type with
254 -- the reserved word 'limited' in its declaration.
255
256 procedure Check_Anonymous_Access_Components
257 (Typ_Decl : Node_Id;
258 Typ : Entity_Id;
259 Prev : Entity_Id;
260 Comp_List : Node_Id);
261 -- Ada 2005 AI-382: an access component in a record definition can refer to
262 -- the enclosing record, in which case it denotes the type itself, and not
263 -- the current instance of the type. We create an anonymous access type for
264 -- the component, and flag it as an access to a component, so accessibility
265 -- checks are properly performed on it. The declaration of the access type
266 -- is placed ahead of that of the record to prevent order-of-elaboration
267 -- circularity issues in Gigi. We create an incomplete type for the record
268 -- declaration, which is the designated type of the anonymous access.
269
270 procedure Check_Delta_Expression (E : Node_Id);
271 -- Check that the expression represented by E is suitable for use as a
272 -- delta expression, i.e. it is of real type and is static.
273
274 procedure Check_Digits_Expression (E : Node_Id);
275 -- Check that the expression represented by E is suitable for use as a
276 -- digits expression, i.e. it is of integer type, positive and static.
277
278 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id);
279 -- Validate the initialization of an object declaration. T is the required
280 -- type, and Exp is the initialization expression.
281
282 procedure Check_Interfaces (N : Node_Id; Def : Node_Id);
283 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
284
285 procedure Check_Or_Process_Discriminants
286 (N : Node_Id;
287 T : Entity_Id;
288 Prev : Entity_Id := Empty);
289 -- If N is the full declaration of the completion T of an incomplete or
290 -- private type, check its discriminants (which are already known to be
291 -- conformant with those of the partial view, see Find_Type_Name),
292 -- otherwise process them. Prev is the entity of the partial declaration,
293 -- if any.
294
295 procedure Check_Real_Bound (Bound : Node_Id);
296 -- Check given bound for being of real type and static. If not, post an
297 -- appropriate message, and rewrite the bound with the real literal zero.
298
299 procedure Constant_Redeclaration
300 (Id : Entity_Id;
301 N : Node_Id;
302 T : out Entity_Id);
303 -- Various checks on legality of full declaration of deferred constant.
304 -- Id is the entity for the redeclaration, N is the N_Object_Declaration,
305 -- node. The caller has not yet set any attributes of this entity.
306
307 function Contain_Interface
308 (Iface : Entity_Id;
309 Ifaces : Elist_Id) return Boolean;
310 -- Ada 2005: Determine whether Iface is present in the list Ifaces
311
312 procedure Convert_Scalar_Bounds
313 (N : Node_Id;
314 Parent_Type : Entity_Id;
315 Derived_Type : Entity_Id;
316 Loc : Source_Ptr);
317 -- For derived scalar types, convert the bounds in the type definition to
318 -- the derived type, and complete their analysis. Given a constraint of the
319 -- form ".. new T range Lo .. Hi", Lo and Hi are analyzed and resolved with
320 -- T'Base, the parent_type. The bounds of the derived type (the anonymous
321 -- base) are copies of Lo and Hi. Finally, the bounds of the derived
322 -- subtype are conversions of those bounds to the derived_type, so that
323 -- their typing is consistent.
324
325 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id);
326 -- Copies attributes from array base type T2 to array base type T1. Copies
327 -- only attributes that apply to base types, but not subtypes.
328
329 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id);
330 -- Copies attributes from array subtype T2 to array subtype T1. Copies
331 -- attributes that apply to both subtypes and base types.
332
333 procedure Create_Constrained_Components
334 (Subt : Entity_Id;
335 Decl_Node : Node_Id;
336 Typ : Entity_Id;
337 Constraints : Elist_Id);
338 -- Build the list of entities for a constrained discriminated record
339 -- subtype. If a component depends on a discriminant, replace its subtype
340 -- using the discriminant values in the discriminant constraint. Subt
341 -- is the defining identifier for the subtype whose list of constrained
342 -- entities we will create. Decl_Node is the type declaration node where
343 -- we will attach all the itypes created. Typ is the base discriminated
344 -- type for the subtype Subt. Constraints is the list of discriminant
345 -- constraints for Typ.
346
347 function Constrain_Component_Type
348 (Comp : Entity_Id;
349 Constrained_Typ : Entity_Id;
350 Related_Node : Node_Id;
351 Typ : Entity_Id;
352 Constraints : Elist_Id) return Entity_Id;
353 -- Given a discriminated base type Typ, a list of discriminant constraints,
354 -- Constraints, for Typ and a component Comp of Typ, create and return the
355 -- type corresponding to Etype (Comp) where all discriminant references
356 -- are replaced with the corresponding constraint. If Etype (Comp) contains
357 -- no discriminant references then it is returned as-is. Constrained_Typ
358 -- is the final constrained subtype to which the constrained component
359 -- belongs. Related_Node is the node where we attach all created itypes.
360
361 procedure Constrain_Access
362 (Def_Id : in out Entity_Id;
363 S : Node_Id;
364 Related_Nod : Node_Id);
365 -- Apply a list of constraints to an access type. If Def_Id is empty, it is
366 -- an anonymous type created for a subtype indication. In that case it is
367 -- created in the procedure and attached to Related_Nod.
368
369 procedure Constrain_Array
370 (Def_Id : in out Entity_Id;
371 SI : Node_Id;
372 Related_Nod : Node_Id;
373 Related_Id : Entity_Id;
374 Suffix : Character);
375 -- Apply a list of index constraints to an unconstrained array type. The
376 -- first parameter is the entity for the resulting subtype. A value of
377 -- Empty for Def_Id indicates that an implicit type must be created, but
378 -- creation is delayed (and must be done by this procedure) because other
379 -- subsidiary implicit types must be created first (which is why Def_Id
380 -- is an in/out parameter). The second parameter is a subtype indication
381 -- node for the constrained array to be created (e.g. something of the
382 -- form string (1 .. 10)). Related_Nod gives the place where this type
383 -- has to be inserted in the tree. The Related_Id and Suffix parameters
384 -- are used to build the associated Implicit type name.
385
386 procedure Constrain_Concurrent
387 (Def_Id : in out Entity_Id;
388 SI : Node_Id;
389 Related_Nod : Node_Id;
390 Related_Id : Entity_Id;
391 Suffix : Character);
392 -- Apply list of discriminant constraints to an unconstrained concurrent
393 -- type.
394 --
395 -- SI is the N_Subtype_Indication node containing the constraint and
396 -- the unconstrained type to constrain.
397 --
398 -- Def_Id is the entity for the resulting constrained subtype. A value
399 -- of Empty for Def_Id indicates that an implicit type must be created,
400 -- but creation is delayed (and must be done by this procedure) because
401 -- other subsidiary implicit types must be created first (which is why
402 -- Def_Id is an in/out parameter).
403 --
404 -- Related_Nod gives the place where this type has to be inserted
405 -- in the tree.
406 --
407 -- The last two arguments are used to create its external name if needed.
408
409 function Constrain_Corresponding_Record
410 (Prot_Subt : Entity_Id;
411 Corr_Rec : Entity_Id;
412 Related_Nod : Node_Id) return Entity_Id;
413 -- When constraining a protected type or task type with discriminants,
414 -- constrain the corresponding record with the same discriminant values.
415
416 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id);
417 -- Constrain a decimal fixed point type with a digits constraint and/or a
418 -- range constraint, and build E_Decimal_Fixed_Point_Subtype entity.
419
420 procedure Constrain_Discriminated_Type
421 (Def_Id : Entity_Id;
422 S : Node_Id;
423 Related_Nod : Node_Id;
424 For_Access : Boolean := False);
425 -- Process discriminant constraints of composite type. Verify that values
426 -- have been provided for all discriminants, that the original type is
427 -- unconstrained, and that the types of the supplied expressions match
428 -- the discriminant types. The first three parameters are like in routine
429 -- Constrain_Concurrent. See Build_Discriminated_Subtype for an explanation
430 -- of For_Access.
431
432 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id);
433 -- Constrain an enumeration type with a range constraint. This is identical
434 -- to Constrain_Integer, but for the Ekind of the resulting subtype.
435
436 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id);
437 -- Constrain a floating point type with either a digits constraint
438 -- and/or a range constraint, building a E_Floating_Point_Subtype.
439
440 procedure Constrain_Index
441 (Index : Node_Id;
442 S : Node_Id;
443 Related_Nod : Node_Id;
444 Related_Id : Entity_Id;
445 Suffix : Character;
446 Suffix_Index : Nat);
447 -- Process an index constraint S in a constrained array declaration. The
448 -- constraint can be a subtype name, or a range with or without an explicit
449 -- subtype mark. The index is the corresponding index of the unconstrained
450 -- array. The Related_Id and Suffix parameters are used to build the
451 -- associated Implicit type name.
452
453 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id);
454 -- Build subtype of a signed or modular integer type
455
456 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id);
457 -- Constrain an ordinary fixed point type with a range constraint, and
458 -- build an E_Ordinary_Fixed_Point_Subtype entity.
459
460 procedure Copy_And_Swap (Priv, Full : Entity_Id);
461 -- Copy the Priv entity into the entity of its full declaration then swap
462 -- the two entities in such a manner that the former private type is now
463 -- seen as a full type.
464
465 procedure Decimal_Fixed_Point_Type_Declaration
466 (T : Entity_Id;
467 Def : Node_Id);
468 -- Create a new decimal fixed point type, and apply the constraint to
469 -- obtain a subtype of this new type.
470
471 procedure Complete_Private_Subtype
472 (Priv : Entity_Id;
473 Full : Entity_Id;
474 Full_Base : Entity_Id;
475 Related_Nod : Node_Id);
476 -- Complete the implicit full view of a private subtype by setting the
477 -- appropriate semantic fields. If the full view of the parent is a record
478 -- type, build constrained components of subtype.
479
480 procedure Derive_Progenitor_Subprograms
481 (Parent_Type : Entity_Id;
482 Tagged_Type : Entity_Id);
483 -- Ada 2005 (AI-251): To complete type derivation, collect the primitive
484 -- operations of progenitors of Tagged_Type, and replace the subsidiary
485 -- subtypes with Tagged_Type, to build the specs of the inherited interface
486 -- primitives. The derived primitives are aliased to those of the
487 -- interface. This routine takes care also of transferring to the full view
488 -- subprograms associated with the partial view of Tagged_Type that cover
489 -- interface primitives.
490
491 procedure Derived_Standard_Character
492 (N : Node_Id;
493 Parent_Type : Entity_Id;
494 Derived_Type : Entity_Id);
495 -- Subsidiary procedure to Build_Derived_Enumeration_Type which handles
496 -- derivations from types Standard.Character and Standard.Wide_Character.
497
498 procedure Derived_Type_Declaration
499 (T : Entity_Id;
500 N : Node_Id;
501 Is_Completion : Boolean);
502 -- Process a derived type declaration. Build_Derived_Type is invoked
503 -- to process the actual derived type definition. Parameters N and
504 -- Is_Completion have the same meaning as in Build_Derived_Type.
505 -- T is the N_Defining_Identifier for the entity defined in the
506 -- N_Full_Type_Declaration node N, that is T is the derived type.
507
508 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id);
509 -- Insert each literal in symbol table, as an overloadable identifier. Each
510 -- enumeration type is mapped into a sequence of integers, and each literal
511 -- is defined as a constant with integer value. If any of the literals are
512 -- character literals, the type is a character type, which means that
513 -- strings are legal aggregates for arrays of components of the type.
514
515 function Expand_To_Stored_Constraint
516 (Typ : Entity_Id;
517 Constraint : Elist_Id) return Elist_Id;
518 -- Given a constraint (i.e. a list of expressions) on the discriminants of
519 -- Typ, expand it into a constraint on the stored discriminants and return
520 -- the new list of expressions constraining the stored discriminants.
521
522 function Find_Type_Of_Object
523 (Obj_Def : Node_Id;
524 Related_Nod : Node_Id) return Entity_Id;
525 -- Get type entity for object referenced by Obj_Def, attaching the implicit
526 -- types generated to Related_Nod.
527
528 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id);
529 -- Create a new float and apply the constraint to obtain subtype of it
530
531 function Has_Range_Constraint (N : Node_Id) return Boolean;
532 -- Given an N_Subtype_Indication node N, return True if a range constraint
533 -- is present, either directly, or as part of a digits or delta constraint.
534 -- In addition, a digits constraint in the decimal case returns True, since
535 -- it establishes a default range if no explicit range is present.
536
537 function Inherit_Components
538 (N : Node_Id;
539 Parent_Base : Entity_Id;
540 Derived_Base : Entity_Id;
541 Is_Tagged : Boolean;
542 Inherit_Discr : Boolean;
543 Discs : Elist_Id) return Elist_Id;
544 -- Called from Build_Derived_Record_Type to inherit the components of
545 -- Parent_Base (a base type) into the Derived_Base (the derived base type).
546 -- For more information on derived types and component inheritance please
547 -- consult the comment above the body of Build_Derived_Record_Type.
548 --
549 -- N is the original derived type declaration
550 --
551 -- Is_Tagged is set if we are dealing with tagged types
552 --
553 -- If Inherit_Discr is set, Derived_Base inherits its discriminants from
554 -- Parent_Base, otherwise no discriminants are inherited.
555 --
556 -- Discs gives the list of constraints that apply to Parent_Base in the
557 -- derived type declaration. If Discs is set to No_Elist, then we have
558 -- the following situation:
559 --
560 -- type Parent (D1..Dn : ..) is [tagged] record ...;
561 -- type Derived is new Parent [with ...];
562 --
563 -- which gets treated as
564 --
565 -- type Derived (D1..Dn : ..) is new Parent (D1,..,Dn) [with ...];
566 --
567 -- For untagged types the returned value is an association list. The list
568 -- starts from the association (Parent_Base => Derived_Base), and then it
569 -- contains a sequence of the associations of the form
570 --
571 -- (Old_Component => New_Component),
572 --
573 -- where Old_Component is the Entity_Id of a component in Parent_Base and
574 -- New_Component is the Entity_Id of the corresponding component in
575 -- Derived_Base. For untagged records, this association list is needed when
576 -- copying the record declaration for the derived base. In the tagged case
577 -- the value returned is irrelevant.
578
579 procedure Inherit_Predicate_Flags (Subt, Par : Entity_Id);
580 -- Propagate static and dynamic predicate flags from a parent to the
581 -- subtype in a subtype declaration with and without constraints.
582
583 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean;
584 -- Subsidiary to Check_Abstract_Overriding and Derive_Subprogram.
585 -- Determine whether subprogram Subp is a procedure subject to pragma
586 -- Extensions_Visible with value False and has at least one controlling
587 -- parameter of mode OUT.
588
589 function Is_Valid_Constraint_Kind
590 (T_Kind : Type_Kind;
591 Constraint_Kind : Node_Kind) return Boolean;
592 -- Returns True if it is legal to apply the given kind of constraint to the
593 -- given kind of type (index constraint to an array type, for example).
594
595 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id);
596 -- Create new modular type. Verify that modulus is in bounds
597
598 procedure New_Concatenation_Op (Typ : Entity_Id);
599 -- Create an abbreviated declaration for an operator in order to
600 -- materialize concatenation on array types.
601
602 procedure Ordinary_Fixed_Point_Type_Declaration
603 (T : Entity_Id;
604 Def : Node_Id);
605 -- Create a new ordinary fixed point type, and apply the constraint to
606 -- obtain subtype of it.
607
608 procedure Prepare_Private_Subtype_Completion
609 (Id : Entity_Id;
610 Related_Nod : Node_Id);
611 -- Id is a subtype of some private type. Creates the full declaration
612 -- associated with Id whenever possible, i.e. when the full declaration
613 -- of the base type is already known. Records each subtype into
614 -- Private_Dependents of the base type.
615
616 procedure Process_Incomplete_Dependents
617 (N : Node_Id;
618 Full_T : Entity_Id;
619 Inc_T : Entity_Id);
620 -- Process all entities that depend on an incomplete type. There include
621 -- subtypes, subprogram types that mention the incomplete type in their
622 -- profiles, and subprogram with access parameters that designate the
623 -- incomplete type.
624
625 -- Inc_T is the defining identifier of an incomplete type declaration, its
626 -- Ekind is E_Incomplete_Type.
627 --
628 -- N is the corresponding N_Full_Type_Declaration for Inc_T.
629 --
630 -- Full_T is N's defining identifier.
631 --
632 -- Subtypes of incomplete types with discriminants are completed when the
633 -- parent type is. This is simpler than private subtypes, because they can
634 -- only appear in the same scope, and there is no need to exchange views.
635 -- Similarly, access_to_subprogram types may have a parameter or a return
636 -- type that is an incomplete type, and that must be replaced with the
637 -- full type.
638 --
639 -- If the full type is tagged, subprogram with access parameters that
640 -- designated the incomplete may be primitive operations of the full type,
641 -- and have to be processed accordingly.
642
643 procedure Process_Real_Range_Specification (Def : Node_Id);
644 -- Given the type definition for a real type, this procedure processes and
645 -- checks the real range specification of this type definition if one is
646 -- present. If errors are found, error messages are posted, and the
647 -- Real_Range_Specification of Def is reset to Empty.
648
649 procedure Record_Type_Declaration
650 (T : Entity_Id;
651 N : Node_Id;
652 Prev : Entity_Id);
653 -- Process a record type declaration (for both untagged and tagged
654 -- records). Parameters T and N are exactly like in procedure
655 -- Derived_Type_Declaration, except that no flag Is_Completion is needed
656 -- for this routine. If this is the completion of an incomplete type
657 -- declaration, Prev is the entity of the incomplete declaration, used for
658 -- cross-referencing. Otherwise Prev = T.
659
660 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id);
661 -- This routine is used to process the actual record type definition (both
662 -- for untagged and tagged records). Def is a record type definition node.
663 -- This procedure analyzes the components in this record type definition.
664 -- Prev_T is the entity for the enclosing record type. It is provided so
665 -- that its Has_Task flag can be set if any of the component have Has_Task
666 -- set. If the declaration is the completion of an incomplete type
667 -- declaration, Prev_T is the original incomplete type, whose full view is
668 -- the record type.
669
670 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id);
671 -- Subsidiary to Build_Derived_Record_Type. For untagged records, we
672 -- build a copy of the declaration tree of the parent, and we create
673 -- independently the list of components for the derived type. Semantic
674 -- information uses the component entities, but record representation
675 -- clauses are validated on the declaration tree. This procedure replaces
676 -- discriminants and components in the declaration with those that have
677 -- been created by Inherit_Components.
678
679 procedure Set_Fixed_Range
680 (E : Entity_Id;
681 Loc : Source_Ptr;
682 Lo : Ureal;
683 Hi : Ureal);
684 -- Build a range node with the given bounds and set it as the Scalar_Range
685 -- of the given fixed-point type entity. Loc is the source location used
686 -- for the constructed range. See body for further details.
687
688 procedure Set_Scalar_Range_For_Subtype
689 (Def_Id : Entity_Id;
690 R : Node_Id;
691 Subt : Entity_Id);
692 -- This routine is used to set the scalar range field for a subtype given
693 -- Def_Id, the entity for the subtype, and R, the range expression for the
694 -- scalar range. Subt provides the parent subtype to be used to analyze,
695 -- resolve, and check the given range.
696
697 procedure Set_Default_SSO (T : Entity_Id);
698 -- T is the entity for an array or record being declared. This procedure
699 -- sets the flags SSO_Set_Low_By_Default/SSO_Set_High_By_Default according
700 -- to the setting of Opt.Default_SSO.
701
702 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id);
703 -- Create a new signed integer entity, and apply the constraint to obtain
704 -- the required first named subtype of this type.
705
706 procedure Set_Stored_Constraint_From_Discriminant_Constraint
707 (E : Entity_Id);
708 -- E is some record type. This routine computes E's Stored_Constraint
709 -- from its Discriminant_Constraint.
710
711 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id);
712 -- Check that an entity in a list of progenitors is an interface,
713 -- emit error otherwise.
714
715 -----------------------
716 -- Access_Definition --
717 -----------------------
718
719 function Access_Definition
720 (Related_Nod : Node_Id;
721 N : Node_Id) return Entity_Id
722 is
723 Anon_Type : Entity_Id;
724 Anon_Scope : Entity_Id;
725 Desig_Type : Entity_Id;
726 Enclosing_Prot_Type : Entity_Id := Empty;
727
728 begin
729 Check_SPARK_05_Restriction ("access type is not allowed", N);
730
731 if Is_Entry (Current_Scope)
732 and then Is_Task_Type (Etype (Scope (Current_Scope)))
733 then
734 Error_Msg_N ("task entries cannot have access parameters", N);
735 return Empty;
736 end if;
737
738 -- Ada 2005: For an object declaration the corresponding anonymous
739 -- type is declared in the current scope.
740
741 -- If the access definition is the return type of another access to
742 -- function, scope is the current one, because it is the one of the
743 -- current type declaration, except for the pathological case below.
744
745 if Nkind_In (Related_Nod, N_Object_Declaration,
746 N_Access_Function_Definition)
747 then
748 Anon_Scope := Current_Scope;
749
750 -- A pathological case: function returning access functions that
751 -- return access functions, etc. Each anonymous access type created
752 -- is in the enclosing scope of the outermost function.
753
754 declare
755 Par : Node_Id;
756
757 begin
758 Par := Related_Nod;
759 while Nkind_In (Par, N_Access_Function_Definition,
760 N_Access_Definition)
761 loop
762 Par := Parent (Par);
763 end loop;
764
765 if Nkind (Par) = N_Function_Specification then
766 Anon_Scope := Scope (Defining_Entity (Par));
767 end if;
768 end;
769
770 -- For the anonymous function result case, retrieve the scope of the
771 -- function specification's associated entity rather than using the
772 -- current scope. The current scope will be the function itself if the
773 -- formal part is currently being analyzed, but will be the parent scope
774 -- in the case of a parameterless function, and we always want to use
775 -- the function's parent scope. Finally, if the function is a child
776 -- unit, we must traverse the tree to retrieve the proper entity.
777
778 elsif Nkind (Related_Nod) = N_Function_Specification
779 and then Nkind (Parent (N)) /= N_Parameter_Specification
780 then
781 -- If the current scope is a protected type, the anonymous access
782 -- is associated with one of the protected operations, and must
783 -- be available in the scope that encloses the protected declaration.
784 -- Otherwise the type is in the scope enclosing the subprogram.
785
786 -- If the function has formals, The return type of a subprogram
787 -- declaration is analyzed in the scope of the subprogram (see
788 -- Process_Formals) and thus the protected type, if present, is
789 -- the scope of the current function scope.
790
791 if Ekind (Current_Scope) = E_Protected_Type then
792 Enclosing_Prot_Type := Current_Scope;
793
794 elsif Ekind (Current_Scope) = E_Function
795 and then Ekind (Scope (Current_Scope)) = E_Protected_Type
796 then
797 Enclosing_Prot_Type := Scope (Current_Scope);
798 end if;
799
800 if Present (Enclosing_Prot_Type) then
801 Anon_Scope := Scope (Enclosing_Prot_Type);
802
803 else
804 Anon_Scope := Scope (Defining_Entity (Related_Nod));
805 end if;
806
807 -- For an access type definition, if the current scope is a child
808 -- unit it is the scope of the type.
809
810 elsif Is_Compilation_Unit (Current_Scope) then
811 Anon_Scope := Current_Scope;
812
813 -- For access formals, access components, and access discriminants, the
814 -- scope is that of the enclosing declaration,
815
816 else
817 Anon_Scope := Scope (Current_Scope);
818 end if;
819
820 Anon_Type :=
821 Create_Itype
822 (E_Anonymous_Access_Type, Related_Nod, Scope_Id => Anon_Scope);
823
824 if All_Present (N)
825 and then Ada_Version >= Ada_2005
826 then
827 Error_Msg_N ("ALL is not permitted for anonymous access types", N);
828 end if;
829
830 -- Ada 2005 (AI-254): In case of anonymous access to subprograms call
831 -- the corresponding semantic routine
832
833 if Present (Access_To_Subprogram_Definition (N)) then
834
835 -- Compiler runtime units are compiled in Ada 2005 mode when building
836 -- the runtime library but must also be compilable in Ada 95 mode
837 -- (when bootstrapping the compiler).
838
839 Check_Compiler_Unit ("anonymous access to subprogram", N);
840
841 Access_Subprogram_Declaration
842 (T_Name => Anon_Type,
843 T_Def => Access_To_Subprogram_Definition (N));
844
845 if Ekind (Anon_Type) = E_Access_Protected_Subprogram_Type then
846 Set_Ekind
847 (Anon_Type, E_Anonymous_Access_Protected_Subprogram_Type);
848 else
849 Set_Ekind (Anon_Type, E_Anonymous_Access_Subprogram_Type);
850 end if;
851
852 Set_Can_Use_Internal_Rep
853 (Anon_Type, not Always_Compatible_Rep_On_Target);
854
855 -- If the anonymous access is associated with a protected operation,
856 -- create a reference to it after the enclosing protected definition
857 -- because the itype will be used in the subsequent bodies.
858
859 -- If the anonymous access itself is protected, a full type
860 -- declaratiton will be created for it, so that the equivalent
861 -- record type can be constructed. For further details, see
862 -- Replace_Anonymous_Access_To_Protected-Subprogram.
863
864 if Ekind (Current_Scope) = E_Protected_Type
865 and then not Protected_Present (Access_To_Subprogram_Definition (N))
866 then
867 Build_Itype_Reference (Anon_Type, Parent (Current_Scope));
868 end if;
869
870 return Anon_Type;
871 end if;
872
873 Find_Type (Subtype_Mark (N));
874 Desig_Type := Entity (Subtype_Mark (N));
875
876 Set_Directly_Designated_Type (Anon_Type, Desig_Type);
877 Set_Etype (Anon_Type, Anon_Type);
878
879 -- Make sure the anonymous access type has size and alignment fields
880 -- set, as required by gigi. This is necessary in the case of the
881 -- Task_Body_Procedure.
882
883 if not Has_Private_Component (Desig_Type) then
884 Layout_Type (Anon_Type);
885 end if;
886
887 -- Ada 2005 (AI-231): Ada 2005 semantics for anonymous access differs
888 -- from Ada 95 semantics. In Ada 2005, anonymous access must specify if
889 -- the null value is allowed. In Ada 95 the null value is never allowed.
890
891 if Ada_Version >= Ada_2005 then
892 Set_Can_Never_Be_Null (Anon_Type, Null_Exclusion_Present (N));
893 else
894 Set_Can_Never_Be_Null (Anon_Type, True);
895 end if;
896
897 -- The anonymous access type is as public as the discriminated type or
898 -- subprogram that defines it. It is imported (for back-end purposes)
899 -- if the designated type is.
900
901 Set_Is_Public (Anon_Type, Is_Public (Scope (Anon_Type)));
902
903 -- Ada 2005 (AI-231): Propagate the access-constant attribute
904
905 Set_Is_Access_Constant (Anon_Type, Constant_Present (N));
906
907 -- The context is either a subprogram declaration, object declaration,
908 -- or an access discriminant, in a private or a full type declaration.
909 -- In the case of a subprogram, if the designated type is incomplete,
910 -- the operation will be a primitive operation of the full type, to be
911 -- updated subsequently. If the type is imported through a limited_with
912 -- clause, the subprogram is not a primitive operation of the type
913 -- (which is declared elsewhere in some other scope).
914
915 if Ekind (Desig_Type) = E_Incomplete_Type
916 and then not From_Limited_With (Desig_Type)
917 and then Is_Overloadable (Current_Scope)
918 then
919 Append_Elmt (Current_Scope, Private_Dependents (Desig_Type));
920 Set_Has_Delayed_Freeze (Current_Scope);
921 end if;
922
923 -- Ada 2005: If the designated type is an interface that may contain
924 -- tasks, create a Master entity for the declaration. This must be done
925 -- before expansion of the full declaration, because the declaration may
926 -- include an expression that is an allocator, whose expansion needs the
927 -- proper Master for the created tasks.
928
929 if Nkind (Related_Nod) = N_Object_Declaration and then Expander_Active
930 then
931 if Is_Interface (Desig_Type) and then Is_Limited_Record (Desig_Type)
932 then
933 Build_Class_Wide_Master (Anon_Type);
934
935 -- Similarly, if the type is an anonymous access that designates
936 -- tasks, create a master entity for it in the current context.
937
938 elsif Has_Task (Desig_Type) and then Comes_From_Source (Related_Nod)
939 then
940 Build_Master_Entity (Defining_Identifier (Related_Nod));
941 Build_Master_Renaming (Anon_Type);
942 end if;
943 end if;
944
945 -- For a private component of a protected type, it is imperative that
946 -- the back-end elaborate the type immediately after the protected
947 -- declaration, because this type will be used in the declarations
948 -- created for the component within each protected body, so we must
949 -- create an itype reference for it now.
950
951 if Nkind (Parent (Related_Nod)) = N_Protected_Definition then
952 Build_Itype_Reference (Anon_Type, Parent (Parent (Related_Nod)));
953
954 -- Similarly, if the access definition is the return result of a
955 -- function, create an itype reference for it because it will be used
956 -- within the function body. For a regular function that is not a
957 -- compilation unit, insert reference after the declaration. For a
958 -- protected operation, insert it after the enclosing protected type
959 -- declaration. In either case, do not create a reference for a type
960 -- obtained through a limited_with clause, because this would introduce
961 -- semantic dependencies.
962
963 -- Similarly, do not create a reference if the designated type is a
964 -- generic formal, because no use of it will reach the backend.
965
966 elsif Nkind (Related_Nod) = N_Function_Specification
967 and then not From_Limited_With (Desig_Type)
968 and then not Is_Generic_Type (Desig_Type)
969 then
970 if Present (Enclosing_Prot_Type) then
971 Build_Itype_Reference (Anon_Type, Parent (Enclosing_Prot_Type));
972
973 elsif Is_List_Member (Parent (Related_Nod))
974 and then Nkind (Parent (N)) /= N_Parameter_Specification
975 then
976 Build_Itype_Reference (Anon_Type, Parent (Related_Nod));
977 end if;
978
979 -- Finally, create an itype reference for an object declaration of an
980 -- anonymous access type. This is strictly necessary only for deferred
981 -- constants, but in any case will avoid out-of-scope problems in the
982 -- back-end.
983
984 elsif Nkind (Related_Nod) = N_Object_Declaration then
985 Build_Itype_Reference (Anon_Type, Related_Nod);
986 end if;
987
988 return Anon_Type;
989 end Access_Definition;
990
991 -----------------------------------
992 -- Access_Subprogram_Declaration --
993 -----------------------------------
994
995 procedure Access_Subprogram_Declaration
996 (T_Name : Entity_Id;
997 T_Def : Node_Id)
998 is
999 procedure Check_For_Premature_Usage (Def : Node_Id);
1000 -- Check that type T_Name is not used, directly or recursively, as a
1001 -- parameter or a return type in Def. Def is either a subtype, an
1002 -- access_definition, or an access_to_subprogram_definition.
1003
1004 -------------------------------
1005 -- Check_For_Premature_Usage --
1006 -------------------------------
1007
1008 procedure Check_For_Premature_Usage (Def : Node_Id) is
1009 Param : Node_Id;
1010
1011 begin
1012 -- Check for a subtype mark
1013
1014 if Nkind (Def) in N_Has_Etype then
1015 if Etype (Def) = T_Name then
1016 Error_Msg_N
1017 ("type& cannot be used before end of its declaration", Def);
1018 end if;
1019
1020 -- If this is not a subtype, then this is an access_definition
1021
1022 elsif Nkind (Def) = N_Access_Definition then
1023 if Present (Access_To_Subprogram_Definition (Def)) then
1024 Check_For_Premature_Usage
1025 (Access_To_Subprogram_Definition (Def));
1026 else
1027 Check_For_Premature_Usage (Subtype_Mark (Def));
1028 end if;
1029
1030 -- The only cases left are N_Access_Function_Definition and
1031 -- N_Access_Procedure_Definition.
1032
1033 else
1034 if Present (Parameter_Specifications (Def)) then
1035 Param := First (Parameter_Specifications (Def));
1036 while Present (Param) loop
1037 Check_For_Premature_Usage (Parameter_Type (Param));
1038 Param := Next (Param);
1039 end loop;
1040 end if;
1041
1042 if Nkind (Def) = N_Access_Function_Definition then
1043 Check_For_Premature_Usage (Result_Definition (Def));
1044 end if;
1045 end if;
1046 end Check_For_Premature_Usage;
1047
1048 -- Local variables
1049
1050 Formals : constant List_Id := Parameter_Specifications (T_Def);
1051 Formal : Entity_Id;
1052 D_Ityp : Node_Id;
1053 Desig_Type : constant Entity_Id :=
1054 Create_Itype (E_Subprogram_Type, Parent (T_Def));
1055
1056 -- Start of processing for Access_Subprogram_Declaration
1057
1058 begin
1059 Check_SPARK_05_Restriction ("access type is not allowed", T_Def);
1060
1061 -- Associate the Itype node with the inner full-type declaration or
1062 -- subprogram spec or entry body. This is required to handle nested
1063 -- anonymous declarations. For example:
1064
1065 -- procedure P
1066 -- (X : access procedure
1067 -- (Y : access procedure
1068 -- (Z : access T)))
1069
1070 D_Ityp := Associated_Node_For_Itype (Desig_Type);
1071 while not (Nkind_In (D_Ityp, N_Full_Type_Declaration,
1072 N_Private_Type_Declaration,
1073 N_Private_Extension_Declaration,
1074 N_Procedure_Specification,
1075 N_Function_Specification,
1076 N_Entry_Body)
1077
1078 or else
1079 Nkind_In (D_Ityp, N_Object_Declaration,
1080 N_Object_Renaming_Declaration,
1081 N_Formal_Object_Declaration,
1082 N_Formal_Type_Declaration,
1083 N_Task_Type_Declaration,
1084 N_Protected_Type_Declaration))
1085 loop
1086 D_Ityp := Parent (D_Ityp);
1087 pragma Assert (D_Ityp /= Empty);
1088 end loop;
1089
1090 Set_Associated_Node_For_Itype (Desig_Type, D_Ityp);
1091
1092 if Nkind_In (D_Ityp, N_Procedure_Specification,
1093 N_Function_Specification)
1094 then
1095 Set_Scope (Desig_Type, Scope (Defining_Entity (D_Ityp)));
1096
1097 elsif Nkind_In (D_Ityp, N_Full_Type_Declaration,
1098 N_Object_Declaration,
1099 N_Object_Renaming_Declaration,
1100 N_Formal_Type_Declaration)
1101 then
1102 Set_Scope (Desig_Type, Scope (Defining_Identifier (D_Ityp)));
1103 end if;
1104
1105 if Nkind (T_Def) = N_Access_Function_Definition then
1106 if Nkind (Result_Definition (T_Def)) = N_Access_Definition then
1107 declare
1108 Acc : constant Node_Id := Result_Definition (T_Def);
1109
1110 begin
1111 if Present (Access_To_Subprogram_Definition (Acc))
1112 and then
1113 Protected_Present (Access_To_Subprogram_Definition (Acc))
1114 then
1115 Set_Etype
1116 (Desig_Type,
1117 Replace_Anonymous_Access_To_Protected_Subprogram
1118 (T_Def));
1119
1120 else
1121 Set_Etype
1122 (Desig_Type,
1123 Access_Definition (T_Def, Result_Definition (T_Def)));
1124 end if;
1125 end;
1126
1127 else
1128 Analyze (Result_Definition (T_Def));
1129
1130 declare
1131 Typ : constant Entity_Id := Entity (Result_Definition (T_Def));
1132
1133 begin
1134 -- If a null exclusion is imposed on the result type, then
1135 -- create a null-excluding itype (an access subtype) and use
1136 -- it as the function's Etype.
1137
1138 if Is_Access_Type (Typ)
1139 and then Null_Exclusion_In_Return_Present (T_Def)
1140 then
1141 Set_Etype (Desig_Type,
1142 Create_Null_Excluding_Itype
1143 (T => Typ,
1144 Related_Nod => T_Def,
1145 Scope_Id => Current_Scope));
1146
1147 else
1148 if From_Limited_With (Typ) then
1149
1150 -- AI05-151: Incomplete types are allowed in all basic
1151 -- declarations, including access to subprograms.
1152
1153 if Ada_Version >= Ada_2012 then
1154 null;
1155
1156 else
1157 Error_Msg_NE
1158 ("illegal use of incomplete type&",
1159 Result_Definition (T_Def), Typ);
1160 end if;
1161
1162 elsif Ekind (Current_Scope) = E_Package
1163 and then In_Private_Part (Current_Scope)
1164 then
1165 if Ekind (Typ) = E_Incomplete_Type then
1166 Append_Elmt (Desig_Type, Private_Dependents (Typ));
1167
1168 elsif Is_Class_Wide_Type (Typ)
1169 and then Ekind (Etype (Typ)) = E_Incomplete_Type
1170 then
1171 Append_Elmt
1172 (Desig_Type, Private_Dependents (Etype (Typ)));
1173 end if;
1174 end if;
1175
1176 Set_Etype (Desig_Type, Typ);
1177 end if;
1178 end;
1179 end if;
1180
1181 if not (Is_Type (Etype (Desig_Type))) then
1182 Error_Msg_N
1183 ("expect type in function specification",
1184 Result_Definition (T_Def));
1185 end if;
1186
1187 else
1188 Set_Etype (Desig_Type, Standard_Void_Type);
1189 end if;
1190
1191 if Present (Formals) then
1192 Push_Scope (Desig_Type);
1193
1194 -- Some special tests here. These special tests can be removed
1195 -- if and when Itypes always have proper parent pointers to their
1196 -- declarations???
1197
1198 -- Special test 1) Link defining_identifier of formals. Required by
1199 -- First_Formal to provide its functionality.
1200
1201 declare
1202 F : Node_Id;
1203
1204 begin
1205 F := First (Formals);
1206
1207 -- In ASIS mode, the access_to_subprogram may be analyzed twice,
1208 -- when it is part of an unconstrained type and subtype expansion
1209 -- is disabled. To avoid back-end problems with shared profiles,
1210 -- use previous subprogram type as the designated type, and then
1211 -- remove scope added above.
1212
1213 if ASIS_Mode and then Present (Scope (Defining_Identifier (F)))
1214 then
1215 Set_Etype (T_Name, T_Name);
1216 Init_Size_Align (T_Name);
1217 Set_Directly_Designated_Type (T_Name,
1218 Scope (Defining_Identifier (F)));
1219 End_Scope;
1220 return;
1221 end if;
1222
1223 while Present (F) loop
1224 if No (Parent (Defining_Identifier (F))) then
1225 Set_Parent (Defining_Identifier (F), F);
1226 end if;
1227
1228 Next (F);
1229 end loop;
1230 end;
1231
1232 Process_Formals (Formals, Parent (T_Def));
1233
1234 -- Special test 2) End_Scope requires that the parent pointer be set
1235 -- to something reasonable, but Itypes don't have parent pointers. So
1236 -- we set it and then unset it ???
1237
1238 Set_Parent (Desig_Type, T_Name);
1239 End_Scope;
1240 Set_Parent (Desig_Type, Empty);
1241 end if;
1242
1243 -- Check for premature usage of the type being defined
1244
1245 Check_For_Premature_Usage (T_Def);
1246
1247 -- The return type and/or any parameter type may be incomplete. Mark the
1248 -- subprogram_type as depending on the incomplete type, so that it can
1249 -- be updated when the full type declaration is seen. This only applies
1250 -- to incomplete types declared in some enclosing scope, not to limited
1251 -- views from other packages.
1252
1253 -- Prior to Ada 2012, access to functions can only have in_parameters.
1254
1255 if Present (Formals) then
1256 Formal := First_Formal (Desig_Type);
1257 while Present (Formal) loop
1258 if Ekind (Formal) /= E_In_Parameter
1259 and then Nkind (T_Def) = N_Access_Function_Definition
1260 and then Ada_Version < Ada_2012
1261 then
1262 Error_Msg_N ("functions can only have IN parameters", Formal);
1263 end if;
1264
1265 if Ekind (Etype (Formal)) = E_Incomplete_Type
1266 and then In_Open_Scopes (Scope (Etype (Formal)))
1267 then
1268 Append_Elmt (Desig_Type, Private_Dependents (Etype (Formal)));
1269 Set_Has_Delayed_Freeze (Desig_Type);
1270 end if;
1271
1272 Next_Formal (Formal);
1273 end loop;
1274 end if;
1275
1276 -- Check whether an indirect call without actuals may be possible. This
1277 -- is used when resolving calls whose result is then indexed.
1278
1279 May_Need_Actuals (Desig_Type);
1280
1281 -- If the return type is incomplete, this is legal as long as the type
1282 -- is declared in the current scope and will be completed in it (rather
1283 -- than being part of limited view).
1284
1285 if Ekind (Etype (Desig_Type)) = E_Incomplete_Type
1286 and then not Has_Delayed_Freeze (Desig_Type)
1287 and then In_Open_Scopes (Scope (Etype (Desig_Type)))
1288 then
1289 Append_Elmt (Desig_Type, Private_Dependents (Etype (Desig_Type)));
1290 Set_Has_Delayed_Freeze (Desig_Type);
1291 end if;
1292
1293 Check_Delayed_Subprogram (Desig_Type);
1294
1295 if Protected_Present (T_Def) then
1296 Set_Ekind (T_Name, E_Access_Protected_Subprogram_Type);
1297 Set_Convention (Desig_Type, Convention_Protected);
1298 else
1299 Set_Ekind (T_Name, E_Access_Subprogram_Type);
1300 end if;
1301
1302 Set_Can_Use_Internal_Rep (T_Name, not Always_Compatible_Rep_On_Target);
1303
1304 Set_Etype (T_Name, T_Name);
1305 Init_Size_Align (T_Name);
1306 Set_Directly_Designated_Type (T_Name, Desig_Type);
1307
1308 Generate_Reference_To_Formals (T_Name);
1309
1310 -- Ada 2005 (AI-231): Propagate the null-excluding attribute
1311
1312 Set_Can_Never_Be_Null (T_Name, Null_Exclusion_Present (T_Def));
1313
1314 Check_Restriction (No_Access_Subprograms, T_Def);
1315 end Access_Subprogram_Declaration;
1316
1317 ----------------------------
1318 -- Access_Type_Declaration --
1319 ----------------------------
1320
1321 procedure Access_Type_Declaration (T : Entity_Id; Def : Node_Id) is
1322 P : constant Node_Id := Parent (Def);
1323 S : constant Node_Id := Subtype_Indication (Def);
1324
1325 Full_Desig : Entity_Id;
1326
1327 begin
1328 Check_SPARK_05_Restriction ("access type is not allowed", Def);
1329
1330 -- Check for permissible use of incomplete type
1331
1332 if Nkind (S) /= N_Subtype_Indication then
1333 Analyze (S);
1334
1335 if Present (Entity (S))
1336 and then Ekind (Root_Type (Entity (S))) = E_Incomplete_Type
1337 then
1338 Set_Directly_Designated_Type (T, Entity (S));
1339
1340 -- If the designated type is a limited view, we cannot tell if
1341 -- the full view contains tasks, and there is no way to handle
1342 -- that full view in a client. We create a master entity for the
1343 -- scope, which will be used when a client determines that one
1344 -- is needed.
1345
1346 if From_Limited_With (Entity (S))
1347 and then not Is_Class_Wide_Type (Entity (S))
1348 then
1349 Set_Ekind (T, E_Access_Type);
1350 Build_Master_Entity (T);
1351 Build_Master_Renaming (T);
1352 end if;
1353
1354 else
1355 Set_Directly_Designated_Type (T, Process_Subtype (S, P, T, 'P'));
1356 end if;
1357
1358 -- If the access definition is of the form: ACCESS NOT NULL ..
1359 -- the subtype indication must be of an access type. Create
1360 -- a null-excluding subtype of it.
1361
1362 if Null_Excluding_Subtype (Def) then
1363 if not Is_Access_Type (Entity (S)) then
1364 Error_Msg_N ("null exclusion must apply to access type", Def);
1365
1366 else
1367 declare
1368 Loc : constant Source_Ptr := Sloc (S);
1369 Decl : Node_Id;
1370 Nam : constant Entity_Id := Make_Temporary (Loc, 'S');
1371
1372 begin
1373 Decl :=
1374 Make_Subtype_Declaration (Loc,
1375 Defining_Identifier => Nam,
1376 Subtype_Indication =>
1377 New_Occurrence_Of (Entity (S), Loc));
1378 Set_Null_Exclusion_Present (Decl);
1379 Insert_Before (Parent (Def), Decl);
1380 Analyze (Decl);
1381 Set_Entity (S, Nam);
1382 end;
1383 end if;
1384 end if;
1385
1386 else
1387 Set_Directly_Designated_Type (T,
1388 Process_Subtype (S, P, T, 'P'));
1389 end if;
1390
1391 if All_Present (Def) or Constant_Present (Def) then
1392 Set_Ekind (T, E_General_Access_Type);
1393 else
1394 Set_Ekind (T, E_Access_Type);
1395 end if;
1396
1397 Full_Desig := Designated_Type (T);
1398
1399 if Base_Type (Full_Desig) = T then
1400 Error_Msg_N ("access type cannot designate itself", S);
1401
1402 -- In Ada 2005, the type may have a limited view through some unit in
1403 -- its own context, allowing the following circularity that cannot be
1404 -- detected earlier.
1405
1406 elsif Is_Class_Wide_Type (Full_Desig) and then Etype (Full_Desig) = T
1407 then
1408 Error_Msg_N
1409 ("access type cannot designate its own class-wide type", S);
1410
1411 -- Clean up indication of tagged status to prevent cascaded errors
1412
1413 Set_Is_Tagged_Type (T, False);
1414 end if;
1415
1416 Set_Etype (T, T);
1417
1418 -- If the type has appeared already in a with_type clause, it is frozen
1419 -- and the pointer size is already set. Else, initialize.
1420
1421 if not From_Limited_With (T) then
1422 Init_Size_Align (T);
1423 end if;
1424
1425 -- Note that Has_Task is always false, since the access type itself
1426 -- is not a task type. See Einfo for more description on this point.
1427 -- Exactly the same consideration applies to Has_Controlled_Component
1428 -- and to Has_Protected.
1429
1430 Set_Has_Task (T, False);
1431 Set_Has_Protected (T, False);
1432 Set_Has_Timing_Event (T, False);
1433 Set_Has_Controlled_Component (T, False);
1434
1435 -- Initialize field Finalization_Master explicitly to Empty, to avoid
1436 -- problems where an incomplete view of this entity has been previously
1437 -- established by a limited with and an overlaid version of this field
1438 -- (Stored_Constraint) was initialized for the incomplete view.
1439
1440 -- This reset is performed in most cases except where the access type
1441 -- has been created for the purposes of allocating or deallocating a
1442 -- build-in-place object. Such access types have explicitly set pools
1443 -- and finalization masters.
1444
1445 if No (Associated_Storage_Pool (T)) then
1446 Set_Finalization_Master (T, Empty);
1447 end if;
1448
1449 -- Ada 2005 (AI-231): Propagate the null-excluding and access-constant
1450 -- attributes
1451
1452 Set_Can_Never_Be_Null (T, Null_Exclusion_Present (Def));
1453 Set_Is_Access_Constant (T, Constant_Present (Def));
1454 end Access_Type_Declaration;
1455
1456 ----------------------------------
1457 -- Add_Interface_Tag_Components --
1458 ----------------------------------
1459
1460 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id) is
1461 Loc : constant Source_Ptr := Sloc (N);
1462 L : List_Id;
1463 Last_Tag : Node_Id;
1464
1465 procedure Add_Tag (Iface : Entity_Id);
1466 -- Add tag for one of the progenitor interfaces
1467
1468 -------------
1469 -- Add_Tag --
1470 -------------
1471
1472 procedure Add_Tag (Iface : Entity_Id) is
1473 Decl : Node_Id;
1474 Def : Node_Id;
1475 Tag : Entity_Id;
1476 Offset : Entity_Id;
1477
1478 begin
1479 pragma Assert (Is_Tagged_Type (Iface) and then Is_Interface (Iface));
1480
1481 -- This is a reasonable place to propagate predicates
1482
1483 if Has_Predicates (Iface) then
1484 Set_Has_Predicates (Typ);
1485 end if;
1486
1487 Def :=
1488 Make_Component_Definition (Loc,
1489 Aliased_Present => True,
1490 Subtype_Indication =>
1491 New_Occurrence_Of (RTE (RE_Interface_Tag), Loc));
1492
1493 Tag := Make_Temporary (Loc, 'V');
1494
1495 Decl :=
1496 Make_Component_Declaration (Loc,
1497 Defining_Identifier => Tag,
1498 Component_Definition => Def);
1499
1500 Analyze_Component_Declaration (Decl);
1501
1502 Set_Analyzed (Decl);
1503 Set_Ekind (Tag, E_Component);
1504 Set_Is_Tag (Tag);
1505 Set_Is_Aliased (Tag);
1506 Set_Related_Type (Tag, Iface);
1507 Init_Component_Location (Tag);
1508
1509 pragma Assert (Is_Frozen (Iface));
1510
1511 Set_DT_Entry_Count (Tag,
1512 DT_Entry_Count (First_Entity (Iface)));
1513
1514 if No (Last_Tag) then
1515 Prepend (Decl, L);
1516 else
1517 Insert_After (Last_Tag, Decl);
1518 end if;
1519
1520 Last_Tag := Decl;
1521
1522 -- If the ancestor has discriminants we need to give special support
1523 -- to store the offset_to_top value of the secondary dispatch tables.
1524 -- For this purpose we add a supplementary component just after the
1525 -- field that contains the tag associated with each secondary DT.
1526
1527 if Typ /= Etype (Typ) and then Has_Discriminants (Etype (Typ)) then
1528 Def :=
1529 Make_Component_Definition (Loc,
1530 Subtype_Indication =>
1531 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
1532
1533 Offset := Make_Temporary (Loc, 'V');
1534
1535 Decl :=
1536 Make_Component_Declaration (Loc,
1537 Defining_Identifier => Offset,
1538 Component_Definition => Def);
1539
1540 Analyze_Component_Declaration (Decl);
1541
1542 Set_Analyzed (Decl);
1543 Set_Ekind (Offset, E_Component);
1544 Set_Is_Aliased (Offset);
1545 Set_Related_Type (Offset, Iface);
1546 Init_Component_Location (Offset);
1547 Insert_After (Last_Tag, Decl);
1548 Last_Tag := Decl;
1549 end if;
1550 end Add_Tag;
1551
1552 -- Local variables
1553
1554 Elmt : Elmt_Id;
1555 Ext : Node_Id;
1556 Comp : Node_Id;
1557
1558 -- Start of processing for Add_Interface_Tag_Components
1559
1560 begin
1561 if not RTE_Available (RE_Interface_Tag) then
1562 Error_Msg
1563 ("(Ada 2005) interface types not supported by this run-time!",
1564 Sloc (N));
1565 return;
1566 end if;
1567
1568 if Ekind (Typ) /= E_Record_Type
1569 or else (Is_Concurrent_Record_Type (Typ)
1570 and then Is_Empty_List (Abstract_Interface_List (Typ)))
1571 or else (not Is_Concurrent_Record_Type (Typ)
1572 and then No (Interfaces (Typ))
1573 and then Is_Empty_Elmt_List (Interfaces (Typ)))
1574 then
1575 return;
1576 end if;
1577
1578 -- Find the current last tag
1579
1580 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1581 Ext := Record_Extension_Part (Type_Definition (N));
1582 else
1583 pragma Assert (Nkind (Type_Definition (N)) = N_Record_Definition);
1584 Ext := Type_Definition (N);
1585 end if;
1586
1587 Last_Tag := Empty;
1588
1589 if not (Present (Component_List (Ext))) then
1590 Set_Null_Present (Ext, False);
1591 L := New_List;
1592 Set_Component_List (Ext,
1593 Make_Component_List (Loc,
1594 Component_Items => L,
1595 Null_Present => False));
1596 else
1597 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1598 L := Component_Items
1599 (Component_List
1600 (Record_Extension_Part
1601 (Type_Definition (N))));
1602 else
1603 L := Component_Items
1604 (Component_List
1605 (Type_Definition (N)));
1606 end if;
1607
1608 -- Find the last tag component
1609
1610 Comp := First (L);
1611 while Present (Comp) loop
1612 if Nkind (Comp) = N_Component_Declaration
1613 and then Is_Tag (Defining_Identifier (Comp))
1614 then
1615 Last_Tag := Comp;
1616 end if;
1617
1618 Next (Comp);
1619 end loop;
1620 end if;
1621
1622 -- At this point L references the list of components and Last_Tag
1623 -- references the current last tag (if any). Now we add the tag
1624 -- corresponding with all the interfaces that are not implemented
1625 -- by the parent.
1626
1627 if Present (Interfaces (Typ)) then
1628 Elmt := First_Elmt (Interfaces (Typ));
1629 while Present (Elmt) loop
1630 Add_Tag (Node (Elmt));
1631 Next_Elmt (Elmt);
1632 end loop;
1633 end if;
1634 end Add_Interface_Tag_Components;
1635
1636 -------------------------------------
1637 -- Add_Internal_Interface_Entities --
1638 -------------------------------------
1639
1640 procedure Add_Internal_Interface_Entities (Tagged_Type : Entity_Id) is
1641 Elmt : Elmt_Id;
1642 Iface : Entity_Id;
1643 Iface_Elmt : Elmt_Id;
1644 Iface_Prim : Entity_Id;
1645 Ifaces_List : Elist_Id;
1646 New_Subp : Entity_Id := Empty;
1647 Prim : Entity_Id;
1648 Restore_Scope : Boolean := False;
1649
1650 begin
1651 pragma Assert (Ada_Version >= Ada_2005
1652 and then Is_Record_Type (Tagged_Type)
1653 and then Is_Tagged_Type (Tagged_Type)
1654 and then Has_Interfaces (Tagged_Type)
1655 and then not Is_Interface (Tagged_Type));
1656
1657 -- Ensure that the internal entities are added to the scope of the type
1658
1659 if Scope (Tagged_Type) /= Current_Scope then
1660 Push_Scope (Scope (Tagged_Type));
1661 Restore_Scope := True;
1662 end if;
1663
1664 Collect_Interfaces (Tagged_Type, Ifaces_List);
1665
1666 Iface_Elmt := First_Elmt (Ifaces_List);
1667 while Present (Iface_Elmt) loop
1668 Iface := Node (Iface_Elmt);
1669
1670 -- Originally we excluded here from this processing interfaces that
1671 -- are parents of Tagged_Type because their primitives are located
1672 -- in the primary dispatch table (and hence no auxiliary internal
1673 -- entities are required to handle secondary dispatch tables in such
1674 -- case). However, these auxiliary entities are also required to
1675 -- handle derivations of interfaces in formals of generics (see
1676 -- Derive_Subprograms).
1677
1678 Elmt := First_Elmt (Primitive_Operations (Iface));
1679 while Present (Elmt) loop
1680 Iface_Prim := Node (Elmt);
1681
1682 if not Is_Predefined_Dispatching_Operation (Iface_Prim) then
1683 Prim :=
1684 Find_Primitive_Covering_Interface
1685 (Tagged_Type => Tagged_Type,
1686 Iface_Prim => Iface_Prim);
1687
1688 if No (Prim) and then Serious_Errors_Detected > 0 then
1689 goto Continue;
1690 end if;
1691
1692 pragma Assert (Present (Prim));
1693
1694 -- Ada 2012 (AI05-0197): If the name of the covering primitive
1695 -- differs from the name of the interface primitive then it is
1696 -- a private primitive inherited from a parent type. In such
1697 -- case, given that Tagged_Type covers the interface, the
1698 -- inherited private primitive becomes visible. For such
1699 -- purpose we add a new entity that renames the inherited
1700 -- private primitive.
1701
1702 if Chars (Prim) /= Chars (Iface_Prim) then
1703 pragma Assert (Has_Suffix (Prim, 'P'));
1704 Derive_Subprogram
1705 (New_Subp => New_Subp,
1706 Parent_Subp => Iface_Prim,
1707 Derived_Type => Tagged_Type,
1708 Parent_Type => Iface);
1709 Set_Alias (New_Subp, Prim);
1710 Set_Is_Abstract_Subprogram
1711 (New_Subp, Is_Abstract_Subprogram (Prim));
1712 end if;
1713
1714 Derive_Subprogram
1715 (New_Subp => New_Subp,
1716 Parent_Subp => Iface_Prim,
1717 Derived_Type => Tagged_Type,
1718 Parent_Type => Iface);
1719
1720 declare
1721 Anc : Entity_Id;
1722 begin
1723 if Is_Inherited_Operation (Prim)
1724 and then Present (Alias (Prim))
1725 then
1726 Anc := Alias (Prim);
1727 else
1728 Anc := Overridden_Operation (Prim);
1729 end if;
1730
1731 -- Apply legality checks in RM 6.1.1 (10-13) concerning
1732 -- nonconforming preconditions in both an ancestor and
1733 -- a progenitor operation.
1734
1735 if Present (Anc)
1736 and then Has_Non_Trivial_Precondition (Anc)
1737 and then Has_Non_Trivial_Precondition (Iface_Prim)
1738 then
1739 if Is_Abstract_Subprogram (Prim)
1740 or else
1741 (Ekind (Prim) = E_Procedure
1742 and then Nkind (Parent (Prim)) =
1743 N_Procedure_Specification
1744 and then Null_Present (Parent (Prim)))
1745 then
1746 null;
1747
1748 -- The inherited operation must be overridden
1749
1750 elsif not Comes_From_Source (Prim) then
1751 Error_Msg_NE
1752 ("&inherits non-conforming preconditions and must "
1753 & "be overridden (RM 6.1.1 (10-16)",
1754 Parent (Tagged_Type), Prim);
1755 end if;
1756 end if;
1757 end;
1758
1759 -- Ada 2005 (AI-251): Decorate internal entity Iface_Subp
1760 -- associated with interface types. These entities are
1761 -- only registered in the list of primitives of its
1762 -- corresponding tagged type because they are only used
1763 -- to fill the contents of the secondary dispatch tables.
1764 -- Therefore they are removed from the homonym chains.
1765
1766 Set_Is_Hidden (New_Subp);
1767 Set_Is_Internal (New_Subp);
1768 Set_Alias (New_Subp, Prim);
1769 Set_Is_Abstract_Subprogram
1770 (New_Subp, Is_Abstract_Subprogram (Prim));
1771 Set_Interface_Alias (New_Subp, Iface_Prim);
1772
1773 -- If the returned type is an interface then propagate it to
1774 -- the returned type. Needed by the thunk to generate the code
1775 -- which displaces "this" to reference the corresponding
1776 -- secondary dispatch table in the returned object.
1777
1778 if Is_Interface (Etype (Iface_Prim)) then
1779 Set_Etype (New_Subp, Etype (Iface_Prim));
1780 end if;
1781
1782 -- Internal entities associated with interface types are only
1783 -- registered in the list of primitives of the tagged type.
1784 -- They are only used to fill the contents of the secondary
1785 -- dispatch tables. Therefore they are not needed in the
1786 -- homonym chains.
1787
1788 Remove_Homonym (New_Subp);
1789
1790 -- Hidden entities associated with interfaces must have set
1791 -- the Has_Delay_Freeze attribute to ensure that, in case
1792 -- of locally defined tagged types (or compiling with static
1793 -- dispatch tables generation disabled) the corresponding
1794 -- entry of the secondary dispatch table is filled when such
1795 -- an entity is frozen. This is an expansion activity that must
1796 -- be suppressed for ASIS because it leads to gigi elaboration
1797 -- issues in annotate mode.
1798
1799 if not ASIS_Mode then
1800 Set_Has_Delayed_Freeze (New_Subp);
1801 end if;
1802 end if;
1803
1804 <<Continue>>
1805 Next_Elmt (Elmt);
1806 end loop;
1807
1808 Next_Elmt (Iface_Elmt);
1809 end loop;
1810
1811 if Restore_Scope then
1812 Pop_Scope;
1813 end if;
1814 end Add_Internal_Interface_Entities;
1815
1816 -----------------------------------
1817 -- Analyze_Component_Declaration --
1818 -----------------------------------
1819
1820 procedure Analyze_Component_Declaration (N : Node_Id) is
1821 Loc : constant Source_Ptr := Sloc (Component_Definition (N));
1822 Id : constant Entity_Id := Defining_Identifier (N);
1823 E : constant Node_Id := Expression (N);
1824 Typ : constant Node_Id :=
1825 Subtype_Indication (Component_Definition (N));
1826 T : Entity_Id;
1827 P : Entity_Id;
1828
1829 function Contains_POC (Constr : Node_Id) return Boolean;
1830 -- Determines whether a constraint uses the discriminant of a record
1831 -- type thus becoming a per-object constraint (POC).
1832
1833 function Is_Known_Limited (Typ : Entity_Id) return Boolean;
1834 -- Typ is the type of the current component, check whether this type is
1835 -- a limited type. Used to validate declaration against that of
1836 -- enclosing record.
1837
1838 ------------------
1839 -- Contains_POC --
1840 ------------------
1841
1842 function Contains_POC (Constr : Node_Id) return Boolean is
1843 begin
1844 -- Prevent cascaded errors
1845
1846 if Error_Posted (Constr) then
1847 return False;
1848 end if;
1849
1850 case Nkind (Constr) is
1851 when N_Attribute_Reference =>
1852 return Attribute_Name (Constr) = Name_Access
1853 and then Prefix (Constr) = Scope (Entity (Prefix (Constr)));
1854
1855 when N_Discriminant_Association =>
1856 return Denotes_Discriminant (Expression (Constr));
1857
1858 when N_Identifier =>
1859 return Denotes_Discriminant (Constr);
1860
1861 when N_Index_Or_Discriminant_Constraint =>
1862 declare
1863 IDC : Node_Id;
1864
1865 begin
1866 IDC := First (Constraints (Constr));
1867 while Present (IDC) loop
1868
1869 -- One per-object constraint is sufficient
1870
1871 if Contains_POC (IDC) then
1872 return True;
1873 end if;
1874
1875 Next (IDC);
1876 end loop;
1877
1878 return False;
1879 end;
1880
1881 when N_Range =>
1882 return Denotes_Discriminant (Low_Bound (Constr))
1883 or else
1884 Denotes_Discriminant (High_Bound (Constr));
1885
1886 when N_Range_Constraint =>
1887 return Denotes_Discriminant (Range_Expression (Constr));
1888
1889 when others =>
1890 return False;
1891 end case;
1892 end Contains_POC;
1893
1894 ----------------------
1895 -- Is_Known_Limited --
1896 ----------------------
1897
1898 function Is_Known_Limited (Typ : Entity_Id) return Boolean is
1899 P : constant Entity_Id := Etype (Typ);
1900 R : constant Entity_Id := Root_Type (Typ);
1901
1902 begin
1903 if Is_Limited_Record (Typ) then
1904 return True;
1905
1906 -- If the root type is limited (and not a limited interface)
1907 -- so is the current type
1908
1909 elsif Is_Limited_Record (R)
1910 and then (not Is_Interface (R) or else not Is_Limited_Interface (R))
1911 then
1912 return True;
1913
1914 -- Else the type may have a limited interface progenitor, but a
1915 -- limited record parent.
1916
1917 elsif R /= P and then Is_Limited_Record (P) then
1918 return True;
1919
1920 else
1921 return False;
1922 end if;
1923 end Is_Known_Limited;
1924
1925 -- Start of processing for Analyze_Component_Declaration
1926
1927 begin
1928 Generate_Definition (Id);
1929 Enter_Name (Id);
1930
1931 if Present (Typ) then
1932 T := Find_Type_Of_Object
1933 (Subtype_Indication (Component_Definition (N)), N);
1934
1935 if not Nkind_In (Typ, N_Identifier, N_Expanded_Name) then
1936 Check_SPARK_05_Restriction ("subtype mark required", Typ);
1937 end if;
1938
1939 -- Ada 2005 (AI-230): Access Definition case
1940
1941 else
1942 pragma Assert (Present
1943 (Access_Definition (Component_Definition (N))));
1944
1945 T := Access_Definition
1946 (Related_Nod => N,
1947 N => Access_Definition (Component_Definition (N)));
1948 Set_Is_Local_Anonymous_Access (T);
1949
1950 -- Ada 2005 (AI-254)
1951
1952 if Present (Access_To_Subprogram_Definition
1953 (Access_Definition (Component_Definition (N))))
1954 and then Protected_Present (Access_To_Subprogram_Definition
1955 (Access_Definition
1956 (Component_Definition (N))))
1957 then
1958 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1959 end if;
1960 end if;
1961
1962 -- If the subtype is a constrained subtype of the enclosing record,
1963 -- (which must have a partial view) the back-end does not properly
1964 -- handle the recursion. Rewrite the component declaration with an
1965 -- explicit subtype indication, which is acceptable to Gigi. We can copy
1966 -- the tree directly because side effects have already been removed from
1967 -- discriminant constraints.
1968
1969 if Ekind (T) = E_Access_Subtype
1970 and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
1971 and then Comes_From_Source (T)
1972 and then Nkind (Parent (T)) = N_Subtype_Declaration
1973 and then Etype (Directly_Designated_Type (T)) = Current_Scope
1974 then
1975 Rewrite
1976 (Subtype_Indication (Component_Definition (N)),
1977 New_Copy_Tree (Subtype_Indication (Parent (T))));
1978 T := Find_Type_Of_Object
1979 (Subtype_Indication (Component_Definition (N)), N);
1980 end if;
1981
1982 -- If the component declaration includes a default expression, then we
1983 -- check that the component is not of a limited type (RM 3.7(5)),
1984 -- and do the special preanalysis of the expression (see section on
1985 -- "Handling of Default and Per-Object Expressions" in the spec of
1986 -- package Sem).
1987
1988 if Present (E) then
1989 Check_SPARK_05_Restriction ("default expression is not allowed", E);
1990 Preanalyze_Default_Expression (E, T);
1991 Check_Initialization (T, E);
1992
1993 if Ada_Version >= Ada_2005
1994 and then Ekind (T) = E_Anonymous_Access_Type
1995 and then Etype (E) /= Any_Type
1996 then
1997 -- Check RM 3.9.2(9): "if the expected type for an expression is
1998 -- an anonymous access-to-specific tagged type, then the object
1999 -- designated by the expression shall not be dynamically tagged
2000 -- unless it is a controlling operand in a call on a dispatching
2001 -- operation"
2002
2003 if Is_Tagged_Type (Directly_Designated_Type (T))
2004 and then
2005 Ekind (Directly_Designated_Type (T)) /= E_Class_Wide_Type
2006 and then
2007 Ekind (Directly_Designated_Type (Etype (E))) =
2008 E_Class_Wide_Type
2009 then
2010 Error_Msg_N
2011 ("access to specific tagged type required (RM 3.9.2(9))", E);
2012 end if;
2013
2014 -- (Ada 2005: AI-230): Accessibility check for anonymous
2015 -- components
2016
2017 if Type_Access_Level (Etype (E)) >
2018 Deepest_Type_Access_Level (T)
2019 then
2020 Error_Msg_N
2021 ("expression has deeper access level than component " &
2022 "(RM 3.10.2 (12.2))", E);
2023 end if;
2024
2025 -- The initialization expression is a reference to an access
2026 -- discriminant. The type of the discriminant is always deeper
2027 -- than any access type.
2028
2029 if Ekind (Etype (E)) = E_Anonymous_Access_Type
2030 and then Is_Entity_Name (E)
2031 and then Ekind (Entity (E)) = E_In_Parameter
2032 and then Present (Discriminal_Link (Entity (E)))
2033 then
2034 Error_Msg_N
2035 ("discriminant has deeper accessibility level than target",
2036 E);
2037 end if;
2038 end if;
2039 end if;
2040
2041 -- The parent type may be a private view with unknown discriminants,
2042 -- and thus unconstrained. Regular components must be constrained.
2043
2044 if not Is_Definite_Subtype (T) and then Chars (Id) /= Name_uParent then
2045 if Is_Class_Wide_Type (T) then
2046 Error_Msg_N
2047 ("class-wide subtype with unknown discriminants" &
2048 " in component declaration",
2049 Subtype_Indication (Component_Definition (N)));
2050 else
2051 Error_Msg_N
2052 ("unconstrained subtype in component declaration",
2053 Subtype_Indication (Component_Definition (N)));
2054 end if;
2055
2056 -- Components cannot be abstract, except for the special case of
2057 -- the _Parent field (case of extending an abstract tagged type)
2058
2059 elsif Is_Abstract_Type (T) and then Chars (Id) /= Name_uParent then
2060 Error_Msg_N ("type of a component cannot be abstract", N);
2061 end if;
2062
2063 Set_Etype (Id, T);
2064 Set_Is_Aliased (Id, Aliased_Present (Component_Definition (N)));
2065
2066 -- The component declaration may have a per-object constraint, set
2067 -- the appropriate flag in the defining identifier of the subtype.
2068
2069 if Present (Subtype_Indication (Component_Definition (N))) then
2070 declare
2071 Sindic : constant Node_Id :=
2072 Subtype_Indication (Component_Definition (N));
2073 begin
2074 if Nkind (Sindic) = N_Subtype_Indication
2075 and then Present (Constraint (Sindic))
2076 and then Contains_POC (Constraint (Sindic))
2077 then
2078 Set_Has_Per_Object_Constraint (Id);
2079 end if;
2080 end;
2081 end if;
2082
2083 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
2084 -- out some static checks.
2085
2086 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (T) then
2087 Null_Exclusion_Static_Checks (N);
2088 end if;
2089
2090 -- If this component is private (or depends on a private type), flag the
2091 -- record type to indicate that some operations are not available.
2092
2093 P := Private_Component (T);
2094
2095 if Present (P) then
2096
2097 -- Check for circular definitions
2098
2099 if P = Any_Type then
2100 Set_Etype (Id, Any_Type);
2101
2102 -- There is a gap in the visibility of operations only if the
2103 -- component type is not defined in the scope of the record type.
2104
2105 elsif Scope (P) = Scope (Current_Scope) then
2106 null;
2107
2108 elsif Is_Limited_Type (P) then
2109 Set_Is_Limited_Composite (Current_Scope);
2110
2111 else
2112 Set_Is_Private_Composite (Current_Scope);
2113 end if;
2114 end if;
2115
2116 if P /= Any_Type
2117 and then Is_Limited_Type (T)
2118 and then Chars (Id) /= Name_uParent
2119 and then Is_Tagged_Type (Current_Scope)
2120 then
2121 if Is_Derived_Type (Current_Scope)
2122 and then not Is_Known_Limited (Current_Scope)
2123 then
2124 Error_Msg_N
2125 ("extension of nonlimited type cannot have limited components",
2126 N);
2127
2128 if Is_Interface (Root_Type (Current_Scope)) then
2129 Error_Msg_N
2130 ("\limitedness is not inherited from limited interface", N);
2131 Error_Msg_N ("\add LIMITED to type indication", N);
2132 end if;
2133
2134 Explain_Limited_Type (T, N);
2135 Set_Etype (Id, Any_Type);
2136 Set_Is_Limited_Composite (Current_Scope, False);
2137
2138 elsif not Is_Derived_Type (Current_Scope)
2139 and then not Is_Limited_Record (Current_Scope)
2140 and then not Is_Concurrent_Type (Current_Scope)
2141 then
2142 Error_Msg_N
2143 ("nonlimited tagged type cannot have limited components", N);
2144 Explain_Limited_Type (T, N);
2145 Set_Etype (Id, Any_Type);
2146 Set_Is_Limited_Composite (Current_Scope, False);
2147 end if;
2148 end if;
2149
2150 -- If the component is an unconstrained task or protected type with
2151 -- discriminants, the component and the enclosing record are limited
2152 -- and the component is constrained by its default values. Compute
2153 -- its actual subtype, else it may be allocated the maximum size by
2154 -- the backend, and possibly overflow.
2155
2156 if Is_Concurrent_Type (T)
2157 and then not Is_Constrained (T)
2158 and then Has_Discriminants (T)
2159 and then not Has_Discriminants (Current_Scope)
2160 then
2161 declare
2162 Act_T : constant Entity_Id := Build_Default_Subtype (T, N);
2163
2164 begin
2165 Set_Etype (Id, Act_T);
2166
2167 -- Rewrite component definition to use the constrained subtype
2168
2169 Rewrite (Component_Definition (N),
2170 Make_Component_Definition (Loc,
2171 Subtype_Indication => New_Occurrence_Of (Act_T, Loc)));
2172 end;
2173 end if;
2174
2175 Set_Original_Record_Component (Id, Id);
2176
2177 if Has_Aspects (N) then
2178 Analyze_Aspect_Specifications (N, Id);
2179 end if;
2180
2181 Analyze_Dimension (N);
2182 end Analyze_Component_Declaration;
2183
2184 --------------------------
2185 -- Analyze_Declarations --
2186 --------------------------
2187
2188 procedure Analyze_Declarations (L : List_Id) is
2189 Decl : Node_Id;
2190
2191 procedure Adjust_Decl;
2192 -- Adjust Decl not to include implicit label declarations, since these
2193 -- have strange Sloc values that result in elaboration check problems.
2194 -- (They have the sloc of the label as found in the source, and that
2195 -- is ahead of the current declarative part).
2196
2197 procedure Build_Assertion_Bodies (Decls : List_Id; Context : Node_Id);
2198 -- Create the subprogram bodies which verify the run-time semantics of
2199 -- the pragmas listed below for each elibigle type found in declarative
2200 -- list Decls. The pragmas are:
2201 --
2202 -- Default_Initial_Condition
2203 -- Invariant
2204 -- Type_Invariant
2205 --
2206 -- Context denotes the owner of the declarative list.
2207
2208 procedure Check_Entry_Contracts;
2209 -- Perform a pre-analysis of the pre- and postconditions of an entry
2210 -- declaration. This must be done before full resolution and creation
2211 -- of the parameter block, etc. to catch illegal uses within the
2212 -- contract expression. Full analysis of the expression is done when
2213 -- the contract is processed.
2214
2215 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id);
2216 -- Determine whether Body_Decl denotes the body of a late controlled
2217 -- primitive (either Initialize, Adjust or Finalize). If this is the
2218 -- case, add a proper spec if the body lacks one. The spec is inserted
2219 -- before Body_Decl and immediately analyzed.
2220
2221 procedure Remove_Partial_Visible_Refinements (Spec_Id : Entity_Id);
2222 -- Spec_Id is the entity of a package that may define abstract states,
2223 -- and in the case of a child unit, whose ancestors may define abstract
2224 -- states. If the states have partial visible refinement, remove the
2225 -- partial visibility of each constituent at the end of the package
2226 -- spec and body declarations.
2227
2228 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id);
2229 -- Spec_Id is the entity of a package that may define abstract states.
2230 -- If the states have visible refinement, remove the visibility of each
2231 -- constituent at the end of the package body declaration.
2232
2233 procedure Resolve_Aspects;
2234 -- Utility to resolve the expressions of aspects at the end of a list of
2235 -- declarations.
2236
2237 function Uses_Unseen_Lib_Unit_Priv (Pkg : Entity_Id) return Boolean;
2238 -- Check if an inner package has entities within it that rely on library
2239 -- level private types where the full view has not been seen.
2240
2241 -----------------
2242 -- Adjust_Decl --
2243 -----------------
2244
2245 procedure Adjust_Decl is
2246 begin
2247 while Present (Prev (Decl))
2248 and then Nkind (Decl) = N_Implicit_Label_Declaration
2249 loop
2250 Prev (Decl);
2251 end loop;
2252 end Adjust_Decl;
2253
2254 ----------------------------
2255 -- Build_Assertion_Bodies --
2256 ----------------------------
2257
2258 procedure Build_Assertion_Bodies (Decls : List_Id; Context : Node_Id) is
2259 procedure Build_Assertion_Bodies_For_Type (Typ : Entity_Id);
2260 -- Create the subprogram bodies which verify the run-time semantics
2261 -- of the pragmas listed below for type Typ. The pragmas are:
2262 --
2263 -- Default_Initial_Condition
2264 -- Invariant
2265 -- Type_Invariant
2266
2267 -------------------------------------
2268 -- Build_Assertion_Bodies_For_Type --
2269 -------------------------------------
2270
2271 procedure Build_Assertion_Bodies_For_Type (Typ : Entity_Id) is
2272 begin
2273 -- Preanalyze and resolve the Default_Initial_Condition assertion
2274 -- expression at the end of the declarations to catch any errors.
2275
2276 if Has_DIC (Typ) then
2277 Build_DIC_Procedure_Body (Typ);
2278 end if;
2279
2280 if Nkind (Context) = N_Package_Specification then
2281
2282 -- Preanalyze and resolve the class-wide invariants of an
2283 -- interface at the end of whichever declarative part has the
2284 -- interface type. Note that an interface may be declared in
2285 -- any non-package declarative part, but reaching the end of
2286 -- such a declarative part will always freeze the type and
2287 -- generate the invariant procedure (see Freeze_Type).
2288
2289 if Is_Interface (Typ) then
2290
2291 -- Interfaces are treated as the partial view of a private
2292 -- type, in order to achieve uniformity with the general
2293 -- case. As a result, an interface receives only a "partial"
2294 -- invariant procedure, which is never called.
2295
2296 if Has_Own_Invariants (Typ) then
2297 Build_Invariant_Procedure_Body
2298 (Typ => Typ,
2299 Partial_Invariant => True);
2300 end if;
2301
2302 -- Preanalyze and resolve the invariants of a private type
2303 -- at the end of the visible declarations to catch potential
2304 -- errors. Inherited class-wide invariants are not included
2305 -- because they have already been resolved.
2306
2307 elsif Decls = Visible_Declarations (Context)
2308 and then Ekind_In (Typ, E_Limited_Private_Type,
2309 E_Private_Type,
2310 E_Record_Type_With_Private)
2311 and then Has_Own_Invariants (Typ)
2312 then
2313 Build_Invariant_Procedure_Body
2314 (Typ => Typ,
2315 Partial_Invariant => True);
2316
2317 -- Preanalyze and resolve the invariants of a private type's
2318 -- full view at the end of the private declarations to catch
2319 -- potential errors.
2320
2321 elsif Decls = Private_Declarations (Context)
2322 and then not Is_Private_Type (Typ)
2323 and then Has_Private_Declaration (Typ)
2324 and then Has_Invariants (Typ)
2325 then
2326 Build_Invariant_Procedure_Body (Typ);
2327 end if;
2328 end if;
2329 end Build_Assertion_Bodies_For_Type;
2330
2331 -- Local variables
2332
2333 Decl : Node_Id;
2334 Decl_Id : Entity_Id;
2335
2336 -- Start of processing for Build_Assertion_Bodies
2337
2338 begin
2339 Decl := First (Decls);
2340 while Present (Decl) loop
2341 if Is_Declaration (Decl) then
2342 Decl_Id := Defining_Entity (Decl);
2343
2344 if Is_Type (Decl_Id) then
2345 Build_Assertion_Bodies_For_Type (Decl_Id);
2346 end if;
2347 end if;
2348
2349 Next (Decl);
2350 end loop;
2351 end Build_Assertion_Bodies;
2352
2353 ---------------------------
2354 -- Check_Entry_Contracts --
2355 ---------------------------
2356
2357 procedure Check_Entry_Contracts is
2358 ASN : Node_Id;
2359 Ent : Entity_Id;
2360 Exp : Node_Id;
2361
2362 begin
2363 Ent := First_Entity (Current_Scope);
2364 while Present (Ent) loop
2365
2366 -- This only concerns entries with pre/postconditions
2367
2368 if Ekind (Ent) = E_Entry
2369 and then Present (Contract (Ent))
2370 and then Present (Pre_Post_Conditions (Contract (Ent)))
2371 then
2372 ASN := Pre_Post_Conditions (Contract (Ent));
2373 Push_Scope (Ent);
2374 Install_Formals (Ent);
2375
2376 -- Pre/postconditions are rewritten as Check pragmas. Analysis
2377 -- is performed on a copy of the pragma expression, to prevent
2378 -- modifying the original expression.
2379
2380 while Present (ASN) loop
2381 if Nkind (ASN) = N_Pragma then
2382 Exp :=
2383 New_Copy_Tree
2384 (Expression
2385 (First (Pragma_Argument_Associations (ASN))));
2386 Set_Parent (Exp, ASN);
2387
2388 Preanalyze_Assert_Expression (Exp, Standard_Boolean);
2389 end if;
2390
2391 ASN := Next_Pragma (ASN);
2392 end loop;
2393
2394 End_Scope;
2395 end if;
2396
2397 Next_Entity (Ent);
2398 end loop;
2399 end Check_Entry_Contracts;
2400
2401 --------------------------------------
2402 -- Handle_Late_Controlled_Primitive --
2403 --------------------------------------
2404
2405 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id) is
2406 Body_Spec : constant Node_Id := Specification (Body_Decl);
2407 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2408 Loc : constant Source_Ptr := Sloc (Body_Id);
2409 Params : constant List_Id :=
2410 Parameter_Specifications (Body_Spec);
2411 Spec : Node_Id;
2412 Spec_Id : Entity_Id;
2413 Typ : Node_Id;
2414
2415 begin
2416 -- Consider only procedure bodies whose name matches one of the three
2417 -- controlled primitives.
2418
2419 if Nkind (Body_Spec) /= N_Procedure_Specification
2420 or else not Nam_In (Chars (Body_Id), Name_Adjust,
2421 Name_Finalize,
2422 Name_Initialize)
2423 then
2424 return;
2425
2426 -- A controlled primitive must have exactly one formal which is not
2427 -- an anonymous access type.
2428
2429 elsif List_Length (Params) /= 1 then
2430 return;
2431 end if;
2432
2433 Typ := Parameter_Type (First (Params));
2434
2435 if Nkind (Typ) = N_Access_Definition then
2436 return;
2437 end if;
2438
2439 Find_Type (Typ);
2440
2441 -- The type of the formal must be derived from [Limited_]Controlled
2442
2443 if not Is_Controlled (Entity (Typ)) then
2444 return;
2445 end if;
2446
2447 -- Check whether a specification exists for this body. We do not
2448 -- analyze the spec of the body in full, because it will be analyzed
2449 -- again when the body is properly analyzed, and we cannot create
2450 -- duplicate entries in the formals chain. We look for an explicit
2451 -- specification because the body may be an overriding operation and
2452 -- an inherited spec may be present.
2453
2454 Spec_Id := Current_Entity (Body_Id);
2455
2456 while Present (Spec_Id) loop
2457 if Ekind_In (Spec_Id, E_Procedure, E_Generic_Procedure)
2458 and then Scope (Spec_Id) = Current_Scope
2459 and then Present (First_Formal (Spec_Id))
2460 and then No (Next_Formal (First_Formal (Spec_Id)))
2461 and then Etype (First_Formal (Spec_Id)) = Entity (Typ)
2462 and then Comes_From_Source (Spec_Id)
2463 then
2464 return;
2465 end if;
2466
2467 Spec_Id := Homonym (Spec_Id);
2468 end loop;
2469
2470 -- At this point the body is known to be a late controlled primitive.
2471 -- Generate a matching spec and insert it before the body. Note the
2472 -- use of Copy_Separate_Tree - we want an entirely separate semantic
2473 -- tree in this case.
2474
2475 Spec := Copy_Separate_Tree (Body_Spec);
2476
2477 -- Ensure that the subprogram declaration does not inherit the null
2478 -- indicator from the body as we now have a proper spec/body pair.
2479
2480 Set_Null_Present (Spec, False);
2481
2482 -- Ensure that the freeze node is inserted after the declaration of
2483 -- the primitive since its expansion will freeze the primitive.
2484
2485 Decl := Make_Subprogram_Declaration (Loc, Specification => Spec);
2486
2487 Insert_Before_And_Analyze (Body_Decl, Decl);
2488 end Handle_Late_Controlled_Primitive;
2489
2490 ----------------------------------------
2491 -- Remove_Partial_Visible_Refinements --
2492 ----------------------------------------
2493
2494 procedure Remove_Partial_Visible_Refinements (Spec_Id : Entity_Id) is
2495 State_Elmt : Elmt_Id;
2496 begin
2497 if Present (Abstract_States (Spec_Id)) then
2498 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2499 while Present (State_Elmt) loop
2500 Set_Has_Partial_Visible_Refinement (Node (State_Elmt), False);
2501 Next_Elmt (State_Elmt);
2502 end loop;
2503 end if;
2504
2505 -- For a child unit, also hide the partial state refinement from
2506 -- ancestor packages.
2507
2508 if Is_Child_Unit (Spec_Id) then
2509 Remove_Partial_Visible_Refinements (Scope (Spec_Id));
2510 end if;
2511 end Remove_Partial_Visible_Refinements;
2512
2513 --------------------------------
2514 -- Remove_Visible_Refinements --
2515 --------------------------------
2516
2517 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id) is
2518 State_Elmt : Elmt_Id;
2519 begin
2520 if Present (Abstract_States (Spec_Id)) then
2521 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2522 while Present (State_Elmt) loop
2523 Set_Has_Visible_Refinement (Node (State_Elmt), False);
2524 Next_Elmt (State_Elmt);
2525 end loop;
2526 end if;
2527 end Remove_Visible_Refinements;
2528
2529 ---------------------
2530 -- Resolve_Aspects --
2531 ---------------------
2532
2533 procedure Resolve_Aspects is
2534 E : Entity_Id;
2535
2536 begin
2537 E := First_Entity (Current_Scope);
2538 while Present (E) loop
2539 Resolve_Aspect_Expressions (E);
2540 Next_Entity (E);
2541 end loop;
2542 end Resolve_Aspects;
2543
2544 -------------------------------
2545 -- Uses_Unseen_Lib_Unit_Priv --
2546 -------------------------------
2547
2548 function Uses_Unseen_Lib_Unit_Priv (Pkg : Entity_Id) return Boolean is
2549 Curr : Entity_Id;
2550
2551 begin
2552 -- Avoid looking through scopes that do not meet the precondition of
2553 -- Pkg not being within a library unit spec.
2554
2555 if not Is_Compilation_Unit (Pkg)
2556 and then not Is_Generic_Instance (Pkg)
2557 and then not In_Package_Body (Enclosing_Lib_Unit_Entity (Pkg))
2558 then
2559 -- Loop through all entities in the current scope to identify
2560 -- an entity that depends on a private type.
2561
2562 Curr := First_Entity (Pkg);
2563 loop
2564 if Nkind (Curr) in N_Entity
2565 and then Depends_On_Private (Curr)
2566 then
2567 return True;
2568 end if;
2569
2570 exit when Last_Entity (Current_Scope) = Curr;
2571 Curr := Next_Entity (Curr);
2572 end loop;
2573 end if;
2574
2575 return False;
2576 end Uses_Unseen_Lib_Unit_Priv;
2577
2578 -- Local variables
2579
2580 Context : Node_Id := Empty;
2581 Freeze_From : Entity_Id := Empty;
2582 Next_Decl : Node_Id;
2583
2584 Body_Seen : Boolean := False;
2585 -- Flag set when the first body [stub] is encountered
2586
2587 -- Start of processing for Analyze_Declarations
2588
2589 begin
2590 if Restriction_Check_Required (SPARK_05) then
2591 Check_Later_Vs_Basic_Declarations (L, During_Parsing => False);
2592 end if;
2593
2594 Decl := First (L);
2595 while Present (Decl) loop
2596
2597 -- Package spec cannot contain a package declaration in SPARK
2598
2599 if Nkind (Decl) = N_Package_Declaration
2600 and then Nkind (Parent (L)) = N_Package_Specification
2601 then
2602 Check_SPARK_05_Restriction
2603 ("package specification cannot contain a package declaration",
2604 Decl);
2605 end if;
2606
2607 -- Complete analysis of declaration
2608
2609 Analyze (Decl);
2610 Next_Decl := Next (Decl);
2611
2612 if No (Freeze_From) then
2613 Freeze_From := First_Entity (Current_Scope);
2614 end if;
2615
2616 -- At the end of a declarative part, freeze remaining entities
2617 -- declared in it. The end of the visible declarations of package
2618 -- specification is not the end of a declarative part if private
2619 -- declarations are present. The end of a package declaration is a
2620 -- freezing point only if it a library package. A task definition or
2621 -- protected type definition is not a freeze point either. Finally,
2622 -- we do not freeze entities in generic scopes, because there is no
2623 -- code generated for them and freeze nodes will be generated for
2624 -- the instance.
2625
2626 -- The end of a package instantiation is not a freeze point, but
2627 -- for now we make it one, because the generic body is inserted
2628 -- (currently) immediately after. Generic instantiations will not
2629 -- be a freeze point once delayed freezing of bodies is implemented.
2630 -- (This is needed in any case for early instantiations ???).
2631
2632 if No (Next_Decl) then
2633 if Nkind (Parent (L)) = N_Component_List then
2634 null;
2635
2636 elsif Nkind_In (Parent (L), N_Protected_Definition,
2637 N_Task_Definition)
2638 then
2639 Check_Entry_Contracts;
2640
2641 elsif Nkind (Parent (L)) /= N_Package_Specification then
2642 if Nkind (Parent (L)) = N_Package_Body then
2643 Freeze_From := First_Entity (Current_Scope);
2644 end if;
2645
2646 -- There may have been several freezing points previously,
2647 -- for example object declarations or subprogram bodies, but
2648 -- at the end of a declarative part we check freezing from
2649 -- the beginning, even though entities may already be frozen,
2650 -- in order to perform visibility checks on delayed aspects.
2651
2652 Adjust_Decl;
2653 Freeze_All (First_Entity (Current_Scope), Decl);
2654 Freeze_From := Last_Entity (Current_Scope);
2655
2656 -- Current scope is a package specification
2657
2658 elsif Scope (Current_Scope) /= Standard_Standard
2659 and then not Is_Child_Unit (Current_Scope)
2660 and then No (Generic_Parent (Parent (L)))
2661 then
2662 -- This is needed in all cases to catch visibility errors in
2663 -- aspect expressions, but several large user tests are now
2664 -- rejected. Pending notification we restrict this call to
2665 -- ASIS mode.
2666
2667 if ASIS_Mode then
2668 Resolve_Aspects;
2669 end if;
2670
2671 elsif L /= Visible_Declarations (Parent (L))
2672 or else No (Private_Declarations (Parent (L)))
2673 or else Is_Empty_List (Private_Declarations (Parent (L)))
2674 then
2675 Adjust_Decl;
2676
2677 -- End of a package declaration
2678
2679 -- In compilation mode the expansion of freeze node takes care
2680 -- of resolving expressions of all aspects in the list. In ASIS
2681 -- mode this must be done explicitly.
2682
2683 if ASIS_Mode
2684 and then Scope (Current_Scope) = Standard_Standard
2685 then
2686 Resolve_Aspects;
2687 end if;
2688
2689 -- This is a freeze point because it is the end of a
2690 -- compilation unit.
2691
2692 Freeze_All (First_Entity (Current_Scope), Decl);
2693 Freeze_From := Last_Entity (Current_Scope);
2694
2695 -- At the end of the visible declarations the expressions in
2696 -- aspects of all entities declared so far must be resolved.
2697 -- The entities themselves might be frozen later, and the
2698 -- generated pragmas and attribute definition clauses analyzed
2699 -- in full at that point, but name resolution must take place
2700 -- now.
2701 -- In addition to being the proper semantics, this is mandatory
2702 -- within generic units, because global name capture requires
2703 -- those expressions to be analyzed, given that the generated
2704 -- pragmas do not appear in the original generic tree.
2705
2706 elsif Serious_Errors_Detected = 0 then
2707 Resolve_Aspects;
2708 end if;
2709
2710 -- If next node is a body then freeze all types before the body.
2711 -- An exception occurs for some expander-generated bodies. If these
2712 -- are generated at places where in general language rules would not
2713 -- allow a freeze point, then we assume that the expander has
2714 -- explicitly checked that all required types are properly frozen,
2715 -- and we do not cause general freezing here. This special circuit
2716 -- is used when the encountered body is marked as having already
2717 -- been analyzed.
2718
2719 -- In all other cases (bodies that come from source, and expander
2720 -- generated bodies that have not been analyzed yet), freeze all
2721 -- types now. Note that in the latter case, the expander must take
2722 -- care to attach the bodies at a proper place in the tree so as to
2723 -- not cause unwanted freezing at that point.
2724
2725 -- It is also necessary to check for a case where both an expression
2726 -- function is used and the current scope depends on an unseen
2727 -- private type from a library unit, otherwise premature freezing of
2728 -- the private type will occur.
2729
2730 elsif not Analyzed (Next_Decl) and then Is_Body (Next_Decl)
2731 and then ((Nkind (Next_Decl) /= N_Subprogram_Body
2732 or else not Was_Expression_Function (Next_Decl))
2733 or else not Uses_Unseen_Lib_Unit_Priv (Current_Scope))
2734 then
2735 -- When a controlled type is frozen, the expander generates stream
2736 -- and controlled-type support routines. If the freeze is caused
2737 -- by the stand-alone body of Initialize, Adjust, or Finalize, the
2738 -- expander will end up using the wrong version of these routines,
2739 -- as the body has not been processed yet. To remedy this, detect
2740 -- a late controlled primitive and create a proper spec for it.
2741 -- This ensures that the primitive will override its inherited
2742 -- counterpart before the freeze takes place.
2743
2744 -- If the declaration we just processed is a body, do not attempt
2745 -- to examine Next_Decl as the late primitive idiom can only apply
2746 -- to the first encountered body.
2747
2748 -- The spec of the late primitive is not generated in ASIS mode to
2749 -- ensure a consistent list of primitives that indicates the true
2750 -- semantic structure of the program (which is not relevant when
2751 -- generating executable code).
2752
2753 -- ??? A cleaner approach may be possible and/or this solution
2754 -- could be extended to general-purpose late primitives, TBD.
2755
2756 if not ASIS_Mode
2757 and then not Body_Seen
2758 and then not Is_Body (Decl)
2759 then
2760 Body_Seen := True;
2761
2762 if Nkind (Next_Decl) = N_Subprogram_Body then
2763 Handle_Late_Controlled_Primitive (Next_Decl);
2764 end if;
2765 end if;
2766
2767 Adjust_Decl;
2768
2769 -- The generated body of an expression function does not freeze,
2770 -- unless it is a completion, in which case only the expression
2771 -- itself freezes. This is handled when the body itself is
2772 -- analyzed (see Freeze_Expr_Types, sem_ch6.adb).
2773
2774 Freeze_All (Freeze_From, Decl);
2775 Freeze_From := Last_Entity (Current_Scope);
2776 end if;
2777
2778 Decl := Next_Decl;
2779 end loop;
2780
2781 -- Post-freezing actions
2782
2783 if Present (L) then
2784 Context := Parent (L);
2785
2786 -- Analyze the contracts of packages and their bodies
2787
2788 if Nkind (Context) = N_Package_Specification then
2789
2790 -- When a package has private declarations, its contract must be
2791 -- analyzed at the end of the said declarations. This way both the
2792 -- analysis and freeze actions are properly synchronized in case
2793 -- of private type use within the contract.
2794
2795 if L = Private_Declarations (Context) then
2796 Analyze_Package_Contract (Defining_Entity (Context));
2797
2798 -- Otherwise the contract is analyzed at the end of the visible
2799 -- declarations.
2800
2801 elsif L = Visible_Declarations (Context)
2802 and then No (Private_Declarations (Context))
2803 then
2804 Analyze_Package_Contract (Defining_Entity (Context));
2805 end if;
2806
2807 elsif Nkind (Context) = N_Package_Body then
2808 Analyze_Package_Body_Contract (Defining_Entity (Context));
2809 end if;
2810
2811 -- Analyze the contracts of various constructs now due to the delayed
2812 -- visibility needs of their aspects and pragmas.
2813
2814 Analyze_Contracts (L);
2815
2816 if Nkind (Context) = N_Package_Body then
2817
2818 -- Ensure that all abstract states and objects declared in the
2819 -- state space of a package body are utilized as constituents.
2820
2821 Check_Unused_Body_States (Defining_Entity (Context));
2822
2823 -- State refinements are visible up to the end of the package body
2824 -- declarations. Hide the state refinements from visibility to
2825 -- restore the original state conditions.
2826
2827 Remove_Visible_Refinements (Corresponding_Spec (Context));
2828 Remove_Partial_Visible_Refinements (Corresponding_Spec (Context));
2829
2830 elsif Nkind (Context) = N_Package_Declaration then
2831
2832 -- Partial state refinements are visible up to the end of the
2833 -- package spec declarations. Hide the partial state refinements
2834 -- from visibility to restore the original state conditions.
2835
2836 Remove_Partial_Visible_Refinements (Corresponding_Spec (Context));
2837 end if;
2838
2839 -- Verify that all abstract states found in any package declared in
2840 -- the input declarative list have proper refinements. The check is
2841 -- performed only when the context denotes a block, entry, package,
2842 -- protected, subprogram, or task body (SPARK RM 7.2.2(3)).
2843
2844 Check_State_Refinements (Context);
2845
2846 -- Create the subprogram bodies which verify the run-time semantics
2847 -- of pragmas Default_Initial_Condition and [Type_]Invariant for all
2848 -- types within the current declarative list. This ensures that all
2849 -- assertion expressions are preanalyzed and resolved at the end of
2850 -- the declarative part. Note that the resolution happens even when
2851 -- freezing does not take place.
2852
2853 Build_Assertion_Bodies (L, Context);
2854 end if;
2855 end Analyze_Declarations;
2856
2857 -----------------------------------
2858 -- Analyze_Full_Type_Declaration --
2859 -----------------------------------
2860
2861 procedure Analyze_Full_Type_Declaration (N : Node_Id) is
2862 Def : constant Node_Id := Type_Definition (N);
2863 Def_Id : constant Entity_Id := Defining_Identifier (N);
2864 T : Entity_Id;
2865 Prev : Entity_Id;
2866
2867 Is_Remote : constant Boolean :=
2868 (Is_Remote_Types (Current_Scope)
2869 or else Is_Remote_Call_Interface (Current_Scope))
2870 and then not (In_Private_Part (Current_Scope)
2871 or else In_Package_Body (Current_Scope));
2872
2873 procedure Check_Nonoverridable_Aspects;
2874 -- Apply the rule in RM 13.1.1(18.4/4) on iterator aspects that cannot
2875 -- be overridden, and can only be confirmed on derivation.
2876
2877 procedure Check_Ops_From_Incomplete_Type;
2878 -- If there is a tagged incomplete partial view of the type, traverse
2879 -- the primitives of the incomplete view and change the type of any
2880 -- controlling formals and result to indicate the full view. The
2881 -- primitives will be added to the full type's primitive operations
2882 -- list later in Sem_Disp.Check_Operation_From_Incomplete_Type (which
2883 -- is called from Process_Incomplete_Dependents).
2884
2885 ----------------------------------
2886 -- Check_Nonoverridable_Aspects --
2887 ----------------------------------
2888
2889 procedure Check_Nonoverridable_Aspects is
2890 function Get_Aspect_Spec
2891 (Specs : List_Id;
2892 Aspect_Name : Name_Id) return Node_Id;
2893 -- Check whether a list of aspect specifications includes an entry
2894 -- for a specific aspect. The list is either that of a partial or
2895 -- a full view.
2896
2897 ---------------------
2898 -- Get_Aspect_Spec --
2899 ---------------------
2900
2901 function Get_Aspect_Spec
2902 (Specs : List_Id;
2903 Aspect_Name : Name_Id) return Node_Id
2904 is
2905 Spec : Node_Id;
2906
2907 begin
2908 Spec := First (Specs);
2909 while Present (Spec) loop
2910 if Chars (Identifier (Spec)) = Aspect_Name then
2911 return Spec;
2912 end if;
2913 Next (Spec);
2914 end loop;
2915
2916 return Empty;
2917 end Get_Aspect_Spec;
2918
2919 -- Local variables
2920
2921 Prev_Aspects : constant List_Id :=
2922 Aspect_Specifications (Parent (Def_Id));
2923 Par_Type : Entity_Id;
2924 Prev_Aspect : Node_Id;
2925
2926 -- Start of processing for Check_Nonoverridable_Aspects
2927
2928 begin
2929 -- Get parent type of derived type. Note that Prev is the entity in
2930 -- the partial declaration, but its contents are now those of full
2931 -- view, while Def_Id reflects the partial view.
2932
2933 if Is_Private_Type (Def_Id) then
2934 Par_Type := Etype (Full_View (Def_Id));
2935 else
2936 Par_Type := Etype (Def_Id);
2937 end if;
2938
2939 -- If there is an inherited Implicit_Dereference, verify that it is
2940 -- made explicit in the partial view.
2941
2942 if Has_Discriminants (Base_Type (Par_Type))
2943 and then Nkind (Parent (Prev)) = N_Full_Type_Declaration
2944 and then Present (Discriminant_Specifications (Parent (Prev)))
2945 and then Present (Get_Reference_Discriminant (Par_Type))
2946 then
2947 Prev_Aspect :=
2948 Get_Aspect_Spec (Prev_Aspects, Name_Implicit_Dereference);
2949
2950 if No (Prev_Aspect)
2951 and then Present
2952 (Discriminant_Specifications
2953 (Original_Node (Parent (Prev))))
2954 then
2955 Error_Msg_N
2956 ("type does not inherit implicit dereference", Prev);
2957
2958 else
2959 -- If one of the views has the aspect specified, verify that it
2960 -- is consistent with that of the parent.
2961
2962 declare
2963 Par_Discr : constant Entity_Id :=
2964 Get_Reference_Discriminant (Par_Type);
2965 Cur_Discr : constant Entity_Id :=
2966 Get_Reference_Discriminant (Prev);
2967
2968 begin
2969 if Corresponding_Discriminant (Cur_Discr) /= Par_Discr then
2970 Error_Msg_N ("aspect incosistent with that of parent", N);
2971 end if;
2972
2973 -- Check that specification in partial view matches the
2974 -- inherited aspect. Compare names directly because aspect
2975 -- expression may not be analyzed.
2976
2977 if Present (Prev_Aspect)
2978 and then Nkind (Expression (Prev_Aspect)) = N_Identifier
2979 and then Chars (Expression (Prev_Aspect)) /=
2980 Chars (Cur_Discr)
2981 then
2982 Error_Msg_N
2983 ("aspect incosistent with that of parent", N);
2984 end if;
2985 end;
2986 end if;
2987 end if;
2988
2989 -- TBD : other nonoverridable aspects.
2990 end Check_Nonoverridable_Aspects;
2991
2992 ------------------------------------
2993 -- Check_Ops_From_Incomplete_Type --
2994 ------------------------------------
2995
2996 procedure Check_Ops_From_Incomplete_Type is
2997 Elmt : Elmt_Id;
2998 Formal : Entity_Id;
2999 Op : Entity_Id;
3000
3001 begin
3002 if Prev /= T
3003 and then Ekind (Prev) = E_Incomplete_Type
3004 and then Is_Tagged_Type (Prev)
3005 and then Is_Tagged_Type (T)
3006 then
3007 Elmt := First_Elmt (Primitive_Operations (Prev));
3008 while Present (Elmt) loop
3009 Op := Node (Elmt);
3010
3011 Formal := First_Formal (Op);
3012 while Present (Formal) loop
3013 if Etype (Formal) = Prev then
3014 Set_Etype (Formal, T);
3015 end if;
3016
3017 Next_Formal (Formal);
3018 end loop;
3019
3020 if Etype (Op) = Prev then
3021 Set_Etype (Op, T);
3022 end if;
3023
3024 Next_Elmt (Elmt);
3025 end loop;
3026 end if;
3027 end Check_Ops_From_Incomplete_Type;
3028
3029 -- Start of processing for Analyze_Full_Type_Declaration
3030
3031 begin
3032 Prev := Find_Type_Name (N);
3033
3034 -- The full view, if present, now points to the current type. If there
3035 -- is an incomplete partial view, set a link to it, to simplify the
3036 -- retrieval of primitive operations of the type.
3037
3038 -- Ada 2005 (AI-50217): If the type was previously decorated when
3039 -- imported through a LIMITED WITH clause, it appears as incomplete
3040 -- but has no full view.
3041
3042 if Ekind (Prev) = E_Incomplete_Type
3043 and then Present (Full_View (Prev))
3044 then
3045 T := Full_View (Prev);
3046 Set_Incomplete_View (N, Parent (Prev));
3047 else
3048 T := Prev;
3049 end if;
3050
3051 Set_Is_Pure (T, Is_Pure (Current_Scope));
3052
3053 -- We set the flag Is_First_Subtype here. It is needed to set the
3054 -- corresponding flag for the Implicit class-wide-type created
3055 -- during tagged types processing.
3056
3057 Set_Is_First_Subtype (T, True);
3058
3059 -- Only composite types other than array types are allowed to have
3060 -- discriminants.
3061
3062 case Nkind (Def) is
3063
3064 -- For derived types, the rule will be checked once we've figured
3065 -- out the parent type.
3066
3067 when N_Derived_Type_Definition =>
3068 null;
3069
3070 -- For record types, discriminants are allowed, unless we are in
3071 -- SPARK.
3072
3073 when N_Record_Definition =>
3074 if Present (Discriminant_Specifications (N)) then
3075 Check_SPARK_05_Restriction
3076 ("discriminant type is not allowed",
3077 Defining_Identifier
3078 (First (Discriminant_Specifications (N))));
3079 end if;
3080
3081 when others =>
3082 if Present (Discriminant_Specifications (N)) then
3083 Error_Msg_N
3084 ("elementary or array type cannot have discriminants",
3085 Defining_Identifier
3086 (First (Discriminant_Specifications (N))));
3087 end if;
3088 end case;
3089
3090 -- Elaborate the type definition according to kind, and generate
3091 -- subsidiary (implicit) subtypes where needed. We skip this if it was
3092 -- already done (this happens during the reanalysis that follows a call
3093 -- to the high level optimizer).
3094
3095 if not Analyzed (T) then
3096 Set_Analyzed (T);
3097
3098 case Nkind (Def) is
3099 when N_Access_To_Subprogram_Definition =>
3100 Access_Subprogram_Declaration (T, Def);
3101
3102 -- If this is a remote access to subprogram, we must create the
3103 -- equivalent fat pointer type, and related subprograms.
3104
3105 if Is_Remote then
3106 Process_Remote_AST_Declaration (N);
3107 end if;
3108
3109 -- Validate categorization rule against access type declaration
3110 -- usually a violation in Pure unit, Shared_Passive unit.
3111
3112 Validate_Access_Type_Declaration (T, N);
3113
3114 when N_Access_To_Object_Definition =>
3115 Access_Type_Declaration (T, Def);
3116
3117 -- Validate categorization rule against access type declaration
3118 -- usually a violation in Pure unit, Shared_Passive unit.
3119
3120 Validate_Access_Type_Declaration (T, N);
3121
3122 -- If we are in a Remote_Call_Interface package and define a
3123 -- RACW, then calling stubs and specific stream attributes
3124 -- must be added.
3125
3126 if Is_Remote
3127 and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
3128 then
3129 Add_RACW_Features (Def_Id);
3130 end if;
3131
3132 when N_Array_Type_Definition =>
3133 Array_Type_Declaration (T, Def);
3134
3135 when N_Derived_Type_Definition =>
3136 Derived_Type_Declaration (T, N, T /= Def_Id);
3137
3138 when N_Enumeration_Type_Definition =>
3139 Enumeration_Type_Declaration (T, Def);
3140
3141 when N_Floating_Point_Definition =>
3142 Floating_Point_Type_Declaration (T, Def);
3143
3144 when N_Decimal_Fixed_Point_Definition =>
3145 Decimal_Fixed_Point_Type_Declaration (T, Def);
3146
3147 when N_Ordinary_Fixed_Point_Definition =>
3148 Ordinary_Fixed_Point_Type_Declaration (T, Def);
3149
3150 when N_Signed_Integer_Type_Definition =>
3151 Signed_Integer_Type_Declaration (T, Def);
3152
3153 when N_Modular_Type_Definition =>
3154 Modular_Type_Declaration (T, Def);
3155
3156 when N_Record_Definition =>
3157 Record_Type_Declaration (T, N, Prev);
3158
3159 -- If declaration has a parse error, nothing to elaborate.
3160
3161 when N_Error =>
3162 null;
3163
3164 when others =>
3165 raise Program_Error;
3166 end case;
3167 end if;
3168
3169 if Etype (T) = Any_Type then
3170 return;
3171 end if;
3172
3173 -- Controlled type is not allowed in SPARK
3174
3175 if Is_Visibly_Controlled (T) then
3176 Check_SPARK_05_Restriction ("controlled type is not allowed", N);
3177 end if;
3178
3179 -- Some common processing for all types
3180
3181 Set_Depends_On_Private (T, Has_Private_Component (T));
3182 Check_Ops_From_Incomplete_Type;
3183
3184 -- Both the declared entity, and its anonymous base type if one was
3185 -- created, need freeze nodes allocated.
3186
3187 declare
3188 B : constant Entity_Id := Base_Type (T);
3189
3190 begin
3191 -- In the case where the base type differs from the first subtype, we
3192 -- pre-allocate a freeze node, and set the proper link to the first
3193 -- subtype. Freeze_Entity will use this preallocated freeze node when
3194 -- it freezes the entity.
3195
3196 -- This does not apply if the base type is a generic type, whose
3197 -- declaration is independent of the current derived definition.
3198
3199 if B /= T and then not Is_Generic_Type (B) then
3200 Ensure_Freeze_Node (B);
3201 Set_First_Subtype_Link (Freeze_Node (B), T);
3202 end if;
3203
3204 -- A type that is imported through a limited_with clause cannot
3205 -- generate any code, and thus need not be frozen. However, an access
3206 -- type with an imported designated type needs a finalization list,
3207 -- which may be referenced in some other package that has non-limited
3208 -- visibility on the designated type. Thus we must create the
3209 -- finalization list at the point the access type is frozen, to
3210 -- prevent unsatisfied references at link time.
3211
3212 if not From_Limited_With (T) or else Is_Access_Type (T) then
3213 Set_Has_Delayed_Freeze (T);
3214 end if;
3215 end;
3216
3217 -- Case where T is the full declaration of some private type which has
3218 -- been swapped in Defining_Identifier (N).
3219
3220 if T /= Def_Id and then Is_Private_Type (Def_Id) then
3221 Process_Full_View (N, T, Def_Id);
3222
3223 -- Record the reference. The form of this is a little strange, since
3224 -- the full declaration has been swapped in. So the first parameter
3225 -- here represents the entity to which a reference is made which is
3226 -- the "real" entity, i.e. the one swapped in, and the second
3227 -- parameter provides the reference location.
3228
3229 -- Also, we want to kill Has_Pragma_Unreferenced temporarily here
3230 -- since we don't want a complaint about the full type being an
3231 -- unwanted reference to the private type
3232
3233 declare
3234 B : constant Boolean := Has_Pragma_Unreferenced (T);
3235 begin
3236 Set_Has_Pragma_Unreferenced (T, False);
3237 Generate_Reference (T, T, 'c');
3238 Set_Has_Pragma_Unreferenced (T, B);
3239 end;
3240
3241 Set_Completion_Referenced (Def_Id);
3242
3243 -- For completion of incomplete type, process incomplete dependents
3244 -- and always mark the full type as referenced (it is the incomplete
3245 -- type that we get for any real reference).
3246
3247 elsif Ekind (Prev) = E_Incomplete_Type then
3248 Process_Incomplete_Dependents (N, T, Prev);
3249 Generate_Reference (Prev, Def_Id, 'c');
3250 Set_Completion_Referenced (Def_Id);
3251
3252 -- If not private type or incomplete type completion, this is a real
3253 -- definition of a new entity, so record it.
3254
3255 else
3256 Generate_Definition (Def_Id);
3257 end if;
3258
3259 -- Propagate any pending access types whose finalization masters need to
3260 -- be fully initialized from the partial to the full view. Guard against
3261 -- an illegal full view that remains unanalyzed.
3262
3263 if Is_Type (Def_Id) and then Is_Incomplete_Or_Private_Type (Prev) then
3264 Set_Pending_Access_Types (Def_Id, Pending_Access_Types (Prev));
3265 end if;
3266
3267 if Chars (Scope (Def_Id)) = Name_System
3268 and then Chars (Def_Id) = Name_Address
3269 and then Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (N)))
3270 then
3271 Set_Is_Descendant_Of_Address (Def_Id);
3272 Set_Is_Descendant_Of_Address (Base_Type (Def_Id));
3273 Set_Is_Descendant_Of_Address (Prev);
3274 end if;
3275
3276 Set_Optimize_Alignment_Flags (Def_Id);
3277 Check_Eliminated (Def_Id);
3278
3279 -- If the declaration is a completion and aspects are present, apply
3280 -- them to the entity for the type which is currently the partial
3281 -- view, but which is the one that will be frozen.
3282
3283 if Has_Aspects (N) then
3284
3285 -- In most cases the partial view is a private type, and both views
3286 -- appear in different declarative parts. In the unusual case where
3287 -- the partial view is incomplete, perform the analysis on the
3288 -- full view, to prevent freezing anomalies with the corresponding
3289 -- class-wide type, which otherwise might be frozen before the
3290 -- dispatch table is built.
3291
3292 if Prev /= Def_Id
3293 and then Ekind (Prev) /= E_Incomplete_Type
3294 then
3295 Analyze_Aspect_Specifications (N, Prev);
3296
3297 -- Normal case
3298
3299 else
3300 Analyze_Aspect_Specifications (N, Def_Id);
3301 end if;
3302 end if;
3303
3304 if Is_Derived_Type (Prev)
3305 and then Def_Id /= Prev
3306 then
3307 Check_Nonoverridable_Aspects;
3308 end if;
3309 end Analyze_Full_Type_Declaration;
3310
3311 ----------------------------------
3312 -- Analyze_Incomplete_Type_Decl --
3313 ----------------------------------
3314
3315 procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
3316 F : constant Boolean := Is_Pure (Current_Scope);
3317 T : Entity_Id;
3318
3319 begin
3320 Check_SPARK_05_Restriction ("incomplete type is not allowed", N);
3321
3322 Generate_Definition (Defining_Identifier (N));
3323
3324 -- Process an incomplete declaration. The identifier must not have been
3325 -- declared already in the scope. However, an incomplete declaration may
3326 -- appear in the private part of a package, for a private type that has
3327 -- already been declared.
3328
3329 -- In this case, the discriminants (if any) must match
3330
3331 T := Find_Type_Name (N);
3332
3333 Set_Ekind (T, E_Incomplete_Type);
3334 Init_Size_Align (T);
3335 Set_Is_First_Subtype (T, True);
3336 Set_Etype (T, T);
3337
3338 -- Ada 2005 (AI-326): Minimum decoration to give support to tagged
3339 -- incomplete types.
3340
3341 if Tagged_Present (N) then
3342 Set_Is_Tagged_Type (T, True);
3343 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
3344 Make_Class_Wide_Type (T);
3345 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3346 end if;
3347
3348 Set_Stored_Constraint (T, No_Elist);
3349
3350 if Present (Discriminant_Specifications (N)) then
3351 Push_Scope (T);
3352 Process_Discriminants (N);
3353 End_Scope;
3354 end if;
3355
3356 -- If the type has discriminants, nontrivial subtypes may be declared
3357 -- before the full view of the type. The full views of those subtypes
3358 -- will be built after the full view of the type.
3359
3360 Set_Private_Dependents (T, New_Elmt_List);
3361 Set_Is_Pure (T, F);
3362 end Analyze_Incomplete_Type_Decl;
3363
3364 -----------------------------------
3365 -- Analyze_Interface_Declaration --
3366 -----------------------------------
3367
3368 procedure Analyze_Interface_Declaration (T : Entity_Id; Def : Node_Id) is
3369 CW : constant Entity_Id := Class_Wide_Type (T);
3370
3371 begin
3372 Set_Is_Tagged_Type (T);
3373 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
3374
3375 Set_Is_Limited_Record (T, Limited_Present (Def)
3376 or else Task_Present (Def)
3377 or else Protected_Present (Def)
3378 or else Synchronized_Present (Def));
3379
3380 -- Type is abstract if full declaration carries keyword, or if previous
3381 -- partial view did.
3382
3383 Set_Is_Abstract_Type (T);
3384 Set_Is_Interface (T);
3385
3386 -- Type is a limited interface if it includes the keyword limited, task,
3387 -- protected, or synchronized.
3388
3389 Set_Is_Limited_Interface
3390 (T, Limited_Present (Def)
3391 or else Protected_Present (Def)
3392 or else Synchronized_Present (Def)
3393 or else Task_Present (Def));
3394
3395 Set_Interfaces (T, New_Elmt_List);
3396 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3397
3398 -- Complete the decoration of the class-wide entity if it was already
3399 -- built (i.e. during the creation of the limited view)
3400
3401 if Present (CW) then
3402 Set_Is_Interface (CW);
3403 Set_Is_Limited_Interface (CW, Is_Limited_Interface (T));
3404 end if;
3405
3406 -- Check runtime support for synchronized interfaces
3407
3408 if (Is_Task_Interface (T)
3409 or else Is_Protected_Interface (T)
3410 or else Is_Synchronized_Interface (T))
3411 and then not RTE_Available (RE_Select_Specific_Data)
3412 then
3413 Error_Msg_CRT ("synchronized interfaces", T);
3414 end if;
3415 end Analyze_Interface_Declaration;
3416
3417 -----------------------------
3418 -- Analyze_Itype_Reference --
3419 -----------------------------
3420
3421 -- Nothing to do. This node is placed in the tree only for the benefit of
3422 -- back end processing, and has no effect on the semantic processing.
3423
3424 procedure Analyze_Itype_Reference (N : Node_Id) is
3425 begin
3426 pragma Assert (Is_Itype (Itype (N)));
3427 null;
3428 end Analyze_Itype_Reference;
3429
3430 --------------------------------
3431 -- Analyze_Number_Declaration --
3432 --------------------------------
3433
3434 procedure Analyze_Number_Declaration (N : Node_Id) is
3435 E : constant Node_Id := Expression (N);
3436 Id : constant Entity_Id := Defining_Identifier (N);
3437 Index : Interp_Index;
3438 It : Interp;
3439 T : Entity_Id;
3440
3441 begin
3442 Generate_Definition (Id);
3443 Enter_Name (Id);
3444
3445 -- This is an optimization of a common case of an integer literal
3446
3447 if Nkind (E) = N_Integer_Literal then
3448 Set_Is_Static_Expression (E, True);
3449 Set_Etype (E, Universal_Integer);
3450
3451 Set_Etype (Id, Universal_Integer);
3452 Set_Ekind (Id, E_Named_Integer);
3453 Set_Is_Frozen (Id, True);
3454 return;
3455 end if;
3456
3457 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3458
3459 -- Process expression, replacing error by integer zero, to avoid
3460 -- cascaded errors or aborts further along in the processing
3461
3462 -- Replace Error by integer zero, which seems least likely to cause
3463 -- cascaded errors.
3464
3465 if E = Error then
3466 Rewrite (E, Make_Integer_Literal (Sloc (E), Uint_0));
3467 Set_Error_Posted (E);
3468 end if;
3469
3470 Analyze (E);
3471
3472 -- Verify that the expression is static and numeric. If
3473 -- the expression is overloaded, we apply the preference
3474 -- rule that favors root numeric types.
3475
3476 if not Is_Overloaded (E) then
3477 T := Etype (E);
3478 if Has_Dynamic_Predicate_Aspect (T) then
3479 Error_Msg_N
3480 ("subtype has dynamic predicate, "
3481 & "not allowed in number declaration", N);
3482 end if;
3483
3484 else
3485 T := Any_Type;
3486
3487 Get_First_Interp (E, Index, It);
3488 while Present (It.Typ) loop
3489 if (Is_Integer_Type (It.Typ) or else Is_Real_Type (It.Typ))
3490 and then (Scope (Base_Type (It.Typ))) = Standard_Standard
3491 then
3492 if T = Any_Type then
3493 T := It.Typ;
3494
3495 elsif It.Typ = Universal_Real
3496 or else
3497 It.Typ = Universal_Integer
3498 then
3499 -- Choose universal interpretation over any other
3500
3501 T := It.Typ;
3502 exit;
3503 end if;
3504 end if;
3505
3506 Get_Next_Interp (Index, It);
3507 end loop;
3508 end if;
3509
3510 if Is_Integer_Type (T) then
3511 Resolve (E, T);
3512 Set_Etype (Id, Universal_Integer);
3513 Set_Ekind (Id, E_Named_Integer);
3514
3515 elsif Is_Real_Type (T) then
3516
3517 -- Because the real value is converted to universal_real, this is a
3518 -- legal context for a universal fixed expression.
3519
3520 if T = Universal_Fixed then
3521 declare
3522 Loc : constant Source_Ptr := Sloc (N);
3523 Conv : constant Node_Id := Make_Type_Conversion (Loc,
3524 Subtype_Mark =>
3525 New_Occurrence_Of (Universal_Real, Loc),
3526 Expression => Relocate_Node (E));
3527
3528 begin
3529 Rewrite (E, Conv);
3530 Analyze (E);
3531 end;
3532
3533 elsif T = Any_Fixed then
3534 Error_Msg_N ("illegal context for mixed mode operation", E);
3535
3536 -- Expression is of the form : universal_fixed * integer. Try to
3537 -- resolve as universal_real.
3538
3539 T := Universal_Real;
3540 Set_Etype (E, T);
3541 end if;
3542
3543 Resolve (E, T);
3544 Set_Etype (Id, Universal_Real);
3545 Set_Ekind (Id, E_Named_Real);
3546
3547 else
3548 Wrong_Type (E, Any_Numeric);
3549 Resolve (E, T);
3550
3551 Set_Etype (Id, T);
3552 Set_Ekind (Id, E_Constant);
3553 Set_Never_Set_In_Source (Id, True);
3554 Set_Is_True_Constant (Id, True);
3555 return;
3556 end if;
3557
3558 if Nkind_In (E, N_Integer_Literal, N_Real_Literal) then
3559 Set_Etype (E, Etype (Id));
3560 end if;
3561
3562 if not Is_OK_Static_Expression (E) then
3563 Flag_Non_Static_Expr
3564 ("non-static expression used in number declaration!", E);
3565 Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
3566 Set_Etype (E, Any_Type);
3567 end if;
3568
3569 Analyze_Dimension (N);
3570 end Analyze_Number_Declaration;
3571
3572 --------------------------------
3573 -- Analyze_Object_Declaration --
3574 --------------------------------
3575
3576 -- WARNING: This routine manages Ghost regions. Return statements must be
3577 -- replaced by gotos which jump to the end of the routine and restore the
3578 -- Ghost mode.
3579
3580 procedure Analyze_Object_Declaration (N : Node_Id) is
3581 Loc : constant Source_Ptr := Sloc (N);
3582 Id : constant Entity_Id := Defining_Identifier (N);
3583 Act_T : Entity_Id;
3584 T : Entity_Id;
3585
3586 E : Node_Id := Expression (N);
3587 -- E is set to Expression (N) throughout this routine. When
3588 -- Expression (N) is modified, E is changed accordingly.
3589
3590 Prev_Entity : Entity_Id := Empty;
3591
3592 function Count_Tasks (T : Entity_Id) return Uint;
3593 -- This function is called when a non-generic library level object of a
3594 -- task type is declared. Its function is to count the static number of
3595 -- tasks declared within the type (it is only called if Has_Task is set
3596 -- for T). As a side effect, if an array of tasks with non-static bounds
3597 -- or a variant record type is encountered, Check_Restriction is called
3598 -- indicating the count is unknown.
3599
3600 function Delayed_Aspect_Present return Boolean;
3601 -- If the declaration has an expression that is an aggregate, and it
3602 -- has aspects that require delayed analysis, the resolution of the
3603 -- aggregate must be deferred to the freeze point of the objet. This
3604 -- special processing was created for address clauses, but it must
3605 -- also apply to Alignment. This must be done before the aspect
3606 -- specifications are analyzed because we must handle the aggregate
3607 -- before the analysis of the object declaration is complete.
3608
3609 -- Any other relevant delayed aspects on object declarations ???
3610
3611 -----------------
3612 -- Count_Tasks --
3613 -----------------
3614
3615 function Count_Tasks (T : Entity_Id) return Uint is
3616 C : Entity_Id;
3617 X : Node_Id;
3618 V : Uint;
3619
3620 begin
3621 if Is_Task_Type (T) then
3622 return Uint_1;
3623
3624 elsif Is_Record_Type (T) then
3625 if Has_Discriminants (T) then
3626 Check_Restriction (Max_Tasks, N);
3627 return Uint_0;
3628
3629 else
3630 V := Uint_0;
3631 C := First_Component (T);
3632 while Present (C) loop
3633 V := V + Count_Tasks (Etype (C));
3634 Next_Component (C);
3635 end loop;
3636
3637 return V;
3638 end if;
3639
3640 elsif Is_Array_Type (T) then
3641 X := First_Index (T);
3642 V := Count_Tasks (Component_Type (T));
3643 while Present (X) loop
3644 C := Etype (X);
3645
3646 if not Is_OK_Static_Subtype (C) then
3647 Check_Restriction (Max_Tasks, N);
3648 return Uint_0;
3649 else
3650 V := V * (UI_Max (Uint_0,
3651 Expr_Value (Type_High_Bound (C)) -
3652 Expr_Value (Type_Low_Bound (C)) + Uint_1));
3653 end if;
3654
3655 Next_Index (X);
3656 end loop;
3657
3658 return V;
3659
3660 else
3661 return Uint_0;
3662 end if;
3663 end Count_Tasks;
3664
3665 ----------------------------
3666 -- Delayed_Aspect_Present --
3667 ----------------------------
3668
3669 function Delayed_Aspect_Present return Boolean is
3670 A : Node_Id;
3671 A_Id : Aspect_Id;
3672
3673 begin
3674 if Present (Aspect_Specifications (N)) then
3675 A := First (Aspect_Specifications (N));
3676 A_Id := Get_Aspect_Id (Chars (Identifier (A)));
3677 while Present (A) loop
3678 if A_Id = Aspect_Alignment or else A_Id = Aspect_Address then
3679 return True;
3680 end if;
3681
3682 Next (A);
3683 end loop;
3684 end if;
3685
3686 return False;
3687 end Delayed_Aspect_Present;
3688
3689 -- Local variables
3690
3691 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
3692 -- Save the Ghost mode to restore on exit
3693
3694 Related_Id : Entity_Id;
3695
3696 -- Start of processing for Analyze_Object_Declaration
3697
3698 begin
3699 -- There are three kinds of implicit types generated by an
3700 -- object declaration:
3701
3702 -- 1. Those generated by the original Object Definition
3703
3704 -- 2. Those generated by the Expression
3705
3706 -- 3. Those used to constrain the Object Definition with the
3707 -- expression constraints when the definition is unconstrained.
3708
3709 -- They must be generated in this order to avoid order of elaboration
3710 -- issues. Thus the first step (after entering the name) is to analyze
3711 -- the object definition.
3712
3713 if Constant_Present (N) then
3714 Prev_Entity := Current_Entity_In_Scope (Id);
3715
3716 if Present (Prev_Entity)
3717 and then
3718 -- If the homograph is an implicit subprogram, it is overridden
3719 -- by the current declaration.
3720
3721 ((Is_Overloadable (Prev_Entity)
3722 and then Is_Inherited_Operation (Prev_Entity))
3723
3724 -- The current object is a discriminal generated for an entry
3725 -- family index. Even though the index is a constant, in this
3726 -- particular context there is no true constant redeclaration.
3727 -- Enter_Name will handle the visibility.
3728
3729 or else
3730 (Is_Discriminal (Id)
3731 and then Ekind (Discriminal_Link (Id)) =
3732 E_Entry_Index_Parameter)
3733
3734 -- The current object is the renaming for a generic declared
3735 -- within the instance.
3736
3737 or else
3738 (Ekind (Prev_Entity) = E_Package
3739 and then Nkind (Parent (Prev_Entity)) =
3740 N_Package_Renaming_Declaration
3741 and then not Comes_From_Source (Prev_Entity)
3742 and then
3743 Is_Generic_Instance (Renamed_Entity (Prev_Entity)))
3744
3745 -- The entity may be a homonym of a private component of the
3746 -- enclosing protected object, for which we create a local
3747 -- renaming declaration. The declaration is legal, even if
3748 -- useless when it just captures that component.
3749
3750 or else
3751 (Ekind (Scope (Current_Scope)) = E_Protected_Type
3752 and then Nkind (Parent (Prev_Entity)) =
3753 N_Object_Renaming_Declaration))
3754 then
3755 Prev_Entity := Empty;
3756 end if;
3757 end if;
3758
3759 if Present (Prev_Entity) then
3760
3761 -- The object declaration is Ghost when it completes a deferred Ghost
3762 -- constant.
3763
3764 Mark_And_Set_Ghost_Completion (N, Prev_Entity);
3765
3766 Constant_Redeclaration (Id, N, T);
3767
3768 Generate_Reference (Prev_Entity, Id, 'c');
3769 Set_Completion_Referenced (Id);
3770
3771 if Error_Posted (N) then
3772
3773 -- Type mismatch or illegal redeclaration; do not analyze
3774 -- expression to avoid cascaded errors.
3775
3776 T := Find_Type_Of_Object (Object_Definition (N), N);
3777 Set_Etype (Id, T);
3778 Set_Ekind (Id, E_Variable);
3779 goto Leave;
3780 end if;
3781
3782 -- In the normal case, enter identifier at the start to catch premature
3783 -- usage in the initialization expression.
3784
3785 else
3786 Generate_Definition (Id);
3787 Enter_Name (Id);
3788
3789 Mark_Coextensions (N, Object_Definition (N));
3790
3791 T := Find_Type_Of_Object (Object_Definition (N), N);
3792
3793 if Nkind (Object_Definition (N)) = N_Access_Definition
3794 and then Present
3795 (Access_To_Subprogram_Definition (Object_Definition (N)))
3796 and then Protected_Present
3797 (Access_To_Subprogram_Definition (Object_Definition (N)))
3798 then
3799 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
3800 end if;
3801
3802 if Error_Posted (Id) then
3803 Set_Etype (Id, T);
3804 Set_Ekind (Id, E_Variable);
3805 goto Leave;
3806 end if;
3807 end if;
3808
3809 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
3810 -- out some static checks.
3811
3812 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (T) then
3813
3814 -- In case of aggregates we must also take care of the correct
3815 -- initialization of nested aggregates bug this is done at the
3816 -- point of the analysis of the aggregate (see sem_aggr.adb) ???
3817
3818 if Present (Expression (N))
3819 and then Nkind (Expression (N)) = N_Aggregate
3820 then
3821 null;
3822
3823 else
3824 declare
3825 Save_Typ : constant Entity_Id := Etype (Id);
3826 begin
3827 Set_Etype (Id, T); -- Temp. decoration for static checks
3828 Null_Exclusion_Static_Checks (N);
3829 Set_Etype (Id, Save_Typ);
3830 end;
3831 end if;
3832 end if;
3833
3834 -- Object is marked pure if it is in a pure scope
3835
3836 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3837
3838 -- If deferred constant, make sure context is appropriate. We detect
3839 -- a deferred constant as a constant declaration with no expression.
3840 -- A deferred constant can appear in a package body if its completion
3841 -- is by means of an interface pragma.
3842
3843 if Constant_Present (N) and then No (E) then
3844
3845 -- A deferred constant may appear in the declarative part of the
3846 -- following constructs:
3847
3848 -- blocks
3849 -- entry bodies
3850 -- extended return statements
3851 -- package specs
3852 -- package bodies
3853 -- subprogram bodies
3854 -- task bodies
3855
3856 -- When declared inside a package spec, a deferred constant must be
3857 -- completed by a full constant declaration or pragma Import. In all
3858 -- other cases, the only proper completion is pragma Import. Extended
3859 -- return statements are flagged as invalid contexts because they do
3860 -- not have a declarative part and so cannot accommodate the pragma.
3861
3862 if Ekind (Current_Scope) = E_Return_Statement then
3863 Error_Msg_N
3864 ("invalid context for deferred constant declaration (RM 7.4)",
3865 N);
3866 Error_Msg_N
3867 ("\declaration requires an initialization expression",
3868 N);
3869 Set_Constant_Present (N, False);
3870
3871 -- In Ada 83, deferred constant must be of private type
3872
3873 elsif not Is_Private_Type (T) then
3874 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
3875 Error_Msg_N
3876 ("(Ada 83) deferred constant must be private type", N);
3877 end if;
3878 end if;
3879
3880 -- If not a deferred constant, then the object declaration freezes
3881 -- its type, unless the object is of an anonymous type and has delayed
3882 -- aspects. In that case the type is frozen when the object itself is.
3883
3884 else
3885 Check_Fully_Declared (T, N);
3886
3887 if Has_Delayed_Aspects (Id)
3888 and then Is_Array_Type (T)
3889 and then Is_Itype (T)
3890 then
3891 Set_Has_Delayed_Freeze (T);
3892 else
3893 Freeze_Before (N, T);
3894 end if;
3895 end if;
3896
3897 -- If the object was created by a constrained array definition, then
3898 -- set the link in both the anonymous base type and anonymous subtype
3899 -- that are built to represent the array type to point to the object.
3900
3901 if Nkind (Object_Definition (Declaration_Node (Id))) =
3902 N_Constrained_Array_Definition
3903 then
3904 Set_Related_Array_Object (T, Id);
3905 Set_Related_Array_Object (Base_Type (T), Id);
3906 end if;
3907
3908 -- Special checks for protected objects not at library level
3909
3910 if Has_Protected (T) and then not Is_Library_Level_Entity (Id) then
3911 Check_Restriction (No_Local_Protected_Objects, Id);
3912
3913 -- Protected objects with interrupt handlers must be at library level
3914
3915 -- Ada 2005: This test is not needed (and the corresponding clause
3916 -- in the RM is removed) because accessibility checks are sufficient
3917 -- to make handlers not at the library level illegal.
3918
3919 -- AI05-0303: The AI is in fact a binding interpretation, and thus
3920 -- applies to the '95 version of the language as well.
3921
3922 if Is_Protected_Type (T)
3923 and then Has_Interrupt_Handler (T)
3924 and then Ada_Version < Ada_95
3925 then
3926 Error_Msg_N
3927 ("interrupt object can only be declared at library level", Id);
3928 end if;
3929 end if;
3930
3931 -- Check for violation of No_Local_Timing_Events
3932
3933 if Has_Timing_Event (T) and then not Is_Library_Level_Entity (Id) then
3934 Check_Restriction (No_Local_Timing_Events, Id);
3935 end if;
3936
3937 -- The actual subtype of the object is the nominal subtype, unless
3938 -- the nominal one is unconstrained and obtained from the expression.
3939
3940 Act_T := T;
3941
3942 -- These checks should be performed before the initialization expression
3943 -- is considered, so that the Object_Definition node is still the same
3944 -- as in source code.
3945
3946 -- In SPARK, the nominal subtype is always given by a subtype mark
3947 -- and must not be unconstrained. (The only exception to this is the
3948 -- acceptance of declarations of constants of type String.)
3949
3950 if not Nkind_In (Object_Definition (N), N_Expanded_Name, N_Identifier)
3951 then
3952 Check_SPARK_05_Restriction
3953 ("subtype mark required", Object_Definition (N));
3954
3955 elsif Is_Array_Type (T)
3956 and then not Is_Constrained (T)
3957 and then T /= Standard_String
3958 then
3959 Check_SPARK_05_Restriction
3960 ("subtype mark of constrained type expected",
3961 Object_Definition (N));
3962 end if;
3963
3964 -- There are no aliased objects in SPARK
3965
3966 if Aliased_Present (N) then
3967 Check_SPARK_05_Restriction ("aliased object is not allowed", N);
3968 end if;
3969
3970 -- Process initialization expression if present and not in error
3971
3972 if Present (E) and then E /= Error then
3973
3974 -- Generate an error in case of CPP class-wide object initialization.
3975 -- Required because otherwise the expansion of the class-wide
3976 -- assignment would try to use 'size to initialize the object
3977 -- (primitive that is not available in CPP tagged types).
3978
3979 if Is_Class_Wide_Type (Act_T)
3980 and then
3981 (Is_CPP_Class (Root_Type (Etype (Act_T)))
3982 or else
3983 (Present (Full_View (Root_Type (Etype (Act_T))))
3984 and then
3985 Is_CPP_Class (Full_View (Root_Type (Etype (Act_T))))))
3986 then
3987 Error_Msg_N
3988 ("predefined assignment not available for 'C'P'P tagged types",
3989 E);
3990 end if;
3991
3992 Mark_Coextensions (N, E);
3993 Analyze (E);
3994
3995 -- In case of errors detected in the analysis of the expression,
3996 -- decorate it with the expected type to avoid cascaded errors
3997
3998 if No (Etype (E)) then
3999 Set_Etype (E, T);
4000 end if;
4001
4002 -- If an initialization expression is present, then we set the
4003 -- Is_True_Constant flag. It will be reset if this is a variable
4004 -- and it is indeed modified.
4005
4006 Set_Is_True_Constant (Id, True);
4007
4008 -- If we are analyzing a constant declaration, set its completion
4009 -- flag after analyzing and resolving the expression.
4010
4011 if Constant_Present (N) then
4012 Set_Has_Completion (Id);
4013 end if;
4014
4015 -- Set type and resolve (type may be overridden later on). Note:
4016 -- Ekind (Id) must still be E_Void at this point so that incorrect
4017 -- early usage within E is properly diagnosed.
4018
4019 Set_Etype (Id, T);
4020
4021 -- If the expression is an aggregate we must look ahead to detect
4022 -- the possible presence of an address clause, and defer resolution
4023 -- and expansion of the aggregate to the freeze point of the entity.
4024
4025 -- This is not always legal because the aggregate may contain other
4026 -- references that need freezing, e.g. references to other entities
4027 -- with address clauses. In any case, when compiling with -gnatI the
4028 -- presence of the address clause must be ignored.
4029
4030 if Comes_From_Source (N)
4031 and then Expander_Active
4032 and then Nkind (E) = N_Aggregate
4033 and then
4034 ((Present (Following_Address_Clause (N))
4035 and then not Ignore_Rep_Clauses)
4036 or else Delayed_Aspect_Present)
4037 then
4038 Set_Etype (E, T);
4039
4040 else
4041 Resolve (E, T);
4042 end if;
4043
4044 -- No further action needed if E is a call to an inlined function
4045 -- which returns an unconstrained type and it has been expanded into
4046 -- a procedure call. In that case N has been replaced by an object
4047 -- declaration without initializing expression and it has been
4048 -- analyzed (see Expand_Inlined_Call).
4049
4050 if Back_End_Inlining
4051 and then Expander_Active
4052 and then Nkind (E) = N_Function_Call
4053 and then Nkind (Name (E)) in N_Has_Entity
4054 and then Is_Inlined (Entity (Name (E)))
4055 and then not Is_Constrained (Etype (E))
4056 and then Analyzed (N)
4057 and then No (Expression (N))
4058 then
4059 goto Leave;
4060 end if;
4061
4062 -- If E is null and has been replaced by an N_Raise_Constraint_Error
4063 -- node (which was marked already-analyzed), we need to set the type
4064 -- to something other than Any_Access in order to keep gigi happy.
4065
4066 if Etype (E) = Any_Access then
4067 Set_Etype (E, T);
4068 end if;
4069
4070 -- If the object is an access to variable, the initialization
4071 -- expression cannot be an access to constant.
4072
4073 if Is_Access_Type (T)
4074 and then not Is_Access_Constant (T)
4075 and then Is_Access_Type (Etype (E))
4076 and then Is_Access_Constant (Etype (E))
4077 then
4078 Error_Msg_N
4079 ("access to variable cannot be initialized with an "
4080 & "access-to-constant expression", E);
4081 end if;
4082
4083 if not Assignment_OK (N) then
4084 Check_Initialization (T, E);
4085 end if;
4086
4087 Check_Unset_Reference (E);
4088
4089 -- If this is a variable, then set current value. If this is a
4090 -- declared constant of a scalar type with a static expression,
4091 -- indicate that it is always valid.
4092
4093 if not Constant_Present (N) then
4094 if Compile_Time_Known_Value (E) then
4095 Set_Current_Value (Id, E);
4096 end if;
4097
4098 elsif Is_Scalar_Type (T) and then Is_OK_Static_Expression (E) then
4099 Set_Is_Known_Valid (Id);
4100 end if;
4101
4102 -- Deal with setting of null flags
4103
4104 if Is_Access_Type (T) then
4105 if Known_Non_Null (E) then
4106 Set_Is_Known_Non_Null (Id, True);
4107 elsif Known_Null (E) and then not Can_Never_Be_Null (Id) then
4108 Set_Is_Known_Null (Id, True);
4109 end if;
4110 end if;
4111
4112 -- Check incorrect use of dynamically tagged expressions
4113
4114 if Is_Tagged_Type (T) then
4115 Check_Dynamically_Tagged_Expression
4116 (Expr => E,
4117 Typ => T,
4118 Related_Nod => N);
4119 end if;
4120
4121 Apply_Scalar_Range_Check (E, T);
4122 Apply_Static_Length_Check (E, T);
4123
4124 if Nkind (Original_Node (N)) = N_Object_Declaration
4125 and then Comes_From_Source (Original_Node (N))
4126
4127 -- Only call test if needed
4128
4129 and then Restriction_Check_Required (SPARK_05)
4130 and then not Is_SPARK_05_Initialization_Expr (Original_Node (E))
4131 then
4132 Check_SPARK_05_Restriction
4133 ("initialization expression is not appropriate", E);
4134 end if;
4135
4136 -- A formal parameter of a specific tagged type whose related
4137 -- subprogram is subject to pragma Extensions_Visible with value
4138 -- "False" cannot be implicitly converted to a class-wide type by
4139 -- means of an initialization expression (SPARK RM 6.1.7(3)). Do
4140 -- not consider internally generated expressions.
4141
4142 if Is_Class_Wide_Type (T)
4143 and then Comes_From_Source (E)
4144 and then Is_EVF_Expression (E)
4145 then
4146 Error_Msg_N
4147 ("formal parameter cannot be implicitly converted to "
4148 & "class-wide type when Extensions_Visible is False", E);
4149 end if;
4150 end if;
4151
4152 -- If the No_Streams restriction is set, check that the type of the
4153 -- object is not, and does not contain, any subtype derived from
4154 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
4155 -- Has_Stream just for efficiency reasons. There is no point in
4156 -- spending time on a Has_Stream check if the restriction is not set.
4157
4158 if Restriction_Check_Required (No_Streams) then
4159 if Has_Stream (T) then
4160 Check_Restriction (No_Streams, N);
4161 end if;
4162 end if;
4163
4164 -- Deal with predicate check before we start to do major rewriting. It
4165 -- is OK to initialize and then check the initialized value, since the
4166 -- object goes out of scope if we get a predicate failure. Note that we
4167 -- do this in the analyzer and not the expander because the analyzer
4168 -- does some substantial rewriting in some cases.
4169
4170 -- We need a predicate check if the type has predicates that are not
4171 -- ignored, and if either there is an initializing expression, or for
4172 -- default initialization when we have at least one case of an explicit
4173 -- default initial value and then this is not an internal declaration
4174 -- whose initialization comes later (as for an aggregate expansion).
4175
4176 if not Suppress_Assignment_Checks (N)
4177 and then Present (Predicate_Function (T))
4178 and then not Predicates_Ignored (T)
4179 and then not No_Initialization (N)
4180 and then
4181 (Present (E)
4182 or else
4183 Is_Partially_Initialized_Type (T, Include_Implicit => False))
4184 then
4185 -- If the type has a static predicate and the expression is known at
4186 -- compile time, see if the expression satisfies the predicate.
4187
4188 if Present (E) then
4189 Check_Expression_Against_Static_Predicate (E, T);
4190 end if;
4191
4192 -- If the type is a null record and there is no explicit initial
4193 -- expression, no predicate check applies.
4194
4195 if No (E) and then Is_Null_Record_Type (T) then
4196 null;
4197
4198 -- Do not generate a predicate check if the initialization expression
4199 -- is a type conversion because the conversion has been subjected to
4200 -- the same check. This is a small optimization which avoid redundant
4201 -- checks.
4202
4203 elsif Present (E) and then Nkind (E) = N_Type_Conversion then
4204 null;
4205
4206 else
4207 Insert_After (N,
4208 Make_Predicate_Check (T, New_Occurrence_Of (Id, Loc)));
4209 end if;
4210 end if;
4211
4212 -- Case of unconstrained type
4213
4214 if not Is_Definite_Subtype (T) then
4215
4216 -- In SPARK, a declaration of unconstrained type is allowed
4217 -- only for constants of type string.
4218
4219 if Is_String_Type (T) and then not Constant_Present (N) then
4220 Check_SPARK_05_Restriction
4221 ("declaration of object of unconstrained type not allowed", N);
4222 end if;
4223
4224 -- Nothing to do in deferred constant case
4225
4226 if Constant_Present (N) and then No (E) then
4227 null;
4228
4229 -- Case of no initialization present
4230
4231 elsif No (E) then
4232 if No_Initialization (N) then
4233 null;
4234
4235 elsif Is_Class_Wide_Type (T) then
4236 Error_Msg_N
4237 ("initialization required in class-wide declaration ", N);
4238
4239 else
4240 Error_Msg_N
4241 ("unconstrained subtype not allowed (need initialization)",
4242 Object_Definition (N));
4243
4244 if Is_Record_Type (T) and then Has_Discriminants (T) then
4245 Error_Msg_N
4246 ("\provide initial value or explicit discriminant values",
4247 Object_Definition (N));
4248
4249 Error_Msg_NE
4250 ("\or give default discriminant values for type&",
4251 Object_Definition (N), T);
4252
4253 elsif Is_Array_Type (T) then
4254 Error_Msg_N
4255 ("\provide initial value or explicit array bounds",
4256 Object_Definition (N));
4257 end if;
4258 end if;
4259
4260 -- Case of initialization present but in error. Set initial
4261 -- expression as absent (but do not make above complaints)
4262
4263 elsif E = Error then
4264 Set_Expression (N, Empty);
4265 E := Empty;
4266
4267 -- Case of initialization present
4268
4269 else
4270 -- Check restrictions in Ada 83
4271
4272 if not Constant_Present (N) then
4273
4274 -- Unconstrained variables not allowed in Ada 83 mode
4275
4276 if Ada_Version = Ada_83
4277 and then Comes_From_Source (Object_Definition (N))
4278 then
4279 Error_Msg_N
4280 ("(Ada 83) unconstrained variable not allowed",
4281 Object_Definition (N));
4282 end if;
4283 end if;
4284
4285 -- Now we constrain the variable from the initializing expression
4286
4287 -- If the expression is an aggregate, it has been expanded into
4288 -- individual assignments. Retrieve the actual type from the
4289 -- expanded construct.
4290
4291 if Is_Array_Type (T)
4292 and then No_Initialization (N)
4293 and then Nkind (Original_Node (E)) = N_Aggregate
4294 then
4295 Act_T := Etype (E);
4296
4297 -- In case of class-wide interface object declarations we delay
4298 -- the generation of the equivalent record type declarations until
4299 -- its expansion because there are cases in they are not required.
4300
4301 elsif Is_Interface (T) then
4302 null;
4303
4304 -- In GNATprove mode, Expand_Subtype_From_Expr does nothing. Thus,
4305 -- we should prevent the generation of another Itype with the
4306 -- same name as the one already generated, or we end up with
4307 -- two identical types in GNATprove.
4308
4309 elsif GNATprove_Mode then
4310 null;
4311
4312 -- If the type is an unchecked union, no subtype can be built from
4313 -- the expression. Rewrite declaration as a renaming, which the
4314 -- back-end can handle properly. This is a rather unusual case,
4315 -- because most unchecked_union declarations have default values
4316 -- for discriminants and are thus not indefinite.
4317
4318 elsif Is_Unchecked_Union (T) then
4319 if Constant_Present (N) or else Nkind (E) = N_Function_Call then
4320 Set_Ekind (Id, E_Constant);
4321 else
4322 Set_Ekind (Id, E_Variable);
4323 end if;
4324
4325 Rewrite (N,
4326 Make_Object_Renaming_Declaration (Loc,
4327 Defining_Identifier => Id,
4328 Subtype_Mark => New_Occurrence_Of (T, Loc),
4329 Name => E));
4330
4331 Set_Renamed_Object (Id, E);
4332 Freeze_Before (N, T);
4333 Set_Is_Frozen (Id);
4334 goto Leave;
4335
4336 else
4337 -- Ensure that the generated subtype has a unique external name
4338 -- when the related object is public. This guarantees that the
4339 -- subtype and its bounds will not be affected by switches or
4340 -- pragmas that may offset the internal counter due to extra
4341 -- generated code.
4342
4343 if Is_Public (Id) then
4344 Related_Id := Id;
4345 else
4346 Related_Id := Empty;
4347 end if;
4348
4349 Expand_Subtype_From_Expr
4350 (N => N,
4351 Unc_Type => T,
4352 Subtype_Indic => Object_Definition (N),
4353 Exp => E,
4354 Related_Id => Related_Id);
4355
4356 Act_T := Find_Type_Of_Object (Object_Definition (N), N);
4357 end if;
4358
4359 Set_Is_Constr_Subt_For_U_Nominal (Act_T);
4360
4361 if Aliased_Present (N) then
4362 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4363 end if;
4364
4365 Freeze_Before (N, Act_T);
4366 Freeze_Before (N, T);
4367 end if;
4368
4369 elsif Is_Array_Type (T)
4370 and then No_Initialization (N)
4371 and then (Nkind (Original_Node (E)) = N_Aggregate
4372 or else (Nkind (Original_Node (E)) = N_Qualified_Expression
4373 and then Nkind (Original_Node (Expression
4374 (Original_Node (E)))) = N_Aggregate))
4375 then
4376 if not Is_Entity_Name (Object_Definition (N)) then
4377 Act_T := Etype (E);
4378 Check_Compile_Time_Size (Act_T);
4379
4380 if Aliased_Present (N) then
4381 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4382 end if;
4383 end if;
4384
4385 -- When the given object definition and the aggregate are specified
4386 -- independently, and their lengths might differ do a length check.
4387 -- This cannot happen if the aggregate is of the form (others =>...)
4388
4389 if not Is_Constrained (T) then
4390 null;
4391
4392 elsif Nkind (E) = N_Raise_Constraint_Error then
4393
4394 -- Aggregate is statically illegal. Place back in declaration
4395
4396 Set_Expression (N, E);
4397 Set_No_Initialization (N, False);
4398
4399 elsif T = Etype (E) then
4400 null;
4401
4402 elsif Nkind (E) = N_Aggregate
4403 and then Present (Component_Associations (E))
4404 and then Present (Choice_List (First (Component_Associations (E))))
4405 and then
4406 Nkind (First (Choice_List (First (Component_Associations (E))))) =
4407 N_Others_Choice
4408 then
4409 null;
4410
4411 else
4412 Apply_Length_Check (E, T);
4413 end if;
4414
4415 -- If the type is limited unconstrained with defaulted discriminants and
4416 -- there is no expression, then the object is constrained by the
4417 -- defaults, so it is worthwhile building the corresponding subtype.
4418
4419 elsif (Is_Limited_Record (T) or else Is_Concurrent_Type (T))
4420 and then not Is_Constrained (T)
4421 and then Has_Discriminants (T)
4422 then
4423 if No (E) then
4424 Act_T := Build_Default_Subtype (T, N);
4425 else
4426 -- Ada 2005: A limited object may be initialized by means of an
4427 -- aggregate. If the type has default discriminants it has an
4428 -- unconstrained nominal type, Its actual subtype will be obtained
4429 -- from the aggregate, and not from the default discriminants.
4430
4431 Act_T := Etype (E);
4432 end if;
4433
4434 Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
4435
4436 elsif Nkind (E) = N_Function_Call
4437 and then Constant_Present (N)
4438 and then Has_Unconstrained_Elements (Etype (E))
4439 then
4440 -- The back-end has problems with constants of a discriminated type
4441 -- with defaults, if the initial value is a function call. We
4442 -- generate an intermediate temporary that will receive a reference
4443 -- to the result of the call. The initialization expression then
4444 -- becomes a dereference of that temporary.
4445
4446 Remove_Side_Effects (E);
4447
4448 -- If this is a constant declaration of an unconstrained type and
4449 -- the initialization is an aggregate, we can use the subtype of the
4450 -- aggregate for the declared entity because it is immutable.
4451
4452 elsif not Is_Constrained (T)
4453 and then Has_Discriminants (T)
4454 and then Constant_Present (N)
4455 and then not Has_Unchecked_Union (T)
4456 and then Nkind (E) = N_Aggregate
4457 then
4458 Act_T := Etype (E);
4459 end if;
4460
4461 -- Check No_Wide_Characters restriction
4462
4463 Check_Wide_Character_Restriction (T, Object_Definition (N));
4464
4465 -- Indicate this is not set in source. Certainly true for constants, and
4466 -- true for variables so far (will be reset for a variable if and when
4467 -- we encounter a modification in the source).
4468
4469 Set_Never_Set_In_Source (Id);
4470
4471 -- Now establish the proper kind and type of the object
4472
4473 if Constant_Present (N) then
4474 Set_Ekind (Id, E_Constant);
4475 Set_Is_True_Constant (Id);
4476
4477 else
4478 Set_Ekind (Id, E_Variable);
4479
4480 -- A variable is set as shared passive if it appears in a shared
4481 -- passive package, and is at the outer level. This is not done for
4482 -- entities generated during expansion, because those are always
4483 -- manipulated locally.
4484
4485 if Is_Shared_Passive (Current_Scope)
4486 and then Is_Library_Level_Entity (Id)
4487 and then Comes_From_Source (Id)
4488 then
4489 Set_Is_Shared_Passive (Id);
4490 Check_Shared_Var (Id, T, N);
4491 end if;
4492
4493 -- Set Has_Initial_Value if initializing expression present. Note
4494 -- that if there is no initializing expression, we leave the state
4495 -- of this flag unchanged (usually it will be False, but notably in
4496 -- the case of exception choice variables, it will already be true).
4497
4498 if Present (E) then
4499 Set_Has_Initial_Value (Id);
4500 end if;
4501 end if;
4502
4503 -- Initialize alignment and size and capture alignment setting
4504
4505 Init_Alignment (Id);
4506 Init_Esize (Id);
4507 Set_Optimize_Alignment_Flags (Id);
4508
4509 -- Deal with aliased case
4510
4511 if Aliased_Present (N) then
4512 Set_Is_Aliased (Id);
4513
4514 -- If the object is aliased and the type is unconstrained with
4515 -- defaulted discriminants and there is no expression, then the
4516 -- object is constrained by the defaults, so it is worthwhile
4517 -- building the corresponding subtype.
4518
4519 -- Ada 2005 (AI-363): If the aliased object is discriminated and
4520 -- unconstrained, then only establish an actual subtype if the
4521 -- nominal subtype is indefinite. In definite cases the object is
4522 -- unconstrained in Ada 2005.
4523
4524 if No (E)
4525 and then Is_Record_Type (T)
4526 and then not Is_Constrained (T)
4527 and then Has_Discriminants (T)
4528 and then (Ada_Version < Ada_2005
4529 or else not Is_Definite_Subtype (T))
4530 then
4531 Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
4532 end if;
4533 end if;
4534
4535 -- Now we can set the type of the object
4536
4537 Set_Etype (Id, Act_T);
4538
4539 -- Non-constant object is marked to be treated as volatile if type is
4540 -- volatile and we clear the Current_Value setting that may have been
4541 -- set above. Doing so for constants isn't required and might interfere
4542 -- with possible uses of the object as a static expression in contexts
4543 -- incompatible with volatility (e.g. as a case-statement alternative).
4544
4545 if Ekind (Id) /= E_Constant and then Treat_As_Volatile (Etype (Id)) then
4546 Set_Treat_As_Volatile (Id);
4547 Set_Current_Value (Id, Empty);
4548 end if;
4549
4550 -- Deal with controlled types
4551
4552 if Has_Controlled_Component (Etype (Id))
4553 or else Is_Controlled (Etype (Id))
4554 then
4555 if not Is_Library_Level_Entity (Id) then
4556 Check_Restriction (No_Nested_Finalization, N);
4557 else
4558 Validate_Controlled_Object (Id);
4559 end if;
4560 end if;
4561
4562 if Has_Task (Etype (Id)) then
4563 Check_Restriction (No_Tasking, N);
4564
4565 -- Deal with counting max tasks
4566
4567 -- Nothing to do if inside a generic
4568
4569 if Inside_A_Generic then
4570 null;
4571
4572 -- If library level entity, then count tasks
4573
4574 elsif Is_Library_Level_Entity (Id) then
4575 Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
4576
4577 -- If not library level entity, then indicate we don't know max
4578 -- tasks and also check task hierarchy restriction and blocking
4579 -- operation (since starting a task is definitely blocking).
4580
4581 else
4582 Check_Restriction (Max_Tasks, N);
4583 Check_Restriction (No_Task_Hierarchy, N);
4584 Check_Potentially_Blocking_Operation (N);
4585 end if;
4586
4587 -- A rather specialized test. If we see two tasks being declared
4588 -- of the same type in the same object declaration, and the task
4589 -- has an entry with an address clause, we know that program error
4590 -- will be raised at run time since we can't have two tasks with
4591 -- entries at the same address.
4592
4593 if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
4594 declare
4595 E : Entity_Id;
4596
4597 begin
4598 E := First_Entity (Etype (Id));
4599 while Present (E) loop
4600 if Ekind (E) = E_Entry
4601 and then Present (Get_Attribute_Definition_Clause
4602 (E, Attribute_Address))
4603 then
4604 Error_Msg_Warn := SPARK_Mode /= On;
4605 Error_Msg_N
4606 ("more than one task with same entry address<<", N);
4607 Error_Msg_N ("\Program_Error [<<", N);
4608 Insert_Action (N,
4609 Make_Raise_Program_Error (Loc,
4610 Reason => PE_Duplicated_Entry_Address));
4611 exit;
4612 end if;
4613
4614 Next_Entity (E);
4615 end loop;
4616 end;
4617 end if;
4618 end if;
4619
4620 -- Some simple constant-propagation: if the expression is a constant
4621 -- string initialized with a literal, share the literal. This avoids
4622 -- a run-time copy.
4623
4624 if Present (E)
4625 and then Is_Entity_Name (E)
4626 and then Ekind (Entity (E)) = E_Constant
4627 and then Base_Type (Etype (E)) = Standard_String
4628 then
4629 declare
4630 Val : constant Node_Id := Constant_Value (Entity (E));
4631 begin
4632 if Present (Val) and then Nkind (Val) = N_String_Literal then
4633 Rewrite (E, New_Copy (Val));
4634 end if;
4635 end;
4636 end if;
4637
4638 -- Another optimization: if the nominal subtype is unconstrained and
4639 -- the expression is a function call that returns an unconstrained
4640 -- type, rewrite the declaration as a renaming of the result of the
4641 -- call. The exceptions below are cases where the copy is expected,
4642 -- either by the back end (Aliased case) or by the semantics, as for
4643 -- initializing controlled types or copying tags for class-wide types.
4644
4645 if Present (E)
4646 and then Nkind (E) = N_Explicit_Dereference
4647 and then Nkind (Original_Node (E)) = N_Function_Call
4648 and then not Is_Library_Level_Entity (Id)
4649 and then not Is_Constrained (Underlying_Type (T))
4650 and then not Is_Aliased (Id)
4651 and then not Is_Class_Wide_Type (T)
4652 and then not Is_Controlled_Active (T)
4653 and then not Has_Controlled_Component (Base_Type (T))
4654 and then Expander_Active
4655 then
4656 Rewrite (N,
4657 Make_Object_Renaming_Declaration (Loc,
4658 Defining_Identifier => Id,
4659 Access_Definition => Empty,
4660 Subtype_Mark => New_Occurrence_Of
4661 (Base_Type (Etype (Id)), Loc),
4662 Name => E));
4663
4664 Set_Renamed_Object (Id, E);
4665
4666 -- Force generation of debugging information for the constant and for
4667 -- the renamed function call.
4668
4669 Set_Debug_Info_Needed (Id);
4670 Set_Debug_Info_Needed (Entity (Prefix (E)));
4671 end if;
4672
4673 if Present (Prev_Entity)
4674 and then Is_Frozen (Prev_Entity)
4675 and then not Error_Posted (Id)
4676 then
4677 Error_Msg_N ("full constant declaration appears too late", N);
4678 end if;
4679
4680 Check_Eliminated (Id);
4681
4682 -- Deal with setting In_Private_Part flag if in private part
4683
4684 if Ekind (Scope (Id)) = E_Package
4685 and then In_Private_Part (Scope (Id))
4686 then
4687 Set_In_Private_Part (Id);
4688 end if;
4689
4690 <<Leave>>
4691 -- Initialize the refined state of a variable here because this is a
4692 -- common destination for legal and illegal object declarations.
4693
4694 if Ekind (Id) = E_Variable then
4695 Set_Encapsulating_State (Id, Empty);
4696 end if;
4697
4698 if Has_Aspects (N) then
4699 Analyze_Aspect_Specifications (N, Id);
4700 end if;
4701
4702 Analyze_Dimension (N);
4703
4704 -- Verify whether the object declaration introduces an illegal hidden
4705 -- state within a package subject to a null abstract state.
4706
4707 if Ekind (Id) = E_Variable then
4708 Check_No_Hidden_State (Id);
4709 end if;
4710
4711 Restore_Ghost_Mode (Saved_GM);
4712 end Analyze_Object_Declaration;
4713
4714 ---------------------------
4715 -- Analyze_Others_Choice --
4716 ---------------------------
4717
4718 -- Nothing to do for the others choice node itself, the semantic analysis
4719 -- of the others choice will occur as part of the processing of the parent
4720
4721 procedure Analyze_Others_Choice (N : Node_Id) is
4722 pragma Warnings (Off, N);
4723 begin
4724 null;
4725 end Analyze_Others_Choice;
4726
4727 -------------------------------------------
4728 -- Analyze_Private_Extension_Declaration --
4729 -------------------------------------------
4730
4731 procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
4732 Indic : constant Node_Id := Subtype_Indication (N);
4733 T : constant Entity_Id := Defining_Identifier (N);
4734 Iface : Entity_Id;
4735 Iface_Elmt : Elmt_Id;
4736 Parent_Base : Entity_Id;
4737 Parent_Type : Entity_Id;
4738
4739 begin
4740 -- Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
4741
4742 if Is_Non_Empty_List (Interface_List (N)) then
4743 declare
4744 Intf : Node_Id;
4745 T : Entity_Id;
4746
4747 begin
4748 Intf := First (Interface_List (N));
4749 while Present (Intf) loop
4750 T := Find_Type_Of_Subtype_Indic (Intf);
4751
4752 Diagnose_Interface (Intf, T);
4753 Next (Intf);
4754 end loop;
4755 end;
4756 end if;
4757
4758 Generate_Definition (T);
4759
4760 -- For other than Ada 2012, just enter the name in the current scope
4761
4762 if Ada_Version < Ada_2012 then
4763 Enter_Name (T);
4764
4765 -- Ada 2012 (AI05-0162): Enter the name in the current scope handling
4766 -- case of private type that completes an incomplete type.
4767
4768 else
4769 declare
4770 Prev : Entity_Id;
4771
4772 begin
4773 Prev := Find_Type_Name (N);
4774
4775 pragma Assert (Prev = T
4776 or else (Ekind (Prev) = E_Incomplete_Type
4777 and then Present (Full_View (Prev))
4778 and then Full_View (Prev) = T));
4779 end;
4780 end if;
4781
4782 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
4783 Parent_Base := Base_Type (Parent_Type);
4784
4785 if Parent_Type = Any_Type or else Etype (Parent_Type) = Any_Type then
4786 Set_Ekind (T, Ekind (Parent_Type));
4787 Set_Etype (T, Any_Type);
4788 goto Leave;
4789
4790 elsif not Is_Tagged_Type (Parent_Type) then
4791 Error_Msg_N
4792 ("parent of type extension must be a tagged type ", Indic);
4793 goto Leave;
4794
4795 elsif Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
4796 Error_Msg_N ("premature derivation of incomplete type", Indic);
4797 goto Leave;
4798
4799 elsif Is_Concurrent_Type (Parent_Type) then
4800 Error_Msg_N
4801 ("parent type of a private extension cannot be a synchronized "
4802 & "tagged type (RM 3.9.1 (3/1))", N);
4803
4804 Set_Etype (T, Any_Type);
4805 Set_Ekind (T, E_Limited_Private_Type);
4806 Set_Private_Dependents (T, New_Elmt_List);
4807 Set_Error_Posted (T);
4808 goto Leave;
4809 end if;
4810
4811 -- Perhaps the parent type should be changed to the class-wide type's
4812 -- specific type in this case to prevent cascading errors ???
4813
4814 if Is_Class_Wide_Type (Parent_Type) then
4815 Error_Msg_N
4816 ("parent of type extension must not be a class-wide type", Indic);
4817 goto Leave;
4818 end if;
4819
4820 if (not Is_Package_Or_Generic_Package (Current_Scope)
4821 and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
4822 or else In_Private_Part (Current_Scope)
4823 then
4824 Error_Msg_N ("invalid context for private extension", N);
4825 end if;
4826
4827 -- Set common attributes
4828
4829 Set_Is_Pure (T, Is_Pure (Current_Scope));
4830 Set_Scope (T, Current_Scope);
4831 Set_Ekind (T, E_Record_Type_With_Private);
4832 Init_Size_Align (T);
4833 Set_Default_SSO (T);
4834
4835 Set_Etype (T, Parent_Base);
4836 Propagate_Concurrent_Flags (T, Parent_Base);
4837
4838 Set_Convention (T, Convention (Parent_Type));
4839 Set_First_Rep_Item (T, First_Rep_Item (Parent_Type));
4840 Set_Is_First_Subtype (T);
4841 Make_Class_Wide_Type (T);
4842
4843 if Unknown_Discriminants_Present (N) then
4844 Set_Discriminant_Constraint (T, No_Elist);
4845 end if;
4846
4847 Build_Derived_Record_Type (N, Parent_Type, T);
4848
4849 -- A private extension inherits the Default_Initial_Condition pragma
4850 -- coming from any parent type within the derivation chain.
4851
4852 if Has_DIC (Parent_Type) then
4853 Set_Has_Inherited_DIC (T);
4854 end if;
4855
4856 -- A private extension inherits any class-wide invariants coming from a
4857 -- parent type or an interface. Note that the invariant procedure of the
4858 -- parent type should not be inherited because the private extension may
4859 -- define invariants of its own.
4860
4861 if Has_Inherited_Invariants (Parent_Type)
4862 or else Has_Inheritable_Invariants (Parent_Type)
4863 then
4864 Set_Has_Inherited_Invariants (T);
4865
4866 elsif Present (Interfaces (T)) then
4867 Iface_Elmt := First_Elmt (Interfaces (T));
4868 while Present (Iface_Elmt) loop
4869 Iface := Node (Iface_Elmt);
4870
4871 if Has_Inheritable_Invariants (Iface) then
4872 Set_Has_Inherited_Invariants (T);
4873 exit;
4874 end if;
4875
4876 Next_Elmt (Iface_Elmt);
4877 end loop;
4878 end if;
4879
4880 -- Ada 2005 (AI-443): Synchronized private extension or a rewritten
4881 -- synchronized formal derived type.
4882
4883 if Ada_Version >= Ada_2005 and then Synchronized_Present (N) then
4884 Set_Is_Limited_Record (T);
4885
4886 -- Formal derived type case
4887
4888 if Is_Generic_Type (T) then
4889
4890 -- The parent must be a tagged limited type or a synchronized
4891 -- interface.
4892
4893 if (not Is_Tagged_Type (Parent_Type)
4894 or else not Is_Limited_Type (Parent_Type))
4895 and then
4896 (not Is_Interface (Parent_Type)
4897 or else not Is_Synchronized_Interface (Parent_Type))
4898 then
4899 Error_Msg_NE
4900 ("parent type of & must be tagged limited or synchronized",
4901 N, T);
4902 end if;
4903
4904 -- The progenitors (if any) must be limited or synchronized
4905 -- interfaces.
4906
4907 if Present (Interfaces (T)) then
4908 Iface_Elmt := First_Elmt (Interfaces (T));
4909 while Present (Iface_Elmt) loop
4910 Iface := Node (Iface_Elmt);
4911
4912 if not Is_Limited_Interface (Iface)
4913 and then not Is_Synchronized_Interface (Iface)
4914 then
4915 Error_Msg_NE
4916 ("progenitor & must be limited or synchronized",
4917 N, Iface);
4918 end if;
4919
4920 Next_Elmt (Iface_Elmt);
4921 end loop;
4922 end if;
4923
4924 -- Regular derived extension, the parent must be a limited or
4925 -- synchronized interface.
4926
4927 else
4928 if not Is_Interface (Parent_Type)
4929 or else (not Is_Limited_Interface (Parent_Type)
4930 and then not Is_Synchronized_Interface (Parent_Type))
4931 then
4932 Error_Msg_NE
4933 ("parent type of & must be limited interface", N, T);
4934 end if;
4935 end if;
4936
4937 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
4938 -- extension with a synchronized parent must be explicitly declared
4939 -- synchronized, because the full view will be a synchronized type.
4940 -- This must be checked before the check for limited types below,
4941 -- to ensure that types declared limited are not allowed to extend
4942 -- synchronized interfaces.
4943
4944 elsif Is_Interface (Parent_Type)
4945 and then Is_Synchronized_Interface (Parent_Type)
4946 and then not Synchronized_Present (N)
4947 then
4948 Error_Msg_NE
4949 ("private extension of& must be explicitly synchronized",
4950 N, Parent_Type);
4951
4952 elsif Limited_Present (N) then
4953 Set_Is_Limited_Record (T);
4954
4955 if not Is_Limited_Type (Parent_Type)
4956 and then
4957 (not Is_Interface (Parent_Type)
4958 or else not Is_Limited_Interface (Parent_Type))
4959 then
4960 Error_Msg_NE ("parent type& of limited extension must be limited",
4961 N, Parent_Type);
4962 end if;
4963 end if;
4964
4965 -- Remember that its parent type has a private extension. Used to warn
4966 -- on public primitives of the parent type defined after its private
4967 -- extensions (see Check_Dispatching_Operation).
4968
4969 Set_Has_Private_Extension (Parent_Type);
4970
4971 <<Leave>>
4972 if Has_Aspects (N) then
4973 Analyze_Aspect_Specifications (N, T);
4974 end if;
4975 end Analyze_Private_Extension_Declaration;
4976
4977 ---------------------------------
4978 -- Analyze_Subtype_Declaration --
4979 ---------------------------------
4980
4981 procedure Analyze_Subtype_Declaration
4982 (N : Node_Id;
4983 Skip : Boolean := False)
4984 is
4985 Id : constant Entity_Id := Defining_Identifier (N);
4986 R_Checks : Check_Result;
4987 T : Entity_Id;
4988
4989 begin
4990 Generate_Definition (Id);
4991 Set_Is_Pure (Id, Is_Pure (Current_Scope));
4992 Init_Size_Align (Id);
4993
4994 -- The following guard condition on Enter_Name is to handle cases where
4995 -- the defining identifier has already been entered into the scope but
4996 -- the declaration as a whole needs to be analyzed.
4997
4998 -- This case in particular happens for derived enumeration types. The
4999 -- derived enumeration type is processed as an inserted enumeration type
5000 -- declaration followed by a rewritten subtype declaration. The defining
5001 -- identifier, however, is entered into the name scope very early in the
5002 -- processing of the original type declaration and therefore needs to be
5003 -- avoided here, when the created subtype declaration is analyzed. (See
5004 -- Build_Derived_Types)
5005
5006 -- This also happens when the full view of a private type is derived
5007 -- type with constraints. In this case the entity has been introduced
5008 -- in the private declaration.
5009
5010 -- Finally this happens in some complex cases when validity checks are
5011 -- enabled, where the same subtype declaration may be analyzed twice.
5012 -- This can happen if the subtype is created by the pre-analysis of
5013 -- an attribute tht gives the range of a loop statement, and the loop
5014 -- itself appears within an if_statement that will be rewritten during
5015 -- expansion.
5016
5017 if Skip
5018 or else (Present (Etype (Id))
5019 and then (Is_Private_Type (Etype (Id))
5020 or else Is_Task_Type (Etype (Id))
5021 or else Is_Rewrite_Substitution (N)))
5022 then
5023 null;
5024
5025 elsif Current_Entity (Id) = Id then
5026 null;
5027
5028 else
5029 Enter_Name (Id);
5030 end if;
5031
5032 T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
5033
5034 -- Class-wide equivalent types of records with unknown discriminants
5035 -- involve the generation of an itype which serves as the private view
5036 -- of a constrained record subtype. In such cases the base type of the
5037 -- current subtype we are processing is the private itype. Use the full
5038 -- of the private itype when decorating various attributes.
5039
5040 if Is_Itype (T)
5041 and then Is_Private_Type (T)
5042 and then Present (Full_View (T))
5043 then
5044 T := Full_View (T);
5045 end if;
5046
5047 -- Inherit common attributes
5048
5049 Set_Is_Volatile (Id, Is_Volatile (T));
5050 Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
5051 Set_Is_Generic_Type (Id, Is_Generic_Type (Base_Type (T)));
5052 Set_Convention (Id, Convention (T));
5053
5054 -- If ancestor has predicates then so does the subtype, and in addition
5055 -- we must delay the freeze to properly arrange predicate inheritance.
5056
5057 -- The Ancestor_Type test is really unpleasant, there seem to be cases
5058 -- in which T = ID, so the above tests and assignments do nothing???
5059
5060 if Has_Predicates (T)
5061 or else (Present (Ancestor_Subtype (T))
5062 and then Has_Predicates (Ancestor_Subtype (T)))
5063 then
5064 Set_Has_Predicates (Id);
5065 Set_Has_Delayed_Freeze (Id);
5066
5067 -- Generated subtypes inherit the predicate function from the parent
5068 -- (no aspects to examine on the generated declaration).
5069
5070 if not Comes_From_Source (N) then
5071 Set_Ekind (Id, Ekind (T));
5072
5073 if Present (Predicate_Function (T)) then
5074 Set_Predicate_Function (Id, Predicate_Function (T));
5075
5076 elsif Present (Ancestor_Subtype (T))
5077 and then Has_Predicates (Ancestor_Subtype (T))
5078 and then Present (Predicate_Function (Ancestor_Subtype (T)))
5079 then
5080 Set_Predicate_Function (Id,
5081 Predicate_Function (Ancestor_Subtype (T)));
5082 end if;
5083 end if;
5084 end if;
5085
5086 -- Subtype of Boolean cannot have a constraint in SPARK
5087
5088 if Is_Boolean_Type (T)
5089 and then Nkind (Subtype_Indication (N)) = N_Subtype_Indication
5090 then
5091 Check_SPARK_05_Restriction
5092 ("subtype of Boolean cannot have constraint", N);
5093 end if;
5094
5095 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5096 declare
5097 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
5098 One_Cstr : Node_Id;
5099 Low : Node_Id;
5100 High : Node_Id;
5101
5102 begin
5103 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint then
5104 One_Cstr := First (Constraints (Cstr));
5105 while Present (One_Cstr) loop
5106
5107 -- Index or discriminant constraint in SPARK must be a
5108 -- subtype mark.
5109
5110 if not
5111 Nkind_In (One_Cstr, N_Identifier, N_Expanded_Name)
5112 then
5113 Check_SPARK_05_Restriction
5114 ("subtype mark required", One_Cstr);
5115
5116 -- String subtype must have a lower bound of 1 in SPARK.
5117 -- Note that we do not need to test for the non-static case
5118 -- here, since that was already taken care of in
5119 -- Process_Range_Expr_In_Decl.
5120
5121 elsif Base_Type (T) = Standard_String then
5122 Get_Index_Bounds (One_Cstr, Low, High);
5123
5124 if Is_OK_Static_Expression (Low)
5125 and then Expr_Value (Low) /= 1
5126 then
5127 Check_SPARK_05_Restriction
5128 ("String subtype must have lower bound of 1", N);
5129 end if;
5130 end if;
5131
5132 Next (One_Cstr);
5133 end loop;
5134 end if;
5135 end;
5136 end if;
5137
5138 -- In the case where there is no constraint given in the subtype
5139 -- indication, Process_Subtype just returns the Subtype_Mark, so its
5140 -- semantic attributes must be established here.
5141
5142 if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
5143 Set_Etype (Id, Base_Type (T));
5144
5145 -- Subtype of unconstrained array without constraint is not allowed
5146 -- in SPARK.
5147
5148 if Is_Array_Type (T) and then not Is_Constrained (T) then
5149 Check_SPARK_05_Restriction
5150 ("subtype of unconstrained array must have constraint", N);
5151 end if;
5152
5153 case Ekind (T) is
5154 when Array_Kind =>
5155 Set_Ekind (Id, E_Array_Subtype);
5156 Copy_Array_Subtype_Attributes (Id, T);
5157
5158 when Decimal_Fixed_Point_Kind =>
5159 Set_Ekind (Id, E_Decimal_Fixed_Point_Subtype);
5160 Set_Digits_Value (Id, Digits_Value (T));
5161 Set_Delta_Value (Id, Delta_Value (T));
5162 Set_Scale_Value (Id, Scale_Value (T));
5163 Set_Small_Value (Id, Small_Value (T));
5164 Set_Scalar_Range (Id, Scalar_Range (T));
5165 Set_Machine_Radix_10 (Id, Machine_Radix_10 (T));
5166 Set_Is_Constrained (Id, Is_Constrained (T));
5167 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5168 Set_RM_Size (Id, RM_Size (T));
5169
5170 when Enumeration_Kind =>
5171 Set_Ekind (Id, E_Enumeration_Subtype);
5172 Set_First_Literal (Id, First_Literal (Base_Type (T)));
5173 Set_Scalar_Range (Id, Scalar_Range (T));
5174 Set_Is_Character_Type (Id, Is_Character_Type (T));
5175 Set_Is_Constrained (Id, Is_Constrained (T));
5176 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5177 Set_RM_Size (Id, RM_Size (T));
5178 Inherit_Predicate_Flags (Id, T);
5179
5180 when Ordinary_Fixed_Point_Kind =>
5181 Set_Ekind (Id, E_Ordinary_Fixed_Point_Subtype);
5182 Set_Scalar_Range (Id, Scalar_Range (T));
5183 Set_Small_Value (Id, Small_Value (T));
5184 Set_Delta_Value (Id, Delta_Value (T));
5185 Set_Is_Constrained (Id, Is_Constrained (T));
5186 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5187 Set_RM_Size (Id, RM_Size (T));
5188
5189 when Float_Kind =>
5190 Set_Ekind (Id, E_Floating_Point_Subtype);
5191 Set_Scalar_Range (Id, Scalar_Range (T));
5192 Set_Digits_Value (Id, Digits_Value (T));
5193 Set_Is_Constrained (Id, Is_Constrained (T));
5194
5195 -- If the floating point type has dimensions, these will be
5196 -- inherited subsequently when Analyze_Dimensions is called.
5197
5198 when Signed_Integer_Kind =>
5199 Set_Ekind (Id, E_Signed_Integer_Subtype);
5200 Set_Scalar_Range (Id, Scalar_Range (T));
5201 Set_Is_Constrained (Id, Is_Constrained (T));
5202 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5203 Set_RM_Size (Id, RM_Size (T));
5204 Inherit_Predicate_Flags (Id, T);
5205
5206 when Modular_Integer_Kind =>
5207 Set_Ekind (Id, E_Modular_Integer_Subtype);
5208 Set_Scalar_Range (Id, Scalar_Range (T));
5209 Set_Is_Constrained (Id, Is_Constrained (T));
5210 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5211 Set_RM_Size (Id, RM_Size (T));
5212 Inherit_Predicate_Flags (Id, T);
5213
5214 when Class_Wide_Kind =>
5215 Set_Ekind (Id, E_Class_Wide_Subtype);
5216 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5217 Set_Cloned_Subtype (Id, T);
5218 Set_Is_Tagged_Type (Id, True);
5219 Set_Has_Unknown_Discriminants
5220 (Id, True);
5221 Set_No_Tagged_Streams_Pragma
5222 (Id, No_Tagged_Streams_Pragma (T));
5223
5224 if Ekind (T) = E_Class_Wide_Subtype then
5225 Set_Equivalent_Type (Id, Equivalent_Type (T));
5226 end if;
5227
5228 when E_Record_Subtype
5229 | E_Record_Type
5230 =>
5231 Set_Ekind (Id, E_Record_Subtype);
5232
5233 if Ekind (T) = E_Record_Subtype
5234 and then Present (Cloned_Subtype (T))
5235 then
5236 Set_Cloned_Subtype (Id, Cloned_Subtype (T));
5237 else
5238 Set_Cloned_Subtype (Id, T);
5239 end if;
5240
5241 Set_First_Entity (Id, First_Entity (T));
5242 Set_Last_Entity (Id, Last_Entity (T));
5243 Set_Has_Discriminants (Id, Has_Discriminants (T));
5244 Set_Is_Constrained (Id, Is_Constrained (T));
5245 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5246 Set_Has_Implicit_Dereference
5247 (Id, Has_Implicit_Dereference (T));
5248 Set_Has_Unknown_Discriminants
5249 (Id, Has_Unknown_Discriminants (T));
5250
5251 if Has_Discriminants (T) then
5252 Set_Discriminant_Constraint
5253 (Id, Discriminant_Constraint (T));
5254 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5255
5256 elsif Has_Unknown_Discriminants (Id) then
5257 Set_Discriminant_Constraint (Id, No_Elist);
5258 end if;
5259
5260 if Is_Tagged_Type (T) then
5261 Set_Is_Tagged_Type (Id, True);
5262 Set_No_Tagged_Streams_Pragma
5263 (Id, No_Tagged_Streams_Pragma (T));
5264 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5265 Set_Direct_Primitive_Operations
5266 (Id, Direct_Primitive_Operations (T));
5267 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5268
5269 if Is_Interface (T) then
5270 Set_Is_Interface (Id);
5271 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
5272 end if;
5273 end if;
5274
5275 when Private_Kind =>
5276 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
5277 Set_Has_Discriminants (Id, Has_Discriminants (T));
5278 Set_Is_Constrained (Id, Is_Constrained (T));
5279 Set_First_Entity (Id, First_Entity (T));
5280 Set_Last_Entity (Id, Last_Entity (T));
5281 Set_Private_Dependents (Id, New_Elmt_List);
5282 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5283 Set_Has_Implicit_Dereference
5284 (Id, Has_Implicit_Dereference (T));
5285 Set_Has_Unknown_Discriminants
5286 (Id, Has_Unknown_Discriminants (T));
5287 Set_Known_To_Have_Preelab_Init
5288 (Id, Known_To_Have_Preelab_Init (T));
5289
5290 if Is_Tagged_Type (T) then
5291 Set_Is_Tagged_Type (Id);
5292 Set_No_Tagged_Streams_Pragma (Id,
5293 No_Tagged_Streams_Pragma (T));
5294 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5295 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5296 Set_Direct_Primitive_Operations (Id,
5297 Direct_Primitive_Operations (T));
5298 end if;
5299
5300 -- In general the attributes of the subtype of a private type
5301 -- are the attributes of the partial view of parent. However,
5302 -- the full view may be a discriminated type, and the subtype
5303 -- must share the discriminant constraint to generate correct
5304 -- calls to initialization procedures.
5305
5306 if Has_Discriminants (T) then
5307 Set_Discriminant_Constraint
5308 (Id, Discriminant_Constraint (T));
5309 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5310
5311 elsif Present (Full_View (T))
5312 and then Has_Discriminants (Full_View (T))
5313 then
5314 Set_Discriminant_Constraint
5315 (Id, Discriminant_Constraint (Full_View (T)));
5316 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5317
5318 -- This would seem semantically correct, but apparently
5319 -- generates spurious errors about missing components ???
5320
5321 -- Set_Has_Discriminants (Id);
5322 end if;
5323
5324 Prepare_Private_Subtype_Completion (Id, N);
5325
5326 -- If this is the subtype of a constrained private type with
5327 -- discriminants that has got a full view and we also have
5328 -- built a completion just above, show that the completion
5329 -- is a clone of the full view to the back-end.
5330
5331 if Has_Discriminants (T)
5332 and then not Has_Unknown_Discriminants (T)
5333 and then not Is_Empty_Elmt_List (Discriminant_Constraint (T))
5334 and then Present (Full_View (T))
5335 and then Present (Full_View (Id))
5336 then
5337 Set_Cloned_Subtype (Full_View (Id), Full_View (T));
5338 end if;
5339
5340 when Access_Kind =>
5341 Set_Ekind (Id, E_Access_Subtype);
5342 Set_Is_Constrained (Id, Is_Constrained (T));
5343 Set_Is_Access_Constant
5344 (Id, Is_Access_Constant (T));
5345 Set_Directly_Designated_Type
5346 (Id, Designated_Type (T));
5347 Set_Can_Never_Be_Null (Id, Can_Never_Be_Null (T));
5348
5349 -- A Pure library_item must not contain the declaration of a
5350 -- named access type, except within a subprogram, generic
5351 -- subprogram, task unit, or protected unit, or if it has
5352 -- a specified Storage_Size of zero (RM05-10.2.1(15.4-15.5)).
5353
5354 if Comes_From_Source (Id)
5355 and then In_Pure_Unit
5356 and then not In_Subprogram_Task_Protected_Unit
5357 and then not No_Pool_Assigned (Id)
5358 then
5359 Error_Msg_N
5360 ("named access types not allowed in pure unit", N);
5361 end if;
5362
5363 when Concurrent_Kind =>
5364 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
5365 Set_Corresponding_Record_Type (Id,
5366 Corresponding_Record_Type (T));
5367 Set_First_Entity (Id, First_Entity (T));
5368 Set_First_Private_Entity (Id, First_Private_Entity (T));
5369 Set_Has_Discriminants (Id, Has_Discriminants (T));
5370 Set_Is_Constrained (Id, Is_Constrained (T));
5371 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5372 Set_Last_Entity (Id, Last_Entity (T));
5373
5374 if Is_Tagged_Type (T) then
5375 Set_No_Tagged_Streams_Pragma
5376 (Id, No_Tagged_Streams_Pragma (T));
5377 end if;
5378
5379 if Has_Discriminants (T) then
5380 Set_Discriminant_Constraint
5381 (Id, Discriminant_Constraint (T));
5382 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5383 end if;
5384
5385 when Incomplete_Kind =>
5386 if Ada_Version >= Ada_2005 then
5387
5388 -- In Ada 2005 an incomplete type can be explicitly tagged:
5389 -- propagate indication. Note that we also have to include
5390 -- subtypes for Ada 2012 extended use of incomplete types.
5391
5392 Set_Ekind (Id, E_Incomplete_Subtype);
5393 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5394 Set_Private_Dependents (Id, New_Elmt_List);
5395
5396 if Is_Tagged_Type (Id) then
5397 Set_No_Tagged_Streams_Pragma
5398 (Id, No_Tagged_Streams_Pragma (T));
5399 Set_Direct_Primitive_Operations (Id, New_Elmt_List);
5400 end if;
5401
5402 -- Ada 2005 (AI-412): Decorate an incomplete subtype of an
5403 -- incomplete type visible through a limited with clause.
5404
5405 if From_Limited_With (T)
5406 and then Present (Non_Limited_View (T))
5407 then
5408 Set_From_Limited_With (Id);
5409 Set_Non_Limited_View (Id, Non_Limited_View (T));
5410
5411 -- Ada 2005 (AI-412): Add the regular incomplete subtype
5412 -- to the private dependents of the original incomplete
5413 -- type for future transformation.
5414
5415 else
5416 Append_Elmt (Id, Private_Dependents (T));
5417 end if;
5418
5419 -- If the subtype name denotes an incomplete type an error
5420 -- was already reported by Process_Subtype.
5421
5422 else
5423 Set_Etype (Id, Any_Type);
5424 end if;
5425
5426 when others =>
5427 raise Program_Error;
5428 end case;
5429 end if;
5430
5431 if Etype (Id) = Any_Type then
5432 goto Leave;
5433 end if;
5434
5435 -- Some common processing on all types
5436
5437 Set_Size_Info (Id, T);
5438 Set_First_Rep_Item (Id, First_Rep_Item (T));
5439
5440 -- If the parent type is a generic actual, so is the subtype. This may
5441 -- happen in a nested instance. Why Comes_From_Source test???
5442
5443 if not Comes_From_Source (N) then
5444 Set_Is_Generic_Actual_Type (Id, Is_Generic_Actual_Type (T));
5445 end if;
5446
5447 -- If this is a subtype declaration for an actual in an instance,
5448 -- inherit static and dynamic predicates if any.
5449
5450 -- If declaration has no aspect specifications, inherit predicate
5451 -- info as well. Unclear how to handle the case of both specified
5452 -- and inherited predicates ??? Other inherited aspects, such as
5453 -- invariants, should be OK, but the combination with later pragmas
5454 -- may also require special merging.
5455
5456 if Has_Predicates (T)
5457 and then Present (Predicate_Function (T))
5458 and then
5459 ((In_Instance and then not Comes_From_Source (N))
5460 or else No (Aspect_Specifications (N)))
5461 then
5462 Set_Subprograms_For_Type (Id, Subprograms_For_Type (T));
5463
5464 if Has_Static_Predicate (T) then
5465 Set_Has_Static_Predicate (Id);
5466 Set_Static_Discrete_Predicate (Id, Static_Discrete_Predicate (T));
5467 end if;
5468 end if;
5469
5470 -- Remaining processing depends on characteristics of base type
5471
5472 T := Etype (Id);
5473
5474 Set_Is_Immediately_Visible (Id, True);
5475 Set_Depends_On_Private (Id, Has_Private_Component (T));
5476 Set_Is_Descendant_Of_Address (Id, Is_Descendant_Of_Address (T));
5477
5478 if Is_Interface (T) then
5479 Set_Is_Interface (Id);
5480 end if;
5481
5482 if Present (Generic_Parent_Type (N))
5483 and then
5484 (Nkind (Parent (Generic_Parent_Type (N))) /=
5485 N_Formal_Type_Declaration
5486 or else Nkind (Formal_Type_Definition
5487 (Parent (Generic_Parent_Type (N)))) /=
5488 N_Formal_Private_Type_Definition)
5489 then
5490 if Is_Tagged_Type (Id) then
5491
5492 -- If this is a generic actual subtype for a synchronized type,
5493 -- the primitive operations are those of the corresponding record
5494 -- for which there is a separate subtype declaration.
5495
5496 if Is_Concurrent_Type (Id) then
5497 null;
5498 elsif Is_Class_Wide_Type (Id) then
5499 Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
5500 else
5501 Derive_Subprograms (Generic_Parent_Type (N), Id, T);
5502 end if;
5503
5504 elsif Scope (Etype (Id)) /= Standard_Standard then
5505 Derive_Subprograms (Generic_Parent_Type (N), Id);
5506 end if;
5507 end if;
5508
5509 if Is_Private_Type (T) and then Present (Full_View (T)) then
5510 Conditional_Delay (Id, Full_View (T));
5511
5512 -- The subtypes of components or subcomponents of protected types
5513 -- do not need freeze nodes, which would otherwise appear in the
5514 -- wrong scope (before the freeze node for the protected type). The
5515 -- proper subtypes are those of the subcomponents of the corresponding
5516 -- record.
5517
5518 elsif Ekind (Scope (Id)) /= E_Protected_Type
5519 and then Present (Scope (Scope (Id))) -- error defense
5520 and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
5521 then
5522 Conditional_Delay (Id, T);
5523 end if;
5524
5525 -- Check that Constraint_Error is raised for a scalar subtype indication
5526 -- when the lower or upper bound of a non-null range lies outside the
5527 -- range of the type mark.
5528
5529 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5530 if Is_Scalar_Type (Etype (Id))
5531 and then Scalar_Range (Id) /=
5532 Scalar_Range
5533 (Etype (Subtype_Mark (Subtype_Indication (N))))
5534 then
5535 Apply_Range_Check
5536 (Scalar_Range (Id),
5537 Etype (Subtype_Mark (Subtype_Indication (N))));
5538
5539 -- In the array case, check compatibility for each index
5540
5541 elsif Is_Array_Type (Etype (Id)) and then Present (First_Index (Id))
5542 then
5543 -- This really should be a subprogram that finds the indications
5544 -- to check???
5545
5546 declare
5547 Subt_Index : Node_Id := First_Index (Id);
5548 Target_Index : Node_Id :=
5549 First_Index (Etype
5550 (Subtype_Mark (Subtype_Indication (N))));
5551 Has_Dyn_Chk : Boolean := Has_Dynamic_Range_Check (N);
5552
5553 begin
5554 while Present (Subt_Index) loop
5555 if ((Nkind (Subt_Index) = N_Identifier
5556 and then Ekind (Entity (Subt_Index)) in Scalar_Kind)
5557 or else Nkind (Subt_Index) = N_Subtype_Indication)
5558 and then
5559 Nkind (Scalar_Range (Etype (Subt_Index))) = N_Range
5560 then
5561 declare
5562 Target_Typ : constant Entity_Id :=
5563 Etype (Target_Index);
5564 begin
5565 R_Checks :=
5566 Get_Range_Checks
5567 (Scalar_Range (Etype (Subt_Index)),
5568 Target_Typ,
5569 Etype (Subt_Index),
5570 Defining_Identifier (N));
5571
5572 -- Reset Has_Dynamic_Range_Check on the subtype to
5573 -- prevent elision of the index check due to a dynamic
5574 -- check generated for a preceding index (needed since
5575 -- Insert_Range_Checks tries to avoid generating
5576 -- redundant checks on a given declaration).
5577
5578 Set_Has_Dynamic_Range_Check (N, False);
5579
5580 Insert_Range_Checks
5581 (R_Checks,
5582 N,
5583 Target_Typ,
5584 Sloc (Defining_Identifier (N)));
5585
5586 -- Record whether this index involved a dynamic check
5587
5588 Has_Dyn_Chk :=
5589 Has_Dyn_Chk or else Has_Dynamic_Range_Check (N);
5590 end;
5591 end if;
5592
5593 Next_Index (Subt_Index);
5594 Next_Index (Target_Index);
5595 end loop;
5596
5597 -- Finally, mark whether the subtype involves dynamic checks
5598
5599 Set_Has_Dynamic_Range_Check (N, Has_Dyn_Chk);
5600 end;
5601 end if;
5602 end if;
5603
5604 Set_Optimize_Alignment_Flags (Id);
5605 Check_Eliminated (Id);
5606
5607 <<Leave>>
5608 if Has_Aspects (N) then
5609 Analyze_Aspect_Specifications (N, Id);
5610 end if;
5611
5612 Analyze_Dimension (N);
5613
5614 -- Check No_Dynamic_Sized_Objects restriction, which disallows subtype
5615 -- indications on composite types where the constraints are dynamic.
5616 -- Note that object declarations and aggregates generate implicit
5617 -- subtype declarations, which this covers. One special case is that the
5618 -- implicitly generated "=" for discriminated types includes an
5619 -- offending subtype declaration, which is harmless, so we ignore it
5620 -- here.
5621
5622 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5623 declare
5624 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
5625 begin
5626 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint
5627 and then not (Is_Internal (Id)
5628 and then Is_TSS (Scope (Id),
5629 TSS_Composite_Equality))
5630 and then not Within_Init_Proc
5631 and then not All_Composite_Constraints_Static (Cstr)
5632 then
5633 Check_Restriction (No_Dynamic_Sized_Objects, Cstr);
5634 end if;
5635 end;
5636 end if;
5637 end Analyze_Subtype_Declaration;
5638
5639 --------------------------------
5640 -- Analyze_Subtype_Indication --
5641 --------------------------------
5642
5643 procedure Analyze_Subtype_Indication (N : Node_Id) is
5644 T : constant Entity_Id := Subtype_Mark (N);
5645 R : constant Node_Id := Range_Expression (Constraint (N));
5646
5647 begin
5648 Analyze (T);
5649
5650 if R /= Error then
5651 Analyze (R);
5652 Set_Etype (N, Etype (R));
5653 Resolve (R, Entity (T));
5654 else
5655 Set_Error_Posted (R);
5656 Set_Error_Posted (T);
5657 end if;
5658 end Analyze_Subtype_Indication;
5659
5660 --------------------------
5661 -- Analyze_Variant_Part --
5662 --------------------------
5663
5664 procedure Analyze_Variant_Part (N : Node_Id) is
5665 Discr_Name : Node_Id;
5666 Discr_Type : Entity_Id;
5667
5668 procedure Process_Variant (A : Node_Id);
5669 -- Analyze declarations for a single variant
5670
5671 package Analyze_Variant_Choices is
5672 new Generic_Analyze_Choices (Process_Variant);
5673 use Analyze_Variant_Choices;
5674
5675 ---------------------
5676 -- Process_Variant --
5677 ---------------------
5678
5679 procedure Process_Variant (A : Node_Id) is
5680 CL : constant Node_Id := Component_List (A);
5681 begin
5682 if not Null_Present (CL) then
5683 Analyze_Declarations (Component_Items (CL));
5684
5685 if Present (Variant_Part (CL)) then
5686 Analyze (Variant_Part (CL));
5687 end if;
5688 end if;
5689 end Process_Variant;
5690
5691 -- Start of processing for Analyze_Variant_Part
5692
5693 begin
5694 Discr_Name := Name (N);
5695 Analyze (Discr_Name);
5696
5697 -- If Discr_Name bad, get out (prevent cascaded errors)
5698
5699 if Etype (Discr_Name) = Any_Type then
5700 return;
5701 end if;
5702
5703 -- Check invalid discriminant in variant part
5704
5705 if Ekind (Entity (Discr_Name)) /= E_Discriminant then
5706 Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
5707 end if;
5708
5709 Discr_Type := Etype (Entity (Discr_Name));
5710
5711 if not Is_Discrete_Type (Discr_Type) then
5712 Error_Msg_N
5713 ("discriminant in a variant part must be of a discrete type",
5714 Name (N));
5715 return;
5716 end if;
5717
5718 -- Now analyze the choices, which also analyzes the declarations that
5719 -- are associated with each choice.
5720
5721 Analyze_Choices (Variants (N), Discr_Type);
5722
5723 -- Note: we used to instantiate and call Check_Choices here to check
5724 -- that the choices covered the discriminant, but it's too early to do
5725 -- that because of statically predicated subtypes, whose analysis may
5726 -- be deferred to their freeze point which may be as late as the freeze
5727 -- point of the containing record. So this call is now to be found in
5728 -- Freeze_Record_Declaration.
5729
5730 end Analyze_Variant_Part;
5731
5732 ----------------------------
5733 -- Array_Type_Declaration --
5734 ----------------------------
5735
5736 procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
5737 Component_Def : constant Node_Id := Component_Definition (Def);
5738 Component_Typ : constant Node_Id := Subtype_Indication (Component_Def);
5739 P : constant Node_Id := Parent (Def);
5740 Element_Type : Entity_Id;
5741 Implicit_Base : Entity_Id;
5742 Index : Node_Id;
5743 Nb_Index : Nat;
5744 Priv : Entity_Id;
5745 Related_Id : Entity_Id := Empty;
5746
5747 begin
5748 if Nkind (Def) = N_Constrained_Array_Definition then
5749 Index := First (Discrete_Subtype_Definitions (Def));
5750 else
5751 Index := First (Subtype_Marks (Def));
5752 end if;
5753
5754 -- Find proper names for the implicit types which may be public. In case
5755 -- of anonymous arrays we use the name of the first object of that type
5756 -- as prefix.
5757
5758 if No (T) then
5759 Related_Id := Defining_Identifier (P);
5760 else
5761 Related_Id := T;
5762 end if;
5763
5764 Nb_Index := 1;
5765 while Present (Index) loop
5766 Analyze (Index);
5767
5768 -- Test for odd case of trying to index a type by the type itself
5769
5770 if Is_Entity_Name (Index) and then Entity (Index) = T then
5771 Error_Msg_N ("type& cannot be indexed by itself", Index);
5772 Set_Entity (Index, Standard_Boolean);
5773 Set_Etype (Index, Standard_Boolean);
5774 end if;
5775
5776 -- Check SPARK restriction requiring a subtype mark
5777
5778 if not Nkind_In (Index, N_Identifier, N_Expanded_Name) then
5779 Check_SPARK_05_Restriction ("subtype mark required", Index);
5780 end if;
5781
5782 -- Add a subtype declaration for each index of private array type
5783 -- declaration whose etype is also private. For example:
5784
5785 -- package Pkg is
5786 -- type Index is private;
5787 -- private
5788 -- type Table is array (Index) of ...
5789 -- end;
5790
5791 -- This is currently required by the expander for the internally
5792 -- generated equality subprogram of records with variant parts in
5793 -- which the etype of some component is such private type.
5794
5795 if Ekind (Current_Scope) = E_Package
5796 and then In_Private_Part (Current_Scope)
5797 and then Has_Private_Declaration (Etype (Index))
5798 then
5799 declare
5800 Loc : constant Source_Ptr := Sloc (Def);
5801 Decl : Entity_Id;
5802 New_E : Entity_Id;
5803
5804 begin
5805 New_E := Make_Temporary (Loc, 'T');
5806 Set_Is_Internal (New_E);
5807
5808 Decl :=
5809 Make_Subtype_Declaration (Loc,
5810 Defining_Identifier => New_E,
5811 Subtype_Indication =>
5812 New_Occurrence_Of (Etype (Index), Loc));
5813
5814 Insert_Before (Parent (Def), Decl);
5815 Analyze (Decl);
5816 Set_Etype (Index, New_E);
5817
5818 -- If the index is a range the Entity attribute is not
5819 -- available. Example:
5820
5821 -- package Pkg is
5822 -- type T is private;
5823 -- private
5824 -- type T is new Natural;
5825 -- Table : array (T(1) .. T(10)) of Boolean;
5826 -- end Pkg;
5827
5828 if Nkind (Index) /= N_Range then
5829 Set_Entity (Index, New_E);
5830 end if;
5831 end;
5832 end if;
5833
5834 Make_Index (Index, P, Related_Id, Nb_Index);
5835
5836 -- Check error of subtype with predicate for index type
5837
5838 Bad_Predicated_Subtype_Use
5839 ("subtype& has predicate, not allowed as index subtype",
5840 Index, Etype (Index));
5841
5842 -- Move to next index
5843
5844 Next_Index (Index);
5845 Nb_Index := Nb_Index + 1;
5846 end loop;
5847
5848 -- Process subtype indication if one is present
5849
5850 if Present (Component_Typ) then
5851 Element_Type := Process_Subtype (Component_Typ, P, Related_Id, 'C');
5852
5853 Set_Etype (Component_Typ, Element_Type);
5854
5855 if not Nkind_In (Component_Typ, N_Identifier, N_Expanded_Name) then
5856 Check_SPARK_05_Restriction
5857 ("subtype mark required", Component_Typ);
5858 end if;
5859
5860 -- Ada 2005 (AI-230): Access Definition case
5861
5862 else pragma Assert (Present (Access_Definition (Component_Def)));
5863
5864 -- Indicate that the anonymous access type is created by the
5865 -- array type declaration.
5866
5867 Element_Type := Access_Definition
5868 (Related_Nod => P,
5869 N => Access_Definition (Component_Def));
5870 Set_Is_Local_Anonymous_Access (Element_Type);
5871
5872 -- Propagate the parent. This field is needed if we have to generate
5873 -- the master_id associated with an anonymous access to task type
5874 -- component (see Expand_N_Full_Type_Declaration.Build_Master)
5875
5876 Set_Parent (Element_Type, Parent (T));
5877
5878 -- Ada 2005 (AI-230): In case of components that are anonymous access
5879 -- types the level of accessibility depends on the enclosing type
5880 -- declaration
5881
5882 Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
5883
5884 -- Ada 2005 (AI-254)
5885
5886 declare
5887 CD : constant Node_Id :=
5888 Access_To_Subprogram_Definition
5889 (Access_Definition (Component_Def));
5890 begin
5891 if Present (CD) and then Protected_Present (CD) then
5892 Element_Type :=
5893 Replace_Anonymous_Access_To_Protected_Subprogram (Def);
5894 end if;
5895 end;
5896 end if;
5897
5898 -- Constrained array case
5899
5900 if No (T) then
5901 T := Create_Itype (E_Void, P, Related_Id, 'T');
5902 end if;
5903
5904 if Nkind (Def) = N_Constrained_Array_Definition then
5905
5906 -- Establish Implicit_Base as unconstrained base type
5907
5908 Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
5909
5910 Set_Etype (Implicit_Base, Implicit_Base);
5911 Set_Scope (Implicit_Base, Current_Scope);
5912 Set_Has_Delayed_Freeze (Implicit_Base);
5913 Set_Default_SSO (Implicit_Base);
5914
5915 -- The constrained array type is a subtype of the unconstrained one
5916
5917 Set_Ekind (T, E_Array_Subtype);
5918 Init_Size_Align (T);
5919 Set_Etype (T, Implicit_Base);
5920 Set_Scope (T, Current_Scope);
5921 Set_Is_Constrained (T);
5922 Set_First_Index (T,
5923 First (Discrete_Subtype_Definitions (Def)));
5924 Set_Has_Delayed_Freeze (T);
5925
5926 -- Complete setup of implicit base type
5927
5928 Set_Component_Size (Implicit_Base, Uint_0);
5929 Set_Component_Type (Implicit_Base, Element_Type);
5930 Set_Finalize_Storage_Only
5931 (Implicit_Base,
5932 Finalize_Storage_Only (Element_Type));
5933 Set_First_Index (Implicit_Base, First_Index (T));
5934 Set_Has_Controlled_Component
5935 (Implicit_Base,
5936 Has_Controlled_Component (Element_Type)
5937 or else Is_Controlled_Active (Element_Type));
5938 Set_Packed_Array_Impl_Type
5939 (Implicit_Base, Empty);
5940
5941 Propagate_Concurrent_Flags (Implicit_Base, Element_Type);
5942
5943 -- Unconstrained array case
5944
5945 else
5946 Set_Ekind (T, E_Array_Type);
5947 Init_Size_Align (T);
5948 Set_Etype (T, T);
5949 Set_Scope (T, Current_Scope);
5950 Set_Component_Size (T, Uint_0);
5951 Set_Is_Constrained (T, False);
5952 Set_First_Index (T, First (Subtype_Marks (Def)));
5953 Set_Has_Delayed_Freeze (T, True);
5954 Propagate_Concurrent_Flags (T, Element_Type);
5955 Set_Has_Controlled_Component (T, Has_Controlled_Component
5956 (Element_Type)
5957 or else
5958 Is_Controlled_Active (Element_Type));
5959 Set_Finalize_Storage_Only (T, Finalize_Storage_Only
5960 (Element_Type));
5961 Set_Default_SSO (T);
5962 end if;
5963
5964 -- Common attributes for both cases
5965
5966 Set_Component_Type (Base_Type (T), Element_Type);
5967 Set_Packed_Array_Impl_Type (T, Empty);
5968
5969 if Aliased_Present (Component_Definition (Def)) then
5970 Check_SPARK_05_Restriction
5971 ("aliased is not allowed", Component_Definition (Def));
5972 Set_Has_Aliased_Components (Etype (T));
5973 end if;
5974
5975 -- Ada 2005 (AI-231): Propagate the null-excluding attribute to the
5976 -- array type to ensure that objects of this type are initialized.
5977
5978 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (Element_Type) then
5979 Set_Can_Never_Be_Null (T);
5980
5981 if Null_Exclusion_Present (Component_Definition (Def))
5982
5983 -- No need to check itypes because in their case this check was
5984 -- done at their point of creation
5985
5986 and then not Is_Itype (Element_Type)
5987 then
5988 Error_Msg_N
5989 ("`NOT NULL` not allowed (null already excluded)",
5990 Subtype_Indication (Component_Definition (Def)));
5991 end if;
5992 end if;
5993
5994 Priv := Private_Component (Element_Type);
5995
5996 if Present (Priv) then
5997
5998 -- Check for circular definitions
5999
6000 if Priv = Any_Type then
6001 Set_Component_Type (Etype (T), Any_Type);
6002
6003 -- There is a gap in the visibility of operations on the composite
6004 -- type only if the component type is defined in a different scope.
6005
6006 elsif Scope (Priv) = Current_Scope then
6007 null;
6008
6009 elsif Is_Limited_Type (Priv) then
6010 Set_Is_Limited_Composite (Etype (T));
6011 Set_Is_Limited_Composite (T);
6012 else
6013 Set_Is_Private_Composite (Etype (T));
6014 Set_Is_Private_Composite (T);
6015 end if;
6016 end if;
6017
6018 -- A syntax error in the declaration itself may lead to an empty index
6019 -- list, in which case do a minimal patch.
6020
6021 if No (First_Index (T)) then
6022 Error_Msg_N ("missing index definition in array type declaration", T);
6023
6024 declare
6025 Indexes : constant List_Id :=
6026 New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
6027 begin
6028 Set_Discrete_Subtype_Definitions (Def, Indexes);
6029 Set_First_Index (T, First (Indexes));
6030 return;
6031 end;
6032 end if;
6033
6034 -- Create a concatenation operator for the new type. Internal array
6035 -- types created for packed entities do not need such, they are
6036 -- compatible with the user-defined type.
6037
6038 if Number_Dimensions (T) = 1
6039 and then not Is_Packed_Array_Impl_Type (T)
6040 then
6041 New_Concatenation_Op (T);
6042 end if;
6043
6044 -- In the case of an unconstrained array the parser has already verified
6045 -- that all the indexes are unconstrained but we still need to make sure
6046 -- that the element type is constrained.
6047
6048 if not Is_Definite_Subtype (Element_Type) then
6049 Error_Msg_N
6050 ("unconstrained element type in array declaration",
6051 Subtype_Indication (Component_Def));
6052
6053 elsif Is_Abstract_Type (Element_Type) then
6054 Error_Msg_N
6055 ("the type of a component cannot be abstract",
6056 Subtype_Indication (Component_Def));
6057 end if;
6058
6059 -- There may be an invariant declared for the component type, but
6060 -- the construction of the component invariant checking procedure
6061 -- takes place during expansion.
6062 end Array_Type_Declaration;
6063
6064 ------------------------------------------------------
6065 -- Replace_Anonymous_Access_To_Protected_Subprogram --
6066 ------------------------------------------------------
6067
6068 function Replace_Anonymous_Access_To_Protected_Subprogram
6069 (N : Node_Id) return Entity_Id
6070 is
6071 Loc : constant Source_Ptr := Sloc (N);
6072
6073 Curr_Scope : constant Scope_Stack_Entry :=
6074 Scope_Stack.Table (Scope_Stack.Last);
6075
6076 Anon : constant Entity_Id := Make_Temporary (Loc, 'S');
6077
6078 Acc : Node_Id;
6079 -- Access definition in declaration
6080
6081 Comp : Node_Id;
6082 -- Object definition or formal definition with an access definition
6083
6084 Decl : Node_Id;
6085 -- Declaration of anonymous access to subprogram type
6086
6087 Spec : Node_Id;
6088 -- Original specification in access to subprogram
6089
6090 P : Node_Id;
6091
6092 begin
6093 Set_Is_Internal (Anon);
6094
6095 case Nkind (N) is
6096 when N_Constrained_Array_Definition
6097 | N_Component_Declaration
6098 | N_Unconstrained_Array_Definition
6099 =>
6100 Comp := Component_Definition (N);
6101 Acc := Access_Definition (Comp);
6102
6103 when N_Discriminant_Specification =>
6104 Comp := Discriminant_Type (N);
6105 Acc := Comp;
6106
6107 when N_Parameter_Specification =>
6108 Comp := Parameter_Type (N);
6109 Acc := Comp;
6110
6111 when N_Access_Function_Definition =>
6112 Comp := Result_Definition (N);
6113 Acc := Comp;
6114
6115 when N_Object_Declaration =>
6116 Comp := Object_Definition (N);
6117 Acc := Comp;
6118
6119 when N_Function_Specification =>
6120 Comp := Result_Definition (N);
6121 Acc := Comp;
6122
6123 when others =>
6124 raise Program_Error;
6125 end case;
6126
6127 Spec := Access_To_Subprogram_Definition (Acc);
6128
6129 Decl :=
6130 Make_Full_Type_Declaration (Loc,
6131 Defining_Identifier => Anon,
6132 Type_Definition => Copy_Separate_Tree (Spec));
6133
6134 Mark_Rewrite_Insertion (Decl);
6135
6136 -- In ASIS mode, analyze the profile on the original node, because
6137 -- the separate copy does not provide enough links to recover the
6138 -- original tree. Analysis is limited to type annotations, within
6139 -- a temporary scope that serves as an anonymous subprogram to collect
6140 -- otherwise useless temporaries and itypes.
6141
6142 if ASIS_Mode then
6143 declare
6144 Typ : constant Entity_Id := Make_Temporary (Loc, 'S');
6145
6146 begin
6147 if Nkind (Spec) = N_Access_Function_Definition then
6148 Set_Ekind (Typ, E_Function);
6149 else
6150 Set_Ekind (Typ, E_Procedure);
6151 end if;
6152
6153 Set_Parent (Typ, N);
6154 Set_Scope (Typ, Current_Scope);
6155 Push_Scope (Typ);
6156
6157 -- Nothing to do if procedure is parameterless
6158
6159 if Present (Parameter_Specifications (Spec)) then
6160 Process_Formals (Parameter_Specifications (Spec), Spec);
6161 end if;
6162
6163 if Nkind (Spec) = N_Access_Function_Definition then
6164 declare
6165 Def : constant Node_Id := Result_Definition (Spec);
6166
6167 begin
6168 -- The result might itself be an anonymous access type, so
6169 -- have to recurse.
6170
6171 if Nkind (Def) = N_Access_Definition then
6172 if Present (Access_To_Subprogram_Definition (Def)) then
6173 Set_Etype
6174 (Def,
6175 Replace_Anonymous_Access_To_Protected_Subprogram
6176 (Spec));
6177 else
6178 Find_Type (Subtype_Mark (Def));
6179 end if;
6180
6181 else
6182 Find_Type (Def);
6183 end if;
6184 end;
6185 end if;
6186
6187 End_Scope;
6188 end;
6189 end if;
6190
6191 -- Insert the new declaration in the nearest enclosing scope. If the
6192 -- parent is a body and N is its return type, the declaration belongs
6193 -- in the enclosing scope. Likewise if N is the type of a parameter.
6194
6195 P := Parent (N);
6196
6197 if Nkind (N) = N_Function_Specification
6198 and then Nkind (P) = N_Subprogram_Body
6199 then
6200 P := Parent (P);
6201 elsif Nkind (N) = N_Parameter_Specification
6202 and then Nkind (P) in N_Subprogram_Specification
6203 and then Nkind (Parent (P)) = N_Subprogram_Body
6204 then
6205 P := Parent (Parent (P));
6206 end if;
6207
6208 while Present (P) and then not Has_Declarations (P) loop
6209 P := Parent (P);
6210 end loop;
6211
6212 pragma Assert (Present (P));
6213
6214 if Nkind (P) = N_Package_Specification then
6215 Prepend (Decl, Visible_Declarations (P));
6216 else
6217 Prepend (Decl, Declarations (P));
6218 end if;
6219
6220 -- Replace the anonymous type with an occurrence of the new declaration.
6221 -- In all cases the rewritten node does not have the null-exclusion
6222 -- attribute because (if present) it was already inherited by the
6223 -- anonymous entity (Anon). Thus, in case of components we do not
6224 -- inherit this attribute.
6225
6226 if Nkind (N) = N_Parameter_Specification then
6227 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6228 Set_Etype (Defining_Identifier (N), Anon);
6229 Set_Null_Exclusion_Present (N, False);
6230
6231 elsif Nkind (N) = N_Object_Declaration then
6232 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6233 Set_Etype (Defining_Identifier (N), Anon);
6234
6235 elsif Nkind (N) = N_Access_Function_Definition then
6236 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6237
6238 elsif Nkind (N) = N_Function_Specification then
6239 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6240 Set_Etype (Defining_Unit_Name (N), Anon);
6241
6242 else
6243 Rewrite (Comp,
6244 Make_Component_Definition (Loc,
6245 Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
6246 end if;
6247
6248 Mark_Rewrite_Insertion (Comp);
6249
6250 if Nkind_In (N, N_Object_Declaration, N_Access_Function_Definition)
6251 or else (Nkind (Parent (N)) = N_Full_Type_Declaration
6252 and then not Is_Type (Current_Scope))
6253 then
6254
6255 -- Declaration can be analyzed in the current scope.
6256
6257 Analyze (Decl);
6258
6259 else
6260 -- Temporarily remove the current scope (record or subprogram) from
6261 -- the stack to add the new declarations to the enclosing scope.
6262 -- The anonymous entity is an Itype with the proper attributes.
6263
6264 Scope_Stack.Decrement_Last;
6265 Analyze (Decl);
6266 Set_Is_Itype (Anon);
6267 Set_Associated_Node_For_Itype (Anon, N);
6268 Scope_Stack.Append (Curr_Scope);
6269 end if;
6270
6271 Set_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
6272 Set_Can_Use_Internal_Rep (Anon, not Always_Compatible_Rep_On_Target);
6273 return Anon;
6274 end Replace_Anonymous_Access_To_Protected_Subprogram;
6275
6276 -------------------------------
6277 -- Build_Derived_Access_Type --
6278 -------------------------------
6279
6280 procedure Build_Derived_Access_Type
6281 (N : Node_Id;
6282 Parent_Type : Entity_Id;
6283 Derived_Type : Entity_Id)
6284 is
6285 S : constant Node_Id := Subtype_Indication (Type_Definition (N));
6286
6287 Desig_Type : Entity_Id;
6288 Discr : Entity_Id;
6289 Discr_Con_Elist : Elist_Id;
6290 Discr_Con_El : Elmt_Id;
6291 Subt : Entity_Id;
6292
6293 begin
6294 -- Set the designated type so it is available in case this is an access
6295 -- to a self-referential type, e.g. a standard list type with a next
6296 -- pointer. Will be reset after subtype is built.
6297
6298 Set_Directly_Designated_Type
6299 (Derived_Type, Designated_Type (Parent_Type));
6300
6301 Subt := Process_Subtype (S, N);
6302
6303 if Nkind (S) /= N_Subtype_Indication
6304 and then Subt /= Base_Type (Subt)
6305 then
6306 Set_Ekind (Derived_Type, E_Access_Subtype);
6307 end if;
6308
6309 if Ekind (Derived_Type) = E_Access_Subtype then
6310 declare
6311 Pbase : constant Entity_Id := Base_Type (Parent_Type);
6312 Ibase : constant Entity_Id :=
6313 Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
6314 Svg_Chars : constant Name_Id := Chars (Ibase);
6315 Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
6316
6317 begin
6318 Copy_Node (Pbase, Ibase);
6319
6320 -- Restore Itype status after Copy_Node
6321
6322 Set_Is_Itype (Ibase);
6323 Set_Associated_Node_For_Itype (Ibase, N);
6324
6325 Set_Chars (Ibase, Svg_Chars);
6326 Set_Next_Entity (Ibase, Svg_Next_E);
6327 Set_Sloc (Ibase, Sloc (Derived_Type));
6328 Set_Scope (Ibase, Scope (Derived_Type));
6329 Set_Freeze_Node (Ibase, Empty);
6330 Set_Is_Frozen (Ibase, False);
6331 Set_Comes_From_Source (Ibase, False);
6332 Set_Is_First_Subtype (Ibase, False);
6333
6334 Set_Etype (Ibase, Pbase);
6335 Set_Etype (Derived_Type, Ibase);
6336 end;
6337 end if;
6338
6339 Set_Directly_Designated_Type
6340 (Derived_Type, Designated_Type (Subt));
6341
6342 Set_Is_Constrained (Derived_Type, Is_Constrained (Subt));
6343 Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
6344 Set_Size_Info (Derived_Type, Parent_Type);
6345 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
6346 Set_Depends_On_Private (Derived_Type,
6347 Has_Private_Component (Derived_Type));
6348 Conditional_Delay (Derived_Type, Subt);
6349
6350 -- Ada 2005 (AI-231): Set the null-exclusion attribute, and verify
6351 -- that it is not redundant.
6352
6353 if Null_Exclusion_Present (Type_Definition (N)) then
6354 Set_Can_Never_Be_Null (Derived_Type);
6355
6356 elsif Can_Never_Be_Null (Parent_Type) then
6357 Set_Can_Never_Be_Null (Derived_Type);
6358 end if;
6359
6360 -- Note: we do not copy the Storage_Size_Variable, since we always go to
6361 -- the root type for this information.
6362
6363 -- Apply range checks to discriminants for derived record case
6364 -- ??? THIS CODE SHOULD NOT BE HERE REALLY.
6365
6366 Desig_Type := Designated_Type (Derived_Type);
6367
6368 if Is_Composite_Type (Desig_Type)
6369 and then (not Is_Array_Type (Desig_Type))
6370 and then Has_Discriminants (Desig_Type)
6371 and then Base_Type (Desig_Type) /= Desig_Type
6372 then
6373 Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
6374 Discr_Con_El := First_Elmt (Discr_Con_Elist);
6375
6376 Discr := First_Discriminant (Base_Type (Desig_Type));
6377 while Present (Discr_Con_El) loop
6378 Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
6379 Next_Elmt (Discr_Con_El);
6380 Next_Discriminant (Discr);
6381 end loop;
6382 end if;
6383 end Build_Derived_Access_Type;
6384
6385 ------------------------------
6386 -- Build_Derived_Array_Type --
6387 ------------------------------
6388
6389 procedure Build_Derived_Array_Type
6390 (N : Node_Id;
6391 Parent_Type : Entity_Id;
6392 Derived_Type : Entity_Id)
6393 is
6394 Loc : constant Source_Ptr := Sloc (N);
6395 Tdef : constant Node_Id := Type_Definition (N);
6396 Indic : constant Node_Id := Subtype_Indication (Tdef);
6397 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
6398 Implicit_Base : Entity_Id;
6399 New_Indic : Node_Id;
6400
6401 procedure Make_Implicit_Base;
6402 -- If the parent subtype is constrained, the derived type is a subtype
6403 -- of an implicit base type derived from the parent base.
6404
6405 ------------------------
6406 -- Make_Implicit_Base --
6407 ------------------------
6408
6409 procedure Make_Implicit_Base is
6410 begin
6411 Implicit_Base :=
6412 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
6413
6414 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
6415 Set_Etype (Implicit_Base, Parent_Base);
6416
6417 Copy_Array_Subtype_Attributes (Implicit_Base, Parent_Base);
6418 Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
6419
6420 Set_Has_Delayed_Freeze (Implicit_Base, True);
6421 end Make_Implicit_Base;
6422
6423 -- Start of processing for Build_Derived_Array_Type
6424
6425 begin
6426 if not Is_Constrained (Parent_Type) then
6427 if Nkind (Indic) /= N_Subtype_Indication then
6428 Set_Ekind (Derived_Type, E_Array_Type);
6429
6430 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6431 Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
6432
6433 Set_Has_Delayed_Freeze (Derived_Type, True);
6434
6435 else
6436 Make_Implicit_Base;
6437 Set_Etype (Derived_Type, Implicit_Base);
6438
6439 New_Indic :=
6440 Make_Subtype_Declaration (Loc,
6441 Defining_Identifier => Derived_Type,
6442 Subtype_Indication =>
6443 Make_Subtype_Indication (Loc,
6444 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
6445 Constraint => Constraint (Indic)));
6446
6447 Rewrite (N, New_Indic);
6448 Analyze (N);
6449 end if;
6450
6451 else
6452 if Nkind (Indic) /= N_Subtype_Indication then
6453 Make_Implicit_Base;
6454
6455 Set_Ekind (Derived_Type, Ekind (Parent_Type));
6456 Set_Etype (Derived_Type, Implicit_Base);
6457 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6458
6459 else
6460 Error_Msg_N ("illegal constraint on constrained type", Indic);
6461 end if;
6462 end if;
6463
6464 -- If parent type is not a derived type itself, and is declared in
6465 -- closed scope (e.g. a subprogram), then we must explicitly introduce
6466 -- the new type's concatenation operator since Derive_Subprograms
6467 -- will not inherit the parent's operator. If the parent type is
6468 -- unconstrained, the operator is of the unconstrained base type.
6469
6470 if Number_Dimensions (Parent_Type) = 1
6471 and then not Is_Limited_Type (Parent_Type)
6472 and then not Is_Derived_Type (Parent_Type)
6473 and then not Is_Package_Or_Generic_Package
6474 (Scope (Base_Type (Parent_Type)))
6475 then
6476 if not Is_Constrained (Parent_Type)
6477 and then Is_Constrained (Derived_Type)
6478 then
6479 New_Concatenation_Op (Implicit_Base);
6480 else
6481 New_Concatenation_Op (Derived_Type);
6482 end if;
6483 end if;
6484 end Build_Derived_Array_Type;
6485
6486 -----------------------------------
6487 -- Build_Derived_Concurrent_Type --
6488 -----------------------------------
6489
6490 procedure Build_Derived_Concurrent_Type
6491 (N : Node_Id;
6492 Parent_Type : Entity_Id;
6493 Derived_Type : Entity_Id)
6494 is
6495 Loc : constant Source_Ptr := Sloc (N);
6496
6497 Corr_Record : constant Entity_Id := Make_Temporary (Loc, 'C');
6498 Corr_Decl : Node_Id;
6499 Corr_Decl_Needed : Boolean;
6500 -- If the derived type has fewer discriminants than its parent, the
6501 -- corresponding record is also a derived type, in order to account for
6502 -- the bound discriminants. We create a full type declaration for it in
6503 -- this case.
6504
6505 Constraint_Present : constant Boolean :=
6506 Nkind (Subtype_Indication (Type_Definition (N))) =
6507 N_Subtype_Indication;
6508
6509 D_Constraint : Node_Id;
6510 New_Constraint : Elist_Id;
6511 Old_Disc : Entity_Id;
6512 New_Disc : Entity_Id;
6513 New_N : Node_Id;
6514
6515 begin
6516 Set_Stored_Constraint (Derived_Type, No_Elist);
6517 Corr_Decl_Needed := False;
6518 Old_Disc := Empty;
6519
6520 if Present (Discriminant_Specifications (N))
6521 and then Constraint_Present
6522 then
6523 Old_Disc := First_Discriminant (Parent_Type);
6524 New_Disc := First (Discriminant_Specifications (N));
6525 while Present (New_Disc) and then Present (Old_Disc) loop
6526 Next_Discriminant (Old_Disc);
6527 Next (New_Disc);
6528 end loop;
6529 end if;
6530
6531 if Present (Old_Disc) and then Expander_Active then
6532
6533 -- The new type has fewer discriminants, so we need to create a new
6534 -- corresponding record, which is derived from the corresponding
6535 -- record of the parent, and has a stored constraint that captures
6536 -- the values of the discriminant constraints. The corresponding
6537 -- record is needed only if expander is active and code generation is
6538 -- enabled.
6539
6540 -- The type declaration for the derived corresponding record has the
6541 -- same discriminant part and constraints as the current declaration.
6542 -- Copy the unanalyzed tree to build declaration.
6543
6544 Corr_Decl_Needed := True;
6545 New_N := Copy_Separate_Tree (N);
6546
6547 Corr_Decl :=
6548 Make_Full_Type_Declaration (Loc,
6549 Defining_Identifier => Corr_Record,
6550 Discriminant_Specifications =>
6551 Discriminant_Specifications (New_N),
6552 Type_Definition =>
6553 Make_Derived_Type_Definition (Loc,
6554 Subtype_Indication =>
6555 Make_Subtype_Indication (Loc,
6556 Subtype_Mark =>
6557 New_Occurrence_Of
6558 (Corresponding_Record_Type (Parent_Type), Loc),
6559 Constraint =>
6560 Constraint
6561 (Subtype_Indication (Type_Definition (New_N))))));
6562 end if;
6563
6564 -- Copy Storage_Size and Relative_Deadline variables if task case
6565
6566 if Is_Task_Type (Parent_Type) then
6567 Set_Storage_Size_Variable (Derived_Type,
6568 Storage_Size_Variable (Parent_Type));
6569 Set_Relative_Deadline_Variable (Derived_Type,
6570 Relative_Deadline_Variable (Parent_Type));
6571 end if;
6572
6573 if Present (Discriminant_Specifications (N)) then
6574 Push_Scope (Derived_Type);
6575 Check_Or_Process_Discriminants (N, Derived_Type);
6576
6577 if Constraint_Present then
6578 New_Constraint :=
6579 Expand_To_Stored_Constraint
6580 (Parent_Type,
6581 Build_Discriminant_Constraints
6582 (Parent_Type,
6583 Subtype_Indication (Type_Definition (N)), True));
6584 end if;
6585
6586 End_Scope;
6587
6588 elsif Constraint_Present then
6589
6590 -- Build constrained subtype, copying the constraint, and derive
6591 -- from it to create a derived constrained type.
6592
6593 declare
6594 Loc : constant Source_Ptr := Sloc (N);
6595 Anon : constant Entity_Id :=
6596 Make_Defining_Identifier (Loc,
6597 Chars => New_External_Name (Chars (Derived_Type), 'T'));
6598 Decl : Node_Id;
6599
6600 begin
6601 Decl :=
6602 Make_Subtype_Declaration (Loc,
6603 Defining_Identifier => Anon,
6604 Subtype_Indication =>
6605 New_Copy_Tree (Subtype_Indication (Type_Definition (N))));
6606 Insert_Before (N, Decl);
6607 Analyze (Decl);
6608
6609 Rewrite (Subtype_Indication (Type_Definition (N)),
6610 New_Occurrence_Of (Anon, Loc));
6611 Set_Analyzed (Derived_Type, False);
6612 Analyze (N);
6613 return;
6614 end;
6615 end if;
6616
6617 -- By default, operations and private data are inherited from parent.
6618 -- However, in the presence of bound discriminants, a new corresponding
6619 -- record will be created, see below.
6620
6621 Set_Has_Discriminants
6622 (Derived_Type, Has_Discriminants (Parent_Type));
6623 Set_Corresponding_Record_Type
6624 (Derived_Type, Corresponding_Record_Type (Parent_Type));
6625
6626 -- Is_Constrained is set according the parent subtype, but is set to
6627 -- False if the derived type is declared with new discriminants.
6628
6629 Set_Is_Constrained
6630 (Derived_Type,
6631 (Is_Constrained (Parent_Type) or else Constraint_Present)
6632 and then not Present (Discriminant_Specifications (N)));
6633
6634 if Constraint_Present then
6635 if not Has_Discriminants (Parent_Type) then
6636 Error_Msg_N ("untagged parent must have discriminants", N);
6637
6638 elsif Present (Discriminant_Specifications (N)) then
6639
6640 -- Verify that new discriminants are used to constrain old ones
6641
6642 D_Constraint :=
6643 First
6644 (Constraints
6645 (Constraint (Subtype_Indication (Type_Definition (N)))));
6646
6647 Old_Disc := First_Discriminant (Parent_Type);
6648
6649 while Present (D_Constraint) loop
6650 if Nkind (D_Constraint) /= N_Discriminant_Association then
6651
6652 -- Positional constraint. If it is a reference to a new
6653 -- discriminant, it constrains the corresponding old one.
6654
6655 if Nkind (D_Constraint) = N_Identifier then
6656 New_Disc := First_Discriminant (Derived_Type);
6657 while Present (New_Disc) loop
6658 exit when Chars (New_Disc) = Chars (D_Constraint);
6659 Next_Discriminant (New_Disc);
6660 end loop;
6661
6662 if Present (New_Disc) then
6663 Set_Corresponding_Discriminant (New_Disc, Old_Disc);
6664 end if;
6665 end if;
6666
6667 Next_Discriminant (Old_Disc);
6668
6669 -- if this is a named constraint, search by name for the old
6670 -- discriminants constrained by the new one.
6671
6672 elsif Nkind (Expression (D_Constraint)) = N_Identifier then
6673
6674 -- Find new discriminant with that name
6675
6676 New_Disc := First_Discriminant (Derived_Type);
6677 while Present (New_Disc) loop
6678 exit when
6679 Chars (New_Disc) = Chars (Expression (D_Constraint));
6680 Next_Discriminant (New_Disc);
6681 end loop;
6682
6683 if Present (New_Disc) then
6684
6685 -- Verify that new discriminant renames some discriminant
6686 -- of the parent type, and associate the new discriminant
6687 -- with one or more old ones that it renames.
6688
6689 declare
6690 Selector : Node_Id;
6691
6692 begin
6693 Selector := First (Selector_Names (D_Constraint));
6694 while Present (Selector) loop
6695 Old_Disc := First_Discriminant (Parent_Type);
6696 while Present (Old_Disc) loop
6697 exit when Chars (Old_Disc) = Chars (Selector);
6698 Next_Discriminant (Old_Disc);
6699 end loop;
6700
6701 if Present (Old_Disc) then
6702 Set_Corresponding_Discriminant
6703 (New_Disc, Old_Disc);
6704 end if;
6705
6706 Next (Selector);
6707 end loop;
6708 end;
6709 end if;
6710 end if;
6711
6712 Next (D_Constraint);
6713 end loop;
6714
6715 New_Disc := First_Discriminant (Derived_Type);
6716 while Present (New_Disc) loop
6717 if No (Corresponding_Discriminant (New_Disc)) then
6718 Error_Msg_NE
6719 ("new discriminant& must constrain old one", N, New_Disc);
6720
6721 elsif not
6722 Subtypes_Statically_Compatible
6723 (Etype (New_Disc),
6724 Etype (Corresponding_Discriminant (New_Disc)))
6725 then
6726 Error_Msg_NE
6727 ("& not statically compatible with parent discriminant",
6728 N, New_Disc);
6729 end if;
6730
6731 Next_Discriminant (New_Disc);
6732 end loop;
6733 end if;
6734
6735 elsif Present (Discriminant_Specifications (N)) then
6736 Error_Msg_N
6737 ("missing discriminant constraint in untagged derivation", N);
6738 end if;
6739
6740 -- The entity chain of the derived type includes the new discriminants
6741 -- but shares operations with the parent.
6742
6743 if Present (Discriminant_Specifications (N)) then
6744 Old_Disc := First_Discriminant (Parent_Type);
6745 while Present (Old_Disc) loop
6746 if No (Next_Entity (Old_Disc))
6747 or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
6748 then
6749 Set_Next_Entity
6750 (Last_Entity (Derived_Type), Next_Entity (Old_Disc));
6751 exit;
6752 end if;
6753
6754 Next_Discriminant (Old_Disc);
6755 end loop;
6756
6757 else
6758 Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
6759 if Has_Discriminants (Parent_Type) then
6760 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
6761 Set_Discriminant_Constraint (
6762 Derived_Type, Discriminant_Constraint (Parent_Type));
6763 end if;
6764 end if;
6765
6766 Set_Last_Entity (Derived_Type, Last_Entity (Parent_Type));
6767
6768 Set_Has_Completion (Derived_Type);
6769
6770 if Corr_Decl_Needed then
6771 Set_Stored_Constraint (Derived_Type, New_Constraint);
6772 Insert_After (N, Corr_Decl);
6773 Analyze (Corr_Decl);
6774 Set_Corresponding_Record_Type (Derived_Type, Corr_Record);
6775 end if;
6776 end Build_Derived_Concurrent_Type;
6777
6778 ------------------------------------
6779 -- Build_Derived_Enumeration_Type --
6780 ------------------------------------
6781
6782 procedure Build_Derived_Enumeration_Type
6783 (N : Node_Id;
6784 Parent_Type : Entity_Id;
6785 Derived_Type : Entity_Id)
6786 is
6787 Loc : constant Source_Ptr := Sloc (N);
6788 Def : constant Node_Id := Type_Definition (N);
6789 Indic : constant Node_Id := Subtype_Indication (Def);
6790 Implicit_Base : Entity_Id;
6791 Literal : Entity_Id;
6792 New_Lit : Entity_Id;
6793 Literals_List : List_Id;
6794 Type_Decl : Node_Id;
6795 Hi, Lo : Node_Id;
6796 Rang_Expr : Node_Id;
6797
6798 begin
6799 -- Since types Standard.Character and Standard.[Wide_]Wide_Character do
6800 -- not have explicit literals lists we need to process types derived
6801 -- from them specially. This is handled by Derived_Standard_Character.
6802 -- If the parent type is a generic type, there are no literals either,
6803 -- and we construct the same skeletal representation as for the generic
6804 -- parent type.
6805
6806 if Is_Standard_Character_Type (Parent_Type) then
6807 Derived_Standard_Character (N, Parent_Type, Derived_Type);
6808
6809 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
6810 declare
6811 Lo : Node_Id;
6812 Hi : Node_Id;
6813
6814 begin
6815 if Nkind (Indic) /= N_Subtype_Indication then
6816 Lo :=
6817 Make_Attribute_Reference (Loc,
6818 Attribute_Name => Name_First,
6819 Prefix => New_Occurrence_Of (Derived_Type, Loc));
6820 Set_Etype (Lo, Derived_Type);
6821
6822 Hi :=
6823 Make_Attribute_Reference (Loc,
6824 Attribute_Name => Name_Last,
6825 Prefix => New_Occurrence_Of (Derived_Type, Loc));
6826 Set_Etype (Hi, Derived_Type);
6827
6828 Set_Scalar_Range (Derived_Type,
6829 Make_Range (Loc,
6830 Low_Bound => Lo,
6831 High_Bound => Hi));
6832 else
6833
6834 -- Analyze subtype indication and verify compatibility
6835 -- with parent type.
6836
6837 if Base_Type (Process_Subtype (Indic, N)) /=
6838 Base_Type (Parent_Type)
6839 then
6840 Error_Msg_N
6841 ("illegal constraint for formal discrete type", N);
6842 end if;
6843 end if;
6844 end;
6845
6846 else
6847 -- If a constraint is present, analyze the bounds to catch
6848 -- premature usage of the derived literals.
6849
6850 if Nkind (Indic) = N_Subtype_Indication
6851 and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
6852 then
6853 Analyze (Low_Bound (Range_Expression (Constraint (Indic))));
6854 Analyze (High_Bound (Range_Expression (Constraint (Indic))));
6855 end if;
6856
6857 -- Introduce an implicit base type for the derived type even if there
6858 -- is no constraint attached to it, since this seems closer to the
6859 -- Ada semantics. Build a full type declaration tree for the derived
6860 -- type using the implicit base type as the defining identifier. The
6861 -- build a subtype declaration tree which applies the constraint (if
6862 -- any) have it replace the derived type declaration.
6863
6864 Literal := First_Literal (Parent_Type);
6865 Literals_List := New_List;
6866 while Present (Literal)
6867 and then Ekind (Literal) = E_Enumeration_Literal
6868 loop
6869 -- Literals of the derived type have the same representation as
6870 -- those of the parent type, but this representation can be
6871 -- overridden by an explicit representation clause. Indicate
6872 -- that there is no explicit representation given yet. These
6873 -- derived literals are implicit operations of the new type,
6874 -- and can be overridden by explicit ones.
6875
6876 if Nkind (Literal) = N_Defining_Character_Literal then
6877 New_Lit :=
6878 Make_Defining_Character_Literal (Loc, Chars (Literal));
6879 else
6880 New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
6881 end if;
6882
6883 Set_Ekind (New_Lit, E_Enumeration_Literal);
6884 Set_Enumeration_Pos (New_Lit, Enumeration_Pos (Literal));
6885 Set_Enumeration_Rep (New_Lit, Enumeration_Rep (Literal));
6886 Set_Enumeration_Rep_Expr (New_Lit, Empty);
6887 Set_Alias (New_Lit, Literal);
6888 Set_Is_Known_Valid (New_Lit, True);
6889
6890 Append (New_Lit, Literals_List);
6891 Next_Literal (Literal);
6892 end loop;
6893
6894 Implicit_Base :=
6895 Make_Defining_Identifier (Sloc (Derived_Type),
6896 Chars => New_External_Name (Chars (Derived_Type), 'B'));
6897
6898 -- Indicate the proper nature of the derived type. This must be done
6899 -- before analysis of the literals, to recognize cases when a literal
6900 -- may be hidden by a previous explicit function definition (cf.
6901 -- c83031a).
6902
6903 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
6904 Set_Etype (Derived_Type, Implicit_Base);
6905
6906 Type_Decl :=
6907 Make_Full_Type_Declaration (Loc,
6908 Defining_Identifier => Implicit_Base,
6909 Discriminant_Specifications => No_List,
6910 Type_Definition =>
6911 Make_Enumeration_Type_Definition (Loc, Literals_List));
6912
6913 Mark_Rewrite_Insertion (Type_Decl);
6914 Insert_Before (N, Type_Decl);
6915 Analyze (Type_Decl);
6916
6917 -- The anonymous base now has a full declaration, but this base
6918 -- is not a first subtype.
6919
6920 Set_Is_First_Subtype (Implicit_Base, False);
6921
6922 -- After the implicit base is analyzed its Etype needs to be changed
6923 -- to reflect the fact that it is derived from the parent type which
6924 -- was ignored during analysis. We also set the size at this point.
6925
6926 Set_Etype (Implicit_Base, Parent_Type);
6927
6928 Set_Size_Info (Implicit_Base, Parent_Type);
6929 Set_RM_Size (Implicit_Base, RM_Size (Parent_Type));
6930 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
6931
6932 -- Copy other flags from parent type
6933
6934 Set_Has_Non_Standard_Rep
6935 (Implicit_Base, Has_Non_Standard_Rep
6936 (Parent_Type));
6937 Set_Has_Pragma_Ordered
6938 (Implicit_Base, Has_Pragma_Ordered
6939 (Parent_Type));
6940 Set_Has_Delayed_Freeze (Implicit_Base);
6941
6942 -- Process the subtype indication including a validation check on the
6943 -- constraint, if any. If a constraint is given, its bounds must be
6944 -- implicitly converted to the new type.
6945
6946 if Nkind (Indic) = N_Subtype_Indication then
6947 declare
6948 R : constant Node_Id :=
6949 Range_Expression (Constraint (Indic));
6950
6951 begin
6952 if Nkind (R) = N_Range then
6953 Hi := Build_Scalar_Bound
6954 (High_Bound (R), Parent_Type, Implicit_Base);
6955 Lo := Build_Scalar_Bound
6956 (Low_Bound (R), Parent_Type, Implicit_Base);
6957
6958 else
6959 -- Constraint is a Range attribute. Replace with explicit
6960 -- mention of the bounds of the prefix, which must be a
6961 -- subtype.
6962
6963 Analyze (Prefix (R));
6964 Hi :=
6965 Convert_To (Implicit_Base,
6966 Make_Attribute_Reference (Loc,
6967 Attribute_Name => Name_Last,
6968 Prefix =>
6969 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
6970
6971 Lo :=
6972 Convert_To (Implicit_Base,
6973 Make_Attribute_Reference (Loc,
6974 Attribute_Name => Name_First,
6975 Prefix =>
6976 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
6977 end if;
6978 end;
6979
6980 else
6981 Hi :=
6982 Build_Scalar_Bound
6983 (Type_High_Bound (Parent_Type),
6984 Parent_Type, Implicit_Base);
6985 Lo :=
6986 Build_Scalar_Bound
6987 (Type_Low_Bound (Parent_Type),
6988 Parent_Type, Implicit_Base);
6989 end if;
6990
6991 Rang_Expr :=
6992 Make_Range (Loc,
6993 Low_Bound => Lo,
6994 High_Bound => Hi);
6995
6996 -- If we constructed a default range for the case where no range
6997 -- was given, then the expressions in the range must not freeze
6998 -- since they do not correspond to expressions in the source.
6999 -- However, if the type inherits predicates the expressions will
7000 -- be elaborated earlier and must freeze.
7001
7002 if Nkind (Indic) /= N_Subtype_Indication
7003 and then not Has_Predicates (Derived_Type)
7004 then
7005 Set_Must_Not_Freeze (Lo);
7006 Set_Must_Not_Freeze (Hi);
7007 Set_Must_Not_Freeze (Rang_Expr);
7008 end if;
7009
7010 Rewrite (N,
7011 Make_Subtype_Declaration (Loc,
7012 Defining_Identifier => Derived_Type,
7013 Subtype_Indication =>
7014 Make_Subtype_Indication (Loc,
7015 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
7016 Constraint =>
7017 Make_Range_Constraint (Loc,
7018 Range_Expression => Rang_Expr))));
7019
7020 Analyze (N);
7021
7022 -- Propagate the aspects from the original type declaration to the
7023 -- declaration of the implicit base.
7024
7025 Move_Aspects (From => Original_Node (N), To => Type_Decl);
7026
7027 -- Apply a range check. Since this range expression doesn't have an
7028 -- Etype, we have to specifically pass the Source_Typ parameter. Is
7029 -- this right???
7030
7031 if Nkind (Indic) = N_Subtype_Indication then
7032 Apply_Range_Check
7033 (Range_Expression (Constraint (Indic)), Parent_Type,
7034 Source_Typ => Entity (Subtype_Mark (Indic)));
7035 end if;
7036 end if;
7037 end Build_Derived_Enumeration_Type;
7038
7039 --------------------------------
7040 -- Build_Derived_Numeric_Type --
7041 --------------------------------
7042
7043 procedure Build_Derived_Numeric_Type
7044 (N : Node_Id;
7045 Parent_Type : Entity_Id;
7046 Derived_Type : Entity_Id)
7047 is
7048 Loc : constant Source_Ptr := Sloc (N);
7049 Tdef : constant Node_Id := Type_Definition (N);
7050 Indic : constant Node_Id := Subtype_Indication (Tdef);
7051 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
7052 No_Constraint : constant Boolean := Nkind (Indic) /=
7053 N_Subtype_Indication;
7054 Implicit_Base : Entity_Id;
7055
7056 Lo : Node_Id;
7057 Hi : Node_Id;
7058
7059 begin
7060 -- Process the subtype indication including a validation check on
7061 -- the constraint if any.
7062
7063 Discard_Node (Process_Subtype (Indic, N));
7064
7065 -- Introduce an implicit base type for the derived type even if there
7066 -- is no constraint attached to it, since this seems closer to the Ada
7067 -- semantics.
7068
7069 Implicit_Base :=
7070 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
7071
7072 Set_Etype (Implicit_Base, Parent_Base);
7073 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
7074 Set_Size_Info (Implicit_Base, Parent_Base);
7075 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
7076 Set_Parent (Implicit_Base, Parent (Derived_Type));
7077 Set_Is_Known_Valid (Implicit_Base, Is_Known_Valid (Parent_Base));
7078
7079 -- Set RM Size for discrete type or decimal fixed-point type
7080 -- Ordinary fixed-point is excluded, why???
7081
7082 if Is_Discrete_Type (Parent_Base)
7083 or else Is_Decimal_Fixed_Point_Type (Parent_Base)
7084 then
7085 Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
7086 end if;
7087
7088 Set_Has_Delayed_Freeze (Implicit_Base);
7089
7090 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
7091 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
7092
7093 Set_Scalar_Range (Implicit_Base,
7094 Make_Range (Loc,
7095 Low_Bound => Lo,
7096 High_Bound => Hi));
7097
7098 if Has_Infinities (Parent_Base) then
7099 Set_Includes_Infinities (Scalar_Range (Implicit_Base));
7100 end if;
7101
7102 -- The Derived_Type, which is the entity of the declaration, is a
7103 -- subtype of the implicit base. Its Ekind is a subtype, even in the
7104 -- absence of an explicit constraint.
7105
7106 Set_Etype (Derived_Type, Implicit_Base);
7107
7108 -- If we did not have a constraint, then the Ekind is set from the
7109 -- parent type (otherwise Process_Subtype has set the bounds)
7110
7111 if No_Constraint then
7112 Set_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
7113 end if;
7114
7115 -- If we did not have a range constraint, then set the range from the
7116 -- parent type. Otherwise, the Process_Subtype call has set the bounds.
7117
7118 if No_Constraint or else not Has_Range_Constraint (Indic) then
7119 Set_Scalar_Range (Derived_Type,
7120 Make_Range (Loc,
7121 Low_Bound => New_Copy_Tree (Type_Low_Bound (Parent_Type)),
7122 High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
7123 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7124
7125 if Has_Infinities (Parent_Type) then
7126 Set_Includes_Infinities (Scalar_Range (Derived_Type));
7127 end if;
7128
7129 Set_Is_Known_Valid (Derived_Type, Is_Known_Valid (Parent_Type));
7130 end if;
7131
7132 Set_Is_Descendant_Of_Address (Derived_Type,
7133 Is_Descendant_Of_Address (Parent_Type));
7134 Set_Is_Descendant_Of_Address (Implicit_Base,
7135 Is_Descendant_Of_Address (Parent_Type));
7136
7137 -- Set remaining type-specific fields, depending on numeric type
7138
7139 if Is_Modular_Integer_Type (Parent_Type) then
7140 Set_Modulus (Implicit_Base, Modulus (Parent_Base));
7141
7142 Set_Non_Binary_Modulus
7143 (Implicit_Base, Non_Binary_Modulus (Parent_Base));
7144
7145 Set_Is_Known_Valid
7146 (Implicit_Base, Is_Known_Valid (Parent_Base));
7147
7148 elsif Is_Floating_Point_Type (Parent_Type) then
7149
7150 -- Digits of base type is always copied from the digits value of
7151 -- the parent base type, but the digits of the derived type will
7152 -- already have been set if there was a constraint present.
7153
7154 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
7155 Set_Float_Rep (Implicit_Base, Float_Rep (Parent_Base));
7156
7157 if No_Constraint then
7158 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
7159 end if;
7160
7161 elsif Is_Fixed_Point_Type (Parent_Type) then
7162
7163 -- Small of base type and derived type are always copied from the
7164 -- parent base type, since smalls never change. The delta of the
7165 -- base type is also copied from the parent base type. However the
7166 -- delta of the derived type will have been set already if a
7167 -- constraint was present.
7168
7169 Set_Small_Value (Derived_Type, Small_Value (Parent_Base));
7170 Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
7171 Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
7172
7173 if No_Constraint then
7174 Set_Delta_Value (Derived_Type, Delta_Value (Parent_Type));
7175 end if;
7176
7177 -- The scale and machine radix in the decimal case are always
7178 -- copied from the parent base type.
7179
7180 if Is_Decimal_Fixed_Point_Type (Parent_Type) then
7181 Set_Scale_Value (Derived_Type, Scale_Value (Parent_Base));
7182 Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
7183
7184 Set_Machine_Radix_10
7185 (Derived_Type, Machine_Radix_10 (Parent_Base));
7186 Set_Machine_Radix_10
7187 (Implicit_Base, Machine_Radix_10 (Parent_Base));
7188
7189 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
7190
7191 if No_Constraint then
7192 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
7193
7194 else
7195 -- the analysis of the subtype_indication sets the
7196 -- digits value of the derived type.
7197
7198 null;
7199 end if;
7200 end if;
7201 end if;
7202
7203 if Is_Integer_Type (Parent_Type) then
7204 Set_Has_Shift_Operator
7205 (Implicit_Base, Has_Shift_Operator (Parent_Type));
7206 end if;
7207
7208 -- The type of the bounds is that of the parent type, and they
7209 -- must be converted to the derived type.
7210
7211 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
7212
7213 -- The implicit_base should be frozen when the derived type is frozen,
7214 -- but note that it is used in the conversions of the bounds. For fixed
7215 -- types we delay the determination of the bounds until the proper
7216 -- freezing point. For other numeric types this is rejected by GCC, for
7217 -- reasons that are currently unclear (???), so we choose to freeze the
7218 -- implicit base now. In the case of integers and floating point types
7219 -- this is harmless because subsequent representation clauses cannot
7220 -- affect anything, but it is still baffling that we cannot use the
7221 -- same mechanism for all derived numeric types.
7222
7223 -- There is a further complication: actually some representation
7224 -- clauses can affect the implicit base type. For example, attribute
7225 -- definition clauses for stream-oriented attributes need to set the
7226 -- corresponding TSS entries on the base type, and this normally
7227 -- cannot be done after the base type is frozen, so the circuitry in
7228 -- Sem_Ch13.New_Stream_Subprogram must account for this possibility
7229 -- and not use Set_TSS in this case.
7230
7231 -- There are also consequences for the case of delayed representation
7232 -- aspects for some cases. For example, a Size aspect is delayed and
7233 -- should not be evaluated to the freeze point. This early freezing
7234 -- means that the size attribute evaluation happens too early???
7235
7236 if Is_Fixed_Point_Type (Parent_Type) then
7237 Conditional_Delay (Implicit_Base, Parent_Type);
7238 else
7239 Freeze_Before (N, Implicit_Base);
7240 end if;
7241 end Build_Derived_Numeric_Type;
7242
7243 --------------------------------
7244 -- Build_Derived_Private_Type --
7245 --------------------------------
7246
7247 procedure Build_Derived_Private_Type
7248 (N : Node_Id;
7249 Parent_Type : Entity_Id;
7250 Derived_Type : Entity_Id;
7251 Is_Completion : Boolean;
7252 Derive_Subps : Boolean := True)
7253 is
7254 Loc : constant Source_Ptr := Sloc (N);
7255 Par_Base : constant Entity_Id := Base_Type (Parent_Type);
7256 Par_Scope : constant Entity_Id := Scope (Par_Base);
7257 Full_N : constant Node_Id := New_Copy_Tree (N);
7258 Full_Der : Entity_Id := New_Copy (Derived_Type);
7259 Full_P : Entity_Id;
7260
7261 procedure Build_Full_Derivation;
7262 -- Build full derivation, i.e. derive from the full view
7263
7264 procedure Copy_And_Build;
7265 -- Copy derived type declaration, replace parent with its full view,
7266 -- and build derivation
7267
7268 ---------------------------
7269 -- Build_Full_Derivation --
7270 ---------------------------
7271
7272 procedure Build_Full_Derivation is
7273 begin
7274 -- If parent scope is not open, install the declarations
7275
7276 if not In_Open_Scopes (Par_Scope) then
7277 Install_Private_Declarations (Par_Scope);
7278 Install_Visible_Declarations (Par_Scope);
7279 Copy_And_Build;
7280 Uninstall_Declarations (Par_Scope);
7281
7282 -- If parent scope is open and in another unit, and parent has a
7283 -- completion, then the derivation is taking place in the visible
7284 -- part of a child unit. In that case retrieve the full view of
7285 -- the parent momentarily.
7286
7287 elsif not In_Same_Source_Unit (N, Parent_Type) then
7288 Full_P := Full_View (Parent_Type);
7289 Exchange_Declarations (Parent_Type);
7290 Copy_And_Build;
7291 Exchange_Declarations (Full_P);
7292
7293 -- Otherwise it is a local derivation
7294
7295 else
7296 Copy_And_Build;
7297 end if;
7298 end Build_Full_Derivation;
7299
7300 --------------------
7301 -- Copy_And_Build --
7302 --------------------
7303
7304 procedure Copy_And_Build is
7305 Full_Parent : Entity_Id := Parent_Type;
7306
7307 begin
7308 -- If the parent is itself derived from another private type,
7309 -- installing the private declarations has not affected its
7310 -- privacy status, so use its own full view explicitly.
7311
7312 if Is_Private_Type (Full_Parent)
7313 and then Present (Full_View (Full_Parent))
7314 then
7315 Full_Parent := Full_View (Full_Parent);
7316 end if;
7317
7318 -- And its underlying full view if necessary
7319
7320 if Is_Private_Type (Full_Parent)
7321 and then Present (Underlying_Full_View (Full_Parent))
7322 then
7323 Full_Parent := Underlying_Full_View (Full_Parent);
7324 end if;
7325
7326 -- For record, access and most enumeration types, derivation from
7327 -- the full view requires a fully-fledged declaration. In the other
7328 -- cases, just use an itype.
7329
7330 if Ekind (Full_Parent) in Record_Kind
7331 or else Ekind (Full_Parent) in Access_Kind
7332 or else
7333 (Ekind (Full_Parent) in Enumeration_Kind
7334 and then not Is_Standard_Character_Type (Full_Parent)
7335 and then not Is_Generic_Type (Root_Type (Full_Parent)))
7336 then
7337 -- Copy and adjust declaration to provide a completion for what
7338 -- is originally a private declaration. Indicate that full view
7339 -- is internally generated.
7340
7341 Set_Comes_From_Source (Full_N, False);
7342 Set_Comes_From_Source (Full_Der, False);
7343 Set_Parent (Full_Der, Full_N);
7344 Set_Defining_Identifier (Full_N, Full_Der);
7345
7346 -- If there are no constraints, adjust the subtype mark
7347
7348 if Nkind (Subtype_Indication (Type_Definition (Full_N))) /=
7349 N_Subtype_Indication
7350 then
7351 Set_Subtype_Indication
7352 (Type_Definition (Full_N),
7353 New_Occurrence_Of (Full_Parent, Sloc (Full_N)));
7354 end if;
7355
7356 Insert_After (N, Full_N);
7357
7358 -- Build full view of derived type from full view of parent which
7359 -- is now installed. Subprograms have been derived on the partial
7360 -- view, the completion does not derive them anew.
7361
7362 if Ekind (Full_Parent) in Record_Kind then
7363
7364 -- If parent type is tagged, the completion inherits the proper
7365 -- primitive operations.
7366
7367 if Is_Tagged_Type (Parent_Type) then
7368 Build_Derived_Record_Type
7369 (Full_N, Full_Parent, Full_Der, Derive_Subps);
7370 else
7371 Build_Derived_Record_Type
7372 (Full_N, Full_Parent, Full_Der, Derive_Subps => False);
7373 end if;
7374
7375 else
7376 Build_Derived_Type
7377 (Full_N, Full_Parent, Full_Der,
7378 Is_Completion => False, Derive_Subps => False);
7379 end if;
7380
7381 -- The full declaration has been introduced into the tree and
7382 -- processed in the step above. It should not be analyzed again
7383 -- (when encountered later in the current list of declarations)
7384 -- to prevent spurious name conflicts. The full entity remains
7385 -- invisible.
7386
7387 Set_Analyzed (Full_N);
7388
7389 else
7390 Full_Der :=
7391 Make_Defining_Identifier (Sloc (Derived_Type),
7392 Chars => Chars (Derived_Type));
7393 Set_Is_Itype (Full_Der);
7394 Set_Associated_Node_For_Itype (Full_Der, N);
7395 Set_Parent (Full_Der, N);
7396 Build_Derived_Type
7397 (N, Full_Parent, Full_Der,
7398 Is_Completion => False, Derive_Subps => False);
7399 end if;
7400
7401 Set_Has_Private_Declaration (Full_Der);
7402 Set_Has_Private_Declaration (Derived_Type);
7403
7404 Set_Scope (Full_Der, Scope (Derived_Type));
7405 Set_Is_First_Subtype (Full_Der, Is_First_Subtype (Derived_Type));
7406 Set_Has_Size_Clause (Full_Der, False);
7407 Set_Has_Alignment_Clause (Full_Der, False);
7408 Set_Has_Delayed_Freeze (Full_Der);
7409 Set_Is_Frozen (Full_Der, False);
7410 Set_Freeze_Node (Full_Der, Empty);
7411 Set_Depends_On_Private (Full_Der, Has_Private_Component (Full_Der));
7412 Set_Is_Public (Full_Der, Is_Public (Derived_Type));
7413
7414 -- The convention on the base type may be set in the private part
7415 -- and not propagated to the subtype until later, so we obtain the
7416 -- convention from the base type of the parent.
7417
7418 Set_Convention (Full_Der, Convention (Base_Type (Full_Parent)));
7419 end Copy_And_Build;
7420
7421 -- Start of processing for Build_Derived_Private_Type
7422
7423 begin
7424 if Is_Tagged_Type (Parent_Type) then
7425 Full_P := Full_View (Parent_Type);
7426
7427 -- A type extension of a type with unknown discriminants is an
7428 -- indefinite type that the back-end cannot handle directly.
7429 -- We treat it as a private type, and build a completion that is
7430 -- derived from the full view of the parent, and hopefully has
7431 -- known discriminants.
7432
7433 -- If the full view of the parent type has an underlying record view,
7434 -- use it to generate the underlying record view of this derived type
7435 -- (required for chains of derivations with unknown discriminants).
7436
7437 -- Minor optimization: we avoid the generation of useless underlying
7438 -- record view entities if the private type declaration has unknown
7439 -- discriminants but its corresponding full view has no
7440 -- discriminants.
7441
7442 if Has_Unknown_Discriminants (Parent_Type)
7443 and then Present (Full_P)
7444 and then (Has_Discriminants (Full_P)
7445 or else Present (Underlying_Record_View (Full_P)))
7446 and then not In_Open_Scopes (Par_Scope)
7447 and then Expander_Active
7448 then
7449 declare
7450 Full_Der : constant Entity_Id := Make_Temporary (Loc, 'T');
7451 New_Ext : constant Node_Id :=
7452 Copy_Separate_Tree
7453 (Record_Extension_Part (Type_Definition (N)));
7454 Decl : Node_Id;
7455
7456 begin
7457 Build_Derived_Record_Type
7458 (N, Parent_Type, Derived_Type, Derive_Subps);
7459
7460 -- Build anonymous completion, as a derivation from the full
7461 -- view of the parent. This is not a completion in the usual
7462 -- sense, because the current type is not private.
7463
7464 Decl :=
7465 Make_Full_Type_Declaration (Loc,
7466 Defining_Identifier => Full_Der,
7467 Type_Definition =>
7468 Make_Derived_Type_Definition (Loc,
7469 Subtype_Indication =>
7470 New_Copy_Tree
7471 (Subtype_Indication (Type_Definition (N))),
7472 Record_Extension_Part => New_Ext));
7473
7474 -- If the parent type has an underlying record view, use it
7475 -- here to build the new underlying record view.
7476
7477 if Present (Underlying_Record_View (Full_P)) then
7478 pragma Assert
7479 (Nkind (Subtype_Indication (Type_Definition (Decl)))
7480 = N_Identifier);
7481 Set_Entity (Subtype_Indication (Type_Definition (Decl)),
7482 Underlying_Record_View (Full_P));
7483 end if;
7484
7485 Install_Private_Declarations (Par_Scope);
7486 Install_Visible_Declarations (Par_Scope);
7487 Insert_Before (N, Decl);
7488
7489 -- Mark entity as an underlying record view before analysis,
7490 -- to avoid generating the list of its primitive operations
7491 -- (which is not really required for this entity) and thus
7492 -- prevent spurious errors associated with missing overriding
7493 -- of abstract primitives (overridden only for Derived_Type).
7494
7495 Set_Ekind (Full_Der, E_Record_Type);
7496 Set_Is_Underlying_Record_View (Full_Der);
7497 Set_Default_SSO (Full_Der);
7498
7499 Analyze (Decl);
7500
7501 pragma Assert (Has_Discriminants (Full_Der)
7502 and then not Has_Unknown_Discriminants (Full_Der));
7503
7504 Uninstall_Declarations (Par_Scope);
7505
7506 -- Freeze the underlying record view, to prevent generation of
7507 -- useless dispatching information, which is simply shared with
7508 -- the real derived type.
7509
7510 Set_Is_Frozen (Full_Der);
7511
7512 -- If the derived type has access discriminants, create
7513 -- references to their anonymous types now, to prevent
7514 -- back-end problems when their first use is in generated
7515 -- bodies of primitives.
7516
7517 declare
7518 E : Entity_Id;
7519
7520 begin
7521 E := First_Entity (Full_Der);
7522
7523 while Present (E) loop
7524 if Ekind (E) = E_Discriminant
7525 and then Ekind (Etype (E)) = E_Anonymous_Access_Type
7526 then
7527 Build_Itype_Reference (Etype (E), Decl);
7528 end if;
7529
7530 Next_Entity (E);
7531 end loop;
7532 end;
7533
7534 -- Set up links between real entity and underlying record view
7535
7536 Set_Underlying_Record_View (Derived_Type, Base_Type (Full_Der));
7537 Set_Underlying_Record_View (Base_Type (Full_Der), Derived_Type);
7538 end;
7539
7540 -- If discriminants are known, build derived record
7541
7542 else
7543 Build_Derived_Record_Type
7544 (N, Parent_Type, Derived_Type, Derive_Subps);
7545 end if;
7546
7547 return;
7548
7549 elsif Has_Discriminants (Parent_Type) then
7550
7551 -- Build partial view of derived type from partial view of parent.
7552 -- This must be done before building the full derivation because the
7553 -- second derivation will modify the discriminants of the first and
7554 -- the discriminants are chained with the rest of the components in
7555 -- the full derivation.
7556
7557 Build_Derived_Record_Type
7558 (N, Parent_Type, Derived_Type, Derive_Subps);
7559
7560 -- Build the full derivation if this is not the anonymous derived
7561 -- base type created by Build_Derived_Record_Type in the constrained
7562 -- case (see point 5. of its head comment) since we build it for the
7563 -- derived subtype. And skip it for protected types altogether, as
7564 -- gigi does not use these types directly.
7565
7566 if Present (Full_View (Parent_Type))
7567 and then not Is_Itype (Derived_Type)
7568 and then not (Ekind (Full_View (Parent_Type)) in Protected_Kind)
7569 then
7570 declare
7571 Der_Base : constant Entity_Id := Base_Type (Derived_Type);
7572 Discr : Entity_Id;
7573 Last_Discr : Entity_Id;
7574
7575 begin
7576 -- If this is not a completion, construct the implicit full
7577 -- view by deriving from the full view of the parent type.
7578 -- But if this is a completion, the derived private type
7579 -- being built is a full view and the full derivation can
7580 -- only be its underlying full view.
7581
7582 Build_Full_Derivation;
7583
7584 if not Is_Completion then
7585 Set_Full_View (Derived_Type, Full_Der);
7586 else
7587 Set_Underlying_Full_View (Derived_Type, Full_Der);
7588 Set_Is_Underlying_Full_View (Full_Der);
7589 end if;
7590
7591 if not Is_Base_Type (Derived_Type) then
7592 Set_Full_View (Der_Base, Base_Type (Full_Der));
7593 end if;
7594
7595 -- Copy the discriminant list from full view to the partial
7596 -- view (base type and its subtype). Gigi requires that the
7597 -- partial and full views have the same discriminants.
7598
7599 -- Note that since the partial view points to discriminants
7600 -- in the full view, their scope will be that of the full
7601 -- view. This might cause some front end problems and need
7602 -- adjustment???
7603
7604 Discr := First_Discriminant (Base_Type (Full_Der));
7605 Set_First_Entity (Der_Base, Discr);
7606
7607 loop
7608 Last_Discr := Discr;
7609 Next_Discriminant (Discr);
7610 exit when No (Discr);
7611 end loop;
7612
7613 Set_Last_Entity (Der_Base, Last_Discr);
7614 Set_First_Entity (Derived_Type, First_Entity (Der_Base));
7615 Set_Last_Entity (Derived_Type, Last_Entity (Der_Base));
7616
7617 Set_Stored_Constraint
7618 (Full_Der, Stored_Constraint (Derived_Type));
7619 end;
7620 end if;
7621
7622 elsif Present (Full_View (Parent_Type))
7623 and then Has_Discriminants (Full_View (Parent_Type))
7624 then
7625 if Has_Unknown_Discriminants (Parent_Type)
7626 and then Nkind (Subtype_Indication (Type_Definition (N))) =
7627 N_Subtype_Indication
7628 then
7629 Error_Msg_N
7630 ("cannot constrain type with unknown discriminants",
7631 Subtype_Indication (Type_Definition (N)));
7632 return;
7633 end if;
7634
7635 -- If this is not a completion, construct the implicit full view by
7636 -- deriving from the full view of the parent type. But if this is a
7637 -- completion, the derived private type being built is a full view
7638 -- and the full derivation can only be its underlying full view.
7639
7640 Build_Full_Derivation;
7641
7642 if not Is_Completion then
7643 Set_Full_View (Derived_Type, Full_Der);
7644 else
7645 Set_Underlying_Full_View (Derived_Type, Full_Der);
7646 Set_Is_Underlying_Full_View (Full_Der);
7647 end if;
7648
7649 -- In any case, the primitive operations are inherited from the
7650 -- parent type, not from the internal full view.
7651
7652 Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
7653
7654 if Derive_Subps then
7655 Derive_Subprograms (Parent_Type, Derived_Type);
7656 end if;
7657
7658 Set_Stored_Constraint (Derived_Type, No_Elist);
7659 Set_Is_Constrained
7660 (Derived_Type, Is_Constrained (Full_View (Parent_Type)));
7661
7662 else
7663 -- Untagged type, No discriminants on either view
7664
7665 if Nkind (Subtype_Indication (Type_Definition (N))) =
7666 N_Subtype_Indication
7667 then
7668 Error_Msg_N
7669 ("illegal constraint on type without discriminants", N);
7670 end if;
7671
7672 if Present (Discriminant_Specifications (N))
7673 and then Present (Full_View (Parent_Type))
7674 and then not Is_Tagged_Type (Full_View (Parent_Type))
7675 then
7676 Error_Msg_N ("cannot add discriminants to untagged type", N);
7677 end if;
7678
7679 Set_Stored_Constraint (Derived_Type, No_Elist);
7680 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7681 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
7682 Set_Disable_Controlled (Derived_Type, Disable_Controlled
7683 (Parent_Type));
7684 Set_Has_Controlled_Component
7685 (Derived_Type, Has_Controlled_Component
7686 (Parent_Type));
7687
7688 -- Direct controlled types do not inherit Finalize_Storage_Only flag
7689
7690 if not Is_Controlled_Active (Parent_Type) then
7691 Set_Finalize_Storage_Only
7692 (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
7693 end if;
7694
7695 -- If this is not a completion, construct the implicit full view by
7696 -- deriving from the full view of the parent type.
7697
7698 -- ??? If the parent is untagged private and its completion is
7699 -- tagged, this mechanism will not work because we cannot derive from
7700 -- the tagged full view unless we have an extension.
7701
7702 if Present (Full_View (Parent_Type))
7703 and then not Is_Tagged_Type (Full_View (Parent_Type))
7704 and then not Is_Completion
7705 then
7706 Build_Full_Derivation;
7707 Set_Full_View (Derived_Type, Full_Der);
7708 end if;
7709 end if;
7710
7711 Set_Has_Unknown_Discriminants (Derived_Type,
7712 Has_Unknown_Discriminants (Parent_Type));
7713
7714 if Is_Private_Type (Derived_Type) then
7715 Set_Private_Dependents (Derived_Type, New_Elmt_List);
7716 end if;
7717
7718 -- If the parent base type is in scope, add the derived type to its
7719 -- list of private dependents, because its full view may become
7720 -- visible subsequently (in a nested private part, a body, or in a
7721 -- further child unit).
7722
7723 if Is_Private_Type (Par_Base) and then In_Open_Scopes (Par_Scope) then
7724 Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
7725
7726 -- Check for unusual case where a type completed by a private
7727 -- derivation occurs within a package nested in a child unit, and
7728 -- the parent is declared in an ancestor.
7729
7730 if Is_Child_Unit (Scope (Current_Scope))
7731 and then Is_Completion
7732 and then In_Private_Part (Current_Scope)
7733 and then Scope (Parent_Type) /= Current_Scope
7734
7735 -- Note that if the parent has a completion in the private part,
7736 -- (which is itself a derivation from some other private type)
7737 -- it is that completion that is visible, there is no full view
7738 -- available, and no special processing is needed.
7739
7740 and then Present (Full_View (Parent_Type))
7741 then
7742 -- In this case, the full view of the parent type will become
7743 -- visible in the body of the enclosing child, and only then will
7744 -- the current type be possibly non-private. Build an underlying
7745 -- full view that will be installed when the enclosing child body
7746 -- is compiled.
7747
7748 if Present (Underlying_Full_View (Derived_Type)) then
7749 Full_Der := Underlying_Full_View (Derived_Type);
7750 else
7751 Build_Full_Derivation;
7752 Set_Underlying_Full_View (Derived_Type, Full_Der);
7753 Set_Is_Underlying_Full_View (Full_Der);
7754 end if;
7755
7756 -- The full view will be used to swap entities on entry/exit to
7757 -- the body, and must appear in the entity list for the package.
7758
7759 Append_Entity (Full_Der, Scope (Derived_Type));
7760 end if;
7761 end if;
7762 end Build_Derived_Private_Type;
7763
7764 -------------------------------
7765 -- Build_Derived_Record_Type --
7766 -------------------------------
7767
7768 -- 1. INTRODUCTION
7769
7770 -- Ideally we would like to use the same model of type derivation for
7771 -- tagged and untagged record types. Unfortunately this is not quite
7772 -- possible because the semantics of representation clauses is different
7773 -- for tagged and untagged records under inheritance. Consider the
7774 -- following:
7775
7776 -- type R (...) is [tagged] record ... end record;
7777 -- type T (...) is new R (...) [with ...];
7778
7779 -- The representation clauses for T can specify a completely different
7780 -- record layout from R's. Hence the same component can be placed in two
7781 -- very different positions in objects of type T and R. If R and T are
7782 -- tagged types, representation clauses for T can only specify the layout
7783 -- of non inherited components, thus components that are common in R and T
7784 -- have the same position in objects of type R and T.
7785
7786 -- This has two implications. The first is that the entire tree for R's
7787 -- declaration needs to be copied for T in the untagged case, so that T
7788 -- can be viewed as a record type of its own with its own representation
7789 -- clauses. The second implication is the way we handle discriminants.
7790 -- Specifically, in the untagged case we need a way to communicate to Gigi
7791 -- what are the real discriminants in the record, while for the semantics
7792 -- we need to consider those introduced by the user to rename the
7793 -- discriminants in the parent type. This is handled by introducing the
7794 -- notion of stored discriminants. See below for more.
7795
7796 -- Fortunately the way regular components are inherited can be handled in
7797 -- the same way in tagged and untagged types.
7798
7799 -- To complicate things a bit more the private view of a private extension
7800 -- cannot be handled in the same way as the full view (for one thing the
7801 -- semantic rules are somewhat different). We will explain what differs
7802 -- below.
7803
7804 -- 2. DISCRIMINANTS UNDER INHERITANCE
7805
7806 -- The semantic rules governing the discriminants of derived types are
7807 -- quite subtle.
7808
7809 -- type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
7810 -- [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
7811
7812 -- If parent type has discriminants, then the discriminants that are
7813 -- declared in the derived type are [3.4 (11)]:
7814
7815 -- o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
7816 -- there is one;
7817
7818 -- o Otherwise, each discriminant of the parent type (implicitly declared
7819 -- in the same order with the same specifications). In this case, the
7820 -- discriminants are said to be "inherited", or if unknown in the parent
7821 -- are also unknown in the derived type.
7822
7823 -- Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
7824
7825 -- o The parent subtype must be constrained;
7826
7827 -- o If the parent type is not a tagged type, then each discriminant of
7828 -- the derived type must be used in the constraint defining a parent
7829 -- subtype. [Implementation note: This ensures that the new discriminant
7830 -- can share storage with an existing discriminant.]
7831
7832 -- For the derived type each discriminant of the parent type is either
7833 -- inherited, constrained to equal some new discriminant of the derived
7834 -- type, or constrained to the value of an expression.
7835
7836 -- When inherited or constrained to equal some new discriminant, the
7837 -- parent discriminant and the discriminant of the derived type are said
7838 -- to "correspond".
7839
7840 -- If a discriminant of the parent type is constrained to a specific value
7841 -- in the derived type definition, then the discriminant is said to be
7842 -- "specified" by that derived type definition.
7843
7844 -- 3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
7845
7846 -- We have spoken about stored discriminants in point 1 (introduction)
7847 -- above. There are two sort of stored discriminants: implicit and
7848 -- explicit. As long as the derived type inherits the same discriminants as
7849 -- the root record type, stored discriminants are the same as regular
7850 -- discriminants, and are said to be implicit. However, if any discriminant
7851 -- in the root type was renamed in the derived type, then the derived
7852 -- type will contain explicit stored discriminants. Explicit stored
7853 -- discriminants are discriminants in addition to the semantically visible
7854 -- discriminants defined for the derived type. Stored discriminants are
7855 -- used by Gigi to figure out what are the physical discriminants in
7856 -- objects of the derived type (see precise definition in einfo.ads).
7857 -- As an example, consider the following:
7858
7859 -- type R (D1, D2, D3 : Int) is record ... end record;
7860 -- type T1 is new R;
7861 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
7862 -- type T3 is new T2;
7863 -- type T4 (Y : Int) is new T3 (Y, 99);
7864
7865 -- The following table summarizes the discriminants and stored
7866 -- discriminants in R and T1 through T4.
7867
7868 -- Type Discrim Stored Discrim Comment
7869 -- R (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in R
7870 -- T1 (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in T1
7871 -- T2 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T2
7872 -- T3 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T3
7873 -- T4 (Y) (D1, D2, D3) Girder discrims EXPLICIT in T4
7874
7875 -- Field Corresponding_Discriminant (abbreviated CD below) allows us to
7876 -- find the corresponding discriminant in the parent type, while
7877 -- Original_Record_Component (abbreviated ORC below), the actual physical
7878 -- component that is renamed. Finally the field Is_Completely_Hidden
7879 -- (abbreviated ICH below) is set for all explicit stored discriminants
7880 -- (see einfo.ads for more info). For the above example this gives:
7881
7882 -- Discrim CD ORC ICH
7883 -- ^^^^^^^ ^^ ^^^ ^^^
7884 -- D1 in R empty itself no
7885 -- D2 in R empty itself no
7886 -- D3 in R empty itself no
7887
7888 -- D1 in T1 D1 in R itself no
7889 -- D2 in T1 D2 in R itself no
7890 -- D3 in T1 D3 in R itself no
7891
7892 -- X1 in T2 D3 in T1 D3 in T2 no
7893 -- X2 in T2 D1 in T1 D1 in T2 no
7894 -- D1 in T2 empty itself yes
7895 -- D2 in T2 empty itself yes
7896 -- D3 in T2 empty itself yes
7897
7898 -- X1 in T3 X1 in T2 D3 in T3 no
7899 -- X2 in T3 X2 in T2 D1 in T3 no
7900 -- D1 in T3 empty itself yes
7901 -- D2 in T3 empty itself yes
7902 -- D3 in T3 empty itself yes
7903
7904 -- Y in T4 X1 in T3 D3 in T3 no
7905 -- D1 in T3 empty itself yes
7906 -- D2 in T3 empty itself yes
7907 -- D3 in T3 empty itself yes
7908
7909 -- 4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
7910
7911 -- Type derivation for tagged types is fairly straightforward. If no
7912 -- discriminants are specified by the derived type, these are inherited
7913 -- from the parent. No explicit stored discriminants are ever necessary.
7914 -- The only manipulation that is done to the tree is that of adding a
7915 -- _parent field with parent type and constrained to the same constraint
7916 -- specified for the parent in the derived type definition. For instance:
7917
7918 -- type R (D1, D2, D3 : Int) is tagged record ... end record;
7919 -- type T1 is new R with null record;
7920 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
7921
7922 -- are changed into:
7923
7924 -- type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
7925 -- _parent : R (D1, D2, D3);
7926 -- end record;
7927
7928 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
7929 -- _parent : T1 (X2, 88, X1);
7930 -- end record;
7931
7932 -- The discriminants actually present in R, T1 and T2 as well as their CD,
7933 -- ORC and ICH fields are:
7934
7935 -- Discrim CD ORC ICH
7936 -- ^^^^^^^ ^^ ^^^ ^^^
7937 -- D1 in R empty itself no
7938 -- D2 in R empty itself no
7939 -- D3 in R empty itself no
7940
7941 -- D1 in T1 D1 in R D1 in R no
7942 -- D2 in T1 D2 in R D2 in R no
7943 -- D3 in T1 D3 in R D3 in R no
7944
7945 -- X1 in T2 D3 in T1 D3 in R no
7946 -- X2 in T2 D1 in T1 D1 in R no
7947
7948 -- 5. FIRST TRANSFORMATION FOR DERIVED RECORDS
7949 --
7950 -- Regardless of whether we dealing with a tagged or untagged type
7951 -- we will transform all derived type declarations of the form
7952 --
7953 -- type T is new R (...) [with ...];
7954 -- or
7955 -- subtype S is R (...);
7956 -- type T is new S [with ...];
7957 -- into
7958 -- type BT is new R [with ...];
7959 -- subtype T is BT (...);
7960 --
7961 -- That is, the base derived type is constrained only if it has no
7962 -- discriminants. The reason for doing this is that GNAT's semantic model
7963 -- assumes that a base type with discriminants is unconstrained.
7964 --
7965 -- Note that, strictly speaking, the above transformation is not always
7966 -- correct. Consider for instance the following excerpt from ACVC b34011a:
7967 --
7968 -- procedure B34011A is
7969 -- type REC (D : integer := 0) is record
7970 -- I : Integer;
7971 -- end record;
7972
7973 -- package P is
7974 -- type T6 is new Rec;
7975 -- function F return T6;
7976 -- end P;
7977
7978 -- use P;
7979 -- package Q6 is
7980 -- type U is new T6 (Q6.F.I); -- ERROR: Q6.F.
7981 -- end Q6;
7982 --
7983 -- The definition of Q6.U is illegal. However transforming Q6.U into
7984
7985 -- type BaseU is new T6;
7986 -- subtype U is BaseU (Q6.F.I)
7987
7988 -- turns U into a legal subtype, which is incorrect. To avoid this problem
7989 -- we always analyze the constraint (in this case (Q6.F.I)) before applying
7990 -- the transformation described above.
7991
7992 -- There is another instance where the above transformation is incorrect.
7993 -- Consider:
7994
7995 -- package Pack is
7996 -- type Base (D : Integer) is tagged null record;
7997 -- procedure P (X : Base);
7998
7999 -- type Der is new Base (2) with null record;
8000 -- procedure P (X : Der);
8001 -- end Pack;
8002
8003 -- Then the above transformation turns this into
8004
8005 -- type Der_Base is new Base with null record;
8006 -- -- procedure P (X : Base) is implicitly inherited here
8007 -- -- as procedure P (X : Der_Base).
8008
8009 -- subtype Der is Der_Base (2);
8010 -- procedure P (X : Der);
8011 -- -- The overriding of P (X : Der_Base) is illegal since we
8012 -- -- have a parameter conformance problem.
8013
8014 -- To get around this problem, after having semantically processed Der_Base
8015 -- and the rewritten subtype declaration for Der, we copy Der_Base field
8016 -- Discriminant_Constraint from Der so that when parameter conformance is
8017 -- checked when P is overridden, no semantic errors are flagged.
8018
8019 -- 6. SECOND TRANSFORMATION FOR DERIVED RECORDS
8020
8021 -- Regardless of whether we are dealing with a tagged or untagged type
8022 -- we will transform all derived type declarations of the form
8023
8024 -- type R (D1, .., Dn : ...) is [tagged] record ...;
8025 -- type T is new R [with ...];
8026 -- into
8027 -- type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
8028
8029 -- The reason for such transformation is that it allows us to implement a
8030 -- very clean form of component inheritance as explained below.
8031
8032 -- Note that this transformation is not achieved by direct tree rewriting
8033 -- and manipulation, but rather by redoing the semantic actions that the
8034 -- above transformation will entail. This is done directly in routine
8035 -- Inherit_Components.
8036
8037 -- 7. TYPE DERIVATION AND COMPONENT INHERITANCE
8038
8039 -- In both tagged and untagged derived types, regular non discriminant
8040 -- components are inherited in the derived type from the parent type. In
8041 -- the absence of discriminants component, inheritance is straightforward
8042 -- as components can simply be copied from the parent.
8043
8044 -- If the parent has discriminants, inheriting components constrained with
8045 -- these discriminants requires caution. Consider the following example:
8046
8047 -- type R (D1, D2 : Positive) is [tagged] record
8048 -- S : String (D1 .. D2);
8049 -- end record;
8050
8051 -- type T1 is new R [with null record];
8052 -- type T2 (X : positive) is new R (1, X) [with null record];
8053
8054 -- As explained in 6. above, T1 is rewritten as
8055 -- type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
8056 -- which makes the treatment for T1 and T2 identical.
8057
8058 -- What we want when inheriting S, is that references to D1 and D2 in R are
8059 -- replaced with references to their correct constraints, i.e. D1 and D2 in
8060 -- T1 and 1 and X in T2. So all R's discriminant references are replaced
8061 -- with either discriminant references in the derived type or expressions.
8062 -- This replacement is achieved as follows: before inheriting R's
8063 -- components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
8064 -- created in the scope of T1 (resp. scope of T2) so that discriminants D1
8065 -- and D2 of T1 are visible (resp. discriminant X of T2 is visible).
8066 -- For T2, for instance, this has the effect of replacing String (D1 .. D2)
8067 -- by String (1 .. X).
8068
8069 -- 8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
8070
8071 -- We explain here the rules governing private type extensions relevant to
8072 -- type derivation. These rules are explained on the following example:
8073
8074 -- type D [(...)] is new A [(...)] with private; <-- partial view
8075 -- type D [(...)] is new P [(...)] with null record; <-- full view
8076
8077 -- Type A is called the ancestor subtype of the private extension.
8078 -- Type P is the parent type of the full view of the private extension. It
8079 -- must be A or a type derived from A.
8080
8081 -- The rules concerning the discriminants of private type extensions are
8082 -- [7.3(10-13)]:
8083
8084 -- o If a private extension inherits known discriminants from the ancestor
8085 -- subtype, then the full view must also inherit its discriminants from
8086 -- the ancestor subtype and the parent subtype of the full view must be
8087 -- constrained if and only if the ancestor subtype is constrained.
8088
8089 -- o If a partial view has unknown discriminants, then the full view may
8090 -- define a definite or an indefinite subtype, with or without
8091 -- discriminants.
8092
8093 -- o If a partial view has neither known nor unknown discriminants, then
8094 -- the full view must define a definite subtype.
8095
8096 -- o If the ancestor subtype of a private extension has constrained
8097 -- discriminants, then the parent subtype of the full view must impose a
8098 -- statically matching constraint on those discriminants.
8099
8100 -- This means that only the following forms of private extensions are
8101 -- allowed:
8102
8103 -- type D is new A with private; <-- partial view
8104 -- type D is new P with null record; <-- full view
8105
8106 -- If A has no discriminants than P has no discriminants, otherwise P must
8107 -- inherit A's discriminants.
8108
8109 -- type D is new A (...) with private; <-- partial view
8110 -- type D is new P (:::) with null record; <-- full view
8111
8112 -- P must inherit A's discriminants and (...) and (:::) must statically
8113 -- match.
8114
8115 -- subtype A is R (...);
8116 -- type D is new A with private; <-- partial view
8117 -- type D is new P with null record; <-- full view
8118
8119 -- P must have inherited R's discriminants and must be derived from A or
8120 -- any of its subtypes.
8121
8122 -- type D (..) is new A with private; <-- partial view
8123 -- type D (..) is new P [(:::)] with null record; <-- full view
8124
8125 -- No specific constraints on P's discriminants or constraint (:::).
8126 -- Note that A can be unconstrained, but the parent subtype P must either
8127 -- be constrained or (:::) must be present.
8128
8129 -- type D (..) is new A [(...)] with private; <-- partial view
8130 -- type D (..) is new P [(:::)] with null record; <-- full view
8131
8132 -- P's constraints on A's discriminants must statically match those
8133 -- imposed by (...).
8134
8135 -- 9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
8136
8137 -- The full view of a private extension is handled exactly as described
8138 -- above. The model chose for the private view of a private extension is
8139 -- the same for what concerns discriminants (i.e. they receive the same
8140 -- treatment as in the tagged case). However, the private view of the
8141 -- private extension always inherits the components of the parent base,
8142 -- without replacing any discriminant reference. Strictly speaking this is
8143 -- incorrect. However, Gigi never uses this view to generate code so this
8144 -- is a purely semantic issue. In theory, a set of transformations similar
8145 -- to those given in 5. and 6. above could be applied to private views of
8146 -- private extensions to have the same model of component inheritance as
8147 -- for non private extensions. However, this is not done because it would
8148 -- further complicate private type processing. Semantically speaking, this
8149 -- leaves us in an uncomfortable situation. As an example consider:
8150
8151 -- package Pack is
8152 -- type R (D : integer) is tagged record
8153 -- S : String (1 .. D);
8154 -- end record;
8155 -- procedure P (X : R);
8156 -- type T is new R (1) with private;
8157 -- private
8158 -- type T is new R (1) with null record;
8159 -- end;
8160
8161 -- This is transformed into:
8162
8163 -- package Pack is
8164 -- type R (D : integer) is tagged record
8165 -- S : String (1 .. D);
8166 -- end record;
8167 -- procedure P (X : R);
8168 -- type T is new R (1) with private;
8169 -- private
8170 -- type BaseT is new R with null record;
8171 -- subtype T is BaseT (1);
8172 -- end;
8173
8174 -- (strictly speaking the above is incorrect Ada)
8175
8176 -- From the semantic standpoint the private view of private extension T
8177 -- should be flagged as constrained since one can clearly have
8178 --
8179 -- Obj : T;
8180 --
8181 -- in a unit withing Pack. However, when deriving subprograms for the
8182 -- private view of private extension T, T must be seen as unconstrained
8183 -- since T has discriminants (this is a constraint of the current
8184 -- subprogram derivation model). Thus, when processing the private view of
8185 -- a private extension such as T, we first mark T as unconstrained, we
8186 -- process it, we perform program derivation and just before returning from
8187 -- Build_Derived_Record_Type we mark T as constrained.
8188
8189 -- ??? Are there are other uncomfortable cases that we will have to
8190 -- deal with.
8191
8192 -- 10. RECORD_TYPE_WITH_PRIVATE complications
8193
8194 -- Types that are derived from a visible record type and have a private
8195 -- extension present other peculiarities. They behave mostly like private
8196 -- types, but if they have primitive operations defined, these will not
8197 -- have the proper signatures for further inheritance, because other
8198 -- primitive operations will use the implicit base that we define for
8199 -- private derivations below. This affect subprogram inheritance (see
8200 -- Derive_Subprograms for details). We also derive the implicit base from
8201 -- the base type of the full view, so that the implicit base is a record
8202 -- type and not another private type, This avoids infinite loops.
8203
8204 procedure Build_Derived_Record_Type
8205 (N : Node_Id;
8206 Parent_Type : Entity_Id;
8207 Derived_Type : Entity_Id;
8208 Derive_Subps : Boolean := True)
8209 is
8210 Discriminant_Specs : constant Boolean :=
8211 Present (Discriminant_Specifications (N));
8212 Is_Tagged : constant Boolean := Is_Tagged_Type (Parent_Type);
8213 Loc : constant Source_Ptr := Sloc (N);
8214 Private_Extension : constant Boolean :=
8215 Nkind (N) = N_Private_Extension_Declaration;
8216 Assoc_List : Elist_Id;
8217 Constraint_Present : Boolean;
8218 Constrs : Elist_Id;
8219 Discrim : Entity_Id;
8220 Indic : Node_Id;
8221 Inherit_Discrims : Boolean := False;
8222 Last_Discrim : Entity_Id;
8223 New_Base : Entity_Id;
8224 New_Decl : Node_Id;
8225 New_Discrs : Elist_Id;
8226 New_Indic : Node_Id;
8227 Parent_Base : Entity_Id;
8228 Save_Etype : Entity_Id;
8229 Save_Discr_Constr : Elist_Id;
8230 Save_Next_Entity : Entity_Id;
8231 Type_Def : Node_Id;
8232
8233 Discs : Elist_Id := New_Elmt_List;
8234 -- An empty Discs list means that there were no constraints in the
8235 -- subtype indication or that there was an error processing it.
8236
8237 begin
8238 if Ekind (Parent_Type) = E_Record_Type_With_Private
8239 and then Present (Full_View (Parent_Type))
8240 and then Has_Discriminants (Parent_Type)
8241 then
8242 Parent_Base := Base_Type (Full_View (Parent_Type));
8243 else
8244 Parent_Base := Base_Type (Parent_Type);
8245 end if;
8246
8247 -- AI05-0115 : if this is a derivation from a private type in some
8248 -- other scope that may lead to invisible components for the derived
8249 -- type, mark it accordingly.
8250
8251 if Is_Private_Type (Parent_Type) then
8252 if Scope (Parent_Type) = Scope (Derived_Type) then
8253 null;
8254
8255 elsif In_Open_Scopes (Scope (Parent_Type))
8256 and then In_Private_Part (Scope (Parent_Type))
8257 then
8258 null;
8259
8260 else
8261 Set_Has_Private_Ancestor (Derived_Type);
8262 end if;
8263
8264 else
8265 Set_Has_Private_Ancestor
8266 (Derived_Type, Has_Private_Ancestor (Parent_Type));
8267 end if;
8268
8269 -- Before we start the previously documented transformations, here is
8270 -- little fix for size and alignment of tagged types. Normally when we
8271 -- derive type D from type P, we copy the size and alignment of P as the
8272 -- default for D, and in the absence of explicit representation clauses
8273 -- for D, the size and alignment are indeed the same as the parent.
8274
8275 -- But this is wrong for tagged types, since fields may be added, and
8276 -- the default size may need to be larger, and the default alignment may
8277 -- need to be larger.
8278
8279 -- We therefore reset the size and alignment fields in the tagged case.
8280 -- Note that the size and alignment will in any case be at least as
8281 -- large as the parent type (since the derived type has a copy of the
8282 -- parent type in the _parent field)
8283
8284 -- The type is also marked as being tagged here, which is needed when
8285 -- processing components with a self-referential anonymous access type
8286 -- in the call to Check_Anonymous_Access_Components below. Note that
8287 -- this flag is also set later on for completeness.
8288
8289 if Is_Tagged then
8290 Set_Is_Tagged_Type (Derived_Type);
8291 Init_Size_Align (Derived_Type);
8292 end if;
8293
8294 -- STEP 0a: figure out what kind of derived type declaration we have
8295
8296 if Private_Extension then
8297 Type_Def := N;
8298 Set_Ekind (Derived_Type, E_Record_Type_With_Private);
8299 Set_Default_SSO (Derived_Type);
8300
8301 else
8302 Type_Def := Type_Definition (N);
8303
8304 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
8305 -- Parent_Base can be a private type or private extension. However,
8306 -- for tagged types with an extension the newly added fields are
8307 -- visible and hence the Derived_Type is always an E_Record_Type.
8308 -- (except that the parent may have its own private fields).
8309 -- For untagged types we preserve the Ekind of the Parent_Base.
8310
8311 if Present (Record_Extension_Part (Type_Def)) then
8312 Set_Ekind (Derived_Type, E_Record_Type);
8313 Set_Default_SSO (Derived_Type);
8314
8315 -- Create internal access types for components with anonymous
8316 -- access types.
8317
8318 if Ada_Version >= Ada_2005 then
8319 Check_Anonymous_Access_Components
8320 (N, Derived_Type, Derived_Type,
8321 Component_List (Record_Extension_Part (Type_Def)));
8322 end if;
8323
8324 else
8325 Set_Ekind (Derived_Type, Ekind (Parent_Base));
8326 end if;
8327 end if;
8328
8329 -- Indic can either be an N_Identifier if the subtype indication
8330 -- contains no constraint or an N_Subtype_Indication if the subtype
8331 -- indication has a constraint.
8332
8333 Indic := Subtype_Indication (Type_Def);
8334 Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
8335
8336 -- Check that the type has visible discriminants. The type may be
8337 -- a private type with unknown discriminants whose full view has
8338 -- discriminants which are invisible.
8339
8340 if Constraint_Present then
8341 if not Has_Discriminants (Parent_Base)
8342 or else
8343 (Has_Unknown_Discriminants (Parent_Base)
8344 and then Is_Private_Type (Parent_Base))
8345 then
8346 Error_Msg_N
8347 ("invalid constraint: type has no discriminant",
8348 Constraint (Indic));
8349
8350 Constraint_Present := False;
8351 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
8352
8353 elsif Is_Constrained (Parent_Type) then
8354 Error_Msg_N
8355 ("invalid constraint: parent type is already constrained",
8356 Constraint (Indic));
8357
8358 Constraint_Present := False;
8359 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
8360 end if;
8361 end if;
8362
8363 -- STEP 0b: If needed, apply transformation given in point 5. above
8364
8365 if not Private_Extension
8366 and then Has_Discriminants (Parent_Type)
8367 and then not Discriminant_Specs
8368 and then (Is_Constrained (Parent_Type) or else Constraint_Present)
8369 then
8370 -- First, we must analyze the constraint (see comment in point 5.)
8371 -- The constraint may come from the subtype indication of the full
8372 -- declaration.
8373
8374 if Constraint_Present then
8375 New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
8376
8377 -- If there is no explicit constraint, there might be one that is
8378 -- inherited from a constrained parent type. In that case verify that
8379 -- it conforms to the constraint in the partial view. In perverse
8380 -- cases the parent subtypes of the partial and full view can have
8381 -- different constraints.
8382
8383 elsif Present (Stored_Constraint (Parent_Type)) then
8384 New_Discrs := Stored_Constraint (Parent_Type);
8385
8386 else
8387 New_Discrs := No_Elist;
8388 end if;
8389
8390 if Has_Discriminants (Derived_Type)
8391 and then Has_Private_Declaration (Derived_Type)
8392 and then Present (Discriminant_Constraint (Derived_Type))
8393 and then Present (New_Discrs)
8394 then
8395 -- Verify that constraints of the full view statically match
8396 -- those given in the partial view.
8397
8398 declare
8399 C1, C2 : Elmt_Id;
8400
8401 begin
8402 C1 := First_Elmt (New_Discrs);
8403 C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
8404 while Present (C1) and then Present (C2) loop
8405 if Fully_Conformant_Expressions (Node (C1), Node (C2))
8406 or else
8407 (Is_OK_Static_Expression (Node (C1))
8408 and then Is_OK_Static_Expression (Node (C2))
8409 and then
8410 Expr_Value (Node (C1)) = Expr_Value (Node (C2)))
8411 then
8412 null;
8413
8414 else
8415 if Constraint_Present then
8416 Error_Msg_N
8417 ("constraint not conformant to previous declaration",
8418 Node (C1));
8419 else
8420 Error_Msg_N
8421 ("constraint of full view is incompatible "
8422 & "with partial view", N);
8423 end if;
8424 end if;
8425
8426 Next_Elmt (C1);
8427 Next_Elmt (C2);
8428 end loop;
8429 end;
8430 end if;
8431
8432 -- Insert and analyze the declaration for the unconstrained base type
8433
8434 New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
8435
8436 New_Decl :=
8437 Make_Full_Type_Declaration (Loc,
8438 Defining_Identifier => New_Base,
8439 Type_Definition =>
8440 Make_Derived_Type_Definition (Loc,
8441 Abstract_Present => Abstract_Present (Type_Def),
8442 Limited_Present => Limited_Present (Type_Def),
8443 Subtype_Indication =>
8444 New_Occurrence_Of (Parent_Base, Loc),
8445 Record_Extension_Part =>
8446 Relocate_Node (Record_Extension_Part (Type_Def)),
8447 Interface_List => Interface_List (Type_Def)));
8448
8449 Set_Parent (New_Decl, Parent (N));
8450 Mark_Rewrite_Insertion (New_Decl);
8451 Insert_Before (N, New_Decl);
8452
8453 -- In the extension case, make sure ancestor is frozen appropriately
8454 -- (see also non-discriminated case below).
8455
8456 if Present (Record_Extension_Part (Type_Def))
8457 or else Is_Interface (Parent_Base)
8458 then
8459 Freeze_Before (New_Decl, Parent_Type);
8460 end if;
8461
8462 -- Note that this call passes False for the Derive_Subps parameter
8463 -- because subprogram derivation is deferred until after creating
8464 -- the subtype (see below).
8465
8466 Build_Derived_Type
8467 (New_Decl, Parent_Base, New_Base,
8468 Is_Completion => False, Derive_Subps => False);
8469
8470 -- ??? This needs re-examination to determine whether the
8471 -- above call can simply be replaced by a call to Analyze.
8472
8473 Set_Analyzed (New_Decl);
8474
8475 -- Insert and analyze the declaration for the constrained subtype
8476
8477 if Constraint_Present then
8478 New_Indic :=
8479 Make_Subtype_Indication (Loc,
8480 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
8481 Constraint => Relocate_Node (Constraint (Indic)));
8482
8483 else
8484 declare
8485 Constr_List : constant List_Id := New_List;
8486 C : Elmt_Id;
8487 Expr : Node_Id;
8488
8489 begin
8490 C := First_Elmt (Discriminant_Constraint (Parent_Type));
8491 while Present (C) loop
8492 Expr := Node (C);
8493
8494 -- It is safe here to call New_Copy_Tree since we called
8495 -- Force_Evaluation on each constraint previously
8496 -- in Build_Discriminant_Constraints.
8497
8498 Append (New_Copy_Tree (Expr), To => Constr_List);
8499
8500 Next_Elmt (C);
8501 end loop;
8502
8503 New_Indic :=
8504 Make_Subtype_Indication (Loc,
8505 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
8506 Constraint =>
8507 Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
8508 end;
8509 end if;
8510
8511 Rewrite (N,
8512 Make_Subtype_Declaration (Loc,
8513 Defining_Identifier => Derived_Type,
8514 Subtype_Indication => New_Indic));
8515
8516 Analyze (N);
8517
8518 -- Derivation of subprograms must be delayed until the full subtype
8519 -- has been established, to ensure proper overriding of subprograms
8520 -- inherited by full types. If the derivations occurred as part of
8521 -- the call to Build_Derived_Type above, then the check for type
8522 -- conformance would fail because earlier primitive subprograms
8523 -- could still refer to the full type prior the change to the new
8524 -- subtype and hence would not match the new base type created here.
8525 -- Subprograms are not derived, however, when Derive_Subps is False
8526 -- (since otherwise there could be redundant derivations).
8527
8528 if Derive_Subps then
8529 Derive_Subprograms (Parent_Type, Derived_Type);
8530 end if;
8531
8532 -- For tagged types the Discriminant_Constraint of the new base itype
8533 -- is inherited from the first subtype so that no subtype conformance
8534 -- problem arise when the first subtype overrides primitive
8535 -- operations inherited by the implicit base type.
8536
8537 if Is_Tagged then
8538 Set_Discriminant_Constraint
8539 (New_Base, Discriminant_Constraint (Derived_Type));
8540 end if;
8541
8542 return;
8543 end if;
8544
8545 -- If we get here Derived_Type will have no discriminants or it will be
8546 -- a discriminated unconstrained base type.
8547
8548 -- STEP 1a: perform preliminary actions/checks for derived tagged types
8549
8550 if Is_Tagged then
8551
8552 -- The parent type is frozen for non-private extensions (RM 13.14(7))
8553 -- The declaration of a specific descendant of an interface type
8554 -- freezes the interface type (RM 13.14).
8555
8556 if not Private_Extension or else Is_Interface (Parent_Base) then
8557 Freeze_Before (N, Parent_Type);
8558 end if;
8559
8560 -- In Ada 2005 (AI-344), the restriction that a derived tagged type
8561 -- cannot be declared at a deeper level than its parent type is
8562 -- removed. The check on derivation within a generic body is also
8563 -- relaxed, but there's a restriction that a derived tagged type
8564 -- cannot be declared in a generic body if it's derived directly
8565 -- or indirectly from a formal type of that generic.
8566
8567 if Ada_Version >= Ada_2005 then
8568 if Present (Enclosing_Generic_Body (Derived_Type)) then
8569 declare
8570 Ancestor_Type : Entity_Id;
8571
8572 begin
8573 -- Check to see if any ancestor of the derived type is a
8574 -- formal type.
8575
8576 Ancestor_Type := Parent_Type;
8577 while not Is_Generic_Type (Ancestor_Type)
8578 and then Etype (Ancestor_Type) /= Ancestor_Type
8579 loop
8580 Ancestor_Type := Etype (Ancestor_Type);
8581 end loop;
8582
8583 -- If the derived type does have a formal type as an
8584 -- ancestor, then it's an error if the derived type is
8585 -- declared within the body of the generic unit that
8586 -- declares the formal type in its generic formal part. It's
8587 -- sufficient to check whether the ancestor type is declared
8588 -- inside the same generic body as the derived type (such as
8589 -- within a nested generic spec), in which case the
8590 -- derivation is legal. If the formal type is declared
8591 -- outside of that generic body, then it's guaranteed that
8592 -- the derived type is declared within the generic body of
8593 -- the generic unit declaring the formal type.
8594
8595 if Is_Generic_Type (Ancestor_Type)
8596 and then Enclosing_Generic_Body (Ancestor_Type) /=
8597 Enclosing_Generic_Body (Derived_Type)
8598 then
8599 Error_Msg_NE
8600 ("parent type of& must not be descendant of formal type"
8601 & " of an enclosing generic body",
8602 Indic, Derived_Type);
8603 end if;
8604 end;
8605 end if;
8606
8607 elsif Type_Access_Level (Derived_Type) /=
8608 Type_Access_Level (Parent_Type)
8609 and then not Is_Generic_Type (Derived_Type)
8610 then
8611 if Is_Controlled (Parent_Type) then
8612 Error_Msg_N
8613 ("controlled type must be declared at the library level",
8614 Indic);
8615 else
8616 Error_Msg_N
8617 ("type extension at deeper accessibility level than parent",
8618 Indic);
8619 end if;
8620
8621 else
8622 declare
8623 GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
8624 begin
8625 if Present (GB)
8626 and then GB /= Enclosing_Generic_Body (Parent_Base)
8627 then
8628 Error_Msg_NE
8629 ("parent type of& must not be outside generic body"
8630 & " (RM 3.9.1(4))",
8631 Indic, Derived_Type);
8632 end if;
8633 end;
8634 end if;
8635 end if;
8636
8637 -- Ada 2005 (AI-251)
8638
8639 if Ada_Version >= Ada_2005 and then Is_Tagged then
8640
8641 -- "The declaration of a specific descendant of an interface type
8642 -- freezes the interface type" (RM 13.14).
8643
8644 declare
8645 Iface : Node_Id;
8646 begin
8647 if Is_Non_Empty_List (Interface_List (Type_Def)) then
8648 Iface := First (Interface_List (Type_Def));
8649 while Present (Iface) loop
8650 Freeze_Before (N, Etype (Iface));
8651 Next (Iface);
8652 end loop;
8653 end if;
8654 end;
8655 end if;
8656
8657 -- STEP 1b : preliminary cleanup of the full view of private types
8658
8659 -- If the type is already marked as having discriminants, then it's the
8660 -- completion of a private type or private extension and we need to
8661 -- retain the discriminants from the partial view if the current
8662 -- declaration has Discriminant_Specifications so that we can verify
8663 -- conformance. However, we must remove any existing components that
8664 -- were inherited from the parent (and attached in Copy_And_Swap)
8665 -- because the full type inherits all appropriate components anyway, and
8666 -- we do not want the partial view's components interfering.
8667
8668 if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
8669 Discrim := First_Discriminant (Derived_Type);
8670 loop
8671 Last_Discrim := Discrim;
8672 Next_Discriminant (Discrim);
8673 exit when No (Discrim);
8674 end loop;
8675
8676 Set_Last_Entity (Derived_Type, Last_Discrim);
8677
8678 -- In all other cases wipe out the list of inherited components (even
8679 -- inherited discriminants), it will be properly rebuilt here.
8680
8681 else
8682 Set_First_Entity (Derived_Type, Empty);
8683 Set_Last_Entity (Derived_Type, Empty);
8684 end if;
8685
8686 -- STEP 1c: Initialize some flags for the Derived_Type
8687
8688 -- The following flags must be initialized here so that
8689 -- Process_Discriminants can check that discriminants of tagged types do
8690 -- not have a default initial value and that access discriminants are
8691 -- only specified for limited records. For completeness, these flags are
8692 -- also initialized along with all the other flags below.
8693
8694 -- AI-419: Limitedness is not inherited from an interface parent, so to
8695 -- be limited in that case the type must be explicitly declared as
8696 -- limited. However, task and protected interfaces are always limited.
8697
8698 if Limited_Present (Type_Def) then
8699 Set_Is_Limited_Record (Derived_Type);
8700
8701 elsif Is_Limited_Record (Parent_Type)
8702 or else (Present (Full_View (Parent_Type))
8703 and then Is_Limited_Record (Full_View (Parent_Type)))
8704 then
8705 if not Is_Interface (Parent_Type)
8706 or else Is_Synchronized_Interface (Parent_Type)
8707 or else Is_Protected_Interface (Parent_Type)
8708 or else Is_Task_Interface (Parent_Type)
8709 then
8710 Set_Is_Limited_Record (Derived_Type);
8711 end if;
8712 end if;
8713
8714 -- STEP 2a: process discriminants of derived type if any
8715
8716 Push_Scope (Derived_Type);
8717
8718 if Discriminant_Specs then
8719 Set_Has_Unknown_Discriminants (Derived_Type, False);
8720
8721 -- The following call initializes fields Has_Discriminants and
8722 -- Discriminant_Constraint, unless we are processing the completion
8723 -- of a private type declaration.
8724
8725 Check_Or_Process_Discriminants (N, Derived_Type);
8726
8727 -- For untagged types, the constraint on the Parent_Type must be
8728 -- present and is used to rename the discriminants.
8729
8730 if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
8731 Error_Msg_N ("untagged parent must have discriminants", Indic);
8732
8733 elsif not Is_Tagged and then not Constraint_Present then
8734 Error_Msg_N
8735 ("discriminant constraint needed for derived untagged records",
8736 Indic);
8737
8738 -- Otherwise the parent subtype must be constrained unless we have a
8739 -- private extension.
8740
8741 elsif not Constraint_Present
8742 and then not Private_Extension
8743 and then not Is_Constrained (Parent_Type)
8744 then
8745 Error_Msg_N
8746 ("unconstrained type not allowed in this context", Indic);
8747
8748 elsif Constraint_Present then
8749 -- The following call sets the field Corresponding_Discriminant
8750 -- for the discriminants in the Derived_Type.
8751
8752 Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
8753
8754 -- For untagged types all new discriminants must rename
8755 -- discriminants in the parent. For private extensions new
8756 -- discriminants cannot rename old ones (implied by [7.3(13)]).
8757
8758 Discrim := First_Discriminant (Derived_Type);
8759 while Present (Discrim) loop
8760 if not Is_Tagged
8761 and then No (Corresponding_Discriminant (Discrim))
8762 then
8763 Error_Msg_N
8764 ("new discriminants must constrain old ones", Discrim);
8765
8766 elsif Private_Extension
8767 and then Present (Corresponding_Discriminant (Discrim))
8768 then
8769 Error_Msg_N
8770 ("only static constraints allowed for parent"
8771 & " discriminants in the partial view", Indic);
8772 exit;
8773 end if;
8774
8775 -- If a new discriminant is used in the constraint, then its
8776 -- subtype must be statically compatible with the parent
8777 -- discriminant's subtype (3.7(15)).
8778
8779 -- However, if the record contains an array constrained by
8780 -- the discriminant but with some different bound, the compiler
8781 -- tries to create a smaller range for the discriminant type.
8782 -- (See exp_ch3.Adjust_Discriminants). In this case, where
8783 -- the discriminant type is a scalar type, the check must use
8784 -- the original discriminant type in the parent declaration.
8785
8786 declare
8787 Corr_Disc : constant Entity_Id :=
8788 Corresponding_Discriminant (Discrim);
8789 Disc_Type : constant Entity_Id := Etype (Discrim);
8790 Corr_Type : Entity_Id;
8791
8792 begin
8793 if Present (Corr_Disc) then
8794 if Is_Scalar_Type (Disc_Type) then
8795 Corr_Type :=
8796 Entity (Discriminant_Type (Parent (Corr_Disc)));
8797 else
8798 Corr_Type := Etype (Corr_Disc);
8799 end if;
8800
8801 if not
8802 Subtypes_Statically_Compatible (Disc_Type, Corr_Type)
8803 then
8804 Error_Msg_N
8805 ("subtype must be compatible "
8806 & "with parent discriminant",
8807 Discrim);
8808 end if;
8809 end if;
8810 end;
8811
8812 Next_Discriminant (Discrim);
8813 end loop;
8814
8815 -- Check whether the constraints of the full view statically
8816 -- match those imposed by the parent subtype [7.3(13)].
8817
8818 if Present (Stored_Constraint (Derived_Type)) then
8819 declare
8820 C1, C2 : Elmt_Id;
8821
8822 begin
8823 C1 := First_Elmt (Discs);
8824 C2 := First_Elmt (Stored_Constraint (Derived_Type));
8825 while Present (C1) and then Present (C2) loop
8826 if not
8827 Fully_Conformant_Expressions (Node (C1), Node (C2))
8828 then
8829 Error_Msg_N
8830 ("not conformant with previous declaration",
8831 Node (C1));
8832 end if;
8833
8834 Next_Elmt (C1);
8835 Next_Elmt (C2);
8836 end loop;
8837 end;
8838 end if;
8839 end if;
8840
8841 -- STEP 2b: No new discriminants, inherit discriminants if any
8842
8843 else
8844 if Private_Extension then
8845 Set_Has_Unknown_Discriminants
8846 (Derived_Type,
8847 Has_Unknown_Discriminants (Parent_Type)
8848 or else Unknown_Discriminants_Present (N));
8849
8850 -- The partial view of the parent may have unknown discriminants,
8851 -- but if the full view has discriminants and the parent type is
8852 -- in scope they must be inherited.
8853
8854 elsif Has_Unknown_Discriminants (Parent_Type)
8855 and then
8856 (not Has_Discriminants (Parent_Type)
8857 or else not In_Open_Scopes (Scope (Parent_Type)))
8858 then
8859 Set_Has_Unknown_Discriminants (Derived_Type);
8860 end if;
8861
8862 if not Has_Unknown_Discriminants (Derived_Type)
8863 and then not Has_Unknown_Discriminants (Parent_Base)
8864 and then Has_Discriminants (Parent_Type)
8865 then
8866 Inherit_Discrims := True;
8867 Set_Has_Discriminants
8868 (Derived_Type, True);
8869 Set_Discriminant_Constraint
8870 (Derived_Type, Discriminant_Constraint (Parent_Base));
8871 end if;
8872
8873 -- The following test is true for private types (remember
8874 -- transformation 5. is not applied to those) and in an error
8875 -- situation.
8876
8877 if Constraint_Present then
8878 Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
8879 end if;
8880
8881 -- For now mark a new derived type as constrained only if it has no
8882 -- discriminants. At the end of Build_Derived_Record_Type we properly
8883 -- set this flag in the case of private extensions. See comments in
8884 -- point 9. just before body of Build_Derived_Record_Type.
8885
8886 Set_Is_Constrained
8887 (Derived_Type,
8888 not (Inherit_Discrims
8889 or else Has_Unknown_Discriminants (Derived_Type)));
8890 end if;
8891
8892 -- STEP 3: initialize fields of derived type
8893
8894 Set_Is_Tagged_Type (Derived_Type, Is_Tagged);
8895 Set_Stored_Constraint (Derived_Type, No_Elist);
8896
8897 -- Ada 2005 (AI-251): Private type-declarations can implement interfaces
8898 -- but cannot be interfaces
8899
8900 if not Private_Extension
8901 and then Ekind (Derived_Type) /= E_Private_Type
8902 and then Ekind (Derived_Type) /= E_Limited_Private_Type
8903 then
8904 if Interface_Present (Type_Def) then
8905 Analyze_Interface_Declaration (Derived_Type, Type_Def);
8906 end if;
8907
8908 Set_Interfaces (Derived_Type, No_Elist);
8909 end if;
8910
8911 -- Fields inherited from the Parent_Type
8912
8913 Set_Has_Specified_Layout
8914 (Derived_Type, Has_Specified_Layout (Parent_Type));
8915 Set_Is_Limited_Composite
8916 (Derived_Type, Is_Limited_Composite (Parent_Type));
8917 Set_Is_Private_Composite
8918 (Derived_Type, Is_Private_Composite (Parent_Type));
8919
8920 if Is_Tagged_Type (Parent_Type) then
8921 Set_No_Tagged_Streams_Pragma
8922 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
8923 end if;
8924
8925 -- Fields inherited from the Parent_Base
8926
8927 Set_Has_Controlled_Component
8928 (Derived_Type, Has_Controlled_Component (Parent_Base));
8929 Set_Has_Non_Standard_Rep
8930 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
8931 Set_Has_Primitive_Operations
8932 (Derived_Type, Has_Primitive_Operations (Parent_Base));
8933
8934 -- Fields inherited from the Parent_Base in the non-private case
8935
8936 if Ekind (Derived_Type) = E_Record_Type then
8937 Set_Has_Complex_Representation
8938 (Derived_Type, Has_Complex_Representation (Parent_Base));
8939 end if;
8940
8941 -- Fields inherited from the Parent_Base for record types
8942
8943 if Is_Record_Type (Derived_Type) then
8944 declare
8945 Parent_Full : Entity_Id;
8946
8947 begin
8948 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
8949 -- Parent_Base can be a private type or private extension. Go
8950 -- to the full view here to get the E_Record_Type specific flags.
8951
8952 if Present (Full_View (Parent_Base)) then
8953 Parent_Full := Full_View (Parent_Base);
8954 else
8955 Parent_Full := Parent_Base;
8956 end if;
8957
8958 Set_OK_To_Reorder_Components
8959 (Derived_Type, OK_To_Reorder_Components (Parent_Full));
8960 end;
8961 end if;
8962
8963 -- Set fields for private derived types
8964
8965 if Is_Private_Type (Derived_Type) then
8966 Set_Depends_On_Private (Derived_Type, True);
8967 Set_Private_Dependents (Derived_Type, New_Elmt_List);
8968
8969 -- Inherit fields from non private record types. If this is the
8970 -- completion of a derivation from a private type, the parent itself
8971 -- is private, and the attributes come from its full view, which must
8972 -- be present.
8973
8974 else
8975 if Is_Private_Type (Parent_Base)
8976 and then not Is_Record_Type (Parent_Base)
8977 then
8978 Set_Component_Alignment
8979 (Derived_Type, Component_Alignment (Full_View (Parent_Base)));
8980 Set_C_Pass_By_Copy
8981 (Derived_Type, C_Pass_By_Copy (Full_View (Parent_Base)));
8982 else
8983 Set_Component_Alignment
8984 (Derived_Type, Component_Alignment (Parent_Base));
8985 Set_C_Pass_By_Copy
8986 (Derived_Type, C_Pass_By_Copy (Parent_Base));
8987 end if;
8988 end if;
8989
8990 -- Set fields for tagged types
8991
8992 if Is_Tagged then
8993 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
8994
8995 -- All tagged types defined in Ada.Finalization are controlled
8996
8997 if Chars (Scope (Derived_Type)) = Name_Finalization
8998 and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
8999 and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
9000 then
9001 Set_Is_Controlled (Derived_Type);
9002 else
9003 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Base));
9004 end if;
9005
9006 -- Minor optimization: there is no need to generate the class-wide
9007 -- entity associated with an underlying record view.
9008
9009 if not Is_Underlying_Record_View (Derived_Type) then
9010 Make_Class_Wide_Type (Derived_Type);
9011 end if;
9012
9013 Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
9014
9015 if Has_Discriminants (Derived_Type)
9016 and then Constraint_Present
9017 then
9018 Set_Stored_Constraint
9019 (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
9020 end if;
9021
9022 if Ada_Version >= Ada_2005 then
9023 declare
9024 Ifaces_List : Elist_Id;
9025
9026 begin
9027 -- Checks rules 3.9.4 (13/2 and 14/2)
9028
9029 if Comes_From_Source (Derived_Type)
9030 and then not Is_Private_Type (Derived_Type)
9031 and then Is_Interface (Parent_Type)
9032 and then not Is_Interface (Derived_Type)
9033 then
9034 if Is_Task_Interface (Parent_Type) then
9035 Error_Msg_N
9036 ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
9037 Derived_Type);
9038
9039 elsif Is_Protected_Interface (Parent_Type) then
9040 Error_Msg_N
9041 ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
9042 Derived_Type);
9043 end if;
9044 end if;
9045
9046 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
9047
9048 Check_Interfaces (N, Type_Def);
9049
9050 -- Ada 2005 (AI-251): Collect the list of progenitors that are
9051 -- not already in the parents.
9052
9053 Collect_Interfaces
9054 (T => Derived_Type,
9055 Ifaces_List => Ifaces_List,
9056 Exclude_Parents => True);
9057
9058 Set_Interfaces (Derived_Type, Ifaces_List);
9059
9060 -- If the derived type is the anonymous type created for
9061 -- a declaration whose parent has a constraint, propagate
9062 -- the interface list to the source type. This must be done
9063 -- prior to the completion of the analysis of the source type
9064 -- because the components in the extension may contain current
9065 -- instances whose legality depends on some ancestor.
9066
9067 if Is_Itype (Derived_Type) then
9068 declare
9069 Def : constant Node_Id :=
9070 Associated_Node_For_Itype (Derived_Type);
9071 begin
9072 if Present (Def)
9073 and then Nkind (Def) = N_Full_Type_Declaration
9074 then
9075 Set_Interfaces
9076 (Defining_Identifier (Def), Ifaces_List);
9077 end if;
9078 end;
9079 end if;
9080
9081 -- A type extension is automatically Ghost when one of its
9082 -- progenitors is Ghost (SPARK RM 6.9(9)). This property is
9083 -- also inherited when the parent type is Ghost, but this is
9084 -- done in Build_Derived_Type as the mechanism also handles
9085 -- untagged derivations.
9086
9087 if Implements_Ghost_Interface (Derived_Type) then
9088 Set_Is_Ghost_Entity (Derived_Type);
9089 end if;
9090 end;
9091 end if;
9092
9093 else
9094 Set_Is_Packed (Derived_Type, Is_Packed (Parent_Base));
9095 Set_Has_Non_Standard_Rep
9096 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
9097 end if;
9098
9099 -- STEP 4: Inherit components from the parent base and constrain them.
9100 -- Apply the second transformation described in point 6. above.
9101
9102 if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
9103 or else not Has_Discriminants (Parent_Type)
9104 or else not Is_Constrained (Parent_Type)
9105 then
9106 Constrs := Discs;
9107 else
9108 Constrs := Discriminant_Constraint (Parent_Type);
9109 end if;
9110
9111 Assoc_List :=
9112 Inherit_Components
9113 (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
9114
9115 -- STEP 5a: Copy the parent record declaration for untagged types
9116
9117 Set_Has_Implicit_Dereference
9118 (Derived_Type, Has_Implicit_Dereference (Parent_Type));
9119
9120 if not Is_Tagged then
9121
9122 -- Discriminant_Constraint (Derived_Type) has been properly
9123 -- constructed. Save it and temporarily set it to Empty because we
9124 -- do not want the call to New_Copy_Tree below to mess this list.
9125
9126 if Has_Discriminants (Derived_Type) then
9127 Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
9128 Set_Discriminant_Constraint (Derived_Type, No_Elist);
9129 else
9130 Save_Discr_Constr := No_Elist;
9131 end if;
9132
9133 -- Save the Etype field of Derived_Type. It is correctly set now,
9134 -- but the call to New_Copy tree may remap it to point to itself,
9135 -- which is not what we want. Ditto for the Next_Entity field.
9136
9137 Save_Etype := Etype (Derived_Type);
9138 Save_Next_Entity := Next_Entity (Derived_Type);
9139
9140 -- Assoc_List maps all stored discriminants in the Parent_Base to
9141 -- stored discriminants in the Derived_Type. It is fundamental that
9142 -- no types or itypes with discriminants other than the stored
9143 -- discriminants appear in the entities declared inside
9144 -- Derived_Type, since the back end cannot deal with it.
9145
9146 New_Decl :=
9147 New_Copy_Tree
9148 (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
9149
9150 -- Restore the fields saved prior to the New_Copy_Tree call
9151 -- and compute the stored constraint.
9152
9153 Set_Etype (Derived_Type, Save_Etype);
9154 Set_Next_Entity (Derived_Type, Save_Next_Entity);
9155
9156 if Has_Discriminants (Derived_Type) then
9157 Set_Discriminant_Constraint
9158 (Derived_Type, Save_Discr_Constr);
9159 Set_Stored_Constraint
9160 (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
9161 Replace_Components (Derived_Type, New_Decl);
9162 end if;
9163
9164 -- Insert the new derived type declaration
9165
9166 Rewrite (N, New_Decl);
9167
9168 -- STEP 5b: Complete the processing for record extensions in generics
9169
9170 -- There is no completion for record extensions declared in the
9171 -- parameter part of a generic, so we need to complete processing for
9172 -- these generic record extensions here. The Record_Type_Definition call
9173 -- will change the Ekind of the components from E_Void to E_Component.
9174
9175 elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
9176 Record_Type_Definition (Empty, Derived_Type);
9177
9178 -- STEP 5c: Process the record extension for non private tagged types
9179
9180 elsif not Private_Extension then
9181 Expand_Record_Extension (Derived_Type, Type_Def);
9182
9183 -- Note : previously in ASIS mode we set the Parent_Subtype of the
9184 -- derived type to propagate some semantic information. This led
9185 -- to other ASIS failures and has been removed.
9186
9187 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
9188 -- implemented interfaces if we are in expansion mode
9189
9190 if Expander_Active
9191 and then Has_Interfaces (Derived_Type)
9192 then
9193 Add_Interface_Tag_Components (N, Derived_Type);
9194 end if;
9195
9196 -- Analyze the record extension
9197
9198 Record_Type_Definition
9199 (Record_Extension_Part (Type_Def), Derived_Type);
9200 end if;
9201
9202 End_Scope;
9203
9204 -- Nothing else to do if there is an error in the derivation.
9205 -- An unusual case: the full view may be derived from a type in an
9206 -- instance, when the partial view was used illegally as an actual
9207 -- in that instance, leading to a circular definition.
9208
9209 if Etype (Derived_Type) = Any_Type
9210 or else Etype (Parent_Type) = Derived_Type
9211 then
9212 return;
9213 end if;
9214
9215 -- Set delayed freeze and then derive subprograms, we need to do
9216 -- this in this order so that derived subprograms inherit the
9217 -- derived freeze if necessary.
9218
9219 Set_Has_Delayed_Freeze (Derived_Type);
9220
9221 if Derive_Subps then
9222 Derive_Subprograms (Parent_Type, Derived_Type);
9223 end if;
9224
9225 -- If we have a private extension which defines a constrained derived
9226 -- type mark as constrained here after we have derived subprograms. See
9227 -- comment on point 9. just above the body of Build_Derived_Record_Type.
9228
9229 if Private_Extension and then Inherit_Discrims then
9230 if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
9231 Set_Is_Constrained (Derived_Type, True);
9232 Set_Discriminant_Constraint (Derived_Type, Discs);
9233
9234 elsif Is_Constrained (Parent_Type) then
9235 Set_Is_Constrained
9236 (Derived_Type, True);
9237 Set_Discriminant_Constraint
9238 (Derived_Type, Discriminant_Constraint (Parent_Type));
9239 end if;
9240 end if;
9241
9242 -- Update the class-wide type, which shares the now-completed entity
9243 -- list with its specific type. In case of underlying record views,
9244 -- we do not generate the corresponding class wide entity.
9245
9246 if Is_Tagged
9247 and then not Is_Underlying_Record_View (Derived_Type)
9248 then
9249 Set_First_Entity
9250 (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
9251 Set_Last_Entity
9252 (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
9253 end if;
9254
9255 Check_Function_Writable_Actuals (N);
9256 end Build_Derived_Record_Type;
9257
9258 ------------------------
9259 -- Build_Derived_Type --
9260 ------------------------
9261
9262 procedure Build_Derived_Type
9263 (N : Node_Id;
9264 Parent_Type : Entity_Id;
9265 Derived_Type : Entity_Id;
9266 Is_Completion : Boolean;
9267 Derive_Subps : Boolean := True)
9268 is
9269 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
9270
9271 begin
9272 -- Set common attributes
9273
9274 Set_Scope (Derived_Type, Current_Scope);
9275
9276 Set_Etype (Derived_Type, Parent_Base);
9277 Set_Ekind (Derived_Type, Ekind (Parent_Base));
9278 Propagate_Concurrent_Flags (Derived_Type, Parent_Base);
9279
9280 Set_Size_Info (Derived_Type, Parent_Type);
9281 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
9282 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
9283 Set_Disable_Controlled (Derived_Type, Disable_Controlled (Parent_Type));
9284
9285 Set_Is_Tagged_Type (Derived_Type, Is_Tagged_Type (Parent_Type));
9286 Set_Is_Volatile (Derived_Type, Is_Volatile (Parent_Type));
9287
9288 if Is_Tagged_Type (Derived_Type) then
9289 Set_No_Tagged_Streams_Pragma
9290 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
9291 end if;
9292
9293 -- If the parent has primitive routines, set the derived type link
9294
9295 if Has_Primitive_Operations (Parent_Type) then
9296 Set_Derived_Type_Link (Parent_Base, Derived_Type);
9297 end if;
9298
9299 -- If the parent type is a private subtype, the convention on the base
9300 -- type may be set in the private part, and not propagated to the
9301 -- subtype until later, so we obtain the convention from the base type.
9302
9303 Set_Convention (Derived_Type, Convention (Parent_Base));
9304
9305 -- Set SSO default for record or array type
9306
9307 if (Is_Array_Type (Derived_Type) or else Is_Record_Type (Derived_Type))
9308 and then Is_Base_Type (Derived_Type)
9309 then
9310 Set_Default_SSO (Derived_Type);
9311 end if;
9312
9313 -- A derived type inherits the Default_Initial_Condition pragma coming
9314 -- from any parent type within the derivation chain.
9315
9316 if Has_DIC (Parent_Type) then
9317 Set_Has_Inherited_DIC (Derived_Type);
9318 end if;
9319
9320 -- A derived type inherits any class-wide invariants coming from a
9321 -- parent type or an interface. Note that the invariant procedure of
9322 -- the parent type should not be inherited because the derived type may
9323 -- define invariants of its own.
9324
9325 if not Is_Interface (Derived_Type) then
9326 if Has_Inherited_Invariants (Parent_Type)
9327 or else Has_Inheritable_Invariants (Parent_Type)
9328 then
9329 Set_Has_Inherited_Invariants (Derived_Type);
9330
9331 elsif Is_Concurrent_Type (Derived_Type)
9332 or else Is_Tagged_Type (Derived_Type)
9333 then
9334 declare
9335 Iface : Entity_Id;
9336 Ifaces : Elist_Id;
9337 Iface_Elmt : Elmt_Id;
9338
9339 begin
9340 Collect_Interfaces
9341 (T => Derived_Type,
9342 Ifaces_List => Ifaces,
9343 Exclude_Parents => True);
9344
9345 if Present (Ifaces) then
9346 Iface_Elmt := First_Elmt (Ifaces);
9347 while Present (Iface_Elmt) loop
9348 Iface := Node (Iface_Elmt);
9349
9350 if Has_Inheritable_Invariants (Iface) then
9351 Set_Has_Inherited_Invariants (Derived_Type);
9352 exit;
9353 end if;
9354
9355 Next_Elmt (Iface_Elmt);
9356 end loop;
9357 end if;
9358 end;
9359 end if;
9360 end if;
9361
9362 -- We similarly inherit predicates. Note that for scalar derived types
9363 -- the predicate is inherited from the first subtype, and not from its
9364 -- (anonymous) base type.
9365
9366 if Has_Predicates (Parent_Type)
9367 or else Has_Predicates (First_Subtype (Parent_Type))
9368 then
9369 Set_Has_Predicates (Derived_Type);
9370 end if;
9371
9372 -- The derived type inherits the representation clauses of the parent
9373
9374 Inherit_Rep_Item_Chain (Derived_Type, Parent_Type);
9375
9376 -- If the parent type has delayed rep aspects, then mark the derived
9377 -- type as possibly inheriting a delayed rep aspect.
9378
9379 if Has_Delayed_Rep_Aspects (Parent_Type) then
9380 Set_May_Inherit_Delayed_Rep_Aspects (Derived_Type);
9381 end if;
9382
9383 -- A derived type becomes Ghost when its parent type is also Ghost
9384 -- (SPARK RM 6.9(9)). Note that the Ghost-related attributes are not
9385 -- directly inherited because the Ghost policy in effect may differ.
9386
9387 if Is_Ghost_Entity (Parent_Type) then
9388 Set_Is_Ghost_Entity (Derived_Type);
9389 end if;
9390
9391 -- Type dependent processing
9392
9393 case Ekind (Parent_Type) is
9394 when Numeric_Kind =>
9395 Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
9396
9397 when Array_Kind =>
9398 Build_Derived_Array_Type (N, Parent_Type, Derived_Type);
9399
9400 when Class_Wide_Kind
9401 | E_Record_Subtype
9402 | E_Record_Type
9403 =>
9404 Build_Derived_Record_Type
9405 (N, Parent_Type, Derived_Type, Derive_Subps);
9406 return;
9407
9408 when Enumeration_Kind =>
9409 Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
9410
9411 when Access_Kind =>
9412 Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
9413
9414 when Incomplete_Or_Private_Kind =>
9415 Build_Derived_Private_Type
9416 (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
9417
9418 -- For discriminated types, the derivation includes deriving
9419 -- primitive operations. For others it is done below.
9420
9421 if Is_Tagged_Type (Parent_Type)
9422 or else Has_Discriminants (Parent_Type)
9423 or else (Present (Full_View (Parent_Type))
9424 and then Has_Discriminants (Full_View (Parent_Type)))
9425 then
9426 return;
9427 end if;
9428
9429 when Concurrent_Kind =>
9430 Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
9431
9432 when others =>
9433 raise Program_Error;
9434 end case;
9435
9436 -- Nothing more to do if some error occurred
9437
9438 if Etype (Derived_Type) = Any_Type then
9439 return;
9440 end if;
9441
9442 -- Set delayed freeze and then derive subprograms, we need to do this
9443 -- in this order so that derived subprograms inherit the derived freeze
9444 -- if necessary.
9445
9446 Set_Has_Delayed_Freeze (Derived_Type);
9447
9448 if Derive_Subps then
9449 Derive_Subprograms (Parent_Type, Derived_Type);
9450 end if;
9451
9452 Set_Has_Primitive_Operations
9453 (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
9454 end Build_Derived_Type;
9455
9456 -----------------------
9457 -- Build_Discriminal --
9458 -----------------------
9459
9460 procedure Build_Discriminal (Discrim : Entity_Id) is
9461 D_Minal : Entity_Id;
9462 CR_Disc : Entity_Id;
9463
9464 begin
9465 -- A discriminal has the same name as the discriminant
9466
9467 D_Minal := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9468
9469 Set_Ekind (D_Minal, E_In_Parameter);
9470 Set_Mechanism (D_Minal, Default_Mechanism);
9471 Set_Etype (D_Minal, Etype (Discrim));
9472 Set_Scope (D_Minal, Current_Scope);
9473 Set_Parent (D_Minal, Parent (Discrim));
9474
9475 Set_Discriminal (Discrim, D_Minal);
9476 Set_Discriminal_Link (D_Minal, Discrim);
9477
9478 -- For task types, build at once the discriminants of the corresponding
9479 -- record, which are needed if discriminants are used in entry defaults
9480 -- and in family bounds.
9481
9482 if Is_Concurrent_Type (Current_Scope)
9483 or else
9484 Is_Limited_Type (Current_Scope)
9485 then
9486 CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9487
9488 Set_Ekind (CR_Disc, E_In_Parameter);
9489 Set_Mechanism (CR_Disc, Default_Mechanism);
9490 Set_Etype (CR_Disc, Etype (Discrim));
9491 Set_Scope (CR_Disc, Current_Scope);
9492 Set_Discriminal_Link (CR_Disc, Discrim);
9493 Set_CR_Discriminant (Discrim, CR_Disc);
9494 end if;
9495 end Build_Discriminal;
9496
9497 ------------------------------------
9498 -- Build_Discriminant_Constraints --
9499 ------------------------------------
9500
9501 function Build_Discriminant_Constraints
9502 (T : Entity_Id;
9503 Def : Node_Id;
9504 Derived_Def : Boolean := False) return Elist_Id
9505 is
9506 C : constant Node_Id := Constraint (Def);
9507 Nb_Discr : constant Nat := Number_Discriminants (T);
9508
9509 Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
9510 -- Saves the expression corresponding to a given discriminant in T
9511
9512 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
9513 -- Return the Position number within array Discr_Expr of a discriminant
9514 -- D within the discriminant list of the discriminated type T.
9515
9516 procedure Process_Discriminant_Expression
9517 (Expr : Node_Id;
9518 D : Entity_Id);
9519 -- If this is a discriminant constraint on a partial view, do not
9520 -- generate an overflow check on the discriminant expression. The check
9521 -- will be generated when constraining the full view. Otherwise the
9522 -- backend creates duplicate symbols for the temporaries corresponding
9523 -- to the expressions to be checked, causing spurious assembler errors.
9524
9525 ------------------
9526 -- Pos_Of_Discr --
9527 ------------------
9528
9529 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
9530 Disc : Entity_Id;
9531
9532 begin
9533 Disc := First_Discriminant (T);
9534 for J in Discr_Expr'Range loop
9535 if Disc = D then
9536 return J;
9537 end if;
9538
9539 Next_Discriminant (Disc);
9540 end loop;
9541
9542 -- Note: Since this function is called on discriminants that are
9543 -- known to belong to the discriminated type, falling through the
9544 -- loop with no match signals an internal compiler error.
9545
9546 raise Program_Error;
9547 end Pos_Of_Discr;
9548
9549 -------------------------------------
9550 -- Process_Discriminant_Expression --
9551 -------------------------------------
9552
9553 procedure Process_Discriminant_Expression
9554 (Expr : Node_Id;
9555 D : Entity_Id)
9556 is
9557 BDT : constant Entity_Id := Base_Type (Etype (D));
9558
9559 begin
9560 -- If this is a discriminant constraint on a partial view, do
9561 -- not generate an overflow on the discriminant expression. The
9562 -- check will be generated when constraining the full view.
9563
9564 if Is_Private_Type (T)
9565 and then Present (Full_View (T))
9566 then
9567 Analyze_And_Resolve (Expr, BDT, Suppress => Overflow_Check);
9568 else
9569 Analyze_And_Resolve (Expr, BDT);
9570 end if;
9571 end Process_Discriminant_Expression;
9572
9573 -- Declarations local to Build_Discriminant_Constraints
9574
9575 Discr : Entity_Id;
9576 E : Entity_Id;
9577 Elist : constant Elist_Id := New_Elmt_List;
9578
9579 Constr : Node_Id;
9580 Expr : Node_Id;
9581 Id : Node_Id;
9582 Position : Nat;
9583 Found : Boolean;
9584
9585 Discrim_Present : Boolean := False;
9586
9587 -- Start of processing for Build_Discriminant_Constraints
9588
9589 begin
9590 -- The following loop will process positional associations only.
9591 -- For a positional association, the (single) discriminant is
9592 -- implicitly specified by position, in textual order (RM 3.7.2).
9593
9594 Discr := First_Discriminant (T);
9595 Constr := First (Constraints (C));
9596 for D in Discr_Expr'Range loop
9597 exit when Nkind (Constr) = N_Discriminant_Association;
9598
9599 if No (Constr) then
9600 Error_Msg_N ("too few discriminants given in constraint", C);
9601 return New_Elmt_List;
9602
9603 elsif Nkind (Constr) = N_Range
9604 or else (Nkind (Constr) = N_Attribute_Reference
9605 and then Attribute_Name (Constr) = Name_Range)
9606 then
9607 Error_Msg_N
9608 ("a range is not a valid discriminant constraint", Constr);
9609 Discr_Expr (D) := Error;
9610
9611 else
9612 Process_Discriminant_Expression (Constr, Discr);
9613 Discr_Expr (D) := Constr;
9614 end if;
9615
9616 Next_Discriminant (Discr);
9617 Next (Constr);
9618 end loop;
9619
9620 if No (Discr) and then Present (Constr) then
9621 Error_Msg_N ("too many discriminants given in constraint", Constr);
9622 return New_Elmt_List;
9623 end if;
9624
9625 -- Named associations can be given in any order, but if both positional
9626 -- and named associations are used in the same discriminant constraint,
9627 -- then positional associations must occur first, at their normal
9628 -- position. Hence once a named association is used, the rest of the
9629 -- discriminant constraint must use only named associations.
9630
9631 while Present (Constr) loop
9632
9633 -- Positional association forbidden after a named association
9634
9635 if Nkind (Constr) /= N_Discriminant_Association then
9636 Error_Msg_N ("positional association follows named one", Constr);
9637 return New_Elmt_List;
9638
9639 -- Otherwise it is a named association
9640
9641 else
9642 -- E records the type of the discriminants in the named
9643 -- association. All the discriminants specified in the same name
9644 -- association must have the same type.
9645
9646 E := Empty;
9647
9648 -- Search the list of discriminants in T to see if the simple name
9649 -- given in the constraint matches any of them.
9650
9651 Id := First (Selector_Names (Constr));
9652 while Present (Id) loop
9653 Found := False;
9654
9655 -- If Original_Discriminant is present, we are processing a
9656 -- generic instantiation and this is an instance node. We need
9657 -- to find the name of the corresponding discriminant in the
9658 -- actual record type T and not the name of the discriminant in
9659 -- the generic formal. Example:
9660
9661 -- generic
9662 -- type G (D : int) is private;
9663 -- package P is
9664 -- subtype W is G (D => 1);
9665 -- end package;
9666 -- type Rec (X : int) is record ... end record;
9667 -- package Q is new P (G => Rec);
9668
9669 -- At the point of the instantiation, formal type G is Rec
9670 -- and therefore when reanalyzing "subtype W is G (D => 1);"
9671 -- which really looks like "subtype W is Rec (D => 1);" at
9672 -- the point of instantiation, we want to find the discriminant
9673 -- that corresponds to D in Rec, i.e. X.
9674
9675 if Present (Original_Discriminant (Id))
9676 and then In_Instance
9677 then
9678 Discr := Find_Corresponding_Discriminant (Id, T);
9679 Found := True;
9680
9681 else
9682 Discr := First_Discriminant (T);
9683 while Present (Discr) loop
9684 if Chars (Discr) = Chars (Id) then
9685 Found := True;
9686 exit;
9687 end if;
9688
9689 Next_Discriminant (Discr);
9690 end loop;
9691
9692 if not Found then
9693 Error_Msg_N ("& does not match any discriminant", Id);
9694 return New_Elmt_List;
9695
9696 -- If the parent type is a generic formal, preserve the
9697 -- name of the discriminant for subsequent instances.
9698 -- see comment at the beginning of this if statement.
9699
9700 elsif Is_Generic_Type (Root_Type (T)) then
9701 Set_Original_Discriminant (Id, Discr);
9702 end if;
9703 end if;
9704
9705 Position := Pos_Of_Discr (T, Discr);
9706
9707 if Present (Discr_Expr (Position)) then
9708 Error_Msg_N ("duplicate constraint for discriminant&", Id);
9709
9710 else
9711 -- Each discriminant specified in the same named association
9712 -- must be associated with a separate copy of the
9713 -- corresponding expression.
9714
9715 if Present (Next (Id)) then
9716 Expr := New_Copy_Tree (Expression (Constr));
9717 Set_Parent (Expr, Parent (Expression (Constr)));
9718 else
9719 Expr := Expression (Constr);
9720 end if;
9721
9722 Discr_Expr (Position) := Expr;
9723 Process_Discriminant_Expression (Expr, Discr);
9724 end if;
9725
9726 -- A discriminant association with more than one discriminant
9727 -- name is only allowed if the named discriminants are all of
9728 -- the same type (RM 3.7.1(8)).
9729
9730 if E = Empty then
9731 E := Base_Type (Etype (Discr));
9732
9733 elsif Base_Type (Etype (Discr)) /= E then
9734 Error_Msg_N
9735 ("all discriminants in an association " &
9736 "must have the same type", Id);
9737 end if;
9738
9739 Next (Id);
9740 end loop;
9741 end if;
9742
9743 Next (Constr);
9744 end loop;
9745
9746 -- A discriminant constraint must provide exactly one value for each
9747 -- discriminant of the type (RM 3.7.1(8)).
9748
9749 for J in Discr_Expr'Range loop
9750 if No (Discr_Expr (J)) then
9751 Error_Msg_N ("too few discriminants given in constraint", C);
9752 return New_Elmt_List;
9753 end if;
9754 end loop;
9755
9756 -- Determine if there are discriminant expressions in the constraint
9757
9758 for J in Discr_Expr'Range loop
9759 if Denotes_Discriminant
9760 (Discr_Expr (J), Check_Concurrent => True)
9761 then
9762 Discrim_Present := True;
9763 end if;
9764 end loop;
9765
9766 -- Build an element list consisting of the expressions given in the
9767 -- discriminant constraint and apply the appropriate checks. The list
9768 -- is constructed after resolving any named discriminant associations
9769 -- and therefore the expressions appear in the textual order of the
9770 -- discriminants.
9771
9772 Discr := First_Discriminant (T);
9773 for J in Discr_Expr'Range loop
9774 if Discr_Expr (J) /= Error then
9775 Append_Elmt (Discr_Expr (J), Elist);
9776
9777 -- If any of the discriminant constraints is given by a
9778 -- discriminant and we are in a derived type declaration we
9779 -- have a discriminant renaming. Establish link between new
9780 -- and old discriminant. The new discriminant has an implicit
9781 -- dereference if the old one does.
9782
9783 if Denotes_Discriminant (Discr_Expr (J)) then
9784 if Derived_Def then
9785 declare
9786 New_Discr : constant Entity_Id := Entity (Discr_Expr (J));
9787
9788 begin
9789 Set_Corresponding_Discriminant (New_Discr, Discr);
9790 Set_Has_Implicit_Dereference (New_Discr,
9791 Has_Implicit_Dereference (Discr));
9792 end;
9793 end if;
9794
9795 -- Force the evaluation of non-discriminant expressions.
9796 -- If we have found a discriminant in the constraint 3.4(26)
9797 -- and 3.8(18) demand that no range checks are performed are
9798 -- after evaluation. If the constraint is for a component
9799 -- definition that has a per-object constraint, expressions are
9800 -- evaluated but not checked either. In all other cases perform
9801 -- a range check.
9802
9803 else
9804 if Discrim_Present then
9805 null;
9806
9807 elsif Nkind (Parent (Parent (Def))) = N_Component_Declaration
9808 and then Has_Per_Object_Constraint
9809 (Defining_Identifier (Parent (Parent (Def))))
9810 then
9811 null;
9812
9813 elsif Is_Access_Type (Etype (Discr)) then
9814 Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
9815
9816 else
9817 Apply_Range_Check (Discr_Expr (J), Etype (Discr));
9818 end if;
9819
9820 Force_Evaluation (Discr_Expr (J));
9821 end if;
9822
9823 -- Check that the designated type of an access discriminant's
9824 -- expression is not a class-wide type unless the discriminant's
9825 -- designated type is also class-wide.
9826
9827 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
9828 and then not Is_Class_Wide_Type
9829 (Designated_Type (Etype (Discr)))
9830 and then Etype (Discr_Expr (J)) /= Any_Type
9831 and then Is_Class_Wide_Type
9832 (Designated_Type (Etype (Discr_Expr (J))))
9833 then
9834 Wrong_Type (Discr_Expr (J), Etype (Discr));
9835
9836 elsif Is_Access_Type (Etype (Discr))
9837 and then not Is_Access_Constant (Etype (Discr))
9838 and then Is_Access_Type (Etype (Discr_Expr (J)))
9839 and then Is_Access_Constant (Etype (Discr_Expr (J)))
9840 then
9841 Error_Msg_NE
9842 ("constraint for discriminant& must be access to variable",
9843 Def, Discr);
9844 end if;
9845 end if;
9846
9847 Next_Discriminant (Discr);
9848 end loop;
9849
9850 return Elist;
9851 end Build_Discriminant_Constraints;
9852
9853 ---------------------------------
9854 -- Build_Discriminated_Subtype --
9855 ---------------------------------
9856
9857 procedure Build_Discriminated_Subtype
9858 (T : Entity_Id;
9859 Def_Id : Entity_Id;
9860 Elist : Elist_Id;
9861 Related_Nod : Node_Id;
9862 For_Access : Boolean := False)
9863 is
9864 Has_Discrs : constant Boolean := Has_Discriminants (T);
9865 Constrained : constant Boolean :=
9866 (Has_Discrs
9867 and then not Is_Empty_Elmt_List (Elist)
9868 and then not Is_Class_Wide_Type (T))
9869 or else Is_Constrained (T);
9870
9871 begin
9872 if Ekind (T) = E_Record_Type then
9873 if For_Access then
9874 Set_Ekind (Def_Id, E_Private_Subtype);
9875 Set_Is_For_Access_Subtype (Def_Id, True);
9876 else
9877 Set_Ekind (Def_Id, E_Record_Subtype);
9878 end if;
9879
9880 -- Inherit preelaboration flag from base, for types for which it
9881 -- may have been set: records, private types, protected types.
9882
9883 Set_Known_To_Have_Preelab_Init
9884 (Def_Id, Known_To_Have_Preelab_Init (T));
9885
9886 elsif Ekind (T) = E_Task_Type then
9887 Set_Ekind (Def_Id, E_Task_Subtype);
9888
9889 elsif Ekind (T) = E_Protected_Type then
9890 Set_Ekind (Def_Id, E_Protected_Subtype);
9891 Set_Known_To_Have_Preelab_Init
9892 (Def_Id, Known_To_Have_Preelab_Init (T));
9893
9894 elsif Is_Private_Type (T) then
9895 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
9896 Set_Known_To_Have_Preelab_Init
9897 (Def_Id, Known_To_Have_Preelab_Init (T));
9898
9899 -- Private subtypes may have private dependents
9900
9901 Set_Private_Dependents (Def_Id, New_Elmt_List);
9902
9903 elsif Is_Class_Wide_Type (T) then
9904 Set_Ekind (Def_Id, E_Class_Wide_Subtype);
9905
9906 else
9907 -- Incomplete type. Attach subtype to list of dependents, to be
9908 -- completed with full view of parent type, unless is it the
9909 -- designated subtype of a record component within an init_proc.
9910 -- This last case arises for a component of an access type whose
9911 -- designated type is incomplete (e.g. a Taft Amendment type).
9912 -- The designated subtype is within an inner scope, and needs no
9913 -- elaboration, because only the access type is needed in the
9914 -- initialization procedure.
9915
9916 Set_Ekind (Def_Id, Ekind (T));
9917
9918 if For_Access and then Within_Init_Proc then
9919 null;
9920 else
9921 Append_Elmt (Def_Id, Private_Dependents (T));
9922 end if;
9923 end if;
9924
9925 Set_Etype (Def_Id, T);
9926 Init_Size_Align (Def_Id);
9927 Set_Has_Discriminants (Def_Id, Has_Discrs);
9928 Set_Is_Constrained (Def_Id, Constrained);
9929
9930 Set_First_Entity (Def_Id, First_Entity (T));
9931 Set_Last_Entity (Def_Id, Last_Entity (T));
9932 Set_Has_Implicit_Dereference
9933 (Def_Id, Has_Implicit_Dereference (T));
9934 Set_Has_Pragma_Unreferenced_Objects
9935 (Def_Id, Has_Pragma_Unreferenced_Objects (T));
9936
9937 -- If the subtype is the completion of a private declaration, there may
9938 -- have been representation clauses for the partial view, and they must
9939 -- be preserved. Build_Derived_Type chains the inherited clauses with
9940 -- the ones appearing on the extension. If this comes from a subtype
9941 -- declaration, all clauses are inherited.
9942
9943 if No (First_Rep_Item (Def_Id)) then
9944 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
9945 end if;
9946
9947 if Is_Tagged_Type (T) then
9948 Set_Is_Tagged_Type (Def_Id);
9949 Set_No_Tagged_Streams_Pragma (Def_Id, No_Tagged_Streams_Pragma (T));
9950 Make_Class_Wide_Type (Def_Id);
9951 end if;
9952
9953 Set_Stored_Constraint (Def_Id, No_Elist);
9954
9955 if Has_Discrs then
9956 Set_Discriminant_Constraint (Def_Id, Elist);
9957 Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
9958 end if;
9959
9960 if Is_Tagged_Type (T) then
9961
9962 -- Ada 2005 (AI-251): In case of concurrent types we inherit the
9963 -- concurrent record type (which has the list of primitive
9964 -- operations).
9965
9966 if Ada_Version >= Ada_2005
9967 and then Is_Concurrent_Type (T)
9968 then
9969 Set_Corresponding_Record_Type (Def_Id,
9970 Corresponding_Record_Type (T));
9971 else
9972 Set_Direct_Primitive_Operations (Def_Id,
9973 Direct_Primitive_Operations (T));
9974 end if;
9975
9976 Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
9977 end if;
9978
9979 -- Subtypes introduced by component declarations do not need to be
9980 -- marked as delayed, and do not get freeze nodes, because the semantics
9981 -- verifies that the parents of the subtypes are frozen before the
9982 -- enclosing record is frozen.
9983
9984 if not Is_Type (Scope (Def_Id)) then
9985 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
9986
9987 if Is_Private_Type (T)
9988 and then Present (Full_View (T))
9989 then
9990 Conditional_Delay (Def_Id, Full_View (T));
9991 else
9992 Conditional_Delay (Def_Id, T);
9993 end if;
9994 end if;
9995
9996 if Is_Record_Type (T) then
9997 Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
9998
9999 if Has_Discrs
10000 and then not Is_Empty_Elmt_List (Elist)
10001 and then not For_Access
10002 then
10003 Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
10004 elsif not For_Access then
10005 Set_Cloned_Subtype (Def_Id, T);
10006 end if;
10007 end if;
10008 end Build_Discriminated_Subtype;
10009
10010 ---------------------------
10011 -- Build_Itype_Reference --
10012 ---------------------------
10013
10014 procedure Build_Itype_Reference
10015 (Ityp : Entity_Id;
10016 Nod : Node_Id)
10017 is
10018 IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
10019 begin
10020
10021 -- Itype references are only created for use by the back-end
10022
10023 if Inside_A_Generic then
10024 return;
10025 else
10026 Set_Itype (IR, Ityp);
10027 Insert_After (Nod, IR);
10028 end if;
10029 end Build_Itype_Reference;
10030
10031 ------------------------
10032 -- Build_Scalar_Bound --
10033 ------------------------
10034
10035 function Build_Scalar_Bound
10036 (Bound : Node_Id;
10037 Par_T : Entity_Id;
10038 Der_T : Entity_Id) return Node_Id
10039 is
10040 New_Bound : Entity_Id;
10041
10042 begin
10043 -- Note: not clear why this is needed, how can the original bound
10044 -- be unanalyzed at this point? and if it is, what business do we
10045 -- have messing around with it? and why is the base type of the
10046 -- parent type the right type for the resolution. It probably is
10047 -- not. It is OK for the new bound we are creating, but not for
10048 -- the old one??? Still if it never happens, no problem.
10049
10050 Analyze_And_Resolve (Bound, Base_Type (Par_T));
10051
10052 if Nkind_In (Bound, N_Integer_Literal, N_Real_Literal) then
10053 New_Bound := New_Copy (Bound);
10054 Set_Etype (New_Bound, Der_T);
10055 Set_Analyzed (New_Bound);
10056
10057 elsif Is_Entity_Name (Bound) then
10058 New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
10059
10060 -- The following is almost certainly wrong. What business do we have
10061 -- relocating a node (Bound) that is presumably still attached to
10062 -- the tree elsewhere???
10063
10064 else
10065 New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
10066 end if;
10067
10068 Set_Etype (New_Bound, Der_T);
10069 return New_Bound;
10070 end Build_Scalar_Bound;
10071
10072 --------------------------------
10073 -- Build_Underlying_Full_View --
10074 --------------------------------
10075
10076 procedure Build_Underlying_Full_View
10077 (N : Node_Id;
10078 Typ : Entity_Id;
10079 Par : Entity_Id)
10080 is
10081 Loc : constant Source_Ptr := Sloc (N);
10082 Subt : constant Entity_Id :=
10083 Make_Defining_Identifier
10084 (Loc, New_External_Name (Chars (Typ), 'S'));
10085
10086 Constr : Node_Id;
10087 Indic : Node_Id;
10088 C : Node_Id;
10089 Id : Node_Id;
10090
10091 procedure Set_Discriminant_Name (Id : Node_Id);
10092 -- If the derived type has discriminants, they may rename discriminants
10093 -- of the parent. When building the full view of the parent, we need to
10094 -- recover the names of the original discriminants if the constraint is
10095 -- given by named associations.
10096
10097 ---------------------------
10098 -- Set_Discriminant_Name --
10099 ---------------------------
10100
10101 procedure Set_Discriminant_Name (Id : Node_Id) is
10102 Disc : Entity_Id;
10103
10104 begin
10105 Set_Original_Discriminant (Id, Empty);
10106
10107 if Has_Discriminants (Typ) then
10108 Disc := First_Discriminant (Typ);
10109 while Present (Disc) loop
10110 if Chars (Disc) = Chars (Id)
10111 and then Present (Corresponding_Discriminant (Disc))
10112 then
10113 Set_Chars (Id, Chars (Corresponding_Discriminant (Disc)));
10114 end if;
10115 Next_Discriminant (Disc);
10116 end loop;
10117 end if;
10118 end Set_Discriminant_Name;
10119
10120 -- Start of processing for Build_Underlying_Full_View
10121
10122 begin
10123 if Nkind (N) = N_Full_Type_Declaration then
10124 Constr := Constraint (Subtype_Indication (Type_Definition (N)));
10125
10126 elsif Nkind (N) = N_Subtype_Declaration then
10127 Constr := New_Copy_Tree (Constraint (Subtype_Indication (N)));
10128
10129 elsif Nkind (N) = N_Component_Declaration then
10130 Constr :=
10131 New_Copy_Tree
10132 (Constraint (Subtype_Indication (Component_Definition (N))));
10133
10134 else
10135 raise Program_Error;
10136 end if;
10137
10138 C := First (Constraints (Constr));
10139 while Present (C) loop
10140 if Nkind (C) = N_Discriminant_Association then
10141 Id := First (Selector_Names (C));
10142 while Present (Id) loop
10143 Set_Discriminant_Name (Id);
10144 Next (Id);
10145 end loop;
10146 end if;
10147
10148 Next (C);
10149 end loop;
10150
10151 Indic :=
10152 Make_Subtype_Declaration (Loc,
10153 Defining_Identifier => Subt,
10154 Subtype_Indication =>
10155 Make_Subtype_Indication (Loc,
10156 Subtype_Mark => New_Occurrence_Of (Par, Loc),
10157 Constraint => New_Copy_Tree (Constr)));
10158
10159 -- If this is a component subtype for an outer itype, it is not
10160 -- a list member, so simply set the parent link for analysis: if
10161 -- the enclosing type does not need to be in a declarative list,
10162 -- neither do the components.
10163
10164 if Is_List_Member (N)
10165 and then Nkind (N) /= N_Component_Declaration
10166 then
10167 Insert_Before (N, Indic);
10168 else
10169 Set_Parent (Indic, Parent (N));
10170 end if;
10171
10172 Analyze (Indic);
10173 Set_Underlying_Full_View (Typ, Full_View (Subt));
10174 Set_Is_Underlying_Full_View (Full_View (Subt));
10175 end Build_Underlying_Full_View;
10176
10177 -------------------------------
10178 -- Check_Abstract_Overriding --
10179 -------------------------------
10180
10181 procedure Check_Abstract_Overriding (T : Entity_Id) is
10182 Alias_Subp : Entity_Id;
10183 Elmt : Elmt_Id;
10184 Op_List : Elist_Id;
10185 Subp : Entity_Id;
10186 Type_Def : Node_Id;
10187
10188 procedure Check_Pragma_Implemented (Subp : Entity_Id);
10189 -- Ada 2012 (AI05-0030): Subprogram Subp overrides an interface routine
10190 -- which has pragma Implemented already set. Check whether Subp's entity
10191 -- kind conforms to the implementation kind of the overridden routine.
10192
10193 procedure Check_Pragma_Implemented
10194 (Subp : Entity_Id;
10195 Iface_Subp : Entity_Id);
10196 -- Ada 2012 (AI05-0030): Subprogram Subp overrides interface routine
10197 -- Iface_Subp and both entities have pragma Implemented already set on
10198 -- them. Check whether the two implementation kinds are conforming.
10199
10200 procedure Inherit_Pragma_Implemented
10201 (Subp : Entity_Id;
10202 Iface_Subp : Entity_Id);
10203 -- Ada 2012 (AI05-0030): Interface primitive Subp overrides interface
10204 -- subprogram Iface_Subp which has been marked by pragma Implemented.
10205 -- Propagate the implementation kind of Iface_Subp to Subp.
10206
10207 ------------------------------
10208 -- Check_Pragma_Implemented --
10209 ------------------------------
10210
10211 procedure Check_Pragma_Implemented (Subp : Entity_Id) is
10212 Iface_Alias : constant Entity_Id := Interface_Alias (Subp);
10213 Impl_Kind : constant Name_Id := Implementation_Kind (Iface_Alias);
10214 Subp_Alias : constant Entity_Id := Alias (Subp);
10215 Contr_Typ : Entity_Id;
10216 Impl_Subp : Entity_Id;
10217
10218 begin
10219 -- Subp must have an alias since it is a hidden entity used to link
10220 -- an interface subprogram to its overriding counterpart.
10221
10222 pragma Assert (Present (Subp_Alias));
10223
10224 -- Handle aliases to synchronized wrappers
10225
10226 Impl_Subp := Subp_Alias;
10227
10228 if Is_Primitive_Wrapper (Impl_Subp) then
10229 Impl_Subp := Wrapped_Entity (Impl_Subp);
10230 end if;
10231
10232 -- Extract the type of the controlling formal
10233
10234 Contr_Typ := Etype (First_Formal (Subp_Alias));
10235
10236 if Is_Concurrent_Record_Type (Contr_Typ) then
10237 Contr_Typ := Corresponding_Concurrent_Type (Contr_Typ);
10238 end if;
10239
10240 -- An interface subprogram whose implementation kind is By_Entry must
10241 -- be implemented by an entry.
10242
10243 if Impl_Kind = Name_By_Entry
10244 and then Ekind (Impl_Subp) /= E_Entry
10245 then
10246 Error_Msg_Node_2 := Iface_Alias;
10247 Error_Msg_NE
10248 ("type & must implement abstract subprogram & with an entry",
10249 Subp_Alias, Contr_Typ);
10250
10251 elsif Impl_Kind = Name_By_Protected_Procedure then
10252
10253 -- An interface subprogram whose implementation kind is By_
10254 -- Protected_Procedure cannot be implemented by a primitive
10255 -- procedure of a task type.
10256
10257 if Ekind (Contr_Typ) /= E_Protected_Type then
10258 Error_Msg_Node_2 := Contr_Typ;
10259 Error_Msg_NE
10260 ("interface subprogram & cannot be implemented by a " &
10261 "primitive procedure of task type &", Subp_Alias,
10262 Iface_Alias);
10263
10264 -- An interface subprogram whose implementation kind is By_
10265 -- Protected_Procedure must be implemented by a procedure.
10266
10267 elsif Ekind (Impl_Subp) /= E_Procedure then
10268 Error_Msg_Node_2 := Iface_Alias;
10269 Error_Msg_NE
10270 ("type & must implement abstract subprogram & with a " &
10271 "procedure", Subp_Alias, Contr_Typ);
10272
10273 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
10274 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
10275 then
10276 Error_Msg_Name_1 := Impl_Kind;
10277 Error_Msg_N
10278 ("overriding operation& must have synchronization%",
10279 Subp_Alias);
10280 end if;
10281
10282 -- If primitive has Optional synchronization, overriding operation
10283 -- must match if it has an explicit synchronization..
10284
10285 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
10286 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
10287 then
10288 Error_Msg_Name_1 := Impl_Kind;
10289 Error_Msg_N
10290 ("overriding operation& must have syncrhonization%",
10291 Subp_Alias);
10292 end if;
10293 end Check_Pragma_Implemented;
10294
10295 ------------------------------
10296 -- Check_Pragma_Implemented --
10297 ------------------------------
10298
10299 procedure Check_Pragma_Implemented
10300 (Subp : Entity_Id;
10301 Iface_Subp : Entity_Id)
10302 is
10303 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
10304 Subp_Kind : constant Name_Id := Implementation_Kind (Subp);
10305
10306 begin
10307 -- Ada 2012 (AI05-0030): The implementation kinds of an overridden
10308 -- and overriding subprogram are different. In general this is an
10309 -- error except when the implementation kind of the overridden
10310 -- subprograms is By_Any or Optional.
10311
10312 if Iface_Kind /= Subp_Kind
10313 and then Iface_Kind /= Name_By_Any
10314 and then Iface_Kind /= Name_Optional
10315 then
10316 if Iface_Kind = Name_By_Entry then
10317 Error_Msg_N
10318 ("incompatible implementation kind, overridden subprogram " &
10319 "is marked By_Entry", Subp);
10320 else
10321 Error_Msg_N
10322 ("incompatible implementation kind, overridden subprogram " &
10323 "is marked By_Protected_Procedure", Subp);
10324 end if;
10325 end if;
10326 end Check_Pragma_Implemented;
10327
10328 --------------------------------
10329 -- Inherit_Pragma_Implemented --
10330 --------------------------------
10331
10332 procedure Inherit_Pragma_Implemented
10333 (Subp : Entity_Id;
10334 Iface_Subp : Entity_Id)
10335 is
10336 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
10337 Loc : constant Source_Ptr := Sloc (Subp);
10338 Impl_Prag : Node_Id;
10339
10340 begin
10341 -- Since the implementation kind is stored as a representation item
10342 -- rather than a flag, create a pragma node.
10343
10344 Impl_Prag :=
10345 Make_Pragma (Loc,
10346 Chars => Name_Implemented,
10347 Pragma_Argument_Associations => New_List (
10348 Make_Pragma_Argument_Association (Loc,
10349 Expression => New_Occurrence_Of (Subp, Loc)),
10350
10351 Make_Pragma_Argument_Association (Loc,
10352 Expression => Make_Identifier (Loc, Iface_Kind))));
10353
10354 -- The pragma doesn't need to be analyzed because it is internally
10355 -- built. It is safe to directly register it as a rep item since we
10356 -- are only interested in the characters of the implementation kind.
10357
10358 Record_Rep_Item (Subp, Impl_Prag);
10359 end Inherit_Pragma_Implemented;
10360
10361 -- Start of processing for Check_Abstract_Overriding
10362
10363 begin
10364 Op_List := Primitive_Operations (T);
10365
10366 -- Loop to check primitive operations
10367
10368 Elmt := First_Elmt (Op_List);
10369 while Present (Elmt) loop
10370 Subp := Node (Elmt);
10371 Alias_Subp := Alias (Subp);
10372
10373 -- Inherited subprograms are identified by the fact that they do not
10374 -- come from source, and the associated source location is the
10375 -- location of the first subtype of the derived type.
10376
10377 -- Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
10378 -- subprograms that "require overriding".
10379
10380 -- Special exception, do not complain about failure to override the
10381 -- stream routines _Input and _Output, as well as the primitive
10382 -- operations used in dispatching selects since we always provide
10383 -- automatic overridings for these subprograms.
10384
10385 -- The partial view of T may have been a private extension, for
10386 -- which inherited functions dispatching on result are abstract.
10387 -- If the full view is a null extension, there is no need for
10388 -- overriding in Ada 2005, but wrappers need to be built for them
10389 -- (see exp_ch3, Build_Controlling_Function_Wrappers).
10390
10391 if Is_Null_Extension (T)
10392 and then Has_Controlling_Result (Subp)
10393 and then Ada_Version >= Ada_2005
10394 and then Present (Alias_Subp)
10395 and then not Comes_From_Source (Subp)
10396 and then not Is_Abstract_Subprogram (Alias_Subp)
10397 and then not Is_Access_Type (Etype (Subp))
10398 then
10399 null;
10400
10401 -- Ada 2005 (AI-251): Internal entities of interfaces need no
10402 -- processing because this check is done with the aliased
10403 -- entity
10404
10405 elsif Present (Interface_Alias (Subp)) then
10406 null;
10407
10408 elsif (Is_Abstract_Subprogram (Subp)
10409 or else Requires_Overriding (Subp)
10410 or else
10411 (Has_Controlling_Result (Subp)
10412 and then Present (Alias_Subp)
10413 and then not Comes_From_Source (Subp)
10414 and then Sloc (Subp) = Sloc (First_Subtype (T))))
10415 and then not Is_TSS (Subp, TSS_Stream_Input)
10416 and then not Is_TSS (Subp, TSS_Stream_Output)
10417 and then not Is_Abstract_Type (T)
10418 and then not Is_Predefined_Interface_Primitive (Subp)
10419
10420 -- Ada 2005 (AI-251): Do not consider hidden entities associated
10421 -- with abstract interface types because the check will be done
10422 -- with the aliased entity (otherwise we generate a duplicated
10423 -- error message).
10424
10425 and then not Present (Interface_Alias (Subp))
10426 then
10427 if Present (Alias_Subp) then
10428
10429 -- Only perform the check for a derived subprogram when the
10430 -- type has an explicit record extension. This avoids incorrect
10431 -- flagging of abstract subprograms for the case of a type
10432 -- without an extension that is derived from a formal type
10433 -- with a tagged actual (can occur within a private part).
10434
10435 -- Ada 2005 (AI-391): In the case of an inherited function with
10436 -- a controlling result of the type, the rule does not apply if
10437 -- the type is a null extension (unless the parent function
10438 -- itself is abstract, in which case the function must still be
10439 -- be overridden). The expander will generate an overriding
10440 -- wrapper function calling the parent subprogram (see
10441 -- Exp_Ch3.Make_Controlling_Wrapper_Functions).
10442
10443 Type_Def := Type_Definition (Parent (T));
10444
10445 if Nkind (Type_Def) = N_Derived_Type_Definition
10446 and then Present (Record_Extension_Part (Type_Def))
10447 and then
10448 (Ada_Version < Ada_2005
10449 or else not Is_Null_Extension (T)
10450 or else Ekind (Subp) = E_Procedure
10451 or else not Has_Controlling_Result (Subp)
10452 or else Is_Abstract_Subprogram (Alias_Subp)
10453 or else Requires_Overriding (Subp)
10454 or else Is_Access_Type (Etype (Subp)))
10455 then
10456 -- Avoid reporting error in case of abstract predefined
10457 -- primitive inherited from interface type because the
10458 -- body of internally generated predefined primitives
10459 -- of tagged types are generated later by Freeze_Type
10460
10461 if Is_Interface (Root_Type (T))
10462 and then Is_Abstract_Subprogram (Subp)
10463 and then Is_Predefined_Dispatching_Operation (Subp)
10464 and then not Comes_From_Source (Ultimate_Alias (Subp))
10465 then
10466 null;
10467
10468 -- A null extension is not obliged to override an inherited
10469 -- procedure subject to pragma Extensions_Visible with value
10470 -- False and at least one controlling OUT parameter
10471 -- (SPARK RM 6.1.7(6)).
10472
10473 elsif Is_Null_Extension (T)
10474 and then Is_EVF_Procedure (Subp)
10475 then
10476 null;
10477
10478 else
10479 Error_Msg_NE
10480 ("type must be declared abstract or & overridden",
10481 T, Subp);
10482
10483 -- Traverse the whole chain of aliased subprograms to
10484 -- complete the error notification. This is especially
10485 -- useful for traceability of the chain of entities when
10486 -- the subprogram corresponds with an interface
10487 -- subprogram (which may be defined in another package).
10488
10489 if Present (Alias_Subp) then
10490 declare
10491 E : Entity_Id;
10492
10493 begin
10494 E := Subp;
10495 while Present (Alias (E)) loop
10496
10497 -- Avoid reporting redundant errors on entities
10498 -- inherited from interfaces
10499
10500 if Sloc (E) /= Sloc (T) then
10501 Error_Msg_Sloc := Sloc (E);
10502 Error_Msg_NE
10503 ("\& has been inherited #", T, Subp);
10504 end if;
10505
10506 E := Alias (E);
10507 end loop;
10508
10509 Error_Msg_Sloc := Sloc (E);
10510
10511 -- AI05-0068: report if there is an overriding
10512 -- non-abstract subprogram that is invisible.
10513
10514 if Is_Hidden (E)
10515 and then not Is_Abstract_Subprogram (E)
10516 then
10517 Error_Msg_NE
10518 ("\& subprogram# is not visible",
10519 T, Subp);
10520
10521 -- Clarify the case where a non-null extension must
10522 -- override inherited procedure subject to pragma
10523 -- Extensions_Visible with value False and at least
10524 -- one controlling OUT param.
10525
10526 elsif Is_EVF_Procedure (E) then
10527 Error_Msg_NE
10528 ("\& # is subject to Extensions_Visible False",
10529 T, Subp);
10530
10531 else
10532 Error_Msg_NE
10533 ("\& has been inherited from subprogram #",
10534 T, Subp);
10535 end if;
10536 end;
10537 end if;
10538 end if;
10539
10540 -- Ada 2005 (AI-345): Protected or task type implementing
10541 -- abstract interfaces.
10542
10543 elsif Is_Concurrent_Record_Type (T)
10544 and then Present (Interfaces (T))
10545 then
10546 -- There is no need to check here RM 9.4(11.9/3) since we
10547 -- are processing the corresponding record type and the
10548 -- mode of the overriding subprograms was verified by
10549 -- Check_Conformance when the corresponding concurrent
10550 -- type declaration was analyzed.
10551
10552 Error_Msg_NE
10553 ("interface subprogram & must be overridden", T, Subp);
10554
10555 -- Examine primitive operations of synchronized type to find
10556 -- homonyms that have the wrong profile.
10557
10558 declare
10559 Prim : Entity_Id;
10560
10561 begin
10562 Prim := First_Entity (Corresponding_Concurrent_Type (T));
10563 while Present (Prim) loop
10564 if Chars (Prim) = Chars (Subp) then
10565 Error_Msg_NE
10566 ("profile is not type conformant with prefixed "
10567 & "view profile of inherited operation&",
10568 Prim, Subp);
10569 end if;
10570
10571 Next_Entity (Prim);
10572 end loop;
10573 end;
10574 end if;
10575
10576 else
10577 Error_Msg_Node_2 := T;
10578 Error_Msg_N
10579 ("abstract subprogram& not allowed for type&", Subp);
10580
10581 -- Also post unconditional warning on the type (unconditional
10582 -- so that if there are more than one of these cases, we get
10583 -- them all, and not just the first one).
10584
10585 Error_Msg_Node_2 := Subp;
10586 Error_Msg_N ("nonabstract type& has abstract subprogram&!", T);
10587 end if;
10588
10589 -- A subprogram subject to pragma Extensions_Visible with value
10590 -- "True" cannot override a subprogram subject to the same pragma
10591 -- with value "False" (SPARK RM 6.1.7(5)).
10592
10593 elsif Extensions_Visible_Status (Subp) = Extensions_Visible_True
10594 and then Present (Overridden_Operation (Subp))
10595 and then Extensions_Visible_Status (Overridden_Operation (Subp)) =
10596 Extensions_Visible_False
10597 then
10598 Error_Msg_Sloc := Sloc (Overridden_Operation (Subp));
10599 Error_Msg_N
10600 ("subprogram & with Extensions_Visible True cannot override "
10601 & "subprogram # with Extensions_Visible False", Subp);
10602 end if;
10603
10604 -- Ada 2012 (AI05-0030): Perform checks related to pragma Implemented
10605
10606 -- Subp is an expander-generated procedure which maps an interface
10607 -- alias to a protected wrapper. The interface alias is flagged by
10608 -- pragma Implemented. Ensure that Subp is a procedure when the
10609 -- implementation kind is By_Protected_Procedure or an entry when
10610 -- By_Entry.
10611
10612 if Ada_Version >= Ada_2012
10613 and then Is_Hidden (Subp)
10614 and then Present (Interface_Alias (Subp))
10615 and then Has_Rep_Pragma (Interface_Alias (Subp), Name_Implemented)
10616 then
10617 Check_Pragma_Implemented (Subp);
10618 end if;
10619
10620 -- Subp is an interface primitive which overrides another interface
10621 -- primitive marked with pragma Implemented.
10622
10623 if Ada_Version >= Ada_2012
10624 and then Present (Overridden_Operation (Subp))
10625 and then Has_Rep_Pragma
10626 (Overridden_Operation (Subp), Name_Implemented)
10627 then
10628 -- If the overriding routine is also marked by Implemented, check
10629 -- that the two implementation kinds are conforming.
10630
10631 if Has_Rep_Pragma (Subp, Name_Implemented) then
10632 Check_Pragma_Implemented
10633 (Subp => Subp,
10634 Iface_Subp => Overridden_Operation (Subp));
10635
10636 -- Otherwise the overriding routine inherits the implementation
10637 -- kind from the overridden subprogram.
10638
10639 else
10640 Inherit_Pragma_Implemented
10641 (Subp => Subp,
10642 Iface_Subp => Overridden_Operation (Subp));
10643 end if;
10644 end if;
10645
10646 -- If the operation is a wrapper for a synchronized primitive, it
10647 -- may be called indirectly through a dispatching select. We assume
10648 -- that it will be referenced elsewhere indirectly, and suppress
10649 -- warnings about an unused entity.
10650
10651 if Is_Primitive_Wrapper (Subp)
10652 and then Present (Wrapped_Entity (Subp))
10653 then
10654 Set_Referenced (Wrapped_Entity (Subp));
10655 end if;
10656
10657 Next_Elmt (Elmt);
10658 end loop;
10659 end Check_Abstract_Overriding;
10660
10661 ------------------------------------------------
10662 -- Check_Access_Discriminant_Requires_Limited --
10663 ------------------------------------------------
10664
10665 procedure Check_Access_Discriminant_Requires_Limited
10666 (D : Node_Id;
10667 Loc : Node_Id)
10668 is
10669 begin
10670 -- A discriminant_specification for an access discriminant shall appear
10671 -- only in the declaration for a task or protected type, or for a type
10672 -- with the reserved word 'limited' in its definition or in one of its
10673 -- ancestors (RM 3.7(10)).
10674
10675 -- AI-0063: The proper condition is that type must be immutably limited,
10676 -- or else be a partial view.
10677
10678 if Nkind (Discriminant_Type (D)) = N_Access_Definition then
10679 if Is_Limited_View (Current_Scope)
10680 or else
10681 (Nkind (Parent (Current_Scope)) = N_Private_Type_Declaration
10682 and then Limited_Present (Parent (Current_Scope)))
10683 then
10684 null;
10685
10686 else
10687 Error_Msg_N
10688 ("access discriminants allowed only for limited types", Loc);
10689 end if;
10690 end if;
10691 end Check_Access_Discriminant_Requires_Limited;
10692
10693 -----------------------------------
10694 -- Check_Aliased_Component_Types --
10695 -----------------------------------
10696
10697 procedure Check_Aliased_Component_Types (T : Entity_Id) is
10698 C : Entity_Id;
10699
10700 begin
10701 -- ??? Also need to check components of record extensions, but not
10702 -- components of protected types (which are always limited).
10703
10704 -- Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
10705 -- types to be unconstrained. This is safe because it is illegal to
10706 -- create access subtypes to such types with explicit discriminant
10707 -- constraints.
10708
10709 if not Is_Limited_Type (T) then
10710 if Ekind (T) = E_Record_Type then
10711 C := First_Component (T);
10712 while Present (C) loop
10713 if Is_Aliased (C)
10714 and then Has_Discriminants (Etype (C))
10715 and then not Is_Constrained (Etype (C))
10716 and then not In_Instance_Body
10717 and then Ada_Version < Ada_2005
10718 then
10719 Error_Msg_N
10720 ("aliased component must be constrained (RM 3.6(11))",
10721 C);
10722 end if;
10723
10724 Next_Component (C);
10725 end loop;
10726
10727 elsif Ekind (T) = E_Array_Type then
10728 if Has_Aliased_Components (T)
10729 and then Has_Discriminants (Component_Type (T))
10730 and then not Is_Constrained (Component_Type (T))
10731 and then not In_Instance_Body
10732 and then Ada_Version < Ada_2005
10733 then
10734 Error_Msg_N
10735 ("aliased component type must be constrained (RM 3.6(11))",
10736 T);
10737 end if;
10738 end if;
10739 end if;
10740 end Check_Aliased_Component_Types;
10741
10742 ---------------------------------------
10743 -- Check_Anonymous_Access_Components --
10744 ---------------------------------------
10745
10746 procedure Check_Anonymous_Access_Components
10747 (Typ_Decl : Node_Id;
10748 Typ : Entity_Id;
10749 Prev : Entity_Id;
10750 Comp_List : Node_Id)
10751 is
10752 Loc : constant Source_Ptr := Sloc (Typ_Decl);
10753 Anon_Access : Entity_Id;
10754 Acc_Def : Node_Id;
10755 Comp : Node_Id;
10756 Comp_Def : Node_Id;
10757 Decl : Node_Id;
10758 Type_Def : Node_Id;
10759
10760 procedure Build_Incomplete_Type_Declaration;
10761 -- If the record type contains components that include an access to the
10762 -- current record, then create an incomplete type declaration for the
10763 -- record, to be used as the designated type of the anonymous access.
10764 -- This is done only once, and only if there is no previous partial
10765 -- view of the type.
10766
10767 function Designates_T (Subt : Node_Id) return Boolean;
10768 -- Check whether a node designates the enclosing record type, or 'Class
10769 -- of that type
10770
10771 function Mentions_T (Acc_Def : Node_Id) return Boolean;
10772 -- Check whether an access definition includes a reference to
10773 -- the enclosing record type. The reference can be a subtype mark
10774 -- in the access definition itself, a 'Class attribute reference, or
10775 -- recursively a reference appearing in a parameter specification
10776 -- or result definition of an access_to_subprogram definition.
10777
10778 --------------------------------------
10779 -- Build_Incomplete_Type_Declaration --
10780 --------------------------------------
10781
10782 procedure Build_Incomplete_Type_Declaration is
10783 Decl : Node_Id;
10784 Inc_T : Entity_Id;
10785 H : Entity_Id;
10786
10787 -- Is_Tagged indicates whether the type is tagged. It is tagged if
10788 -- it's "is new ... with record" or else "is tagged record ...".
10789
10790 Is_Tagged : constant Boolean :=
10791 (Nkind (Type_Definition (Typ_Decl)) = N_Derived_Type_Definition
10792 and then
10793 Present (Record_Extension_Part (Type_Definition (Typ_Decl))))
10794 or else
10795 (Nkind (Type_Definition (Typ_Decl)) = N_Record_Definition
10796 and then Tagged_Present (Type_Definition (Typ_Decl)));
10797
10798 begin
10799 -- If there is a previous partial view, no need to create a new one
10800 -- If the partial view, given by Prev, is incomplete, If Prev is
10801 -- a private declaration, full declaration is flagged accordingly.
10802
10803 if Prev /= Typ then
10804 if Is_Tagged then
10805 Make_Class_Wide_Type (Prev);
10806 Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
10807 Set_Etype (Class_Wide_Type (Typ), Typ);
10808 end if;
10809
10810 return;
10811
10812 elsif Has_Private_Declaration (Typ) then
10813
10814 -- If we refer to T'Class inside T, and T is the completion of a
10815 -- private type, then make sure the class-wide type exists.
10816
10817 if Is_Tagged then
10818 Make_Class_Wide_Type (Typ);
10819 end if;
10820
10821 return;
10822
10823 -- If there was a previous anonymous access type, the incomplete
10824 -- type declaration will have been created already.
10825
10826 elsif Present (Current_Entity (Typ))
10827 and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
10828 and then Full_View (Current_Entity (Typ)) = Typ
10829 then
10830 if Is_Tagged
10831 and then Comes_From_Source (Current_Entity (Typ))
10832 and then not Is_Tagged_Type (Current_Entity (Typ))
10833 then
10834 Make_Class_Wide_Type (Typ);
10835 Error_Msg_N
10836 ("incomplete view of tagged type should be declared tagged??",
10837 Parent (Current_Entity (Typ)));
10838 end if;
10839 return;
10840
10841 else
10842 Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
10843 Decl := Make_Incomplete_Type_Declaration (Loc, Inc_T);
10844
10845 -- Type has already been inserted into the current scope. Remove
10846 -- it, and add incomplete declaration for type, so that subsequent
10847 -- anonymous access types can use it. The entity is unchained from
10848 -- the homonym list and from immediate visibility. After analysis,
10849 -- the entity in the incomplete declaration becomes immediately
10850 -- visible in the record declaration that follows.
10851
10852 H := Current_Entity (Typ);
10853
10854 if H = Typ then
10855 Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
10856 else
10857 while Present (H)
10858 and then Homonym (H) /= Typ
10859 loop
10860 H := Homonym (Typ);
10861 end loop;
10862
10863 Set_Homonym (H, Homonym (Typ));
10864 end if;
10865
10866 Insert_Before (Typ_Decl, Decl);
10867 Analyze (Decl);
10868 Set_Full_View (Inc_T, Typ);
10869
10870 if Is_Tagged then
10871
10872 -- Create a common class-wide type for both views, and set the
10873 -- Etype of the class-wide type to the full view.
10874
10875 Make_Class_Wide_Type (Inc_T);
10876 Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
10877 Set_Etype (Class_Wide_Type (Typ), Typ);
10878 end if;
10879 end if;
10880 end Build_Incomplete_Type_Declaration;
10881
10882 ------------------
10883 -- Designates_T --
10884 ------------------
10885
10886 function Designates_T (Subt : Node_Id) return Boolean is
10887 Type_Id : constant Name_Id := Chars (Typ);
10888
10889 function Names_T (Nam : Node_Id) return Boolean;
10890 -- The record type has not been introduced in the current scope
10891 -- yet, so we must examine the name of the type itself, either
10892 -- an identifier T, or an expanded name of the form P.T, where
10893 -- P denotes the current scope.
10894
10895 -------------
10896 -- Names_T --
10897 -------------
10898
10899 function Names_T (Nam : Node_Id) return Boolean is
10900 begin
10901 if Nkind (Nam) = N_Identifier then
10902 return Chars (Nam) = Type_Id;
10903
10904 elsif Nkind (Nam) = N_Selected_Component then
10905 if Chars (Selector_Name (Nam)) = Type_Id then
10906 if Nkind (Prefix (Nam)) = N_Identifier then
10907 return Chars (Prefix (Nam)) = Chars (Current_Scope);
10908
10909 elsif Nkind (Prefix (Nam)) = N_Selected_Component then
10910 return Chars (Selector_Name (Prefix (Nam))) =
10911 Chars (Current_Scope);
10912 else
10913 return False;
10914 end if;
10915
10916 else
10917 return False;
10918 end if;
10919
10920 else
10921 return False;
10922 end if;
10923 end Names_T;
10924
10925 -- Start of processing for Designates_T
10926
10927 begin
10928 if Nkind (Subt) = N_Identifier then
10929 return Chars (Subt) = Type_Id;
10930
10931 -- Reference can be through an expanded name which has not been
10932 -- analyzed yet, and which designates enclosing scopes.
10933
10934 elsif Nkind (Subt) = N_Selected_Component then
10935 if Names_T (Subt) then
10936 return True;
10937
10938 -- Otherwise it must denote an entity that is already visible.
10939 -- The access definition may name a subtype of the enclosing
10940 -- type, if there is a previous incomplete declaration for it.
10941
10942 else
10943 Find_Selected_Component (Subt);
10944 return
10945 Is_Entity_Name (Subt)
10946 and then Scope (Entity (Subt)) = Current_Scope
10947 and then
10948 (Chars (Base_Type (Entity (Subt))) = Type_Id
10949 or else
10950 (Is_Class_Wide_Type (Entity (Subt))
10951 and then
10952 Chars (Etype (Base_Type (Entity (Subt)))) =
10953 Type_Id));
10954 end if;
10955
10956 -- A reference to the current type may appear as the prefix of
10957 -- a 'Class attribute.
10958
10959 elsif Nkind (Subt) = N_Attribute_Reference
10960 and then Attribute_Name (Subt) = Name_Class
10961 then
10962 return Names_T (Prefix (Subt));
10963
10964 else
10965 return False;
10966 end if;
10967 end Designates_T;
10968
10969 ----------------
10970 -- Mentions_T --
10971 ----------------
10972
10973 function Mentions_T (Acc_Def : Node_Id) return Boolean is
10974 Param_Spec : Node_Id;
10975
10976 Acc_Subprg : constant Node_Id :=
10977 Access_To_Subprogram_Definition (Acc_Def);
10978
10979 begin
10980 if No (Acc_Subprg) then
10981 return Designates_T (Subtype_Mark (Acc_Def));
10982 end if;
10983
10984 -- Component is an access_to_subprogram: examine its formals,
10985 -- and result definition in the case of an access_to_function.
10986
10987 Param_Spec := First (Parameter_Specifications (Acc_Subprg));
10988 while Present (Param_Spec) loop
10989 if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
10990 and then Mentions_T (Parameter_Type (Param_Spec))
10991 then
10992 return True;
10993
10994 elsif Designates_T (Parameter_Type (Param_Spec)) then
10995 return True;
10996 end if;
10997
10998 Next (Param_Spec);
10999 end loop;
11000
11001 if Nkind (Acc_Subprg) = N_Access_Function_Definition then
11002 if Nkind (Result_Definition (Acc_Subprg)) =
11003 N_Access_Definition
11004 then
11005 return Mentions_T (Result_Definition (Acc_Subprg));
11006 else
11007 return Designates_T (Result_Definition (Acc_Subprg));
11008 end if;
11009 end if;
11010
11011 return False;
11012 end Mentions_T;
11013
11014 -- Start of processing for Check_Anonymous_Access_Components
11015
11016 begin
11017 if No (Comp_List) then
11018 return;
11019 end if;
11020
11021 Comp := First (Component_Items (Comp_List));
11022 while Present (Comp) loop
11023 if Nkind (Comp) = N_Component_Declaration
11024 and then Present
11025 (Access_Definition (Component_Definition (Comp)))
11026 and then
11027 Mentions_T (Access_Definition (Component_Definition (Comp)))
11028 then
11029 Comp_Def := Component_Definition (Comp);
11030 Acc_Def :=
11031 Access_To_Subprogram_Definition (Access_Definition (Comp_Def));
11032
11033 Build_Incomplete_Type_Declaration;
11034 Anon_Access := Make_Temporary (Loc, 'S');
11035
11036 -- Create a declaration for the anonymous access type: either
11037 -- an access_to_object or an access_to_subprogram.
11038
11039 if Present (Acc_Def) then
11040 if Nkind (Acc_Def) = N_Access_Function_Definition then
11041 Type_Def :=
11042 Make_Access_Function_Definition (Loc,
11043 Parameter_Specifications =>
11044 Parameter_Specifications (Acc_Def),
11045 Result_Definition => Result_Definition (Acc_Def));
11046 else
11047 Type_Def :=
11048 Make_Access_Procedure_Definition (Loc,
11049 Parameter_Specifications =>
11050 Parameter_Specifications (Acc_Def));
11051 end if;
11052
11053 else
11054 Type_Def :=
11055 Make_Access_To_Object_Definition (Loc,
11056 Subtype_Indication =>
11057 Relocate_Node
11058 (Subtype_Mark (Access_Definition (Comp_Def))));
11059
11060 Set_Constant_Present
11061 (Type_Def, Constant_Present (Access_Definition (Comp_Def)));
11062 Set_All_Present
11063 (Type_Def, All_Present (Access_Definition (Comp_Def)));
11064 end if;
11065
11066 Set_Null_Exclusion_Present
11067 (Type_Def,
11068 Null_Exclusion_Present (Access_Definition (Comp_Def)));
11069
11070 Decl :=
11071 Make_Full_Type_Declaration (Loc,
11072 Defining_Identifier => Anon_Access,
11073 Type_Definition => Type_Def);
11074
11075 Insert_Before (Typ_Decl, Decl);
11076 Analyze (Decl);
11077
11078 -- If an access to subprogram, create the extra formals
11079
11080 if Present (Acc_Def) then
11081 Create_Extra_Formals (Designated_Type (Anon_Access));
11082
11083 -- If an access to object, preserve entity of designated type,
11084 -- for ASIS use, before rewriting the component definition.
11085
11086 else
11087 declare
11088 Desig : Entity_Id;
11089
11090 begin
11091 Desig := Entity (Subtype_Indication (Type_Def));
11092
11093 -- If the access definition is to the current record,
11094 -- the visible entity at this point is an incomplete
11095 -- type. Retrieve the full view to simplify ASIS queries
11096
11097 if Ekind (Desig) = E_Incomplete_Type then
11098 Desig := Full_View (Desig);
11099 end if;
11100
11101 Set_Entity
11102 (Subtype_Mark (Access_Definition (Comp_Def)), Desig);
11103 end;
11104 end if;
11105
11106 Rewrite (Comp_Def,
11107 Make_Component_Definition (Loc,
11108 Subtype_Indication =>
11109 New_Occurrence_Of (Anon_Access, Loc)));
11110
11111 if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
11112 Set_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
11113 else
11114 Set_Ekind (Anon_Access, E_Anonymous_Access_Type);
11115 end if;
11116
11117 Set_Is_Local_Anonymous_Access (Anon_Access);
11118 end if;
11119
11120 Next (Comp);
11121 end loop;
11122
11123 if Present (Variant_Part (Comp_List)) then
11124 declare
11125 V : Node_Id;
11126 begin
11127 V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
11128 while Present (V) loop
11129 Check_Anonymous_Access_Components
11130 (Typ_Decl, Typ, Prev, Component_List (V));
11131 Next_Non_Pragma (V);
11132 end loop;
11133 end;
11134 end if;
11135 end Check_Anonymous_Access_Components;
11136
11137 ----------------------
11138 -- Check_Completion --
11139 ----------------------
11140
11141 procedure Check_Completion (Body_Id : Node_Id := Empty) is
11142 E : Entity_Id;
11143
11144 procedure Post_Error;
11145 -- Post error message for lack of completion for entity E
11146
11147 ----------------
11148 -- Post_Error --
11149 ----------------
11150
11151 procedure Post_Error is
11152 procedure Missing_Body;
11153 -- Output missing body message
11154
11155 ------------------
11156 -- Missing_Body --
11157 ------------------
11158
11159 procedure Missing_Body is
11160 begin
11161 -- Spec is in same unit, so we can post on spec
11162
11163 if In_Same_Source_Unit (Body_Id, E) then
11164 Error_Msg_N ("missing body for &", E);
11165
11166 -- Spec is in a separate unit, so we have to post on the body
11167
11168 else
11169 Error_Msg_NE ("missing body for & declared#!", Body_Id, E);
11170 end if;
11171 end Missing_Body;
11172
11173 -- Start of processing for Post_Error
11174
11175 begin
11176 if not Comes_From_Source (E) then
11177 if Ekind_In (E, E_Task_Type, E_Protected_Type) then
11178
11179 -- It may be an anonymous protected type created for a
11180 -- single variable. Post error on variable, if present.
11181
11182 declare
11183 Var : Entity_Id;
11184
11185 begin
11186 Var := First_Entity (Current_Scope);
11187 while Present (Var) loop
11188 exit when Etype (Var) = E
11189 and then Comes_From_Source (Var);
11190
11191 Next_Entity (Var);
11192 end loop;
11193
11194 if Present (Var) then
11195 E := Var;
11196 end if;
11197 end;
11198 end if;
11199 end if;
11200
11201 -- If a generated entity has no completion, then either previous
11202 -- semantic errors have disabled the expansion phase, or else we had
11203 -- missing subunits, or else we are compiling without expansion,
11204 -- or else something is very wrong.
11205
11206 if not Comes_From_Source (E) then
11207 pragma Assert
11208 (Serious_Errors_Detected > 0
11209 or else Configurable_Run_Time_Violations > 0
11210 or else Subunits_Missing
11211 or else not Expander_Active);
11212 return;
11213
11214 -- Here for source entity
11215
11216 else
11217 -- Here if no body to post the error message, so we post the error
11218 -- on the declaration that has no completion. This is not really
11219 -- the right place to post it, think about this later ???
11220
11221 if No (Body_Id) then
11222 if Is_Type (E) then
11223 Error_Msg_NE
11224 ("missing full declaration for }", Parent (E), E);
11225 else
11226 Error_Msg_NE ("missing body for &", Parent (E), E);
11227 end if;
11228
11229 -- Package body has no completion for a declaration that appears
11230 -- in the corresponding spec. Post error on the body, with a
11231 -- reference to the non-completed declaration.
11232
11233 else
11234 Error_Msg_Sloc := Sloc (E);
11235
11236 if Is_Type (E) then
11237 Error_Msg_NE ("missing full declaration for }!", Body_Id, E);
11238
11239 elsif Is_Overloadable (E)
11240 and then Current_Entity_In_Scope (E) /= E
11241 then
11242 -- It may be that the completion is mistyped and appears as
11243 -- a distinct overloading of the entity.
11244
11245 declare
11246 Candidate : constant Entity_Id :=
11247 Current_Entity_In_Scope (E);
11248 Decl : constant Node_Id :=
11249 Unit_Declaration_Node (Candidate);
11250
11251 begin
11252 if Is_Overloadable (Candidate)
11253 and then Ekind (Candidate) = Ekind (E)
11254 and then Nkind (Decl) = N_Subprogram_Body
11255 and then Acts_As_Spec (Decl)
11256 then
11257 Check_Type_Conformant (Candidate, E);
11258
11259 else
11260 Missing_Body;
11261 end if;
11262 end;
11263
11264 else
11265 Missing_Body;
11266 end if;
11267 end if;
11268 end if;
11269 end Post_Error;
11270
11271 -- Local variables
11272
11273 Pack_Id : constant Entity_Id := Current_Scope;
11274
11275 -- Start of processing for Check_Completion
11276
11277 begin
11278 E := First_Entity (Pack_Id);
11279 while Present (E) loop
11280 if Is_Intrinsic_Subprogram (E) then
11281 null;
11282
11283 -- The following situation requires special handling: a child unit
11284 -- that appears in the context clause of the body of its parent:
11285
11286 -- procedure Parent.Child (...);
11287
11288 -- with Parent.Child;
11289 -- package body Parent is
11290
11291 -- Here Parent.Child appears as a local entity, but should not be
11292 -- flagged as requiring completion, because it is a compilation
11293 -- unit.
11294
11295 -- Ignore missing completion for a subprogram that does not come from
11296 -- source (including the _Call primitive operation of RAS types,
11297 -- which has to have the flag Comes_From_Source for other purposes):
11298 -- we assume that the expander will provide the missing completion.
11299 -- In case of previous errors, other expansion actions that provide
11300 -- bodies for null procedures with not be invoked, so inhibit message
11301 -- in those cases.
11302
11303 -- Note that E_Operator is not in the list that follows, because
11304 -- this kind is reserved for predefined operators, that are
11305 -- intrinsic and do not need completion.
11306
11307 elsif Ekind_In (E, E_Function,
11308 E_Procedure,
11309 E_Generic_Function,
11310 E_Generic_Procedure)
11311 then
11312 if Has_Completion (E) then
11313 null;
11314
11315 elsif Is_Subprogram (E) and then Is_Abstract_Subprogram (E) then
11316 null;
11317
11318 elsif Is_Subprogram (E)
11319 and then (not Comes_From_Source (E)
11320 or else Chars (E) = Name_uCall)
11321 then
11322 null;
11323
11324 elsif
11325 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
11326 then
11327 null;
11328
11329 elsif Nkind (Parent (E)) = N_Procedure_Specification
11330 and then Null_Present (Parent (E))
11331 and then Serious_Errors_Detected > 0
11332 then
11333 null;
11334
11335 else
11336 Post_Error;
11337 end if;
11338
11339 elsif Is_Entry (E) then
11340 if not Has_Completion (E) and then
11341 (Ekind (Scope (E)) = E_Protected_Object
11342 or else Ekind (Scope (E)) = E_Protected_Type)
11343 then
11344 Post_Error;
11345 end if;
11346
11347 elsif Is_Package_Or_Generic_Package (E) then
11348 if Unit_Requires_Body (E) then
11349 if not Has_Completion (E)
11350 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
11351 N_Compilation_Unit
11352 then
11353 Post_Error;
11354 end if;
11355
11356 elsif not Is_Child_Unit (E) then
11357 May_Need_Implicit_Body (E);
11358 end if;
11359
11360 -- A formal incomplete type (Ada 2012) does not require a completion;
11361 -- other incomplete type declarations do.
11362
11363 elsif Ekind (E) = E_Incomplete_Type
11364 and then No (Underlying_Type (E))
11365 and then not Is_Generic_Type (E)
11366 then
11367 Post_Error;
11368
11369 elsif Ekind_In (E, E_Task_Type, E_Protected_Type)
11370 and then not Has_Completion (E)
11371 then
11372 Post_Error;
11373
11374 -- A single task declared in the current scope is a constant, verify
11375 -- that the body of its anonymous type is in the same scope. If the
11376 -- task is defined elsewhere, this may be a renaming declaration for
11377 -- which no completion is needed.
11378
11379 elsif Ekind (E) = E_Constant
11380 and then Ekind (Etype (E)) = E_Task_Type
11381 and then not Has_Completion (Etype (E))
11382 and then Scope (Etype (E)) = Current_Scope
11383 then
11384 Post_Error;
11385
11386 elsif Ekind (E) = E_Protected_Object
11387 and then not Has_Completion (Etype (E))
11388 then
11389 Post_Error;
11390
11391 elsif Ekind (E) = E_Record_Type then
11392 if Is_Tagged_Type (E) then
11393 Check_Abstract_Overriding (E);
11394 Check_Conventions (E);
11395 end if;
11396
11397 Check_Aliased_Component_Types (E);
11398
11399 elsif Ekind (E) = E_Array_Type then
11400 Check_Aliased_Component_Types (E);
11401
11402 end if;
11403
11404 Next_Entity (E);
11405 end loop;
11406 end Check_Completion;
11407
11408 ------------------------------------
11409 -- Check_CPP_Type_Has_No_Defaults --
11410 ------------------------------------
11411
11412 procedure Check_CPP_Type_Has_No_Defaults (T : Entity_Id) is
11413 Tdef : constant Node_Id := Type_Definition (Declaration_Node (T));
11414 Clist : Node_Id;
11415 Comp : Node_Id;
11416
11417 begin
11418 -- Obtain the component list
11419
11420 if Nkind (Tdef) = N_Record_Definition then
11421 Clist := Component_List (Tdef);
11422 else pragma Assert (Nkind (Tdef) = N_Derived_Type_Definition);
11423 Clist := Component_List (Record_Extension_Part (Tdef));
11424 end if;
11425
11426 -- Check all components to ensure no default expressions
11427
11428 if Present (Clist) then
11429 Comp := First (Component_Items (Clist));
11430 while Present (Comp) loop
11431 if Present (Expression (Comp)) then
11432 Error_Msg_N
11433 ("component of imported 'C'P'P type cannot have "
11434 & "default expression", Expression (Comp));
11435 end if;
11436
11437 Next (Comp);
11438 end loop;
11439 end if;
11440 end Check_CPP_Type_Has_No_Defaults;
11441
11442 ----------------------------
11443 -- Check_Delta_Expression --
11444 ----------------------------
11445
11446 procedure Check_Delta_Expression (E : Node_Id) is
11447 begin
11448 if not (Is_Real_Type (Etype (E))) then
11449 Wrong_Type (E, Any_Real);
11450
11451 elsif not Is_OK_Static_Expression (E) then
11452 Flag_Non_Static_Expr
11453 ("non-static expression used for delta value!", E);
11454
11455 elsif not UR_Is_Positive (Expr_Value_R (E)) then
11456 Error_Msg_N ("delta expression must be positive", E);
11457
11458 else
11459 return;
11460 end if;
11461
11462 -- If any of above errors occurred, then replace the incorrect
11463 -- expression by the real 0.1, which should prevent further errors.
11464
11465 Rewrite (E,
11466 Make_Real_Literal (Sloc (E), Ureal_Tenth));
11467 Analyze_And_Resolve (E, Standard_Float);
11468 end Check_Delta_Expression;
11469
11470 -----------------------------
11471 -- Check_Digits_Expression --
11472 -----------------------------
11473
11474 procedure Check_Digits_Expression (E : Node_Id) is
11475 begin
11476 if not (Is_Integer_Type (Etype (E))) then
11477 Wrong_Type (E, Any_Integer);
11478
11479 elsif not Is_OK_Static_Expression (E) then
11480 Flag_Non_Static_Expr
11481 ("non-static expression used for digits value!", E);
11482
11483 elsif Expr_Value (E) <= 0 then
11484 Error_Msg_N ("digits value must be greater than zero", E);
11485
11486 else
11487 return;
11488 end if;
11489
11490 -- If any of above errors occurred, then replace the incorrect
11491 -- expression by the integer 1, which should prevent further errors.
11492
11493 Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
11494 Analyze_And_Resolve (E, Standard_Integer);
11495
11496 end Check_Digits_Expression;
11497
11498 --------------------------
11499 -- Check_Initialization --
11500 --------------------------
11501
11502 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
11503 begin
11504 -- Special processing for limited types
11505
11506 if Is_Limited_Type (T)
11507 and then not In_Instance
11508 and then not In_Inlined_Body
11509 then
11510 if not OK_For_Limited_Init (T, Exp) then
11511
11512 -- In GNAT mode, this is just a warning, to allow it to be evilly
11513 -- turned off. Otherwise it is a real error.
11514
11515 if GNAT_Mode then
11516 Error_Msg_N
11517 ("??cannot initialize entities of limited type!", Exp);
11518
11519 elsif Ada_Version < Ada_2005 then
11520
11521 -- The side effect removal machinery may generate illegal Ada
11522 -- code to avoid the usage of access types and 'reference in
11523 -- SPARK mode. Since this is legal code with respect to theorem
11524 -- proving, do not emit the error.
11525
11526 if GNATprove_Mode
11527 and then Nkind (Exp) = N_Function_Call
11528 and then Nkind (Parent (Exp)) = N_Object_Declaration
11529 and then not Comes_From_Source
11530 (Defining_Identifier (Parent (Exp)))
11531 then
11532 null;
11533
11534 else
11535 Error_Msg_N
11536 ("cannot initialize entities of limited type", Exp);
11537 Explain_Limited_Type (T, Exp);
11538 end if;
11539
11540 else
11541 -- Specialize error message according to kind of illegal
11542 -- initial expression.
11543
11544 if Nkind (Exp) = N_Type_Conversion
11545 and then Nkind (Expression (Exp)) = N_Function_Call
11546 then
11547 Error_Msg_N
11548 ("illegal context for call"
11549 & " to function with limited result", Exp);
11550
11551 else
11552 Error_Msg_N
11553 ("initialization of limited object requires aggregate "
11554 & "or function call", Exp);
11555 end if;
11556 end if;
11557 end if;
11558 end if;
11559
11560 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag gets
11561 -- set unless we can be sure that no range check is required.
11562
11563 if (GNATprove_Mode or not Expander_Active)
11564 and then Is_Scalar_Type (T)
11565 and then not Is_In_Range (Exp, T, Assume_Valid => True)
11566 then
11567 Set_Do_Range_Check (Exp);
11568 end if;
11569 end Check_Initialization;
11570
11571 ----------------------
11572 -- Check_Interfaces --
11573 ----------------------
11574
11575 procedure Check_Interfaces (N : Node_Id; Def : Node_Id) is
11576 Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
11577
11578 Iface : Node_Id;
11579 Iface_Def : Node_Id;
11580 Iface_Typ : Entity_Id;
11581 Parent_Node : Node_Id;
11582
11583 Is_Task : Boolean := False;
11584 -- Set True if parent type or any progenitor is a task interface
11585
11586 Is_Protected : Boolean := False;
11587 -- Set True if parent type or any progenitor is a protected interface
11588
11589 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
11590 -- Check that a progenitor is compatible with declaration. If an error
11591 -- message is output, it is posted on Error_Node.
11592
11593 ------------------
11594 -- Check_Ifaces --
11595 ------------------
11596
11597 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
11598 Iface_Id : constant Entity_Id :=
11599 Defining_Identifier (Parent (Iface_Def));
11600 Type_Def : Node_Id;
11601
11602 begin
11603 if Nkind (N) = N_Private_Extension_Declaration then
11604 Type_Def := N;
11605 else
11606 Type_Def := Type_Definition (N);
11607 end if;
11608
11609 if Is_Task_Interface (Iface_Id) then
11610 Is_Task := True;
11611
11612 elsif Is_Protected_Interface (Iface_Id) then
11613 Is_Protected := True;
11614 end if;
11615
11616 if Is_Synchronized_Interface (Iface_Id) then
11617
11618 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
11619 -- extension derived from a synchronized interface must explicitly
11620 -- be declared synchronized, because the full view will be a
11621 -- synchronized type.
11622
11623 if Nkind (N) = N_Private_Extension_Declaration then
11624 if not Synchronized_Present (N) then
11625 Error_Msg_NE
11626 ("private extension of& must be explicitly synchronized",
11627 N, Iface_Id);
11628 end if;
11629
11630 -- However, by 3.9.4(16/2), a full type that is a record extension
11631 -- is never allowed to derive from a synchronized interface (note
11632 -- that interfaces must be excluded from this check, because those
11633 -- are represented by derived type definitions in some cases).
11634
11635 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition
11636 and then not Interface_Present (Type_Definition (N))
11637 then
11638 Error_Msg_N ("record extension cannot derive from synchronized "
11639 & "interface", Error_Node);
11640 end if;
11641 end if;
11642
11643 -- Check that the characteristics of the progenitor are compatible
11644 -- with the explicit qualifier in the declaration.
11645 -- The check only applies to qualifiers that come from source.
11646 -- Limited_Present also appears in the declaration of corresponding
11647 -- records, and the check does not apply to them.
11648
11649 if Limited_Present (Type_Def)
11650 and then not
11651 Is_Concurrent_Record_Type (Defining_Identifier (N))
11652 then
11653 if Is_Limited_Interface (Parent_Type)
11654 and then not Is_Limited_Interface (Iface_Id)
11655 then
11656 Error_Msg_NE
11657 ("progenitor & must be limited interface",
11658 Error_Node, Iface_Id);
11659
11660 elsif
11661 (Task_Present (Iface_Def)
11662 or else Protected_Present (Iface_Def)
11663 or else Synchronized_Present (Iface_Def))
11664 and then Nkind (N) /= N_Private_Extension_Declaration
11665 and then not Error_Posted (N)
11666 then
11667 Error_Msg_NE
11668 ("progenitor & must be limited interface",
11669 Error_Node, Iface_Id);
11670 end if;
11671
11672 -- Protected interfaces can only inherit from limited, synchronized
11673 -- or protected interfaces.
11674
11675 elsif Nkind (N) = N_Full_Type_Declaration
11676 and then Protected_Present (Type_Def)
11677 then
11678 if Limited_Present (Iface_Def)
11679 or else Synchronized_Present (Iface_Def)
11680 or else Protected_Present (Iface_Def)
11681 then
11682 null;
11683
11684 elsif Task_Present (Iface_Def) then
11685 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
11686 & "from task interface", Error_Node);
11687
11688 else
11689 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
11690 & "from non-limited interface", Error_Node);
11691 end if;
11692
11693 -- Ada 2005 (AI-345): Synchronized interfaces can only inherit from
11694 -- limited and synchronized.
11695
11696 elsif Synchronized_Present (Type_Def) then
11697 if Limited_Present (Iface_Def)
11698 or else Synchronized_Present (Iface_Def)
11699 then
11700 null;
11701
11702 elsif Protected_Present (Iface_Def)
11703 and then Nkind (N) /= N_Private_Extension_Declaration
11704 then
11705 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11706 & "from protected interface", Error_Node);
11707
11708 elsif Task_Present (Iface_Def)
11709 and then Nkind (N) /= N_Private_Extension_Declaration
11710 then
11711 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11712 & "from task interface", Error_Node);
11713
11714 elsif not Is_Limited_Interface (Iface_Id) then
11715 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11716 & "from non-limited interface", Error_Node);
11717 end if;
11718
11719 -- Ada 2005 (AI-345): Task interfaces can only inherit from limited,
11720 -- synchronized or task interfaces.
11721
11722 elsif Nkind (N) = N_Full_Type_Declaration
11723 and then Task_Present (Type_Def)
11724 then
11725 if Limited_Present (Iface_Def)
11726 or else Synchronized_Present (Iface_Def)
11727 or else Task_Present (Iface_Def)
11728 then
11729 null;
11730
11731 elsif Protected_Present (Iface_Def) then
11732 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
11733 & "protected interface", Error_Node);
11734
11735 else
11736 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
11737 & "non-limited interface", Error_Node);
11738 end if;
11739 end if;
11740 end Check_Ifaces;
11741
11742 -- Start of processing for Check_Interfaces
11743
11744 begin
11745 if Is_Interface (Parent_Type) then
11746 if Is_Task_Interface (Parent_Type) then
11747 Is_Task := True;
11748
11749 elsif Is_Protected_Interface (Parent_Type) then
11750 Is_Protected := True;
11751 end if;
11752 end if;
11753
11754 if Nkind (N) = N_Private_Extension_Declaration then
11755
11756 -- Check that progenitors are compatible with declaration
11757
11758 Iface := First (Interface_List (Def));
11759 while Present (Iface) loop
11760 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
11761
11762 Parent_Node := Parent (Base_Type (Iface_Typ));
11763 Iface_Def := Type_Definition (Parent_Node);
11764
11765 if not Is_Interface (Iface_Typ) then
11766 Diagnose_Interface (Iface, Iface_Typ);
11767 else
11768 Check_Ifaces (Iface_Def, Iface);
11769 end if;
11770
11771 Next (Iface);
11772 end loop;
11773
11774 if Is_Task and Is_Protected then
11775 Error_Msg_N
11776 ("type cannot derive from task and protected interface", N);
11777 end if;
11778
11779 return;
11780 end if;
11781
11782 -- Full type declaration of derived type.
11783 -- Check compatibility with parent if it is interface type
11784
11785 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
11786 and then Is_Interface (Parent_Type)
11787 then
11788 Parent_Node := Parent (Parent_Type);
11789
11790 -- More detailed checks for interface varieties
11791
11792 Check_Ifaces
11793 (Iface_Def => Type_Definition (Parent_Node),
11794 Error_Node => Subtype_Indication (Type_Definition (N)));
11795 end if;
11796
11797 Iface := First (Interface_List (Def));
11798 while Present (Iface) loop
11799 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
11800
11801 Parent_Node := Parent (Base_Type (Iface_Typ));
11802 Iface_Def := Type_Definition (Parent_Node);
11803
11804 if not Is_Interface (Iface_Typ) then
11805 Diagnose_Interface (Iface, Iface_Typ);
11806
11807 else
11808 -- "The declaration of a specific descendant of an interface
11809 -- type freezes the interface type" RM 13.14
11810
11811 Freeze_Before (N, Iface_Typ);
11812 Check_Ifaces (Iface_Def, Error_Node => Iface);
11813 end if;
11814
11815 Next (Iface);
11816 end loop;
11817
11818 if Is_Task and Is_Protected then
11819 Error_Msg_N
11820 ("type cannot derive from task and protected interface", N);
11821 end if;
11822 end Check_Interfaces;
11823
11824 ------------------------------------
11825 -- Check_Or_Process_Discriminants --
11826 ------------------------------------
11827
11828 -- If an incomplete or private type declaration was already given for the
11829 -- type, the discriminants may have already been processed if they were
11830 -- present on the incomplete declaration. In this case a full conformance
11831 -- check has been performed in Find_Type_Name, and we then recheck here
11832 -- some properties that can't be checked on the partial view alone.
11833 -- Otherwise we call Process_Discriminants.
11834
11835 procedure Check_Or_Process_Discriminants
11836 (N : Node_Id;
11837 T : Entity_Id;
11838 Prev : Entity_Id := Empty)
11839 is
11840 begin
11841 if Has_Discriminants (T) then
11842
11843 -- Discriminants are already set on T if they were already present
11844 -- on the partial view. Make them visible to component declarations.
11845
11846 declare
11847 D : Entity_Id;
11848 -- Discriminant on T (full view) referencing expr on partial view
11849
11850 Prev_D : Entity_Id;
11851 -- Entity of corresponding discriminant on partial view
11852
11853 New_D : Node_Id;
11854 -- Discriminant specification for full view, expression is
11855 -- the syntactic copy on full view (which has been checked for
11856 -- conformance with partial view), only used here to post error
11857 -- message.
11858
11859 begin
11860 D := First_Discriminant (T);
11861 New_D := First (Discriminant_Specifications (N));
11862 while Present (D) loop
11863 Prev_D := Current_Entity (D);
11864 Set_Current_Entity (D);
11865 Set_Is_Immediately_Visible (D);
11866 Set_Homonym (D, Prev_D);
11867
11868 -- Handle the case where there is an untagged partial view and
11869 -- the full view is tagged: must disallow discriminants with
11870 -- defaults, unless compiling for Ada 2012, which allows a
11871 -- limited tagged type to have defaulted discriminants (see
11872 -- AI05-0214). However, suppress error here if it was already
11873 -- reported on the default expression of the partial view.
11874
11875 if Is_Tagged_Type (T)
11876 and then Present (Expression (Parent (D)))
11877 and then (not Is_Limited_Type (Current_Scope)
11878 or else Ada_Version < Ada_2012)
11879 and then not Error_Posted (Expression (Parent (D)))
11880 then
11881 if Ada_Version >= Ada_2012 then
11882 Error_Msg_N
11883 ("discriminants of nonlimited tagged type cannot have "
11884 & "defaults",
11885 Expression (New_D));
11886 else
11887 Error_Msg_N
11888 ("discriminants of tagged type cannot have defaults",
11889 Expression (New_D));
11890 end if;
11891 end if;
11892
11893 -- Ada 2005 (AI-230): Access discriminant allowed in
11894 -- non-limited record types.
11895
11896 if Ada_Version < Ada_2005 then
11897
11898 -- This restriction gets applied to the full type here. It
11899 -- has already been applied earlier to the partial view.
11900
11901 Check_Access_Discriminant_Requires_Limited (Parent (D), N);
11902 end if;
11903
11904 Next_Discriminant (D);
11905 Next (New_D);
11906 end loop;
11907 end;
11908
11909 elsif Present (Discriminant_Specifications (N)) then
11910 Process_Discriminants (N, Prev);
11911 end if;
11912 end Check_Or_Process_Discriminants;
11913
11914 ----------------------
11915 -- Check_Real_Bound --
11916 ----------------------
11917
11918 procedure Check_Real_Bound (Bound : Node_Id) is
11919 begin
11920 if not Is_Real_Type (Etype (Bound)) then
11921 Error_Msg_N
11922 ("bound in real type definition must be of real type", Bound);
11923
11924 elsif not Is_OK_Static_Expression (Bound) then
11925 Flag_Non_Static_Expr
11926 ("non-static expression used for real type bound!", Bound);
11927
11928 else
11929 return;
11930 end if;
11931
11932 Rewrite
11933 (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
11934 Analyze (Bound);
11935 Resolve (Bound, Standard_Float);
11936 end Check_Real_Bound;
11937
11938 ------------------------------
11939 -- Complete_Private_Subtype --
11940 ------------------------------
11941
11942 procedure Complete_Private_Subtype
11943 (Priv : Entity_Id;
11944 Full : Entity_Id;
11945 Full_Base : Entity_Id;
11946 Related_Nod : Node_Id)
11947 is
11948 Save_Next_Entity : Entity_Id;
11949 Save_Homonym : Entity_Id;
11950
11951 begin
11952 -- Set semantic attributes for (implicit) private subtype completion.
11953 -- If the full type has no discriminants, then it is a copy of the
11954 -- full view of the base. Otherwise, it is a subtype of the base with
11955 -- a possible discriminant constraint. Save and restore the original
11956 -- Next_Entity field of full to ensure that the calls to Copy_Node do
11957 -- not corrupt the entity chain.
11958
11959 -- Note that the type of the full view is the same entity as the type
11960 -- of the partial view. In this fashion, the subtype has access to the
11961 -- correct view of the parent.
11962
11963 Save_Next_Entity := Next_Entity (Full);
11964 Save_Homonym := Homonym (Priv);
11965
11966 case Ekind (Full_Base) is
11967 when Class_Wide_Kind
11968 | Private_Kind
11969 | Protected_Kind
11970 | Task_Kind
11971 | E_Record_Subtype
11972 | E_Record_Type
11973 =>
11974 Copy_Node (Priv, Full);
11975
11976 Set_Has_Discriminants
11977 (Full, Has_Discriminants (Full_Base));
11978 Set_Has_Unknown_Discriminants
11979 (Full, Has_Unknown_Discriminants (Full_Base));
11980 Set_First_Entity (Full, First_Entity (Full_Base));
11981 Set_Last_Entity (Full, Last_Entity (Full_Base));
11982
11983 -- If the underlying base type is constrained, we know that the
11984 -- full view of the subtype is constrained as well (the converse
11985 -- is not necessarily true).
11986
11987 if Is_Constrained (Full_Base) then
11988 Set_Is_Constrained (Full);
11989 end if;
11990
11991 when others =>
11992 Copy_Node (Full_Base, Full);
11993
11994 Set_Chars (Full, Chars (Priv));
11995 Conditional_Delay (Full, Priv);
11996 Set_Sloc (Full, Sloc (Priv));
11997 end case;
11998
11999 Set_Next_Entity (Full, Save_Next_Entity);
12000 Set_Homonym (Full, Save_Homonym);
12001 Set_Associated_Node_For_Itype (Full, Related_Nod);
12002
12003 -- Set common attributes for all subtypes: kind, convention, etc.
12004
12005 Set_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
12006 Set_Convention (Full, Convention (Full_Base));
12007
12008 -- The Etype of the full view is inconsistent. Gigi needs to see the
12009 -- structural full view, which is what the current scheme gives: the
12010 -- Etype of the full view is the etype of the full base. However, if the
12011 -- full base is a derived type, the full view then looks like a subtype
12012 -- of the parent, not a subtype of the full base. If instead we write:
12013
12014 -- Set_Etype (Full, Full_Base);
12015
12016 -- then we get inconsistencies in the front-end (confusion between
12017 -- views). Several outstanding bugs are related to this ???
12018
12019 Set_Is_First_Subtype (Full, False);
12020 Set_Scope (Full, Scope (Priv));
12021 Set_Size_Info (Full, Full_Base);
12022 Set_RM_Size (Full, RM_Size (Full_Base));
12023 Set_Is_Itype (Full);
12024
12025 -- A subtype of a private-type-without-discriminants, whose full-view
12026 -- has discriminants with default expressions, is not constrained.
12027
12028 if not Has_Discriminants (Priv) then
12029 Set_Is_Constrained (Full, Is_Constrained (Full_Base));
12030
12031 if Has_Discriminants (Full_Base) then
12032 Set_Discriminant_Constraint
12033 (Full, Discriminant_Constraint (Full_Base));
12034
12035 -- The partial view may have been indefinite, the full view
12036 -- might not be.
12037
12038 Set_Has_Unknown_Discriminants
12039 (Full, Has_Unknown_Discriminants (Full_Base));
12040 end if;
12041 end if;
12042
12043 Set_First_Rep_Item (Full, First_Rep_Item (Full_Base));
12044 Set_Depends_On_Private (Full, Has_Private_Component (Full));
12045
12046 -- Freeze the private subtype entity if its parent is delayed, and not
12047 -- already frozen. We skip this processing if the type is an anonymous
12048 -- subtype of a record component, or is the corresponding record of a
12049 -- protected type, since these are processed when the enclosing type
12050 -- is frozen. If the parent type is declared in a nested package then
12051 -- the freezing of the private and full views also happens later.
12052
12053 if not Is_Type (Scope (Full)) then
12054 if Is_Itype (Priv)
12055 and then In_Same_Source_Unit (Full, Full_Base)
12056 and then Scope (Full_Base) /= Scope (Full)
12057 then
12058 Set_Has_Delayed_Freeze (Full);
12059 Set_Has_Delayed_Freeze (Priv);
12060
12061 else
12062 Set_Has_Delayed_Freeze (Full,
12063 Has_Delayed_Freeze (Full_Base)
12064 and then not Is_Frozen (Full_Base));
12065 end if;
12066 end if;
12067
12068 Set_Freeze_Node (Full, Empty);
12069 Set_Is_Frozen (Full, False);
12070 Set_Full_View (Priv, Full);
12071
12072 if Has_Discriminants (Full) then
12073 Set_Stored_Constraint_From_Discriminant_Constraint (Full);
12074 Set_Stored_Constraint (Priv, Stored_Constraint (Full));
12075
12076 if Has_Unknown_Discriminants (Full) then
12077 Set_Discriminant_Constraint (Full, No_Elist);
12078 end if;
12079 end if;
12080
12081 if Ekind (Full_Base) = E_Record_Type
12082 and then Has_Discriminants (Full_Base)
12083 and then Has_Discriminants (Priv) -- might not, if errors
12084 and then not Has_Unknown_Discriminants (Priv)
12085 and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
12086 then
12087 Create_Constrained_Components
12088 (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
12089
12090 -- If the full base is itself derived from private, build a congruent
12091 -- subtype of its underlying type, for use by the back end. For a
12092 -- constrained record component, the declaration cannot be placed on
12093 -- the component list, but it must nevertheless be built an analyzed, to
12094 -- supply enough information for Gigi to compute the size of component.
12095
12096 elsif Ekind (Full_Base) in Private_Kind
12097 and then Is_Derived_Type (Full_Base)
12098 and then Has_Discriminants (Full_Base)
12099 and then (Ekind (Current_Scope) /= E_Record_Subtype)
12100 then
12101 if not Is_Itype (Priv)
12102 and then
12103 Nkind (Subtype_Indication (Parent (Priv))) = N_Subtype_Indication
12104 then
12105 Build_Underlying_Full_View
12106 (Parent (Priv), Full, Etype (Full_Base));
12107
12108 elsif Nkind (Related_Nod) = N_Component_Declaration then
12109 Build_Underlying_Full_View (Related_Nod, Full, Etype (Full_Base));
12110 end if;
12111
12112 elsif Is_Record_Type (Full_Base) then
12113
12114 -- Show Full is simply a renaming of Full_Base
12115
12116 Set_Cloned_Subtype (Full, Full_Base);
12117 end if;
12118
12119 -- It is unsafe to share the bounds of a scalar type, because the Itype
12120 -- is elaborated on demand, and if a bound is non-static then different
12121 -- orders of elaboration in different units will lead to different
12122 -- external symbols.
12123
12124 if Is_Scalar_Type (Full_Base) then
12125 Set_Scalar_Range (Full,
12126 Make_Range (Sloc (Related_Nod),
12127 Low_Bound =>
12128 Duplicate_Subexpr_No_Checks (Type_Low_Bound (Full_Base)),
12129 High_Bound =>
12130 Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
12131
12132 -- This completion inherits the bounds of the full parent, but if
12133 -- the parent is an unconstrained floating point type, so is the
12134 -- completion.
12135
12136 if Is_Floating_Point_Type (Full_Base) then
12137 Set_Includes_Infinities
12138 (Scalar_Range (Full), Has_Infinities (Full_Base));
12139 end if;
12140 end if;
12141
12142 -- ??? It seems that a lot of fields are missing that should be copied
12143 -- from Full_Base to Full. Here are some that are introduced in a
12144 -- non-disruptive way but a cleanup is necessary.
12145
12146 if Is_Tagged_Type (Full_Base) then
12147 Set_Is_Tagged_Type (Full);
12148 Set_Direct_Primitive_Operations
12149 (Full, Direct_Primitive_Operations (Full_Base));
12150 Set_No_Tagged_Streams_Pragma
12151 (Full, No_Tagged_Streams_Pragma (Full_Base));
12152
12153 -- Inherit class_wide type of full_base in case the partial view was
12154 -- not tagged. Otherwise it has already been created when the private
12155 -- subtype was analyzed.
12156
12157 if No (Class_Wide_Type (Full)) then
12158 Set_Class_Wide_Type (Full, Class_Wide_Type (Full_Base));
12159 end if;
12160
12161 -- If this is a subtype of a protected or task type, constrain its
12162 -- corresponding record, unless this is a subtype without constraints,
12163 -- i.e. a simple renaming as with an actual subtype in an instance.
12164
12165 elsif Is_Concurrent_Type (Full_Base) then
12166 if Has_Discriminants (Full)
12167 and then Present (Corresponding_Record_Type (Full_Base))
12168 and then
12169 not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
12170 then
12171 Set_Corresponding_Record_Type (Full,
12172 Constrain_Corresponding_Record
12173 (Full, Corresponding_Record_Type (Full_Base), Related_Nod));
12174
12175 else
12176 Set_Corresponding_Record_Type (Full,
12177 Corresponding_Record_Type (Full_Base));
12178 end if;
12179 end if;
12180
12181 -- Link rep item chain, and also setting of Has_Predicates from private
12182 -- subtype to full subtype, since we will need these on the full subtype
12183 -- to create the predicate function. Note that the full subtype may
12184 -- already have rep items, inherited from the full view of the base
12185 -- type, so we must be sure not to overwrite these entries.
12186
12187 declare
12188 Append : Boolean;
12189 Item : Node_Id;
12190 Next_Item : Node_Id;
12191 Priv_Item : Node_Id;
12192
12193 begin
12194 Item := First_Rep_Item (Full);
12195 Priv_Item := First_Rep_Item (Priv);
12196
12197 -- If no existing rep items on full type, we can just link directly
12198 -- to the list of items on the private type, if any exist.. Same if
12199 -- the rep items are only those inherited from the base
12200
12201 if (No (Item)
12202 or else Nkind (Item) /= N_Aspect_Specification
12203 or else Entity (Item) = Full_Base)
12204 and then Present (First_Rep_Item (Priv))
12205 then
12206 Set_First_Rep_Item (Full, Priv_Item);
12207
12208 -- Otherwise, search to the end of items currently linked to the full
12209 -- subtype and append the private items to the end. However, if Priv
12210 -- and Full already have the same list of rep items, then the append
12211 -- is not done, as that would create a circularity.
12212 --
12213 -- The partial view may have a predicate and the rep item lists of
12214 -- both views agree when inherited from the same ancestor. In that
12215 -- case, simply propagate the list from one view to the other.
12216 -- A more complex analysis needed here ???
12217
12218 elsif Present (Priv_Item)
12219 and then Item = Next_Rep_Item (Priv_Item)
12220 then
12221 Set_First_Rep_Item (Full, Priv_Item);
12222
12223 elsif Item /= Priv_Item then
12224 Append := True;
12225 loop
12226 Next_Item := Next_Rep_Item (Item);
12227 exit when No (Next_Item);
12228 Item := Next_Item;
12229
12230 -- If the private view has aspect specifications, the full view
12231 -- inherits them. Since these aspects may already have been
12232 -- attached to the full view during derivation, do not append
12233 -- them if already present.
12234
12235 if Item = First_Rep_Item (Priv) then
12236 Append := False;
12237 exit;
12238 end if;
12239 end loop;
12240
12241 -- And link the private type items at the end of the chain
12242
12243 if Append then
12244 Set_Next_Rep_Item (Item, First_Rep_Item (Priv));
12245 end if;
12246 end if;
12247 end;
12248
12249 -- Make sure Has_Predicates is set on full type if it is set on the
12250 -- private type. Note that it may already be set on the full type and
12251 -- if so, we don't want to unset it. Similarly, propagate information
12252 -- about delayed aspects, because the corresponding pragmas must be
12253 -- analyzed when one of the views is frozen. This last step is needed
12254 -- in particular when the full type is a scalar type for which an
12255 -- anonymous base type is constructed.
12256
12257 -- The predicate functions are generated either at the freeze point
12258 -- of the type or at the end of the visible part, and we must avoid
12259 -- generating them twice.
12260
12261 if Has_Predicates (Priv) then
12262 Set_Has_Predicates (Full);
12263
12264 if Present (Predicate_Function (Priv))
12265 and then No (Predicate_Function (Full))
12266 then
12267 Set_Predicate_Function (Full, Predicate_Function (Priv));
12268 end if;
12269 end if;
12270
12271 if Has_Delayed_Aspects (Priv) then
12272 Set_Has_Delayed_Aspects (Full);
12273 end if;
12274 end Complete_Private_Subtype;
12275
12276 ----------------------------
12277 -- Constant_Redeclaration --
12278 ----------------------------
12279
12280 procedure Constant_Redeclaration
12281 (Id : Entity_Id;
12282 N : Node_Id;
12283 T : out Entity_Id)
12284 is
12285 Prev : constant Entity_Id := Current_Entity_In_Scope (Id);
12286 Obj_Def : constant Node_Id := Object_Definition (N);
12287 New_T : Entity_Id;
12288
12289 procedure Check_Possible_Deferred_Completion
12290 (Prev_Id : Entity_Id;
12291 Prev_Obj_Def : Node_Id;
12292 Curr_Obj_Def : Node_Id);
12293 -- Determine whether the two object definitions describe the partial
12294 -- and the full view of a constrained deferred constant. Generate
12295 -- a subtype for the full view and verify that it statically matches
12296 -- the subtype of the partial view.
12297
12298 procedure Check_Recursive_Declaration (Typ : Entity_Id);
12299 -- If deferred constant is an access type initialized with an allocator,
12300 -- check whether there is an illegal recursion in the definition,
12301 -- through a default value of some record subcomponent. This is normally
12302 -- detected when generating init procs, but requires this additional
12303 -- mechanism when expansion is disabled.
12304
12305 ----------------------------------------
12306 -- Check_Possible_Deferred_Completion --
12307 ----------------------------------------
12308
12309 procedure Check_Possible_Deferred_Completion
12310 (Prev_Id : Entity_Id;
12311 Prev_Obj_Def : Node_Id;
12312 Curr_Obj_Def : Node_Id)
12313 is
12314 begin
12315 if Nkind (Prev_Obj_Def) = N_Subtype_Indication
12316 and then Present (Constraint (Prev_Obj_Def))
12317 and then Nkind (Curr_Obj_Def) = N_Subtype_Indication
12318 and then Present (Constraint (Curr_Obj_Def))
12319 then
12320 declare
12321 Loc : constant Source_Ptr := Sloc (N);
12322 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
12323 Decl : constant Node_Id :=
12324 Make_Subtype_Declaration (Loc,
12325 Defining_Identifier => Def_Id,
12326 Subtype_Indication =>
12327 Relocate_Node (Curr_Obj_Def));
12328
12329 begin
12330 Insert_Before_And_Analyze (N, Decl);
12331 Set_Etype (Id, Def_Id);
12332
12333 if not Subtypes_Statically_Match (Etype (Prev_Id), Def_Id) then
12334 Error_Msg_Sloc := Sloc (Prev_Id);
12335 Error_Msg_N ("subtype does not statically match deferred "
12336 & "declaration #", N);
12337 end if;
12338 end;
12339 end if;
12340 end Check_Possible_Deferred_Completion;
12341
12342 ---------------------------------
12343 -- Check_Recursive_Declaration --
12344 ---------------------------------
12345
12346 procedure Check_Recursive_Declaration (Typ : Entity_Id) is
12347 Comp : Entity_Id;
12348
12349 begin
12350 if Is_Record_Type (Typ) then
12351 Comp := First_Component (Typ);
12352 while Present (Comp) loop
12353 if Comes_From_Source (Comp) then
12354 if Present (Expression (Parent (Comp)))
12355 and then Is_Entity_Name (Expression (Parent (Comp)))
12356 and then Entity (Expression (Parent (Comp))) = Prev
12357 then
12358 Error_Msg_Sloc := Sloc (Parent (Comp));
12359 Error_Msg_NE
12360 ("illegal circularity with declaration for & #",
12361 N, Comp);
12362 return;
12363
12364 elsif Is_Record_Type (Etype (Comp)) then
12365 Check_Recursive_Declaration (Etype (Comp));
12366 end if;
12367 end if;
12368
12369 Next_Component (Comp);
12370 end loop;
12371 end if;
12372 end Check_Recursive_Declaration;
12373
12374 -- Start of processing for Constant_Redeclaration
12375
12376 begin
12377 if Nkind (Parent (Prev)) = N_Object_Declaration then
12378 if Nkind (Object_Definition
12379 (Parent (Prev))) = N_Subtype_Indication
12380 then
12381 -- Find type of new declaration. The constraints of the two
12382 -- views must match statically, but there is no point in
12383 -- creating an itype for the full view.
12384
12385 if Nkind (Obj_Def) = N_Subtype_Indication then
12386 Find_Type (Subtype_Mark (Obj_Def));
12387 New_T := Entity (Subtype_Mark (Obj_Def));
12388
12389 else
12390 Find_Type (Obj_Def);
12391 New_T := Entity (Obj_Def);
12392 end if;
12393
12394 T := Etype (Prev);
12395
12396 else
12397 -- The full view may impose a constraint, even if the partial
12398 -- view does not, so construct the subtype.
12399
12400 New_T := Find_Type_Of_Object (Obj_Def, N);
12401 T := New_T;
12402 end if;
12403
12404 else
12405 -- Current declaration is illegal, diagnosed below in Enter_Name
12406
12407 T := Empty;
12408 New_T := Any_Type;
12409 end if;
12410
12411 -- If previous full declaration or a renaming declaration exists, or if
12412 -- a homograph is present, let Enter_Name handle it, either with an
12413 -- error or with the removal of an overridden implicit subprogram.
12414 -- The previous one is a full declaration if it has an expression
12415 -- (which in the case of an aggregate is indicated by the Init flag).
12416
12417 if Ekind (Prev) /= E_Constant
12418 or else Nkind (Parent (Prev)) = N_Object_Renaming_Declaration
12419 or else Present (Expression (Parent (Prev)))
12420 or else Has_Init_Expression (Parent (Prev))
12421 or else Present (Full_View (Prev))
12422 then
12423 Enter_Name (Id);
12424
12425 -- Verify that types of both declarations match, or else that both types
12426 -- are anonymous access types whose designated subtypes statically match
12427 -- (as allowed in Ada 2005 by AI-385).
12428
12429 elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
12430 and then
12431 (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
12432 or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
12433 or else Is_Access_Constant (Etype (New_T)) /=
12434 Is_Access_Constant (Etype (Prev))
12435 or else Can_Never_Be_Null (Etype (New_T)) /=
12436 Can_Never_Be_Null (Etype (Prev))
12437 or else Null_Exclusion_Present (Parent (Prev)) /=
12438 Null_Exclusion_Present (Parent (Id))
12439 or else not Subtypes_Statically_Match
12440 (Designated_Type (Etype (Prev)),
12441 Designated_Type (Etype (New_T))))
12442 then
12443 Error_Msg_Sloc := Sloc (Prev);
12444 Error_Msg_N ("type does not match declaration#", N);
12445 Set_Full_View (Prev, Id);
12446 Set_Etype (Id, Any_Type);
12447
12448 -- A deferred constant whose type is an anonymous array is always
12449 -- illegal (unless imported). A detailed error message might be
12450 -- helpful for Ada beginners.
12451
12452 if Nkind (Object_Definition (Parent (Prev)))
12453 = N_Constrained_Array_Definition
12454 and then Nkind (Object_Definition (N))
12455 = N_Constrained_Array_Definition
12456 then
12457 Error_Msg_N ("\each anonymous array is a distinct type", N);
12458 Error_Msg_N ("a deferred constant must have a named type",
12459 Object_Definition (Parent (Prev)));
12460 end if;
12461
12462 elsif
12463 Null_Exclusion_Present (Parent (Prev))
12464 and then not Null_Exclusion_Present (N)
12465 then
12466 Error_Msg_Sloc := Sloc (Prev);
12467 Error_Msg_N ("null-exclusion does not match declaration#", N);
12468 Set_Full_View (Prev, Id);
12469 Set_Etype (Id, Any_Type);
12470
12471 -- If so, process the full constant declaration
12472
12473 else
12474 -- RM 7.4 (6): If the subtype defined by the subtype_indication in
12475 -- the deferred declaration is constrained, then the subtype defined
12476 -- by the subtype_indication in the full declaration shall match it
12477 -- statically.
12478
12479 Check_Possible_Deferred_Completion
12480 (Prev_Id => Prev,
12481 Prev_Obj_Def => Object_Definition (Parent (Prev)),
12482 Curr_Obj_Def => Obj_Def);
12483
12484 Set_Full_View (Prev, Id);
12485 Set_Is_Public (Id, Is_Public (Prev));
12486 Set_Is_Internal (Id);
12487 Append_Entity (Id, Current_Scope);
12488
12489 -- Check ALIASED present if present before (RM 7.4(7))
12490
12491 if Is_Aliased (Prev)
12492 and then not Aliased_Present (N)
12493 then
12494 Error_Msg_Sloc := Sloc (Prev);
12495 Error_Msg_N ("ALIASED required (see declaration #)", N);
12496 end if;
12497
12498 -- Check that placement is in private part and that the incomplete
12499 -- declaration appeared in the visible part.
12500
12501 if Ekind (Current_Scope) = E_Package
12502 and then not In_Private_Part (Current_Scope)
12503 then
12504 Error_Msg_Sloc := Sloc (Prev);
12505 Error_Msg_N
12506 ("full constant for declaration # must be in private part", N);
12507
12508 elsif Ekind (Current_Scope) = E_Package
12509 and then
12510 List_Containing (Parent (Prev)) /=
12511 Visible_Declarations (Package_Specification (Current_Scope))
12512 then
12513 Error_Msg_N
12514 ("deferred constant must be declared in visible part",
12515 Parent (Prev));
12516 end if;
12517
12518 if Is_Access_Type (T)
12519 and then Nkind (Expression (N)) = N_Allocator
12520 then
12521 Check_Recursive_Declaration (Designated_Type (T));
12522 end if;
12523
12524 -- A deferred constant is a visible entity. If type has invariants,
12525 -- verify that the initial value satisfies them.
12526
12527 if Has_Invariants (T) and then Present (Invariant_Procedure (T)) then
12528 Insert_After (N,
12529 Make_Invariant_Call (New_Occurrence_Of (Prev, Sloc (N))));
12530 end if;
12531 end if;
12532 end Constant_Redeclaration;
12533
12534 ----------------------
12535 -- Constrain_Access --
12536 ----------------------
12537
12538 procedure Constrain_Access
12539 (Def_Id : in out Entity_Id;
12540 S : Node_Id;
12541 Related_Nod : Node_Id)
12542 is
12543 T : constant Entity_Id := Entity (Subtype_Mark (S));
12544 Desig_Type : constant Entity_Id := Designated_Type (T);
12545 Desig_Subtype : Entity_Id := Create_Itype (E_Void, Related_Nod);
12546 Constraint_OK : Boolean := True;
12547
12548 begin
12549 if Is_Array_Type (Desig_Type) then
12550 Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
12551
12552 elsif (Is_Record_Type (Desig_Type)
12553 or else Is_Incomplete_Or_Private_Type (Desig_Type))
12554 and then not Is_Constrained (Desig_Type)
12555 then
12556 -- ??? The following code is a temporary bypass to ignore a
12557 -- discriminant constraint on access type if it is constraining
12558 -- the current record. Avoid creating the implicit subtype of the
12559 -- record we are currently compiling since right now, we cannot
12560 -- handle these. For now, just return the access type itself.
12561
12562 if Desig_Type = Current_Scope
12563 and then No (Def_Id)
12564 then
12565 Set_Ekind (Desig_Subtype, E_Record_Subtype);
12566 Def_Id := Entity (Subtype_Mark (S));
12567
12568 -- This call added to ensure that the constraint is analyzed
12569 -- (needed for a B test). Note that we still return early from
12570 -- this procedure to avoid recursive processing. ???
12571
12572 Constrain_Discriminated_Type
12573 (Desig_Subtype, S, Related_Nod, For_Access => True);
12574 return;
12575 end if;
12576
12577 -- Enforce rule that the constraint is illegal if there is an
12578 -- unconstrained view of the designated type. This means that the
12579 -- partial view (either a private type declaration or a derivation
12580 -- from a private type) has no discriminants. (Defect Report
12581 -- 8652/0008, Technical Corrigendum 1, checked by ACATS B371001).
12582
12583 -- Rule updated for Ada 2005: The private type is said to have
12584 -- a constrained partial view, given that objects of the type
12585 -- can be declared. Furthermore, the rule applies to all access
12586 -- types, unlike the rule concerning default discriminants (see
12587 -- RM 3.7.1(7/3))
12588
12589 if (Ekind (T) = E_General_Access_Type or else Ada_Version >= Ada_2005)
12590 and then Has_Private_Declaration (Desig_Type)
12591 and then In_Open_Scopes (Scope (Desig_Type))
12592 and then Has_Discriminants (Desig_Type)
12593 then
12594 declare
12595 Pack : constant Node_Id :=
12596 Unit_Declaration_Node (Scope (Desig_Type));
12597 Decls : List_Id;
12598 Decl : Node_Id;
12599
12600 begin
12601 if Nkind (Pack) = N_Package_Declaration then
12602 Decls := Visible_Declarations (Specification (Pack));
12603 Decl := First (Decls);
12604 while Present (Decl) loop
12605 if (Nkind (Decl) = N_Private_Type_Declaration
12606 and then Chars (Defining_Identifier (Decl)) =
12607 Chars (Desig_Type))
12608
12609 or else
12610 (Nkind (Decl) = N_Full_Type_Declaration
12611 and then
12612 Chars (Defining_Identifier (Decl)) =
12613 Chars (Desig_Type)
12614 and then Is_Derived_Type (Desig_Type)
12615 and then
12616 Has_Private_Declaration (Etype (Desig_Type)))
12617 then
12618 if No (Discriminant_Specifications (Decl)) then
12619 Error_Msg_N
12620 ("cannot constrain access type if designated "
12621 & "type has constrained partial view", S);
12622 end if;
12623
12624 exit;
12625 end if;
12626
12627 Next (Decl);
12628 end loop;
12629 end if;
12630 end;
12631 end if;
12632
12633 Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
12634 For_Access => True);
12635
12636 elsif Is_Concurrent_Type (Desig_Type)
12637 and then not Is_Constrained (Desig_Type)
12638 then
12639 Constrain_Concurrent (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
12640
12641 else
12642 Error_Msg_N ("invalid constraint on access type", S);
12643
12644 -- We simply ignore an invalid constraint
12645
12646 Desig_Subtype := Desig_Type;
12647 Constraint_OK := False;
12648 end if;
12649
12650 if No (Def_Id) then
12651 Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
12652 else
12653 Set_Ekind (Def_Id, E_Access_Subtype);
12654 end if;
12655
12656 if Constraint_OK then
12657 Set_Etype (Def_Id, Base_Type (T));
12658
12659 if Is_Private_Type (Desig_Type) then
12660 Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
12661 end if;
12662 else
12663 Set_Etype (Def_Id, Any_Type);
12664 end if;
12665
12666 Set_Size_Info (Def_Id, T);
12667 Set_Is_Constrained (Def_Id, Constraint_OK);
12668 Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
12669 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
12670 Set_Is_Access_Constant (Def_Id, Is_Access_Constant (T));
12671
12672 Conditional_Delay (Def_Id, T);
12673
12674 -- AI-363 : Subtypes of general access types whose designated types have
12675 -- default discriminants are disallowed. In instances, the rule has to
12676 -- be checked against the actual, of which T is the subtype. In a
12677 -- generic body, the rule is checked assuming that the actual type has
12678 -- defaulted discriminants.
12679
12680 if Ada_Version >= Ada_2005 or else Warn_On_Ada_2005_Compatibility then
12681 if Ekind (Base_Type (T)) = E_General_Access_Type
12682 and then Has_Defaulted_Discriminants (Desig_Type)
12683 then
12684 if Ada_Version < Ada_2005 then
12685 Error_Msg_N
12686 ("access subtype of general access type would not " &
12687 "be allowed in Ada 2005?y?", S);
12688 else
12689 Error_Msg_N
12690 ("access subtype of general access type not allowed", S);
12691 end if;
12692
12693 Error_Msg_N ("\discriminants have defaults", S);
12694
12695 elsif Is_Access_Type (T)
12696 and then Is_Generic_Type (Desig_Type)
12697 and then Has_Discriminants (Desig_Type)
12698 and then In_Package_Body (Current_Scope)
12699 then
12700 if Ada_Version < Ada_2005 then
12701 Error_Msg_N
12702 ("access subtype would not be allowed in generic body "
12703 & "in Ada 2005?y?", S);
12704 else
12705 Error_Msg_N
12706 ("access subtype not allowed in generic body", S);
12707 end if;
12708
12709 Error_Msg_N
12710 ("\designated type is a discriminated formal", S);
12711 end if;
12712 end if;
12713 end Constrain_Access;
12714
12715 ---------------------
12716 -- Constrain_Array --
12717 ---------------------
12718
12719 procedure Constrain_Array
12720 (Def_Id : in out Entity_Id;
12721 SI : Node_Id;
12722 Related_Nod : Node_Id;
12723 Related_Id : Entity_Id;
12724 Suffix : Character)
12725 is
12726 C : constant Node_Id := Constraint (SI);
12727 Number_Of_Constraints : Nat := 0;
12728 Index : Node_Id;
12729 S, T : Entity_Id;
12730 Constraint_OK : Boolean := True;
12731
12732 begin
12733 T := Entity (Subtype_Mark (SI));
12734
12735 if Is_Access_Type (T) then
12736 T := Designated_Type (T);
12737 end if;
12738
12739 -- If an index constraint follows a subtype mark in a subtype indication
12740 -- then the type or subtype denoted by the subtype mark must not already
12741 -- impose an index constraint. The subtype mark must denote either an
12742 -- unconstrained array type or an access type whose designated type
12743 -- is such an array type... (RM 3.6.1)
12744
12745 if Is_Constrained (T) then
12746 Error_Msg_N ("array type is already constrained", Subtype_Mark (SI));
12747 Constraint_OK := False;
12748
12749 else
12750 S := First (Constraints (C));
12751 while Present (S) loop
12752 Number_Of_Constraints := Number_Of_Constraints + 1;
12753 Next (S);
12754 end loop;
12755
12756 -- In either case, the index constraint must provide a discrete
12757 -- range for each index of the array type and the type of each
12758 -- discrete range must be the same as that of the corresponding
12759 -- index. (RM 3.6.1)
12760
12761 if Number_Of_Constraints /= Number_Dimensions (T) then
12762 Error_Msg_NE ("incorrect number of index constraints for }", C, T);
12763 Constraint_OK := False;
12764
12765 else
12766 S := First (Constraints (C));
12767 Index := First_Index (T);
12768 Analyze (Index);
12769
12770 -- Apply constraints to each index type
12771
12772 for J in 1 .. Number_Of_Constraints loop
12773 Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
12774 Next (Index);
12775 Next (S);
12776 end loop;
12777
12778 end if;
12779 end if;
12780
12781 if No (Def_Id) then
12782 Def_Id :=
12783 Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
12784 Set_Parent (Def_Id, Related_Nod);
12785
12786 else
12787 Set_Ekind (Def_Id, E_Array_Subtype);
12788 end if;
12789
12790 Set_Size_Info (Def_Id, (T));
12791 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
12792 Set_Etype (Def_Id, Base_Type (T));
12793
12794 if Constraint_OK then
12795 Set_First_Index (Def_Id, First (Constraints (C)));
12796 else
12797 Set_First_Index (Def_Id, First_Index (T));
12798 end if;
12799
12800 Set_Is_Constrained (Def_Id, True);
12801 Set_Is_Aliased (Def_Id, Is_Aliased (T));
12802 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
12803
12804 Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
12805 Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
12806
12807 -- A subtype does not inherit the Packed_Array_Impl_Type of is parent.
12808 -- We need to initialize the attribute because if Def_Id is previously
12809 -- analyzed through a limited_with clause, it will have the attributes
12810 -- of an incomplete type, one of which is an Elist that overlaps the
12811 -- Packed_Array_Impl_Type field.
12812
12813 Set_Packed_Array_Impl_Type (Def_Id, Empty);
12814
12815 -- Build a freeze node if parent still needs one. Also make sure that
12816 -- the Depends_On_Private status is set because the subtype will need
12817 -- reprocessing at the time the base type does, and also we must set a
12818 -- conditional delay.
12819
12820 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
12821 Conditional_Delay (Def_Id, T);
12822 end Constrain_Array;
12823
12824 ------------------------------
12825 -- Constrain_Component_Type --
12826 ------------------------------
12827
12828 function Constrain_Component_Type
12829 (Comp : Entity_Id;
12830 Constrained_Typ : Entity_Id;
12831 Related_Node : Node_Id;
12832 Typ : Entity_Id;
12833 Constraints : Elist_Id) return Entity_Id
12834 is
12835 Loc : constant Source_Ptr := Sloc (Constrained_Typ);
12836 Compon_Type : constant Entity_Id := Etype (Comp);
12837
12838 function Build_Constrained_Array_Type
12839 (Old_Type : Entity_Id) return Entity_Id;
12840 -- If Old_Type is an array type, one of whose indexes is constrained
12841 -- by a discriminant, build an Itype whose constraint replaces the
12842 -- discriminant with its value in the constraint.
12843
12844 function Build_Constrained_Discriminated_Type
12845 (Old_Type : Entity_Id) return Entity_Id;
12846 -- Ditto for record components
12847
12848 function Build_Constrained_Access_Type
12849 (Old_Type : Entity_Id) return Entity_Id;
12850 -- Ditto for access types. Makes use of previous two functions, to
12851 -- constrain designated type.
12852
12853 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id;
12854 -- T is an array or discriminated type, C is a list of constraints
12855 -- that apply to T. This routine builds the constrained subtype.
12856
12857 function Is_Discriminant (Expr : Node_Id) return Boolean;
12858 -- Returns True if Expr is a discriminant
12859
12860 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id;
12861 -- Find the value of discriminant Discrim in Constraint
12862
12863 -----------------------------------
12864 -- Build_Constrained_Access_Type --
12865 -----------------------------------
12866
12867 function Build_Constrained_Access_Type
12868 (Old_Type : Entity_Id) return Entity_Id
12869 is
12870 Desig_Type : constant Entity_Id := Designated_Type (Old_Type);
12871 Itype : Entity_Id;
12872 Desig_Subtype : Entity_Id;
12873 Scop : Entity_Id;
12874
12875 begin
12876 -- if the original access type was not embedded in the enclosing
12877 -- type definition, there is no need to produce a new access
12878 -- subtype. In fact every access type with an explicit constraint
12879 -- generates an itype whose scope is the enclosing record.
12880
12881 if not Is_Type (Scope (Old_Type)) then
12882 return Old_Type;
12883
12884 elsif Is_Array_Type (Desig_Type) then
12885 Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
12886
12887 elsif Has_Discriminants (Desig_Type) then
12888
12889 -- This may be an access type to an enclosing record type for
12890 -- which we are constructing the constrained components. Return
12891 -- the enclosing record subtype. This is not always correct,
12892 -- but avoids infinite recursion. ???
12893
12894 Desig_Subtype := Any_Type;
12895
12896 for J in reverse 0 .. Scope_Stack.Last loop
12897 Scop := Scope_Stack.Table (J).Entity;
12898
12899 if Is_Type (Scop)
12900 and then Base_Type (Scop) = Base_Type (Desig_Type)
12901 then
12902 Desig_Subtype := Scop;
12903 end if;
12904
12905 exit when not Is_Type (Scop);
12906 end loop;
12907
12908 if Desig_Subtype = Any_Type then
12909 Desig_Subtype :=
12910 Build_Constrained_Discriminated_Type (Desig_Type);
12911 end if;
12912
12913 else
12914 return Old_Type;
12915 end if;
12916
12917 if Desig_Subtype /= Desig_Type then
12918
12919 -- The Related_Node better be here or else we won't be able
12920 -- to attach new itypes to a node in the tree.
12921
12922 pragma Assert (Present (Related_Node));
12923
12924 Itype := Create_Itype (E_Access_Subtype, Related_Node);
12925
12926 Set_Etype (Itype, Base_Type (Old_Type));
12927 Set_Size_Info (Itype, (Old_Type));
12928 Set_Directly_Designated_Type (Itype, Desig_Subtype);
12929 Set_Depends_On_Private (Itype, Has_Private_Component
12930 (Old_Type));
12931 Set_Is_Access_Constant (Itype, Is_Access_Constant
12932 (Old_Type));
12933
12934 -- The new itype needs freezing when it depends on a not frozen
12935 -- type and the enclosing subtype needs freezing.
12936
12937 if Has_Delayed_Freeze (Constrained_Typ)
12938 and then not Is_Frozen (Constrained_Typ)
12939 then
12940 Conditional_Delay (Itype, Base_Type (Old_Type));
12941 end if;
12942
12943 return Itype;
12944
12945 else
12946 return Old_Type;
12947 end if;
12948 end Build_Constrained_Access_Type;
12949
12950 ----------------------------------
12951 -- Build_Constrained_Array_Type --
12952 ----------------------------------
12953
12954 function Build_Constrained_Array_Type
12955 (Old_Type : Entity_Id) return Entity_Id
12956 is
12957 Lo_Expr : Node_Id;
12958 Hi_Expr : Node_Id;
12959 Old_Index : Node_Id;
12960 Range_Node : Node_Id;
12961 Constr_List : List_Id;
12962
12963 Need_To_Create_Itype : Boolean := False;
12964
12965 begin
12966 Old_Index := First_Index (Old_Type);
12967 while Present (Old_Index) loop
12968 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
12969
12970 if Is_Discriminant (Lo_Expr)
12971 or else
12972 Is_Discriminant (Hi_Expr)
12973 then
12974 Need_To_Create_Itype := True;
12975 end if;
12976
12977 Next_Index (Old_Index);
12978 end loop;
12979
12980 if Need_To_Create_Itype then
12981 Constr_List := New_List;
12982
12983 Old_Index := First_Index (Old_Type);
12984 while Present (Old_Index) loop
12985 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
12986
12987 if Is_Discriminant (Lo_Expr) then
12988 Lo_Expr := Get_Discr_Value (Lo_Expr);
12989 end if;
12990
12991 if Is_Discriminant (Hi_Expr) then
12992 Hi_Expr := Get_Discr_Value (Hi_Expr);
12993 end if;
12994
12995 Range_Node :=
12996 Make_Range
12997 (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
12998
12999 Append (Range_Node, To => Constr_List);
13000
13001 Next_Index (Old_Index);
13002 end loop;
13003
13004 return Build_Subtype (Old_Type, Constr_List);
13005
13006 else
13007 return Old_Type;
13008 end if;
13009 end Build_Constrained_Array_Type;
13010
13011 ------------------------------------------
13012 -- Build_Constrained_Discriminated_Type --
13013 ------------------------------------------
13014
13015 function Build_Constrained_Discriminated_Type
13016 (Old_Type : Entity_Id) return Entity_Id
13017 is
13018 Expr : Node_Id;
13019 Constr_List : List_Id;
13020 Old_Constraint : Elmt_Id;
13021
13022 Need_To_Create_Itype : Boolean := False;
13023
13024 begin
13025 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
13026 while Present (Old_Constraint) loop
13027 Expr := Node (Old_Constraint);
13028
13029 if Is_Discriminant (Expr) then
13030 Need_To_Create_Itype := True;
13031 end if;
13032
13033 Next_Elmt (Old_Constraint);
13034 end loop;
13035
13036 if Need_To_Create_Itype then
13037 Constr_List := New_List;
13038
13039 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
13040 while Present (Old_Constraint) loop
13041 Expr := Node (Old_Constraint);
13042
13043 if Is_Discriminant (Expr) then
13044 Expr := Get_Discr_Value (Expr);
13045 end if;
13046
13047 Append (New_Copy_Tree (Expr), To => Constr_List);
13048
13049 Next_Elmt (Old_Constraint);
13050 end loop;
13051
13052 return Build_Subtype (Old_Type, Constr_List);
13053
13054 else
13055 return Old_Type;
13056 end if;
13057 end Build_Constrained_Discriminated_Type;
13058
13059 -------------------
13060 -- Build_Subtype --
13061 -------------------
13062
13063 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id is
13064 Indic : Node_Id;
13065 Subtyp_Decl : Node_Id;
13066 Def_Id : Entity_Id;
13067 Btyp : Entity_Id := Base_Type (T);
13068
13069 begin
13070 -- The Related_Node better be here or else we won't be able to
13071 -- attach new itypes to a node in the tree.
13072
13073 pragma Assert (Present (Related_Node));
13074
13075 -- If the view of the component's type is incomplete or private
13076 -- with unknown discriminants, then the constraint must be applied
13077 -- to the full type.
13078
13079 if Has_Unknown_Discriminants (Btyp)
13080 and then Present (Underlying_Type (Btyp))
13081 then
13082 Btyp := Underlying_Type (Btyp);
13083 end if;
13084
13085 Indic :=
13086 Make_Subtype_Indication (Loc,
13087 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
13088 Constraint => Make_Index_Or_Discriminant_Constraint (Loc, C));
13089
13090 Def_Id := Create_Itype (Ekind (T), Related_Node);
13091
13092 Subtyp_Decl :=
13093 Make_Subtype_Declaration (Loc,
13094 Defining_Identifier => Def_Id,
13095 Subtype_Indication => Indic);
13096
13097 Set_Parent (Subtyp_Decl, Parent (Related_Node));
13098
13099 -- Itypes must be analyzed with checks off (see package Itypes)
13100
13101 Analyze (Subtyp_Decl, Suppress => All_Checks);
13102
13103 return Def_Id;
13104 end Build_Subtype;
13105
13106 ---------------------
13107 -- Get_Discr_Value --
13108 ---------------------
13109
13110 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id is
13111 D : Entity_Id;
13112 E : Elmt_Id;
13113
13114 begin
13115 -- The discriminant may be declared for the type, in which case we
13116 -- find it by iterating over the list of discriminants. If the
13117 -- discriminant is inherited from a parent type, it appears as the
13118 -- corresponding discriminant of the current type. This will be the
13119 -- case when constraining an inherited component whose constraint is
13120 -- given by a discriminant of the parent.
13121
13122 D := First_Discriminant (Typ);
13123 E := First_Elmt (Constraints);
13124
13125 while Present (D) loop
13126 if D = Entity (Discrim)
13127 or else D = CR_Discriminant (Entity (Discrim))
13128 or else Corresponding_Discriminant (D) = Entity (Discrim)
13129 then
13130 return Node (E);
13131 end if;
13132
13133 Next_Discriminant (D);
13134 Next_Elmt (E);
13135 end loop;
13136
13137 -- The Corresponding_Discriminant mechanism is incomplete, because
13138 -- the correspondence between new and old discriminants is not one
13139 -- to one: one new discriminant can constrain several old ones. In
13140 -- that case, scan sequentially the stored_constraint, the list of
13141 -- discriminants of the parents, and the constraints.
13142
13143 -- Previous code checked for the present of the Stored_Constraint
13144 -- list for the derived type, but did not use it at all. Should it
13145 -- be present when the component is a discriminated task type?
13146
13147 if Is_Derived_Type (Typ)
13148 and then Scope (Entity (Discrim)) = Etype (Typ)
13149 then
13150 D := First_Discriminant (Etype (Typ));
13151 E := First_Elmt (Constraints);
13152 while Present (D) loop
13153 if D = Entity (Discrim) then
13154 return Node (E);
13155 end if;
13156
13157 Next_Discriminant (D);
13158 Next_Elmt (E);
13159 end loop;
13160 end if;
13161
13162 -- Something is wrong if we did not find the value
13163
13164 raise Program_Error;
13165 end Get_Discr_Value;
13166
13167 ---------------------
13168 -- Is_Discriminant --
13169 ---------------------
13170
13171 function Is_Discriminant (Expr : Node_Id) return Boolean is
13172 Discrim_Scope : Entity_Id;
13173
13174 begin
13175 if Denotes_Discriminant (Expr) then
13176 Discrim_Scope := Scope (Entity (Expr));
13177
13178 -- Either we have a reference to one of Typ's discriminants,
13179
13180 pragma Assert (Discrim_Scope = Typ
13181
13182 -- or to the discriminants of the parent type, in the case
13183 -- of a derivation of a tagged type with variants.
13184
13185 or else Discrim_Scope = Etype (Typ)
13186 or else Full_View (Discrim_Scope) = Etype (Typ)
13187
13188 -- or same as above for the case where the discriminants
13189 -- were declared in Typ's private view.
13190
13191 or else (Is_Private_Type (Discrim_Scope)
13192 and then Chars (Discrim_Scope) = Chars (Typ))
13193
13194 -- or else we are deriving from the full view and the
13195 -- discriminant is declared in the private entity.
13196
13197 or else (Is_Private_Type (Typ)
13198 and then Chars (Discrim_Scope) = Chars (Typ))
13199
13200 -- Or we are constrained the corresponding record of a
13201 -- synchronized type that completes a private declaration.
13202
13203 or else (Is_Concurrent_Record_Type (Typ)
13204 and then
13205 Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
13206
13207 -- or we have a class-wide type, in which case make sure the
13208 -- discriminant found belongs to the root type.
13209
13210 or else (Is_Class_Wide_Type (Typ)
13211 and then Etype (Typ) = Discrim_Scope));
13212
13213 return True;
13214 end if;
13215
13216 -- In all other cases we have something wrong
13217
13218 return False;
13219 end Is_Discriminant;
13220
13221 -- Start of processing for Constrain_Component_Type
13222
13223 begin
13224 if Nkind (Parent (Comp)) = N_Component_Declaration
13225 and then Comes_From_Source (Parent (Comp))
13226 and then Comes_From_Source
13227 (Subtype_Indication (Component_Definition (Parent (Comp))))
13228 and then
13229 Is_Entity_Name
13230 (Subtype_Indication (Component_Definition (Parent (Comp))))
13231 then
13232 return Compon_Type;
13233
13234 elsif Is_Array_Type (Compon_Type) then
13235 return Build_Constrained_Array_Type (Compon_Type);
13236
13237 elsif Has_Discriminants (Compon_Type) then
13238 return Build_Constrained_Discriminated_Type (Compon_Type);
13239
13240 elsif Is_Access_Type (Compon_Type) then
13241 return Build_Constrained_Access_Type (Compon_Type);
13242
13243 else
13244 return Compon_Type;
13245 end if;
13246 end Constrain_Component_Type;
13247
13248 --------------------------
13249 -- Constrain_Concurrent --
13250 --------------------------
13251
13252 -- For concurrent types, the associated record value type carries the same
13253 -- discriminants, so when we constrain a concurrent type, we must constrain
13254 -- the corresponding record type as well.
13255
13256 procedure Constrain_Concurrent
13257 (Def_Id : in out Entity_Id;
13258 SI : Node_Id;
13259 Related_Nod : Node_Id;
13260 Related_Id : Entity_Id;
13261 Suffix : Character)
13262 is
13263 -- Retrieve Base_Type to ensure getting to the concurrent type in the
13264 -- case of a private subtype (needed when only doing semantic analysis).
13265
13266 T_Ent : Entity_Id := Base_Type (Entity (Subtype_Mark (SI)));
13267 T_Val : Entity_Id;
13268
13269 begin
13270 if Is_Access_Type (T_Ent) then
13271 T_Ent := Designated_Type (T_Ent);
13272 end if;
13273
13274 T_Val := Corresponding_Record_Type (T_Ent);
13275
13276 if Present (T_Val) then
13277
13278 if No (Def_Id) then
13279 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
13280
13281 -- Elaborate itype now, as it may be used in a subsequent
13282 -- synchronized operation in another scope.
13283
13284 if Nkind (Related_Nod) = N_Full_Type_Declaration then
13285 Build_Itype_Reference (Def_Id, Related_Nod);
13286 end if;
13287 end if;
13288
13289 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
13290
13291 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13292 Set_Corresponding_Record_Type (Def_Id,
13293 Constrain_Corresponding_Record (Def_Id, T_Val, Related_Nod));
13294
13295 else
13296 -- If there is no associated record, expansion is disabled and this
13297 -- is a generic context. Create a subtype in any case, so that
13298 -- semantic analysis can proceed.
13299
13300 if No (Def_Id) then
13301 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
13302 end if;
13303
13304 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
13305 end if;
13306 end Constrain_Concurrent;
13307
13308 ------------------------------------
13309 -- Constrain_Corresponding_Record --
13310 ------------------------------------
13311
13312 function Constrain_Corresponding_Record
13313 (Prot_Subt : Entity_Id;
13314 Corr_Rec : Entity_Id;
13315 Related_Nod : Node_Id) return Entity_Id
13316 is
13317 T_Sub : constant Entity_Id :=
13318 Create_Itype (E_Record_Subtype, Related_Nod, Corr_Rec, 'C');
13319
13320 begin
13321 Set_Etype (T_Sub, Corr_Rec);
13322 Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
13323 Set_Is_Constrained (T_Sub, True);
13324 Set_First_Entity (T_Sub, First_Entity (Corr_Rec));
13325 Set_Last_Entity (T_Sub, Last_Entity (Corr_Rec));
13326
13327 if Has_Discriminants (Prot_Subt) then -- False only if errors.
13328 Set_Discriminant_Constraint
13329 (T_Sub, Discriminant_Constraint (Prot_Subt));
13330 Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
13331 Create_Constrained_Components
13332 (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
13333 end if;
13334
13335 Set_Depends_On_Private (T_Sub, Has_Private_Component (T_Sub));
13336
13337 if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
13338 Conditional_Delay (T_Sub, Corr_Rec);
13339
13340 else
13341 -- This is a component subtype: it will be frozen in the context of
13342 -- the enclosing record's init_proc, so that discriminant references
13343 -- are resolved to discriminals. (Note: we used to skip freezing
13344 -- altogether in that case, which caused errors downstream for
13345 -- components of a bit packed array type).
13346
13347 Set_Has_Delayed_Freeze (T_Sub);
13348 end if;
13349
13350 return T_Sub;
13351 end Constrain_Corresponding_Record;
13352
13353 -----------------------
13354 -- Constrain_Decimal --
13355 -----------------------
13356
13357 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id) is
13358 T : constant Entity_Id := Entity (Subtype_Mark (S));
13359 C : constant Node_Id := Constraint (S);
13360 Loc : constant Source_Ptr := Sloc (C);
13361 Range_Expr : Node_Id;
13362 Digits_Expr : Node_Id;
13363 Digits_Val : Uint;
13364 Bound_Val : Ureal;
13365
13366 begin
13367 Set_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
13368
13369 if Nkind (C) = N_Range_Constraint then
13370 Range_Expr := Range_Expression (C);
13371 Digits_Val := Digits_Value (T);
13372
13373 else
13374 pragma Assert (Nkind (C) = N_Digits_Constraint);
13375
13376 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
13377
13378 Digits_Expr := Digits_Expression (C);
13379 Analyze_And_Resolve (Digits_Expr, Any_Integer);
13380
13381 Check_Digits_Expression (Digits_Expr);
13382 Digits_Val := Expr_Value (Digits_Expr);
13383
13384 if Digits_Val > Digits_Value (T) then
13385 Error_Msg_N
13386 ("digits expression is incompatible with subtype", C);
13387 Digits_Val := Digits_Value (T);
13388 end if;
13389
13390 if Present (Range_Constraint (C)) then
13391 Range_Expr := Range_Expression (Range_Constraint (C));
13392 else
13393 Range_Expr := Empty;
13394 end if;
13395 end if;
13396
13397 Set_Etype (Def_Id, Base_Type (T));
13398 Set_Size_Info (Def_Id, (T));
13399 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13400 Set_Delta_Value (Def_Id, Delta_Value (T));
13401 Set_Scale_Value (Def_Id, Scale_Value (T));
13402 Set_Small_Value (Def_Id, Small_Value (T));
13403 Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
13404 Set_Digits_Value (Def_Id, Digits_Val);
13405
13406 -- Manufacture range from given digits value if no range present
13407
13408 if No (Range_Expr) then
13409 Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
13410 Range_Expr :=
13411 Make_Range (Loc,
13412 Low_Bound =>
13413 Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
13414 High_Bound =>
13415 Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
13416 end if;
13417
13418 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
13419 Set_Discrete_RM_Size (Def_Id);
13420
13421 -- Unconditionally delay the freeze, since we cannot set size
13422 -- information in all cases correctly until the freeze point.
13423
13424 Set_Has_Delayed_Freeze (Def_Id);
13425 end Constrain_Decimal;
13426
13427 ----------------------------------
13428 -- Constrain_Discriminated_Type --
13429 ----------------------------------
13430
13431 procedure Constrain_Discriminated_Type
13432 (Def_Id : Entity_Id;
13433 S : Node_Id;
13434 Related_Nod : Node_Id;
13435 For_Access : Boolean := False)
13436 is
13437 E : Entity_Id := Entity (Subtype_Mark (S));
13438 T : Entity_Id;
13439
13440 procedure Fixup_Bad_Constraint;
13441 -- Called after finding a bad constraint, and after having posted an
13442 -- appropriate error message. The goal is to leave type Def_Id in as
13443 -- reasonable state as possible.
13444
13445 --------------------------
13446 -- Fixup_Bad_Constraint --
13447 --------------------------
13448
13449 procedure Fixup_Bad_Constraint is
13450 begin
13451 -- Set a reasonable Ekind for the entity. For an incomplete type,
13452 -- we can't do much, but for other types, we can set the proper
13453 -- corresponding subtype kind.
13454
13455 if Ekind (T) = E_Incomplete_Type then
13456 Set_Ekind (Def_Id, Ekind (T));
13457 else
13458 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
13459 end if;
13460
13461 -- Set Etype to the known type, to reduce chances of cascaded errors
13462
13463 Set_Etype (Def_Id, E);
13464 Set_Error_Posted (Def_Id);
13465 end Fixup_Bad_Constraint;
13466
13467 -- Local variables
13468
13469 C : Node_Id;
13470 Constr : Elist_Id := New_Elmt_List;
13471
13472 -- Start of processing for Constrain_Discriminated_Type
13473
13474 begin
13475 C := Constraint (S);
13476
13477 -- A discriminant constraint is only allowed in a subtype indication,
13478 -- after a subtype mark. This subtype mark must denote either a type
13479 -- with discriminants, or an access type whose designated type is a
13480 -- type with discriminants. A discriminant constraint specifies the
13481 -- values of these discriminants (RM 3.7.2(5)).
13482
13483 T := Base_Type (Entity (Subtype_Mark (S)));
13484
13485 if Is_Access_Type (T) then
13486 T := Designated_Type (T);
13487 end if;
13488
13489 -- In an instance it may be necessary to retrieve the full view of a
13490 -- type with unknown discriminants, or a full view with defaulted
13491 -- discriminants. In other contexts the constraint is illegal.
13492
13493 if In_Instance
13494 and then Is_Private_Type (T)
13495 and then Present (Full_View (T))
13496 and then
13497 (Has_Unknown_Discriminants (T)
13498 or else
13499 (not Has_Discriminants (T)
13500 and then Has_Discriminants (Full_View (T))
13501 and then Present (Discriminant_Default_Value
13502 (First_Discriminant (Full_View (T))))))
13503 then
13504 T := Full_View (T);
13505 E := Full_View (E);
13506 end if;
13507
13508 -- Ada 2005 (AI-412): Constrained incomplete subtypes are illegal. Avoid
13509 -- generating an error for access-to-incomplete subtypes.
13510
13511 if Ada_Version >= Ada_2005
13512 and then Ekind (T) = E_Incomplete_Type
13513 and then Nkind (Parent (S)) = N_Subtype_Declaration
13514 and then not Is_Itype (Def_Id)
13515 then
13516 -- A little sanity check: emit an error message if the type has
13517 -- discriminants to begin with. Type T may be a regular incomplete
13518 -- type or imported via a limited with clause.
13519
13520 if Has_Discriminants (T)
13521 or else (From_Limited_With (T)
13522 and then Present (Non_Limited_View (T))
13523 and then Nkind (Parent (Non_Limited_View (T))) =
13524 N_Full_Type_Declaration
13525 and then Present (Discriminant_Specifications
13526 (Parent (Non_Limited_View (T)))))
13527 then
13528 Error_Msg_N
13529 ("(Ada 2005) incomplete subtype may not be constrained", C);
13530 else
13531 Error_Msg_N ("invalid constraint: type has no discriminant", C);
13532 end if;
13533
13534 Fixup_Bad_Constraint;
13535 return;
13536
13537 -- Check that the type has visible discriminants. The type may be
13538 -- a private type with unknown discriminants whose full view has
13539 -- discriminants which are invisible.
13540
13541 elsif not Has_Discriminants (T)
13542 or else
13543 (Has_Unknown_Discriminants (T)
13544 and then Is_Private_Type (T))
13545 then
13546 Error_Msg_N ("invalid constraint: type has no discriminant", C);
13547 Fixup_Bad_Constraint;
13548 return;
13549
13550 elsif Is_Constrained (E)
13551 or else (Ekind (E) = E_Class_Wide_Subtype
13552 and then Present (Discriminant_Constraint (E)))
13553 then
13554 Error_Msg_N ("type is already constrained", Subtype_Mark (S));
13555 Fixup_Bad_Constraint;
13556 return;
13557 end if;
13558
13559 -- T may be an unconstrained subtype (e.g. a generic actual). Constraint
13560 -- applies to the base type.
13561
13562 T := Base_Type (T);
13563
13564 Constr := Build_Discriminant_Constraints (T, S);
13565
13566 -- If the list returned was empty we had an error in building the
13567 -- discriminant constraint. We have also already signalled an error
13568 -- in the incomplete type case
13569
13570 if Is_Empty_Elmt_List (Constr) then
13571 Fixup_Bad_Constraint;
13572 return;
13573 end if;
13574
13575 Build_Discriminated_Subtype (T, Def_Id, Constr, Related_Nod, For_Access);
13576 end Constrain_Discriminated_Type;
13577
13578 ---------------------------
13579 -- Constrain_Enumeration --
13580 ---------------------------
13581
13582 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id) is
13583 T : constant Entity_Id := Entity (Subtype_Mark (S));
13584 C : constant Node_Id := Constraint (S);
13585
13586 begin
13587 Set_Ekind (Def_Id, E_Enumeration_Subtype);
13588
13589 Set_First_Literal (Def_Id, First_Literal (Base_Type (T)));
13590
13591 Set_Etype (Def_Id, Base_Type (T));
13592 Set_Size_Info (Def_Id, (T));
13593 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13594 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
13595
13596 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13597
13598 Set_Discrete_RM_Size (Def_Id);
13599 end Constrain_Enumeration;
13600
13601 ----------------------
13602 -- Constrain_Float --
13603 ----------------------
13604
13605 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id) is
13606 T : constant Entity_Id := Entity (Subtype_Mark (S));
13607 C : Node_Id;
13608 D : Node_Id;
13609 Rais : Node_Id;
13610
13611 begin
13612 Set_Ekind (Def_Id, E_Floating_Point_Subtype);
13613
13614 Set_Etype (Def_Id, Base_Type (T));
13615 Set_Size_Info (Def_Id, (T));
13616 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13617
13618 -- Process the constraint
13619
13620 C := Constraint (S);
13621
13622 -- Digits constraint present
13623
13624 if Nkind (C) = N_Digits_Constraint then
13625
13626 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
13627 Check_Restriction (No_Obsolescent_Features, C);
13628
13629 if Warn_On_Obsolescent_Feature then
13630 Error_Msg_N
13631 ("subtype digits constraint is an " &
13632 "obsolescent feature (RM J.3(8))?j?", C);
13633 end if;
13634
13635 D := Digits_Expression (C);
13636 Analyze_And_Resolve (D, Any_Integer);
13637 Check_Digits_Expression (D);
13638 Set_Digits_Value (Def_Id, Expr_Value (D));
13639
13640 -- Check that digits value is in range. Obviously we can do this
13641 -- at compile time, but it is strictly a runtime check, and of
13642 -- course there is an ACVC test that checks this.
13643
13644 if Digits_Value (Def_Id) > Digits_Value (T) then
13645 Error_Msg_Uint_1 := Digits_Value (T);
13646 Error_Msg_N ("??digits value is too large, maximum is ^", D);
13647 Rais :=
13648 Make_Raise_Constraint_Error (Sloc (D),
13649 Reason => CE_Range_Check_Failed);
13650 Insert_Action (Declaration_Node (Def_Id), Rais);
13651 end if;
13652
13653 C := Range_Constraint (C);
13654
13655 -- No digits constraint present
13656
13657 else
13658 Set_Digits_Value (Def_Id, Digits_Value (T));
13659 end if;
13660
13661 -- Range constraint present
13662
13663 if Nkind (C) = N_Range_Constraint then
13664 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13665
13666 -- No range constraint present
13667
13668 else
13669 pragma Assert (No (C));
13670 Set_Scalar_Range (Def_Id, Scalar_Range (T));
13671 end if;
13672
13673 Set_Is_Constrained (Def_Id);
13674 end Constrain_Float;
13675
13676 ---------------------
13677 -- Constrain_Index --
13678 ---------------------
13679
13680 procedure Constrain_Index
13681 (Index : Node_Id;
13682 S : Node_Id;
13683 Related_Nod : Node_Id;
13684 Related_Id : Entity_Id;
13685 Suffix : Character;
13686 Suffix_Index : Nat)
13687 is
13688 Def_Id : Entity_Id;
13689 R : Node_Id := Empty;
13690 T : constant Entity_Id := Etype (Index);
13691
13692 begin
13693 Def_Id :=
13694 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
13695 Set_Etype (Def_Id, Base_Type (T));
13696
13697 if Nkind (S) = N_Range
13698 or else
13699 (Nkind (S) = N_Attribute_Reference
13700 and then Attribute_Name (S) = Name_Range)
13701 then
13702 -- A Range attribute will be transformed into N_Range by Resolve
13703
13704 Analyze (S);
13705 Set_Etype (S, T);
13706 R := S;
13707
13708 Process_Range_Expr_In_Decl (R, T);
13709
13710 if not Error_Posted (S)
13711 and then
13712 (Nkind (S) /= N_Range
13713 or else not Covers (T, (Etype (Low_Bound (S))))
13714 or else not Covers (T, (Etype (High_Bound (S)))))
13715 then
13716 if Base_Type (T) /= Any_Type
13717 and then Etype (Low_Bound (S)) /= Any_Type
13718 and then Etype (High_Bound (S)) /= Any_Type
13719 then
13720 Error_Msg_N ("range expected", S);
13721 end if;
13722 end if;
13723
13724 elsif Nkind (S) = N_Subtype_Indication then
13725
13726 -- The parser has verified that this is a discrete indication
13727
13728 Resolve_Discrete_Subtype_Indication (S, T);
13729 Bad_Predicated_Subtype_Use
13730 ("subtype& has predicate, not allowed in index constraint",
13731 S, Entity (Subtype_Mark (S)));
13732
13733 R := Range_Expression (Constraint (S));
13734
13735 -- Capture values of bounds and generate temporaries for them if
13736 -- needed, since checks may cause duplication of the expressions
13737 -- which must not be reevaluated.
13738
13739 -- The forced evaluation removes side effects from expressions, which
13740 -- should occur also in GNATprove mode. Otherwise, we end up with
13741 -- unexpected insertions of actions at places where this is not
13742 -- supposed to occur, e.g. on default parameters of a call.
13743
13744 if Expander_Active or GNATprove_Mode then
13745 Force_Evaluation
13746 (Low_Bound (R), Related_Id => Def_Id, Is_Low_Bound => True);
13747 Force_Evaluation
13748 (High_Bound (R), Related_Id => Def_Id, Is_High_Bound => True);
13749 end if;
13750
13751 elsif Nkind (S) = N_Discriminant_Association then
13752
13753 -- Syntactically valid in subtype indication
13754
13755 Error_Msg_N ("invalid index constraint", S);
13756 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
13757 return;
13758
13759 -- Subtype_Mark case, no anonymous subtypes to construct
13760
13761 else
13762 Analyze (S);
13763
13764 if Is_Entity_Name (S) then
13765 if not Is_Type (Entity (S)) then
13766 Error_Msg_N ("expect subtype mark for index constraint", S);
13767
13768 elsif Base_Type (Entity (S)) /= Base_Type (T) then
13769 Wrong_Type (S, Base_Type (T));
13770
13771 -- Check error of subtype with predicate in index constraint
13772
13773 else
13774 Bad_Predicated_Subtype_Use
13775 ("subtype& has predicate, not allowed in index constraint",
13776 S, Entity (S));
13777 end if;
13778
13779 return;
13780
13781 else
13782 Error_Msg_N ("invalid index constraint", S);
13783 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
13784 return;
13785 end if;
13786 end if;
13787
13788 -- Complete construction of the Itype
13789
13790 if Is_Modular_Integer_Type (T) then
13791 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
13792
13793 elsif Is_Integer_Type (T) then
13794 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
13795
13796 else
13797 Set_Ekind (Def_Id, E_Enumeration_Subtype);
13798 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
13799 Set_First_Literal (Def_Id, First_Literal (T));
13800 end if;
13801
13802 Set_Size_Info (Def_Id, (T));
13803 Set_RM_Size (Def_Id, RM_Size (T));
13804 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13805
13806 Set_Scalar_Range (Def_Id, R);
13807
13808 Set_Etype (S, Def_Id);
13809 Set_Discrete_RM_Size (Def_Id);
13810 end Constrain_Index;
13811
13812 -----------------------
13813 -- Constrain_Integer --
13814 -----------------------
13815
13816 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id) is
13817 T : constant Entity_Id := Entity (Subtype_Mark (S));
13818 C : constant Node_Id := Constraint (S);
13819
13820 begin
13821 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13822
13823 if Is_Modular_Integer_Type (T) then
13824 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
13825 else
13826 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
13827 end if;
13828
13829 Set_Etype (Def_Id, Base_Type (T));
13830 Set_Size_Info (Def_Id, (T));
13831 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13832 Set_Discrete_RM_Size (Def_Id);
13833 end Constrain_Integer;
13834
13835 ------------------------------
13836 -- Constrain_Ordinary_Fixed --
13837 ------------------------------
13838
13839 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id) is
13840 T : constant Entity_Id := Entity (Subtype_Mark (S));
13841 C : Node_Id;
13842 D : Node_Id;
13843 Rais : Node_Id;
13844
13845 begin
13846 Set_Ekind (Def_Id, E_Ordinary_Fixed_Point_Subtype);
13847 Set_Etype (Def_Id, Base_Type (T));
13848 Set_Size_Info (Def_Id, (T));
13849 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13850 Set_Small_Value (Def_Id, Small_Value (T));
13851
13852 -- Process the constraint
13853
13854 C := Constraint (S);
13855
13856 -- Delta constraint present
13857
13858 if Nkind (C) = N_Delta_Constraint then
13859
13860 Check_SPARK_05_Restriction ("delta constraint is not allowed", S);
13861 Check_Restriction (No_Obsolescent_Features, C);
13862
13863 if Warn_On_Obsolescent_Feature then
13864 Error_Msg_S
13865 ("subtype delta constraint is an " &
13866 "obsolescent feature (RM J.3(7))?j?");
13867 end if;
13868
13869 D := Delta_Expression (C);
13870 Analyze_And_Resolve (D, Any_Real);
13871 Check_Delta_Expression (D);
13872 Set_Delta_Value (Def_Id, Expr_Value_R (D));
13873
13874 -- Check that delta value is in range. Obviously we can do this
13875 -- at compile time, but it is strictly a runtime check, and of
13876 -- course there is an ACVC test that checks this.
13877
13878 if Delta_Value (Def_Id) < Delta_Value (T) then
13879 Error_Msg_N ("??delta value is too small", D);
13880 Rais :=
13881 Make_Raise_Constraint_Error (Sloc (D),
13882 Reason => CE_Range_Check_Failed);
13883 Insert_Action (Declaration_Node (Def_Id), Rais);
13884 end if;
13885
13886 C := Range_Constraint (C);
13887
13888 -- No delta constraint present
13889
13890 else
13891 Set_Delta_Value (Def_Id, Delta_Value (T));
13892 end if;
13893
13894 -- Range constraint present
13895
13896 if Nkind (C) = N_Range_Constraint then
13897 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13898
13899 -- No range constraint present
13900
13901 else
13902 pragma Assert (No (C));
13903 Set_Scalar_Range (Def_Id, Scalar_Range (T));
13904 end if;
13905
13906 Set_Discrete_RM_Size (Def_Id);
13907
13908 -- Unconditionally delay the freeze, since we cannot set size
13909 -- information in all cases correctly until the freeze point.
13910
13911 Set_Has_Delayed_Freeze (Def_Id);
13912 end Constrain_Ordinary_Fixed;
13913
13914 -----------------------
13915 -- Contain_Interface --
13916 -----------------------
13917
13918 function Contain_Interface
13919 (Iface : Entity_Id;
13920 Ifaces : Elist_Id) return Boolean
13921 is
13922 Iface_Elmt : Elmt_Id;
13923
13924 begin
13925 if Present (Ifaces) then
13926 Iface_Elmt := First_Elmt (Ifaces);
13927 while Present (Iface_Elmt) loop
13928 if Node (Iface_Elmt) = Iface then
13929 return True;
13930 end if;
13931
13932 Next_Elmt (Iface_Elmt);
13933 end loop;
13934 end if;
13935
13936 return False;
13937 end Contain_Interface;
13938
13939 ---------------------------
13940 -- Convert_Scalar_Bounds --
13941 ---------------------------
13942
13943 procedure Convert_Scalar_Bounds
13944 (N : Node_Id;
13945 Parent_Type : Entity_Id;
13946 Derived_Type : Entity_Id;
13947 Loc : Source_Ptr)
13948 is
13949 Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
13950
13951 Lo : Node_Id;
13952 Hi : Node_Id;
13953 Rng : Node_Id;
13954
13955 begin
13956 -- Defend against previous errors
13957
13958 if No (Scalar_Range (Derived_Type)) then
13959 Check_Error_Detected;
13960 return;
13961 end if;
13962
13963 Lo := Build_Scalar_Bound
13964 (Type_Low_Bound (Derived_Type),
13965 Parent_Type, Implicit_Base);
13966
13967 Hi := Build_Scalar_Bound
13968 (Type_High_Bound (Derived_Type),
13969 Parent_Type, Implicit_Base);
13970
13971 Rng :=
13972 Make_Range (Loc,
13973 Low_Bound => Lo,
13974 High_Bound => Hi);
13975
13976 Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
13977
13978 Set_Parent (Rng, N);
13979 Set_Scalar_Range (Derived_Type, Rng);
13980
13981 -- Analyze the bounds
13982
13983 Analyze_And_Resolve (Lo, Implicit_Base);
13984 Analyze_And_Resolve (Hi, Implicit_Base);
13985
13986 -- Analyze the range itself, except that we do not analyze it if
13987 -- the bounds are real literals, and we have a fixed-point type.
13988 -- The reason for this is that we delay setting the bounds in this
13989 -- case till we know the final Small and Size values (see circuit
13990 -- in Freeze.Freeze_Fixed_Point_Type for further details).
13991
13992 if Is_Fixed_Point_Type (Parent_Type)
13993 and then Nkind (Lo) = N_Real_Literal
13994 and then Nkind (Hi) = N_Real_Literal
13995 then
13996 return;
13997
13998 -- Here we do the analysis of the range
13999
14000 -- Note: we do this manually, since if we do a normal Analyze and
14001 -- Resolve call, there are problems with the conversions used for
14002 -- the derived type range.
14003
14004 else
14005 Set_Etype (Rng, Implicit_Base);
14006 Set_Analyzed (Rng, True);
14007 end if;
14008 end Convert_Scalar_Bounds;
14009
14010 -------------------
14011 -- Copy_And_Swap --
14012 -------------------
14013
14014 procedure Copy_And_Swap (Priv, Full : Entity_Id) is
14015 begin
14016 -- Initialize new full declaration entity by copying the pertinent
14017 -- fields of the corresponding private declaration entity.
14018
14019 -- We temporarily set Ekind to a value appropriate for a type to
14020 -- avoid assert failures in Einfo from checking for setting type
14021 -- attributes on something that is not a type. Ekind (Priv) is an
14022 -- appropriate choice, since it allowed the attributes to be set
14023 -- in the first place. This Ekind value will be modified later.
14024
14025 Set_Ekind (Full, Ekind (Priv));
14026
14027 -- Also set Etype temporarily to Any_Type, again, in the absence
14028 -- of errors, it will be properly reset, and if there are errors,
14029 -- then we want a value of Any_Type to remain.
14030
14031 Set_Etype (Full, Any_Type);
14032
14033 -- Now start copying attributes
14034
14035 Set_Has_Discriminants (Full, Has_Discriminants (Priv));
14036
14037 if Has_Discriminants (Full) then
14038 Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
14039 Set_Stored_Constraint (Full, Stored_Constraint (Priv));
14040 end if;
14041
14042 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
14043 Set_Homonym (Full, Homonym (Priv));
14044 Set_Is_Immediately_Visible (Full, Is_Immediately_Visible (Priv));
14045 Set_Is_Public (Full, Is_Public (Priv));
14046 Set_Is_Pure (Full, Is_Pure (Priv));
14047 Set_Is_Tagged_Type (Full, Is_Tagged_Type (Priv));
14048 Set_Has_Pragma_Unmodified (Full, Has_Pragma_Unmodified (Priv));
14049 Set_Has_Pragma_Unreferenced (Full, Has_Pragma_Unreferenced (Priv));
14050 Set_Has_Pragma_Unreferenced_Objects
14051 (Full, Has_Pragma_Unreferenced_Objects
14052 (Priv));
14053
14054 Conditional_Delay (Full, Priv);
14055
14056 if Is_Tagged_Type (Full) then
14057 Set_Direct_Primitive_Operations
14058 (Full, Direct_Primitive_Operations (Priv));
14059 Set_No_Tagged_Streams_Pragma
14060 (Full, No_Tagged_Streams_Pragma (Priv));
14061
14062 if Is_Base_Type (Priv) then
14063 Set_Class_Wide_Type (Full, Class_Wide_Type (Priv));
14064 end if;
14065 end if;
14066
14067 Set_Is_Volatile (Full, Is_Volatile (Priv));
14068 Set_Treat_As_Volatile (Full, Treat_As_Volatile (Priv));
14069 Set_Scope (Full, Scope (Priv));
14070 Set_Next_Entity (Full, Next_Entity (Priv));
14071 Set_First_Entity (Full, First_Entity (Priv));
14072 Set_Last_Entity (Full, Last_Entity (Priv));
14073
14074 -- If access types have been recorded for later handling, keep them in
14075 -- the full view so that they get handled when the full view freeze
14076 -- node is expanded.
14077
14078 if Present (Freeze_Node (Priv))
14079 and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
14080 then
14081 Ensure_Freeze_Node (Full);
14082 Set_Access_Types_To_Process
14083 (Freeze_Node (Full),
14084 Access_Types_To_Process (Freeze_Node (Priv)));
14085 end if;
14086
14087 -- Swap the two entities. Now Private is the full type entity and Full
14088 -- is the private one. They will be swapped back at the end of the
14089 -- private part. This swapping ensures that the entity that is visible
14090 -- in the private part is the full declaration.
14091
14092 Exchange_Entities (Priv, Full);
14093 Append_Entity (Full, Scope (Full));
14094 end Copy_And_Swap;
14095
14096 -------------------------------------
14097 -- Copy_Array_Base_Type_Attributes --
14098 -------------------------------------
14099
14100 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
14101 begin
14102 Set_Component_Alignment (T1, Component_Alignment (T2));
14103 Set_Component_Type (T1, Component_Type (T2));
14104 Set_Component_Size (T1, Component_Size (T2));
14105 Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
14106 Set_Has_Non_Standard_Rep (T1, Has_Non_Standard_Rep (T2));
14107 Propagate_Concurrent_Flags (T1, T2);
14108 Set_Is_Packed (T1, Is_Packed (T2));
14109 Set_Has_Aliased_Components (T1, Has_Aliased_Components (T2));
14110 Set_Has_Atomic_Components (T1, Has_Atomic_Components (T2));
14111 Set_Has_Volatile_Components (T1, Has_Volatile_Components (T2));
14112 end Copy_Array_Base_Type_Attributes;
14113
14114 -----------------------------------
14115 -- Copy_Array_Subtype_Attributes --
14116 -----------------------------------
14117
14118 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
14119 begin
14120 Set_Size_Info (T1, T2);
14121
14122 Set_First_Index (T1, First_Index (T2));
14123 Set_Is_Aliased (T1, Is_Aliased (T2));
14124 Set_Is_Volatile (T1, Is_Volatile (T2));
14125 Set_Treat_As_Volatile (T1, Treat_As_Volatile (T2));
14126 Set_Is_Constrained (T1, Is_Constrained (T2));
14127 Set_Depends_On_Private (T1, Has_Private_Component (T2));
14128 Inherit_Rep_Item_Chain (T1, T2);
14129 Set_Convention (T1, Convention (T2));
14130 Set_Is_Limited_Composite (T1, Is_Limited_Composite (T2));
14131 Set_Is_Private_Composite (T1, Is_Private_Composite (T2));
14132 Set_Packed_Array_Impl_Type (T1, Packed_Array_Impl_Type (T2));
14133 end Copy_Array_Subtype_Attributes;
14134
14135 -----------------------------------
14136 -- Create_Constrained_Components --
14137 -----------------------------------
14138
14139 procedure Create_Constrained_Components
14140 (Subt : Entity_Id;
14141 Decl_Node : Node_Id;
14142 Typ : Entity_Id;
14143 Constraints : Elist_Id)
14144 is
14145 Loc : constant Source_Ptr := Sloc (Subt);
14146 Comp_List : constant Elist_Id := New_Elmt_List;
14147 Parent_Type : constant Entity_Id := Etype (Typ);
14148 Assoc_List : constant List_Id := New_List;
14149 Discr_Val : Elmt_Id;
14150 Errors : Boolean;
14151 New_C : Entity_Id;
14152 Old_C : Entity_Id;
14153 Is_Static : Boolean := True;
14154
14155 procedure Collect_Fixed_Components (Typ : Entity_Id);
14156 -- Collect parent type components that do not appear in a variant part
14157
14158 procedure Create_All_Components;
14159 -- Iterate over Comp_List to create the components of the subtype
14160
14161 function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
14162 -- Creates a new component from Old_Compon, copying all the fields from
14163 -- it, including its Etype, inserts the new component in the Subt entity
14164 -- chain and returns the new component.
14165
14166 function Is_Variant_Record (T : Entity_Id) return Boolean;
14167 -- If true, and discriminants are static, collect only components from
14168 -- variants selected by discriminant values.
14169
14170 ------------------------------
14171 -- Collect_Fixed_Components --
14172 ------------------------------
14173
14174 procedure Collect_Fixed_Components (Typ : Entity_Id) is
14175 begin
14176 -- Build association list for discriminants, and find components of the
14177 -- variant part selected by the values of the discriminants.
14178
14179 Old_C := First_Discriminant (Typ);
14180 Discr_Val := First_Elmt (Constraints);
14181 while Present (Old_C) loop
14182 Append_To (Assoc_List,
14183 Make_Component_Association (Loc,
14184 Choices => New_List (New_Occurrence_Of (Old_C, Loc)),
14185 Expression => New_Copy (Node (Discr_Val))));
14186
14187 Next_Elmt (Discr_Val);
14188 Next_Discriminant (Old_C);
14189 end loop;
14190
14191 -- The tag and the possible parent component are unconditionally in
14192 -- the subtype.
14193
14194 if Is_Tagged_Type (Typ) or else Has_Controlled_Component (Typ) then
14195 Old_C := First_Component (Typ);
14196 while Present (Old_C) loop
14197 if Nam_In (Chars (Old_C), Name_uTag, Name_uParent) then
14198 Append_Elmt (Old_C, Comp_List);
14199 end if;
14200
14201 Next_Component (Old_C);
14202 end loop;
14203 end if;
14204 end Collect_Fixed_Components;
14205
14206 ---------------------------
14207 -- Create_All_Components --
14208 ---------------------------
14209
14210 procedure Create_All_Components is
14211 Comp : Elmt_Id;
14212
14213 begin
14214 Comp := First_Elmt (Comp_List);
14215 while Present (Comp) loop
14216 Old_C := Node (Comp);
14217 New_C := Create_Component (Old_C);
14218
14219 Set_Etype
14220 (New_C,
14221 Constrain_Component_Type
14222 (Old_C, Subt, Decl_Node, Typ, Constraints));
14223 Set_Is_Public (New_C, Is_Public (Subt));
14224
14225 Next_Elmt (Comp);
14226 end loop;
14227 end Create_All_Components;
14228
14229 ----------------------
14230 -- Create_Component --
14231 ----------------------
14232
14233 function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
14234 New_Compon : constant Entity_Id := New_Copy (Old_Compon);
14235
14236 begin
14237 if Ekind (Old_Compon) = E_Discriminant
14238 and then Is_Completely_Hidden (Old_Compon)
14239 then
14240 -- This is a shadow discriminant created for a discriminant of
14241 -- the parent type, which needs to be present in the subtype.
14242 -- Give the shadow discriminant an internal name that cannot
14243 -- conflict with that of visible components.
14244
14245 Set_Chars (New_Compon, New_Internal_Name ('C'));
14246 end if;
14247
14248 -- Set the parent so we have a proper link for freezing etc. This is
14249 -- not a real parent pointer, since of course our parent does not own
14250 -- up to us and reference us, we are an illegitimate child of the
14251 -- original parent.
14252
14253 Set_Parent (New_Compon, Parent (Old_Compon));
14254
14255 -- If the old component's Esize was already determined and is a
14256 -- static value, then the new component simply inherits it. Otherwise
14257 -- the old component's size may require run-time determination, but
14258 -- the new component's size still might be statically determinable
14259 -- (if, for example it has a static constraint). In that case we want
14260 -- Layout_Type to recompute the component's size, so we reset its
14261 -- size and positional fields.
14262
14263 if Frontend_Layout_On_Target
14264 and then not Known_Static_Esize (Old_Compon)
14265 then
14266 Set_Esize (New_Compon, Uint_0);
14267 Init_Normalized_First_Bit (New_Compon);
14268 Init_Normalized_Position (New_Compon);
14269 Init_Normalized_Position_Max (New_Compon);
14270 end if;
14271
14272 -- We do not want this node marked as Comes_From_Source, since
14273 -- otherwise it would get first class status and a separate cross-
14274 -- reference line would be generated. Illegitimate children do not
14275 -- rate such recognition.
14276
14277 Set_Comes_From_Source (New_Compon, False);
14278
14279 -- But it is a real entity, and a birth certificate must be properly
14280 -- registered by entering it into the entity list.
14281
14282 Enter_Name (New_Compon);
14283
14284 return New_Compon;
14285 end Create_Component;
14286
14287 -----------------------
14288 -- Is_Variant_Record --
14289 -----------------------
14290
14291 function Is_Variant_Record (T : Entity_Id) return Boolean is
14292 begin
14293 return Nkind (Parent (T)) = N_Full_Type_Declaration
14294 and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
14295 and then Present (Component_List (Type_Definition (Parent (T))))
14296 and then
14297 Present
14298 (Variant_Part (Component_List (Type_Definition (Parent (T)))));
14299 end Is_Variant_Record;
14300
14301 -- Start of processing for Create_Constrained_Components
14302
14303 begin
14304 pragma Assert (Subt /= Base_Type (Subt));
14305 pragma Assert (Typ = Base_Type (Typ));
14306
14307 Set_First_Entity (Subt, Empty);
14308 Set_Last_Entity (Subt, Empty);
14309
14310 -- Check whether constraint is fully static, in which case we can
14311 -- optimize the list of components.
14312
14313 Discr_Val := First_Elmt (Constraints);
14314 while Present (Discr_Val) loop
14315 if not Is_OK_Static_Expression (Node (Discr_Val)) then
14316 Is_Static := False;
14317 exit;
14318 end if;
14319
14320 Next_Elmt (Discr_Val);
14321 end loop;
14322
14323 Set_Has_Static_Discriminants (Subt, Is_Static);
14324
14325 Push_Scope (Subt);
14326
14327 -- Inherit the discriminants of the parent type
14328
14329 Add_Discriminants : declare
14330 Num_Disc : Nat;
14331 Num_Gird : Nat;
14332
14333 begin
14334 Num_Disc := 0;
14335 Old_C := First_Discriminant (Typ);
14336
14337 while Present (Old_C) loop
14338 Num_Disc := Num_Disc + 1;
14339 New_C := Create_Component (Old_C);
14340 Set_Is_Public (New_C, Is_Public (Subt));
14341 Next_Discriminant (Old_C);
14342 end loop;
14343
14344 -- For an untagged derived subtype, the number of discriminants may
14345 -- be smaller than the number of inherited discriminants, because
14346 -- several of them may be renamed by a single new discriminant or
14347 -- constrained. In this case, add the hidden discriminants back into
14348 -- the subtype, because they need to be present if the optimizer of
14349 -- the GCC 4.x back-end decides to break apart assignments between
14350 -- objects using the parent view into member-wise assignments.
14351
14352 Num_Gird := 0;
14353
14354 if Is_Derived_Type (Typ)
14355 and then not Is_Tagged_Type (Typ)
14356 then
14357 Old_C := First_Stored_Discriminant (Typ);
14358
14359 while Present (Old_C) loop
14360 Num_Gird := Num_Gird + 1;
14361 Next_Stored_Discriminant (Old_C);
14362 end loop;
14363 end if;
14364
14365 if Num_Gird > Num_Disc then
14366
14367 -- Find out multiple uses of new discriminants, and add hidden
14368 -- components for the extra renamed discriminants. We recognize
14369 -- multiple uses through the Corresponding_Discriminant of a
14370 -- new discriminant: if it constrains several old discriminants,
14371 -- this field points to the last one in the parent type. The
14372 -- stored discriminants of the derived type have the same name
14373 -- as those of the parent.
14374
14375 declare
14376 Constr : Elmt_Id;
14377 New_Discr : Entity_Id;
14378 Old_Discr : Entity_Id;
14379
14380 begin
14381 Constr := First_Elmt (Stored_Constraint (Typ));
14382 Old_Discr := First_Stored_Discriminant (Typ);
14383 while Present (Constr) loop
14384 if Is_Entity_Name (Node (Constr))
14385 and then Ekind (Entity (Node (Constr))) = E_Discriminant
14386 then
14387 New_Discr := Entity (Node (Constr));
14388
14389 if Chars (Corresponding_Discriminant (New_Discr)) /=
14390 Chars (Old_Discr)
14391 then
14392 -- The new discriminant has been used to rename a
14393 -- subsequent old discriminant. Introduce a shadow
14394 -- component for the current old discriminant.
14395
14396 New_C := Create_Component (Old_Discr);
14397 Set_Original_Record_Component (New_C, Old_Discr);
14398 end if;
14399
14400 else
14401 -- The constraint has eliminated the old discriminant.
14402 -- Introduce a shadow component.
14403
14404 New_C := Create_Component (Old_Discr);
14405 Set_Original_Record_Component (New_C, Old_Discr);
14406 end if;
14407
14408 Next_Elmt (Constr);
14409 Next_Stored_Discriminant (Old_Discr);
14410 end loop;
14411 end;
14412 end if;
14413 end Add_Discriminants;
14414
14415 if Is_Static
14416 and then Is_Variant_Record (Typ)
14417 then
14418 Collect_Fixed_Components (Typ);
14419
14420 Gather_Components (
14421 Typ,
14422 Component_List (Type_Definition (Parent (Typ))),
14423 Governed_By => Assoc_List,
14424 Into => Comp_List,
14425 Report_Errors => Errors);
14426 pragma Assert (not Errors
14427 or else Serious_Errors_Detected > 0);
14428
14429 Create_All_Components;
14430
14431 -- If the subtype declaration is created for a tagged type derivation
14432 -- with constraints, we retrieve the record definition of the parent
14433 -- type to select the components of the proper variant.
14434
14435 elsif Is_Static
14436 and then Is_Tagged_Type (Typ)
14437 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
14438 and then
14439 Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
14440 and then Is_Variant_Record (Parent_Type)
14441 then
14442 Collect_Fixed_Components (Typ);
14443
14444 Gather_Components
14445 (Typ,
14446 Component_List (Type_Definition (Parent (Parent_Type))),
14447 Governed_By => Assoc_List,
14448 Into => Comp_List,
14449 Report_Errors => Errors);
14450
14451 -- Note: previously there was a check at this point that no errors
14452 -- were detected. As a consequence of AI05-220 there may be an error
14453 -- if an inherited discriminant that controls a variant has a non-
14454 -- static constraint.
14455
14456 -- If the tagged derivation has a type extension, collect all the
14457 -- new components therein.
14458
14459 if Present (Record_Extension_Part (Type_Definition (Parent (Typ))))
14460 then
14461 Old_C := First_Component (Typ);
14462 while Present (Old_C) loop
14463 if Original_Record_Component (Old_C) = Old_C
14464 and then Chars (Old_C) /= Name_uTag
14465 and then Chars (Old_C) /= Name_uParent
14466 then
14467 Append_Elmt (Old_C, Comp_List);
14468 end if;
14469
14470 Next_Component (Old_C);
14471 end loop;
14472 end if;
14473
14474 Create_All_Components;
14475
14476 else
14477 -- If discriminants are not static, or if this is a multi-level type
14478 -- extension, we have to include all components of the parent type.
14479
14480 Old_C := First_Component (Typ);
14481 while Present (Old_C) loop
14482 New_C := Create_Component (Old_C);
14483
14484 Set_Etype
14485 (New_C,
14486 Constrain_Component_Type
14487 (Old_C, Subt, Decl_Node, Typ, Constraints));
14488 Set_Is_Public (New_C, Is_Public (Subt));
14489
14490 Next_Component (Old_C);
14491 end loop;
14492 end if;
14493
14494 End_Scope;
14495 end Create_Constrained_Components;
14496
14497 ------------------------------------------
14498 -- Decimal_Fixed_Point_Type_Declaration --
14499 ------------------------------------------
14500
14501 procedure Decimal_Fixed_Point_Type_Declaration
14502 (T : Entity_Id;
14503 Def : Node_Id)
14504 is
14505 Loc : constant Source_Ptr := Sloc (Def);
14506 Digs_Expr : constant Node_Id := Digits_Expression (Def);
14507 Delta_Expr : constant Node_Id := Delta_Expression (Def);
14508 Implicit_Base : Entity_Id;
14509 Digs_Val : Uint;
14510 Delta_Val : Ureal;
14511 Scale_Val : Uint;
14512 Bound_Val : Ureal;
14513
14514 begin
14515 Check_SPARK_05_Restriction
14516 ("decimal fixed point type is not allowed", Def);
14517 Check_Restriction (No_Fixed_Point, Def);
14518
14519 -- Create implicit base type
14520
14521 Implicit_Base :=
14522 Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
14523 Set_Etype (Implicit_Base, Implicit_Base);
14524
14525 -- Analyze and process delta expression
14526
14527 Analyze_And_Resolve (Delta_Expr, Universal_Real);
14528
14529 Check_Delta_Expression (Delta_Expr);
14530 Delta_Val := Expr_Value_R (Delta_Expr);
14531
14532 -- Check delta is power of 10, and determine scale value from it
14533
14534 declare
14535 Val : Ureal;
14536
14537 begin
14538 Scale_Val := Uint_0;
14539 Val := Delta_Val;
14540
14541 if Val < Ureal_1 then
14542 while Val < Ureal_1 loop
14543 Val := Val * Ureal_10;
14544 Scale_Val := Scale_Val + 1;
14545 end loop;
14546
14547 if Scale_Val > 18 then
14548 Error_Msg_N ("scale exceeds maximum value of 18", Def);
14549 Scale_Val := UI_From_Int (+18);
14550 end if;
14551
14552 else
14553 while Val > Ureal_1 loop
14554 Val := Val / Ureal_10;
14555 Scale_Val := Scale_Val - 1;
14556 end loop;
14557
14558 if Scale_Val < -18 then
14559 Error_Msg_N ("scale is less than minimum value of -18", Def);
14560 Scale_Val := UI_From_Int (-18);
14561 end if;
14562 end if;
14563
14564 if Val /= Ureal_1 then
14565 Error_Msg_N ("delta expression must be a power of 10", Def);
14566 Delta_Val := Ureal_10 ** (-Scale_Val);
14567 end if;
14568 end;
14569
14570 -- Set delta, scale and small (small = delta for decimal type)
14571
14572 Set_Delta_Value (Implicit_Base, Delta_Val);
14573 Set_Scale_Value (Implicit_Base, Scale_Val);
14574 Set_Small_Value (Implicit_Base, Delta_Val);
14575
14576 -- Analyze and process digits expression
14577
14578 Analyze_And_Resolve (Digs_Expr, Any_Integer);
14579 Check_Digits_Expression (Digs_Expr);
14580 Digs_Val := Expr_Value (Digs_Expr);
14581
14582 if Digs_Val > 18 then
14583 Digs_Val := UI_From_Int (+18);
14584 Error_Msg_N ("digits value out of range, maximum is 18", Digs_Expr);
14585 end if;
14586
14587 Set_Digits_Value (Implicit_Base, Digs_Val);
14588 Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
14589
14590 -- Set range of base type from digits value for now. This will be
14591 -- expanded to represent the true underlying base range by Freeze.
14592
14593 Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
14594
14595 -- Note: We leave size as zero for now, size will be set at freeze
14596 -- time. We have to do this for ordinary fixed-point, because the size
14597 -- depends on the specified small, and we might as well do the same for
14598 -- decimal fixed-point.
14599
14600 pragma Assert (Esize (Implicit_Base) = Uint_0);
14601
14602 -- If there are bounds given in the declaration use them as the
14603 -- bounds of the first named subtype.
14604
14605 if Present (Real_Range_Specification (Def)) then
14606 declare
14607 RRS : constant Node_Id := Real_Range_Specification (Def);
14608 Low : constant Node_Id := Low_Bound (RRS);
14609 High : constant Node_Id := High_Bound (RRS);
14610 Low_Val : Ureal;
14611 High_Val : Ureal;
14612
14613 begin
14614 Analyze_And_Resolve (Low, Any_Real);
14615 Analyze_And_Resolve (High, Any_Real);
14616 Check_Real_Bound (Low);
14617 Check_Real_Bound (High);
14618 Low_Val := Expr_Value_R (Low);
14619 High_Val := Expr_Value_R (High);
14620
14621 if Low_Val < (-Bound_Val) then
14622 Error_Msg_N
14623 ("range low bound too small for digits value", Low);
14624 Low_Val := -Bound_Val;
14625 end if;
14626
14627 if High_Val > Bound_Val then
14628 Error_Msg_N
14629 ("range high bound too large for digits value", High);
14630 High_Val := Bound_Val;
14631 end if;
14632
14633 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
14634 end;
14635
14636 -- If no explicit range, use range that corresponds to given
14637 -- digits value. This will end up as the final range for the
14638 -- first subtype.
14639
14640 else
14641 Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
14642 end if;
14643
14644 -- Complete entity for first subtype. The inheritance of the rep item
14645 -- chain ensures that SPARK-related pragmas are not clobbered when the
14646 -- decimal fixed point type acts as a full view of a private type.
14647
14648 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
14649 Set_Etype (T, Implicit_Base);
14650 Set_Size_Info (T, Implicit_Base);
14651 Inherit_Rep_Item_Chain (T, Implicit_Base);
14652 Set_Digits_Value (T, Digs_Val);
14653 Set_Delta_Value (T, Delta_Val);
14654 Set_Small_Value (T, Delta_Val);
14655 Set_Scale_Value (T, Scale_Val);
14656 Set_Is_Constrained (T);
14657 end Decimal_Fixed_Point_Type_Declaration;
14658
14659 -----------------------------------
14660 -- Derive_Progenitor_Subprograms --
14661 -----------------------------------
14662
14663 procedure Derive_Progenitor_Subprograms
14664 (Parent_Type : Entity_Id;
14665 Tagged_Type : Entity_Id)
14666 is
14667 E : Entity_Id;
14668 Elmt : Elmt_Id;
14669 Iface : Entity_Id;
14670 Iface_Elmt : Elmt_Id;
14671 Iface_Subp : Entity_Id;
14672 New_Subp : Entity_Id := Empty;
14673 Prim_Elmt : Elmt_Id;
14674 Subp : Entity_Id;
14675 Typ : Entity_Id;
14676
14677 begin
14678 pragma Assert (Ada_Version >= Ada_2005
14679 and then Is_Record_Type (Tagged_Type)
14680 and then Is_Tagged_Type (Tagged_Type)
14681 and then Has_Interfaces (Tagged_Type));
14682
14683 -- Step 1: Transfer to the full-view primitives associated with the
14684 -- partial-view that cover interface primitives. Conceptually this
14685 -- work should be done later by Process_Full_View; done here to
14686 -- simplify its implementation at later stages. It can be safely
14687 -- done here because interfaces must be visible in the partial and
14688 -- private view (RM 7.3(7.3/2)).
14689
14690 -- Small optimization: This work is only required if the parent may
14691 -- have entities whose Alias attribute reference an interface primitive.
14692 -- Such a situation may occur if the parent is an abstract type and the
14693 -- primitive has not been yet overridden or if the parent is a generic
14694 -- formal type covering interfaces.
14695
14696 -- If the tagged type is not abstract, it cannot have abstract
14697 -- primitives (the only entities in the list of primitives of
14698 -- non-abstract tagged types that can reference abstract primitives
14699 -- through its Alias attribute are the internal entities that have
14700 -- attribute Interface_Alias, and these entities are generated later
14701 -- by Add_Internal_Interface_Entities).
14702
14703 if In_Private_Part (Current_Scope)
14704 and then (Is_Abstract_Type (Parent_Type)
14705 or else
14706 Is_Generic_Type (Parent_Type))
14707 then
14708 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
14709 while Present (Elmt) loop
14710 Subp := Node (Elmt);
14711
14712 -- At this stage it is not possible to have entities in the list
14713 -- of primitives that have attribute Interface_Alias.
14714
14715 pragma Assert (No (Interface_Alias (Subp)));
14716
14717 Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
14718
14719 if Is_Interface (Typ) then
14720 E := Find_Primitive_Covering_Interface
14721 (Tagged_Type => Tagged_Type,
14722 Iface_Prim => Subp);
14723
14724 if Present (E)
14725 and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
14726 then
14727 Replace_Elmt (Elmt, E);
14728 Remove_Homonym (Subp);
14729 end if;
14730 end if;
14731
14732 Next_Elmt (Elmt);
14733 end loop;
14734 end if;
14735
14736 -- Step 2: Add primitives of progenitors that are not implemented by
14737 -- parents of Tagged_Type.
14738
14739 if Present (Interfaces (Base_Type (Tagged_Type))) then
14740 Iface_Elmt := First_Elmt (Interfaces (Base_Type (Tagged_Type)));
14741 while Present (Iface_Elmt) loop
14742 Iface := Node (Iface_Elmt);
14743
14744 Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
14745 while Present (Prim_Elmt) loop
14746 Iface_Subp := Node (Prim_Elmt);
14747
14748 -- Exclude derivation of predefined primitives except those
14749 -- that come from source, or are inherited from one that comes
14750 -- from source. Required to catch declarations of equality
14751 -- operators of interfaces. For example:
14752
14753 -- type Iface is interface;
14754 -- function "=" (Left, Right : Iface) return Boolean;
14755
14756 if not Is_Predefined_Dispatching_Operation (Iface_Subp)
14757 or else Comes_From_Source (Ultimate_Alias (Iface_Subp))
14758 then
14759 E := Find_Primitive_Covering_Interface
14760 (Tagged_Type => Tagged_Type,
14761 Iface_Prim => Iface_Subp);
14762
14763 -- If not found we derive a new primitive leaving its alias
14764 -- attribute referencing the interface primitive.
14765
14766 if No (E) then
14767 Derive_Subprogram
14768 (New_Subp, Iface_Subp, Tagged_Type, Iface);
14769
14770 -- Ada 2012 (AI05-0197): If the covering primitive's name
14771 -- differs from the name of the interface primitive then it
14772 -- is a private primitive inherited from a parent type. In
14773 -- such case, given that Tagged_Type covers the interface,
14774 -- the inherited private primitive becomes visible. For such
14775 -- purpose we add a new entity that renames the inherited
14776 -- private primitive.
14777
14778 elsif Chars (E) /= Chars (Iface_Subp) then
14779 pragma Assert (Has_Suffix (E, 'P'));
14780 Derive_Subprogram
14781 (New_Subp, Iface_Subp, Tagged_Type, Iface);
14782 Set_Alias (New_Subp, E);
14783 Set_Is_Abstract_Subprogram (New_Subp,
14784 Is_Abstract_Subprogram (E));
14785
14786 -- Propagate to the full view interface entities associated
14787 -- with the partial view.
14788
14789 elsif In_Private_Part (Current_Scope)
14790 and then Present (Alias (E))
14791 and then Alias (E) = Iface_Subp
14792 and then
14793 List_Containing (Parent (E)) /=
14794 Private_Declarations
14795 (Specification
14796 (Unit_Declaration_Node (Current_Scope)))
14797 then
14798 Append_Elmt (E, Primitive_Operations (Tagged_Type));
14799 end if;
14800 end if;
14801
14802 Next_Elmt (Prim_Elmt);
14803 end loop;
14804
14805 Next_Elmt (Iface_Elmt);
14806 end loop;
14807 end if;
14808 end Derive_Progenitor_Subprograms;
14809
14810 -----------------------
14811 -- Derive_Subprogram --
14812 -----------------------
14813
14814 procedure Derive_Subprogram
14815 (New_Subp : out Entity_Id;
14816 Parent_Subp : Entity_Id;
14817 Derived_Type : Entity_Id;
14818 Parent_Type : Entity_Id;
14819 Actual_Subp : Entity_Id := Empty)
14820 is
14821 Formal : Entity_Id;
14822 -- Formal parameter of parent primitive operation
14823
14824 Formal_Of_Actual : Entity_Id;
14825 -- Formal parameter of actual operation, when the derivation is to
14826 -- create a renaming for a primitive operation of an actual in an
14827 -- instantiation.
14828
14829 New_Formal : Entity_Id;
14830 -- Formal of inherited operation
14831
14832 Visible_Subp : Entity_Id := Parent_Subp;
14833
14834 function Is_Private_Overriding return Boolean;
14835 -- If Subp is a private overriding of a visible operation, the inherited
14836 -- operation derives from the overridden op (even though its body is the
14837 -- overriding one) and the inherited operation is visible now. See
14838 -- sem_disp to see the full details of the handling of the overridden
14839 -- subprogram, which is removed from the list of primitive operations of
14840 -- the type. The overridden subprogram is saved locally in Visible_Subp,
14841 -- and used to diagnose abstract operations that need overriding in the
14842 -- derived type.
14843
14844 procedure Replace_Type (Id, New_Id : Entity_Id);
14845 -- When the type is an anonymous access type, create a new access type
14846 -- designating the derived type.
14847
14848 procedure Set_Derived_Name;
14849 -- This procedure sets the appropriate Chars name for New_Subp. This
14850 -- is normally just a copy of the parent name. An exception arises for
14851 -- type support subprograms, where the name is changed to reflect the
14852 -- name of the derived type, e.g. if type foo is derived from type bar,
14853 -- then a procedure barDA is derived with a name fooDA.
14854
14855 ---------------------------
14856 -- Is_Private_Overriding --
14857 ---------------------------
14858
14859 function Is_Private_Overriding return Boolean is
14860 Prev : Entity_Id;
14861
14862 begin
14863 -- If the parent is not a dispatching operation there is no
14864 -- need to investigate overridings
14865
14866 if not Is_Dispatching_Operation (Parent_Subp) then
14867 return False;
14868 end if;
14869
14870 -- The visible operation that is overridden is a homonym of the
14871 -- parent subprogram. We scan the homonym chain to find the one
14872 -- whose alias is the subprogram we are deriving.
14873
14874 Prev := Current_Entity (Parent_Subp);
14875 while Present (Prev) loop
14876 if Ekind (Prev) = Ekind (Parent_Subp)
14877 and then Alias (Prev) = Parent_Subp
14878 and then Scope (Parent_Subp) = Scope (Prev)
14879 and then not Is_Hidden (Prev)
14880 then
14881 Visible_Subp := Prev;
14882 return True;
14883 end if;
14884
14885 Prev := Homonym (Prev);
14886 end loop;
14887
14888 return False;
14889 end Is_Private_Overriding;
14890
14891 ------------------
14892 -- Replace_Type --
14893 ------------------
14894
14895 procedure Replace_Type (Id, New_Id : Entity_Id) is
14896 Id_Type : constant Entity_Id := Etype (Id);
14897 Acc_Type : Entity_Id;
14898 Par : constant Node_Id := Parent (Derived_Type);
14899
14900 begin
14901 -- When the type is an anonymous access type, create a new access
14902 -- type designating the derived type. This itype must be elaborated
14903 -- at the point of the derivation, not on subsequent calls that may
14904 -- be out of the proper scope for Gigi, so we insert a reference to
14905 -- it after the derivation.
14906
14907 if Ekind (Id_Type) = E_Anonymous_Access_Type then
14908 declare
14909 Desig_Typ : Entity_Id := Designated_Type (Id_Type);
14910
14911 begin
14912 if Ekind (Desig_Typ) = E_Record_Type_With_Private
14913 and then Present (Full_View (Desig_Typ))
14914 and then not Is_Private_Type (Parent_Type)
14915 then
14916 Desig_Typ := Full_View (Desig_Typ);
14917 end if;
14918
14919 if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
14920
14921 -- Ada 2005 (AI-251): Handle also derivations of abstract
14922 -- interface primitives.
14923
14924 or else (Is_Interface (Desig_Typ)
14925 and then not Is_Class_Wide_Type (Desig_Typ))
14926 then
14927 Acc_Type := New_Copy (Id_Type);
14928 Set_Etype (Acc_Type, Acc_Type);
14929 Set_Scope (Acc_Type, New_Subp);
14930
14931 -- Set size of anonymous access type. If we have an access
14932 -- to an unconstrained array, this is a fat pointer, so it
14933 -- is sizes at twice addtress size.
14934
14935 if Is_Array_Type (Desig_Typ)
14936 and then not Is_Constrained (Desig_Typ)
14937 then
14938 Init_Size (Acc_Type, 2 * System_Address_Size);
14939
14940 -- Other cases use a thin pointer
14941
14942 else
14943 Init_Size (Acc_Type, System_Address_Size);
14944 end if;
14945
14946 -- Set remaining characterstics of anonymous access type
14947
14948 Init_Alignment (Acc_Type);
14949 Set_Directly_Designated_Type (Acc_Type, Derived_Type);
14950
14951 Set_Etype (New_Id, Acc_Type);
14952 Set_Scope (New_Id, New_Subp);
14953
14954 -- Create a reference to it
14955
14956 Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
14957
14958 else
14959 Set_Etype (New_Id, Id_Type);
14960 end if;
14961 end;
14962
14963 -- In Ada2012, a formal may have an incomplete type but the type
14964 -- derivation that inherits the primitive follows the full view.
14965
14966 elsif Base_Type (Id_Type) = Base_Type (Parent_Type)
14967 or else
14968 (Ekind (Id_Type) = E_Record_Type_With_Private
14969 and then Present (Full_View (Id_Type))
14970 and then
14971 Base_Type (Full_View (Id_Type)) = Base_Type (Parent_Type))
14972 or else
14973 (Ada_Version >= Ada_2012
14974 and then Ekind (Id_Type) = E_Incomplete_Type
14975 and then Full_View (Id_Type) = Parent_Type)
14976 then
14977 -- Constraint checks on formals are generated during expansion,
14978 -- based on the signature of the original subprogram. The bounds
14979 -- of the derived type are not relevant, and thus we can use
14980 -- the base type for the formals. However, the return type may be
14981 -- used in a context that requires that the proper static bounds
14982 -- be used (a case statement, for example) and for those cases
14983 -- we must use the derived type (first subtype), not its base.
14984
14985 -- If the derived_type_definition has no constraints, we know that
14986 -- the derived type has the same constraints as the first subtype
14987 -- of the parent, and we can also use it rather than its base,
14988 -- which can lead to more efficient code.
14989
14990 if Etype (Id) = Parent_Type then
14991 if Is_Scalar_Type (Parent_Type)
14992 and then
14993 Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
14994 then
14995 Set_Etype (New_Id, Derived_Type);
14996
14997 elsif Nkind (Par) = N_Full_Type_Declaration
14998 and then
14999 Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
15000 and then
15001 Is_Entity_Name
15002 (Subtype_Indication (Type_Definition (Par)))
15003 then
15004 Set_Etype (New_Id, Derived_Type);
15005
15006 else
15007 Set_Etype (New_Id, Base_Type (Derived_Type));
15008 end if;
15009
15010 else
15011 Set_Etype (New_Id, Base_Type (Derived_Type));
15012 end if;
15013
15014 else
15015 Set_Etype (New_Id, Etype (Id));
15016 end if;
15017 end Replace_Type;
15018
15019 ----------------------
15020 -- Set_Derived_Name --
15021 ----------------------
15022
15023 procedure Set_Derived_Name is
15024 Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
15025 begin
15026 if Nm = TSS_Null then
15027 Set_Chars (New_Subp, Chars (Parent_Subp));
15028 else
15029 Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
15030 end if;
15031 end Set_Derived_Name;
15032
15033 -- Start of processing for Derive_Subprogram
15034
15035 begin
15036 New_Subp := New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
15037 Set_Ekind (New_Subp, Ekind (Parent_Subp));
15038
15039 -- Check whether the inherited subprogram is a private operation that
15040 -- should be inherited but not yet made visible. Such subprograms can
15041 -- become visible at a later point (e.g., the private part of a public
15042 -- child unit) via Declare_Inherited_Private_Subprograms. If the
15043 -- following predicate is true, then this is not such a private
15044 -- operation and the subprogram simply inherits the name of the parent
15045 -- subprogram. Note the special check for the names of controlled
15046 -- operations, which are currently exempted from being inherited with
15047 -- a hidden name because they must be findable for generation of
15048 -- implicit run-time calls.
15049
15050 if not Is_Hidden (Parent_Subp)
15051 or else Is_Internal (Parent_Subp)
15052 or else Is_Private_Overriding
15053 or else Is_Internal_Name (Chars (Parent_Subp))
15054 or else (Is_Controlled (Parent_Type)
15055 and then Nam_In (Chars (Parent_Subp), Name_Adjust,
15056 Name_Finalize,
15057 Name_Initialize))
15058 then
15059 Set_Derived_Name;
15060
15061 -- An inherited dispatching equality will be overridden by an internally
15062 -- generated one, or by an explicit one, so preserve its name and thus
15063 -- its entry in the dispatch table. Otherwise, if Parent_Subp is a
15064 -- private operation it may become invisible if the full view has
15065 -- progenitors, and the dispatch table will be malformed.
15066 -- We check that the type is limited to handle the anomalous declaration
15067 -- of Limited_Controlled, which is derived from a non-limited type, and
15068 -- which is handled specially elsewhere as well.
15069
15070 elsif Chars (Parent_Subp) = Name_Op_Eq
15071 and then Is_Dispatching_Operation (Parent_Subp)
15072 and then Etype (Parent_Subp) = Standard_Boolean
15073 and then not Is_Limited_Type (Etype (First_Formal (Parent_Subp)))
15074 and then
15075 Etype (First_Formal (Parent_Subp)) =
15076 Etype (Next_Formal (First_Formal (Parent_Subp)))
15077 then
15078 Set_Derived_Name;
15079
15080 -- If parent is hidden, this can be a regular derivation if the
15081 -- parent is immediately visible in a non-instantiating context,
15082 -- or if we are in the private part of an instance. This test
15083 -- should still be refined ???
15084
15085 -- The test for In_Instance_Not_Visible avoids inheriting the derived
15086 -- operation as a non-visible operation in cases where the parent
15087 -- subprogram might not be visible now, but was visible within the
15088 -- original generic, so it would be wrong to make the inherited
15089 -- subprogram non-visible now. (Not clear if this test is fully
15090 -- correct; are there any cases where we should declare the inherited
15091 -- operation as not visible to avoid it being overridden, e.g., when
15092 -- the parent type is a generic actual with private primitives ???)
15093
15094 -- (they should be treated the same as other private inherited
15095 -- subprograms, but it's not clear how to do this cleanly). ???
15096
15097 elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
15098 and then Is_Immediately_Visible (Parent_Subp)
15099 and then not In_Instance)
15100 or else In_Instance_Not_Visible
15101 then
15102 Set_Derived_Name;
15103
15104 -- Ada 2005 (AI-251): Regular derivation if the parent subprogram
15105 -- overrides an interface primitive because interface primitives
15106 -- must be visible in the partial view of the parent (RM 7.3 (7.3/2))
15107
15108 elsif Ada_Version >= Ada_2005
15109 and then Is_Dispatching_Operation (Parent_Subp)
15110 and then Present (Covered_Interface_Op (Parent_Subp))
15111 then
15112 Set_Derived_Name;
15113
15114 -- Otherwise, the type is inheriting a private operation, so enter it
15115 -- with a special name so it can't be overridden.
15116
15117 else
15118 Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
15119 end if;
15120
15121 Set_Parent (New_Subp, Parent (Derived_Type));
15122
15123 if Present (Actual_Subp) then
15124 Replace_Type (Actual_Subp, New_Subp);
15125 else
15126 Replace_Type (Parent_Subp, New_Subp);
15127 end if;
15128
15129 Conditional_Delay (New_Subp, Parent_Subp);
15130
15131 -- If we are creating a renaming for a primitive operation of an
15132 -- actual of a generic derived type, we must examine the signature
15133 -- of the actual primitive, not that of the generic formal, which for
15134 -- example may be an interface. However the name and initial value
15135 -- of the inherited operation are those of the formal primitive.
15136
15137 Formal := First_Formal (Parent_Subp);
15138
15139 if Present (Actual_Subp) then
15140 Formal_Of_Actual := First_Formal (Actual_Subp);
15141 else
15142 Formal_Of_Actual := Empty;
15143 end if;
15144
15145 while Present (Formal) loop
15146 New_Formal := New_Copy (Formal);
15147
15148 -- Normally we do not go copying parents, but in the case of
15149 -- formals, we need to link up to the declaration (which is the
15150 -- parameter specification), and it is fine to link up to the
15151 -- original formal's parameter specification in this case.
15152
15153 Set_Parent (New_Formal, Parent (Formal));
15154 Append_Entity (New_Formal, New_Subp);
15155
15156 if Present (Formal_Of_Actual) then
15157 Replace_Type (Formal_Of_Actual, New_Formal);
15158 Next_Formal (Formal_Of_Actual);
15159 else
15160 Replace_Type (Formal, New_Formal);
15161 end if;
15162
15163 Next_Formal (Formal);
15164 end loop;
15165
15166 -- If this derivation corresponds to a tagged generic actual, then
15167 -- primitive operations rename those of the actual. Otherwise the
15168 -- primitive operations rename those of the parent type, If the parent
15169 -- renames an intrinsic operator, so does the new subprogram. We except
15170 -- concatenation, which is always properly typed, and does not get
15171 -- expanded as other intrinsic operations.
15172
15173 if No (Actual_Subp) then
15174 if Is_Intrinsic_Subprogram (Parent_Subp) then
15175 Set_Is_Intrinsic_Subprogram (New_Subp);
15176
15177 if Present (Alias (Parent_Subp))
15178 and then Chars (Parent_Subp) /= Name_Op_Concat
15179 then
15180 Set_Alias (New_Subp, Alias (Parent_Subp));
15181 else
15182 Set_Alias (New_Subp, Parent_Subp);
15183 end if;
15184
15185 else
15186 Set_Alias (New_Subp, Parent_Subp);
15187 end if;
15188
15189 else
15190 Set_Alias (New_Subp, Actual_Subp);
15191 end if;
15192
15193 -- Derived subprograms of a tagged type must inherit the convention
15194 -- of the parent subprogram (a requirement of AI-117). Derived
15195 -- subprograms of untagged types simply get convention Ada by default.
15196
15197 -- If the derived type is a tagged generic formal type with unknown
15198 -- discriminants, its convention is intrinsic (RM 6.3.1 (8)).
15199
15200 -- However, if the type is derived from a generic formal, the further
15201 -- inherited subprogram has the convention of the non-generic ancestor.
15202 -- Otherwise there would be no way to override the operation.
15203 -- (This is subject to forthcoming ARG discussions).
15204
15205 if Is_Tagged_Type (Derived_Type) then
15206 if Is_Generic_Type (Derived_Type)
15207 and then Has_Unknown_Discriminants (Derived_Type)
15208 then
15209 Set_Convention (New_Subp, Convention_Intrinsic);
15210
15211 else
15212 if Is_Generic_Type (Parent_Type)
15213 and then Has_Unknown_Discriminants (Parent_Type)
15214 then
15215 Set_Convention (New_Subp, Convention (Alias (Parent_Subp)));
15216 else
15217 Set_Convention (New_Subp, Convention (Parent_Subp));
15218 end if;
15219 end if;
15220 end if;
15221
15222 -- Predefined controlled operations retain their name even if the parent
15223 -- is hidden (see above), but they are not primitive operations if the
15224 -- ancestor is not visible, for example if the parent is a private
15225 -- extension completed with a controlled extension. Note that a full
15226 -- type that is controlled can break privacy: the flag Is_Controlled is
15227 -- set on both views of the type.
15228
15229 if Is_Controlled (Parent_Type)
15230 and then Nam_In (Chars (Parent_Subp), Name_Initialize,
15231 Name_Adjust,
15232 Name_Finalize)
15233 and then Is_Hidden (Parent_Subp)
15234 and then not Is_Visibly_Controlled (Parent_Type)
15235 then
15236 Set_Is_Hidden (New_Subp);
15237 end if;
15238
15239 Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
15240 Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
15241
15242 if Ekind (Parent_Subp) = E_Procedure then
15243 Set_Is_Valued_Procedure
15244 (New_Subp, Is_Valued_Procedure (Parent_Subp));
15245 else
15246 Set_Has_Controlling_Result
15247 (New_Subp, Has_Controlling_Result (Parent_Subp));
15248 end if;
15249
15250 -- No_Return must be inherited properly. If this is overridden in the
15251 -- case of a dispatching operation, then a check is made in Sem_Disp
15252 -- that the overriding operation is also No_Return (no such check is
15253 -- required for the case of non-dispatching operation.
15254
15255 Set_No_Return (New_Subp, No_Return (Parent_Subp));
15256
15257 -- A derived function with a controlling result is abstract. If the
15258 -- Derived_Type is a nonabstract formal generic derived type, then
15259 -- inherited operations are not abstract: the required check is done at
15260 -- instantiation time. If the derivation is for a generic actual, the
15261 -- function is not abstract unless the actual is.
15262
15263 if Is_Generic_Type (Derived_Type)
15264 and then not Is_Abstract_Type (Derived_Type)
15265 then
15266 null;
15267
15268 -- Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
15269 -- properties of the subprogram, as defined in RM-3.9.3(4/2-6/2).
15270
15271 -- A subprogram subject to pragma Extensions_Visible with value False
15272 -- requires overriding if the subprogram has at least one controlling
15273 -- OUT parameter (SPARK RM 6.1.7(6)).
15274
15275 elsif Ada_Version >= Ada_2005
15276 and then (Is_Abstract_Subprogram (Alias (New_Subp))
15277 or else (Is_Tagged_Type (Derived_Type)
15278 and then Etype (New_Subp) = Derived_Type
15279 and then not Is_Null_Extension (Derived_Type))
15280 or else (Is_Tagged_Type (Derived_Type)
15281 and then Ekind (Etype (New_Subp)) =
15282 E_Anonymous_Access_Type
15283 and then Designated_Type (Etype (New_Subp)) =
15284 Derived_Type
15285 and then not Is_Null_Extension (Derived_Type))
15286 or else (Comes_From_Source (Alias (New_Subp))
15287 and then Is_EVF_Procedure (Alias (New_Subp))))
15288 and then No (Actual_Subp)
15289 then
15290 if not Is_Tagged_Type (Derived_Type)
15291 or else Is_Abstract_Type (Derived_Type)
15292 or else Is_Abstract_Subprogram (Alias (New_Subp))
15293 then
15294 Set_Is_Abstract_Subprogram (New_Subp);
15295 else
15296 Set_Requires_Overriding (New_Subp);
15297 end if;
15298
15299 elsif Ada_Version < Ada_2005
15300 and then (Is_Abstract_Subprogram (Alias (New_Subp))
15301 or else (Is_Tagged_Type (Derived_Type)
15302 and then Etype (New_Subp) = Derived_Type
15303 and then No (Actual_Subp)))
15304 then
15305 Set_Is_Abstract_Subprogram (New_Subp);
15306
15307 -- AI05-0097 : an inherited operation that dispatches on result is
15308 -- abstract if the derived type is abstract, even if the parent type
15309 -- is concrete and the derived type is a null extension.
15310
15311 elsif Has_Controlling_Result (Alias (New_Subp))
15312 and then Is_Abstract_Type (Etype (New_Subp))
15313 then
15314 Set_Is_Abstract_Subprogram (New_Subp);
15315
15316 -- Finally, if the parent type is abstract we must verify that all
15317 -- inherited operations are either non-abstract or overridden, or that
15318 -- the derived type itself is abstract (this check is performed at the
15319 -- end of a package declaration, in Check_Abstract_Overriding). A
15320 -- private overriding in the parent type will not be visible in the
15321 -- derivation if we are not in an inner package or in a child unit of
15322 -- the parent type, in which case the abstractness of the inherited
15323 -- operation is carried to the new subprogram.
15324
15325 elsif Is_Abstract_Type (Parent_Type)
15326 and then not In_Open_Scopes (Scope (Parent_Type))
15327 and then Is_Private_Overriding
15328 and then Is_Abstract_Subprogram (Visible_Subp)
15329 then
15330 if No (Actual_Subp) then
15331 Set_Alias (New_Subp, Visible_Subp);
15332 Set_Is_Abstract_Subprogram (New_Subp, True);
15333
15334 else
15335 -- If this is a derivation for an instance of a formal derived
15336 -- type, abstractness comes from the primitive operation of the
15337 -- actual, not from the operation inherited from the ancestor.
15338
15339 Set_Is_Abstract_Subprogram
15340 (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
15341 end if;
15342 end if;
15343
15344 New_Overloaded_Entity (New_Subp, Derived_Type);
15345
15346 -- Ada RM 6.1.1 (15): If a subprogram inherits nonconforming class-wide
15347 -- preconditions and the derived type is abstract, the derived operation
15348 -- is abstract as well if parent subprogram is not abstract or null.
15349
15350 if Is_Abstract_Type (Derived_Type)
15351 and then Has_Non_Trivial_Precondition (Parent_Subp)
15352 and then Present (Interfaces (Derived_Type))
15353 then
15354 Set_Is_Dispatching_Operation (New_Subp);
15355
15356 declare
15357 Iface_Prim : constant Entity_Id := Covered_Interface_Op (New_Subp);
15358
15359 begin
15360 if Present (Iface_Prim)
15361 and then Has_Non_Trivial_Precondition (Iface_Prim)
15362 then
15363 Set_Is_Abstract_Subprogram (New_Subp);
15364 end if;
15365 end;
15366 end if;
15367
15368 -- Check for case of a derived subprogram for the instantiation of a
15369 -- formal derived tagged type, if so mark the subprogram as dispatching
15370 -- and inherit the dispatching attributes of the actual subprogram. The
15371 -- derived subprogram is effectively renaming of the actual subprogram,
15372 -- so it needs to have the same attributes as the actual.
15373
15374 if Present (Actual_Subp)
15375 and then Is_Dispatching_Operation (Actual_Subp)
15376 then
15377 Set_Is_Dispatching_Operation (New_Subp);
15378
15379 if Present (DTC_Entity (Actual_Subp)) then
15380 Set_DTC_Entity (New_Subp, DTC_Entity (Actual_Subp));
15381 Set_DT_Position_Value (New_Subp, DT_Position (Actual_Subp));
15382 end if;
15383 end if;
15384
15385 -- Indicate that a derived subprogram does not require a body and that
15386 -- it does not require processing of default expressions.
15387
15388 Set_Has_Completion (New_Subp);
15389 Set_Default_Expressions_Processed (New_Subp);
15390
15391 if Ekind (New_Subp) = E_Function then
15392 Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
15393 end if;
15394 end Derive_Subprogram;
15395
15396 ------------------------
15397 -- Derive_Subprograms --
15398 ------------------------
15399
15400 procedure Derive_Subprograms
15401 (Parent_Type : Entity_Id;
15402 Derived_Type : Entity_Id;
15403 Generic_Actual : Entity_Id := Empty)
15404 is
15405 Op_List : constant Elist_Id :=
15406 Collect_Primitive_Operations (Parent_Type);
15407
15408 function Check_Derived_Type return Boolean;
15409 -- Check that all the entities derived from Parent_Type are found in
15410 -- the list of primitives of Derived_Type exactly in the same order.
15411
15412 procedure Derive_Interface_Subprogram
15413 (New_Subp : out Entity_Id;
15414 Subp : Entity_Id;
15415 Actual_Subp : Entity_Id);
15416 -- Derive New_Subp from the ultimate alias of the parent subprogram Subp
15417 -- (which is an interface primitive). If Generic_Actual is present then
15418 -- Actual_Subp is the actual subprogram corresponding with the generic
15419 -- subprogram Subp.
15420
15421 ------------------------
15422 -- Check_Derived_Type --
15423 ------------------------
15424
15425 function Check_Derived_Type return Boolean is
15426 E : Entity_Id;
15427 Elmt : Elmt_Id;
15428 List : Elist_Id;
15429 New_Subp : Entity_Id;
15430 Op_Elmt : Elmt_Id;
15431 Subp : Entity_Id;
15432
15433 begin
15434 -- Traverse list of entities in the current scope searching for
15435 -- an incomplete type whose full-view is derived type.
15436
15437 E := First_Entity (Scope (Derived_Type));
15438 while Present (E) and then E /= Derived_Type loop
15439 if Ekind (E) = E_Incomplete_Type
15440 and then Present (Full_View (E))
15441 and then Full_View (E) = Derived_Type
15442 then
15443 -- Disable this test if Derived_Type completes an incomplete
15444 -- type because in such case more primitives can be added
15445 -- later to the list of primitives of Derived_Type by routine
15446 -- Process_Incomplete_Dependents
15447
15448 return True;
15449 end if;
15450
15451 E := Next_Entity (E);
15452 end loop;
15453
15454 List := Collect_Primitive_Operations (Derived_Type);
15455 Elmt := First_Elmt (List);
15456
15457 Op_Elmt := First_Elmt (Op_List);
15458 while Present (Op_Elmt) loop
15459 Subp := Node (Op_Elmt);
15460 New_Subp := Node (Elmt);
15461
15462 -- At this early stage Derived_Type has no entities with attribute
15463 -- Interface_Alias. In addition, such primitives are always
15464 -- located at the end of the list of primitives of Parent_Type.
15465 -- Therefore, if found we can safely stop processing pending
15466 -- entities.
15467
15468 exit when Present (Interface_Alias (Subp));
15469
15470 -- Handle hidden entities
15471
15472 if not Is_Predefined_Dispatching_Operation (Subp)
15473 and then Is_Hidden (Subp)
15474 then
15475 if Present (New_Subp)
15476 and then Primitive_Names_Match (Subp, New_Subp)
15477 then
15478 Next_Elmt (Elmt);
15479 end if;
15480
15481 else
15482 if not Present (New_Subp)
15483 or else Ekind (Subp) /= Ekind (New_Subp)
15484 or else not Primitive_Names_Match (Subp, New_Subp)
15485 then
15486 return False;
15487 end if;
15488
15489 Next_Elmt (Elmt);
15490 end if;
15491
15492 Next_Elmt (Op_Elmt);
15493 end loop;
15494
15495 return True;
15496 end Check_Derived_Type;
15497
15498 ---------------------------------
15499 -- Derive_Interface_Subprogram --
15500 ---------------------------------
15501
15502 procedure Derive_Interface_Subprogram
15503 (New_Subp : out Entity_Id;
15504 Subp : Entity_Id;
15505 Actual_Subp : Entity_Id)
15506 is
15507 Iface_Subp : constant Entity_Id := Ultimate_Alias (Subp);
15508 Iface_Type : constant Entity_Id := Find_Dispatching_Type (Iface_Subp);
15509
15510 begin
15511 pragma Assert (Is_Interface (Iface_Type));
15512
15513 Derive_Subprogram
15514 (New_Subp => New_Subp,
15515 Parent_Subp => Iface_Subp,
15516 Derived_Type => Derived_Type,
15517 Parent_Type => Iface_Type,
15518 Actual_Subp => Actual_Subp);
15519
15520 -- Given that this new interface entity corresponds with a primitive
15521 -- of the parent that was not overridden we must leave it associated
15522 -- with its parent primitive to ensure that it will share the same
15523 -- dispatch table slot when overridden. We must set the Alias to Subp
15524 -- (instead of Iface_Subp), and we must fix Is_Abstract_Subprogram
15525 -- (in case we inherited Subp from Iface_Type via a nonabstract
15526 -- generic formal type).
15527
15528 if No (Actual_Subp) then
15529 Set_Alias (New_Subp, Subp);
15530
15531 declare
15532 T : Entity_Id := Find_Dispatching_Type (Subp);
15533 begin
15534 while Etype (T) /= T loop
15535 if Is_Generic_Type (T) and then not Is_Abstract_Type (T) then
15536 Set_Is_Abstract_Subprogram (New_Subp, False);
15537 exit;
15538 end if;
15539
15540 T := Etype (T);
15541 end loop;
15542 end;
15543
15544 -- For instantiations this is not needed since the previous call to
15545 -- Derive_Subprogram leaves the entity well decorated.
15546
15547 else
15548 pragma Assert (Alias (New_Subp) = Actual_Subp);
15549 null;
15550 end if;
15551 end Derive_Interface_Subprogram;
15552
15553 -- Local variables
15554
15555 Alias_Subp : Entity_Id;
15556 Act_List : Elist_Id;
15557 Act_Elmt : Elmt_Id;
15558 Act_Subp : Entity_Id := Empty;
15559 Elmt : Elmt_Id;
15560 Need_Search : Boolean := False;
15561 New_Subp : Entity_Id := Empty;
15562 Parent_Base : Entity_Id;
15563 Subp : Entity_Id;
15564
15565 -- Start of processing for Derive_Subprograms
15566
15567 begin
15568 if Ekind (Parent_Type) = E_Record_Type_With_Private
15569 and then Has_Discriminants (Parent_Type)
15570 and then Present (Full_View (Parent_Type))
15571 then
15572 Parent_Base := Full_View (Parent_Type);
15573 else
15574 Parent_Base := Parent_Type;
15575 end if;
15576
15577 if Present (Generic_Actual) then
15578 Act_List := Collect_Primitive_Operations (Generic_Actual);
15579 Act_Elmt := First_Elmt (Act_List);
15580 else
15581 Act_List := No_Elist;
15582 Act_Elmt := No_Elmt;
15583 end if;
15584
15585 -- Derive primitives inherited from the parent. Note that if the generic
15586 -- actual is present, this is not really a type derivation, it is a
15587 -- completion within an instance.
15588
15589 -- Case 1: Derived_Type does not implement interfaces
15590
15591 if not Is_Tagged_Type (Derived_Type)
15592 or else (not Has_Interfaces (Derived_Type)
15593 and then not (Present (Generic_Actual)
15594 and then Has_Interfaces (Generic_Actual)))
15595 then
15596 Elmt := First_Elmt (Op_List);
15597 while Present (Elmt) loop
15598 Subp := Node (Elmt);
15599
15600 -- Literals are derived earlier in the process of building the
15601 -- derived type, and are skipped here.
15602
15603 if Ekind (Subp) = E_Enumeration_Literal then
15604 null;
15605
15606 -- The actual is a direct descendant and the common primitive
15607 -- operations appear in the same order.
15608
15609 -- If the generic parent type is present, the derived type is an
15610 -- instance of a formal derived type, and within the instance its
15611 -- operations are those of the actual. We derive from the formal
15612 -- type but make the inherited operations aliases of the
15613 -- corresponding operations of the actual.
15614
15615 else
15616 pragma Assert (No (Node (Act_Elmt))
15617 or else (Primitive_Names_Match (Subp, Node (Act_Elmt))
15618 and then
15619 Type_Conformant
15620 (Subp, Node (Act_Elmt),
15621 Skip_Controlling_Formals => True)));
15622
15623 Derive_Subprogram
15624 (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
15625
15626 if Present (Act_Elmt) then
15627 Next_Elmt (Act_Elmt);
15628 end if;
15629 end if;
15630
15631 Next_Elmt (Elmt);
15632 end loop;
15633
15634 -- Case 2: Derived_Type implements interfaces
15635
15636 else
15637 -- If the parent type has no predefined primitives we remove
15638 -- predefined primitives from the list of primitives of generic
15639 -- actual to simplify the complexity of this algorithm.
15640
15641 if Present (Generic_Actual) then
15642 declare
15643 Has_Predefined_Primitives : Boolean := False;
15644
15645 begin
15646 -- Check if the parent type has predefined primitives
15647
15648 Elmt := First_Elmt (Op_List);
15649 while Present (Elmt) loop
15650 Subp := Node (Elmt);
15651
15652 if Is_Predefined_Dispatching_Operation (Subp)
15653 and then not Comes_From_Source (Ultimate_Alias (Subp))
15654 then
15655 Has_Predefined_Primitives := True;
15656 exit;
15657 end if;
15658
15659 Next_Elmt (Elmt);
15660 end loop;
15661
15662 -- Remove predefined primitives of Generic_Actual. We must use
15663 -- an auxiliary list because in case of tagged types the value
15664 -- returned by Collect_Primitive_Operations is the value stored
15665 -- in its Primitive_Operations attribute (and we don't want to
15666 -- modify its current contents).
15667
15668 if not Has_Predefined_Primitives then
15669 declare
15670 Aux_List : constant Elist_Id := New_Elmt_List;
15671
15672 begin
15673 Elmt := First_Elmt (Act_List);
15674 while Present (Elmt) loop
15675 Subp := Node (Elmt);
15676
15677 if not Is_Predefined_Dispatching_Operation (Subp)
15678 or else Comes_From_Source (Subp)
15679 then
15680 Append_Elmt (Subp, Aux_List);
15681 end if;
15682
15683 Next_Elmt (Elmt);
15684 end loop;
15685
15686 Act_List := Aux_List;
15687 end;
15688 end if;
15689
15690 Act_Elmt := First_Elmt (Act_List);
15691 Act_Subp := Node (Act_Elmt);
15692 end;
15693 end if;
15694
15695 -- Stage 1: If the generic actual is not present we derive the
15696 -- primitives inherited from the parent type. If the generic parent
15697 -- type is present, the derived type is an instance of a formal
15698 -- derived type, and within the instance its operations are those of
15699 -- the actual. We derive from the formal type but make the inherited
15700 -- operations aliases of the corresponding operations of the actual.
15701
15702 Elmt := First_Elmt (Op_List);
15703 while Present (Elmt) loop
15704 Subp := Node (Elmt);
15705 Alias_Subp := Ultimate_Alias (Subp);
15706
15707 -- Do not derive internal entities of the parent that link
15708 -- interface primitives with their covering primitive. These
15709 -- entities will be added to this type when frozen.
15710
15711 if Present (Interface_Alias (Subp)) then
15712 goto Continue;
15713 end if;
15714
15715 -- If the generic actual is present find the corresponding
15716 -- operation in the generic actual. If the parent type is a
15717 -- direct ancestor of the derived type then, even if it is an
15718 -- interface, the operations are inherited from the primary
15719 -- dispatch table and are in the proper order. If we detect here
15720 -- that primitives are not in the same order we traverse the list
15721 -- of primitive operations of the actual to find the one that
15722 -- implements the interface primitive.
15723
15724 if Need_Search
15725 or else
15726 (Present (Generic_Actual)
15727 and then Present (Act_Subp)
15728 and then not
15729 (Primitive_Names_Match (Subp, Act_Subp)
15730 and then
15731 Type_Conformant (Subp, Act_Subp,
15732 Skip_Controlling_Formals => True)))
15733 then
15734 pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual,
15735 Use_Full_View => True));
15736
15737 -- Remember that we need searching for all pending primitives
15738
15739 Need_Search := True;
15740
15741 -- Handle entities associated with interface primitives
15742
15743 if Present (Alias_Subp)
15744 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
15745 and then not Is_Predefined_Dispatching_Operation (Subp)
15746 then
15747 -- Search for the primitive in the homonym chain
15748
15749 Act_Subp :=
15750 Find_Primitive_Covering_Interface
15751 (Tagged_Type => Generic_Actual,
15752 Iface_Prim => Alias_Subp);
15753
15754 -- Previous search may not locate primitives covering
15755 -- interfaces defined in generics units or instantiations.
15756 -- (it fails if the covering primitive has formals whose
15757 -- type is also defined in generics or instantiations).
15758 -- In such case we search in the list of primitives of the
15759 -- generic actual for the internal entity that links the
15760 -- interface primitive and the covering primitive.
15761
15762 if No (Act_Subp)
15763 and then Is_Generic_Type (Parent_Type)
15764 then
15765 -- This code has been designed to handle only generic
15766 -- formals that implement interfaces that are defined
15767 -- in a generic unit or instantiation. If this code is
15768 -- needed for other cases we must review it because
15769 -- (given that it relies on Original_Location to locate
15770 -- the primitive of Generic_Actual that covers the
15771 -- interface) it could leave linked through attribute
15772 -- Alias entities of unrelated instantiations).
15773
15774 pragma Assert
15775 (Is_Generic_Unit
15776 (Scope (Find_Dispatching_Type (Alias_Subp)))
15777 or else
15778 Instantiation_Depth
15779 (Sloc (Find_Dispatching_Type (Alias_Subp))) > 0);
15780
15781 declare
15782 Iface_Prim_Loc : constant Source_Ptr :=
15783 Original_Location (Sloc (Alias_Subp));
15784
15785 Elmt : Elmt_Id;
15786 Prim : Entity_Id;
15787
15788 begin
15789 Elmt :=
15790 First_Elmt (Primitive_Operations (Generic_Actual));
15791
15792 Search : while Present (Elmt) loop
15793 Prim := Node (Elmt);
15794
15795 if Present (Interface_Alias (Prim))
15796 and then Original_Location
15797 (Sloc (Interface_Alias (Prim))) =
15798 Iface_Prim_Loc
15799 then
15800 Act_Subp := Alias (Prim);
15801 exit Search;
15802 end if;
15803
15804 Next_Elmt (Elmt);
15805 end loop Search;
15806 end;
15807 end if;
15808
15809 pragma Assert (Present (Act_Subp)
15810 or else Is_Abstract_Type (Generic_Actual)
15811 or else Serious_Errors_Detected > 0);
15812
15813 -- Handle predefined primitives plus the rest of user-defined
15814 -- primitives
15815
15816 else
15817 Act_Elmt := First_Elmt (Act_List);
15818 while Present (Act_Elmt) loop
15819 Act_Subp := Node (Act_Elmt);
15820
15821 exit when Primitive_Names_Match (Subp, Act_Subp)
15822 and then Type_Conformant
15823 (Subp, Act_Subp,
15824 Skip_Controlling_Formals => True)
15825 and then No (Interface_Alias (Act_Subp));
15826
15827 Next_Elmt (Act_Elmt);
15828 end loop;
15829
15830 if No (Act_Elmt) then
15831 Act_Subp := Empty;
15832 end if;
15833 end if;
15834 end if;
15835
15836 -- Case 1: If the parent is a limited interface then it has the
15837 -- predefined primitives of synchronized interfaces. However, the
15838 -- actual type may be a non-limited type and hence it does not
15839 -- have such primitives.
15840
15841 if Present (Generic_Actual)
15842 and then not Present (Act_Subp)
15843 and then Is_Limited_Interface (Parent_Base)
15844 and then Is_Predefined_Interface_Primitive (Subp)
15845 then
15846 null;
15847
15848 -- Case 2: Inherit entities associated with interfaces that were
15849 -- not covered by the parent type. We exclude here null interface
15850 -- primitives because they do not need special management.
15851
15852 -- We also exclude interface operations that are renamings. If the
15853 -- subprogram is an explicit renaming of an interface primitive,
15854 -- it is a regular primitive operation, and the presence of its
15855 -- alias is not relevant: it has to be derived like any other
15856 -- primitive.
15857
15858 elsif Present (Alias (Subp))
15859 and then Nkind (Unit_Declaration_Node (Subp)) /=
15860 N_Subprogram_Renaming_Declaration
15861 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
15862 and then not
15863 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
15864 and then Null_Present (Parent (Alias_Subp)))
15865 then
15866 -- If this is an abstract private type then we transfer the
15867 -- derivation of the interface primitive from the partial view
15868 -- to the full view. This is safe because all the interfaces
15869 -- must be visible in the partial view. Done to avoid adding
15870 -- a new interface derivation to the private part of the
15871 -- enclosing package; otherwise this new derivation would be
15872 -- decorated as hidden when the analysis of the enclosing
15873 -- package completes.
15874
15875 if Is_Abstract_Type (Derived_Type)
15876 and then In_Private_Part (Current_Scope)
15877 and then Has_Private_Declaration (Derived_Type)
15878 then
15879 declare
15880 Partial_View : Entity_Id;
15881 Elmt : Elmt_Id;
15882 Ent : Entity_Id;
15883
15884 begin
15885 Partial_View := First_Entity (Current_Scope);
15886 loop
15887 exit when No (Partial_View)
15888 or else (Has_Private_Declaration (Partial_View)
15889 and then
15890 Full_View (Partial_View) = Derived_Type);
15891
15892 Next_Entity (Partial_View);
15893 end loop;
15894
15895 -- If the partial view was not found then the source code
15896 -- has errors and the derivation is not needed.
15897
15898 if Present (Partial_View) then
15899 Elmt :=
15900 First_Elmt (Primitive_Operations (Partial_View));
15901 while Present (Elmt) loop
15902 Ent := Node (Elmt);
15903
15904 if Present (Alias (Ent))
15905 and then Ultimate_Alias (Ent) = Alias (Subp)
15906 then
15907 Append_Elmt
15908 (Ent, Primitive_Operations (Derived_Type));
15909 exit;
15910 end if;
15911
15912 Next_Elmt (Elmt);
15913 end loop;
15914
15915 -- If the interface primitive was not found in the
15916 -- partial view then this interface primitive was
15917 -- overridden. We add a derivation to activate in
15918 -- Derive_Progenitor_Subprograms the machinery to
15919 -- search for it.
15920
15921 if No (Elmt) then
15922 Derive_Interface_Subprogram
15923 (New_Subp => New_Subp,
15924 Subp => Subp,
15925 Actual_Subp => Act_Subp);
15926 end if;
15927 end if;
15928 end;
15929 else
15930 Derive_Interface_Subprogram
15931 (New_Subp => New_Subp,
15932 Subp => Subp,
15933 Actual_Subp => Act_Subp);
15934 end if;
15935
15936 -- Case 3: Common derivation
15937
15938 else
15939 Derive_Subprogram
15940 (New_Subp => New_Subp,
15941 Parent_Subp => Subp,
15942 Derived_Type => Derived_Type,
15943 Parent_Type => Parent_Base,
15944 Actual_Subp => Act_Subp);
15945 end if;
15946
15947 -- No need to update Act_Elm if we must search for the
15948 -- corresponding operation in the generic actual
15949
15950 if not Need_Search
15951 and then Present (Act_Elmt)
15952 then
15953 Next_Elmt (Act_Elmt);
15954 Act_Subp := Node (Act_Elmt);
15955 end if;
15956
15957 <<Continue>>
15958 Next_Elmt (Elmt);
15959 end loop;
15960
15961 -- Inherit additional operations from progenitors. If the derived
15962 -- type is a generic actual, there are not new primitive operations
15963 -- for the type because it has those of the actual, and therefore
15964 -- nothing needs to be done. The renamings generated above are not
15965 -- primitive operations, and their purpose is simply to make the
15966 -- proper operations visible within an instantiation.
15967
15968 if No (Generic_Actual) then
15969 Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
15970 end if;
15971 end if;
15972
15973 -- Final check: Direct descendants must have their primitives in the
15974 -- same order. We exclude from this test untagged types and instances
15975 -- of formal derived types. We skip this test if we have already
15976 -- reported serious errors in the sources.
15977
15978 pragma Assert (not Is_Tagged_Type (Derived_Type)
15979 or else Present (Generic_Actual)
15980 or else Serious_Errors_Detected > 0
15981 or else Check_Derived_Type);
15982 end Derive_Subprograms;
15983
15984 --------------------------------
15985 -- Derived_Standard_Character --
15986 --------------------------------
15987
15988 procedure Derived_Standard_Character
15989 (N : Node_Id;
15990 Parent_Type : Entity_Id;
15991 Derived_Type : Entity_Id)
15992 is
15993 Loc : constant Source_Ptr := Sloc (N);
15994 Def : constant Node_Id := Type_Definition (N);
15995 Indic : constant Node_Id := Subtype_Indication (Def);
15996 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
15997 Implicit_Base : constant Entity_Id :=
15998 Create_Itype
15999 (E_Enumeration_Type, N, Derived_Type, 'B');
16000
16001 Lo : Node_Id;
16002 Hi : Node_Id;
16003
16004 begin
16005 Discard_Node (Process_Subtype (Indic, N));
16006
16007 Set_Etype (Implicit_Base, Parent_Base);
16008 Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
16009 Set_RM_Size (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
16010
16011 Set_Is_Character_Type (Implicit_Base, True);
16012 Set_Has_Delayed_Freeze (Implicit_Base);
16013
16014 -- The bounds of the implicit base are the bounds of the parent base.
16015 -- Note that their type is the parent base.
16016
16017 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
16018 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
16019
16020 Set_Scalar_Range (Implicit_Base,
16021 Make_Range (Loc,
16022 Low_Bound => Lo,
16023 High_Bound => Hi));
16024
16025 Conditional_Delay (Derived_Type, Parent_Type);
16026
16027 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
16028 Set_Etype (Derived_Type, Implicit_Base);
16029 Set_Size_Info (Derived_Type, Parent_Type);
16030
16031 if Unknown_RM_Size (Derived_Type) then
16032 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
16033 end if;
16034
16035 Set_Is_Character_Type (Derived_Type, True);
16036
16037 if Nkind (Indic) /= N_Subtype_Indication then
16038
16039 -- If no explicit constraint, the bounds are those
16040 -- of the parent type.
16041
16042 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Type));
16043 Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
16044 Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
16045 end if;
16046
16047 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
16048
16049 -- Because the implicit base is used in the conversion of the bounds, we
16050 -- have to freeze it now. This is similar to what is done for numeric
16051 -- types, and it equally suspicious, but otherwise a non-static bound
16052 -- will have a reference to an unfrozen type, which is rejected by Gigi
16053 -- (???). This requires specific care for definition of stream
16054 -- attributes. For details, see comments at the end of
16055 -- Build_Derived_Numeric_Type.
16056
16057 Freeze_Before (N, Implicit_Base);
16058 end Derived_Standard_Character;
16059
16060 ------------------------------
16061 -- Derived_Type_Declaration --
16062 ------------------------------
16063
16064 procedure Derived_Type_Declaration
16065 (T : Entity_Id;
16066 N : Node_Id;
16067 Is_Completion : Boolean)
16068 is
16069 Parent_Type : Entity_Id;
16070
16071 function Comes_From_Generic (Typ : Entity_Id) return Boolean;
16072 -- Check whether the parent type is a generic formal, or derives
16073 -- directly or indirectly from one.
16074
16075 ------------------------
16076 -- Comes_From_Generic --
16077 ------------------------
16078
16079 function Comes_From_Generic (Typ : Entity_Id) return Boolean is
16080 begin
16081 if Is_Generic_Type (Typ) then
16082 return True;
16083
16084 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
16085 return True;
16086
16087 elsif Is_Private_Type (Typ)
16088 and then Present (Full_View (Typ))
16089 and then Is_Generic_Type (Root_Type (Full_View (Typ)))
16090 then
16091 return True;
16092
16093 elsif Is_Generic_Actual_Type (Typ) then
16094 return True;
16095
16096 else
16097 return False;
16098 end if;
16099 end Comes_From_Generic;
16100
16101 -- Local variables
16102
16103 Def : constant Node_Id := Type_Definition (N);
16104 Iface_Def : Node_Id;
16105 Indic : constant Node_Id := Subtype_Indication (Def);
16106 Extension : constant Node_Id := Record_Extension_Part (Def);
16107 Parent_Node : Node_Id;
16108 Taggd : Boolean;
16109
16110 -- Start of processing for Derived_Type_Declaration
16111
16112 begin
16113 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
16114
16115 -- Ada 2005 (AI-251): In case of interface derivation check that the
16116 -- parent is also an interface.
16117
16118 if Interface_Present (Def) then
16119 Check_SPARK_05_Restriction ("interface is not allowed", Def);
16120
16121 if not Is_Interface (Parent_Type) then
16122 Diagnose_Interface (Indic, Parent_Type);
16123
16124 else
16125 Parent_Node := Parent (Base_Type (Parent_Type));
16126 Iface_Def := Type_Definition (Parent_Node);
16127
16128 -- Ada 2005 (AI-251): Limited interfaces can only inherit from
16129 -- other limited interfaces.
16130
16131 if Limited_Present (Def) then
16132 if Limited_Present (Iface_Def) then
16133 null;
16134
16135 elsif Protected_Present (Iface_Def) then
16136 Error_Msg_NE
16137 ("descendant of & must be declared as a protected "
16138 & "interface", N, Parent_Type);
16139
16140 elsif Synchronized_Present (Iface_Def) then
16141 Error_Msg_NE
16142 ("descendant of & must be declared as a synchronized "
16143 & "interface", N, Parent_Type);
16144
16145 elsif Task_Present (Iface_Def) then
16146 Error_Msg_NE
16147 ("descendant of & must be declared as a task interface",
16148 N, Parent_Type);
16149
16150 else
16151 Error_Msg_N
16152 ("(Ada 2005) limited interface cannot inherit from "
16153 & "non-limited interface", Indic);
16154 end if;
16155
16156 -- Ada 2005 (AI-345): Non-limited interfaces can only inherit
16157 -- from non-limited or limited interfaces.
16158
16159 elsif not Protected_Present (Def)
16160 and then not Synchronized_Present (Def)
16161 and then not Task_Present (Def)
16162 then
16163 if Limited_Present (Iface_Def) then
16164 null;
16165
16166 elsif Protected_Present (Iface_Def) then
16167 Error_Msg_NE
16168 ("descendant of & must be declared as a protected "
16169 & "interface", N, Parent_Type);
16170
16171 elsif Synchronized_Present (Iface_Def) then
16172 Error_Msg_NE
16173 ("descendant of & must be declared as a synchronized "
16174 & "interface", N, Parent_Type);
16175
16176 elsif Task_Present (Iface_Def) then
16177 Error_Msg_NE
16178 ("descendant of & must be declared as a task interface",
16179 N, Parent_Type);
16180 else
16181 null;
16182 end if;
16183 end if;
16184 end if;
16185 end if;
16186
16187 if Is_Tagged_Type (Parent_Type)
16188 and then Is_Concurrent_Type (Parent_Type)
16189 and then not Is_Interface (Parent_Type)
16190 then
16191 Error_Msg_N
16192 ("parent type of a record extension cannot be a synchronized "
16193 & "tagged type (RM 3.9.1 (3/1))", N);
16194 Set_Etype (T, Any_Type);
16195 return;
16196 end if;
16197
16198 -- Ada 2005 (AI-251): Decorate all the names in the list of ancestor
16199 -- interfaces
16200
16201 if Is_Tagged_Type (Parent_Type)
16202 and then Is_Non_Empty_List (Interface_List (Def))
16203 then
16204 declare
16205 Intf : Node_Id;
16206 T : Entity_Id;
16207
16208 begin
16209 Intf := First (Interface_List (Def));
16210 while Present (Intf) loop
16211 T := Find_Type_Of_Subtype_Indic (Intf);
16212
16213 if not Is_Interface (T) then
16214 Diagnose_Interface (Intf, T);
16215
16216 -- Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
16217 -- a limited type from having a nonlimited progenitor.
16218
16219 elsif (Limited_Present (Def)
16220 or else (not Is_Interface (Parent_Type)
16221 and then Is_Limited_Type (Parent_Type)))
16222 and then not Is_Limited_Interface (T)
16223 then
16224 Error_Msg_NE
16225 ("progenitor interface& of limited type must be limited",
16226 N, T);
16227 end if;
16228
16229 Next (Intf);
16230 end loop;
16231 end;
16232 end if;
16233
16234 if Parent_Type = Any_Type
16235 or else Etype (Parent_Type) = Any_Type
16236 or else (Is_Class_Wide_Type (Parent_Type)
16237 and then Etype (Parent_Type) = T)
16238 then
16239 -- If Parent_Type is undefined or illegal, make new type into a
16240 -- subtype of Any_Type, and set a few attributes to prevent cascaded
16241 -- errors. If this is a self-definition, emit error now.
16242
16243 if T = Parent_Type or else T = Etype (Parent_Type) then
16244 Error_Msg_N ("type cannot be used in its own definition", Indic);
16245 end if;
16246
16247 Set_Ekind (T, Ekind (Parent_Type));
16248 Set_Etype (T, Any_Type);
16249 Set_Scalar_Range (T, Scalar_Range (Any_Type));
16250
16251 if Is_Tagged_Type (T)
16252 and then Is_Record_Type (T)
16253 then
16254 Set_Direct_Primitive_Operations (T, New_Elmt_List);
16255 end if;
16256
16257 return;
16258 end if;
16259
16260 -- Ada 2005 (AI-251): The case in which the parent of the full-view is
16261 -- an interface is special because the list of interfaces in the full
16262 -- view can be given in any order. For example:
16263
16264 -- type A is interface;
16265 -- type B is interface and A;
16266 -- type D is new B with private;
16267 -- private
16268 -- type D is new A and B with null record; -- 1 --
16269
16270 -- In this case we perform the following transformation of -1-:
16271
16272 -- type D is new B and A with null record;
16273
16274 -- If the parent of the full-view covers the parent of the partial-view
16275 -- we have two possible cases:
16276
16277 -- 1) They have the same parent
16278 -- 2) The parent of the full-view implements some further interfaces
16279
16280 -- In both cases we do not need to perform the transformation. In the
16281 -- first case the source program is correct and the transformation is
16282 -- not needed; in the second case the source program does not fulfill
16283 -- the no-hidden interfaces rule (AI-396) and the error will be reported
16284 -- later.
16285
16286 -- This transformation not only simplifies the rest of the analysis of
16287 -- this type declaration but also simplifies the correct generation of
16288 -- the object layout to the expander.
16289
16290 if In_Private_Part (Current_Scope)
16291 and then Is_Interface (Parent_Type)
16292 then
16293 declare
16294 Iface : Node_Id;
16295 Partial_View : Entity_Id;
16296 Partial_View_Parent : Entity_Id;
16297 New_Iface : Node_Id;
16298
16299 begin
16300 -- Look for the associated private type declaration
16301
16302 Partial_View := First_Entity (Current_Scope);
16303 loop
16304 exit when No (Partial_View)
16305 or else (Has_Private_Declaration (Partial_View)
16306 and then Full_View (Partial_View) = T);
16307
16308 Next_Entity (Partial_View);
16309 end loop;
16310
16311 -- If the partial view was not found then the source code has
16312 -- errors and the transformation is not needed.
16313
16314 if Present (Partial_View) then
16315 Partial_View_Parent := Etype (Partial_View);
16316
16317 -- If the parent of the full-view covers the parent of the
16318 -- partial-view we have nothing else to do.
16319
16320 if Interface_Present_In_Ancestor
16321 (Parent_Type, Partial_View_Parent)
16322 then
16323 null;
16324
16325 -- Traverse the list of interfaces of the full-view to look
16326 -- for the parent of the partial-view and perform the tree
16327 -- transformation.
16328
16329 else
16330 Iface := First (Interface_List (Def));
16331 while Present (Iface) loop
16332 if Etype (Iface) = Etype (Partial_View) then
16333 Rewrite (Subtype_Indication (Def),
16334 New_Copy (Subtype_Indication
16335 (Parent (Partial_View))));
16336
16337 New_Iface :=
16338 Make_Identifier (Sloc (N), Chars (Parent_Type));
16339 Append (New_Iface, Interface_List (Def));
16340
16341 -- Analyze the transformed code
16342
16343 Derived_Type_Declaration (T, N, Is_Completion);
16344 return;
16345 end if;
16346
16347 Next (Iface);
16348 end loop;
16349 end if;
16350 end if;
16351 end;
16352 end if;
16353
16354 -- Only composite types other than array types are allowed to have
16355 -- discriminants.
16356
16357 if Present (Discriminant_Specifications (N)) then
16358 if (Is_Elementary_Type (Parent_Type)
16359 or else
16360 Is_Array_Type (Parent_Type))
16361 and then not Error_Posted (N)
16362 then
16363 Error_Msg_N
16364 ("elementary or array type cannot have discriminants",
16365 Defining_Identifier (First (Discriminant_Specifications (N))));
16366 Set_Has_Discriminants (T, False);
16367
16368 -- The type is allowed to have discriminants
16369
16370 else
16371 Check_SPARK_05_Restriction ("discriminant type is not allowed", N);
16372 end if;
16373 end if;
16374
16375 -- In Ada 83, a derived type defined in a package specification cannot
16376 -- be used for further derivation until the end of its visible part.
16377 -- Note that derivation in the private part of the package is allowed.
16378
16379 if Ada_Version = Ada_83
16380 and then Is_Derived_Type (Parent_Type)
16381 and then In_Visible_Part (Scope (Parent_Type))
16382 then
16383 if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
16384 Error_Msg_N
16385 ("(Ada 83): premature use of type for derivation", Indic);
16386 end if;
16387 end if;
16388
16389 -- Check for early use of incomplete or private type
16390
16391 if Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
16392 Error_Msg_N ("premature derivation of incomplete type", Indic);
16393 return;
16394
16395 elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
16396 and then not Comes_From_Generic (Parent_Type))
16397 or else Has_Private_Component (Parent_Type)
16398 then
16399 -- The ancestor type of a formal type can be incomplete, in which
16400 -- case only the operations of the partial view are available in the
16401 -- generic. Subsequent checks may be required when the full view is
16402 -- analyzed to verify that a derivation from a tagged type has an
16403 -- extension.
16404
16405 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
16406 null;
16407
16408 elsif No (Underlying_Type (Parent_Type))
16409 or else Has_Private_Component (Parent_Type)
16410 then
16411 Error_Msg_N
16412 ("premature derivation of derived or private type", Indic);
16413
16414 -- Flag the type itself as being in error, this prevents some
16415 -- nasty problems with subsequent uses of the malformed type.
16416
16417 Set_Error_Posted (T);
16418
16419 -- Check that within the immediate scope of an untagged partial
16420 -- view it's illegal to derive from the partial view if the
16421 -- full view is tagged. (7.3(7))
16422
16423 -- We verify that the Parent_Type is a partial view by checking
16424 -- that it is not a Full_Type_Declaration (i.e. a private type or
16425 -- private extension declaration), to distinguish a partial view
16426 -- from a derivation from a private type which also appears as
16427 -- E_Private_Type. If the parent base type is not declared in an
16428 -- enclosing scope there is no need to check.
16429
16430 elsif Present (Full_View (Parent_Type))
16431 and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
16432 and then not Is_Tagged_Type (Parent_Type)
16433 and then Is_Tagged_Type (Full_View (Parent_Type))
16434 and then In_Open_Scopes (Scope (Base_Type (Parent_Type)))
16435 then
16436 Error_Msg_N
16437 ("premature derivation from type with tagged full view",
16438 Indic);
16439 end if;
16440 end if;
16441
16442 -- Check that form of derivation is appropriate
16443
16444 Taggd := Is_Tagged_Type (Parent_Type);
16445
16446 -- Set the parent type to the class-wide type's specific type in this
16447 -- case to prevent cascading errors
16448
16449 if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
16450 Error_Msg_N ("parent type must not be a class-wide type", Indic);
16451 Set_Etype (T, Etype (Parent_Type));
16452 return;
16453 end if;
16454
16455 if Present (Extension) and then not Taggd then
16456 Error_Msg_N
16457 ("type derived from untagged type cannot have extension", Indic);
16458
16459 elsif No (Extension) and then Taggd then
16460
16461 -- If this declaration is within a private part (or body) of a
16462 -- generic instantiation then the derivation is allowed (the parent
16463 -- type can only appear tagged in this case if it's a generic actual
16464 -- type, since it would otherwise have been rejected in the analysis
16465 -- of the generic template).
16466
16467 if not Is_Generic_Actual_Type (Parent_Type)
16468 or else In_Visible_Part (Scope (Parent_Type))
16469 then
16470 if Is_Class_Wide_Type (Parent_Type) then
16471 Error_Msg_N
16472 ("parent type must not be a class-wide type", Indic);
16473
16474 -- Use specific type to prevent cascaded errors.
16475
16476 Parent_Type := Etype (Parent_Type);
16477
16478 else
16479 Error_Msg_N
16480 ("type derived from tagged type must have extension", Indic);
16481 end if;
16482 end if;
16483 end if;
16484
16485 -- AI-443: Synchronized formal derived types require a private
16486 -- extension. There is no point in checking the ancestor type or
16487 -- the progenitors since the construct is wrong to begin with.
16488
16489 if Ada_Version >= Ada_2005
16490 and then Is_Generic_Type (T)
16491 and then Present (Original_Node (N))
16492 then
16493 declare
16494 Decl : constant Node_Id := Original_Node (N);
16495
16496 begin
16497 if Nkind (Decl) = N_Formal_Type_Declaration
16498 and then Nkind (Formal_Type_Definition (Decl)) =
16499 N_Formal_Derived_Type_Definition
16500 and then Synchronized_Present (Formal_Type_Definition (Decl))
16501 and then No (Extension)
16502
16503 -- Avoid emitting a duplicate error message
16504
16505 and then not Error_Posted (Indic)
16506 then
16507 Error_Msg_N
16508 ("synchronized derived type must have extension", N);
16509 end if;
16510 end;
16511 end if;
16512
16513 if Null_Exclusion_Present (Def)
16514 and then not Is_Access_Type (Parent_Type)
16515 then
16516 Error_Msg_N ("null exclusion can only apply to an access type", N);
16517 end if;
16518
16519 -- Avoid deriving parent primitives of underlying record views
16520
16521 Build_Derived_Type (N, Parent_Type, T, Is_Completion,
16522 Derive_Subps => not Is_Underlying_Record_View (T));
16523
16524 -- AI-419: The parent type of an explicitly limited derived type must
16525 -- be a limited type or a limited interface.
16526
16527 if Limited_Present (Def) then
16528 Set_Is_Limited_Record (T);
16529
16530 if Is_Interface (T) then
16531 Set_Is_Limited_Interface (T);
16532 end if;
16533
16534 if not Is_Limited_Type (Parent_Type)
16535 and then
16536 (not Is_Interface (Parent_Type)
16537 or else not Is_Limited_Interface (Parent_Type))
16538 then
16539 -- AI05-0096: a derivation in the private part of an instance is
16540 -- legal if the generic formal is untagged limited, and the actual
16541 -- is non-limited.
16542
16543 if Is_Generic_Actual_Type (Parent_Type)
16544 and then In_Private_Part (Current_Scope)
16545 and then
16546 not Is_Tagged_Type
16547 (Generic_Parent_Type (Parent (Parent_Type)))
16548 then
16549 null;
16550
16551 else
16552 Error_Msg_NE
16553 ("parent type& of limited type must be limited",
16554 N, Parent_Type);
16555 end if;
16556 end if;
16557 end if;
16558
16559 -- In SPARK, there are no derived type definitions other than type
16560 -- extensions of tagged record types.
16561
16562 if No (Extension) then
16563 Check_SPARK_05_Restriction
16564 ("derived type is not allowed", Original_Node (N));
16565 end if;
16566 end Derived_Type_Declaration;
16567
16568 ------------------------
16569 -- Diagnose_Interface --
16570 ------------------------
16571
16572 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id) is
16573 begin
16574 if not Is_Interface (E) and then E /= Any_Type then
16575 Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
16576 end if;
16577 end Diagnose_Interface;
16578
16579 ----------------------------------
16580 -- Enumeration_Type_Declaration --
16581 ----------------------------------
16582
16583 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
16584 Ev : Uint;
16585 L : Node_Id;
16586 R_Node : Node_Id;
16587 B_Node : Node_Id;
16588
16589 begin
16590 -- Create identifier node representing lower bound
16591
16592 B_Node := New_Node (N_Identifier, Sloc (Def));
16593 L := First (Literals (Def));
16594 Set_Chars (B_Node, Chars (L));
16595 Set_Entity (B_Node, L);
16596 Set_Etype (B_Node, T);
16597 Set_Is_Static_Expression (B_Node, True);
16598
16599 R_Node := New_Node (N_Range, Sloc (Def));
16600 Set_Low_Bound (R_Node, B_Node);
16601
16602 Set_Ekind (T, E_Enumeration_Type);
16603 Set_First_Literal (T, L);
16604 Set_Etype (T, T);
16605 Set_Is_Constrained (T);
16606
16607 Ev := Uint_0;
16608
16609 -- Loop through literals of enumeration type setting pos and rep values
16610 -- except that if the Ekind is already set, then it means the literal
16611 -- was already constructed (case of a derived type declaration and we
16612 -- should not disturb the Pos and Rep values.
16613
16614 while Present (L) loop
16615 if Ekind (L) /= E_Enumeration_Literal then
16616 Set_Ekind (L, E_Enumeration_Literal);
16617 Set_Enumeration_Pos (L, Ev);
16618 Set_Enumeration_Rep (L, Ev);
16619 Set_Is_Known_Valid (L, True);
16620 end if;
16621
16622 Set_Etype (L, T);
16623 New_Overloaded_Entity (L);
16624 Generate_Definition (L);
16625 Set_Convention (L, Convention_Intrinsic);
16626
16627 -- Case of character literal
16628
16629 if Nkind (L) = N_Defining_Character_Literal then
16630 Set_Is_Character_Type (T, True);
16631
16632 -- Check violation of No_Wide_Characters
16633
16634 if Restriction_Check_Required (No_Wide_Characters) then
16635 Get_Name_String (Chars (L));
16636
16637 if Name_Len >= 3 and then Name_Buffer (1 .. 2) = "QW" then
16638 Check_Restriction (No_Wide_Characters, L);
16639 end if;
16640 end if;
16641 end if;
16642
16643 Ev := Ev + 1;
16644 Next (L);
16645 end loop;
16646
16647 -- Now create a node representing upper bound
16648
16649 B_Node := New_Node (N_Identifier, Sloc (Def));
16650 Set_Chars (B_Node, Chars (Last (Literals (Def))));
16651 Set_Entity (B_Node, Last (Literals (Def)));
16652 Set_Etype (B_Node, T);
16653 Set_Is_Static_Expression (B_Node, True);
16654
16655 Set_High_Bound (R_Node, B_Node);
16656
16657 -- Initialize various fields of the type. Some of this information
16658 -- may be overwritten later through rep.clauses.
16659
16660 Set_Scalar_Range (T, R_Node);
16661 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
16662 Set_Enum_Esize (T);
16663 Set_Enum_Pos_To_Rep (T, Empty);
16664
16665 -- Set Discard_Names if configuration pragma set, or if there is
16666 -- a parameterless pragma in the current declarative region
16667
16668 if Global_Discard_Names or else Discard_Names (Scope (T)) then
16669 Set_Discard_Names (T);
16670 end if;
16671
16672 -- Process end label if there is one
16673
16674 if Present (Def) then
16675 Process_End_Label (Def, 'e', T);
16676 end if;
16677 end Enumeration_Type_Declaration;
16678
16679 ---------------------------------
16680 -- Expand_To_Stored_Constraint --
16681 ---------------------------------
16682
16683 function Expand_To_Stored_Constraint
16684 (Typ : Entity_Id;
16685 Constraint : Elist_Id) return Elist_Id
16686 is
16687 Explicitly_Discriminated_Type : Entity_Id;
16688 Expansion : Elist_Id;
16689 Discriminant : Entity_Id;
16690
16691 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
16692 -- Find the nearest type that actually specifies discriminants
16693
16694 ---------------------------------
16695 -- Type_With_Explicit_Discrims --
16696 ---------------------------------
16697
16698 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
16699 Typ : constant E := Base_Type (Id);
16700
16701 begin
16702 if Ekind (Typ) in Incomplete_Or_Private_Kind then
16703 if Present (Full_View (Typ)) then
16704 return Type_With_Explicit_Discrims (Full_View (Typ));
16705 end if;
16706
16707 else
16708 if Has_Discriminants (Typ) then
16709 return Typ;
16710 end if;
16711 end if;
16712
16713 if Etype (Typ) = Typ then
16714 return Empty;
16715 elsif Has_Discriminants (Typ) then
16716 return Typ;
16717 else
16718 return Type_With_Explicit_Discrims (Etype (Typ));
16719 end if;
16720
16721 end Type_With_Explicit_Discrims;
16722
16723 -- Start of processing for Expand_To_Stored_Constraint
16724
16725 begin
16726 if No (Constraint) or else Is_Empty_Elmt_List (Constraint) then
16727 return No_Elist;
16728 end if;
16729
16730 Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
16731
16732 if No (Explicitly_Discriminated_Type) then
16733 return No_Elist;
16734 end if;
16735
16736 Expansion := New_Elmt_List;
16737
16738 Discriminant :=
16739 First_Stored_Discriminant (Explicitly_Discriminated_Type);
16740 while Present (Discriminant) loop
16741 Append_Elmt
16742 (Get_Discriminant_Value
16743 (Discriminant, Explicitly_Discriminated_Type, Constraint),
16744 To => Expansion);
16745 Next_Stored_Discriminant (Discriminant);
16746 end loop;
16747
16748 return Expansion;
16749 end Expand_To_Stored_Constraint;
16750
16751 ---------------------------
16752 -- Find_Hidden_Interface --
16753 ---------------------------
16754
16755 function Find_Hidden_Interface
16756 (Src : Elist_Id;
16757 Dest : Elist_Id) return Entity_Id
16758 is
16759 Iface : Entity_Id;
16760 Iface_Elmt : Elmt_Id;
16761
16762 begin
16763 if Present (Src) and then Present (Dest) then
16764 Iface_Elmt := First_Elmt (Src);
16765 while Present (Iface_Elmt) loop
16766 Iface := Node (Iface_Elmt);
16767
16768 if Is_Interface (Iface)
16769 and then not Contain_Interface (Iface, Dest)
16770 then
16771 return Iface;
16772 end if;
16773
16774 Next_Elmt (Iface_Elmt);
16775 end loop;
16776 end if;
16777
16778 return Empty;
16779 end Find_Hidden_Interface;
16780
16781 --------------------
16782 -- Find_Type_Name --
16783 --------------------
16784
16785 function Find_Type_Name (N : Node_Id) return Entity_Id is
16786 Id : constant Entity_Id := Defining_Identifier (N);
16787 New_Id : Entity_Id;
16788 Prev : Entity_Id;
16789 Prev_Par : Node_Id;
16790
16791 procedure Check_Duplicate_Aspects;
16792 -- Check that aspects specified in a completion have not been specified
16793 -- already in the partial view.
16794
16795 procedure Tag_Mismatch;
16796 -- Diagnose a tagged partial view whose full view is untagged. We post
16797 -- the message on the full view, with a reference to the previous
16798 -- partial view. The partial view can be private or incomplete, and
16799 -- these are handled in a different manner, so we determine the position
16800 -- of the error message from the respective slocs of both.
16801
16802 -----------------------------
16803 -- Check_Duplicate_Aspects --
16804 -----------------------------
16805
16806 procedure Check_Duplicate_Aspects is
16807 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id;
16808 -- Return the corresponding aspect of the partial view which matches
16809 -- the aspect id of Asp. Return Empty is no such aspect exists.
16810
16811 -----------------------------
16812 -- Get_Partial_View_Aspect --
16813 -----------------------------
16814
16815 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id is
16816 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp);
16817 Prev_Asps : constant List_Id := Aspect_Specifications (Prev_Par);
16818 Prev_Asp : Node_Id;
16819
16820 begin
16821 if Present (Prev_Asps) then
16822 Prev_Asp := First (Prev_Asps);
16823 while Present (Prev_Asp) loop
16824 if Get_Aspect_Id (Prev_Asp) = Asp_Id then
16825 return Prev_Asp;
16826 end if;
16827
16828 Next (Prev_Asp);
16829 end loop;
16830 end if;
16831
16832 return Empty;
16833 end Get_Partial_View_Aspect;
16834
16835 -- Local variables
16836
16837 Full_Asps : constant List_Id := Aspect_Specifications (N);
16838 Full_Asp : Node_Id;
16839 Part_Asp : Node_Id;
16840
16841 -- Start of processing for Check_Duplicate_Aspects
16842
16843 begin
16844 if Present (Full_Asps) then
16845 Full_Asp := First (Full_Asps);
16846 while Present (Full_Asp) loop
16847 Part_Asp := Get_Partial_View_Aspect (Full_Asp);
16848
16849 -- An aspect and its class-wide counterpart are two distinct
16850 -- aspects and may apply to both views of an entity.
16851
16852 if Present (Part_Asp)
16853 and then Class_Present (Part_Asp) = Class_Present (Full_Asp)
16854 then
16855 Error_Msg_N
16856 ("aspect already specified in private declaration",
16857 Full_Asp);
16858
16859 Remove (Full_Asp);
16860 return;
16861 end if;
16862
16863 if Has_Discriminants (Prev)
16864 and then not Has_Unknown_Discriminants (Prev)
16865 and then Get_Aspect_Id (Full_Asp) =
16866 Aspect_Implicit_Dereference
16867 then
16868 Error_Msg_N
16869 ("cannot specify aspect if partial view has known "
16870 & "discriminants", Full_Asp);
16871 end if;
16872
16873 Next (Full_Asp);
16874 end loop;
16875 end if;
16876 end Check_Duplicate_Aspects;
16877
16878 ------------------
16879 -- Tag_Mismatch --
16880 ------------------
16881
16882 procedure Tag_Mismatch is
16883 begin
16884 if Sloc (Prev) < Sloc (Id) then
16885 if Ada_Version >= Ada_2012
16886 and then Nkind (N) = N_Private_Type_Declaration
16887 then
16888 Error_Msg_NE
16889 ("declaration of private } must be a tagged type ", Id, Prev);
16890 else
16891 Error_Msg_NE
16892 ("full declaration of } must be a tagged type ", Id, Prev);
16893 end if;
16894
16895 else
16896 if Ada_Version >= Ada_2012
16897 and then Nkind (N) = N_Private_Type_Declaration
16898 then
16899 Error_Msg_NE
16900 ("declaration of private } must be a tagged type ", Prev, Id);
16901 else
16902 Error_Msg_NE
16903 ("full declaration of } must be a tagged type ", Prev, Id);
16904 end if;
16905 end if;
16906 end Tag_Mismatch;
16907
16908 -- Start of processing for Find_Type_Name
16909
16910 begin
16911 -- Find incomplete declaration, if one was given
16912
16913 Prev := Current_Entity_In_Scope (Id);
16914
16915 -- New type declaration
16916
16917 if No (Prev) then
16918 Enter_Name (Id);
16919 return Id;
16920
16921 -- Previous declaration exists
16922
16923 else
16924 Prev_Par := Parent (Prev);
16925
16926 -- Error if not incomplete/private case except if previous
16927 -- declaration is implicit, etc. Enter_Name will emit error if
16928 -- appropriate.
16929
16930 if not Is_Incomplete_Or_Private_Type (Prev) then
16931 Enter_Name (Id);
16932 New_Id := Id;
16933
16934 -- Check invalid completion of private or incomplete type
16935
16936 elsif not Nkind_In (N, N_Full_Type_Declaration,
16937 N_Task_Type_Declaration,
16938 N_Protected_Type_Declaration)
16939 and then
16940 (Ada_Version < Ada_2012
16941 or else not Is_Incomplete_Type (Prev)
16942 or else not Nkind_In (N, N_Private_Type_Declaration,
16943 N_Private_Extension_Declaration))
16944 then
16945 -- Completion must be a full type declarations (RM 7.3(4))
16946
16947 Error_Msg_Sloc := Sloc (Prev);
16948 Error_Msg_NE ("invalid completion of }", Id, Prev);
16949
16950 -- Set scope of Id to avoid cascaded errors. Entity is never
16951 -- examined again, except when saving globals in generics.
16952
16953 Set_Scope (Id, Current_Scope);
16954 New_Id := Id;
16955
16956 -- If this is a repeated incomplete declaration, no further
16957 -- checks are possible.
16958
16959 if Nkind (N) = N_Incomplete_Type_Declaration then
16960 return Prev;
16961 end if;
16962
16963 -- Case of full declaration of incomplete type
16964
16965 elsif Ekind (Prev) = E_Incomplete_Type
16966 and then (Ada_Version < Ada_2012
16967 or else No (Full_View (Prev))
16968 or else not Is_Private_Type (Full_View (Prev)))
16969 then
16970 -- Indicate that the incomplete declaration has a matching full
16971 -- declaration. The defining occurrence of the incomplete
16972 -- declaration remains the visible one, and the procedure
16973 -- Get_Full_View dereferences it whenever the type is used.
16974
16975 if Present (Full_View (Prev)) then
16976 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
16977 end if;
16978
16979 Set_Full_View (Prev, Id);
16980 Append_Entity (Id, Current_Scope);
16981 Set_Is_Public (Id, Is_Public (Prev));
16982 Set_Is_Internal (Id);
16983 New_Id := Prev;
16984
16985 -- If the incomplete view is tagged, a class_wide type has been
16986 -- created already. Use it for the private type as well, in order
16987 -- to prevent multiple incompatible class-wide types that may be
16988 -- created for self-referential anonymous access components.
16989
16990 if Is_Tagged_Type (Prev)
16991 and then Present (Class_Wide_Type (Prev))
16992 then
16993 Set_Ekind (Id, Ekind (Prev)); -- will be reset later
16994 Set_Class_Wide_Type (Id, Class_Wide_Type (Prev));
16995
16996 -- Type of the class-wide type is the current Id. Previously
16997 -- this was not done for private declarations because of order-
16998 -- of-elaboration issues in the back end, but gigi now handles
16999 -- this properly.
17000
17001 Set_Etype (Class_Wide_Type (Id), Id);
17002 end if;
17003
17004 -- Case of full declaration of private type
17005
17006 else
17007 -- If the private type was a completion of an incomplete type then
17008 -- update Prev to reference the private type
17009
17010 if Ada_Version >= Ada_2012
17011 and then Ekind (Prev) = E_Incomplete_Type
17012 and then Present (Full_View (Prev))
17013 and then Is_Private_Type (Full_View (Prev))
17014 then
17015 Prev := Full_View (Prev);
17016 Prev_Par := Parent (Prev);
17017 end if;
17018
17019 if Nkind (N) = N_Full_Type_Declaration
17020 and then Nkind_In
17021 (Type_Definition (N), N_Record_Definition,
17022 N_Derived_Type_Definition)
17023 and then Interface_Present (Type_Definition (N))
17024 then
17025 Error_Msg_N
17026 ("completion of private type cannot be an interface", N);
17027 end if;
17028
17029 if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
17030 if Etype (Prev) /= Prev then
17031
17032 -- Prev is a private subtype or a derived type, and needs
17033 -- no completion.
17034
17035 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
17036 New_Id := Id;
17037
17038 elsif Ekind (Prev) = E_Private_Type
17039 and then Nkind_In (N, N_Task_Type_Declaration,
17040 N_Protected_Type_Declaration)
17041 then
17042 Error_Msg_N
17043 ("completion of nonlimited type cannot be limited", N);
17044
17045 elsif Ekind (Prev) = E_Record_Type_With_Private
17046 and then Nkind_In (N, N_Task_Type_Declaration,
17047 N_Protected_Type_Declaration)
17048 then
17049 if not Is_Limited_Record (Prev) then
17050 Error_Msg_N
17051 ("completion of nonlimited type cannot be limited", N);
17052
17053 elsif No (Interface_List (N)) then
17054 Error_Msg_N
17055 ("completion of tagged private type must be tagged",
17056 N);
17057 end if;
17058 end if;
17059
17060 -- Ada 2005 (AI-251): Private extension declaration of a task
17061 -- type or a protected type. This case arises when covering
17062 -- interface types.
17063
17064 elsif Nkind_In (N, N_Task_Type_Declaration,
17065 N_Protected_Type_Declaration)
17066 then
17067 null;
17068
17069 elsif Nkind (N) /= N_Full_Type_Declaration
17070 or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
17071 then
17072 Error_Msg_N
17073 ("full view of private extension must be an extension", N);
17074
17075 elsif not (Abstract_Present (Parent (Prev)))
17076 and then Abstract_Present (Type_Definition (N))
17077 then
17078 Error_Msg_N
17079 ("full view of non-abstract extension cannot be abstract", N);
17080 end if;
17081
17082 if not In_Private_Part (Current_Scope) then
17083 Error_Msg_N
17084 ("declaration of full view must appear in private part", N);
17085 end if;
17086
17087 if Ada_Version >= Ada_2012 then
17088 Check_Duplicate_Aspects;
17089 end if;
17090
17091 Copy_And_Swap (Prev, Id);
17092 Set_Has_Private_Declaration (Prev);
17093 Set_Has_Private_Declaration (Id);
17094
17095 -- AI12-0133: Indicate whether we have a partial view with
17096 -- unknown discriminants, in which case initialization of objects
17097 -- of the type do not receive an invariant check.
17098
17099 Set_Partial_View_Has_Unknown_Discr
17100 (Prev, Has_Unknown_Discriminants (Id));
17101
17102 -- Preserve aspect and iterator flags that may have been set on
17103 -- the partial view.
17104
17105 Set_Has_Delayed_Aspects (Prev, Has_Delayed_Aspects (Id));
17106 Set_Has_Implicit_Dereference (Prev, Has_Implicit_Dereference (Id));
17107
17108 -- If no error, propagate freeze_node from private to full view.
17109 -- It may have been generated for an early operational item.
17110
17111 if Present (Freeze_Node (Id))
17112 and then Serious_Errors_Detected = 0
17113 and then No (Full_View (Id))
17114 then
17115 Set_Freeze_Node (Prev, Freeze_Node (Id));
17116 Set_Freeze_Node (Id, Empty);
17117 Set_First_Rep_Item (Prev, First_Rep_Item (Id));
17118 end if;
17119
17120 Set_Full_View (Id, Prev);
17121 New_Id := Prev;
17122 end if;
17123
17124 -- Verify that full declaration conforms to partial one
17125
17126 if Is_Incomplete_Or_Private_Type (Prev)
17127 and then Present (Discriminant_Specifications (Prev_Par))
17128 then
17129 if Present (Discriminant_Specifications (N)) then
17130 if Ekind (Prev) = E_Incomplete_Type then
17131 Check_Discriminant_Conformance (N, Prev, Prev);
17132 else
17133 Check_Discriminant_Conformance (N, Prev, Id);
17134 end if;
17135
17136 else
17137 Error_Msg_N
17138 ("missing discriminants in full type declaration", N);
17139
17140 -- To avoid cascaded errors on subsequent use, share the
17141 -- discriminants of the partial view.
17142
17143 Set_Discriminant_Specifications (N,
17144 Discriminant_Specifications (Prev_Par));
17145 end if;
17146 end if;
17147
17148 -- A prior untagged partial view can have an associated class-wide
17149 -- type due to use of the class attribute, and in this case the full
17150 -- type must also be tagged. This Ada 95 usage is deprecated in favor
17151 -- of incomplete tagged declarations, but we check for it.
17152
17153 if Is_Type (Prev)
17154 and then (Is_Tagged_Type (Prev)
17155 or else Present (Class_Wide_Type (Prev)))
17156 then
17157 -- Ada 2012 (AI05-0162): A private type may be the completion of
17158 -- an incomplete type.
17159
17160 if Ada_Version >= Ada_2012
17161 and then Is_Incomplete_Type (Prev)
17162 and then Nkind_In (N, N_Private_Type_Declaration,
17163 N_Private_Extension_Declaration)
17164 then
17165 -- No need to check private extensions since they are tagged
17166
17167 if Nkind (N) = N_Private_Type_Declaration
17168 and then not Tagged_Present (N)
17169 then
17170 Tag_Mismatch;
17171 end if;
17172
17173 -- The full declaration is either a tagged type (including
17174 -- a synchronized type that implements interfaces) or a
17175 -- type extension, otherwise this is an error.
17176
17177 elsif Nkind_In (N, N_Task_Type_Declaration,
17178 N_Protected_Type_Declaration)
17179 then
17180 if No (Interface_List (N)) and then not Error_Posted (N) then
17181 Tag_Mismatch;
17182 end if;
17183
17184 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
17185
17186 -- Indicate that the previous declaration (tagged incomplete
17187 -- or private declaration) requires the same on the full one.
17188
17189 if not Tagged_Present (Type_Definition (N)) then
17190 Tag_Mismatch;
17191 Set_Is_Tagged_Type (Id);
17192 end if;
17193
17194 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
17195 if No (Record_Extension_Part (Type_Definition (N))) then
17196 Error_Msg_NE
17197 ("full declaration of } must be a record extension",
17198 Prev, Id);
17199
17200 -- Set some attributes to produce a usable full view
17201
17202 Set_Is_Tagged_Type (Id);
17203 end if;
17204
17205 else
17206 Tag_Mismatch;
17207 end if;
17208 end if;
17209
17210 if Present (Prev)
17211 and then Nkind (Parent (Prev)) = N_Incomplete_Type_Declaration
17212 and then Present (Premature_Use (Parent (Prev)))
17213 then
17214 Error_Msg_Sloc := Sloc (N);
17215 Error_Msg_N
17216 ("\full declaration #", Premature_Use (Parent (Prev)));
17217 end if;
17218
17219 return New_Id;
17220 end if;
17221 end Find_Type_Name;
17222
17223 -------------------------
17224 -- Find_Type_Of_Object --
17225 -------------------------
17226
17227 function Find_Type_Of_Object
17228 (Obj_Def : Node_Id;
17229 Related_Nod : Node_Id) return Entity_Id
17230 is
17231 Def_Kind : constant Node_Kind := Nkind (Obj_Def);
17232 P : Node_Id := Parent (Obj_Def);
17233 T : Entity_Id;
17234 Nam : Name_Id;
17235
17236 begin
17237 -- If the parent is a component_definition node we climb to the
17238 -- component_declaration node
17239
17240 if Nkind (P) = N_Component_Definition then
17241 P := Parent (P);
17242 end if;
17243
17244 -- Case of an anonymous array subtype
17245
17246 if Nkind_In (Def_Kind, N_Constrained_Array_Definition,
17247 N_Unconstrained_Array_Definition)
17248 then
17249 T := Empty;
17250 Array_Type_Declaration (T, Obj_Def);
17251
17252 -- Create an explicit subtype whenever possible
17253
17254 elsif Nkind (P) /= N_Component_Declaration
17255 and then Def_Kind = N_Subtype_Indication
17256 then
17257 -- Base name of subtype on object name, which will be unique in
17258 -- the current scope.
17259
17260 -- If this is a duplicate declaration, return base type, to avoid
17261 -- generating duplicate anonymous types.
17262
17263 if Error_Posted (P) then
17264 Analyze (Subtype_Mark (Obj_Def));
17265 return Entity (Subtype_Mark (Obj_Def));
17266 end if;
17267
17268 Nam :=
17269 New_External_Name
17270 (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
17271
17272 T := Make_Defining_Identifier (Sloc (P), Nam);
17273
17274 Insert_Action (Obj_Def,
17275 Make_Subtype_Declaration (Sloc (P),
17276 Defining_Identifier => T,
17277 Subtype_Indication => Relocate_Node (Obj_Def)));
17278
17279 -- This subtype may need freezing, and this will not be done
17280 -- automatically if the object declaration is not in declarative
17281 -- part. Since this is an object declaration, the type cannot always
17282 -- be frozen here. Deferred constants do not freeze their type
17283 -- (which often enough will be private).
17284
17285 if Nkind (P) = N_Object_Declaration
17286 and then Constant_Present (P)
17287 and then No (Expression (P))
17288 then
17289 null;
17290
17291 -- Here we freeze the base type of object type to catch premature use
17292 -- of discriminated private type without a full view.
17293
17294 else
17295 Insert_Actions (Obj_Def, Freeze_Entity (Base_Type (T), P));
17296 end if;
17297
17298 -- Ada 2005 AI-406: the object definition in an object declaration
17299 -- can be an access definition.
17300
17301 elsif Def_Kind = N_Access_Definition then
17302 T := Access_Definition (Related_Nod, Obj_Def);
17303
17304 Set_Is_Local_Anonymous_Access
17305 (T,
17306 V => (Ada_Version < Ada_2012)
17307 or else (Nkind (P) /= N_Object_Declaration)
17308 or else Is_Library_Level_Entity (Defining_Identifier (P)));
17309
17310 -- Otherwise, the object definition is just a subtype_mark
17311
17312 else
17313 T := Process_Subtype (Obj_Def, Related_Nod);
17314
17315 -- If expansion is disabled an object definition that is an aggregate
17316 -- will not get expanded and may lead to scoping problems in the back
17317 -- end, if the object is referenced in an inner scope. In that case
17318 -- create an itype reference for the object definition now. This
17319 -- may be redundant in some cases, but harmless.
17320
17321 if Is_Itype (T)
17322 and then Nkind (Related_Nod) = N_Object_Declaration
17323 and then ASIS_Mode
17324 then
17325 Build_Itype_Reference (T, Related_Nod);
17326 end if;
17327 end if;
17328
17329 return T;
17330 end Find_Type_Of_Object;
17331
17332 --------------------------------
17333 -- Find_Type_Of_Subtype_Indic --
17334 --------------------------------
17335
17336 function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
17337 Typ : Entity_Id;
17338
17339 begin
17340 -- Case of subtype mark with a constraint
17341
17342 if Nkind (S) = N_Subtype_Indication then
17343 Find_Type (Subtype_Mark (S));
17344 Typ := Entity (Subtype_Mark (S));
17345
17346 if not
17347 Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
17348 then
17349 Error_Msg_N
17350 ("incorrect constraint for this kind of type", Constraint (S));
17351 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
17352 end if;
17353
17354 -- Otherwise we have a subtype mark without a constraint
17355
17356 elsif Error_Posted (S) then
17357 Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
17358 return Any_Type;
17359
17360 else
17361 Find_Type (S);
17362 Typ := Entity (S);
17363 end if;
17364
17365 -- Check No_Wide_Characters restriction
17366
17367 Check_Wide_Character_Restriction (Typ, S);
17368
17369 return Typ;
17370 end Find_Type_Of_Subtype_Indic;
17371
17372 -------------------------------------
17373 -- Floating_Point_Type_Declaration --
17374 -------------------------------------
17375
17376 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
17377 Digs : constant Node_Id := Digits_Expression (Def);
17378 Max_Digs_Val : constant Uint := Digits_Value (Standard_Long_Long_Float);
17379 Digs_Val : Uint;
17380 Base_Typ : Entity_Id;
17381 Implicit_Base : Entity_Id;
17382 Bound : Node_Id;
17383
17384 function Can_Derive_From (E : Entity_Id) return Boolean;
17385 -- Find if given digits value, and possibly a specified range, allows
17386 -- derivation from specified type
17387
17388 function Find_Base_Type return Entity_Id;
17389 -- Find a predefined base type that Def can derive from, or generate
17390 -- an error and substitute Long_Long_Float if none exists.
17391
17392 ---------------------
17393 -- Can_Derive_From --
17394 ---------------------
17395
17396 function Can_Derive_From (E : Entity_Id) return Boolean is
17397 Spec : constant Entity_Id := Real_Range_Specification (Def);
17398
17399 begin
17400 -- Check specified "digits" constraint
17401
17402 if Digs_Val > Digits_Value (E) then
17403 return False;
17404 end if;
17405
17406 -- Check for matching range, if specified
17407
17408 if Present (Spec) then
17409 if Expr_Value_R (Type_Low_Bound (E)) >
17410 Expr_Value_R (Low_Bound (Spec))
17411 then
17412 return False;
17413 end if;
17414
17415 if Expr_Value_R (Type_High_Bound (E)) <
17416 Expr_Value_R (High_Bound (Spec))
17417 then
17418 return False;
17419 end if;
17420 end if;
17421
17422 return True;
17423 end Can_Derive_From;
17424
17425 --------------------
17426 -- Find_Base_Type --
17427 --------------------
17428
17429 function Find_Base_Type return Entity_Id is
17430 Choice : Elmt_Id := First_Elmt (Predefined_Float_Types);
17431
17432 begin
17433 -- Iterate over the predefined types in order, returning the first
17434 -- one that Def can derive from.
17435
17436 while Present (Choice) loop
17437 if Can_Derive_From (Node (Choice)) then
17438 return Node (Choice);
17439 end if;
17440
17441 Next_Elmt (Choice);
17442 end loop;
17443
17444 -- If we can't derive from any existing type, use Long_Long_Float
17445 -- and give appropriate message explaining the problem.
17446
17447 if Digs_Val > Max_Digs_Val then
17448 -- It might be the case that there is a type with the requested
17449 -- range, just not the combination of digits and range.
17450
17451 Error_Msg_N
17452 ("no predefined type has requested range and precision",
17453 Real_Range_Specification (Def));
17454
17455 else
17456 Error_Msg_N
17457 ("range too large for any predefined type",
17458 Real_Range_Specification (Def));
17459 end if;
17460
17461 return Standard_Long_Long_Float;
17462 end Find_Base_Type;
17463
17464 -- Start of processing for Floating_Point_Type_Declaration
17465
17466 begin
17467 Check_Restriction (No_Floating_Point, Def);
17468
17469 -- Create an implicit base type
17470
17471 Implicit_Base :=
17472 Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
17473
17474 -- Analyze and verify digits value
17475
17476 Analyze_And_Resolve (Digs, Any_Integer);
17477 Check_Digits_Expression (Digs);
17478 Digs_Val := Expr_Value (Digs);
17479
17480 -- Process possible range spec and find correct type to derive from
17481
17482 Process_Real_Range_Specification (Def);
17483
17484 -- Check that requested number of digits is not too high.
17485
17486 if Digs_Val > Max_Digs_Val then
17487
17488 -- The check for Max_Base_Digits may be somewhat expensive, as it
17489 -- requires reading System, so only do it when necessary.
17490
17491 declare
17492 Max_Base_Digits : constant Uint :=
17493 Expr_Value
17494 (Expression
17495 (Parent (RTE (RE_Max_Base_Digits))));
17496
17497 begin
17498 if Digs_Val > Max_Base_Digits then
17499 Error_Msg_Uint_1 := Max_Base_Digits;
17500 Error_Msg_N ("digits value out of range, maximum is ^", Digs);
17501
17502 elsif No (Real_Range_Specification (Def)) then
17503 Error_Msg_Uint_1 := Max_Digs_Val;
17504 Error_Msg_N ("types with more than ^ digits need range spec "
17505 & "(RM 3.5.7(6))", Digs);
17506 end if;
17507 end;
17508 end if;
17509
17510 -- Find a suitable type to derive from or complain and use a substitute
17511
17512 Base_Typ := Find_Base_Type;
17513
17514 -- If there are bounds given in the declaration use them as the bounds
17515 -- of the type, otherwise use the bounds of the predefined base type
17516 -- that was chosen based on the Digits value.
17517
17518 if Present (Real_Range_Specification (Def)) then
17519 Set_Scalar_Range (T, Real_Range_Specification (Def));
17520 Set_Is_Constrained (T);
17521
17522 -- The bounds of this range must be converted to machine numbers
17523 -- in accordance with RM 4.9(38).
17524
17525 Bound := Type_Low_Bound (T);
17526
17527 if Nkind (Bound) = N_Real_Literal then
17528 Set_Realval
17529 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
17530 Set_Is_Machine_Number (Bound);
17531 end if;
17532
17533 Bound := Type_High_Bound (T);
17534
17535 if Nkind (Bound) = N_Real_Literal then
17536 Set_Realval
17537 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
17538 Set_Is_Machine_Number (Bound);
17539 end if;
17540
17541 else
17542 Set_Scalar_Range (T, Scalar_Range (Base_Typ));
17543 end if;
17544
17545 -- Complete definition of implicit base and declared first subtype. The
17546 -- inheritance of the rep item chain ensures that SPARK-related pragmas
17547 -- are not clobbered when the floating point type acts as a full view of
17548 -- a private type.
17549
17550 Set_Etype (Implicit_Base, Base_Typ);
17551 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
17552 Set_Size_Info (Implicit_Base, Base_Typ);
17553 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
17554 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
17555 Set_Digits_Value (Implicit_Base, Digits_Value (Base_Typ));
17556 Set_Float_Rep (Implicit_Base, Float_Rep (Base_Typ));
17557
17558 Set_Ekind (T, E_Floating_Point_Subtype);
17559 Set_Etype (T, Implicit_Base);
17560 Set_Size_Info (T, Implicit_Base);
17561 Set_RM_Size (T, RM_Size (Implicit_Base));
17562 Inherit_Rep_Item_Chain (T, Implicit_Base);
17563 Set_Digits_Value (T, Digs_Val);
17564 end Floating_Point_Type_Declaration;
17565
17566 ----------------------------
17567 -- Get_Discriminant_Value --
17568 ----------------------------
17569
17570 -- This is the situation:
17571
17572 -- There is a non-derived type
17573
17574 -- type T0 (Dx, Dy, Dz...)
17575
17576 -- There are zero or more levels of derivation, with each derivation
17577 -- either purely inheriting the discriminants, or defining its own.
17578
17579 -- type Ti is new Ti-1
17580 -- or
17581 -- type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
17582 -- or
17583 -- subtype Ti is ...
17584
17585 -- The subtype issue is avoided by the use of Original_Record_Component,
17586 -- and the fact that derived subtypes also derive the constraints.
17587
17588 -- This chain leads back from
17589
17590 -- Typ_For_Constraint
17591
17592 -- Typ_For_Constraint has discriminants, and the value for each
17593 -- discriminant is given by its corresponding Elmt of Constraints.
17594
17595 -- Discriminant is some discriminant in this hierarchy
17596
17597 -- We need to return its value
17598
17599 -- We do this by recursively searching each level, and looking for
17600 -- Discriminant. Once we get to the bottom, we start backing up
17601 -- returning the value for it which may in turn be a discriminant
17602 -- further up, so on the backup we continue the substitution.
17603
17604 function Get_Discriminant_Value
17605 (Discriminant : Entity_Id;
17606 Typ_For_Constraint : Entity_Id;
17607 Constraint : Elist_Id) return Node_Id
17608 is
17609 function Root_Corresponding_Discriminant
17610 (Discr : Entity_Id) return Entity_Id;
17611 -- Given a discriminant, traverse the chain of inherited discriminants
17612 -- and return the topmost discriminant.
17613
17614 function Search_Derivation_Levels
17615 (Ti : Entity_Id;
17616 Discrim_Values : Elist_Id;
17617 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
17618 -- This is the routine that performs the recursive search of levels
17619 -- as described above.
17620
17621 -------------------------------------
17622 -- Root_Corresponding_Discriminant --
17623 -------------------------------------
17624
17625 function Root_Corresponding_Discriminant
17626 (Discr : Entity_Id) return Entity_Id
17627 is
17628 D : Entity_Id;
17629
17630 begin
17631 D := Discr;
17632 while Present (Corresponding_Discriminant (D)) loop
17633 D := Corresponding_Discriminant (D);
17634 end loop;
17635
17636 return D;
17637 end Root_Corresponding_Discriminant;
17638
17639 ------------------------------
17640 -- Search_Derivation_Levels --
17641 ------------------------------
17642
17643 function Search_Derivation_Levels
17644 (Ti : Entity_Id;
17645 Discrim_Values : Elist_Id;
17646 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
17647 is
17648 Assoc : Elmt_Id;
17649 Disc : Entity_Id;
17650 Result : Node_Or_Entity_Id;
17651 Result_Entity : Node_Id;
17652
17653 begin
17654 -- If inappropriate type, return Error, this happens only in
17655 -- cascaded error situations, and we want to avoid a blow up.
17656
17657 if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
17658 return Error;
17659 end if;
17660
17661 -- Look deeper if possible. Use Stored_Constraints only for
17662 -- untagged types. For tagged types use the given constraint.
17663 -- This asymmetry needs explanation???
17664
17665 if not Stored_Discrim_Values
17666 and then Present (Stored_Constraint (Ti))
17667 and then not Is_Tagged_Type (Ti)
17668 then
17669 Result :=
17670 Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
17671 else
17672 declare
17673 Td : constant Entity_Id := Etype (Ti);
17674
17675 begin
17676 if Td = Ti then
17677 Result := Discriminant;
17678
17679 else
17680 if Present (Stored_Constraint (Ti)) then
17681 Result :=
17682 Search_Derivation_Levels
17683 (Td, Stored_Constraint (Ti), True);
17684 else
17685 Result :=
17686 Search_Derivation_Levels
17687 (Td, Discrim_Values, Stored_Discrim_Values);
17688 end if;
17689 end if;
17690 end;
17691 end if;
17692
17693 -- Extra underlying places to search, if not found above. For
17694 -- concurrent types, the relevant discriminant appears in the
17695 -- corresponding record. For a type derived from a private type
17696 -- without discriminant, the full view inherits the discriminants
17697 -- of the full view of the parent.
17698
17699 if Result = Discriminant then
17700 if Is_Concurrent_Type (Ti)
17701 and then Present (Corresponding_Record_Type (Ti))
17702 then
17703 Result :=
17704 Search_Derivation_Levels (
17705 Corresponding_Record_Type (Ti),
17706 Discrim_Values,
17707 Stored_Discrim_Values);
17708
17709 elsif Is_Private_Type (Ti)
17710 and then not Has_Discriminants (Ti)
17711 and then Present (Full_View (Ti))
17712 and then Etype (Full_View (Ti)) /= Ti
17713 then
17714 Result :=
17715 Search_Derivation_Levels (
17716 Full_View (Ti),
17717 Discrim_Values,
17718 Stored_Discrim_Values);
17719 end if;
17720 end if;
17721
17722 -- If Result is not a (reference to a) discriminant, return it,
17723 -- otherwise set Result_Entity to the discriminant.
17724
17725 if Nkind (Result) = N_Defining_Identifier then
17726 pragma Assert (Result = Discriminant);
17727 Result_Entity := Result;
17728
17729 else
17730 if not Denotes_Discriminant (Result) then
17731 return Result;
17732 end if;
17733
17734 Result_Entity := Entity (Result);
17735 end if;
17736
17737 -- See if this level of derivation actually has discriminants because
17738 -- tagged derivations can add them, hence the lower levels need not
17739 -- have any.
17740
17741 if not Has_Discriminants (Ti) then
17742 return Result;
17743 end if;
17744
17745 -- Scan Ti's discriminants for Result_Entity, and return its
17746 -- corresponding value, if any.
17747
17748 Result_Entity := Original_Record_Component (Result_Entity);
17749
17750 Assoc := First_Elmt (Discrim_Values);
17751
17752 if Stored_Discrim_Values then
17753 Disc := First_Stored_Discriminant (Ti);
17754 else
17755 Disc := First_Discriminant (Ti);
17756 end if;
17757
17758 while Present (Disc) loop
17759
17760 -- If no further associations return the discriminant, value will
17761 -- be found on the second pass.
17762
17763 if No (Assoc) then
17764 return Result;
17765 end if;
17766
17767 if Original_Record_Component (Disc) = Result_Entity then
17768 return Node (Assoc);
17769 end if;
17770
17771 Next_Elmt (Assoc);
17772
17773 if Stored_Discrim_Values then
17774 Next_Stored_Discriminant (Disc);
17775 else
17776 Next_Discriminant (Disc);
17777 end if;
17778 end loop;
17779
17780 -- Could not find it
17781
17782 return Result;
17783 end Search_Derivation_Levels;
17784
17785 -- Local Variables
17786
17787 Result : Node_Or_Entity_Id;
17788
17789 -- Start of processing for Get_Discriminant_Value
17790
17791 begin
17792 -- ??? This routine is a gigantic mess and will be deleted. For the
17793 -- time being just test for the trivial case before calling recurse.
17794
17795 -- We are now celebrating the 20th anniversary of this comment!
17796
17797 if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
17798 declare
17799 D : Entity_Id;
17800 E : Elmt_Id;
17801
17802 begin
17803 D := First_Discriminant (Typ_For_Constraint);
17804 E := First_Elmt (Constraint);
17805 while Present (D) loop
17806 if Chars (D) = Chars (Discriminant) then
17807 return Node (E);
17808 end if;
17809
17810 Next_Discriminant (D);
17811 Next_Elmt (E);
17812 end loop;
17813 end;
17814 end if;
17815
17816 Result := Search_Derivation_Levels
17817 (Typ_For_Constraint, Constraint, False);
17818
17819 -- ??? hack to disappear when this routine is gone
17820
17821 if Nkind (Result) = N_Defining_Identifier then
17822 declare
17823 D : Entity_Id;
17824 E : Elmt_Id;
17825
17826 begin
17827 D := First_Discriminant (Typ_For_Constraint);
17828 E := First_Elmt (Constraint);
17829 while Present (D) loop
17830 if Root_Corresponding_Discriminant (D) = Discriminant then
17831 return Node (E);
17832 end if;
17833
17834 Next_Discriminant (D);
17835 Next_Elmt (E);
17836 end loop;
17837 end;
17838 end if;
17839
17840 pragma Assert (Nkind (Result) /= N_Defining_Identifier);
17841 return Result;
17842 end Get_Discriminant_Value;
17843
17844 --------------------------
17845 -- Has_Range_Constraint --
17846 --------------------------
17847
17848 function Has_Range_Constraint (N : Node_Id) return Boolean is
17849 C : constant Node_Id := Constraint (N);
17850
17851 begin
17852 if Nkind (C) = N_Range_Constraint then
17853 return True;
17854
17855 elsif Nkind (C) = N_Digits_Constraint then
17856 return
17857 Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
17858 or else Present (Range_Constraint (C));
17859
17860 elsif Nkind (C) = N_Delta_Constraint then
17861 return Present (Range_Constraint (C));
17862
17863 else
17864 return False;
17865 end if;
17866 end Has_Range_Constraint;
17867
17868 ------------------------
17869 -- Inherit_Components --
17870 ------------------------
17871
17872 function Inherit_Components
17873 (N : Node_Id;
17874 Parent_Base : Entity_Id;
17875 Derived_Base : Entity_Id;
17876 Is_Tagged : Boolean;
17877 Inherit_Discr : Boolean;
17878 Discs : Elist_Id) return Elist_Id
17879 is
17880 Assoc_List : constant Elist_Id := New_Elmt_List;
17881
17882 procedure Inherit_Component
17883 (Old_C : Entity_Id;
17884 Plain_Discrim : Boolean := False;
17885 Stored_Discrim : Boolean := False);
17886 -- Inherits component Old_C from Parent_Base to the Derived_Base. If
17887 -- Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
17888 -- True, Old_C is a stored discriminant. If they are both false then
17889 -- Old_C is a regular component.
17890
17891 -----------------------
17892 -- Inherit_Component --
17893 -----------------------
17894
17895 procedure Inherit_Component
17896 (Old_C : Entity_Id;
17897 Plain_Discrim : Boolean := False;
17898 Stored_Discrim : Boolean := False)
17899 is
17900 procedure Set_Anonymous_Type (Id : Entity_Id);
17901 -- Id denotes the entity of an access discriminant or anonymous
17902 -- access component. Set the type of Id to either the same type of
17903 -- Old_C or create a new one depending on whether the parent and
17904 -- the child types are in the same scope.
17905
17906 ------------------------
17907 -- Set_Anonymous_Type --
17908 ------------------------
17909
17910 procedure Set_Anonymous_Type (Id : Entity_Id) is
17911 Old_Typ : constant Entity_Id := Etype (Old_C);
17912
17913 begin
17914 if Scope (Parent_Base) = Scope (Derived_Base) then
17915 Set_Etype (Id, Old_Typ);
17916
17917 -- The parent and the derived type are in two different scopes.
17918 -- Reuse the type of the original discriminant / component by
17919 -- copying it in order to preserve all attributes.
17920
17921 else
17922 declare
17923 Typ : constant Entity_Id := New_Copy (Old_Typ);
17924
17925 begin
17926 Set_Etype (Id, Typ);
17927
17928 -- Since we do not generate component declarations for
17929 -- inherited components, associate the itype with the
17930 -- derived type.
17931
17932 Set_Associated_Node_For_Itype (Typ, Parent (Derived_Base));
17933 Set_Scope (Typ, Derived_Base);
17934 end;
17935 end if;
17936 end Set_Anonymous_Type;
17937
17938 -- Local variables and constants
17939
17940 New_C : constant Entity_Id := New_Copy (Old_C);
17941
17942 Corr_Discrim : Entity_Id;
17943 Discrim : Entity_Id;
17944
17945 -- Start of processing for Inherit_Component
17946
17947 begin
17948 pragma Assert (not Is_Tagged or not Stored_Discrim);
17949
17950 Set_Parent (New_C, Parent (Old_C));
17951
17952 -- Regular discriminants and components must be inserted in the scope
17953 -- of the Derived_Base. Do it here.
17954
17955 if not Stored_Discrim then
17956 Enter_Name (New_C);
17957 end if;
17958
17959 -- For tagged types the Original_Record_Component must point to
17960 -- whatever this field was pointing to in the parent type. This has
17961 -- already been achieved by the call to New_Copy above.
17962
17963 if not Is_Tagged then
17964 Set_Original_Record_Component (New_C, New_C);
17965 end if;
17966
17967 -- Set the proper type of an access discriminant
17968
17969 if Ekind (New_C) = E_Discriminant
17970 and then Ekind (Etype (New_C)) = E_Anonymous_Access_Type
17971 then
17972 Set_Anonymous_Type (New_C);
17973 end if;
17974
17975 -- If we have inherited a component then see if its Etype contains
17976 -- references to Parent_Base discriminants. In this case, replace
17977 -- these references with the constraints given in Discs. We do not
17978 -- do this for the partial view of private types because this is
17979 -- not needed (only the components of the full view will be used
17980 -- for code generation) and cause problem. We also avoid this
17981 -- transformation in some error situations.
17982
17983 if Ekind (New_C) = E_Component then
17984
17985 -- Set the proper type of an anonymous access component
17986
17987 if Ekind (Etype (New_C)) = E_Anonymous_Access_Type then
17988 Set_Anonymous_Type (New_C);
17989
17990 elsif (Is_Private_Type (Derived_Base)
17991 and then not Is_Generic_Type (Derived_Base))
17992 or else (Is_Empty_Elmt_List (Discs)
17993 and then not Expander_Active)
17994 then
17995 Set_Etype (New_C, Etype (Old_C));
17996
17997 else
17998 -- The current component introduces a circularity of the
17999 -- following kind:
18000
18001 -- limited with Pack_2;
18002 -- package Pack_1 is
18003 -- type T_1 is tagged record
18004 -- Comp : access Pack_2.T_2;
18005 -- ...
18006 -- end record;
18007 -- end Pack_1;
18008
18009 -- with Pack_1;
18010 -- package Pack_2 is
18011 -- type T_2 is new Pack_1.T_1 with ...;
18012 -- end Pack_2;
18013
18014 Set_Etype
18015 (New_C,
18016 Constrain_Component_Type
18017 (Old_C, Derived_Base, N, Parent_Base, Discs));
18018 end if;
18019 end if;
18020
18021 -- In derived tagged types it is illegal to reference a non
18022 -- discriminant component in the parent type. To catch this, mark
18023 -- these components with an Ekind of E_Void. This will be reset in
18024 -- Record_Type_Definition after processing the record extension of
18025 -- the derived type.
18026
18027 -- If the declaration is a private extension, there is no further
18028 -- record extension to process, and the components retain their
18029 -- current kind, because they are visible at this point.
18030
18031 if Is_Tagged and then Ekind (New_C) = E_Component
18032 and then Nkind (N) /= N_Private_Extension_Declaration
18033 then
18034 Set_Ekind (New_C, E_Void);
18035 end if;
18036
18037 if Plain_Discrim then
18038 Set_Corresponding_Discriminant (New_C, Old_C);
18039 Build_Discriminal (New_C);
18040
18041 -- If we are explicitly inheriting a stored discriminant it will be
18042 -- completely hidden.
18043
18044 elsif Stored_Discrim then
18045 Set_Corresponding_Discriminant (New_C, Empty);
18046 Set_Discriminal (New_C, Empty);
18047 Set_Is_Completely_Hidden (New_C);
18048
18049 -- Set the Original_Record_Component of each discriminant in the
18050 -- derived base to point to the corresponding stored that we just
18051 -- created.
18052
18053 Discrim := First_Discriminant (Derived_Base);
18054 while Present (Discrim) loop
18055 Corr_Discrim := Corresponding_Discriminant (Discrim);
18056
18057 -- Corr_Discrim could be missing in an error situation
18058
18059 if Present (Corr_Discrim)
18060 and then Original_Record_Component (Corr_Discrim) = Old_C
18061 then
18062 Set_Original_Record_Component (Discrim, New_C);
18063 end if;
18064
18065 Next_Discriminant (Discrim);
18066 end loop;
18067
18068 Append_Entity (New_C, Derived_Base);
18069 end if;
18070
18071 if not Is_Tagged then
18072 Append_Elmt (Old_C, Assoc_List);
18073 Append_Elmt (New_C, Assoc_List);
18074 end if;
18075 end Inherit_Component;
18076
18077 -- Variables local to Inherit_Component
18078
18079 Loc : constant Source_Ptr := Sloc (N);
18080
18081 Parent_Discrim : Entity_Id;
18082 Stored_Discrim : Entity_Id;
18083 D : Entity_Id;
18084 Component : Entity_Id;
18085
18086 -- Start of processing for Inherit_Components
18087
18088 begin
18089 if not Is_Tagged then
18090 Append_Elmt (Parent_Base, Assoc_List);
18091 Append_Elmt (Derived_Base, Assoc_List);
18092 end if;
18093
18094 -- Inherit parent discriminants if needed
18095
18096 if Inherit_Discr then
18097 Parent_Discrim := First_Discriminant (Parent_Base);
18098 while Present (Parent_Discrim) loop
18099 Inherit_Component (Parent_Discrim, Plain_Discrim => True);
18100 Next_Discriminant (Parent_Discrim);
18101 end loop;
18102 end if;
18103
18104 -- Create explicit stored discrims for untagged types when necessary
18105
18106 if not Has_Unknown_Discriminants (Derived_Base)
18107 and then Has_Discriminants (Parent_Base)
18108 and then not Is_Tagged
18109 and then
18110 (not Inherit_Discr
18111 or else First_Discriminant (Parent_Base) /=
18112 First_Stored_Discriminant (Parent_Base))
18113 then
18114 Stored_Discrim := First_Stored_Discriminant (Parent_Base);
18115 while Present (Stored_Discrim) loop
18116 Inherit_Component (Stored_Discrim, Stored_Discrim => True);
18117 Next_Stored_Discriminant (Stored_Discrim);
18118 end loop;
18119 end if;
18120
18121 -- See if we can apply the second transformation for derived types, as
18122 -- explained in point 6. in the comments above Build_Derived_Record_Type
18123 -- This is achieved by appending Derived_Base discriminants into Discs,
18124 -- which has the side effect of returning a non empty Discs list to the
18125 -- caller of Inherit_Components, which is what we want. This must be
18126 -- done for private derived types if there are explicit stored
18127 -- discriminants, to ensure that we can retrieve the values of the
18128 -- constraints provided in the ancestors.
18129
18130 if Inherit_Discr
18131 and then Is_Empty_Elmt_List (Discs)
18132 and then Present (First_Discriminant (Derived_Base))
18133 and then
18134 (not Is_Private_Type (Derived_Base)
18135 or else Is_Completely_Hidden
18136 (First_Stored_Discriminant (Derived_Base))
18137 or else Is_Generic_Type (Derived_Base))
18138 then
18139 D := First_Discriminant (Derived_Base);
18140 while Present (D) loop
18141 Append_Elmt (New_Occurrence_Of (D, Loc), Discs);
18142 Next_Discriminant (D);
18143 end loop;
18144 end if;
18145
18146 -- Finally, inherit non-discriminant components unless they are not
18147 -- visible because defined or inherited from the full view of the
18148 -- parent. Don't inherit the _parent field of the parent type.
18149
18150 Component := First_Entity (Parent_Base);
18151 while Present (Component) loop
18152
18153 -- Ada 2005 (AI-251): Do not inherit components associated with
18154 -- secondary tags of the parent.
18155
18156 if Ekind (Component) = E_Component
18157 and then Present (Related_Type (Component))
18158 then
18159 null;
18160
18161 elsif Ekind (Component) /= E_Component
18162 or else Chars (Component) = Name_uParent
18163 then
18164 null;
18165
18166 -- If the derived type is within the parent type's declarative
18167 -- region, then the components can still be inherited even though
18168 -- they aren't visible at this point. This can occur for cases
18169 -- such as within public child units where the components must
18170 -- become visible upon entering the child unit's private part.
18171
18172 elsif not Is_Visible_Component (Component)
18173 and then not In_Open_Scopes (Scope (Parent_Base))
18174 then
18175 null;
18176
18177 elsif Ekind_In (Derived_Base, E_Private_Type,
18178 E_Limited_Private_Type)
18179 then
18180 null;
18181
18182 else
18183 Inherit_Component (Component);
18184 end if;
18185
18186 Next_Entity (Component);
18187 end loop;
18188
18189 -- For tagged derived types, inherited discriminants cannot be used in
18190 -- component declarations of the record extension part. To achieve this
18191 -- we mark the inherited discriminants as not visible.
18192
18193 if Is_Tagged and then Inherit_Discr then
18194 D := First_Discriminant (Derived_Base);
18195 while Present (D) loop
18196 Set_Is_Immediately_Visible (D, False);
18197 Next_Discriminant (D);
18198 end loop;
18199 end if;
18200
18201 return Assoc_List;
18202 end Inherit_Components;
18203
18204 -----------------------------
18205 -- Inherit_Predicate_Flags --
18206 -----------------------------
18207
18208 procedure Inherit_Predicate_Flags (Subt, Par : Entity_Id) is
18209 begin
18210 Set_Has_Predicates (Subt, Has_Predicates (Par));
18211 Set_Has_Static_Predicate_Aspect
18212 (Subt, Has_Static_Predicate_Aspect (Par));
18213 Set_Has_Dynamic_Predicate_Aspect
18214 (Subt, Has_Dynamic_Predicate_Aspect (Par));
18215 end Inherit_Predicate_Flags;
18216
18217 ----------------------
18218 -- Is_EVF_Procedure --
18219 ----------------------
18220
18221 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean is
18222 Formal : Entity_Id;
18223
18224 begin
18225 -- Examine the formals of an Extensions_Visible False procedure looking
18226 -- for a controlling OUT parameter.
18227
18228 if Ekind (Subp) = E_Procedure
18229 and then Extensions_Visible_Status (Subp) = Extensions_Visible_False
18230 then
18231 Formal := First_Formal (Subp);
18232 while Present (Formal) loop
18233 if Ekind (Formal) = E_Out_Parameter
18234 and then Is_Controlling_Formal (Formal)
18235 then
18236 return True;
18237 end if;
18238
18239 Next_Formal (Formal);
18240 end loop;
18241 end if;
18242
18243 return False;
18244 end Is_EVF_Procedure;
18245
18246 -----------------------
18247 -- Is_Null_Extension --
18248 -----------------------
18249
18250 function Is_Null_Extension (T : Entity_Id) return Boolean is
18251 Type_Decl : constant Node_Id := Parent (Base_Type (T));
18252 Comp_List : Node_Id;
18253 Comp : Node_Id;
18254
18255 begin
18256 if Nkind (Type_Decl) /= N_Full_Type_Declaration
18257 or else not Is_Tagged_Type (T)
18258 or else Nkind (Type_Definition (Type_Decl)) /=
18259 N_Derived_Type_Definition
18260 or else No (Record_Extension_Part (Type_Definition (Type_Decl)))
18261 then
18262 return False;
18263 end if;
18264
18265 Comp_List :=
18266 Component_List (Record_Extension_Part (Type_Definition (Type_Decl)));
18267
18268 if Present (Discriminant_Specifications (Type_Decl)) then
18269 return False;
18270
18271 elsif Present (Comp_List)
18272 and then Is_Non_Empty_List (Component_Items (Comp_List))
18273 then
18274 Comp := First (Component_Items (Comp_List));
18275
18276 -- Only user-defined components are relevant. The component list
18277 -- may also contain a parent component and internal components
18278 -- corresponding to secondary tags, but these do not determine
18279 -- whether this is a null extension.
18280
18281 while Present (Comp) loop
18282 if Comes_From_Source (Comp) then
18283 return False;
18284 end if;
18285
18286 Next (Comp);
18287 end loop;
18288
18289 return True;
18290
18291 else
18292 return True;
18293 end if;
18294 end Is_Null_Extension;
18295
18296 ------------------------------
18297 -- Is_Valid_Constraint_Kind --
18298 ------------------------------
18299
18300 function Is_Valid_Constraint_Kind
18301 (T_Kind : Type_Kind;
18302 Constraint_Kind : Node_Kind) return Boolean
18303 is
18304 begin
18305 case T_Kind is
18306 when Enumeration_Kind
18307 | Integer_Kind
18308 =>
18309 return Constraint_Kind = N_Range_Constraint;
18310
18311 when Decimal_Fixed_Point_Kind =>
18312 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
18313 N_Range_Constraint);
18314
18315 when Ordinary_Fixed_Point_Kind =>
18316 return Nkind_In (Constraint_Kind, N_Delta_Constraint,
18317 N_Range_Constraint);
18318
18319 when Float_Kind =>
18320 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
18321 N_Range_Constraint);
18322
18323 when Access_Kind
18324 | Array_Kind
18325 | Class_Wide_Kind
18326 | Concurrent_Kind
18327 | Private_Kind
18328 | E_Incomplete_Type
18329 | E_Record_Subtype
18330 | E_Record_Type
18331 =>
18332 return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
18333
18334 when others =>
18335 return True; -- Error will be detected later
18336 end case;
18337 end Is_Valid_Constraint_Kind;
18338
18339 --------------------------
18340 -- Is_Visible_Component --
18341 --------------------------
18342
18343 function Is_Visible_Component
18344 (C : Entity_Id;
18345 N : Node_Id := Empty) return Boolean
18346 is
18347 Original_Comp : Entity_Id := Empty;
18348 Original_Type : Entity_Id;
18349 Type_Scope : Entity_Id;
18350
18351 function Is_Local_Type (Typ : Entity_Id) return Boolean;
18352 -- Check whether parent type of inherited component is declared locally,
18353 -- possibly within a nested package or instance. The current scope is
18354 -- the derived record itself.
18355
18356 -------------------
18357 -- Is_Local_Type --
18358 -------------------
18359
18360 function Is_Local_Type (Typ : Entity_Id) return Boolean is
18361 Scop : Entity_Id;
18362
18363 begin
18364 Scop := Scope (Typ);
18365 while Present (Scop)
18366 and then Scop /= Standard_Standard
18367 loop
18368 if Scop = Scope (Current_Scope) then
18369 return True;
18370 end if;
18371
18372 Scop := Scope (Scop);
18373 end loop;
18374
18375 return False;
18376 end Is_Local_Type;
18377
18378 -- Start of processing for Is_Visible_Component
18379
18380 begin
18381 if Ekind_In (C, E_Component, E_Discriminant) then
18382 Original_Comp := Original_Record_Component (C);
18383 end if;
18384
18385 if No (Original_Comp) then
18386
18387 -- Premature usage, or previous error
18388
18389 return False;
18390
18391 else
18392 Original_Type := Scope (Original_Comp);
18393 Type_Scope := Scope (Base_Type (Scope (C)));
18394 end if;
18395
18396 -- This test only concerns tagged types
18397
18398 if not Is_Tagged_Type (Original_Type) then
18399 return True;
18400
18401 -- If it is _Parent or _Tag, there is no visibility issue
18402
18403 elsif not Comes_From_Source (Original_Comp) then
18404 return True;
18405
18406 -- Discriminants are visible unless the (private) type has unknown
18407 -- discriminants. If the discriminant reference is inserted for a
18408 -- discriminant check on a full view it is also visible.
18409
18410 elsif Ekind (Original_Comp) = E_Discriminant
18411 and then
18412 (not Has_Unknown_Discriminants (Original_Type)
18413 or else (Present (N)
18414 and then Nkind (N) = N_Selected_Component
18415 and then Nkind (Prefix (N)) = N_Type_Conversion
18416 and then not Comes_From_Source (Prefix (N))))
18417 then
18418 return True;
18419
18420 -- In the body of an instantiation, check the visibility of a component
18421 -- in case it has a homograph that is a primitive operation of a private
18422 -- type which was not visible in the generic unit.
18423
18424 -- Should Is_Prefixed_Call be propagated from template to instance???
18425
18426 elsif In_Instance_Body then
18427 if not Is_Tagged_Type (Original_Type)
18428 or else not Is_Private_Type (Original_Type)
18429 then
18430 return True;
18431
18432 else
18433 declare
18434 Subp_Elmt : Elmt_Id;
18435
18436 begin
18437 Subp_Elmt := First_Elmt (Primitive_Operations (Original_Type));
18438 while Present (Subp_Elmt) loop
18439
18440 -- The component is hidden by a primitive operation
18441
18442 if Chars (Node (Subp_Elmt)) = Chars (C) then
18443 return False;
18444 end if;
18445
18446 Next_Elmt (Subp_Elmt);
18447 end loop;
18448
18449 return True;
18450 end;
18451 end if;
18452
18453 -- If the component has been declared in an ancestor which is currently
18454 -- a private type, then it is not visible. The same applies if the
18455 -- component's containing type is not in an open scope and the original
18456 -- component's enclosing type is a visible full view of a private type
18457 -- (which can occur in cases where an attempt is being made to reference
18458 -- a component in a sibling package that is inherited from a visible
18459 -- component of a type in an ancestor package; the component in the
18460 -- sibling package should not be visible even though the component it
18461 -- inherited from is visible). This does not apply however in the case
18462 -- where the scope of the type is a private child unit, or when the
18463 -- parent comes from a local package in which the ancestor is currently
18464 -- visible. The latter suppression of visibility is needed for cases
18465 -- that are tested in B730006.
18466
18467 elsif Is_Private_Type (Original_Type)
18468 or else
18469 (not Is_Private_Descendant (Type_Scope)
18470 and then not In_Open_Scopes (Type_Scope)
18471 and then Has_Private_Declaration (Original_Type))
18472 then
18473 -- If the type derives from an entity in a formal package, there
18474 -- are no additional visible components.
18475
18476 if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
18477 N_Formal_Package_Declaration
18478 then
18479 return False;
18480
18481 -- if we are not in the private part of the current package, there
18482 -- are no additional visible components.
18483
18484 elsif Ekind (Scope (Current_Scope)) = E_Package
18485 and then not In_Private_Part (Scope (Current_Scope))
18486 then
18487 return False;
18488 else
18489 return
18490 Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
18491 and then In_Open_Scopes (Scope (Original_Type))
18492 and then Is_Local_Type (Type_Scope);
18493 end if;
18494
18495 -- There is another weird way in which a component may be invisible when
18496 -- the private and the full view are not derived from the same ancestor.
18497 -- Here is an example :
18498
18499 -- type A1 is tagged record F1 : integer; end record;
18500 -- type A2 is new A1 with record F2 : integer; end record;
18501 -- type T is new A1 with private;
18502 -- private
18503 -- type T is new A2 with null record;
18504
18505 -- In this case, the full view of T inherits F1 and F2 but the private
18506 -- view inherits only F1
18507
18508 else
18509 declare
18510 Ancestor : Entity_Id := Scope (C);
18511
18512 begin
18513 loop
18514 if Ancestor = Original_Type then
18515 return True;
18516
18517 -- The ancestor may have a partial view of the original type,
18518 -- but if the full view is in scope, as in a child body, the
18519 -- component is visible.
18520
18521 elsif In_Private_Part (Scope (Original_Type))
18522 and then Full_View (Ancestor) = Original_Type
18523 then
18524 return True;
18525
18526 elsif Ancestor = Etype (Ancestor) then
18527
18528 -- No further ancestors to examine
18529
18530 return False;
18531 end if;
18532
18533 Ancestor := Etype (Ancestor);
18534 end loop;
18535 end;
18536 end if;
18537 end Is_Visible_Component;
18538
18539 --------------------------
18540 -- Make_Class_Wide_Type --
18541 --------------------------
18542
18543 procedure Make_Class_Wide_Type (T : Entity_Id) is
18544 CW_Type : Entity_Id;
18545 CW_Name : Name_Id;
18546 Next_E : Entity_Id;
18547
18548 begin
18549 if Present (Class_Wide_Type (T)) then
18550
18551 -- The class-wide type is a partially decorated entity created for a
18552 -- unanalyzed tagged type referenced through a limited with clause.
18553 -- When the tagged type is analyzed, its class-wide type needs to be
18554 -- redecorated. Note that we reuse the entity created by Decorate_
18555 -- Tagged_Type in order to preserve all links.
18556
18557 if Materialize_Entity (Class_Wide_Type (T)) then
18558 CW_Type := Class_Wide_Type (T);
18559 Set_Materialize_Entity (CW_Type, False);
18560
18561 -- The class wide type can have been defined by the partial view, in
18562 -- which case everything is already done.
18563
18564 else
18565 return;
18566 end if;
18567
18568 -- Default case, we need to create a new class-wide type
18569
18570 else
18571 CW_Type :=
18572 New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
18573 end if;
18574
18575 -- Inherit root type characteristics
18576
18577 CW_Name := Chars (CW_Type);
18578 Next_E := Next_Entity (CW_Type);
18579 Copy_Node (T, CW_Type);
18580 Set_Comes_From_Source (CW_Type, False);
18581 Set_Chars (CW_Type, CW_Name);
18582 Set_Parent (CW_Type, Parent (T));
18583 Set_Next_Entity (CW_Type, Next_E);
18584
18585 -- Ensure we have a new freeze node for the class-wide type. The partial
18586 -- view may have freeze action of its own, requiring a proper freeze
18587 -- node, and the same freeze node cannot be shared between the two
18588 -- types.
18589
18590 Set_Has_Delayed_Freeze (CW_Type);
18591 Set_Freeze_Node (CW_Type, Empty);
18592
18593 -- Customize the class-wide type: It has no prim. op., it cannot be
18594 -- abstract, its Etype points back to the specific root type, and it
18595 -- cannot have any invariants.
18596
18597 Set_Ekind (CW_Type, E_Class_Wide_Type);
18598 Set_Is_Tagged_Type (CW_Type, True);
18599 Set_Direct_Primitive_Operations (CW_Type, New_Elmt_List);
18600 Set_Is_Abstract_Type (CW_Type, False);
18601 Set_Is_Constrained (CW_Type, False);
18602 Set_Is_First_Subtype (CW_Type, Is_First_Subtype (T));
18603 Set_Default_SSO (CW_Type);
18604 Set_Has_Inheritable_Invariants (CW_Type, False);
18605 Set_Has_Inherited_Invariants (CW_Type, False);
18606 Set_Has_Own_Invariants (CW_Type, False);
18607
18608 if Ekind (T) = E_Class_Wide_Subtype then
18609 Set_Etype (CW_Type, Etype (Base_Type (T)));
18610 else
18611 Set_Etype (CW_Type, T);
18612 end if;
18613
18614 Set_No_Tagged_Streams_Pragma (CW_Type, No_Tagged_Streams);
18615
18616 -- If this is the class_wide type of a constrained subtype, it does
18617 -- not have discriminants.
18618
18619 Set_Has_Discriminants (CW_Type,
18620 Has_Discriminants (T) and then not Is_Constrained (T));
18621
18622 Set_Has_Unknown_Discriminants (CW_Type, True);
18623 Set_Class_Wide_Type (T, CW_Type);
18624 Set_Equivalent_Type (CW_Type, Empty);
18625
18626 -- The class-wide type of a class-wide type is itself (RM 3.9(14))
18627
18628 Set_Class_Wide_Type (CW_Type, CW_Type);
18629 end Make_Class_Wide_Type;
18630
18631 ----------------
18632 -- Make_Index --
18633 ----------------
18634
18635 procedure Make_Index
18636 (N : Node_Id;
18637 Related_Nod : Node_Id;
18638 Related_Id : Entity_Id := Empty;
18639 Suffix_Index : Nat := 1;
18640 In_Iter_Schm : Boolean := False)
18641 is
18642 R : Node_Id;
18643 T : Entity_Id;
18644 Def_Id : Entity_Id := Empty;
18645 Found : Boolean := False;
18646
18647 begin
18648 -- For a discrete range used in a constrained array definition and
18649 -- defined by a range, an implicit conversion to the predefined type
18650 -- INTEGER is assumed if each bound is either a numeric literal, a named
18651 -- number, or an attribute, and the type of both bounds (prior to the
18652 -- implicit conversion) is the type universal_integer. Otherwise, both
18653 -- bounds must be of the same discrete type, other than universal
18654 -- integer; this type must be determinable independently of the
18655 -- context, but using the fact that the type must be discrete and that
18656 -- both bounds must have the same type.
18657
18658 -- Character literals also have a universal type in the absence of
18659 -- of additional context, and are resolved to Standard_Character.
18660
18661 if Nkind (N) = N_Range then
18662
18663 -- The index is given by a range constraint. The bounds are known
18664 -- to be of a consistent type.
18665
18666 if not Is_Overloaded (N) then
18667 T := Etype (N);
18668
18669 -- For universal bounds, choose the specific predefined type
18670
18671 if T = Universal_Integer then
18672 T := Standard_Integer;
18673
18674 elsif T = Any_Character then
18675 Ambiguous_Character (Low_Bound (N));
18676
18677 T := Standard_Character;
18678 end if;
18679
18680 -- The node may be overloaded because some user-defined operators
18681 -- are available, but if a universal interpretation exists it is
18682 -- also the selected one.
18683
18684 elsif Universal_Interpretation (N) = Universal_Integer then
18685 T := Standard_Integer;
18686
18687 else
18688 T := Any_Type;
18689
18690 declare
18691 Ind : Interp_Index;
18692 It : Interp;
18693
18694 begin
18695 Get_First_Interp (N, Ind, It);
18696 while Present (It.Typ) loop
18697 if Is_Discrete_Type (It.Typ) then
18698
18699 if Found
18700 and then not Covers (It.Typ, T)
18701 and then not Covers (T, It.Typ)
18702 then
18703 Error_Msg_N ("ambiguous bounds in discrete range", N);
18704 exit;
18705 else
18706 T := It.Typ;
18707 Found := True;
18708 end if;
18709 end if;
18710
18711 Get_Next_Interp (Ind, It);
18712 end loop;
18713
18714 if T = Any_Type then
18715 Error_Msg_N ("discrete type required for range", N);
18716 Set_Etype (N, Any_Type);
18717 return;
18718
18719 elsif T = Universal_Integer then
18720 T := Standard_Integer;
18721 end if;
18722 end;
18723 end if;
18724
18725 if not Is_Discrete_Type (T) then
18726 Error_Msg_N ("discrete type required for range", N);
18727 Set_Etype (N, Any_Type);
18728 return;
18729 end if;
18730
18731 if Nkind (Low_Bound (N)) = N_Attribute_Reference
18732 and then Attribute_Name (Low_Bound (N)) = Name_First
18733 and then Is_Entity_Name (Prefix (Low_Bound (N)))
18734 and then Is_Type (Entity (Prefix (Low_Bound (N))))
18735 and then Is_Discrete_Type (Entity (Prefix (Low_Bound (N))))
18736 then
18737 -- The type of the index will be the type of the prefix, as long
18738 -- as the upper bound is 'Last of the same type.
18739
18740 Def_Id := Entity (Prefix (Low_Bound (N)));
18741
18742 if Nkind (High_Bound (N)) /= N_Attribute_Reference
18743 or else Attribute_Name (High_Bound (N)) /= Name_Last
18744 or else not Is_Entity_Name (Prefix (High_Bound (N)))
18745 or else Entity (Prefix (High_Bound (N))) /= Def_Id
18746 then
18747 Def_Id := Empty;
18748 end if;
18749 end if;
18750
18751 R := N;
18752 Process_Range_Expr_In_Decl (R, T, In_Iter_Schm => In_Iter_Schm);
18753
18754 elsif Nkind (N) = N_Subtype_Indication then
18755
18756 -- The index is given by a subtype with a range constraint
18757
18758 T := Base_Type (Entity (Subtype_Mark (N)));
18759
18760 if not Is_Discrete_Type (T) then
18761 Error_Msg_N ("discrete type required for range", N);
18762 Set_Etype (N, Any_Type);
18763 return;
18764 end if;
18765
18766 R := Range_Expression (Constraint (N));
18767
18768 Resolve (R, T);
18769 Process_Range_Expr_In_Decl
18770 (R, Entity (Subtype_Mark (N)), In_Iter_Schm => In_Iter_Schm);
18771
18772 elsif Nkind (N) = N_Attribute_Reference then
18773
18774 -- Catch beginner's error (use of attribute other than 'Range)
18775
18776 if Attribute_Name (N) /= Name_Range then
18777 Error_Msg_N ("expect attribute ''Range", N);
18778 Set_Etype (N, Any_Type);
18779 return;
18780 end if;
18781
18782 -- If the node denotes the range of a type mark, that is also the
18783 -- resulting type, and we do not need to create an Itype for it.
18784
18785 if Is_Entity_Name (Prefix (N))
18786 and then Comes_From_Source (N)
18787 and then Is_Type (Entity (Prefix (N)))
18788 and then Is_Discrete_Type (Entity (Prefix (N)))
18789 then
18790 Def_Id := Entity (Prefix (N));
18791 end if;
18792
18793 Analyze_And_Resolve (N);
18794 T := Etype (N);
18795 R := N;
18796
18797 -- If none of the above, must be a subtype. We convert this to a
18798 -- range attribute reference because in the case of declared first
18799 -- named subtypes, the types in the range reference can be different
18800 -- from the type of the entity. A range attribute normalizes the
18801 -- reference and obtains the correct types for the bounds.
18802
18803 -- This transformation is in the nature of an expansion, is only
18804 -- done if expansion is active. In particular, it is not done on
18805 -- formal generic types, because we need to retain the name of the
18806 -- original index for instantiation purposes.
18807
18808 else
18809 if not Is_Entity_Name (N) or else not Is_Type (Entity (N)) then
18810 Error_Msg_N ("invalid subtype mark in discrete range ", N);
18811 Set_Etype (N, Any_Integer);
18812 return;
18813
18814 else
18815 -- The type mark may be that of an incomplete type. It is only
18816 -- now that we can get the full view, previous analysis does
18817 -- not look specifically for a type mark.
18818
18819 Set_Entity (N, Get_Full_View (Entity (N)));
18820 Set_Etype (N, Entity (N));
18821 Def_Id := Entity (N);
18822
18823 if not Is_Discrete_Type (Def_Id) then
18824 Error_Msg_N ("discrete type required for index", N);
18825 Set_Etype (N, Any_Type);
18826 return;
18827 end if;
18828 end if;
18829
18830 if Expander_Active then
18831 Rewrite (N,
18832 Make_Attribute_Reference (Sloc (N),
18833 Attribute_Name => Name_Range,
18834 Prefix => Relocate_Node (N)));
18835
18836 -- The original was a subtype mark that does not freeze. This
18837 -- means that the rewritten version must not freeze either.
18838
18839 Set_Must_Not_Freeze (N);
18840 Set_Must_Not_Freeze (Prefix (N));
18841 Analyze_And_Resolve (N);
18842 T := Etype (N);
18843 R := N;
18844
18845 -- If expander is inactive, type is legal, nothing else to construct
18846
18847 else
18848 return;
18849 end if;
18850 end if;
18851
18852 if not Is_Discrete_Type (T) then
18853 Error_Msg_N ("discrete type required for range", N);
18854 Set_Etype (N, Any_Type);
18855 return;
18856
18857 elsif T = Any_Type then
18858 Set_Etype (N, Any_Type);
18859 return;
18860 end if;
18861
18862 -- We will now create the appropriate Itype to describe the range, but
18863 -- first a check. If we originally had a subtype, then we just label
18864 -- the range with this subtype. Not only is there no need to construct
18865 -- a new subtype, but it is wrong to do so for two reasons:
18866
18867 -- 1. A legality concern, if we have a subtype, it must not freeze,
18868 -- and the Itype would cause freezing incorrectly
18869
18870 -- 2. An efficiency concern, if we created an Itype, it would not be
18871 -- recognized as the same type for the purposes of eliminating
18872 -- checks in some circumstances.
18873
18874 -- We signal this case by setting the subtype entity in Def_Id
18875
18876 if No (Def_Id) then
18877 Def_Id :=
18878 Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
18879 Set_Etype (Def_Id, Base_Type (T));
18880
18881 if Is_Signed_Integer_Type (T) then
18882 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
18883
18884 elsif Is_Modular_Integer_Type (T) then
18885 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
18886
18887 else
18888 Set_Ekind (Def_Id, E_Enumeration_Subtype);
18889 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
18890 Set_First_Literal (Def_Id, First_Literal (T));
18891 end if;
18892
18893 Set_Size_Info (Def_Id, (T));
18894 Set_RM_Size (Def_Id, RM_Size (T));
18895 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
18896
18897 Set_Scalar_Range (Def_Id, R);
18898 Conditional_Delay (Def_Id, T);
18899
18900 if Nkind (N) = N_Subtype_Indication then
18901 Inherit_Predicate_Flags (Def_Id, Entity (Subtype_Mark (N)));
18902 end if;
18903
18904 -- In the subtype indication case, if the immediate parent of the
18905 -- new subtype is non-static, then the subtype we create is non-
18906 -- static, even if its bounds are static.
18907
18908 if Nkind (N) = N_Subtype_Indication
18909 and then not Is_OK_Static_Subtype (Entity (Subtype_Mark (N)))
18910 then
18911 Set_Is_Non_Static_Subtype (Def_Id);
18912 end if;
18913 end if;
18914
18915 -- Final step is to label the index with this constructed type
18916
18917 Set_Etype (N, Def_Id);
18918 end Make_Index;
18919
18920 ------------------------------
18921 -- Modular_Type_Declaration --
18922 ------------------------------
18923
18924 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
18925 Mod_Expr : constant Node_Id := Expression (Def);
18926 M_Val : Uint;
18927
18928 procedure Set_Modular_Size (Bits : Int);
18929 -- Sets RM_Size to Bits, and Esize to normal word size above this
18930
18931 ----------------------
18932 -- Set_Modular_Size --
18933 ----------------------
18934
18935 procedure Set_Modular_Size (Bits : Int) is
18936 begin
18937 Set_RM_Size (T, UI_From_Int (Bits));
18938
18939 if Bits <= 8 then
18940 Init_Esize (T, 8);
18941
18942 elsif Bits <= 16 then
18943 Init_Esize (T, 16);
18944
18945 elsif Bits <= 32 then
18946 Init_Esize (T, 32);
18947
18948 else
18949 Init_Esize (T, System_Max_Binary_Modulus_Power);
18950 end if;
18951
18952 if not Non_Binary_Modulus (T) and then Esize (T) = RM_Size (T) then
18953 Set_Is_Known_Valid (T);
18954 end if;
18955 end Set_Modular_Size;
18956
18957 -- Start of processing for Modular_Type_Declaration
18958
18959 begin
18960 -- If the mod expression is (exactly) 2 * literal, where literal is
18961 -- 64 or less,then almost certainly the * was meant to be **. Warn.
18962
18963 if Warn_On_Suspicious_Modulus_Value
18964 and then Nkind (Mod_Expr) = N_Op_Multiply
18965 and then Nkind (Left_Opnd (Mod_Expr)) = N_Integer_Literal
18966 and then Intval (Left_Opnd (Mod_Expr)) = Uint_2
18967 and then Nkind (Right_Opnd (Mod_Expr)) = N_Integer_Literal
18968 and then Intval (Right_Opnd (Mod_Expr)) <= Uint_64
18969 then
18970 Error_Msg_N
18971 ("suspicious MOD value, was '*'* intended'??M?", Mod_Expr);
18972 end if;
18973
18974 -- Proceed with analysis of mod expression
18975
18976 Analyze_And_Resolve (Mod_Expr, Any_Integer);
18977 Set_Etype (T, T);
18978 Set_Ekind (T, E_Modular_Integer_Type);
18979 Init_Alignment (T);
18980 Set_Is_Constrained (T);
18981
18982 if not Is_OK_Static_Expression (Mod_Expr) then
18983 Flag_Non_Static_Expr
18984 ("non-static expression used for modular type bound!", Mod_Expr);
18985 M_Val := 2 ** System_Max_Binary_Modulus_Power;
18986 else
18987 M_Val := Expr_Value (Mod_Expr);
18988 end if;
18989
18990 if M_Val < 1 then
18991 Error_Msg_N ("modulus value must be positive", Mod_Expr);
18992 M_Val := 2 ** System_Max_Binary_Modulus_Power;
18993 end if;
18994
18995 if M_Val > 2 ** Standard_Long_Integer_Size then
18996 Check_Restriction (No_Long_Long_Integers, Mod_Expr);
18997 end if;
18998
18999 Set_Modulus (T, M_Val);
19000
19001 -- Create bounds for the modular type based on the modulus given in
19002 -- the type declaration and then analyze and resolve those bounds.
19003
19004 Set_Scalar_Range (T,
19005 Make_Range (Sloc (Mod_Expr),
19006 Low_Bound => Make_Integer_Literal (Sloc (Mod_Expr), 0),
19007 High_Bound => Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
19008
19009 -- Properly analyze the literals for the range. We do this manually
19010 -- because we can't go calling Resolve, since we are resolving these
19011 -- bounds with the type, and this type is certainly not complete yet.
19012
19013 Set_Etype (Low_Bound (Scalar_Range (T)), T);
19014 Set_Etype (High_Bound (Scalar_Range (T)), T);
19015 Set_Is_Static_Expression (Low_Bound (Scalar_Range (T)));
19016 Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
19017
19018 -- Loop through powers of two to find number of bits required
19019
19020 for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
19021
19022 -- Binary case
19023
19024 if M_Val = 2 ** Bits then
19025 Set_Modular_Size (Bits);
19026 return;
19027
19028 -- Nonbinary case
19029
19030 elsif M_Val < 2 ** Bits then
19031 Check_SPARK_05_Restriction ("modulus should be a power of 2", T);
19032 Set_Non_Binary_Modulus (T);
19033
19034 if Bits > System_Max_Nonbinary_Modulus_Power then
19035 Error_Msg_Uint_1 :=
19036 UI_From_Int (System_Max_Nonbinary_Modulus_Power);
19037 Error_Msg_F
19038 ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
19039 Set_Modular_Size (System_Max_Binary_Modulus_Power);
19040 return;
19041
19042 else
19043 -- In the nonbinary case, set size as per RM 13.3(55)
19044
19045 Set_Modular_Size (Bits);
19046 return;
19047 end if;
19048 end if;
19049
19050 end loop;
19051
19052 -- If we fall through, then the size exceed System.Max_Binary_Modulus
19053 -- so we just signal an error and set the maximum size.
19054
19055 Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
19056 Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
19057
19058 Set_Modular_Size (System_Max_Binary_Modulus_Power);
19059 Init_Alignment (T);
19060
19061 end Modular_Type_Declaration;
19062
19063 --------------------------
19064 -- New_Concatenation_Op --
19065 --------------------------
19066
19067 procedure New_Concatenation_Op (Typ : Entity_Id) is
19068 Loc : constant Source_Ptr := Sloc (Typ);
19069 Op : Entity_Id;
19070
19071 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
19072 -- Create abbreviated declaration for the formal of a predefined
19073 -- Operator 'Op' of type 'Typ'
19074
19075 --------------------
19076 -- Make_Op_Formal --
19077 --------------------
19078
19079 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
19080 Formal : Entity_Id;
19081 begin
19082 Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
19083 Set_Etype (Formal, Typ);
19084 Set_Mechanism (Formal, Default_Mechanism);
19085 return Formal;
19086 end Make_Op_Formal;
19087
19088 -- Start of processing for New_Concatenation_Op
19089
19090 begin
19091 Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
19092
19093 Set_Ekind (Op, E_Operator);
19094 Set_Scope (Op, Current_Scope);
19095 Set_Etype (Op, Typ);
19096 Set_Homonym (Op, Get_Name_Entity_Id (Name_Op_Concat));
19097 Set_Is_Immediately_Visible (Op);
19098 Set_Is_Intrinsic_Subprogram (Op);
19099 Set_Has_Completion (Op);
19100 Append_Entity (Op, Current_Scope);
19101
19102 Set_Name_Entity_Id (Name_Op_Concat, Op);
19103
19104 Append_Entity (Make_Op_Formal (Typ, Op), Op);
19105 Append_Entity (Make_Op_Formal (Typ, Op), Op);
19106 end New_Concatenation_Op;
19107
19108 -------------------------
19109 -- OK_For_Limited_Init --
19110 -------------------------
19111
19112 -- ???Check all calls of this, and compare the conditions under which it's
19113 -- called.
19114
19115 function OK_For_Limited_Init
19116 (Typ : Entity_Id;
19117 Exp : Node_Id) return Boolean
19118 is
19119 begin
19120 return Is_CPP_Constructor_Call (Exp)
19121 or else (Ada_Version >= Ada_2005
19122 and then not Debug_Flag_Dot_L
19123 and then OK_For_Limited_Init_In_05 (Typ, Exp));
19124 end OK_For_Limited_Init;
19125
19126 -------------------------------
19127 -- OK_For_Limited_Init_In_05 --
19128 -------------------------------
19129
19130 function OK_For_Limited_Init_In_05
19131 (Typ : Entity_Id;
19132 Exp : Node_Id) return Boolean
19133 is
19134 begin
19135 -- An object of a limited interface type can be initialized with any
19136 -- expression of a nonlimited descendant type. However this does not
19137 -- apply if this is a view conversion of some other expression. This
19138 -- is checked below.
19139
19140 if Is_Class_Wide_Type (Typ)
19141 and then Is_Limited_Interface (Typ)
19142 and then not Is_Limited_Type (Etype (Exp))
19143 and then Nkind (Exp) /= N_Type_Conversion
19144 then
19145 return True;
19146 end if;
19147
19148 -- Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
19149 -- case of limited aggregates (including extension aggregates), and
19150 -- function calls. The function call may have been given in prefixed
19151 -- notation, in which case the original node is an indexed component.
19152 -- If the function is parameterless, the original node was an explicit
19153 -- dereference. The function may also be parameterless, in which case
19154 -- the source node is just an identifier.
19155
19156 -- A branch of a conditional expression may have been removed if the
19157 -- condition is statically known. This happens during expansion, and
19158 -- thus will not happen if previous errors were encountered. The check
19159 -- will have been performed on the chosen branch, which replaces the
19160 -- original conditional expression.
19161
19162 if No (Exp) then
19163 return True;
19164 end if;
19165
19166 case Nkind (Original_Node (Exp)) is
19167 when N_Aggregate
19168 | N_Extension_Aggregate
19169 | N_Function_Call
19170 | N_Op
19171 =>
19172 return True;
19173
19174 when N_Identifier =>
19175 return Present (Entity (Original_Node (Exp)))
19176 and then Ekind (Entity (Original_Node (Exp))) = E_Function;
19177
19178 when N_Qualified_Expression =>
19179 return
19180 OK_For_Limited_Init_In_05
19181 (Typ, Expression (Original_Node (Exp)));
19182
19183 -- Ada 2005 (AI-251): If a class-wide interface object is initialized
19184 -- with a function call, the expander has rewritten the call into an
19185 -- N_Type_Conversion node to force displacement of the pointer to
19186 -- reference the component containing the secondary dispatch table.
19187 -- Otherwise a type conversion is not a legal context.
19188 -- A return statement for a build-in-place function returning a
19189 -- synchronized type also introduces an unchecked conversion.
19190
19191 when N_Type_Conversion
19192 | N_Unchecked_Type_Conversion
19193 =>
19194 return not Comes_From_Source (Exp)
19195 and then
19196 OK_For_Limited_Init_In_05
19197 (Typ, Expression (Original_Node (Exp)));
19198
19199 when N_Explicit_Dereference
19200 | N_Indexed_Component
19201 | N_Selected_Component
19202 =>
19203 return Nkind (Exp) = N_Function_Call;
19204
19205 -- A use of 'Input is a function call, hence allowed. Normally the
19206 -- attribute will be changed to a call, but the attribute by itself
19207 -- can occur with -gnatc.
19208
19209 when N_Attribute_Reference =>
19210 return Attribute_Name (Original_Node (Exp)) = Name_Input;
19211
19212 -- For a case expression, all dependent expressions must be legal
19213
19214 when N_Case_Expression =>
19215 declare
19216 Alt : Node_Id;
19217
19218 begin
19219 Alt := First (Alternatives (Original_Node (Exp)));
19220 while Present (Alt) loop
19221 if not OK_For_Limited_Init_In_05 (Typ, Expression (Alt)) then
19222 return False;
19223 end if;
19224
19225 Next (Alt);
19226 end loop;
19227
19228 return True;
19229 end;
19230
19231 -- For an if expression, all dependent expressions must be legal
19232
19233 when N_If_Expression =>
19234 declare
19235 Then_Expr : constant Node_Id :=
19236 Next (First (Expressions (Original_Node (Exp))));
19237 Else_Expr : constant Node_Id := Next (Then_Expr);
19238 begin
19239 return OK_For_Limited_Init_In_05 (Typ, Then_Expr)
19240 and then
19241 OK_For_Limited_Init_In_05 (Typ, Else_Expr);
19242 end;
19243
19244 when others =>
19245 return False;
19246 end case;
19247 end OK_For_Limited_Init_In_05;
19248
19249 -------------------------------------------
19250 -- Ordinary_Fixed_Point_Type_Declaration --
19251 -------------------------------------------
19252
19253 procedure Ordinary_Fixed_Point_Type_Declaration
19254 (T : Entity_Id;
19255 Def : Node_Id)
19256 is
19257 Loc : constant Source_Ptr := Sloc (Def);
19258 Delta_Expr : constant Node_Id := Delta_Expression (Def);
19259 RRS : constant Node_Id := Real_Range_Specification (Def);
19260 Implicit_Base : Entity_Id;
19261 Delta_Val : Ureal;
19262 Small_Val : Ureal;
19263 Low_Val : Ureal;
19264 High_Val : Ureal;
19265
19266 begin
19267 Check_Restriction (No_Fixed_Point, Def);
19268
19269 -- Create implicit base type
19270
19271 Implicit_Base :=
19272 Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
19273 Set_Etype (Implicit_Base, Implicit_Base);
19274
19275 -- Analyze and process delta expression
19276
19277 Analyze_And_Resolve (Delta_Expr, Any_Real);
19278
19279 Check_Delta_Expression (Delta_Expr);
19280 Delta_Val := Expr_Value_R (Delta_Expr);
19281
19282 Set_Delta_Value (Implicit_Base, Delta_Val);
19283
19284 -- Compute default small from given delta, which is the largest power
19285 -- of two that does not exceed the given delta value.
19286
19287 declare
19288 Tmp : Ureal;
19289 Scale : Int;
19290
19291 begin
19292 Tmp := Ureal_1;
19293 Scale := 0;
19294
19295 if Delta_Val < Ureal_1 then
19296 while Delta_Val < Tmp loop
19297 Tmp := Tmp / Ureal_2;
19298 Scale := Scale + 1;
19299 end loop;
19300
19301 else
19302 loop
19303 Tmp := Tmp * Ureal_2;
19304 exit when Tmp > Delta_Val;
19305 Scale := Scale - 1;
19306 end loop;
19307 end if;
19308
19309 Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
19310 end;
19311
19312 Set_Small_Value (Implicit_Base, Small_Val);
19313
19314 -- If no range was given, set a dummy range
19315
19316 if RRS <= Empty_Or_Error then
19317 Low_Val := -Small_Val;
19318 High_Val := Small_Val;
19319
19320 -- Otherwise analyze and process given range
19321
19322 else
19323 declare
19324 Low : constant Node_Id := Low_Bound (RRS);
19325 High : constant Node_Id := High_Bound (RRS);
19326
19327 begin
19328 Analyze_And_Resolve (Low, Any_Real);
19329 Analyze_And_Resolve (High, Any_Real);
19330 Check_Real_Bound (Low);
19331 Check_Real_Bound (High);
19332
19333 -- Obtain and set the range
19334
19335 Low_Val := Expr_Value_R (Low);
19336 High_Val := Expr_Value_R (High);
19337
19338 if Low_Val > High_Val then
19339 Error_Msg_NE ("??fixed point type& has null range", Def, T);
19340 end if;
19341 end;
19342 end if;
19343
19344 -- The range for both the implicit base and the declared first subtype
19345 -- cannot be set yet, so we use the special routine Set_Fixed_Range to
19346 -- set a temporary range in place. Note that the bounds of the base
19347 -- type will be widened to be symmetrical and to fill the available
19348 -- bits when the type is frozen.
19349
19350 -- We could do this with all discrete types, and probably should, but
19351 -- we absolutely have to do it for fixed-point, since the end-points
19352 -- of the range and the size are determined by the small value, which
19353 -- could be reset before the freeze point.
19354
19355 Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
19356 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
19357
19358 -- Complete definition of first subtype. The inheritance of the rep item
19359 -- chain ensures that SPARK-related pragmas are not clobbered when the
19360 -- ordinary fixed point type acts as a full view of a private type.
19361
19362 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
19363 Set_Etype (T, Implicit_Base);
19364 Init_Size_Align (T);
19365 Inherit_Rep_Item_Chain (T, Implicit_Base);
19366 Set_Small_Value (T, Small_Val);
19367 Set_Delta_Value (T, Delta_Val);
19368 Set_Is_Constrained (T);
19369 end Ordinary_Fixed_Point_Type_Declaration;
19370
19371 ----------------------------------
19372 -- Preanalyze_Assert_Expression --
19373 ----------------------------------
19374
19375 procedure Preanalyze_Assert_Expression (N : Node_Id; T : Entity_Id) is
19376 begin
19377 In_Assertion_Expr := In_Assertion_Expr + 1;
19378 Preanalyze_Spec_Expression (N, T);
19379 In_Assertion_Expr := In_Assertion_Expr - 1;
19380 end Preanalyze_Assert_Expression;
19381
19382 -----------------------------------
19383 -- Preanalyze_Default_Expression --
19384 -----------------------------------
19385
19386 procedure Preanalyze_Default_Expression (N : Node_Id; T : Entity_Id) is
19387 Save_In_Default_Expr : constant Boolean := In_Default_Expr;
19388 begin
19389 In_Default_Expr := True;
19390 Preanalyze_Spec_Expression (N, T);
19391 In_Default_Expr := Save_In_Default_Expr;
19392 end Preanalyze_Default_Expression;
19393
19394 --------------------------------
19395 -- Preanalyze_Spec_Expression --
19396 --------------------------------
19397
19398 procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
19399 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
19400 begin
19401 In_Spec_Expression := True;
19402 Preanalyze_And_Resolve (N, T);
19403 In_Spec_Expression := Save_In_Spec_Expression;
19404 end Preanalyze_Spec_Expression;
19405
19406 ----------------------------------------
19407 -- Prepare_Private_Subtype_Completion --
19408 ----------------------------------------
19409
19410 procedure Prepare_Private_Subtype_Completion
19411 (Id : Entity_Id;
19412 Related_Nod : Node_Id)
19413 is
19414 Id_B : constant Entity_Id := Base_Type (Id);
19415 Full_B : Entity_Id := Full_View (Id_B);
19416 Full : Entity_Id;
19417
19418 begin
19419 if Present (Full_B) then
19420
19421 -- Get to the underlying full view if necessary
19422
19423 if Is_Private_Type (Full_B)
19424 and then Present (Underlying_Full_View (Full_B))
19425 then
19426 Full_B := Underlying_Full_View (Full_B);
19427 end if;
19428
19429 -- The Base_Type is already completed, we can complete the subtype
19430 -- now. We have to create a new entity with the same name, Thus we
19431 -- can't use Create_Itype.
19432
19433 Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
19434 Set_Is_Itype (Full);
19435 Set_Associated_Node_For_Itype (Full, Related_Nod);
19436 Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
19437 end if;
19438
19439 -- The parent subtype may be private, but the base might not, in some
19440 -- nested instances. In that case, the subtype does not need to be
19441 -- exchanged. It would still be nice to make private subtypes and their
19442 -- bases consistent at all times ???
19443
19444 if Is_Private_Type (Id_B) then
19445 Append_Elmt (Id, Private_Dependents (Id_B));
19446 end if;
19447 end Prepare_Private_Subtype_Completion;
19448
19449 ---------------------------
19450 -- Process_Discriminants --
19451 ---------------------------
19452
19453 procedure Process_Discriminants
19454 (N : Node_Id;
19455 Prev : Entity_Id := Empty)
19456 is
19457 Elist : constant Elist_Id := New_Elmt_List;
19458 Id : Node_Id;
19459 Discr : Node_Id;
19460 Discr_Number : Uint;
19461 Discr_Type : Entity_Id;
19462 Default_Present : Boolean := False;
19463 Default_Not_Present : Boolean := False;
19464
19465 begin
19466 -- A composite type other than an array type can have discriminants.
19467 -- On entry, the current scope is the composite type.
19468
19469 -- The discriminants are initially entered into the scope of the type
19470 -- via Enter_Name with the default Ekind of E_Void to prevent premature
19471 -- use, as explained at the end of this procedure.
19472
19473 Discr := First (Discriminant_Specifications (N));
19474 while Present (Discr) loop
19475 Enter_Name (Defining_Identifier (Discr));
19476
19477 -- For navigation purposes we add a reference to the discriminant
19478 -- in the entity for the type. If the current declaration is a
19479 -- completion, place references on the partial view. Otherwise the
19480 -- type is the current scope.
19481
19482 if Present (Prev) then
19483
19484 -- The references go on the partial view, if present. If the
19485 -- partial view has discriminants, the references have been
19486 -- generated already.
19487
19488 if not Has_Discriminants (Prev) then
19489 Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
19490 end if;
19491 else
19492 Generate_Reference
19493 (Current_Scope, Defining_Identifier (Discr), 'd');
19494 end if;
19495
19496 if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
19497 Discr_Type := Access_Definition (Discr, Discriminant_Type (Discr));
19498
19499 -- Ada 2005 (AI-254)
19500
19501 if Present (Access_To_Subprogram_Definition
19502 (Discriminant_Type (Discr)))
19503 and then Protected_Present (Access_To_Subprogram_Definition
19504 (Discriminant_Type (Discr)))
19505 then
19506 Discr_Type :=
19507 Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
19508 end if;
19509
19510 else
19511 Find_Type (Discriminant_Type (Discr));
19512 Discr_Type := Etype (Discriminant_Type (Discr));
19513
19514 if Error_Posted (Discriminant_Type (Discr)) then
19515 Discr_Type := Any_Type;
19516 end if;
19517 end if;
19518
19519 -- Handling of discriminants that are access types
19520
19521 if Is_Access_Type (Discr_Type) then
19522
19523 -- Ada 2005 (AI-230): Access discriminant allowed in non-
19524 -- limited record types
19525
19526 if Ada_Version < Ada_2005 then
19527 Check_Access_Discriminant_Requires_Limited
19528 (Discr, Discriminant_Type (Discr));
19529 end if;
19530
19531 if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
19532 Error_Msg_N
19533 ("(Ada 83) access discriminant not allowed", Discr);
19534 end if;
19535
19536 -- If not access type, must be a discrete type
19537
19538 elsif not Is_Discrete_Type (Discr_Type) then
19539 Error_Msg_N
19540 ("discriminants must have a discrete or access type",
19541 Discriminant_Type (Discr));
19542 end if;
19543
19544 Set_Etype (Defining_Identifier (Discr), Discr_Type);
19545
19546 -- If a discriminant specification includes the assignment compound
19547 -- delimiter followed by an expression, the expression is the default
19548 -- expression of the discriminant; the default expression must be of
19549 -- the type of the discriminant. (RM 3.7.1) Since this expression is
19550 -- a default expression, we do the special preanalysis, since this
19551 -- expression does not freeze (see section "Handling of Default and
19552 -- Per-Object Expressions" in spec of package Sem).
19553
19554 if Present (Expression (Discr)) then
19555 Preanalyze_Spec_Expression (Expression (Discr), Discr_Type);
19556
19557 -- Legaity checks
19558
19559 if Nkind (N) = N_Formal_Type_Declaration then
19560 Error_Msg_N
19561 ("discriminant defaults not allowed for formal type",
19562 Expression (Discr));
19563
19564 -- Flag an error for a tagged type with defaulted discriminants,
19565 -- excluding limited tagged types when compiling for Ada 2012
19566 -- (see AI05-0214).
19567
19568 elsif Is_Tagged_Type (Current_Scope)
19569 and then (not Is_Limited_Type (Current_Scope)
19570 or else Ada_Version < Ada_2012)
19571 and then Comes_From_Source (N)
19572 then
19573 -- Note: see similar test in Check_Or_Process_Discriminants, to
19574 -- handle the (illegal) case of the completion of an untagged
19575 -- view with discriminants with defaults by a tagged full view.
19576 -- We skip the check if Discr does not come from source, to
19577 -- account for the case of an untagged derived type providing
19578 -- defaults for a renamed discriminant from a private untagged
19579 -- ancestor with a tagged full view (ACATS B460006).
19580
19581 if Ada_Version >= Ada_2012 then
19582 Error_Msg_N
19583 ("discriminants of nonlimited tagged type cannot have"
19584 & " defaults",
19585 Expression (Discr));
19586 else
19587 Error_Msg_N
19588 ("discriminants of tagged type cannot have defaults",
19589 Expression (Discr));
19590 end if;
19591
19592 else
19593 Default_Present := True;
19594 Append_Elmt (Expression (Discr), Elist);
19595
19596 -- Tag the defining identifiers for the discriminants with
19597 -- their corresponding default expressions from the tree.
19598
19599 Set_Discriminant_Default_Value
19600 (Defining_Identifier (Discr), Expression (Discr));
19601 end if;
19602
19603 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag
19604 -- gets set unless we can be sure that no range check is required.
19605
19606 if (GNATprove_Mode or not Expander_Active)
19607 and then not
19608 Is_In_Range
19609 (Expression (Discr), Discr_Type, Assume_Valid => True)
19610 then
19611 Set_Do_Range_Check (Expression (Discr));
19612 end if;
19613
19614 -- No default discriminant value given
19615
19616 else
19617 Default_Not_Present := True;
19618 end if;
19619
19620 -- Ada 2005 (AI-231): Create an Itype that is a duplicate of
19621 -- Discr_Type but with the null-exclusion attribute
19622
19623 if Ada_Version >= Ada_2005 then
19624
19625 -- Ada 2005 (AI-231): Static checks
19626
19627 if Can_Never_Be_Null (Discr_Type) then
19628 Null_Exclusion_Static_Checks (Discr);
19629
19630 elsif Is_Access_Type (Discr_Type)
19631 and then Null_Exclusion_Present (Discr)
19632
19633 -- No need to check itypes because in their case this check
19634 -- was done at their point of creation
19635
19636 and then not Is_Itype (Discr_Type)
19637 then
19638 if Can_Never_Be_Null (Discr_Type) then
19639 Error_Msg_NE
19640 ("`NOT NULL` not allowed (& already excludes null)",
19641 Discr,
19642 Discr_Type);
19643 end if;
19644
19645 Set_Etype (Defining_Identifier (Discr),
19646 Create_Null_Excluding_Itype
19647 (T => Discr_Type,
19648 Related_Nod => Discr));
19649
19650 -- Check for improper null exclusion if the type is otherwise
19651 -- legal for a discriminant.
19652
19653 elsif Null_Exclusion_Present (Discr)
19654 and then Is_Discrete_Type (Discr_Type)
19655 then
19656 Error_Msg_N
19657 ("null exclusion can only apply to an access type", Discr);
19658 end if;
19659
19660 -- Ada 2005 (AI-402): access discriminants of nonlimited types
19661 -- can't have defaults. Synchronized types, or types that are
19662 -- explicitly limited are fine, but special tests apply to derived
19663 -- types in generics: in a generic body we have to assume the
19664 -- worst, and therefore defaults are not allowed if the parent is
19665 -- a generic formal private type (see ACATS B370001).
19666
19667 if Is_Access_Type (Discr_Type) and then Default_Present then
19668 if Ekind (Discr_Type) /= E_Anonymous_Access_Type
19669 or else Is_Limited_Record (Current_Scope)
19670 or else Is_Concurrent_Type (Current_Scope)
19671 or else Is_Concurrent_Record_Type (Current_Scope)
19672 or else Ekind (Current_Scope) = E_Limited_Private_Type
19673 then
19674 if not Is_Derived_Type (Current_Scope)
19675 or else not Is_Generic_Type (Etype (Current_Scope))
19676 or else not In_Package_Body (Scope (Etype (Current_Scope)))
19677 or else Limited_Present
19678 (Type_Definition (Parent (Current_Scope)))
19679 then
19680 null;
19681
19682 else
19683 Error_Msg_N
19684 ("access discriminants of nonlimited types cannot "
19685 & "have defaults", Expression (Discr));
19686 end if;
19687
19688 elsif Present (Expression (Discr)) then
19689 Error_Msg_N
19690 ("(Ada 2005) access discriminants of nonlimited types "
19691 & "cannot have defaults", Expression (Discr));
19692 end if;
19693 end if;
19694 end if;
19695
19696 -- A discriminant cannot be effectively volatile (SPARK RM 7.1.3(6)).
19697 -- This check is relevant only when SPARK_Mode is on as it is not a
19698 -- standard Ada legality rule.
19699
19700 if SPARK_Mode = On
19701 and then Is_Effectively_Volatile (Defining_Identifier (Discr))
19702 then
19703 Error_Msg_N ("discriminant cannot be volatile", Discr);
19704 end if;
19705
19706 Next (Discr);
19707 end loop;
19708
19709 -- An element list consisting of the default expressions of the
19710 -- discriminants is constructed in the above loop and used to set
19711 -- the Discriminant_Constraint attribute for the type. If an object
19712 -- is declared of this (record or task) type without any explicit
19713 -- discriminant constraint given, this element list will form the
19714 -- actual parameters for the corresponding initialization procedure
19715 -- for the type.
19716
19717 Set_Discriminant_Constraint (Current_Scope, Elist);
19718 Set_Stored_Constraint (Current_Scope, No_Elist);
19719
19720 -- Default expressions must be provided either for all or for none
19721 -- of the discriminants of a discriminant part. (RM 3.7.1)
19722
19723 if Default_Present and then Default_Not_Present then
19724 Error_Msg_N
19725 ("incomplete specification of defaults for discriminants", N);
19726 end if;
19727
19728 -- The use of the name of a discriminant is not allowed in default
19729 -- expressions of a discriminant part if the specification of the
19730 -- discriminant is itself given in the discriminant part. (RM 3.7.1)
19731
19732 -- To detect this, the discriminant names are entered initially with an
19733 -- Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
19734 -- attempt to use a void entity (for example in an expression that is
19735 -- type-checked) produces the error message: premature usage. Now after
19736 -- completing the semantic analysis of the discriminant part, we can set
19737 -- the Ekind of all the discriminants appropriately.
19738
19739 Discr := First (Discriminant_Specifications (N));
19740 Discr_Number := Uint_1;
19741 while Present (Discr) loop
19742 Id := Defining_Identifier (Discr);
19743 Set_Ekind (Id, E_Discriminant);
19744 Init_Component_Location (Id);
19745 Init_Esize (Id);
19746 Set_Discriminant_Number (Id, Discr_Number);
19747
19748 -- Make sure this is always set, even in illegal programs
19749
19750 Set_Corresponding_Discriminant (Id, Empty);
19751
19752 -- Initialize the Original_Record_Component to the entity itself.
19753 -- Inherit_Components will propagate the right value to
19754 -- discriminants in derived record types.
19755
19756 Set_Original_Record_Component (Id, Id);
19757
19758 -- Create the discriminal for the discriminant
19759
19760 Build_Discriminal (Id);
19761
19762 Next (Discr);
19763 Discr_Number := Discr_Number + 1;
19764 end loop;
19765
19766 Set_Has_Discriminants (Current_Scope);
19767 end Process_Discriminants;
19768
19769 -----------------------
19770 -- Process_Full_View --
19771 -----------------------
19772
19773 -- WARNING: This routine manages Ghost regions. Return statements must be
19774 -- replaced by gotos which jump to the end of the routine and restore the
19775 -- Ghost mode.
19776
19777 procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
19778 procedure Collect_Implemented_Interfaces
19779 (Typ : Entity_Id;
19780 Ifaces : Elist_Id);
19781 -- Ada 2005: Gather all the interfaces that Typ directly or
19782 -- inherently implements. Duplicate entries are not added to
19783 -- the list Ifaces.
19784
19785 ------------------------------------
19786 -- Collect_Implemented_Interfaces --
19787 ------------------------------------
19788
19789 procedure Collect_Implemented_Interfaces
19790 (Typ : Entity_Id;
19791 Ifaces : Elist_Id)
19792 is
19793 Iface : Entity_Id;
19794 Iface_Elmt : Elmt_Id;
19795
19796 begin
19797 -- Abstract interfaces are only associated with tagged record types
19798
19799 if not Is_Tagged_Type (Typ) or else not Is_Record_Type (Typ) then
19800 return;
19801 end if;
19802
19803 -- Recursively climb to the ancestors
19804
19805 if Etype (Typ) /= Typ
19806
19807 -- Protect the frontend against wrong cyclic declarations like:
19808
19809 -- type B is new A with private;
19810 -- type C is new A with private;
19811 -- private
19812 -- type B is new C with null record;
19813 -- type C is new B with null record;
19814
19815 and then Etype (Typ) /= Priv_T
19816 and then Etype (Typ) /= Full_T
19817 then
19818 -- Keep separate the management of private type declarations
19819
19820 if Ekind (Typ) = E_Record_Type_With_Private then
19821
19822 -- Handle the following illegal usage:
19823 -- type Private_Type is tagged private;
19824 -- private
19825 -- type Private_Type is new Type_Implementing_Iface;
19826
19827 if Present (Full_View (Typ))
19828 and then Etype (Typ) /= Full_View (Typ)
19829 then
19830 if Is_Interface (Etype (Typ)) then
19831 Append_Unique_Elmt (Etype (Typ), Ifaces);
19832 end if;
19833
19834 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
19835 end if;
19836
19837 -- Non-private types
19838
19839 else
19840 if Is_Interface (Etype (Typ)) then
19841 Append_Unique_Elmt (Etype (Typ), Ifaces);
19842 end if;
19843
19844 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
19845 end if;
19846 end if;
19847
19848 -- Handle entities in the list of abstract interfaces
19849
19850 if Present (Interfaces (Typ)) then
19851 Iface_Elmt := First_Elmt (Interfaces (Typ));
19852 while Present (Iface_Elmt) loop
19853 Iface := Node (Iface_Elmt);
19854
19855 pragma Assert (Is_Interface (Iface));
19856
19857 if not Contain_Interface (Iface, Ifaces) then
19858 Append_Elmt (Iface, Ifaces);
19859 Collect_Implemented_Interfaces (Iface, Ifaces);
19860 end if;
19861
19862 Next_Elmt (Iface_Elmt);
19863 end loop;
19864 end if;
19865 end Collect_Implemented_Interfaces;
19866
19867 -- Local variables
19868
19869 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
19870
19871 Full_Indic : Node_Id;
19872 Full_Parent : Entity_Id;
19873 Priv_Parent : Entity_Id;
19874
19875 -- Start of processing for Process_Full_View
19876
19877 begin
19878 Mark_And_Set_Ghost_Completion (N, Priv_T);
19879
19880 -- First some sanity checks that must be done after semantic
19881 -- decoration of the full view and thus cannot be placed with other
19882 -- similar checks in Find_Type_Name
19883
19884 if not Is_Limited_Type (Priv_T)
19885 and then (Is_Limited_Type (Full_T)
19886 or else Is_Limited_Composite (Full_T))
19887 then
19888 if In_Instance then
19889 null;
19890 else
19891 Error_Msg_N
19892 ("completion of nonlimited type cannot be limited", Full_T);
19893 Explain_Limited_Type (Full_T, Full_T);
19894 end if;
19895
19896 elsif Is_Abstract_Type (Full_T)
19897 and then not Is_Abstract_Type (Priv_T)
19898 then
19899 Error_Msg_N
19900 ("completion of nonabstract type cannot be abstract", Full_T);
19901
19902 elsif Is_Tagged_Type (Priv_T)
19903 and then Is_Limited_Type (Priv_T)
19904 and then not Is_Limited_Type (Full_T)
19905 then
19906 -- If pragma CPP_Class was applied to the private declaration
19907 -- propagate the limitedness to the full-view
19908
19909 if Is_CPP_Class (Priv_T) then
19910 Set_Is_Limited_Record (Full_T);
19911
19912 -- GNAT allow its own definition of Limited_Controlled to disobey
19913 -- this rule in order in ease the implementation. This test is safe
19914 -- because Root_Controlled is defined in a child of System that
19915 -- normal programs are not supposed to use.
19916
19917 elsif Is_RTE (Etype (Full_T), RE_Root_Controlled) then
19918 Set_Is_Limited_Composite (Full_T);
19919 else
19920 Error_Msg_N
19921 ("completion of limited tagged type must be limited", Full_T);
19922 end if;
19923
19924 elsif Is_Generic_Type (Priv_T) then
19925 Error_Msg_N ("generic type cannot have a completion", Full_T);
19926 end if;
19927
19928 -- Check that ancestor interfaces of private and full views are
19929 -- consistent. We omit this check for synchronized types because
19930 -- they are performed on the corresponding record type when frozen.
19931
19932 if Ada_Version >= Ada_2005
19933 and then Is_Tagged_Type (Priv_T)
19934 and then Is_Tagged_Type (Full_T)
19935 and then not Is_Concurrent_Type (Full_T)
19936 then
19937 declare
19938 Iface : Entity_Id;
19939 Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
19940 Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
19941
19942 begin
19943 Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
19944 Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
19945
19946 -- Ada 2005 (AI-251): The partial view shall be a descendant of
19947 -- an interface type if and only if the full type is descendant
19948 -- of the interface type (AARM 7.3 (7.3/2)).
19949
19950 Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
19951
19952 if Present (Iface) then
19953 Error_Msg_NE
19954 ("interface in partial view& not implemented by full type "
19955 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
19956 end if;
19957
19958 Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
19959
19960 if Present (Iface) then
19961 Error_Msg_NE
19962 ("interface & not implemented by partial view "
19963 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
19964 end if;
19965 end;
19966 end if;
19967
19968 if Is_Tagged_Type (Priv_T)
19969 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
19970 and then Is_Derived_Type (Full_T)
19971 then
19972 Priv_Parent := Etype (Priv_T);
19973
19974 -- The full view of a private extension may have been transformed
19975 -- into an unconstrained derived type declaration and a subtype
19976 -- declaration (see build_derived_record_type for details).
19977
19978 if Nkind (N) = N_Subtype_Declaration then
19979 Full_Indic := Subtype_Indication (N);
19980 Full_Parent := Etype (Base_Type (Full_T));
19981 else
19982 Full_Indic := Subtype_Indication (Type_Definition (N));
19983 Full_Parent := Etype (Full_T);
19984 end if;
19985
19986 -- Check that the parent type of the full type is a descendant of
19987 -- the ancestor subtype given in the private extension. If either
19988 -- entity has an Etype equal to Any_Type then we had some previous
19989 -- error situation [7.3(8)].
19990
19991 if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
19992 goto Leave;
19993
19994 -- Ada 2005 (AI-251): Interfaces in the full type can be given in
19995 -- any order. Therefore we don't have to check that its parent must
19996 -- be a descendant of the parent of the private type declaration.
19997
19998 elsif Is_Interface (Priv_Parent)
19999 and then Is_Interface (Full_Parent)
20000 then
20001 null;
20002
20003 -- Ada 2005 (AI-251): If the parent of the private type declaration
20004 -- is an interface there is no need to check that it is an ancestor
20005 -- of the associated full type declaration. The required tests for
20006 -- this case are performed by Build_Derived_Record_Type.
20007
20008 elsif not Is_Interface (Base_Type (Priv_Parent))
20009 and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
20010 then
20011 Error_Msg_N
20012 ("parent of full type must descend from parent of private "
20013 & "extension", Full_Indic);
20014
20015 -- First check a formal restriction, and then proceed with checking
20016 -- Ada rules. Since the formal restriction is not a serious error, we
20017 -- don't prevent further error detection for this check, hence the
20018 -- ELSE.
20019
20020 else
20021 -- In formal mode, when completing a private extension the type
20022 -- named in the private part must be exactly the same as that
20023 -- named in the visible part.
20024
20025 if Priv_Parent /= Full_Parent then
20026 Error_Msg_Name_1 := Chars (Priv_Parent);
20027 Check_SPARK_05_Restriction ("% expected", Full_Indic);
20028 end if;
20029
20030 -- Check the rules of 7.3(10): if the private extension inherits
20031 -- known discriminants, then the full type must also inherit those
20032 -- discriminants from the same (ancestor) type, and the parent
20033 -- subtype of the full type must be constrained if and only if
20034 -- the ancestor subtype of the private extension is constrained.
20035
20036 if No (Discriminant_Specifications (Parent (Priv_T)))
20037 and then not Has_Unknown_Discriminants (Priv_T)
20038 and then Has_Discriminants (Base_Type (Priv_Parent))
20039 then
20040 declare
20041 Priv_Indic : constant Node_Id :=
20042 Subtype_Indication (Parent (Priv_T));
20043
20044 Priv_Constr : constant Boolean :=
20045 Is_Constrained (Priv_Parent)
20046 or else
20047 Nkind (Priv_Indic) = N_Subtype_Indication
20048 or else
20049 Is_Constrained (Entity (Priv_Indic));
20050
20051 Full_Constr : constant Boolean :=
20052 Is_Constrained (Full_Parent)
20053 or else
20054 Nkind (Full_Indic) = N_Subtype_Indication
20055 or else
20056 Is_Constrained (Entity (Full_Indic));
20057
20058 Priv_Discr : Entity_Id;
20059 Full_Discr : Entity_Id;
20060
20061 begin
20062 Priv_Discr := First_Discriminant (Priv_Parent);
20063 Full_Discr := First_Discriminant (Full_Parent);
20064 while Present (Priv_Discr) and then Present (Full_Discr) loop
20065 if Original_Record_Component (Priv_Discr) =
20066 Original_Record_Component (Full_Discr)
20067 or else
20068 Corresponding_Discriminant (Priv_Discr) =
20069 Corresponding_Discriminant (Full_Discr)
20070 then
20071 null;
20072 else
20073 exit;
20074 end if;
20075
20076 Next_Discriminant (Priv_Discr);
20077 Next_Discriminant (Full_Discr);
20078 end loop;
20079
20080 if Present (Priv_Discr) or else Present (Full_Discr) then
20081 Error_Msg_N
20082 ("full view must inherit discriminants of the parent "
20083 & "type used in the private extension", Full_Indic);
20084
20085 elsif Priv_Constr and then not Full_Constr then
20086 Error_Msg_N
20087 ("parent subtype of full type must be constrained",
20088 Full_Indic);
20089
20090 elsif Full_Constr and then not Priv_Constr then
20091 Error_Msg_N
20092 ("parent subtype of full type must be unconstrained",
20093 Full_Indic);
20094 end if;
20095 end;
20096
20097 -- Check the rules of 7.3(12): if a partial view has neither
20098 -- known or unknown discriminants, then the full type
20099 -- declaration shall define a definite subtype.
20100
20101 elsif not Has_Unknown_Discriminants (Priv_T)
20102 and then not Has_Discriminants (Priv_T)
20103 and then not Is_Constrained (Full_T)
20104 then
20105 Error_Msg_N
20106 ("full view must define a constrained type if partial view "
20107 & "has no discriminants", Full_T);
20108 end if;
20109
20110 -- ??????? Do we implement the following properly ?????
20111 -- If the ancestor subtype of a private extension has constrained
20112 -- discriminants, then the parent subtype of the full view shall
20113 -- impose a statically matching constraint on those discriminants
20114 -- [7.3(13)].
20115 end if;
20116
20117 else
20118 -- For untagged types, verify that a type without discriminants is
20119 -- not completed with an unconstrained type. A separate error message
20120 -- is produced if the full type has defaulted discriminants.
20121
20122 if Is_Definite_Subtype (Priv_T)
20123 and then not Is_Definite_Subtype (Full_T)
20124 then
20125 Error_Msg_Sloc := Sloc (Parent (Priv_T));
20126 Error_Msg_NE
20127 ("full view of& not compatible with declaration#",
20128 Full_T, Priv_T);
20129
20130 if not Is_Tagged_Type (Full_T) then
20131 Error_Msg_N
20132 ("\one is constrained, the other unconstrained", Full_T);
20133 end if;
20134 end if;
20135 end if;
20136
20137 -- AI-419: verify that the use of "limited" is consistent
20138
20139 declare
20140 Orig_Decl : constant Node_Id := Original_Node (N);
20141
20142 begin
20143 if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
20144 and then Nkind (Orig_Decl) = N_Full_Type_Declaration
20145 and then Nkind
20146 (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
20147 then
20148 if not Limited_Present (Parent (Priv_T))
20149 and then not Synchronized_Present (Parent (Priv_T))
20150 and then Limited_Present (Type_Definition (Orig_Decl))
20151 then
20152 Error_Msg_N
20153 ("full view of non-limited extension cannot be limited", N);
20154
20155 -- Conversely, if the partial view carries the limited keyword,
20156 -- the full view must as well, even if it may be redundant.
20157
20158 elsif Limited_Present (Parent (Priv_T))
20159 and then not Limited_Present (Type_Definition (Orig_Decl))
20160 then
20161 Error_Msg_N
20162 ("full view of limited extension must be explicitly limited",
20163 N);
20164 end if;
20165 end if;
20166 end;
20167
20168 -- Ada 2005 (AI-443): A synchronized private extension must be
20169 -- completed by a task or protected type.
20170
20171 if Ada_Version >= Ada_2005
20172 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
20173 and then Synchronized_Present (Parent (Priv_T))
20174 and then not Is_Concurrent_Type (Full_T)
20175 then
20176 Error_Msg_N ("full view of synchronized extension must " &
20177 "be synchronized type", N);
20178 end if;
20179
20180 -- Ada 2005 AI-363: if the full view has discriminants with
20181 -- defaults, it is illegal to declare constrained access subtypes
20182 -- whose designated type is the current type. This allows objects
20183 -- of the type that are declared in the heap to be unconstrained.
20184
20185 if not Has_Unknown_Discriminants (Priv_T)
20186 and then not Has_Discriminants (Priv_T)
20187 and then Has_Discriminants (Full_T)
20188 and then
20189 Present (Discriminant_Default_Value (First_Discriminant (Full_T)))
20190 then
20191 Set_Has_Constrained_Partial_View (Full_T);
20192 Set_Has_Constrained_Partial_View (Priv_T);
20193 end if;
20194
20195 -- Create a full declaration for all its subtypes recorded in
20196 -- Private_Dependents and swap them similarly to the base type. These
20197 -- are subtypes that have been define before the full declaration of
20198 -- the private type. We also swap the entry in Private_Dependents list
20199 -- so we can properly restore the private view on exit from the scope.
20200
20201 declare
20202 Priv_Elmt : Elmt_Id;
20203 Priv_Scop : Entity_Id;
20204 Priv : Entity_Id;
20205 Full : Entity_Id;
20206
20207 begin
20208 Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
20209 while Present (Priv_Elmt) loop
20210 Priv := Node (Priv_Elmt);
20211 Priv_Scop := Scope (Priv);
20212
20213 if Ekind_In (Priv, E_Private_Subtype,
20214 E_Limited_Private_Subtype,
20215 E_Record_Subtype_With_Private)
20216 then
20217 Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
20218 Set_Is_Itype (Full);
20219 Set_Parent (Full, Parent (Priv));
20220 Set_Associated_Node_For_Itype (Full, N);
20221
20222 -- Now we need to complete the private subtype, but since the
20223 -- base type has already been swapped, we must also swap the
20224 -- subtypes (and thus, reverse the arguments in the call to
20225 -- Complete_Private_Subtype). Also note that we may need to
20226 -- re-establish the scope of the private subtype.
20227
20228 Copy_And_Swap (Priv, Full);
20229
20230 if not In_Open_Scopes (Priv_Scop) then
20231 Push_Scope (Priv_Scop);
20232
20233 else
20234 -- Reset Priv_Scop to Empty to indicate no scope was pushed
20235
20236 Priv_Scop := Empty;
20237 end if;
20238
20239 Complete_Private_Subtype (Full, Priv, Full_T, N);
20240
20241 if Present (Priv_Scop) then
20242 Pop_Scope;
20243 end if;
20244
20245 Replace_Elmt (Priv_Elmt, Full);
20246 end if;
20247
20248 Next_Elmt (Priv_Elmt);
20249 end loop;
20250 end;
20251
20252 -- If the private view was tagged, copy the new primitive operations
20253 -- from the private view to the full view.
20254
20255 if Is_Tagged_Type (Full_T) then
20256 declare
20257 Disp_Typ : Entity_Id;
20258 Full_List : Elist_Id;
20259 Prim : Entity_Id;
20260 Prim_Elmt : Elmt_Id;
20261 Priv_List : Elist_Id;
20262
20263 function Contains
20264 (E : Entity_Id;
20265 L : Elist_Id) return Boolean;
20266 -- Determine whether list L contains element E
20267
20268 --------------
20269 -- Contains --
20270 --------------
20271
20272 function Contains
20273 (E : Entity_Id;
20274 L : Elist_Id) return Boolean
20275 is
20276 List_Elmt : Elmt_Id;
20277
20278 begin
20279 List_Elmt := First_Elmt (L);
20280 while Present (List_Elmt) loop
20281 if Node (List_Elmt) = E then
20282 return True;
20283 end if;
20284
20285 Next_Elmt (List_Elmt);
20286 end loop;
20287
20288 return False;
20289 end Contains;
20290
20291 -- Start of processing
20292
20293 begin
20294 if Is_Tagged_Type (Priv_T) then
20295 Priv_List := Primitive_Operations (Priv_T);
20296 Prim_Elmt := First_Elmt (Priv_List);
20297
20298 -- In the case of a concurrent type completing a private tagged
20299 -- type, primitives may have been declared in between the two
20300 -- views. These subprograms need to be wrapped the same way
20301 -- entries and protected procedures are handled because they
20302 -- cannot be directly shared by the two views.
20303
20304 if Is_Concurrent_Type (Full_T) then
20305 declare
20306 Conc_Typ : constant Entity_Id :=
20307 Corresponding_Record_Type (Full_T);
20308 Curr_Nod : Node_Id := Parent (Conc_Typ);
20309 Wrap_Spec : Node_Id;
20310
20311 begin
20312 while Present (Prim_Elmt) loop
20313 Prim := Node (Prim_Elmt);
20314
20315 if Comes_From_Source (Prim)
20316 and then not Is_Abstract_Subprogram (Prim)
20317 then
20318 Wrap_Spec :=
20319 Make_Subprogram_Declaration (Sloc (Prim),
20320 Specification =>
20321 Build_Wrapper_Spec
20322 (Subp_Id => Prim,
20323 Obj_Typ => Conc_Typ,
20324 Formals =>
20325 Parameter_Specifications
20326 (Parent (Prim))));
20327
20328 Insert_After (Curr_Nod, Wrap_Spec);
20329 Curr_Nod := Wrap_Spec;
20330
20331 Analyze (Wrap_Spec);
20332
20333 -- Remove the wrapper from visibility to avoid
20334 -- spurious conflict with the wrapped entity.
20335
20336 Set_Is_Immediately_Visible
20337 (Defining_Entity (Specification (Wrap_Spec)),
20338 False);
20339 end if;
20340
20341 Next_Elmt (Prim_Elmt);
20342 end loop;
20343
20344 goto Leave;
20345 end;
20346
20347 -- For non-concurrent types, transfer explicit primitives, but
20348 -- omit those inherited from the parent of the private view
20349 -- since they will be re-inherited later on.
20350
20351 else
20352 Full_List := Primitive_Operations (Full_T);
20353
20354 while Present (Prim_Elmt) loop
20355 Prim := Node (Prim_Elmt);
20356
20357 if Comes_From_Source (Prim)
20358 and then not Contains (Prim, Full_List)
20359 then
20360 Append_Elmt (Prim, Full_List);
20361 end if;
20362
20363 Next_Elmt (Prim_Elmt);
20364 end loop;
20365 end if;
20366
20367 -- Untagged private view
20368
20369 else
20370 Full_List := Primitive_Operations (Full_T);
20371
20372 -- In this case the partial view is untagged, so here we locate
20373 -- all of the earlier primitives that need to be treated as
20374 -- dispatching (those that appear between the two views). Note
20375 -- that these additional operations must all be new operations
20376 -- (any earlier operations that override inherited operations
20377 -- of the full view will already have been inserted in the
20378 -- primitives list, marked by Check_Operation_From_Private_View
20379 -- as dispatching. Note that implicit "/=" operators are
20380 -- excluded from being added to the primitives list since they
20381 -- shouldn't be treated as dispatching (tagged "/=" is handled
20382 -- specially).
20383
20384 Prim := Next_Entity (Full_T);
20385 while Present (Prim) and then Prim /= Priv_T loop
20386 if Ekind_In (Prim, E_Procedure, E_Function) then
20387 Disp_Typ := Find_Dispatching_Type (Prim);
20388
20389 if Disp_Typ = Full_T
20390 and then (Chars (Prim) /= Name_Op_Ne
20391 or else Comes_From_Source (Prim))
20392 then
20393 Check_Controlling_Formals (Full_T, Prim);
20394
20395 if not Is_Dispatching_Operation (Prim) then
20396 Append_Elmt (Prim, Full_List);
20397 Set_Is_Dispatching_Operation (Prim, True);
20398 Set_DT_Position_Value (Prim, No_Uint);
20399 end if;
20400
20401 elsif Is_Dispatching_Operation (Prim)
20402 and then Disp_Typ /= Full_T
20403 then
20404
20405 -- Verify that it is not otherwise controlled by a
20406 -- formal or a return value of type T.
20407
20408 Check_Controlling_Formals (Disp_Typ, Prim);
20409 end if;
20410 end if;
20411
20412 Next_Entity (Prim);
20413 end loop;
20414 end if;
20415
20416 -- For the tagged case, the two views can share the same primitive
20417 -- operations list and the same class-wide type. Update attributes
20418 -- of the class-wide type which depend on the full declaration.
20419
20420 if Is_Tagged_Type (Priv_T) then
20421 Set_Direct_Primitive_Operations (Priv_T, Full_List);
20422 Set_Class_Wide_Type
20423 (Base_Type (Full_T), Class_Wide_Type (Priv_T));
20424
20425 Propagate_Concurrent_Flags (Class_Wide_Type (Priv_T), Full_T);
20426 end if;
20427 end;
20428 end if;
20429
20430 -- Ada 2005 AI 161: Check preelaborable initialization consistency
20431
20432 if Known_To_Have_Preelab_Init (Priv_T) then
20433
20434 -- Case where there is a pragma Preelaborable_Initialization. We
20435 -- always allow this in predefined units, which is cheating a bit,
20436 -- but it means we don't have to struggle to meet the requirements in
20437 -- the RM for having Preelaborable Initialization. Otherwise we
20438 -- require that the type meets the RM rules. But we can't check that
20439 -- yet, because of the rule about overriding Initialize, so we simply
20440 -- set a flag that will be checked at freeze time.
20441
20442 if not In_Predefined_Unit (Full_T) then
20443 Set_Must_Have_Preelab_Init (Full_T);
20444 end if;
20445 end if;
20446
20447 -- If pragma CPP_Class was applied to the private type declaration,
20448 -- propagate it now to the full type declaration.
20449
20450 if Is_CPP_Class (Priv_T) then
20451 Set_Is_CPP_Class (Full_T);
20452 Set_Convention (Full_T, Convention_CPP);
20453
20454 -- Check that components of imported CPP types do not have default
20455 -- expressions.
20456
20457 Check_CPP_Type_Has_No_Defaults (Full_T);
20458 end if;
20459
20460 -- If the private view has user specified stream attributes, then so has
20461 -- the full view.
20462
20463 -- Why the test, how could these flags be already set in Full_T ???
20464
20465 if Has_Specified_Stream_Read (Priv_T) then
20466 Set_Has_Specified_Stream_Read (Full_T);
20467 end if;
20468
20469 if Has_Specified_Stream_Write (Priv_T) then
20470 Set_Has_Specified_Stream_Write (Full_T);
20471 end if;
20472
20473 if Has_Specified_Stream_Input (Priv_T) then
20474 Set_Has_Specified_Stream_Input (Full_T);
20475 end if;
20476
20477 if Has_Specified_Stream_Output (Priv_T) then
20478 Set_Has_Specified_Stream_Output (Full_T);
20479 end if;
20480
20481 -- Propagate Default_Initial_Condition-related attributes from the
20482 -- partial view to the full view and its base type.
20483
20484 Propagate_DIC_Attributes (Full_T, From_Typ => Priv_T);
20485 Propagate_DIC_Attributes (Base_Type (Full_T), From_Typ => Priv_T);
20486
20487 -- Propagate invariant-related attributes from the partial view to the
20488 -- full view and its base type.
20489
20490 Propagate_Invariant_Attributes (Full_T, From_Typ => Priv_T);
20491 Propagate_Invariant_Attributes (Base_Type (Full_T), From_Typ => Priv_T);
20492
20493 -- AI12-0041: Detect an attempt to inherit a class-wide type invariant
20494 -- in the full view without advertising the inheritance in the partial
20495 -- view. This can only occur when the partial view has no parent type
20496 -- and the full view has an interface as a parent. Any other scenarios
20497 -- are illegal because implemented interfaces must match between the
20498 -- two views.
20499
20500 if Is_Tagged_Type (Priv_T) and then Is_Tagged_Type (Full_T) then
20501 declare
20502 Full_Par : constant Entity_Id := Etype (Full_T);
20503 Priv_Par : constant Entity_Id := Etype (Priv_T);
20504
20505 begin
20506 if not Is_Interface (Priv_Par)
20507 and then Is_Interface (Full_Par)
20508 and then Has_Inheritable_Invariants (Full_Par)
20509 then
20510 Error_Msg_N
20511 ("hidden inheritance of class-wide type invariants not "
20512 & "allowed", N);
20513 end if;
20514 end;
20515 end if;
20516
20517 -- Propagate predicates to full type, and predicate function if already
20518 -- defined. It is not clear that this can actually happen? the partial
20519 -- view cannot be frozen yet, and the predicate function has not been
20520 -- built. Still it is a cheap check and seems safer to make it.
20521
20522 if Has_Predicates (Priv_T) then
20523 Set_Has_Predicates (Full_T);
20524
20525 if Present (Predicate_Function (Priv_T)) then
20526 Set_Predicate_Function (Full_T, Predicate_Function (Priv_T));
20527 end if;
20528 end if;
20529
20530 <<Leave>>
20531 Restore_Ghost_Mode (Saved_GM);
20532 end Process_Full_View;
20533
20534 -----------------------------------
20535 -- Process_Incomplete_Dependents --
20536 -----------------------------------
20537
20538 procedure Process_Incomplete_Dependents
20539 (N : Node_Id;
20540 Full_T : Entity_Id;
20541 Inc_T : Entity_Id)
20542 is
20543 Inc_Elmt : Elmt_Id;
20544 Priv_Dep : Entity_Id;
20545 New_Subt : Entity_Id;
20546
20547 Disc_Constraint : Elist_Id;
20548
20549 begin
20550 if No (Private_Dependents (Inc_T)) then
20551 return;
20552 end if;
20553
20554 -- Itypes that may be generated by the completion of an incomplete
20555 -- subtype are not used by the back-end and not attached to the tree.
20556 -- They are created only for constraint-checking purposes.
20557
20558 Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
20559 while Present (Inc_Elmt) loop
20560 Priv_Dep := Node (Inc_Elmt);
20561
20562 if Ekind (Priv_Dep) = E_Subprogram_Type then
20563
20564 -- An Access_To_Subprogram type may have a return type or a
20565 -- parameter type that is incomplete. Replace with the full view.
20566
20567 if Etype (Priv_Dep) = Inc_T then
20568 Set_Etype (Priv_Dep, Full_T);
20569 end if;
20570
20571 declare
20572 Formal : Entity_Id;
20573
20574 begin
20575 Formal := First_Formal (Priv_Dep);
20576 while Present (Formal) loop
20577 if Etype (Formal) = Inc_T then
20578 Set_Etype (Formal, Full_T);
20579 end if;
20580
20581 Next_Formal (Formal);
20582 end loop;
20583 end;
20584
20585 elsif Is_Overloadable (Priv_Dep) then
20586
20587 -- If a subprogram in the incomplete dependents list is primitive
20588 -- for a tagged full type then mark it as a dispatching operation,
20589 -- check whether it overrides an inherited subprogram, and check
20590 -- restrictions on its controlling formals. Note that a protected
20591 -- operation is never dispatching: only its wrapper operation
20592 -- (which has convention Ada) is.
20593
20594 if Is_Tagged_Type (Full_T)
20595 and then Is_Primitive (Priv_Dep)
20596 and then Convention (Priv_Dep) /= Convention_Protected
20597 then
20598 Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
20599 Set_Is_Dispatching_Operation (Priv_Dep);
20600 Check_Controlling_Formals (Full_T, Priv_Dep);
20601 end if;
20602
20603 elsif Ekind (Priv_Dep) = E_Subprogram_Body then
20604
20605 -- Can happen during processing of a body before the completion
20606 -- of a TA type. Ignore, because spec is also on dependent list.
20607
20608 return;
20609
20610 -- Ada 2005 (AI-412): Transform a regular incomplete subtype into a
20611 -- corresponding subtype of the full view.
20612
20613 elsif Ekind (Priv_Dep) = E_Incomplete_Subtype then
20614 Set_Subtype_Indication
20615 (Parent (Priv_Dep), New_Occurrence_Of (Full_T, Sloc (Priv_Dep)));
20616 Set_Etype (Priv_Dep, Full_T);
20617 Set_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
20618 Set_Analyzed (Parent (Priv_Dep), False);
20619
20620 -- Reanalyze the declaration, suppressing the call to
20621 -- Enter_Name to avoid duplicate names.
20622
20623 Analyze_Subtype_Declaration
20624 (N => Parent (Priv_Dep),
20625 Skip => True);
20626
20627 -- Dependent is a subtype
20628
20629 else
20630 -- We build a new subtype indication using the full view of the
20631 -- incomplete parent. The discriminant constraints have been
20632 -- elaborated already at the point of the subtype declaration.
20633
20634 New_Subt := Create_Itype (E_Void, N);
20635
20636 if Has_Discriminants (Full_T) then
20637 Disc_Constraint := Discriminant_Constraint (Priv_Dep);
20638 else
20639 Disc_Constraint := No_Elist;
20640 end if;
20641
20642 Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
20643 Set_Full_View (Priv_Dep, New_Subt);
20644 end if;
20645
20646 Next_Elmt (Inc_Elmt);
20647 end loop;
20648 end Process_Incomplete_Dependents;
20649
20650 --------------------------------
20651 -- Process_Range_Expr_In_Decl --
20652 --------------------------------
20653
20654 procedure Process_Range_Expr_In_Decl
20655 (R : Node_Id;
20656 T : Entity_Id;
20657 Subtyp : Entity_Id := Empty;
20658 Check_List : List_Id := Empty_List;
20659 R_Check_Off : Boolean := False;
20660 In_Iter_Schm : Boolean := False)
20661 is
20662 Lo, Hi : Node_Id;
20663 R_Checks : Check_Result;
20664 Insert_Node : Node_Id;
20665 Def_Id : Entity_Id;
20666
20667 begin
20668 Analyze_And_Resolve (R, Base_Type (T));
20669
20670 if Nkind (R) = N_Range then
20671
20672 -- In SPARK, all ranges should be static, with the exception of the
20673 -- discrete type definition of a loop parameter specification.
20674
20675 if not In_Iter_Schm
20676 and then not Is_OK_Static_Range (R)
20677 then
20678 Check_SPARK_05_Restriction ("range should be static", R);
20679 end if;
20680
20681 Lo := Low_Bound (R);
20682 Hi := High_Bound (R);
20683
20684 -- Validity checks on the range of a quantified expression are
20685 -- delayed until the construct is transformed into a loop.
20686
20687 if Nkind (Parent (R)) = N_Loop_Parameter_Specification
20688 and then Nkind (Parent (Parent (R))) = N_Quantified_Expression
20689 then
20690 null;
20691
20692 -- We need to ensure validity of the bounds here, because if we
20693 -- go ahead and do the expansion, then the expanded code will get
20694 -- analyzed with range checks suppressed and we miss the check.
20695
20696 -- WARNING: The capture of the range bounds with xxx_FIRST/_LAST and
20697 -- the temporaries generated by routine Remove_Side_Effects by means
20698 -- of validity checks must use the same names. When a range appears
20699 -- in the parent of a generic, the range is processed with checks
20700 -- disabled as part of the generic context and with checks enabled
20701 -- for code generation purposes. This leads to link issues as the
20702 -- generic contains references to xxx_FIRST/_LAST, but the inlined
20703 -- template sees the temporaries generated by Remove_Side_Effects.
20704
20705 else
20706 Validity_Check_Range (R, Subtyp);
20707 end if;
20708
20709 -- If there were errors in the declaration, try and patch up some
20710 -- common mistakes in the bounds. The cases handled are literals
20711 -- which are Integer where the expected type is Real and vice versa.
20712 -- These corrections allow the compilation process to proceed further
20713 -- along since some basic assumptions of the format of the bounds
20714 -- are guaranteed.
20715
20716 if Etype (R) = Any_Type then
20717 if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
20718 Rewrite (Lo,
20719 Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
20720
20721 elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
20722 Rewrite (Hi,
20723 Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
20724
20725 elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
20726 Rewrite (Lo,
20727 Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
20728
20729 elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
20730 Rewrite (Hi,
20731 Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
20732 end if;
20733
20734 Set_Etype (Lo, T);
20735 Set_Etype (Hi, T);
20736 end if;
20737
20738 -- If the bounds of the range have been mistakenly given as string
20739 -- literals (perhaps in place of character literals), then an error
20740 -- has already been reported, but we rewrite the string literal as a
20741 -- bound of the range's type to avoid blowups in later processing
20742 -- that looks at static values.
20743
20744 if Nkind (Lo) = N_String_Literal then
20745 Rewrite (Lo,
20746 Make_Attribute_Reference (Sloc (Lo),
20747 Prefix => New_Occurrence_Of (T, Sloc (Lo)),
20748 Attribute_Name => Name_First));
20749 Analyze_And_Resolve (Lo);
20750 end if;
20751
20752 if Nkind (Hi) = N_String_Literal then
20753 Rewrite (Hi,
20754 Make_Attribute_Reference (Sloc (Hi),
20755 Prefix => New_Occurrence_Of (T, Sloc (Hi)),
20756 Attribute_Name => Name_First));
20757 Analyze_And_Resolve (Hi);
20758 end if;
20759
20760 -- If bounds aren't scalar at this point then exit, avoiding
20761 -- problems with further processing of the range in this procedure.
20762
20763 if not Is_Scalar_Type (Etype (Lo)) then
20764 return;
20765 end if;
20766
20767 -- Resolve (actually Sem_Eval) has checked that the bounds are in
20768 -- then range of the base type. Here we check whether the bounds
20769 -- are in the range of the subtype itself. Note that if the bounds
20770 -- represent the null range the Constraint_Error exception should
20771 -- not be raised.
20772
20773 -- ??? The following code should be cleaned up as follows
20774
20775 -- 1. The Is_Null_Range (Lo, Hi) test should disappear since it
20776 -- is done in the call to Range_Check (R, T); below
20777
20778 -- 2. The use of R_Check_Off should be investigated and possibly
20779 -- removed, this would clean up things a bit.
20780
20781 if Is_Null_Range (Lo, Hi) then
20782 null;
20783
20784 else
20785 -- Capture values of bounds and generate temporaries for them
20786 -- if needed, before applying checks, since checks may cause
20787 -- duplication of the expression without forcing evaluation.
20788
20789 -- The forced evaluation removes side effects from expressions,
20790 -- which should occur also in GNATprove mode. Otherwise, we end up
20791 -- with unexpected insertions of actions at places where this is
20792 -- not supposed to occur, e.g. on default parameters of a call.
20793
20794 if Expander_Active or GNATprove_Mode then
20795
20796 -- Call Force_Evaluation to create declarations as needed to
20797 -- deal with side effects, and also create typ_FIRST/LAST
20798 -- entities for bounds if we have a subtype name.
20799
20800 -- Note: we do this transformation even if expansion is not
20801 -- active if we are in GNATprove_Mode since the transformation
20802 -- is in general required to ensure that the resulting tree has
20803 -- proper Ada semantics.
20804
20805 Force_Evaluation
20806 (Lo, Related_Id => Subtyp, Is_Low_Bound => True);
20807 Force_Evaluation
20808 (Hi, Related_Id => Subtyp, Is_High_Bound => True);
20809 end if;
20810
20811 -- We use a flag here instead of suppressing checks on the type
20812 -- because the type we check against isn't necessarily the place
20813 -- where we put the check.
20814
20815 if not R_Check_Off then
20816 R_Checks := Get_Range_Checks (R, T);
20817
20818 -- Look up tree to find an appropriate insertion point. We
20819 -- can't just use insert_actions because later processing
20820 -- depends on the insertion node. Prior to Ada 2012 the
20821 -- insertion point could only be a declaration or a loop, but
20822 -- quantified expressions can appear within any context in an
20823 -- expression, and the insertion point can be any statement,
20824 -- pragma, or declaration.
20825
20826 Insert_Node := Parent (R);
20827 while Present (Insert_Node) loop
20828 exit when
20829 Nkind (Insert_Node) in N_Declaration
20830 and then
20831 not Nkind_In
20832 (Insert_Node, N_Component_Declaration,
20833 N_Loop_Parameter_Specification,
20834 N_Function_Specification,
20835 N_Procedure_Specification);
20836
20837 exit when Nkind (Insert_Node) in N_Later_Decl_Item
20838 or else Nkind (Insert_Node) in
20839 N_Statement_Other_Than_Procedure_Call
20840 or else Nkind_In (Insert_Node, N_Procedure_Call_Statement,
20841 N_Pragma);
20842
20843 Insert_Node := Parent (Insert_Node);
20844 end loop;
20845
20846 -- Why would Type_Decl not be present??? Without this test,
20847 -- short regression tests fail.
20848
20849 if Present (Insert_Node) then
20850
20851 -- Case of loop statement. Verify that the range is part
20852 -- of the subtype indication of the iteration scheme.
20853
20854 if Nkind (Insert_Node) = N_Loop_Statement then
20855 declare
20856 Indic : Node_Id;
20857
20858 begin
20859 Indic := Parent (R);
20860 while Present (Indic)
20861 and then Nkind (Indic) /= N_Subtype_Indication
20862 loop
20863 Indic := Parent (Indic);
20864 end loop;
20865
20866 if Present (Indic) then
20867 Def_Id := Etype (Subtype_Mark (Indic));
20868
20869 Insert_Range_Checks
20870 (R_Checks,
20871 Insert_Node,
20872 Def_Id,
20873 Sloc (Insert_Node),
20874 R,
20875 Do_Before => True);
20876 end if;
20877 end;
20878
20879 -- Insertion before a declaration. If the declaration
20880 -- includes discriminants, the list of applicable checks
20881 -- is given by the caller.
20882
20883 elsif Nkind (Insert_Node) in N_Declaration then
20884 Def_Id := Defining_Identifier (Insert_Node);
20885
20886 if (Ekind (Def_Id) = E_Record_Type
20887 and then Depends_On_Discriminant (R))
20888 or else
20889 (Ekind (Def_Id) = E_Protected_Type
20890 and then Has_Discriminants (Def_Id))
20891 then
20892 Append_Range_Checks
20893 (R_Checks,
20894 Check_List, Def_Id, Sloc (Insert_Node), R);
20895
20896 else
20897 Insert_Range_Checks
20898 (R_Checks,
20899 Insert_Node, Def_Id, Sloc (Insert_Node), R);
20900
20901 end if;
20902
20903 -- Insertion before a statement. Range appears in the
20904 -- context of a quantified expression. Insertion will
20905 -- take place when expression is expanded.
20906
20907 else
20908 null;
20909 end if;
20910 end if;
20911 end if;
20912 end if;
20913
20914 -- Case of other than an explicit N_Range node
20915
20916 -- The forced evaluation removes side effects from expressions, which
20917 -- should occur also in GNATprove mode. Otherwise, we end up with
20918 -- unexpected insertions of actions at places where this is not
20919 -- supposed to occur, e.g. on default parameters of a call.
20920
20921 elsif Expander_Active or GNATprove_Mode then
20922 Get_Index_Bounds (R, Lo, Hi);
20923 Force_Evaluation (Lo);
20924 Force_Evaluation (Hi);
20925 end if;
20926 end Process_Range_Expr_In_Decl;
20927
20928 --------------------------------------
20929 -- Process_Real_Range_Specification --
20930 --------------------------------------
20931
20932 procedure Process_Real_Range_Specification (Def : Node_Id) is
20933 Spec : constant Node_Id := Real_Range_Specification (Def);
20934 Lo : Node_Id;
20935 Hi : Node_Id;
20936 Err : Boolean := False;
20937
20938 procedure Analyze_Bound (N : Node_Id);
20939 -- Analyze and check one bound
20940
20941 -------------------
20942 -- Analyze_Bound --
20943 -------------------
20944
20945 procedure Analyze_Bound (N : Node_Id) is
20946 begin
20947 Analyze_And_Resolve (N, Any_Real);
20948
20949 if not Is_OK_Static_Expression (N) then
20950 Flag_Non_Static_Expr
20951 ("bound in real type definition is not static!", N);
20952 Err := True;
20953 end if;
20954 end Analyze_Bound;
20955
20956 -- Start of processing for Process_Real_Range_Specification
20957
20958 begin
20959 if Present (Spec) then
20960 Lo := Low_Bound (Spec);
20961 Hi := High_Bound (Spec);
20962 Analyze_Bound (Lo);
20963 Analyze_Bound (Hi);
20964
20965 -- If error, clear away junk range specification
20966
20967 if Err then
20968 Set_Real_Range_Specification (Def, Empty);
20969 end if;
20970 end if;
20971 end Process_Real_Range_Specification;
20972
20973 ---------------------
20974 -- Process_Subtype --
20975 ---------------------
20976
20977 function Process_Subtype
20978 (S : Node_Id;
20979 Related_Nod : Node_Id;
20980 Related_Id : Entity_Id := Empty;
20981 Suffix : Character := ' ') return Entity_Id
20982 is
20983 P : Node_Id;
20984 Def_Id : Entity_Id;
20985 Error_Node : Node_Id;
20986 Full_View_Id : Entity_Id;
20987 Subtype_Mark_Id : Entity_Id;
20988
20989 May_Have_Null_Exclusion : Boolean;
20990
20991 procedure Check_Incomplete (T : Node_Id);
20992 -- Called to verify that an incomplete type is not used prematurely
20993
20994 ----------------------
20995 -- Check_Incomplete --
20996 ----------------------
20997
20998 procedure Check_Incomplete (T : Node_Id) is
20999 begin
21000 -- Ada 2005 (AI-412): Incomplete subtypes are legal
21001
21002 if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
21003 and then
21004 not (Ada_Version >= Ada_2005
21005 and then
21006 (Nkind (Parent (T)) = N_Subtype_Declaration
21007 or else (Nkind (Parent (T)) = N_Subtype_Indication
21008 and then Nkind (Parent (Parent (T))) =
21009 N_Subtype_Declaration)))
21010 then
21011 Error_Msg_N ("invalid use of type before its full declaration", T);
21012 end if;
21013 end Check_Incomplete;
21014
21015 -- Start of processing for Process_Subtype
21016
21017 begin
21018 -- Case of no constraints present
21019
21020 if Nkind (S) /= N_Subtype_Indication then
21021 Find_Type (S);
21022 Check_Incomplete (S);
21023 P := Parent (S);
21024
21025 -- Ada 2005 (AI-231): Static check
21026
21027 if Ada_Version >= Ada_2005
21028 and then Present (P)
21029 and then Null_Exclusion_Present (P)
21030 and then Nkind (P) /= N_Access_To_Object_Definition
21031 and then not Is_Access_Type (Entity (S))
21032 then
21033 Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
21034 end if;
21035
21036 -- The following is ugly, can't we have a range or even a flag???
21037
21038 May_Have_Null_Exclusion :=
21039 Nkind_In (P, N_Access_Definition,
21040 N_Access_Function_Definition,
21041 N_Access_Procedure_Definition,
21042 N_Access_To_Object_Definition,
21043 N_Allocator,
21044 N_Component_Definition)
21045 or else
21046 Nkind_In (P, N_Derived_Type_Definition,
21047 N_Discriminant_Specification,
21048 N_Formal_Object_Declaration,
21049 N_Object_Declaration,
21050 N_Object_Renaming_Declaration,
21051 N_Parameter_Specification,
21052 N_Subtype_Declaration);
21053
21054 -- Create an Itype that is a duplicate of Entity (S) but with the
21055 -- null-exclusion attribute.
21056
21057 if May_Have_Null_Exclusion
21058 and then Is_Access_Type (Entity (S))
21059 and then Null_Exclusion_Present (P)
21060
21061 -- No need to check the case of an access to object definition.
21062 -- It is correct to define double not-null pointers.
21063
21064 -- Example:
21065 -- type Not_Null_Int_Ptr is not null access Integer;
21066 -- type Acc is not null access Not_Null_Int_Ptr;
21067
21068 and then Nkind (P) /= N_Access_To_Object_Definition
21069 then
21070 if Can_Never_Be_Null (Entity (S)) then
21071 case Nkind (Related_Nod) is
21072 when N_Full_Type_Declaration =>
21073 if Nkind (Type_Definition (Related_Nod))
21074 in N_Array_Type_Definition
21075 then
21076 Error_Node :=
21077 Subtype_Indication
21078 (Component_Definition
21079 (Type_Definition (Related_Nod)));
21080 else
21081 Error_Node :=
21082 Subtype_Indication (Type_Definition (Related_Nod));
21083 end if;
21084
21085 when N_Subtype_Declaration =>
21086 Error_Node := Subtype_Indication (Related_Nod);
21087
21088 when N_Object_Declaration =>
21089 Error_Node := Object_Definition (Related_Nod);
21090
21091 when N_Component_Declaration =>
21092 Error_Node :=
21093 Subtype_Indication (Component_Definition (Related_Nod));
21094
21095 when N_Allocator =>
21096 Error_Node := Expression (Related_Nod);
21097
21098 when others =>
21099 pragma Assert (False);
21100 Error_Node := Related_Nod;
21101 end case;
21102
21103 Error_Msg_NE
21104 ("`NOT NULL` not allowed (& already excludes null)",
21105 Error_Node,
21106 Entity (S));
21107 end if;
21108
21109 Set_Etype (S,
21110 Create_Null_Excluding_Itype
21111 (T => Entity (S),
21112 Related_Nod => P));
21113 Set_Entity (S, Etype (S));
21114 end if;
21115
21116 return Entity (S);
21117
21118 -- Case of constraint present, so that we have an N_Subtype_Indication
21119 -- node (this node is created only if constraints are present).
21120
21121 else
21122 Find_Type (Subtype_Mark (S));
21123
21124 if Nkind (Parent (S)) /= N_Access_To_Object_Definition
21125 and then not
21126 (Nkind (Parent (S)) = N_Subtype_Declaration
21127 and then Is_Itype (Defining_Identifier (Parent (S))))
21128 then
21129 Check_Incomplete (Subtype_Mark (S));
21130 end if;
21131
21132 P := Parent (S);
21133 Subtype_Mark_Id := Entity (Subtype_Mark (S));
21134
21135 -- Explicit subtype declaration case
21136
21137 if Nkind (P) = N_Subtype_Declaration then
21138 Def_Id := Defining_Identifier (P);
21139
21140 -- Explicit derived type definition case
21141
21142 elsif Nkind (P) = N_Derived_Type_Definition then
21143 Def_Id := Defining_Identifier (Parent (P));
21144
21145 -- Implicit case, the Def_Id must be created as an implicit type.
21146 -- The one exception arises in the case of concurrent types, array
21147 -- and access types, where other subsidiary implicit types may be
21148 -- created and must appear before the main implicit type. In these
21149 -- cases we leave Def_Id set to Empty as a signal that Create_Itype
21150 -- has not yet been called to create Def_Id.
21151
21152 else
21153 if Is_Array_Type (Subtype_Mark_Id)
21154 or else Is_Concurrent_Type (Subtype_Mark_Id)
21155 or else Is_Access_Type (Subtype_Mark_Id)
21156 then
21157 Def_Id := Empty;
21158
21159 -- For the other cases, we create a new unattached Itype,
21160 -- and set the indication to ensure it gets attached later.
21161
21162 else
21163 Def_Id :=
21164 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
21165 end if;
21166 end if;
21167
21168 -- If the kind of constraint is invalid for this kind of type,
21169 -- then give an error, and then pretend no constraint was given.
21170
21171 if not Is_Valid_Constraint_Kind
21172 (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
21173 then
21174 Error_Msg_N
21175 ("incorrect constraint for this kind of type", Constraint (S));
21176
21177 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
21178
21179 -- Set Ekind of orphan itype, to prevent cascaded errors
21180
21181 if Present (Def_Id) then
21182 Set_Ekind (Def_Id, Ekind (Any_Type));
21183 end if;
21184
21185 -- Make recursive call, having got rid of the bogus constraint
21186
21187 return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
21188 end if;
21189
21190 -- Remaining processing depends on type. Select on Base_Type kind to
21191 -- ensure getting to the concrete type kind in the case of a private
21192 -- subtype (needed when only doing semantic analysis).
21193
21194 case Ekind (Base_Type (Subtype_Mark_Id)) is
21195 when Access_Kind =>
21196
21197 -- If this is a constraint on a class-wide type, discard it.
21198 -- There is currently no way to express a partial discriminant
21199 -- constraint on a type with unknown discriminants. This is
21200 -- a pathology that the ACATS wisely decides not to test.
21201
21202 if Is_Class_Wide_Type (Designated_Type (Subtype_Mark_Id)) then
21203 if Comes_From_Source (S) then
21204 Error_Msg_N
21205 ("constraint on class-wide type ignored??",
21206 Constraint (S));
21207 end if;
21208
21209 if Nkind (P) = N_Subtype_Declaration then
21210 Set_Subtype_Indication (P,
21211 New_Occurrence_Of (Subtype_Mark_Id, Sloc (S)));
21212 end if;
21213
21214 return Subtype_Mark_Id;
21215 end if;
21216
21217 Constrain_Access (Def_Id, S, Related_Nod);
21218
21219 if Expander_Active
21220 and then Is_Itype (Designated_Type (Def_Id))
21221 and then Nkind (Related_Nod) = N_Subtype_Declaration
21222 and then not Is_Incomplete_Type (Designated_Type (Def_Id))
21223 then
21224 Build_Itype_Reference
21225 (Designated_Type (Def_Id), Related_Nod);
21226 end if;
21227
21228 when Array_Kind =>
21229 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
21230
21231 when Decimal_Fixed_Point_Kind =>
21232 Constrain_Decimal (Def_Id, S);
21233
21234 when Enumeration_Kind =>
21235 Constrain_Enumeration (Def_Id, S);
21236 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
21237
21238 when Ordinary_Fixed_Point_Kind =>
21239 Constrain_Ordinary_Fixed (Def_Id, S);
21240
21241 when Float_Kind =>
21242 Constrain_Float (Def_Id, S);
21243
21244 when Integer_Kind =>
21245 Constrain_Integer (Def_Id, S);
21246 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
21247
21248 when Class_Wide_Kind
21249 | E_Incomplete_Type
21250 | E_Record_Subtype
21251 | E_Record_Type
21252 =>
21253 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
21254
21255 if Ekind (Def_Id) = E_Incomplete_Type then
21256 Set_Private_Dependents (Def_Id, New_Elmt_List);
21257 end if;
21258
21259 when Private_Kind =>
21260 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
21261
21262 -- The base type may be private but Def_Id may be a full view
21263 -- in an instance.
21264
21265 if Is_Private_Type (Def_Id) then
21266 Set_Private_Dependents (Def_Id, New_Elmt_List);
21267 end if;
21268
21269 -- In case of an invalid constraint prevent further processing
21270 -- since the type constructed is missing expected fields.
21271
21272 if Etype (Def_Id) = Any_Type then
21273 return Def_Id;
21274 end if;
21275
21276 -- If the full view is that of a task with discriminants,
21277 -- we must constrain both the concurrent type and its
21278 -- corresponding record type. Otherwise we will just propagate
21279 -- the constraint to the full view, if available.
21280
21281 if Present (Full_View (Subtype_Mark_Id))
21282 and then Has_Discriminants (Subtype_Mark_Id)
21283 and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
21284 then
21285 Full_View_Id :=
21286 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
21287
21288 Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
21289 Constrain_Concurrent (Full_View_Id, S,
21290 Related_Nod, Related_Id, Suffix);
21291 Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
21292 Set_Full_View (Def_Id, Full_View_Id);
21293
21294 -- Introduce an explicit reference to the private subtype,
21295 -- to prevent scope anomalies in gigi if first use appears
21296 -- in a nested context, e.g. a later function body.
21297 -- Should this be generated in other contexts than a full
21298 -- type declaration?
21299
21300 if Is_Itype (Def_Id)
21301 and then
21302 Nkind (Parent (P)) = N_Full_Type_Declaration
21303 then
21304 Build_Itype_Reference (Def_Id, Parent (P));
21305 end if;
21306
21307 else
21308 Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
21309 end if;
21310
21311 when Concurrent_Kind =>
21312 Constrain_Concurrent (Def_Id, S,
21313 Related_Nod, Related_Id, Suffix);
21314
21315 when others =>
21316 Error_Msg_N ("invalid subtype mark in subtype indication", S);
21317 end case;
21318
21319 -- Size and Convention are always inherited from the base type
21320
21321 Set_Size_Info (Def_Id, (Subtype_Mark_Id));
21322 Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
21323
21324 return Def_Id;
21325 end if;
21326 end Process_Subtype;
21327
21328 -----------------------------
21329 -- Record_Type_Declaration --
21330 -----------------------------
21331
21332 procedure Record_Type_Declaration
21333 (T : Entity_Id;
21334 N : Node_Id;
21335 Prev : Entity_Id)
21336 is
21337 Def : constant Node_Id := Type_Definition (N);
21338 Is_Tagged : Boolean;
21339 Tag_Comp : Entity_Id;
21340
21341 begin
21342 -- These flags must be initialized before calling Process_Discriminants
21343 -- because this routine makes use of them.
21344
21345 Set_Ekind (T, E_Record_Type);
21346 Set_Etype (T, T);
21347 Init_Size_Align (T);
21348 Set_Interfaces (T, No_Elist);
21349 Set_Stored_Constraint (T, No_Elist);
21350 Set_Default_SSO (T);
21351
21352 -- Normal case
21353
21354 if Ada_Version < Ada_2005 or else not Interface_Present (Def) then
21355 if Limited_Present (Def) then
21356 Check_SPARK_05_Restriction ("limited is not allowed", N);
21357 end if;
21358
21359 if Abstract_Present (Def) then
21360 Check_SPARK_05_Restriction ("abstract is not allowed", N);
21361 end if;
21362
21363 -- The flag Is_Tagged_Type might have already been set by
21364 -- Find_Type_Name if it detected an error for declaration T. This
21365 -- arises in the case of private tagged types where the full view
21366 -- omits the word tagged.
21367
21368 Is_Tagged :=
21369 Tagged_Present (Def)
21370 or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
21371
21372 Set_Is_Limited_Record (T, Limited_Present (Def));
21373
21374 if Is_Tagged then
21375 Set_Is_Tagged_Type (T, True);
21376 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
21377 end if;
21378
21379 -- Type is abstract if full declaration carries keyword, or if
21380 -- previous partial view did.
21381
21382 Set_Is_Abstract_Type (T, Is_Abstract_Type (T)
21383 or else Abstract_Present (Def));
21384
21385 else
21386 Check_SPARK_05_Restriction ("interface is not allowed", N);
21387
21388 Is_Tagged := True;
21389 Analyze_Interface_Declaration (T, Def);
21390
21391 if Present (Discriminant_Specifications (N)) then
21392 Error_Msg_N
21393 ("interface types cannot have discriminants",
21394 Defining_Identifier
21395 (First (Discriminant_Specifications (N))));
21396 end if;
21397 end if;
21398
21399 -- First pass: if there are self-referential access components,
21400 -- create the required anonymous access type declarations, and if
21401 -- need be an incomplete type declaration for T itself.
21402
21403 Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
21404
21405 if Ada_Version >= Ada_2005
21406 and then Present (Interface_List (Def))
21407 then
21408 Check_Interfaces (N, Def);
21409
21410 declare
21411 Ifaces_List : Elist_Id;
21412
21413 begin
21414 -- Ada 2005 (AI-251): Collect the list of progenitors that are not
21415 -- already in the parents.
21416
21417 Collect_Interfaces
21418 (T => T,
21419 Ifaces_List => Ifaces_List,
21420 Exclude_Parents => True);
21421
21422 Set_Interfaces (T, Ifaces_List);
21423 end;
21424 end if;
21425
21426 -- Records constitute a scope for the component declarations within.
21427 -- The scope is created prior to the processing of these declarations.
21428 -- Discriminants are processed first, so that they are visible when
21429 -- processing the other components. The Ekind of the record type itself
21430 -- is set to E_Record_Type (subtypes appear as E_Record_Subtype).
21431
21432 -- Enter record scope
21433
21434 Push_Scope (T);
21435
21436 -- If an incomplete or private type declaration was already given for
21437 -- the type, then this scope already exists, and the discriminants have
21438 -- been declared within. We must verify that the full declaration
21439 -- matches the incomplete one.
21440
21441 Check_Or_Process_Discriminants (N, T, Prev);
21442
21443 Set_Is_Constrained (T, not Has_Discriminants (T));
21444 Set_Has_Delayed_Freeze (T, True);
21445
21446 -- For tagged types add a manually analyzed component corresponding
21447 -- to the component _tag, the corresponding piece of tree will be
21448 -- expanded as part of the freezing actions if it is not a CPP_Class.
21449
21450 if Is_Tagged then
21451
21452 -- Do not add the tag unless we are in expansion mode
21453
21454 if Expander_Active then
21455 Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
21456 Enter_Name (Tag_Comp);
21457
21458 Set_Ekind (Tag_Comp, E_Component);
21459 Set_Is_Tag (Tag_Comp);
21460 Set_Is_Aliased (Tag_Comp);
21461 Set_Etype (Tag_Comp, RTE (RE_Tag));
21462 Set_DT_Entry_Count (Tag_Comp, No_Uint);
21463 Set_Original_Record_Component (Tag_Comp, Tag_Comp);
21464 Init_Component_Location (Tag_Comp);
21465
21466 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
21467 -- implemented interfaces.
21468
21469 if Has_Interfaces (T) then
21470 Add_Interface_Tag_Components (N, T);
21471 end if;
21472 end if;
21473
21474 Make_Class_Wide_Type (T);
21475 Set_Direct_Primitive_Operations (T, New_Elmt_List);
21476 end if;
21477
21478 -- We must suppress range checks when processing record components in
21479 -- the presence of discriminants, since we don't want spurious checks to
21480 -- be generated during their analysis, but Suppress_Range_Checks flags
21481 -- must be reset the after processing the record definition.
21482
21483 -- Note: this is the only use of Kill_Range_Checks, and is a bit odd,
21484 -- couldn't we just use the normal range check suppression method here.
21485 -- That would seem cleaner ???
21486
21487 if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
21488 Set_Kill_Range_Checks (T, True);
21489 Record_Type_Definition (Def, Prev);
21490 Set_Kill_Range_Checks (T, False);
21491 else
21492 Record_Type_Definition (Def, Prev);
21493 end if;
21494
21495 -- Exit from record scope
21496
21497 End_Scope;
21498
21499 -- Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
21500 -- the implemented interfaces and associate them an aliased entity.
21501
21502 if Is_Tagged
21503 and then not Is_Empty_List (Interface_List (Def))
21504 then
21505 Derive_Progenitor_Subprograms (T, T);
21506 end if;
21507
21508 Check_Function_Writable_Actuals (N);
21509 end Record_Type_Declaration;
21510
21511 ----------------------------
21512 -- Record_Type_Definition --
21513 ----------------------------
21514
21515 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
21516 Component : Entity_Id;
21517 Ctrl_Components : Boolean := False;
21518 Final_Storage_Only : Boolean;
21519 T : Entity_Id;
21520
21521 begin
21522 if Ekind (Prev_T) = E_Incomplete_Type then
21523 T := Full_View (Prev_T);
21524 else
21525 T := Prev_T;
21526 end if;
21527
21528 -- In SPARK, tagged types and type extensions may only be declared in
21529 -- the specification of library unit packages.
21530
21531 if Present (Def) and then Is_Tagged_Type (T) then
21532 declare
21533 Typ : Node_Id;
21534 Ctxt : Node_Id;
21535
21536 begin
21537 if Nkind (Parent (Def)) = N_Full_Type_Declaration then
21538 Typ := Parent (Def);
21539 else
21540 pragma Assert
21541 (Nkind (Parent (Def)) = N_Derived_Type_Definition);
21542 Typ := Parent (Parent (Def));
21543 end if;
21544
21545 Ctxt := Parent (Typ);
21546
21547 if Nkind (Ctxt) = N_Package_Body
21548 and then Nkind (Parent (Ctxt)) = N_Compilation_Unit
21549 then
21550 Check_SPARK_05_Restriction
21551 ("type should be defined in package specification", Typ);
21552
21553 elsif Nkind (Ctxt) /= N_Package_Specification
21554 or else Nkind (Parent (Parent (Ctxt))) /= N_Compilation_Unit
21555 then
21556 Check_SPARK_05_Restriction
21557 ("type should be defined in library unit package", Typ);
21558 end if;
21559 end;
21560 end if;
21561
21562 Final_Storage_Only := not Is_Controlled_Active (T);
21563
21564 -- Ada 2005: Check whether an explicit Limited is present in a derived
21565 -- type declaration.
21566
21567 if Nkind (Parent (Def)) = N_Derived_Type_Definition
21568 and then Limited_Present (Parent (Def))
21569 then
21570 Set_Is_Limited_Record (T);
21571 end if;
21572
21573 -- If the component list of a record type is defined by the reserved
21574 -- word null and there is no discriminant part, then the record type has
21575 -- no components and all records of the type are null records (RM 3.7)
21576 -- This procedure is also called to process the extension part of a
21577 -- record extension, in which case the current scope may have inherited
21578 -- components.
21579
21580 if No (Def)
21581 or else No (Component_List (Def))
21582 or else Null_Present (Component_List (Def))
21583 then
21584 if not Is_Tagged_Type (T) then
21585 Check_SPARK_05_Restriction ("untagged record cannot be null", Def);
21586 end if;
21587
21588 else
21589 Analyze_Declarations (Component_Items (Component_List (Def)));
21590
21591 if Present (Variant_Part (Component_List (Def))) then
21592 Check_SPARK_05_Restriction ("variant part is not allowed", Def);
21593 Analyze (Variant_Part (Component_List (Def)));
21594 end if;
21595 end if;
21596
21597 -- After completing the semantic analysis of the record definition,
21598 -- record components, both new and inherited, are accessible. Set their
21599 -- kind accordingly. Exclude malformed itypes from illegal declarations,
21600 -- whose Ekind may be void.
21601
21602 Component := First_Entity (Current_Scope);
21603 while Present (Component) loop
21604 if Ekind (Component) = E_Void
21605 and then not Is_Itype (Component)
21606 then
21607 Set_Ekind (Component, E_Component);
21608 Init_Component_Location (Component);
21609 end if;
21610
21611 Propagate_Concurrent_Flags (T, Etype (Component));
21612
21613 if Ekind (Component) /= E_Component then
21614 null;
21615
21616 -- Do not set Has_Controlled_Component on a class-wide equivalent
21617 -- type. See Make_CW_Equivalent_Type.
21618
21619 elsif not Is_Class_Wide_Equivalent_Type (T)
21620 and then (Has_Controlled_Component (Etype (Component))
21621 or else (Chars (Component) /= Name_uParent
21622 and then Is_Controlled_Active
21623 (Etype (Component))))
21624 then
21625 Set_Has_Controlled_Component (T, True);
21626 Final_Storage_Only :=
21627 Final_Storage_Only
21628 and then Finalize_Storage_Only (Etype (Component));
21629 Ctrl_Components := True;
21630 end if;
21631
21632 Next_Entity (Component);
21633 end loop;
21634
21635 -- A Type is Finalize_Storage_Only only if all its controlled components
21636 -- are also.
21637
21638 if Ctrl_Components then
21639 Set_Finalize_Storage_Only (T, Final_Storage_Only);
21640 end if;
21641
21642 -- Place reference to end record on the proper entity, which may
21643 -- be a partial view.
21644
21645 if Present (Def) then
21646 Process_End_Label (Def, 'e', Prev_T);
21647 end if;
21648 end Record_Type_Definition;
21649
21650 ------------------------
21651 -- Replace_Components --
21652 ------------------------
21653
21654 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id) is
21655 function Process (N : Node_Id) return Traverse_Result;
21656
21657 -------------
21658 -- Process --
21659 -------------
21660
21661 function Process (N : Node_Id) return Traverse_Result is
21662 Comp : Entity_Id;
21663
21664 begin
21665 if Nkind (N) = N_Discriminant_Specification then
21666 Comp := First_Discriminant (Typ);
21667 while Present (Comp) loop
21668 if Chars (Comp) = Chars (Defining_Identifier (N)) then
21669 Set_Defining_Identifier (N, Comp);
21670 exit;
21671 end if;
21672
21673 Next_Discriminant (Comp);
21674 end loop;
21675
21676 elsif Nkind (N) = N_Component_Declaration then
21677 Comp := First_Component (Typ);
21678 while Present (Comp) loop
21679 if Chars (Comp) = Chars (Defining_Identifier (N)) then
21680 Set_Defining_Identifier (N, Comp);
21681 exit;
21682 end if;
21683
21684 Next_Component (Comp);
21685 end loop;
21686 end if;
21687
21688 return OK;
21689 end Process;
21690
21691 procedure Replace is new Traverse_Proc (Process);
21692
21693 -- Start of processing for Replace_Components
21694
21695 begin
21696 Replace (Decl);
21697 end Replace_Components;
21698
21699 -------------------------------
21700 -- Set_Completion_Referenced --
21701 -------------------------------
21702
21703 procedure Set_Completion_Referenced (E : Entity_Id) is
21704 begin
21705 -- If in main unit, mark entity that is a completion as referenced,
21706 -- warnings go on the partial view when needed.
21707
21708 if In_Extended_Main_Source_Unit (E) then
21709 Set_Referenced (E);
21710 end if;
21711 end Set_Completion_Referenced;
21712
21713 ---------------------
21714 -- Set_Default_SSO --
21715 ---------------------
21716
21717 procedure Set_Default_SSO (T : Entity_Id) is
21718 begin
21719 case Opt.Default_SSO is
21720 when ' ' =>
21721 null;
21722 when 'L' =>
21723 Set_SSO_Set_Low_By_Default (T, True);
21724 when 'H' =>
21725 Set_SSO_Set_High_By_Default (T, True);
21726 when others =>
21727 raise Program_Error;
21728 end case;
21729 end Set_Default_SSO;
21730
21731 ---------------------
21732 -- Set_Fixed_Range --
21733 ---------------------
21734
21735 -- The range for fixed-point types is complicated by the fact that we
21736 -- do not know the exact end points at the time of the declaration. This
21737 -- is true for three reasons:
21738
21739 -- A size clause may affect the fudging of the end-points.
21740 -- A small clause may affect the values of the end-points.
21741 -- We try to include the end-points if it does not affect the size.
21742
21743 -- This means that the actual end-points must be established at the
21744 -- point when the type is frozen. Meanwhile, we first narrow the range
21745 -- as permitted (so that it will fit if necessary in a small specified
21746 -- size), and then build a range subtree with these narrowed bounds.
21747 -- Set_Fixed_Range constructs the range from real literal values, and
21748 -- sets the range as the Scalar_Range of the given fixed-point type entity.
21749
21750 -- The parent of this range is set to point to the entity so that it is
21751 -- properly hooked into the tree (unlike normal Scalar_Range entries for
21752 -- other scalar types, which are just pointers to the range in the
21753 -- original tree, this would otherwise be an orphan).
21754
21755 -- The tree is left unanalyzed. When the type is frozen, the processing
21756 -- in Freeze.Freeze_Fixed_Point_Type notices that the range is not
21757 -- analyzed, and uses this as an indication that it should complete
21758 -- work on the range (it will know the final small and size values).
21759
21760 procedure Set_Fixed_Range
21761 (E : Entity_Id;
21762 Loc : Source_Ptr;
21763 Lo : Ureal;
21764 Hi : Ureal)
21765 is
21766 S : constant Node_Id :=
21767 Make_Range (Loc,
21768 Low_Bound => Make_Real_Literal (Loc, Lo),
21769 High_Bound => Make_Real_Literal (Loc, Hi));
21770 begin
21771 Set_Scalar_Range (E, S);
21772 Set_Parent (S, E);
21773
21774 -- Before the freeze point, the bounds of a fixed point are universal
21775 -- and carry the corresponding type.
21776
21777 Set_Etype (Low_Bound (S), Universal_Real);
21778 Set_Etype (High_Bound (S), Universal_Real);
21779 end Set_Fixed_Range;
21780
21781 ----------------------------------
21782 -- Set_Scalar_Range_For_Subtype --
21783 ----------------------------------
21784
21785 procedure Set_Scalar_Range_For_Subtype
21786 (Def_Id : Entity_Id;
21787 R : Node_Id;
21788 Subt : Entity_Id)
21789 is
21790 Kind : constant Entity_Kind := Ekind (Def_Id);
21791
21792 begin
21793 -- Defend against previous error
21794
21795 if Nkind (R) = N_Error then
21796 return;
21797 end if;
21798
21799 Set_Scalar_Range (Def_Id, R);
21800
21801 -- We need to link the range into the tree before resolving it so
21802 -- that types that are referenced, including importantly the subtype
21803 -- itself, are properly frozen (Freeze_Expression requires that the
21804 -- expression be properly linked into the tree). Of course if it is
21805 -- already linked in, then we do not disturb the current link.
21806
21807 if No (Parent (R)) then
21808 Set_Parent (R, Def_Id);
21809 end if;
21810
21811 -- Reset the kind of the subtype during analysis of the range, to
21812 -- catch possible premature use in the bounds themselves.
21813
21814 Set_Ekind (Def_Id, E_Void);
21815 Process_Range_Expr_In_Decl (R, Subt, Subtyp => Def_Id);
21816 Set_Ekind (Def_Id, Kind);
21817 end Set_Scalar_Range_For_Subtype;
21818
21819 --------------------------------------------------------
21820 -- Set_Stored_Constraint_From_Discriminant_Constraint --
21821 --------------------------------------------------------
21822
21823 procedure Set_Stored_Constraint_From_Discriminant_Constraint
21824 (E : Entity_Id)
21825 is
21826 begin
21827 -- Make sure set if encountered during Expand_To_Stored_Constraint
21828
21829 Set_Stored_Constraint (E, No_Elist);
21830
21831 -- Give it the right value
21832
21833 if Is_Constrained (E) and then Has_Discriminants (E) then
21834 Set_Stored_Constraint (E,
21835 Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
21836 end if;
21837 end Set_Stored_Constraint_From_Discriminant_Constraint;
21838
21839 -------------------------------------
21840 -- Signed_Integer_Type_Declaration --
21841 -------------------------------------
21842
21843 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
21844 Implicit_Base : Entity_Id;
21845 Base_Typ : Entity_Id;
21846 Lo_Val : Uint;
21847 Hi_Val : Uint;
21848 Errs : Boolean := False;
21849 Lo : Node_Id;
21850 Hi : Node_Id;
21851
21852 function Can_Derive_From (E : Entity_Id) return Boolean;
21853 -- Determine whether given bounds allow derivation from specified type
21854
21855 procedure Check_Bound (Expr : Node_Id);
21856 -- Check bound to make sure it is integral and static. If not, post
21857 -- appropriate error message and set Errs flag
21858
21859 ---------------------
21860 -- Can_Derive_From --
21861 ---------------------
21862
21863 -- Note we check both bounds against both end values, to deal with
21864 -- strange types like ones with a range of 0 .. -12341234.
21865
21866 function Can_Derive_From (E : Entity_Id) return Boolean is
21867 Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
21868 Hi : constant Uint := Expr_Value (Type_High_Bound (E));
21869 begin
21870 return Lo <= Lo_Val and then Lo_Val <= Hi
21871 and then
21872 Lo <= Hi_Val and then Hi_Val <= Hi;
21873 end Can_Derive_From;
21874
21875 -----------------
21876 -- Check_Bound --
21877 -----------------
21878
21879 procedure Check_Bound (Expr : Node_Id) is
21880 begin
21881 -- If a range constraint is used as an integer type definition, each
21882 -- bound of the range must be defined by a static expression of some
21883 -- integer type, but the two bounds need not have the same integer
21884 -- type (Negative bounds are allowed.) (RM 3.5.4)
21885
21886 if not Is_Integer_Type (Etype (Expr)) then
21887 Error_Msg_N
21888 ("integer type definition bounds must be of integer type", Expr);
21889 Errs := True;
21890
21891 elsif not Is_OK_Static_Expression (Expr) then
21892 Flag_Non_Static_Expr
21893 ("non-static expression used for integer type bound!", Expr);
21894 Errs := True;
21895
21896 -- The bounds are folded into literals, and we set their type to be
21897 -- universal, to avoid typing difficulties: we cannot set the type
21898 -- of the literal to the new type, because this would be a forward
21899 -- reference for the back end, and if the original type is user-
21900 -- defined this can lead to spurious semantic errors (e.g. 2928-003).
21901
21902 else
21903 if Is_Entity_Name (Expr) then
21904 Fold_Uint (Expr, Expr_Value (Expr), True);
21905 end if;
21906
21907 Set_Etype (Expr, Universal_Integer);
21908 end if;
21909 end Check_Bound;
21910
21911 -- Start of processing for Signed_Integer_Type_Declaration
21912
21913 begin
21914 -- Create an anonymous base type
21915
21916 Implicit_Base :=
21917 Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
21918
21919 -- Analyze and check the bounds, they can be of any integer type
21920
21921 Lo := Low_Bound (Def);
21922 Hi := High_Bound (Def);
21923
21924 -- Arbitrarily use Integer as the type if either bound had an error
21925
21926 if Hi = Error or else Lo = Error then
21927 Base_Typ := Any_Integer;
21928 Set_Error_Posted (T, True);
21929
21930 -- Here both bounds are OK expressions
21931
21932 else
21933 Analyze_And_Resolve (Lo, Any_Integer);
21934 Analyze_And_Resolve (Hi, Any_Integer);
21935
21936 Check_Bound (Lo);
21937 Check_Bound (Hi);
21938
21939 if Errs then
21940 Hi := Type_High_Bound (Standard_Long_Long_Integer);
21941 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
21942 end if;
21943
21944 -- Find type to derive from
21945
21946 Lo_Val := Expr_Value (Lo);
21947 Hi_Val := Expr_Value (Hi);
21948
21949 if Can_Derive_From (Standard_Short_Short_Integer) then
21950 Base_Typ := Base_Type (Standard_Short_Short_Integer);
21951
21952 elsif Can_Derive_From (Standard_Short_Integer) then
21953 Base_Typ := Base_Type (Standard_Short_Integer);
21954
21955 elsif Can_Derive_From (Standard_Integer) then
21956 Base_Typ := Base_Type (Standard_Integer);
21957
21958 elsif Can_Derive_From (Standard_Long_Integer) then
21959 Base_Typ := Base_Type (Standard_Long_Integer);
21960
21961 elsif Can_Derive_From (Standard_Long_Long_Integer) then
21962 Check_Restriction (No_Long_Long_Integers, Def);
21963 Base_Typ := Base_Type (Standard_Long_Long_Integer);
21964
21965 else
21966 Base_Typ := Base_Type (Standard_Long_Long_Integer);
21967 Error_Msg_N ("integer type definition bounds out of range", Def);
21968 Hi := Type_High_Bound (Standard_Long_Long_Integer);
21969 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
21970 end if;
21971 end if;
21972
21973 -- Complete both implicit base and declared first subtype entities. The
21974 -- inheritance of the rep item chain ensures that SPARK-related pragmas
21975 -- are not clobbered when the signed integer type acts as a full view of
21976 -- a private type.
21977
21978 Set_Etype (Implicit_Base, Base_Typ);
21979 Set_Size_Info (Implicit_Base, Base_Typ);
21980 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
21981 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
21982 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
21983
21984 Set_Ekind (T, E_Signed_Integer_Subtype);
21985 Set_Etype (T, Implicit_Base);
21986 Set_Size_Info (T, Implicit_Base);
21987 Inherit_Rep_Item_Chain (T, Implicit_Base);
21988 Set_Scalar_Range (T, Def);
21989 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
21990 Set_Is_Constrained (T);
21991 end Signed_Integer_Type_Declaration;
21992
21993 end Sem_Ch3;