sem_ch3.adb (Analyze_Object_Declaration): New function Has_Delayed_Aspect...
[gcc.git] / gcc / ada / sem_ch3.adb
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ C H 3 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2015, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
25
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Debug; use Debug;
30 with Elists; use Elists;
31 with Einfo; use Einfo;
32 with Errout; use Errout;
33 with Eval_Fat; use Eval_Fat;
34 with Exp_Ch3; use Exp_Ch3;
35 with Exp_Ch9; use Exp_Ch9;
36 with Exp_Disp; use Exp_Disp;
37 with Exp_Dist; use Exp_Dist;
38 with Exp_Tss; use Exp_Tss;
39 with Exp_Util; use Exp_Util;
40 with Fname; use Fname;
41 with Freeze; use Freeze;
42 with Ghost; use Ghost;
43 with Itypes; use Itypes;
44 with Layout; use Layout;
45 with Lib; use Lib;
46 with Lib.Xref; use Lib.Xref;
47 with Namet; use Namet;
48 with Nmake; use Nmake;
49 with Opt; use Opt;
50 with Restrict; use Restrict;
51 with Rident; use Rident;
52 with Rtsfind; use Rtsfind;
53 with Sem; use Sem;
54 with Sem_Aux; use Sem_Aux;
55 with Sem_Case; use Sem_Case;
56 with Sem_Cat; use Sem_Cat;
57 with Sem_Ch6; use Sem_Ch6;
58 with Sem_Ch7; use Sem_Ch7;
59 with Sem_Ch8; use Sem_Ch8;
60 with Sem_Ch10; use Sem_Ch10;
61 with Sem_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_Prag; use Sem_Prag;
69 with Sem_Res; use Sem_Res;
70 with Sem_Smem; use Sem_Smem;
71 with Sem_Type; use Sem_Type;
72 with Sem_Util; use Sem_Util;
73 with Sem_Warn; use Sem_Warn;
74 with Stand; use Stand;
75 with Sinfo; use Sinfo;
76 with Sinput; use Sinput;
77 with Snames; use Snames;
78 with Targparm; use Targparm;
79 with Tbuild; use Tbuild;
80 with Ttypes; use Ttypes;
81 with Uintp; use Uintp;
82 with Urealp; use Urealp;
83
84 package body Sem_Ch3 is
85
86 -----------------------
87 -- Local Subprograms --
88 -----------------------
89
90 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id);
91 -- Ada 2005 (AI-251): Add the tag components corresponding to all the
92 -- abstract interface types implemented by a record type or a derived
93 -- record type.
94
95 procedure Analyze_Object_Contract (Obj_Id : Entity_Id);
96 -- Analyze all delayed pragmas chained on the contract of object Obj_Id as
97 -- if they appeared at the end of the declarative region. The pragmas to be
98 -- considered are:
99 -- Async_Readers
100 -- Async_Writers
101 -- Effective_Reads
102 -- Effective_Writes
103 -- Part_Of
104
105 procedure Build_Derived_Type
106 (N : Node_Id;
107 Parent_Type : Entity_Id;
108 Derived_Type : Entity_Id;
109 Is_Completion : Boolean;
110 Derive_Subps : Boolean := True);
111 -- Create and decorate a Derived_Type given the Parent_Type entity. N is
112 -- the N_Full_Type_Declaration node containing the derived type definition.
113 -- Parent_Type is the entity for the parent type in the derived type
114 -- definition and Derived_Type the actual derived type. Is_Completion must
115 -- be set to False if Derived_Type is the N_Defining_Identifier node in N
116 -- (i.e. Derived_Type = Defining_Identifier (N)). In this case N is not the
117 -- completion of a private type declaration. If Is_Completion is set to
118 -- True, N is the completion of a private type declaration and Derived_Type
119 -- is different from the defining identifier inside N (i.e. Derived_Type /=
120 -- Defining_Identifier (N)). Derive_Subps indicates whether the parent
121 -- subprograms should be derived. The only case where this parameter is
122 -- False is when Build_Derived_Type is recursively called to process an
123 -- implicit derived full type for a type derived from a private type (in
124 -- that case the subprograms must only be derived for the private view of
125 -- the type).
126 --
127 -- ??? These flags need a bit of re-examination and re-documentation:
128 -- ??? are they both necessary (both seem related to the recursion)?
129
130 procedure Build_Derived_Access_Type
131 (N : Node_Id;
132 Parent_Type : Entity_Id;
133 Derived_Type : Entity_Id);
134 -- Subsidiary procedure to Build_Derived_Type. For a derived access type,
135 -- create an implicit base if the parent type is constrained or if the
136 -- subtype indication has a constraint.
137
138 procedure Build_Derived_Array_Type
139 (N : Node_Id;
140 Parent_Type : Entity_Id;
141 Derived_Type : Entity_Id);
142 -- Subsidiary procedure to Build_Derived_Type. For a derived array type,
143 -- create an implicit base if the parent type is constrained or if the
144 -- subtype indication has a constraint.
145
146 procedure Build_Derived_Concurrent_Type
147 (N : Node_Id;
148 Parent_Type : Entity_Id;
149 Derived_Type : Entity_Id);
150 -- Subsidiary procedure to Build_Derived_Type. For a derived task or
151 -- protected type, inherit entries and protected subprograms, check
152 -- legality of discriminant constraints if any.
153
154 procedure Build_Derived_Enumeration_Type
155 (N : Node_Id;
156 Parent_Type : Entity_Id;
157 Derived_Type : Entity_Id);
158 -- Subsidiary procedure to Build_Derived_Type. For a derived enumeration
159 -- type, we must create a new list of literals. Types derived from
160 -- Character and [Wide_]Wide_Character are special-cased.
161
162 procedure Build_Derived_Numeric_Type
163 (N : Node_Id;
164 Parent_Type : Entity_Id;
165 Derived_Type : Entity_Id);
166 -- Subsidiary procedure to Build_Derived_Type. For numeric types, create
167 -- an anonymous base type, and propagate constraint to subtype if needed.
168
169 procedure Build_Derived_Private_Type
170 (N : Node_Id;
171 Parent_Type : Entity_Id;
172 Derived_Type : Entity_Id;
173 Is_Completion : Boolean;
174 Derive_Subps : Boolean := True);
175 -- Subsidiary procedure to Build_Derived_Type. This procedure is complex
176 -- because the parent may or may not have a completion, and the derivation
177 -- may itself be a completion.
178
179 procedure Build_Derived_Record_Type
180 (N : Node_Id;
181 Parent_Type : Entity_Id;
182 Derived_Type : Entity_Id;
183 Derive_Subps : Boolean := True);
184 -- Subsidiary procedure used for tagged and untagged record types
185 -- by Build_Derived_Type and Analyze_Private_Extension_Declaration.
186 -- All parameters are as in Build_Derived_Type except that N, in
187 -- addition to being an N_Full_Type_Declaration node, can also be an
188 -- N_Private_Extension_Declaration node. See the definition of this routine
189 -- for much more info. Derive_Subps indicates whether subprograms should be
190 -- derived from the parent type. The only case where Derive_Subps is False
191 -- is for an implicit derived full type for a type derived from a private
192 -- type (see Build_Derived_Type).
193
194 procedure Build_Discriminal (Discrim : Entity_Id);
195 -- Create the discriminal corresponding to discriminant Discrim, that is
196 -- the parameter corresponding to Discrim to be used in initialization
197 -- procedures for the type where Discrim is a discriminant. Discriminals
198 -- are not used during semantic analysis, and are not fully defined
199 -- entities until expansion. Thus they are not given a scope until
200 -- initialization procedures are built.
201
202 function Build_Discriminant_Constraints
203 (T : Entity_Id;
204 Def : Node_Id;
205 Derived_Def : Boolean := False) return Elist_Id;
206 -- Validate discriminant constraints and return the list of the constraints
207 -- in order of discriminant declarations, where T is the discriminated
208 -- unconstrained type. Def is the N_Subtype_Indication node where the
209 -- discriminants constraints for T are specified. Derived_Def is True
210 -- when building the discriminant constraints in a derived type definition
211 -- of the form "type D (...) is new T (xxx)". In this case T is the parent
212 -- type and Def is the constraint "(xxx)" on T and this routine sets the
213 -- Corresponding_Discriminant field of the discriminants in the derived
214 -- type D to point to the corresponding discriminants in the parent type T.
215
216 procedure Build_Discriminated_Subtype
217 (T : Entity_Id;
218 Def_Id : Entity_Id;
219 Elist : Elist_Id;
220 Related_Nod : Node_Id;
221 For_Access : Boolean := False);
222 -- Subsidiary procedure to Constrain_Discriminated_Type and to
223 -- Process_Incomplete_Dependents. Given
224 --
225 -- T (a possibly discriminated base type)
226 -- Def_Id (a very partially built subtype for T),
227 --
228 -- the call completes Def_Id to be the appropriate E_*_Subtype.
229 --
230 -- The Elist is the list of discriminant constraints if any (it is set
231 -- to No_Elist if T is not a discriminated type, and to an empty list if
232 -- T has discriminants but there are no discriminant constraints). The
233 -- Related_Nod is the same as Decl_Node in Create_Constrained_Components.
234 -- The For_Access says whether or not this subtype is really constraining
235 -- an access type. That is its sole purpose is the designated type of an
236 -- access type -- in which case a Private_Subtype Is_For_Access_Subtype
237 -- is built to avoid freezing T when the access subtype is frozen.
238
239 function Build_Scalar_Bound
240 (Bound : Node_Id;
241 Par_T : Entity_Id;
242 Der_T : Entity_Id) return Node_Id;
243 -- The bounds of a derived scalar type are conversions of the bounds of
244 -- the parent type. Optimize the representation if the bounds are literals.
245 -- Needs a more complete spec--what are the parameters exactly, and what
246 -- exactly is the returned value, and how is Bound affected???
247
248 procedure Build_Underlying_Full_View
249 (N : Node_Id;
250 Typ : Entity_Id;
251 Par : Entity_Id);
252 -- If the completion of a private type is itself derived from a private
253 -- type, or if the full view of a private subtype is itself private, the
254 -- back-end has no way to compute the actual size of this type. We build
255 -- an internal subtype declaration of the proper parent type to convey
256 -- this information. This extra mechanism is needed because a full
257 -- view cannot itself have a full view (it would get clobbered during
258 -- view exchanges).
259
260 procedure Check_Access_Discriminant_Requires_Limited
261 (D : Node_Id;
262 Loc : Node_Id);
263 -- Check the restriction that the type to which an access discriminant
264 -- belongs must be a concurrent type or a descendant of a type with
265 -- the reserved word 'limited' in its declaration.
266
267 procedure Check_Anonymous_Access_Components
268 (Typ_Decl : Node_Id;
269 Typ : Entity_Id;
270 Prev : Entity_Id;
271 Comp_List : Node_Id);
272 -- Ada 2005 AI-382: an access component in a record definition can refer to
273 -- the enclosing record, in which case it denotes the type itself, and not
274 -- the current instance of the type. We create an anonymous access type for
275 -- the component, and flag it as an access to a component, so accessibility
276 -- checks are properly performed on it. The declaration of the access type
277 -- is placed ahead of that of the record to prevent order-of-elaboration
278 -- circularity issues in Gigi. We create an incomplete type for the record
279 -- declaration, which is the designated type of the anonymous access.
280
281 procedure Check_Delta_Expression (E : Node_Id);
282 -- Check that the expression represented by E is suitable for use as a
283 -- delta expression, i.e. it is of real type and is static.
284
285 procedure Check_Digits_Expression (E : Node_Id);
286 -- Check that the expression represented by E is suitable for use as a
287 -- digits expression, i.e. it is of integer type, positive and static.
288
289 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id);
290 -- Validate the initialization of an object declaration. T is the required
291 -- type, and Exp is the initialization expression.
292
293 procedure Check_Interfaces (N : Node_Id; Def : Node_Id);
294 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
295
296 procedure Check_Or_Process_Discriminants
297 (N : Node_Id;
298 T : Entity_Id;
299 Prev : Entity_Id := Empty);
300 -- If N is the full declaration of the completion T of an incomplete or
301 -- private type, check its discriminants (which are already known to be
302 -- conformant with those of the partial view, see Find_Type_Name),
303 -- otherwise process them. Prev is the entity of the partial declaration,
304 -- if any.
305
306 procedure Check_Real_Bound (Bound : Node_Id);
307 -- Check given bound for being of real type and static. If not, post an
308 -- appropriate message, and rewrite the bound with the real literal zero.
309
310 procedure Constant_Redeclaration
311 (Id : Entity_Id;
312 N : Node_Id;
313 T : out Entity_Id);
314 -- Various checks on legality of full declaration of deferred constant.
315 -- Id is the entity for the redeclaration, N is the N_Object_Declaration,
316 -- node. The caller has not yet set any attributes of this entity.
317
318 function Contain_Interface
319 (Iface : Entity_Id;
320 Ifaces : Elist_Id) return Boolean;
321 -- Ada 2005: Determine whether Iface is present in the list Ifaces
322
323 procedure Convert_Scalar_Bounds
324 (N : Node_Id;
325 Parent_Type : Entity_Id;
326 Derived_Type : Entity_Id;
327 Loc : Source_Ptr);
328 -- For derived scalar types, convert the bounds in the type definition to
329 -- the derived type, and complete their analysis. Given a constraint of the
330 -- form ".. new T range Lo .. Hi", Lo and Hi are analyzed and resolved with
331 -- T'Base, the parent_type. The bounds of the derived type (the anonymous
332 -- base) are copies of Lo and Hi. Finally, the bounds of the derived
333 -- subtype are conversions of those bounds to the derived_type, so that
334 -- their typing is consistent.
335
336 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id);
337 -- Copies attributes from array base type T2 to array base type T1. Copies
338 -- only attributes that apply to base types, but not subtypes.
339
340 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id);
341 -- Copies attributes from array subtype T2 to array subtype T1. Copies
342 -- attributes that apply to both subtypes and base types.
343
344 procedure Create_Constrained_Components
345 (Subt : Entity_Id;
346 Decl_Node : Node_Id;
347 Typ : Entity_Id;
348 Constraints : Elist_Id);
349 -- Build the list of entities for a constrained discriminated record
350 -- subtype. If a component depends on a discriminant, replace its subtype
351 -- using the discriminant values in the discriminant constraint. Subt
352 -- is the defining identifier for the subtype whose list of constrained
353 -- entities we will create. Decl_Node is the type declaration node where
354 -- we will attach all the itypes created. Typ is the base discriminated
355 -- type for the subtype Subt. Constraints is the list of discriminant
356 -- constraints for Typ.
357
358 function Constrain_Component_Type
359 (Comp : Entity_Id;
360 Constrained_Typ : Entity_Id;
361 Related_Node : Node_Id;
362 Typ : Entity_Id;
363 Constraints : Elist_Id) return Entity_Id;
364 -- Given a discriminated base type Typ, a list of discriminant constraints,
365 -- Constraints, for Typ and a component Comp of Typ, create and return the
366 -- type corresponding to Etype (Comp) where all discriminant references
367 -- are replaced with the corresponding constraint. If Etype (Comp) contains
368 -- no discriminant references then it is returned as-is. Constrained_Typ
369 -- is the final constrained subtype to which the constrained component
370 -- belongs. Related_Node is the node where we attach all created itypes.
371
372 procedure Constrain_Access
373 (Def_Id : in out Entity_Id;
374 S : Node_Id;
375 Related_Nod : Node_Id);
376 -- Apply a list of constraints to an access type. If Def_Id is empty, it is
377 -- an anonymous type created for a subtype indication. In that case it is
378 -- created in the procedure and attached to Related_Nod.
379
380 procedure Constrain_Array
381 (Def_Id : in out Entity_Id;
382 SI : Node_Id;
383 Related_Nod : Node_Id;
384 Related_Id : Entity_Id;
385 Suffix : Character);
386 -- Apply a list of index constraints to an unconstrained array type. The
387 -- first parameter is the entity for the resulting subtype. A value of
388 -- Empty for Def_Id indicates that an implicit type must be created, but
389 -- creation is delayed (and must be done by this procedure) because other
390 -- subsidiary implicit types must be created first (which is why Def_Id
391 -- is an in/out parameter). The second parameter is a subtype indication
392 -- node for the constrained array to be created (e.g. something of the
393 -- form string (1 .. 10)). Related_Nod gives the place where this type
394 -- has to be inserted in the tree. The Related_Id and Suffix parameters
395 -- are used to build the associated Implicit type name.
396
397 procedure Constrain_Concurrent
398 (Def_Id : in out Entity_Id;
399 SI : Node_Id;
400 Related_Nod : Node_Id;
401 Related_Id : Entity_Id;
402 Suffix : Character);
403 -- Apply list of discriminant constraints to an unconstrained concurrent
404 -- type.
405 --
406 -- SI is the N_Subtype_Indication node containing the constraint and
407 -- the unconstrained type to constrain.
408 --
409 -- Def_Id is the entity for the resulting constrained subtype. A value
410 -- of Empty for Def_Id indicates that an implicit type must be created,
411 -- but creation is delayed (and must be done by this procedure) because
412 -- other subsidiary implicit types must be created first (which is why
413 -- Def_Id is an in/out parameter).
414 --
415 -- Related_Nod gives the place where this type has to be inserted
416 -- in the tree.
417 --
418 -- The last two arguments are used to create its external name if needed.
419
420 function Constrain_Corresponding_Record
421 (Prot_Subt : Entity_Id;
422 Corr_Rec : Entity_Id;
423 Related_Nod : Node_Id) return Entity_Id;
424 -- When constraining a protected type or task type with discriminants,
425 -- constrain the corresponding record with the same discriminant values.
426
427 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id);
428 -- Constrain a decimal fixed point type with a digits constraint and/or a
429 -- range constraint, and build E_Decimal_Fixed_Point_Subtype entity.
430
431 procedure Constrain_Discriminated_Type
432 (Def_Id : Entity_Id;
433 S : Node_Id;
434 Related_Nod : Node_Id;
435 For_Access : Boolean := False);
436 -- Process discriminant constraints of composite type. Verify that values
437 -- have been provided for all discriminants, that the original type is
438 -- unconstrained, and that the types of the supplied expressions match
439 -- the discriminant types. The first three parameters are like in routine
440 -- Constrain_Concurrent. See Build_Discriminated_Subtype for an explanation
441 -- of For_Access.
442
443 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id);
444 -- Constrain an enumeration type with a range constraint. This is identical
445 -- to Constrain_Integer, but for the Ekind of the resulting subtype.
446
447 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id);
448 -- Constrain a floating point type with either a digits constraint
449 -- and/or a range constraint, building a E_Floating_Point_Subtype.
450
451 procedure Constrain_Index
452 (Index : Node_Id;
453 S : Node_Id;
454 Related_Nod : Node_Id;
455 Related_Id : Entity_Id;
456 Suffix : Character;
457 Suffix_Index : Nat);
458 -- Process an index constraint S in a constrained array declaration. The
459 -- constraint can be a subtype name, or a range with or without an explicit
460 -- subtype mark. The index is the corresponding index of the unconstrained
461 -- array. The Related_Id and Suffix parameters are used to build the
462 -- associated Implicit type name.
463
464 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id);
465 -- Build subtype of a signed or modular integer type
466
467 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id);
468 -- Constrain an ordinary fixed point type with a range constraint, and
469 -- build an E_Ordinary_Fixed_Point_Subtype entity.
470
471 procedure Copy_And_Swap (Priv, Full : Entity_Id);
472 -- Copy the Priv entity into the entity of its full declaration then swap
473 -- the two entities in such a manner that the former private type is now
474 -- seen as a full type.
475
476 procedure Decimal_Fixed_Point_Type_Declaration
477 (T : Entity_Id;
478 Def : Node_Id);
479 -- Create a new decimal fixed point type, and apply the constraint to
480 -- obtain a subtype of this new type.
481
482 procedure Complete_Private_Subtype
483 (Priv : Entity_Id;
484 Full : Entity_Id;
485 Full_Base : Entity_Id;
486 Related_Nod : Node_Id);
487 -- Complete the implicit full view of a private subtype by setting the
488 -- appropriate semantic fields. If the full view of the parent is a record
489 -- type, build constrained components of subtype.
490
491 procedure Derive_Progenitor_Subprograms
492 (Parent_Type : Entity_Id;
493 Tagged_Type : Entity_Id);
494 -- Ada 2005 (AI-251): To complete type derivation, collect the primitive
495 -- operations of progenitors of Tagged_Type, and replace the subsidiary
496 -- subtypes with Tagged_Type, to build the specs of the inherited interface
497 -- primitives. The derived primitives are aliased to those of the
498 -- interface. This routine takes care also of transferring to the full view
499 -- subprograms associated with the partial view of Tagged_Type that cover
500 -- interface primitives.
501
502 procedure Derived_Standard_Character
503 (N : Node_Id;
504 Parent_Type : Entity_Id;
505 Derived_Type : Entity_Id);
506 -- Subsidiary procedure to Build_Derived_Enumeration_Type which handles
507 -- derivations from types Standard.Character and Standard.Wide_Character.
508
509 procedure Derived_Type_Declaration
510 (T : Entity_Id;
511 N : Node_Id;
512 Is_Completion : Boolean);
513 -- Process a derived type declaration. Build_Derived_Type is invoked
514 -- to process the actual derived type definition. Parameters N and
515 -- Is_Completion have the same meaning as in Build_Derived_Type.
516 -- T is the N_Defining_Identifier for the entity defined in the
517 -- N_Full_Type_Declaration node N, that is T is the derived type.
518
519 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id);
520 -- Insert each literal in symbol table, as an overloadable identifier. Each
521 -- enumeration type is mapped into a sequence of integers, and each literal
522 -- is defined as a constant with integer value. If any of the literals are
523 -- character literals, the type is a character type, which means that
524 -- strings are legal aggregates for arrays of components of the type.
525
526 function Expand_To_Stored_Constraint
527 (Typ : Entity_Id;
528 Constraint : Elist_Id) return Elist_Id;
529 -- Given a constraint (i.e. a list of expressions) on the discriminants of
530 -- Typ, expand it into a constraint on the stored discriminants and return
531 -- the new list of expressions constraining the stored discriminants.
532
533 function Find_Type_Of_Object
534 (Obj_Def : Node_Id;
535 Related_Nod : Node_Id) return Entity_Id;
536 -- Get type entity for object referenced by Obj_Def, attaching the implicit
537 -- types generated to Related_Nod.
538
539 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id);
540 -- Create a new float and apply the constraint to obtain subtype of it
541
542 function Has_Range_Constraint (N : Node_Id) return Boolean;
543 -- Given an N_Subtype_Indication node N, return True if a range constraint
544 -- is present, either directly, or as part of a digits or delta constraint.
545 -- In addition, a digits constraint in the decimal case returns True, since
546 -- it establishes a default range if no explicit range is present.
547
548 function Inherit_Components
549 (N : Node_Id;
550 Parent_Base : Entity_Id;
551 Derived_Base : Entity_Id;
552 Is_Tagged : Boolean;
553 Inherit_Discr : Boolean;
554 Discs : Elist_Id) return Elist_Id;
555 -- Called from Build_Derived_Record_Type to inherit the components of
556 -- Parent_Base (a base type) into the Derived_Base (the derived base type).
557 -- For more information on derived types and component inheritance please
558 -- consult the comment above the body of Build_Derived_Record_Type.
559 --
560 -- N is the original derived type declaration
561 --
562 -- Is_Tagged is set if we are dealing with tagged types
563 --
564 -- If Inherit_Discr is set, Derived_Base inherits its discriminants from
565 -- Parent_Base, otherwise no discriminants are inherited.
566 --
567 -- Discs gives the list of constraints that apply to Parent_Base in the
568 -- derived type declaration. If Discs is set to No_Elist, then we have
569 -- the following situation:
570 --
571 -- type Parent (D1..Dn : ..) is [tagged] record ...;
572 -- type Derived is new Parent [with ...];
573 --
574 -- which gets treated as
575 --
576 -- type Derived (D1..Dn : ..) is new Parent (D1,..,Dn) [with ...];
577 --
578 -- For untagged types the returned value is an association list. The list
579 -- starts from the association (Parent_Base => Derived_Base), and then it
580 -- contains a sequence of the associations of the form
581 --
582 -- (Old_Component => New_Component),
583 --
584 -- where Old_Component is the Entity_Id of a component in Parent_Base and
585 -- New_Component is the Entity_Id of the corresponding component in
586 -- Derived_Base. For untagged records, this association list is needed when
587 -- copying the record declaration for the derived base. In the tagged case
588 -- the value returned is irrelevant.
589
590 procedure Inherit_Predicate_Flags (Subt, Par : Entity_Id);
591 -- Propagate static and dynamic predicate flags from a parent to the
592 -- subtype in a subtype declaration with and without constraints.
593
594 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean;
595 -- Subsidiary to Check_Abstract_Overriding and Derive_Subprogram.
596 -- Determine whether subprogram Subp is a procedure subject to pragma
597 -- Extensions_Visible with value False and has at least one controlling
598 -- parameter of mode OUT.
599
600 function Is_Valid_Constraint_Kind
601 (T_Kind : Type_Kind;
602 Constraint_Kind : Node_Kind) return Boolean;
603 -- Returns True if it is legal to apply the given kind of constraint to the
604 -- given kind of type (index constraint to an array type, for example).
605
606 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id);
607 -- Create new modular type. Verify that modulus is in bounds
608
609 procedure New_Concatenation_Op (Typ : Entity_Id);
610 -- Create an abbreviated declaration for an operator in order to
611 -- materialize concatenation on array types.
612
613 procedure Ordinary_Fixed_Point_Type_Declaration
614 (T : Entity_Id;
615 Def : Node_Id);
616 -- Create a new ordinary fixed point type, and apply the constraint to
617 -- obtain subtype of it.
618
619 procedure Prepare_Private_Subtype_Completion
620 (Id : Entity_Id;
621 Related_Nod : Node_Id);
622 -- Id is a subtype of some private type. Creates the full declaration
623 -- associated with Id whenever possible, i.e. when the full declaration
624 -- of the base type is already known. Records each subtype into
625 -- Private_Dependents of the base type.
626
627 procedure Process_Incomplete_Dependents
628 (N : Node_Id;
629 Full_T : Entity_Id;
630 Inc_T : Entity_Id);
631 -- Process all entities that depend on an incomplete type. There include
632 -- subtypes, subprogram types that mention the incomplete type in their
633 -- profiles, and subprogram with access parameters that designate the
634 -- incomplete type.
635
636 -- Inc_T is the defining identifier of an incomplete type declaration, its
637 -- Ekind is E_Incomplete_Type.
638 --
639 -- N is the corresponding N_Full_Type_Declaration for Inc_T.
640 --
641 -- Full_T is N's defining identifier.
642 --
643 -- Subtypes of incomplete types with discriminants are completed when the
644 -- parent type is. This is simpler than private subtypes, because they can
645 -- only appear in the same scope, and there is no need to exchange views.
646 -- Similarly, access_to_subprogram types may have a parameter or a return
647 -- type that is an incomplete type, and that must be replaced with the
648 -- full type.
649 --
650 -- If the full type is tagged, subprogram with access parameters that
651 -- designated the incomplete may be primitive operations of the full type,
652 -- and have to be processed accordingly.
653
654 procedure Process_Real_Range_Specification (Def : Node_Id);
655 -- Given the type definition for a real type, this procedure processes and
656 -- checks the real range specification of this type definition if one is
657 -- present. If errors are found, error messages are posted, and the
658 -- Real_Range_Specification of Def is reset to Empty.
659
660 procedure Propagate_Default_Init_Cond_Attributes
661 (From_Typ : Entity_Id;
662 To_Typ : Entity_Id;
663 Parent_To_Derivation : Boolean := False;
664 Private_To_Full_View : Boolean := False);
665 -- Subsidiary to routines Build_Derived_Type and Process_Full_View. Inherit
666 -- all attributes related to pragma Default_Initial_Condition from From_Typ
667 -- to To_Typ. Flag Parent_To_Derivation should be set when the context is
668 -- the creation of a derived type. Flag Private_To_Full_View should be set
669 -- when processing both views of a private type.
670
671 procedure Record_Type_Declaration
672 (T : Entity_Id;
673 N : Node_Id;
674 Prev : Entity_Id);
675 -- Process a record type declaration (for both untagged and tagged
676 -- records). Parameters T and N are exactly like in procedure
677 -- Derived_Type_Declaration, except that no flag Is_Completion is needed
678 -- for this routine. If this is the completion of an incomplete type
679 -- declaration, Prev is the entity of the incomplete declaration, used for
680 -- cross-referencing. Otherwise Prev = T.
681
682 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id);
683 -- This routine is used to process the actual record type definition (both
684 -- for untagged and tagged records). Def is a record type definition node.
685 -- This procedure analyzes the components in this record type definition.
686 -- Prev_T is the entity for the enclosing record type. It is provided so
687 -- that its Has_Task flag can be set if any of the component have Has_Task
688 -- set. If the declaration is the completion of an incomplete type
689 -- declaration, Prev_T is the original incomplete type, whose full view is
690 -- the record type.
691
692 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id);
693 -- Subsidiary to Build_Derived_Record_Type. For untagged records, we
694 -- build a copy of the declaration tree of the parent, and we create
695 -- independently the list of components for the derived type. Semantic
696 -- information uses the component entities, but record representation
697 -- clauses are validated on the declaration tree. This procedure replaces
698 -- discriminants and components in the declaration with those that have
699 -- been created by Inherit_Components.
700
701 procedure Set_Fixed_Range
702 (E : Entity_Id;
703 Loc : Source_Ptr;
704 Lo : Ureal;
705 Hi : Ureal);
706 -- Build a range node with the given bounds and set it as the Scalar_Range
707 -- of the given fixed-point type entity. Loc is the source location used
708 -- for the constructed range. See body for further details.
709
710 procedure Set_Scalar_Range_For_Subtype
711 (Def_Id : Entity_Id;
712 R : Node_Id;
713 Subt : Entity_Id);
714 -- This routine is used to set the scalar range field for a subtype given
715 -- Def_Id, the entity for the subtype, and R, the range expression for the
716 -- scalar range. Subt provides the parent subtype to be used to analyze,
717 -- resolve, and check the given range.
718
719 procedure Set_Default_SSO (T : Entity_Id);
720 -- T is the entity for an array or record being declared. This procedure
721 -- sets the flags SSO_Set_Low_By_Default/SSO_Set_High_By_Default according
722 -- to the setting of Opt.Default_SSO.
723
724 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id);
725 -- Create a new signed integer entity, and apply the constraint to obtain
726 -- the required first named subtype of this type.
727
728 procedure Set_Stored_Constraint_From_Discriminant_Constraint
729 (E : Entity_Id);
730 -- E is some record type. This routine computes E's Stored_Constraint
731 -- from its Discriminant_Constraint.
732
733 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id);
734 -- Check that an entity in a list of progenitors is an interface,
735 -- emit error otherwise.
736
737 -----------------------
738 -- Access_Definition --
739 -----------------------
740
741 function Access_Definition
742 (Related_Nod : Node_Id;
743 N : Node_Id) return Entity_Id
744 is
745 Anon_Type : Entity_Id;
746 Anon_Scope : Entity_Id;
747 Desig_Type : Entity_Id;
748 Enclosing_Prot_Type : Entity_Id := Empty;
749
750 begin
751 Check_SPARK_05_Restriction ("access type is not allowed", N);
752
753 if Is_Entry (Current_Scope)
754 and then Is_Task_Type (Etype (Scope (Current_Scope)))
755 then
756 Error_Msg_N ("task entries cannot have access parameters", N);
757 return Empty;
758 end if;
759
760 -- Ada 2005: For an object declaration the corresponding anonymous
761 -- type is declared in the current scope.
762
763 -- If the access definition is the return type of another access to
764 -- function, scope is the current one, because it is the one of the
765 -- current type declaration, except for the pathological case below.
766
767 if Nkind_In (Related_Nod, N_Object_Declaration,
768 N_Access_Function_Definition)
769 then
770 Anon_Scope := Current_Scope;
771
772 -- A pathological case: function returning access functions that
773 -- return access functions, etc. Each anonymous access type created
774 -- is in the enclosing scope of the outermost function.
775
776 declare
777 Par : Node_Id;
778
779 begin
780 Par := Related_Nod;
781 while Nkind_In (Par, N_Access_Function_Definition,
782 N_Access_Definition)
783 loop
784 Par := Parent (Par);
785 end loop;
786
787 if Nkind (Par) = N_Function_Specification then
788 Anon_Scope := Scope (Defining_Entity (Par));
789 end if;
790 end;
791
792 -- For the anonymous function result case, retrieve the scope of the
793 -- function specification's associated entity rather than using the
794 -- current scope. The current scope will be the function itself if the
795 -- formal part is currently being analyzed, but will be the parent scope
796 -- in the case of a parameterless function, and we always want to use
797 -- the function's parent scope. Finally, if the function is a child
798 -- unit, we must traverse the tree to retrieve the proper entity.
799
800 elsif Nkind (Related_Nod) = N_Function_Specification
801 and then Nkind (Parent (N)) /= N_Parameter_Specification
802 then
803 -- If the current scope is a protected type, the anonymous access
804 -- is associated with one of the protected operations, and must
805 -- be available in the scope that encloses the protected declaration.
806 -- Otherwise the type is in the scope enclosing the subprogram.
807
808 -- If the function has formals, The return type of a subprogram
809 -- declaration is analyzed in the scope of the subprogram (see
810 -- Process_Formals) and thus the protected type, if present, is
811 -- the scope of the current function scope.
812
813 if Ekind (Current_Scope) = E_Protected_Type then
814 Enclosing_Prot_Type := Current_Scope;
815
816 elsif Ekind (Current_Scope) = E_Function
817 and then Ekind (Scope (Current_Scope)) = E_Protected_Type
818 then
819 Enclosing_Prot_Type := Scope (Current_Scope);
820 end if;
821
822 if Present (Enclosing_Prot_Type) then
823 Anon_Scope := Scope (Enclosing_Prot_Type);
824
825 else
826 Anon_Scope := Scope (Defining_Entity (Related_Nod));
827 end if;
828
829 -- For an access type definition, if the current scope is a child
830 -- unit it is the scope of the type.
831
832 elsif Is_Compilation_Unit (Current_Scope) then
833 Anon_Scope := Current_Scope;
834
835 -- For access formals, access components, and access discriminants, the
836 -- scope is that of the enclosing declaration,
837
838 else
839 Anon_Scope := Scope (Current_Scope);
840 end if;
841
842 Anon_Type :=
843 Create_Itype
844 (E_Anonymous_Access_Type, Related_Nod, Scope_Id => Anon_Scope);
845
846 if All_Present (N)
847 and then Ada_Version >= Ada_2005
848 then
849 Error_Msg_N ("ALL is not permitted for anonymous access types", N);
850 end if;
851
852 -- Ada 2005 (AI-254): In case of anonymous access to subprograms call
853 -- the corresponding semantic routine
854
855 if Present (Access_To_Subprogram_Definition (N)) then
856
857 -- Compiler runtime units are compiled in Ada 2005 mode when building
858 -- the runtime library but must also be compilable in Ada 95 mode
859 -- (when bootstrapping the compiler).
860
861 Check_Compiler_Unit ("anonymous access to subprogram", N);
862
863 Access_Subprogram_Declaration
864 (T_Name => Anon_Type,
865 T_Def => Access_To_Subprogram_Definition (N));
866
867 if Ekind (Anon_Type) = E_Access_Protected_Subprogram_Type then
868 Set_Ekind
869 (Anon_Type, E_Anonymous_Access_Protected_Subprogram_Type);
870 else
871 Set_Ekind (Anon_Type, E_Anonymous_Access_Subprogram_Type);
872 end if;
873
874 Set_Can_Use_Internal_Rep
875 (Anon_Type, not Always_Compatible_Rep_On_Target);
876
877 -- If the anonymous access is associated with a protected operation,
878 -- create a reference to it after the enclosing protected definition
879 -- because the itype will be used in the subsequent bodies.
880
881 -- If the anonymous access itself is protected, a full type
882 -- declaratiton will be created for it, so that the equivalent
883 -- record type can be constructed. For further details, see
884 -- Replace_Anonymous_Access_To_Protected-Subprogram.
885
886 if Ekind (Current_Scope) = E_Protected_Type
887 and then not Protected_Present (Access_To_Subprogram_Definition (N))
888 then
889 Build_Itype_Reference (Anon_Type, Parent (Current_Scope));
890 end if;
891
892 return Anon_Type;
893 end if;
894
895 Find_Type (Subtype_Mark (N));
896 Desig_Type := Entity (Subtype_Mark (N));
897
898 Set_Directly_Designated_Type (Anon_Type, Desig_Type);
899 Set_Etype (Anon_Type, Anon_Type);
900
901 -- Make sure the anonymous access type has size and alignment fields
902 -- set, as required by gigi. This is necessary in the case of the
903 -- Task_Body_Procedure.
904
905 if not Has_Private_Component (Desig_Type) then
906 Layout_Type (Anon_Type);
907 end if;
908
909 -- Ada 2005 (AI-231): Ada 2005 semantics for anonymous access differs
910 -- from Ada 95 semantics. In Ada 2005, anonymous access must specify if
911 -- the null value is allowed. In Ada 95 the null value is never allowed.
912
913 if Ada_Version >= Ada_2005 then
914 Set_Can_Never_Be_Null (Anon_Type, Null_Exclusion_Present (N));
915 else
916 Set_Can_Never_Be_Null (Anon_Type, True);
917 end if;
918
919 -- The anonymous access type is as public as the discriminated type or
920 -- subprogram that defines it. It is imported (for back-end purposes)
921 -- if the designated type is.
922
923 Set_Is_Public (Anon_Type, Is_Public (Scope (Anon_Type)));
924
925 -- Ada 2005 (AI-231): Propagate the access-constant attribute
926
927 Set_Is_Access_Constant (Anon_Type, Constant_Present (N));
928
929 -- The context is either a subprogram declaration, object declaration,
930 -- or an access discriminant, in a private or a full type declaration.
931 -- In the case of a subprogram, if the designated type is incomplete,
932 -- the operation will be a primitive operation of the full type, to be
933 -- updated subsequently. If the type is imported through a limited_with
934 -- clause, the subprogram is not a primitive operation of the type
935 -- (which is declared elsewhere in some other scope).
936
937 if Ekind (Desig_Type) = E_Incomplete_Type
938 and then not From_Limited_With (Desig_Type)
939 and then Is_Overloadable (Current_Scope)
940 then
941 Append_Elmt (Current_Scope, Private_Dependents (Desig_Type));
942 Set_Has_Delayed_Freeze (Current_Scope);
943 end if;
944
945 -- Ada 2005: If the designated type is an interface that may contain
946 -- tasks, create a Master entity for the declaration. This must be done
947 -- before expansion of the full declaration, because the declaration may
948 -- include an expression that is an allocator, whose expansion needs the
949 -- proper Master for the created tasks.
950
951 if Nkind (Related_Nod) = N_Object_Declaration and then Expander_Active
952 then
953 if Is_Interface (Desig_Type) and then Is_Limited_Record (Desig_Type)
954 then
955 Build_Class_Wide_Master (Anon_Type);
956
957 -- Similarly, if the type is an anonymous access that designates
958 -- tasks, create a master entity for it in the current context.
959
960 elsif Has_Task (Desig_Type) and then Comes_From_Source (Related_Nod)
961 then
962 Build_Master_Entity (Defining_Identifier (Related_Nod));
963 Build_Master_Renaming (Anon_Type);
964 end if;
965 end if;
966
967 -- For a private component of a protected type, it is imperative that
968 -- the back-end elaborate the type immediately after the protected
969 -- declaration, because this type will be used in the declarations
970 -- created for the component within each protected body, so we must
971 -- create an itype reference for it now.
972
973 if Nkind (Parent (Related_Nod)) = N_Protected_Definition then
974 Build_Itype_Reference (Anon_Type, Parent (Parent (Related_Nod)));
975
976 -- Similarly, if the access definition is the return result of a
977 -- function, create an itype reference for it because it will be used
978 -- within the function body. For a regular function that is not a
979 -- compilation unit, insert reference after the declaration. For a
980 -- protected operation, insert it after the enclosing protected type
981 -- declaration. In either case, do not create a reference for a type
982 -- obtained through a limited_with clause, because this would introduce
983 -- semantic dependencies.
984
985 -- Similarly, do not create a reference if the designated type is a
986 -- generic formal, because no use of it will reach the backend.
987
988 elsif Nkind (Related_Nod) = N_Function_Specification
989 and then not From_Limited_With (Desig_Type)
990 and then not Is_Generic_Type (Desig_Type)
991 then
992 if Present (Enclosing_Prot_Type) then
993 Build_Itype_Reference (Anon_Type, Parent (Enclosing_Prot_Type));
994
995 elsif Is_List_Member (Parent (Related_Nod))
996 and then Nkind (Parent (N)) /= N_Parameter_Specification
997 then
998 Build_Itype_Reference (Anon_Type, Parent (Related_Nod));
999 end if;
1000
1001 -- Finally, create an itype reference for an object declaration of an
1002 -- anonymous access type. This is strictly necessary only for deferred
1003 -- constants, but in any case will avoid out-of-scope problems in the
1004 -- back-end.
1005
1006 elsif Nkind (Related_Nod) = N_Object_Declaration then
1007 Build_Itype_Reference (Anon_Type, Related_Nod);
1008 end if;
1009
1010 return Anon_Type;
1011 end Access_Definition;
1012
1013 -----------------------------------
1014 -- Access_Subprogram_Declaration --
1015 -----------------------------------
1016
1017 procedure Access_Subprogram_Declaration
1018 (T_Name : Entity_Id;
1019 T_Def : Node_Id)
1020 is
1021 procedure Check_For_Premature_Usage (Def : Node_Id);
1022 -- Check that type T_Name is not used, directly or recursively, as a
1023 -- parameter or a return type in Def. Def is either a subtype, an
1024 -- access_definition, or an access_to_subprogram_definition.
1025
1026 -------------------------------
1027 -- Check_For_Premature_Usage --
1028 -------------------------------
1029
1030 procedure Check_For_Premature_Usage (Def : Node_Id) is
1031 Param : Node_Id;
1032
1033 begin
1034 -- Check for a subtype mark
1035
1036 if Nkind (Def) in N_Has_Etype then
1037 if Etype (Def) = T_Name then
1038 Error_Msg_N
1039 ("type& cannot be used before end of its declaration", Def);
1040 end if;
1041
1042 -- If this is not a subtype, then this is an access_definition
1043
1044 elsif Nkind (Def) = N_Access_Definition then
1045 if Present (Access_To_Subprogram_Definition (Def)) then
1046 Check_For_Premature_Usage
1047 (Access_To_Subprogram_Definition (Def));
1048 else
1049 Check_For_Premature_Usage (Subtype_Mark (Def));
1050 end if;
1051
1052 -- The only cases left are N_Access_Function_Definition and
1053 -- N_Access_Procedure_Definition.
1054
1055 else
1056 if Present (Parameter_Specifications (Def)) then
1057 Param := First (Parameter_Specifications (Def));
1058 while Present (Param) loop
1059 Check_For_Premature_Usage (Parameter_Type (Param));
1060 Param := Next (Param);
1061 end loop;
1062 end if;
1063
1064 if Nkind (Def) = N_Access_Function_Definition then
1065 Check_For_Premature_Usage (Result_Definition (Def));
1066 end if;
1067 end if;
1068 end Check_For_Premature_Usage;
1069
1070 -- Local variables
1071
1072 Formals : constant List_Id := Parameter_Specifications (T_Def);
1073 Formal : Entity_Id;
1074 D_Ityp : Node_Id;
1075 Desig_Type : constant Entity_Id :=
1076 Create_Itype (E_Subprogram_Type, Parent (T_Def));
1077
1078 -- Start of processing for Access_Subprogram_Declaration
1079
1080 begin
1081 Check_SPARK_05_Restriction ("access type is not allowed", T_Def);
1082
1083 -- Associate the Itype node with the inner full-type declaration or
1084 -- subprogram spec or entry body. This is required to handle nested
1085 -- anonymous declarations. For example:
1086
1087 -- procedure P
1088 -- (X : access procedure
1089 -- (Y : access procedure
1090 -- (Z : access T)))
1091
1092 D_Ityp := Associated_Node_For_Itype (Desig_Type);
1093 while not (Nkind_In (D_Ityp, N_Full_Type_Declaration,
1094 N_Private_Type_Declaration,
1095 N_Private_Extension_Declaration,
1096 N_Procedure_Specification,
1097 N_Function_Specification,
1098 N_Entry_Body)
1099
1100 or else
1101 Nkind_In (D_Ityp, N_Object_Declaration,
1102 N_Object_Renaming_Declaration,
1103 N_Formal_Object_Declaration,
1104 N_Formal_Type_Declaration,
1105 N_Task_Type_Declaration,
1106 N_Protected_Type_Declaration))
1107 loop
1108 D_Ityp := Parent (D_Ityp);
1109 pragma Assert (D_Ityp /= Empty);
1110 end loop;
1111
1112 Set_Associated_Node_For_Itype (Desig_Type, D_Ityp);
1113
1114 if Nkind_In (D_Ityp, N_Procedure_Specification,
1115 N_Function_Specification)
1116 then
1117 Set_Scope (Desig_Type, Scope (Defining_Entity (D_Ityp)));
1118
1119 elsif Nkind_In (D_Ityp, N_Full_Type_Declaration,
1120 N_Object_Declaration,
1121 N_Object_Renaming_Declaration,
1122 N_Formal_Type_Declaration)
1123 then
1124 Set_Scope (Desig_Type, Scope (Defining_Identifier (D_Ityp)));
1125 end if;
1126
1127 if Nkind (T_Def) = N_Access_Function_Definition then
1128 if Nkind (Result_Definition (T_Def)) = N_Access_Definition then
1129 declare
1130 Acc : constant Node_Id := Result_Definition (T_Def);
1131
1132 begin
1133 if Present (Access_To_Subprogram_Definition (Acc))
1134 and then
1135 Protected_Present (Access_To_Subprogram_Definition (Acc))
1136 then
1137 Set_Etype
1138 (Desig_Type,
1139 Replace_Anonymous_Access_To_Protected_Subprogram
1140 (T_Def));
1141
1142 else
1143 Set_Etype
1144 (Desig_Type,
1145 Access_Definition (T_Def, Result_Definition (T_Def)));
1146 end if;
1147 end;
1148
1149 else
1150 Analyze (Result_Definition (T_Def));
1151
1152 declare
1153 Typ : constant Entity_Id := Entity (Result_Definition (T_Def));
1154
1155 begin
1156 -- If a null exclusion is imposed on the result type, then
1157 -- create a null-excluding itype (an access subtype) and use
1158 -- it as the function's Etype.
1159
1160 if Is_Access_Type (Typ)
1161 and then Null_Exclusion_In_Return_Present (T_Def)
1162 then
1163 Set_Etype (Desig_Type,
1164 Create_Null_Excluding_Itype
1165 (T => Typ,
1166 Related_Nod => T_Def,
1167 Scope_Id => Current_Scope));
1168
1169 else
1170 if From_Limited_With (Typ) then
1171
1172 -- AI05-151: Incomplete types are allowed in all basic
1173 -- declarations, including access to subprograms.
1174
1175 if Ada_Version >= Ada_2012 then
1176 null;
1177
1178 else
1179 Error_Msg_NE
1180 ("illegal use of incomplete type&",
1181 Result_Definition (T_Def), Typ);
1182 end if;
1183
1184 elsif Ekind (Current_Scope) = E_Package
1185 and then In_Private_Part (Current_Scope)
1186 then
1187 if Ekind (Typ) = E_Incomplete_Type then
1188 Append_Elmt (Desig_Type, Private_Dependents (Typ));
1189
1190 elsif Is_Class_Wide_Type (Typ)
1191 and then Ekind (Etype (Typ)) = E_Incomplete_Type
1192 then
1193 Append_Elmt
1194 (Desig_Type, Private_Dependents (Etype (Typ)));
1195 end if;
1196 end if;
1197
1198 Set_Etype (Desig_Type, Typ);
1199 end if;
1200 end;
1201 end if;
1202
1203 if not (Is_Type (Etype (Desig_Type))) then
1204 Error_Msg_N
1205 ("expect type in function specification",
1206 Result_Definition (T_Def));
1207 end if;
1208
1209 else
1210 Set_Etype (Desig_Type, Standard_Void_Type);
1211 end if;
1212
1213 if Present (Formals) then
1214 Push_Scope (Desig_Type);
1215
1216 -- Some special tests here. These special tests can be removed
1217 -- if and when Itypes always have proper parent pointers to their
1218 -- declarations???
1219
1220 -- Special test 1) Link defining_identifier of formals. Required by
1221 -- First_Formal to provide its functionality.
1222
1223 declare
1224 F : Node_Id;
1225
1226 begin
1227 F := First (Formals);
1228
1229 -- In ASIS mode, the access_to_subprogram may be analyzed twice,
1230 -- when it is part of an unconstrained type and subtype expansion
1231 -- is disabled. To avoid back-end problems with shared profiles,
1232 -- use previous subprogram type as the designated type, and then
1233 -- remove scope added above.
1234
1235 if ASIS_Mode and then Present (Scope (Defining_Identifier (F)))
1236 then
1237 Set_Etype (T_Name, T_Name);
1238 Init_Size_Align (T_Name);
1239 Set_Directly_Designated_Type (T_Name,
1240 Scope (Defining_Identifier (F)));
1241 End_Scope;
1242 return;
1243 end if;
1244
1245 while Present (F) loop
1246 if No (Parent (Defining_Identifier (F))) then
1247 Set_Parent (Defining_Identifier (F), F);
1248 end if;
1249
1250 Next (F);
1251 end loop;
1252 end;
1253
1254 Process_Formals (Formals, Parent (T_Def));
1255
1256 -- Special test 2) End_Scope requires that the parent pointer be set
1257 -- to something reasonable, but Itypes don't have parent pointers. So
1258 -- we set it and then unset it ???
1259
1260 Set_Parent (Desig_Type, T_Name);
1261 End_Scope;
1262 Set_Parent (Desig_Type, Empty);
1263 end if;
1264
1265 -- Check for premature usage of the type being defined
1266
1267 Check_For_Premature_Usage (T_Def);
1268
1269 -- The return type and/or any parameter type may be incomplete. Mark the
1270 -- subprogram_type as depending on the incomplete type, so that it can
1271 -- be updated when the full type declaration is seen. This only applies
1272 -- to incomplete types declared in some enclosing scope, not to limited
1273 -- views from other packages.
1274
1275 -- Prior to Ada 2012, access to functions can only have in_parameters.
1276
1277 if Present (Formals) then
1278 Formal := First_Formal (Desig_Type);
1279 while Present (Formal) loop
1280 if Ekind (Formal) /= E_In_Parameter
1281 and then Nkind (T_Def) = N_Access_Function_Definition
1282 and then Ada_Version < Ada_2012
1283 then
1284 Error_Msg_N ("functions can only have IN parameters", Formal);
1285 end if;
1286
1287 if Ekind (Etype (Formal)) = E_Incomplete_Type
1288 and then In_Open_Scopes (Scope (Etype (Formal)))
1289 then
1290 Append_Elmt (Desig_Type, Private_Dependents (Etype (Formal)));
1291 Set_Has_Delayed_Freeze (Desig_Type);
1292 end if;
1293
1294 Next_Formal (Formal);
1295 end loop;
1296 end if;
1297
1298 -- Check whether an indirect call without actuals may be possible. This
1299 -- is used when resolving calls whose result is then indexed.
1300
1301 May_Need_Actuals (Desig_Type);
1302
1303 -- If the return type is incomplete, this is legal as long as the type
1304 -- is declared in the current scope and will be completed in it (rather
1305 -- than being part of limited view).
1306
1307 if Ekind (Etype (Desig_Type)) = E_Incomplete_Type
1308 and then not Has_Delayed_Freeze (Desig_Type)
1309 and then In_Open_Scopes (Scope (Etype (Desig_Type)))
1310 then
1311 Append_Elmt (Desig_Type, Private_Dependents (Etype (Desig_Type)));
1312 Set_Has_Delayed_Freeze (Desig_Type);
1313 end if;
1314
1315 Check_Delayed_Subprogram (Desig_Type);
1316
1317 if Protected_Present (T_Def) then
1318 Set_Ekind (T_Name, E_Access_Protected_Subprogram_Type);
1319 Set_Convention (Desig_Type, Convention_Protected);
1320 else
1321 Set_Ekind (T_Name, E_Access_Subprogram_Type);
1322 end if;
1323
1324 Set_Can_Use_Internal_Rep (T_Name, not Always_Compatible_Rep_On_Target);
1325
1326 Set_Etype (T_Name, T_Name);
1327 Init_Size_Align (T_Name);
1328 Set_Directly_Designated_Type (T_Name, Desig_Type);
1329
1330 Generate_Reference_To_Formals (T_Name);
1331
1332 -- Ada 2005 (AI-231): Propagate the null-excluding attribute
1333
1334 Set_Can_Never_Be_Null (T_Name, Null_Exclusion_Present (T_Def));
1335
1336 Check_Restriction (No_Access_Subprograms, T_Def);
1337 end Access_Subprogram_Declaration;
1338
1339 ----------------------------
1340 -- Access_Type_Declaration --
1341 ----------------------------
1342
1343 procedure Access_Type_Declaration (T : Entity_Id; Def : Node_Id) is
1344 P : constant Node_Id := Parent (Def);
1345 S : constant Node_Id := Subtype_Indication (Def);
1346
1347 Full_Desig : Entity_Id;
1348
1349 begin
1350 Check_SPARK_05_Restriction ("access type is not allowed", Def);
1351
1352 -- Check for permissible use of incomplete type
1353
1354 if Nkind (S) /= N_Subtype_Indication then
1355 Analyze (S);
1356
1357 if Ekind (Root_Type (Entity (S))) = E_Incomplete_Type then
1358 Set_Directly_Designated_Type (T, Entity (S));
1359
1360 -- If the designated type is a limited view, we cannot tell if
1361 -- the full view contains tasks, and there is no way to handle
1362 -- that full view in a client. We create a master entity for the
1363 -- scope, which will be used when a client determines that one
1364 -- is needed.
1365
1366 if From_Limited_With (Entity (S))
1367 and then not Is_Class_Wide_Type (Entity (S))
1368 then
1369 Set_Ekind (T, E_Access_Type);
1370 Build_Master_Entity (T);
1371 Build_Master_Renaming (T);
1372 end if;
1373
1374 else
1375 Set_Directly_Designated_Type (T, Process_Subtype (S, P, T, 'P'));
1376 end if;
1377
1378 -- If the access definition is of the form: ACCESS NOT NULL ..
1379 -- the subtype indication must be of an access type. Create
1380 -- a null-excluding subtype of it.
1381
1382 if Null_Excluding_Subtype (Def) then
1383 if not Is_Access_Type (Entity (S)) then
1384 Error_Msg_N ("null exclusion must apply to access type", Def);
1385
1386 else
1387 declare
1388 Loc : constant Source_Ptr := Sloc (S);
1389 Decl : Node_Id;
1390 Nam : constant Entity_Id := Make_Temporary (Loc, 'S');
1391
1392 begin
1393 Decl :=
1394 Make_Subtype_Declaration (Loc,
1395 Defining_Identifier => Nam,
1396 Subtype_Indication =>
1397 New_Occurrence_Of (Entity (S), Loc));
1398 Set_Null_Exclusion_Present (Decl);
1399 Insert_Before (Parent (Def), Decl);
1400 Analyze (Decl);
1401 Set_Entity (S, Nam);
1402 end;
1403 end if;
1404 end if;
1405
1406 else
1407 Set_Directly_Designated_Type (T,
1408 Process_Subtype (S, P, T, 'P'));
1409 end if;
1410
1411 if All_Present (Def) or Constant_Present (Def) then
1412 Set_Ekind (T, E_General_Access_Type);
1413 else
1414 Set_Ekind (T, E_Access_Type);
1415 end if;
1416
1417 Full_Desig := Designated_Type (T);
1418
1419 if Base_Type (Full_Desig) = T then
1420 Error_Msg_N ("access type cannot designate itself", S);
1421
1422 -- In Ada 2005, the type may have a limited view through some unit in
1423 -- its own context, allowing the following circularity that cannot be
1424 -- detected earlier.
1425
1426 elsif Is_Class_Wide_Type (Full_Desig) and then Etype (Full_Desig) = T
1427 then
1428 Error_Msg_N
1429 ("access type cannot designate its own classwide type", S);
1430
1431 -- Clean up indication of tagged status to prevent cascaded errors
1432
1433 Set_Is_Tagged_Type (T, False);
1434 end if;
1435
1436 Set_Etype (T, T);
1437
1438 -- If the type has appeared already in a with_type clause, it is frozen
1439 -- and the pointer size is already set. Else, initialize.
1440
1441 if not From_Limited_With (T) then
1442 Init_Size_Align (T);
1443 end if;
1444
1445 -- Note that Has_Task is always false, since the access type itself
1446 -- is not a task type. See Einfo for more description on this point.
1447 -- Exactly the same consideration applies to Has_Controlled_Component
1448 -- and to Has_Protected.
1449
1450 Set_Has_Task (T, False);
1451 Set_Has_Controlled_Component (T, False);
1452 Set_Has_Protected (T, False);
1453
1454 -- Initialize field Finalization_Master explicitly to Empty, to avoid
1455 -- problems where an incomplete view of this entity has been previously
1456 -- established by a limited with and an overlaid version of this field
1457 -- (Stored_Constraint) was initialized for the incomplete view.
1458
1459 -- This reset is performed in most cases except where the access type
1460 -- has been created for the purposes of allocating or deallocating a
1461 -- build-in-place object. Such access types have explicitly set pools
1462 -- and finalization masters.
1463
1464 if No (Associated_Storage_Pool (T)) then
1465 Set_Finalization_Master (T, Empty);
1466 end if;
1467
1468 -- Ada 2005 (AI-231): Propagate the null-excluding and access-constant
1469 -- attributes
1470
1471 Set_Can_Never_Be_Null (T, Null_Exclusion_Present (Def));
1472 Set_Is_Access_Constant (T, Constant_Present (Def));
1473 end Access_Type_Declaration;
1474
1475 ----------------------------------
1476 -- Add_Interface_Tag_Components --
1477 ----------------------------------
1478
1479 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id) is
1480 Loc : constant Source_Ptr := Sloc (N);
1481 L : List_Id;
1482 Last_Tag : Node_Id;
1483
1484 procedure Add_Tag (Iface : Entity_Id);
1485 -- Add tag for one of the progenitor interfaces
1486
1487 -------------
1488 -- Add_Tag --
1489 -------------
1490
1491 procedure Add_Tag (Iface : Entity_Id) is
1492 Decl : Node_Id;
1493 Def : Node_Id;
1494 Tag : Entity_Id;
1495 Offset : Entity_Id;
1496
1497 begin
1498 pragma Assert (Is_Tagged_Type (Iface) and then Is_Interface (Iface));
1499
1500 -- This is a reasonable place to propagate predicates
1501
1502 if Has_Predicates (Iface) then
1503 Set_Has_Predicates (Typ);
1504 end if;
1505
1506 Def :=
1507 Make_Component_Definition (Loc,
1508 Aliased_Present => True,
1509 Subtype_Indication =>
1510 New_Occurrence_Of (RTE (RE_Interface_Tag), Loc));
1511
1512 Tag := Make_Temporary (Loc, 'V');
1513
1514 Decl :=
1515 Make_Component_Declaration (Loc,
1516 Defining_Identifier => Tag,
1517 Component_Definition => Def);
1518
1519 Analyze_Component_Declaration (Decl);
1520
1521 Set_Analyzed (Decl);
1522 Set_Ekind (Tag, E_Component);
1523 Set_Is_Tag (Tag);
1524 Set_Is_Aliased (Tag);
1525 Set_Related_Type (Tag, Iface);
1526 Init_Component_Location (Tag);
1527
1528 pragma Assert (Is_Frozen (Iface));
1529
1530 Set_DT_Entry_Count (Tag,
1531 DT_Entry_Count (First_Entity (Iface)));
1532
1533 if No (Last_Tag) then
1534 Prepend (Decl, L);
1535 else
1536 Insert_After (Last_Tag, Decl);
1537 end if;
1538
1539 Last_Tag := Decl;
1540
1541 -- If the ancestor has discriminants we need to give special support
1542 -- to store the offset_to_top value of the secondary dispatch tables.
1543 -- For this purpose we add a supplementary component just after the
1544 -- field that contains the tag associated with each secondary DT.
1545
1546 if Typ /= Etype (Typ) and then Has_Discriminants (Etype (Typ)) then
1547 Def :=
1548 Make_Component_Definition (Loc,
1549 Subtype_Indication =>
1550 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
1551
1552 Offset := Make_Temporary (Loc, 'V');
1553
1554 Decl :=
1555 Make_Component_Declaration (Loc,
1556 Defining_Identifier => Offset,
1557 Component_Definition => Def);
1558
1559 Analyze_Component_Declaration (Decl);
1560
1561 Set_Analyzed (Decl);
1562 Set_Ekind (Offset, E_Component);
1563 Set_Is_Aliased (Offset);
1564 Set_Related_Type (Offset, Iface);
1565 Init_Component_Location (Offset);
1566 Insert_After (Last_Tag, Decl);
1567 Last_Tag := Decl;
1568 end if;
1569 end Add_Tag;
1570
1571 -- Local variables
1572
1573 Elmt : Elmt_Id;
1574 Ext : Node_Id;
1575 Comp : Node_Id;
1576
1577 -- Start of processing for Add_Interface_Tag_Components
1578
1579 begin
1580 if not RTE_Available (RE_Interface_Tag) then
1581 Error_Msg
1582 ("(Ada 2005) interface types not supported by this run-time!",
1583 Sloc (N));
1584 return;
1585 end if;
1586
1587 if Ekind (Typ) /= E_Record_Type
1588 or else (Is_Concurrent_Record_Type (Typ)
1589 and then Is_Empty_List (Abstract_Interface_List (Typ)))
1590 or else (not Is_Concurrent_Record_Type (Typ)
1591 and then No (Interfaces (Typ))
1592 and then Is_Empty_Elmt_List (Interfaces (Typ)))
1593 then
1594 return;
1595 end if;
1596
1597 -- Find the current last tag
1598
1599 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1600 Ext := Record_Extension_Part (Type_Definition (N));
1601 else
1602 pragma Assert (Nkind (Type_Definition (N)) = N_Record_Definition);
1603 Ext := Type_Definition (N);
1604 end if;
1605
1606 Last_Tag := Empty;
1607
1608 if not (Present (Component_List (Ext))) then
1609 Set_Null_Present (Ext, False);
1610 L := New_List;
1611 Set_Component_List (Ext,
1612 Make_Component_List (Loc,
1613 Component_Items => L,
1614 Null_Present => False));
1615 else
1616 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1617 L := Component_Items
1618 (Component_List
1619 (Record_Extension_Part
1620 (Type_Definition (N))));
1621 else
1622 L := Component_Items
1623 (Component_List
1624 (Type_Definition (N)));
1625 end if;
1626
1627 -- Find the last tag component
1628
1629 Comp := First (L);
1630 while Present (Comp) loop
1631 if Nkind (Comp) = N_Component_Declaration
1632 and then Is_Tag (Defining_Identifier (Comp))
1633 then
1634 Last_Tag := Comp;
1635 end if;
1636
1637 Next (Comp);
1638 end loop;
1639 end if;
1640
1641 -- At this point L references the list of components and Last_Tag
1642 -- references the current last tag (if any). Now we add the tag
1643 -- corresponding with all the interfaces that are not implemented
1644 -- by the parent.
1645
1646 if Present (Interfaces (Typ)) then
1647 Elmt := First_Elmt (Interfaces (Typ));
1648 while Present (Elmt) loop
1649 Add_Tag (Node (Elmt));
1650 Next_Elmt (Elmt);
1651 end loop;
1652 end if;
1653 end Add_Interface_Tag_Components;
1654
1655 -------------------------------------
1656 -- Add_Internal_Interface_Entities --
1657 -------------------------------------
1658
1659 procedure Add_Internal_Interface_Entities (Tagged_Type : Entity_Id) is
1660 Elmt : Elmt_Id;
1661 Iface : Entity_Id;
1662 Iface_Elmt : Elmt_Id;
1663 Iface_Prim : Entity_Id;
1664 Ifaces_List : Elist_Id;
1665 New_Subp : Entity_Id := Empty;
1666 Prim : Entity_Id;
1667 Restore_Scope : Boolean := False;
1668
1669 begin
1670 pragma Assert (Ada_Version >= Ada_2005
1671 and then Is_Record_Type (Tagged_Type)
1672 and then Is_Tagged_Type (Tagged_Type)
1673 and then Has_Interfaces (Tagged_Type)
1674 and then not Is_Interface (Tagged_Type));
1675
1676 -- Ensure that the internal entities are added to the scope of the type
1677
1678 if Scope (Tagged_Type) /= Current_Scope then
1679 Push_Scope (Scope (Tagged_Type));
1680 Restore_Scope := True;
1681 end if;
1682
1683 Collect_Interfaces (Tagged_Type, Ifaces_List);
1684
1685 Iface_Elmt := First_Elmt (Ifaces_List);
1686 while Present (Iface_Elmt) loop
1687 Iface := Node (Iface_Elmt);
1688
1689 -- Originally we excluded here from this processing interfaces that
1690 -- are parents of Tagged_Type because their primitives are located
1691 -- in the primary dispatch table (and hence no auxiliary internal
1692 -- entities are required to handle secondary dispatch tables in such
1693 -- case). However, these auxiliary entities are also required to
1694 -- handle derivations of interfaces in formals of generics (see
1695 -- Derive_Subprograms).
1696
1697 Elmt := First_Elmt (Primitive_Operations (Iface));
1698 while Present (Elmt) loop
1699 Iface_Prim := Node (Elmt);
1700
1701 if not Is_Predefined_Dispatching_Operation (Iface_Prim) then
1702 Prim :=
1703 Find_Primitive_Covering_Interface
1704 (Tagged_Type => Tagged_Type,
1705 Iface_Prim => Iface_Prim);
1706
1707 if No (Prim) and then Serious_Errors_Detected > 0 then
1708 goto Continue;
1709 end if;
1710
1711 pragma Assert (Present (Prim));
1712
1713 -- Ada 2012 (AI05-0197): If the name of the covering primitive
1714 -- differs from the name of the interface primitive then it is
1715 -- a private primitive inherited from a parent type. In such
1716 -- case, given that Tagged_Type covers the interface, the
1717 -- inherited private primitive becomes visible. For such
1718 -- purpose we add a new entity that renames the inherited
1719 -- private primitive.
1720
1721 if Chars (Prim) /= Chars (Iface_Prim) then
1722 pragma Assert (Has_Suffix (Prim, 'P'));
1723 Derive_Subprogram
1724 (New_Subp => New_Subp,
1725 Parent_Subp => Iface_Prim,
1726 Derived_Type => Tagged_Type,
1727 Parent_Type => Iface);
1728 Set_Alias (New_Subp, Prim);
1729 Set_Is_Abstract_Subprogram
1730 (New_Subp, Is_Abstract_Subprogram (Prim));
1731 end if;
1732
1733 Derive_Subprogram
1734 (New_Subp => New_Subp,
1735 Parent_Subp => Iface_Prim,
1736 Derived_Type => Tagged_Type,
1737 Parent_Type => Iface);
1738
1739 -- Ada 2005 (AI-251): Decorate internal entity Iface_Subp
1740 -- associated with interface types. These entities are
1741 -- only registered in the list of primitives of its
1742 -- corresponding tagged type because they are only used
1743 -- to fill the contents of the secondary dispatch tables.
1744 -- Therefore they are removed from the homonym chains.
1745
1746 Set_Is_Hidden (New_Subp);
1747 Set_Is_Internal (New_Subp);
1748 Set_Alias (New_Subp, Prim);
1749 Set_Is_Abstract_Subprogram
1750 (New_Subp, Is_Abstract_Subprogram (Prim));
1751 Set_Interface_Alias (New_Subp, Iface_Prim);
1752
1753 -- If the returned type is an interface then propagate it to
1754 -- the returned type. Needed by the thunk to generate the code
1755 -- which displaces "this" to reference the corresponding
1756 -- secondary dispatch table in the returned object.
1757
1758 if Is_Interface (Etype (Iface_Prim)) then
1759 Set_Etype (New_Subp, Etype (Iface_Prim));
1760 end if;
1761
1762 -- Internal entities associated with interface types are
1763 -- only registered in the list of primitives of the tagged
1764 -- type. They are only used to fill the contents of the
1765 -- secondary dispatch tables. Therefore they are not needed
1766 -- in the homonym chains.
1767
1768 Remove_Homonym (New_Subp);
1769
1770 -- Hidden entities associated with interfaces must have set
1771 -- the Has_Delay_Freeze attribute to ensure that, in case of
1772 -- locally defined tagged types (or compiling with static
1773 -- dispatch tables generation disabled) the corresponding
1774 -- entry of the secondary dispatch table is filled when
1775 -- such an entity is frozen.
1776
1777 Set_Has_Delayed_Freeze (New_Subp);
1778 end if;
1779
1780 <<Continue>>
1781 Next_Elmt (Elmt);
1782 end loop;
1783
1784 Next_Elmt (Iface_Elmt);
1785 end loop;
1786
1787 if Restore_Scope then
1788 Pop_Scope;
1789 end if;
1790 end Add_Internal_Interface_Entities;
1791
1792 -----------------------------------
1793 -- Analyze_Component_Declaration --
1794 -----------------------------------
1795
1796 procedure Analyze_Component_Declaration (N : Node_Id) is
1797 Loc : constant Source_Ptr := Sloc (N);
1798 Id : constant Entity_Id := Defining_Identifier (N);
1799 E : constant Node_Id := Expression (N);
1800 Typ : constant Node_Id :=
1801 Subtype_Indication (Component_Definition (N));
1802 T : Entity_Id;
1803 P : Entity_Id;
1804
1805 function Contains_POC (Constr : Node_Id) return Boolean;
1806 -- Determines whether a constraint uses the discriminant of a record
1807 -- type thus becoming a per-object constraint (POC).
1808
1809 function Is_Known_Limited (Typ : Entity_Id) return Boolean;
1810 -- Typ is the type of the current component, check whether this type is
1811 -- a limited type. Used to validate declaration against that of
1812 -- enclosing record.
1813
1814 ------------------
1815 -- Contains_POC --
1816 ------------------
1817
1818 function Contains_POC (Constr : Node_Id) return Boolean is
1819 begin
1820 -- Prevent cascaded errors
1821
1822 if Error_Posted (Constr) then
1823 return False;
1824 end if;
1825
1826 case Nkind (Constr) is
1827 when N_Attribute_Reference =>
1828 return Attribute_Name (Constr) = Name_Access
1829 and then Prefix (Constr) = Scope (Entity (Prefix (Constr)));
1830
1831 when N_Discriminant_Association =>
1832 return Denotes_Discriminant (Expression (Constr));
1833
1834 when N_Identifier =>
1835 return Denotes_Discriminant (Constr);
1836
1837 when N_Index_Or_Discriminant_Constraint =>
1838 declare
1839 IDC : Node_Id;
1840
1841 begin
1842 IDC := First (Constraints (Constr));
1843 while Present (IDC) loop
1844
1845 -- One per-object constraint is sufficient
1846
1847 if Contains_POC (IDC) then
1848 return True;
1849 end if;
1850
1851 Next (IDC);
1852 end loop;
1853
1854 return False;
1855 end;
1856
1857 when N_Range =>
1858 return Denotes_Discriminant (Low_Bound (Constr))
1859 or else
1860 Denotes_Discriminant (High_Bound (Constr));
1861
1862 when N_Range_Constraint =>
1863 return Denotes_Discriminant (Range_Expression (Constr));
1864
1865 when others =>
1866 return False;
1867
1868 end case;
1869 end Contains_POC;
1870
1871 ----------------------
1872 -- Is_Known_Limited --
1873 ----------------------
1874
1875 function Is_Known_Limited (Typ : Entity_Id) return Boolean is
1876 P : constant Entity_Id := Etype (Typ);
1877 R : constant Entity_Id := Root_Type (Typ);
1878
1879 begin
1880 if Is_Limited_Record (Typ) then
1881 return True;
1882
1883 -- If the root type is limited (and not a limited interface)
1884 -- so is the current type
1885
1886 elsif Is_Limited_Record (R)
1887 and then (not Is_Interface (R) or else not Is_Limited_Interface (R))
1888 then
1889 return True;
1890
1891 -- Else the type may have a limited interface progenitor, but a
1892 -- limited record parent.
1893
1894 elsif R /= P and then Is_Limited_Record (P) then
1895 return True;
1896
1897 else
1898 return False;
1899 end if;
1900 end Is_Known_Limited;
1901
1902 -- Start of processing for Analyze_Component_Declaration
1903
1904 begin
1905 Generate_Definition (Id);
1906 Enter_Name (Id);
1907
1908 if Present (Typ) then
1909 T := Find_Type_Of_Object
1910 (Subtype_Indication (Component_Definition (N)), N);
1911
1912 if not Nkind_In (Typ, N_Identifier, N_Expanded_Name) then
1913 Check_SPARK_05_Restriction ("subtype mark required", Typ);
1914 end if;
1915
1916 -- Ada 2005 (AI-230): Access Definition case
1917
1918 else
1919 pragma Assert (Present
1920 (Access_Definition (Component_Definition (N))));
1921
1922 T := Access_Definition
1923 (Related_Nod => N,
1924 N => Access_Definition (Component_Definition (N)));
1925 Set_Is_Local_Anonymous_Access (T);
1926
1927 -- Ada 2005 (AI-254)
1928
1929 if Present (Access_To_Subprogram_Definition
1930 (Access_Definition (Component_Definition (N))))
1931 and then Protected_Present (Access_To_Subprogram_Definition
1932 (Access_Definition
1933 (Component_Definition (N))))
1934 then
1935 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1936 end if;
1937 end if;
1938
1939 -- If the subtype is a constrained subtype of the enclosing record,
1940 -- (which must have a partial view) the back-end does not properly
1941 -- handle the recursion. Rewrite the component declaration with an
1942 -- explicit subtype indication, which is acceptable to Gigi. We can copy
1943 -- the tree directly because side effects have already been removed from
1944 -- discriminant constraints.
1945
1946 if Ekind (T) = E_Access_Subtype
1947 and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
1948 and then Comes_From_Source (T)
1949 and then Nkind (Parent (T)) = N_Subtype_Declaration
1950 and then Etype (Directly_Designated_Type (T)) = Current_Scope
1951 then
1952 Rewrite
1953 (Subtype_Indication (Component_Definition (N)),
1954 New_Copy_Tree (Subtype_Indication (Parent (T))));
1955 T := Find_Type_Of_Object
1956 (Subtype_Indication (Component_Definition (N)), N);
1957 end if;
1958
1959 -- If the component declaration includes a default expression, then we
1960 -- check that the component is not of a limited type (RM 3.7(5)),
1961 -- and do the special preanalysis of the expression (see section on
1962 -- "Handling of Default and Per-Object Expressions" in the spec of
1963 -- package Sem).
1964
1965 if Present (E) then
1966 Check_SPARK_05_Restriction ("default expression is not allowed", E);
1967 Preanalyze_Default_Expression (E, T);
1968 Check_Initialization (T, E);
1969
1970 if Ada_Version >= Ada_2005
1971 and then Ekind (T) = E_Anonymous_Access_Type
1972 and then Etype (E) /= Any_Type
1973 then
1974 -- Check RM 3.9.2(9): "if the expected type for an expression is
1975 -- an anonymous access-to-specific tagged type, then the object
1976 -- designated by the expression shall not be dynamically tagged
1977 -- unless it is a controlling operand in a call on a dispatching
1978 -- operation"
1979
1980 if Is_Tagged_Type (Directly_Designated_Type (T))
1981 and then
1982 Ekind (Directly_Designated_Type (T)) /= E_Class_Wide_Type
1983 and then
1984 Ekind (Directly_Designated_Type (Etype (E))) =
1985 E_Class_Wide_Type
1986 then
1987 Error_Msg_N
1988 ("access to specific tagged type required (RM 3.9.2(9))", E);
1989 end if;
1990
1991 -- (Ada 2005: AI-230): Accessibility check for anonymous
1992 -- components
1993
1994 if Type_Access_Level (Etype (E)) >
1995 Deepest_Type_Access_Level (T)
1996 then
1997 Error_Msg_N
1998 ("expression has deeper access level than component " &
1999 "(RM 3.10.2 (12.2))", E);
2000 end if;
2001
2002 -- The initialization expression is a reference to an access
2003 -- discriminant. The type of the discriminant is always deeper
2004 -- than any access type.
2005
2006 if Ekind (Etype (E)) = E_Anonymous_Access_Type
2007 and then Is_Entity_Name (E)
2008 and then Ekind (Entity (E)) = E_In_Parameter
2009 and then Present (Discriminal_Link (Entity (E)))
2010 then
2011 Error_Msg_N
2012 ("discriminant has deeper accessibility level than target",
2013 E);
2014 end if;
2015 end if;
2016 end if;
2017
2018 -- The parent type may be a private view with unknown discriminants,
2019 -- and thus unconstrained. Regular components must be constrained.
2020
2021 if Is_Indefinite_Subtype (T) and then Chars (Id) /= Name_uParent then
2022 if Is_Class_Wide_Type (T) then
2023 Error_Msg_N
2024 ("class-wide subtype with unknown discriminants" &
2025 " in component declaration",
2026 Subtype_Indication (Component_Definition (N)));
2027 else
2028 Error_Msg_N
2029 ("unconstrained subtype in component declaration",
2030 Subtype_Indication (Component_Definition (N)));
2031 end if;
2032
2033 -- Components cannot be abstract, except for the special case of
2034 -- the _Parent field (case of extending an abstract tagged type)
2035
2036 elsif Is_Abstract_Type (T) and then Chars (Id) /= Name_uParent then
2037 Error_Msg_N ("type of a component cannot be abstract", N);
2038 end if;
2039
2040 Set_Etype (Id, T);
2041 Set_Is_Aliased (Id, Aliased_Present (Component_Definition (N)));
2042
2043 -- The component declaration may have a per-object constraint, set
2044 -- the appropriate flag in the defining identifier of the subtype.
2045
2046 if Present (Subtype_Indication (Component_Definition (N))) then
2047 declare
2048 Sindic : constant Node_Id :=
2049 Subtype_Indication (Component_Definition (N));
2050 begin
2051 if Nkind (Sindic) = N_Subtype_Indication
2052 and then Present (Constraint (Sindic))
2053 and then Contains_POC (Constraint (Sindic))
2054 then
2055 Set_Has_Per_Object_Constraint (Id);
2056 end if;
2057 end;
2058 end if;
2059
2060 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
2061 -- out some static checks.
2062
2063 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (T) then
2064 Null_Exclusion_Static_Checks (N);
2065 end if;
2066
2067 -- If this component is private (or depends on a private type), flag the
2068 -- record type to indicate that some operations are not available.
2069
2070 P := Private_Component (T);
2071
2072 if Present (P) then
2073
2074 -- Check for circular definitions
2075
2076 if P = Any_Type then
2077 Set_Etype (Id, Any_Type);
2078
2079 -- There is a gap in the visibility of operations only if the
2080 -- component type is not defined in the scope of the record type.
2081
2082 elsif Scope (P) = Scope (Current_Scope) then
2083 null;
2084
2085 elsif Is_Limited_Type (P) then
2086 Set_Is_Limited_Composite (Current_Scope);
2087
2088 else
2089 Set_Is_Private_Composite (Current_Scope);
2090 end if;
2091 end if;
2092
2093 if P /= Any_Type
2094 and then Is_Limited_Type (T)
2095 and then Chars (Id) /= Name_uParent
2096 and then Is_Tagged_Type (Current_Scope)
2097 then
2098 if Is_Derived_Type (Current_Scope)
2099 and then not Is_Known_Limited (Current_Scope)
2100 then
2101 Error_Msg_N
2102 ("extension of nonlimited type cannot have limited components",
2103 N);
2104
2105 if Is_Interface (Root_Type (Current_Scope)) then
2106 Error_Msg_N
2107 ("\limitedness is not inherited from limited interface", N);
2108 Error_Msg_N ("\add LIMITED to type indication", N);
2109 end if;
2110
2111 Explain_Limited_Type (T, N);
2112 Set_Etype (Id, Any_Type);
2113 Set_Is_Limited_Composite (Current_Scope, False);
2114
2115 elsif not Is_Derived_Type (Current_Scope)
2116 and then not Is_Limited_Record (Current_Scope)
2117 and then not Is_Concurrent_Type (Current_Scope)
2118 then
2119 Error_Msg_N
2120 ("nonlimited tagged type cannot have limited components", N);
2121 Explain_Limited_Type (T, N);
2122 Set_Etype (Id, Any_Type);
2123 Set_Is_Limited_Composite (Current_Scope, False);
2124 end if;
2125 end if;
2126
2127 -- If the component is an unconstrained task or protected type with
2128 -- discriminants, the component and the enclosing record are limited
2129 -- and the component is constrained by its default values. Compute
2130 -- its actual subtype, else it may be allocated the maximum size by
2131 -- the backend, and possibly overflow.
2132
2133 if Is_Concurrent_Type (T)
2134 and then not Is_Constrained (T)
2135 and then Has_Discriminants (T)
2136 and then not Has_Discriminants (Current_Scope)
2137 then
2138 declare
2139 Act_T : constant Entity_Id := Build_Default_Subtype (T, N);
2140 begin
2141 Set_Etype (Id, Act_T);
2142 Set_Component_Definition (N,
2143 Make_Component_Definition (Loc,
2144 Subtype_Indication => New_Occurrence_Of (Act_T, Loc)));
2145 end;
2146 end if;
2147
2148 Set_Original_Record_Component (Id, Id);
2149
2150 if Has_Aspects (N) then
2151 Analyze_Aspect_Specifications (N, Id);
2152 end if;
2153
2154 Analyze_Dimension (N);
2155 end Analyze_Component_Declaration;
2156
2157 --------------------------
2158 -- Analyze_Declarations --
2159 --------------------------
2160
2161 procedure Analyze_Declarations (L : List_Id) is
2162 Decl : Node_Id;
2163
2164 procedure Adjust_Decl;
2165 -- Adjust Decl not to include implicit label declarations, since these
2166 -- have strange Sloc values that result in elaboration check problems.
2167 -- (They have the sloc of the label as found in the source, and that
2168 -- is ahead of the current declarative part).
2169
2170 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id);
2171 -- Determine whether Body_Decl denotes the body of a late controlled
2172 -- primitive (either Initialize, Adjust or Finalize). If this is the
2173 -- case, add a proper spec if the body lacks one. The spec is inserted
2174 -- before Body_Decl and immedately analyzed.
2175
2176 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id);
2177 -- Spec_Id is the entity of a package that may define abstract states.
2178 -- If the states have visible refinement, remove the visibility of each
2179 -- constituent at the end of the package body declarations.
2180
2181 -----------------
2182 -- Adjust_Decl --
2183 -----------------
2184
2185 procedure Adjust_Decl is
2186 begin
2187 while Present (Prev (Decl))
2188 and then Nkind (Decl) = N_Implicit_Label_Declaration
2189 loop
2190 Prev (Decl);
2191 end loop;
2192 end Adjust_Decl;
2193
2194 --------------------------------------
2195 -- Handle_Late_Controlled_Primitive --
2196 --------------------------------------
2197
2198 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id) is
2199 Body_Spec : constant Node_Id := Specification (Body_Decl);
2200 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2201 Loc : constant Source_Ptr := Sloc (Body_Id);
2202 Params : constant List_Id :=
2203 Parameter_Specifications (Body_Spec);
2204 Spec : Node_Id;
2205 Spec_Id : Entity_Id;
2206 Typ : Node_Id;
2207
2208 begin
2209 -- Consider only procedure bodies whose name matches one of the three
2210 -- controlled primitives.
2211
2212 if Nkind (Body_Spec) /= N_Procedure_Specification
2213 or else not Nam_In (Chars (Body_Id), Name_Adjust,
2214 Name_Finalize,
2215 Name_Initialize)
2216 then
2217 return;
2218
2219 -- A controlled primitive must have exactly one formal which is not
2220 -- an anonymous access type.
2221
2222 elsif List_Length (Params) /= 1 then
2223 return;
2224 end if;
2225
2226 Typ := Parameter_Type (First (Params));
2227
2228 if Nkind (Typ) = N_Access_Definition then
2229 return;
2230 end if;
2231
2232 Find_Type (Typ);
2233
2234 -- The type of the formal must be derived from [Limited_]Controlled
2235
2236 if not Is_Controlled (Entity (Typ)) then
2237 return;
2238 end if;
2239
2240 -- Check whether a specification exists for this body. We do not
2241 -- analyze the spec of the body in full, because it will be analyzed
2242 -- again when the body is properly analyzed, and we cannot create
2243 -- duplicate entries in the formals chain. We look for an explicit
2244 -- specification because the body may be an overriding operation and
2245 -- an inherited spec may be present.
2246
2247 Spec_Id := Current_Entity (Body_Id);
2248
2249 while Present (Spec_Id) loop
2250 if Ekind_In (Spec_Id, E_Procedure, E_Generic_Procedure)
2251 and then Scope (Spec_Id) = Current_Scope
2252 and then Present (First_Formal (Spec_Id))
2253 and then No (Next_Formal (First_Formal (Spec_Id)))
2254 and then Etype (First_Formal (Spec_Id)) = Entity (Typ)
2255 and then Comes_From_Source (Spec_Id)
2256 then
2257 return;
2258 end if;
2259
2260 Spec_Id := Homonym (Spec_Id);
2261 end loop;
2262
2263 -- At this point the body is known to be a late controlled primitive.
2264 -- Generate a matching spec and insert it before the body. Note the
2265 -- use of Copy_Separate_Tree - we want an entirely separate semantic
2266 -- tree in this case.
2267
2268 Spec := Copy_Separate_Tree (Body_Spec);
2269
2270 -- Ensure that the subprogram declaration does not inherit the null
2271 -- indicator from the body as we now have a proper spec/body pair.
2272
2273 Set_Null_Present (Spec, False);
2274
2275 Insert_Before_And_Analyze (Body_Decl,
2276 Make_Subprogram_Declaration (Loc, Specification => Spec));
2277 end Handle_Late_Controlled_Primitive;
2278
2279 --------------------------------
2280 -- Remove_Visible_Refinements --
2281 --------------------------------
2282
2283 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id) is
2284 State_Elmt : Elmt_Id;
2285 begin
2286 if Present (Abstract_States (Spec_Id)) then
2287 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2288 while Present (State_Elmt) loop
2289 Set_Has_Visible_Refinement (Node (State_Elmt), False);
2290 Next_Elmt (State_Elmt);
2291 end loop;
2292 end if;
2293 end Remove_Visible_Refinements;
2294
2295 -- Local variables
2296
2297 Context : Node_Id;
2298 Freeze_From : Entity_Id := Empty;
2299 Next_Decl : Node_Id;
2300 Spec_Id : Entity_Id;
2301
2302 Body_Seen : Boolean := False;
2303 -- Flag set when the first body [stub] is encountered
2304
2305 In_Package_Body : Boolean := False;
2306 -- Flag set when the current declaration list belongs to a package body
2307
2308 -- Start of processing for Analyze_Declarations
2309
2310 begin
2311 if Restriction_Check_Required (SPARK_05) then
2312 Check_Later_Vs_Basic_Declarations (L, During_Parsing => False);
2313 end if;
2314
2315 Decl := First (L);
2316 while Present (Decl) loop
2317
2318 -- Package spec cannot contain a package declaration in SPARK
2319
2320 if Nkind (Decl) = N_Package_Declaration
2321 and then Nkind (Parent (L)) = N_Package_Specification
2322 then
2323 Check_SPARK_05_Restriction
2324 ("package specification cannot contain a package declaration",
2325 Decl);
2326 end if;
2327
2328 -- Complete analysis of declaration
2329
2330 Analyze (Decl);
2331 Next_Decl := Next (Decl);
2332
2333 if No (Freeze_From) then
2334 Freeze_From := First_Entity (Current_Scope);
2335 end if;
2336
2337 -- At the end of a declarative part, freeze remaining entities
2338 -- declared in it. The end of the visible declarations of package
2339 -- specification is not the end of a declarative part if private
2340 -- declarations are present. The end of a package declaration is a
2341 -- freezing point only if it a library package. A task definition or
2342 -- protected type definition is not a freeze point either. Finally,
2343 -- we do not freeze entities in generic scopes, because there is no
2344 -- code generated for them and freeze nodes will be generated for
2345 -- the instance.
2346
2347 -- The end of a package instantiation is not a freeze point, but
2348 -- for now we make it one, because the generic body is inserted
2349 -- (currently) immediately after. Generic instantiations will not
2350 -- be a freeze point once delayed freezing of bodies is implemented.
2351 -- (This is needed in any case for early instantiations ???).
2352
2353 if No (Next_Decl) then
2354 if Nkind_In (Parent (L), N_Component_List,
2355 N_Task_Definition,
2356 N_Protected_Definition)
2357 then
2358 null;
2359
2360 elsif Nkind (Parent (L)) /= N_Package_Specification then
2361 if Nkind (Parent (L)) = N_Package_Body then
2362 Freeze_From := First_Entity (Current_Scope);
2363 end if;
2364
2365 -- There may have been several freezing points previously,
2366 -- for example object declarations or subprogram bodies, but
2367 -- at the end of a declarative part we check freezing from
2368 -- the beginning, even though entities may already be frozen,
2369 -- in order to perform visibility checks on delayed aspects.
2370
2371 Adjust_Decl;
2372 Freeze_All (First_Entity (Current_Scope), Decl);
2373 Freeze_From := Last_Entity (Current_Scope);
2374
2375 elsif Scope (Current_Scope) /= Standard_Standard
2376 and then not Is_Child_Unit (Current_Scope)
2377 and then No (Generic_Parent (Parent (L)))
2378 then
2379 null;
2380
2381 elsif L /= Visible_Declarations (Parent (L))
2382 or else No (Private_Declarations (Parent (L)))
2383 or else Is_Empty_List (Private_Declarations (Parent (L)))
2384 then
2385 Adjust_Decl;
2386 Freeze_All (First_Entity (Current_Scope), Decl);
2387 Freeze_From := Last_Entity (Current_Scope);
2388 end if;
2389
2390 -- If next node is a body then freeze all types before the body.
2391 -- An exception occurs for some expander-generated bodies. If these
2392 -- are generated at places where in general language rules would not
2393 -- allow a freeze point, then we assume that the expander has
2394 -- explicitly checked that all required types are properly frozen,
2395 -- and we do not cause general freezing here. This special circuit
2396 -- is used when the encountered body is marked as having already
2397 -- been analyzed.
2398
2399 -- In all other cases (bodies that come from source, and expander
2400 -- generated bodies that have not been analyzed yet), freeze all
2401 -- types now. Note that in the latter case, the expander must take
2402 -- care to attach the bodies at a proper place in the tree so as to
2403 -- not cause unwanted freezing at that point.
2404
2405 elsif not Analyzed (Next_Decl) and then Is_Body (Next_Decl) then
2406
2407 -- When a controlled type is frozen, the expander generates stream
2408 -- and controlled type support routines. If the freeze is caused
2409 -- by the stand alone body of Initialize, Adjust and Finalize, the
2410 -- expander will end up using the wrong version of these routines
2411 -- as the body has not been processed yet. To remedy this, detect
2412 -- a late controlled primitive and create a proper spec for it.
2413 -- This ensures that the primitive will override its inherited
2414 -- counterpart before the freeze takes place.
2415
2416 -- If the declaration we just processed is a body, do not attempt
2417 -- to examine Next_Decl as the late primitive idiom can only apply
2418 -- to the first encountered body.
2419
2420 -- The spec of the late primitive is not generated in ASIS mode to
2421 -- ensure a consistent list of primitives that indicates the true
2422 -- semantic structure of the program (which is not relevant when
2423 -- generating executable code.
2424
2425 -- ??? a cleaner approach may be possible and/or this solution
2426 -- could be extended to general-purpose late primitives, TBD.
2427
2428 if not ASIS_Mode and then not Body_Seen and then not Is_Body (Decl)
2429 then
2430 Body_Seen := True;
2431
2432 if Nkind (Next_Decl) = N_Subprogram_Body then
2433 Handle_Late_Controlled_Primitive (Next_Decl);
2434 end if;
2435 end if;
2436
2437 Adjust_Decl;
2438 Freeze_All (Freeze_From, Decl);
2439 Freeze_From := Last_Entity (Current_Scope);
2440 end if;
2441
2442 Decl := Next_Decl;
2443 end loop;
2444
2445 -- Analyze the contracts of packages and their bodies
2446
2447 if Present (L) then
2448 Context := Parent (L);
2449
2450 if Nkind (Context) = N_Package_Specification then
2451
2452 -- When a package has private declarations, its contract must be
2453 -- analyzed at the end of the said declarations. This way both the
2454 -- analysis and freeze actions are properly synchronized in case
2455 -- of private type use within the contract.
2456
2457 if L = Private_Declarations (Context) then
2458 Analyze_Package_Contract (Defining_Entity (Context));
2459
2460 -- Build the bodies of the default initial condition procedures
2461 -- for all types subject to pragma Default_Initial_Condition.
2462 -- From a purely Ada stand point, this is a freezing activity,
2463 -- however freezing is not available under GNATprove_Mode. To
2464 -- accomodate both scenarios, the bodies are build at the end
2465 -- of private declaration analysis.
2466
2467 Build_Default_Init_Cond_Procedure_Bodies (L);
2468
2469 -- Otherwise the contract is analyzed at the end of the visible
2470 -- declarations.
2471
2472 elsif L = Visible_Declarations (Context)
2473 and then No (Private_Declarations (Context))
2474 then
2475 Analyze_Package_Contract (Defining_Entity (Context));
2476 end if;
2477
2478 elsif Nkind (Context) = N_Package_Body then
2479 In_Package_Body := True;
2480 Spec_Id := Corresponding_Spec (Context);
2481
2482 Analyze_Package_Body_Contract (Defining_Entity (Context));
2483 end if;
2484 end if;
2485
2486 -- Analyze the contracts of subprogram declarations, subprogram bodies
2487 -- and variables now due to the delayed visibility requirements of their
2488 -- aspects.
2489
2490 Decl := First (L);
2491 while Present (Decl) loop
2492 if Nkind (Decl) = N_Object_Declaration then
2493 Analyze_Object_Contract (Defining_Entity (Decl));
2494
2495 elsif Nkind_In (Decl, N_Abstract_Subprogram_Declaration,
2496 N_Generic_Subprogram_Declaration,
2497 N_Subprogram_Declaration)
2498 then
2499 Analyze_Subprogram_Contract (Defining_Entity (Decl));
2500
2501 elsif Nkind (Decl) = N_Subprogram_Body then
2502 Analyze_Subprogram_Body_Contract (Defining_Entity (Decl));
2503
2504 elsif Nkind (Decl) = N_Subprogram_Body_Stub then
2505 Analyze_Subprogram_Body_Stub_Contract (Defining_Entity (Decl));
2506 end if;
2507
2508 Next (Decl);
2509 end loop;
2510
2511 -- State refinements are visible upto the end the of the package body
2512 -- declarations. Hide the refinements from visibility to restore the
2513 -- original state conditions.
2514
2515 if In_Package_Body then
2516 Remove_Visible_Refinements (Spec_Id);
2517 end if;
2518 end Analyze_Declarations;
2519
2520 -----------------------------------
2521 -- Analyze_Full_Type_Declaration --
2522 -----------------------------------
2523
2524 procedure Analyze_Full_Type_Declaration (N : Node_Id) is
2525 Def : constant Node_Id := Type_Definition (N);
2526 Def_Id : constant Entity_Id := Defining_Identifier (N);
2527 T : Entity_Id;
2528 Prev : Entity_Id;
2529
2530 Is_Remote : constant Boolean :=
2531 (Is_Remote_Types (Current_Scope)
2532 or else Is_Remote_Call_Interface (Current_Scope))
2533 and then not (In_Private_Part (Current_Scope)
2534 or else In_Package_Body (Current_Scope));
2535
2536 procedure Check_Ops_From_Incomplete_Type;
2537 -- If there is a tagged incomplete partial view of the type, traverse
2538 -- the primitives of the incomplete view and change the type of any
2539 -- controlling formals and result to indicate the full view. The
2540 -- primitives will be added to the full type's primitive operations
2541 -- list later in Sem_Disp.Check_Operation_From_Incomplete_Type (which
2542 -- is called from Process_Incomplete_Dependents).
2543
2544 ------------------------------------
2545 -- Check_Ops_From_Incomplete_Type --
2546 ------------------------------------
2547
2548 procedure Check_Ops_From_Incomplete_Type is
2549 Elmt : Elmt_Id;
2550 Formal : Entity_Id;
2551 Op : Entity_Id;
2552
2553 begin
2554 if Prev /= T
2555 and then Ekind (Prev) = E_Incomplete_Type
2556 and then Is_Tagged_Type (Prev)
2557 and then Is_Tagged_Type (T)
2558 then
2559 Elmt := First_Elmt (Primitive_Operations (Prev));
2560 while Present (Elmt) loop
2561 Op := Node (Elmt);
2562
2563 Formal := First_Formal (Op);
2564 while Present (Formal) loop
2565 if Etype (Formal) = Prev then
2566 Set_Etype (Formal, T);
2567 end if;
2568
2569 Next_Formal (Formal);
2570 end loop;
2571
2572 if Etype (Op) = Prev then
2573 Set_Etype (Op, T);
2574 end if;
2575
2576 Next_Elmt (Elmt);
2577 end loop;
2578 end if;
2579 end Check_Ops_From_Incomplete_Type;
2580
2581 -- Start of processing for Analyze_Full_Type_Declaration
2582
2583 begin
2584 Prev := Find_Type_Name (N);
2585
2586 -- The type declaration may be subject to pragma Ghost with policy
2587 -- Ignore. Set the mode now to ensure that any nodes generated during
2588 -- analysis and expansion are properly flagged as ignored Ghost.
2589
2590 Set_Ghost_Mode (N, Prev);
2591
2592 -- The full view, if present, now points to the current type. If there
2593 -- is an incomplete partial view, set a link to it, to simplify the
2594 -- retrieval of primitive operations of the type.
2595
2596 -- Ada 2005 (AI-50217): If the type was previously decorated when
2597 -- imported through a LIMITED WITH clause, it appears as incomplete
2598 -- but has no full view.
2599
2600 if Ekind (Prev) = E_Incomplete_Type
2601 and then Present (Full_View (Prev))
2602 then
2603 T := Full_View (Prev);
2604 Set_Incomplete_View (N, Parent (Prev));
2605 else
2606 T := Prev;
2607 end if;
2608
2609 Set_Is_Pure (T, Is_Pure (Current_Scope));
2610
2611 -- We set the flag Is_First_Subtype here. It is needed to set the
2612 -- corresponding flag for the Implicit class-wide-type created
2613 -- during tagged types processing.
2614
2615 Set_Is_First_Subtype (T, True);
2616
2617 -- Only composite types other than array types are allowed to have
2618 -- discriminants.
2619
2620 case Nkind (Def) is
2621
2622 -- For derived types, the rule will be checked once we've figured
2623 -- out the parent type.
2624
2625 when N_Derived_Type_Definition =>
2626 null;
2627
2628 -- For record types, discriminants are allowed, unless we are in
2629 -- SPARK.
2630
2631 when N_Record_Definition =>
2632 if Present (Discriminant_Specifications (N)) then
2633 Check_SPARK_05_Restriction
2634 ("discriminant type is not allowed",
2635 Defining_Identifier
2636 (First (Discriminant_Specifications (N))));
2637 end if;
2638
2639 when others =>
2640 if Present (Discriminant_Specifications (N)) then
2641 Error_Msg_N
2642 ("elementary or array type cannot have discriminants",
2643 Defining_Identifier
2644 (First (Discriminant_Specifications (N))));
2645 end if;
2646 end case;
2647
2648 -- Elaborate the type definition according to kind, and generate
2649 -- subsidiary (implicit) subtypes where needed. We skip this if it was
2650 -- already done (this happens during the reanalysis that follows a call
2651 -- to the high level optimizer).
2652
2653 if not Analyzed (T) then
2654 Set_Analyzed (T);
2655
2656 case Nkind (Def) is
2657 when N_Access_To_Subprogram_Definition =>
2658 Access_Subprogram_Declaration (T, Def);
2659
2660 -- If this is a remote access to subprogram, we must create the
2661 -- equivalent fat pointer type, and related subprograms.
2662
2663 if Is_Remote then
2664 Process_Remote_AST_Declaration (N);
2665 end if;
2666
2667 -- Validate categorization rule against access type declaration
2668 -- usually a violation in Pure unit, Shared_Passive unit.
2669
2670 Validate_Access_Type_Declaration (T, N);
2671
2672 when N_Access_To_Object_Definition =>
2673 Access_Type_Declaration (T, Def);
2674
2675 -- Validate categorization rule against access type declaration
2676 -- usually a violation in Pure unit, Shared_Passive unit.
2677
2678 Validate_Access_Type_Declaration (T, N);
2679
2680 -- If we are in a Remote_Call_Interface package and define a
2681 -- RACW, then calling stubs and specific stream attributes
2682 -- must be added.
2683
2684 if Is_Remote
2685 and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
2686 then
2687 Add_RACW_Features (Def_Id);
2688 end if;
2689
2690 when N_Array_Type_Definition =>
2691 Array_Type_Declaration (T, Def);
2692
2693 when N_Derived_Type_Definition =>
2694 Derived_Type_Declaration (T, N, T /= Def_Id);
2695
2696 when N_Enumeration_Type_Definition =>
2697 Enumeration_Type_Declaration (T, Def);
2698
2699 when N_Floating_Point_Definition =>
2700 Floating_Point_Type_Declaration (T, Def);
2701
2702 when N_Decimal_Fixed_Point_Definition =>
2703 Decimal_Fixed_Point_Type_Declaration (T, Def);
2704
2705 when N_Ordinary_Fixed_Point_Definition =>
2706 Ordinary_Fixed_Point_Type_Declaration (T, Def);
2707
2708 when N_Signed_Integer_Type_Definition =>
2709 Signed_Integer_Type_Declaration (T, Def);
2710
2711 when N_Modular_Type_Definition =>
2712 Modular_Type_Declaration (T, Def);
2713
2714 when N_Record_Definition =>
2715 Record_Type_Declaration (T, N, Prev);
2716
2717 -- If declaration has a parse error, nothing to elaborate.
2718
2719 when N_Error =>
2720 null;
2721
2722 when others =>
2723 raise Program_Error;
2724
2725 end case;
2726 end if;
2727
2728 if Etype (T) = Any_Type then
2729 return;
2730 end if;
2731
2732 -- Controlled type is not allowed in SPARK
2733
2734 if Is_Visibly_Controlled (T) then
2735 Check_SPARK_05_Restriction ("controlled type is not allowed", N);
2736 end if;
2737
2738 -- A type declared within a Ghost region is automatically Ghost
2739 -- (SPARK RM 6.9(2)).
2740
2741 if Comes_From_Source (T) and then Ghost_Mode > None then
2742 Set_Is_Ghost_Entity (T);
2743 end if;
2744
2745 -- Some common processing for all types
2746
2747 Set_Depends_On_Private (T, Has_Private_Component (T));
2748 Check_Ops_From_Incomplete_Type;
2749
2750 -- Both the declared entity, and its anonymous base type if one was
2751 -- created, need freeze nodes allocated.
2752
2753 declare
2754 B : constant Entity_Id := Base_Type (T);
2755
2756 begin
2757 -- In the case where the base type differs from the first subtype, we
2758 -- pre-allocate a freeze node, and set the proper link to the first
2759 -- subtype. Freeze_Entity will use this preallocated freeze node when
2760 -- it freezes the entity.
2761
2762 -- This does not apply if the base type is a generic type, whose
2763 -- declaration is independent of the current derived definition.
2764
2765 if B /= T and then not Is_Generic_Type (B) then
2766 Ensure_Freeze_Node (B);
2767 Set_First_Subtype_Link (Freeze_Node (B), T);
2768 end if;
2769
2770 -- A type that is imported through a limited_with clause cannot
2771 -- generate any code, and thus need not be frozen. However, an access
2772 -- type with an imported designated type needs a finalization list,
2773 -- which may be referenced in some other package that has non-limited
2774 -- visibility on the designated type. Thus we must create the
2775 -- finalization list at the point the access type is frozen, to
2776 -- prevent unsatisfied references at link time.
2777
2778 if not From_Limited_With (T) or else Is_Access_Type (T) then
2779 Set_Has_Delayed_Freeze (T);
2780 end if;
2781 end;
2782
2783 -- Case where T is the full declaration of some private type which has
2784 -- been swapped in Defining_Identifier (N).
2785
2786 if T /= Def_Id and then Is_Private_Type (Def_Id) then
2787 Process_Full_View (N, T, Def_Id);
2788
2789 -- Record the reference. The form of this is a little strange, since
2790 -- the full declaration has been swapped in. So the first parameter
2791 -- here represents the entity to which a reference is made which is
2792 -- the "real" entity, i.e. the one swapped in, and the second
2793 -- parameter provides the reference location.
2794
2795 -- Also, we want to kill Has_Pragma_Unreferenced temporarily here
2796 -- since we don't want a complaint about the full type being an
2797 -- unwanted reference to the private type
2798
2799 declare
2800 B : constant Boolean := Has_Pragma_Unreferenced (T);
2801 begin
2802 Set_Has_Pragma_Unreferenced (T, False);
2803 Generate_Reference (T, T, 'c');
2804 Set_Has_Pragma_Unreferenced (T, B);
2805 end;
2806
2807 Set_Completion_Referenced (Def_Id);
2808
2809 -- For completion of incomplete type, process incomplete dependents
2810 -- and always mark the full type as referenced (it is the incomplete
2811 -- type that we get for any real reference).
2812
2813 elsif Ekind (Prev) = E_Incomplete_Type then
2814 Process_Incomplete_Dependents (N, T, Prev);
2815 Generate_Reference (Prev, Def_Id, 'c');
2816 Set_Completion_Referenced (Def_Id);
2817
2818 -- If not private type or incomplete type completion, this is a real
2819 -- definition of a new entity, so record it.
2820
2821 else
2822 Generate_Definition (Def_Id);
2823 end if;
2824
2825 -- Propagate any pending access types whose finalization masters need to
2826 -- be fully initialized from the partial to the full view. Guard against
2827 -- an illegal full view that remains unanalyzed.
2828
2829 if Is_Type (Def_Id) and then Is_Incomplete_Or_Private_Type (Prev) then
2830 Set_Pending_Access_Types (Def_Id, Pending_Access_Types (Prev));
2831 end if;
2832
2833 if Chars (Scope (Def_Id)) = Name_System
2834 and then Chars (Def_Id) = Name_Address
2835 and then Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (N)))
2836 then
2837 Set_Is_Descendent_Of_Address (Def_Id);
2838 Set_Is_Descendent_Of_Address (Base_Type (Def_Id));
2839 Set_Is_Descendent_Of_Address (Prev);
2840 end if;
2841
2842 Set_Optimize_Alignment_Flags (Def_Id);
2843 Check_Eliminated (Def_Id);
2844
2845 -- If the declaration is a completion and aspects are present, apply
2846 -- them to the entity for the type which is currently the partial
2847 -- view, but which is the one that will be frozen.
2848
2849 if Has_Aspects (N) then
2850
2851 -- In most cases the partial view is a private type, and both views
2852 -- appear in different declarative parts. In the unusual case where
2853 -- the partial view is incomplete, perform the analysis on the
2854 -- full view, to prevent freezing anomalies with the corresponding
2855 -- class-wide type, which otherwise might be frozen before the
2856 -- dispatch table is built.
2857
2858 if Prev /= Def_Id
2859 and then Ekind (Prev) /= E_Incomplete_Type
2860 then
2861 Analyze_Aspect_Specifications (N, Prev);
2862
2863 -- Normal case
2864
2865 else
2866 Analyze_Aspect_Specifications (N, Def_Id);
2867 end if;
2868 end if;
2869 end Analyze_Full_Type_Declaration;
2870
2871 ----------------------------------
2872 -- Analyze_Incomplete_Type_Decl --
2873 ----------------------------------
2874
2875 procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
2876 F : constant Boolean := Is_Pure (Current_Scope);
2877 T : Entity_Id;
2878
2879 begin
2880 Check_SPARK_05_Restriction ("incomplete type is not allowed", N);
2881
2882 Generate_Definition (Defining_Identifier (N));
2883
2884 -- Process an incomplete declaration. The identifier must not have been
2885 -- declared already in the scope. However, an incomplete declaration may
2886 -- appear in the private part of a package, for a private type that has
2887 -- already been declared.
2888
2889 -- In this case, the discriminants (if any) must match
2890
2891 T := Find_Type_Name (N);
2892
2893 Set_Ekind (T, E_Incomplete_Type);
2894 Init_Size_Align (T);
2895 Set_Is_First_Subtype (T, True);
2896 Set_Etype (T, T);
2897
2898 -- An incomplete type declared within a Ghost region is automatically
2899 -- Ghost (SPARK RM 6.9(2)).
2900
2901 if Ghost_Mode > None then
2902 Set_Is_Ghost_Entity (T);
2903 end if;
2904
2905 -- Ada 2005 (AI-326): Minimum decoration to give support to tagged
2906 -- incomplete types.
2907
2908 if Tagged_Present (N) then
2909 Set_Is_Tagged_Type (T, True);
2910 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
2911 Make_Class_Wide_Type (T);
2912 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2913 end if;
2914
2915 Push_Scope (T);
2916
2917 Set_Stored_Constraint (T, No_Elist);
2918
2919 if Present (Discriminant_Specifications (N)) then
2920 Process_Discriminants (N);
2921 end if;
2922
2923 End_Scope;
2924
2925 -- If the type has discriminants, non-trivial subtypes may be
2926 -- declared before the full view of the type. The full views of those
2927 -- subtypes will be built after the full view of the type.
2928
2929 Set_Private_Dependents (T, New_Elmt_List);
2930 Set_Is_Pure (T, F);
2931 end Analyze_Incomplete_Type_Decl;
2932
2933 -----------------------------------
2934 -- Analyze_Interface_Declaration --
2935 -----------------------------------
2936
2937 procedure Analyze_Interface_Declaration (T : Entity_Id; Def : Node_Id) is
2938 CW : constant Entity_Id := Class_Wide_Type (T);
2939
2940 begin
2941 Set_Is_Tagged_Type (T);
2942 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
2943
2944 Set_Is_Limited_Record (T, Limited_Present (Def)
2945 or else Task_Present (Def)
2946 or else Protected_Present (Def)
2947 or else Synchronized_Present (Def));
2948
2949 -- Type is abstract if full declaration carries keyword, or if previous
2950 -- partial view did.
2951
2952 Set_Is_Abstract_Type (T);
2953 Set_Is_Interface (T);
2954
2955 -- Type is a limited interface if it includes the keyword limited, task,
2956 -- protected, or synchronized.
2957
2958 Set_Is_Limited_Interface
2959 (T, Limited_Present (Def)
2960 or else Protected_Present (Def)
2961 or else Synchronized_Present (Def)
2962 or else Task_Present (Def));
2963
2964 Set_Interfaces (T, New_Elmt_List);
2965 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2966
2967 -- Complete the decoration of the class-wide entity if it was already
2968 -- built (i.e. during the creation of the limited view)
2969
2970 if Present (CW) then
2971 Set_Is_Interface (CW);
2972 Set_Is_Limited_Interface (CW, Is_Limited_Interface (T));
2973 end if;
2974
2975 -- Check runtime support for synchronized interfaces
2976
2977 if VM_Target = No_VM
2978 and then (Is_Task_Interface (T)
2979 or else Is_Protected_Interface (T)
2980 or else Is_Synchronized_Interface (T))
2981 and then not RTE_Available (RE_Select_Specific_Data)
2982 then
2983 Error_Msg_CRT ("synchronized interfaces", T);
2984 end if;
2985 end Analyze_Interface_Declaration;
2986
2987 -----------------------------
2988 -- Analyze_Itype_Reference --
2989 -----------------------------
2990
2991 -- Nothing to do. This node is placed in the tree only for the benefit of
2992 -- back end processing, and has no effect on the semantic processing.
2993
2994 procedure Analyze_Itype_Reference (N : Node_Id) is
2995 begin
2996 pragma Assert (Is_Itype (Itype (N)));
2997 null;
2998 end Analyze_Itype_Reference;
2999
3000 --------------------------------
3001 -- Analyze_Number_Declaration --
3002 --------------------------------
3003
3004 procedure Analyze_Number_Declaration (N : Node_Id) is
3005 Id : constant Entity_Id := Defining_Identifier (N);
3006 E : constant Node_Id := Expression (N);
3007 T : Entity_Id;
3008 Index : Interp_Index;
3009 It : Interp;
3010
3011 begin
3012 -- The number declaration may be subject to pragma Ghost with policy
3013 -- Ignore. Set the mode now to ensure that any nodes generated during
3014 -- analysis and expansion are properly flagged as ignored Ghost.
3015
3016 Set_Ghost_Mode (N);
3017
3018 Generate_Definition (Id);
3019 Enter_Name (Id);
3020
3021 -- A number declared within a Ghost region is automatically Ghost
3022 -- (SPARK RM 6.9(2)).
3023
3024 if Ghost_Mode > None then
3025 Set_Is_Ghost_Entity (Id);
3026 end if;
3027
3028 -- This is an optimization of a common case of an integer literal
3029
3030 if Nkind (E) = N_Integer_Literal then
3031 Set_Is_Static_Expression (E, True);
3032 Set_Etype (E, Universal_Integer);
3033
3034 Set_Etype (Id, Universal_Integer);
3035 Set_Ekind (Id, E_Named_Integer);
3036 Set_Is_Frozen (Id, True);
3037 return;
3038 end if;
3039
3040 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3041
3042 -- Process expression, replacing error by integer zero, to avoid
3043 -- cascaded errors or aborts further along in the processing
3044
3045 -- Replace Error by integer zero, which seems least likely to cause
3046 -- cascaded errors.
3047
3048 if E = Error then
3049 Rewrite (E, Make_Integer_Literal (Sloc (E), Uint_0));
3050 Set_Error_Posted (E);
3051 end if;
3052
3053 Analyze (E);
3054
3055 -- Verify that the expression is static and numeric. If
3056 -- the expression is overloaded, we apply the preference
3057 -- rule that favors root numeric types.
3058
3059 if not Is_Overloaded (E) then
3060 T := Etype (E);
3061 if Has_Dynamic_Predicate_Aspect (T) then
3062 Error_Msg_N
3063 ("subtype has dynamic predicate, "
3064 & "not allowed in number declaration", N);
3065 end if;
3066
3067 else
3068 T := Any_Type;
3069
3070 Get_First_Interp (E, Index, It);
3071 while Present (It.Typ) loop
3072 if (Is_Integer_Type (It.Typ) or else Is_Real_Type (It.Typ))
3073 and then (Scope (Base_Type (It.Typ))) = Standard_Standard
3074 then
3075 if T = Any_Type then
3076 T := It.Typ;
3077
3078 elsif It.Typ = Universal_Real
3079 or else
3080 It.Typ = Universal_Integer
3081 then
3082 -- Choose universal interpretation over any other
3083
3084 T := It.Typ;
3085 exit;
3086 end if;
3087 end if;
3088
3089 Get_Next_Interp (Index, It);
3090 end loop;
3091 end if;
3092
3093 if Is_Integer_Type (T) then
3094 Resolve (E, T);
3095 Set_Etype (Id, Universal_Integer);
3096 Set_Ekind (Id, E_Named_Integer);
3097
3098 elsif Is_Real_Type (T) then
3099
3100 -- Because the real value is converted to universal_real, this is a
3101 -- legal context for a universal fixed expression.
3102
3103 if T = Universal_Fixed then
3104 declare
3105 Loc : constant Source_Ptr := Sloc (N);
3106 Conv : constant Node_Id := Make_Type_Conversion (Loc,
3107 Subtype_Mark =>
3108 New_Occurrence_Of (Universal_Real, Loc),
3109 Expression => Relocate_Node (E));
3110
3111 begin
3112 Rewrite (E, Conv);
3113 Analyze (E);
3114 end;
3115
3116 elsif T = Any_Fixed then
3117 Error_Msg_N ("illegal context for mixed mode operation", E);
3118
3119 -- Expression is of the form : universal_fixed * integer. Try to
3120 -- resolve as universal_real.
3121
3122 T := Universal_Real;
3123 Set_Etype (E, T);
3124 end if;
3125
3126 Resolve (E, T);
3127 Set_Etype (Id, Universal_Real);
3128 Set_Ekind (Id, E_Named_Real);
3129
3130 else
3131 Wrong_Type (E, Any_Numeric);
3132 Resolve (E, T);
3133
3134 Set_Etype (Id, T);
3135 Set_Ekind (Id, E_Constant);
3136 Set_Never_Set_In_Source (Id, True);
3137 Set_Is_True_Constant (Id, True);
3138 return;
3139 end if;
3140
3141 if Nkind_In (E, N_Integer_Literal, N_Real_Literal) then
3142 Set_Etype (E, Etype (Id));
3143 end if;
3144
3145 if not Is_OK_Static_Expression (E) then
3146 Flag_Non_Static_Expr
3147 ("non-static expression used in number declaration!", E);
3148 Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
3149 Set_Etype (E, Any_Type);
3150 end if;
3151 end Analyze_Number_Declaration;
3152
3153 -----------------------------
3154 -- Analyze_Object_Contract --
3155 -----------------------------
3156
3157 procedure Analyze_Object_Contract (Obj_Id : Entity_Id) is
3158 Obj_Typ : constant Entity_Id := Etype (Obj_Id);
3159 AR_Val : Boolean := False;
3160 AW_Val : Boolean := False;
3161 ER_Val : Boolean := False;
3162 EW_Val : Boolean := False;
3163 Prag : Node_Id;
3164 Seen : Boolean := False;
3165
3166 begin
3167 -- The loop parameter in an element iterator over a formal container
3168 -- is declared with an object declaration but no contracts apply.
3169
3170 if Ekind (Obj_Id) = E_Loop_Parameter then
3171 return;
3172 end if;
3173
3174 if Ekind (Obj_Id) = E_Constant then
3175
3176 -- A constant cannot be effectively volatile. This check is only
3177 -- relevant with SPARK_Mode on as it is not a standard Ada legality
3178 -- rule. Do not flag internally-generated constants that map generic
3179 -- formals to actuals in instantiations (SPARK RM 7.1.3(6)).
3180
3181 if SPARK_Mode = On
3182 and then Is_Effectively_Volatile (Obj_Id)
3183 and then No (Corresponding_Generic_Association (Parent (Obj_Id)))
3184
3185 -- Don't give this for internally generated entities (such as the
3186 -- FIRST and LAST temporaries generated for bounds).
3187
3188 and then Comes_From_Source (Obj_Id)
3189 then
3190 Error_Msg_N ("constant cannot be volatile", Obj_Id);
3191 end if;
3192
3193 else pragma Assert (Ekind (Obj_Id) = E_Variable);
3194
3195 -- The following checks are only relevant when SPARK_Mode is on as
3196 -- they are not standard Ada legality rules. Internally generated
3197 -- temporaries are ignored.
3198
3199 if SPARK_Mode = On and then Comes_From_Source (Obj_Id) then
3200 if Is_Effectively_Volatile (Obj_Id) then
3201
3202 -- The declaration of an effectively volatile object must
3203 -- appear at the library level (SPARK RM 7.1.3(7), C.6(6)).
3204
3205 if not Is_Library_Level_Entity (Obj_Id) then
3206 Error_Msg_N
3207 ("volatile variable & must be declared at library level",
3208 Obj_Id);
3209
3210 -- An object of a discriminated type cannot be effectively
3211 -- volatile (SPARK RM C.6(4)).
3212
3213 elsif Has_Discriminants (Obj_Typ) then
3214 Error_Msg_N
3215 ("discriminated object & cannot be volatile", Obj_Id);
3216
3217 -- An object of a tagged type cannot be effectively volatile
3218 -- (SPARK RM C.6(5)).
3219
3220 elsif Is_Tagged_Type (Obj_Typ) then
3221 Error_Msg_N ("tagged object & cannot be volatile", Obj_Id);
3222 end if;
3223
3224 -- The object is not effectively volatile
3225
3226 else
3227 -- A non-effectively volatile object cannot have effectively
3228 -- volatile components (SPARK RM 7.1.3(7)).
3229
3230 if not Is_Effectively_Volatile (Obj_Id)
3231 and then Has_Volatile_Component (Obj_Typ)
3232 then
3233 Error_Msg_N
3234 ("non-volatile object & cannot have volatile components",
3235 Obj_Id);
3236 end if;
3237 end if;
3238 end if;
3239
3240 if Is_Ghost_Entity (Obj_Id) then
3241
3242 -- A Ghost object cannot be effectively volatile (SPARK RM 6.9(8))
3243
3244 if Is_Effectively_Volatile (Obj_Id) then
3245 Error_Msg_N ("ghost variable & cannot be volatile", Obj_Id);
3246
3247 -- A Ghost object cannot be imported or exported (SPARK RM 6.9(8))
3248
3249 elsif Is_Imported (Obj_Id) then
3250 Error_Msg_N ("ghost object & cannot be imported", Obj_Id);
3251
3252 elsif Is_Exported (Obj_Id) then
3253 Error_Msg_N ("ghost object & cannot be exported", Obj_Id);
3254 end if;
3255 end if;
3256
3257 -- Analyze all external properties
3258
3259 Prag := Get_Pragma (Obj_Id, Pragma_Async_Readers);
3260
3261 if Present (Prag) then
3262 Analyze_External_Property_In_Decl_Part (Prag, AR_Val);
3263 Seen := True;
3264 end if;
3265
3266 Prag := Get_Pragma (Obj_Id, Pragma_Async_Writers);
3267
3268 if Present (Prag) then
3269 Analyze_External_Property_In_Decl_Part (Prag, AW_Val);
3270 Seen := True;
3271 end if;
3272
3273 Prag := Get_Pragma (Obj_Id, Pragma_Effective_Reads);
3274
3275 if Present (Prag) then
3276 Analyze_External_Property_In_Decl_Part (Prag, ER_Val);
3277 Seen := True;
3278 end if;
3279
3280 Prag := Get_Pragma (Obj_Id, Pragma_Effective_Writes);
3281
3282 if Present (Prag) then
3283 Analyze_External_Property_In_Decl_Part (Prag, EW_Val);
3284 Seen := True;
3285 end if;
3286
3287 -- Verify the mutual interaction of the various external properties
3288
3289 if Seen then
3290 Check_External_Properties (Obj_Id, AR_Val, AW_Val, ER_Val, EW_Val);
3291 end if;
3292
3293 -- Check whether the lack of indicator Part_Of agrees with the
3294 -- placement of the variable with respect to the state space.
3295
3296 Prag := Get_Pragma (Obj_Id, Pragma_Part_Of);
3297
3298 if No (Prag) then
3299 Check_Missing_Part_Of (Obj_Id);
3300 end if;
3301 end if;
3302
3303 -- A ghost object cannot be imported or exported (SPARK RM 6.9(8))
3304
3305 if Is_Ghost_Entity (Obj_Id) then
3306 if Is_Exported (Obj_Id) then
3307 Error_Msg_N ("ghost object & cannot be exported", Obj_Id);
3308
3309 elsif Is_Imported (Obj_Id) then
3310 Error_Msg_N ("ghost object & cannot be imported", Obj_Id);
3311 end if;
3312 end if;
3313 end Analyze_Object_Contract;
3314
3315 --------------------------------
3316 -- Analyze_Object_Declaration --
3317 --------------------------------
3318
3319 procedure Analyze_Object_Declaration (N : Node_Id) is
3320 Loc : constant Source_Ptr := Sloc (N);
3321 Id : constant Entity_Id := Defining_Identifier (N);
3322 T : Entity_Id;
3323 Act_T : Entity_Id;
3324
3325 E : Node_Id := Expression (N);
3326 -- E is set to Expression (N) throughout this routine. When
3327 -- Expression (N) is modified, E is changed accordingly.
3328
3329 Prev_Entity : Entity_Id := Empty;
3330
3331 function Count_Tasks (T : Entity_Id) return Uint;
3332 -- This function is called when a non-generic library level object of a
3333 -- task type is declared. Its function is to count the static number of
3334 -- tasks declared within the type (it is only called if Has_Tasks is set
3335 -- for T). As a side effect, if an array of tasks with non-static bounds
3336 -- or a variant record type is encountered, Check_Restrictions is called
3337 -- indicating the count is unknown.
3338
3339 function Delayed_Aspect_Present return Boolean;
3340 -- If the declaration has an expression that is an aggregate, and it
3341 -- has aspects that require delayed analysis, the resolution of the
3342 -- aggregate must be deferred to the freeze point of the objet. This
3343 -- special processing was created for address clauses, but it must
3344 -- also apply to Alignment.
3345 -- This must be done before the aspect specifications are analyzed
3346 -- because we must handle the aggregate before the analysis of the
3347 -- object declaration is complete.
3348
3349 -- any other relevant delayed aspects on object declarations ???
3350
3351 -----------------
3352 -- Count_Tasks --
3353 -----------------
3354
3355 function Count_Tasks (T : Entity_Id) return Uint is
3356 C : Entity_Id;
3357 X : Node_Id;
3358 V : Uint;
3359
3360 begin
3361 if Is_Task_Type (T) then
3362 return Uint_1;
3363
3364 elsif Is_Record_Type (T) then
3365 if Has_Discriminants (T) then
3366 Check_Restriction (Max_Tasks, N);
3367 return Uint_0;
3368
3369 else
3370 V := Uint_0;
3371 C := First_Component (T);
3372 while Present (C) loop
3373 V := V + Count_Tasks (Etype (C));
3374 Next_Component (C);
3375 end loop;
3376
3377 return V;
3378 end if;
3379
3380 elsif Is_Array_Type (T) then
3381 X := First_Index (T);
3382 V := Count_Tasks (Component_Type (T));
3383 while Present (X) loop
3384 C := Etype (X);
3385
3386 if not Is_OK_Static_Subtype (C) then
3387 Check_Restriction (Max_Tasks, N);
3388 return Uint_0;
3389 else
3390 V := V * (UI_Max (Uint_0,
3391 Expr_Value (Type_High_Bound (C)) -
3392 Expr_Value (Type_Low_Bound (C)) + Uint_1));
3393 end if;
3394
3395 Next_Index (X);
3396 end loop;
3397
3398 return V;
3399
3400 else
3401 return Uint_0;
3402 end if;
3403 end Count_Tasks;
3404
3405 ----------------------------
3406 -- Delayed_Aspect_Present --
3407 ----------------------------
3408
3409 function Delayed_Aspect_Present return Boolean is
3410 A : Node_Id;
3411 A_Id : Aspect_Id;
3412
3413 begin
3414 if Present (Aspect_Specifications (N)) then
3415 A := First (Aspect_Specifications (N));
3416 A_Id := Get_Aspect_Id (Chars (Identifier (A)));
3417 while Present (A) loop
3418 if
3419 A_Id = Aspect_Alignment or else A_Id = Aspect_Address
3420 then
3421 return True;
3422 end if;
3423
3424 Next (A);
3425 end loop;
3426 end if;
3427
3428 return False;
3429 end Delayed_Aspect_Present;
3430
3431 -- Start of processing for Analyze_Object_Declaration
3432
3433 begin
3434 -- There are three kinds of implicit types generated by an
3435 -- object declaration:
3436
3437 -- 1. Those generated by the original Object Definition
3438
3439 -- 2. Those generated by the Expression
3440
3441 -- 3. Those used to constrain the Object Definition with the
3442 -- expression constraints when the definition is unconstrained.
3443
3444 -- They must be generated in this order to avoid order of elaboration
3445 -- issues. Thus the first step (after entering the name) is to analyze
3446 -- the object definition.
3447
3448 if Constant_Present (N) then
3449 Prev_Entity := Current_Entity_In_Scope (Id);
3450
3451 if Present (Prev_Entity)
3452 and then
3453 -- If the homograph is an implicit subprogram, it is overridden
3454 -- by the current declaration.
3455
3456 ((Is_Overloadable (Prev_Entity)
3457 and then Is_Inherited_Operation (Prev_Entity))
3458
3459 -- The current object is a discriminal generated for an entry
3460 -- family index. Even though the index is a constant, in this
3461 -- particular context there is no true constant redeclaration.
3462 -- Enter_Name will handle the visibility.
3463
3464 or else
3465 (Is_Discriminal (Id)
3466 and then Ekind (Discriminal_Link (Id)) =
3467 E_Entry_Index_Parameter)
3468
3469 -- The current object is the renaming for a generic declared
3470 -- within the instance.
3471
3472 or else
3473 (Ekind (Prev_Entity) = E_Package
3474 and then Nkind (Parent (Prev_Entity)) =
3475 N_Package_Renaming_Declaration
3476 and then not Comes_From_Source (Prev_Entity)
3477 and then
3478 Is_Generic_Instance (Renamed_Entity (Prev_Entity))))
3479 then
3480 Prev_Entity := Empty;
3481 end if;
3482 end if;
3483
3484 -- The object declaration may be subject to pragma Ghost with policy
3485 -- Ignore. Set the mode now to ensure that any nodes generated during
3486 -- analysis and expansion are properly flagged as ignored Ghost.
3487
3488 Set_Ghost_Mode (N, Prev_Entity);
3489
3490 if Present (Prev_Entity) then
3491 Constant_Redeclaration (Id, N, T);
3492
3493 Generate_Reference (Prev_Entity, Id, 'c');
3494 Set_Completion_Referenced (Id);
3495
3496 if Error_Posted (N) then
3497
3498 -- Type mismatch or illegal redeclaration, Do not analyze
3499 -- expression to avoid cascaded errors.
3500
3501 T := Find_Type_Of_Object (Object_Definition (N), N);
3502 Set_Etype (Id, T);
3503 Set_Ekind (Id, E_Variable);
3504 goto Leave;
3505 end if;
3506
3507 -- In the normal case, enter identifier at the start to catch premature
3508 -- usage in the initialization expression.
3509
3510 else
3511 Generate_Definition (Id);
3512 Enter_Name (Id);
3513
3514 Mark_Coextensions (N, Object_Definition (N));
3515
3516 T := Find_Type_Of_Object (Object_Definition (N), N);
3517
3518 if Nkind (Object_Definition (N)) = N_Access_Definition
3519 and then Present
3520 (Access_To_Subprogram_Definition (Object_Definition (N)))
3521 and then Protected_Present
3522 (Access_To_Subprogram_Definition (Object_Definition (N)))
3523 then
3524 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
3525 end if;
3526
3527 if Error_Posted (Id) then
3528 Set_Etype (Id, T);
3529 Set_Ekind (Id, E_Variable);
3530 goto Leave;
3531 end if;
3532 end if;
3533
3534 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
3535 -- out some static checks
3536
3537 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (T) then
3538
3539 -- In case of aggregates we must also take care of the correct
3540 -- initialization of nested aggregates bug this is done at the
3541 -- point of the analysis of the aggregate (see sem_aggr.adb).
3542
3543 if Present (Expression (N))
3544 and then Nkind (Expression (N)) = N_Aggregate
3545 then
3546 null;
3547
3548 else
3549 declare
3550 Save_Typ : constant Entity_Id := Etype (Id);
3551 begin
3552 Set_Etype (Id, T); -- Temp. decoration for static checks
3553 Null_Exclusion_Static_Checks (N);
3554 Set_Etype (Id, Save_Typ);
3555 end;
3556 end if;
3557 end if;
3558
3559 -- Object is marked pure if it is in a pure scope
3560
3561 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3562
3563 -- If deferred constant, make sure context is appropriate. We detect
3564 -- a deferred constant as a constant declaration with no expression.
3565 -- A deferred constant can appear in a package body if its completion
3566 -- is by means of an interface pragma.
3567
3568 if Constant_Present (N) and then No (E) then
3569
3570 -- A deferred constant may appear in the declarative part of the
3571 -- following constructs:
3572
3573 -- blocks
3574 -- entry bodies
3575 -- extended return statements
3576 -- package specs
3577 -- package bodies
3578 -- subprogram bodies
3579 -- task bodies
3580
3581 -- When declared inside a package spec, a deferred constant must be
3582 -- completed by a full constant declaration or pragma Import. In all
3583 -- other cases, the only proper completion is pragma Import. Extended
3584 -- return statements are flagged as invalid contexts because they do
3585 -- not have a declarative part and so cannot accommodate the pragma.
3586
3587 if Ekind (Current_Scope) = E_Return_Statement then
3588 Error_Msg_N
3589 ("invalid context for deferred constant declaration (RM 7.4)",
3590 N);
3591 Error_Msg_N
3592 ("\declaration requires an initialization expression",
3593 N);
3594 Set_Constant_Present (N, False);
3595
3596 -- In Ada 83, deferred constant must be of private type
3597
3598 elsif not Is_Private_Type (T) then
3599 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
3600 Error_Msg_N
3601 ("(Ada 83) deferred constant must be private type", N);
3602 end if;
3603 end if;
3604
3605 -- If not a deferred constant, then the object declaration freezes
3606 -- its type, unless the object is of an anonymous type and has delayed
3607 -- aspects. In that case the type is frozen when the object itself is.
3608
3609 else
3610 Check_Fully_Declared (T, N);
3611
3612 if Has_Delayed_Aspects (Id)
3613 and then Is_Array_Type (T)
3614 and then Is_Itype (T)
3615 then
3616 Set_Has_Delayed_Freeze (T);
3617 else
3618 Freeze_Before (N, T);
3619 end if;
3620 end if;
3621
3622 -- If the object was created by a constrained array definition, then
3623 -- set the link in both the anonymous base type and anonymous subtype
3624 -- that are built to represent the array type to point to the object.
3625
3626 if Nkind (Object_Definition (Declaration_Node (Id))) =
3627 N_Constrained_Array_Definition
3628 then
3629 Set_Related_Array_Object (T, Id);
3630 Set_Related_Array_Object (Base_Type (T), Id);
3631 end if;
3632
3633 -- Special checks for protected objects not at library level
3634
3635 if Is_Protected_Type (T)
3636 and then not Is_Library_Level_Entity (Id)
3637 then
3638 Check_Restriction (No_Local_Protected_Objects, Id);
3639
3640 -- Protected objects with interrupt handlers must be at library level
3641
3642 -- Ada 2005: This test is not needed (and the corresponding clause
3643 -- in the RM is removed) because accessibility checks are sufficient
3644 -- to make handlers not at the library level illegal.
3645
3646 -- AI05-0303: The AI is in fact a binding interpretation, and thus
3647 -- applies to the '95 version of the language as well.
3648
3649 if Has_Interrupt_Handler (T) and then Ada_Version < Ada_95 then
3650 Error_Msg_N
3651 ("interrupt object can only be declared at library level", Id);
3652 end if;
3653 end if;
3654
3655 -- The actual subtype of the object is the nominal subtype, unless
3656 -- the nominal one is unconstrained and obtained from the expression.
3657
3658 Act_T := T;
3659
3660 -- These checks should be performed before the initialization expression
3661 -- is considered, so that the Object_Definition node is still the same
3662 -- as in source code.
3663
3664 -- In SPARK, the nominal subtype is always given by a subtype mark
3665 -- and must not be unconstrained. (The only exception to this is the
3666 -- acceptance of declarations of constants of type String.)
3667
3668 if not Nkind_In (Object_Definition (N), N_Expanded_Name, N_Identifier)
3669 then
3670 Check_SPARK_05_Restriction
3671 ("subtype mark required", Object_Definition (N));
3672
3673 elsif Is_Array_Type (T)
3674 and then not Is_Constrained (T)
3675 and then T /= Standard_String
3676 then
3677 Check_SPARK_05_Restriction
3678 ("subtype mark of constrained type expected",
3679 Object_Definition (N));
3680 end if;
3681
3682 -- There are no aliased objects in SPARK
3683
3684 if Aliased_Present (N) then
3685 Check_SPARK_05_Restriction ("aliased object is not allowed", N);
3686 end if;
3687
3688 -- Process initialization expression if present and not in error
3689
3690 if Present (E) and then E /= Error then
3691
3692 -- Generate an error in case of CPP class-wide object initialization.
3693 -- Required because otherwise the expansion of the class-wide
3694 -- assignment would try to use 'size to initialize the object
3695 -- (primitive that is not available in CPP tagged types).
3696
3697 if Is_Class_Wide_Type (Act_T)
3698 and then
3699 (Is_CPP_Class (Root_Type (Etype (Act_T)))
3700 or else
3701 (Present (Full_View (Root_Type (Etype (Act_T))))
3702 and then
3703 Is_CPP_Class (Full_View (Root_Type (Etype (Act_T))))))
3704 then
3705 Error_Msg_N
3706 ("predefined assignment not available for 'C'P'P tagged types",
3707 E);
3708 end if;
3709
3710 Mark_Coextensions (N, E);
3711 Analyze (E);
3712
3713 -- In case of errors detected in the analysis of the expression,
3714 -- decorate it with the expected type to avoid cascaded errors
3715
3716 if No (Etype (E)) then
3717 Set_Etype (E, T);
3718 end if;
3719
3720 -- If an initialization expression is present, then we set the
3721 -- Is_True_Constant flag. It will be reset if this is a variable
3722 -- and it is indeed modified.
3723
3724 Set_Is_True_Constant (Id, True);
3725
3726 -- If we are analyzing a constant declaration, set its completion
3727 -- flag after analyzing and resolving the expression.
3728
3729 if Constant_Present (N) then
3730 Set_Has_Completion (Id);
3731 end if;
3732
3733 -- Set type and resolve (type may be overridden later on). Note:
3734 -- Ekind (Id) must still be E_Void at this point so that incorrect
3735 -- early usage within E is properly diagnosed.
3736
3737 Set_Etype (Id, T);
3738
3739 -- If the expression is an aggregate we must look ahead to detect
3740 -- the possible presence of an address clause, and defer resolution
3741 -- and expansion of the aggregate to the freeze point of the entity.
3742
3743 if Comes_From_Source (N)
3744 and then Expander_Active
3745 and then Nkind (E) = N_Aggregate
3746 and then (Present (Following_Address_Clause (N))
3747 or else Delayed_Aspect_Present)
3748 then
3749 Set_Etype (E, T);
3750
3751 else
3752 Resolve (E, T);
3753 end if;
3754
3755 -- No further action needed if E is a call to an inlined function
3756 -- which returns an unconstrained type and it has been expanded into
3757 -- a procedure call. In that case N has been replaced by an object
3758 -- declaration without initializing expression and it has been
3759 -- analyzed (see Expand_Inlined_Call).
3760
3761 if Back_End_Inlining
3762 and then Expander_Active
3763 and then Nkind (E) = N_Function_Call
3764 and then Nkind (Name (E)) in N_Has_Entity
3765 and then Is_Inlined (Entity (Name (E)))
3766 and then not Is_Constrained (Etype (E))
3767 and then Analyzed (N)
3768 and then No (Expression (N))
3769 then
3770 return;
3771 end if;
3772
3773 -- If E is null and has been replaced by an N_Raise_Constraint_Error
3774 -- node (which was marked already-analyzed), we need to set the type
3775 -- to something other than Any_Access in order to keep gigi happy.
3776
3777 if Etype (E) = Any_Access then
3778 Set_Etype (E, T);
3779 end if;
3780
3781 -- If the object is an access to variable, the initialization
3782 -- expression cannot be an access to constant.
3783
3784 if Is_Access_Type (T)
3785 and then not Is_Access_Constant (T)
3786 and then Is_Access_Type (Etype (E))
3787 and then Is_Access_Constant (Etype (E))
3788 then
3789 Error_Msg_N
3790 ("access to variable cannot be initialized with an "
3791 & "access-to-constant expression", E);
3792 end if;
3793
3794 if not Assignment_OK (N) then
3795 Check_Initialization (T, E);
3796 end if;
3797
3798 Check_Unset_Reference (E);
3799
3800 -- If this is a variable, then set current value. If this is a
3801 -- declared constant of a scalar type with a static expression,
3802 -- indicate that it is always valid.
3803
3804 if not Constant_Present (N) then
3805 if Compile_Time_Known_Value (E) then
3806 Set_Current_Value (Id, E);
3807 end if;
3808
3809 elsif Is_Scalar_Type (T) and then Is_OK_Static_Expression (E) then
3810 Set_Is_Known_Valid (Id);
3811 end if;
3812
3813 -- Deal with setting of null flags
3814
3815 if Is_Access_Type (T) then
3816 if Known_Non_Null (E) then
3817 Set_Is_Known_Non_Null (Id, True);
3818 elsif Known_Null (E) and then not Can_Never_Be_Null (Id) then
3819 Set_Is_Known_Null (Id, True);
3820 end if;
3821 end if;
3822
3823 -- Check incorrect use of dynamically tagged expressions
3824
3825 if Is_Tagged_Type (T) then
3826 Check_Dynamically_Tagged_Expression
3827 (Expr => E,
3828 Typ => T,
3829 Related_Nod => N);
3830 end if;
3831
3832 Apply_Scalar_Range_Check (E, T);
3833 Apply_Static_Length_Check (E, T);
3834
3835 if Nkind (Original_Node (N)) = N_Object_Declaration
3836 and then Comes_From_Source (Original_Node (N))
3837
3838 -- Only call test if needed
3839
3840 and then Restriction_Check_Required (SPARK_05)
3841 and then not Is_SPARK_05_Initialization_Expr (Original_Node (E))
3842 then
3843 Check_SPARK_05_Restriction
3844 ("initialization expression is not appropriate", E);
3845 end if;
3846
3847 -- A formal parameter of a specific tagged type whose related
3848 -- subprogram is subject to pragma Extensions_Visible with value
3849 -- "False" cannot be implicitly converted to a class-wide type by
3850 -- means of an initialization expression (SPARK RM 6.1.7(3)).
3851
3852 if Is_Class_Wide_Type (T) and then Is_EVF_Expression (E) then
3853 Error_Msg_N
3854 ("formal parameter with Extensions_Visible False cannot be "
3855 & "implicitly converted to class-wide type", E);
3856 end if;
3857 end if;
3858
3859 -- If the No_Streams restriction is set, check that the type of the
3860 -- object is not, and does not contain, any subtype derived from
3861 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
3862 -- Has_Stream just for efficiency reasons. There is no point in
3863 -- spending time on a Has_Stream check if the restriction is not set.
3864
3865 if Restriction_Check_Required (No_Streams) then
3866 if Has_Stream (T) then
3867 Check_Restriction (No_Streams, N);
3868 end if;
3869 end if;
3870
3871 -- Deal with predicate check before we start to do major rewriting. It
3872 -- is OK to initialize and then check the initialized value, since the
3873 -- object goes out of scope if we get a predicate failure. Note that we
3874 -- do this in the analyzer and not the expander because the analyzer
3875 -- does some substantial rewriting in some cases.
3876
3877 -- We need a predicate check if the type has predicates, and if either
3878 -- there is an initializing expression, or for default initialization
3879 -- when we have at least one case of an explicit default initial value
3880 -- and then this is not an internal declaration whose initialization
3881 -- comes later (as for an aggregate expansion).
3882
3883 if not Suppress_Assignment_Checks (N)
3884 and then Present (Predicate_Function (T))
3885 and then not No_Initialization (N)
3886 and then
3887 (Present (E)
3888 or else
3889 Is_Partially_Initialized_Type (T, Include_Implicit => False))
3890 then
3891 -- If the type has a static predicate and the expression is known at
3892 -- compile time, see if the expression satisfies the predicate.
3893
3894 if Present (E) then
3895 Check_Expression_Against_Static_Predicate (E, T);
3896 end if;
3897
3898 Insert_After (N,
3899 Make_Predicate_Check (T, New_Occurrence_Of (Id, Loc)));
3900 end if;
3901
3902 -- Case of unconstrained type
3903
3904 if Is_Indefinite_Subtype (T) then
3905
3906 -- In SPARK, a declaration of unconstrained type is allowed
3907 -- only for constants of type string.
3908
3909 if Is_String_Type (T) and then not Constant_Present (N) then
3910 Check_SPARK_05_Restriction
3911 ("declaration of object of unconstrained type not allowed", N);
3912 end if;
3913
3914 -- Nothing to do in deferred constant case
3915
3916 if Constant_Present (N) and then No (E) then
3917 null;
3918
3919 -- Case of no initialization present
3920
3921 elsif No (E) then
3922 if No_Initialization (N) then
3923 null;
3924
3925 elsif Is_Class_Wide_Type (T) then
3926 Error_Msg_N
3927 ("initialization required in class-wide declaration ", N);
3928
3929 else
3930 Error_Msg_N
3931 ("unconstrained subtype not allowed (need initialization)",
3932 Object_Definition (N));
3933
3934 if Is_Record_Type (T) and then Has_Discriminants (T) then
3935 Error_Msg_N
3936 ("\provide initial value or explicit discriminant values",
3937 Object_Definition (N));
3938
3939 Error_Msg_NE
3940 ("\or give default discriminant values for type&",
3941 Object_Definition (N), T);
3942
3943 elsif Is_Array_Type (T) then
3944 Error_Msg_N
3945 ("\provide initial value or explicit array bounds",
3946 Object_Definition (N));
3947 end if;
3948 end if;
3949
3950 -- Case of initialization present but in error. Set initial
3951 -- expression as absent (but do not make above complaints)
3952
3953 elsif E = Error then
3954 Set_Expression (N, Empty);
3955 E := Empty;
3956
3957 -- Case of initialization present
3958
3959 else
3960 -- Check restrictions in Ada 83
3961
3962 if not Constant_Present (N) then
3963
3964 -- Unconstrained variables not allowed in Ada 83 mode
3965
3966 if Ada_Version = Ada_83
3967 and then Comes_From_Source (Object_Definition (N))
3968 then
3969 Error_Msg_N
3970 ("(Ada 83) unconstrained variable not allowed",
3971 Object_Definition (N));
3972 end if;
3973 end if;
3974
3975 -- Now we constrain the variable from the initializing expression
3976
3977 -- If the expression is an aggregate, it has been expanded into
3978 -- individual assignments. Retrieve the actual type from the
3979 -- expanded construct.
3980
3981 if Is_Array_Type (T)
3982 and then No_Initialization (N)
3983 and then Nkind (Original_Node (E)) = N_Aggregate
3984 then
3985 Act_T := Etype (E);
3986
3987 -- In case of class-wide interface object declarations we delay
3988 -- the generation of the equivalent record type declarations until
3989 -- its expansion because there are cases in they are not required.
3990
3991 elsif Is_Interface (T) then
3992 null;
3993
3994 -- In GNATprove mode, Expand_Subtype_From_Expr does nothing. Thus,
3995 -- we should prevent the generation of another Itype with the
3996 -- same name as the one already generated, or we end up with
3997 -- two identical types in GNATprove.
3998
3999 elsif GNATprove_Mode then
4000 null;
4001
4002 -- If the type is an unchecked union, no subtype can be built from
4003 -- the expression. Rewrite declaration as a renaming, which the
4004 -- back-end can handle properly. This is a rather unusual case,
4005 -- because most unchecked_union declarations have default values
4006 -- for discriminants and are thus not indefinite.
4007
4008 elsif Is_Unchecked_Union (T) then
4009 if Constant_Present (N) or else Nkind (E) = N_Function_Call then
4010 Set_Ekind (Id, E_Constant);
4011 else
4012 Set_Ekind (Id, E_Variable);
4013 end if;
4014
4015 -- An object declared within a Ghost region is automatically
4016 -- Ghost (SPARK RM 6.9(2)).
4017
4018 if Comes_From_Source (Id) and then Ghost_Mode > None then
4019 Set_Is_Ghost_Entity (Id);
4020
4021 -- The Ghost policy in effect at the point of declaration
4022 -- and at the point of completion must match
4023 -- (SPARK RM 6.9(15)).
4024
4025 if Present (Prev_Entity)
4026 and then Is_Ghost_Entity (Prev_Entity)
4027 then
4028 Check_Ghost_Completion (Prev_Entity, Id);
4029 end if;
4030 end if;
4031
4032 Rewrite (N,
4033 Make_Object_Renaming_Declaration (Loc,
4034 Defining_Identifier => Id,
4035 Subtype_Mark => New_Occurrence_Of (T, Loc),
4036 Name => E));
4037
4038 Set_Renamed_Object (Id, E);
4039 Freeze_Before (N, T);
4040 Set_Is_Frozen (Id);
4041 return;
4042
4043 else
4044 Expand_Subtype_From_Expr (N, T, Object_Definition (N), E);
4045 Act_T := Find_Type_Of_Object (Object_Definition (N), N);
4046 end if;
4047
4048 Set_Is_Constr_Subt_For_U_Nominal (Act_T);
4049
4050 if Aliased_Present (N) then
4051 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4052 end if;
4053
4054 Freeze_Before (N, Act_T);
4055 Freeze_Before (N, T);
4056 end if;
4057
4058 elsif Is_Array_Type (T)
4059 and then No_Initialization (N)
4060 and then Nkind (Original_Node (E)) = N_Aggregate
4061 then
4062 if not Is_Entity_Name (Object_Definition (N)) then
4063 Act_T := Etype (E);
4064 Check_Compile_Time_Size (Act_T);
4065
4066 if Aliased_Present (N) then
4067 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4068 end if;
4069 end if;
4070
4071 -- When the given object definition and the aggregate are specified
4072 -- independently, and their lengths might differ do a length check.
4073 -- This cannot happen if the aggregate is of the form (others =>...)
4074
4075 if not Is_Constrained (T) then
4076 null;
4077
4078 elsif Nkind (E) = N_Raise_Constraint_Error then
4079
4080 -- Aggregate is statically illegal. Place back in declaration
4081
4082 Set_Expression (N, E);
4083 Set_No_Initialization (N, False);
4084
4085 elsif T = Etype (E) then
4086 null;
4087
4088 elsif Nkind (E) = N_Aggregate
4089 and then Present (Component_Associations (E))
4090 and then Present (Choices (First (Component_Associations (E))))
4091 and then Nkind (First
4092 (Choices (First (Component_Associations (E))))) = N_Others_Choice
4093 then
4094 null;
4095
4096 else
4097 Apply_Length_Check (E, T);
4098 end if;
4099
4100 -- If the type is limited unconstrained with defaulted discriminants and
4101 -- there is no expression, then the object is constrained by the
4102 -- defaults, so it is worthwhile building the corresponding subtype.
4103
4104 elsif (Is_Limited_Record (T) or else Is_Concurrent_Type (T))
4105 and then not Is_Constrained (T)
4106 and then Has_Discriminants (T)
4107 then
4108 if No (E) then
4109 Act_T := Build_Default_Subtype (T, N);
4110 else
4111 -- Ada 2005: A limited object may be initialized by means of an
4112 -- aggregate. If the type has default discriminants it has an
4113 -- unconstrained nominal type, Its actual subtype will be obtained
4114 -- from the aggregate, and not from the default discriminants.
4115
4116 Act_T := Etype (E);
4117 end if;
4118
4119 Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
4120
4121 elsif Nkind (E) = N_Function_Call
4122 and then Constant_Present (N)
4123 and then Has_Unconstrained_Elements (Etype (E))
4124 then
4125 -- The back-end has problems with constants of a discriminated type
4126 -- with defaults, if the initial value is a function call. We
4127 -- generate an intermediate temporary that will receive a reference
4128 -- to the result of the call. The initialization expression then
4129 -- becomes a dereference of that temporary.
4130
4131 Remove_Side_Effects (E);
4132
4133 -- If this is a constant declaration of an unconstrained type and
4134 -- the initialization is an aggregate, we can use the subtype of the
4135 -- aggregate for the declared entity because it is immutable.
4136
4137 elsif not Is_Constrained (T)
4138 and then Has_Discriminants (T)
4139 and then Constant_Present (N)
4140 and then not Has_Unchecked_Union (T)
4141 and then Nkind (E) = N_Aggregate
4142 then
4143 Act_T := Etype (E);
4144 end if;
4145
4146 -- Check No_Wide_Characters restriction
4147
4148 Check_Wide_Character_Restriction (T, Object_Definition (N));
4149
4150 -- Indicate this is not set in source. Certainly true for constants, and
4151 -- true for variables so far (will be reset for a variable if and when
4152 -- we encounter a modification in the source).
4153
4154 Set_Never_Set_In_Source (Id);
4155
4156 -- Now establish the proper kind and type of the object
4157
4158 if Constant_Present (N) then
4159 Set_Ekind (Id, E_Constant);
4160 Set_Is_True_Constant (Id);
4161
4162 else
4163 Set_Ekind (Id, E_Variable);
4164
4165 -- A variable is set as shared passive if it appears in a shared
4166 -- passive package, and is at the outer level. This is not done for
4167 -- entities generated during expansion, because those are always
4168 -- manipulated locally.
4169
4170 if Is_Shared_Passive (Current_Scope)
4171 and then Is_Library_Level_Entity (Id)
4172 and then Comes_From_Source (Id)
4173 then
4174 Set_Is_Shared_Passive (Id);
4175 Check_Shared_Var (Id, T, N);
4176 end if;
4177
4178 -- Set Has_Initial_Value if initializing expression present. Note
4179 -- that if there is no initializing expression, we leave the state
4180 -- of this flag unchanged (usually it will be False, but notably in
4181 -- the case of exception choice variables, it will already be true).
4182
4183 if Present (E) then
4184 Set_Has_Initial_Value (Id);
4185 end if;
4186 end if;
4187
4188 -- Initialize alignment and size and capture alignment setting
4189
4190 Init_Alignment (Id);
4191 Init_Esize (Id);
4192 Set_Optimize_Alignment_Flags (Id);
4193
4194 -- An object declared within a Ghost region is automatically Ghost
4195 -- (SPARK RM 6.9(2)).
4196
4197 if Comes_From_Source (Id)
4198 and then (Ghost_Mode > None
4199 or else (Present (Prev_Entity)
4200 and then Is_Ghost_Entity (Prev_Entity)))
4201 then
4202 Set_Is_Ghost_Entity (Id);
4203
4204 -- The Ghost policy in effect at the point of declaration and at the
4205 -- point of completion must match (SPARK RM 6.9(16)).
4206
4207 if Present (Prev_Entity) and then Is_Ghost_Entity (Prev_Entity) then
4208 Check_Ghost_Completion (Prev_Entity, Id);
4209 end if;
4210 end if;
4211
4212 -- Deal with aliased case
4213
4214 if Aliased_Present (N) then
4215 Set_Is_Aliased (Id);
4216
4217 -- If the object is aliased and the type is unconstrained with
4218 -- defaulted discriminants and there is no expression, then the
4219 -- object is constrained by the defaults, so it is worthwhile
4220 -- building the corresponding subtype.
4221
4222 -- Ada 2005 (AI-363): If the aliased object is discriminated and
4223 -- unconstrained, then only establish an actual subtype if the
4224 -- nominal subtype is indefinite. In definite cases the object is
4225 -- unconstrained in Ada 2005.
4226
4227 if No (E)
4228 and then Is_Record_Type (T)
4229 and then not Is_Constrained (T)
4230 and then Has_Discriminants (T)
4231 and then (Ada_Version < Ada_2005 or else Is_Indefinite_Subtype (T))
4232 then
4233 Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
4234 end if;
4235 end if;
4236
4237 -- Now we can set the type of the object
4238
4239 Set_Etype (Id, Act_T);
4240
4241 -- Non-constant object is marked to be treated as volatile if type is
4242 -- volatile and we clear the Current_Value setting that may have been
4243 -- set above. Doing so for constants isn't required and might interfere
4244 -- with possible uses of the object as a static expression in contexts
4245 -- incompatible with volatility (e.g. as a case-statement alternative).
4246
4247 if Ekind (Id) /= E_Constant and then Treat_As_Volatile (Etype (Id)) then
4248 Set_Treat_As_Volatile (Id);
4249 Set_Current_Value (Id, Empty);
4250 end if;
4251
4252 -- Deal with controlled types
4253
4254 if Has_Controlled_Component (Etype (Id))
4255 or else Is_Controlled (Etype (Id))
4256 then
4257 if not Is_Library_Level_Entity (Id) then
4258 Check_Restriction (No_Nested_Finalization, N);
4259 else
4260 Validate_Controlled_Object (Id);
4261 end if;
4262 end if;
4263
4264 if Has_Task (Etype (Id)) then
4265 Check_Restriction (No_Tasking, N);
4266
4267 -- Deal with counting max tasks
4268
4269 -- Nothing to do if inside a generic
4270
4271 if Inside_A_Generic then
4272 null;
4273
4274 -- If library level entity, then count tasks
4275
4276 elsif Is_Library_Level_Entity (Id) then
4277 Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
4278
4279 -- If not library level entity, then indicate we don't know max
4280 -- tasks and also check task hierarchy restriction and blocking
4281 -- operation (since starting a task is definitely blocking).
4282
4283 else
4284 Check_Restriction (Max_Tasks, N);
4285 Check_Restriction (No_Task_Hierarchy, N);
4286 Check_Potentially_Blocking_Operation (N);
4287 end if;
4288
4289 -- A rather specialized test. If we see two tasks being declared
4290 -- of the same type in the same object declaration, and the task
4291 -- has an entry with an address clause, we know that program error
4292 -- will be raised at run time since we can't have two tasks with
4293 -- entries at the same address.
4294
4295 if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
4296 declare
4297 E : Entity_Id;
4298
4299 begin
4300 E := First_Entity (Etype (Id));
4301 while Present (E) loop
4302 if Ekind (E) = E_Entry
4303 and then Present (Get_Attribute_Definition_Clause
4304 (E, Attribute_Address))
4305 then
4306 Error_Msg_Warn := SPARK_Mode /= On;
4307 Error_Msg_N
4308 ("more than one task with same entry address<<", N);
4309 Error_Msg_N ("\Program_Error [<<", N);
4310 Insert_Action (N,
4311 Make_Raise_Program_Error (Loc,
4312 Reason => PE_Duplicated_Entry_Address));
4313 exit;
4314 end if;
4315
4316 Next_Entity (E);
4317 end loop;
4318 end;
4319 end if;
4320 end if;
4321
4322 -- Some simple constant-propagation: if the expression is a constant
4323 -- string initialized with a literal, share the literal. This avoids
4324 -- a run-time copy.
4325
4326 if Present (E)
4327 and then Is_Entity_Name (E)
4328 and then Ekind (Entity (E)) = E_Constant
4329 and then Base_Type (Etype (E)) = Standard_String
4330 then
4331 declare
4332 Val : constant Node_Id := Constant_Value (Entity (E));
4333 begin
4334 if Present (Val) and then Nkind (Val) = N_String_Literal then
4335 Rewrite (E, New_Copy (Val));
4336 end if;
4337 end;
4338 end if;
4339
4340 -- Another optimization: if the nominal subtype is unconstrained and
4341 -- the expression is a function call that returns an unconstrained
4342 -- type, rewrite the declaration as a renaming of the result of the
4343 -- call. The exceptions below are cases where the copy is expected,
4344 -- either by the back end (Aliased case) or by the semantics, as for
4345 -- initializing controlled types or copying tags for classwide types.
4346
4347 if Present (E)
4348 and then Nkind (E) = N_Explicit_Dereference
4349 and then Nkind (Original_Node (E)) = N_Function_Call
4350 and then not Is_Library_Level_Entity (Id)
4351 and then not Is_Constrained (Underlying_Type (T))
4352 and then not Is_Aliased (Id)
4353 and then not Is_Class_Wide_Type (T)
4354 and then not Is_Controlled (T)
4355 and then not Has_Controlled_Component (Base_Type (T))
4356 and then Expander_Active
4357 then
4358 Rewrite (N,
4359 Make_Object_Renaming_Declaration (Loc,
4360 Defining_Identifier => Id,
4361 Access_Definition => Empty,
4362 Subtype_Mark => New_Occurrence_Of
4363 (Base_Type (Etype (Id)), Loc),
4364 Name => E));
4365
4366 Set_Renamed_Object (Id, E);
4367
4368 -- Force generation of debugging information for the constant and for
4369 -- the renamed function call.
4370
4371 Set_Debug_Info_Needed (Id);
4372 Set_Debug_Info_Needed (Entity (Prefix (E)));
4373 end if;
4374
4375 if Present (Prev_Entity)
4376 and then Is_Frozen (Prev_Entity)
4377 and then not Error_Posted (Id)
4378 then
4379 Error_Msg_N ("full constant declaration appears too late", N);
4380 end if;
4381
4382 Check_Eliminated (Id);
4383
4384 -- Deal with setting In_Private_Part flag if in private part
4385
4386 if Ekind (Scope (Id)) = E_Package and then In_Private_Part (Scope (Id))
4387 then
4388 Set_In_Private_Part (Id);
4389 end if;
4390
4391 -- Check for violation of No_Local_Timing_Events
4392
4393 if Restriction_Check_Required (No_Local_Timing_Events)
4394 and then not Is_Library_Level_Entity (Id)
4395 and then Is_RTE (Etype (Id), RE_Timing_Event)
4396 then
4397 Check_Restriction (No_Local_Timing_Events, N);
4398 end if;
4399
4400 <<Leave>>
4401 -- Initialize the refined state of a variable here because this is a
4402 -- common destination for legal and illegal object declarations.
4403
4404 if Ekind (Id) = E_Variable then
4405 Set_Encapsulating_State (Id, Empty);
4406 end if;
4407
4408 if Has_Aspects (N) then
4409 Analyze_Aspect_Specifications (N, Id);
4410 end if;
4411
4412 Analyze_Dimension (N);
4413
4414 -- Verify whether the object declaration introduces an illegal hidden
4415 -- state within a package subject to a null abstract state.
4416
4417 if Ekind (Id) = E_Variable then
4418 Check_No_Hidden_State (Id);
4419 end if;
4420 end Analyze_Object_Declaration;
4421
4422 ---------------------------
4423 -- Analyze_Others_Choice --
4424 ---------------------------
4425
4426 -- Nothing to do for the others choice node itself, the semantic analysis
4427 -- of the others choice will occur as part of the processing of the parent
4428
4429 procedure Analyze_Others_Choice (N : Node_Id) is
4430 pragma Warnings (Off, N);
4431 begin
4432 null;
4433 end Analyze_Others_Choice;
4434
4435 -------------------------------------------
4436 -- Analyze_Private_Extension_Declaration --
4437 -------------------------------------------
4438
4439 procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
4440 T : constant Entity_Id := Defining_Identifier (N);
4441 Indic : constant Node_Id := Subtype_Indication (N);
4442 Parent_Type : Entity_Id;
4443 Parent_Base : Entity_Id;
4444
4445 begin
4446 -- The private extension declaration may be subject to pragma Ghost with
4447 -- policy Ignore. Set the mode now to ensure that any nodes generated
4448 -- during analysis and expansion are properly flagged as ignored Ghost.
4449
4450 Set_Ghost_Mode (N);
4451
4452 -- Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
4453
4454 if Is_Non_Empty_List (Interface_List (N)) then
4455 declare
4456 Intf : Node_Id;
4457 T : Entity_Id;
4458
4459 begin
4460 Intf := First (Interface_List (N));
4461 while Present (Intf) loop
4462 T := Find_Type_Of_Subtype_Indic (Intf);
4463
4464 Diagnose_Interface (Intf, T);
4465 Next (Intf);
4466 end loop;
4467 end;
4468 end if;
4469
4470 Generate_Definition (T);
4471
4472 -- For other than Ada 2012, just enter the name in the current scope
4473
4474 if Ada_Version < Ada_2012 then
4475 Enter_Name (T);
4476
4477 -- Ada 2012 (AI05-0162): Enter the name in the current scope handling
4478 -- case of private type that completes an incomplete type.
4479
4480 else
4481 declare
4482 Prev : Entity_Id;
4483
4484 begin
4485 Prev := Find_Type_Name (N);
4486
4487 pragma Assert (Prev = T
4488 or else (Ekind (Prev) = E_Incomplete_Type
4489 and then Present (Full_View (Prev))
4490 and then Full_View (Prev) = T));
4491 end;
4492 end if;
4493
4494 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
4495 Parent_Base := Base_Type (Parent_Type);
4496
4497 if Parent_Type = Any_Type or else Etype (Parent_Type) = Any_Type then
4498 Set_Ekind (T, Ekind (Parent_Type));
4499 Set_Etype (T, Any_Type);
4500 goto Leave;
4501
4502 elsif not Is_Tagged_Type (Parent_Type) then
4503 Error_Msg_N
4504 ("parent of type extension must be a tagged type ", Indic);
4505 goto Leave;
4506
4507 elsif Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
4508 Error_Msg_N ("premature derivation of incomplete type", Indic);
4509 goto Leave;
4510
4511 elsif Is_Concurrent_Type (Parent_Type) then
4512 Error_Msg_N
4513 ("parent type of a private extension cannot be "
4514 & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
4515
4516 Set_Etype (T, Any_Type);
4517 Set_Ekind (T, E_Limited_Private_Type);
4518 Set_Private_Dependents (T, New_Elmt_List);
4519 Set_Error_Posted (T);
4520 goto Leave;
4521 end if;
4522
4523 -- Perhaps the parent type should be changed to the class-wide type's
4524 -- specific type in this case to prevent cascading errors ???
4525
4526 if Is_Class_Wide_Type (Parent_Type) then
4527 Error_Msg_N
4528 ("parent of type extension must not be a class-wide type", Indic);
4529 goto Leave;
4530 end if;
4531
4532 if (not Is_Package_Or_Generic_Package (Current_Scope)
4533 and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
4534 or else In_Private_Part (Current_Scope)
4535
4536 then
4537 Error_Msg_N ("invalid context for private extension", N);
4538 end if;
4539
4540 -- Set common attributes
4541
4542 Set_Is_Pure (T, Is_Pure (Current_Scope));
4543 Set_Scope (T, Current_Scope);
4544 Set_Ekind (T, E_Record_Type_With_Private);
4545 Init_Size_Align (T);
4546 Set_Default_SSO (T);
4547
4548 Set_Etype (T, Parent_Base);
4549 Set_Has_Task (T, Has_Task (Parent_Base));
4550 Set_Has_Protected (T, Has_Task (Parent_Base));
4551
4552 Set_Convention (T, Convention (Parent_Type));
4553 Set_First_Rep_Item (T, First_Rep_Item (Parent_Type));
4554 Set_Is_First_Subtype (T);
4555 Make_Class_Wide_Type (T);
4556
4557 if Unknown_Discriminants_Present (N) then
4558 Set_Discriminant_Constraint (T, No_Elist);
4559 end if;
4560
4561 Build_Derived_Record_Type (N, Parent_Type, T);
4562
4563 -- Propagate inherited invariant information. The new type has
4564 -- invariants, if the parent type has inheritable invariants,
4565 -- and these invariants can in turn be inherited.
4566
4567 if Has_Inheritable_Invariants (Parent_Type) then
4568 Set_Has_Inheritable_Invariants (T);
4569 Set_Has_Invariants (T);
4570 end if;
4571
4572 -- Ada 2005 (AI-443): Synchronized private extension or a rewritten
4573 -- synchronized formal derived type.
4574
4575 if Ada_Version >= Ada_2005 and then Synchronized_Present (N) then
4576 Set_Is_Limited_Record (T);
4577
4578 -- Formal derived type case
4579
4580 if Is_Generic_Type (T) then
4581
4582 -- The parent must be a tagged limited type or a synchronized
4583 -- interface.
4584
4585 if (not Is_Tagged_Type (Parent_Type)
4586 or else not Is_Limited_Type (Parent_Type))
4587 and then
4588 (not Is_Interface (Parent_Type)
4589 or else not Is_Synchronized_Interface (Parent_Type))
4590 then
4591 Error_Msg_NE ("parent type of & must be tagged limited " &
4592 "or synchronized", N, T);
4593 end if;
4594
4595 -- The progenitors (if any) must be limited or synchronized
4596 -- interfaces.
4597
4598 if Present (Interfaces (T)) then
4599 declare
4600 Iface : Entity_Id;
4601 Iface_Elmt : Elmt_Id;
4602
4603 begin
4604 Iface_Elmt := First_Elmt (Interfaces (T));
4605 while Present (Iface_Elmt) loop
4606 Iface := Node (Iface_Elmt);
4607
4608 if not Is_Limited_Interface (Iface)
4609 and then not Is_Synchronized_Interface (Iface)
4610 then
4611 Error_Msg_NE ("progenitor & must be limited " &
4612 "or synchronized", N, Iface);
4613 end if;
4614
4615 Next_Elmt (Iface_Elmt);
4616 end loop;
4617 end;
4618 end if;
4619
4620 -- Regular derived extension, the parent must be a limited or
4621 -- synchronized interface.
4622
4623 else
4624 if not Is_Interface (Parent_Type)
4625 or else (not Is_Limited_Interface (Parent_Type)
4626 and then not Is_Synchronized_Interface (Parent_Type))
4627 then
4628 Error_Msg_NE
4629 ("parent type of & must be limited interface", N, T);
4630 end if;
4631 end if;
4632
4633 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
4634 -- extension with a synchronized parent must be explicitly declared
4635 -- synchronized, because the full view will be a synchronized type.
4636 -- This must be checked before the check for limited types below,
4637 -- to ensure that types declared limited are not allowed to extend
4638 -- synchronized interfaces.
4639
4640 elsif Is_Interface (Parent_Type)
4641 and then Is_Synchronized_Interface (Parent_Type)
4642 and then not Synchronized_Present (N)
4643 then
4644 Error_Msg_NE
4645 ("private extension of& must be explicitly synchronized",
4646 N, Parent_Type);
4647
4648 elsif Limited_Present (N) then
4649 Set_Is_Limited_Record (T);
4650
4651 if not Is_Limited_Type (Parent_Type)
4652 and then
4653 (not Is_Interface (Parent_Type)
4654 or else not Is_Limited_Interface (Parent_Type))
4655 then
4656 Error_Msg_NE ("parent type& of limited extension must be limited",
4657 N, Parent_Type);
4658 end if;
4659 end if;
4660
4661 <<Leave>>
4662 if Has_Aspects (N) then
4663 Analyze_Aspect_Specifications (N, T);
4664 end if;
4665 end Analyze_Private_Extension_Declaration;
4666
4667 ---------------------------------
4668 -- Analyze_Subtype_Declaration --
4669 ---------------------------------
4670
4671 procedure Analyze_Subtype_Declaration
4672 (N : Node_Id;
4673 Skip : Boolean := False)
4674 is
4675 Id : constant Entity_Id := Defining_Identifier (N);
4676 T : Entity_Id;
4677 R_Checks : Check_Result;
4678
4679 begin
4680 -- The subtype declaration may be subject to pragma Ghost with policy
4681 -- Ignore. Set the mode now to ensure that any nodes generated during
4682 -- analysis and expansion are properly flagged as ignored Ghost.
4683
4684 Set_Ghost_Mode (N);
4685
4686 Generate_Definition (Id);
4687 Set_Is_Pure (Id, Is_Pure (Current_Scope));
4688 Init_Size_Align (Id);
4689
4690 -- The following guard condition on Enter_Name is to handle cases where
4691 -- the defining identifier has already been entered into the scope but
4692 -- the declaration as a whole needs to be analyzed.
4693
4694 -- This case in particular happens for derived enumeration types. The
4695 -- derived enumeration type is processed as an inserted enumeration type
4696 -- declaration followed by a rewritten subtype declaration. The defining
4697 -- identifier, however, is entered into the name scope very early in the
4698 -- processing of the original type declaration and therefore needs to be
4699 -- avoided here, when the created subtype declaration is analyzed. (See
4700 -- Build_Derived_Types)
4701
4702 -- This also happens when the full view of a private type is derived
4703 -- type with constraints. In this case the entity has been introduced
4704 -- in the private declaration.
4705
4706 -- Finally this happens in some complex cases when validity checks are
4707 -- enabled, where the same subtype declaration may be analyzed twice.
4708 -- This can happen if the subtype is created by the pre-analysis of
4709 -- an attribute tht gives the range of a loop statement, and the loop
4710 -- itself appears within an if_statement that will be rewritten during
4711 -- expansion.
4712
4713 if Skip
4714 or else (Present (Etype (Id))
4715 and then (Is_Private_Type (Etype (Id))
4716 or else Is_Task_Type (Etype (Id))
4717 or else Is_Rewrite_Substitution (N)))
4718 then
4719 null;
4720
4721 elsif Current_Entity (Id) = Id then
4722 null;
4723
4724 else
4725 Enter_Name (Id);
4726 end if;
4727
4728 T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
4729
4730 -- Class-wide equivalent types of records with unknown discriminants
4731 -- involve the generation of an itype which serves as the private view
4732 -- of a constrained record subtype. In such cases the base type of the
4733 -- current subtype we are processing is the private itype. Use the full
4734 -- of the private itype when decorating various attributes.
4735
4736 if Is_Itype (T)
4737 and then Is_Private_Type (T)
4738 and then Present (Full_View (T))
4739 then
4740 T := Full_View (T);
4741 end if;
4742
4743 -- Inherit common attributes
4744
4745 Set_Is_Volatile (Id, Is_Volatile (T));
4746 Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
4747 Set_Is_Generic_Type (Id, Is_Generic_Type (Base_Type (T)));
4748 Set_Convention (Id, Convention (T));
4749
4750 -- If ancestor has predicates then so does the subtype, and in addition
4751 -- we must delay the freeze to properly arrange predicate inheritance.
4752
4753 -- The Ancestor_Type test is really unpleasant, there seem to be cases
4754 -- in which T = ID, so the above tests and assignments do nothing???
4755
4756 if Has_Predicates (T)
4757 or else (Present (Ancestor_Subtype (T))
4758 and then Has_Predicates (Ancestor_Subtype (T)))
4759 then
4760 Set_Has_Predicates (Id);
4761 Set_Has_Delayed_Freeze (Id);
4762 end if;
4763
4764 -- Subtype of Boolean cannot have a constraint in SPARK
4765
4766 if Is_Boolean_Type (T)
4767 and then Nkind (Subtype_Indication (N)) = N_Subtype_Indication
4768 then
4769 Check_SPARK_05_Restriction
4770 ("subtype of Boolean cannot have constraint", N);
4771 end if;
4772
4773 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
4774 declare
4775 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
4776 One_Cstr : Node_Id;
4777 Low : Node_Id;
4778 High : Node_Id;
4779
4780 begin
4781 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint then
4782 One_Cstr := First (Constraints (Cstr));
4783 while Present (One_Cstr) loop
4784
4785 -- Index or discriminant constraint in SPARK must be a
4786 -- subtype mark.
4787
4788 if not
4789 Nkind_In (One_Cstr, N_Identifier, N_Expanded_Name)
4790 then
4791 Check_SPARK_05_Restriction
4792 ("subtype mark required", One_Cstr);
4793
4794 -- String subtype must have a lower bound of 1 in SPARK.
4795 -- Note that we do not need to test for the non-static case
4796 -- here, since that was already taken care of in
4797 -- Process_Range_Expr_In_Decl.
4798
4799 elsif Base_Type (T) = Standard_String then
4800 Get_Index_Bounds (One_Cstr, Low, High);
4801
4802 if Is_OK_Static_Expression (Low)
4803 and then Expr_Value (Low) /= 1
4804 then
4805 Check_SPARK_05_Restriction
4806 ("String subtype must have lower bound of 1", N);
4807 end if;
4808 end if;
4809
4810 Next (One_Cstr);
4811 end loop;
4812 end if;
4813 end;
4814 end if;
4815
4816 -- In the case where there is no constraint given in the subtype
4817 -- indication, Process_Subtype just returns the Subtype_Mark, so its
4818 -- semantic attributes must be established here.
4819
4820 if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
4821 Set_Etype (Id, Base_Type (T));
4822
4823 -- Subtype of unconstrained array without constraint is not allowed
4824 -- in SPARK.
4825
4826 if Is_Array_Type (T) and then not Is_Constrained (T) then
4827 Check_SPARK_05_Restriction
4828 ("subtype of unconstrained array must have constraint", N);
4829 end if;
4830
4831 case Ekind (T) is
4832 when Array_Kind =>
4833 Set_Ekind (Id, E_Array_Subtype);
4834 Copy_Array_Subtype_Attributes (Id, T);
4835
4836 when Decimal_Fixed_Point_Kind =>
4837 Set_Ekind (Id, E_Decimal_Fixed_Point_Subtype);
4838 Set_Digits_Value (Id, Digits_Value (T));
4839 Set_Delta_Value (Id, Delta_Value (T));
4840 Set_Scale_Value (Id, Scale_Value (T));
4841 Set_Small_Value (Id, Small_Value (T));
4842 Set_Scalar_Range (Id, Scalar_Range (T));
4843 Set_Machine_Radix_10 (Id, Machine_Radix_10 (T));
4844 Set_Is_Constrained (Id, Is_Constrained (T));
4845 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4846 Set_RM_Size (Id, RM_Size (T));
4847
4848 when Enumeration_Kind =>
4849 Set_Ekind (Id, E_Enumeration_Subtype);
4850 Set_First_Literal (Id, First_Literal (Base_Type (T)));
4851 Set_Scalar_Range (Id, Scalar_Range (T));
4852 Set_Is_Character_Type (Id, Is_Character_Type (T));
4853 Set_Is_Constrained (Id, Is_Constrained (T));
4854 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4855 Set_RM_Size (Id, RM_Size (T));
4856 Inherit_Predicate_Flags (Id, T);
4857
4858 when Ordinary_Fixed_Point_Kind =>
4859 Set_Ekind (Id, E_Ordinary_Fixed_Point_Subtype);
4860 Set_Scalar_Range (Id, Scalar_Range (T));
4861 Set_Small_Value (Id, Small_Value (T));
4862 Set_Delta_Value (Id, Delta_Value (T));
4863 Set_Is_Constrained (Id, Is_Constrained (T));
4864 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4865 Set_RM_Size (Id, RM_Size (T));
4866
4867 when Float_Kind =>
4868 Set_Ekind (Id, E_Floating_Point_Subtype);
4869 Set_Scalar_Range (Id, Scalar_Range (T));
4870 Set_Digits_Value (Id, Digits_Value (T));
4871 Set_Is_Constrained (Id, Is_Constrained (T));
4872
4873 when Signed_Integer_Kind =>
4874 Set_Ekind (Id, E_Signed_Integer_Subtype);
4875 Set_Scalar_Range (Id, Scalar_Range (T));
4876 Set_Is_Constrained (Id, Is_Constrained (T));
4877 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4878 Set_RM_Size (Id, RM_Size (T));
4879 Inherit_Predicate_Flags (Id, T);
4880
4881 when Modular_Integer_Kind =>
4882 Set_Ekind (Id, E_Modular_Integer_Subtype);
4883 Set_Scalar_Range (Id, Scalar_Range (T));
4884 Set_Is_Constrained (Id, Is_Constrained (T));
4885 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4886 Set_RM_Size (Id, RM_Size (T));
4887 Inherit_Predicate_Flags (Id, T);
4888
4889 when Class_Wide_Kind =>
4890 Set_Ekind (Id, E_Class_Wide_Subtype);
4891 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
4892 Set_Cloned_Subtype (Id, T);
4893 Set_Is_Tagged_Type (Id, True);
4894 Set_Has_Unknown_Discriminants
4895 (Id, True);
4896 Set_No_Tagged_Streams_Pragma
4897 (Id, No_Tagged_Streams_Pragma (T));
4898
4899 if Ekind (T) = E_Class_Wide_Subtype then
4900 Set_Equivalent_Type (Id, Equivalent_Type (T));
4901 end if;
4902
4903 when E_Record_Type | E_Record_Subtype =>
4904 Set_Ekind (Id, E_Record_Subtype);
4905
4906 if Ekind (T) = E_Record_Subtype
4907 and then Present (Cloned_Subtype (T))
4908 then
4909 Set_Cloned_Subtype (Id, Cloned_Subtype (T));
4910 else
4911 Set_Cloned_Subtype (Id, T);
4912 end if;
4913
4914 Set_First_Entity (Id, First_Entity (T));
4915 Set_Last_Entity (Id, Last_Entity (T));
4916 Set_Has_Discriminants (Id, Has_Discriminants (T));
4917 Set_Is_Constrained (Id, Is_Constrained (T));
4918 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
4919 Set_Has_Implicit_Dereference
4920 (Id, Has_Implicit_Dereference (T));
4921 Set_Has_Unknown_Discriminants
4922 (Id, Has_Unknown_Discriminants (T));
4923
4924 if Has_Discriminants (T) then
4925 Set_Discriminant_Constraint
4926 (Id, Discriminant_Constraint (T));
4927 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
4928
4929 elsif Has_Unknown_Discriminants (Id) then
4930 Set_Discriminant_Constraint (Id, No_Elist);
4931 end if;
4932
4933 if Is_Tagged_Type (T) then
4934 Set_Is_Tagged_Type (Id, True);
4935 Set_No_Tagged_Streams_Pragma
4936 (Id, No_Tagged_Streams_Pragma (T));
4937 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
4938 Set_Direct_Primitive_Operations
4939 (Id, Direct_Primitive_Operations (T));
4940 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
4941
4942 if Is_Interface (T) then
4943 Set_Is_Interface (Id);
4944 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
4945 end if;
4946 end if;
4947
4948 when Private_Kind =>
4949 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
4950 Set_Has_Discriminants (Id, Has_Discriminants (T));
4951 Set_Is_Constrained (Id, Is_Constrained (T));
4952 Set_First_Entity (Id, First_Entity (T));
4953 Set_Last_Entity (Id, Last_Entity (T));
4954 Set_Private_Dependents (Id, New_Elmt_List);
4955 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
4956 Set_Has_Implicit_Dereference
4957 (Id, Has_Implicit_Dereference (T));
4958 Set_Has_Unknown_Discriminants
4959 (Id, Has_Unknown_Discriminants (T));
4960 Set_Known_To_Have_Preelab_Init
4961 (Id, Known_To_Have_Preelab_Init (T));
4962
4963 if Is_Tagged_Type (T) then
4964 Set_Is_Tagged_Type (Id);
4965 Set_No_Tagged_Streams_Pragma (Id,
4966 No_Tagged_Streams_Pragma (T));
4967 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
4968 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
4969 Set_Direct_Primitive_Operations (Id,
4970 Direct_Primitive_Operations (T));
4971 end if;
4972
4973 -- In general the attributes of the subtype of a private type
4974 -- are the attributes of the partial view of parent. However,
4975 -- the full view may be a discriminated type, and the subtype
4976 -- must share the discriminant constraint to generate correct
4977 -- calls to initialization procedures.
4978
4979 if Has_Discriminants (T) then
4980 Set_Discriminant_Constraint
4981 (Id, Discriminant_Constraint (T));
4982 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
4983
4984 elsif Present (Full_View (T))
4985 and then Has_Discriminants (Full_View (T))
4986 then
4987 Set_Discriminant_Constraint
4988 (Id, Discriminant_Constraint (Full_View (T)));
4989 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
4990
4991 -- This would seem semantically correct, but apparently
4992 -- generates spurious errors about missing components ???
4993
4994 -- Set_Has_Discriminants (Id);
4995 end if;
4996
4997 Prepare_Private_Subtype_Completion (Id, N);
4998
4999 -- If this is the subtype of a constrained private type with
5000 -- discriminants that has got a full view and we also have
5001 -- built a completion just above, show that the completion
5002 -- is a clone of the full view to the back-end.
5003
5004 if Has_Discriminants (T)
5005 and then not Has_Unknown_Discriminants (T)
5006 and then not Is_Empty_Elmt_List (Discriminant_Constraint (T))
5007 and then Present (Full_View (T))
5008 and then Present (Full_View (Id))
5009 then
5010 Set_Cloned_Subtype (Full_View (Id), Full_View (T));
5011 end if;
5012
5013 when Access_Kind =>
5014 Set_Ekind (Id, E_Access_Subtype);
5015 Set_Is_Constrained (Id, Is_Constrained (T));
5016 Set_Is_Access_Constant
5017 (Id, Is_Access_Constant (T));
5018 Set_Directly_Designated_Type
5019 (Id, Designated_Type (T));
5020 Set_Can_Never_Be_Null (Id, Can_Never_Be_Null (T));
5021
5022 -- A Pure library_item must not contain the declaration of a
5023 -- named access type, except within a subprogram, generic
5024 -- subprogram, task unit, or protected unit, or if it has
5025 -- a specified Storage_Size of zero (RM05-10.2.1(15.4-15.5)).
5026
5027 if Comes_From_Source (Id)
5028 and then In_Pure_Unit
5029 and then not In_Subprogram_Task_Protected_Unit
5030 and then not No_Pool_Assigned (Id)
5031 then
5032 Error_Msg_N
5033 ("named access types not allowed in pure unit", N);
5034 end if;
5035
5036 when Concurrent_Kind =>
5037 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
5038 Set_Corresponding_Record_Type (Id,
5039 Corresponding_Record_Type (T));
5040 Set_First_Entity (Id, First_Entity (T));
5041 Set_First_Private_Entity (Id, First_Private_Entity (T));
5042 Set_Has_Discriminants (Id, Has_Discriminants (T));
5043 Set_Is_Constrained (Id, Is_Constrained (T));
5044 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5045 Set_Last_Entity (Id, Last_Entity (T));
5046
5047 if Is_Tagged_Type (T) then
5048 Set_No_Tagged_Streams_Pragma
5049 (Id, No_Tagged_Streams_Pragma (T));
5050 end if;
5051
5052 if Has_Discriminants (T) then
5053 Set_Discriminant_Constraint
5054 (Id, Discriminant_Constraint (T));
5055 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5056 end if;
5057
5058 when Incomplete_Kind =>
5059 if Ada_Version >= Ada_2005 then
5060
5061 -- In Ada 2005 an incomplete type can be explicitly tagged:
5062 -- propagate indication. Note that we also have to include
5063 -- subtypes for Ada 2012 extended use of incomplete types.
5064
5065 Set_Ekind (Id, E_Incomplete_Subtype);
5066 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5067 Set_Private_Dependents (Id, New_Elmt_List);
5068
5069 if Is_Tagged_Type (Id) then
5070 Set_No_Tagged_Streams_Pragma
5071 (Id, No_Tagged_Streams_Pragma (T));
5072 Set_Direct_Primitive_Operations (Id, New_Elmt_List);
5073 end if;
5074
5075 -- Ada 2005 (AI-412): Decorate an incomplete subtype of an
5076 -- incomplete type visible through a limited with clause.
5077
5078 if From_Limited_With (T)
5079 and then Present (Non_Limited_View (T))
5080 then
5081 Set_From_Limited_With (Id);
5082 Set_Non_Limited_View (Id, Non_Limited_View (T));
5083
5084 -- Ada 2005 (AI-412): Add the regular incomplete subtype
5085 -- to the private dependents of the original incomplete
5086 -- type for future transformation.
5087
5088 else
5089 Append_Elmt (Id, Private_Dependents (T));
5090 end if;
5091
5092 -- If the subtype name denotes an incomplete type an error
5093 -- was already reported by Process_Subtype.
5094
5095 else
5096 Set_Etype (Id, Any_Type);
5097 end if;
5098
5099 when others =>
5100 raise Program_Error;
5101 end case;
5102 end if;
5103
5104 if Etype (Id) = Any_Type then
5105 goto Leave;
5106 end if;
5107
5108 -- Some common processing on all types
5109
5110 Set_Size_Info (Id, T);
5111 Set_First_Rep_Item (Id, First_Rep_Item (T));
5112
5113 -- If the parent type is a generic actual, so is the subtype. This may
5114 -- happen in a nested instance. Why Comes_From_Source test???
5115
5116 if not Comes_From_Source (N) then
5117 Set_Is_Generic_Actual_Type (Id, Is_Generic_Actual_Type (T));
5118 end if;
5119
5120 T := Etype (Id);
5121
5122 Set_Is_Immediately_Visible (Id, True);
5123 Set_Depends_On_Private (Id, Has_Private_Component (T));
5124 Set_Is_Descendent_Of_Address (Id, Is_Descendent_Of_Address (T));
5125
5126 if Is_Interface (T) then
5127 Set_Is_Interface (Id);
5128 end if;
5129
5130 if Present (Generic_Parent_Type (N))
5131 and then
5132 (Nkind (Parent (Generic_Parent_Type (N))) /=
5133 N_Formal_Type_Declaration
5134 or else Nkind (Formal_Type_Definition
5135 (Parent (Generic_Parent_Type (N)))) /=
5136 N_Formal_Private_Type_Definition)
5137 then
5138 if Is_Tagged_Type (Id) then
5139
5140 -- If this is a generic actual subtype for a synchronized type,
5141 -- the primitive operations are those of the corresponding record
5142 -- for which there is a separate subtype declaration.
5143
5144 if Is_Concurrent_Type (Id) then
5145 null;
5146 elsif Is_Class_Wide_Type (Id) then
5147 Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
5148 else
5149 Derive_Subprograms (Generic_Parent_Type (N), Id, T);
5150 end if;
5151
5152 elsif Scope (Etype (Id)) /= Standard_Standard then
5153 Derive_Subprograms (Generic_Parent_Type (N), Id);
5154 end if;
5155 end if;
5156
5157 if Is_Private_Type (T) and then Present (Full_View (T)) then
5158 Conditional_Delay (Id, Full_View (T));
5159
5160 -- The subtypes of components or subcomponents of protected types
5161 -- do not need freeze nodes, which would otherwise appear in the
5162 -- wrong scope (before the freeze node for the protected type). The
5163 -- proper subtypes are those of the subcomponents of the corresponding
5164 -- record.
5165
5166 elsif Ekind (Scope (Id)) /= E_Protected_Type
5167 and then Present (Scope (Scope (Id))) -- error defense
5168 and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
5169 then
5170 Conditional_Delay (Id, T);
5171 end if;
5172
5173 -- Check that Constraint_Error is raised for a scalar subtype indication
5174 -- when the lower or upper bound of a non-null range lies outside the
5175 -- range of the type mark.
5176
5177 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5178 if Is_Scalar_Type (Etype (Id))
5179 and then Scalar_Range (Id) /=
5180 Scalar_Range (Etype (Subtype_Mark
5181 (Subtype_Indication (N))))
5182 then
5183 Apply_Range_Check
5184 (Scalar_Range (Id),
5185 Etype (Subtype_Mark (Subtype_Indication (N))));
5186
5187 -- In the array case, check compatibility for each index
5188
5189 elsif Is_Array_Type (Etype (Id)) and then Present (First_Index (Id))
5190 then
5191 -- This really should be a subprogram that finds the indications
5192 -- to check???
5193
5194 declare
5195 Subt_Index : Node_Id := First_Index (Id);
5196 Target_Index : Node_Id :=
5197 First_Index (Etype
5198 (Subtype_Mark (Subtype_Indication (N))));
5199 Has_Dyn_Chk : Boolean := Has_Dynamic_Range_Check (N);
5200
5201 begin
5202 while Present (Subt_Index) loop
5203 if ((Nkind (Subt_Index) = N_Identifier
5204 and then Ekind (Entity (Subt_Index)) in Scalar_Kind)
5205 or else Nkind (Subt_Index) = N_Subtype_Indication)
5206 and then
5207 Nkind (Scalar_Range (Etype (Subt_Index))) = N_Range
5208 then
5209 declare
5210 Target_Typ : constant Entity_Id :=
5211 Etype (Target_Index);
5212 begin
5213 R_Checks :=
5214 Get_Range_Checks
5215 (Scalar_Range (Etype (Subt_Index)),
5216 Target_Typ,
5217 Etype (Subt_Index),
5218 Defining_Identifier (N));
5219
5220 -- Reset Has_Dynamic_Range_Check on the subtype to
5221 -- prevent elision of the index check due to a dynamic
5222 -- check generated for a preceding index (needed since
5223 -- Insert_Range_Checks tries to avoid generating
5224 -- redundant checks on a given declaration).
5225
5226 Set_Has_Dynamic_Range_Check (N, False);
5227
5228 Insert_Range_Checks
5229 (R_Checks,
5230 N,
5231 Target_Typ,
5232 Sloc (Defining_Identifier (N)));
5233
5234 -- Record whether this index involved a dynamic check
5235
5236 Has_Dyn_Chk :=
5237 Has_Dyn_Chk or else Has_Dynamic_Range_Check (N);
5238 end;
5239 end if;
5240
5241 Next_Index (Subt_Index);
5242 Next_Index (Target_Index);
5243 end loop;
5244
5245 -- Finally, mark whether the subtype involves dynamic checks
5246
5247 Set_Has_Dynamic_Range_Check (N, Has_Dyn_Chk);
5248 end;
5249 end if;
5250 end if;
5251
5252 -- A type invariant applies to any subtype in its scope, in particular
5253 -- to a generic actual.
5254
5255 if Has_Invariants (T) and then In_Open_Scopes (Scope (T)) then
5256 Set_Has_Invariants (Id);
5257 Set_Invariant_Procedure (Id, Invariant_Procedure (T));
5258 end if;
5259
5260 -- Make sure that generic actual types are properly frozen. The subtype
5261 -- is marked as a generic actual type when the enclosing instance is
5262 -- analyzed, so here we identify the subtype from the tree structure.
5263
5264 if Expander_Active
5265 and then Is_Generic_Actual_Type (Id)
5266 and then In_Instance
5267 and then not Comes_From_Source (N)
5268 and then Nkind (Subtype_Indication (N)) /= N_Subtype_Indication
5269 and then Is_Frozen (T)
5270 then
5271 Freeze_Before (N, Id);
5272 end if;
5273
5274 Set_Optimize_Alignment_Flags (Id);
5275 Check_Eliminated (Id);
5276
5277 <<Leave>>
5278 if Has_Aspects (N) then
5279 Analyze_Aspect_Specifications (N, Id);
5280 end if;
5281
5282 Analyze_Dimension (N);
5283 end Analyze_Subtype_Declaration;
5284
5285 --------------------------------
5286 -- Analyze_Subtype_Indication --
5287 --------------------------------
5288
5289 procedure Analyze_Subtype_Indication (N : Node_Id) is
5290 T : constant Entity_Id := Subtype_Mark (N);
5291 R : constant Node_Id := Range_Expression (Constraint (N));
5292
5293 begin
5294 Analyze (T);
5295
5296 if R /= Error then
5297 Analyze (R);
5298 Set_Etype (N, Etype (R));
5299 Resolve (R, Entity (T));
5300 else
5301 Set_Error_Posted (R);
5302 Set_Error_Posted (T);
5303 end if;
5304 end Analyze_Subtype_Indication;
5305
5306 --------------------------
5307 -- Analyze_Variant_Part --
5308 --------------------------
5309
5310 procedure Analyze_Variant_Part (N : Node_Id) is
5311 Discr_Name : Node_Id;
5312 Discr_Type : Entity_Id;
5313
5314 procedure Process_Variant (A : Node_Id);
5315 -- Analyze declarations for a single variant
5316
5317 package Analyze_Variant_Choices is
5318 new Generic_Analyze_Choices (Process_Variant);
5319 use Analyze_Variant_Choices;
5320
5321 ---------------------
5322 -- Process_Variant --
5323 ---------------------
5324
5325 procedure Process_Variant (A : Node_Id) is
5326 CL : constant Node_Id := Component_List (A);
5327 begin
5328 if not Null_Present (CL) then
5329 Analyze_Declarations (Component_Items (CL));
5330
5331 if Present (Variant_Part (CL)) then
5332 Analyze (Variant_Part (CL));
5333 end if;
5334 end if;
5335 end Process_Variant;
5336
5337 -- Start of processing for Analyze_Variant_Part
5338
5339 begin
5340 Discr_Name := Name (N);
5341 Analyze (Discr_Name);
5342
5343 -- If Discr_Name bad, get out (prevent cascaded errors)
5344
5345 if Etype (Discr_Name) = Any_Type then
5346 return;
5347 end if;
5348
5349 -- Check invalid discriminant in variant part
5350
5351 if Ekind (Entity (Discr_Name)) /= E_Discriminant then
5352 Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
5353 end if;
5354
5355 Discr_Type := Etype (Entity (Discr_Name));
5356
5357 if not Is_Discrete_Type (Discr_Type) then
5358 Error_Msg_N
5359 ("discriminant in a variant part must be of a discrete type",
5360 Name (N));
5361 return;
5362 end if;
5363
5364 -- Now analyze the choices, which also analyzes the declarations that
5365 -- are associated with each choice.
5366
5367 Analyze_Choices (Variants (N), Discr_Type);
5368
5369 -- Note: we used to instantiate and call Check_Choices here to check
5370 -- that the choices covered the discriminant, but it's too early to do
5371 -- that because of statically predicated subtypes, whose analysis may
5372 -- be deferred to their freeze point which may be as late as the freeze
5373 -- point of the containing record. So this call is now to be found in
5374 -- Freeze_Record_Declaration.
5375
5376 end Analyze_Variant_Part;
5377
5378 ----------------------------
5379 -- Array_Type_Declaration --
5380 ----------------------------
5381
5382 procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
5383 Component_Def : constant Node_Id := Component_Definition (Def);
5384 Component_Typ : constant Node_Id := Subtype_Indication (Component_Def);
5385 Element_Type : Entity_Id;
5386 Implicit_Base : Entity_Id;
5387 Index : Node_Id;
5388 Related_Id : Entity_Id := Empty;
5389 Nb_Index : Nat;
5390 P : constant Node_Id := Parent (Def);
5391 Priv : Entity_Id;
5392
5393 begin
5394 if Nkind (Def) = N_Constrained_Array_Definition then
5395 Index := First (Discrete_Subtype_Definitions (Def));
5396 else
5397 Index := First (Subtype_Marks (Def));
5398 end if;
5399
5400 -- Find proper names for the implicit types which may be public. In case
5401 -- of anonymous arrays we use the name of the first object of that type
5402 -- as prefix.
5403
5404 if No (T) then
5405 Related_Id := Defining_Identifier (P);
5406 else
5407 Related_Id := T;
5408 end if;
5409
5410 Nb_Index := 1;
5411 while Present (Index) loop
5412 Analyze (Index);
5413
5414 -- Test for odd case of trying to index a type by the type itself
5415
5416 if Is_Entity_Name (Index) and then Entity (Index) = T then
5417 Error_Msg_N ("type& cannot be indexed by itself", Index);
5418 Set_Entity (Index, Standard_Boolean);
5419 Set_Etype (Index, Standard_Boolean);
5420 end if;
5421
5422 -- Check SPARK restriction requiring a subtype mark
5423
5424 if not Nkind_In (Index, N_Identifier, N_Expanded_Name) then
5425 Check_SPARK_05_Restriction ("subtype mark required", Index);
5426 end if;
5427
5428 -- Add a subtype declaration for each index of private array type
5429 -- declaration whose etype is also private. For example:
5430
5431 -- package Pkg is
5432 -- type Index is private;
5433 -- private
5434 -- type Table is array (Index) of ...
5435 -- end;
5436
5437 -- This is currently required by the expander for the internally
5438 -- generated equality subprogram of records with variant parts in
5439 -- which the etype of some component is such private type.
5440
5441 if Ekind (Current_Scope) = E_Package
5442 and then In_Private_Part (Current_Scope)
5443 and then Has_Private_Declaration (Etype (Index))
5444 then
5445 declare
5446 Loc : constant Source_Ptr := Sloc (Def);
5447 New_E : Entity_Id;
5448 Decl : Entity_Id;
5449
5450 begin
5451 New_E := Make_Temporary (Loc, 'T');
5452 Set_Is_Internal (New_E);
5453
5454 Decl :=
5455 Make_Subtype_Declaration (Loc,
5456 Defining_Identifier => New_E,
5457 Subtype_Indication =>
5458 New_Occurrence_Of (Etype (Index), Loc));
5459
5460 Insert_Before (Parent (Def), Decl);
5461 Analyze (Decl);
5462 Set_Etype (Index, New_E);
5463
5464 -- If the index is a range the Entity attribute is not
5465 -- available. Example:
5466
5467 -- package Pkg is
5468 -- type T is private;
5469 -- private
5470 -- type T is new Natural;
5471 -- Table : array (T(1) .. T(10)) of Boolean;
5472 -- end Pkg;
5473
5474 if Nkind (Index) /= N_Range then
5475 Set_Entity (Index, New_E);
5476 end if;
5477 end;
5478 end if;
5479
5480 Make_Index (Index, P, Related_Id, Nb_Index);
5481
5482 -- Check error of subtype with predicate for index type
5483
5484 Bad_Predicated_Subtype_Use
5485 ("subtype& has predicate, not allowed as index subtype",
5486 Index, Etype (Index));
5487
5488 -- Move to next index
5489
5490 Next_Index (Index);
5491 Nb_Index := Nb_Index + 1;
5492 end loop;
5493
5494 -- Process subtype indication if one is present
5495
5496 if Present (Component_Typ) then
5497 Element_Type := Process_Subtype (Component_Typ, P, Related_Id, 'C');
5498
5499 Set_Etype (Component_Typ, Element_Type);
5500
5501 if not Nkind_In (Component_Typ, N_Identifier, N_Expanded_Name) then
5502 Check_SPARK_05_Restriction
5503 ("subtype mark required", Component_Typ);
5504 end if;
5505
5506 -- Ada 2005 (AI-230): Access Definition case
5507
5508 else pragma Assert (Present (Access_Definition (Component_Def)));
5509
5510 -- Indicate that the anonymous access type is created by the
5511 -- array type declaration.
5512
5513 Element_Type := Access_Definition
5514 (Related_Nod => P,
5515 N => Access_Definition (Component_Def));
5516 Set_Is_Local_Anonymous_Access (Element_Type);
5517
5518 -- Propagate the parent. This field is needed if we have to generate
5519 -- the master_id associated with an anonymous access to task type
5520 -- component (see Expand_N_Full_Type_Declaration.Build_Master)
5521
5522 Set_Parent (Element_Type, Parent (T));
5523
5524 -- Ada 2005 (AI-230): In case of components that are anonymous access
5525 -- types the level of accessibility depends on the enclosing type
5526 -- declaration
5527
5528 Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
5529
5530 -- Ada 2005 (AI-254)
5531
5532 declare
5533 CD : constant Node_Id :=
5534 Access_To_Subprogram_Definition
5535 (Access_Definition (Component_Def));
5536 begin
5537 if Present (CD) and then Protected_Present (CD) then
5538 Element_Type :=
5539 Replace_Anonymous_Access_To_Protected_Subprogram (Def);
5540 end if;
5541 end;
5542 end if;
5543
5544 -- Constrained array case
5545
5546 if No (T) then
5547 T := Create_Itype (E_Void, P, Related_Id, 'T');
5548 end if;
5549
5550 if Nkind (Def) = N_Constrained_Array_Definition then
5551
5552 -- Establish Implicit_Base as unconstrained base type
5553
5554 Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
5555
5556 Set_Etype (Implicit_Base, Implicit_Base);
5557 Set_Scope (Implicit_Base, Current_Scope);
5558 Set_Has_Delayed_Freeze (Implicit_Base);
5559 Set_Default_SSO (Implicit_Base);
5560
5561 -- The constrained array type is a subtype of the unconstrained one
5562
5563 Set_Ekind (T, E_Array_Subtype);
5564 Init_Size_Align (T);
5565 Set_Etype (T, Implicit_Base);
5566 Set_Scope (T, Current_Scope);
5567 Set_Is_Constrained (T);
5568 Set_First_Index (T,
5569 First (Discrete_Subtype_Definitions (Def)));
5570 Set_Has_Delayed_Freeze (T);
5571
5572 -- Complete setup of implicit base type
5573
5574 Set_First_Index (Implicit_Base, First_Index (T));
5575 Set_Component_Type (Implicit_Base, Element_Type);
5576 Set_Has_Task (Implicit_Base, Has_Task (Element_Type));
5577 Set_Has_Protected (Implicit_Base, Has_Protected (Element_Type));
5578 Set_Component_Size (Implicit_Base, Uint_0);
5579 Set_Packed_Array_Impl_Type (Implicit_Base, Empty);
5580 Set_Has_Controlled_Component (Implicit_Base,
5581 Has_Controlled_Component (Element_Type)
5582 or else Is_Controlled (Element_Type));
5583 Set_Finalize_Storage_Only (Implicit_Base,
5584 Finalize_Storage_Only (Element_Type));
5585
5586 -- Inherit the "ghostness" from the constrained array type
5587
5588 if Is_Ghost_Entity (T) or else Ghost_Mode > None then
5589 Set_Is_Ghost_Entity (Implicit_Base);
5590 end if;
5591
5592 -- Unconstrained array case
5593
5594 else
5595 Set_Ekind (T, E_Array_Type);
5596 Init_Size_Align (T);
5597 Set_Etype (T, T);
5598 Set_Scope (T, Current_Scope);
5599 Set_Component_Size (T, Uint_0);
5600 Set_Is_Constrained (T, False);
5601 Set_First_Index (T, First (Subtype_Marks (Def)));
5602 Set_Has_Delayed_Freeze (T, True);
5603 Set_Has_Task (T, Has_Task (Element_Type));
5604 Set_Has_Protected (T, Has_Protected (Element_Type));
5605 Set_Has_Controlled_Component (T, Has_Controlled_Component
5606 (Element_Type)
5607 or else
5608 Is_Controlled (Element_Type));
5609 Set_Finalize_Storage_Only (T, Finalize_Storage_Only
5610 (Element_Type));
5611 Set_Default_SSO (T);
5612 end if;
5613
5614 -- Common attributes for both cases
5615
5616 Set_Component_Type (Base_Type (T), Element_Type);
5617 Set_Packed_Array_Impl_Type (T, Empty);
5618
5619 if Aliased_Present (Component_Definition (Def)) then
5620 Check_SPARK_05_Restriction
5621 ("aliased is not allowed", Component_Definition (Def));
5622 Set_Has_Aliased_Components (Etype (T));
5623 end if;
5624
5625 -- Ada 2005 (AI-231): Propagate the null-excluding attribute to the
5626 -- array type to ensure that objects of this type are initialized.
5627
5628 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (Element_Type) then
5629 Set_Can_Never_Be_Null (T);
5630
5631 if Null_Exclusion_Present (Component_Definition (Def))
5632
5633 -- No need to check itypes because in their case this check was
5634 -- done at their point of creation
5635
5636 and then not Is_Itype (Element_Type)
5637 then
5638 Error_Msg_N
5639 ("`NOT NULL` not allowed (null already excluded)",
5640 Subtype_Indication (Component_Definition (Def)));
5641 end if;
5642 end if;
5643
5644 Priv := Private_Component (Element_Type);
5645
5646 if Present (Priv) then
5647
5648 -- Check for circular definitions
5649
5650 if Priv = Any_Type then
5651 Set_Component_Type (Etype (T), Any_Type);
5652
5653 -- There is a gap in the visibility of operations on the composite
5654 -- type only if the component type is defined in a different scope.
5655
5656 elsif Scope (Priv) = Current_Scope then
5657 null;
5658
5659 elsif Is_Limited_Type (Priv) then
5660 Set_Is_Limited_Composite (Etype (T));
5661 Set_Is_Limited_Composite (T);
5662 else
5663 Set_Is_Private_Composite (Etype (T));
5664 Set_Is_Private_Composite (T);
5665 end if;
5666 end if;
5667
5668 -- A syntax error in the declaration itself may lead to an empty index
5669 -- list, in which case do a minimal patch.
5670
5671 if No (First_Index (T)) then
5672 Error_Msg_N ("missing index definition in array type declaration", T);
5673
5674 declare
5675 Indexes : constant List_Id :=
5676 New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
5677 begin
5678 Set_Discrete_Subtype_Definitions (Def, Indexes);
5679 Set_First_Index (T, First (Indexes));
5680 return;
5681 end;
5682 end if;
5683
5684 -- Create a concatenation operator for the new type. Internal array
5685 -- types created for packed entities do not need such, they are
5686 -- compatible with the user-defined type.
5687
5688 if Number_Dimensions (T) = 1
5689 and then not Is_Packed_Array_Impl_Type (T)
5690 then
5691 New_Concatenation_Op (T);
5692 end if;
5693
5694 -- In the case of an unconstrained array the parser has already verified
5695 -- that all the indexes are unconstrained but we still need to make sure
5696 -- that the element type is constrained.
5697
5698 if Is_Indefinite_Subtype (Element_Type) then
5699 Error_Msg_N
5700 ("unconstrained element type in array declaration",
5701 Subtype_Indication (Component_Def));
5702
5703 elsif Is_Abstract_Type (Element_Type) then
5704 Error_Msg_N
5705 ("the type of a component cannot be abstract",
5706 Subtype_Indication (Component_Def));
5707 end if;
5708
5709 -- There may be an invariant declared for the component type, but
5710 -- the construction of the component invariant checking procedure
5711 -- takes place during expansion.
5712 end Array_Type_Declaration;
5713
5714 ------------------------------------------------------
5715 -- Replace_Anonymous_Access_To_Protected_Subprogram --
5716 ------------------------------------------------------
5717
5718 function Replace_Anonymous_Access_To_Protected_Subprogram
5719 (N : Node_Id) return Entity_Id
5720 is
5721 Loc : constant Source_Ptr := Sloc (N);
5722
5723 Curr_Scope : constant Scope_Stack_Entry :=
5724 Scope_Stack.Table (Scope_Stack.Last);
5725
5726 Anon : constant Entity_Id := Make_Temporary (Loc, 'S');
5727
5728 Acc : Node_Id;
5729 -- Access definition in declaration
5730
5731 Comp : Node_Id;
5732 -- Object definition or formal definition with an access definition
5733
5734 Decl : Node_Id;
5735 -- Declaration of anonymous access to subprogram type
5736
5737 Spec : Node_Id;
5738 -- Original specification in access to subprogram
5739
5740 P : Node_Id;
5741
5742 begin
5743 Set_Is_Internal (Anon);
5744
5745 case Nkind (N) is
5746 when N_Component_Declaration |
5747 N_Unconstrained_Array_Definition |
5748 N_Constrained_Array_Definition =>
5749 Comp := Component_Definition (N);
5750 Acc := Access_Definition (Comp);
5751
5752 when N_Discriminant_Specification =>
5753 Comp := Discriminant_Type (N);
5754 Acc := Comp;
5755
5756 when N_Parameter_Specification =>
5757 Comp := Parameter_Type (N);
5758 Acc := Comp;
5759
5760 when N_Access_Function_Definition =>
5761 Comp := Result_Definition (N);
5762 Acc := Comp;
5763
5764 when N_Object_Declaration =>
5765 Comp := Object_Definition (N);
5766 Acc := Comp;
5767
5768 when N_Function_Specification =>
5769 Comp := Result_Definition (N);
5770 Acc := Comp;
5771
5772 when others =>
5773 raise Program_Error;
5774 end case;
5775
5776 Spec := Access_To_Subprogram_Definition (Acc);
5777
5778 Decl :=
5779 Make_Full_Type_Declaration (Loc,
5780 Defining_Identifier => Anon,
5781 Type_Definition => Copy_Separate_Tree (Spec));
5782
5783 Mark_Rewrite_Insertion (Decl);
5784
5785 -- In ASIS mode, analyze the profile on the original node, because
5786 -- the separate copy does not provide enough links to recover the
5787 -- original tree. Analysis is limited to type annotations, within
5788 -- a temporary scope that serves as an anonymous subprogram to collect
5789 -- otherwise useless temporaries and itypes.
5790
5791 if ASIS_Mode then
5792 declare
5793 Typ : constant Entity_Id := Make_Temporary (Loc, 'S');
5794
5795 begin
5796 if Nkind (Spec) = N_Access_Function_Definition then
5797 Set_Ekind (Typ, E_Function);
5798 else
5799 Set_Ekind (Typ, E_Procedure);
5800 end if;
5801
5802 Set_Parent (Typ, N);
5803 Set_Scope (Typ, Current_Scope);
5804 Push_Scope (Typ);
5805
5806 Process_Formals (Parameter_Specifications (Spec), Spec);
5807
5808 if Nkind (Spec) = N_Access_Function_Definition then
5809 declare
5810 Def : constant Node_Id := Result_Definition (Spec);
5811
5812 begin
5813 -- The result might itself be an anonymous access type, so
5814 -- have to recurse.
5815
5816 if Nkind (Def) = N_Access_Definition then
5817 if Present (Access_To_Subprogram_Definition (Def)) then
5818 Set_Etype
5819 (Def,
5820 Replace_Anonymous_Access_To_Protected_Subprogram
5821 (Spec));
5822 else
5823 Find_Type (Subtype_Mark (Def));
5824 end if;
5825
5826 else
5827 Find_Type (Def);
5828 end if;
5829 end;
5830 end if;
5831
5832 End_Scope;
5833 end;
5834 end if;
5835
5836 -- Insert the new declaration in the nearest enclosing scope. If the
5837 -- node is a body and N is its return type, the declaration belongs in
5838 -- the enclosing scope.
5839
5840 P := Parent (N);
5841
5842 if Nkind (P) = N_Subprogram_Body
5843 and then Nkind (N) = N_Function_Specification
5844 then
5845 P := Parent (P);
5846 end if;
5847
5848 while Present (P) and then not Has_Declarations (P) loop
5849 P := Parent (P);
5850 end loop;
5851
5852 pragma Assert (Present (P));
5853
5854 if Nkind (P) = N_Package_Specification then
5855 Prepend (Decl, Visible_Declarations (P));
5856 else
5857 Prepend (Decl, Declarations (P));
5858 end if;
5859
5860 -- Replace the anonymous type with an occurrence of the new declaration.
5861 -- In all cases the rewritten node does not have the null-exclusion
5862 -- attribute because (if present) it was already inherited by the
5863 -- anonymous entity (Anon). Thus, in case of components we do not
5864 -- inherit this attribute.
5865
5866 if Nkind (N) = N_Parameter_Specification then
5867 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5868 Set_Etype (Defining_Identifier (N), Anon);
5869 Set_Null_Exclusion_Present (N, False);
5870
5871 elsif Nkind (N) = N_Object_Declaration then
5872 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5873 Set_Etype (Defining_Identifier (N), Anon);
5874
5875 elsif Nkind (N) = N_Access_Function_Definition then
5876 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5877
5878 elsif Nkind (N) = N_Function_Specification then
5879 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5880 Set_Etype (Defining_Unit_Name (N), Anon);
5881
5882 else
5883 Rewrite (Comp,
5884 Make_Component_Definition (Loc,
5885 Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
5886 end if;
5887
5888 Mark_Rewrite_Insertion (Comp);
5889
5890 if Nkind_In (N, N_Object_Declaration, N_Access_Function_Definition) then
5891 Analyze (Decl);
5892
5893 else
5894 -- Temporarily remove the current scope (record or subprogram) from
5895 -- the stack to add the new declarations to the enclosing scope.
5896
5897 Scope_Stack.Decrement_Last;
5898 Analyze (Decl);
5899 Set_Is_Itype (Anon);
5900 Scope_Stack.Append (Curr_Scope);
5901 end if;
5902
5903 Set_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
5904 Set_Can_Use_Internal_Rep (Anon, not Always_Compatible_Rep_On_Target);
5905 return Anon;
5906 end Replace_Anonymous_Access_To_Protected_Subprogram;
5907
5908 -------------------------------
5909 -- Build_Derived_Access_Type --
5910 -------------------------------
5911
5912 procedure Build_Derived_Access_Type
5913 (N : Node_Id;
5914 Parent_Type : Entity_Id;
5915 Derived_Type : Entity_Id)
5916 is
5917 S : constant Node_Id := Subtype_Indication (Type_Definition (N));
5918
5919 Desig_Type : Entity_Id;
5920 Discr : Entity_Id;
5921 Discr_Con_Elist : Elist_Id;
5922 Discr_Con_El : Elmt_Id;
5923 Subt : Entity_Id;
5924
5925 begin
5926 -- Set the designated type so it is available in case this is an access
5927 -- to a self-referential type, e.g. a standard list type with a next
5928 -- pointer. Will be reset after subtype is built.
5929
5930 Set_Directly_Designated_Type
5931 (Derived_Type, Designated_Type (Parent_Type));
5932
5933 Subt := Process_Subtype (S, N);
5934
5935 if Nkind (S) /= N_Subtype_Indication
5936 and then Subt /= Base_Type (Subt)
5937 then
5938 Set_Ekind (Derived_Type, E_Access_Subtype);
5939 end if;
5940
5941 if Ekind (Derived_Type) = E_Access_Subtype then
5942 declare
5943 Pbase : constant Entity_Id := Base_Type (Parent_Type);
5944 Ibase : constant Entity_Id :=
5945 Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
5946 Svg_Chars : constant Name_Id := Chars (Ibase);
5947 Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
5948
5949 begin
5950 Copy_Node (Pbase, Ibase);
5951
5952 Set_Chars (Ibase, Svg_Chars);
5953 Set_Next_Entity (Ibase, Svg_Next_E);
5954 Set_Sloc (Ibase, Sloc (Derived_Type));
5955 Set_Scope (Ibase, Scope (Derived_Type));
5956 Set_Freeze_Node (Ibase, Empty);
5957 Set_Is_Frozen (Ibase, False);
5958 Set_Comes_From_Source (Ibase, False);
5959 Set_Is_First_Subtype (Ibase, False);
5960
5961 Set_Etype (Ibase, Pbase);
5962 Set_Etype (Derived_Type, Ibase);
5963 end;
5964 end if;
5965
5966 Set_Directly_Designated_Type
5967 (Derived_Type, Designated_Type (Subt));
5968
5969 Set_Is_Constrained (Derived_Type, Is_Constrained (Subt));
5970 Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
5971 Set_Size_Info (Derived_Type, Parent_Type);
5972 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
5973 Set_Depends_On_Private (Derived_Type,
5974 Has_Private_Component (Derived_Type));
5975 Conditional_Delay (Derived_Type, Subt);
5976
5977 -- Ada 2005 (AI-231): Set the null-exclusion attribute, and verify
5978 -- that it is not redundant.
5979
5980 if Null_Exclusion_Present (Type_Definition (N)) then
5981 Set_Can_Never_Be_Null (Derived_Type);
5982
5983 -- What is with the "AND THEN FALSE" here ???
5984
5985 if Can_Never_Be_Null (Parent_Type)
5986 and then False
5987 then
5988 Error_Msg_NE
5989 ("`NOT NULL` not allowed (& already excludes null)",
5990 N, Parent_Type);
5991 end if;
5992
5993 elsif Can_Never_Be_Null (Parent_Type) then
5994 Set_Can_Never_Be_Null (Derived_Type);
5995 end if;
5996
5997 -- Note: we do not copy the Storage_Size_Variable, since we always go to
5998 -- the root type for this information.
5999
6000 -- Apply range checks to discriminants for derived record case
6001 -- ??? THIS CODE SHOULD NOT BE HERE REALLY.
6002
6003 Desig_Type := Designated_Type (Derived_Type);
6004 if Is_Composite_Type (Desig_Type)
6005 and then (not Is_Array_Type (Desig_Type))
6006 and then Has_Discriminants (Desig_Type)
6007 and then Base_Type (Desig_Type) /= Desig_Type
6008 then
6009 Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
6010 Discr_Con_El := First_Elmt (Discr_Con_Elist);
6011
6012 Discr := First_Discriminant (Base_Type (Desig_Type));
6013 while Present (Discr_Con_El) loop
6014 Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
6015 Next_Elmt (Discr_Con_El);
6016 Next_Discriminant (Discr);
6017 end loop;
6018 end if;
6019 end Build_Derived_Access_Type;
6020
6021 ------------------------------
6022 -- Build_Derived_Array_Type --
6023 ------------------------------
6024
6025 procedure Build_Derived_Array_Type
6026 (N : Node_Id;
6027 Parent_Type : Entity_Id;
6028 Derived_Type : Entity_Id)
6029 is
6030 Loc : constant Source_Ptr := Sloc (N);
6031 Tdef : constant Node_Id := Type_Definition (N);
6032 Indic : constant Node_Id := Subtype_Indication (Tdef);
6033 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
6034 Implicit_Base : Entity_Id;
6035 New_Indic : Node_Id;
6036
6037 procedure Make_Implicit_Base;
6038 -- If the parent subtype is constrained, the derived type is a subtype
6039 -- of an implicit base type derived from the parent base.
6040
6041 ------------------------
6042 -- Make_Implicit_Base --
6043 ------------------------
6044
6045 procedure Make_Implicit_Base is
6046 begin
6047 Implicit_Base :=
6048 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
6049
6050 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
6051 Set_Etype (Implicit_Base, Parent_Base);
6052
6053 Copy_Array_Subtype_Attributes (Implicit_Base, Parent_Base);
6054 Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
6055
6056 Set_Has_Delayed_Freeze (Implicit_Base, True);
6057
6058 -- Inherit the "ghostness" from the parent base type
6059
6060 if Is_Ghost_Entity (Parent_Base) or else Ghost_Mode > None then
6061 Set_Is_Ghost_Entity (Implicit_Base);
6062 end if;
6063 end Make_Implicit_Base;
6064
6065 -- Start of processing for Build_Derived_Array_Type
6066
6067 begin
6068 if not Is_Constrained (Parent_Type) then
6069 if Nkind (Indic) /= N_Subtype_Indication then
6070 Set_Ekind (Derived_Type, E_Array_Type);
6071
6072 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6073 Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
6074
6075 Set_Has_Delayed_Freeze (Derived_Type, True);
6076
6077 else
6078 Make_Implicit_Base;
6079 Set_Etype (Derived_Type, Implicit_Base);
6080
6081 New_Indic :=
6082 Make_Subtype_Declaration (Loc,
6083 Defining_Identifier => Derived_Type,
6084 Subtype_Indication =>
6085 Make_Subtype_Indication (Loc,
6086 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
6087 Constraint => Constraint (Indic)));
6088
6089 Rewrite (N, New_Indic);
6090 Analyze (N);
6091 end if;
6092
6093 else
6094 if Nkind (Indic) /= N_Subtype_Indication then
6095 Make_Implicit_Base;
6096
6097 Set_Ekind (Derived_Type, Ekind (Parent_Type));
6098 Set_Etype (Derived_Type, Implicit_Base);
6099 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6100
6101 else
6102 Error_Msg_N ("illegal constraint on constrained type", Indic);
6103 end if;
6104 end if;
6105
6106 -- If parent type is not a derived type itself, and is declared in
6107 -- closed scope (e.g. a subprogram), then we must explicitly introduce
6108 -- the new type's concatenation operator since Derive_Subprograms
6109 -- will not inherit the parent's operator. If the parent type is
6110 -- unconstrained, the operator is of the unconstrained base type.
6111
6112 if Number_Dimensions (Parent_Type) = 1
6113 and then not Is_Limited_Type (Parent_Type)
6114 and then not Is_Derived_Type (Parent_Type)
6115 and then not Is_Package_Or_Generic_Package
6116 (Scope (Base_Type (Parent_Type)))
6117 then
6118 if not Is_Constrained (Parent_Type)
6119 and then Is_Constrained (Derived_Type)
6120 then
6121 New_Concatenation_Op (Implicit_Base);
6122 else
6123 New_Concatenation_Op (Derived_Type);
6124 end if;
6125 end if;
6126 end Build_Derived_Array_Type;
6127
6128 -----------------------------------
6129 -- Build_Derived_Concurrent_Type --
6130 -----------------------------------
6131
6132 procedure Build_Derived_Concurrent_Type
6133 (N : Node_Id;
6134 Parent_Type : Entity_Id;
6135 Derived_Type : Entity_Id)
6136 is
6137 Loc : constant Source_Ptr := Sloc (N);
6138
6139 Corr_Record : constant Entity_Id := Make_Temporary (Loc, 'C');
6140 Corr_Decl : Node_Id;
6141 Corr_Decl_Needed : Boolean;
6142 -- If the derived type has fewer discriminants than its parent, the
6143 -- corresponding record is also a derived type, in order to account for
6144 -- the bound discriminants. We create a full type declaration for it in
6145 -- this case.
6146
6147 Constraint_Present : constant Boolean :=
6148 Nkind (Subtype_Indication (Type_Definition (N))) =
6149 N_Subtype_Indication;
6150
6151 D_Constraint : Node_Id;
6152 New_Constraint : Elist_Id;
6153 Old_Disc : Entity_Id;
6154 New_Disc : Entity_Id;
6155 New_N : Node_Id;
6156
6157 begin
6158 Set_Stored_Constraint (Derived_Type, No_Elist);
6159 Corr_Decl_Needed := False;
6160 Old_Disc := Empty;
6161
6162 if Present (Discriminant_Specifications (N))
6163 and then Constraint_Present
6164 then
6165 Old_Disc := First_Discriminant (Parent_Type);
6166 New_Disc := First (Discriminant_Specifications (N));
6167 while Present (New_Disc) and then Present (Old_Disc) loop
6168 Next_Discriminant (Old_Disc);
6169 Next (New_Disc);
6170 end loop;
6171 end if;
6172
6173 if Present (Old_Disc) and then Expander_Active then
6174
6175 -- The new type has fewer discriminants, so we need to create a new
6176 -- corresponding record, which is derived from the corresponding
6177 -- record of the parent, and has a stored constraint that captures
6178 -- the values of the discriminant constraints. The corresponding
6179 -- record is needed only if expander is active and code generation is
6180 -- enabled.
6181
6182 -- The type declaration for the derived corresponding record has the
6183 -- same discriminant part and constraints as the current declaration.
6184 -- Copy the unanalyzed tree to build declaration.
6185
6186 Corr_Decl_Needed := True;
6187 New_N := Copy_Separate_Tree (N);
6188
6189 Corr_Decl :=
6190 Make_Full_Type_Declaration (Loc,
6191 Defining_Identifier => Corr_Record,
6192 Discriminant_Specifications =>
6193 Discriminant_Specifications (New_N),
6194 Type_Definition =>
6195 Make_Derived_Type_Definition (Loc,
6196 Subtype_Indication =>
6197 Make_Subtype_Indication (Loc,
6198 Subtype_Mark =>
6199 New_Occurrence_Of
6200 (Corresponding_Record_Type (Parent_Type), Loc),
6201 Constraint =>
6202 Constraint
6203 (Subtype_Indication (Type_Definition (New_N))))));
6204 end if;
6205
6206 -- Copy Storage_Size and Relative_Deadline variables if task case
6207
6208 if Is_Task_Type (Parent_Type) then
6209 Set_Storage_Size_Variable (Derived_Type,
6210 Storage_Size_Variable (Parent_Type));
6211 Set_Relative_Deadline_Variable (Derived_Type,
6212 Relative_Deadline_Variable (Parent_Type));
6213 end if;
6214
6215 if Present (Discriminant_Specifications (N)) then
6216 Push_Scope (Derived_Type);
6217 Check_Or_Process_Discriminants (N, Derived_Type);
6218
6219 if Constraint_Present then
6220 New_Constraint :=
6221 Expand_To_Stored_Constraint
6222 (Parent_Type,
6223 Build_Discriminant_Constraints
6224 (Parent_Type,
6225 Subtype_Indication (Type_Definition (N)), True));
6226 end if;
6227
6228 End_Scope;
6229
6230 elsif Constraint_Present then
6231
6232 -- Build constrained subtype, copying the constraint, and derive
6233 -- from it to create a derived constrained type.
6234
6235 declare
6236 Loc : constant Source_Ptr := Sloc (N);
6237 Anon : constant Entity_Id :=
6238 Make_Defining_Identifier (Loc,
6239 Chars => New_External_Name (Chars (Derived_Type), 'T'));
6240 Decl : Node_Id;
6241
6242 begin
6243 Decl :=
6244 Make_Subtype_Declaration (Loc,
6245 Defining_Identifier => Anon,
6246 Subtype_Indication =>
6247 New_Copy_Tree (Subtype_Indication (Type_Definition (N))));
6248 Insert_Before (N, Decl);
6249 Analyze (Decl);
6250
6251 Rewrite (Subtype_Indication (Type_Definition (N)),
6252 New_Occurrence_Of (Anon, Loc));
6253 Set_Analyzed (Derived_Type, False);
6254 Analyze (N);
6255 return;
6256 end;
6257 end if;
6258
6259 -- By default, operations and private data are inherited from parent.
6260 -- However, in the presence of bound discriminants, a new corresponding
6261 -- record will be created, see below.
6262
6263 Set_Has_Discriminants
6264 (Derived_Type, Has_Discriminants (Parent_Type));
6265 Set_Corresponding_Record_Type
6266 (Derived_Type, Corresponding_Record_Type (Parent_Type));
6267
6268 -- Is_Constrained is set according the parent subtype, but is set to
6269 -- False if the derived type is declared with new discriminants.
6270
6271 Set_Is_Constrained
6272 (Derived_Type,
6273 (Is_Constrained (Parent_Type) or else Constraint_Present)
6274 and then not Present (Discriminant_Specifications (N)));
6275
6276 if Constraint_Present then
6277 if not Has_Discriminants (Parent_Type) then
6278 Error_Msg_N ("untagged parent must have discriminants", N);
6279
6280 elsif Present (Discriminant_Specifications (N)) then
6281
6282 -- Verify that new discriminants are used to constrain old ones
6283
6284 D_Constraint :=
6285 First
6286 (Constraints
6287 (Constraint (Subtype_Indication (Type_Definition (N)))));
6288
6289 Old_Disc := First_Discriminant (Parent_Type);
6290
6291 while Present (D_Constraint) loop
6292 if Nkind (D_Constraint) /= N_Discriminant_Association then
6293
6294 -- Positional constraint. If it is a reference to a new
6295 -- discriminant, it constrains the corresponding old one.
6296
6297 if Nkind (D_Constraint) = N_Identifier then
6298 New_Disc := First_Discriminant (Derived_Type);
6299 while Present (New_Disc) loop
6300 exit when Chars (New_Disc) = Chars (D_Constraint);
6301 Next_Discriminant (New_Disc);
6302 end loop;
6303
6304 if Present (New_Disc) then
6305 Set_Corresponding_Discriminant (New_Disc, Old_Disc);
6306 end if;
6307 end if;
6308
6309 Next_Discriminant (Old_Disc);
6310
6311 -- if this is a named constraint, search by name for the old
6312 -- discriminants constrained by the new one.
6313
6314 elsif Nkind (Expression (D_Constraint)) = N_Identifier then
6315
6316 -- Find new discriminant with that name
6317
6318 New_Disc := First_Discriminant (Derived_Type);
6319 while Present (New_Disc) loop
6320 exit when
6321 Chars (New_Disc) = Chars (Expression (D_Constraint));
6322 Next_Discriminant (New_Disc);
6323 end loop;
6324
6325 if Present (New_Disc) then
6326
6327 -- Verify that new discriminant renames some discriminant
6328 -- of the parent type, and associate the new discriminant
6329 -- with one or more old ones that it renames.
6330
6331 declare
6332 Selector : Node_Id;
6333
6334 begin
6335 Selector := First (Selector_Names (D_Constraint));
6336 while Present (Selector) loop
6337 Old_Disc := First_Discriminant (Parent_Type);
6338 while Present (Old_Disc) loop
6339 exit when Chars (Old_Disc) = Chars (Selector);
6340 Next_Discriminant (Old_Disc);
6341 end loop;
6342
6343 if Present (Old_Disc) then
6344 Set_Corresponding_Discriminant
6345 (New_Disc, Old_Disc);
6346 end if;
6347
6348 Next (Selector);
6349 end loop;
6350 end;
6351 end if;
6352 end if;
6353
6354 Next (D_Constraint);
6355 end loop;
6356
6357 New_Disc := First_Discriminant (Derived_Type);
6358 while Present (New_Disc) loop
6359 if No (Corresponding_Discriminant (New_Disc)) then
6360 Error_Msg_NE
6361 ("new discriminant& must constrain old one", N, New_Disc);
6362
6363 elsif not
6364 Subtypes_Statically_Compatible
6365 (Etype (New_Disc),
6366 Etype (Corresponding_Discriminant (New_Disc)))
6367 then
6368 Error_Msg_NE
6369 ("& not statically compatible with parent discriminant",
6370 N, New_Disc);
6371 end if;
6372
6373 Next_Discriminant (New_Disc);
6374 end loop;
6375 end if;
6376
6377 elsif Present (Discriminant_Specifications (N)) then
6378 Error_Msg_N
6379 ("missing discriminant constraint in untagged derivation", N);
6380 end if;
6381
6382 -- The entity chain of the derived type includes the new discriminants
6383 -- but shares operations with the parent.
6384
6385 if Present (Discriminant_Specifications (N)) then
6386 Old_Disc := First_Discriminant (Parent_Type);
6387 while Present (Old_Disc) loop
6388 if No (Next_Entity (Old_Disc))
6389 or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
6390 then
6391 Set_Next_Entity
6392 (Last_Entity (Derived_Type), Next_Entity (Old_Disc));
6393 exit;
6394 end if;
6395
6396 Next_Discriminant (Old_Disc);
6397 end loop;
6398
6399 else
6400 Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
6401 if Has_Discriminants (Parent_Type) then
6402 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
6403 Set_Discriminant_Constraint (
6404 Derived_Type, Discriminant_Constraint (Parent_Type));
6405 end if;
6406 end if;
6407
6408 Set_Last_Entity (Derived_Type, Last_Entity (Parent_Type));
6409
6410 Set_Has_Completion (Derived_Type);
6411
6412 if Corr_Decl_Needed then
6413 Set_Stored_Constraint (Derived_Type, New_Constraint);
6414 Insert_After (N, Corr_Decl);
6415 Analyze (Corr_Decl);
6416 Set_Corresponding_Record_Type (Derived_Type, Corr_Record);
6417 end if;
6418 end Build_Derived_Concurrent_Type;
6419
6420 ------------------------------------
6421 -- Build_Derived_Enumeration_Type --
6422 ------------------------------------
6423
6424 procedure Build_Derived_Enumeration_Type
6425 (N : Node_Id;
6426 Parent_Type : Entity_Id;
6427 Derived_Type : Entity_Id)
6428 is
6429 Loc : constant Source_Ptr := Sloc (N);
6430 Def : constant Node_Id := Type_Definition (N);
6431 Indic : constant Node_Id := Subtype_Indication (Def);
6432 Implicit_Base : Entity_Id;
6433 Literal : Entity_Id;
6434 New_Lit : Entity_Id;
6435 Literals_List : List_Id;
6436 Type_Decl : Node_Id;
6437 Hi, Lo : Node_Id;
6438 Rang_Expr : Node_Id;
6439
6440 begin
6441 -- Since types Standard.Character and Standard.[Wide_]Wide_Character do
6442 -- not have explicit literals lists we need to process types derived
6443 -- from them specially. This is handled by Derived_Standard_Character.
6444 -- If the parent type is a generic type, there are no literals either,
6445 -- and we construct the same skeletal representation as for the generic
6446 -- parent type.
6447
6448 if Is_Standard_Character_Type (Parent_Type) then
6449 Derived_Standard_Character (N, Parent_Type, Derived_Type);
6450
6451 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
6452 declare
6453 Lo : Node_Id;
6454 Hi : Node_Id;
6455
6456 begin
6457 if Nkind (Indic) /= N_Subtype_Indication then
6458 Lo :=
6459 Make_Attribute_Reference (Loc,
6460 Attribute_Name => Name_First,
6461 Prefix => New_Occurrence_Of (Derived_Type, Loc));
6462 Set_Etype (Lo, Derived_Type);
6463
6464 Hi :=
6465 Make_Attribute_Reference (Loc,
6466 Attribute_Name => Name_Last,
6467 Prefix => New_Occurrence_Of (Derived_Type, Loc));
6468 Set_Etype (Hi, Derived_Type);
6469
6470 Set_Scalar_Range (Derived_Type,
6471 Make_Range (Loc,
6472 Low_Bound => Lo,
6473 High_Bound => Hi));
6474 else
6475
6476 -- Analyze subtype indication and verify compatibility
6477 -- with parent type.
6478
6479 if Base_Type (Process_Subtype (Indic, N)) /=
6480 Base_Type (Parent_Type)
6481 then
6482 Error_Msg_N
6483 ("illegal constraint for formal discrete type", N);
6484 end if;
6485 end if;
6486 end;
6487
6488 else
6489 -- If a constraint is present, analyze the bounds to catch
6490 -- premature usage of the derived literals.
6491
6492 if Nkind (Indic) = N_Subtype_Indication
6493 and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
6494 then
6495 Analyze (Low_Bound (Range_Expression (Constraint (Indic))));
6496 Analyze (High_Bound (Range_Expression (Constraint (Indic))));
6497 end if;
6498
6499 -- Introduce an implicit base type for the derived type even if there
6500 -- is no constraint attached to it, since this seems closer to the
6501 -- Ada semantics. Build a full type declaration tree for the derived
6502 -- type using the implicit base type as the defining identifier. The
6503 -- build a subtype declaration tree which applies the constraint (if
6504 -- any) have it replace the derived type declaration.
6505
6506 Literal := First_Literal (Parent_Type);
6507 Literals_List := New_List;
6508 while Present (Literal)
6509 and then Ekind (Literal) = E_Enumeration_Literal
6510 loop
6511 -- Literals of the derived type have the same representation as
6512 -- those of the parent type, but this representation can be
6513 -- overridden by an explicit representation clause. Indicate
6514 -- that there is no explicit representation given yet. These
6515 -- derived literals are implicit operations of the new type,
6516 -- and can be overridden by explicit ones.
6517
6518 if Nkind (Literal) = N_Defining_Character_Literal then
6519 New_Lit :=
6520 Make_Defining_Character_Literal (Loc, Chars (Literal));
6521 else
6522 New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
6523 end if;
6524
6525 Set_Ekind (New_Lit, E_Enumeration_Literal);
6526 Set_Enumeration_Pos (New_Lit, Enumeration_Pos (Literal));
6527 Set_Enumeration_Rep (New_Lit, Enumeration_Rep (Literal));
6528 Set_Enumeration_Rep_Expr (New_Lit, Empty);
6529 Set_Alias (New_Lit, Literal);
6530 Set_Is_Known_Valid (New_Lit, True);
6531
6532 Append (New_Lit, Literals_List);
6533 Next_Literal (Literal);
6534 end loop;
6535
6536 Implicit_Base :=
6537 Make_Defining_Identifier (Sloc (Derived_Type),
6538 Chars => New_External_Name (Chars (Derived_Type), 'B'));
6539
6540 -- Indicate the proper nature of the derived type. This must be done
6541 -- before analysis of the literals, to recognize cases when a literal
6542 -- may be hidden by a previous explicit function definition (cf.
6543 -- c83031a).
6544
6545 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
6546 Set_Etype (Derived_Type, Implicit_Base);
6547
6548 Type_Decl :=
6549 Make_Full_Type_Declaration (Loc,
6550 Defining_Identifier => Implicit_Base,
6551 Discriminant_Specifications => No_List,
6552 Type_Definition =>
6553 Make_Enumeration_Type_Definition (Loc, Literals_List));
6554
6555 Mark_Rewrite_Insertion (Type_Decl);
6556 Insert_Before (N, Type_Decl);
6557 Analyze (Type_Decl);
6558
6559 -- After the implicit base is analyzed its Etype needs to be changed
6560 -- to reflect the fact that it is derived from the parent type which
6561 -- was ignored during analysis. We also set the size at this point.
6562
6563 Set_Etype (Implicit_Base, Parent_Type);
6564
6565 Set_Size_Info (Implicit_Base, Parent_Type);
6566 Set_RM_Size (Implicit_Base, RM_Size (Parent_Type));
6567 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
6568
6569 -- Copy other flags from parent type
6570
6571 Set_Has_Non_Standard_Rep
6572 (Implicit_Base, Has_Non_Standard_Rep
6573 (Parent_Type));
6574 Set_Has_Pragma_Ordered
6575 (Implicit_Base, Has_Pragma_Ordered
6576 (Parent_Type));
6577 Set_Has_Delayed_Freeze (Implicit_Base);
6578
6579 -- Process the subtype indication including a validation check on the
6580 -- constraint, if any. If a constraint is given, its bounds must be
6581 -- implicitly converted to the new type.
6582
6583 if Nkind (Indic) = N_Subtype_Indication then
6584 declare
6585 R : constant Node_Id :=
6586 Range_Expression (Constraint (Indic));
6587
6588 begin
6589 if Nkind (R) = N_Range then
6590 Hi := Build_Scalar_Bound
6591 (High_Bound (R), Parent_Type, Implicit_Base);
6592 Lo := Build_Scalar_Bound
6593 (Low_Bound (R), Parent_Type, Implicit_Base);
6594
6595 else
6596 -- Constraint is a Range attribute. Replace with explicit
6597 -- mention of the bounds of the prefix, which must be a
6598 -- subtype.
6599
6600 Analyze (Prefix (R));
6601 Hi :=
6602 Convert_To (Implicit_Base,
6603 Make_Attribute_Reference (Loc,
6604 Attribute_Name => Name_Last,
6605 Prefix =>
6606 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
6607
6608 Lo :=
6609 Convert_To (Implicit_Base,
6610 Make_Attribute_Reference (Loc,
6611 Attribute_Name => Name_First,
6612 Prefix =>
6613 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
6614 end if;
6615 end;
6616
6617 else
6618 Hi :=
6619 Build_Scalar_Bound
6620 (Type_High_Bound (Parent_Type),
6621 Parent_Type, Implicit_Base);
6622 Lo :=
6623 Build_Scalar_Bound
6624 (Type_Low_Bound (Parent_Type),
6625 Parent_Type, Implicit_Base);
6626 end if;
6627
6628 Rang_Expr :=
6629 Make_Range (Loc,
6630 Low_Bound => Lo,
6631 High_Bound => Hi);
6632
6633 -- If we constructed a default range for the case where no range
6634 -- was given, then the expressions in the range must not freeze
6635 -- since they do not correspond to expressions in the source.
6636
6637 if Nkind (Indic) /= N_Subtype_Indication then
6638 Set_Must_Not_Freeze (Lo);
6639 Set_Must_Not_Freeze (Hi);
6640 Set_Must_Not_Freeze (Rang_Expr);
6641 end if;
6642
6643 Rewrite (N,
6644 Make_Subtype_Declaration (Loc,
6645 Defining_Identifier => Derived_Type,
6646 Subtype_Indication =>
6647 Make_Subtype_Indication (Loc,
6648 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
6649 Constraint =>
6650 Make_Range_Constraint (Loc,
6651 Range_Expression => Rang_Expr))));
6652
6653 Analyze (N);
6654
6655 -- Propagate the aspects from the original type declaration to the
6656 -- declaration of the implicit base.
6657
6658 Move_Aspects (From => Original_Node (N), To => Type_Decl);
6659
6660 -- Apply a range check. Since this range expression doesn't have an
6661 -- Etype, we have to specifically pass the Source_Typ parameter. Is
6662 -- this right???
6663
6664 if Nkind (Indic) = N_Subtype_Indication then
6665 Apply_Range_Check
6666 (Range_Expression (Constraint (Indic)), Parent_Type,
6667 Source_Typ => Entity (Subtype_Mark (Indic)));
6668 end if;
6669 end if;
6670 end Build_Derived_Enumeration_Type;
6671
6672 --------------------------------
6673 -- Build_Derived_Numeric_Type --
6674 --------------------------------
6675
6676 procedure Build_Derived_Numeric_Type
6677 (N : Node_Id;
6678 Parent_Type : Entity_Id;
6679 Derived_Type : Entity_Id)
6680 is
6681 Loc : constant Source_Ptr := Sloc (N);
6682 Tdef : constant Node_Id := Type_Definition (N);
6683 Indic : constant Node_Id := Subtype_Indication (Tdef);
6684 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
6685 No_Constraint : constant Boolean := Nkind (Indic) /=
6686 N_Subtype_Indication;
6687 Implicit_Base : Entity_Id;
6688
6689 Lo : Node_Id;
6690 Hi : Node_Id;
6691
6692 begin
6693 -- Process the subtype indication including a validation check on
6694 -- the constraint if any.
6695
6696 Discard_Node (Process_Subtype (Indic, N));
6697
6698 -- Introduce an implicit base type for the derived type even if there
6699 -- is no constraint attached to it, since this seems closer to the Ada
6700 -- semantics.
6701
6702 Implicit_Base :=
6703 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
6704
6705 Set_Etype (Implicit_Base, Parent_Base);
6706 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
6707 Set_Size_Info (Implicit_Base, Parent_Base);
6708 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
6709 Set_Parent (Implicit_Base, Parent (Derived_Type));
6710 Set_Is_Known_Valid (Implicit_Base, Is_Known_Valid (Parent_Base));
6711
6712 -- Set RM Size for discrete type or decimal fixed-point type
6713 -- Ordinary fixed-point is excluded, why???
6714
6715 if Is_Discrete_Type (Parent_Base)
6716 or else Is_Decimal_Fixed_Point_Type (Parent_Base)
6717 then
6718 Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
6719 end if;
6720
6721 Set_Has_Delayed_Freeze (Implicit_Base);
6722
6723 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
6724 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
6725
6726 Set_Scalar_Range (Implicit_Base,
6727 Make_Range (Loc,
6728 Low_Bound => Lo,
6729 High_Bound => Hi));
6730
6731 if Has_Infinities (Parent_Base) then
6732 Set_Includes_Infinities (Scalar_Range (Implicit_Base));
6733 end if;
6734
6735 -- The Derived_Type, which is the entity of the declaration, is a
6736 -- subtype of the implicit base. Its Ekind is a subtype, even in the
6737 -- absence of an explicit constraint.
6738
6739 Set_Etype (Derived_Type, Implicit_Base);
6740
6741 -- If we did not have a constraint, then the Ekind is set from the
6742 -- parent type (otherwise Process_Subtype has set the bounds)
6743
6744 if No_Constraint then
6745 Set_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
6746 end if;
6747
6748 -- If we did not have a range constraint, then set the range from the
6749 -- parent type. Otherwise, the Process_Subtype call has set the bounds.
6750
6751 if No_Constraint or else not Has_Range_Constraint (Indic) then
6752 Set_Scalar_Range (Derived_Type,
6753 Make_Range (Loc,
6754 Low_Bound => New_Copy_Tree (Type_Low_Bound (Parent_Type)),
6755 High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
6756 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
6757
6758 if Has_Infinities (Parent_Type) then
6759 Set_Includes_Infinities (Scalar_Range (Derived_Type));
6760 end if;
6761
6762 Set_Is_Known_Valid (Derived_Type, Is_Known_Valid (Parent_Type));
6763 end if;
6764
6765 Set_Is_Descendent_Of_Address (Derived_Type,
6766 Is_Descendent_Of_Address (Parent_Type));
6767 Set_Is_Descendent_Of_Address (Implicit_Base,
6768 Is_Descendent_Of_Address (Parent_Type));
6769
6770 -- Set remaining type-specific fields, depending on numeric type
6771
6772 if Is_Modular_Integer_Type (Parent_Type) then
6773 Set_Modulus (Implicit_Base, Modulus (Parent_Base));
6774
6775 Set_Non_Binary_Modulus
6776 (Implicit_Base, Non_Binary_Modulus (Parent_Base));
6777
6778 Set_Is_Known_Valid
6779 (Implicit_Base, Is_Known_Valid (Parent_Base));
6780
6781 elsif Is_Floating_Point_Type (Parent_Type) then
6782
6783 -- Digits of base type is always copied from the digits value of
6784 -- the parent base type, but the digits of the derived type will
6785 -- already have been set if there was a constraint present.
6786
6787 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
6788 Set_Float_Rep (Implicit_Base, Float_Rep (Parent_Base));
6789
6790 if No_Constraint then
6791 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
6792 end if;
6793
6794 elsif Is_Fixed_Point_Type (Parent_Type) then
6795
6796 -- Small of base type and derived type are always copied from the
6797 -- parent base type, since smalls never change. The delta of the
6798 -- base type is also copied from the parent base type. However the
6799 -- delta of the derived type will have been set already if a
6800 -- constraint was present.
6801
6802 Set_Small_Value (Derived_Type, Small_Value (Parent_Base));
6803 Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
6804 Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
6805
6806 if No_Constraint then
6807 Set_Delta_Value (Derived_Type, Delta_Value (Parent_Type));
6808 end if;
6809
6810 -- The scale and machine radix in the decimal case are always
6811 -- copied from the parent base type.
6812
6813 if Is_Decimal_Fixed_Point_Type (Parent_Type) then
6814 Set_Scale_Value (Derived_Type, Scale_Value (Parent_Base));
6815 Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
6816
6817 Set_Machine_Radix_10
6818 (Derived_Type, Machine_Radix_10 (Parent_Base));
6819 Set_Machine_Radix_10
6820 (Implicit_Base, Machine_Radix_10 (Parent_Base));
6821
6822 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
6823
6824 if No_Constraint then
6825 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
6826
6827 else
6828 -- the analysis of the subtype_indication sets the
6829 -- digits value of the derived type.
6830
6831 null;
6832 end if;
6833 end if;
6834 end if;
6835
6836 if Is_Integer_Type (Parent_Type) then
6837 Set_Has_Shift_Operator
6838 (Implicit_Base, Has_Shift_Operator (Parent_Type));
6839 end if;
6840
6841 -- The type of the bounds is that of the parent type, and they
6842 -- must be converted to the derived type.
6843
6844 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
6845
6846 -- The implicit_base should be frozen when the derived type is frozen,
6847 -- but note that it is used in the conversions of the bounds. For fixed
6848 -- types we delay the determination of the bounds until the proper
6849 -- freezing point. For other numeric types this is rejected by GCC, for
6850 -- reasons that are currently unclear (???), so we choose to freeze the
6851 -- implicit base now. In the case of integers and floating point types
6852 -- this is harmless because subsequent representation clauses cannot
6853 -- affect anything, but it is still baffling that we cannot use the
6854 -- same mechanism for all derived numeric types.
6855
6856 -- There is a further complication: actually some representation
6857 -- clauses can affect the implicit base type. For example, attribute
6858 -- definition clauses for stream-oriented attributes need to set the
6859 -- corresponding TSS entries on the base type, and this normally
6860 -- cannot be done after the base type is frozen, so the circuitry in
6861 -- Sem_Ch13.New_Stream_Subprogram must account for this possibility
6862 -- and not use Set_TSS in this case.
6863
6864 -- There are also consequences for the case of delayed representation
6865 -- aspects for some cases. For example, a Size aspect is delayed and
6866 -- should not be evaluated to the freeze point. This early freezing
6867 -- means that the size attribute evaluation happens too early???
6868
6869 if Is_Fixed_Point_Type (Parent_Type) then
6870 Conditional_Delay (Implicit_Base, Parent_Type);
6871 else
6872 Freeze_Before (N, Implicit_Base);
6873 end if;
6874 end Build_Derived_Numeric_Type;
6875
6876 --------------------------------
6877 -- Build_Derived_Private_Type --
6878 --------------------------------
6879
6880 procedure Build_Derived_Private_Type
6881 (N : Node_Id;
6882 Parent_Type : Entity_Id;
6883 Derived_Type : Entity_Id;
6884 Is_Completion : Boolean;
6885 Derive_Subps : Boolean := True)
6886 is
6887 Loc : constant Source_Ptr := Sloc (N);
6888 Par_Base : constant Entity_Id := Base_Type (Parent_Type);
6889 Par_Scope : constant Entity_Id := Scope (Par_Base);
6890 Full_N : constant Node_Id := New_Copy_Tree (N);
6891 Full_Der : Entity_Id := New_Copy (Derived_Type);
6892 Full_P : Entity_Id;
6893
6894 procedure Build_Full_Derivation;
6895 -- Build full derivation, i.e. derive from the full view
6896
6897 procedure Copy_And_Build;
6898 -- Copy derived type declaration, replace parent with its full view,
6899 -- and build derivation
6900
6901 ---------------------------
6902 -- Build_Full_Derivation --
6903 ---------------------------
6904
6905 procedure Build_Full_Derivation is
6906 begin
6907 -- If parent scope is not open, install the declarations
6908
6909 if not In_Open_Scopes (Par_Scope) then
6910 Install_Private_Declarations (Par_Scope);
6911 Install_Visible_Declarations (Par_Scope);
6912 Copy_And_Build;
6913 Uninstall_Declarations (Par_Scope);
6914
6915 -- If parent scope is open and in another unit, and parent has a
6916 -- completion, then the derivation is taking place in the visible
6917 -- part of a child unit. In that case retrieve the full view of
6918 -- the parent momentarily.
6919
6920 elsif not In_Same_Source_Unit (N, Parent_Type) then
6921 Full_P := Full_View (Parent_Type);
6922 Exchange_Declarations (Parent_Type);
6923 Copy_And_Build;
6924 Exchange_Declarations (Full_P);
6925
6926 -- Otherwise it is a local derivation
6927
6928 else
6929 Copy_And_Build;
6930 end if;
6931 end Build_Full_Derivation;
6932
6933 --------------------
6934 -- Copy_And_Build --
6935 --------------------
6936
6937 procedure Copy_And_Build is
6938 Full_Parent : Entity_Id := Parent_Type;
6939
6940 begin
6941 -- If the parent is itself derived from another private type,
6942 -- installing the private declarations has not affected its
6943 -- privacy status, so use its own full view explicitly.
6944
6945 if Is_Private_Type (Full_Parent)
6946 and then Present (Full_View (Full_Parent))
6947 then
6948 Full_Parent := Full_View (Full_Parent);
6949 end if;
6950
6951 -- And its underlying full view if necessary
6952
6953 if Is_Private_Type (Full_Parent)
6954 and then Present (Underlying_Full_View (Full_Parent))
6955 then
6956 Full_Parent := Underlying_Full_View (Full_Parent);
6957 end if;
6958
6959 -- For record, access and most enumeration types, derivation from
6960 -- the full view requires a fully-fledged declaration. In the other
6961 -- cases, just use an itype.
6962
6963 if Ekind (Full_Parent) in Record_Kind
6964 or else Ekind (Full_Parent) in Access_Kind
6965 or else
6966 (Ekind (Full_Parent) in Enumeration_Kind
6967 and then not Is_Standard_Character_Type (Full_Parent)
6968 and then not Is_Generic_Type (Root_Type (Full_Parent)))
6969 then
6970 -- Copy and adjust declaration to provide a completion for what
6971 -- is originally a private declaration. Indicate that full view
6972 -- is internally generated.
6973
6974 Set_Comes_From_Source (Full_N, False);
6975 Set_Comes_From_Source (Full_Der, False);
6976 Set_Parent (Full_Der, Full_N);
6977 Set_Defining_Identifier (Full_N, Full_Der);
6978
6979 -- If there are no constraints, adjust the subtype mark
6980
6981 if Nkind (Subtype_Indication (Type_Definition (Full_N))) /=
6982 N_Subtype_Indication
6983 then
6984 Set_Subtype_Indication
6985 (Type_Definition (Full_N),
6986 New_Occurrence_Of (Full_Parent, Sloc (Full_N)));
6987 end if;
6988
6989 Insert_After (N, Full_N);
6990
6991 -- Build full view of derived type from full view of parent which
6992 -- is now installed. Subprograms have been derived on the partial
6993 -- view, the completion does not derive them anew.
6994
6995 if Ekind (Full_Parent) in Record_Kind then
6996
6997 -- If parent type is tagged, the completion inherits the proper
6998 -- primitive operations.
6999
7000 if Is_Tagged_Type (Parent_Type) then
7001 Build_Derived_Record_Type
7002 (Full_N, Full_Parent, Full_Der, Derive_Subps);
7003 else
7004 Build_Derived_Record_Type
7005 (Full_N, Full_Parent, Full_Der, Derive_Subps => False);
7006 end if;
7007
7008 else
7009 Build_Derived_Type
7010 (Full_N, Full_Parent, Full_Der,
7011 Is_Completion => False, Derive_Subps => False);
7012 end if;
7013
7014 -- The full declaration has been introduced into the tree and
7015 -- processed in the step above. It should not be analyzed again
7016 -- (when encountered later in the current list of declarations)
7017 -- to prevent spurious name conflicts. The full entity remains
7018 -- invisible.
7019
7020 Set_Analyzed (Full_N);
7021
7022 else
7023 Full_Der :=
7024 Make_Defining_Identifier (Sloc (Derived_Type),
7025 Chars => Chars (Derived_Type));
7026 Set_Is_Itype (Full_Der);
7027 Set_Associated_Node_For_Itype (Full_Der, N);
7028 Set_Parent (Full_Der, N);
7029 Build_Derived_Type
7030 (N, Full_Parent, Full_Der,
7031 Is_Completion => False, Derive_Subps => False);
7032 end if;
7033
7034 Set_Has_Private_Declaration (Full_Der);
7035 Set_Has_Private_Declaration (Derived_Type);
7036
7037 Set_Scope (Full_Der, Scope (Derived_Type));
7038 Set_Is_First_Subtype (Full_Der, Is_First_Subtype (Derived_Type));
7039 Set_Has_Size_Clause (Full_Der, False);
7040 Set_Has_Alignment_Clause (Full_Der, False);
7041 Set_Has_Delayed_Freeze (Full_Der);
7042 Set_Is_Frozen (Full_Der, False);
7043 Set_Freeze_Node (Full_Der, Empty);
7044 Set_Depends_On_Private (Full_Der, Has_Private_Component (Full_Der));
7045 Set_Is_Public (Full_Der, Is_Public (Derived_Type));
7046
7047 -- The convention on the base type may be set in the private part
7048 -- and not propagated to the subtype until later, so we obtain the
7049 -- convention from the base type of the parent.
7050
7051 Set_Convention (Full_Der, Convention (Base_Type (Full_Parent)));
7052 end Copy_And_Build;
7053
7054 -- Start of processing for Build_Derived_Private_Type
7055
7056 begin
7057 if Is_Tagged_Type (Parent_Type) then
7058 Full_P := Full_View (Parent_Type);
7059
7060 -- A type extension of a type with unknown discriminants is an
7061 -- indefinite type that the back-end cannot handle directly.
7062 -- We treat it as a private type, and build a completion that is
7063 -- derived from the full view of the parent, and hopefully has
7064 -- known discriminants.
7065
7066 -- If the full view of the parent type has an underlying record view,
7067 -- use it to generate the underlying record view of this derived type
7068 -- (required for chains of derivations with unknown discriminants).
7069
7070 -- Minor optimization: we avoid the generation of useless underlying
7071 -- record view entities if the private type declaration has unknown
7072 -- discriminants but its corresponding full view has no
7073 -- discriminants.
7074
7075 if Has_Unknown_Discriminants (Parent_Type)
7076 and then Present (Full_P)
7077 and then (Has_Discriminants (Full_P)
7078 or else Present (Underlying_Record_View (Full_P)))
7079 and then not In_Open_Scopes (Par_Scope)
7080 and then Expander_Active
7081 then
7082 declare
7083 Full_Der : constant Entity_Id := Make_Temporary (Loc, 'T');
7084 New_Ext : constant Node_Id :=
7085 Copy_Separate_Tree
7086 (Record_Extension_Part (Type_Definition (N)));
7087 Decl : Node_Id;
7088
7089 begin
7090 Build_Derived_Record_Type
7091 (N, Parent_Type, Derived_Type, Derive_Subps);
7092
7093 -- Build anonymous completion, as a derivation from the full
7094 -- view of the parent. This is not a completion in the usual
7095 -- sense, because the current type is not private.
7096
7097 Decl :=
7098 Make_Full_Type_Declaration (Loc,
7099 Defining_Identifier => Full_Der,
7100 Type_Definition =>
7101 Make_Derived_Type_Definition (Loc,
7102 Subtype_Indication =>
7103 New_Copy_Tree
7104 (Subtype_Indication (Type_Definition (N))),
7105 Record_Extension_Part => New_Ext));
7106
7107 -- If the parent type has an underlying record view, use it
7108 -- here to build the new underlying record view.
7109
7110 if Present (Underlying_Record_View (Full_P)) then
7111 pragma Assert
7112 (Nkind (Subtype_Indication (Type_Definition (Decl)))
7113 = N_Identifier);
7114 Set_Entity (Subtype_Indication (Type_Definition (Decl)),
7115 Underlying_Record_View (Full_P));
7116 end if;
7117
7118 Install_Private_Declarations (Par_Scope);
7119 Install_Visible_Declarations (Par_Scope);
7120 Insert_Before (N, Decl);
7121
7122 -- Mark entity as an underlying record view before analysis,
7123 -- to avoid generating the list of its primitive operations
7124 -- (which is not really required for this entity) and thus
7125 -- prevent spurious errors associated with missing overriding
7126 -- of abstract primitives (overridden only for Derived_Type).
7127
7128 Set_Ekind (Full_Der, E_Record_Type);
7129 Set_Is_Underlying_Record_View (Full_Der);
7130 Set_Default_SSO (Full_Der);
7131
7132 Analyze (Decl);
7133
7134 pragma Assert (Has_Discriminants (Full_Der)
7135 and then not Has_Unknown_Discriminants (Full_Der));
7136
7137 Uninstall_Declarations (Par_Scope);
7138
7139 -- Freeze the underlying record view, to prevent generation of
7140 -- useless dispatching information, which is simply shared with
7141 -- the real derived type.
7142
7143 Set_Is_Frozen (Full_Der);
7144
7145 -- If the derived type has access discriminants, create
7146 -- references to their anonymous types now, to prevent
7147 -- back-end problems when their first use is in generated
7148 -- bodies of primitives.
7149
7150 declare
7151 E : Entity_Id;
7152
7153 begin
7154 E := First_Entity (Full_Der);
7155
7156 while Present (E) loop
7157 if Ekind (E) = E_Discriminant
7158 and then Ekind (Etype (E)) = E_Anonymous_Access_Type
7159 then
7160 Build_Itype_Reference (Etype (E), Decl);
7161 end if;
7162
7163 Next_Entity (E);
7164 end loop;
7165 end;
7166
7167 -- Set up links between real entity and underlying record view
7168
7169 Set_Underlying_Record_View (Derived_Type, Base_Type (Full_Der));
7170 Set_Underlying_Record_View (Base_Type (Full_Der), Derived_Type);
7171 end;
7172
7173 -- If discriminants are known, build derived record
7174
7175 else
7176 Build_Derived_Record_Type
7177 (N, Parent_Type, Derived_Type, Derive_Subps);
7178 end if;
7179
7180 return;
7181
7182 elsif Has_Discriminants (Parent_Type) then
7183
7184 -- Build partial view of derived type from partial view of parent.
7185 -- This must be done before building the full derivation because the
7186 -- second derivation will modify the discriminants of the first and
7187 -- the discriminants are chained with the rest of the components in
7188 -- the full derivation.
7189
7190 Build_Derived_Record_Type
7191 (N, Parent_Type, Derived_Type, Derive_Subps);
7192
7193 -- Build the full derivation if this is not the anonymous derived
7194 -- base type created by Build_Derived_Record_Type in the constrained
7195 -- case (see point 5. of its head comment) since we build it for the
7196 -- derived subtype. And skip it for protected types altogether, as
7197 -- gigi does not use these types directly.
7198
7199 if Present (Full_View (Parent_Type))
7200 and then not Is_Itype (Derived_Type)
7201 and then not (Ekind (Full_View (Parent_Type)) in Protected_Kind)
7202 then
7203 declare
7204 Der_Base : constant Entity_Id := Base_Type (Derived_Type);
7205 Discr : Entity_Id;
7206 Last_Discr : Entity_Id;
7207
7208 begin
7209 -- If this is not a completion, construct the implicit full
7210 -- view by deriving from the full view of the parent type.
7211 -- But if this is a completion, the derived private type
7212 -- being built is a full view and the full derivation can
7213 -- only be its underlying full view.
7214
7215 Build_Full_Derivation;
7216
7217 if not Is_Completion then
7218 Set_Full_View (Derived_Type, Full_Der);
7219 else
7220 Set_Underlying_Full_View (Derived_Type, Full_Der);
7221 end if;
7222
7223 if not Is_Base_Type (Derived_Type) then
7224 Set_Full_View (Der_Base, Base_Type (Full_Der));
7225 end if;
7226
7227 -- Copy the discriminant list from full view to the partial
7228 -- view (base type and its subtype). Gigi requires that the
7229 -- partial and full views have the same discriminants.
7230
7231 -- Note that since the partial view points to discriminants
7232 -- in the full view, their scope will be that of the full
7233 -- view. This might cause some front end problems and need
7234 -- adjustment???
7235
7236 Discr := First_Discriminant (Base_Type (Full_Der));
7237 Set_First_Entity (Der_Base, Discr);
7238
7239 loop
7240 Last_Discr := Discr;
7241 Next_Discriminant (Discr);
7242 exit when No (Discr);
7243 end loop;
7244
7245 Set_Last_Entity (Der_Base, Last_Discr);
7246 Set_First_Entity (Derived_Type, First_Entity (Der_Base));
7247 Set_Last_Entity (Derived_Type, Last_Entity (Der_Base));
7248
7249 Set_Stored_Constraint
7250 (Full_Der, Stored_Constraint (Derived_Type));
7251 end;
7252 end if;
7253
7254 elsif Present (Full_View (Parent_Type))
7255 and then Has_Discriminants (Full_View (Parent_Type))
7256 then
7257 if Has_Unknown_Discriminants (Parent_Type)
7258 and then Nkind (Subtype_Indication (Type_Definition (N))) =
7259 N_Subtype_Indication
7260 then
7261 Error_Msg_N
7262 ("cannot constrain type with unknown discriminants",
7263 Subtype_Indication (Type_Definition (N)));
7264 return;
7265 end if;
7266
7267 -- If this is not a completion, construct the implicit full view by
7268 -- deriving from the full view of the parent type. But if this is a
7269 -- completion, the derived private type being built is a full view
7270 -- and the full derivation can only be its underlying full view.
7271
7272 Build_Full_Derivation;
7273
7274 if not Is_Completion then
7275 Set_Full_View (Derived_Type, Full_Der);
7276 else
7277 Set_Underlying_Full_View (Derived_Type, Full_Der);
7278 end if;
7279
7280 -- In any case, the primitive operations are inherited from the
7281 -- parent type, not from the internal full view.
7282
7283 Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
7284
7285 if Derive_Subps then
7286 Derive_Subprograms (Parent_Type, Derived_Type);
7287 end if;
7288
7289 Set_Stored_Constraint (Derived_Type, No_Elist);
7290 Set_Is_Constrained
7291 (Derived_Type, Is_Constrained (Full_View (Parent_Type)));
7292
7293 else
7294 -- Untagged type, No discriminants on either view
7295
7296 if Nkind (Subtype_Indication (Type_Definition (N))) =
7297 N_Subtype_Indication
7298 then
7299 Error_Msg_N
7300 ("illegal constraint on type without discriminants", N);
7301 end if;
7302
7303 if Present (Discriminant_Specifications (N))
7304 and then Present (Full_View (Parent_Type))
7305 and then not Is_Tagged_Type (Full_View (Parent_Type))
7306 then
7307 Error_Msg_N ("cannot add discriminants to untagged type", N);
7308 end if;
7309
7310 Set_Stored_Constraint (Derived_Type, No_Elist);
7311 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7312 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
7313 Set_Has_Controlled_Component
7314 (Derived_Type, Has_Controlled_Component
7315 (Parent_Type));
7316
7317 -- Direct controlled types do not inherit Finalize_Storage_Only flag
7318
7319 if not Is_Controlled (Parent_Type) then
7320 Set_Finalize_Storage_Only
7321 (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
7322 end if;
7323
7324 -- If this is not a completion, construct the implicit full view by
7325 -- deriving from the full view of the parent type.
7326
7327 -- ??? If the parent is untagged private and its completion is
7328 -- tagged, this mechanism will not work because we cannot derive from
7329 -- the tagged full view unless we have an extension.
7330
7331 if Present (Full_View (Parent_Type))
7332 and then not Is_Tagged_Type (Full_View (Parent_Type))
7333 and then not Is_Completion
7334 then
7335 Build_Full_Derivation;
7336 Set_Full_View (Derived_Type, Full_Der);
7337 end if;
7338 end if;
7339
7340 Set_Has_Unknown_Discriminants (Derived_Type,
7341 Has_Unknown_Discriminants (Parent_Type));
7342
7343 if Is_Private_Type (Derived_Type) then
7344 Set_Private_Dependents (Derived_Type, New_Elmt_List);
7345 end if;
7346
7347 -- If the parent base type is in scope, add the derived type to its
7348 -- list of private dependents, because its full view may become
7349 -- visible subsequently (in a nested private part, a body, or in a
7350 -- further child unit).
7351
7352 if Is_Private_Type (Par_Base) and then In_Open_Scopes (Par_Scope) then
7353 Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
7354
7355 -- Check for unusual case where a type completed by a private
7356 -- derivation occurs within a package nested in a child unit, and
7357 -- the parent is declared in an ancestor.
7358
7359 if Is_Child_Unit (Scope (Current_Scope))
7360 and then Is_Completion
7361 and then In_Private_Part (Current_Scope)
7362 and then Scope (Parent_Type) /= Current_Scope
7363
7364 -- Note that if the parent has a completion in the private part,
7365 -- (which is itself a derivation from some other private type)
7366 -- it is that completion that is visible, there is no full view
7367 -- available, and no special processing is needed.
7368
7369 and then Present (Full_View (Parent_Type))
7370 then
7371 -- In this case, the full view of the parent type will become
7372 -- visible in the body of the enclosing child, and only then will
7373 -- the current type be possibly non-private. Build an underlying
7374 -- full view that will be installed when the enclosing child body
7375 -- is compiled.
7376
7377 if Present (Underlying_Full_View (Derived_Type)) then
7378 Full_Der := Underlying_Full_View (Derived_Type);
7379 else
7380 Build_Full_Derivation;
7381 Set_Underlying_Full_View (Derived_Type, Full_Der);
7382 end if;
7383
7384 -- The full view will be used to swap entities on entry/exit to
7385 -- the body, and must appear in the entity list for the package.
7386
7387 Append_Entity (Full_Der, Scope (Derived_Type));
7388 end if;
7389 end if;
7390 end Build_Derived_Private_Type;
7391
7392 -------------------------------
7393 -- Build_Derived_Record_Type --
7394 -------------------------------
7395
7396 -- 1. INTRODUCTION
7397
7398 -- Ideally we would like to use the same model of type derivation for
7399 -- tagged and untagged record types. Unfortunately this is not quite
7400 -- possible because the semantics of representation clauses is different
7401 -- for tagged and untagged records under inheritance. Consider the
7402 -- following:
7403
7404 -- type R (...) is [tagged] record ... end record;
7405 -- type T (...) is new R (...) [with ...];
7406
7407 -- The representation clauses for T can specify a completely different
7408 -- record layout from R's. Hence the same component can be placed in two
7409 -- very different positions in objects of type T and R. If R and T are
7410 -- tagged types, representation clauses for T can only specify the layout
7411 -- of non inherited components, thus components that are common in R and T
7412 -- have the same position in objects of type R and T.
7413
7414 -- This has two implications. The first is that the entire tree for R's
7415 -- declaration needs to be copied for T in the untagged case, so that T
7416 -- can be viewed as a record type of its own with its own representation
7417 -- clauses. The second implication is the way we handle discriminants.
7418 -- Specifically, in the untagged case we need a way to communicate to Gigi
7419 -- what are the real discriminants in the record, while for the semantics
7420 -- we need to consider those introduced by the user to rename the
7421 -- discriminants in the parent type. This is handled by introducing the
7422 -- notion of stored discriminants. See below for more.
7423
7424 -- Fortunately the way regular components are inherited can be handled in
7425 -- the same way in tagged and untagged types.
7426
7427 -- To complicate things a bit more the private view of a private extension
7428 -- cannot be handled in the same way as the full view (for one thing the
7429 -- semantic rules are somewhat different). We will explain what differs
7430 -- below.
7431
7432 -- 2. DISCRIMINANTS UNDER INHERITANCE
7433
7434 -- The semantic rules governing the discriminants of derived types are
7435 -- quite subtle.
7436
7437 -- type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
7438 -- [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
7439
7440 -- If parent type has discriminants, then the discriminants that are
7441 -- declared in the derived type are [3.4 (11)]:
7442
7443 -- o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
7444 -- there is one;
7445
7446 -- o Otherwise, each discriminant of the parent type (implicitly declared
7447 -- in the same order with the same specifications). In this case, the
7448 -- discriminants are said to be "inherited", or if unknown in the parent
7449 -- are also unknown in the derived type.
7450
7451 -- Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
7452
7453 -- o The parent subtype must be constrained;
7454
7455 -- o If the parent type is not a tagged type, then each discriminant of
7456 -- the derived type must be used in the constraint defining a parent
7457 -- subtype. [Implementation note: This ensures that the new discriminant
7458 -- can share storage with an existing discriminant.]
7459
7460 -- For the derived type each discriminant of the parent type is either
7461 -- inherited, constrained to equal some new discriminant of the derived
7462 -- type, or constrained to the value of an expression.
7463
7464 -- When inherited or constrained to equal some new discriminant, the
7465 -- parent discriminant and the discriminant of the derived type are said
7466 -- to "correspond".
7467
7468 -- If a discriminant of the parent type is constrained to a specific value
7469 -- in the derived type definition, then the discriminant is said to be
7470 -- "specified" by that derived type definition.
7471
7472 -- 3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
7473
7474 -- We have spoken about stored discriminants in point 1 (introduction)
7475 -- above. There are two sort of stored discriminants: implicit and
7476 -- explicit. As long as the derived type inherits the same discriminants as
7477 -- the root record type, stored discriminants are the same as regular
7478 -- discriminants, and are said to be implicit. However, if any discriminant
7479 -- in the root type was renamed in the derived type, then the derived
7480 -- type will contain explicit stored discriminants. Explicit stored
7481 -- discriminants are discriminants in addition to the semantically visible
7482 -- discriminants defined for the derived type. Stored discriminants are
7483 -- used by Gigi to figure out what are the physical discriminants in
7484 -- objects of the derived type (see precise definition in einfo.ads).
7485 -- As an example, consider the following:
7486
7487 -- type R (D1, D2, D3 : Int) is record ... end record;
7488 -- type T1 is new R;
7489 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
7490 -- type T3 is new T2;
7491 -- type T4 (Y : Int) is new T3 (Y, 99);
7492
7493 -- The following table summarizes the discriminants and stored
7494 -- discriminants in R and T1 through T4.
7495
7496 -- Type Discrim Stored Discrim Comment
7497 -- R (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in R
7498 -- T1 (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in T1
7499 -- T2 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T2
7500 -- T3 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T3
7501 -- T4 (Y) (D1, D2, D3) Girder discrims EXPLICIT in T4
7502
7503 -- Field Corresponding_Discriminant (abbreviated CD below) allows us to
7504 -- find the corresponding discriminant in the parent type, while
7505 -- Original_Record_Component (abbreviated ORC below), the actual physical
7506 -- component that is renamed. Finally the field Is_Completely_Hidden
7507 -- (abbreviated ICH below) is set for all explicit stored discriminants
7508 -- (see einfo.ads for more info). For the above example this gives:
7509
7510 -- Discrim CD ORC ICH
7511 -- ^^^^^^^ ^^ ^^^ ^^^
7512 -- D1 in R empty itself no
7513 -- D2 in R empty itself no
7514 -- D3 in R empty itself no
7515
7516 -- D1 in T1 D1 in R itself no
7517 -- D2 in T1 D2 in R itself no
7518 -- D3 in T1 D3 in R itself no
7519
7520 -- X1 in T2 D3 in T1 D3 in T2 no
7521 -- X2 in T2 D1 in T1 D1 in T2 no
7522 -- D1 in T2 empty itself yes
7523 -- D2 in T2 empty itself yes
7524 -- D3 in T2 empty itself yes
7525
7526 -- X1 in T3 X1 in T2 D3 in T3 no
7527 -- X2 in T3 X2 in T2 D1 in T3 no
7528 -- D1 in T3 empty itself yes
7529 -- D2 in T3 empty itself yes
7530 -- D3 in T3 empty itself yes
7531
7532 -- Y in T4 X1 in T3 D3 in T3 no
7533 -- D1 in T3 empty itself yes
7534 -- D2 in T3 empty itself yes
7535 -- D3 in T3 empty itself yes
7536
7537 -- 4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
7538
7539 -- Type derivation for tagged types is fairly straightforward. If no
7540 -- discriminants are specified by the derived type, these are inherited
7541 -- from the parent. No explicit stored discriminants are ever necessary.
7542 -- The only manipulation that is done to the tree is that of adding a
7543 -- _parent field with parent type and constrained to the same constraint
7544 -- specified for the parent in the derived type definition. For instance:
7545
7546 -- type R (D1, D2, D3 : Int) is tagged record ... end record;
7547 -- type T1 is new R with null record;
7548 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
7549
7550 -- are changed into:
7551
7552 -- type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
7553 -- _parent : R (D1, D2, D3);
7554 -- end record;
7555
7556 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
7557 -- _parent : T1 (X2, 88, X1);
7558 -- end record;
7559
7560 -- The discriminants actually present in R, T1 and T2 as well as their CD,
7561 -- ORC and ICH fields are:
7562
7563 -- Discrim CD ORC ICH
7564 -- ^^^^^^^ ^^ ^^^ ^^^
7565 -- D1 in R empty itself no
7566 -- D2 in R empty itself no
7567 -- D3 in R empty itself no
7568
7569 -- D1 in T1 D1 in R D1 in R no
7570 -- D2 in T1 D2 in R D2 in R no
7571 -- D3 in T1 D3 in R D3 in R no
7572
7573 -- X1 in T2 D3 in T1 D3 in R no
7574 -- X2 in T2 D1 in T1 D1 in R no
7575
7576 -- 5. FIRST TRANSFORMATION FOR DERIVED RECORDS
7577 --
7578 -- Regardless of whether we dealing with a tagged or untagged type
7579 -- we will transform all derived type declarations of the form
7580 --
7581 -- type T is new R (...) [with ...];
7582 -- or
7583 -- subtype S is R (...);
7584 -- type T is new S [with ...];
7585 -- into
7586 -- type BT is new R [with ...];
7587 -- subtype T is BT (...);
7588 --
7589 -- That is, the base derived type is constrained only if it has no
7590 -- discriminants. The reason for doing this is that GNAT's semantic model
7591 -- assumes that a base type with discriminants is unconstrained.
7592 --
7593 -- Note that, strictly speaking, the above transformation is not always
7594 -- correct. Consider for instance the following excerpt from ACVC b34011a:
7595 --
7596 -- procedure B34011A is
7597 -- type REC (D : integer := 0) is record
7598 -- I : Integer;
7599 -- end record;
7600
7601 -- package P is
7602 -- type T6 is new Rec;
7603 -- function F return T6;
7604 -- end P;
7605
7606 -- use P;
7607 -- package Q6 is
7608 -- type U is new T6 (Q6.F.I); -- ERROR: Q6.F.
7609 -- end Q6;
7610 --
7611 -- The definition of Q6.U is illegal. However transforming Q6.U into
7612
7613 -- type BaseU is new T6;
7614 -- subtype U is BaseU (Q6.F.I)
7615
7616 -- turns U into a legal subtype, which is incorrect. To avoid this problem
7617 -- we always analyze the constraint (in this case (Q6.F.I)) before applying
7618 -- the transformation described above.
7619
7620 -- There is another instance where the above transformation is incorrect.
7621 -- Consider:
7622
7623 -- package Pack is
7624 -- type Base (D : Integer) is tagged null record;
7625 -- procedure P (X : Base);
7626
7627 -- type Der is new Base (2) with null record;
7628 -- procedure P (X : Der);
7629 -- end Pack;
7630
7631 -- Then the above transformation turns this into
7632
7633 -- type Der_Base is new Base with null record;
7634 -- -- procedure P (X : Base) is implicitly inherited here
7635 -- -- as procedure P (X : Der_Base).
7636
7637 -- subtype Der is Der_Base (2);
7638 -- procedure P (X : Der);
7639 -- -- The overriding of P (X : Der_Base) is illegal since we
7640 -- -- have a parameter conformance problem.
7641
7642 -- To get around this problem, after having semantically processed Der_Base
7643 -- and the rewritten subtype declaration for Der, we copy Der_Base field
7644 -- Discriminant_Constraint from Der so that when parameter conformance is
7645 -- checked when P is overridden, no semantic errors are flagged.
7646
7647 -- 6. SECOND TRANSFORMATION FOR DERIVED RECORDS
7648
7649 -- Regardless of whether we are dealing with a tagged or untagged type
7650 -- we will transform all derived type declarations of the form
7651
7652 -- type R (D1, .., Dn : ...) is [tagged] record ...;
7653 -- type T is new R [with ...];
7654 -- into
7655 -- type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
7656
7657 -- The reason for such transformation is that it allows us to implement a
7658 -- very clean form of component inheritance as explained below.
7659
7660 -- Note that this transformation is not achieved by direct tree rewriting
7661 -- and manipulation, but rather by redoing the semantic actions that the
7662 -- above transformation will entail. This is done directly in routine
7663 -- Inherit_Components.
7664
7665 -- 7. TYPE DERIVATION AND COMPONENT INHERITANCE
7666
7667 -- In both tagged and untagged derived types, regular non discriminant
7668 -- components are inherited in the derived type from the parent type. In
7669 -- the absence of discriminants component, inheritance is straightforward
7670 -- as components can simply be copied from the parent.
7671
7672 -- If the parent has discriminants, inheriting components constrained with
7673 -- these discriminants requires caution. Consider the following example:
7674
7675 -- type R (D1, D2 : Positive) is [tagged] record
7676 -- S : String (D1 .. D2);
7677 -- end record;
7678
7679 -- type T1 is new R [with null record];
7680 -- type T2 (X : positive) is new R (1, X) [with null record];
7681
7682 -- As explained in 6. above, T1 is rewritten as
7683 -- type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
7684 -- which makes the treatment for T1 and T2 identical.
7685
7686 -- What we want when inheriting S, is that references to D1 and D2 in R are
7687 -- replaced with references to their correct constraints, i.e. D1 and D2 in
7688 -- T1 and 1 and X in T2. So all R's discriminant references are replaced
7689 -- with either discriminant references in the derived type or expressions.
7690 -- This replacement is achieved as follows: before inheriting R's
7691 -- components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
7692 -- created in the scope of T1 (resp. scope of T2) so that discriminants D1
7693 -- and D2 of T1 are visible (resp. discriminant X of T2 is visible).
7694 -- For T2, for instance, this has the effect of replacing String (D1 .. D2)
7695 -- by String (1 .. X).
7696
7697 -- 8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
7698
7699 -- We explain here the rules governing private type extensions relevant to
7700 -- type derivation. These rules are explained on the following example:
7701
7702 -- type D [(...)] is new A [(...)] with private; <-- partial view
7703 -- type D [(...)] is new P [(...)] with null record; <-- full view
7704
7705 -- Type A is called the ancestor subtype of the private extension.
7706 -- Type P is the parent type of the full view of the private extension. It
7707 -- must be A or a type derived from A.
7708
7709 -- The rules concerning the discriminants of private type extensions are
7710 -- [7.3(10-13)]:
7711
7712 -- o If a private extension inherits known discriminants from the ancestor
7713 -- subtype, then the full view must also inherit its discriminants from
7714 -- the ancestor subtype and the parent subtype of the full view must be
7715 -- constrained if and only if the ancestor subtype is constrained.
7716
7717 -- o If a partial view has unknown discriminants, then the full view may
7718 -- define a definite or an indefinite subtype, with or without
7719 -- discriminants.
7720
7721 -- o If a partial view has neither known nor unknown discriminants, then
7722 -- the full view must define a definite subtype.
7723
7724 -- o If the ancestor subtype of a private extension has constrained
7725 -- discriminants, then the parent subtype of the full view must impose a
7726 -- statically matching constraint on those discriminants.
7727
7728 -- This means that only the following forms of private extensions are
7729 -- allowed:
7730
7731 -- type D is new A with private; <-- partial view
7732 -- type D is new P with null record; <-- full view
7733
7734 -- If A has no discriminants than P has no discriminants, otherwise P must
7735 -- inherit A's discriminants.
7736
7737 -- type D is new A (...) with private; <-- partial view
7738 -- type D is new P (:::) with null record; <-- full view
7739
7740 -- P must inherit A's discriminants and (...) and (:::) must statically
7741 -- match.
7742
7743 -- subtype A is R (...);
7744 -- type D is new A with private; <-- partial view
7745 -- type D is new P with null record; <-- full view
7746
7747 -- P must have inherited R's discriminants and must be derived from A or
7748 -- any of its subtypes.
7749
7750 -- type D (..) is new A with private; <-- partial view
7751 -- type D (..) is new P [(:::)] with null record; <-- full view
7752
7753 -- No specific constraints on P's discriminants or constraint (:::).
7754 -- Note that A can be unconstrained, but the parent subtype P must either
7755 -- be constrained or (:::) must be present.
7756
7757 -- type D (..) is new A [(...)] with private; <-- partial view
7758 -- type D (..) is new P [(:::)] with null record; <-- full view
7759
7760 -- P's constraints on A's discriminants must statically match those
7761 -- imposed by (...).
7762
7763 -- 9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
7764
7765 -- The full view of a private extension is handled exactly as described
7766 -- above. The model chose for the private view of a private extension is
7767 -- the same for what concerns discriminants (i.e. they receive the same
7768 -- treatment as in the tagged case). However, the private view of the
7769 -- private extension always inherits the components of the parent base,
7770 -- without replacing any discriminant reference. Strictly speaking this is
7771 -- incorrect. However, Gigi never uses this view to generate code so this
7772 -- is a purely semantic issue. In theory, a set of transformations similar
7773 -- to those given in 5. and 6. above could be applied to private views of
7774 -- private extensions to have the same model of component inheritance as
7775 -- for non private extensions. However, this is not done because it would
7776 -- further complicate private type processing. Semantically speaking, this
7777 -- leaves us in an uncomfortable situation. As an example consider:
7778
7779 -- package Pack is
7780 -- type R (D : integer) is tagged record
7781 -- S : String (1 .. D);
7782 -- end record;
7783 -- procedure P (X : R);
7784 -- type T is new R (1) with private;
7785 -- private
7786 -- type T is new R (1) with null record;
7787 -- end;
7788
7789 -- This is transformed into:
7790
7791 -- package Pack is
7792 -- type R (D : integer) is tagged record
7793 -- S : String (1 .. D);
7794 -- end record;
7795 -- procedure P (X : R);
7796 -- type T is new R (1) with private;
7797 -- private
7798 -- type BaseT is new R with null record;
7799 -- subtype T is BaseT (1);
7800 -- end;
7801
7802 -- (strictly speaking the above is incorrect Ada)
7803
7804 -- From the semantic standpoint the private view of private extension T
7805 -- should be flagged as constrained since one can clearly have
7806 --
7807 -- Obj : T;
7808 --
7809 -- in a unit withing Pack. However, when deriving subprograms for the
7810 -- private view of private extension T, T must be seen as unconstrained
7811 -- since T has discriminants (this is a constraint of the current
7812 -- subprogram derivation model). Thus, when processing the private view of
7813 -- a private extension such as T, we first mark T as unconstrained, we
7814 -- process it, we perform program derivation and just before returning from
7815 -- Build_Derived_Record_Type we mark T as constrained.
7816
7817 -- ??? Are there are other uncomfortable cases that we will have to
7818 -- deal with.
7819
7820 -- 10. RECORD_TYPE_WITH_PRIVATE complications
7821
7822 -- Types that are derived from a visible record type and have a private
7823 -- extension present other peculiarities. They behave mostly like private
7824 -- types, but if they have primitive operations defined, these will not
7825 -- have the proper signatures for further inheritance, because other
7826 -- primitive operations will use the implicit base that we define for
7827 -- private derivations below. This affect subprogram inheritance (see
7828 -- Derive_Subprograms for details). We also derive the implicit base from
7829 -- the base type of the full view, so that the implicit base is a record
7830 -- type and not another private type, This avoids infinite loops.
7831
7832 procedure Build_Derived_Record_Type
7833 (N : Node_Id;
7834 Parent_Type : Entity_Id;
7835 Derived_Type : Entity_Id;
7836 Derive_Subps : Boolean := True)
7837 is
7838 Discriminant_Specs : constant Boolean :=
7839 Present (Discriminant_Specifications (N));
7840 Is_Tagged : constant Boolean := Is_Tagged_Type (Parent_Type);
7841 Loc : constant Source_Ptr := Sloc (N);
7842 Private_Extension : constant Boolean :=
7843 Nkind (N) = N_Private_Extension_Declaration;
7844 Assoc_List : Elist_Id;
7845 Constraint_Present : Boolean;
7846 Constrs : Elist_Id;
7847 Discrim : Entity_Id;
7848 Indic : Node_Id;
7849 Inherit_Discrims : Boolean := False;
7850 Last_Discrim : Entity_Id;
7851 New_Base : Entity_Id;
7852 New_Decl : Node_Id;
7853 New_Discrs : Elist_Id;
7854 New_Indic : Node_Id;
7855 Parent_Base : Entity_Id;
7856 Save_Etype : Entity_Id;
7857 Save_Discr_Constr : Elist_Id;
7858 Save_Next_Entity : Entity_Id;
7859 Type_Def : Node_Id;
7860
7861 Discs : Elist_Id := New_Elmt_List;
7862 -- An empty Discs list means that there were no constraints in the
7863 -- subtype indication or that there was an error processing it.
7864
7865 begin
7866 if Ekind (Parent_Type) = E_Record_Type_With_Private
7867 and then Present (Full_View (Parent_Type))
7868 and then Has_Discriminants (Parent_Type)
7869 then
7870 Parent_Base := Base_Type (Full_View (Parent_Type));
7871 else
7872 Parent_Base := Base_Type (Parent_Type);
7873 end if;
7874
7875 -- AI05-0115 : if this is a derivation from a private type in some
7876 -- other scope that may lead to invisible components for the derived
7877 -- type, mark it accordingly.
7878
7879 if Is_Private_Type (Parent_Type) then
7880 if Scope (Parent_Type) = Scope (Derived_Type) then
7881 null;
7882
7883 elsif In_Open_Scopes (Scope (Parent_Type))
7884 and then In_Private_Part (Scope (Parent_Type))
7885 then
7886 null;
7887
7888 else
7889 Set_Has_Private_Ancestor (Derived_Type);
7890 end if;
7891
7892 else
7893 Set_Has_Private_Ancestor
7894 (Derived_Type, Has_Private_Ancestor (Parent_Type));
7895 end if;
7896
7897 -- Before we start the previously documented transformations, here is
7898 -- little fix for size and alignment of tagged types. Normally when we
7899 -- derive type D from type P, we copy the size and alignment of P as the
7900 -- default for D, and in the absence of explicit representation clauses
7901 -- for D, the size and alignment are indeed the same as the parent.
7902
7903 -- But this is wrong for tagged types, since fields may be added, and
7904 -- the default size may need to be larger, and the default alignment may
7905 -- need to be larger.
7906
7907 -- We therefore reset the size and alignment fields in the tagged case.
7908 -- Note that the size and alignment will in any case be at least as
7909 -- large as the parent type (since the derived type has a copy of the
7910 -- parent type in the _parent field)
7911
7912 -- The type is also marked as being tagged here, which is needed when
7913 -- processing components with a self-referential anonymous access type
7914 -- in the call to Check_Anonymous_Access_Components below. Note that
7915 -- this flag is also set later on for completeness.
7916
7917 if Is_Tagged then
7918 Set_Is_Tagged_Type (Derived_Type);
7919 Init_Size_Align (Derived_Type);
7920 end if;
7921
7922 -- STEP 0a: figure out what kind of derived type declaration we have
7923
7924 if Private_Extension then
7925 Type_Def := N;
7926 Set_Ekind (Derived_Type, E_Record_Type_With_Private);
7927 Set_Default_SSO (Derived_Type);
7928
7929 else
7930 Type_Def := Type_Definition (N);
7931
7932 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
7933 -- Parent_Base can be a private type or private extension. However,
7934 -- for tagged types with an extension the newly added fields are
7935 -- visible and hence the Derived_Type is always an E_Record_Type.
7936 -- (except that the parent may have its own private fields).
7937 -- For untagged types we preserve the Ekind of the Parent_Base.
7938
7939 if Present (Record_Extension_Part (Type_Def)) then
7940 Set_Ekind (Derived_Type, E_Record_Type);
7941 Set_Default_SSO (Derived_Type);
7942
7943 -- Create internal access types for components with anonymous
7944 -- access types.
7945
7946 if Ada_Version >= Ada_2005 then
7947 Check_Anonymous_Access_Components
7948 (N, Derived_Type, Derived_Type,
7949 Component_List (Record_Extension_Part (Type_Def)));
7950 end if;
7951
7952 else
7953 Set_Ekind (Derived_Type, Ekind (Parent_Base));
7954 end if;
7955 end if;
7956
7957 -- Indic can either be an N_Identifier if the subtype indication
7958 -- contains no constraint or an N_Subtype_Indication if the subtype
7959 -- indication has a constraint.
7960
7961 Indic := Subtype_Indication (Type_Def);
7962 Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
7963
7964 -- Check that the type has visible discriminants. The type may be
7965 -- a private type with unknown discriminants whose full view has
7966 -- discriminants which are invisible.
7967
7968 if Constraint_Present then
7969 if not Has_Discriminants (Parent_Base)
7970 or else
7971 (Has_Unknown_Discriminants (Parent_Base)
7972 and then Is_Private_Type (Parent_Base))
7973 then
7974 Error_Msg_N
7975 ("invalid constraint: type has no discriminant",
7976 Constraint (Indic));
7977
7978 Constraint_Present := False;
7979 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
7980
7981 elsif Is_Constrained (Parent_Type) then
7982 Error_Msg_N
7983 ("invalid constraint: parent type is already constrained",
7984 Constraint (Indic));
7985
7986 Constraint_Present := False;
7987 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
7988 end if;
7989 end if;
7990
7991 -- STEP 0b: If needed, apply transformation given in point 5. above
7992
7993 if not Private_Extension
7994 and then Has_Discriminants (Parent_Type)
7995 and then not Discriminant_Specs
7996 and then (Is_Constrained (Parent_Type) or else Constraint_Present)
7997 then
7998 -- First, we must analyze the constraint (see comment in point 5.)
7999 -- The constraint may come from the subtype indication of the full
8000 -- declaration.
8001
8002 if Constraint_Present then
8003 New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
8004
8005 -- If there is no explicit constraint, there might be one that is
8006 -- inherited from a constrained parent type. In that case verify that
8007 -- it conforms to the constraint in the partial view. In perverse
8008 -- cases the parent subtypes of the partial and full view can have
8009 -- different constraints.
8010
8011 elsif Present (Stored_Constraint (Parent_Type)) then
8012 New_Discrs := Stored_Constraint (Parent_Type);
8013
8014 else
8015 New_Discrs := No_Elist;
8016 end if;
8017
8018 if Has_Discriminants (Derived_Type)
8019 and then Has_Private_Declaration (Derived_Type)
8020 and then Present (Discriminant_Constraint (Derived_Type))
8021 and then Present (New_Discrs)
8022 then
8023 -- Verify that constraints of the full view statically match
8024 -- those given in the partial view.
8025
8026 declare
8027 C1, C2 : Elmt_Id;
8028
8029 begin
8030 C1 := First_Elmt (New_Discrs);
8031 C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
8032 while Present (C1) and then Present (C2) loop
8033 if Fully_Conformant_Expressions (Node (C1), Node (C2))
8034 or else
8035 (Is_OK_Static_Expression (Node (C1))
8036 and then Is_OK_Static_Expression (Node (C2))
8037 and then
8038 Expr_Value (Node (C1)) = Expr_Value (Node (C2)))
8039 then
8040 null;
8041
8042 else
8043 if Constraint_Present then
8044 Error_Msg_N
8045 ("constraint not conformant to previous declaration",
8046 Node (C1));
8047 else
8048 Error_Msg_N
8049 ("constraint of full view is incompatible "
8050 & "with partial view", N);
8051 end if;
8052 end if;
8053
8054 Next_Elmt (C1);
8055 Next_Elmt (C2);
8056 end loop;
8057 end;
8058 end if;
8059
8060 -- Insert and analyze the declaration for the unconstrained base type
8061
8062 New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
8063
8064 New_Decl :=
8065 Make_Full_Type_Declaration (Loc,
8066 Defining_Identifier => New_Base,
8067 Type_Definition =>
8068 Make_Derived_Type_Definition (Loc,
8069 Abstract_Present => Abstract_Present (Type_Def),
8070 Limited_Present => Limited_Present (Type_Def),
8071 Subtype_Indication =>
8072 New_Occurrence_Of (Parent_Base, Loc),
8073 Record_Extension_Part =>
8074 Relocate_Node (Record_Extension_Part (Type_Def)),
8075 Interface_List => Interface_List (Type_Def)));
8076
8077 Set_Parent (New_Decl, Parent (N));
8078 Mark_Rewrite_Insertion (New_Decl);
8079 Insert_Before (N, New_Decl);
8080
8081 -- In the extension case, make sure ancestor is frozen appropriately
8082 -- (see also non-discriminated case below).
8083
8084 if Present (Record_Extension_Part (Type_Def))
8085 or else Is_Interface (Parent_Base)
8086 then
8087 Freeze_Before (New_Decl, Parent_Type);
8088 end if;
8089
8090 -- Note that this call passes False for the Derive_Subps parameter
8091 -- because subprogram derivation is deferred until after creating
8092 -- the subtype (see below).
8093
8094 Build_Derived_Type
8095 (New_Decl, Parent_Base, New_Base,
8096 Is_Completion => False, Derive_Subps => False);
8097
8098 -- ??? This needs re-examination to determine whether the
8099 -- above call can simply be replaced by a call to Analyze.
8100
8101 Set_Analyzed (New_Decl);
8102
8103 -- Insert and analyze the declaration for the constrained subtype
8104
8105 if Constraint_Present then
8106 New_Indic :=
8107 Make_Subtype_Indication (Loc,
8108 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
8109 Constraint => Relocate_Node (Constraint (Indic)));
8110
8111 else
8112 declare
8113 Constr_List : constant List_Id := New_List;
8114 C : Elmt_Id;
8115 Expr : Node_Id;
8116
8117 begin
8118 C := First_Elmt (Discriminant_Constraint (Parent_Type));
8119 while Present (C) loop
8120 Expr := Node (C);
8121
8122 -- It is safe here to call New_Copy_Tree since we called
8123 -- Force_Evaluation on each constraint previously
8124 -- in Build_Discriminant_Constraints.
8125
8126 Append (New_Copy_Tree (Expr), To => Constr_List);
8127
8128 Next_Elmt (C);
8129 end loop;
8130
8131 New_Indic :=
8132 Make_Subtype_Indication (Loc,
8133 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
8134 Constraint =>
8135 Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
8136 end;
8137 end if;
8138
8139 Rewrite (N,
8140 Make_Subtype_Declaration (Loc,
8141 Defining_Identifier => Derived_Type,
8142 Subtype_Indication => New_Indic));
8143
8144 Analyze (N);
8145
8146 -- Derivation of subprograms must be delayed until the full subtype
8147 -- has been established, to ensure proper overriding of subprograms
8148 -- inherited by full types. If the derivations occurred as part of
8149 -- the call to Build_Derived_Type above, then the check for type
8150 -- conformance would fail because earlier primitive subprograms
8151 -- could still refer to the full type prior the change to the new
8152 -- subtype and hence would not match the new base type created here.
8153 -- Subprograms are not derived, however, when Derive_Subps is False
8154 -- (since otherwise there could be redundant derivations).
8155
8156 if Derive_Subps then
8157 Derive_Subprograms (Parent_Type, Derived_Type);
8158 end if;
8159
8160 -- For tagged types the Discriminant_Constraint of the new base itype
8161 -- is inherited from the first subtype so that no subtype conformance
8162 -- problem arise when the first subtype overrides primitive
8163 -- operations inherited by the implicit base type.
8164
8165 if Is_Tagged then
8166 Set_Discriminant_Constraint
8167 (New_Base, Discriminant_Constraint (Derived_Type));
8168 end if;
8169
8170 return;
8171 end if;
8172
8173 -- If we get here Derived_Type will have no discriminants or it will be
8174 -- a discriminated unconstrained base type.
8175
8176 -- STEP 1a: perform preliminary actions/checks for derived tagged types
8177
8178 if Is_Tagged then
8179
8180 -- The parent type is frozen for non-private extensions (RM 13.14(7))
8181 -- The declaration of a specific descendant of an interface type
8182 -- freezes the interface type (RM 13.14).
8183
8184 if not Private_Extension or else Is_Interface (Parent_Base) then
8185 Freeze_Before (N, Parent_Type);
8186 end if;
8187
8188 -- In Ada 2005 (AI-344), the restriction that a derived tagged type
8189 -- cannot be declared at a deeper level than its parent type is
8190 -- removed. The check on derivation within a generic body is also
8191 -- relaxed, but there's a restriction that a derived tagged type
8192 -- cannot be declared in a generic body if it's derived directly
8193 -- or indirectly from a formal type of that generic.
8194
8195 if Ada_Version >= Ada_2005 then
8196 if Present (Enclosing_Generic_Body (Derived_Type)) then
8197 declare
8198 Ancestor_Type : Entity_Id;
8199
8200 begin
8201 -- Check to see if any ancestor of the derived type is a
8202 -- formal type.
8203
8204 Ancestor_Type := Parent_Type;
8205 while not Is_Generic_Type (Ancestor_Type)
8206 and then Etype (Ancestor_Type) /= Ancestor_Type
8207 loop
8208 Ancestor_Type := Etype (Ancestor_Type);
8209 end loop;
8210
8211 -- If the derived type does have a formal type as an
8212 -- ancestor, then it's an error if the derived type is
8213 -- declared within the body of the generic unit that
8214 -- declares the formal type in its generic formal part. It's
8215 -- sufficient to check whether the ancestor type is declared
8216 -- inside the same generic body as the derived type (such as
8217 -- within a nested generic spec), in which case the
8218 -- derivation is legal. If the formal type is declared
8219 -- outside of that generic body, then it's guaranteed that
8220 -- the derived type is declared within the generic body of
8221 -- the generic unit declaring the formal type.
8222
8223 if Is_Generic_Type (Ancestor_Type)
8224 and then Enclosing_Generic_Body (Ancestor_Type) /=
8225 Enclosing_Generic_Body (Derived_Type)
8226 then
8227 Error_Msg_NE
8228 ("parent type of& must not be descendant of formal type"
8229 & " of an enclosing generic body",
8230 Indic, Derived_Type);
8231 end if;
8232 end;
8233 end if;
8234
8235 elsif Type_Access_Level (Derived_Type) /=
8236 Type_Access_Level (Parent_Type)
8237 and then not Is_Generic_Type (Derived_Type)
8238 then
8239 if Is_Controlled (Parent_Type) then
8240 Error_Msg_N
8241 ("controlled type must be declared at the library level",
8242 Indic);
8243 else
8244 Error_Msg_N
8245 ("type extension at deeper accessibility level than parent",
8246 Indic);
8247 end if;
8248
8249 else
8250 declare
8251 GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
8252 begin
8253 if Present (GB)
8254 and then GB /= Enclosing_Generic_Body (Parent_Base)
8255 then
8256 Error_Msg_NE
8257 ("parent type of& must not be outside generic body"
8258 & " (RM 3.9.1(4))",
8259 Indic, Derived_Type);
8260 end if;
8261 end;
8262 end if;
8263 end if;
8264
8265 -- Ada 2005 (AI-251)
8266
8267 if Ada_Version >= Ada_2005 and then Is_Tagged then
8268
8269 -- "The declaration of a specific descendant of an interface type
8270 -- freezes the interface type" (RM 13.14).
8271
8272 declare
8273 Iface : Node_Id;
8274 begin
8275 if Is_Non_Empty_List (Interface_List (Type_Def)) then
8276 Iface := First (Interface_List (Type_Def));
8277 while Present (Iface) loop
8278 Freeze_Before (N, Etype (Iface));
8279 Next (Iface);
8280 end loop;
8281 end if;
8282 end;
8283 end if;
8284
8285 -- STEP 1b : preliminary cleanup of the full view of private types
8286
8287 -- If the type is already marked as having discriminants, then it's the
8288 -- completion of a private type or private extension and we need to
8289 -- retain the discriminants from the partial view if the current
8290 -- declaration has Discriminant_Specifications so that we can verify
8291 -- conformance. However, we must remove any existing components that
8292 -- were inherited from the parent (and attached in Copy_And_Swap)
8293 -- because the full type inherits all appropriate components anyway, and
8294 -- we do not want the partial view's components interfering.
8295
8296 if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
8297 Discrim := First_Discriminant (Derived_Type);
8298 loop
8299 Last_Discrim := Discrim;
8300 Next_Discriminant (Discrim);
8301 exit when No (Discrim);
8302 end loop;
8303
8304 Set_Last_Entity (Derived_Type, Last_Discrim);
8305
8306 -- In all other cases wipe out the list of inherited components (even
8307 -- inherited discriminants), it will be properly rebuilt here.
8308
8309 else
8310 Set_First_Entity (Derived_Type, Empty);
8311 Set_Last_Entity (Derived_Type, Empty);
8312 end if;
8313
8314 -- STEP 1c: Initialize some flags for the Derived_Type
8315
8316 -- The following flags must be initialized here so that
8317 -- Process_Discriminants can check that discriminants of tagged types do
8318 -- not have a default initial value and that access discriminants are
8319 -- only specified for limited records. For completeness, these flags are
8320 -- also initialized along with all the other flags below.
8321
8322 -- AI-419: Limitedness is not inherited from an interface parent, so to
8323 -- be limited in that case the type must be explicitly declared as
8324 -- limited. However, task and protected interfaces are always limited.
8325
8326 if Limited_Present (Type_Def) then
8327 Set_Is_Limited_Record (Derived_Type);
8328
8329 elsif Is_Limited_Record (Parent_Type)
8330 or else (Present (Full_View (Parent_Type))
8331 and then Is_Limited_Record (Full_View (Parent_Type)))
8332 then
8333 if not Is_Interface (Parent_Type)
8334 or else Is_Synchronized_Interface (Parent_Type)
8335 or else Is_Protected_Interface (Parent_Type)
8336 or else Is_Task_Interface (Parent_Type)
8337 then
8338 Set_Is_Limited_Record (Derived_Type);
8339 end if;
8340 end if;
8341
8342 -- STEP 2a: process discriminants of derived type if any
8343
8344 Push_Scope (Derived_Type);
8345
8346 if Discriminant_Specs then
8347 Set_Has_Unknown_Discriminants (Derived_Type, False);
8348
8349 -- The following call initializes fields Has_Discriminants and
8350 -- Discriminant_Constraint, unless we are processing the completion
8351 -- of a private type declaration.
8352
8353 Check_Or_Process_Discriminants (N, Derived_Type);
8354
8355 -- For untagged types, the constraint on the Parent_Type must be
8356 -- present and is used to rename the discriminants.
8357
8358 if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
8359 Error_Msg_N ("untagged parent must have discriminants", Indic);
8360
8361 elsif not Is_Tagged and then not Constraint_Present then
8362 Error_Msg_N
8363 ("discriminant constraint needed for derived untagged records",
8364 Indic);
8365
8366 -- Otherwise the parent subtype must be constrained unless we have a
8367 -- private extension.
8368
8369 elsif not Constraint_Present
8370 and then not Private_Extension
8371 and then not Is_Constrained (Parent_Type)
8372 then
8373 Error_Msg_N
8374 ("unconstrained type not allowed in this context", Indic);
8375
8376 elsif Constraint_Present then
8377 -- The following call sets the field Corresponding_Discriminant
8378 -- for the discriminants in the Derived_Type.
8379
8380 Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
8381
8382 -- For untagged types all new discriminants must rename
8383 -- discriminants in the parent. For private extensions new
8384 -- discriminants cannot rename old ones (implied by [7.3(13)]).
8385
8386 Discrim := First_Discriminant (Derived_Type);
8387 while Present (Discrim) loop
8388 if not Is_Tagged
8389 and then No (Corresponding_Discriminant (Discrim))
8390 then
8391 Error_Msg_N
8392 ("new discriminants must constrain old ones", Discrim);
8393
8394 elsif Private_Extension
8395 and then Present (Corresponding_Discriminant (Discrim))
8396 then
8397 Error_Msg_N
8398 ("only static constraints allowed for parent"
8399 & " discriminants in the partial view", Indic);
8400 exit;
8401 end if;
8402
8403 -- If a new discriminant is used in the constraint, then its
8404 -- subtype must be statically compatible with the parent
8405 -- discriminant's subtype (3.7(15)).
8406
8407 -- However, if the record contains an array constrained by
8408 -- the discriminant but with some different bound, the compiler
8409 -- attemps to create a smaller range for the discriminant type.
8410 -- (See exp_ch3.Adjust_Discriminants). In this case, where
8411 -- the discriminant type is a scalar type, the check must use
8412 -- the original discriminant type in the parent declaration.
8413
8414 declare
8415 Corr_Disc : constant Entity_Id :=
8416 Corresponding_Discriminant (Discrim);
8417 Disc_Type : constant Entity_Id := Etype (Discrim);
8418 Corr_Type : Entity_Id;
8419
8420 begin
8421 if Present (Corr_Disc) then
8422 if Is_Scalar_Type (Disc_Type) then
8423 Corr_Type :=
8424 Entity (Discriminant_Type (Parent (Corr_Disc)));
8425 else
8426 Corr_Type := Etype (Corr_Disc);
8427 end if;
8428
8429 if not
8430 Subtypes_Statically_Compatible (Disc_Type, Corr_Type)
8431 then
8432 Error_Msg_N
8433 ("subtype must be compatible "
8434 & "with parent discriminant",
8435 Discrim);
8436 end if;
8437 end if;
8438 end;
8439
8440 Next_Discriminant (Discrim);
8441 end loop;
8442
8443 -- Check whether the constraints of the full view statically
8444 -- match those imposed by the parent subtype [7.3(13)].
8445
8446 if Present (Stored_Constraint (Derived_Type)) then
8447 declare
8448 C1, C2 : Elmt_Id;
8449
8450 begin
8451 C1 := First_Elmt (Discs);
8452 C2 := First_Elmt (Stored_Constraint (Derived_Type));
8453 while Present (C1) and then Present (C2) loop
8454 if not
8455 Fully_Conformant_Expressions (Node (C1), Node (C2))
8456 then
8457 Error_Msg_N
8458 ("not conformant with previous declaration",
8459 Node (C1));
8460 end if;
8461
8462 Next_Elmt (C1);
8463 Next_Elmt (C2);
8464 end loop;
8465 end;
8466 end if;
8467 end if;
8468
8469 -- STEP 2b: No new discriminants, inherit discriminants if any
8470
8471 else
8472 if Private_Extension then
8473 Set_Has_Unknown_Discriminants
8474 (Derived_Type,
8475 Has_Unknown_Discriminants (Parent_Type)
8476 or else Unknown_Discriminants_Present (N));
8477
8478 -- The partial view of the parent may have unknown discriminants,
8479 -- but if the full view has discriminants and the parent type is
8480 -- in scope they must be inherited.
8481
8482 elsif Has_Unknown_Discriminants (Parent_Type)
8483 and then
8484 (not Has_Discriminants (Parent_Type)
8485 or else not In_Open_Scopes (Scope (Parent_Type)))
8486 then
8487 Set_Has_Unknown_Discriminants (Derived_Type);
8488 end if;
8489
8490 if not Has_Unknown_Discriminants (Derived_Type)
8491 and then not Has_Unknown_Discriminants (Parent_Base)
8492 and then Has_Discriminants (Parent_Type)
8493 then
8494 Inherit_Discrims := True;
8495 Set_Has_Discriminants
8496 (Derived_Type, True);
8497 Set_Discriminant_Constraint
8498 (Derived_Type, Discriminant_Constraint (Parent_Base));
8499 end if;
8500
8501 -- The following test is true for private types (remember
8502 -- transformation 5. is not applied to those) and in an error
8503 -- situation.
8504
8505 if Constraint_Present then
8506 Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
8507 end if;
8508
8509 -- For now mark a new derived type as constrained only if it has no
8510 -- discriminants. At the end of Build_Derived_Record_Type we properly
8511 -- set this flag in the case of private extensions. See comments in
8512 -- point 9. just before body of Build_Derived_Record_Type.
8513
8514 Set_Is_Constrained
8515 (Derived_Type,
8516 not (Inherit_Discrims
8517 or else Has_Unknown_Discriminants (Derived_Type)));
8518 end if;
8519
8520 -- STEP 3: initialize fields of derived type
8521
8522 Set_Is_Tagged_Type (Derived_Type, Is_Tagged);
8523 Set_Stored_Constraint (Derived_Type, No_Elist);
8524
8525 -- Ada 2005 (AI-251): Private type-declarations can implement interfaces
8526 -- but cannot be interfaces
8527
8528 if not Private_Extension
8529 and then Ekind (Derived_Type) /= E_Private_Type
8530 and then Ekind (Derived_Type) /= E_Limited_Private_Type
8531 then
8532 if Interface_Present (Type_Def) then
8533 Analyze_Interface_Declaration (Derived_Type, Type_Def);
8534 end if;
8535
8536 Set_Interfaces (Derived_Type, No_Elist);
8537 end if;
8538
8539 -- Fields inherited from the Parent_Type
8540
8541 Set_Has_Specified_Layout
8542 (Derived_Type, Has_Specified_Layout (Parent_Type));
8543 Set_Is_Limited_Composite
8544 (Derived_Type, Is_Limited_Composite (Parent_Type));
8545 Set_Is_Private_Composite
8546 (Derived_Type, Is_Private_Composite (Parent_Type));
8547
8548 if Is_Tagged_Type (Parent_Type) then
8549 Set_No_Tagged_Streams_Pragma
8550 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
8551 end if;
8552
8553 -- Fields inherited from the Parent_Base
8554
8555 Set_Has_Controlled_Component
8556 (Derived_Type, Has_Controlled_Component (Parent_Base));
8557 Set_Has_Non_Standard_Rep
8558 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
8559 Set_Has_Primitive_Operations
8560 (Derived_Type, Has_Primitive_Operations (Parent_Base));
8561
8562 -- Fields inherited from the Parent_Base in the non-private case
8563
8564 if Ekind (Derived_Type) = E_Record_Type then
8565 Set_Has_Complex_Representation
8566 (Derived_Type, Has_Complex_Representation (Parent_Base));
8567 end if;
8568
8569 -- Fields inherited from the Parent_Base for record types
8570
8571 if Is_Record_Type (Derived_Type) then
8572 declare
8573 Parent_Full : Entity_Id;
8574
8575 begin
8576 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
8577 -- Parent_Base can be a private type or private extension. Go
8578 -- to the full view here to get the E_Record_Type specific flags.
8579
8580 if Present (Full_View (Parent_Base)) then
8581 Parent_Full := Full_View (Parent_Base);
8582 else
8583 Parent_Full := Parent_Base;
8584 end if;
8585
8586 Set_OK_To_Reorder_Components
8587 (Derived_Type, OK_To_Reorder_Components (Parent_Full));
8588 end;
8589 end if;
8590
8591 -- Set fields for private derived types
8592
8593 if Is_Private_Type (Derived_Type) then
8594 Set_Depends_On_Private (Derived_Type, True);
8595 Set_Private_Dependents (Derived_Type, New_Elmt_List);
8596
8597 -- Inherit fields from non private record types. If this is the
8598 -- completion of a derivation from a private type, the parent itself
8599 -- is private, and the attributes come from its full view, which must
8600 -- be present.
8601
8602 else
8603 if Is_Private_Type (Parent_Base)
8604 and then not Is_Record_Type (Parent_Base)
8605 then
8606 Set_Component_Alignment
8607 (Derived_Type, Component_Alignment (Full_View (Parent_Base)));
8608 Set_C_Pass_By_Copy
8609 (Derived_Type, C_Pass_By_Copy (Full_View (Parent_Base)));
8610 else
8611 Set_Component_Alignment
8612 (Derived_Type, Component_Alignment (Parent_Base));
8613 Set_C_Pass_By_Copy
8614 (Derived_Type, C_Pass_By_Copy (Parent_Base));
8615 end if;
8616 end if;
8617
8618 -- Set fields for tagged types
8619
8620 if Is_Tagged then
8621 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
8622
8623 -- All tagged types defined in Ada.Finalization are controlled
8624
8625 if Chars (Scope (Derived_Type)) = Name_Finalization
8626 and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
8627 and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
8628 then
8629 Set_Is_Controlled (Derived_Type);
8630 else
8631 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Base));
8632 end if;
8633
8634 -- Minor optimization: there is no need to generate the class-wide
8635 -- entity associated with an underlying record view.
8636
8637 if not Is_Underlying_Record_View (Derived_Type) then
8638 Make_Class_Wide_Type (Derived_Type);
8639 end if;
8640
8641 Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
8642
8643 if Has_Discriminants (Derived_Type)
8644 and then Constraint_Present
8645 then
8646 Set_Stored_Constraint
8647 (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
8648 end if;
8649
8650 if Ada_Version >= Ada_2005 then
8651 declare
8652 Ifaces_List : Elist_Id;
8653
8654 begin
8655 -- Checks rules 3.9.4 (13/2 and 14/2)
8656
8657 if Comes_From_Source (Derived_Type)
8658 and then not Is_Private_Type (Derived_Type)
8659 and then Is_Interface (Parent_Type)
8660 and then not Is_Interface (Derived_Type)
8661 then
8662 if Is_Task_Interface (Parent_Type) then
8663 Error_Msg_N
8664 ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
8665 Derived_Type);
8666
8667 elsif Is_Protected_Interface (Parent_Type) then
8668 Error_Msg_N
8669 ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
8670 Derived_Type);
8671 end if;
8672 end if;
8673
8674 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
8675
8676 Check_Interfaces (N, Type_Def);
8677
8678 -- Ada 2005 (AI-251): Collect the list of progenitors that are
8679 -- not already in the parents.
8680
8681 Collect_Interfaces
8682 (T => Derived_Type,
8683 Ifaces_List => Ifaces_List,
8684 Exclude_Parents => True);
8685
8686 Set_Interfaces (Derived_Type, Ifaces_List);
8687
8688 -- If the derived type is the anonymous type created for
8689 -- a declaration whose parent has a constraint, propagate
8690 -- the interface list to the source type. This must be done
8691 -- prior to the completion of the analysis of the source type
8692 -- because the components in the extension may contain current
8693 -- instances whose legality depends on some ancestor.
8694
8695 if Is_Itype (Derived_Type) then
8696 declare
8697 Def : constant Node_Id :=
8698 Associated_Node_For_Itype (Derived_Type);
8699 begin
8700 if Present (Def)
8701 and then Nkind (Def) = N_Full_Type_Declaration
8702 then
8703 Set_Interfaces
8704 (Defining_Identifier (Def), Ifaces_List);
8705 end if;
8706 end;
8707 end if;
8708
8709 -- Propagate inherited invariant information of parents
8710 -- and progenitors
8711
8712 if Ada_Version >= Ada_2012
8713 and then not Is_Interface (Derived_Type)
8714 then
8715 if Has_Inheritable_Invariants (Parent_Type) then
8716 Set_Has_Invariants (Derived_Type);
8717 Set_Has_Inheritable_Invariants (Derived_Type);
8718
8719 elsif not Is_Empty_Elmt_List (Ifaces_List) then
8720 declare
8721 AI : Elmt_Id;
8722
8723 begin
8724 AI := First_Elmt (Ifaces_List);
8725 while Present (AI) loop
8726 if Has_Inheritable_Invariants (Node (AI)) then
8727 Set_Has_Invariants (Derived_Type);
8728 Set_Has_Inheritable_Invariants (Derived_Type);
8729
8730 exit;
8731 end if;
8732
8733 Next_Elmt (AI);
8734 end loop;
8735 end;
8736 end if;
8737 end if;
8738
8739 -- A type extension is automatically Ghost when one of its
8740 -- progenitors is Ghost (SPARK RM 6.9(9)). This property is
8741 -- also inherited when the parent type is Ghost, but this is
8742 -- done in Build_Derived_Type as the mechanism also handles
8743 -- untagged derivations.
8744
8745 if Implements_Ghost_Interface (Derived_Type) then
8746 Set_Is_Ghost_Entity (Derived_Type);
8747 end if;
8748 end;
8749 end if;
8750
8751 else
8752 Set_Is_Packed (Derived_Type, Is_Packed (Parent_Base));
8753 Set_Has_Non_Standard_Rep
8754 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
8755 end if;
8756
8757 -- STEP 4: Inherit components from the parent base and constrain them.
8758 -- Apply the second transformation described in point 6. above.
8759
8760 if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
8761 or else not Has_Discriminants (Parent_Type)
8762 or else not Is_Constrained (Parent_Type)
8763 then
8764 Constrs := Discs;
8765 else
8766 Constrs := Discriminant_Constraint (Parent_Type);
8767 end if;
8768
8769 Assoc_List :=
8770 Inherit_Components
8771 (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
8772
8773 -- STEP 5a: Copy the parent record declaration for untagged types
8774
8775 if not Is_Tagged then
8776
8777 -- Discriminant_Constraint (Derived_Type) has been properly
8778 -- constructed. Save it and temporarily set it to Empty because we
8779 -- do not want the call to New_Copy_Tree below to mess this list.
8780
8781 if Has_Discriminants (Derived_Type) then
8782 Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
8783 Set_Discriminant_Constraint (Derived_Type, No_Elist);
8784 else
8785 Save_Discr_Constr := No_Elist;
8786 end if;
8787
8788 -- Save the Etype field of Derived_Type. It is correctly set now,
8789 -- but the call to New_Copy tree may remap it to point to itself,
8790 -- which is not what we want. Ditto for the Next_Entity field.
8791
8792 Save_Etype := Etype (Derived_Type);
8793 Save_Next_Entity := Next_Entity (Derived_Type);
8794
8795 -- Assoc_List maps all stored discriminants in the Parent_Base to
8796 -- stored discriminants in the Derived_Type. It is fundamental that
8797 -- no types or itypes with discriminants other than the stored
8798 -- discriminants appear in the entities declared inside
8799 -- Derived_Type, since the back end cannot deal with it.
8800
8801 New_Decl :=
8802 New_Copy_Tree
8803 (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
8804
8805 -- Restore the fields saved prior to the New_Copy_Tree call
8806 -- and compute the stored constraint.
8807
8808 Set_Etype (Derived_Type, Save_Etype);
8809 Set_Next_Entity (Derived_Type, Save_Next_Entity);
8810
8811 if Has_Discriminants (Derived_Type) then
8812 Set_Discriminant_Constraint
8813 (Derived_Type, Save_Discr_Constr);
8814 Set_Stored_Constraint
8815 (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
8816 Replace_Components (Derived_Type, New_Decl);
8817 Set_Has_Implicit_Dereference
8818 (Derived_Type, Has_Implicit_Dereference (Parent_Type));
8819 end if;
8820
8821 -- Insert the new derived type declaration
8822
8823 Rewrite (N, New_Decl);
8824
8825 -- STEP 5b: Complete the processing for record extensions in generics
8826
8827 -- There is no completion for record extensions declared in the
8828 -- parameter part of a generic, so we need to complete processing for
8829 -- these generic record extensions here. The Record_Type_Definition call
8830 -- will change the Ekind of the components from E_Void to E_Component.
8831
8832 elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
8833 Record_Type_Definition (Empty, Derived_Type);
8834
8835 -- STEP 5c: Process the record extension for non private tagged types
8836
8837 elsif not Private_Extension then
8838 Expand_Record_Extension (Derived_Type, Type_Def);
8839
8840 -- Note : previously in ASIS mode we set the Parent_Subtype of the
8841 -- derived type to propagate some semantic information. This led
8842 -- to other ASIS failures and has been removed.
8843
8844 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
8845 -- implemented interfaces if we are in expansion mode
8846
8847 if Expander_Active
8848 and then Has_Interfaces (Derived_Type)
8849 then
8850 Add_Interface_Tag_Components (N, Derived_Type);
8851 end if;
8852
8853 -- Analyze the record extension
8854
8855 Record_Type_Definition
8856 (Record_Extension_Part (Type_Def), Derived_Type);
8857 end if;
8858
8859 End_Scope;
8860
8861 -- Nothing else to do if there is an error in the derivation.
8862 -- An unusual case: the full view may be derived from a type in an
8863 -- instance, when the partial view was used illegally as an actual
8864 -- in that instance, leading to a circular definition.
8865
8866 if Etype (Derived_Type) = Any_Type
8867 or else Etype (Parent_Type) = Derived_Type
8868 then
8869 return;
8870 end if;
8871
8872 -- Set delayed freeze and then derive subprograms, we need to do
8873 -- this in this order so that derived subprograms inherit the
8874 -- derived freeze if necessary.
8875
8876 Set_Has_Delayed_Freeze (Derived_Type);
8877
8878 if Derive_Subps then
8879 Derive_Subprograms (Parent_Type, Derived_Type);
8880 end if;
8881
8882 -- If we have a private extension which defines a constrained derived
8883 -- type mark as constrained here after we have derived subprograms. See
8884 -- comment on point 9. just above the body of Build_Derived_Record_Type.
8885
8886 if Private_Extension and then Inherit_Discrims then
8887 if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
8888 Set_Is_Constrained (Derived_Type, True);
8889 Set_Discriminant_Constraint (Derived_Type, Discs);
8890
8891 elsif Is_Constrained (Parent_Type) then
8892 Set_Is_Constrained
8893 (Derived_Type, True);
8894 Set_Discriminant_Constraint
8895 (Derived_Type, Discriminant_Constraint (Parent_Type));
8896 end if;
8897 end if;
8898
8899 -- Update the class-wide type, which shares the now-completed entity
8900 -- list with its specific type. In case of underlying record views,
8901 -- we do not generate the corresponding class wide entity.
8902
8903 if Is_Tagged
8904 and then not Is_Underlying_Record_View (Derived_Type)
8905 then
8906 Set_First_Entity
8907 (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
8908 Set_Last_Entity
8909 (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
8910 end if;
8911
8912 Check_Function_Writable_Actuals (N);
8913 end Build_Derived_Record_Type;
8914
8915 ------------------------
8916 -- Build_Derived_Type --
8917 ------------------------
8918
8919 procedure Build_Derived_Type
8920 (N : Node_Id;
8921 Parent_Type : Entity_Id;
8922 Derived_Type : Entity_Id;
8923 Is_Completion : Boolean;
8924 Derive_Subps : Boolean := True)
8925 is
8926 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
8927
8928 begin
8929 -- Set common attributes
8930
8931 Set_Scope (Derived_Type, Current_Scope);
8932
8933 Set_Etype (Derived_Type, Parent_Base);
8934 Set_Ekind (Derived_Type, Ekind (Parent_Base));
8935 Set_Has_Task (Derived_Type, Has_Task (Parent_Base));
8936 Set_Has_Protected (Derived_Type, Has_Protected (Parent_Base));
8937
8938 Set_Size_Info (Derived_Type, Parent_Type);
8939 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
8940 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
8941 Set_Is_Tagged_Type (Derived_Type, Is_Tagged_Type (Parent_Type));
8942 Set_Is_Volatile (Derived_Type, Is_Volatile (Parent_Type));
8943
8944 if Is_Tagged_Type (Derived_Type) then
8945 Set_No_Tagged_Streams_Pragma
8946 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
8947 end if;
8948
8949 -- If the parent has primitive routines, set the derived type link
8950
8951 if Has_Primitive_Operations (Parent_Type) then
8952 Set_Derived_Type_Link (Parent_Base, Derived_Type);
8953 end if;
8954
8955 -- If the parent type is a private subtype, the convention on the base
8956 -- type may be set in the private part, and not propagated to the
8957 -- subtype until later, so we obtain the convention from the base type.
8958
8959 Set_Convention (Derived_Type, Convention (Parent_Base));
8960
8961 -- Set SSO default for record or array type
8962
8963 if (Is_Array_Type (Derived_Type) or else Is_Record_Type (Derived_Type))
8964 and then Is_Base_Type (Derived_Type)
8965 then
8966 Set_Default_SSO (Derived_Type);
8967 end if;
8968
8969 -- Propagate invariant information. The new type has invariants if
8970 -- they are inherited from the parent type, and these invariants can
8971 -- be further inherited, so both flags are set.
8972
8973 -- We similarly inherit predicates
8974
8975 if Has_Predicates (Parent_Type) then
8976 Set_Has_Predicates (Derived_Type);
8977 end if;
8978
8979 -- The derived type inherits the representation clauses of the parent
8980
8981 Inherit_Rep_Item_Chain (Derived_Type, Parent_Type);
8982
8983 -- Propagate the attributes related to pragma Default_Initial_Condition
8984 -- from the parent type to the private extension. A derived type always
8985 -- inherits the default initial condition flag from the parent type. If
8986 -- the derived type carries its own Default_Initial_Condition pragma,
8987 -- the flag is later reset in Analyze_Pragma. Note that both flags are
8988 -- mutually exclusive.
8989
8990 Propagate_Default_Init_Cond_Attributes
8991 (From_Typ => Parent_Type,
8992 To_Typ => Derived_Type,
8993 Parent_To_Derivation => True);
8994
8995 -- If the parent type has delayed rep aspects, then mark the derived
8996 -- type as possibly inheriting a delayed rep aspect.
8997
8998 if Has_Delayed_Rep_Aspects (Parent_Type) then
8999 Set_May_Inherit_Delayed_Rep_Aspects (Derived_Type);
9000 end if;
9001
9002 -- Propagate the attributes related to pragma Ghost from the parent type
9003 -- to the derived type or type extension (SPARK RM 6.9(9)).
9004
9005 if Is_Ghost_Entity (Parent_Type) then
9006 Set_Is_Ghost_Entity (Derived_Type);
9007 end if;
9008
9009 -- Type dependent processing
9010
9011 case Ekind (Parent_Type) is
9012 when Numeric_Kind =>
9013 Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
9014
9015 when Array_Kind =>
9016 Build_Derived_Array_Type (N, Parent_Type, Derived_Type);
9017
9018 when E_Record_Type
9019 | E_Record_Subtype
9020 | Class_Wide_Kind =>
9021 Build_Derived_Record_Type
9022 (N, Parent_Type, Derived_Type, Derive_Subps);
9023 return;
9024
9025 when Enumeration_Kind =>
9026 Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
9027
9028 when Access_Kind =>
9029 Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
9030
9031 when Incomplete_Or_Private_Kind =>
9032 Build_Derived_Private_Type
9033 (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
9034
9035 -- For discriminated types, the derivation includes deriving
9036 -- primitive operations. For others it is done below.
9037
9038 if Is_Tagged_Type (Parent_Type)
9039 or else Has_Discriminants (Parent_Type)
9040 or else (Present (Full_View (Parent_Type))
9041 and then Has_Discriminants (Full_View (Parent_Type)))
9042 then
9043 return;
9044 end if;
9045
9046 when Concurrent_Kind =>
9047 Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
9048
9049 when others =>
9050 raise Program_Error;
9051 end case;
9052
9053 -- Nothing more to do if some error occurred
9054
9055 if Etype (Derived_Type) = Any_Type then
9056 return;
9057 end if;
9058
9059 -- Set delayed freeze and then derive subprograms, we need to do this
9060 -- in this order so that derived subprograms inherit the derived freeze
9061 -- if necessary.
9062
9063 Set_Has_Delayed_Freeze (Derived_Type);
9064
9065 if Derive_Subps then
9066 Derive_Subprograms (Parent_Type, Derived_Type);
9067 end if;
9068
9069 Set_Has_Primitive_Operations
9070 (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
9071 end Build_Derived_Type;
9072
9073 -----------------------
9074 -- Build_Discriminal --
9075 -----------------------
9076
9077 procedure Build_Discriminal (Discrim : Entity_Id) is
9078 D_Minal : Entity_Id;
9079 CR_Disc : Entity_Id;
9080
9081 begin
9082 -- A discriminal has the same name as the discriminant
9083
9084 D_Minal := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9085
9086 Set_Ekind (D_Minal, E_In_Parameter);
9087 Set_Mechanism (D_Minal, Default_Mechanism);
9088 Set_Etype (D_Minal, Etype (Discrim));
9089 Set_Scope (D_Minal, Current_Scope);
9090
9091 Set_Discriminal (Discrim, D_Minal);
9092 Set_Discriminal_Link (D_Minal, Discrim);
9093
9094 -- For task types, build at once the discriminants of the corresponding
9095 -- record, which are needed if discriminants are used in entry defaults
9096 -- and in family bounds.
9097
9098 if Is_Concurrent_Type (Current_Scope)
9099 or else
9100 Is_Limited_Type (Current_Scope)
9101 then
9102 CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9103
9104 Set_Ekind (CR_Disc, E_In_Parameter);
9105 Set_Mechanism (CR_Disc, Default_Mechanism);
9106 Set_Etype (CR_Disc, Etype (Discrim));
9107 Set_Scope (CR_Disc, Current_Scope);
9108 Set_Discriminal_Link (CR_Disc, Discrim);
9109 Set_CR_Discriminant (Discrim, CR_Disc);
9110 end if;
9111 end Build_Discriminal;
9112
9113 ------------------------------------
9114 -- Build_Discriminant_Constraints --
9115 ------------------------------------
9116
9117 function Build_Discriminant_Constraints
9118 (T : Entity_Id;
9119 Def : Node_Id;
9120 Derived_Def : Boolean := False) return Elist_Id
9121 is
9122 C : constant Node_Id := Constraint (Def);
9123 Nb_Discr : constant Nat := Number_Discriminants (T);
9124
9125 Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
9126 -- Saves the expression corresponding to a given discriminant in T
9127
9128 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
9129 -- Return the Position number within array Discr_Expr of a discriminant
9130 -- D within the discriminant list of the discriminated type T.
9131
9132 procedure Process_Discriminant_Expression
9133 (Expr : Node_Id;
9134 D : Entity_Id);
9135 -- If this is a discriminant constraint on a partial view, do not
9136 -- generate an overflow check on the discriminant expression. The check
9137 -- will be generated when constraining the full view. Otherwise the
9138 -- backend creates duplicate symbols for the temporaries corresponding
9139 -- to the expressions to be checked, causing spurious assembler errors.
9140
9141 ------------------
9142 -- Pos_Of_Discr --
9143 ------------------
9144
9145 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
9146 Disc : Entity_Id;
9147
9148 begin
9149 Disc := First_Discriminant (T);
9150 for J in Discr_Expr'Range loop
9151 if Disc = D then
9152 return J;
9153 end if;
9154
9155 Next_Discriminant (Disc);
9156 end loop;
9157
9158 -- Note: Since this function is called on discriminants that are
9159 -- known to belong to the discriminated type, falling through the
9160 -- loop with no match signals an internal compiler error.
9161
9162 raise Program_Error;
9163 end Pos_Of_Discr;
9164
9165 -------------------------------------
9166 -- Process_Discriminant_Expression --
9167 -------------------------------------
9168
9169 procedure Process_Discriminant_Expression
9170 (Expr : Node_Id;
9171 D : Entity_Id)
9172 is
9173 BDT : constant Entity_Id := Base_Type (Etype (D));
9174
9175 begin
9176 -- If this is a discriminant constraint on a partial view, do
9177 -- not generate an overflow on the discriminant expression. The
9178 -- check will be generated when constraining the full view.
9179
9180 if Is_Private_Type (T)
9181 and then Present (Full_View (T))
9182 then
9183 Analyze_And_Resolve (Expr, BDT, Suppress => Overflow_Check);
9184 else
9185 Analyze_And_Resolve (Expr, BDT);
9186 end if;
9187 end Process_Discriminant_Expression;
9188
9189 -- Declarations local to Build_Discriminant_Constraints
9190
9191 Discr : Entity_Id;
9192 E : Entity_Id;
9193 Elist : constant Elist_Id := New_Elmt_List;
9194
9195 Constr : Node_Id;
9196 Expr : Node_Id;
9197 Id : Node_Id;
9198 Position : Nat;
9199 Found : Boolean;
9200
9201 Discrim_Present : Boolean := False;
9202
9203 -- Start of processing for Build_Discriminant_Constraints
9204
9205 begin
9206 -- The following loop will process positional associations only.
9207 -- For a positional association, the (single) discriminant is
9208 -- implicitly specified by position, in textual order (RM 3.7.2).
9209
9210 Discr := First_Discriminant (T);
9211 Constr := First (Constraints (C));
9212 for D in Discr_Expr'Range loop
9213 exit when Nkind (Constr) = N_Discriminant_Association;
9214
9215 if No (Constr) then
9216 Error_Msg_N ("too few discriminants given in constraint", C);
9217 return New_Elmt_List;
9218
9219 elsif Nkind (Constr) = N_Range
9220 or else (Nkind (Constr) = N_Attribute_Reference
9221 and then Attribute_Name (Constr) = Name_Range)
9222 then
9223 Error_Msg_N
9224 ("a range is not a valid discriminant constraint", Constr);
9225 Discr_Expr (D) := Error;
9226
9227 else
9228 Process_Discriminant_Expression (Constr, Discr);
9229 Discr_Expr (D) := Constr;
9230 end if;
9231
9232 Next_Discriminant (Discr);
9233 Next (Constr);
9234 end loop;
9235
9236 if No (Discr) and then Present (Constr) then
9237 Error_Msg_N ("too many discriminants given in constraint", Constr);
9238 return New_Elmt_List;
9239 end if;
9240
9241 -- Named associations can be given in any order, but if both positional
9242 -- and named associations are used in the same discriminant constraint,
9243 -- then positional associations must occur first, at their normal
9244 -- position. Hence once a named association is used, the rest of the
9245 -- discriminant constraint must use only named associations.
9246
9247 while Present (Constr) loop
9248
9249 -- Positional association forbidden after a named association
9250
9251 if Nkind (Constr) /= N_Discriminant_Association then
9252 Error_Msg_N ("positional association follows named one", Constr);
9253 return New_Elmt_List;
9254
9255 -- Otherwise it is a named association
9256
9257 else
9258 -- E records the type of the discriminants in the named
9259 -- association. All the discriminants specified in the same name
9260 -- association must have the same type.
9261
9262 E := Empty;
9263
9264 -- Search the list of discriminants in T to see if the simple name
9265 -- given in the constraint matches any of them.
9266
9267 Id := First (Selector_Names (Constr));
9268 while Present (Id) loop
9269 Found := False;
9270
9271 -- If Original_Discriminant is present, we are processing a
9272 -- generic instantiation and this is an instance node. We need
9273 -- to find the name of the corresponding discriminant in the
9274 -- actual record type T and not the name of the discriminant in
9275 -- the generic formal. Example:
9276
9277 -- generic
9278 -- type G (D : int) is private;
9279 -- package P is
9280 -- subtype W is G (D => 1);
9281 -- end package;
9282 -- type Rec (X : int) is record ... end record;
9283 -- package Q is new P (G => Rec);
9284
9285 -- At the point of the instantiation, formal type G is Rec
9286 -- and therefore when reanalyzing "subtype W is G (D => 1);"
9287 -- which really looks like "subtype W is Rec (D => 1);" at
9288 -- the point of instantiation, we want to find the discriminant
9289 -- that corresponds to D in Rec, i.e. X.
9290
9291 if Present (Original_Discriminant (Id))
9292 and then In_Instance
9293 then
9294 Discr := Find_Corresponding_Discriminant (Id, T);
9295 Found := True;
9296
9297 else
9298 Discr := First_Discriminant (T);
9299 while Present (Discr) loop
9300 if Chars (Discr) = Chars (Id) then
9301 Found := True;
9302 exit;
9303 end if;
9304
9305 Next_Discriminant (Discr);
9306 end loop;
9307
9308 if not Found then
9309 Error_Msg_N ("& does not match any discriminant", Id);
9310 return New_Elmt_List;
9311
9312 -- If the parent type is a generic formal, preserve the
9313 -- name of the discriminant for subsequent instances.
9314 -- see comment at the beginning of this if statement.
9315
9316 elsif Is_Generic_Type (Root_Type (T)) then
9317 Set_Original_Discriminant (Id, Discr);
9318 end if;
9319 end if;
9320
9321 Position := Pos_Of_Discr (T, Discr);
9322
9323 if Present (Discr_Expr (Position)) then
9324 Error_Msg_N ("duplicate constraint for discriminant&", Id);
9325
9326 else
9327 -- Each discriminant specified in the same named association
9328 -- must be associated with a separate copy of the
9329 -- corresponding expression.
9330
9331 if Present (Next (Id)) then
9332 Expr := New_Copy_Tree (Expression (Constr));
9333 Set_Parent (Expr, Parent (Expression (Constr)));
9334 else
9335 Expr := Expression (Constr);
9336 end if;
9337
9338 Discr_Expr (Position) := Expr;
9339 Process_Discriminant_Expression (Expr, Discr);
9340 end if;
9341
9342 -- A discriminant association with more than one discriminant
9343 -- name is only allowed if the named discriminants are all of
9344 -- the same type (RM 3.7.1(8)).
9345
9346 if E = Empty then
9347 E := Base_Type (Etype (Discr));
9348
9349 elsif Base_Type (Etype (Discr)) /= E then
9350 Error_Msg_N
9351 ("all discriminants in an association " &
9352 "must have the same type", Id);
9353 end if;
9354
9355 Next (Id);
9356 end loop;
9357 end if;
9358
9359 Next (Constr);
9360 end loop;
9361
9362 -- A discriminant constraint must provide exactly one value for each
9363 -- discriminant of the type (RM 3.7.1(8)).
9364
9365 for J in Discr_Expr'Range loop
9366 if No (Discr_Expr (J)) then
9367 Error_Msg_N ("too few discriminants given in constraint", C);
9368 return New_Elmt_List;
9369 end if;
9370 end loop;
9371
9372 -- Determine if there are discriminant expressions in the constraint
9373
9374 for J in Discr_Expr'Range loop
9375 if Denotes_Discriminant
9376 (Discr_Expr (J), Check_Concurrent => True)
9377 then
9378 Discrim_Present := True;
9379 end if;
9380 end loop;
9381
9382 -- Build an element list consisting of the expressions given in the
9383 -- discriminant constraint and apply the appropriate checks. The list
9384 -- is constructed after resolving any named discriminant associations
9385 -- and therefore the expressions appear in the textual order of the
9386 -- discriminants.
9387
9388 Discr := First_Discriminant (T);
9389 for J in Discr_Expr'Range loop
9390 if Discr_Expr (J) /= Error then
9391 Append_Elmt (Discr_Expr (J), Elist);
9392
9393 -- If any of the discriminant constraints is given by a
9394 -- discriminant and we are in a derived type declaration we
9395 -- have a discriminant renaming. Establish link between new
9396 -- and old discriminant.
9397
9398 if Denotes_Discriminant (Discr_Expr (J)) then
9399 if Derived_Def then
9400 Set_Corresponding_Discriminant
9401 (Entity (Discr_Expr (J)), Discr);
9402 end if;
9403
9404 -- Force the evaluation of non-discriminant expressions.
9405 -- If we have found a discriminant in the constraint 3.4(26)
9406 -- and 3.8(18) demand that no range checks are performed are
9407 -- after evaluation. If the constraint is for a component
9408 -- definition that has a per-object constraint, expressions are
9409 -- evaluated but not checked either. In all other cases perform
9410 -- a range check.
9411
9412 else
9413 if Discrim_Present then
9414 null;
9415
9416 elsif Nkind (Parent (Parent (Def))) = N_Component_Declaration
9417 and then
9418 Has_Per_Object_Constraint
9419 (Defining_Identifier (Parent (Parent (Def))))
9420 then
9421 null;
9422
9423 elsif Is_Access_Type (Etype (Discr)) then
9424 Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
9425
9426 else
9427 Apply_Range_Check (Discr_Expr (J), Etype (Discr));
9428 end if;
9429
9430 Force_Evaluation (Discr_Expr (J));
9431 end if;
9432
9433 -- Check that the designated type of an access discriminant's
9434 -- expression is not a class-wide type unless the discriminant's
9435 -- designated type is also class-wide.
9436
9437 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
9438 and then not Is_Class_Wide_Type
9439 (Designated_Type (Etype (Discr)))
9440 and then Etype (Discr_Expr (J)) /= Any_Type
9441 and then Is_Class_Wide_Type
9442 (Designated_Type (Etype (Discr_Expr (J))))
9443 then
9444 Wrong_Type (Discr_Expr (J), Etype (Discr));
9445
9446 elsif Is_Access_Type (Etype (Discr))
9447 and then not Is_Access_Constant (Etype (Discr))
9448 and then Is_Access_Type (Etype (Discr_Expr (J)))
9449 and then Is_Access_Constant (Etype (Discr_Expr (J)))
9450 then
9451 Error_Msg_NE
9452 ("constraint for discriminant& must be access to variable",
9453 Def, Discr);
9454 end if;
9455 end if;
9456
9457 Next_Discriminant (Discr);
9458 end loop;
9459
9460 return Elist;
9461 end Build_Discriminant_Constraints;
9462
9463 ---------------------------------
9464 -- Build_Discriminated_Subtype --
9465 ---------------------------------
9466
9467 procedure Build_Discriminated_Subtype
9468 (T : Entity_Id;
9469 Def_Id : Entity_Id;
9470 Elist : Elist_Id;
9471 Related_Nod : Node_Id;
9472 For_Access : Boolean := False)
9473 is
9474 Has_Discrs : constant Boolean := Has_Discriminants (T);
9475 Constrained : constant Boolean :=
9476 (Has_Discrs
9477 and then not Is_Empty_Elmt_List (Elist)
9478 and then not Is_Class_Wide_Type (T))
9479 or else Is_Constrained (T);
9480
9481 begin
9482 if Ekind (T) = E_Record_Type then
9483 if For_Access then
9484 Set_Ekind (Def_Id, E_Private_Subtype);
9485 Set_Is_For_Access_Subtype (Def_Id, True);
9486 else
9487 Set_Ekind (Def_Id, E_Record_Subtype);
9488 end if;
9489
9490 -- Inherit preelaboration flag from base, for types for which it
9491 -- may have been set: records, private types, protected types.
9492
9493 Set_Known_To_Have_Preelab_Init
9494 (Def_Id, Known_To_Have_Preelab_Init (T));
9495
9496 elsif Ekind (T) = E_Task_Type then
9497 Set_Ekind (Def_Id, E_Task_Subtype);
9498
9499 elsif Ekind (T) = E_Protected_Type then
9500 Set_Ekind (Def_Id, E_Protected_Subtype);
9501 Set_Known_To_Have_Preelab_Init
9502 (Def_Id, Known_To_Have_Preelab_Init (T));
9503
9504 elsif Is_Private_Type (T) then
9505 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
9506 Set_Known_To_Have_Preelab_Init
9507 (Def_Id, Known_To_Have_Preelab_Init (T));
9508
9509 -- Private subtypes may have private dependents
9510
9511 Set_Private_Dependents (Def_Id, New_Elmt_List);
9512
9513 elsif Is_Class_Wide_Type (T) then
9514 Set_Ekind (Def_Id, E_Class_Wide_Subtype);
9515
9516 else
9517 -- Incomplete type. Attach subtype to list of dependents, to be
9518 -- completed with full view of parent type, unless is it the
9519 -- designated subtype of a record component within an init_proc.
9520 -- This last case arises for a component of an access type whose
9521 -- designated type is incomplete (e.g. a Taft Amendment type).
9522 -- The designated subtype is within an inner scope, and needs no
9523 -- elaboration, because only the access type is needed in the
9524 -- initialization procedure.
9525
9526 Set_Ekind (Def_Id, Ekind (T));
9527
9528 if For_Access and then Within_Init_Proc then
9529 null;
9530 else
9531 Append_Elmt (Def_Id, Private_Dependents (T));
9532 end if;
9533 end if;
9534
9535 Set_Etype (Def_Id, T);
9536 Init_Size_Align (Def_Id);
9537 Set_Has_Discriminants (Def_Id, Has_Discrs);
9538 Set_Is_Constrained (Def_Id, Constrained);
9539
9540 Set_First_Entity (Def_Id, First_Entity (T));
9541 Set_Last_Entity (Def_Id, Last_Entity (T));
9542 Set_Has_Implicit_Dereference
9543 (Def_Id, Has_Implicit_Dereference (T));
9544
9545 -- If the subtype is the completion of a private declaration, there may
9546 -- have been representation clauses for the partial view, and they must
9547 -- be preserved. Build_Derived_Type chains the inherited clauses with
9548 -- the ones appearing on the extension. If this comes from a subtype
9549 -- declaration, all clauses are inherited.
9550
9551 if No (First_Rep_Item (Def_Id)) then
9552 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
9553 end if;
9554
9555 if Is_Tagged_Type (T) then
9556 Set_Is_Tagged_Type (Def_Id);
9557 Set_No_Tagged_Streams_Pragma (Def_Id, No_Tagged_Streams_Pragma (T));
9558 Make_Class_Wide_Type (Def_Id);
9559 end if;
9560
9561 Set_Stored_Constraint (Def_Id, No_Elist);
9562
9563 if Has_Discrs then
9564 Set_Discriminant_Constraint (Def_Id, Elist);
9565 Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
9566 end if;
9567
9568 if Is_Tagged_Type (T) then
9569
9570 -- Ada 2005 (AI-251): In case of concurrent types we inherit the
9571 -- concurrent record type (which has the list of primitive
9572 -- operations).
9573
9574 if Ada_Version >= Ada_2005
9575 and then Is_Concurrent_Type (T)
9576 then
9577 Set_Corresponding_Record_Type (Def_Id,
9578 Corresponding_Record_Type (T));
9579 else
9580 Set_Direct_Primitive_Operations (Def_Id,
9581 Direct_Primitive_Operations (T));
9582 end if;
9583
9584 Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
9585 end if;
9586
9587 -- Subtypes introduced by component declarations do not need to be
9588 -- marked as delayed, and do not get freeze nodes, because the semantics
9589 -- verifies that the parents of the subtypes are frozen before the
9590 -- enclosing record is frozen.
9591
9592 if not Is_Type (Scope (Def_Id)) then
9593 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
9594
9595 if Is_Private_Type (T)
9596 and then Present (Full_View (T))
9597 then
9598 Conditional_Delay (Def_Id, Full_View (T));
9599 else
9600 Conditional_Delay (Def_Id, T);
9601 end if;
9602 end if;
9603
9604 if Is_Record_Type (T) then
9605 Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
9606
9607 if Has_Discrs
9608 and then not Is_Empty_Elmt_List (Elist)
9609 and then not For_Access
9610 then
9611 Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
9612 elsif not For_Access then
9613 Set_Cloned_Subtype (Def_Id, T);
9614 end if;
9615 end if;
9616 end Build_Discriminated_Subtype;
9617
9618 ---------------------------
9619 -- Build_Itype_Reference --
9620 ---------------------------
9621
9622 procedure Build_Itype_Reference
9623 (Ityp : Entity_Id;
9624 Nod : Node_Id)
9625 is
9626 IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
9627 begin
9628
9629 -- Itype references are only created for use by the back-end
9630
9631 if Inside_A_Generic then
9632 return;
9633 else
9634 Set_Itype (IR, Ityp);
9635 Insert_After (Nod, IR);
9636 end if;
9637 end Build_Itype_Reference;
9638
9639 ------------------------
9640 -- Build_Scalar_Bound --
9641 ------------------------
9642
9643 function Build_Scalar_Bound
9644 (Bound : Node_Id;
9645 Par_T : Entity_Id;
9646 Der_T : Entity_Id) return Node_Id
9647 is
9648 New_Bound : Entity_Id;
9649
9650 begin
9651 -- Note: not clear why this is needed, how can the original bound
9652 -- be unanalyzed at this point? and if it is, what business do we
9653 -- have messing around with it? and why is the base type of the
9654 -- parent type the right type for the resolution. It probably is
9655 -- not. It is OK for the new bound we are creating, but not for
9656 -- the old one??? Still if it never happens, no problem.
9657
9658 Analyze_And_Resolve (Bound, Base_Type (Par_T));
9659
9660 if Nkind_In (Bound, N_Integer_Literal, N_Real_Literal) then
9661 New_Bound := New_Copy (Bound);
9662 Set_Etype (New_Bound, Der_T);
9663 Set_Analyzed (New_Bound);
9664
9665 elsif Is_Entity_Name (Bound) then
9666 New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
9667
9668 -- The following is almost certainly wrong. What business do we have
9669 -- relocating a node (Bound) that is presumably still attached to
9670 -- the tree elsewhere???
9671
9672 else
9673 New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
9674 end if;
9675
9676 Set_Etype (New_Bound, Der_T);
9677 return New_Bound;
9678 end Build_Scalar_Bound;
9679
9680 --------------------------------
9681 -- Build_Underlying_Full_View --
9682 --------------------------------
9683
9684 procedure Build_Underlying_Full_View
9685 (N : Node_Id;
9686 Typ : Entity_Id;
9687 Par : Entity_Id)
9688 is
9689 Loc : constant Source_Ptr := Sloc (N);
9690 Subt : constant Entity_Id :=
9691 Make_Defining_Identifier
9692 (Loc, New_External_Name (Chars (Typ), 'S'));
9693
9694 Constr : Node_Id;
9695 Indic : Node_Id;
9696 C : Node_Id;
9697 Id : Node_Id;
9698
9699 procedure Set_Discriminant_Name (Id : Node_Id);
9700 -- If the derived type has discriminants, they may rename discriminants
9701 -- of the parent. When building the full view of the parent, we need to
9702 -- recover the names of the original discriminants if the constraint is
9703 -- given by named associations.
9704
9705 ---------------------------
9706 -- Set_Discriminant_Name --
9707 ---------------------------
9708
9709 procedure Set_Discriminant_Name (Id : Node_Id) is
9710 Disc : Entity_Id;
9711
9712 begin
9713 Set_Original_Discriminant (Id, Empty);
9714
9715 if Has_Discriminants (Typ) then
9716 Disc := First_Discriminant (Typ);
9717 while Present (Disc) loop
9718 if Chars (Disc) = Chars (Id)
9719 and then Present (Corresponding_Discriminant (Disc))
9720 then
9721 Set_Chars (Id, Chars (Corresponding_Discriminant (Disc)));
9722 end if;
9723 Next_Discriminant (Disc);
9724 end loop;
9725 end if;
9726 end Set_Discriminant_Name;
9727
9728 -- Start of processing for Build_Underlying_Full_View
9729
9730 begin
9731 if Nkind (N) = N_Full_Type_Declaration then
9732 Constr := Constraint (Subtype_Indication (Type_Definition (N)));
9733
9734 elsif Nkind (N) = N_Subtype_Declaration then
9735 Constr := New_Copy_Tree (Constraint (Subtype_Indication (N)));
9736
9737 elsif Nkind (N) = N_Component_Declaration then
9738 Constr :=
9739 New_Copy_Tree
9740 (Constraint (Subtype_Indication (Component_Definition (N))));
9741
9742 else
9743 raise Program_Error;
9744 end if;
9745
9746 C := First (Constraints (Constr));
9747 while Present (C) loop
9748 if Nkind (C) = N_Discriminant_Association then
9749 Id := First (Selector_Names (C));
9750 while Present (Id) loop
9751 Set_Discriminant_Name (Id);
9752 Next (Id);
9753 end loop;
9754 end if;
9755
9756 Next (C);
9757 end loop;
9758
9759 Indic :=
9760 Make_Subtype_Declaration (Loc,
9761 Defining_Identifier => Subt,
9762 Subtype_Indication =>
9763 Make_Subtype_Indication (Loc,
9764 Subtype_Mark => New_Occurrence_Of (Par, Loc),
9765 Constraint => New_Copy_Tree (Constr)));
9766
9767 -- If this is a component subtype for an outer itype, it is not
9768 -- a list member, so simply set the parent link for analysis: if
9769 -- the enclosing type does not need to be in a declarative list,
9770 -- neither do the components.
9771
9772 if Is_List_Member (N)
9773 and then Nkind (N) /= N_Component_Declaration
9774 then
9775 Insert_Before (N, Indic);
9776 else
9777 Set_Parent (Indic, Parent (N));
9778 end if;
9779
9780 Analyze (Indic);
9781 Set_Underlying_Full_View (Typ, Full_View (Subt));
9782 end Build_Underlying_Full_View;
9783
9784 -------------------------------
9785 -- Check_Abstract_Overriding --
9786 -------------------------------
9787
9788 procedure Check_Abstract_Overriding (T : Entity_Id) is
9789 Alias_Subp : Entity_Id;
9790 Elmt : Elmt_Id;
9791 Op_List : Elist_Id;
9792 Subp : Entity_Id;
9793 Type_Def : Node_Id;
9794
9795 procedure Check_Pragma_Implemented (Subp : Entity_Id);
9796 -- Ada 2012 (AI05-0030): Subprogram Subp overrides an interface routine
9797 -- which has pragma Implemented already set. Check whether Subp's entity
9798 -- kind conforms to the implementation kind of the overridden routine.
9799
9800 procedure Check_Pragma_Implemented
9801 (Subp : Entity_Id;
9802 Iface_Subp : Entity_Id);
9803 -- Ada 2012 (AI05-0030): Subprogram Subp overrides interface routine
9804 -- Iface_Subp and both entities have pragma Implemented already set on
9805 -- them. Check whether the two implementation kinds are conforming.
9806
9807 procedure Inherit_Pragma_Implemented
9808 (Subp : Entity_Id;
9809 Iface_Subp : Entity_Id);
9810 -- Ada 2012 (AI05-0030): Interface primitive Subp overrides interface
9811 -- subprogram Iface_Subp which has been marked by pragma Implemented.
9812 -- Propagate the implementation kind of Iface_Subp to Subp.
9813
9814 ------------------------------
9815 -- Check_Pragma_Implemented --
9816 ------------------------------
9817
9818 procedure Check_Pragma_Implemented (Subp : Entity_Id) is
9819 Iface_Alias : constant Entity_Id := Interface_Alias (Subp);
9820 Impl_Kind : constant Name_Id := Implementation_Kind (Iface_Alias);
9821 Subp_Alias : constant Entity_Id := Alias (Subp);
9822 Contr_Typ : Entity_Id;
9823 Impl_Subp : Entity_Id;
9824
9825 begin
9826 -- Subp must have an alias since it is a hidden entity used to link
9827 -- an interface subprogram to its overriding counterpart.
9828
9829 pragma Assert (Present (Subp_Alias));
9830
9831 -- Handle aliases to synchronized wrappers
9832
9833 Impl_Subp := Subp_Alias;
9834
9835 if Is_Primitive_Wrapper (Impl_Subp) then
9836 Impl_Subp := Wrapped_Entity (Impl_Subp);
9837 end if;
9838
9839 -- Extract the type of the controlling formal
9840
9841 Contr_Typ := Etype (First_Formal (Subp_Alias));
9842
9843 if Is_Concurrent_Record_Type (Contr_Typ) then
9844 Contr_Typ := Corresponding_Concurrent_Type (Contr_Typ);
9845 end if;
9846
9847 -- An interface subprogram whose implementation kind is By_Entry must
9848 -- be implemented by an entry.
9849
9850 if Impl_Kind = Name_By_Entry
9851 and then Ekind (Impl_Subp) /= E_Entry
9852 then
9853 Error_Msg_Node_2 := Iface_Alias;
9854 Error_Msg_NE
9855 ("type & must implement abstract subprogram & with an entry",
9856 Subp_Alias, Contr_Typ);
9857
9858 elsif Impl_Kind = Name_By_Protected_Procedure then
9859
9860 -- An interface subprogram whose implementation kind is By_
9861 -- Protected_Procedure cannot be implemented by a primitive
9862 -- procedure of a task type.
9863
9864 if Ekind (Contr_Typ) /= E_Protected_Type then
9865 Error_Msg_Node_2 := Contr_Typ;
9866 Error_Msg_NE
9867 ("interface subprogram & cannot be implemented by a " &
9868 "primitive procedure of task type &", Subp_Alias,
9869 Iface_Alias);
9870
9871 -- An interface subprogram whose implementation kind is By_
9872 -- Protected_Procedure must be implemented by a procedure.
9873
9874 elsif Ekind (Impl_Subp) /= E_Procedure then
9875 Error_Msg_Node_2 := Iface_Alias;
9876 Error_Msg_NE
9877 ("type & must implement abstract subprogram & with a " &
9878 "procedure", Subp_Alias, Contr_Typ);
9879
9880 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
9881 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
9882 then
9883 Error_Msg_Name_1 := Impl_Kind;
9884 Error_Msg_N
9885 ("overriding operation& must have synchronization%",
9886 Subp_Alias);
9887 end if;
9888
9889 -- If primitive has Optional synchronization, overriding operation
9890 -- must match if it has an explicit synchronization..
9891
9892 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
9893 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
9894 then
9895 Error_Msg_Name_1 := Impl_Kind;
9896 Error_Msg_N
9897 ("overriding operation& must have syncrhonization%",
9898 Subp_Alias);
9899 end if;
9900 end Check_Pragma_Implemented;
9901
9902 ------------------------------
9903 -- Check_Pragma_Implemented --
9904 ------------------------------
9905
9906 procedure Check_Pragma_Implemented
9907 (Subp : Entity_Id;
9908 Iface_Subp : Entity_Id)
9909 is
9910 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
9911 Subp_Kind : constant Name_Id := Implementation_Kind (Subp);
9912
9913 begin
9914 -- Ada 2012 (AI05-0030): The implementation kinds of an overridden
9915 -- and overriding subprogram are different. In general this is an
9916 -- error except when the implementation kind of the overridden
9917 -- subprograms is By_Any or Optional.
9918
9919 if Iface_Kind /= Subp_Kind
9920 and then Iface_Kind /= Name_By_Any
9921 and then Iface_Kind /= Name_Optional
9922 then
9923 if Iface_Kind = Name_By_Entry then
9924 Error_Msg_N
9925 ("incompatible implementation kind, overridden subprogram " &
9926 "is marked By_Entry", Subp);
9927 else
9928 Error_Msg_N
9929 ("incompatible implementation kind, overridden subprogram " &
9930 "is marked By_Protected_Procedure", Subp);
9931 end if;
9932 end if;
9933 end Check_Pragma_Implemented;
9934
9935 --------------------------------
9936 -- Inherit_Pragma_Implemented --
9937 --------------------------------
9938
9939 procedure Inherit_Pragma_Implemented
9940 (Subp : Entity_Id;
9941 Iface_Subp : Entity_Id)
9942 is
9943 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
9944 Loc : constant Source_Ptr := Sloc (Subp);
9945 Impl_Prag : Node_Id;
9946
9947 begin
9948 -- Since the implementation kind is stored as a representation item
9949 -- rather than a flag, create a pragma node.
9950
9951 Impl_Prag :=
9952 Make_Pragma (Loc,
9953 Chars => Name_Implemented,
9954 Pragma_Argument_Associations => New_List (
9955 Make_Pragma_Argument_Association (Loc,
9956 Expression => New_Occurrence_Of (Subp, Loc)),
9957
9958 Make_Pragma_Argument_Association (Loc,
9959 Expression => Make_Identifier (Loc, Iface_Kind))));
9960
9961 -- The pragma doesn't need to be analyzed because it is internally
9962 -- built. It is safe to directly register it as a rep item since we
9963 -- are only interested in the characters of the implementation kind.
9964
9965 Record_Rep_Item (Subp, Impl_Prag);
9966 end Inherit_Pragma_Implemented;
9967
9968 -- Start of processing for Check_Abstract_Overriding
9969
9970 begin
9971 Op_List := Primitive_Operations (T);
9972
9973 -- Loop to check primitive operations
9974
9975 Elmt := First_Elmt (Op_List);
9976 while Present (Elmt) loop
9977 Subp := Node (Elmt);
9978 Alias_Subp := Alias (Subp);
9979
9980 -- Inherited subprograms are identified by the fact that they do not
9981 -- come from source, and the associated source location is the
9982 -- location of the first subtype of the derived type.
9983
9984 -- Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
9985 -- subprograms that "require overriding".
9986
9987 -- Special exception, do not complain about failure to override the
9988 -- stream routines _Input and _Output, as well as the primitive
9989 -- operations used in dispatching selects since we always provide
9990 -- automatic overridings for these subprograms.
9991
9992 -- Also ignore this rule for convention CIL since .NET libraries
9993 -- do bizarre things with interfaces???
9994
9995 -- The partial view of T may have been a private extension, for
9996 -- which inherited functions dispatching on result are abstract.
9997 -- If the full view is a null extension, there is no need for
9998 -- overriding in Ada 2005, but wrappers need to be built for them
9999 -- (see exp_ch3, Build_Controlling_Function_Wrappers).
10000
10001 if Is_Null_Extension (T)
10002 and then Has_Controlling_Result (Subp)
10003 and then Ada_Version >= Ada_2005
10004 and then Present (Alias_Subp)
10005 and then not Comes_From_Source (Subp)
10006 and then not Is_Abstract_Subprogram (Alias_Subp)
10007 and then not Is_Access_Type (Etype (Subp))
10008 then
10009 null;
10010
10011 -- Ada 2005 (AI-251): Internal entities of interfaces need no
10012 -- processing because this check is done with the aliased
10013 -- entity
10014
10015 elsif Present (Interface_Alias (Subp)) then
10016 null;
10017
10018 elsif (Is_Abstract_Subprogram (Subp)
10019 or else Requires_Overriding (Subp)
10020 or else
10021 (Has_Controlling_Result (Subp)
10022 and then Present (Alias_Subp)
10023 and then not Comes_From_Source (Subp)
10024 and then Sloc (Subp) = Sloc (First_Subtype (T))))
10025 and then not Is_TSS (Subp, TSS_Stream_Input)
10026 and then not Is_TSS (Subp, TSS_Stream_Output)
10027 and then not Is_Abstract_Type (T)
10028 and then Convention (T) /= Convention_CIL
10029 and then not Is_Predefined_Interface_Primitive (Subp)
10030
10031 -- Ada 2005 (AI-251): Do not consider hidden entities associated
10032 -- with abstract interface types because the check will be done
10033 -- with the aliased entity (otherwise we generate a duplicated
10034 -- error message).
10035
10036 and then not Present (Interface_Alias (Subp))
10037 then
10038 if Present (Alias_Subp) then
10039
10040 -- Only perform the check for a derived subprogram when the
10041 -- type has an explicit record extension. This avoids incorrect
10042 -- flagging of abstract subprograms for the case of a type
10043 -- without an extension that is derived from a formal type
10044 -- with a tagged actual (can occur within a private part).
10045
10046 -- Ada 2005 (AI-391): In the case of an inherited function with
10047 -- a controlling result of the type, the rule does not apply if
10048 -- the type is a null extension (unless the parent function
10049 -- itself is abstract, in which case the function must still be
10050 -- be overridden). The expander will generate an overriding
10051 -- wrapper function calling the parent subprogram (see
10052 -- Exp_Ch3.Make_Controlling_Wrapper_Functions).
10053
10054 Type_Def := Type_Definition (Parent (T));
10055
10056 if Nkind (Type_Def) = N_Derived_Type_Definition
10057 and then Present (Record_Extension_Part (Type_Def))
10058 and then
10059 (Ada_Version < Ada_2005
10060 or else not Is_Null_Extension (T)
10061 or else Ekind (Subp) = E_Procedure
10062 or else not Has_Controlling_Result (Subp)
10063 or else Is_Abstract_Subprogram (Alias_Subp)
10064 or else Requires_Overriding (Subp)
10065 or else Is_Access_Type (Etype (Subp)))
10066 then
10067 -- Avoid reporting error in case of abstract predefined
10068 -- primitive inherited from interface type because the
10069 -- body of internally generated predefined primitives
10070 -- of tagged types are generated later by Freeze_Type
10071
10072 if Is_Interface (Root_Type (T))
10073 and then Is_Abstract_Subprogram (Subp)
10074 and then Is_Predefined_Dispatching_Operation (Subp)
10075 and then not Comes_From_Source (Ultimate_Alias (Subp))
10076 then
10077 null;
10078
10079 -- A null extension is not obliged to override an inherited
10080 -- procedure subject to pragma Extensions_Visible with value
10081 -- False and at least one controlling OUT parameter
10082 -- (SPARK RM 6.1.7(6)).
10083
10084 elsif Is_Null_Extension (T)
10085 and then Is_EVF_Procedure (Subp)
10086 then
10087 null;
10088
10089 else
10090 Error_Msg_NE
10091 ("type must be declared abstract or & overridden",
10092 T, Subp);
10093
10094 -- Traverse the whole chain of aliased subprograms to
10095 -- complete the error notification. This is especially
10096 -- useful for traceability of the chain of entities when
10097 -- the subprogram corresponds with an interface
10098 -- subprogram (which may be defined in another package).
10099
10100 if Present (Alias_Subp) then
10101 declare
10102 E : Entity_Id;
10103
10104 begin
10105 E := Subp;
10106 while Present (Alias (E)) loop
10107
10108 -- Avoid reporting redundant errors on entities
10109 -- inherited from interfaces
10110
10111 if Sloc (E) /= Sloc (T) then
10112 Error_Msg_Sloc := Sloc (E);
10113 Error_Msg_NE
10114 ("\& has been inherited #", T, Subp);
10115 end if;
10116
10117 E := Alias (E);
10118 end loop;
10119
10120 Error_Msg_Sloc := Sloc (E);
10121
10122 -- AI05-0068: report if there is an overriding
10123 -- non-abstract subprogram that is invisible.
10124
10125 if Is_Hidden (E)
10126 and then not Is_Abstract_Subprogram (E)
10127 then
10128 Error_Msg_NE
10129 ("\& subprogram# is not visible",
10130 T, Subp);
10131
10132 -- Clarify the case where a non-null extension must
10133 -- override inherited procedure subject to pragma
10134 -- Extensions_Visible with value False and at least
10135 -- one controlling OUT param.
10136
10137 elsif Is_EVF_Procedure (E) then
10138 Error_Msg_NE
10139 ("\& # is subject to Extensions_Visible False",
10140 T, Subp);
10141
10142 else
10143 Error_Msg_NE
10144 ("\& has been inherited from subprogram #",
10145 T, Subp);
10146 end if;
10147 end;
10148 end if;
10149 end if;
10150
10151 -- Ada 2005 (AI-345): Protected or task type implementing
10152 -- abstract interfaces.
10153
10154 elsif Is_Concurrent_Record_Type (T)
10155 and then Present (Interfaces (T))
10156 then
10157 -- There is no need to check here RM 9.4(11.9/3) since we
10158 -- are processing the corresponding record type and the
10159 -- mode of the overriding subprograms was verified by
10160 -- Check_Conformance when the corresponding concurrent
10161 -- type declaration was analyzed.
10162
10163 Error_Msg_NE
10164 ("interface subprogram & must be overridden", T, Subp);
10165
10166 -- Examine primitive operations of synchronized type to find
10167 -- homonyms that have the wrong profile.
10168
10169 declare
10170 Prim : Entity_Id;
10171
10172 begin
10173 Prim := First_Entity (Corresponding_Concurrent_Type (T));
10174 while Present (Prim) loop
10175 if Chars (Prim) = Chars (Subp) then
10176 Error_Msg_NE
10177 ("profile is not type conformant with prefixed "
10178 & "view profile of inherited operation&",
10179 Prim, Subp);
10180 end if;
10181
10182 Next_Entity (Prim);
10183 end loop;
10184 end;
10185 end if;
10186
10187 else
10188 Error_Msg_Node_2 := T;
10189 Error_Msg_N
10190 ("abstract subprogram& not allowed for type&", Subp);
10191
10192 -- Also post unconditional warning on the type (unconditional
10193 -- so that if there are more than one of these cases, we get
10194 -- them all, and not just the first one).
10195
10196 Error_Msg_Node_2 := Subp;
10197 Error_Msg_N ("nonabstract type& has abstract subprogram&!", T);
10198 end if;
10199
10200 -- A subprogram subject to pragma Extensions_Visible with value
10201 -- "True" cannot override a subprogram subject to the same pragma
10202 -- with value "False" (SPARK RM 6.1.7(5)).
10203
10204 elsif Extensions_Visible_Status (Subp) = Extensions_Visible_True
10205 and then Present (Overridden_Operation (Subp))
10206 and then Extensions_Visible_Status (Overridden_Operation (Subp)) =
10207 Extensions_Visible_False
10208 then
10209 Error_Msg_Sloc := Sloc (Overridden_Operation (Subp));
10210 Error_Msg_N
10211 ("subprogram & with Extensions_Visible True cannot override "
10212 & "subprogram # with Extensions_Visible False", Subp);
10213 end if;
10214
10215 -- Ada 2012 (AI05-0030): Perform checks related to pragma Implemented
10216
10217 -- Subp is an expander-generated procedure which maps an interface
10218 -- alias to a protected wrapper. The interface alias is flagged by
10219 -- pragma Implemented. Ensure that Subp is a procedure when the
10220 -- implementation kind is By_Protected_Procedure or an entry when
10221 -- By_Entry.
10222
10223 if Ada_Version >= Ada_2012
10224 and then Is_Hidden (Subp)
10225 and then Present (Interface_Alias (Subp))
10226 and then Has_Rep_Pragma (Interface_Alias (Subp), Name_Implemented)
10227 then
10228 Check_Pragma_Implemented (Subp);
10229 end if;
10230
10231 -- Subp is an interface primitive which overrides another interface
10232 -- primitive marked with pragma Implemented.
10233
10234 if Ada_Version >= Ada_2012
10235 and then Present (Overridden_Operation (Subp))
10236 and then Has_Rep_Pragma
10237 (Overridden_Operation (Subp), Name_Implemented)
10238 then
10239 -- If the overriding routine is also marked by Implemented, check
10240 -- that the two implementation kinds are conforming.
10241
10242 if Has_Rep_Pragma (Subp, Name_Implemented) then
10243 Check_Pragma_Implemented
10244 (Subp => Subp,
10245 Iface_Subp => Overridden_Operation (Subp));
10246
10247 -- Otherwise the overriding routine inherits the implementation
10248 -- kind from the overridden subprogram.
10249
10250 else
10251 Inherit_Pragma_Implemented
10252 (Subp => Subp,
10253 Iface_Subp => Overridden_Operation (Subp));
10254 end if;
10255 end if;
10256
10257 -- If the operation is a wrapper for a synchronized primitive, it
10258 -- may be called indirectly through a dispatching select. We assume
10259 -- that it will be referenced elsewhere indirectly, and suppress
10260 -- warnings about an unused entity.
10261
10262 if Is_Primitive_Wrapper (Subp)
10263 and then Present (Wrapped_Entity (Subp))
10264 then
10265 Set_Referenced (Wrapped_Entity (Subp));
10266 end if;
10267
10268 Next_Elmt (Elmt);
10269 end loop;
10270 end Check_Abstract_Overriding;
10271
10272 ------------------------------------------------
10273 -- Check_Access_Discriminant_Requires_Limited --
10274 ------------------------------------------------
10275
10276 procedure Check_Access_Discriminant_Requires_Limited
10277 (D : Node_Id;
10278 Loc : Node_Id)
10279 is
10280 begin
10281 -- A discriminant_specification for an access discriminant shall appear
10282 -- only in the declaration for a task or protected type, or for a type
10283 -- with the reserved word 'limited' in its definition or in one of its
10284 -- ancestors (RM 3.7(10)).
10285
10286 -- AI-0063: The proper condition is that type must be immutably limited,
10287 -- or else be a partial view.
10288
10289 if Nkind (Discriminant_Type (D)) = N_Access_Definition then
10290 if Is_Limited_View (Current_Scope)
10291 or else
10292 (Nkind (Parent (Current_Scope)) = N_Private_Type_Declaration
10293 and then Limited_Present (Parent (Current_Scope)))
10294 then
10295 null;
10296
10297 else
10298 Error_Msg_N
10299 ("access discriminants allowed only for limited types", Loc);
10300 end if;
10301 end if;
10302 end Check_Access_Discriminant_Requires_Limited;
10303
10304 -----------------------------------
10305 -- Check_Aliased_Component_Types --
10306 -----------------------------------
10307
10308 procedure Check_Aliased_Component_Types (T : Entity_Id) is
10309 C : Entity_Id;
10310
10311 begin
10312 -- ??? Also need to check components of record extensions, but not
10313 -- components of protected types (which are always limited).
10314
10315 -- Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
10316 -- types to be unconstrained. This is safe because it is illegal to
10317 -- create access subtypes to such types with explicit discriminant
10318 -- constraints.
10319
10320 if not Is_Limited_Type (T) then
10321 if Ekind (T) = E_Record_Type then
10322 C := First_Component (T);
10323 while Present (C) loop
10324 if Is_Aliased (C)
10325 and then Has_Discriminants (Etype (C))
10326 and then not Is_Constrained (Etype (C))
10327 and then not In_Instance_Body
10328 and then Ada_Version < Ada_2005
10329 then
10330 Error_Msg_N
10331 ("aliased component must be constrained (RM 3.6(11))",
10332 C);
10333 end if;
10334
10335 Next_Component (C);
10336 end loop;
10337
10338 elsif Ekind (T) = E_Array_Type then
10339 if Has_Aliased_Components (T)
10340 and then Has_Discriminants (Component_Type (T))
10341 and then not Is_Constrained (Component_Type (T))
10342 and then not In_Instance_Body
10343 and then Ada_Version < Ada_2005
10344 then
10345 Error_Msg_N
10346 ("aliased component type must be constrained (RM 3.6(11))",
10347 T);
10348 end if;
10349 end if;
10350 end if;
10351 end Check_Aliased_Component_Types;
10352
10353 ---------------------------------------
10354 -- Check_Anonymous_Access_Components --
10355 ---------------------------------------
10356
10357 procedure Check_Anonymous_Access_Components
10358 (Typ_Decl : Node_Id;
10359 Typ : Entity_Id;
10360 Prev : Entity_Id;
10361 Comp_List : Node_Id)
10362 is
10363 Loc : constant Source_Ptr := Sloc (Typ_Decl);
10364 Anon_Access : Entity_Id;
10365 Acc_Def : Node_Id;
10366 Comp : Node_Id;
10367 Comp_Def : Node_Id;
10368 Decl : Node_Id;
10369 Type_Def : Node_Id;
10370
10371 procedure Build_Incomplete_Type_Declaration;
10372 -- If the record type contains components that include an access to the
10373 -- current record, then create an incomplete type declaration for the
10374 -- record, to be used as the designated type of the anonymous access.
10375 -- This is done only once, and only if there is no previous partial
10376 -- view of the type.
10377
10378 function Designates_T (Subt : Node_Id) return Boolean;
10379 -- Check whether a node designates the enclosing record type, or 'Class
10380 -- of that type
10381
10382 function Mentions_T (Acc_Def : Node_Id) return Boolean;
10383 -- Check whether an access definition includes a reference to
10384 -- the enclosing record type. The reference can be a subtype mark
10385 -- in the access definition itself, a 'Class attribute reference, or
10386 -- recursively a reference appearing in a parameter specification
10387 -- or result definition of an access_to_subprogram definition.
10388
10389 --------------------------------------
10390 -- Build_Incomplete_Type_Declaration --
10391 --------------------------------------
10392
10393 procedure Build_Incomplete_Type_Declaration is
10394 Decl : Node_Id;
10395 Inc_T : Entity_Id;
10396 H : Entity_Id;
10397
10398 -- Is_Tagged indicates whether the type is tagged. It is tagged if
10399 -- it's "is new ... with record" or else "is tagged record ...".
10400
10401 Is_Tagged : constant Boolean :=
10402 (Nkind (Type_Definition (Typ_Decl)) = N_Derived_Type_Definition
10403 and then
10404 Present (Record_Extension_Part (Type_Definition (Typ_Decl))))
10405 or else
10406 (Nkind (Type_Definition (Typ_Decl)) = N_Record_Definition
10407 and then Tagged_Present (Type_Definition (Typ_Decl)));
10408
10409 begin
10410 -- If there is a previous partial view, no need to create a new one
10411 -- If the partial view, given by Prev, is incomplete, If Prev is
10412 -- a private declaration, full declaration is flagged accordingly.
10413
10414 if Prev /= Typ then
10415 if Is_Tagged then
10416 Make_Class_Wide_Type (Prev);
10417 Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
10418 Set_Etype (Class_Wide_Type (Typ), Typ);
10419 end if;
10420
10421 return;
10422
10423 elsif Has_Private_Declaration (Typ) then
10424
10425 -- If we refer to T'Class inside T, and T is the completion of a
10426 -- private type, then make sure the class-wide type exists.
10427
10428 if Is_Tagged then
10429 Make_Class_Wide_Type (Typ);
10430 end if;
10431
10432 return;
10433
10434 -- If there was a previous anonymous access type, the incomplete
10435 -- type declaration will have been created already.
10436
10437 elsif Present (Current_Entity (Typ))
10438 and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
10439 and then Full_View (Current_Entity (Typ)) = Typ
10440 then
10441 if Is_Tagged
10442 and then Comes_From_Source (Current_Entity (Typ))
10443 and then not Is_Tagged_Type (Current_Entity (Typ))
10444 then
10445 Make_Class_Wide_Type (Typ);
10446 Error_Msg_N
10447 ("incomplete view of tagged type should be declared tagged??",
10448 Parent (Current_Entity (Typ)));
10449 end if;
10450 return;
10451
10452 else
10453 Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
10454 Decl := Make_Incomplete_Type_Declaration (Loc, Inc_T);
10455
10456 -- Type has already been inserted into the current scope. Remove
10457 -- it, and add incomplete declaration for type, so that subsequent
10458 -- anonymous access types can use it. The entity is unchained from
10459 -- the homonym list and from immediate visibility. After analysis,
10460 -- the entity in the incomplete declaration becomes immediately
10461 -- visible in the record declaration that follows.
10462
10463 H := Current_Entity (Typ);
10464
10465 if H = Typ then
10466 Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
10467 else
10468 while Present (H)
10469 and then Homonym (H) /= Typ
10470 loop
10471 H := Homonym (Typ);
10472 end loop;
10473
10474 Set_Homonym (H, Homonym (Typ));
10475 end if;
10476
10477 Insert_Before (Typ_Decl, Decl);
10478 Analyze (Decl);
10479 Set_Full_View (Inc_T, Typ);
10480
10481 if Is_Tagged then
10482
10483 -- Create a common class-wide type for both views, and set the
10484 -- Etype of the class-wide type to the full view.
10485
10486 Make_Class_Wide_Type (Inc_T);
10487 Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
10488 Set_Etype (Class_Wide_Type (Typ), Typ);
10489 end if;
10490 end if;
10491 end Build_Incomplete_Type_Declaration;
10492
10493 ------------------
10494 -- Designates_T --
10495 ------------------
10496
10497 function Designates_T (Subt : Node_Id) return Boolean is
10498 Type_Id : constant Name_Id := Chars (Typ);
10499
10500 function Names_T (Nam : Node_Id) return Boolean;
10501 -- The record type has not been introduced in the current scope
10502 -- yet, so we must examine the name of the type itself, either
10503 -- an identifier T, or an expanded name of the form P.T, where
10504 -- P denotes the current scope.
10505
10506 -------------
10507 -- Names_T --
10508 -------------
10509
10510 function Names_T (Nam : Node_Id) return Boolean is
10511 begin
10512 if Nkind (Nam) = N_Identifier then
10513 return Chars (Nam) = Type_Id;
10514
10515 elsif Nkind (Nam) = N_Selected_Component then
10516 if Chars (Selector_Name (Nam)) = Type_Id then
10517 if Nkind (Prefix (Nam)) = N_Identifier then
10518 return Chars (Prefix (Nam)) = Chars (Current_Scope);
10519
10520 elsif Nkind (Prefix (Nam)) = N_Selected_Component then
10521 return Chars (Selector_Name (Prefix (Nam))) =
10522 Chars (Current_Scope);
10523 else
10524 return False;
10525 end if;
10526
10527 else
10528 return False;
10529 end if;
10530
10531 else
10532 return False;
10533 end if;
10534 end Names_T;
10535
10536 -- Start of processing for Designates_T
10537
10538 begin
10539 if Nkind (Subt) = N_Identifier then
10540 return Chars (Subt) = Type_Id;
10541
10542 -- Reference can be through an expanded name which has not been
10543 -- analyzed yet, and which designates enclosing scopes.
10544
10545 elsif Nkind (Subt) = N_Selected_Component then
10546 if Names_T (Subt) then
10547 return True;
10548
10549 -- Otherwise it must denote an entity that is already visible.
10550 -- The access definition may name a subtype of the enclosing
10551 -- type, if there is a previous incomplete declaration for it.
10552
10553 else
10554 Find_Selected_Component (Subt);
10555 return
10556 Is_Entity_Name (Subt)
10557 and then Scope (Entity (Subt)) = Current_Scope
10558 and then
10559 (Chars (Base_Type (Entity (Subt))) = Type_Id
10560 or else
10561 (Is_Class_Wide_Type (Entity (Subt))
10562 and then
10563 Chars (Etype (Base_Type (Entity (Subt)))) =
10564 Type_Id));
10565 end if;
10566
10567 -- A reference to the current type may appear as the prefix of
10568 -- a 'Class attribute.
10569
10570 elsif Nkind (Subt) = N_Attribute_Reference
10571 and then Attribute_Name (Subt) = Name_Class
10572 then
10573 return Names_T (Prefix (Subt));
10574
10575 else
10576 return False;
10577 end if;
10578 end Designates_T;
10579
10580 ----------------
10581 -- Mentions_T --
10582 ----------------
10583
10584 function Mentions_T (Acc_Def : Node_Id) return Boolean is
10585 Param_Spec : Node_Id;
10586
10587 Acc_Subprg : constant Node_Id :=
10588 Access_To_Subprogram_Definition (Acc_Def);
10589
10590 begin
10591 if No (Acc_Subprg) then
10592 return Designates_T (Subtype_Mark (Acc_Def));
10593 end if;
10594
10595 -- Component is an access_to_subprogram: examine its formals,
10596 -- and result definition in the case of an access_to_function.
10597
10598 Param_Spec := First (Parameter_Specifications (Acc_Subprg));
10599 while Present (Param_Spec) loop
10600 if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
10601 and then Mentions_T (Parameter_Type (Param_Spec))
10602 then
10603 return True;
10604
10605 elsif Designates_T (Parameter_Type (Param_Spec)) then
10606 return True;
10607 end if;
10608
10609 Next (Param_Spec);
10610 end loop;
10611
10612 if Nkind (Acc_Subprg) = N_Access_Function_Definition then
10613 if Nkind (Result_Definition (Acc_Subprg)) =
10614 N_Access_Definition
10615 then
10616 return Mentions_T (Result_Definition (Acc_Subprg));
10617 else
10618 return Designates_T (Result_Definition (Acc_Subprg));
10619 end if;
10620 end if;
10621
10622 return False;
10623 end Mentions_T;
10624
10625 -- Start of processing for Check_Anonymous_Access_Components
10626
10627 begin
10628 if No (Comp_List) then
10629 return;
10630 end if;
10631
10632 Comp := First (Component_Items (Comp_List));
10633 while Present (Comp) loop
10634 if Nkind (Comp) = N_Component_Declaration
10635 and then Present
10636 (Access_Definition (Component_Definition (Comp)))
10637 and then
10638 Mentions_T (Access_Definition (Component_Definition (Comp)))
10639 then
10640 Comp_Def := Component_Definition (Comp);
10641 Acc_Def :=
10642 Access_To_Subprogram_Definition (Access_Definition (Comp_Def));
10643
10644 Build_Incomplete_Type_Declaration;
10645 Anon_Access := Make_Temporary (Loc, 'S');
10646
10647 -- Create a declaration for the anonymous access type: either
10648 -- an access_to_object or an access_to_subprogram.
10649
10650 if Present (Acc_Def) then
10651 if Nkind (Acc_Def) = N_Access_Function_Definition then
10652 Type_Def :=
10653 Make_Access_Function_Definition (Loc,
10654 Parameter_Specifications =>
10655 Parameter_Specifications (Acc_Def),
10656 Result_Definition => Result_Definition (Acc_Def));
10657 else
10658 Type_Def :=
10659 Make_Access_Procedure_Definition (Loc,
10660 Parameter_Specifications =>
10661 Parameter_Specifications (Acc_Def));
10662 end if;
10663
10664 else
10665 Type_Def :=
10666 Make_Access_To_Object_Definition (Loc,
10667 Subtype_Indication =>
10668 Relocate_Node
10669 (Subtype_Mark (Access_Definition (Comp_Def))));
10670
10671 Set_Constant_Present
10672 (Type_Def, Constant_Present (Access_Definition (Comp_Def)));
10673 Set_All_Present
10674 (Type_Def, All_Present (Access_Definition (Comp_Def)));
10675 end if;
10676
10677 Set_Null_Exclusion_Present
10678 (Type_Def,
10679 Null_Exclusion_Present (Access_Definition (Comp_Def)));
10680
10681 Decl :=
10682 Make_Full_Type_Declaration (Loc,
10683 Defining_Identifier => Anon_Access,
10684 Type_Definition => Type_Def);
10685
10686 Insert_Before (Typ_Decl, Decl);
10687 Analyze (Decl);
10688
10689 -- If an access to subprogram, create the extra formals
10690
10691 if Present (Acc_Def) then
10692 Create_Extra_Formals (Designated_Type (Anon_Access));
10693
10694 -- If an access to object, preserve entity of designated type,
10695 -- for ASIS use, before rewriting the component definition.
10696
10697 else
10698 declare
10699 Desig : Entity_Id;
10700
10701 begin
10702 Desig := Entity (Subtype_Indication (Type_Def));
10703
10704 -- If the access definition is to the current record,
10705 -- the visible entity at this point is an incomplete
10706 -- type. Retrieve the full view to simplify ASIS queries
10707
10708 if Ekind (Desig) = E_Incomplete_Type then
10709 Desig := Full_View (Desig);
10710 end if;
10711
10712 Set_Entity
10713 (Subtype_Mark (Access_Definition (Comp_Def)), Desig);
10714 end;
10715 end if;
10716
10717 Rewrite (Comp_Def,
10718 Make_Component_Definition (Loc,
10719 Subtype_Indication =>
10720 New_Occurrence_Of (Anon_Access, Loc)));
10721
10722 if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
10723 Set_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
10724 else
10725 Set_Ekind (Anon_Access, E_Anonymous_Access_Type);
10726 end if;
10727
10728 Set_Is_Local_Anonymous_Access (Anon_Access);
10729 end if;
10730
10731 Next (Comp);
10732 end loop;
10733
10734 if Present (Variant_Part (Comp_List)) then
10735 declare
10736 V : Node_Id;
10737 begin
10738 V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
10739 while Present (V) loop
10740 Check_Anonymous_Access_Components
10741 (Typ_Decl, Typ, Prev, Component_List (V));
10742 Next_Non_Pragma (V);
10743 end loop;
10744 end;
10745 end if;
10746 end Check_Anonymous_Access_Components;
10747
10748 ----------------------
10749 -- Check_Completion --
10750 ----------------------
10751
10752 procedure Check_Completion (Body_Id : Node_Id := Empty) is
10753 E : Entity_Id;
10754
10755 procedure Post_Error;
10756 -- Post error message for lack of completion for entity E
10757
10758 ----------------
10759 -- Post_Error --
10760 ----------------
10761
10762 procedure Post_Error is
10763
10764 procedure Missing_Body;
10765 -- Output missing body message
10766
10767 ------------------
10768 -- Missing_Body --
10769 ------------------
10770
10771 procedure Missing_Body is
10772 begin
10773 -- Spec is in same unit, so we can post on spec
10774
10775 if In_Same_Source_Unit (Body_Id, E) then
10776 Error_Msg_N ("missing body for &", E);
10777
10778 -- Spec is in a separate unit, so we have to post on the body
10779
10780 else
10781 Error_Msg_NE ("missing body for & declared#!", Body_Id, E);
10782 end if;
10783 end Missing_Body;
10784
10785 -- Start of processing for Post_Error
10786
10787 begin
10788 if not Comes_From_Source (E) then
10789
10790 if Ekind_In (E, E_Task_Type, E_Protected_Type) then
10791
10792 -- It may be an anonymous protected type created for a
10793 -- single variable. Post error on variable, if present.
10794
10795 declare
10796 Var : Entity_Id;
10797
10798 begin
10799 Var := First_Entity (Current_Scope);
10800 while Present (Var) loop
10801 exit when Etype (Var) = E
10802 and then Comes_From_Source (Var);
10803
10804 Next_Entity (Var);
10805 end loop;
10806
10807 if Present (Var) then
10808 E := Var;
10809 end if;
10810 end;
10811 end if;
10812 end if;
10813
10814 -- If a generated entity has no completion, then either previous
10815 -- semantic errors have disabled the expansion phase, or else we had
10816 -- missing subunits, or else we are compiling without expansion,
10817 -- or else something is very wrong.
10818
10819 if not Comes_From_Source (E) then
10820 pragma Assert
10821 (Serious_Errors_Detected > 0
10822 or else Configurable_Run_Time_Violations > 0
10823 or else Subunits_Missing
10824 or else not Expander_Active);
10825 return;
10826
10827 -- Here for source entity
10828
10829 else
10830 -- Here if no body to post the error message, so we post the error
10831 -- on the declaration that has no completion. This is not really
10832 -- the right place to post it, think about this later ???
10833
10834 if No (Body_Id) then
10835 if Is_Type (E) then
10836 Error_Msg_NE
10837 ("missing full declaration for }", Parent (E), E);
10838 else
10839 Error_Msg_NE ("missing body for &", Parent (E), E);
10840 end if;
10841
10842 -- Package body has no completion for a declaration that appears
10843 -- in the corresponding spec. Post error on the body, with a
10844 -- reference to the non-completed declaration.
10845
10846 else
10847 Error_Msg_Sloc := Sloc (E);
10848
10849 if Is_Type (E) then
10850 Error_Msg_NE ("missing full declaration for }!", Body_Id, E);
10851
10852 elsif Is_Overloadable (E)
10853 and then Current_Entity_In_Scope (E) /= E
10854 then
10855 -- It may be that the completion is mistyped and appears as
10856 -- a distinct overloading of the entity.
10857
10858 declare
10859 Candidate : constant Entity_Id :=
10860 Current_Entity_In_Scope (E);
10861 Decl : constant Node_Id :=
10862 Unit_Declaration_Node (Candidate);
10863
10864 begin
10865 if Is_Overloadable (Candidate)
10866 and then Ekind (Candidate) = Ekind (E)
10867 and then Nkind (Decl) = N_Subprogram_Body
10868 and then Acts_As_Spec (Decl)
10869 then
10870 Check_Type_Conformant (Candidate, E);
10871
10872 else
10873 Missing_Body;
10874 end if;
10875 end;
10876
10877 else
10878 Missing_Body;
10879 end if;
10880 end if;
10881 end if;
10882 end Post_Error;
10883
10884 -- Local variables
10885
10886 Pack_Id : constant Entity_Id := Current_Scope;
10887
10888 -- Start of processing for Check_Completion
10889
10890 begin
10891 E := First_Entity (Pack_Id);
10892 while Present (E) loop
10893 if Is_Intrinsic_Subprogram (E) then
10894 null;
10895
10896 -- A Ghost entity declared in a non-Ghost package does not force the
10897 -- need for a body (SPARK RM 6.9(11)).
10898
10899 elsif not Is_Ghost_Entity (Pack_Id) and then Is_Ghost_Entity (E) then
10900 null;
10901
10902 -- The following situation requires special handling: a child unit
10903 -- that appears in the context clause of the body of its parent:
10904
10905 -- procedure Parent.Child (...);
10906
10907 -- with Parent.Child;
10908 -- package body Parent is
10909
10910 -- Here Parent.Child appears as a local entity, but should not be
10911 -- flagged as requiring completion, because it is a compilation
10912 -- unit.
10913
10914 -- Ignore missing completion for a subprogram that does not come from
10915 -- source (including the _Call primitive operation of RAS types,
10916 -- which has to have the flag Comes_From_Source for other purposes):
10917 -- we assume that the expander will provide the missing completion.
10918 -- In case of previous errors, other expansion actions that provide
10919 -- bodies for null procedures with not be invoked, so inhibit message
10920 -- in those cases.
10921
10922 -- Note that E_Operator is not in the list that follows, because
10923 -- this kind is reserved for predefined operators, that are
10924 -- intrinsic and do not need completion.
10925
10926 elsif Ekind_In (E, E_Function,
10927 E_Procedure,
10928 E_Generic_Function,
10929 E_Generic_Procedure)
10930 then
10931 if Has_Completion (E) then
10932 null;
10933
10934 elsif Is_Subprogram (E) and then Is_Abstract_Subprogram (E) then
10935 null;
10936
10937 elsif Is_Subprogram (E)
10938 and then (not Comes_From_Source (E)
10939 or else Chars (E) = Name_uCall)
10940 then
10941 null;
10942
10943 elsif
10944 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
10945 then
10946 null;
10947
10948 elsif Nkind (Parent (E)) = N_Procedure_Specification
10949 and then Null_Present (Parent (E))
10950 and then Serious_Errors_Detected > 0
10951 then
10952 null;
10953
10954 else
10955 Post_Error;
10956 end if;
10957
10958 elsif Is_Entry (E) then
10959 if not Has_Completion (E) and then
10960 (Ekind (Scope (E)) = E_Protected_Object
10961 or else Ekind (Scope (E)) = E_Protected_Type)
10962 then
10963 Post_Error;
10964 end if;
10965
10966 elsif Is_Package_Or_Generic_Package (E) then
10967 if Unit_Requires_Body (E) then
10968 if not Has_Completion (E)
10969 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
10970 N_Compilation_Unit
10971 then
10972 Post_Error;
10973 end if;
10974
10975 elsif not Is_Child_Unit (E) then
10976 May_Need_Implicit_Body (E);
10977 end if;
10978
10979 -- A formal incomplete type (Ada 2012) does not require a completion;
10980 -- other incomplete type declarations do.
10981
10982 elsif Ekind (E) = E_Incomplete_Type
10983 and then No (Underlying_Type (E))
10984 and then not Is_Generic_Type (E)
10985 then
10986 Post_Error;
10987
10988 elsif Ekind_In (E, E_Task_Type, E_Protected_Type)
10989 and then not Has_Completion (E)
10990 then
10991 Post_Error;
10992
10993 -- A single task declared in the current scope is a constant, verify
10994 -- that the body of its anonymous type is in the same scope. If the
10995 -- task is defined elsewhere, this may be a renaming declaration for
10996 -- which no completion is needed.
10997
10998 elsif Ekind (E) = E_Constant
10999 and then Ekind (Etype (E)) = E_Task_Type
11000 and then not Has_Completion (Etype (E))
11001 and then Scope (Etype (E)) = Current_Scope
11002 then
11003 Post_Error;
11004
11005 elsif Ekind (E) = E_Protected_Object
11006 and then not Has_Completion (Etype (E))
11007 then
11008 Post_Error;
11009
11010 elsif Ekind (E) = E_Record_Type then
11011 if Is_Tagged_Type (E) then
11012 Check_Abstract_Overriding (E);
11013 Check_Conventions (E);
11014 end if;
11015
11016 Check_Aliased_Component_Types (E);
11017
11018 elsif Ekind (E) = E_Array_Type then
11019 Check_Aliased_Component_Types (E);
11020
11021 end if;
11022
11023 Next_Entity (E);
11024 end loop;
11025 end Check_Completion;
11026
11027 ------------------------------------
11028 -- Check_CPP_Type_Has_No_Defaults --
11029 ------------------------------------
11030
11031 procedure Check_CPP_Type_Has_No_Defaults (T : Entity_Id) is
11032 Tdef : constant Node_Id := Type_Definition (Declaration_Node (T));
11033 Clist : Node_Id;
11034 Comp : Node_Id;
11035
11036 begin
11037 -- Obtain the component list
11038
11039 if Nkind (Tdef) = N_Record_Definition then
11040 Clist := Component_List (Tdef);
11041 else pragma Assert (Nkind (Tdef) = N_Derived_Type_Definition);
11042 Clist := Component_List (Record_Extension_Part (Tdef));
11043 end if;
11044
11045 -- Check all components to ensure no default expressions
11046
11047 if Present (Clist) then
11048 Comp := First (Component_Items (Clist));
11049 while Present (Comp) loop
11050 if Present (Expression (Comp)) then
11051 Error_Msg_N
11052 ("component of imported 'C'P'P type cannot have "
11053 & "default expression", Expression (Comp));
11054 end if;
11055
11056 Next (Comp);
11057 end loop;
11058 end if;
11059 end Check_CPP_Type_Has_No_Defaults;
11060
11061 ----------------------------
11062 -- Check_Delta_Expression --
11063 ----------------------------
11064
11065 procedure Check_Delta_Expression (E : Node_Id) is
11066 begin
11067 if not (Is_Real_Type (Etype (E))) then
11068 Wrong_Type (E, Any_Real);
11069
11070 elsif not Is_OK_Static_Expression (E) then
11071 Flag_Non_Static_Expr
11072 ("non-static expression used for delta value!", E);
11073
11074 elsif not UR_Is_Positive (Expr_Value_R (E)) then
11075 Error_Msg_N ("delta expression must be positive", E);
11076
11077 else
11078 return;
11079 end if;
11080
11081 -- If any of above errors occurred, then replace the incorrect
11082 -- expression by the real 0.1, which should prevent further errors.
11083
11084 Rewrite (E,
11085 Make_Real_Literal (Sloc (E), Ureal_Tenth));
11086 Analyze_And_Resolve (E, Standard_Float);
11087 end Check_Delta_Expression;
11088
11089 -----------------------------
11090 -- Check_Digits_Expression --
11091 -----------------------------
11092
11093 procedure Check_Digits_Expression (E : Node_Id) is
11094 begin
11095 if not (Is_Integer_Type (Etype (E))) then
11096 Wrong_Type (E, Any_Integer);
11097
11098 elsif not Is_OK_Static_Expression (E) then
11099 Flag_Non_Static_Expr
11100 ("non-static expression used for digits value!", E);
11101
11102 elsif Expr_Value (E) <= 0 then
11103 Error_Msg_N ("digits value must be greater than zero", E);
11104
11105 else
11106 return;
11107 end if;
11108
11109 -- If any of above errors occurred, then replace the incorrect
11110 -- expression by the integer 1, which should prevent further errors.
11111
11112 Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
11113 Analyze_And_Resolve (E, Standard_Integer);
11114
11115 end Check_Digits_Expression;
11116
11117 --------------------------
11118 -- Check_Initialization --
11119 --------------------------
11120
11121 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
11122 begin
11123 -- Special processing for limited types
11124
11125 if Is_Limited_Type (T)
11126 and then not In_Instance
11127 and then not In_Inlined_Body
11128 then
11129 if not OK_For_Limited_Init (T, Exp) then
11130
11131 -- In GNAT mode, this is just a warning, to allow it to be evilly
11132 -- turned off. Otherwise it is a real error.
11133
11134 if GNAT_Mode then
11135 Error_Msg_N
11136 ("??cannot initialize entities of limited type!", Exp);
11137
11138 elsif Ada_Version < Ada_2005 then
11139
11140 -- The side effect removal machinery may generate illegal Ada
11141 -- code to avoid the usage of access types and 'reference in
11142 -- SPARK mode. Since this is legal code with respect to theorem
11143 -- proving, do not emit the error.
11144
11145 if GNATprove_Mode
11146 and then Nkind (Exp) = N_Function_Call
11147 and then Nkind (Parent (Exp)) = N_Object_Declaration
11148 and then not Comes_From_Source
11149 (Defining_Identifier (Parent (Exp)))
11150 then
11151 null;
11152
11153 else
11154 Error_Msg_N
11155 ("cannot initialize entities of limited type", Exp);
11156 Explain_Limited_Type (T, Exp);
11157 end if;
11158
11159 else
11160 -- Specialize error message according to kind of illegal
11161 -- initial expression.
11162
11163 if Nkind (Exp) = N_Type_Conversion
11164 and then Nkind (Expression (Exp)) = N_Function_Call
11165 then
11166 Error_Msg_N
11167 ("illegal context for call"
11168 & " to function with limited result", Exp);
11169
11170 else
11171 Error_Msg_N
11172 ("initialization of limited object requires aggregate "
11173 & "or function call", Exp);
11174 end if;
11175 end if;
11176 end if;
11177 end if;
11178
11179 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag gets
11180 -- set unless we can be sure that no range check is required.
11181
11182 if (GNATprove_Mode or not Expander_Active)
11183 and then Is_Scalar_Type (T)
11184 and then not Is_In_Range (Exp, T, Assume_Valid => True)
11185 then
11186 Set_Do_Range_Check (Exp);
11187 end if;
11188 end Check_Initialization;
11189
11190 ----------------------
11191 -- Check_Interfaces --
11192 ----------------------
11193
11194 procedure Check_Interfaces (N : Node_Id; Def : Node_Id) is
11195 Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
11196
11197 Iface : Node_Id;
11198 Iface_Def : Node_Id;
11199 Iface_Typ : Entity_Id;
11200 Parent_Node : Node_Id;
11201
11202 Is_Task : Boolean := False;
11203 -- Set True if parent type or any progenitor is a task interface
11204
11205 Is_Protected : Boolean := False;
11206 -- Set True if parent type or any progenitor is a protected interface
11207
11208 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
11209 -- Check that a progenitor is compatible with declaration. If an error
11210 -- message is output, it is posted on Error_Node.
11211
11212 ------------------
11213 -- Check_Ifaces --
11214 ------------------
11215
11216 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
11217 Iface_Id : constant Entity_Id :=
11218 Defining_Identifier (Parent (Iface_Def));
11219 Type_Def : Node_Id;
11220
11221 begin
11222 if Nkind (N) = N_Private_Extension_Declaration then
11223 Type_Def := N;
11224 else
11225 Type_Def := Type_Definition (N);
11226 end if;
11227
11228 if Is_Task_Interface (Iface_Id) then
11229 Is_Task := True;
11230
11231 elsif Is_Protected_Interface (Iface_Id) then
11232 Is_Protected := True;
11233 end if;
11234
11235 if Is_Synchronized_Interface (Iface_Id) then
11236
11237 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
11238 -- extension derived from a synchronized interface must explicitly
11239 -- be declared synchronized, because the full view will be a
11240 -- synchronized type.
11241
11242 if Nkind (N) = N_Private_Extension_Declaration then
11243 if not Synchronized_Present (N) then
11244 Error_Msg_NE
11245 ("private extension of& must be explicitly synchronized",
11246 N, Iface_Id);
11247 end if;
11248
11249 -- However, by 3.9.4(16/2), a full type that is a record extension
11250 -- is never allowed to derive from a synchronized interface (note
11251 -- that interfaces must be excluded from this check, because those
11252 -- are represented by derived type definitions in some cases).
11253
11254 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition
11255 and then not Interface_Present (Type_Definition (N))
11256 then
11257 Error_Msg_N ("record extension cannot derive from synchronized "
11258 & "interface", Error_Node);
11259 end if;
11260 end if;
11261
11262 -- Check that the characteristics of the progenitor are compatible
11263 -- with the explicit qualifier in the declaration.
11264 -- The check only applies to qualifiers that come from source.
11265 -- Limited_Present also appears in the declaration of corresponding
11266 -- records, and the check does not apply to them.
11267
11268 if Limited_Present (Type_Def)
11269 and then not
11270 Is_Concurrent_Record_Type (Defining_Identifier (N))
11271 then
11272 if Is_Limited_Interface (Parent_Type)
11273 and then not Is_Limited_Interface (Iface_Id)
11274 then
11275 Error_Msg_NE
11276 ("progenitor & must be limited interface",
11277 Error_Node, Iface_Id);
11278
11279 elsif
11280 (Task_Present (Iface_Def)
11281 or else Protected_Present (Iface_Def)
11282 or else Synchronized_Present (Iface_Def))
11283 and then Nkind (N) /= N_Private_Extension_Declaration
11284 and then not Error_Posted (N)
11285 then
11286 Error_Msg_NE
11287 ("progenitor & must be limited interface",
11288 Error_Node, Iface_Id);
11289 end if;
11290
11291 -- Protected interfaces can only inherit from limited, synchronized
11292 -- or protected interfaces.
11293
11294 elsif Nkind (N) = N_Full_Type_Declaration
11295 and then Protected_Present (Type_Def)
11296 then
11297 if Limited_Present (Iface_Def)
11298 or else Synchronized_Present (Iface_Def)
11299 or else Protected_Present (Iface_Def)
11300 then
11301 null;
11302
11303 elsif Task_Present (Iface_Def) then
11304 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
11305 & "from task interface", Error_Node);
11306
11307 else
11308 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
11309 & "from non-limited interface", Error_Node);
11310 end if;
11311
11312 -- Ada 2005 (AI-345): Synchronized interfaces can only inherit from
11313 -- limited and synchronized.
11314
11315 elsif Synchronized_Present (Type_Def) then
11316 if Limited_Present (Iface_Def)
11317 or else Synchronized_Present (Iface_Def)
11318 then
11319 null;
11320
11321 elsif Protected_Present (Iface_Def)
11322 and then Nkind (N) /= N_Private_Extension_Declaration
11323 then
11324 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11325 & "from protected interface", Error_Node);
11326
11327 elsif Task_Present (Iface_Def)
11328 and then Nkind (N) /= N_Private_Extension_Declaration
11329 then
11330 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11331 & "from task interface", Error_Node);
11332
11333 elsif not Is_Limited_Interface (Iface_Id) then
11334 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11335 & "from non-limited interface", Error_Node);
11336 end if;
11337
11338 -- Ada 2005 (AI-345): Task interfaces can only inherit from limited,
11339 -- synchronized or task interfaces.
11340
11341 elsif Nkind (N) = N_Full_Type_Declaration
11342 and then Task_Present (Type_Def)
11343 then
11344 if Limited_Present (Iface_Def)
11345 or else Synchronized_Present (Iface_Def)
11346 or else Task_Present (Iface_Def)
11347 then
11348 null;
11349
11350 elsif Protected_Present (Iface_Def) then
11351 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
11352 & "protected interface", Error_Node);
11353
11354 else
11355 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
11356 & "non-limited interface", Error_Node);
11357 end if;
11358 end if;
11359 end Check_Ifaces;
11360
11361 -- Start of processing for Check_Interfaces
11362
11363 begin
11364 if Is_Interface (Parent_Type) then
11365 if Is_Task_Interface (Parent_Type) then
11366 Is_Task := True;
11367
11368 elsif Is_Protected_Interface (Parent_Type) then
11369 Is_Protected := True;
11370 end if;
11371 end if;
11372
11373 if Nkind (N) = N_Private_Extension_Declaration then
11374
11375 -- Check that progenitors are compatible with declaration
11376
11377 Iface := First (Interface_List (Def));
11378 while Present (Iface) loop
11379 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
11380
11381 Parent_Node := Parent (Base_Type (Iface_Typ));
11382 Iface_Def := Type_Definition (Parent_Node);
11383
11384 if not Is_Interface (Iface_Typ) then
11385 Diagnose_Interface (Iface, Iface_Typ);
11386 else
11387 Check_Ifaces (Iface_Def, Iface);
11388 end if;
11389
11390 Next (Iface);
11391 end loop;
11392
11393 if Is_Task and Is_Protected then
11394 Error_Msg_N
11395 ("type cannot derive from task and protected interface", N);
11396 end if;
11397
11398 return;
11399 end if;
11400
11401 -- Full type declaration of derived type.
11402 -- Check compatibility with parent if it is interface type
11403
11404 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
11405 and then Is_Interface (Parent_Type)
11406 then
11407 Parent_Node := Parent (Parent_Type);
11408
11409 -- More detailed checks for interface varieties
11410
11411 Check_Ifaces
11412 (Iface_Def => Type_Definition (Parent_Node),
11413 Error_Node => Subtype_Indication (Type_Definition (N)));
11414 end if;
11415
11416 Iface := First (Interface_List (Def));
11417 while Present (Iface) loop
11418 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
11419
11420 Parent_Node := Parent (Base_Type (Iface_Typ));
11421 Iface_Def := Type_Definition (Parent_Node);
11422
11423 if not Is_Interface (Iface_Typ) then
11424 Diagnose_Interface (Iface, Iface_Typ);
11425
11426 else
11427 -- "The declaration of a specific descendant of an interface
11428 -- type freezes the interface type" RM 13.14
11429
11430 Freeze_Before (N, Iface_Typ);
11431 Check_Ifaces (Iface_Def, Error_Node => Iface);
11432 end if;
11433
11434 Next (Iface);
11435 end loop;
11436
11437 if Is_Task and Is_Protected then
11438 Error_Msg_N
11439 ("type cannot derive from task and protected interface", N);
11440 end if;
11441 end Check_Interfaces;
11442
11443 ------------------------------------
11444 -- Check_Or_Process_Discriminants --
11445 ------------------------------------
11446
11447 -- If an incomplete or private type declaration was already given for the
11448 -- type, the discriminants may have already been processed if they were
11449 -- present on the incomplete declaration. In this case a full conformance
11450 -- check has been performed in Find_Type_Name, and we then recheck here
11451 -- some properties that can't be checked on the partial view alone.
11452 -- Otherwise we call Process_Discriminants.
11453
11454 procedure Check_Or_Process_Discriminants
11455 (N : Node_Id;
11456 T : Entity_Id;
11457 Prev : Entity_Id := Empty)
11458 is
11459 begin
11460 if Has_Discriminants (T) then
11461
11462 -- Discriminants are already set on T if they were already present
11463 -- on the partial view. Make them visible to component declarations.
11464
11465 declare
11466 D : Entity_Id;
11467 -- Discriminant on T (full view) referencing expr on partial view
11468
11469 Prev_D : Entity_Id;
11470 -- Entity of corresponding discriminant on partial view
11471
11472 New_D : Node_Id;
11473 -- Discriminant specification for full view, expression is
11474 -- the syntactic copy on full view (which has been checked for
11475 -- conformance with partial view), only used here to post error
11476 -- message.
11477
11478 begin
11479 D := First_Discriminant (T);
11480 New_D := First (Discriminant_Specifications (N));
11481 while Present (D) loop
11482 Prev_D := Current_Entity (D);
11483 Set_Current_Entity (D);
11484 Set_Is_Immediately_Visible (D);
11485 Set_Homonym (D, Prev_D);
11486
11487 -- Handle the case where there is an untagged partial view and
11488 -- the full view is tagged: must disallow discriminants with
11489 -- defaults, unless compiling for Ada 2012, which allows a
11490 -- limited tagged type to have defaulted discriminants (see
11491 -- AI05-0214). However, suppress error here if it was already
11492 -- reported on the default expression of the partial view.
11493
11494 if Is_Tagged_Type (T)
11495 and then Present (Expression (Parent (D)))
11496 and then (not Is_Limited_Type (Current_Scope)
11497 or else Ada_Version < Ada_2012)
11498 and then not Error_Posted (Expression (Parent (D)))
11499 then
11500 if Ada_Version >= Ada_2012 then
11501 Error_Msg_N
11502 ("discriminants of nonlimited tagged type cannot have "
11503 & "defaults",
11504 Expression (New_D));
11505 else
11506 Error_Msg_N
11507 ("discriminants of tagged type cannot have defaults",
11508 Expression (New_D));
11509 end if;
11510 end if;
11511
11512 -- Ada 2005 (AI-230): Access discriminant allowed in
11513 -- non-limited record types.
11514
11515 if Ada_Version < Ada_2005 then
11516
11517 -- This restriction gets applied to the full type here. It
11518 -- has already been applied earlier to the partial view.
11519
11520 Check_Access_Discriminant_Requires_Limited (Parent (D), N);
11521 end if;
11522
11523 Next_Discriminant (D);
11524 Next (New_D);
11525 end loop;
11526 end;
11527
11528 elsif Present (Discriminant_Specifications (N)) then
11529 Process_Discriminants (N, Prev);
11530 end if;
11531 end Check_Or_Process_Discriminants;
11532
11533 ----------------------
11534 -- Check_Real_Bound --
11535 ----------------------
11536
11537 procedure Check_Real_Bound (Bound : Node_Id) is
11538 begin
11539 if not Is_Real_Type (Etype (Bound)) then
11540 Error_Msg_N
11541 ("bound in real type definition must be of real type", Bound);
11542
11543 elsif not Is_OK_Static_Expression (Bound) then
11544 Flag_Non_Static_Expr
11545 ("non-static expression used for real type bound!", Bound);
11546
11547 else
11548 return;
11549 end if;
11550
11551 Rewrite
11552 (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
11553 Analyze (Bound);
11554 Resolve (Bound, Standard_Float);
11555 end Check_Real_Bound;
11556
11557 ------------------------------
11558 -- Complete_Private_Subtype --
11559 ------------------------------
11560
11561 procedure Complete_Private_Subtype
11562 (Priv : Entity_Id;
11563 Full : Entity_Id;
11564 Full_Base : Entity_Id;
11565 Related_Nod : Node_Id)
11566 is
11567 Save_Next_Entity : Entity_Id;
11568 Save_Homonym : Entity_Id;
11569
11570 begin
11571 -- Set semantic attributes for (implicit) private subtype completion.
11572 -- If the full type has no discriminants, then it is a copy of the
11573 -- full view of the base. Otherwise, it is a subtype of the base with
11574 -- a possible discriminant constraint. Save and restore the original
11575 -- Next_Entity field of full to ensure that the calls to Copy_Node do
11576 -- not corrupt the entity chain.
11577
11578 -- Note that the type of the full view is the same entity as the type
11579 -- of the partial view. In this fashion, the subtype has access to the
11580 -- correct view of the parent.
11581
11582 Save_Next_Entity := Next_Entity (Full);
11583 Save_Homonym := Homonym (Priv);
11584
11585 case Ekind (Full_Base) is
11586 when E_Record_Type |
11587 E_Record_Subtype |
11588 Class_Wide_Kind |
11589 Private_Kind |
11590 Task_Kind |
11591 Protected_Kind =>
11592 Copy_Node (Priv, Full);
11593
11594 Set_Has_Discriminants
11595 (Full, Has_Discriminants (Full_Base));
11596 Set_Has_Unknown_Discriminants
11597 (Full, Has_Unknown_Discriminants (Full_Base));
11598 Set_First_Entity (Full, First_Entity (Full_Base));
11599 Set_Last_Entity (Full, Last_Entity (Full_Base));
11600
11601 -- If the underlying base type is constrained, we know that the
11602 -- full view of the subtype is constrained as well (the converse
11603 -- is not necessarily true).
11604
11605 if Is_Constrained (Full_Base) then
11606 Set_Is_Constrained (Full);
11607 end if;
11608
11609 when others =>
11610 Copy_Node (Full_Base, Full);
11611
11612 Set_Chars (Full, Chars (Priv));
11613 Conditional_Delay (Full, Priv);
11614 Set_Sloc (Full, Sloc (Priv));
11615 end case;
11616
11617 Set_Next_Entity (Full, Save_Next_Entity);
11618 Set_Homonym (Full, Save_Homonym);
11619 Set_Associated_Node_For_Itype (Full, Related_Nod);
11620
11621 -- Set common attributes for all subtypes: kind, convention, etc.
11622
11623 Set_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
11624 Set_Convention (Full, Convention (Full_Base));
11625
11626 -- The Etype of the full view is inconsistent. Gigi needs to see the
11627 -- structural full view, which is what the current scheme gives: the
11628 -- Etype of the full view is the etype of the full base. However, if the
11629 -- full base is a derived type, the full view then looks like a subtype
11630 -- of the parent, not a subtype of the full base. If instead we write:
11631
11632 -- Set_Etype (Full, Full_Base);
11633
11634 -- then we get inconsistencies in the front-end (confusion between
11635 -- views). Several outstanding bugs are related to this ???
11636
11637 Set_Is_First_Subtype (Full, False);
11638 Set_Scope (Full, Scope (Priv));
11639 Set_Size_Info (Full, Full_Base);
11640 Set_RM_Size (Full, RM_Size (Full_Base));
11641 Set_Is_Itype (Full);
11642
11643 -- A subtype of a private-type-without-discriminants, whose full-view
11644 -- has discriminants with default expressions, is not constrained.
11645
11646 if not Has_Discriminants (Priv) then
11647 Set_Is_Constrained (Full, Is_Constrained (Full_Base));
11648
11649 if Has_Discriminants (Full_Base) then
11650 Set_Discriminant_Constraint
11651 (Full, Discriminant_Constraint (Full_Base));
11652
11653 -- The partial view may have been indefinite, the full view
11654 -- might not be.
11655
11656 Set_Has_Unknown_Discriminants
11657 (Full, Has_Unknown_Discriminants (Full_Base));
11658 end if;
11659 end if;
11660
11661 Set_First_Rep_Item (Full, First_Rep_Item (Full_Base));
11662 Set_Depends_On_Private (Full, Has_Private_Component (Full));
11663
11664 -- Freeze the private subtype entity if its parent is delayed, and not
11665 -- already frozen. We skip this processing if the type is an anonymous
11666 -- subtype of a record component, or is the corresponding record of a
11667 -- protected type, since these are processed when the enclosing type
11668 -- is frozen.
11669
11670 if not Is_Type (Scope (Full)) then
11671 Set_Has_Delayed_Freeze (Full,
11672 Has_Delayed_Freeze (Full_Base)
11673 and then (not Is_Frozen (Full_Base)));
11674 end if;
11675
11676 Set_Freeze_Node (Full, Empty);
11677 Set_Is_Frozen (Full, False);
11678 Set_Full_View (Priv, Full);
11679
11680 if Has_Discriminants (Full) then
11681 Set_Stored_Constraint_From_Discriminant_Constraint (Full);
11682 Set_Stored_Constraint (Priv, Stored_Constraint (Full));
11683
11684 if Has_Unknown_Discriminants (Full) then
11685 Set_Discriminant_Constraint (Full, No_Elist);
11686 end if;
11687 end if;
11688
11689 if Ekind (Full_Base) = E_Record_Type
11690 and then Has_Discriminants (Full_Base)
11691 and then Has_Discriminants (Priv) -- might not, if errors
11692 and then not Has_Unknown_Discriminants (Priv)
11693 and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
11694 then
11695 Create_Constrained_Components
11696 (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
11697
11698 -- If the full base is itself derived from private, build a congruent
11699 -- subtype of its underlying type, for use by the back end. For a
11700 -- constrained record component, the declaration cannot be placed on
11701 -- the component list, but it must nevertheless be built an analyzed, to
11702 -- supply enough information for Gigi to compute the size of component.
11703
11704 elsif Ekind (Full_Base) in Private_Kind
11705 and then Is_Derived_Type (Full_Base)
11706 and then Has_Discriminants (Full_Base)
11707 and then (Ekind (Current_Scope) /= E_Record_Subtype)
11708 then
11709 if not Is_Itype (Priv)
11710 and then
11711 Nkind (Subtype_Indication (Parent (Priv))) = N_Subtype_Indication
11712 then
11713 Build_Underlying_Full_View
11714 (Parent (Priv), Full, Etype (Full_Base));
11715
11716 elsif Nkind (Related_Nod) = N_Component_Declaration then
11717 Build_Underlying_Full_View (Related_Nod, Full, Etype (Full_Base));
11718 end if;
11719
11720 elsif Is_Record_Type (Full_Base) then
11721
11722 -- Show Full is simply a renaming of Full_Base
11723
11724 Set_Cloned_Subtype (Full, Full_Base);
11725 end if;
11726
11727 -- It is unsafe to share the bounds of a scalar type, because the Itype
11728 -- is elaborated on demand, and if a bound is non-static then different
11729 -- orders of elaboration in different units will lead to different
11730 -- external symbols.
11731
11732 if Is_Scalar_Type (Full_Base) then
11733 Set_Scalar_Range (Full,
11734 Make_Range (Sloc (Related_Nod),
11735 Low_Bound =>
11736 Duplicate_Subexpr_No_Checks (Type_Low_Bound (Full_Base)),
11737 High_Bound =>
11738 Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
11739
11740 -- This completion inherits the bounds of the full parent, but if
11741 -- the parent is an unconstrained floating point type, so is the
11742 -- completion.
11743
11744 if Is_Floating_Point_Type (Full_Base) then
11745 Set_Includes_Infinities
11746 (Scalar_Range (Full), Has_Infinities (Full_Base));
11747 end if;
11748 end if;
11749
11750 -- ??? It seems that a lot of fields are missing that should be copied
11751 -- from Full_Base to Full. Here are some that are introduced in a
11752 -- non-disruptive way but a cleanup is necessary.
11753
11754 if Is_Tagged_Type (Full_Base) then
11755 Set_Is_Tagged_Type (Full);
11756 Set_Direct_Primitive_Operations
11757 (Full, Direct_Primitive_Operations (Full_Base));
11758 Set_No_Tagged_Streams_Pragma
11759 (Full, No_Tagged_Streams_Pragma (Full_Base));
11760
11761 -- Inherit class_wide type of full_base in case the partial view was
11762 -- not tagged. Otherwise it has already been created when the private
11763 -- subtype was analyzed.
11764
11765 if No (Class_Wide_Type (Full)) then
11766 Set_Class_Wide_Type (Full, Class_Wide_Type (Full_Base));
11767 end if;
11768
11769 -- If this is a subtype of a protected or task type, constrain its
11770 -- corresponding record, unless this is a subtype without constraints,
11771 -- i.e. a simple renaming as with an actual subtype in an instance.
11772
11773 elsif Is_Concurrent_Type (Full_Base) then
11774 if Has_Discriminants (Full)
11775 and then Present (Corresponding_Record_Type (Full_Base))
11776 and then
11777 not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
11778 then
11779 Set_Corresponding_Record_Type (Full,
11780 Constrain_Corresponding_Record
11781 (Full, Corresponding_Record_Type (Full_Base), Related_Nod));
11782
11783 else
11784 Set_Corresponding_Record_Type (Full,
11785 Corresponding_Record_Type (Full_Base));
11786 end if;
11787 end if;
11788
11789 -- Link rep item chain, and also setting of Has_Predicates from private
11790 -- subtype to full subtype, since we will need these on the full subtype
11791 -- to create the predicate function. Note that the full subtype may
11792 -- already have rep items, inherited from the full view of the base
11793 -- type, so we must be sure not to overwrite these entries.
11794
11795 declare
11796 Append : Boolean;
11797 Item : Node_Id;
11798 Next_Item : Node_Id;
11799
11800 begin
11801 Item := First_Rep_Item (Full);
11802
11803 -- If no existing rep items on full type, we can just link directly
11804 -- to the list of items on the private type, if any exist.. Same if
11805 -- the rep items are only those inherited from the base
11806
11807 if (No (Item)
11808 or else Nkind (Item) /= N_Aspect_Specification
11809 or else Entity (Item) = Full_Base)
11810 and then Present (First_Rep_Item (Priv))
11811 then
11812 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
11813
11814 -- Otherwise, search to the end of items currently linked to the full
11815 -- subtype and append the private items to the end. However, if Priv
11816 -- and Full already have the same list of rep items, then the append
11817 -- is not done, as that would create a circularity.
11818
11819 elsif Item /= First_Rep_Item (Priv) then
11820 Append := True;
11821 loop
11822 Next_Item := Next_Rep_Item (Item);
11823 exit when No (Next_Item);
11824 Item := Next_Item;
11825
11826 -- If the private view has aspect specifications, the full view
11827 -- inherits them. Since these aspects may already have been
11828 -- attached to the full view during derivation, do not append
11829 -- them if already present.
11830
11831 if Item = First_Rep_Item (Priv) then
11832 Append := False;
11833 exit;
11834 end if;
11835 end loop;
11836
11837 -- And link the private type items at the end of the chain
11838
11839 if Append then
11840 Set_Next_Rep_Item (Item, First_Rep_Item (Priv));
11841 end if;
11842 end if;
11843 end;
11844
11845 -- Make sure Has_Predicates is set on full type if it is set on the
11846 -- private type. Note that it may already be set on the full type and
11847 -- if so, we don't want to unset it. Similarly, propagate information
11848 -- about delayed aspects, because the corresponding pragmas must be
11849 -- analyzed when one of the views is frozen. This last step is needed
11850 -- in particular when the full type is a scalar type for which an
11851 -- anonymous base type is constructed.
11852
11853 if Has_Predicates (Priv) then
11854 Set_Has_Predicates (Full);
11855 end if;
11856
11857 if Has_Delayed_Aspects (Priv) then
11858 Set_Has_Delayed_Aspects (Full);
11859 end if;
11860 end Complete_Private_Subtype;
11861
11862 ----------------------------
11863 -- Constant_Redeclaration --
11864 ----------------------------
11865
11866 procedure Constant_Redeclaration
11867 (Id : Entity_Id;
11868 N : Node_Id;
11869 T : out Entity_Id)
11870 is
11871 Prev : constant Entity_Id := Current_Entity_In_Scope (Id);
11872 Obj_Def : constant Node_Id := Object_Definition (N);
11873 New_T : Entity_Id;
11874
11875 procedure Check_Possible_Deferred_Completion
11876 (Prev_Id : Entity_Id;
11877 Prev_Obj_Def : Node_Id;
11878 Curr_Obj_Def : Node_Id);
11879 -- Determine whether the two object definitions describe the partial
11880 -- and the full view of a constrained deferred constant. Generate
11881 -- a subtype for the full view and verify that it statically matches
11882 -- the subtype of the partial view.
11883
11884 procedure Check_Recursive_Declaration (Typ : Entity_Id);
11885 -- If deferred constant is an access type initialized with an allocator,
11886 -- check whether there is an illegal recursion in the definition,
11887 -- through a default value of some record subcomponent. This is normally
11888 -- detected when generating init procs, but requires this additional
11889 -- mechanism when expansion is disabled.
11890
11891 ----------------------------------------
11892 -- Check_Possible_Deferred_Completion --
11893 ----------------------------------------
11894
11895 procedure Check_Possible_Deferred_Completion
11896 (Prev_Id : Entity_Id;
11897 Prev_Obj_Def : Node_Id;
11898 Curr_Obj_Def : Node_Id)
11899 is
11900 begin
11901 if Nkind (Prev_Obj_Def) = N_Subtype_Indication
11902 and then Present (Constraint (Prev_Obj_Def))
11903 and then Nkind (Curr_Obj_Def) = N_Subtype_Indication
11904 and then Present (Constraint (Curr_Obj_Def))
11905 then
11906 declare
11907 Loc : constant Source_Ptr := Sloc (N);
11908 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
11909 Decl : constant Node_Id :=
11910 Make_Subtype_Declaration (Loc,
11911 Defining_Identifier => Def_Id,
11912 Subtype_Indication =>
11913 Relocate_Node (Curr_Obj_Def));
11914
11915 begin
11916 Insert_Before_And_Analyze (N, Decl);
11917 Set_Etype (Id, Def_Id);
11918
11919 if not Subtypes_Statically_Match (Etype (Prev_Id), Def_Id) then
11920 Error_Msg_Sloc := Sloc (Prev_Id);
11921 Error_Msg_N ("subtype does not statically match deferred "
11922 & "declaration #", N);
11923 end if;
11924 end;
11925 end if;
11926 end Check_Possible_Deferred_Completion;
11927
11928 ---------------------------------
11929 -- Check_Recursive_Declaration --
11930 ---------------------------------
11931
11932 procedure Check_Recursive_Declaration (Typ : Entity_Id) is
11933 Comp : Entity_Id;
11934
11935 begin
11936 if Is_Record_Type (Typ) then
11937 Comp := First_Component (Typ);
11938 while Present (Comp) loop
11939 if Comes_From_Source (Comp) then
11940 if Present (Expression (Parent (Comp)))
11941 and then Is_Entity_Name (Expression (Parent (Comp)))
11942 and then Entity (Expression (Parent (Comp))) = Prev
11943 then
11944 Error_Msg_Sloc := Sloc (Parent (Comp));
11945 Error_Msg_NE
11946 ("illegal circularity with declaration for & #",
11947 N, Comp);
11948 return;
11949
11950 elsif Is_Record_Type (Etype (Comp)) then
11951 Check_Recursive_Declaration (Etype (Comp));
11952 end if;
11953 end if;
11954
11955 Next_Component (Comp);
11956 end loop;
11957 end if;
11958 end Check_Recursive_Declaration;
11959
11960 -- Start of processing for Constant_Redeclaration
11961
11962 begin
11963 if Nkind (Parent (Prev)) = N_Object_Declaration then
11964 if Nkind (Object_Definition
11965 (Parent (Prev))) = N_Subtype_Indication
11966 then
11967 -- Find type of new declaration. The constraints of the two
11968 -- views must match statically, but there is no point in
11969 -- creating an itype for the full view.
11970
11971 if Nkind (Obj_Def) = N_Subtype_Indication then
11972 Find_Type (Subtype_Mark (Obj_Def));
11973 New_T := Entity (Subtype_Mark (Obj_Def));
11974
11975 else
11976 Find_Type (Obj_Def);
11977 New_T := Entity (Obj_Def);
11978 end if;
11979
11980 T := Etype (Prev);
11981
11982 else
11983 -- The full view may impose a constraint, even if the partial
11984 -- view does not, so construct the subtype.
11985
11986 New_T := Find_Type_Of_Object (Obj_Def, N);
11987 T := New_T;
11988 end if;
11989
11990 else
11991 -- Current declaration is illegal, diagnosed below in Enter_Name
11992
11993 T := Empty;
11994 New_T := Any_Type;
11995 end if;
11996
11997 -- If previous full declaration or a renaming declaration exists, or if
11998 -- a homograph is present, let Enter_Name handle it, either with an
11999 -- error or with the removal of an overridden implicit subprogram.
12000 -- The previous one is a full declaration if it has an expression
12001 -- (which in the case of an aggregate is indicated by the Init flag).
12002
12003 if Ekind (Prev) /= E_Constant
12004 or else Nkind (Parent (Prev)) = N_Object_Renaming_Declaration
12005 or else Present (Expression (Parent (Prev)))
12006 or else Has_Init_Expression (Parent (Prev))
12007 or else Present (Full_View (Prev))
12008 then
12009 Enter_Name (Id);
12010
12011 -- Verify that types of both declarations match, or else that both types
12012 -- are anonymous access types whose designated subtypes statically match
12013 -- (as allowed in Ada 2005 by AI-385).
12014
12015 elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
12016 and then
12017 (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
12018 or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
12019 or else Is_Access_Constant (Etype (New_T)) /=
12020 Is_Access_Constant (Etype (Prev))
12021 or else Can_Never_Be_Null (Etype (New_T)) /=
12022 Can_Never_Be_Null (Etype (Prev))
12023 or else Null_Exclusion_Present (Parent (Prev)) /=
12024 Null_Exclusion_Present (Parent (Id))
12025 or else not Subtypes_Statically_Match
12026 (Designated_Type (Etype (Prev)),
12027 Designated_Type (Etype (New_T))))
12028 then
12029 Error_Msg_Sloc := Sloc (Prev);
12030 Error_Msg_N ("type does not match declaration#", N);
12031 Set_Full_View (Prev, Id);
12032 Set_Etype (Id, Any_Type);
12033
12034 -- A deferred constant whose type is an anonymous array is always
12035 -- illegal (unless imported). A detailed error message might be
12036 -- helpful for Ada beginners.
12037
12038 if Nkind (Object_Definition (Parent (Prev)))
12039 = N_Constrained_Array_Definition
12040 and then Nkind (Object_Definition (N))
12041 = N_Constrained_Array_Definition
12042 then
12043 Error_Msg_N ("\each anonymous array is a distinct type", N);
12044 Error_Msg_N ("a deferred constant must have a named type",
12045 Object_Definition (Parent (Prev)));
12046 end if;
12047
12048 elsif
12049 Null_Exclusion_Present (Parent (Prev))
12050 and then not Null_Exclusion_Present (N)
12051 then
12052 Error_Msg_Sloc := Sloc (Prev);
12053 Error_Msg_N ("null-exclusion does not match declaration#", N);
12054 Set_Full_View (Prev, Id);
12055 Set_Etype (Id, Any_Type);
12056
12057 -- If so, process the full constant declaration
12058
12059 else
12060 -- RM 7.4 (6): If the subtype defined by the subtype_indication in
12061 -- the deferred declaration is constrained, then the subtype defined
12062 -- by the subtype_indication in the full declaration shall match it
12063 -- statically.
12064
12065 Check_Possible_Deferred_Completion
12066 (Prev_Id => Prev,
12067 Prev_Obj_Def => Object_Definition (Parent (Prev)),
12068 Curr_Obj_Def => Obj_Def);
12069
12070 Set_Full_View (Prev, Id);
12071 Set_Is_Public (Id, Is_Public (Prev));
12072 Set_Is_Internal (Id);
12073 Append_Entity (Id, Current_Scope);
12074
12075 -- Check ALIASED present if present before (RM 7.4(7))
12076
12077 if Is_Aliased (Prev)
12078 and then not Aliased_Present (N)
12079 then
12080 Error_Msg_Sloc := Sloc (Prev);
12081 Error_Msg_N ("ALIASED required (see declaration #)", N);
12082 end if;
12083
12084 -- Check that placement is in private part and that the incomplete
12085 -- declaration appeared in the visible part.
12086
12087 if Ekind (Current_Scope) = E_Package
12088 and then not In_Private_Part (Current_Scope)
12089 then
12090 Error_Msg_Sloc := Sloc (Prev);
12091 Error_Msg_N
12092 ("full constant for declaration # must be in private part", N);
12093
12094 elsif Ekind (Current_Scope) = E_Package
12095 and then
12096 List_Containing (Parent (Prev)) /=
12097 Visible_Declarations (Package_Specification (Current_Scope))
12098 then
12099 Error_Msg_N
12100 ("deferred constant must be declared in visible part",
12101 Parent (Prev));
12102 end if;
12103
12104 if Is_Access_Type (T)
12105 and then Nkind (Expression (N)) = N_Allocator
12106 then
12107 Check_Recursive_Declaration (Designated_Type (T));
12108 end if;
12109
12110 -- A deferred constant is a visible entity. If type has invariants,
12111 -- verify that the initial value satisfies them.
12112
12113 if Has_Invariants (T) and then Present (Invariant_Procedure (T)) then
12114 Insert_After (N,
12115 Make_Invariant_Call (New_Occurrence_Of (Prev, Sloc (N))));
12116 end if;
12117 end if;
12118 end Constant_Redeclaration;
12119
12120 ----------------------
12121 -- Constrain_Access --
12122 ----------------------
12123
12124 procedure Constrain_Access
12125 (Def_Id : in out Entity_Id;
12126 S : Node_Id;
12127 Related_Nod : Node_Id)
12128 is
12129 T : constant Entity_Id := Entity (Subtype_Mark (S));
12130 Desig_Type : constant Entity_Id := Designated_Type (T);
12131 Desig_Subtype : Entity_Id := Create_Itype (E_Void, Related_Nod);
12132 Constraint_OK : Boolean := True;
12133
12134 begin
12135 if Is_Array_Type (Desig_Type) then
12136 Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
12137
12138 elsif (Is_Record_Type (Desig_Type)
12139 or else Is_Incomplete_Or_Private_Type (Desig_Type))
12140 and then not Is_Constrained (Desig_Type)
12141 then
12142 -- ??? The following code is a temporary bypass to ignore a
12143 -- discriminant constraint on access type if it is constraining
12144 -- the current record. Avoid creating the implicit subtype of the
12145 -- record we are currently compiling since right now, we cannot
12146 -- handle these. For now, just return the access type itself.
12147
12148 if Desig_Type = Current_Scope
12149 and then No (Def_Id)
12150 then
12151 Set_Ekind (Desig_Subtype, E_Record_Subtype);
12152 Def_Id := Entity (Subtype_Mark (S));
12153
12154 -- This call added to ensure that the constraint is analyzed
12155 -- (needed for a B test). Note that we still return early from
12156 -- this procedure to avoid recursive processing. ???
12157
12158 Constrain_Discriminated_Type
12159 (Desig_Subtype, S, Related_Nod, For_Access => True);
12160 return;
12161 end if;
12162
12163 -- Enforce rule that the constraint is illegal if there is an
12164 -- unconstrained view of the designated type. This means that the
12165 -- partial view (either a private type declaration or a derivation
12166 -- from a private type) has no discriminants. (Defect Report
12167 -- 8652/0008, Technical Corrigendum 1, checked by ACATS B371001).
12168
12169 -- Rule updated for Ada 2005: The private type is said to have
12170 -- a constrained partial view, given that objects of the type
12171 -- can be declared. Furthermore, the rule applies to all access
12172 -- types, unlike the rule concerning default discriminants (see
12173 -- RM 3.7.1(7/3))
12174
12175 if (Ekind (T) = E_General_Access_Type or else Ada_Version >= Ada_2005)
12176 and then Has_Private_Declaration (Desig_Type)
12177 and then In_Open_Scopes (Scope (Desig_Type))
12178 and then Has_Discriminants (Desig_Type)
12179 then
12180 declare
12181 Pack : constant Node_Id :=
12182 Unit_Declaration_Node (Scope (Desig_Type));
12183 Decls : List_Id;
12184 Decl : Node_Id;
12185
12186 begin
12187 if Nkind (Pack) = N_Package_Declaration then
12188 Decls := Visible_Declarations (Specification (Pack));
12189 Decl := First (Decls);
12190 while Present (Decl) loop
12191 if (Nkind (Decl) = N_Private_Type_Declaration
12192 and then Chars (Defining_Identifier (Decl)) =
12193 Chars (Desig_Type))
12194
12195 or else
12196 (Nkind (Decl) = N_Full_Type_Declaration
12197 and then
12198 Chars (Defining_Identifier (Decl)) =
12199 Chars (Desig_Type)
12200 and then Is_Derived_Type (Desig_Type)
12201 and then
12202 Has_Private_Declaration (Etype (Desig_Type)))
12203 then
12204 if No (Discriminant_Specifications (Decl)) then
12205 Error_Msg_N
12206 ("cannot constrain access type if designated "
12207 & "type has constrained partial view", S);
12208 end if;
12209
12210 exit;
12211 end if;
12212
12213 Next (Decl);
12214 end loop;
12215 end if;
12216 end;
12217 end if;
12218
12219 Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
12220 For_Access => True);
12221
12222 elsif Is_Concurrent_Type (Desig_Type)
12223 and then not Is_Constrained (Desig_Type)
12224 then
12225 Constrain_Concurrent (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
12226
12227 else
12228 Error_Msg_N ("invalid constraint on access type", S);
12229
12230 -- We simply ignore an invalid constraint
12231
12232 Desig_Subtype := Desig_Type;
12233 Constraint_OK := False;
12234 end if;
12235
12236 if No (Def_Id) then
12237 Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
12238 else
12239 Set_Ekind (Def_Id, E_Access_Subtype);
12240 end if;
12241
12242 if Constraint_OK then
12243 Set_Etype (Def_Id, Base_Type (T));
12244
12245 if Is_Private_Type (Desig_Type) then
12246 Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
12247 end if;
12248 else
12249 Set_Etype (Def_Id, Any_Type);
12250 end if;
12251
12252 Set_Size_Info (Def_Id, T);
12253 Set_Is_Constrained (Def_Id, Constraint_OK);
12254 Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
12255 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
12256 Set_Is_Access_Constant (Def_Id, Is_Access_Constant (T));
12257
12258 Conditional_Delay (Def_Id, T);
12259
12260 -- AI-363 : Subtypes of general access types whose designated types have
12261 -- default discriminants are disallowed. In instances, the rule has to
12262 -- be checked against the actual, of which T is the subtype. In a
12263 -- generic body, the rule is checked assuming that the actual type has
12264 -- defaulted discriminants.
12265
12266 if Ada_Version >= Ada_2005 or else Warn_On_Ada_2005_Compatibility then
12267 if Ekind (Base_Type (T)) = E_General_Access_Type
12268 and then Has_Defaulted_Discriminants (Desig_Type)
12269 then
12270 if Ada_Version < Ada_2005 then
12271 Error_Msg_N
12272 ("access subtype of general access type would not " &
12273 "be allowed in Ada 2005?y?", S);
12274 else
12275 Error_Msg_N
12276 ("access subtype of general access type not allowed", S);
12277 end if;
12278
12279 Error_Msg_N ("\discriminants have defaults", S);
12280
12281 elsif Is_Access_Type (T)
12282 and then Is_Generic_Type (Desig_Type)
12283 and then Has_Discriminants (Desig_Type)
12284 and then In_Package_Body (Current_Scope)
12285 then
12286 if Ada_Version < Ada_2005 then
12287 Error_Msg_N
12288 ("access subtype would not be allowed in generic body "
12289 & "in Ada 2005?y?", S);
12290 else
12291 Error_Msg_N
12292 ("access subtype not allowed in generic body", S);
12293 end if;
12294
12295 Error_Msg_N
12296 ("\designated type is a discriminated formal", S);
12297 end if;
12298 end if;
12299 end Constrain_Access;
12300
12301 ---------------------
12302 -- Constrain_Array --
12303 ---------------------
12304
12305 procedure Constrain_Array
12306 (Def_Id : in out Entity_Id;
12307 SI : Node_Id;
12308 Related_Nod : Node_Id;
12309 Related_Id : Entity_Id;
12310 Suffix : Character)
12311 is
12312 C : constant Node_Id := Constraint (SI);
12313 Number_Of_Constraints : Nat := 0;
12314 Index : Node_Id;
12315 S, T : Entity_Id;
12316 Constraint_OK : Boolean := True;
12317
12318 begin
12319 T := Entity (Subtype_Mark (SI));
12320
12321 if Is_Access_Type (T) then
12322 T := Designated_Type (T);
12323 end if;
12324
12325 -- If an index constraint follows a subtype mark in a subtype indication
12326 -- then the type or subtype denoted by the subtype mark must not already
12327 -- impose an index constraint. The subtype mark must denote either an
12328 -- unconstrained array type or an access type whose designated type
12329 -- is such an array type... (RM 3.6.1)
12330
12331 if Is_Constrained (T) then
12332 Error_Msg_N ("array type is already constrained", Subtype_Mark (SI));
12333 Constraint_OK := False;
12334
12335 else
12336 S := First (Constraints (C));
12337 while Present (S) loop
12338 Number_Of_Constraints := Number_Of_Constraints + 1;
12339 Next (S);
12340 end loop;
12341
12342 -- In either case, the index constraint must provide a discrete
12343 -- range for each index of the array type and the type of each
12344 -- discrete range must be the same as that of the corresponding
12345 -- index. (RM 3.6.1)
12346
12347 if Number_Of_Constraints /= Number_Dimensions (T) then
12348 Error_Msg_NE ("incorrect number of index constraints for }", C, T);
12349 Constraint_OK := False;
12350
12351 else
12352 S := First (Constraints (C));
12353 Index := First_Index (T);
12354 Analyze (Index);
12355
12356 -- Apply constraints to each index type
12357
12358 for J in 1 .. Number_Of_Constraints loop
12359 Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
12360 Next (Index);
12361 Next (S);
12362 end loop;
12363
12364 end if;
12365 end if;
12366
12367 if No (Def_Id) then
12368 Def_Id :=
12369 Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
12370 Set_Parent (Def_Id, Related_Nod);
12371
12372 else
12373 Set_Ekind (Def_Id, E_Array_Subtype);
12374 end if;
12375
12376 Set_Size_Info (Def_Id, (T));
12377 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
12378 Set_Etype (Def_Id, Base_Type (T));
12379
12380 if Constraint_OK then
12381 Set_First_Index (Def_Id, First (Constraints (C)));
12382 else
12383 Set_First_Index (Def_Id, First_Index (T));
12384 end if;
12385
12386 Set_Is_Constrained (Def_Id, True);
12387 Set_Is_Aliased (Def_Id, Is_Aliased (T));
12388 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
12389
12390 Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
12391 Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
12392
12393 -- A subtype does not inherit the Packed_Array_Impl_Type of is parent.
12394 -- We need to initialize the attribute because if Def_Id is previously
12395 -- analyzed through a limited_with clause, it will have the attributes
12396 -- of an incomplete type, one of which is an Elist that overlaps the
12397 -- Packed_Array_Impl_Type field.
12398
12399 Set_Packed_Array_Impl_Type (Def_Id, Empty);
12400
12401 -- Build a freeze node if parent still needs one. Also make sure that
12402 -- the Depends_On_Private status is set because the subtype will need
12403 -- reprocessing at the time the base type does, and also we must set a
12404 -- conditional delay.
12405
12406 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
12407 Conditional_Delay (Def_Id, T);
12408 end Constrain_Array;
12409
12410 ------------------------------
12411 -- Constrain_Component_Type --
12412 ------------------------------
12413
12414 function Constrain_Component_Type
12415 (Comp : Entity_Id;
12416 Constrained_Typ : Entity_Id;
12417 Related_Node : Node_Id;
12418 Typ : Entity_Id;
12419 Constraints : Elist_Id) return Entity_Id
12420 is
12421 Loc : constant Source_Ptr := Sloc (Constrained_Typ);
12422 Compon_Type : constant Entity_Id := Etype (Comp);
12423
12424 function Build_Constrained_Array_Type
12425 (Old_Type : Entity_Id) return Entity_Id;
12426 -- If Old_Type is an array type, one of whose indexes is constrained
12427 -- by a discriminant, build an Itype whose constraint replaces the
12428 -- discriminant with its value in the constraint.
12429
12430 function Build_Constrained_Discriminated_Type
12431 (Old_Type : Entity_Id) return Entity_Id;
12432 -- Ditto for record components
12433
12434 function Build_Constrained_Access_Type
12435 (Old_Type : Entity_Id) return Entity_Id;
12436 -- Ditto for access types. Makes use of previous two functions, to
12437 -- constrain designated type.
12438
12439 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id;
12440 -- T is an array or discriminated type, C is a list of constraints
12441 -- that apply to T. This routine builds the constrained subtype.
12442
12443 function Is_Discriminant (Expr : Node_Id) return Boolean;
12444 -- Returns True if Expr is a discriminant
12445
12446 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id;
12447 -- Find the value of discriminant Discrim in Constraint
12448
12449 -----------------------------------
12450 -- Build_Constrained_Access_Type --
12451 -----------------------------------
12452
12453 function Build_Constrained_Access_Type
12454 (Old_Type : Entity_Id) return Entity_Id
12455 is
12456 Desig_Type : constant Entity_Id := Designated_Type (Old_Type);
12457 Itype : Entity_Id;
12458 Desig_Subtype : Entity_Id;
12459 Scop : Entity_Id;
12460
12461 begin
12462 -- if the original access type was not embedded in the enclosing
12463 -- type definition, there is no need to produce a new access
12464 -- subtype. In fact every access type with an explicit constraint
12465 -- generates an itype whose scope is the enclosing record.
12466
12467 if not Is_Type (Scope (Old_Type)) then
12468 return Old_Type;
12469
12470 elsif Is_Array_Type (Desig_Type) then
12471 Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
12472
12473 elsif Has_Discriminants (Desig_Type) then
12474
12475 -- This may be an access type to an enclosing record type for
12476 -- which we are constructing the constrained components. Return
12477 -- the enclosing record subtype. This is not always correct,
12478 -- but avoids infinite recursion. ???
12479
12480 Desig_Subtype := Any_Type;
12481
12482 for J in reverse 0 .. Scope_Stack.Last loop
12483 Scop := Scope_Stack.Table (J).Entity;
12484
12485 if Is_Type (Scop)
12486 and then Base_Type (Scop) = Base_Type (Desig_Type)
12487 then
12488 Desig_Subtype := Scop;
12489 end if;
12490
12491 exit when not Is_Type (Scop);
12492 end loop;
12493
12494 if Desig_Subtype = Any_Type then
12495 Desig_Subtype :=
12496 Build_Constrained_Discriminated_Type (Desig_Type);
12497 end if;
12498
12499 else
12500 return Old_Type;
12501 end if;
12502
12503 if Desig_Subtype /= Desig_Type then
12504
12505 -- The Related_Node better be here or else we won't be able
12506 -- to attach new itypes to a node in the tree.
12507
12508 pragma Assert (Present (Related_Node));
12509
12510 Itype := Create_Itype (E_Access_Subtype, Related_Node);
12511
12512 Set_Etype (Itype, Base_Type (Old_Type));
12513 Set_Size_Info (Itype, (Old_Type));
12514 Set_Directly_Designated_Type (Itype, Desig_Subtype);
12515 Set_Depends_On_Private (Itype, Has_Private_Component
12516 (Old_Type));
12517 Set_Is_Access_Constant (Itype, Is_Access_Constant
12518 (Old_Type));
12519
12520 -- The new itype needs freezing when it depends on a not frozen
12521 -- type and the enclosing subtype needs freezing.
12522
12523 if Has_Delayed_Freeze (Constrained_Typ)
12524 and then not Is_Frozen (Constrained_Typ)
12525 then
12526 Conditional_Delay (Itype, Base_Type (Old_Type));
12527 end if;
12528
12529 return Itype;
12530
12531 else
12532 return Old_Type;
12533 end if;
12534 end Build_Constrained_Access_Type;
12535
12536 ----------------------------------
12537 -- Build_Constrained_Array_Type --
12538 ----------------------------------
12539
12540 function Build_Constrained_Array_Type
12541 (Old_Type : Entity_Id) return Entity_Id
12542 is
12543 Lo_Expr : Node_Id;
12544 Hi_Expr : Node_Id;
12545 Old_Index : Node_Id;
12546 Range_Node : Node_Id;
12547 Constr_List : List_Id;
12548
12549 Need_To_Create_Itype : Boolean := False;
12550
12551 begin
12552 Old_Index := First_Index (Old_Type);
12553 while Present (Old_Index) loop
12554 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
12555
12556 if Is_Discriminant (Lo_Expr)
12557 or else
12558 Is_Discriminant (Hi_Expr)
12559 then
12560 Need_To_Create_Itype := True;
12561 end if;
12562
12563 Next_Index (Old_Index);
12564 end loop;
12565
12566 if Need_To_Create_Itype then
12567 Constr_List := New_List;
12568
12569 Old_Index := First_Index (Old_Type);
12570 while Present (Old_Index) loop
12571 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
12572
12573 if Is_Discriminant (Lo_Expr) then
12574 Lo_Expr := Get_Discr_Value (Lo_Expr);
12575 end if;
12576
12577 if Is_Discriminant (Hi_Expr) then
12578 Hi_Expr := Get_Discr_Value (Hi_Expr);
12579 end if;
12580
12581 Range_Node :=
12582 Make_Range
12583 (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
12584
12585 Append (Range_Node, To => Constr_List);
12586
12587 Next_Index (Old_Index);
12588 end loop;
12589
12590 return Build_Subtype (Old_Type, Constr_List);
12591
12592 else
12593 return Old_Type;
12594 end if;
12595 end Build_Constrained_Array_Type;
12596
12597 ------------------------------------------
12598 -- Build_Constrained_Discriminated_Type --
12599 ------------------------------------------
12600
12601 function Build_Constrained_Discriminated_Type
12602 (Old_Type : Entity_Id) return Entity_Id
12603 is
12604 Expr : Node_Id;
12605 Constr_List : List_Id;
12606 Old_Constraint : Elmt_Id;
12607
12608 Need_To_Create_Itype : Boolean := False;
12609
12610 begin
12611 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
12612 while Present (Old_Constraint) loop
12613 Expr := Node (Old_Constraint);
12614
12615 if Is_Discriminant (Expr) then
12616 Need_To_Create_Itype := True;
12617 end if;
12618
12619 Next_Elmt (Old_Constraint);
12620 end loop;
12621
12622 if Need_To_Create_Itype then
12623 Constr_List := New_List;
12624
12625 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
12626 while Present (Old_Constraint) loop
12627 Expr := Node (Old_Constraint);
12628
12629 if Is_Discriminant (Expr) then
12630 Expr := Get_Discr_Value (Expr);
12631 end if;
12632
12633 Append (New_Copy_Tree (Expr), To => Constr_List);
12634
12635 Next_Elmt (Old_Constraint);
12636 end loop;
12637
12638 return Build_Subtype (Old_Type, Constr_List);
12639
12640 else
12641 return Old_Type;
12642 end if;
12643 end Build_Constrained_Discriminated_Type;
12644
12645 -------------------
12646 -- Build_Subtype --
12647 -------------------
12648
12649 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id is
12650 Indic : Node_Id;
12651 Subtyp_Decl : Node_Id;
12652 Def_Id : Entity_Id;
12653 Btyp : Entity_Id := Base_Type (T);
12654
12655 begin
12656 -- The Related_Node better be here or else we won't be able to
12657 -- attach new itypes to a node in the tree.
12658
12659 pragma Assert (Present (Related_Node));
12660
12661 -- If the view of the component's type is incomplete or private
12662 -- with unknown discriminants, then the constraint must be applied
12663 -- to the full type.
12664
12665 if Has_Unknown_Discriminants (Btyp)
12666 and then Present (Underlying_Type (Btyp))
12667 then
12668 Btyp := Underlying_Type (Btyp);
12669 end if;
12670
12671 Indic :=
12672 Make_Subtype_Indication (Loc,
12673 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
12674 Constraint => Make_Index_Or_Discriminant_Constraint (Loc, C));
12675
12676 Def_Id := Create_Itype (Ekind (T), Related_Node);
12677
12678 Subtyp_Decl :=
12679 Make_Subtype_Declaration (Loc,
12680 Defining_Identifier => Def_Id,
12681 Subtype_Indication => Indic);
12682
12683 Set_Parent (Subtyp_Decl, Parent (Related_Node));
12684
12685 -- Itypes must be analyzed with checks off (see package Itypes)
12686
12687 Analyze (Subtyp_Decl, Suppress => All_Checks);
12688
12689 return Def_Id;
12690 end Build_Subtype;
12691
12692 ---------------------
12693 -- Get_Discr_Value --
12694 ---------------------
12695
12696 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id is
12697 D : Entity_Id;
12698 E : Elmt_Id;
12699
12700 begin
12701 -- The discriminant may be declared for the type, in which case we
12702 -- find it by iterating over the list of discriminants. If the
12703 -- discriminant is inherited from a parent type, it appears as the
12704 -- corresponding discriminant of the current type. This will be the
12705 -- case when constraining an inherited component whose constraint is
12706 -- given by a discriminant of the parent.
12707
12708 D := First_Discriminant (Typ);
12709 E := First_Elmt (Constraints);
12710
12711 while Present (D) loop
12712 if D = Entity (Discrim)
12713 or else D = CR_Discriminant (Entity (Discrim))
12714 or else Corresponding_Discriminant (D) = Entity (Discrim)
12715 then
12716 return Node (E);
12717 end if;
12718
12719 Next_Discriminant (D);
12720 Next_Elmt (E);
12721 end loop;
12722
12723 -- The Corresponding_Discriminant mechanism is incomplete, because
12724 -- the correspondence between new and old discriminants is not one
12725 -- to one: one new discriminant can constrain several old ones. In
12726 -- that case, scan sequentially the stored_constraint, the list of
12727 -- discriminants of the parents, and the constraints.
12728
12729 -- Previous code checked for the present of the Stored_Constraint
12730 -- list for the derived type, but did not use it at all. Should it
12731 -- be present when the component is a discriminated task type?
12732
12733 if Is_Derived_Type (Typ)
12734 and then Scope (Entity (Discrim)) = Etype (Typ)
12735 then
12736 D := First_Discriminant (Etype (Typ));
12737 E := First_Elmt (Constraints);
12738 while Present (D) loop
12739 if D = Entity (Discrim) then
12740 return Node (E);
12741 end if;
12742
12743 Next_Discriminant (D);
12744 Next_Elmt (E);
12745 end loop;
12746 end if;
12747
12748 -- Something is wrong if we did not find the value
12749
12750 raise Program_Error;
12751 end Get_Discr_Value;
12752
12753 ---------------------
12754 -- Is_Discriminant --
12755 ---------------------
12756
12757 function Is_Discriminant (Expr : Node_Id) return Boolean is
12758 Discrim_Scope : Entity_Id;
12759
12760 begin
12761 if Denotes_Discriminant (Expr) then
12762 Discrim_Scope := Scope (Entity (Expr));
12763
12764 -- Either we have a reference to one of Typ's discriminants,
12765
12766 pragma Assert (Discrim_Scope = Typ
12767
12768 -- or to the discriminants of the parent type, in the case
12769 -- of a derivation of a tagged type with variants.
12770
12771 or else Discrim_Scope = Etype (Typ)
12772 or else Full_View (Discrim_Scope) = Etype (Typ)
12773
12774 -- or same as above for the case where the discriminants
12775 -- were declared in Typ's private view.
12776
12777 or else (Is_Private_Type (Discrim_Scope)
12778 and then Chars (Discrim_Scope) = Chars (Typ))
12779
12780 -- or else we are deriving from the full view and the
12781 -- discriminant is declared in the private entity.
12782
12783 or else (Is_Private_Type (Typ)
12784 and then Chars (Discrim_Scope) = Chars (Typ))
12785
12786 -- Or we are constrained the corresponding record of a
12787 -- synchronized type that completes a private declaration.
12788
12789 or else (Is_Concurrent_Record_Type (Typ)
12790 and then
12791 Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
12792
12793 -- or we have a class-wide type, in which case make sure the
12794 -- discriminant found belongs to the root type.
12795
12796 or else (Is_Class_Wide_Type (Typ)
12797 and then Etype (Typ) = Discrim_Scope));
12798
12799 return True;
12800 end if;
12801
12802 -- In all other cases we have something wrong
12803
12804 return False;
12805 end Is_Discriminant;
12806
12807 -- Start of processing for Constrain_Component_Type
12808
12809 begin
12810 if Nkind (Parent (Comp)) = N_Component_Declaration
12811 and then Comes_From_Source (Parent (Comp))
12812 and then Comes_From_Source
12813 (Subtype_Indication (Component_Definition (Parent (Comp))))
12814 and then
12815 Is_Entity_Name
12816 (Subtype_Indication (Component_Definition (Parent (Comp))))
12817 then
12818 return Compon_Type;
12819
12820 elsif Is_Array_Type (Compon_Type) then
12821 return Build_Constrained_Array_Type (Compon_Type);
12822
12823 elsif Has_Discriminants (Compon_Type) then
12824 return Build_Constrained_Discriminated_Type (Compon_Type);
12825
12826 elsif Is_Access_Type (Compon_Type) then
12827 return Build_Constrained_Access_Type (Compon_Type);
12828
12829 else
12830 return Compon_Type;
12831 end if;
12832 end Constrain_Component_Type;
12833
12834 --------------------------
12835 -- Constrain_Concurrent --
12836 --------------------------
12837
12838 -- For concurrent types, the associated record value type carries the same
12839 -- discriminants, so when we constrain a concurrent type, we must constrain
12840 -- the corresponding record type as well.
12841
12842 procedure Constrain_Concurrent
12843 (Def_Id : in out Entity_Id;
12844 SI : Node_Id;
12845 Related_Nod : Node_Id;
12846 Related_Id : Entity_Id;
12847 Suffix : Character)
12848 is
12849 -- Retrieve Base_Type to ensure getting to the concurrent type in the
12850 -- case of a private subtype (needed when only doing semantic analysis).
12851
12852 T_Ent : Entity_Id := Base_Type (Entity (Subtype_Mark (SI)));
12853 T_Val : Entity_Id;
12854
12855 begin
12856 if Is_Access_Type (T_Ent) then
12857 T_Ent := Designated_Type (T_Ent);
12858 end if;
12859
12860 T_Val := Corresponding_Record_Type (T_Ent);
12861
12862 if Present (T_Val) then
12863
12864 if No (Def_Id) then
12865 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
12866 end if;
12867
12868 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
12869
12870 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
12871 Set_Corresponding_Record_Type (Def_Id,
12872 Constrain_Corresponding_Record (Def_Id, T_Val, Related_Nod));
12873
12874 else
12875 -- If there is no associated record, expansion is disabled and this
12876 -- is a generic context. Create a subtype in any case, so that
12877 -- semantic analysis can proceed.
12878
12879 if No (Def_Id) then
12880 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
12881 end if;
12882
12883 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
12884 end if;
12885 end Constrain_Concurrent;
12886
12887 ------------------------------------
12888 -- Constrain_Corresponding_Record --
12889 ------------------------------------
12890
12891 function Constrain_Corresponding_Record
12892 (Prot_Subt : Entity_Id;
12893 Corr_Rec : Entity_Id;
12894 Related_Nod : Node_Id) return Entity_Id
12895 is
12896 T_Sub : constant Entity_Id :=
12897 Create_Itype (E_Record_Subtype, Related_Nod, Corr_Rec, 'C');
12898
12899 begin
12900 Set_Etype (T_Sub, Corr_Rec);
12901 Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
12902 Set_Is_Constrained (T_Sub, True);
12903 Set_First_Entity (T_Sub, First_Entity (Corr_Rec));
12904 Set_Last_Entity (T_Sub, Last_Entity (Corr_Rec));
12905
12906 if Has_Discriminants (Prot_Subt) then -- False only if errors.
12907 Set_Discriminant_Constraint
12908 (T_Sub, Discriminant_Constraint (Prot_Subt));
12909 Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
12910 Create_Constrained_Components
12911 (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
12912 end if;
12913
12914 Set_Depends_On_Private (T_Sub, Has_Private_Component (T_Sub));
12915
12916 if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
12917 Conditional_Delay (T_Sub, Corr_Rec);
12918
12919 else
12920 -- This is a component subtype: it will be frozen in the context of
12921 -- the enclosing record's init_proc, so that discriminant references
12922 -- are resolved to discriminals. (Note: we used to skip freezing
12923 -- altogether in that case, which caused errors downstream for
12924 -- components of a bit packed array type).
12925
12926 Set_Has_Delayed_Freeze (T_Sub);
12927 end if;
12928
12929 return T_Sub;
12930 end Constrain_Corresponding_Record;
12931
12932 -----------------------
12933 -- Constrain_Decimal --
12934 -----------------------
12935
12936 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id) is
12937 T : constant Entity_Id := Entity (Subtype_Mark (S));
12938 C : constant Node_Id := Constraint (S);
12939 Loc : constant Source_Ptr := Sloc (C);
12940 Range_Expr : Node_Id;
12941 Digits_Expr : Node_Id;
12942 Digits_Val : Uint;
12943 Bound_Val : Ureal;
12944
12945 begin
12946 Set_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
12947
12948 if Nkind (C) = N_Range_Constraint then
12949 Range_Expr := Range_Expression (C);
12950 Digits_Val := Digits_Value (T);
12951
12952 else
12953 pragma Assert (Nkind (C) = N_Digits_Constraint);
12954
12955 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
12956
12957 Digits_Expr := Digits_Expression (C);
12958 Analyze_And_Resolve (Digits_Expr, Any_Integer);
12959
12960 Check_Digits_Expression (Digits_Expr);
12961 Digits_Val := Expr_Value (Digits_Expr);
12962
12963 if Digits_Val > Digits_Value (T) then
12964 Error_Msg_N
12965 ("digits expression is incompatible with subtype", C);
12966 Digits_Val := Digits_Value (T);
12967 end if;
12968
12969 if Present (Range_Constraint (C)) then
12970 Range_Expr := Range_Expression (Range_Constraint (C));
12971 else
12972 Range_Expr := Empty;
12973 end if;
12974 end if;
12975
12976 Set_Etype (Def_Id, Base_Type (T));
12977 Set_Size_Info (Def_Id, (T));
12978 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
12979 Set_Delta_Value (Def_Id, Delta_Value (T));
12980 Set_Scale_Value (Def_Id, Scale_Value (T));
12981 Set_Small_Value (Def_Id, Small_Value (T));
12982 Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
12983 Set_Digits_Value (Def_Id, Digits_Val);
12984
12985 -- Manufacture range from given digits value if no range present
12986
12987 if No (Range_Expr) then
12988 Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
12989 Range_Expr :=
12990 Make_Range (Loc,
12991 Low_Bound =>
12992 Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
12993 High_Bound =>
12994 Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
12995 end if;
12996
12997 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
12998 Set_Discrete_RM_Size (Def_Id);
12999
13000 -- Unconditionally delay the freeze, since we cannot set size
13001 -- information in all cases correctly until the freeze point.
13002
13003 Set_Has_Delayed_Freeze (Def_Id);
13004 end Constrain_Decimal;
13005
13006 ----------------------------------
13007 -- Constrain_Discriminated_Type --
13008 ----------------------------------
13009
13010 procedure Constrain_Discriminated_Type
13011 (Def_Id : Entity_Id;
13012 S : Node_Id;
13013 Related_Nod : Node_Id;
13014 For_Access : Boolean := False)
13015 is
13016 E : constant Entity_Id := Entity (Subtype_Mark (S));
13017 T : Entity_Id;
13018 C : Node_Id;
13019 Elist : Elist_Id := New_Elmt_List;
13020
13021 procedure Fixup_Bad_Constraint;
13022 -- This is called after finding a bad constraint, and after having
13023 -- posted an appropriate error message. The mission is to leave the
13024 -- entity T in as reasonable state as possible.
13025
13026 --------------------------
13027 -- Fixup_Bad_Constraint --
13028 --------------------------
13029
13030 procedure Fixup_Bad_Constraint is
13031 begin
13032 -- Set a reasonable Ekind for the entity. For an incomplete type,
13033 -- we can't do much, but for other types, we can set the proper
13034 -- corresponding subtype kind.
13035
13036 if Ekind (T) = E_Incomplete_Type then
13037 Set_Ekind (Def_Id, Ekind (T));
13038 else
13039 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
13040 end if;
13041
13042 -- Set Etype to the known type, to reduce chances of cascaded errors
13043
13044 Set_Etype (Def_Id, E);
13045 Set_Error_Posted (Def_Id);
13046 end Fixup_Bad_Constraint;
13047
13048 -- Start of processing for Constrain_Discriminated_Type
13049
13050 begin
13051 C := Constraint (S);
13052
13053 -- A discriminant constraint is only allowed in a subtype indication,
13054 -- after a subtype mark. This subtype mark must denote either a type
13055 -- with discriminants, or an access type whose designated type is a
13056 -- type with discriminants. A discriminant constraint specifies the
13057 -- values of these discriminants (RM 3.7.2(5)).
13058
13059 T := Base_Type (Entity (Subtype_Mark (S)));
13060
13061 if Is_Access_Type (T) then
13062 T := Designated_Type (T);
13063 end if;
13064
13065 -- Ada 2005 (AI-412): Constrained incomplete subtypes are illegal.
13066 -- Avoid generating an error for access-to-incomplete subtypes.
13067
13068 if Ada_Version >= Ada_2005
13069 and then Ekind (T) = E_Incomplete_Type
13070 and then Nkind (Parent (S)) = N_Subtype_Declaration
13071 and then not Is_Itype (Def_Id)
13072 then
13073 -- A little sanity check, emit an error message if the type
13074 -- has discriminants to begin with. Type T may be a regular
13075 -- incomplete type or imported via a limited with clause.
13076
13077 if Has_Discriminants (T)
13078 or else (From_Limited_With (T)
13079 and then Present (Non_Limited_View (T))
13080 and then Nkind (Parent (Non_Limited_View (T))) =
13081 N_Full_Type_Declaration
13082 and then Present (Discriminant_Specifications
13083 (Parent (Non_Limited_View (T)))))
13084 then
13085 Error_Msg_N
13086 ("(Ada 2005) incomplete subtype may not be constrained", C);
13087 else
13088 Error_Msg_N ("invalid constraint: type has no discriminant", C);
13089 end if;
13090
13091 Fixup_Bad_Constraint;
13092 return;
13093
13094 -- Check that the type has visible discriminants. The type may be
13095 -- a private type with unknown discriminants whose full view has
13096 -- discriminants which are invisible.
13097
13098 elsif not Has_Discriminants (T)
13099 or else
13100 (Has_Unknown_Discriminants (T)
13101 and then Is_Private_Type (T))
13102 then
13103 Error_Msg_N ("invalid constraint: type has no discriminant", C);
13104 Fixup_Bad_Constraint;
13105 return;
13106
13107 elsif Is_Constrained (E)
13108 or else (Ekind (E) = E_Class_Wide_Subtype
13109 and then Present (Discriminant_Constraint (E)))
13110 then
13111 Error_Msg_N ("type is already constrained", Subtype_Mark (S));
13112 Fixup_Bad_Constraint;
13113 return;
13114 end if;
13115
13116 -- T may be an unconstrained subtype (e.g. a generic actual).
13117 -- Constraint applies to the base type.
13118
13119 T := Base_Type (T);
13120
13121 Elist := Build_Discriminant_Constraints (T, S);
13122
13123 -- If the list returned was empty we had an error in building the
13124 -- discriminant constraint. We have also already signalled an error
13125 -- in the incomplete type case
13126
13127 if Is_Empty_Elmt_List (Elist) then
13128 Fixup_Bad_Constraint;
13129 return;
13130 end if;
13131
13132 Build_Discriminated_Subtype (T, Def_Id, Elist, Related_Nod, For_Access);
13133 end Constrain_Discriminated_Type;
13134
13135 ---------------------------
13136 -- Constrain_Enumeration --
13137 ---------------------------
13138
13139 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id) is
13140 T : constant Entity_Id := Entity (Subtype_Mark (S));
13141 C : constant Node_Id := Constraint (S);
13142
13143 begin
13144 Set_Ekind (Def_Id, E_Enumeration_Subtype);
13145
13146 Set_First_Literal (Def_Id, First_Literal (Base_Type (T)));
13147
13148 Set_Etype (Def_Id, Base_Type (T));
13149 Set_Size_Info (Def_Id, (T));
13150 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13151 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
13152
13153 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13154
13155 Set_Discrete_RM_Size (Def_Id);
13156 end Constrain_Enumeration;
13157
13158 ----------------------
13159 -- Constrain_Float --
13160 ----------------------
13161
13162 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id) is
13163 T : constant Entity_Id := Entity (Subtype_Mark (S));
13164 C : Node_Id;
13165 D : Node_Id;
13166 Rais : Node_Id;
13167
13168 begin
13169 Set_Ekind (Def_Id, E_Floating_Point_Subtype);
13170
13171 Set_Etype (Def_Id, Base_Type (T));
13172 Set_Size_Info (Def_Id, (T));
13173 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13174
13175 -- Process the constraint
13176
13177 C := Constraint (S);
13178
13179 -- Digits constraint present
13180
13181 if Nkind (C) = N_Digits_Constraint then
13182
13183 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
13184 Check_Restriction (No_Obsolescent_Features, C);
13185
13186 if Warn_On_Obsolescent_Feature then
13187 Error_Msg_N
13188 ("subtype digits constraint is an " &
13189 "obsolescent feature (RM J.3(8))?j?", C);
13190 end if;
13191
13192 D := Digits_Expression (C);
13193 Analyze_And_Resolve (D, Any_Integer);
13194 Check_Digits_Expression (D);
13195 Set_Digits_Value (Def_Id, Expr_Value (D));
13196
13197 -- Check that digits value is in range. Obviously we can do this
13198 -- at compile time, but it is strictly a runtime check, and of
13199 -- course there is an ACVC test that checks this.
13200
13201 if Digits_Value (Def_Id) > Digits_Value (T) then
13202 Error_Msg_Uint_1 := Digits_Value (T);
13203 Error_Msg_N ("??digits value is too large, maximum is ^", D);
13204 Rais :=
13205 Make_Raise_Constraint_Error (Sloc (D),
13206 Reason => CE_Range_Check_Failed);
13207 Insert_Action (Declaration_Node (Def_Id), Rais);
13208 end if;
13209
13210 C := Range_Constraint (C);
13211
13212 -- No digits constraint present
13213
13214 else
13215 Set_Digits_Value (Def_Id, Digits_Value (T));
13216 end if;
13217
13218 -- Range constraint present
13219
13220 if Nkind (C) = N_Range_Constraint then
13221 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13222
13223 -- No range constraint present
13224
13225 else
13226 pragma Assert (No (C));
13227 Set_Scalar_Range (Def_Id, Scalar_Range (T));
13228 end if;
13229
13230 Set_Is_Constrained (Def_Id);
13231 end Constrain_Float;
13232
13233 ---------------------
13234 -- Constrain_Index --
13235 ---------------------
13236
13237 procedure Constrain_Index
13238 (Index : Node_Id;
13239 S : Node_Id;
13240 Related_Nod : Node_Id;
13241 Related_Id : Entity_Id;
13242 Suffix : Character;
13243 Suffix_Index : Nat)
13244 is
13245 Def_Id : Entity_Id;
13246 R : Node_Id := Empty;
13247 T : constant Entity_Id := Etype (Index);
13248
13249 begin
13250 Def_Id :=
13251 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
13252 Set_Etype (Def_Id, Base_Type (T));
13253
13254 if Nkind (S) = N_Range
13255 or else
13256 (Nkind (S) = N_Attribute_Reference
13257 and then Attribute_Name (S) = Name_Range)
13258 then
13259 -- A Range attribute will be transformed into N_Range by Resolve
13260
13261 Analyze (S);
13262 Set_Etype (S, T);
13263 R := S;
13264
13265 Process_Range_Expr_In_Decl (R, T);
13266
13267 if not Error_Posted (S)
13268 and then
13269 (Nkind (S) /= N_Range
13270 or else not Covers (T, (Etype (Low_Bound (S))))
13271 or else not Covers (T, (Etype (High_Bound (S)))))
13272 then
13273 if Base_Type (T) /= Any_Type
13274 and then Etype (Low_Bound (S)) /= Any_Type
13275 and then Etype (High_Bound (S)) /= Any_Type
13276 then
13277 Error_Msg_N ("range expected", S);
13278 end if;
13279 end if;
13280
13281 elsif Nkind (S) = N_Subtype_Indication then
13282
13283 -- The parser has verified that this is a discrete indication
13284
13285 Resolve_Discrete_Subtype_Indication (S, T);
13286 Bad_Predicated_Subtype_Use
13287 ("subtype& has predicate, not allowed in index constraint",
13288 S, Entity (Subtype_Mark (S)));
13289
13290 R := Range_Expression (Constraint (S));
13291
13292 -- Capture values of bounds and generate temporaries for them if
13293 -- needed, since checks may cause duplication of the expressions
13294 -- which must not be reevaluated.
13295
13296 -- The forced evaluation removes side effects from expressions, which
13297 -- should occur also in GNATprove mode. Otherwise, we end up with
13298 -- unexpected insertions of actions at places where this is not
13299 -- supposed to occur, e.g. on default parameters of a call.
13300
13301 if Expander_Active or GNATprove_Mode then
13302 Force_Evaluation
13303 (Low_Bound (R), Related_Id => Def_Id, Is_Low_Bound => True);
13304 Force_Evaluation
13305 (High_Bound (R), Related_Id => Def_Id, Is_High_Bound => True);
13306 end if;
13307
13308 elsif Nkind (S) = N_Discriminant_Association then
13309
13310 -- Syntactically valid in subtype indication
13311
13312 Error_Msg_N ("invalid index constraint", S);
13313 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
13314 return;
13315
13316 -- Subtype_Mark case, no anonymous subtypes to construct
13317
13318 else
13319 Analyze (S);
13320
13321 if Is_Entity_Name (S) then
13322 if not Is_Type (Entity (S)) then
13323 Error_Msg_N ("expect subtype mark for index constraint", S);
13324
13325 elsif Base_Type (Entity (S)) /= Base_Type (T) then
13326 Wrong_Type (S, Base_Type (T));
13327
13328 -- Check error of subtype with predicate in index constraint
13329
13330 else
13331 Bad_Predicated_Subtype_Use
13332 ("subtype& has predicate, not allowed in index constraint",
13333 S, Entity (S));
13334 end if;
13335
13336 return;
13337
13338 else
13339 Error_Msg_N ("invalid index constraint", S);
13340 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
13341 return;
13342 end if;
13343 end if;
13344
13345 -- Complete construction of the Itype
13346
13347 if Is_Modular_Integer_Type (T) then
13348 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
13349
13350 elsif Is_Integer_Type (T) then
13351 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
13352
13353 else
13354 Set_Ekind (Def_Id, E_Enumeration_Subtype);
13355 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
13356 Set_First_Literal (Def_Id, First_Literal (T));
13357 end if;
13358
13359 Set_Size_Info (Def_Id, (T));
13360 Set_RM_Size (Def_Id, RM_Size (T));
13361 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13362
13363 Set_Scalar_Range (Def_Id, R);
13364
13365 Set_Etype (S, Def_Id);
13366 Set_Discrete_RM_Size (Def_Id);
13367 end Constrain_Index;
13368
13369 -----------------------
13370 -- Constrain_Integer --
13371 -----------------------
13372
13373 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id) is
13374 T : constant Entity_Id := Entity (Subtype_Mark (S));
13375 C : constant Node_Id := Constraint (S);
13376
13377 begin
13378 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13379
13380 if Is_Modular_Integer_Type (T) then
13381 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
13382 else
13383 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
13384 end if;
13385
13386 Set_Etype (Def_Id, Base_Type (T));
13387 Set_Size_Info (Def_Id, (T));
13388 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13389 Set_Discrete_RM_Size (Def_Id);
13390 end Constrain_Integer;
13391
13392 ------------------------------
13393 -- Constrain_Ordinary_Fixed --
13394 ------------------------------
13395
13396 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id) is
13397 T : constant Entity_Id := Entity (Subtype_Mark (S));
13398 C : Node_Id;
13399 D : Node_Id;
13400 Rais : Node_Id;
13401
13402 begin
13403 Set_Ekind (Def_Id, E_Ordinary_Fixed_Point_Subtype);
13404 Set_Etype (Def_Id, Base_Type (T));
13405 Set_Size_Info (Def_Id, (T));
13406 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13407 Set_Small_Value (Def_Id, Small_Value (T));
13408
13409 -- Process the constraint
13410
13411 C := Constraint (S);
13412
13413 -- Delta constraint present
13414
13415 if Nkind (C) = N_Delta_Constraint then
13416
13417 Check_SPARK_05_Restriction ("delta constraint is not allowed", S);
13418 Check_Restriction (No_Obsolescent_Features, C);
13419
13420 if Warn_On_Obsolescent_Feature then
13421 Error_Msg_S
13422 ("subtype delta constraint is an " &
13423 "obsolescent feature (RM J.3(7))?j?");
13424 end if;
13425
13426 D := Delta_Expression (C);
13427 Analyze_And_Resolve (D, Any_Real);
13428 Check_Delta_Expression (D);
13429 Set_Delta_Value (Def_Id, Expr_Value_R (D));
13430
13431 -- Check that delta value is in range. Obviously we can do this
13432 -- at compile time, but it is strictly a runtime check, and of
13433 -- course there is an ACVC test that checks this.
13434
13435 if Delta_Value (Def_Id) < Delta_Value (T) then
13436 Error_Msg_N ("??delta value is too small", D);
13437 Rais :=
13438 Make_Raise_Constraint_Error (Sloc (D),
13439 Reason => CE_Range_Check_Failed);
13440 Insert_Action (Declaration_Node (Def_Id), Rais);
13441 end if;
13442
13443 C := Range_Constraint (C);
13444
13445 -- No delta constraint present
13446
13447 else
13448 Set_Delta_Value (Def_Id, Delta_Value (T));
13449 end if;
13450
13451 -- Range constraint present
13452
13453 if Nkind (C) = N_Range_Constraint then
13454 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13455
13456 -- No range constraint present
13457
13458 else
13459 pragma Assert (No (C));
13460 Set_Scalar_Range (Def_Id, Scalar_Range (T));
13461 end if;
13462
13463 Set_Discrete_RM_Size (Def_Id);
13464
13465 -- Unconditionally delay the freeze, since we cannot set size
13466 -- information in all cases correctly until the freeze point.
13467
13468 Set_Has_Delayed_Freeze (Def_Id);
13469 end Constrain_Ordinary_Fixed;
13470
13471 -----------------------
13472 -- Contain_Interface --
13473 -----------------------
13474
13475 function Contain_Interface
13476 (Iface : Entity_Id;
13477 Ifaces : Elist_Id) return Boolean
13478 is
13479 Iface_Elmt : Elmt_Id;
13480
13481 begin
13482 if Present (Ifaces) then
13483 Iface_Elmt := First_Elmt (Ifaces);
13484 while Present (Iface_Elmt) loop
13485 if Node (Iface_Elmt) = Iface then
13486 return True;
13487 end if;
13488
13489 Next_Elmt (Iface_Elmt);
13490 end loop;
13491 end if;
13492
13493 return False;
13494 end Contain_Interface;
13495
13496 ---------------------------
13497 -- Convert_Scalar_Bounds --
13498 ---------------------------
13499
13500 procedure Convert_Scalar_Bounds
13501 (N : Node_Id;
13502 Parent_Type : Entity_Id;
13503 Derived_Type : Entity_Id;
13504 Loc : Source_Ptr)
13505 is
13506 Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
13507
13508 Lo : Node_Id;
13509 Hi : Node_Id;
13510 Rng : Node_Id;
13511
13512 begin
13513 -- Defend against previous errors
13514
13515 if No (Scalar_Range (Derived_Type)) then
13516 Check_Error_Detected;
13517 return;
13518 end if;
13519
13520 Lo := Build_Scalar_Bound
13521 (Type_Low_Bound (Derived_Type),
13522 Parent_Type, Implicit_Base);
13523
13524 Hi := Build_Scalar_Bound
13525 (Type_High_Bound (Derived_Type),
13526 Parent_Type, Implicit_Base);
13527
13528 Rng :=
13529 Make_Range (Loc,
13530 Low_Bound => Lo,
13531 High_Bound => Hi);
13532
13533 Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
13534
13535 Set_Parent (Rng, N);
13536 Set_Scalar_Range (Derived_Type, Rng);
13537
13538 -- Analyze the bounds
13539
13540 Analyze_And_Resolve (Lo, Implicit_Base);
13541 Analyze_And_Resolve (Hi, Implicit_Base);
13542
13543 -- Analyze the range itself, except that we do not analyze it if
13544 -- the bounds are real literals, and we have a fixed-point type.
13545 -- The reason for this is that we delay setting the bounds in this
13546 -- case till we know the final Small and Size values (see circuit
13547 -- in Freeze.Freeze_Fixed_Point_Type for further details).
13548
13549 if Is_Fixed_Point_Type (Parent_Type)
13550 and then Nkind (Lo) = N_Real_Literal
13551 and then Nkind (Hi) = N_Real_Literal
13552 then
13553 return;
13554
13555 -- Here we do the analysis of the range
13556
13557 -- Note: we do this manually, since if we do a normal Analyze and
13558 -- Resolve call, there are problems with the conversions used for
13559 -- the derived type range.
13560
13561 else
13562 Set_Etype (Rng, Implicit_Base);
13563 Set_Analyzed (Rng, True);
13564 end if;
13565 end Convert_Scalar_Bounds;
13566
13567 -------------------
13568 -- Copy_And_Swap --
13569 -------------------
13570
13571 procedure Copy_And_Swap (Priv, Full : Entity_Id) is
13572 begin
13573 -- Initialize new full declaration entity by copying the pertinent
13574 -- fields of the corresponding private declaration entity.
13575
13576 -- We temporarily set Ekind to a value appropriate for a type to
13577 -- avoid assert failures in Einfo from checking for setting type
13578 -- attributes on something that is not a type. Ekind (Priv) is an
13579 -- appropriate choice, since it allowed the attributes to be set
13580 -- in the first place. This Ekind value will be modified later.
13581
13582 Set_Ekind (Full, Ekind (Priv));
13583
13584 -- Also set Etype temporarily to Any_Type, again, in the absence
13585 -- of errors, it will be properly reset, and if there are errors,
13586 -- then we want a value of Any_Type to remain.
13587
13588 Set_Etype (Full, Any_Type);
13589
13590 -- Now start copying attributes
13591
13592 Set_Has_Discriminants (Full, Has_Discriminants (Priv));
13593
13594 if Has_Discriminants (Full) then
13595 Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
13596 Set_Stored_Constraint (Full, Stored_Constraint (Priv));
13597 end if;
13598
13599 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
13600 Set_Homonym (Full, Homonym (Priv));
13601 Set_Is_Immediately_Visible (Full, Is_Immediately_Visible (Priv));
13602 Set_Is_Public (Full, Is_Public (Priv));
13603 Set_Is_Pure (Full, Is_Pure (Priv));
13604 Set_Is_Tagged_Type (Full, Is_Tagged_Type (Priv));
13605 Set_Has_Pragma_Unmodified (Full, Has_Pragma_Unmodified (Priv));
13606 Set_Has_Pragma_Unreferenced (Full, Has_Pragma_Unreferenced (Priv));
13607 Set_Has_Pragma_Unreferenced_Objects
13608 (Full, Has_Pragma_Unreferenced_Objects
13609 (Priv));
13610
13611 Conditional_Delay (Full, Priv);
13612
13613 if Is_Tagged_Type (Full) then
13614 Set_Direct_Primitive_Operations
13615 (Full, Direct_Primitive_Operations (Priv));
13616 Set_No_Tagged_Streams_Pragma
13617 (Full, No_Tagged_Streams_Pragma (Priv));
13618
13619 if Is_Base_Type (Priv) then
13620 Set_Class_Wide_Type (Full, Class_Wide_Type (Priv));
13621 end if;
13622 end if;
13623
13624 Set_Is_Volatile (Full, Is_Volatile (Priv));
13625 Set_Treat_As_Volatile (Full, Treat_As_Volatile (Priv));
13626 Set_Scope (Full, Scope (Priv));
13627 Set_Next_Entity (Full, Next_Entity (Priv));
13628 Set_First_Entity (Full, First_Entity (Priv));
13629 Set_Last_Entity (Full, Last_Entity (Priv));
13630
13631 -- If access types have been recorded for later handling, keep them in
13632 -- the full view so that they get handled when the full view freeze
13633 -- node is expanded.
13634
13635 if Present (Freeze_Node (Priv))
13636 and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
13637 then
13638 Ensure_Freeze_Node (Full);
13639 Set_Access_Types_To_Process
13640 (Freeze_Node (Full),
13641 Access_Types_To_Process (Freeze_Node (Priv)));
13642 end if;
13643
13644 -- Swap the two entities. Now Private is the full type entity and Full
13645 -- is the private one. They will be swapped back at the end of the
13646 -- private part. This swapping ensures that the entity that is visible
13647 -- in the private part is the full declaration.
13648
13649 Exchange_Entities (Priv, Full);
13650 Append_Entity (Full, Scope (Full));
13651 end Copy_And_Swap;
13652
13653 -------------------------------------
13654 -- Copy_Array_Base_Type_Attributes --
13655 -------------------------------------
13656
13657 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
13658 begin
13659 Set_Component_Alignment (T1, Component_Alignment (T2));
13660 Set_Component_Type (T1, Component_Type (T2));
13661 Set_Component_Size (T1, Component_Size (T2));
13662 Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
13663 Set_Has_Non_Standard_Rep (T1, Has_Non_Standard_Rep (T2));
13664 Set_Has_Protected (T1, Has_Protected (T2));
13665 Set_Has_Task (T1, Has_Task (T2));
13666 Set_Is_Packed (T1, Is_Packed (T2));
13667 Set_Has_Aliased_Components (T1, Has_Aliased_Components (T2));
13668 Set_Has_Atomic_Components (T1, Has_Atomic_Components (T2));
13669 Set_Has_Volatile_Components (T1, Has_Volatile_Components (T2));
13670 end Copy_Array_Base_Type_Attributes;
13671
13672 -----------------------------------
13673 -- Copy_Array_Subtype_Attributes --
13674 -----------------------------------
13675
13676 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
13677 begin
13678 Set_Size_Info (T1, T2);
13679
13680 Set_First_Index (T1, First_Index (T2));
13681 Set_Is_Aliased (T1, Is_Aliased (T2));
13682 Set_Is_Volatile (T1, Is_Volatile (T2));
13683 Set_Treat_As_Volatile (T1, Treat_As_Volatile (T2));
13684 Set_Is_Constrained (T1, Is_Constrained (T2));
13685 Set_Depends_On_Private (T1, Has_Private_Component (T2));
13686 Inherit_Rep_Item_Chain (T1, T2);
13687 Set_Convention (T1, Convention (T2));
13688 Set_Is_Limited_Composite (T1, Is_Limited_Composite (T2));
13689 Set_Is_Private_Composite (T1, Is_Private_Composite (T2));
13690 Set_Packed_Array_Impl_Type (T1, Packed_Array_Impl_Type (T2));
13691 end Copy_Array_Subtype_Attributes;
13692
13693 -----------------------------------
13694 -- Create_Constrained_Components --
13695 -----------------------------------
13696
13697 procedure Create_Constrained_Components
13698 (Subt : Entity_Id;
13699 Decl_Node : Node_Id;
13700 Typ : Entity_Id;
13701 Constraints : Elist_Id)
13702 is
13703 Loc : constant Source_Ptr := Sloc (Subt);
13704 Comp_List : constant Elist_Id := New_Elmt_List;
13705 Parent_Type : constant Entity_Id := Etype (Typ);
13706 Assoc_List : constant List_Id := New_List;
13707 Discr_Val : Elmt_Id;
13708 Errors : Boolean;
13709 New_C : Entity_Id;
13710 Old_C : Entity_Id;
13711 Is_Static : Boolean := True;
13712
13713 procedure Collect_Fixed_Components (Typ : Entity_Id);
13714 -- Collect parent type components that do not appear in a variant part
13715
13716 procedure Create_All_Components;
13717 -- Iterate over Comp_List to create the components of the subtype
13718
13719 function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
13720 -- Creates a new component from Old_Compon, copying all the fields from
13721 -- it, including its Etype, inserts the new component in the Subt entity
13722 -- chain and returns the new component.
13723
13724 function Is_Variant_Record (T : Entity_Id) return Boolean;
13725 -- If true, and discriminants are static, collect only components from
13726 -- variants selected by discriminant values.
13727
13728 ------------------------------
13729 -- Collect_Fixed_Components --
13730 ------------------------------
13731
13732 procedure Collect_Fixed_Components (Typ : Entity_Id) is
13733 begin
13734 -- Build association list for discriminants, and find components of the
13735 -- variant part selected by the values of the discriminants.
13736
13737 Old_C := First_Discriminant (Typ);
13738 Discr_Val := First_Elmt (Constraints);
13739 while Present (Old_C) loop
13740 Append_To (Assoc_List,
13741 Make_Component_Association (Loc,
13742 Choices => New_List (New_Occurrence_Of (Old_C, Loc)),
13743 Expression => New_Copy (Node (Discr_Val))));
13744
13745 Next_Elmt (Discr_Val);
13746 Next_Discriminant (Old_C);
13747 end loop;
13748
13749 -- The tag and the possible parent component are unconditionally in
13750 -- the subtype.
13751
13752 if Is_Tagged_Type (Typ) or else Has_Controlled_Component (Typ) then
13753 Old_C := First_Component (Typ);
13754 while Present (Old_C) loop
13755 if Nam_In (Chars (Old_C), Name_uTag, Name_uParent) then
13756 Append_Elmt (Old_C, Comp_List);
13757 end if;
13758
13759 Next_Component (Old_C);
13760 end loop;
13761 end if;
13762 end Collect_Fixed_Components;
13763
13764 ---------------------------
13765 -- Create_All_Components --
13766 ---------------------------
13767
13768 procedure Create_All_Components is
13769 Comp : Elmt_Id;
13770
13771 begin
13772 Comp := First_Elmt (Comp_List);
13773 while Present (Comp) loop
13774 Old_C := Node (Comp);
13775 New_C := Create_Component (Old_C);
13776
13777 Set_Etype
13778 (New_C,
13779 Constrain_Component_Type
13780 (Old_C, Subt, Decl_Node, Typ, Constraints));
13781 Set_Is_Public (New_C, Is_Public (Subt));
13782
13783 Next_Elmt (Comp);
13784 end loop;
13785 end Create_All_Components;
13786
13787 ----------------------
13788 -- Create_Component --
13789 ----------------------
13790
13791 function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
13792 New_Compon : constant Entity_Id := New_Copy (Old_Compon);
13793
13794 begin
13795 if Ekind (Old_Compon) = E_Discriminant
13796 and then Is_Completely_Hidden (Old_Compon)
13797 then
13798 -- This is a shadow discriminant created for a discriminant of
13799 -- the parent type, which needs to be present in the subtype.
13800 -- Give the shadow discriminant an internal name that cannot
13801 -- conflict with that of visible components.
13802
13803 Set_Chars (New_Compon, New_Internal_Name ('C'));
13804 end if;
13805
13806 -- Set the parent so we have a proper link for freezing etc. This is
13807 -- not a real parent pointer, since of course our parent does not own
13808 -- up to us and reference us, we are an illegitimate child of the
13809 -- original parent.
13810
13811 Set_Parent (New_Compon, Parent (Old_Compon));
13812
13813 -- If the old component's Esize was already determined and is a
13814 -- static value, then the new component simply inherits it. Otherwise
13815 -- the old component's size may require run-time determination, but
13816 -- the new component's size still might be statically determinable
13817 -- (if, for example it has a static constraint). In that case we want
13818 -- Layout_Type to recompute the component's size, so we reset its
13819 -- size and positional fields.
13820
13821 if Frontend_Layout_On_Target
13822 and then not Known_Static_Esize (Old_Compon)
13823 then
13824 Set_Esize (New_Compon, Uint_0);
13825 Init_Normalized_First_Bit (New_Compon);
13826 Init_Normalized_Position (New_Compon);
13827 Init_Normalized_Position_Max (New_Compon);
13828 end if;
13829
13830 -- We do not want this node marked as Comes_From_Source, since
13831 -- otherwise it would get first class status and a separate cross-
13832 -- reference line would be generated. Illegitimate children do not
13833 -- rate such recognition.
13834
13835 Set_Comes_From_Source (New_Compon, False);
13836
13837 -- But it is a real entity, and a birth certificate must be properly
13838 -- registered by entering it into the entity list.
13839
13840 Enter_Name (New_Compon);
13841
13842 return New_Compon;
13843 end Create_Component;
13844
13845 -----------------------
13846 -- Is_Variant_Record --
13847 -----------------------
13848
13849 function Is_Variant_Record (T : Entity_Id) return Boolean is
13850 begin
13851 return Nkind (Parent (T)) = N_Full_Type_Declaration
13852 and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
13853 and then Present (Component_List (Type_Definition (Parent (T))))
13854 and then
13855 Present
13856 (Variant_Part (Component_List (Type_Definition (Parent (T)))));
13857 end Is_Variant_Record;
13858
13859 -- Start of processing for Create_Constrained_Components
13860
13861 begin
13862 pragma Assert (Subt /= Base_Type (Subt));
13863 pragma Assert (Typ = Base_Type (Typ));
13864
13865 Set_First_Entity (Subt, Empty);
13866 Set_Last_Entity (Subt, Empty);
13867
13868 -- Check whether constraint is fully static, in which case we can
13869 -- optimize the list of components.
13870
13871 Discr_Val := First_Elmt (Constraints);
13872 while Present (Discr_Val) loop
13873 if not Is_OK_Static_Expression (Node (Discr_Val)) then
13874 Is_Static := False;
13875 exit;
13876 end if;
13877
13878 Next_Elmt (Discr_Val);
13879 end loop;
13880
13881 Set_Has_Static_Discriminants (Subt, Is_Static);
13882
13883 Push_Scope (Subt);
13884
13885 -- Inherit the discriminants of the parent type
13886
13887 Add_Discriminants : declare
13888 Num_Disc : Int;
13889 Num_Gird : Int;
13890
13891 begin
13892 Num_Disc := 0;
13893 Old_C := First_Discriminant (Typ);
13894
13895 while Present (Old_C) loop
13896 Num_Disc := Num_Disc + 1;
13897 New_C := Create_Component (Old_C);
13898 Set_Is_Public (New_C, Is_Public (Subt));
13899 Next_Discriminant (Old_C);
13900 end loop;
13901
13902 -- For an untagged derived subtype, the number of discriminants may
13903 -- be smaller than the number of inherited discriminants, because
13904 -- several of them may be renamed by a single new discriminant or
13905 -- constrained. In this case, add the hidden discriminants back into
13906 -- the subtype, because they need to be present if the optimizer of
13907 -- the GCC 4.x back-end decides to break apart assignments between
13908 -- objects using the parent view into member-wise assignments.
13909
13910 Num_Gird := 0;
13911
13912 if Is_Derived_Type (Typ)
13913 and then not Is_Tagged_Type (Typ)
13914 then
13915 Old_C := First_Stored_Discriminant (Typ);
13916
13917 while Present (Old_C) loop
13918 Num_Gird := Num_Gird + 1;
13919 Next_Stored_Discriminant (Old_C);
13920 end loop;
13921 end if;
13922
13923 if Num_Gird > Num_Disc then
13924
13925 -- Find out multiple uses of new discriminants, and add hidden
13926 -- components for the extra renamed discriminants. We recognize
13927 -- multiple uses through the Corresponding_Discriminant of a
13928 -- new discriminant: if it constrains several old discriminants,
13929 -- this field points to the last one in the parent type. The
13930 -- stored discriminants of the derived type have the same name
13931 -- as those of the parent.
13932
13933 declare
13934 Constr : Elmt_Id;
13935 New_Discr : Entity_Id;
13936 Old_Discr : Entity_Id;
13937
13938 begin
13939 Constr := First_Elmt (Stored_Constraint (Typ));
13940 Old_Discr := First_Stored_Discriminant (Typ);
13941 while Present (Constr) loop
13942 if Is_Entity_Name (Node (Constr))
13943 and then Ekind (Entity (Node (Constr))) = E_Discriminant
13944 then
13945 New_Discr := Entity (Node (Constr));
13946
13947 if Chars (Corresponding_Discriminant (New_Discr)) /=
13948 Chars (Old_Discr)
13949 then
13950 -- The new discriminant has been used to rename a
13951 -- subsequent old discriminant. Introduce a shadow
13952 -- component for the current old discriminant.
13953
13954 New_C := Create_Component (Old_Discr);
13955 Set_Original_Record_Component (New_C, Old_Discr);
13956 end if;
13957
13958 else
13959 -- The constraint has eliminated the old discriminant.
13960 -- Introduce a shadow component.
13961
13962 New_C := Create_Component (Old_Discr);
13963 Set_Original_Record_Component (New_C, Old_Discr);
13964 end if;
13965
13966 Next_Elmt (Constr);
13967 Next_Stored_Discriminant (Old_Discr);
13968 end loop;
13969 end;
13970 end if;
13971 end Add_Discriminants;
13972
13973 if Is_Static
13974 and then Is_Variant_Record (Typ)
13975 then
13976 Collect_Fixed_Components (Typ);
13977
13978 Gather_Components (
13979 Typ,
13980 Component_List (Type_Definition (Parent (Typ))),
13981 Governed_By => Assoc_List,
13982 Into => Comp_List,
13983 Report_Errors => Errors);
13984 pragma Assert (not Errors);
13985
13986 Create_All_Components;
13987
13988 -- If the subtype declaration is created for a tagged type derivation
13989 -- with constraints, we retrieve the record definition of the parent
13990 -- type to select the components of the proper variant.
13991
13992 elsif Is_Static
13993 and then Is_Tagged_Type (Typ)
13994 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
13995 and then
13996 Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
13997 and then Is_Variant_Record (Parent_Type)
13998 then
13999 Collect_Fixed_Components (Typ);
14000
14001 Gather_Components
14002 (Typ,
14003 Component_List (Type_Definition (Parent (Parent_Type))),
14004 Governed_By => Assoc_List,
14005 Into => Comp_List,
14006 Report_Errors => Errors);
14007
14008 -- Note: previously there was a check at this point that no errors
14009 -- were detected. As a consequence of AI05-220 there may be an error
14010 -- if an inherited discriminant that controls a variant has a non-
14011 -- static constraint.
14012
14013 -- If the tagged derivation has a type extension, collect all the
14014 -- new components therein.
14015
14016 if Present (Record_Extension_Part (Type_Definition (Parent (Typ))))
14017 then
14018 Old_C := First_Component (Typ);
14019 while Present (Old_C) loop
14020 if Original_Record_Component (Old_C) = Old_C
14021 and then Chars (Old_C) /= Name_uTag
14022 and then Chars (Old_C) /= Name_uParent
14023 then
14024 Append_Elmt (Old_C, Comp_List);
14025 end if;
14026
14027 Next_Component (Old_C);
14028 end loop;
14029 end if;
14030
14031 Create_All_Components;
14032
14033 else
14034 -- If discriminants are not static, or if this is a multi-level type
14035 -- extension, we have to include all components of the parent type.
14036
14037 Old_C := First_Component (Typ);
14038 while Present (Old_C) loop
14039 New_C := Create_Component (Old_C);
14040
14041 Set_Etype
14042 (New_C,
14043 Constrain_Component_Type
14044 (Old_C, Subt, Decl_Node, Typ, Constraints));
14045 Set_Is_Public (New_C, Is_Public (Subt));
14046
14047 Next_Component (Old_C);
14048 end loop;
14049 end if;
14050
14051 End_Scope;
14052 end Create_Constrained_Components;
14053
14054 ------------------------------------------
14055 -- Decimal_Fixed_Point_Type_Declaration --
14056 ------------------------------------------
14057
14058 procedure Decimal_Fixed_Point_Type_Declaration
14059 (T : Entity_Id;
14060 Def : Node_Id)
14061 is
14062 Loc : constant Source_Ptr := Sloc (Def);
14063 Digs_Expr : constant Node_Id := Digits_Expression (Def);
14064 Delta_Expr : constant Node_Id := Delta_Expression (Def);
14065 Implicit_Base : Entity_Id;
14066 Digs_Val : Uint;
14067 Delta_Val : Ureal;
14068 Scale_Val : Uint;
14069 Bound_Val : Ureal;
14070
14071 begin
14072 Check_SPARK_05_Restriction
14073 ("decimal fixed point type is not allowed", Def);
14074 Check_Restriction (No_Fixed_Point, Def);
14075
14076 -- Create implicit base type
14077
14078 Implicit_Base :=
14079 Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
14080 Set_Etype (Implicit_Base, Implicit_Base);
14081
14082 -- Analyze and process delta expression
14083
14084 Analyze_And_Resolve (Delta_Expr, Universal_Real);
14085
14086 Check_Delta_Expression (Delta_Expr);
14087 Delta_Val := Expr_Value_R (Delta_Expr);
14088
14089 -- Check delta is power of 10, and determine scale value from it
14090
14091 declare
14092 Val : Ureal;
14093
14094 begin
14095 Scale_Val := Uint_0;
14096 Val := Delta_Val;
14097
14098 if Val < Ureal_1 then
14099 while Val < Ureal_1 loop
14100 Val := Val * Ureal_10;
14101 Scale_Val := Scale_Val + 1;
14102 end loop;
14103
14104 if Scale_Val > 18 then
14105 Error_Msg_N ("scale exceeds maximum value of 18", Def);
14106 Scale_Val := UI_From_Int (+18);
14107 end if;
14108
14109 else
14110 while Val > Ureal_1 loop
14111 Val := Val / Ureal_10;
14112 Scale_Val := Scale_Val - 1;
14113 end loop;
14114
14115 if Scale_Val < -18 then
14116 Error_Msg_N ("scale is less than minimum value of -18", Def);
14117 Scale_Val := UI_From_Int (-18);
14118 end if;
14119 end if;
14120
14121 if Val /= Ureal_1 then
14122 Error_Msg_N ("delta expression must be a power of 10", Def);
14123 Delta_Val := Ureal_10 ** (-Scale_Val);
14124 end if;
14125 end;
14126
14127 -- Set delta, scale and small (small = delta for decimal type)
14128
14129 Set_Delta_Value (Implicit_Base, Delta_Val);
14130 Set_Scale_Value (Implicit_Base, Scale_Val);
14131 Set_Small_Value (Implicit_Base, Delta_Val);
14132
14133 -- Analyze and process digits expression
14134
14135 Analyze_And_Resolve (Digs_Expr, Any_Integer);
14136 Check_Digits_Expression (Digs_Expr);
14137 Digs_Val := Expr_Value (Digs_Expr);
14138
14139 if Digs_Val > 18 then
14140 Digs_Val := UI_From_Int (+18);
14141 Error_Msg_N ("digits value out of range, maximum is 18", Digs_Expr);
14142 end if;
14143
14144 Set_Digits_Value (Implicit_Base, Digs_Val);
14145 Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
14146
14147 -- Set range of base type from digits value for now. This will be
14148 -- expanded to represent the true underlying base range by Freeze.
14149
14150 Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
14151
14152 -- Note: We leave size as zero for now, size will be set at freeze
14153 -- time. We have to do this for ordinary fixed-point, because the size
14154 -- depends on the specified small, and we might as well do the same for
14155 -- decimal fixed-point.
14156
14157 pragma Assert (Esize (Implicit_Base) = Uint_0);
14158
14159 -- If there are bounds given in the declaration use them as the
14160 -- bounds of the first named subtype.
14161
14162 if Present (Real_Range_Specification (Def)) then
14163 declare
14164 RRS : constant Node_Id := Real_Range_Specification (Def);
14165 Low : constant Node_Id := Low_Bound (RRS);
14166 High : constant Node_Id := High_Bound (RRS);
14167 Low_Val : Ureal;
14168 High_Val : Ureal;
14169
14170 begin
14171 Analyze_And_Resolve (Low, Any_Real);
14172 Analyze_And_Resolve (High, Any_Real);
14173 Check_Real_Bound (Low);
14174 Check_Real_Bound (High);
14175 Low_Val := Expr_Value_R (Low);
14176 High_Val := Expr_Value_R (High);
14177
14178 if Low_Val < (-Bound_Val) then
14179 Error_Msg_N
14180 ("range low bound too small for digits value", Low);
14181 Low_Val := -Bound_Val;
14182 end if;
14183
14184 if High_Val > Bound_Val then
14185 Error_Msg_N
14186 ("range high bound too large for digits value", High);
14187 High_Val := Bound_Val;
14188 end if;
14189
14190 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
14191 end;
14192
14193 -- If no explicit range, use range that corresponds to given
14194 -- digits value. This will end up as the final range for the
14195 -- first subtype.
14196
14197 else
14198 Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
14199 end if;
14200
14201 -- Complete entity for first subtype. The inheritance of the rep item
14202 -- chain ensures that SPARK-related pragmas are not clobbered when the
14203 -- decimal fixed point type acts as a full view of a private type.
14204
14205 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
14206 Set_Etype (T, Implicit_Base);
14207 Set_Size_Info (T, Implicit_Base);
14208 Inherit_Rep_Item_Chain (T, Implicit_Base);
14209 Set_Digits_Value (T, Digs_Val);
14210 Set_Delta_Value (T, Delta_Val);
14211 Set_Small_Value (T, Delta_Val);
14212 Set_Scale_Value (T, Scale_Val);
14213 Set_Is_Constrained (T);
14214 end Decimal_Fixed_Point_Type_Declaration;
14215
14216 -----------------------------------
14217 -- Derive_Progenitor_Subprograms --
14218 -----------------------------------
14219
14220 procedure Derive_Progenitor_Subprograms
14221 (Parent_Type : Entity_Id;
14222 Tagged_Type : Entity_Id)
14223 is
14224 E : Entity_Id;
14225 Elmt : Elmt_Id;
14226 Iface : Entity_Id;
14227 Iface_Elmt : Elmt_Id;
14228 Iface_Subp : Entity_Id;
14229 New_Subp : Entity_Id := Empty;
14230 Prim_Elmt : Elmt_Id;
14231 Subp : Entity_Id;
14232 Typ : Entity_Id;
14233
14234 begin
14235 pragma Assert (Ada_Version >= Ada_2005
14236 and then Is_Record_Type (Tagged_Type)
14237 and then Is_Tagged_Type (Tagged_Type)
14238 and then Has_Interfaces (Tagged_Type));
14239
14240 -- Step 1: Transfer to the full-view primitives associated with the
14241 -- partial-view that cover interface primitives. Conceptually this
14242 -- work should be done later by Process_Full_View; done here to
14243 -- simplify its implementation at later stages. It can be safely
14244 -- done here because interfaces must be visible in the partial and
14245 -- private view (RM 7.3(7.3/2)).
14246
14247 -- Small optimization: This work is only required if the parent may
14248 -- have entities whose Alias attribute reference an interface primitive.
14249 -- Such a situation may occur if the parent is an abstract type and the
14250 -- primitive has not been yet overridden or if the parent is a generic
14251 -- formal type covering interfaces.
14252
14253 -- If the tagged type is not abstract, it cannot have abstract
14254 -- primitives (the only entities in the list of primitives of
14255 -- non-abstract tagged types that can reference abstract primitives
14256 -- through its Alias attribute are the internal entities that have
14257 -- attribute Interface_Alias, and these entities are generated later
14258 -- by Add_Internal_Interface_Entities).
14259
14260 if In_Private_Part (Current_Scope)
14261 and then (Is_Abstract_Type (Parent_Type)
14262 or else
14263 Is_Generic_Type (Parent_Type))
14264 then
14265 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
14266 while Present (Elmt) loop
14267 Subp := Node (Elmt);
14268
14269 -- At this stage it is not possible to have entities in the list
14270 -- of primitives that have attribute Interface_Alias.
14271
14272 pragma Assert (No (Interface_Alias (Subp)));
14273
14274 Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
14275
14276 if Is_Interface (Typ) then
14277 E := Find_Primitive_Covering_Interface
14278 (Tagged_Type => Tagged_Type,
14279 Iface_Prim => Subp);
14280
14281 if Present (E)
14282 and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
14283 then
14284 Replace_Elmt (Elmt, E);
14285 Remove_Homonym (Subp);
14286 end if;
14287 end if;
14288
14289 Next_Elmt (Elmt);
14290 end loop;
14291 end if;
14292
14293 -- Step 2: Add primitives of progenitors that are not implemented by
14294 -- parents of Tagged_Type.
14295
14296 if Present (Interfaces (Base_Type (Tagged_Type))) then
14297 Iface_Elmt := First_Elmt (Interfaces (Base_Type (Tagged_Type)));
14298 while Present (Iface_Elmt) loop
14299 Iface := Node (Iface_Elmt);
14300
14301 Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
14302 while Present (Prim_Elmt) loop
14303 Iface_Subp := Node (Prim_Elmt);
14304
14305 -- Exclude derivation of predefined primitives except those
14306 -- that come from source, or are inherited from one that comes
14307 -- from source. Required to catch declarations of equality
14308 -- operators of interfaces. For example:
14309
14310 -- type Iface is interface;
14311 -- function "=" (Left, Right : Iface) return Boolean;
14312
14313 if not Is_Predefined_Dispatching_Operation (Iface_Subp)
14314 or else Comes_From_Source (Ultimate_Alias (Iface_Subp))
14315 then
14316 E := Find_Primitive_Covering_Interface
14317 (Tagged_Type => Tagged_Type,
14318 Iface_Prim => Iface_Subp);
14319
14320 -- If not found we derive a new primitive leaving its alias
14321 -- attribute referencing the interface primitive.
14322
14323 if No (E) then
14324 Derive_Subprogram
14325 (New_Subp, Iface_Subp, Tagged_Type, Iface);
14326
14327 -- Ada 2012 (AI05-0197): If the covering primitive's name
14328 -- differs from the name of the interface primitive then it
14329 -- is a private primitive inherited from a parent type. In
14330 -- such case, given that Tagged_Type covers the interface,
14331 -- the inherited private primitive becomes visible. For such
14332 -- purpose we add a new entity that renames the inherited
14333 -- private primitive.
14334
14335 elsif Chars (E) /= Chars (Iface_Subp) then
14336 pragma Assert (Has_Suffix (E, 'P'));
14337 Derive_Subprogram
14338 (New_Subp, Iface_Subp, Tagged_Type, Iface);
14339 Set_Alias (New_Subp, E);
14340 Set_Is_Abstract_Subprogram (New_Subp,
14341 Is_Abstract_Subprogram (E));
14342
14343 -- Propagate to the full view interface entities associated
14344 -- with the partial view.
14345
14346 elsif In_Private_Part (Current_Scope)
14347 and then Present (Alias (E))
14348 and then Alias (E) = Iface_Subp
14349 and then
14350 List_Containing (Parent (E)) /=
14351 Private_Declarations
14352 (Specification
14353 (Unit_Declaration_Node (Current_Scope)))
14354 then
14355 Append_Elmt (E, Primitive_Operations (Tagged_Type));
14356 end if;
14357 end if;
14358
14359 Next_Elmt (Prim_Elmt);
14360 end loop;
14361
14362 Next_Elmt (Iface_Elmt);
14363 end loop;
14364 end if;
14365 end Derive_Progenitor_Subprograms;
14366
14367 -----------------------
14368 -- Derive_Subprogram --
14369 -----------------------
14370
14371 procedure Derive_Subprogram
14372 (New_Subp : in out Entity_Id;
14373 Parent_Subp : Entity_Id;
14374 Derived_Type : Entity_Id;
14375 Parent_Type : Entity_Id;
14376 Actual_Subp : Entity_Id := Empty)
14377 is
14378 Formal : Entity_Id;
14379 -- Formal parameter of parent primitive operation
14380
14381 Formal_Of_Actual : Entity_Id;
14382 -- Formal parameter of actual operation, when the derivation is to
14383 -- create a renaming for a primitive operation of an actual in an
14384 -- instantiation.
14385
14386 New_Formal : Entity_Id;
14387 -- Formal of inherited operation
14388
14389 Visible_Subp : Entity_Id := Parent_Subp;
14390
14391 function Is_Private_Overriding return Boolean;
14392 -- If Subp is a private overriding of a visible operation, the inherited
14393 -- operation derives from the overridden op (even though its body is the
14394 -- overriding one) and the inherited operation is visible now. See
14395 -- sem_disp to see the full details of the handling of the overridden
14396 -- subprogram, which is removed from the list of primitive operations of
14397 -- the type. The overridden subprogram is saved locally in Visible_Subp,
14398 -- and used to diagnose abstract operations that need overriding in the
14399 -- derived type.
14400
14401 procedure Replace_Type (Id, New_Id : Entity_Id);
14402 -- When the type is an anonymous access type, create a new access type
14403 -- designating the derived type.
14404
14405 procedure Set_Derived_Name;
14406 -- This procedure sets the appropriate Chars name for New_Subp. This
14407 -- is normally just a copy of the parent name. An exception arises for
14408 -- type support subprograms, where the name is changed to reflect the
14409 -- name of the derived type, e.g. if type foo is derived from type bar,
14410 -- then a procedure barDA is derived with a name fooDA.
14411
14412 ---------------------------
14413 -- Is_Private_Overriding --
14414 ---------------------------
14415
14416 function Is_Private_Overriding return Boolean is
14417 Prev : Entity_Id;
14418
14419 begin
14420 -- If the parent is not a dispatching operation there is no
14421 -- need to investigate overridings
14422
14423 if not Is_Dispatching_Operation (Parent_Subp) then
14424 return False;
14425 end if;
14426
14427 -- The visible operation that is overridden is a homonym of the
14428 -- parent subprogram. We scan the homonym chain to find the one
14429 -- whose alias is the subprogram we are deriving.
14430
14431 Prev := Current_Entity (Parent_Subp);
14432 while Present (Prev) loop
14433 if Ekind (Prev) = Ekind (Parent_Subp)
14434 and then Alias (Prev) = Parent_Subp
14435 and then Scope (Parent_Subp) = Scope (Prev)
14436 and then not Is_Hidden (Prev)
14437 then
14438 Visible_Subp := Prev;
14439 return True;
14440 end if;
14441
14442 Prev := Homonym (Prev);
14443 end loop;
14444
14445 return False;
14446 end Is_Private_Overriding;
14447
14448 ------------------
14449 -- Replace_Type --
14450 ------------------
14451
14452 procedure Replace_Type (Id, New_Id : Entity_Id) is
14453 Id_Type : constant Entity_Id := Etype (Id);
14454 Acc_Type : Entity_Id;
14455 Par : constant Node_Id := Parent (Derived_Type);
14456
14457 begin
14458 -- When the type is an anonymous access type, create a new access
14459 -- type designating the derived type. This itype must be elaborated
14460 -- at the point of the derivation, not on subsequent calls that may
14461 -- be out of the proper scope for Gigi, so we insert a reference to
14462 -- it after the derivation.
14463
14464 if Ekind (Id_Type) = E_Anonymous_Access_Type then
14465 declare
14466 Desig_Typ : Entity_Id := Designated_Type (Id_Type);
14467
14468 begin
14469 if Ekind (Desig_Typ) = E_Record_Type_With_Private
14470 and then Present (Full_View (Desig_Typ))
14471 and then not Is_Private_Type (Parent_Type)
14472 then
14473 Desig_Typ := Full_View (Desig_Typ);
14474 end if;
14475
14476 if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
14477
14478 -- Ada 2005 (AI-251): Handle also derivations of abstract
14479 -- interface primitives.
14480
14481 or else (Is_Interface (Desig_Typ)
14482 and then not Is_Class_Wide_Type (Desig_Typ))
14483 then
14484 Acc_Type := New_Copy (Id_Type);
14485 Set_Etype (Acc_Type, Acc_Type);
14486 Set_Scope (Acc_Type, New_Subp);
14487
14488 -- Set size of anonymous access type. If we have an access
14489 -- to an unconstrained array, this is a fat pointer, so it
14490 -- is sizes at twice addtress size.
14491
14492 if Is_Array_Type (Desig_Typ)
14493 and then not Is_Constrained (Desig_Typ)
14494 then
14495 Init_Size (Acc_Type, 2 * System_Address_Size);
14496
14497 -- Other cases use a thin pointer
14498
14499 else
14500 Init_Size (Acc_Type, System_Address_Size);
14501 end if;
14502
14503 -- Set remaining characterstics of anonymous access type
14504
14505 Init_Alignment (Acc_Type);
14506 Set_Directly_Designated_Type (Acc_Type, Derived_Type);
14507
14508 Set_Etype (New_Id, Acc_Type);
14509 Set_Scope (New_Id, New_Subp);
14510
14511 -- Create a reference to it
14512
14513 Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
14514
14515 else
14516 Set_Etype (New_Id, Id_Type);
14517 end if;
14518 end;
14519
14520 -- In Ada2012, a formal may have an incomplete type but the type
14521 -- derivation that inherits the primitive follows the full view.
14522
14523 elsif Base_Type (Id_Type) = Base_Type (Parent_Type)
14524 or else
14525 (Ekind (Id_Type) = E_Record_Type_With_Private
14526 and then Present (Full_View (Id_Type))
14527 and then
14528 Base_Type (Full_View (Id_Type)) = Base_Type (Parent_Type))
14529 or else
14530 (Ada_Version >= Ada_2012
14531 and then Ekind (Id_Type) = E_Incomplete_Type
14532 and then Full_View (Id_Type) = Parent_Type)
14533 then
14534 -- Constraint checks on formals are generated during expansion,
14535 -- based on the signature of the original subprogram. The bounds
14536 -- of the derived type are not relevant, and thus we can use
14537 -- the base type for the formals. However, the return type may be
14538 -- used in a context that requires that the proper static bounds
14539 -- be used (a case statement, for example) and for those cases
14540 -- we must use the derived type (first subtype), not its base.
14541
14542 -- If the derived_type_definition has no constraints, we know that
14543 -- the derived type has the same constraints as the first subtype
14544 -- of the parent, and we can also use it rather than its base,
14545 -- which can lead to more efficient code.
14546
14547 if Etype (Id) = Parent_Type then
14548 if Is_Scalar_Type (Parent_Type)
14549 and then
14550 Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
14551 then
14552 Set_Etype (New_Id, Derived_Type);
14553
14554 elsif Nkind (Par) = N_Full_Type_Declaration
14555 and then
14556 Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
14557 and then
14558 Is_Entity_Name
14559 (Subtype_Indication (Type_Definition (Par)))
14560 then
14561 Set_Etype (New_Id, Derived_Type);
14562
14563 else
14564 Set_Etype (New_Id, Base_Type (Derived_Type));
14565 end if;
14566
14567 else
14568 Set_Etype (New_Id, Base_Type (Derived_Type));
14569 end if;
14570
14571 else
14572 Set_Etype (New_Id, Etype (Id));
14573 end if;
14574 end Replace_Type;
14575
14576 ----------------------
14577 -- Set_Derived_Name --
14578 ----------------------
14579
14580 procedure Set_Derived_Name is
14581 Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
14582 begin
14583 if Nm = TSS_Null then
14584 Set_Chars (New_Subp, Chars (Parent_Subp));
14585 else
14586 Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
14587 end if;
14588 end Set_Derived_Name;
14589
14590 -- Start of processing for Derive_Subprogram
14591
14592 begin
14593 New_Subp := New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
14594 Set_Ekind (New_Subp, Ekind (Parent_Subp));
14595
14596 -- Check whether the inherited subprogram is a private operation that
14597 -- should be inherited but not yet made visible. Such subprograms can
14598 -- become visible at a later point (e.g., the private part of a public
14599 -- child unit) via Declare_Inherited_Private_Subprograms. If the
14600 -- following predicate is true, then this is not such a private
14601 -- operation and the subprogram simply inherits the name of the parent
14602 -- subprogram. Note the special check for the names of controlled
14603 -- operations, which are currently exempted from being inherited with
14604 -- a hidden name because they must be findable for generation of
14605 -- implicit run-time calls.
14606
14607 if not Is_Hidden (Parent_Subp)
14608 or else Is_Internal (Parent_Subp)
14609 or else Is_Private_Overriding
14610 or else Is_Internal_Name (Chars (Parent_Subp))
14611 or else Nam_In (Chars (Parent_Subp), Name_Initialize,
14612 Name_Adjust,
14613 Name_Finalize)
14614 then
14615 Set_Derived_Name;
14616
14617 -- An inherited dispatching equality will be overridden by an internally
14618 -- generated one, or by an explicit one, so preserve its name and thus
14619 -- its entry in the dispatch table. Otherwise, if Parent_Subp is a
14620 -- private operation it may become invisible if the full view has
14621 -- progenitors, and the dispatch table will be malformed.
14622 -- We check that the type is limited to handle the anomalous declaration
14623 -- of Limited_Controlled, which is derived from a non-limited type, and
14624 -- which is handled specially elsewhere as well.
14625
14626 elsif Chars (Parent_Subp) = Name_Op_Eq
14627 and then Is_Dispatching_Operation (Parent_Subp)
14628 and then Etype (Parent_Subp) = Standard_Boolean
14629 and then not Is_Limited_Type (Etype (First_Formal (Parent_Subp)))
14630 and then
14631 Etype (First_Formal (Parent_Subp)) =
14632 Etype (Next_Formal (First_Formal (Parent_Subp)))
14633 then
14634 Set_Derived_Name;
14635
14636 -- If parent is hidden, this can be a regular derivation if the
14637 -- parent is immediately visible in a non-instantiating context,
14638 -- or if we are in the private part of an instance. This test
14639 -- should still be refined ???
14640
14641 -- The test for In_Instance_Not_Visible avoids inheriting the derived
14642 -- operation as a non-visible operation in cases where the parent
14643 -- subprogram might not be visible now, but was visible within the
14644 -- original generic, so it would be wrong to make the inherited
14645 -- subprogram non-visible now. (Not clear if this test is fully
14646 -- correct; are there any cases where we should declare the inherited
14647 -- operation as not visible to avoid it being overridden, e.g., when
14648 -- the parent type is a generic actual with private primitives ???)
14649
14650 -- (they should be treated the same as other private inherited
14651 -- subprograms, but it's not clear how to do this cleanly). ???
14652
14653 elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
14654 and then Is_Immediately_Visible (Parent_Subp)
14655 and then not In_Instance)
14656 or else In_Instance_Not_Visible
14657 then
14658 Set_Derived_Name;
14659
14660 -- Ada 2005 (AI-251): Regular derivation if the parent subprogram
14661 -- overrides an interface primitive because interface primitives
14662 -- must be visible in the partial view of the parent (RM 7.3 (7.3/2))
14663
14664 elsif Ada_Version >= Ada_2005
14665 and then Is_Dispatching_Operation (Parent_Subp)
14666 and then Covers_Some_Interface (Parent_Subp)
14667 then
14668 Set_Derived_Name;
14669
14670 -- Otherwise, the type is inheriting a private operation, so enter
14671 -- it with a special name so it can't be overridden.
14672
14673 else
14674 Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
14675 end if;
14676
14677 Set_Parent (New_Subp, Parent (Derived_Type));
14678
14679 if Present (Actual_Subp) then
14680 Replace_Type (Actual_Subp, New_Subp);
14681 else
14682 Replace_Type (Parent_Subp, New_Subp);
14683 end if;
14684
14685 Conditional_Delay (New_Subp, Parent_Subp);
14686
14687 -- If we are creating a renaming for a primitive operation of an
14688 -- actual of a generic derived type, we must examine the signature
14689 -- of the actual primitive, not that of the generic formal, which for
14690 -- example may be an interface. However the name and initial value
14691 -- of the inherited operation are those of the formal primitive.
14692
14693 Formal := First_Formal (Parent_Subp);
14694
14695 if Present (Actual_Subp) then
14696 Formal_Of_Actual := First_Formal (Actual_Subp);
14697 else
14698 Formal_Of_Actual := Empty;
14699 end if;
14700
14701 while Present (Formal) loop
14702 New_Formal := New_Copy (Formal);
14703
14704 -- Normally we do not go copying parents, but in the case of
14705 -- formals, we need to link up to the declaration (which is the
14706 -- parameter specification), and it is fine to link up to the
14707 -- original formal's parameter specification in this case.
14708
14709 Set_Parent (New_Formal, Parent (Formal));
14710 Append_Entity (New_Formal, New_Subp);
14711
14712 if Present (Formal_Of_Actual) then
14713 Replace_Type (Formal_Of_Actual, New_Formal);
14714 Next_Formal (Formal_Of_Actual);
14715 else
14716 Replace_Type (Formal, New_Formal);
14717 end if;
14718
14719 Next_Formal (Formal);
14720 end loop;
14721
14722 -- If this derivation corresponds to a tagged generic actual, then
14723 -- primitive operations rename those of the actual. Otherwise the
14724 -- primitive operations rename those of the parent type, If the parent
14725 -- renames an intrinsic operator, so does the new subprogram. We except
14726 -- concatenation, which is always properly typed, and does not get
14727 -- expanded as other intrinsic operations.
14728
14729 if No (Actual_Subp) then
14730 if Is_Intrinsic_Subprogram (Parent_Subp) then
14731 Set_Is_Intrinsic_Subprogram (New_Subp);
14732
14733 if Present (Alias (Parent_Subp))
14734 and then Chars (Parent_Subp) /= Name_Op_Concat
14735 then
14736 Set_Alias (New_Subp, Alias (Parent_Subp));
14737 else
14738 Set_Alias (New_Subp, Parent_Subp);
14739 end if;
14740
14741 else
14742 Set_Alias (New_Subp, Parent_Subp);
14743 end if;
14744
14745 else
14746 Set_Alias (New_Subp, Actual_Subp);
14747 end if;
14748
14749 -- Inherit the "ghostness" from the parent subprogram
14750
14751 if Is_Ghost_Entity (Alias (New_Subp)) then
14752 Set_Is_Ghost_Entity (New_Subp);
14753 end if;
14754
14755 -- Derived subprograms of a tagged type must inherit the convention
14756 -- of the parent subprogram (a requirement of AI-117). Derived
14757 -- subprograms of untagged types simply get convention Ada by default.
14758
14759 -- If the derived type is a tagged generic formal type with unknown
14760 -- discriminants, its convention is intrinsic (RM 6.3.1 (8)).
14761
14762 -- However, if the type is derived from a generic formal, the further
14763 -- inherited subprogram has the convention of the non-generic ancestor.
14764 -- Otherwise there would be no way to override the operation.
14765 -- (This is subject to forthcoming ARG discussions).
14766
14767 if Is_Tagged_Type (Derived_Type) then
14768 if Is_Generic_Type (Derived_Type)
14769 and then Has_Unknown_Discriminants (Derived_Type)
14770 then
14771 Set_Convention (New_Subp, Convention_Intrinsic);
14772
14773 else
14774 if Is_Generic_Type (Parent_Type)
14775 and then Has_Unknown_Discriminants (Parent_Type)
14776 then
14777 Set_Convention (New_Subp, Convention (Alias (Parent_Subp)));
14778 else
14779 Set_Convention (New_Subp, Convention (Parent_Subp));
14780 end if;
14781 end if;
14782 end if;
14783
14784 -- Predefined controlled operations retain their name even if the parent
14785 -- is hidden (see above), but they are not primitive operations if the
14786 -- ancestor is not visible, for example if the parent is a private
14787 -- extension completed with a controlled extension. Note that a full
14788 -- type that is controlled can break privacy: the flag Is_Controlled is
14789 -- set on both views of the type.
14790
14791 if Is_Controlled (Parent_Type)
14792 and then Nam_In (Chars (Parent_Subp), Name_Initialize,
14793 Name_Adjust,
14794 Name_Finalize)
14795 and then Is_Hidden (Parent_Subp)
14796 and then not Is_Visibly_Controlled (Parent_Type)
14797 then
14798 Set_Is_Hidden (New_Subp);
14799 end if;
14800
14801 Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
14802 Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
14803
14804 if Ekind (Parent_Subp) = E_Procedure then
14805 Set_Is_Valued_Procedure
14806 (New_Subp, Is_Valued_Procedure (Parent_Subp));
14807 else
14808 Set_Has_Controlling_Result
14809 (New_Subp, Has_Controlling_Result (Parent_Subp));
14810 end if;
14811
14812 -- No_Return must be inherited properly. If this is overridden in the
14813 -- case of a dispatching operation, then a check is made in Sem_Disp
14814 -- that the overriding operation is also No_Return (no such check is
14815 -- required for the case of non-dispatching operation.
14816
14817 Set_No_Return (New_Subp, No_Return (Parent_Subp));
14818
14819 -- A derived function with a controlling result is abstract. If the
14820 -- Derived_Type is a nonabstract formal generic derived type, then
14821 -- inherited operations are not abstract: the required check is done at
14822 -- instantiation time. If the derivation is for a generic actual, the
14823 -- function is not abstract unless the actual is.
14824
14825 if Is_Generic_Type (Derived_Type)
14826 and then not Is_Abstract_Type (Derived_Type)
14827 then
14828 null;
14829
14830 -- Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
14831 -- properties of the subprogram, as defined in RM-3.9.3(4/2-6/2).
14832
14833 -- A subprogram subject to pragma Extensions_Visible with value False
14834 -- requires overriding if the subprogram has at least one controlling
14835 -- OUT parameter (SPARK RM 6.1.7(6)).
14836
14837 elsif Ada_Version >= Ada_2005
14838 and then (Is_Abstract_Subprogram (Alias (New_Subp))
14839 or else (Is_Tagged_Type (Derived_Type)
14840 and then Etype (New_Subp) = Derived_Type
14841 and then not Is_Null_Extension (Derived_Type))
14842 or else (Is_Tagged_Type (Derived_Type)
14843 and then Ekind (Etype (New_Subp)) =
14844 E_Anonymous_Access_Type
14845 and then Designated_Type (Etype (New_Subp)) =
14846 Derived_Type
14847 and then not Is_Null_Extension (Derived_Type))
14848 or else (Comes_From_Source (Alias (New_Subp))
14849 and then Is_EVF_Procedure (Alias (New_Subp))))
14850 and then No (Actual_Subp)
14851 then
14852 if not Is_Tagged_Type (Derived_Type)
14853 or else Is_Abstract_Type (Derived_Type)
14854 or else Is_Abstract_Subprogram (Alias (New_Subp))
14855 then
14856 Set_Is_Abstract_Subprogram (New_Subp);
14857 else
14858 Set_Requires_Overriding (New_Subp);
14859 end if;
14860
14861 elsif Ada_Version < Ada_2005
14862 and then (Is_Abstract_Subprogram (Alias (New_Subp))
14863 or else (Is_Tagged_Type (Derived_Type)
14864 and then Etype (New_Subp) = Derived_Type
14865 and then No (Actual_Subp)))
14866 then
14867 Set_Is_Abstract_Subprogram (New_Subp);
14868
14869 -- AI05-0097 : an inherited operation that dispatches on result is
14870 -- abstract if the derived type is abstract, even if the parent type
14871 -- is concrete and the derived type is a null extension.
14872
14873 elsif Has_Controlling_Result (Alias (New_Subp))
14874 and then Is_Abstract_Type (Etype (New_Subp))
14875 then
14876 Set_Is_Abstract_Subprogram (New_Subp);
14877
14878 -- Finally, if the parent type is abstract we must verify that all
14879 -- inherited operations are either non-abstract or overridden, or that
14880 -- the derived type itself is abstract (this check is performed at the
14881 -- end of a package declaration, in Check_Abstract_Overriding). A
14882 -- private overriding in the parent type will not be visible in the
14883 -- derivation if we are not in an inner package or in a child unit of
14884 -- the parent type, in which case the abstractness of the inherited
14885 -- operation is carried to the new subprogram.
14886
14887 elsif Is_Abstract_Type (Parent_Type)
14888 and then not In_Open_Scopes (Scope (Parent_Type))
14889 and then Is_Private_Overriding
14890 and then Is_Abstract_Subprogram (Visible_Subp)
14891 then
14892 if No (Actual_Subp) then
14893 Set_Alias (New_Subp, Visible_Subp);
14894 Set_Is_Abstract_Subprogram (New_Subp, True);
14895
14896 else
14897 -- If this is a derivation for an instance of a formal derived
14898 -- type, abstractness comes from the primitive operation of the
14899 -- actual, not from the operation inherited from the ancestor.
14900
14901 Set_Is_Abstract_Subprogram
14902 (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
14903 end if;
14904 end if;
14905
14906 New_Overloaded_Entity (New_Subp, Derived_Type);
14907
14908 -- Check for case of a derived subprogram for the instantiation of a
14909 -- formal derived tagged type, if so mark the subprogram as dispatching
14910 -- and inherit the dispatching attributes of the actual subprogram. The
14911 -- derived subprogram is effectively renaming of the actual subprogram,
14912 -- so it needs to have the same attributes as the actual.
14913
14914 if Present (Actual_Subp)
14915 and then Is_Dispatching_Operation (Actual_Subp)
14916 then
14917 Set_Is_Dispatching_Operation (New_Subp);
14918
14919 if Present (DTC_Entity (Actual_Subp)) then
14920 Set_DTC_Entity (New_Subp, DTC_Entity (Actual_Subp));
14921 Set_DT_Position_Value (New_Subp, DT_Position (Actual_Subp));
14922 end if;
14923 end if;
14924
14925 -- Indicate that a derived subprogram does not require a body and that
14926 -- it does not require processing of default expressions.
14927
14928 Set_Has_Completion (New_Subp);
14929 Set_Default_Expressions_Processed (New_Subp);
14930
14931 if Ekind (New_Subp) = E_Function then
14932 Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
14933 end if;
14934 end Derive_Subprogram;
14935
14936 ------------------------
14937 -- Derive_Subprograms --
14938 ------------------------
14939
14940 procedure Derive_Subprograms
14941 (Parent_Type : Entity_Id;
14942 Derived_Type : Entity_Id;
14943 Generic_Actual : Entity_Id := Empty)
14944 is
14945 Op_List : constant Elist_Id :=
14946 Collect_Primitive_Operations (Parent_Type);
14947
14948 function Check_Derived_Type return Boolean;
14949 -- Check that all the entities derived from Parent_Type are found in
14950 -- the list of primitives of Derived_Type exactly in the same order.
14951
14952 procedure Derive_Interface_Subprogram
14953 (New_Subp : in out Entity_Id;
14954 Subp : Entity_Id;
14955 Actual_Subp : Entity_Id);
14956 -- Derive New_Subp from the ultimate alias of the parent subprogram Subp
14957 -- (which is an interface primitive). If Generic_Actual is present then
14958 -- Actual_Subp is the actual subprogram corresponding with the generic
14959 -- subprogram Subp.
14960
14961 function Check_Derived_Type return Boolean is
14962 E : Entity_Id;
14963 Elmt : Elmt_Id;
14964 List : Elist_Id;
14965 New_Subp : Entity_Id;
14966 Op_Elmt : Elmt_Id;
14967 Subp : Entity_Id;
14968
14969 begin
14970 -- Traverse list of entities in the current scope searching for
14971 -- an incomplete type whose full-view is derived type
14972
14973 E := First_Entity (Scope (Derived_Type));
14974 while Present (E) and then E /= Derived_Type loop
14975 if Ekind (E) = E_Incomplete_Type
14976 and then Present (Full_View (E))
14977 and then Full_View (E) = Derived_Type
14978 then
14979 -- Disable this test if Derived_Type completes an incomplete
14980 -- type because in such case more primitives can be added
14981 -- later to the list of primitives of Derived_Type by routine
14982 -- Process_Incomplete_Dependents
14983
14984 return True;
14985 end if;
14986
14987 E := Next_Entity (E);
14988 end loop;
14989
14990 List := Collect_Primitive_Operations (Derived_Type);
14991 Elmt := First_Elmt (List);
14992
14993 Op_Elmt := First_Elmt (Op_List);
14994 while Present (Op_Elmt) loop
14995 Subp := Node (Op_Elmt);
14996 New_Subp := Node (Elmt);
14997
14998 -- At this early stage Derived_Type has no entities with attribute
14999 -- Interface_Alias. In addition, such primitives are always
15000 -- located at the end of the list of primitives of Parent_Type.
15001 -- Therefore, if found we can safely stop processing pending
15002 -- entities.
15003
15004 exit when Present (Interface_Alias (Subp));
15005
15006 -- Handle hidden entities
15007
15008 if not Is_Predefined_Dispatching_Operation (Subp)
15009 and then Is_Hidden (Subp)
15010 then
15011 if Present (New_Subp)
15012 and then Primitive_Names_Match (Subp, New_Subp)
15013 then
15014 Next_Elmt (Elmt);
15015 end if;
15016
15017 else
15018 if not Present (New_Subp)
15019 or else Ekind (Subp) /= Ekind (New_Subp)
15020 or else not Primitive_Names_Match (Subp, New_Subp)
15021 then
15022 return False;
15023 end if;
15024
15025 Next_Elmt (Elmt);
15026 end if;
15027
15028 Next_Elmt (Op_Elmt);
15029 end loop;
15030
15031 return True;
15032 end Check_Derived_Type;
15033
15034 ---------------------------------
15035 -- Derive_Interface_Subprogram --
15036 ---------------------------------
15037
15038 procedure Derive_Interface_Subprogram
15039 (New_Subp : in out Entity_Id;
15040 Subp : Entity_Id;
15041 Actual_Subp : Entity_Id)
15042 is
15043 Iface_Subp : constant Entity_Id := Ultimate_Alias (Subp);
15044 Iface_Type : constant Entity_Id := Find_Dispatching_Type (Iface_Subp);
15045
15046 begin
15047 pragma Assert (Is_Interface (Iface_Type));
15048
15049 Derive_Subprogram
15050 (New_Subp => New_Subp,
15051 Parent_Subp => Iface_Subp,
15052 Derived_Type => Derived_Type,
15053 Parent_Type => Iface_Type,
15054 Actual_Subp => Actual_Subp);
15055
15056 -- Given that this new interface entity corresponds with a primitive
15057 -- of the parent that was not overridden we must leave it associated
15058 -- with its parent primitive to ensure that it will share the same
15059 -- dispatch table slot when overridden.
15060
15061 if No (Actual_Subp) then
15062 Set_Alias (New_Subp, Subp);
15063
15064 -- For instantiations this is not needed since the previous call to
15065 -- Derive_Subprogram leaves the entity well decorated.
15066
15067 else
15068 pragma Assert (Alias (New_Subp) = Actual_Subp);
15069 null;
15070 end if;
15071 end Derive_Interface_Subprogram;
15072
15073 -- Local variables
15074
15075 Alias_Subp : Entity_Id;
15076 Act_List : Elist_Id;
15077 Act_Elmt : Elmt_Id;
15078 Act_Subp : Entity_Id := Empty;
15079 Elmt : Elmt_Id;
15080 Need_Search : Boolean := False;
15081 New_Subp : Entity_Id := Empty;
15082 Parent_Base : Entity_Id;
15083 Subp : Entity_Id;
15084
15085 -- Start of processing for Derive_Subprograms
15086
15087 begin
15088 if Ekind (Parent_Type) = E_Record_Type_With_Private
15089 and then Has_Discriminants (Parent_Type)
15090 and then Present (Full_View (Parent_Type))
15091 then
15092 Parent_Base := Full_View (Parent_Type);
15093 else
15094 Parent_Base := Parent_Type;
15095 end if;
15096
15097 if Present (Generic_Actual) then
15098 Act_List := Collect_Primitive_Operations (Generic_Actual);
15099 Act_Elmt := First_Elmt (Act_List);
15100 else
15101 Act_List := No_Elist;
15102 Act_Elmt := No_Elmt;
15103 end if;
15104
15105 -- Derive primitives inherited from the parent. Note that if the generic
15106 -- actual is present, this is not really a type derivation, it is a
15107 -- completion within an instance.
15108
15109 -- Case 1: Derived_Type does not implement interfaces
15110
15111 if not Is_Tagged_Type (Derived_Type)
15112 or else (not Has_Interfaces (Derived_Type)
15113 and then not (Present (Generic_Actual)
15114 and then Has_Interfaces (Generic_Actual)))
15115 then
15116 Elmt := First_Elmt (Op_List);
15117 while Present (Elmt) loop
15118 Subp := Node (Elmt);
15119
15120 -- Literals are derived earlier in the process of building the
15121 -- derived type, and are skipped here.
15122
15123 if Ekind (Subp) = E_Enumeration_Literal then
15124 null;
15125
15126 -- The actual is a direct descendant and the common primitive
15127 -- operations appear in the same order.
15128
15129 -- If the generic parent type is present, the derived type is an
15130 -- instance of a formal derived type, and within the instance its
15131 -- operations are those of the actual. We derive from the formal
15132 -- type but make the inherited operations aliases of the
15133 -- corresponding operations of the actual.
15134
15135 else
15136 pragma Assert (No (Node (Act_Elmt))
15137 or else (Primitive_Names_Match (Subp, Node (Act_Elmt))
15138 and then
15139 Type_Conformant
15140 (Subp, Node (Act_Elmt),
15141 Skip_Controlling_Formals => True)));
15142
15143 Derive_Subprogram
15144 (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
15145
15146 if Present (Act_Elmt) then
15147 Next_Elmt (Act_Elmt);
15148 end if;
15149 end if;
15150
15151 Next_Elmt (Elmt);
15152 end loop;
15153
15154 -- Case 2: Derived_Type implements interfaces
15155
15156 else
15157 -- If the parent type has no predefined primitives we remove
15158 -- predefined primitives from the list of primitives of generic
15159 -- actual to simplify the complexity of this algorithm.
15160
15161 if Present (Generic_Actual) then
15162 declare
15163 Has_Predefined_Primitives : Boolean := False;
15164
15165 begin
15166 -- Check if the parent type has predefined primitives
15167
15168 Elmt := First_Elmt (Op_List);
15169 while Present (Elmt) loop
15170 Subp := Node (Elmt);
15171
15172 if Is_Predefined_Dispatching_Operation (Subp)
15173 and then not Comes_From_Source (Ultimate_Alias (Subp))
15174 then
15175 Has_Predefined_Primitives := True;
15176 exit;
15177 end if;
15178
15179 Next_Elmt (Elmt);
15180 end loop;
15181
15182 -- Remove predefined primitives of Generic_Actual. We must use
15183 -- an auxiliary list because in case of tagged types the value
15184 -- returned by Collect_Primitive_Operations is the value stored
15185 -- in its Primitive_Operations attribute (and we don't want to
15186 -- modify its current contents).
15187
15188 if not Has_Predefined_Primitives then
15189 declare
15190 Aux_List : constant Elist_Id := New_Elmt_List;
15191
15192 begin
15193 Elmt := First_Elmt (Act_List);
15194 while Present (Elmt) loop
15195 Subp := Node (Elmt);
15196
15197 if not Is_Predefined_Dispatching_Operation (Subp)
15198 or else Comes_From_Source (Subp)
15199 then
15200 Append_Elmt (Subp, Aux_List);
15201 end if;
15202
15203 Next_Elmt (Elmt);
15204 end loop;
15205
15206 Act_List := Aux_List;
15207 end;
15208 end if;
15209
15210 Act_Elmt := First_Elmt (Act_List);
15211 Act_Subp := Node (Act_Elmt);
15212 end;
15213 end if;
15214
15215 -- Stage 1: If the generic actual is not present we derive the
15216 -- primitives inherited from the parent type. If the generic parent
15217 -- type is present, the derived type is an instance of a formal
15218 -- derived type, and within the instance its operations are those of
15219 -- the actual. We derive from the formal type but make the inherited
15220 -- operations aliases of the corresponding operations of the actual.
15221
15222 Elmt := First_Elmt (Op_List);
15223 while Present (Elmt) loop
15224 Subp := Node (Elmt);
15225 Alias_Subp := Ultimate_Alias (Subp);
15226
15227 -- Do not derive internal entities of the parent that link
15228 -- interface primitives with their covering primitive. These
15229 -- entities will be added to this type when frozen.
15230
15231 if Present (Interface_Alias (Subp)) then
15232 goto Continue;
15233 end if;
15234
15235 -- If the generic actual is present find the corresponding
15236 -- operation in the generic actual. If the parent type is a
15237 -- direct ancestor of the derived type then, even if it is an
15238 -- interface, the operations are inherited from the primary
15239 -- dispatch table and are in the proper order. If we detect here
15240 -- that primitives are not in the same order we traverse the list
15241 -- of primitive operations of the actual to find the one that
15242 -- implements the interface primitive.
15243
15244 if Need_Search
15245 or else
15246 (Present (Generic_Actual)
15247 and then Present (Act_Subp)
15248 and then not
15249 (Primitive_Names_Match (Subp, Act_Subp)
15250 and then
15251 Type_Conformant (Subp, Act_Subp,
15252 Skip_Controlling_Formals => True)))
15253 then
15254 pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual,
15255 Use_Full_View => True));
15256
15257 -- Remember that we need searching for all pending primitives
15258
15259 Need_Search := True;
15260
15261 -- Handle entities associated with interface primitives
15262
15263 if Present (Alias_Subp)
15264 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
15265 and then not Is_Predefined_Dispatching_Operation (Subp)
15266 then
15267 -- Search for the primitive in the homonym chain
15268
15269 Act_Subp :=
15270 Find_Primitive_Covering_Interface
15271 (Tagged_Type => Generic_Actual,
15272 Iface_Prim => Alias_Subp);
15273
15274 -- Previous search may not locate primitives covering
15275 -- interfaces defined in generics units or instantiations.
15276 -- (it fails if the covering primitive has formals whose
15277 -- type is also defined in generics or instantiations).
15278 -- In such case we search in the list of primitives of the
15279 -- generic actual for the internal entity that links the
15280 -- interface primitive and the covering primitive.
15281
15282 if No (Act_Subp)
15283 and then Is_Generic_Type (Parent_Type)
15284 then
15285 -- This code has been designed to handle only generic
15286 -- formals that implement interfaces that are defined
15287 -- in a generic unit or instantiation. If this code is
15288 -- needed for other cases we must review it because
15289 -- (given that it relies on Original_Location to locate
15290 -- the primitive of Generic_Actual that covers the
15291 -- interface) it could leave linked through attribute
15292 -- Alias entities of unrelated instantiations).
15293
15294 pragma Assert
15295 (Is_Generic_Unit
15296 (Scope (Find_Dispatching_Type (Alias_Subp)))
15297 or else
15298 Instantiation_Depth
15299 (Sloc (Find_Dispatching_Type (Alias_Subp))) > 0);
15300
15301 declare
15302 Iface_Prim_Loc : constant Source_Ptr :=
15303 Original_Location (Sloc (Alias_Subp));
15304
15305 Elmt : Elmt_Id;
15306 Prim : Entity_Id;
15307
15308 begin
15309 Elmt :=
15310 First_Elmt (Primitive_Operations (Generic_Actual));
15311
15312 Search : while Present (Elmt) loop
15313 Prim := Node (Elmt);
15314
15315 if Present (Interface_Alias (Prim))
15316 and then Original_Location
15317 (Sloc (Interface_Alias (Prim))) =
15318 Iface_Prim_Loc
15319 then
15320 Act_Subp := Alias (Prim);
15321 exit Search;
15322 end if;
15323
15324 Next_Elmt (Elmt);
15325 end loop Search;
15326 end;
15327 end if;
15328
15329 pragma Assert (Present (Act_Subp)
15330 or else Is_Abstract_Type (Generic_Actual)
15331 or else Serious_Errors_Detected > 0);
15332
15333 -- Handle predefined primitives plus the rest of user-defined
15334 -- primitives
15335
15336 else
15337 Act_Elmt := First_Elmt (Act_List);
15338 while Present (Act_Elmt) loop
15339 Act_Subp := Node (Act_Elmt);
15340
15341 exit when Primitive_Names_Match (Subp, Act_Subp)
15342 and then Type_Conformant
15343 (Subp, Act_Subp,
15344 Skip_Controlling_Formals => True)
15345 and then No (Interface_Alias (Act_Subp));
15346
15347 Next_Elmt (Act_Elmt);
15348 end loop;
15349
15350 if No (Act_Elmt) then
15351 Act_Subp := Empty;
15352 end if;
15353 end if;
15354 end if;
15355
15356 -- Case 1: If the parent is a limited interface then it has the
15357 -- predefined primitives of synchronized interfaces. However, the
15358 -- actual type may be a non-limited type and hence it does not
15359 -- have such primitives.
15360
15361 if Present (Generic_Actual)
15362 and then not Present (Act_Subp)
15363 and then Is_Limited_Interface (Parent_Base)
15364 and then Is_Predefined_Interface_Primitive (Subp)
15365 then
15366 null;
15367
15368 -- Case 2: Inherit entities associated with interfaces that were
15369 -- not covered by the parent type. We exclude here null interface
15370 -- primitives because they do not need special management.
15371
15372 -- We also exclude interface operations that are renamings. If the
15373 -- subprogram is an explicit renaming of an interface primitive,
15374 -- it is a regular primitive operation, and the presence of its
15375 -- alias is not relevant: it has to be derived like any other
15376 -- primitive.
15377
15378 elsif Present (Alias (Subp))
15379 and then Nkind (Unit_Declaration_Node (Subp)) /=
15380 N_Subprogram_Renaming_Declaration
15381 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
15382 and then not
15383 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
15384 and then Null_Present (Parent (Alias_Subp)))
15385 then
15386 -- If this is an abstract private type then we transfer the
15387 -- derivation of the interface primitive from the partial view
15388 -- to the full view. This is safe because all the interfaces
15389 -- must be visible in the partial view. Done to avoid adding
15390 -- a new interface derivation to the private part of the
15391 -- enclosing package; otherwise this new derivation would be
15392 -- decorated as hidden when the analysis of the enclosing
15393 -- package completes.
15394
15395 if Is_Abstract_Type (Derived_Type)
15396 and then In_Private_Part (Current_Scope)
15397 and then Has_Private_Declaration (Derived_Type)
15398 then
15399 declare
15400 Partial_View : Entity_Id;
15401 Elmt : Elmt_Id;
15402 Ent : Entity_Id;
15403
15404 begin
15405 Partial_View := First_Entity (Current_Scope);
15406 loop
15407 exit when No (Partial_View)
15408 or else (Has_Private_Declaration (Partial_View)
15409 and then
15410 Full_View (Partial_View) = Derived_Type);
15411
15412 Next_Entity (Partial_View);
15413 end loop;
15414
15415 -- If the partial view was not found then the source code
15416 -- has errors and the derivation is not needed.
15417
15418 if Present (Partial_View) then
15419 Elmt :=
15420 First_Elmt (Primitive_Operations (Partial_View));
15421 while Present (Elmt) loop
15422 Ent := Node (Elmt);
15423
15424 if Present (Alias (Ent))
15425 and then Ultimate_Alias (Ent) = Alias (Subp)
15426 then
15427 Append_Elmt
15428 (Ent, Primitive_Operations (Derived_Type));
15429 exit;
15430 end if;
15431
15432 Next_Elmt (Elmt);
15433 end loop;
15434
15435 -- If the interface primitive was not found in the
15436 -- partial view then this interface primitive was
15437 -- overridden. We add a derivation to activate in
15438 -- Derive_Progenitor_Subprograms the machinery to
15439 -- search for it.
15440
15441 if No (Elmt) then
15442 Derive_Interface_Subprogram
15443 (New_Subp => New_Subp,
15444 Subp => Subp,
15445 Actual_Subp => Act_Subp);
15446 end if;
15447 end if;
15448 end;
15449 else
15450 Derive_Interface_Subprogram
15451 (New_Subp => New_Subp,
15452 Subp => Subp,
15453 Actual_Subp => Act_Subp);
15454 end if;
15455
15456 -- Case 3: Common derivation
15457
15458 else
15459 Derive_Subprogram
15460 (New_Subp => New_Subp,
15461 Parent_Subp => Subp,
15462 Derived_Type => Derived_Type,
15463 Parent_Type => Parent_Base,
15464 Actual_Subp => Act_Subp);
15465 end if;
15466
15467 -- No need to update Act_Elm if we must search for the
15468 -- corresponding operation in the generic actual
15469
15470 if not Need_Search
15471 and then Present (Act_Elmt)
15472 then
15473 Next_Elmt (Act_Elmt);
15474 Act_Subp := Node (Act_Elmt);
15475 end if;
15476
15477 <<Continue>>
15478 Next_Elmt (Elmt);
15479 end loop;
15480
15481 -- Inherit additional operations from progenitors. If the derived
15482 -- type is a generic actual, there are not new primitive operations
15483 -- for the type because it has those of the actual, and therefore
15484 -- nothing needs to be done. The renamings generated above are not
15485 -- primitive operations, and their purpose is simply to make the
15486 -- proper operations visible within an instantiation.
15487
15488 if No (Generic_Actual) then
15489 Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
15490 end if;
15491 end if;
15492
15493 -- Final check: Direct descendants must have their primitives in the
15494 -- same order. We exclude from this test untagged types and instances
15495 -- of formal derived types. We skip this test if we have already
15496 -- reported serious errors in the sources.
15497
15498 pragma Assert (not Is_Tagged_Type (Derived_Type)
15499 or else Present (Generic_Actual)
15500 or else Serious_Errors_Detected > 0
15501 or else Check_Derived_Type);
15502 end Derive_Subprograms;
15503
15504 --------------------------------
15505 -- Derived_Standard_Character --
15506 --------------------------------
15507
15508 procedure Derived_Standard_Character
15509 (N : Node_Id;
15510 Parent_Type : Entity_Id;
15511 Derived_Type : Entity_Id)
15512 is
15513 Loc : constant Source_Ptr := Sloc (N);
15514 Def : constant Node_Id := Type_Definition (N);
15515 Indic : constant Node_Id := Subtype_Indication (Def);
15516 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
15517 Implicit_Base : constant Entity_Id :=
15518 Create_Itype
15519 (E_Enumeration_Type, N, Derived_Type, 'B');
15520
15521 Lo : Node_Id;
15522 Hi : Node_Id;
15523
15524 begin
15525 Discard_Node (Process_Subtype (Indic, N));
15526
15527 Set_Etype (Implicit_Base, Parent_Base);
15528 Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
15529 Set_RM_Size (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
15530
15531 Set_Is_Character_Type (Implicit_Base, True);
15532 Set_Has_Delayed_Freeze (Implicit_Base);
15533
15534 -- The bounds of the implicit base are the bounds of the parent base.
15535 -- Note that their type is the parent base.
15536
15537 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
15538 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
15539
15540 Set_Scalar_Range (Implicit_Base,
15541 Make_Range (Loc,
15542 Low_Bound => Lo,
15543 High_Bound => Hi));
15544
15545 Conditional_Delay (Derived_Type, Parent_Type);
15546
15547 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
15548 Set_Etype (Derived_Type, Implicit_Base);
15549 Set_Size_Info (Derived_Type, Parent_Type);
15550
15551 if Unknown_RM_Size (Derived_Type) then
15552 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
15553 end if;
15554
15555 Set_Is_Character_Type (Derived_Type, True);
15556
15557 if Nkind (Indic) /= N_Subtype_Indication then
15558
15559 -- If no explicit constraint, the bounds are those
15560 -- of the parent type.
15561
15562 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Type));
15563 Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
15564 Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
15565 end if;
15566
15567 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
15568
15569 -- Because the implicit base is used in the conversion of the bounds, we
15570 -- have to freeze it now. This is similar to what is done for numeric
15571 -- types, and it equally suspicious, but otherwise a non-static bound
15572 -- will have a reference to an unfrozen type, which is rejected by Gigi
15573 -- (???). This requires specific care for definition of stream
15574 -- attributes. For details, see comments at the end of
15575 -- Build_Derived_Numeric_Type.
15576
15577 Freeze_Before (N, Implicit_Base);
15578 end Derived_Standard_Character;
15579
15580 ------------------------------
15581 -- Derived_Type_Declaration --
15582 ------------------------------
15583
15584 procedure Derived_Type_Declaration
15585 (T : Entity_Id;
15586 N : Node_Id;
15587 Is_Completion : Boolean)
15588 is
15589 Parent_Type : Entity_Id;
15590
15591 function Comes_From_Generic (Typ : Entity_Id) return Boolean;
15592 -- Check whether the parent type is a generic formal, or derives
15593 -- directly or indirectly from one.
15594
15595 ------------------------
15596 -- Comes_From_Generic --
15597 ------------------------
15598
15599 function Comes_From_Generic (Typ : Entity_Id) return Boolean is
15600 begin
15601 if Is_Generic_Type (Typ) then
15602 return True;
15603
15604 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
15605 return True;
15606
15607 elsif Is_Private_Type (Typ)
15608 and then Present (Full_View (Typ))
15609 and then Is_Generic_Type (Root_Type (Full_View (Typ)))
15610 then
15611 return True;
15612
15613 elsif Is_Generic_Actual_Type (Typ) then
15614 return True;
15615
15616 else
15617 return False;
15618 end if;
15619 end Comes_From_Generic;
15620
15621 -- Local variables
15622
15623 Def : constant Node_Id := Type_Definition (N);
15624 Iface_Def : Node_Id;
15625 Indic : constant Node_Id := Subtype_Indication (Def);
15626 Extension : constant Node_Id := Record_Extension_Part (Def);
15627 Parent_Node : Node_Id;
15628 Taggd : Boolean;
15629
15630 -- Start of processing for Derived_Type_Declaration
15631
15632 begin
15633 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
15634
15635 -- Ada 2005 (AI-251): In case of interface derivation check that the
15636 -- parent is also an interface.
15637
15638 if Interface_Present (Def) then
15639 Check_SPARK_05_Restriction ("interface is not allowed", Def);
15640
15641 if not Is_Interface (Parent_Type) then
15642 Diagnose_Interface (Indic, Parent_Type);
15643
15644 else
15645 Parent_Node := Parent (Base_Type (Parent_Type));
15646 Iface_Def := Type_Definition (Parent_Node);
15647
15648 -- Ada 2005 (AI-251): Limited interfaces can only inherit from
15649 -- other limited interfaces.
15650
15651 if Limited_Present (Def) then
15652 if Limited_Present (Iface_Def) then
15653 null;
15654
15655 elsif Protected_Present (Iface_Def) then
15656 Error_Msg_NE
15657 ("descendant of& must be declared"
15658 & " as a protected interface",
15659 N, Parent_Type);
15660
15661 elsif Synchronized_Present (Iface_Def) then
15662 Error_Msg_NE
15663 ("descendant of& must be declared"
15664 & " as a synchronized interface",
15665 N, Parent_Type);
15666
15667 elsif Task_Present (Iface_Def) then
15668 Error_Msg_NE
15669 ("descendant of& must be declared as a task interface",
15670 N, Parent_Type);
15671
15672 else
15673 Error_Msg_N
15674 ("(Ada 2005) limited interface cannot "
15675 & "inherit from non-limited interface", Indic);
15676 end if;
15677
15678 -- Ada 2005 (AI-345): Non-limited interfaces can only inherit
15679 -- from non-limited or limited interfaces.
15680
15681 elsif not Protected_Present (Def)
15682 and then not Synchronized_Present (Def)
15683 and then not Task_Present (Def)
15684 then
15685 if Limited_Present (Iface_Def) then
15686 null;
15687
15688 elsif Protected_Present (Iface_Def) then
15689 Error_Msg_NE
15690 ("descendant of& must be declared"
15691 & " as a protected interface",
15692 N, Parent_Type);
15693
15694 elsif Synchronized_Present (Iface_Def) then
15695 Error_Msg_NE
15696 ("descendant of& must be declared"
15697 & " as a synchronized interface",
15698 N, Parent_Type);
15699
15700 elsif Task_Present (Iface_Def) then
15701 Error_Msg_NE
15702 ("descendant of& must be declared as a task interface",
15703 N, Parent_Type);
15704 else
15705 null;
15706 end if;
15707 end if;
15708 end if;
15709 end if;
15710
15711 if Is_Tagged_Type (Parent_Type)
15712 and then Is_Concurrent_Type (Parent_Type)
15713 and then not Is_Interface (Parent_Type)
15714 then
15715 Error_Msg_N
15716 ("parent type of a record extension cannot be "
15717 & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
15718 Set_Etype (T, Any_Type);
15719 return;
15720 end if;
15721
15722 -- Ada 2005 (AI-251): Decorate all the names in the list of ancestor
15723 -- interfaces
15724
15725 if Is_Tagged_Type (Parent_Type)
15726 and then Is_Non_Empty_List (Interface_List (Def))
15727 then
15728 declare
15729 Intf : Node_Id;
15730 T : Entity_Id;
15731
15732 begin
15733 Intf := First (Interface_List (Def));
15734 while Present (Intf) loop
15735 T := Find_Type_Of_Subtype_Indic (Intf);
15736
15737 if not Is_Interface (T) then
15738 Diagnose_Interface (Intf, T);
15739
15740 -- Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
15741 -- a limited type from having a nonlimited progenitor.
15742
15743 elsif (Limited_Present (Def)
15744 or else (not Is_Interface (Parent_Type)
15745 and then Is_Limited_Type (Parent_Type)))
15746 and then not Is_Limited_Interface (T)
15747 then
15748 Error_Msg_NE
15749 ("progenitor interface& of limited type must be limited",
15750 N, T);
15751 end if;
15752
15753 Next (Intf);
15754 end loop;
15755 end;
15756 end if;
15757
15758 if Parent_Type = Any_Type
15759 or else Etype (Parent_Type) = Any_Type
15760 or else (Is_Class_Wide_Type (Parent_Type)
15761 and then Etype (Parent_Type) = T)
15762 then
15763 -- If Parent_Type is undefined or illegal, make new type into a
15764 -- subtype of Any_Type, and set a few attributes to prevent cascaded
15765 -- errors. If this is a self-definition, emit error now.
15766
15767 if T = Parent_Type or else T = Etype (Parent_Type) then
15768 Error_Msg_N ("type cannot be used in its own definition", Indic);
15769 end if;
15770
15771 Set_Ekind (T, Ekind (Parent_Type));
15772 Set_Etype (T, Any_Type);
15773 Set_Scalar_Range (T, Scalar_Range (Any_Type));
15774
15775 if Is_Tagged_Type (T)
15776 and then Is_Record_Type (T)
15777 then
15778 Set_Direct_Primitive_Operations (T, New_Elmt_List);
15779 end if;
15780
15781 return;
15782 end if;
15783
15784 -- Ada 2005 (AI-251): The case in which the parent of the full-view is
15785 -- an interface is special because the list of interfaces in the full
15786 -- view can be given in any order. For example:
15787
15788 -- type A is interface;
15789 -- type B is interface and A;
15790 -- type D is new B with private;
15791 -- private
15792 -- type D is new A and B with null record; -- 1 --
15793
15794 -- In this case we perform the following transformation of -1-:
15795
15796 -- type D is new B and A with null record;
15797
15798 -- If the parent of the full-view covers the parent of the partial-view
15799 -- we have two possible cases:
15800
15801 -- 1) They have the same parent
15802 -- 2) The parent of the full-view implements some further interfaces
15803
15804 -- In both cases we do not need to perform the transformation. In the
15805 -- first case the source program is correct and the transformation is
15806 -- not needed; in the second case the source program does not fulfill
15807 -- the no-hidden interfaces rule (AI-396) and the error will be reported
15808 -- later.
15809
15810 -- This transformation not only simplifies the rest of the analysis of
15811 -- this type declaration but also simplifies the correct generation of
15812 -- the object layout to the expander.
15813
15814 if In_Private_Part (Current_Scope)
15815 and then Is_Interface (Parent_Type)
15816 then
15817 declare
15818 Iface : Node_Id;
15819 Partial_View : Entity_Id;
15820 Partial_View_Parent : Entity_Id;
15821 New_Iface : Node_Id;
15822
15823 begin
15824 -- Look for the associated private type declaration
15825
15826 Partial_View := First_Entity (Current_Scope);
15827 loop
15828 exit when No (Partial_View)
15829 or else (Has_Private_Declaration (Partial_View)
15830 and then Full_View (Partial_View) = T);
15831
15832 Next_Entity (Partial_View);
15833 end loop;
15834
15835 -- If the partial view was not found then the source code has
15836 -- errors and the transformation is not needed.
15837
15838 if Present (Partial_View) then
15839 Partial_View_Parent := Etype (Partial_View);
15840
15841 -- If the parent of the full-view covers the parent of the
15842 -- partial-view we have nothing else to do.
15843
15844 if Interface_Present_In_Ancestor
15845 (Parent_Type, Partial_View_Parent)
15846 then
15847 null;
15848
15849 -- Traverse the list of interfaces of the full-view to look
15850 -- for the parent of the partial-view and perform the tree
15851 -- transformation.
15852
15853 else
15854 Iface := First (Interface_List (Def));
15855 while Present (Iface) loop
15856 if Etype (Iface) = Etype (Partial_View) then
15857 Rewrite (Subtype_Indication (Def),
15858 New_Copy (Subtype_Indication
15859 (Parent (Partial_View))));
15860
15861 New_Iface :=
15862 Make_Identifier (Sloc (N), Chars (Parent_Type));
15863 Append (New_Iface, Interface_List (Def));
15864
15865 -- Analyze the transformed code
15866
15867 Derived_Type_Declaration (T, N, Is_Completion);
15868 return;
15869 end if;
15870
15871 Next (Iface);
15872 end loop;
15873 end if;
15874 end if;
15875 end;
15876 end if;
15877
15878 -- Only composite types other than array types are allowed to have
15879 -- discriminants.
15880
15881 if Present (Discriminant_Specifications (N)) then
15882 if (Is_Elementary_Type (Parent_Type)
15883 or else
15884 Is_Array_Type (Parent_Type))
15885 and then not Error_Posted (N)
15886 then
15887 Error_Msg_N
15888 ("elementary or array type cannot have discriminants",
15889 Defining_Identifier (First (Discriminant_Specifications (N))));
15890 Set_Has_Discriminants (T, False);
15891
15892 -- The type is allowed to have discriminants
15893
15894 else
15895 Check_SPARK_05_Restriction ("discriminant type is not allowed", N);
15896 end if;
15897 end if;
15898
15899 -- In Ada 83, a derived type defined in a package specification cannot
15900 -- be used for further derivation until the end of its visible part.
15901 -- Note that derivation in the private part of the package is allowed.
15902
15903 if Ada_Version = Ada_83
15904 and then Is_Derived_Type (Parent_Type)
15905 and then In_Visible_Part (Scope (Parent_Type))
15906 then
15907 if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
15908 Error_Msg_N
15909 ("(Ada 83): premature use of type for derivation", Indic);
15910 end if;
15911 end if;
15912
15913 -- Check for early use of incomplete or private type
15914
15915 if Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
15916 Error_Msg_N ("premature derivation of incomplete type", Indic);
15917 return;
15918
15919 elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
15920 and then not Comes_From_Generic (Parent_Type))
15921 or else Has_Private_Component (Parent_Type)
15922 then
15923 -- The ancestor type of a formal type can be incomplete, in which
15924 -- case only the operations of the partial view are available in the
15925 -- generic. Subsequent checks may be required when the full view is
15926 -- analyzed to verify that a derivation from a tagged type has an
15927 -- extension.
15928
15929 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
15930 null;
15931
15932 elsif No (Underlying_Type (Parent_Type))
15933 or else Has_Private_Component (Parent_Type)
15934 then
15935 Error_Msg_N
15936 ("premature derivation of derived or private type", Indic);
15937
15938 -- Flag the type itself as being in error, this prevents some
15939 -- nasty problems with subsequent uses of the malformed type.
15940
15941 Set_Error_Posted (T);
15942
15943 -- Check that within the immediate scope of an untagged partial
15944 -- view it's illegal to derive from the partial view if the
15945 -- full view is tagged. (7.3(7))
15946
15947 -- We verify that the Parent_Type is a partial view by checking
15948 -- that it is not a Full_Type_Declaration (i.e. a private type or
15949 -- private extension declaration), to distinguish a partial view
15950 -- from a derivation from a private type which also appears as
15951 -- E_Private_Type. If the parent base type is not declared in an
15952 -- enclosing scope there is no need to check.
15953
15954 elsif Present (Full_View (Parent_Type))
15955 and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
15956 and then not Is_Tagged_Type (Parent_Type)
15957 and then Is_Tagged_Type (Full_View (Parent_Type))
15958 and then In_Open_Scopes (Scope (Base_Type (Parent_Type)))
15959 then
15960 Error_Msg_N
15961 ("premature derivation from type with tagged full view",
15962 Indic);
15963 end if;
15964 end if;
15965
15966 -- Check that form of derivation is appropriate
15967
15968 Taggd := Is_Tagged_Type (Parent_Type);
15969
15970 -- Set the parent type to the class-wide type's specific type in this
15971 -- case to prevent cascading errors
15972
15973 if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
15974 Error_Msg_N ("parent type must not be a class-wide type", Indic);
15975 Set_Etype (T, Etype (Parent_Type));
15976 return;
15977 end if;
15978
15979 if Present (Extension) and then not Taggd then
15980 Error_Msg_N
15981 ("type derived from untagged type cannot have extension", Indic);
15982
15983 elsif No (Extension) and then Taggd then
15984
15985 -- If this declaration is within a private part (or body) of a
15986 -- generic instantiation then the derivation is allowed (the parent
15987 -- type can only appear tagged in this case if it's a generic actual
15988 -- type, since it would otherwise have been rejected in the analysis
15989 -- of the generic template).
15990
15991 if not Is_Generic_Actual_Type (Parent_Type)
15992 or else In_Visible_Part (Scope (Parent_Type))
15993 then
15994 if Is_Class_Wide_Type (Parent_Type) then
15995 Error_Msg_N
15996 ("parent type must not be a class-wide type", Indic);
15997
15998 -- Use specific type to prevent cascaded errors.
15999
16000 Parent_Type := Etype (Parent_Type);
16001
16002 else
16003 Error_Msg_N
16004 ("type derived from tagged type must have extension", Indic);
16005 end if;
16006 end if;
16007 end if;
16008
16009 -- AI-443: Synchronized formal derived types require a private
16010 -- extension. There is no point in checking the ancestor type or
16011 -- the progenitors since the construct is wrong to begin with.
16012
16013 if Ada_Version >= Ada_2005
16014 and then Is_Generic_Type (T)
16015 and then Present (Original_Node (N))
16016 then
16017 declare
16018 Decl : constant Node_Id := Original_Node (N);
16019
16020 begin
16021 if Nkind (Decl) = N_Formal_Type_Declaration
16022 and then Nkind (Formal_Type_Definition (Decl)) =
16023 N_Formal_Derived_Type_Definition
16024 and then Synchronized_Present (Formal_Type_Definition (Decl))
16025 and then No (Extension)
16026
16027 -- Avoid emitting a duplicate error message
16028
16029 and then not Error_Posted (Indic)
16030 then
16031 Error_Msg_N
16032 ("synchronized derived type must have extension", N);
16033 end if;
16034 end;
16035 end if;
16036
16037 if Null_Exclusion_Present (Def)
16038 and then not Is_Access_Type (Parent_Type)
16039 then
16040 Error_Msg_N ("null exclusion can only apply to an access type", N);
16041 end if;
16042
16043 -- Avoid deriving parent primitives of underlying record views
16044
16045 Build_Derived_Type (N, Parent_Type, T, Is_Completion,
16046 Derive_Subps => not Is_Underlying_Record_View (T));
16047
16048 -- AI-419: The parent type of an explicitly limited derived type must
16049 -- be a limited type or a limited interface.
16050
16051 if Limited_Present (Def) then
16052 Set_Is_Limited_Record (T);
16053
16054 if Is_Interface (T) then
16055 Set_Is_Limited_Interface (T);
16056 end if;
16057
16058 if not Is_Limited_Type (Parent_Type)
16059 and then
16060 (not Is_Interface (Parent_Type)
16061 or else not Is_Limited_Interface (Parent_Type))
16062 then
16063 -- AI05-0096: a derivation in the private part of an instance is
16064 -- legal if the generic formal is untagged limited, and the actual
16065 -- is non-limited.
16066
16067 if Is_Generic_Actual_Type (Parent_Type)
16068 and then In_Private_Part (Current_Scope)
16069 and then
16070 not Is_Tagged_Type
16071 (Generic_Parent_Type (Parent (Parent_Type)))
16072 then
16073 null;
16074
16075 else
16076 Error_Msg_NE
16077 ("parent type& of limited type must be limited",
16078 N, Parent_Type);
16079 end if;
16080 end if;
16081 end if;
16082
16083 -- In SPARK, there are no derived type definitions other than type
16084 -- extensions of tagged record types.
16085
16086 if No (Extension) then
16087 Check_SPARK_05_Restriction
16088 ("derived type is not allowed", Original_Node (N));
16089 end if;
16090 end Derived_Type_Declaration;
16091
16092 ------------------------
16093 -- Diagnose_Interface --
16094 ------------------------
16095
16096 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id) is
16097 begin
16098 if not Is_Interface (E) and then E /= Any_Type then
16099 Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
16100 end if;
16101 end Diagnose_Interface;
16102
16103 ----------------------------------
16104 -- Enumeration_Type_Declaration --
16105 ----------------------------------
16106
16107 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
16108 Ev : Uint;
16109 L : Node_Id;
16110 R_Node : Node_Id;
16111 B_Node : Node_Id;
16112
16113 begin
16114 -- Create identifier node representing lower bound
16115
16116 B_Node := New_Node (N_Identifier, Sloc (Def));
16117 L := First (Literals (Def));
16118 Set_Chars (B_Node, Chars (L));
16119 Set_Entity (B_Node, L);
16120 Set_Etype (B_Node, T);
16121 Set_Is_Static_Expression (B_Node, True);
16122
16123 R_Node := New_Node (N_Range, Sloc (Def));
16124 Set_Low_Bound (R_Node, B_Node);
16125
16126 Set_Ekind (T, E_Enumeration_Type);
16127 Set_First_Literal (T, L);
16128 Set_Etype (T, T);
16129 Set_Is_Constrained (T);
16130
16131 Ev := Uint_0;
16132
16133 -- Loop through literals of enumeration type setting pos and rep values
16134 -- except that if the Ekind is already set, then it means the literal
16135 -- was already constructed (case of a derived type declaration and we
16136 -- should not disturb the Pos and Rep values.
16137
16138 while Present (L) loop
16139 if Ekind (L) /= E_Enumeration_Literal then
16140 Set_Ekind (L, E_Enumeration_Literal);
16141 Set_Enumeration_Pos (L, Ev);
16142 Set_Enumeration_Rep (L, Ev);
16143 Set_Is_Known_Valid (L, True);
16144 end if;
16145
16146 Set_Etype (L, T);
16147 New_Overloaded_Entity (L);
16148 Generate_Definition (L);
16149 Set_Convention (L, Convention_Intrinsic);
16150
16151 -- Case of character literal
16152
16153 if Nkind (L) = N_Defining_Character_Literal then
16154 Set_Is_Character_Type (T, True);
16155
16156 -- Check violation of No_Wide_Characters
16157
16158 if Restriction_Check_Required (No_Wide_Characters) then
16159 Get_Name_String (Chars (L));
16160
16161 if Name_Len >= 3 and then Name_Buffer (1 .. 2) = "QW" then
16162 Check_Restriction (No_Wide_Characters, L);
16163 end if;
16164 end if;
16165 end if;
16166
16167 Ev := Ev + 1;
16168 Next (L);
16169 end loop;
16170
16171 -- Now create a node representing upper bound
16172
16173 B_Node := New_Node (N_Identifier, Sloc (Def));
16174 Set_Chars (B_Node, Chars (Last (Literals (Def))));
16175 Set_Entity (B_Node, Last (Literals (Def)));
16176 Set_Etype (B_Node, T);
16177 Set_Is_Static_Expression (B_Node, True);
16178
16179 Set_High_Bound (R_Node, B_Node);
16180
16181 -- Initialize various fields of the type. Some of this information
16182 -- may be overwritten later through rep.clauses.
16183
16184 Set_Scalar_Range (T, R_Node);
16185 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
16186 Set_Enum_Esize (T);
16187 Set_Enum_Pos_To_Rep (T, Empty);
16188
16189 -- Set Discard_Names if configuration pragma set, or if there is
16190 -- a parameterless pragma in the current declarative region
16191
16192 if Global_Discard_Names or else Discard_Names (Scope (T)) then
16193 Set_Discard_Names (T);
16194 end if;
16195
16196 -- Process end label if there is one
16197
16198 if Present (Def) then
16199 Process_End_Label (Def, 'e', T);
16200 end if;
16201 end Enumeration_Type_Declaration;
16202
16203 ---------------------------------
16204 -- Expand_To_Stored_Constraint --
16205 ---------------------------------
16206
16207 function Expand_To_Stored_Constraint
16208 (Typ : Entity_Id;
16209 Constraint : Elist_Id) return Elist_Id
16210 is
16211 Explicitly_Discriminated_Type : Entity_Id;
16212 Expansion : Elist_Id;
16213 Discriminant : Entity_Id;
16214
16215 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
16216 -- Find the nearest type that actually specifies discriminants
16217
16218 ---------------------------------
16219 -- Type_With_Explicit_Discrims --
16220 ---------------------------------
16221
16222 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
16223 Typ : constant E := Base_Type (Id);
16224
16225 begin
16226 if Ekind (Typ) in Incomplete_Or_Private_Kind then
16227 if Present (Full_View (Typ)) then
16228 return Type_With_Explicit_Discrims (Full_View (Typ));
16229 end if;
16230
16231 else
16232 if Has_Discriminants (Typ) then
16233 return Typ;
16234 end if;
16235 end if;
16236
16237 if Etype (Typ) = Typ then
16238 return Empty;
16239 elsif Has_Discriminants (Typ) then
16240 return Typ;
16241 else
16242 return Type_With_Explicit_Discrims (Etype (Typ));
16243 end if;
16244
16245 end Type_With_Explicit_Discrims;
16246
16247 -- Start of processing for Expand_To_Stored_Constraint
16248
16249 begin
16250 if No (Constraint) or else Is_Empty_Elmt_List (Constraint) then
16251 return No_Elist;
16252 end if;
16253
16254 Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
16255
16256 if No (Explicitly_Discriminated_Type) then
16257 return No_Elist;
16258 end if;
16259
16260 Expansion := New_Elmt_List;
16261
16262 Discriminant :=
16263 First_Stored_Discriminant (Explicitly_Discriminated_Type);
16264 while Present (Discriminant) loop
16265 Append_Elmt
16266 (Get_Discriminant_Value
16267 (Discriminant, Explicitly_Discriminated_Type, Constraint),
16268 To => Expansion);
16269 Next_Stored_Discriminant (Discriminant);
16270 end loop;
16271
16272 return Expansion;
16273 end Expand_To_Stored_Constraint;
16274
16275 ---------------------------
16276 -- Find_Hidden_Interface --
16277 ---------------------------
16278
16279 function Find_Hidden_Interface
16280 (Src : Elist_Id;
16281 Dest : Elist_Id) return Entity_Id
16282 is
16283 Iface : Entity_Id;
16284 Iface_Elmt : Elmt_Id;
16285
16286 begin
16287 if Present (Src) and then Present (Dest) then
16288 Iface_Elmt := First_Elmt (Src);
16289 while Present (Iface_Elmt) loop
16290 Iface := Node (Iface_Elmt);
16291
16292 if Is_Interface (Iface)
16293 and then not Contain_Interface (Iface, Dest)
16294 then
16295 return Iface;
16296 end if;
16297
16298 Next_Elmt (Iface_Elmt);
16299 end loop;
16300 end if;
16301
16302 return Empty;
16303 end Find_Hidden_Interface;
16304
16305 --------------------
16306 -- Find_Type_Name --
16307 --------------------
16308
16309 function Find_Type_Name (N : Node_Id) return Entity_Id is
16310 Id : constant Entity_Id := Defining_Identifier (N);
16311 Prev : Entity_Id;
16312 New_Id : Entity_Id;
16313 Prev_Par : Node_Id;
16314
16315 procedure Check_Duplicate_Aspects;
16316 -- Check that aspects specified in a completion have not been specified
16317 -- already in the partial view. Type_Invariant and others can be
16318 -- specified on either view but never on both.
16319
16320 procedure Tag_Mismatch;
16321 -- Diagnose a tagged partial view whose full view is untagged.
16322 -- We post the message on the full view, with a reference to
16323 -- the previous partial view. The partial view can be private
16324 -- or incomplete, and these are handled in a different manner,
16325 -- so we determine the position of the error message from the
16326 -- respective slocs of both.
16327
16328 -----------------------------
16329 -- Check_Duplicate_Aspects --
16330 -----------------------------
16331 procedure Check_Duplicate_Aspects is
16332 Prev_Aspects : constant List_Id := Aspect_Specifications (Prev_Par);
16333 Full_Aspects : constant List_Id := Aspect_Specifications (N);
16334 F_Spec, P_Spec : Node_Id;
16335
16336 begin
16337 if Present (Prev_Aspects) and then Present (Full_Aspects) then
16338 F_Spec := First (Full_Aspects);
16339 while Present (F_Spec) loop
16340 P_Spec := First (Prev_Aspects);
16341 while Present (P_Spec) loop
16342 if Chars (Identifier (P_Spec)) = Chars (Identifier (F_Spec))
16343 then
16344 Error_Msg_N
16345 ("aspect already specified in private declaration",
16346 F_Spec);
16347 Remove (F_Spec);
16348 return;
16349 end if;
16350
16351 Next (P_Spec);
16352 end loop;
16353
16354 Next (F_Spec);
16355 end loop;
16356 end if;
16357 end Check_Duplicate_Aspects;
16358
16359 ------------------
16360 -- Tag_Mismatch --
16361 ------------------
16362
16363 procedure Tag_Mismatch is
16364 begin
16365 if Sloc (Prev) < Sloc (Id) then
16366 if Ada_Version >= Ada_2012
16367 and then Nkind (N) = N_Private_Type_Declaration
16368 then
16369 Error_Msg_NE
16370 ("declaration of private } must be a tagged type ", Id, Prev);
16371 else
16372 Error_Msg_NE
16373 ("full declaration of } must be a tagged type ", Id, Prev);
16374 end if;
16375
16376 else
16377 if Ada_Version >= Ada_2012
16378 and then Nkind (N) = N_Private_Type_Declaration
16379 then
16380 Error_Msg_NE
16381 ("declaration of private } must be a tagged type ", Prev, Id);
16382 else
16383 Error_Msg_NE
16384 ("full declaration of } must be a tagged type ", Prev, Id);
16385 end if;
16386 end if;
16387 end Tag_Mismatch;
16388
16389 -- Start of processing for Find_Type_Name
16390
16391 begin
16392 -- Find incomplete declaration, if one was given
16393
16394 Prev := Current_Entity_In_Scope (Id);
16395
16396 -- New type declaration
16397
16398 if No (Prev) then
16399 Enter_Name (Id);
16400 return Id;
16401
16402 -- Previous declaration exists
16403
16404 else
16405 Prev_Par := Parent (Prev);
16406
16407 -- Error if not incomplete/private case except if previous
16408 -- declaration is implicit, etc. Enter_Name will emit error if
16409 -- appropriate.
16410
16411 if not Is_Incomplete_Or_Private_Type (Prev) then
16412 Enter_Name (Id);
16413 New_Id := Id;
16414
16415 -- Check invalid completion of private or incomplete type
16416
16417 elsif not Nkind_In (N, N_Full_Type_Declaration,
16418 N_Task_Type_Declaration,
16419 N_Protected_Type_Declaration)
16420 and then
16421 (Ada_Version < Ada_2012
16422 or else not Is_Incomplete_Type (Prev)
16423 or else not Nkind_In (N, N_Private_Type_Declaration,
16424 N_Private_Extension_Declaration))
16425 then
16426 -- Completion must be a full type declarations (RM 7.3(4))
16427
16428 Error_Msg_Sloc := Sloc (Prev);
16429 Error_Msg_NE ("invalid completion of }", Id, Prev);
16430
16431 -- Set scope of Id to avoid cascaded errors. Entity is never
16432 -- examined again, except when saving globals in generics.
16433
16434 Set_Scope (Id, Current_Scope);
16435 New_Id := Id;
16436
16437 -- If this is a repeated incomplete declaration, no further
16438 -- checks are possible.
16439
16440 if Nkind (N) = N_Incomplete_Type_Declaration then
16441 return Prev;
16442 end if;
16443
16444 -- Case of full declaration of incomplete type
16445
16446 elsif Ekind (Prev) = E_Incomplete_Type
16447 and then (Ada_Version < Ada_2012
16448 or else No (Full_View (Prev))
16449 or else not Is_Private_Type (Full_View (Prev)))
16450 then
16451 -- Indicate that the incomplete declaration has a matching full
16452 -- declaration. The defining occurrence of the incomplete
16453 -- declaration remains the visible one, and the procedure
16454 -- Get_Full_View dereferences it whenever the type is used.
16455
16456 if Present (Full_View (Prev)) then
16457 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
16458 end if;
16459
16460 Set_Full_View (Prev, Id);
16461 Append_Entity (Id, Current_Scope);
16462 Set_Is_Public (Id, Is_Public (Prev));
16463 Set_Is_Internal (Id);
16464 New_Id := Prev;
16465
16466 -- If the incomplete view is tagged, a class_wide type has been
16467 -- created already. Use it for the private type as well, in order
16468 -- to prevent multiple incompatible class-wide types that may be
16469 -- created for self-referential anonymous access components.
16470
16471 if Is_Tagged_Type (Prev)
16472 and then Present (Class_Wide_Type (Prev))
16473 then
16474 Set_Ekind (Id, Ekind (Prev)); -- will be reset later
16475 Set_Class_Wide_Type (Id, Class_Wide_Type (Prev));
16476
16477 -- The type of the classwide type is the current Id. Previously
16478 -- this was not done for private declarations because of order-
16479 -- of elaboration issues in the back-end, but gigi now handles
16480 -- this properly.
16481
16482 Set_Etype (Class_Wide_Type (Id), Id);
16483 end if;
16484
16485 -- Case of full declaration of private type
16486
16487 else
16488 -- If the private type was a completion of an incomplete type then
16489 -- update Prev to reference the private type
16490
16491 if Ada_Version >= Ada_2012
16492 and then Ekind (Prev) = E_Incomplete_Type
16493 and then Present (Full_View (Prev))
16494 and then Is_Private_Type (Full_View (Prev))
16495 then
16496 Prev := Full_View (Prev);
16497 Prev_Par := Parent (Prev);
16498 end if;
16499
16500 if Nkind (N) = N_Full_Type_Declaration
16501 and then Nkind_In
16502 (Type_Definition (N), N_Record_Definition,
16503 N_Derived_Type_Definition)
16504 and then Interface_Present (Type_Definition (N))
16505 then
16506 Error_Msg_N
16507 ("completion of private type cannot be an interface", N);
16508 end if;
16509
16510 if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
16511 if Etype (Prev) /= Prev then
16512
16513 -- Prev is a private subtype or a derived type, and needs
16514 -- no completion.
16515
16516 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
16517 New_Id := Id;
16518
16519 elsif Ekind (Prev) = E_Private_Type
16520 and then Nkind_In (N, N_Task_Type_Declaration,
16521 N_Protected_Type_Declaration)
16522 then
16523 Error_Msg_N
16524 ("completion of nonlimited type cannot be limited", N);
16525
16526 elsif Ekind (Prev) = E_Record_Type_With_Private
16527 and then Nkind_In (N, N_Task_Type_Declaration,
16528 N_Protected_Type_Declaration)
16529 then
16530 if not Is_Limited_Record (Prev) then
16531 Error_Msg_N
16532 ("completion of nonlimited type cannot be limited", N);
16533
16534 elsif No (Interface_List (N)) then
16535 Error_Msg_N
16536 ("completion of tagged private type must be tagged",
16537 N);
16538 end if;
16539 end if;
16540
16541 -- Ada 2005 (AI-251): Private extension declaration of a task
16542 -- type or a protected type. This case arises when covering
16543 -- interface types.
16544
16545 elsif Nkind_In (N, N_Task_Type_Declaration,
16546 N_Protected_Type_Declaration)
16547 then
16548 null;
16549
16550 elsif Nkind (N) /= N_Full_Type_Declaration
16551 or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
16552 then
16553 Error_Msg_N
16554 ("full view of private extension must be an extension", N);
16555
16556 elsif not (Abstract_Present (Parent (Prev)))
16557 and then Abstract_Present (Type_Definition (N))
16558 then
16559 Error_Msg_N
16560 ("full view of non-abstract extension cannot be abstract", N);
16561 end if;
16562
16563 if not In_Private_Part (Current_Scope) then
16564 Error_Msg_N
16565 ("declaration of full view must appear in private part", N);
16566 end if;
16567
16568 if Ada_Version >= Ada_2012 then
16569 Check_Duplicate_Aspects;
16570 end if;
16571
16572 Copy_And_Swap (Prev, Id);
16573 Set_Has_Private_Declaration (Prev);
16574 Set_Has_Private_Declaration (Id);
16575
16576 -- AI12-0133: Indicate whether we have a partial view with
16577 -- unknown discriminants, in which case initialization of objects
16578 -- of the type do not receive an invariant check.
16579
16580 Set_Partial_View_Has_Unknown_Discr
16581 (Prev, Has_Unknown_Discriminants (Id));
16582
16583 -- Preserve aspect and iterator flags that may have been set on
16584 -- the partial view.
16585
16586 Set_Has_Delayed_Aspects (Prev, Has_Delayed_Aspects (Id));
16587 Set_Has_Implicit_Dereference (Prev, Has_Implicit_Dereference (Id));
16588
16589 -- If no error, propagate freeze_node from private to full view.
16590 -- It may have been generated for an early operational item.
16591
16592 if Present (Freeze_Node (Id))
16593 and then Serious_Errors_Detected = 0
16594 and then No (Full_View (Id))
16595 then
16596 Set_Freeze_Node (Prev, Freeze_Node (Id));
16597 Set_Freeze_Node (Id, Empty);
16598 Set_First_Rep_Item (Prev, First_Rep_Item (Id));
16599 end if;
16600
16601 Set_Full_View (Id, Prev);
16602 New_Id := Prev;
16603 end if;
16604
16605 -- Verify that full declaration conforms to partial one
16606
16607 if Is_Incomplete_Or_Private_Type (Prev)
16608 and then Present (Discriminant_Specifications (Prev_Par))
16609 then
16610 if Present (Discriminant_Specifications (N)) then
16611 if Ekind (Prev) = E_Incomplete_Type then
16612 Check_Discriminant_Conformance (N, Prev, Prev);
16613 else
16614 Check_Discriminant_Conformance (N, Prev, Id);
16615 end if;
16616
16617 else
16618 Error_Msg_N
16619 ("missing discriminants in full type declaration", N);
16620
16621 -- To avoid cascaded errors on subsequent use, share the
16622 -- discriminants of the partial view.
16623
16624 Set_Discriminant_Specifications (N,
16625 Discriminant_Specifications (Prev_Par));
16626 end if;
16627 end if;
16628
16629 -- A prior untagged partial view can have an associated class-wide
16630 -- type due to use of the class attribute, and in this case the full
16631 -- type must also be tagged. This Ada 95 usage is deprecated in favor
16632 -- of incomplete tagged declarations, but we check for it.
16633
16634 if Is_Type (Prev)
16635 and then (Is_Tagged_Type (Prev)
16636 or else Present (Class_Wide_Type (Prev)))
16637 then
16638 -- Ada 2012 (AI05-0162): A private type may be the completion of
16639 -- an incomplete type.
16640
16641 if Ada_Version >= Ada_2012
16642 and then Is_Incomplete_Type (Prev)
16643 and then Nkind_In (N, N_Private_Type_Declaration,
16644 N_Private_Extension_Declaration)
16645 then
16646 -- No need to check private extensions since they are tagged
16647
16648 if Nkind (N) = N_Private_Type_Declaration
16649 and then not Tagged_Present (N)
16650 then
16651 Tag_Mismatch;
16652 end if;
16653
16654 -- The full declaration is either a tagged type (including
16655 -- a synchronized type that implements interfaces) or a
16656 -- type extension, otherwise this is an error.
16657
16658 elsif Nkind_In (N, N_Task_Type_Declaration,
16659 N_Protected_Type_Declaration)
16660 then
16661 if No (Interface_List (N)) and then not Error_Posted (N) then
16662 Tag_Mismatch;
16663 end if;
16664
16665 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
16666
16667 -- Indicate that the previous declaration (tagged incomplete
16668 -- or private declaration) requires the same on the full one.
16669
16670 if not Tagged_Present (Type_Definition (N)) then
16671 Tag_Mismatch;
16672 Set_Is_Tagged_Type (Id);
16673 end if;
16674
16675 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
16676 if No (Record_Extension_Part (Type_Definition (N))) then
16677 Error_Msg_NE
16678 ("full declaration of } must be a record extension",
16679 Prev, Id);
16680
16681 -- Set some attributes to produce a usable full view
16682
16683 Set_Is_Tagged_Type (Id);
16684 end if;
16685
16686 else
16687 Tag_Mismatch;
16688 end if;
16689 end if;
16690
16691 if Present (Prev)
16692 and then Nkind (Parent (Prev)) = N_Incomplete_Type_Declaration
16693 and then Present (Premature_Use (Parent (Prev)))
16694 then
16695 Error_Msg_Sloc := Sloc (N);
16696 Error_Msg_N
16697 ("\full declaration #", Premature_Use (Parent (Prev)));
16698 end if;
16699
16700 return New_Id;
16701 end if;
16702 end Find_Type_Name;
16703
16704 -------------------------
16705 -- Find_Type_Of_Object --
16706 -------------------------
16707
16708 function Find_Type_Of_Object
16709 (Obj_Def : Node_Id;
16710 Related_Nod : Node_Id) return Entity_Id
16711 is
16712 Def_Kind : constant Node_Kind := Nkind (Obj_Def);
16713 P : Node_Id := Parent (Obj_Def);
16714 T : Entity_Id;
16715 Nam : Name_Id;
16716
16717 begin
16718 -- If the parent is a component_definition node we climb to the
16719 -- component_declaration node
16720
16721 if Nkind (P) = N_Component_Definition then
16722 P := Parent (P);
16723 end if;
16724
16725 -- Case of an anonymous array subtype
16726
16727 if Nkind_In (Def_Kind, N_Constrained_Array_Definition,
16728 N_Unconstrained_Array_Definition)
16729 then
16730 T := Empty;
16731 Array_Type_Declaration (T, Obj_Def);
16732
16733 -- Create an explicit subtype whenever possible
16734
16735 elsif Nkind (P) /= N_Component_Declaration
16736 and then Def_Kind = N_Subtype_Indication
16737 then
16738 -- Base name of subtype on object name, which will be unique in
16739 -- the current scope.
16740
16741 -- If this is a duplicate declaration, return base type, to avoid
16742 -- generating duplicate anonymous types.
16743
16744 if Error_Posted (P) then
16745 Analyze (Subtype_Mark (Obj_Def));
16746 return Entity (Subtype_Mark (Obj_Def));
16747 end if;
16748
16749 Nam :=
16750 New_External_Name
16751 (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
16752
16753 T := Make_Defining_Identifier (Sloc (P), Nam);
16754
16755 Insert_Action (Obj_Def,
16756 Make_Subtype_Declaration (Sloc (P),
16757 Defining_Identifier => T,
16758 Subtype_Indication => Relocate_Node (Obj_Def)));
16759
16760 -- This subtype may need freezing, and this will not be done
16761 -- automatically if the object declaration is not in declarative
16762 -- part. Since this is an object declaration, the type cannot always
16763 -- be frozen here. Deferred constants do not freeze their type
16764 -- (which often enough will be private).
16765
16766 if Nkind (P) = N_Object_Declaration
16767 and then Constant_Present (P)
16768 and then No (Expression (P))
16769 then
16770 null;
16771
16772 -- Here we freeze the base type of object type to catch premature use
16773 -- of discriminated private type without a full view.
16774
16775 else
16776 Insert_Actions (Obj_Def, Freeze_Entity (Base_Type (T), P));
16777 end if;
16778
16779 -- Ada 2005 AI-406: the object definition in an object declaration
16780 -- can be an access definition.
16781
16782 elsif Def_Kind = N_Access_Definition then
16783 T := Access_Definition (Related_Nod, Obj_Def);
16784
16785 Set_Is_Local_Anonymous_Access
16786 (T,
16787 V => (Ada_Version < Ada_2012)
16788 or else (Nkind (P) /= N_Object_Declaration)
16789 or else Is_Library_Level_Entity (Defining_Identifier (P)));
16790
16791 -- Otherwise, the object definition is just a subtype_mark
16792
16793 else
16794 T := Process_Subtype (Obj_Def, Related_Nod);
16795
16796 -- If expansion is disabled an object definition that is an aggregate
16797 -- will not get expanded and may lead to scoping problems in the back
16798 -- end, if the object is referenced in an inner scope. In that case
16799 -- create an itype reference for the object definition now. This
16800 -- may be redundant in some cases, but harmless.
16801
16802 if Is_Itype (T)
16803 and then Nkind (Related_Nod) = N_Object_Declaration
16804 and then ASIS_Mode
16805 then
16806 Build_Itype_Reference (T, Related_Nod);
16807 end if;
16808 end if;
16809
16810 return T;
16811 end Find_Type_Of_Object;
16812
16813 --------------------------------
16814 -- Find_Type_Of_Subtype_Indic --
16815 --------------------------------
16816
16817 function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
16818 Typ : Entity_Id;
16819
16820 begin
16821 -- Case of subtype mark with a constraint
16822
16823 if Nkind (S) = N_Subtype_Indication then
16824 Find_Type (Subtype_Mark (S));
16825 Typ := Entity (Subtype_Mark (S));
16826
16827 if not
16828 Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
16829 then
16830 Error_Msg_N
16831 ("incorrect constraint for this kind of type", Constraint (S));
16832 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
16833 end if;
16834
16835 -- Otherwise we have a subtype mark without a constraint
16836
16837 elsif Error_Posted (S) then
16838 Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
16839 return Any_Type;
16840
16841 else
16842 Find_Type (S);
16843 Typ := Entity (S);
16844 end if;
16845
16846 -- Check No_Wide_Characters restriction
16847
16848 Check_Wide_Character_Restriction (Typ, S);
16849
16850 return Typ;
16851 end Find_Type_Of_Subtype_Indic;
16852
16853 -------------------------------------
16854 -- Floating_Point_Type_Declaration --
16855 -------------------------------------
16856
16857 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
16858 Digs : constant Node_Id := Digits_Expression (Def);
16859 Max_Digs_Val : constant Uint := Digits_Value (Standard_Long_Long_Float);
16860 Digs_Val : Uint;
16861 Base_Typ : Entity_Id;
16862 Implicit_Base : Entity_Id;
16863 Bound : Node_Id;
16864
16865 function Can_Derive_From (E : Entity_Id) return Boolean;
16866 -- Find if given digits value, and possibly a specified range, allows
16867 -- derivation from specified type
16868
16869 function Find_Base_Type return Entity_Id;
16870 -- Find a predefined base type that Def can derive from, or generate
16871 -- an error and substitute Long_Long_Float if none exists.
16872
16873 ---------------------
16874 -- Can_Derive_From --
16875 ---------------------
16876
16877 function Can_Derive_From (E : Entity_Id) return Boolean is
16878 Spec : constant Entity_Id := Real_Range_Specification (Def);
16879
16880 begin
16881 -- Check specified "digits" constraint
16882
16883 if Digs_Val > Digits_Value (E) then
16884 return False;
16885 end if;
16886
16887 -- Check for matching range, if specified
16888
16889 if Present (Spec) then
16890 if Expr_Value_R (Type_Low_Bound (E)) >
16891 Expr_Value_R (Low_Bound (Spec))
16892 then
16893 return False;
16894 end if;
16895
16896 if Expr_Value_R (Type_High_Bound (E)) <
16897 Expr_Value_R (High_Bound (Spec))
16898 then
16899 return False;
16900 end if;
16901 end if;
16902
16903 return True;
16904 end Can_Derive_From;
16905
16906 --------------------
16907 -- Find_Base_Type --
16908 --------------------
16909
16910 function Find_Base_Type return Entity_Id is
16911 Choice : Elmt_Id := First_Elmt (Predefined_Float_Types);
16912
16913 begin
16914 -- Iterate over the predefined types in order, returning the first
16915 -- one that Def can derive from.
16916
16917 while Present (Choice) loop
16918 if Can_Derive_From (Node (Choice)) then
16919 return Node (Choice);
16920 end if;
16921
16922 Next_Elmt (Choice);
16923 end loop;
16924
16925 -- If we can't derive from any existing type, use Long_Long_Float
16926 -- and give appropriate message explaining the problem.
16927
16928 if Digs_Val > Max_Digs_Val then
16929 -- It might be the case that there is a type with the requested
16930 -- range, just not the combination of digits and range.
16931
16932 Error_Msg_N
16933 ("no predefined type has requested range and precision",
16934 Real_Range_Specification (Def));
16935
16936 else
16937 Error_Msg_N
16938 ("range too large for any predefined type",
16939 Real_Range_Specification (Def));
16940 end if;
16941
16942 return Standard_Long_Long_Float;
16943 end Find_Base_Type;
16944
16945 -- Start of processing for Floating_Point_Type_Declaration
16946
16947 begin
16948 Check_Restriction (No_Floating_Point, Def);
16949
16950 -- Create an implicit base type
16951
16952 Implicit_Base :=
16953 Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
16954
16955 -- Analyze and verify digits value
16956
16957 Analyze_And_Resolve (Digs, Any_Integer);
16958 Check_Digits_Expression (Digs);
16959 Digs_Val := Expr_Value (Digs);
16960
16961 -- Process possible range spec and find correct type to derive from
16962
16963 Process_Real_Range_Specification (Def);
16964
16965 -- Check that requested number of digits is not too high.
16966
16967 if Digs_Val > Max_Digs_Val then
16968
16969 -- The check for Max_Base_Digits may be somewhat expensive, as it
16970 -- requires reading System, so only do it when necessary.
16971
16972 declare
16973 Max_Base_Digits : constant Uint :=
16974 Expr_Value
16975 (Expression
16976 (Parent (RTE (RE_Max_Base_Digits))));
16977
16978 begin
16979 if Digs_Val > Max_Base_Digits then
16980 Error_Msg_Uint_1 := Max_Base_Digits;
16981 Error_Msg_N ("digits value out of range, maximum is ^", Digs);
16982
16983 elsif No (Real_Range_Specification (Def)) then
16984 Error_Msg_Uint_1 := Max_Digs_Val;
16985 Error_Msg_N ("types with more than ^ digits need range spec "
16986 & "(RM 3.5.7(6))", Digs);
16987 end if;
16988 end;
16989 end if;
16990
16991 -- Find a suitable type to derive from or complain and use a substitute
16992
16993 Base_Typ := Find_Base_Type;
16994
16995 -- If there are bounds given in the declaration use them as the bounds
16996 -- of the type, otherwise use the bounds of the predefined base type
16997 -- that was chosen based on the Digits value.
16998
16999 if Present (Real_Range_Specification (Def)) then
17000 Set_Scalar_Range (T, Real_Range_Specification (Def));
17001 Set_Is_Constrained (T);
17002
17003 -- The bounds of this range must be converted to machine numbers
17004 -- in accordance with RM 4.9(38).
17005
17006 Bound := Type_Low_Bound (T);
17007
17008 if Nkind (Bound) = N_Real_Literal then
17009 Set_Realval
17010 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
17011 Set_Is_Machine_Number (Bound);
17012 end if;
17013
17014 Bound := Type_High_Bound (T);
17015
17016 if Nkind (Bound) = N_Real_Literal then
17017 Set_Realval
17018 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
17019 Set_Is_Machine_Number (Bound);
17020 end if;
17021
17022 else
17023 Set_Scalar_Range (T, Scalar_Range (Base_Typ));
17024 end if;
17025
17026 -- Complete definition of implicit base and declared first subtype. The
17027 -- inheritance of the rep item chain ensures that SPARK-related pragmas
17028 -- are not clobbered when the floating point type acts as a full view of
17029 -- a private type.
17030
17031 Set_Etype (Implicit_Base, Base_Typ);
17032 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
17033 Set_Size_Info (Implicit_Base, Base_Typ);
17034 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
17035 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
17036 Set_Digits_Value (Implicit_Base, Digits_Value (Base_Typ));
17037 Set_Float_Rep (Implicit_Base, Float_Rep (Base_Typ));
17038
17039 Set_Ekind (T, E_Floating_Point_Subtype);
17040 Set_Etype (T, Implicit_Base);
17041 Set_Size_Info (T, Implicit_Base);
17042 Set_RM_Size (T, RM_Size (Implicit_Base));
17043 Inherit_Rep_Item_Chain (T, Implicit_Base);
17044 Set_Digits_Value (T, Digs_Val);
17045 end Floating_Point_Type_Declaration;
17046
17047 ----------------------------
17048 -- Get_Discriminant_Value --
17049 ----------------------------
17050
17051 -- This is the situation:
17052
17053 -- There is a non-derived type
17054
17055 -- type T0 (Dx, Dy, Dz...)
17056
17057 -- There are zero or more levels of derivation, with each derivation
17058 -- either purely inheriting the discriminants, or defining its own.
17059
17060 -- type Ti is new Ti-1
17061 -- or
17062 -- type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
17063 -- or
17064 -- subtype Ti is ...
17065
17066 -- The subtype issue is avoided by the use of Original_Record_Component,
17067 -- and the fact that derived subtypes also derive the constraints.
17068
17069 -- This chain leads back from
17070
17071 -- Typ_For_Constraint
17072
17073 -- Typ_For_Constraint has discriminants, and the value for each
17074 -- discriminant is given by its corresponding Elmt of Constraints.
17075
17076 -- Discriminant is some discriminant in this hierarchy
17077
17078 -- We need to return its value
17079
17080 -- We do this by recursively searching each level, and looking for
17081 -- Discriminant. Once we get to the bottom, we start backing up
17082 -- returning the value for it which may in turn be a discriminant
17083 -- further up, so on the backup we continue the substitution.
17084
17085 function Get_Discriminant_Value
17086 (Discriminant : Entity_Id;
17087 Typ_For_Constraint : Entity_Id;
17088 Constraint : Elist_Id) return Node_Id
17089 is
17090 function Root_Corresponding_Discriminant
17091 (Discr : Entity_Id) return Entity_Id;
17092 -- Given a discriminant, traverse the chain of inherited discriminants
17093 -- and return the topmost discriminant.
17094
17095 function Search_Derivation_Levels
17096 (Ti : Entity_Id;
17097 Discrim_Values : Elist_Id;
17098 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
17099 -- This is the routine that performs the recursive search of levels
17100 -- as described above.
17101
17102 -------------------------------------
17103 -- Root_Corresponding_Discriminant --
17104 -------------------------------------
17105
17106 function Root_Corresponding_Discriminant
17107 (Discr : Entity_Id) return Entity_Id
17108 is
17109 D : Entity_Id;
17110
17111 begin
17112 D := Discr;
17113 while Present (Corresponding_Discriminant (D)) loop
17114 D := Corresponding_Discriminant (D);
17115 end loop;
17116
17117 return D;
17118 end Root_Corresponding_Discriminant;
17119
17120 ------------------------------
17121 -- Search_Derivation_Levels --
17122 ------------------------------
17123
17124 function Search_Derivation_Levels
17125 (Ti : Entity_Id;
17126 Discrim_Values : Elist_Id;
17127 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
17128 is
17129 Assoc : Elmt_Id;
17130 Disc : Entity_Id;
17131 Result : Node_Or_Entity_Id;
17132 Result_Entity : Node_Id;
17133
17134 begin
17135 -- If inappropriate type, return Error, this happens only in
17136 -- cascaded error situations, and we want to avoid a blow up.
17137
17138 if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
17139 return Error;
17140 end if;
17141
17142 -- Look deeper if possible. Use Stored_Constraints only for
17143 -- untagged types. For tagged types use the given constraint.
17144 -- This asymmetry needs explanation???
17145
17146 if not Stored_Discrim_Values
17147 and then Present (Stored_Constraint (Ti))
17148 and then not Is_Tagged_Type (Ti)
17149 then
17150 Result :=
17151 Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
17152 else
17153 declare
17154 Td : constant Entity_Id := Etype (Ti);
17155
17156 begin
17157 if Td = Ti then
17158 Result := Discriminant;
17159
17160 else
17161 if Present (Stored_Constraint (Ti)) then
17162 Result :=
17163 Search_Derivation_Levels
17164 (Td, Stored_Constraint (Ti), True);
17165 else
17166 Result :=
17167 Search_Derivation_Levels
17168 (Td, Discrim_Values, Stored_Discrim_Values);
17169 end if;
17170 end if;
17171 end;
17172 end if;
17173
17174 -- Extra underlying places to search, if not found above. For
17175 -- concurrent types, the relevant discriminant appears in the
17176 -- corresponding record. For a type derived from a private type
17177 -- without discriminant, the full view inherits the discriminants
17178 -- of the full view of the parent.
17179
17180 if Result = Discriminant then
17181 if Is_Concurrent_Type (Ti)
17182 and then Present (Corresponding_Record_Type (Ti))
17183 then
17184 Result :=
17185 Search_Derivation_Levels (
17186 Corresponding_Record_Type (Ti),
17187 Discrim_Values,
17188 Stored_Discrim_Values);
17189
17190 elsif Is_Private_Type (Ti)
17191 and then not Has_Discriminants (Ti)
17192 and then Present (Full_View (Ti))
17193 and then Etype (Full_View (Ti)) /= Ti
17194 then
17195 Result :=
17196 Search_Derivation_Levels (
17197 Full_View (Ti),
17198 Discrim_Values,
17199 Stored_Discrim_Values);
17200 end if;
17201 end if;
17202
17203 -- If Result is not a (reference to a) discriminant, return it,
17204 -- otherwise set Result_Entity to the discriminant.
17205
17206 if Nkind (Result) = N_Defining_Identifier then
17207 pragma Assert (Result = Discriminant);
17208 Result_Entity := Result;
17209
17210 else
17211 if not Denotes_Discriminant (Result) then
17212 return Result;
17213 end if;
17214
17215 Result_Entity := Entity (Result);
17216 end if;
17217
17218 -- See if this level of derivation actually has discriminants because
17219 -- tagged derivations can add them, hence the lower levels need not
17220 -- have any.
17221
17222 if not Has_Discriminants (Ti) then
17223 return Result;
17224 end if;
17225
17226 -- Scan Ti's discriminants for Result_Entity, and return its
17227 -- corresponding value, if any.
17228
17229 Result_Entity := Original_Record_Component (Result_Entity);
17230
17231 Assoc := First_Elmt (Discrim_Values);
17232
17233 if Stored_Discrim_Values then
17234 Disc := First_Stored_Discriminant (Ti);
17235 else
17236 Disc := First_Discriminant (Ti);
17237 end if;
17238
17239 while Present (Disc) loop
17240 pragma Assert (Present (Assoc));
17241
17242 if Original_Record_Component (Disc) = Result_Entity then
17243 return Node (Assoc);
17244 end if;
17245
17246 Next_Elmt (Assoc);
17247
17248 if Stored_Discrim_Values then
17249 Next_Stored_Discriminant (Disc);
17250 else
17251 Next_Discriminant (Disc);
17252 end if;
17253 end loop;
17254
17255 -- Could not find it
17256
17257 return Result;
17258 end Search_Derivation_Levels;
17259
17260 -- Local Variables
17261
17262 Result : Node_Or_Entity_Id;
17263
17264 -- Start of processing for Get_Discriminant_Value
17265
17266 begin
17267 -- ??? This routine is a gigantic mess and will be deleted. For the
17268 -- time being just test for the trivial case before calling recurse.
17269
17270 if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
17271 declare
17272 D : Entity_Id;
17273 E : Elmt_Id;
17274
17275 begin
17276 D := First_Discriminant (Typ_For_Constraint);
17277 E := First_Elmt (Constraint);
17278 while Present (D) loop
17279 if Chars (D) = Chars (Discriminant) then
17280 return Node (E);
17281 end if;
17282
17283 Next_Discriminant (D);
17284 Next_Elmt (E);
17285 end loop;
17286 end;
17287 end if;
17288
17289 Result := Search_Derivation_Levels
17290 (Typ_For_Constraint, Constraint, False);
17291
17292 -- ??? hack to disappear when this routine is gone
17293
17294 if Nkind (Result) = N_Defining_Identifier then
17295 declare
17296 D : Entity_Id;
17297 E : Elmt_Id;
17298
17299 begin
17300 D := First_Discriminant (Typ_For_Constraint);
17301 E := First_Elmt (Constraint);
17302 while Present (D) loop
17303 if Root_Corresponding_Discriminant (D) = Discriminant then
17304 return Node (E);
17305 end if;
17306
17307 Next_Discriminant (D);
17308 Next_Elmt (E);
17309 end loop;
17310 end;
17311 end if;
17312
17313 pragma Assert (Nkind (Result) /= N_Defining_Identifier);
17314 return Result;
17315 end Get_Discriminant_Value;
17316
17317 --------------------------
17318 -- Has_Range_Constraint --
17319 --------------------------
17320
17321 function Has_Range_Constraint (N : Node_Id) return Boolean is
17322 C : constant Node_Id := Constraint (N);
17323
17324 begin
17325 if Nkind (C) = N_Range_Constraint then
17326 return True;
17327
17328 elsif Nkind (C) = N_Digits_Constraint then
17329 return
17330 Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
17331 or else Present (Range_Constraint (C));
17332
17333 elsif Nkind (C) = N_Delta_Constraint then
17334 return Present (Range_Constraint (C));
17335
17336 else
17337 return False;
17338 end if;
17339 end Has_Range_Constraint;
17340
17341 ------------------------
17342 -- Inherit_Components --
17343 ------------------------
17344
17345 function Inherit_Components
17346 (N : Node_Id;
17347 Parent_Base : Entity_Id;
17348 Derived_Base : Entity_Id;
17349 Is_Tagged : Boolean;
17350 Inherit_Discr : Boolean;
17351 Discs : Elist_Id) return Elist_Id
17352 is
17353 Assoc_List : constant Elist_Id := New_Elmt_List;
17354
17355 procedure Inherit_Component
17356 (Old_C : Entity_Id;
17357 Plain_Discrim : Boolean := False;
17358 Stored_Discrim : Boolean := False);
17359 -- Inherits component Old_C from Parent_Base to the Derived_Base. If
17360 -- Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
17361 -- True, Old_C is a stored discriminant. If they are both false then
17362 -- Old_C is a regular component.
17363
17364 -----------------------
17365 -- Inherit_Component --
17366 -----------------------
17367
17368 procedure Inherit_Component
17369 (Old_C : Entity_Id;
17370 Plain_Discrim : Boolean := False;
17371 Stored_Discrim : Boolean := False)
17372 is
17373 procedure Set_Anonymous_Type (Id : Entity_Id);
17374 -- Id denotes the entity of an access discriminant or anonymous
17375 -- access component. Set the type of Id to either the same type of
17376 -- Old_C or create a new one depending on whether the parent and
17377 -- the child types are in the same scope.
17378
17379 ------------------------
17380 -- Set_Anonymous_Type --
17381 ------------------------
17382
17383 procedure Set_Anonymous_Type (Id : Entity_Id) is
17384 Old_Typ : constant Entity_Id := Etype (Old_C);
17385
17386 begin
17387 if Scope (Parent_Base) = Scope (Derived_Base) then
17388 Set_Etype (Id, Old_Typ);
17389
17390 -- The parent and the derived type are in two different scopes.
17391 -- Reuse the type of the original discriminant / component by
17392 -- copying it in order to preserve all attributes.
17393
17394 else
17395 declare
17396 Typ : constant Entity_Id := New_Copy (Old_Typ);
17397
17398 begin
17399 Set_Etype (Id, Typ);
17400
17401 -- Since we do not generate component declarations for
17402 -- inherited components, associate the itype with the
17403 -- derived type.
17404
17405 Set_Associated_Node_For_Itype (Typ, Parent (Derived_Base));
17406 Set_Scope (Typ, Derived_Base);
17407 end;
17408 end if;
17409 end Set_Anonymous_Type;
17410
17411 -- Local variables and constants
17412
17413 New_C : constant Entity_Id := New_Copy (Old_C);
17414
17415 Corr_Discrim : Entity_Id;
17416 Discrim : Entity_Id;
17417
17418 -- Start of processing for Inherit_Component
17419
17420 begin
17421 pragma Assert (not Is_Tagged or not Stored_Discrim);
17422
17423 Set_Parent (New_C, Parent (Old_C));
17424
17425 -- Regular discriminants and components must be inserted in the scope
17426 -- of the Derived_Base. Do it here.
17427
17428 if not Stored_Discrim then
17429 Enter_Name (New_C);
17430 end if;
17431
17432 -- For tagged types the Original_Record_Component must point to
17433 -- whatever this field was pointing to in the parent type. This has
17434 -- already been achieved by the call to New_Copy above.
17435
17436 if not Is_Tagged then
17437 Set_Original_Record_Component (New_C, New_C);
17438 end if;
17439
17440 -- Set the proper type of an access discriminant
17441
17442 if Ekind (New_C) = E_Discriminant
17443 and then Ekind (Etype (New_C)) = E_Anonymous_Access_Type
17444 then
17445 Set_Anonymous_Type (New_C);
17446 end if;
17447
17448 -- If we have inherited a component then see if its Etype contains
17449 -- references to Parent_Base discriminants. In this case, replace
17450 -- these references with the constraints given in Discs. We do not
17451 -- do this for the partial view of private types because this is
17452 -- not needed (only the components of the full view will be used
17453 -- for code generation) and cause problem. We also avoid this
17454 -- transformation in some error situations.
17455
17456 if Ekind (New_C) = E_Component then
17457
17458 -- Set the proper type of an anonymous access component
17459
17460 if Ekind (Etype (New_C)) = E_Anonymous_Access_Type then
17461 Set_Anonymous_Type (New_C);
17462
17463 elsif (Is_Private_Type (Derived_Base)
17464 and then not Is_Generic_Type (Derived_Base))
17465 or else (Is_Empty_Elmt_List (Discs)
17466 and then not Expander_Active)
17467 then
17468 Set_Etype (New_C, Etype (Old_C));
17469
17470 else
17471 -- The current component introduces a circularity of the
17472 -- following kind:
17473
17474 -- limited with Pack_2;
17475 -- package Pack_1 is
17476 -- type T_1 is tagged record
17477 -- Comp : access Pack_2.T_2;
17478 -- ...
17479 -- end record;
17480 -- end Pack_1;
17481
17482 -- with Pack_1;
17483 -- package Pack_2 is
17484 -- type T_2 is new Pack_1.T_1 with ...;
17485 -- end Pack_2;
17486
17487 Set_Etype
17488 (New_C,
17489 Constrain_Component_Type
17490 (Old_C, Derived_Base, N, Parent_Base, Discs));
17491 end if;
17492 end if;
17493
17494 -- In derived tagged types it is illegal to reference a non
17495 -- discriminant component in the parent type. To catch this, mark
17496 -- these components with an Ekind of E_Void. This will be reset in
17497 -- Record_Type_Definition after processing the record extension of
17498 -- the derived type.
17499
17500 -- If the declaration is a private extension, there is no further
17501 -- record extension to process, and the components retain their
17502 -- current kind, because they are visible at this point.
17503
17504 if Is_Tagged and then Ekind (New_C) = E_Component
17505 and then Nkind (N) /= N_Private_Extension_Declaration
17506 then
17507 Set_Ekind (New_C, E_Void);
17508 end if;
17509
17510 if Plain_Discrim then
17511 Set_Corresponding_Discriminant (New_C, Old_C);
17512 Build_Discriminal (New_C);
17513
17514 -- If we are explicitly inheriting a stored discriminant it will be
17515 -- completely hidden.
17516
17517 elsif Stored_Discrim then
17518 Set_Corresponding_Discriminant (New_C, Empty);
17519 Set_Discriminal (New_C, Empty);
17520 Set_Is_Completely_Hidden (New_C);
17521
17522 -- Set the Original_Record_Component of each discriminant in the
17523 -- derived base to point to the corresponding stored that we just
17524 -- created.
17525
17526 Discrim := First_Discriminant (Derived_Base);
17527 while Present (Discrim) loop
17528 Corr_Discrim := Corresponding_Discriminant (Discrim);
17529
17530 -- Corr_Discrim could be missing in an error situation
17531
17532 if Present (Corr_Discrim)
17533 and then Original_Record_Component (Corr_Discrim) = Old_C
17534 then
17535 Set_Original_Record_Component (Discrim, New_C);
17536 end if;
17537
17538 Next_Discriminant (Discrim);
17539 end loop;
17540
17541 Append_Entity (New_C, Derived_Base);
17542 end if;
17543
17544 if not Is_Tagged then
17545 Append_Elmt (Old_C, Assoc_List);
17546 Append_Elmt (New_C, Assoc_List);
17547 end if;
17548 end Inherit_Component;
17549
17550 -- Variables local to Inherit_Component
17551
17552 Loc : constant Source_Ptr := Sloc (N);
17553
17554 Parent_Discrim : Entity_Id;
17555 Stored_Discrim : Entity_Id;
17556 D : Entity_Id;
17557 Component : Entity_Id;
17558
17559 -- Start of processing for Inherit_Components
17560
17561 begin
17562 if not Is_Tagged then
17563 Append_Elmt (Parent_Base, Assoc_List);
17564 Append_Elmt (Derived_Base, Assoc_List);
17565 end if;
17566
17567 -- Inherit parent discriminants if needed
17568
17569 if Inherit_Discr then
17570 Parent_Discrim := First_Discriminant (Parent_Base);
17571 while Present (Parent_Discrim) loop
17572 Inherit_Component (Parent_Discrim, Plain_Discrim => True);
17573 Next_Discriminant (Parent_Discrim);
17574 end loop;
17575 end if;
17576
17577 -- Create explicit stored discrims for untagged types when necessary
17578
17579 if not Has_Unknown_Discriminants (Derived_Base)
17580 and then Has_Discriminants (Parent_Base)
17581 and then not Is_Tagged
17582 and then
17583 (not Inherit_Discr
17584 or else First_Discriminant (Parent_Base) /=
17585 First_Stored_Discriminant (Parent_Base))
17586 then
17587 Stored_Discrim := First_Stored_Discriminant (Parent_Base);
17588 while Present (Stored_Discrim) loop
17589 Inherit_Component (Stored_Discrim, Stored_Discrim => True);
17590 Next_Stored_Discriminant (Stored_Discrim);
17591 end loop;
17592 end if;
17593
17594 -- See if we can apply the second transformation for derived types, as
17595 -- explained in point 6. in the comments above Build_Derived_Record_Type
17596 -- This is achieved by appending Derived_Base discriminants into Discs,
17597 -- which has the side effect of returning a non empty Discs list to the
17598 -- caller of Inherit_Components, which is what we want. This must be
17599 -- done for private derived types if there are explicit stored
17600 -- discriminants, to ensure that we can retrieve the values of the
17601 -- constraints provided in the ancestors.
17602
17603 if Inherit_Discr
17604 and then Is_Empty_Elmt_List (Discs)
17605 and then Present (First_Discriminant (Derived_Base))
17606 and then
17607 (not Is_Private_Type (Derived_Base)
17608 or else Is_Completely_Hidden
17609 (First_Stored_Discriminant (Derived_Base))
17610 or else Is_Generic_Type (Derived_Base))
17611 then
17612 D := First_Discriminant (Derived_Base);
17613 while Present (D) loop
17614 Append_Elmt (New_Occurrence_Of (D, Loc), Discs);
17615 Next_Discriminant (D);
17616 end loop;
17617 end if;
17618
17619 -- Finally, inherit non-discriminant components unless they are not
17620 -- visible because defined or inherited from the full view of the
17621 -- parent. Don't inherit the _parent field of the parent type.
17622
17623 Component := First_Entity (Parent_Base);
17624 while Present (Component) loop
17625
17626 -- Ada 2005 (AI-251): Do not inherit components associated with
17627 -- secondary tags of the parent.
17628
17629 if Ekind (Component) = E_Component
17630 and then Present (Related_Type (Component))
17631 then
17632 null;
17633
17634 elsif Ekind (Component) /= E_Component
17635 or else Chars (Component) = Name_uParent
17636 then
17637 null;
17638
17639 -- If the derived type is within the parent type's declarative
17640 -- region, then the components can still be inherited even though
17641 -- they aren't visible at this point. This can occur for cases
17642 -- such as within public child units where the components must
17643 -- become visible upon entering the child unit's private part.
17644
17645 elsif not Is_Visible_Component (Component)
17646 and then not In_Open_Scopes (Scope (Parent_Base))
17647 then
17648 null;
17649
17650 elsif Ekind_In (Derived_Base, E_Private_Type,
17651 E_Limited_Private_Type)
17652 then
17653 null;
17654
17655 else
17656 Inherit_Component (Component);
17657 end if;
17658
17659 Next_Entity (Component);
17660 end loop;
17661
17662 -- For tagged derived types, inherited discriminants cannot be used in
17663 -- component declarations of the record extension part. To achieve this
17664 -- we mark the inherited discriminants as not visible.
17665
17666 if Is_Tagged and then Inherit_Discr then
17667 D := First_Discriminant (Derived_Base);
17668 while Present (D) loop
17669 Set_Is_Immediately_Visible (D, False);
17670 Next_Discriminant (D);
17671 end loop;
17672 end if;
17673
17674 return Assoc_List;
17675 end Inherit_Components;
17676
17677 -----------------------------
17678 -- Inherit_Predicate_Flags --
17679 -----------------------------
17680
17681 procedure Inherit_Predicate_Flags (Subt, Par : Entity_Id) is
17682 begin
17683 Set_Has_Predicates (Subt, Has_Predicates (Par));
17684 Set_Has_Static_Predicate_Aspect
17685 (Subt, Has_Static_Predicate_Aspect (Par));
17686 Set_Has_Dynamic_Predicate_Aspect
17687 (Subt, Has_Dynamic_Predicate_Aspect (Par));
17688 end Inherit_Predicate_Flags;
17689
17690 ----------------------
17691 -- Is_EVF_Procedure --
17692 ----------------------
17693
17694 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean is
17695 Formal : Entity_Id;
17696
17697 begin
17698 -- Examine the formals of an Extensions_Visible False procedure looking
17699 -- for a controlling OUT parameter.
17700
17701 if Ekind (Subp) = E_Procedure
17702 and then Extensions_Visible_Status (Subp) = Extensions_Visible_False
17703 then
17704 Formal := First_Formal (Subp);
17705 while Present (Formal) loop
17706 if Ekind (Formal) = E_Out_Parameter
17707 and then Is_Controlling_Formal (Formal)
17708 then
17709 return True;
17710 end if;
17711
17712 Next_Formal (Formal);
17713 end loop;
17714 end if;
17715
17716 return False;
17717 end Is_EVF_Procedure;
17718
17719 -----------------------
17720 -- Is_Null_Extension --
17721 -----------------------
17722
17723 function Is_Null_Extension (T : Entity_Id) return Boolean is
17724 Type_Decl : constant Node_Id := Parent (Base_Type (T));
17725 Comp_List : Node_Id;
17726 Comp : Node_Id;
17727
17728 begin
17729 if Nkind (Type_Decl) /= N_Full_Type_Declaration
17730 or else not Is_Tagged_Type (T)
17731 or else Nkind (Type_Definition (Type_Decl)) /=
17732 N_Derived_Type_Definition
17733 or else No (Record_Extension_Part (Type_Definition (Type_Decl)))
17734 then
17735 return False;
17736 end if;
17737
17738 Comp_List :=
17739 Component_List (Record_Extension_Part (Type_Definition (Type_Decl)));
17740
17741 if Present (Discriminant_Specifications (Type_Decl)) then
17742 return False;
17743
17744 elsif Present (Comp_List)
17745 and then Is_Non_Empty_List (Component_Items (Comp_List))
17746 then
17747 Comp := First (Component_Items (Comp_List));
17748
17749 -- Only user-defined components are relevant. The component list
17750 -- may also contain a parent component and internal components
17751 -- corresponding to secondary tags, but these do not determine
17752 -- whether this is a null extension.
17753
17754 while Present (Comp) loop
17755 if Comes_From_Source (Comp) then
17756 return False;
17757 end if;
17758
17759 Next (Comp);
17760 end loop;
17761
17762 return True;
17763
17764 else
17765 return True;
17766 end if;
17767 end Is_Null_Extension;
17768
17769 ------------------------------
17770 -- Is_Valid_Constraint_Kind --
17771 ------------------------------
17772
17773 function Is_Valid_Constraint_Kind
17774 (T_Kind : Type_Kind;
17775 Constraint_Kind : Node_Kind) return Boolean
17776 is
17777 begin
17778 case T_Kind is
17779 when Enumeration_Kind |
17780 Integer_Kind =>
17781 return Constraint_Kind = N_Range_Constraint;
17782
17783 when Decimal_Fixed_Point_Kind =>
17784 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
17785 N_Range_Constraint);
17786
17787 when Ordinary_Fixed_Point_Kind =>
17788 return Nkind_In (Constraint_Kind, N_Delta_Constraint,
17789 N_Range_Constraint);
17790
17791 when Float_Kind =>
17792 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
17793 N_Range_Constraint);
17794
17795 when Access_Kind |
17796 Array_Kind |
17797 E_Record_Type |
17798 E_Record_Subtype |
17799 Class_Wide_Kind |
17800 E_Incomplete_Type |
17801 Private_Kind |
17802 Concurrent_Kind =>
17803 return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
17804
17805 when others =>
17806 return True; -- Error will be detected later
17807 end case;
17808 end Is_Valid_Constraint_Kind;
17809
17810 --------------------------
17811 -- Is_Visible_Component --
17812 --------------------------
17813
17814 function Is_Visible_Component
17815 (C : Entity_Id;
17816 N : Node_Id := Empty) return Boolean
17817 is
17818 Original_Comp : Entity_Id := Empty;
17819 Original_Scope : Entity_Id;
17820 Type_Scope : Entity_Id;
17821
17822 function Is_Local_Type (Typ : Entity_Id) return Boolean;
17823 -- Check whether parent type of inherited component is declared locally,
17824 -- possibly within a nested package or instance. The current scope is
17825 -- the derived record itself.
17826
17827 -------------------
17828 -- Is_Local_Type --
17829 -------------------
17830
17831 function Is_Local_Type (Typ : Entity_Id) return Boolean is
17832 Scop : Entity_Id;
17833
17834 begin
17835 Scop := Scope (Typ);
17836 while Present (Scop)
17837 and then Scop /= Standard_Standard
17838 loop
17839 if Scop = Scope (Current_Scope) then
17840 return True;
17841 end if;
17842
17843 Scop := Scope (Scop);
17844 end loop;
17845
17846 return False;
17847 end Is_Local_Type;
17848
17849 -- Start of processing for Is_Visible_Component
17850
17851 begin
17852 if Ekind_In (C, E_Component, E_Discriminant) then
17853 Original_Comp := Original_Record_Component (C);
17854 end if;
17855
17856 if No (Original_Comp) then
17857
17858 -- Premature usage, or previous error
17859
17860 return False;
17861
17862 else
17863 Original_Scope := Scope (Original_Comp);
17864 Type_Scope := Scope (Base_Type (Scope (C)));
17865 end if;
17866
17867 -- This test only concerns tagged types
17868
17869 if not Is_Tagged_Type (Original_Scope) then
17870 return True;
17871
17872 -- If it is _Parent or _Tag, there is no visibility issue
17873
17874 elsif not Comes_From_Source (Original_Comp) then
17875 return True;
17876
17877 -- Discriminants are visible unless the (private) type has unknown
17878 -- discriminants. If the discriminant reference is inserted for a
17879 -- discriminant check on a full view it is also visible.
17880
17881 elsif Ekind (Original_Comp) = E_Discriminant
17882 and then
17883 (not Has_Unknown_Discriminants (Original_Scope)
17884 or else (Present (N)
17885 and then Nkind (N) = N_Selected_Component
17886 and then Nkind (Prefix (N)) = N_Type_Conversion
17887 and then not Comes_From_Source (Prefix (N))))
17888 then
17889 return True;
17890
17891 -- In the body of an instantiation, no need to check for the visibility
17892 -- of a component.
17893
17894 elsif In_Instance_Body then
17895 return True;
17896
17897 -- If the component has been declared in an ancestor which is currently
17898 -- a private type, then it is not visible. The same applies if the
17899 -- component's containing type is not in an open scope and the original
17900 -- component's enclosing type is a visible full view of a private type
17901 -- (which can occur in cases where an attempt is being made to reference
17902 -- a component in a sibling package that is inherited from a visible
17903 -- component of a type in an ancestor package; the component in the
17904 -- sibling package should not be visible even though the component it
17905 -- inherited from is visible). This does not apply however in the case
17906 -- where the scope of the type is a private child unit, or when the
17907 -- parent comes from a local package in which the ancestor is currently
17908 -- visible. The latter suppression of visibility is needed for cases
17909 -- that are tested in B730006.
17910
17911 elsif Is_Private_Type (Original_Scope)
17912 or else
17913 (not Is_Private_Descendant (Type_Scope)
17914 and then not In_Open_Scopes (Type_Scope)
17915 and then Has_Private_Declaration (Original_Scope))
17916 then
17917 -- If the type derives from an entity in a formal package, there
17918 -- are no additional visible components.
17919
17920 if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
17921 N_Formal_Package_Declaration
17922 then
17923 return False;
17924
17925 -- if we are not in the private part of the current package, there
17926 -- are no additional visible components.
17927
17928 elsif Ekind (Scope (Current_Scope)) = E_Package
17929 and then not In_Private_Part (Scope (Current_Scope))
17930 then
17931 return False;
17932 else
17933 return
17934 Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
17935 and then In_Open_Scopes (Scope (Original_Scope))
17936 and then Is_Local_Type (Type_Scope);
17937 end if;
17938
17939 -- There is another weird way in which a component may be invisible when
17940 -- the private and the full view are not derived from the same ancestor.
17941 -- Here is an example :
17942
17943 -- type A1 is tagged record F1 : integer; end record;
17944 -- type A2 is new A1 with record F2 : integer; end record;
17945 -- type T is new A1 with private;
17946 -- private
17947 -- type T is new A2 with null record;
17948
17949 -- In this case, the full view of T inherits F1 and F2 but the private
17950 -- view inherits only F1
17951
17952 else
17953 declare
17954 Ancestor : Entity_Id := Scope (C);
17955
17956 begin
17957 loop
17958 if Ancestor = Original_Scope then
17959 return True;
17960 elsif Ancestor = Etype (Ancestor) then
17961 return False;
17962 end if;
17963
17964 Ancestor := Etype (Ancestor);
17965 end loop;
17966 end;
17967 end if;
17968 end Is_Visible_Component;
17969
17970 --------------------------
17971 -- Make_Class_Wide_Type --
17972 --------------------------
17973
17974 procedure Make_Class_Wide_Type (T : Entity_Id) is
17975 CW_Type : Entity_Id;
17976 CW_Name : Name_Id;
17977 Next_E : Entity_Id;
17978
17979 begin
17980 if Present (Class_Wide_Type (T)) then
17981
17982 -- The class-wide type is a partially decorated entity created for a
17983 -- unanalyzed tagged type referenced through a limited with clause.
17984 -- When the tagged type is analyzed, its class-wide type needs to be
17985 -- redecorated. Note that we reuse the entity created by Decorate_
17986 -- Tagged_Type in order to preserve all links.
17987
17988 if Materialize_Entity (Class_Wide_Type (T)) then
17989 CW_Type := Class_Wide_Type (T);
17990 Set_Materialize_Entity (CW_Type, False);
17991
17992 -- The class wide type can have been defined by the partial view, in
17993 -- which case everything is already done.
17994
17995 else
17996 return;
17997 end if;
17998
17999 -- Default case, we need to create a new class-wide type
18000
18001 else
18002 CW_Type :=
18003 New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
18004 end if;
18005
18006 -- Inherit root type characteristics
18007
18008 CW_Name := Chars (CW_Type);
18009 Next_E := Next_Entity (CW_Type);
18010 Copy_Node (T, CW_Type);
18011 Set_Comes_From_Source (CW_Type, False);
18012 Set_Chars (CW_Type, CW_Name);
18013 Set_Parent (CW_Type, Parent (T));
18014 Set_Next_Entity (CW_Type, Next_E);
18015
18016 -- Ensure we have a new freeze node for the class-wide type. The partial
18017 -- view may have freeze action of its own, requiring a proper freeze
18018 -- node, and the same freeze node cannot be shared between the two
18019 -- types.
18020
18021 Set_Has_Delayed_Freeze (CW_Type);
18022 Set_Freeze_Node (CW_Type, Empty);
18023
18024 -- Customize the class-wide type: It has no prim. op., it cannot be
18025 -- abstract and its Etype points back to the specific root type.
18026
18027 Set_Ekind (CW_Type, E_Class_Wide_Type);
18028 Set_Is_Tagged_Type (CW_Type, True);
18029 Set_Direct_Primitive_Operations (CW_Type, New_Elmt_List);
18030 Set_Is_Abstract_Type (CW_Type, False);
18031 Set_Is_Constrained (CW_Type, False);
18032 Set_Is_First_Subtype (CW_Type, Is_First_Subtype (T));
18033 Set_Default_SSO (CW_Type);
18034
18035 if Ekind (T) = E_Class_Wide_Subtype then
18036 Set_Etype (CW_Type, Etype (Base_Type (T)));
18037 else
18038 Set_Etype (CW_Type, T);
18039 end if;
18040
18041 Set_No_Tagged_Streams_Pragma (CW_Type, No_Tagged_Streams);
18042
18043 -- If this is the class_wide type of a constrained subtype, it does
18044 -- not have discriminants.
18045
18046 Set_Has_Discriminants (CW_Type,
18047 Has_Discriminants (T) and then not Is_Constrained (T));
18048
18049 Set_Has_Unknown_Discriminants (CW_Type, True);
18050 Set_Class_Wide_Type (T, CW_Type);
18051 Set_Equivalent_Type (CW_Type, Empty);
18052
18053 -- The class-wide type of a class-wide type is itself (RM 3.9(14))
18054
18055 Set_Class_Wide_Type (CW_Type, CW_Type);
18056 end Make_Class_Wide_Type;
18057
18058 ----------------
18059 -- Make_Index --
18060 ----------------
18061
18062 procedure Make_Index
18063 (N : Node_Id;
18064 Related_Nod : Node_Id;
18065 Related_Id : Entity_Id := Empty;
18066 Suffix_Index : Nat := 1;
18067 In_Iter_Schm : Boolean := False)
18068 is
18069 R : Node_Id;
18070 T : Entity_Id;
18071 Def_Id : Entity_Id := Empty;
18072 Found : Boolean := False;
18073
18074 begin
18075 -- For a discrete range used in a constrained array definition and
18076 -- defined by a range, an implicit conversion to the predefined type
18077 -- INTEGER is assumed if each bound is either a numeric literal, a named
18078 -- number, or an attribute, and the type of both bounds (prior to the
18079 -- implicit conversion) is the type universal_integer. Otherwise, both
18080 -- bounds must be of the same discrete type, other than universal
18081 -- integer; this type must be determinable independently of the
18082 -- context, but using the fact that the type must be discrete and that
18083 -- both bounds must have the same type.
18084
18085 -- Character literals also have a universal type in the absence of
18086 -- of additional context, and are resolved to Standard_Character.
18087
18088 if Nkind (N) = N_Range then
18089
18090 -- The index is given by a range constraint. The bounds are known
18091 -- to be of a consistent type.
18092
18093 if not Is_Overloaded (N) then
18094 T := Etype (N);
18095
18096 -- For universal bounds, choose the specific predefined type
18097
18098 if T = Universal_Integer then
18099 T := Standard_Integer;
18100
18101 elsif T = Any_Character then
18102 Ambiguous_Character (Low_Bound (N));
18103
18104 T := Standard_Character;
18105 end if;
18106
18107 -- The node may be overloaded because some user-defined operators
18108 -- are available, but if a universal interpretation exists it is
18109 -- also the selected one.
18110
18111 elsif Universal_Interpretation (N) = Universal_Integer then
18112 T := Standard_Integer;
18113
18114 else
18115 T := Any_Type;
18116
18117 declare
18118 Ind : Interp_Index;
18119 It : Interp;
18120
18121 begin
18122 Get_First_Interp (N, Ind, It);
18123 while Present (It.Typ) loop
18124 if Is_Discrete_Type (It.Typ) then
18125
18126 if Found
18127 and then not Covers (It.Typ, T)
18128 and then not Covers (T, It.Typ)
18129 then
18130 Error_Msg_N ("ambiguous bounds in discrete range", N);
18131 exit;
18132 else
18133 T := It.Typ;
18134 Found := True;
18135 end if;
18136 end if;
18137
18138 Get_Next_Interp (Ind, It);
18139 end loop;
18140
18141 if T = Any_Type then
18142 Error_Msg_N ("discrete type required for range", N);
18143 Set_Etype (N, Any_Type);
18144 return;
18145
18146 elsif T = Universal_Integer then
18147 T := Standard_Integer;
18148 end if;
18149 end;
18150 end if;
18151
18152 if not Is_Discrete_Type (T) then
18153 Error_Msg_N ("discrete type required for range", N);
18154 Set_Etype (N, Any_Type);
18155 return;
18156 end if;
18157
18158 if Nkind (Low_Bound (N)) = N_Attribute_Reference
18159 and then Attribute_Name (Low_Bound (N)) = Name_First
18160 and then Is_Entity_Name (Prefix (Low_Bound (N)))
18161 and then Is_Type (Entity (Prefix (Low_Bound (N))))
18162 and then Is_Discrete_Type (Entity (Prefix (Low_Bound (N))))
18163 then
18164 -- The type of the index will be the type of the prefix, as long
18165 -- as the upper bound is 'Last of the same type.
18166
18167 Def_Id := Entity (Prefix (Low_Bound (N)));
18168
18169 if Nkind (High_Bound (N)) /= N_Attribute_Reference
18170 or else Attribute_Name (High_Bound (N)) /= Name_Last
18171 or else not Is_Entity_Name (Prefix (High_Bound (N)))
18172 or else Entity (Prefix (High_Bound (N))) /= Def_Id
18173 then
18174 Def_Id := Empty;
18175 end if;
18176 end if;
18177
18178 R := N;
18179 Process_Range_Expr_In_Decl (R, T, In_Iter_Schm => In_Iter_Schm);
18180
18181 elsif Nkind (N) = N_Subtype_Indication then
18182
18183 -- The index is given by a subtype with a range constraint
18184
18185 T := Base_Type (Entity (Subtype_Mark (N)));
18186
18187 if not Is_Discrete_Type (T) then
18188 Error_Msg_N ("discrete type required for range", N);
18189 Set_Etype (N, Any_Type);
18190 return;
18191 end if;
18192
18193 R := Range_Expression (Constraint (N));
18194
18195 Resolve (R, T);
18196 Process_Range_Expr_In_Decl
18197 (R, Entity (Subtype_Mark (N)), In_Iter_Schm => In_Iter_Schm);
18198
18199 elsif Nkind (N) = N_Attribute_Reference then
18200
18201 -- Catch beginner's error (use of attribute other than 'Range)
18202
18203 if Attribute_Name (N) /= Name_Range then
18204 Error_Msg_N ("expect attribute ''Range", N);
18205 Set_Etype (N, Any_Type);
18206 return;
18207 end if;
18208
18209 -- If the node denotes the range of a type mark, that is also the
18210 -- resulting type, and we do not need to create an Itype for it.
18211
18212 if Is_Entity_Name (Prefix (N))
18213 and then Comes_From_Source (N)
18214 and then Is_Type (Entity (Prefix (N)))
18215 and then Is_Discrete_Type (Entity (Prefix (N)))
18216 then
18217 Def_Id := Entity (Prefix (N));
18218 end if;
18219
18220 Analyze_And_Resolve (N);
18221 T := Etype (N);
18222 R := N;
18223
18224 -- If none of the above, must be a subtype. We convert this to a
18225 -- range attribute reference because in the case of declared first
18226 -- named subtypes, the types in the range reference can be different
18227 -- from the type of the entity. A range attribute normalizes the
18228 -- reference and obtains the correct types for the bounds.
18229
18230 -- This transformation is in the nature of an expansion, is only
18231 -- done if expansion is active. In particular, it is not done on
18232 -- formal generic types, because we need to retain the name of the
18233 -- original index for instantiation purposes.
18234
18235 else
18236 if not Is_Entity_Name (N) or else not Is_Type (Entity (N)) then
18237 Error_Msg_N ("invalid subtype mark in discrete range ", N);
18238 Set_Etype (N, Any_Integer);
18239 return;
18240
18241 else
18242 -- The type mark may be that of an incomplete type. It is only
18243 -- now that we can get the full view, previous analysis does
18244 -- not look specifically for a type mark.
18245
18246 Set_Entity (N, Get_Full_View (Entity (N)));
18247 Set_Etype (N, Entity (N));
18248 Def_Id := Entity (N);
18249
18250 if not Is_Discrete_Type (Def_Id) then
18251 Error_Msg_N ("discrete type required for index", N);
18252 Set_Etype (N, Any_Type);
18253 return;
18254 end if;
18255 end if;
18256
18257 if Expander_Active then
18258 Rewrite (N,
18259 Make_Attribute_Reference (Sloc (N),
18260 Attribute_Name => Name_Range,
18261 Prefix => Relocate_Node (N)));
18262
18263 -- The original was a subtype mark that does not freeze. This
18264 -- means that the rewritten version must not freeze either.
18265
18266 Set_Must_Not_Freeze (N);
18267 Set_Must_Not_Freeze (Prefix (N));
18268 Analyze_And_Resolve (N);
18269 T := Etype (N);
18270 R := N;
18271
18272 -- If expander is inactive, type is legal, nothing else to construct
18273
18274 else
18275 return;
18276 end if;
18277 end if;
18278
18279 if not Is_Discrete_Type (T) then
18280 Error_Msg_N ("discrete type required for range", N);
18281 Set_Etype (N, Any_Type);
18282 return;
18283
18284 elsif T = Any_Type then
18285 Set_Etype (N, Any_Type);
18286 return;
18287 end if;
18288
18289 -- We will now create the appropriate Itype to describe the range, but
18290 -- first a check. If we originally had a subtype, then we just label
18291 -- the range with this subtype. Not only is there no need to construct
18292 -- a new subtype, but it is wrong to do so for two reasons:
18293
18294 -- 1. A legality concern, if we have a subtype, it must not freeze,
18295 -- and the Itype would cause freezing incorrectly
18296
18297 -- 2. An efficiency concern, if we created an Itype, it would not be
18298 -- recognized as the same type for the purposes of eliminating
18299 -- checks in some circumstances.
18300
18301 -- We signal this case by setting the subtype entity in Def_Id
18302
18303 if No (Def_Id) then
18304 Def_Id :=
18305 Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
18306 Set_Etype (Def_Id, Base_Type (T));
18307
18308 if Is_Signed_Integer_Type (T) then
18309 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
18310
18311 elsif Is_Modular_Integer_Type (T) then
18312 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
18313
18314 else
18315 Set_Ekind (Def_Id, E_Enumeration_Subtype);
18316 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
18317 Set_First_Literal (Def_Id, First_Literal (T));
18318 end if;
18319
18320 Set_Size_Info (Def_Id, (T));
18321 Set_RM_Size (Def_Id, RM_Size (T));
18322 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
18323
18324 Set_Scalar_Range (Def_Id, R);
18325 Conditional_Delay (Def_Id, T);
18326
18327 if Nkind (N) = N_Subtype_Indication then
18328 Inherit_Predicate_Flags (Def_Id, Entity (Subtype_Mark (N)));
18329 end if;
18330
18331 -- In the subtype indication case, if the immediate parent of the
18332 -- new subtype is non-static, then the subtype we create is non-
18333 -- static, even if its bounds are static.
18334
18335 if Nkind (N) = N_Subtype_Indication
18336 and then not Is_OK_Static_Subtype (Entity (Subtype_Mark (N)))
18337 then
18338 Set_Is_Non_Static_Subtype (Def_Id);
18339 end if;
18340 end if;
18341
18342 -- Final step is to label the index with this constructed type
18343
18344 Set_Etype (N, Def_Id);
18345 end Make_Index;
18346
18347 ------------------------------
18348 -- Modular_Type_Declaration --
18349 ------------------------------
18350
18351 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
18352 Mod_Expr : constant Node_Id := Expression (Def);
18353 M_Val : Uint;
18354
18355 procedure Set_Modular_Size (Bits : Int);
18356 -- Sets RM_Size to Bits, and Esize to normal word size above this
18357
18358 ----------------------
18359 -- Set_Modular_Size --
18360 ----------------------
18361
18362 procedure Set_Modular_Size (Bits : Int) is
18363 begin
18364 Set_RM_Size (T, UI_From_Int (Bits));
18365
18366 if Bits <= 8 then
18367 Init_Esize (T, 8);
18368
18369 elsif Bits <= 16 then
18370 Init_Esize (T, 16);
18371
18372 elsif Bits <= 32 then
18373 Init_Esize (T, 32);
18374
18375 else
18376 Init_Esize (T, System_Max_Binary_Modulus_Power);
18377 end if;
18378
18379 if not Non_Binary_Modulus (T) and then Esize (T) = RM_Size (T) then
18380 Set_Is_Known_Valid (T);
18381 end if;
18382 end Set_Modular_Size;
18383
18384 -- Start of processing for Modular_Type_Declaration
18385
18386 begin
18387 -- If the mod expression is (exactly) 2 * literal, where literal is
18388 -- 64 or less,then almost certainly the * was meant to be **. Warn.
18389
18390 if Warn_On_Suspicious_Modulus_Value
18391 and then Nkind (Mod_Expr) = N_Op_Multiply
18392 and then Nkind (Left_Opnd (Mod_Expr)) = N_Integer_Literal
18393 and then Intval (Left_Opnd (Mod_Expr)) = Uint_2
18394 and then Nkind (Right_Opnd (Mod_Expr)) = N_Integer_Literal
18395 and then Intval (Right_Opnd (Mod_Expr)) <= Uint_64
18396 then
18397 Error_Msg_N
18398 ("suspicious MOD value, was '*'* intended'??M?", Mod_Expr);
18399 end if;
18400
18401 -- Proceed with analysis of mod expression
18402
18403 Analyze_And_Resolve (Mod_Expr, Any_Integer);
18404 Set_Etype (T, T);
18405 Set_Ekind (T, E_Modular_Integer_Type);
18406 Init_Alignment (T);
18407 Set_Is_Constrained (T);
18408
18409 if not Is_OK_Static_Expression (Mod_Expr) then
18410 Flag_Non_Static_Expr
18411 ("non-static expression used for modular type bound!", Mod_Expr);
18412 M_Val := 2 ** System_Max_Binary_Modulus_Power;
18413 else
18414 M_Val := Expr_Value (Mod_Expr);
18415 end if;
18416
18417 if M_Val < 1 then
18418 Error_Msg_N ("modulus value must be positive", Mod_Expr);
18419 M_Val := 2 ** System_Max_Binary_Modulus_Power;
18420 end if;
18421
18422 if M_Val > 2 ** Standard_Long_Integer_Size then
18423 Check_Restriction (No_Long_Long_Integers, Mod_Expr);
18424 end if;
18425
18426 Set_Modulus (T, M_Val);
18427
18428 -- Create bounds for the modular type based on the modulus given in
18429 -- the type declaration and then analyze and resolve those bounds.
18430
18431 Set_Scalar_Range (T,
18432 Make_Range (Sloc (Mod_Expr),
18433 Low_Bound => Make_Integer_Literal (Sloc (Mod_Expr), 0),
18434 High_Bound => Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
18435
18436 -- Properly analyze the literals for the range. We do this manually
18437 -- because we can't go calling Resolve, since we are resolving these
18438 -- bounds with the type, and this type is certainly not complete yet.
18439
18440 Set_Etype (Low_Bound (Scalar_Range (T)), T);
18441 Set_Etype (High_Bound (Scalar_Range (T)), T);
18442 Set_Is_Static_Expression (Low_Bound (Scalar_Range (T)));
18443 Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
18444
18445 -- Loop through powers of two to find number of bits required
18446
18447 for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
18448
18449 -- Binary case
18450
18451 if M_Val = 2 ** Bits then
18452 Set_Modular_Size (Bits);
18453 return;
18454
18455 -- Non-binary case
18456
18457 elsif M_Val < 2 ** Bits then
18458 Check_SPARK_05_Restriction ("modulus should be a power of 2", T);
18459 Set_Non_Binary_Modulus (T);
18460
18461 if Bits > System_Max_Nonbinary_Modulus_Power then
18462 Error_Msg_Uint_1 :=
18463 UI_From_Int (System_Max_Nonbinary_Modulus_Power);
18464 Error_Msg_F
18465 ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
18466 Set_Modular_Size (System_Max_Binary_Modulus_Power);
18467 return;
18468
18469 else
18470 -- In the non-binary case, set size as per RM 13.3(55)
18471
18472 Set_Modular_Size (Bits);
18473 return;
18474 end if;
18475 end if;
18476
18477 end loop;
18478
18479 -- If we fall through, then the size exceed System.Max_Binary_Modulus
18480 -- so we just signal an error and set the maximum size.
18481
18482 Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
18483 Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
18484
18485 Set_Modular_Size (System_Max_Binary_Modulus_Power);
18486 Init_Alignment (T);
18487
18488 end Modular_Type_Declaration;
18489
18490 --------------------------
18491 -- New_Concatenation_Op --
18492 --------------------------
18493
18494 procedure New_Concatenation_Op (Typ : Entity_Id) is
18495 Loc : constant Source_Ptr := Sloc (Typ);
18496 Op : Entity_Id;
18497
18498 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
18499 -- Create abbreviated declaration for the formal of a predefined
18500 -- Operator 'Op' of type 'Typ'
18501
18502 --------------------
18503 -- Make_Op_Formal --
18504 --------------------
18505
18506 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
18507 Formal : Entity_Id;
18508 begin
18509 Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
18510 Set_Etype (Formal, Typ);
18511 Set_Mechanism (Formal, Default_Mechanism);
18512 return Formal;
18513 end Make_Op_Formal;
18514
18515 -- Start of processing for New_Concatenation_Op
18516
18517 begin
18518 Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
18519
18520 Set_Ekind (Op, E_Operator);
18521 Set_Scope (Op, Current_Scope);
18522 Set_Etype (Op, Typ);
18523 Set_Homonym (Op, Get_Name_Entity_Id (Name_Op_Concat));
18524 Set_Is_Immediately_Visible (Op);
18525 Set_Is_Intrinsic_Subprogram (Op);
18526 Set_Has_Completion (Op);
18527 Append_Entity (Op, Current_Scope);
18528
18529 Set_Name_Entity_Id (Name_Op_Concat, Op);
18530
18531 Append_Entity (Make_Op_Formal (Typ, Op), Op);
18532 Append_Entity (Make_Op_Formal (Typ, Op), Op);
18533 end New_Concatenation_Op;
18534
18535 -------------------------
18536 -- OK_For_Limited_Init --
18537 -------------------------
18538
18539 -- ???Check all calls of this, and compare the conditions under which it's
18540 -- called.
18541
18542 function OK_For_Limited_Init
18543 (Typ : Entity_Id;
18544 Exp : Node_Id) return Boolean
18545 is
18546 begin
18547 return Is_CPP_Constructor_Call (Exp)
18548 or else (Ada_Version >= Ada_2005
18549 and then not Debug_Flag_Dot_L
18550 and then OK_For_Limited_Init_In_05 (Typ, Exp));
18551 end OK_For_Limited_Init;
18552
18553 -------------------------------
18554 -- OK_For_Limited_Init_In_05 --
18555 -------------------------------
18556
18557 function OK_For_Limited_Init_In_05
18558 (Typ : Entity_Id;
18559 Exp : Node_Id) return Boolean
18560 is
18561 begin
18562 -- An object of a limited interface type can be initialized with any
18563 -- expression of a nonlimited descendant type.
18564
18565 if Is_Class_Wide_Type (Typ)
18566 and then Is_Limited_Interface (Typ)
18567 and then not Is_Limited_Type (Etype (Exp))
18568 then
18569 return True;
18570 end if;
18571
18572 -- Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
18573 -- case of limited aggregates (including extension aggregates), and
18574 -- function calls. The function call may have been given in prefixed
18575 -- notation, in which case the original node is an indexed component.
18576 -- If the function is parameterless, the original node was an explicit
18577 -- dereference. The function may also be parameterless, in which case
18578 -- the source node is just an identifier.
18579
18580 case Nkind (Original_Node (Exp)) is
18581 when N_Aggregate | N_Extension_Aggregate | N_Function_Call | N_Op =>
18582 return True;
18583
18584 when N_Identifier =>
18585 return Present (Entity (Original_Node (Exp)))
18586 and then Ekind (Entity (Original_Node (Exp))) = E_Function;
18587
18588 when N_Qualified_Expression =>
18589 return
18590 OK_For_Limited_Init_In_05
18591 (Typ, Expression (Original_Node (Exp)));
18592
18593 -- Ada 2005 (AI-251): If a class-wide interface object is initialized
18594 -- with a function call, the expander has rewritten the call into an
18595 -- N_Type_Conversion node to force displacement of the pointer to
18596 -- reference the component containing the secondary dispatch table.
18597 -- Otherwise a type conversion is not a legal context.
18598 -- A return statement for a build-in-place function returning a
18599 -- synchronized type also introduces an unchecked conversion.
18600
18601 when N_Type_Conversion |
18602 N_Unchecked_Type_Conversion =>
18603 return not Comes_From_Source (Exp)
18604 and then
18605 OK_For_Limited_Init_In_05
18606 (Typ, Expression (Original_Node (Exp)));
18607
18608 when N_Indexed_Component |
18609 N_Selected_Component |
18610 N_Explicit_Dereference =>
18611 return Nkind (Exp) = N_Function_Call;
18612
18613 -- A use of 'Input is a function call, hence allowed. Normally the
18614 -- attribute will be changed to a call, but the attribute by itself
18615 -- can occur with -gnatc.
18616
18617 when N_Attribute_Reference =>
18618 return Attribute_Name (Original_Node (Exp)) = Name_Input;
18619
18620 -- For a case expression, all dependent expressions must be legal
18621
18622 when N_Case_Expression =>
18623 declare
18624 Alt : Node_Id;
18625
18626 begin
18627 Alt := First (Alternatives (Original_Node (Exp)));
18628 while Present (Alt) loop
18629 if not OK_For_Limited_Init_In_05 (Typ, Expression (Alt)) then
18630 return False;
18631 end if;
18632
18633 Next (Alt);
18634 end loop;
18635
18636 return True;
18637 end;
18638
18639 -- For an if expression, all dependent expressions must be legal
18640
18641 when N_If_Expression =>
18642 declare
18643 Then_Expr : constant Node_Id :=
18644 Next (First (Expressions (Original_Node (Exp))));
18645 Else_Expr : constant Node_Id := Next (Then_Expr);
18646 begin
18647 return OK_For_Limited_Init_In_05 (Typ, Then_Expr)
18648 and then
18649 OK_For_Limited_Init_In_05 (Typ, Else_Expr);
18650 end;
18651
18652 when others =>
18653 return False;
18654 end case;
18655 end OK_For_Limited_Init_In_05;
18656
18657 -------------------------------------------
18658 -- Ordinary_Fixed_Point_Type_Declaration --
18659 -------------------------------------------
18660
18661 procedure Ordinary_Fixed_Point_Type_Declaration
18662 (T : Entity_Id;
18663 Def : Node_Id)
18664 is
18665 Loc : constant Source_Ptr := Sloc (Def);
18666 Delta_Expr : constant Node_Id := Delta_Expression (Def);
18667 RRS : constant Node_Id := Real_Range_Specification (Def);
18668 Implicit_Base : Entity_Id;
18669 Delta_Val : Ureal;
18670 Small_Val : Ureal;
18671 Low_Val : Ureal;
18672 High_Val : Ureal;
18673
18674 begin
18675 Check_Restriction (No_Fixed_Point, Def);
18676
18677 -- Create implicit base type
18678
18679 Implicit_Base :=
18680 Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
18681 Set_Etype (Implicit_Base, Implicit_Base);
18682
18683 -- Analyze and process delta expression
18684
18685 Analyze_And_Resolve (Delta_Expr, Any_Real);
18686
18687 Check_Delta_Expression (Delta_Expr);
18688 Delta_Val := Expr_Value_R (Delta_Expr);
18689
18690 Set_Delta_Value (Implicit_Base, Delta_Val);
18691
18692 -- Compute default small from given delta, which is the largest power
18693 -- of two that does not exceed the given delta value.
18694
18695 declare
18696 Tmp : Ureal;
18697 Scale : Int;
18698
18699 begin
18700 Tmp := Ureal_1;
18701 Scale := 0;
18702
18703 if Delta_Val < Ureal_1 then
18704 while Delta_Val < Tmp loop
18705 Tmp := Tmp / Ureal_2;
18706 Scale := Scale + 1;
18707 end loop;
18708
18709 else
18710 loop
18711 Tmp := Tmp * Ureal_2;
18712 exit when Tmp > Delta_Val;
18713 Scale := Scale - 1;
18714 end loop;
18715 end if;
18716
18717 Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
18718 end;
18719
18720 Set_Small_Value (Implicit_Base, Small_Val);
18721
18722 -- If no range was given, set a dummy range
18723
18724 if RRS <= Empty_Or_Error then
18725 Low_Val := -Small_Val;
18726 High_Val := Small_Val;
18727
18728 -- Otherwise analyze and process given range
18729
18730 else
18731 declare
18732 Low : constant Node_Id := Low_Bound (RRS);
18733 High : constant Node_Id := High_Bound (RRS);
18734
18735 begin
18736 Analyze_And_Resolve (Low, Any_Real);
18737 Analyze_And_Resolve (High, Any_Real);
18738 Check_Real_Bound (Low);
18739 Check_Real_Bound (High);
18740
18741 -- Obtain and set the range
18742
18743 Low_Val := Expr_Value_R (Low);
18744 High_Val := Expr_Value_R (High);
18745
18746 if Low_Val > High_Val then
18747 Error_Msg_NE ("??fixed point type& has null range", Def, T);
18748 end if;
18749 end;
18750 end if;
18751
18752 -- The range for both the implicit base and the declared first subtype
18753 -- cannot be set yet, so we use the special routine Set_Fixed_Range to
18754 -- set a temporary range in place. Note that the bounds of the base
18755 -- type will be widened to be symmetrical and to fill the available
18756 -- bits when the type is frozen.
18757
18758 -- We could do this with all discrete types, and probably should, but
18759 -- we absolutely have to do it for fixed-point, since the end-points
18760 -- of the range and the size are determined by the small value, which
18761 -- could be reset before the freeze point.
18762
18763 Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
18764 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
18765
18766 -- Complete definition of first subtype. The inheritance of the rep item
18767 -- chain ensures that SPARK-related pragmas are not clobbered when the
18768 -- ordinary fixed point type acts as a full view of a private type.
18769
18770 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
18771 Set_Etype (T, Implicit_Base);
18772 Init_Size_Align (T);
18773 Inherit_Rep_Item_Chain (T, Implicit_Base);
18774 Set_Small_Value (T, Small_Val);
18775 Set_Delta_Value (T, Delta_Val);
18776 Set_Is_Constrained (T);
18777 end Ordinary_Fixed_Point_Type_Declaration;
18778
18779 ----------------------------------
18780 -- Preanalyze_Assert_Expression --
18781 ----------------------------------
18782
18783 procedure Preanalyze_Assert_Expression (N : Node_Id; T : Entity_Id) is
18784 begin
18785 In_Assertion_Expr := In_Assertion_Expr + 1;
18786 Preanalyze_Spec_Expression (N, T);
18787 In_Assertion_Expr := In_Assertion_Expr - 1;
18788 end Preanalyze_Assert_Expression;
18789
18790 -----------------------------------
18791 -- Preanalyze_Default_Expression --
18792 -----------------------------------
18793
18794 procedure Preanalyze_Default_Expression (N : Node_Id; T : Entity_Id) is
18795 Save_In_Default_Expr : constant Boolean := In_Default_Expr;
18796 begin
18797 In_Default_Expr := True;
18798 Preanalyze_Spec_Expression (N, T);
18799 In_Default_Expr := Save_In_Default_Expr;
18800 end Preanalyze_Default_Expression;
18801
18802 --------------------------------
18803 -- Preanalyze_Spec_Expression --
18804 --------------------------------
18805
18806 procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
18807 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
18808 begin
18809 In_Spec_Expression := True;
18810 Preanalyze_And_Resolve (N, T);
18811 In_Spec_Expression := Save_In_Spec_Expression;
18812 end Preanalyze_Spec_Expression;
18813
18814 ----------------------------------------
18815 -- Prepare_Private_Subtype_Completion --
18816 ----------------------------------------
18817
18818 procedure Prepare_Private_Subtype_Completion
18819 (Id : Entity_Id;
18820 Related_Nod : Node_Id)
18821 is
18822 Id_B : constant Entity_Id := Base_Type (Id);
18823 Full_B : Entity_Id := Full_View (Id_B);
18824 Full : Entity_Id;
18825
18826 begin
18827 if Present (Full_B) then
18828
18829 -- Get to the underlying full view if necessary
18830
18831 if Is_Private_Type (Full_B)
18832 and then Present (Underlying_Full_View (Full_B))
18833 then
18834 Full_B := Underlying_Full_View (Full_B);
18835 end if;
18836
18837 -- The Base_Type is already completed, we can complete the subtype
18838 -- now. We have to create a new entity with the same name, Thus we
18839 -- can't use Create_Itype.
18840
18841 Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
18842 Set_Is_Itype (Full);
18843 Set_Associated_Node_For_Itype (Full, Related_Nod);
18844 Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
18845 end if;
18846
18847 -- The parent subtype may be private, but the base might not, in some
18848 -- nested instances. In that case, the subtype does not need to be
18849 -- exchanged. It would still be nice to make private subtypes and their
18850 -- bases consistent at all times ???
18851
18852 if Is_Private_Type (Id_B) then
18853 Append_Elmt (Id, Private_Dependents (Id_B));
18854 end if;
18855 end Prepare_Private_Subtype_Completion;
18856
18857 ---------------------------
18858 -- Process_Discriminants --
18859 ---------------------------
18860
18861 procedure Process_Discriminants
18862 (N : Node_Id;
18863 Prev : Entity_Id := Empty)
18864 is
18865 Elist : constant Elist_Id := New_Elmt_List;
18866 Id : Node_Id;
18867 Discr : Node_Id;
18868 Discr_Number : Uint;
18869 Discr_Type : Entity_Id;
18870 Default_Present : Boolean := False;
18871 Default_Not_Present : Boolean := False;
18872
18873 begin
18874 -- A composite type other than an array type can have discriminants.
18875 -- On entry, the current scope is the composite type.
18876
18877 -- The discriminants are initially entered into the scope of the type
18878 -- via Enter_Name with the default Ekind of E_Void to prevent premature
18879 -- use, as explained at the end of this procedure.
18880
18881 Discr := First (Discriminant_Specifications (N));
18882 while Present (Discr) loop
18883 Enter_Name (Defining_Identifier (Discr));
18884
18885 -- For navigation purposes we add a reference to the discriminant
18886 -- in the entity for the type. If the current declaration is a
18887 -- completion, place references on the partial view. Otherwise the
18888 -- type is the current scope.
18889
18890 if Present (Prev) then
18891
18892 -- The references go on the partial view, if present. If the
18893 -- partial view has discriminants, the references have been
18894 -- generated already.
18895
18896 if not Has_Discriminants (Prev) then
18897 Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
18898 end if;
18899 else
18900 Generate_Reference
18901 (Current_Scope, Defining_Identifier (Discr), 'd');
18902 end if;
18903
18904 if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
18905 Discr_Type := Access_Definition (Discr, Discriminant_Type (Discr));
18906
18907 -- Ada 2005 (AI-254)
18908
18909 if Present (Access_To_Subprogram_Definition
18910 (Discriminant_Type (Discr)))
18911 and then Protected_Present (Access_To_Subprogram_Definition
18912 (Discriminant_Type (Discr)))
18913 then
18914 Discr_Type :=
18915 Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
18916 end if;
18917
18918 else
18919 Find_Type (Discriminant_Type (Discr));
18920 Discr_Type := Etype (Discriminant_Type (Discr));
18921
18922 if Error_Posted (Discriminant_Type (Discr)) then
18923 Discr_Type := Any_Type;
18924 end if;
18925 end if;
18926
18927 -- Handling of discriminants that are access types
18928
18929 if Is_Access_Type (Discr_Type) then
18930
18931 -- Ada 2005 (AI-230): Access discriminant allowed in non-
18932 -- limited record types
18933
18934 if Ada_Version < Ada_2005 then
18935 Check_Access_Discriminant_Requires_Limited
18936 (Discr, Discriminant_Type (Discr));
18937 end if;
18938
18939 if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
18940 Error_Msg_N
18941 ("(Ada 83) access discriminant not allowed", Discr);
18942 end if;
18943
18944 -- If not access type, must be a discrete type
18945
18946 elsif not Is_Discrete_Type (Discr_Type) then
18947 Error_Msg_N
18948 ("discriminants must have a discrete or access type",
18949 Discriminant_Type (Discr));
18950 end if;
18951
18952 Set_Etype (Defining_Identifier (Discr), Discr_Type);
18953
18954 -- If a discriminant specification includes the assignment compound
18955 -- delimiter followed by an expression, the expression is the default
18956 -- expression of the discriminant; the default expression must be of
18957 -- the type of the discriminant. (RM 3.7.1) Since this expression is
18958 -- a default expression, we do the special preanalysis, since this
18959 -- expression does not freeze (see section "Handling of Default and
18960 -- Per-Object Expressions" in spec of package Sem).
18961
18962 if Present (Expression (Discr)) then
18963 Preanalyze_Spec_Expression (Expression (Discr), Discr_Type);
18964
18965 -- Legaity checks
18966
18967 if Nkind (N) = N_Formal_Type_Declaration then
18968 Error_Msg_N
18969 ("discriminant defaults not allowed for formal type",
18970 Expression (Discr));
18971
18972 -- Flag an error for a tagged type with defaulted discriminants,
18973 -- excluding limited tagged types when compiling for Ada 2012
18974 -- (see AI05-0214).
18975
18976 elsif Is_Tagged_Type (Current_Scope)
18977 and then (not Is_Limited_Type (Current_Scope)
18978 or else Ada_Version < Ada_2012)
18979 and then Comes_From_Source (N)
18980 then
18981 -- Note: see similar test in Check_Or_Process_Discriminants, to
18982 -- handle the (illegal) case of the completion of an untagged
18983 -- view with discriminants with defaults by a tagged full view.
18984 -- We skip the check if Discr does not come from source, to
18985 -- account for the case of an untagged derived type providing
18986 -- defaults for a renamed discriminant from a private untagged
18987 -- ancestor with a tagged full view (ACATS B460006).
18988
18989 if Ada_Version >= Ada_2012 then
18990 Error_Msg_N
18991 ("discriminants of nonlimited tagged type cannot have"
18992 & " defaults",
18993 Expression (Discr));
18994 else
18995 Error_Msg_N
18996 ("discriminants of tagged type cannot have defaults",
18997 Expression (Discr));
18998 end if;
18999
19000 else
19001 Default_Present := True;
19002 Append_Elmt (Expression (Discr), Elist);
19003
19004 -- Tag the defining identifiers for the discriminants with
19005 -- their corresponding default expressions from the tree.
19006
19007 Set_Discriminant_Default_Value
19008 (Defining_Identifier (Discr), Expression (Discr));
19009 end if;
19010
19011 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag
19012 -- gets set unless we can be sure that no range check is required.
19013
19014 if (GNATprove_Mode or not Expander_Active)
19015 and then not
19016 Is_In_Range
19017 (Expression (Discr), Discr_Type, Assume_Valid => True)
19018 then
19019 Set_Do_Range_Check (Expression (Discr));
19020 end if;
19021
19022 -- No default discriminant value given
19023
19024 else
19025 Default_Not_Present := True;
19026 end if;
19027
19028 -- Ada 2005 (AI-231): Create an Itype that is a duplicate of
19029 -- Discr_Type but with the null-exclusion attribute
19030
19031 if Ada_Version >= Ada_2005 then
19032
19033 -- Ada 2005 (AI-231): Static checks
19034
19035 if Can_Never_Be_Null (Discr_Type) then
19036 Null_Exclusion_Static_Checks (Discr);
19037
19038 elsif Is_Access_Type (Discr_Type)
19039 and then Null_Exclusion_Present (Discr)
19040
19041 -- No need to check itypes because in their case this check
19042 -- was done at their point of creation
19043
19044 and then not Is_Itype (Discr_Type)
19045 then
19046 if Can_Never_Be_Null (Discr_Type) then
19047 Error_Msg_NE
19048 ("`NOT NULL` not allowed (& already excludes null)",
19049 Discr,
19050 Discr_Type);
19051 end if;
19052
19053 Set_Etype (Defining_Identifier (Discr),
19054 Create_Null_Excluding_Itype
19055 (T => Discr_Type,
19056 Related_Nod => Discr));
19057
19058 -- Check for improper null exclusion if the type is otherwise
19059 -- legal for a discriminant.
19060
19061 elsif Null_Exclusion_Present (Discr)
19062 and then Is_Discrete_Type (Discr_Type)
19063 then
19064 Error_Msg_N
19065 ("null exclusion can only apply to an access type", Discr);
19066 end if;
19067
19068 -- Ada 2005 (AI-402): access discriminants of nonlimited types
19069 -- can't have defaults. Synchronized types, or types that are
19070 -- explicitly limited are fine, but special tests apply to derived
19071 -- types in generics: in a generic body we have to assume the
19072 -- worst, and therefore defaults are not allowed if the parent is
19073 -- a generic formal private type (see ACATS B370001).
19074
19075 if Is_Access_Type (Discr_Type) and then Default_Present then
19076 if Ekind (Discr_Type) /= E_Anonymous_Access_Type
19077 or else Is_Limited_Record (Current_Scope)
19078 or else Is_Concurrent_Type (Current_Scope)
19079 or else Is_Concurrent_Record_Type (Current_Scope)
19080 or else Ekind (Current_Scope) = E_Limited_Private_Type
19081 then
19082 if not Is_Derived_Type (Current_Scope)
19083 or else not Is_Generic_Type (Etype (Current_Scope))
19084 or else not In_Package_Body (Scope (Etype (Current_Scope)))
19085 or else Limited_Present
19086 (Type_Definition (Parent (Current_Scope)))
19087 then
19088 null;
19089
19090 else
19091 Error_Msg_N
19092 ("access discriminants of nonlimited types cannot "
19093 & "have defaults", Expression (Discr));
19094 end if;
19095
19096 elsif Present (Expression (Discr)) then
19097 Error_Msg_N
19098 ("(Ada 2005) access discriminants of nonlimited types "
19099 & "cannot have defaults", Expression (Discr));
19100 end if;
19101 end if;
19102 end if;
19103
19104 -- A discriminant cannot be effectively volatile. This check is only
19105 -- relevant when SPARK_Mode is on as it is not standard Ada legality
19106 -- rule (SPARK RM 7.1.3(6)).
19107
19108 if SPARK_Mode = On
19109 and then Is_Effectively_Volatile (Defining_Identifier (Discr))
19110 then
19111 Error_Msg_N ("discriminant cannot be volatile", Discr);
19112 end if;
19113
19114 Next (Discr);
19115 end loop;
19116
19117 -- An element list consisting of the default expressions of the
19118 -- discriminants is constructed in the above loop and used to set
19119 -- the Discriminant_Constraint attribute for the type. If an object
19120 -- is declared of this (record or task) type without any explicit
19121 -- discriminant constraint given, this element list will form the
19122 -- actual parameters for the corresponding initialization procedure
19123 -- for the type.
19124
19125 Set_Discriminant_Constraint (Current_Scope, Elist);
19126 Set_Stored_Constraint (Current_Scope, No_Elist);
19127
19128 -- Default expressions must be provided either for all or for none
19129 -- of the discriminants of a discriminant part. (RM 3.7.1)
19130
19131 if Default_Present and then Default_Not_Present then
19132 Error_Msg_N
19133 ("incomplete specification of defaults for discriminants", N);
19134 end if;
19135
19136 -- The use of the name of a discriminant is not allowed in default
19137 -- expressions of a discriminant part if the specification of the
19138 -- discriminant is itself given in the discriminant part. (RM 3.7.1)
19139
19140 -- To detect this, the discriminant names are entered initially with an
19141 -- Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
19142 -- attempt to use a void entity (for example in an expression that is
19143 -- type-checked) produces the error message: premature usage. Now after
19144 -- completing the semantic analysis of the discriminant part, we can set
19145 -- the Ekind of all the discriminants appropriately.
19146
19147 Discr := First (Discriminant_Specifications (N));
19148 Discr_Number := Uint_1;
19149 while Present (Discr) loop
19150 Id := Defining_Identifier (Discr);
19151 Set_Ekind (Id, E_Discriminant);
19152 Init_Component_Location (Id);
19153 Init_Esize (Id);
19154 Set_Discriminant_Number (Id, Discr_Number);
19155
19156 -- Make sure this is always set, even in illegal programs
19157
19158 Set_Corresponding_Discriminant (Id, Empty);
19159
19160 -- Initialize the Original_Record_Component to the entity itself.
19161 -- Inherit_Components will propagate the right value to
19162 -- discriminants in derived record types.
19163
19164 Set_Original_Record_Component (Id, Id);
19165
19166 -- Create the discriminal for the discriminant
19167
19168 Build_Discriminal (Id);
19169
19170 Next (Discr);
19171 Discr_Number := Discr_Number + 1;
19172 end loop;
19173
19174 Set_Has_Discriminants (Current_Scope);
19175 end Process_Discriminants;
19176
19177 -----------------------
19178 -- Process_Full_View --
19179 -----------------------
19180
19181 procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
19182 procedure Collect_Implemented_Interfaces
19183 (Typ : Entity_Id;
19184 Ifaces : Elist_Id);
19185 -- Ada 2005: Gather all the interfaces that Typ directly or
19186 -- inherently implements. Duplicate entries are not added to
19187 -- the list Ifaces.
19188
19189 ------------------------------------
19190 -- Collect_Implemented_Interfaces --
19191 ------------------------------------
19192
19193 procedure Collect_Implemented_Interfaces
19194 (Typ : Entity_Id;
19195 Ifaces : Elist_Id)
19196 is
19197 Iface : Entity_Id;
19198 Iface_Elmt : Elmt_Id;
19199
19200 begin
19201 -- Abstract interfaces are only associated with tagged record types
19202
19203 if not Is_Tagged_Type (Typ) or else not Is_Record_Type (Typ) then
19204 return;
19205 end if;
19206
19207 -- Recursively climb to the ancestors
19208
19209 if Etype (Typ) /= Typ
19210
19211 -- Protect the frontend against wrong cyclic declarations like:
19212
19213 -- type B is new A with private;
19214 -- type C is new A with private;
19215 -- private
19216 -- type B is new C with null record;
19217 -- type C is new B with null record;
19218
19219 and then Etype (Typ) /= Priv_T
19220 and then Etype (Typ) /= Full_T
19221 then
19222 -- Keep separate the management of private type declarations
19223
19224 if Ekind (Typ) = E_Record_Type_With_Private then
19225
19226 -- Handle the following illegal usage:
19227 -- type Private_Type is tagged private;
19228 -- private
19229 -- type Private_Type is new Type_Implementing_Iface;
19230
19231 if Present (Full_View (Typ))
19232 and then Etype (Typ) /= Full_View (Typ)
19233 then
19234 if Is_Interface (Etype (Typ)) then
19235 Append_Unique_Elmt (Etype (Typ), Ifaces);
19236 end if;
19237
19238 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
19239 end if;
19240
19241 -- Non-private types
19242
19243 else
19244 if Is_Interface (Etype (Typ)) then
19245 Append_Unique_Elmt (Etype (Typ), Ifaces);
19246 end if;
19247
19248 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
19249 end if;
19250 end if;
19251
19252 -- Handle entities in the list of abstract interfaces
19253
19254 if Present (Interfaces (Typ)) then
19255 Iface_Elmt := First_Elmt (Interfaces (Typ));
19256 while Present (Iface_Elmt) loop
19257 Iface := Node (Iface_Elmt);
19258
19259 pragma Assert (Is_Interface (Iface));
19260
19261 if not Contain_Interface (Iface, Ifaces) then
19262 Append_Elmt (Iface, Ifaces);
19263 Collect_Implemented_Interfaces (Iface, Ifaces);
19264 end if;
19265
19266 Next_Elmt (Iface_Elmt);
19267 end loop;
19268 end if;
19269 end Collect_Implemented_Interfaces;
19270
19271 -- Local variables
19272
19273 Full_Indic : Node_Id;
19274 Full_Parent : Entity_Id;
19275 Priv_Parent : Entity_Id;
19276
19277 -- Start of processing for Process_Full_View
19278
19279 begin
19280 -- First some sanity checks that must be done after semantic
19281 -- decoration of the full view and thus cannot be placed with other
19282 -- similar checks in Find_Type_Name
19283
19284 if not Is_Limited_Type (Priv_T)
19285 and then (Is_Limited_Type (Full_T)
19286 or else Is_Limited_Composite (Full_T))
19287 then
19288 if In_Instance then
19289 null;
19290 else
19291 Error_Msg_N
19292 ("completion of nonlimited type cannot be limited", Full_T);
19293 Explain_Limited_Type (Full_T, Full_T);
19294 end if;
19295
19296 elsif Is_Abstract_Type (Full_T)
19297 and then not Is_Abstract_Type (Priv_T)
19298 then
19299 Error_Msg_N
19300 ("completion of nonabstract type cannot be abstract", Full_T);
19301
19302 elsif Is_Tagged_Type (Priv_T)
19303 and then Is_Limited_Type (Priv_T)
19304 and then not Is_Limited_Type (Full_T)
19305 then
19306 -- If pragma CPP_Class was applied to the private declaration
19307 -- propagate the limitedness to the full-view
19308
19309 if Is_CPP_Class (Priv_T) then
19310 Set_Is_Limited_Record (Full_T);
19311
19312 -- GNAT allow its own definition of Limited_Controlled to disobey
19313 -- this rule in order in ease the implementation. This test is safe
19314 -- because Root_Controlled is defined in a child of System that
19315 -- normal programs are not supposed to use.
19316
19317 elsif Is_RTE (Etype (Full_T), RE_Root_Controlled) then
19318 Set_Is_Limited_Composite (Full_T);
19319 else
19320 Error_Msg_N
19321 ("completion of limited tagged type must be limited", Full_T);
19322 end if;
19323
19324 elsif Is_Generic_Type (Priv_T) then
19325 Error_Msg_N ("generic type cannot have a completion", Full_T);
19326 end if;
19327
19328 -- Check that ancestor interfaces of private and full views are
19329 -- consistent. We omit this check for synchronized types because
19330 -- they are performed on the corresponding record type when frozen.
19331
19332 if Ada_Version >= Ada_2005
19333 and then Is_Tagged_Type (Priv_T)
19334 and then Is_Tagged_Type (Full_T)
19335 and then not Is_Concurrent_Type (Full_T)
19336 then
19337 declare
19338 Iface : Entity_Id;
19339 Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
19340 Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
19341
19342 begin
19343 Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
19344 Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
19345
19346 -- Ada 2005 (AI-251): The partial view shall be a descendant of
19347 -- an interface type if and only if the full type is descendant
19348 -- of the interface type (AARM 7.3 (7.3/2)).
19349
19350 Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
19351
19352 if Present (Iface) then
19353 Error_Msg_NE
19354 ("interface in partial view& not implemented by full type "
19355 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
19356 end if;
19357
19358 Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
19359
19360 if Present (Iface) then
19361 Error_Msg_NE
19362 ("interface & not implemented by partial view "
19363 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
19364 end if;
19365 end;
19366 end if;
19367
19368 if Is_Tagged_Type (Priv_T)
19369 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
19370 and then Is_Derived_Type (Full_T)
19371 then
19372 Priv_Parent := Etype (Priv_T);
19373
19374 -- The full view of a private extension may have been transformed
19375 -- into an unconstrained derived type declaration and a subtype
19376 -- declaration (see build_derived_record_type for details).
19377
19378 if Nkind (N) = N_Subtype_Declaration then
19379 Full_Indic := Subtype_Indication (N);
19380 Full_Parent := Etype (Base_Type (Full_T));
19381 else
19382 Full_Indic := Subtype_Indication (Type_Definition (N));
19383 Full_Parent := Etype (Full_T);
19384 end if;
19385
19386 -- Check that the parent type of the full type is a descendant of
19387 -- the ancestor subtype given in the private extension. If either
19388 -- entity has an Etype equal to Any_Type then we had some previous
19389 -- error situation [7.3(8)].
19390
19391 if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
19392 return;
19393
19394 -- Ada 2005 (AI-251): Interfaces in the full type can be given in
19395 -- any order. Therefore we don't have to check that its parent must
19396 -- be a descendant of the parent of the private type declaration.
19397
19398 elsif Is_Interface (Priv_Parent)
19399 and then Is_Interface (Full_Parent)
19400 then
19401 null;
19402
19403 -- Ada 2005 (AI-251): If the parent of the private type declaration
19404 -- is an interface there is no need to check that it is an ancestor
19405 -- of the associated full type declaration. The required tests for
19406 -- this case are performed by Build_Derived_Record_Type.
19407
19408 elsif not Is_Interface (Base_Type (Priv_Parent))
19409 and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
19410 then
19411 Error_Msg_N
19412 ("parent of full type must descend from parent"
19413 & " of private extension", Full_Indic);
19414
19415 -- First check a formal restriction, and then proceed with checking
19416 -- Ada rules. Since the formal restriction is not a serious error, we
19417 -- don't prevent further error detection for this check, hence the
19418 -- ELSE.
19419
19420 else
19421 -- In formal mode, when completing a private extension the type
19422 -- named in the private part must be exactly the same as that
19423 -- named in the visible part.
19424
19425 if Priv_Parent /= Full_Parent then
19426 Error_Msg_Name_1 := Chars (Priv_Parent);
19427 Check_SPARK_05_Restriction ("% expected", Full_Indic);
19428 end if;
19429
19430 -- Check the rules of 7.3(10): if the private extension inherits
19431 -- known discriminants, then the full type must also inherit those
19432 -- discriminants from the same (ancestor) type, and the parent
19433 -- subtype of the full type must be constrained if and only if
19434 -- the ancestor subtype of the private extension is constrained.
19435
19436 if No (Discriminant_Specifications (Parent (Priv_T)))
19437 and then not Has_Unknown_Discriminants (Priv_T)
19438 and then Has_Discriminants (Base_Type (Priv_Parent))
19439 then
19440 declare
19441 Priv_Indic : constant Node_Id :=
19442 Subtype_Indication (Parent (Priv_T));
19443
19444 Priv_Constr : constant Boolean :=
19445 Is_Constrained (Priv_Parent)
19446 or else
19447 Nkind (Priv_Indic) = N_Subtype_Indication
19448 or else
19449 Is_Constrained (Entity (Priv_Indic));
19450
19451 Full_Constr : constant Boolean :=
19452 Is_Constrained (Full_Parent)
19453 or else
19454 Nkind (Full_Indic) = N_Subtype_Indication
19455 or else
19456 Is_Constrained (Entity (Full_Indic));
19457
19458 Priv_Discr : Entity_Id;
19459 Full_Discr : Entity_Id;
19460
19461 begin
19462 Priv_Discr := First_Discriminant (Priv_Parent);
19463 Full_Discr := First_Discriminant (Full_Parent);
19464 while Present (Priv_Discr) and then Present (Full_Discr) loop
19465 if Original_Record_Component (Priv_Discr) =
19466 Original_Record_Component (Full_Discr)
19467 or else
19468 Corresponding_Discriminant (Priv_Discr) =
19469 Corresponding_Discriminant (Full_Discr)
19470 then
19471 null;
19472 else
19473 exit;
19474 end if;
19475
19476 Next_Discriminant (Priv_Discr);
19477 Next_Discriminant (Full_Discr);
19478 end loop;
19479
19480 if Present (Priv_Discr) or else Present (Full_Discr) then
19481 Error_Msg_N
19482 ("full view must inherit discriminants of the parent"
19483 & " type used in the private extension", Full_Indic);
19484
19485 elsif Priv_Constr and then not Full_Constr then
19486 Error_Msg_N
19487 ("parent subtype of full type must be constrained",
19488 Full_Indic);
19489
19490 elsif Full_Constr and then not Priv_Constr then
19491 Error_Msg_N
19492 ("parent subtype of full type must be unconstrained",
19493 Full_Indic);
19494 end if;
19495 end;
19496
19497 -- Check the rules of 7.3(12): if a partial view has neither
19498 -- known or unknown discriminants, then the full type
19499 -- declaration shall define a definite subtype.
19500
19501 elsif not Has_Unknown_Discriminants (Priv_T)
19502 and then not Has_Discriminants (Priv_T)
19503 and then not Is_Constrained (Full_T)
19504 then
19505 Error_Msg_N
19506 ("full view must define a constrained type if partial view"
19507 & " has no discriminants", Full_T);
19508 end if;
19509
19510 -- ??????? Do we implement the following properly ?????
19511 -- If the ancestor subtype of a private extension has constrained
19512 -- discriminants, then the parent subtype of the full view shall
19513 -- impose a statically matching constraint on those discriminants
19514 -- [7.3(13)].
19515 end if;
19516
19517 else
19518 -- For untagged types, verify that a type without discriminants is
19519 -- not completed with an unconstrained type. A separate error message
19520 -- is produced if the full type has defaulted discriminants.
19521
19522 if not Is_Indefinite_Subtype (Priv_T)
19523 and then Is_Indefinite_Subtype (Full_T)
19524 then
19525 Error_Msg_Sloc := Sloc (Parent (Priv_T));
19526 Error_Msg_NE
19527 ("full view of& not compatible with declaration#",
19528 Full_T, Priv_T);
19529
19530 if not Is_Tagged_Type (Full_T) then
19531 Error_Msg_N
19532 ("\one is constrained, the other unconstrained", Full_T);
19533 end if;
19534 end if;
19535 end if;
19536
19537 -- AI-419: verify that the use of "limited" is consistent
19538
19539 declare
19540 Orig_Decl : constant Node_Id := Original_Node (N);
19541
19542 begin
19543 if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
19544 and then Nkind (Orig_Decl) = N_Full_Type_Declaration
19545 and then Nkind
19546 (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
19547 then
19548 if not Limited_Present (Parent (Priv_T))
19549 and then not Synchronized_Present (Parent (Priv_T))
19550 and then Limited_Present (Type_Definition (Orig_Decl))
19551 then
19552 Error_Msg_N
19553 ("full view of non-limited extension cannot be limited", N);
19554
19555 -- Conversely, if the partial view carries the limited keyword,
19556 -- the full view must as well, even if it may be redundant.
19557
19558 elsif Limited_Present (Parent (Priv_T))
19559 and then not Limited_Present (Type_Definition (Orig_Decl))
19560 then
19561 Error_Msg_N
19562 ("full view of limited extension must be explicitly limited",
19563 N);
19564 end if;
19565 end if;
19566 end;
19567
19568 -- Ada 2005 (AI-443): A synchronized private extension must be
19569 -- completed by a task or protected type.
19570
19571 if Ada_Version >= Ada_2005
19572 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
19573 and then Synchronized_Present (Parent (Priv_T))
19574 and then not Is_Concurrent_Type (Full_T)
19575 then
19576 Error_Msg_N ("full view of synchronized extension must " &
19577 "be synchronized type", N);
19578 end if;
19579
19580 -- Ada 2005 AI-363: if the full view has discriminants with
19581 -- defaults, it is illegal to declare constrained access subtypes
19582 -- whose designated type is the current type. This allows objects
19583 -- of the type that are declared in the heap to be unconstrained.
19584
19585 if not Has_Unknown_Discriminants (Priv_T)
19586 and then not Has_Discriminants (Priv_T)
19587 and then Has_Discriminants (Full_T)
19588 and then
19589 Present (Discriminant_Default_Value (First_Discriminant (Full_T)))
19590 then
19591 Set_Has_Constrained_Partial_View (Full_T);
19592 Set_Has_Constrained_Partial_View (Priv_T);
19593 end if;
19594
19595 -- Create a full declaration for all its subtypes recorded in
19596 -- Private_Dependents and swap them similarly to the base type. These
19597 -- are subtypes that have been define before the full declaration of
19598 -- the private type. We also swap the entry in Private_Dependents list
19599 -- so we can properly restore the private view on exit from the scope.
19600
19601 declare
19602 Priv_Elmt : Elmt_Id;
19603 Priv_Scop : Entity_Id;
19604 Priv : Entity_Id;
19605 Full : Entity_Id;
19606
19607 begin
19608 Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
19609 while Present (Priv_Elmt) loop
19610 Priv := Node (Priv_Elmt);
19611 Priv_Scop := Scope (Priv);
19612
19613 if Ekind_In (Priv, E_Private_Subtype,
19614 E_Limited_Private_Subtype,
19615 E_Record_Subtype_With_Private)
19616 then
19617 Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
19618 Set_Is_Itype (Full);
19619 Set_Parent (Full, Parent (Priv));
19620 Set_Associated_Node_For_Itype (Full, N);
19621
19622 -- Now we need to complete the private subtype, but since the
19623 -- base type has already been swapped, we must also swap the
19624 -- subtypes (and thus, reverse the arguments in the call to
19625 -- Complete_Private_Subtype). Also note that we may need to
19626 -- re-establish the scope of the private subtype.
19627
19628 Copy_And_Swap (Priv, Full);
19629
19630 if not In_Open_Scopes (Priv_Scop) then
19631 Push_Scope (Priv_Scop);
19632
19633 else
19634 -- Reset Priv_Scop to Empty to indicate no scope was pushed
19635
19636 Priv_Scop := Empty;
19637 end if;
19638
19639 Complete_Private_Subtype (Full, Priv, Full_T, N);
19640
19641 if Present (Priv_Scop) then
19642 Pop_Scope;
19643 end if;
19644
19645 Replace_Elmt (Priv_Elmt, Full);
19646 end if;
19647
19648 Next_Elmt (Priv_Elmt);
19649 end loop;
19650 end;
19651
19652 -- If the private view was tagged, copy the new primitive operations
19653 -- from the private view to the full view.
19654
19655 if Is_Tagged_Type (Full_T) then
19656 declare
19657 Disp_Typ : Entity_Id;
19658 Full_List : Elist_Id;
19659 Prim : Entity_Id;
19660 Prim_Elmt : Elmt_Id;
19661 Priv_List : Elist_Id;
19662
19663 function Contains
19664 (E : Entity_Id;
19665 L : Elist_Id) return Boolean;
19666 -- Determine whether list L contains element E
19667
19668 --------------
19669 -- Contains --
19670 --------------
19671
19672 function Contains
19673 (E : Entity_Id;
19674 L : Elist_Id) return Boolean
19675 is
19676 List_Elmt : Elmt_Id;
19677
19678 begin
19679 List_Elmt := First_Elmt (L);
19680 while Present (List_Elmt) loop
19681 if Node (List_Elmt) = E then
19682 return True;
19683 end if;
19684
19685 Next_Elmt (List_Elmt);
19686 end loop;
19687
19688 return False;
19689 end Contains;
19690
19691 -- Start of processing
19692
19693 begin
19694 if Is_Tagged_Type (Priv_T) then
19695 Priv_List := Primitive_Operations (Priv_T);
19696 Prim_Elmt := First_Elmt (Priv_List);
19697
19698 -- In the case of a concurrent type completing a private tagged
19699 -- type, primitives may have been declared in between the two
19700 -- views. These subprograms need to be wrapped the same way
19701 -- entries and protected procedures are handled because they
19702 -- cannot be directly shared by the two views.
19703
19704 if Is_Concurrent_Type (Full_T) then
19705 declare
19706 Conc_Typ : constant Entity_Id :=
19707 Corresponding_Record_Type (Full_T);
19708 Curr_Nod : Node_Id := Parent (Conc_Typ);
19709 Wrap_Spec : Node_Id;
19710
19711 begin
19712 while Present (Prim_Elmt) loop
19713 Prim := Node (Prim_Elmt);
19714
19715 if Comes_From_Source (Prim)
19716 and then not Is_Abstract_Subprogram (Prim)
19717 then
19718 Wrap_Spec :=
19719 Make_Subprogram_Declaration (Sloc (Prim),
19720 Specification =>
19721 Build_Wrapper_Spec
19722 (Subp_Id => Prim,
19723 Obj_Typ => Conc_Typ,
19724 Formals =>
19725 Parameter_Specifications (
19726 Parent (Prim))));
19727
19728 Insert_After (Curr_Nod, Wrap_Spec);
19729 Curr_Nod := Wrap_Spec;
19730
19731 Analyze (Wrap_Spec);
19732 end if;
19733
19734 Next_Elmt (Prim_Elmt);
19735 end loop;
19736
19737 return;
19738 end;
19739
19740 -- For non-concurrent types, transfer explicit primitives, but
19741 -- omit those inherited from the parent of the private view
19742 -- since they will be re-inherited later on.
19743
19744 else
19745 Full_List := Primitive_Operations (Full_T);
19746
19747 while Present (Prim_Elmt) loop
19748 Prim := Node (Prim_Elmt);
19749
19750 if Comes_From_Source (Prim)
19751 and then not Contains (Prim, Full_List)
19752 then
19753 Append_Elmt (Prim, Full_List);
19754 end if;
19755
19756 Next_Elmt (Prim_Elmt);
19757 end loop;
19758 end if;
19759
19760 -- Untagged private view
19761
19762 else
19763 Full_List := Primitive_Operations (Full_T);
19764
19765 -- In this case the partial view is untagged, so here we locate
19766 -- all of the earlier primitives that need to be treated as
19767 -- dispatching (those that appear between the two views). Note
19768 -- that these additional operations must all be new operations
19769 -- (any earlier operations that override inherited operations
19770 -- of the full view will already have been inserted in the
19771 -- primitives list, marked by Check_Operation_From_Private_View
19772 -- as dispatching. Note that implicit "/=" operators are
19773 -- excluded from being added to the primitives list since they
19774 -- shouldn't be treated as dispatching (tagged "/=" is handled
19775 -- specially).
19776
19777 Prim := Next_Entity (Full_T);
19778 while Present (Prim) and then Prim /= Priv_T loop
19779 if Ekind_In (Prim, E_Procedure, E_Function) then
19780 Disp_Typ := Find_Dispatching_Type (Prim);
19781
19782 if Disp_Typ = Full_T
19783 and then (Chars (Prim) /= Name_Op_Ne
19784 or else Comes_From_Source (Prim))
19785 then
19786 Check_Controlling_Formals (Full_T, Prim);
19787
19788 if not Is_Dispatching_Operation (Prim) then
19789 Append_Elmt (Prim, Full_List);
19790 Set_Is_Dispatching_Operation (Prim, True);
19791 Set_DT_Position_Value (Prim, No_Uint);
19792 end if;
19793
19794 elsif Is_Dispatching_Operation (Prim)
19795 and then Disp_Typ /= Full_T
19796 then
19797
19798 -- Verify that it is not otherwise controlled by a
19799 -- formal or a return value of type T.
19800
19801 Check_Controlling_Formals (Disp_Typ, Prim);
19802 end if;
19803 end if;
19804
19805 Next_Entity (Prim);
19806 end loop;
19807 end if;
19808
19809 -- For the tagged case, the two views can share the same primitive
19810 -- operations list and the same class-wide type. Update attributes
19811 -- of the class-wide type which depend on the full declaration.
19812
19813 if Is_Tagged_Type (Priv_T) then
19814 Set_Direct_Primitive_Operations (Priv_T, Full_List);
19815 Set_Class_Wide_Type
19816 (Base_Type (Full_T), Class_Wide_Type (Priv_T));
19817
19818 Set_Has_Task (Class_Wide_Type (Priv_T), Has_Task (Full_T));
19819 Set_Has_Protected
19820 (Class_Wide_Type (Priv_T), Has_Protected (Full_T));
19821 end if;
19822 end;
19823 end if;
19824
19825 -- Ada 2005 AI 161: Check preelaborable initialization consistency
19826
19827 if Known_To_Have_Preelab_Init (Priv_T) then
19828
19829 -- Case where there is a pragma Preelaborable_Initialization. We
19830 -- always allow this in predefined units, which is cheating a bit,
19831 -- but it means we don't have to struggle to meet the requirements in
19832 -- the RM for having Preelaborable Initialization. Otherwise we
19833 -- require that the type meets the RM rules. But we can't check that
19834 -- yet, because of the rule about overriding Initialize, so we simply
19835 -- set a flag that will be checked at freeze time.
19836
19837 if not In_Predefined_Unit (Full_T) then
19838 Set_Must_Have_Preelab_Init (Full_T);
19839 end if;
19840 end if;
19841
19842 -- If pragma CPP_Class was applied to the private type declaration,
19843 -- propagate it now to the full type declaration.
19844
19845 if Is_CPP_Class (Priv_T) then
19846 Set_Is_CPP_Class (Full_T);
19847 Set_Convention (Full_T, Convention_CPP);
19848
19849 -- Check that components of imported CPP types do not have default
19850 -- expressions.
19851
19852 Check_CPP_Type_Has_No_Defaults (Full_T);
19853 end if;
19854
19855 -- If the private view has user specified stream attributes, then so has
19856 -- the full view.
19857
19858 -- Why the test, how could these flags be already set in Full_T ???
19859
19860 if Has_Specified_Stream_Read (Priv_T) then
19861 Set_Has_Specified_Stream_Read (Full_T);
19862 end if;
19863
19864 if Has_Specified_Stream_Write (Priv_T) then
19865 Set_Has_Specified_Stream_Write (Full_T);
19866 end if;
19867
19868 if Has_Specified_Stream_Input (Priv_T) then
19869 Set_Has_Specified_Stream_Input (Full_T);
19870 end if;
19871
19872 if Has_Specified_Stream_Output (Priv_T) then
19873 Set_Has_Specified_Stream_Output (Full_T);
19874 end if;
19875
19876 -- Propagate the attributes related to pragma Default_Initial_Condition
19877 -- from the private to the full view. Note that both flags are mutually
19878 -- exclusive.
19879
19880 if Has_Default_Init_Cond (Priv_T)
19881 or else Has_Inherited_Default_Init_Cond (Priv_T)
19882 then
19883 Propagate_Default_Init_Cond_Attributes
19884 (From_Typ => Priv_T,
19885 To_Typ => Full_T,
19886 Private_To_Full_View => True);
19887
19888 -- In the case where the full view is derived from another private type,
19889 -- the attributes related to pragma Default_Initial_Condition must be
19890 -- propagated from the full to the private view to maintain consistency
19891 -- of views.
19892
19893 -- package Pack is
19894 -- type Parent_Typ is private
19895 -- with Default_Initial_Condition ...;
19896 -- private
19897 -- type Parent_Typ is ...;
19898 -- end Pack;
19899
19900 -- with Pack; use Pack;
19901 -- package Pack_2 is
19902 -- type Deriv_Typ is private; -- must inherit
19903 -- private
19904 -- type Deriv_Typ is new Parent_Typ; -- must inherit
19905 -- end Pack_2;
19906
19907 elsif Has_Default_Init_Cond (Full_T)
19908 or else Has_Inherited_Default_Init_Cond (Full_T)
19909 then
19910 Propagate_Default_Init_Cond_Attributes
19911 (From_Typ => Full_T,
19912 To_Typ => Priv_T,
19913 Private_To_Full_View => True);
19914 end if;
19915
19916 -- Propagate the attributes related to pragma Ghost from the private to
19917 -- the full view.
19918
19919 if Is_Ghost_Entity (Priv_T) then
19920 Set_Is_Ghost_Entity (Full_T);
19921
19922 -- The Ghost policy in effect at the point of declaration and at the
19923 -- point of completion must match (SPARK RM 6.9(15)).
19924
19925 Check_Ghost_Completion (Priv_T, Full_T);
19926
19927 -- In the case where the private view of a tagged type lacks a parent
19928 -- type and is subject to pragma Ghost, ensure that the parent type
19929 -- specified by the full view is also Ghost (SPARK RM 6.9(9)).
19930
19931 if Is_Derived_Type (Full_T) then
19932 Check_Ghost_Derivation (Full_T);
19933 end if;
19934 end if;
19935
19936 -- Propagate invariants to full type
19937
19938 if Has_Invariants (Priv_T) then
19939 Set_Has_Invariants (Full_T);
19940 Set_Invariant_Procedure (Full_T, Invariant_Procedure (Priv_T));
19941 end if;
19942
19943 if Has_Inheritable_Invariants (Priv_T) then
19944 Set_Has_Inheritable_Invariants (Full_T);
19945 end if;
19946
19947 -- Check hidden inheritance of class-wide type invariants
19948
19949 if Ada_Version >= Ada_2012
19950 and then not Has_Inheritable_Invariants (Full_T)
19951 and then In_Private_Part (Current_Scope)
19952 and then Has_Interfaces (Full_T)
19953 then
19954 declare
19955 Ifaces : Elist_Id;
19956 AI : Elmt_Id;
19957
19958 begin
19959 Collect_Interfaces (Full_T, Ifaces, Exclude_Parents => True);
19960
19961 AI := First_Elmt (Ifaces);
19962 while Present (AI) loop
19963 if Has_Inheritable_Invariants (Node (AI)) then
19964 Error_Msg_N
19965 ("hidden inheritance of class-wide type invariants " &
19966 "not allowed", N);
19967 exit;
19968 end if;
19969
19970 Next_Elmt (AI);
19971 end loop;
19972 end;
19973 end if;
19974
19975 -- Propagate predicates to full type, and predicate function if already
19976 -- defined. It is not clear that this can actually happen? the partial
19977 -- view cannot be frozen yet, and the predicate function has not been
19978 -- built. Still it is a cheap check and seems safer to make it.
19979
19980 if Has_Predicates (Priv_T) then
19981 if Present (Predicate_Function (Priv_T)) then
19982 Set_Predicate_Function (Full_T, Predicate_Function (Priv_T));
19983 end if;
19984
19985 Set_Has_Predicates (Full_T);
19986 end if;
19987 end Process_Full_View;
19988
19989 -----------------------------------
19990 -- Process_Incomplete_Dependents --
19991 -----------------------------------
19992
19993 procedure Process_Incomplete_Dependents
19994 (N : Node_Id;
19995 Full_T : Entity_Id;
19996 Inc_T : Entity_Id)
19997 is
19998 Inc_Elmt : Elmt_Id;
19999 Priv_Dep : Entity_Id;
20000 New_Subt : Entity_Id;
20001
20002 Disc_Constraint : Elist_Id;
20003
20004 begin
20005 if No (Private_Dependents (Inc_T)) then
20006 return;
20007 end if;
20008
20009 -- Itypes that may be generated by the completion of an incomplete
20010 -- subtype are not used by the back-end and not attached to the tree.
20011 -- They are created only for constraint-checking purposes.
20012
20013 Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
20014 while Present (Inc_Elmt) loop
20015 Priv_Dep := Node (Inc_Elmt);
20016
20017 if Ekind (Priv_Dep) = E_Subprogram_Type then
20018
20019 -- An Access_To_Subprogram type may have a return type or a
20020 -- parameter type that is incomplete. Replace with the full view.
20021
20022 if Etype (Priv_Dep) = Inc_T then
20023 Set_Etype (Priv_Dep, Full_T);
20024 end if;
20025
20026 declare
20027 Formal : Entity_Id;
20028
20029 begin
20030 Formal := First_Formal (Priv_Dep);
20031 while Present (Formal) loop
20032 if Etype (Formal) = Inc_T then
20033 Set_Etype (Formal, Full_T);
20034 end if;
20035
20036 Next_Formal (Formal);
20037 end loop;
20038 end;
20039
20040 elsif Is_Overloadable (Priv_Dep) then
20041
20042 -- If a subprogram in the incomplete dependents list is primitive
20043 -- for a tagged full type then mark it as a dispatching operation,
20044 -- check whether it overrides an inherited subprogram, and check
20045 -- restrictions on its controlling formals. Note that a protected
20046 -- operation is never dispatching: only its wrapper operation
20047 -- (which has convention Ada) is.
20048
20049 if Is_Tagged_Type (Full_T)
20050 and then Is_Primitive (Priv_Dep)
20051 and then Convention (Priv_Dep) /= Convention_Protected
20052 then
20053 Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
20054 Set_Is_Dispatching_Operation (Priv_Dep);
20055 Check_Controlling_Formals (Full_T, Priv_Dep);
20056 end if;
20057
20058 elsif Ekind (Priv_Dep) = E_Subprogram_Body then
20059
20060 -- Can happen during processing of a body before the completion
20061 -- of a TA type. Ignore, because spec is also on dependent list.
20062
20063 return;
20064
20065 -- Ada 2005 (AI-412): Transform a regular incomplete subtype into a
20066 -- corresponding subtype of the full view.
20067
20068 elsif Ekind (Priv_Dep) = E_Incomplete_Subtype then
20069 Set_Subtype_Indication
20070 (Parent (Priv_Dep), New_Occurrence_Of (Full_T, Sloc (Priv_Dep)));
20071 Set_Etype (Priv_Dep, Full_T);
20072 Set_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
20073 Set_Analyzed (Parent (Priv_Dep), False);
20074
20075 -- Reanalyze the declaration, suppressing the call to
20076 -- Enter_Name to avoid duplicate names.
20077
20078 Analyze_Subtype_Declaration
20079 (N => Parent (Priv_Dep),
20080 Skip => True);
20081
20082 -- Dependent is a subtype
20083
20084 else
20085 -- We build a new subtype indication using the full view of the
20086 -- incomplete parent. The discriminant constraints have been
20087 -- elaborated already at the point of the subtype declaration.
20088
20089 New_Subt := Create_Itype (E_Void, N);
20090
20091 if Has_Discriminants (Full_T) then
20092 Disc_Constraint := Discriminant_Constraint (Priv_Dep);
20093 else
20094 Disc_Constraint := No_Elist;
20095 end if;
20096
20097 Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
20098 Set_Full_View (Priv_Dep, New_Subt);
20099 end if;
20100
20101 Next_Elmt (Inc_Elmt);
20102 end loop;
20103 end Process_Incomplete_Dependents;
20104
20105 --------------------------------
20106 -- Process_Range_Expr_In_Decl --
20107 --------------------------------
20108
20109 procedure Process_Range_Expr_In_Decl
20110 (R : Node_Id;
20111 T : Entity_Id;
20112 Subtyp : Entity_Id := Empty;
20113 Check_List : List_Id := Empty_List;
20114 R_Check_Off : Boolean := False;
20115 In_Iter_Schm : Boolean := False)
20116 is
20117 Lo, Hi : Node_Id;
20118 R_Checks : Check_Result;
20119 Insert_Node : Node_Id;
20120 Def_Id : Entity_Id;
20121
20122 begin
20123 Analyze_And_Resolve (R, Base_Type (T));
20124
20125 if Nkind (R) = N_Range then
20126
20127 -- In SPARK, all ranges should be static, with the exception of the
20128 -- discrete type definition of a loop parameter specification.
20129
20130 if not In_Iter_Schm
20131 and then not Is_OK_Static_Range (R)
20132 then
20133 Check_SPARK_05_Restriction ("range should be static", R);
20134 end if;
20135
20136 Lo := Low_Bound (R);
20137 Hi := High_Bound (R);
20138
20139 -- Validity checks on the range of a quantified expression are
20140 -- delayed until the construct is transformed into a loop.
20141
20142 if Nkind (Parent (R)) = N_Loop_Parameter_Specification
20143 and then Nkind (Parent (Parent (R))) = N_Quantified_Expression
20144 then
20145 null;
20146
20147 -- We need to ensure validity of the bounds here, because if we
20148 -- go ahead and do the expansion, then the expanded code will get
20149 -- analyzed with range checks suppressed and we miss the check.
20150
20151 -- WARNING: The capture of the range bounds with xxx_FIRST/_LAST and
20152 -- the temporaries generated by routine Remove_Side_Effects by means
20153 -- of validity checks must use the same names. When a range appears
20154 -- in the parent of a generic, the range is processed with checks
20155 -- disabled as part of the generic context and with checks enabled
20156 -- for code generation purposes. This leads to link issues as the
20157 -- generic contains references to xxx_FIRST/_LAST, but the inlined
20158 -- template sees the temporaries generated by Remove_Side_Effects.
20159
20160 else
20161 Validity_Check_Range (R, Subtyp);
20162 end if;
20163
20164 -- If there were errors in the declaration, try and patch up some
20165 -- common mistakes in the bounds. The cases handled are literals
20166 -- which are Integer where the expected type is Real and vice versa.
20167 -- These corrections allow the compilation process to proceed further
20168 -- along since some basic assumptions of the format of the bounds
20169 -- are guaranteed.
20170
20171 if Etype (R) = Any_Type then
20172 if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
20173 Rewrite (Lo,
20174 Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
20175
20176 elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
20177 Rewrite (Hi,
20178 Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
20179
20180 elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
20181 Rewrite (Lo,
20182 Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
20183
20184 elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
20185 Rewrite (Hi,
20186 Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
20187 end if;
20188
20189 Set_Etype (Lo, T);
20190 Set_Etype (Hi, T);
20191 end if;
20192
20193 -- If the bounds of the range have been mistakenly given as string
20194 -- literals (perhaps in place of character literals), then an error
20195 -- has already been reported, but we rewrite the string literal as a
20196 -- bound of the range's type to avoid blowups in later processing
20197 -- that looks at static values.
20198
20199 if Nkind (Lo) = N_String_Literal then
20200 Rewrite (Lo,
20201 Make_Attribute_Reference (Sloc (Lo),
20202 Prefix => New_Occurrence_Of (T, Sloc (Lo)),
20203 Attribute_Name => Name_First));
20204 Analyze_And_Resolve (Lo);
20205 end if;
20206
20207 if Nkind (Hi) = N_String_Literal then
20208 Rewrite (Hi,
20209 Make_Attribute_Reference (Sloc (Hi),
20210 Prefix => New_Occurrence_Of (T, Sloc (Hi)),
20211 Attribute_Name => Name_First));
20212 Analyze_And_Resolve (Hi);
20213 end if;
20214
20215 -- If bounds aren't scalar at this point then exit, avoiding
20216 -- problems with further processing of the range in this procedure.
20217
20218 if not Is_Scalar_Type (Etype (Lo)) then
20219 return;
20220 end if;
20221
20222 -- Resolve (actually Sem_Eval) has checked that the bounds are in
20223 -- then range of the base type. Here we check whether the bounds
20224 -- are in the range of the subtype itself. Note that if the bounds
20225 -- represent the null range the Constraint_Error exception should
20226 -- not be raised.
20227
20228 -- ??? The following code should be cleaned up as follows
20229
20230 -- 1. The Is_Null_Range (Lo, Hi) test should disappear since it
20231 -- is done in the call to Range_Check (R, T); below
20232
20233 -- 2. The use of R_Check_Off should be investigated and possibly
20234 -- removed, this would clean up things a bit.
20235
20236 if Is_Null_Range (Lo, Hi) then
20237 null;
20238
20239 else
20240 -- Capture values of bounds and generate temporaries for them
20241 -- if needed, before applying checks, since checks may cause
20242 -- duplication of the expression without forcing evaluation.
20243
20244 -- The forced evaluation removes side effects from expressions,
20245 -- which should occur also in GNATprove mode. Otherwise, we end up
20246 -- with unexpected insertions of actions at places where this is
20247 -- not supposed to occur, e.g. on default parameters of a call.
20248
20249 if Expander_Active or GNATprove_Mode then
20250
20251 -- Call Force_Evaluation to create declarations as needed to
20252 -- deal with side effects, and also create typ_FIRST/LAST
20253 -- entities for bounds if we have a subtype name.
20254
20255 -- Note: we do this transformation even if expansion is not
20256 -- active if we are in GNATprove_Mode since the transformation
20257 -- is in general required to ensure that the resulting tree has
20258 -- proper Ada semantics.
20259
20260 Force_Evaluation
20261 (Lo, Related_Id => Subtyp, Is_Low_Bound => True);
20262 Force_Evaluation
20263 (Hi, Related_Id => Subtyp, Is_High_Bound => True);
20264 end if;
20265
20266 -- We use a flag here instead of suppressing checks on the type
20267 -- because the type we check against isn't necessarily the place
20268 -- where we put the check.
20269
20270 if not R_Check_Off then
20271 R_Checks := Get_Range_Checks (R, T);
20272
20273 -- Look up tree to find an appropriate insertion point. We
20274 -- can't just use insert_actions because later processing
20275 -- depends on the insertion node. Prior to Ada 2012 the
20276 -- insertion point could only be a declaration or a loop, but
20277 -- quantified expressions can appear within any context in an
20278 -- expression, and the insertion point can be any statement,
20279 -- pragma, or declaration.
20280
20281 Insert_Node := Parent (R);
20282 while Present (Insert_Node) loop
20283 exit when
20284 Nkind (Insert_Node) in N_Declaration
20285 and then
20286 not Nkind_In
20287 (Insert_Node, N_Component_Declaration,
20288 N_Loop_Parameter_Specification,
20289 N_Function_Specification,
20290 N_Procedure_Specification);
20291
20292 exit when Nkind (Insert_Node) in N_Later_Decl_Item
20293 or else Nkind (Insert_Node) in
20294 N_Statement_Other_Than_Procedure_Call
20295 or else Nkind_In (Insert_Node, N_Procedure_Call_Statement,
20296 N_Pragma);
20297
20298 Insert_Node := Parent (Insert_Node);
20299 end loop;
20300
20301 -- Why would Type_Decl not be present??? Without this test,
20302 -- short regression tests fail.
20303
20304 if Present (Insert_Node) then
20305
20306 -- Case of loop statement. Verify that the range is part
20307 -- of the subtype indication of the iteration scheme.
20308
20309 if Nkind (Insert_Node) = N_Loop_Statement then
20310 declare
20311 Indic : Node_Id;
20312
20313 begin
20314 Indic := Parent (R);
20315 while Present (Indic)
20316 and then Nkind (Indic) /= N_Subtype_Indication
20317 loop
20318 Indic := Parent (Indic);
20319 end loop;
20320
20321 if Present (Indic) then
20322 Def_Id := Etype (Subtype_Mark (Indic));
20323
20324 Insert_Range_Checks
20325 (R_Checks,
20326 Insert_Node,
20327 Def_Id,
20328 Sloc (Insert_Node),
20329 R,
20330 Do_Before => True);
20331 end if;
20332 end;
20333
20334 -- Insertion before a declaration. If the declaration
20335 -- includes discriminants, the list of applicable checks
20336 -- is given by the caller.
20337
20338 elsif Nkind (Insert_Node) in N_Declaration then
20339 Def_Id := Defining_Identifier (Insert_Node);
20340
20341 if (Ekind (Def_Id) = E_Record_Type
20342 and then Depends_On_Discriminant (R))
20343 or else
20344 (Ekind (Def_Id) = E_Protected_Type
20345 and then Has_Discriminants (Def_Id))
20346 then
20347 Append_Range_Checks
20348 (R_Checks,
20349 Check_List, Def_Id, Sloc (Insert_Node), R);
20350
20351 else
20352 Insert_Range_Checks
20353 (R_Checks,
20354 Insert_Node, Def_Id, Sloc (Insert_Node), R);
20355
20356 end if;
20357
20358 -- Insertion before a statement. Range appears in the
20359 -- context of a quantified expression. Insertion will
20360 -- take place when expression is expanded.
20361
20362 else
20363 null;
20364 end if;
20365 end if;
20366 end if;
20367 end if;
20368
20369 -- Case of other than an explicit N_Range node
20370
20371 -- The forced evaluation removes side effects from expressions, which
20372 -- should occur also in GNATprove mode. Otherwise, we end up with
20373 -- unexpected insertions of actions at places where this is not
20374 -- supposed to occur, e.g. on default parameters of a call.
20375
20376 elsif Expander_Active or GNATprove_Mode then
20377 Get_Index_Bounds (R, Lo, Hi);
20378 Force_Evaluation (Lo);
20379 Force_Evaluation (Hi);
20380 end if;
20381 end Process_Range_Expr_In_Decl;
20382
20383 --------------------------------------
20384 -- Process_Real_Range_Specification --
20385 --------------------------------------
20386
20387 procedure Process_Real_Range_Specification (Def : Node_Id) is
20388 Spec : constant Node_Id := Real_Range_Specification (Def);
20389 Lo : Node_Id;
20390 Hi : Node_Id;
20391 Err : Boolean := False;
20392
20393 procedure Analyze_Bound (N : Node_Id);
20394 -- Analyze and check one bound
20395
20396 -------------------
20397 -- Analyze_Bound --
20398 -------------------
20399
20400 procedure Analyze_Bound (N : Node_Id) is
20401 begin
20402 Analyze_And_Resolve (N, Any_Real);
20403
20404 if not Is_OK_Static_Expression (N) then
20405 Flag_Non_Static_Expr
20406 ("bound in real type definition is not static!", N);
20407 Err := True;
20408 end if;
20409 end Analyze_Bound;
20410
20411 -- Start of processing for Process_Real_Range_Specification
20412
20413 begin
20414 if Present (Spec) then
20415 Lo := Low_Bound (Spec);
20416 Hi := High_Bound (Spec);
20417 Analyze_Bound (Lo);
20418 Analyze_Bound (Hi);
20419
20420 -- If error, clear away junk range specification
20421
20422 if Err then
20423 Set_Real_Range_Specification (Def, Empty);
20424 end if;
20425 end if;
20426 end Process_Real_Range_Specification;
20427
20428 ---------------------
20429 -- Process_Subtype --
20430 ---------------------
20431
20432 function Process_Subtype
20433 (S : Node_Id;
20434 Related_Nod : Node_Id;
20435 Related_Id : Entity_Id := Empty;
20436 Suffix : Character := ' ') return Entity_Id
20437 is
20438 P : Node_Id;
20439 Def_Id : Entity_Id;
20440 Error_Node : Node_Id;
20441 Full_View_Id : Entity_Id;
20442 Subtype_Mark_Id : Entity_Id;
20443
20444 May_Have_Null_Exclusion : Boolean;
20445
20446 procedure Check_Incomplete (T : Entity_Id);
20447 -- Called to verify that an incomplete type is not used prematurely
20448
20449 ----------------------
20450 -- Check_Incomplete --
20451 ----------------------
20452
20453 procedure Check_Incomplete (T : Entity_Id) is
20454 begin
20455 -- Ada 2005 (AI-412): Incomplete subtypes are legal
20456
20457 if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
20458 and then
20459 not (Ada_Version >= Ada_2005
20460 and then
20461 (Nkind (Parent (T)) = N_Subtype_Declaration
20462 or else (Nkind (Parent (T)) = N_Subtype_Indication
20463 and then Nkind (Parent (Parent (T))) =
20464 N_Subtype_Declaration)))
20465 then
20466 Error_Msg_N ("invalid use of type before its full declaration", T);
20467 end if;
20468 end Check_Incomplete;
20469
20470 -- Start of processing for Process_Subtype
20471
20472 begin
20473 -- Case of no constraints present
20474
20475 if Nkind (S) /= N_Subtype_Indication then
20476 Find_Type (S);
20477 Check_Incomplete (S);
20478 P := Parent (S);
20479
20480 -- Ada 2005 (AI-231): Static check
20481
20482 if Ada_Version >= Ada_2005
20483 and then Present (P)
20484 and then Null_Exclusion_Present (P)
20485 and then Nkind (P) /= N_Access_To_Object_Definition
20486 and then not Is_Access_Type (Entity (S))
20487 then
20488 Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
20489 end if;
20490
20491 -- The following is ugly, can't we have a range or even a flag???
20492
20493 May_Have_Null_Exclusion :=
20494 Nkind_In (P, N_Access_Definition,
20495 N_Access_Function_Definition,
20496 N_Access_Procedure_Definition,
20497 N_Access_To_Object_Definition,
20498 N_Allocator,
20499 N_Component_Definition)
20500 or else
20501 Nkind_In (P, N_Derived_Type_Definition,
20502 N_Discriminant_Specification,
20503 N_Formal_Object_Declaration,
20504 N_Object_Declaration,
20505 N_Object_Renaming_Declaration,
20506 N_Parameter_Specification,
20507 N_Subtype_Declaration);
20508
20509 -- Create an Itype that is a duplicate of Entity (S) but with the
20510 -- null-exclusion attribute.
20511
20512 if May_Have_Null_Exclusion
20513 and then Is_Access_Type (Entity (S))
20514 and then Null_Exclusion_Present (P)
20515
20516 -- No need to check the case of an access to object definition.
20517 -- It is correct to define double not-null pointers.
20518
20519 -- Example:
20520 -- type Not_Null_Int_Ptr is not null access Integer;
20521 -- type Acc is not null access Not_Null_Int_Ptr;
20522
20523 and then Nkind (P) /= N_Access_To_Object_Definition
20524 then
20525 if Can_Never_Be_Null (Entity (S)) then
20526 case Nkind (Related_Nod) is
20527 when N_Full_Type_Declaration =>
20528 if Nkind (Type_Definition (Related_Nod))
20529 in N_Array_Type_Definition
20530 then
20531 Error_Node :=
20532 Subtype_Indication
20533 (Component_Definition
20534 (Type_Definition (Related_Nod)));
20535 else
20536 Error_Node :=
20537 Subtype_Indication (Type_Definition (Related_Nod));
20538 end if;
20539
20540 when N_Subtype_Declaration =>
20541 Error_Node := Subtype_Indication (Related_Nod);
20542
20543 when N_Object_Declaration =>
20544 Error_Node := Object_Definition (Related_Nod);
20545
20546 when N_Component_Declaration =>
20547 Error_Node :=
20548 Subtype_Indication (Component_Definition (Related_Nod));
20549
20550 when N_Allocator =>
20551 Error_Node := Expression (Related_Nod);
20552
20553 when others =>
20554 pragma Assert (False);
20555 Error_Node := Related_Nod;
20556 end case;
20557
20558 Error_Msg_NE
20559 ("`NOT NULL` not allowed (& already excludes null)",
20560 Error_Node,
20561 Entity (S));
20562 end if;
20563
20564 Set_Etype (S,
20565 Create_Null_Excluding_Itype
20566 (T => Entity (S),
20567 Related_Nod => P));
20568 Set_Entity (S, Etype (S));
20569 end if;
20570
20571 return Entity (S);
20572
20573 -- Case of constraint present, so that we have an N_Subtype_Indication
20574 -- node (this node is created only if constraints are present).
20575
20576 else
20577 Find_Type (Subtype_Mark (S));
20578
20579 if Nkind (Parent (S)) /= N_Access_To_Object_Definition
20580 and then not
20581 (Nkind (Parent (S)) = N_Subtype_Declaration
20582 and then Is_Itype (Defining_Identifier (Parent (S))))
20583 then
20584 Check_Incomplete (Subtype_Mark (S));
20585 end if;
20586
20587 P := Parent (S);
20588 Subtype_Mark_Id := Entity (Subtype_Mark (S));
20589
20590 -- Explicit subtype declaration case
20591
20592 if Nkind (P) = N_Subtype_Declaration then
20593 Def_Id := Defining_Identifier (P);
20594
20595 -- Explicit derived type definition case
20596
20597 elsif Nkind (P) = N_Derived_Type_Definition then
20598 Def_Id := Defining_Identifier (Parent (P));
20599
20600 -- Implicit case, the Def_Id must be created as an implicit type.
20601 -- The one exception arises in the case of concurrent types, array
20602 -- and access types, where other subsidiary implicit types may be
20603 -- created and must appear before the main implicit type. In these
20604 -- cases we leave Def_Id set to Empty as a signal that Create_Itype
20605 -- has not yet been called to create Def_Id.
20606
20607 else
20608 if Is_Array_Type (Subtype_Mark_Id)
20609 or else Is_Concurrent_Type (Subtype_Mark_Id)
20610 or else Is_Access_Type (Subtype_Mark_Id)
20611 then
20612 Def_Id := Empty;
20613
20614 -- For the other cases, we create a new unattached Itype,
20615 -- and set the indication to ensure it gets attached later.
20616
20617 else
20618 Def_Id :=
20619 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
20620 end if;
20621 end if;
20622
20623 -- If the kind of constraint is invalid for this kind of type,
20624 -- then give an error, and then pretend no constraint was given.
20625
20626 if not Is_Valid_Constraint_Kind
20627 (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
20628 then
20629 Error_Msg_N
20630 ("incorrect constraint for this kind of type", Constraint (S));
20631
20632 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
20633
20634 -- Set Ekind of orphan itype, to prevent cascaded errors
20635
20636 if Present (Def_Id) then
20637 Set_Ekind (Def_Id, Ekind (Any_Type));
20638 end if;
20639
20640 -- Make recursive call, having got rid of the bogus constraint
20641
20642 return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
20643 end if;
20644
20645 -- Remaining processing depends on type. Select on Base_Type kind to
20646 -- ensure getting to the concrete type kind in the case of a private
20647 -- subtype (needed when only doing semantic analysis).
20648
20649 case Ekind (Base_Type (Subtype_Mark_Id)) is
20650 when Access_Kind =>
20651
20652 -- If this is a constraint on a class-wide type, discard it.
20653 -- There is currently no way to express a partial discriminant
20654 -- constraint on a type with unknown discriminants. This is
20655 -- a pathology that the ACATS wisely decides not to test.
20656
20657 if Is_Class_Wide_Type (Designated_Type (Subtype_Mark_Id)) then
20658 if Comes_From_Source (S) then
20659 Error_Msg_N
20660 ("constraint on class-wide type ignored??",
20661 Constraint (S));
20662 end if;
20663
20664 if Nkind (P) = N_Subtype_Declaration then
20665 Set_Subtype_Indication (P,
20666 New_Occurrence_Of (Subtype_Mark_Id, Sloc (S)));
20667 end if;
20668
20669 return Subtype_Mark_Id;
20670 end if;
20671
20672 Constrain_Access (Def_Id, S, Related_Nod);
20673
20674 if Expander_Active
20675 and then Is_Itype (Designated_Type (Def_Id))
20676 and then Nkind (Related_Nod) = N_Subtype_Declaration
20677 and then not Is_Incomplete_Type (Designated_Type (Def_Id))
20678 then
20679 Build_Itype_Reference
20680 (Designated_Type (Def_Id), Related_Nod);
20681 end if;
20682
20683 when Array_Kind =>
20684 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
20685
20686 when Decimal_Fixed_Point_Kind =>
20687 Constrain_Decimal (Def_Id, S);
20688
20689 when Enumeration_Kind =>
20690 Constrain_Enumeration (Def_Id, S);
20691 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
20692
20693 when Ordinary_Fixed_Point_Kind =>
20694 Constrain_Ordinary_Fixed (Def_Id, S);
20695
20696 when Float_Kind =>
20697 Constrain_Float (Def_Id, S);
20698
20699 when Integer_Kind =>
20700 Constrain_Integer (Def_Id, S);
20701 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
20702
20703 when E_Record_Type |
20704 E_Record_Subtype |
20705 Class_Wide_Kind |
20706 E_Incomplete_Type =>
20707 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
20708
20709 if Ekind (Def_Id) = E_Incomplete_Type then
20710 Set_Private_Dependents (Def_Id, New_Elmt_List);
20711 end if;
20712
20713 when Private_Kind =>
20714 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
20715 Set_Private_Dependents (Def_Id, New_Elmt_List);
20716
20717 -- In case of an invalid constraint prevent further processing
20718 -- since the type constructed is missing expected fields.
20719
20720 if Etype (Def_Id) = Any_Type then
20721 return Def_Id;
20722 end if;
20723
20724 -- If the full view is that of a task with discriminants,
20725 -- we must constrain both the concurrent type and its
20726 -- corresponding record type. Otherwise we will just propagate
20727 -- the constraint to the full view, if available.
20728
20729 if Present (Full_View (Subtype_Mark_Id))
20730 and then Has_Discriminants (Subtype_Mark_Id)
20731 and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
20732 then
20733 Full_View_Id :=
20734 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
20735
20736 Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
20737 Constrain_Concurrent (Full_View_Id, S,
20738 Related_Nod, Related_Id, Suffix);
20739 Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
20740 Set_Full_View (Def_Id, Full_View_Id);
20741
20742 -- Introduce an explicit reference to the private subtype,
20743 -- to prevent scope anomalies in gigi if first use appears
20744 -- in a nested context, e.g. a later function body.
20745 -- Should this be generated in other contexts than a full
20746 -- type declaration?
20747
20748 if Is_Itype (Def_Id)
20749 and then
20750 Nkind (Parent (P)) = N_Full_Type_Declaration
20751 then
20752 Build_Itype_Reference (Def_Id, Parent (P));
20753 end if;
20754
20755 else
20756 Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
20757 end if;
20758
20759 when Concurrent_Kind =>
20760 Constrain_Concurrent (Def_Id, S,
20761 Related_Nod, Related_Id, Suffix);
20762
20763 when others =>
20764 Error_Msg_N ("invalid subtype mark in subtype indication", S);
20765 end case;
20766
20767 -- Size and Convention are always inherited from the base type
20768
20769 Set_Size_Info (Def_Id, (Subtype_Mark_Id));
20770 Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
20771
20772 return Def_Id;
20773 end if;
20774 end Process_Subtype;
20775
20776 --------------------------------------------
20777 -- Propagate_Default_Init_Cond_Attributes --
20778 --------------------------------------------
20779
20780 procedure Propagate_Default_Init_Cond_Attributes
20781 (From_Typ : Entity_Id;
20782 To_Typ : Entity_Id;
20783 Parent_To_Derivation : Boolean := False;
20784 Private_To_Full_View : Boolean := False)
20785 is
20786 procedure Remove_Default_Init_Cond_Procedure (Typ : Entity_Id);
20787 -- Remove the default initial procedure (if any) from the rep chain of
20788 -- type Typ.
20789
20790 ----------------------------------------
20791 -- Remove_Default_Init_Cond_Procedure --
20792 ----------------------------------------
20793
20794 procedure Remove_Default_Init_Cond_Procedure (Typ : Entity_Id) is
20795 Found : Boolean := False;
20796 Prev : Entity_Id;
20797 Subp : Entity_Id;
20798
20799 begin
20800 Prev := Typ;
20801 Subp := Subprograms_For_Type (Typ);
20802 while Present (Subp) loop
20803 if Is_Default_Init_Cond_Procedure (Subp) then
20804 Found := True;
20805 exit;
20806 end if;
20807
20808 Prev := Subp;
20809 Subp := Subprograms_For_Type (Subp);
20810 end loop;
20811
20812 if Found then
20813 Set_Subprograms_For_Type (Prev, Subprograms_For_Type (Subp));
20814 Set_Subprograms_For_Type (Subp, Empty);
20815 end if;
20816 end Remove_Default_Init_Cond_Procedure;
20817
20818 -- Local variables
20819
20820 Inherit_Procedure : Boolean := False;
20821
20822 -- Start of processing for Propagate_Default_Init_Cond_Attributes
20823
20824 begin
20825 if Has_Default_Init_Cond (From_Typ) then
20826
20827 -- A derived type inherits the attributes from its parent type
20828
20829 if Parent_To_Derivation then
20830 Set_Has_Inherited_Default_Init_Cond (To_Typ);
20831
20832 -- A full view shares the attributes with its private view
20833
20834 else
20835 Set_Has_Default_Init_Cond (To_Typ);
20836 end if;
20837
20838 Inherit_Procedure := True;
20839
20840 -- Due to the order of expansion, a derived private type is processed
20841 -- by two routines which both attempt to set the attributes related
20842 -- to pragma Default_Initial_Condition - Build_Derived_Type and then
20843 -- Process_Full_View.
20844
20845 -- package Pack is
20846 -- type Parent_Typ is private
20847 -- with Default_Initial_Condition ...;
20848 -- private
20849 -- type Parent_Typ is ...;
20850 -- end Pack;
20851
20852 -- with Pack; use Pack;
20853 -- package Pack_2 is
20854 -- type Deriv_Typ is private
20855 -- with Default_Initial_Condition ...;
20856 -- private
20857 -- type Deriv_Typ is new Parent_Typ;
20858 -- end Pack_2;
20859
20860 -- When Build_Derived_Type operates, it sets the attributes on the
20861 -- full view without taking into account that the private view may
20862 -- define its own default initial condition procedure. This becomes
20863 -- apparent in Process_Full_View which must undo some of the work by
20864 -- Build_Derived_Type and propagate the attributes from the private
20865 -- to the full view.
20866
20867 if Private_To_Full_View then
20868 Set_Has_Inherited_Default_Init_Cond (To_Typ, False);
20869 Remove_Default_Init_Cond_Procedure (To_Typ);
20870 end if;
20871
20872 -- A type must inherit the default initial condition procedure from a
20873 -- parent type when the parent itself is inheriting the procedure or
20874 -- when it is defining one. This circuitry is also used when dealing
20875 -- with the private / full view of a type.
20876
20877 elsif Has_Inherited_Default_Init_Cond (From_Typ)
20878 or (Parent_To_Derivation
20879 and Present (Get_Pragma
20880 (From_Typ, Pragma_Default_Initial_Condition)))
20881 then
20882 Set_Has_Inherited_Default_Init_Cond (To_Typ);
20883 Inherit_Procedure := True;
20884 end if;
20885
20886 if Inherit_Procedure
20887 and then No (Default_Init_Cond_Procedure (To_Typ))
20888 then
20889 Set_Default_Init_Cond_Procedure
20890 (To_Typ, Default_Init_Cond_Procedure (From_Typ));
20891 end if;
20892 end Propagate_Default_Init_Cond_Attributes;
20893
20894 -----------------------------
20895 -- Record_Type_Declaration --
20896 -----------------------------
20897
20898 procedure Record_Type_Declaration
20899 (T : Entity_Id;
20900 N : Node_Id;
20901 Prev : Entity_Id)
20902 is
20903 Def : constant Node_Id := Type_Definition (N);
20904 Is_Tagged : Boolean;
20905 Tag_Comp : Entity_Id;
20906
20907 begin
20908 -- These flags must be initialized before calling Process_Discriminants
20909 -- because this routine makes use of them.
20910
20911 Set_Ekind (T, E_Record_Type);
20912 Set_Etype (T, T);
20913 Init_Size_Align (T);
20914 Set_Interfaces (T, No_Elist);
20915 Set_Stored_Constraint (T, No_Elist);
20916 Set_Default_SSO (T);
20917
20918 -- Normal case
20919
20920 if Ada_Version < Ada_2005 or else not Interface_Present (Def) then
20921 if Limited_Present (Def) then
20922 Check_SPARK_05_Restriction ("limited is not allowed", N);
20923 end if;
20924
20925 if Abstract_Present (Def) then
20926 Check_SPARK_05_Restriction ("abstract is not allowed", N);
20927 end if;
20928
20929 -- The flag Is_Tagged_Type might have already been set by
20930 -- Find_Type_Name if it detected an error for declaration T. This
20931 -- arises in the case of private tagged types where the full view
20932 -- omits the word tagged.
20933
20934 Is_Tagged :=
20935 Tagged_Present (Def)
20936 or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
20937
20938 Set_Is_Limited_Record (T, Limited_Present (Def));
20939
20940 if Is_Tagged then
20941 Set_Is_Tagged_Type (T, True);
20942 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
20943 end if;
20944
20945 -- Type is abstract if full declaration carries keyword, or if
20946 -- previous partial view did.
20947
20948 Set_Is_Abstract_Type (T, Is_Abstract_Type (T)
20949 or else Abstract_Present (Def));
20950
20951 else
20952 Check_SPARK_05_Restriction ("interface is not allowed", N);
20953
20954 Is_Tagged := True;
20955 Analyze_Interface_Declaration (T, Def);
20956
20957 if Present (Discriminant_Specifications (N)) then
20958 Error_Msg_N
20959 ("interface types cannot have discriminants",
20960 Defining_Identifier
20961 (First (Discriminant_Specifications (N))));
20962 end if;
20963 end if;
20964
20965 -- First pass: if there are self-referential access components,
20966 -- create the required anonymous access type declarations, and if
20967 -- need be an incomplete type declaration for T itself.
20968
20969 Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
20970
20971 if Ada_Version >= Ada_2005
20972 and then Present (Interface_List (Def))
20973 then
20974 Check_Interfaces (N, Def);
20975
20976 declare
20977 Ifaces_List : Elist_Id;
20978
20979 begin
20980 -- Ada 2005 (AI-251): Collect the list of progenitors that are not
20981 -- already in the parents.
20982
20983 Collect_Interfaces
20984 (T => T,
20985 Ifaces_List => Ifaces_List,
20986 Exclude_Parents => True);
20987
20988 Set_Interfaces (T, Ifaces_List);
20989 end;
20990 end if;
20991
20992 -- Records constitute a scope for the component declarations within.
20993 -- The scope is created prior to the processing of these declarations.
20994 -- Discriminants are processed first, so that they are visible when
20995 -- processing the other components. The Ekind of the record type itself
20996 -- is set to E_Record_Type (subtypes appear as E_Record_Subtype).
20997
20998 -- Enter record scope
20999
21000 Push_Scope (T);
21001
21002 -- If an incomplete or private type declaration was already given for
21003 -- the type, then this scope already exists, and the discriminants have
21004 -- been declared within. We must verify that the full declaration
21005 -- matches the incomplete one.
21006
21007 Check_Or_Process_Discriminants (N, T, Prev);
21008
21009 Set_Is_Constrained (T, not Has_Discriminants (T));
21010 Set_Has_Delayed_Freeze (T, True);
21011
21012 -- For tagged types add a manually analyzed component corresponding
21013 -- to the component _tag, the corresponding piece of tree will be
21014 -- expanded as part of the freezing actions if it is not a CPP_Class.
21015
21016 if Is_Tagged then
21017
21018 -- Do not add the tag unless we are in expansion mode
21019
21020 if Expander_Active then
21021 Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
21022 Enter_Name (Tag_Comp);
21023
21024 Set_Ekind (Tag_Comp, E_Component);
21025 Set_Is_Tag (Tag_Comp);
21026 Set_Is_Aliased (Tag_Comp);
21027 Set_Etype (Tag_Comp, RTE (RE_Tag));
21028 Set_DT_Entry_Count (Tag_Comp, No_Uint);
21029 Set_Original_Record_Component (Tag_Comp, Tag_Comp);
21030 Init_Component_Location (Tag_Comp);
21031
21032 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
21033 -- implemented interfaces.
21034
21035 if Has_Interfaces (T) then
21036 Add_Interface_Tag_Components (N, T);
21037 end if;
21038 end if;
21039
21040 Make_Class_Wide_Type (T);
21041 Set_Direct_Primitive_Operations (T, New_Elmt_List);
21042 end if;
21043
21044 -- We must suppress range checks when processing record components in
21045 -- the presence of discriminants, since we don't want spurious checks to
21046 -- be generated during their analysis, but Suppress_Range_Checks flags
21047 -- must be reset the after processing the record definition.
21048
21049 -- Note: this is the only use of Kill_Range_Checks, and is a bit odd,
21050 -- couldn't we just use the normal range check suppression method here.
21051 -- That would seem cleaner ???
21052
21053 if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
21054 Set_Kill_Range_Checks (T, True);
21055 Record_Type_Definition (Def, Prev);
21056 Set_Kill_Range_Checks (T, False);
21057 else
21058 Record_Type_Definition (Def, Prev);
21059 end if;
21060
21061 -- Exit from record scope
21062
21063 End_Scope;
21064
21065 -- Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
21066 -- the implemented interfaces and associate them an aliased entity.
21067
21068 if Is_Tagged
21069 and then not Is_Empty_List (Interface_List (Def))
21070 then
21071 Derive_Progenitor_Subprograms (T, T);
21072 end if;
21073
21074 Check_Function_Writable_Actuals (N);
21075 end Record_Type_Declaration;
21076
21077 ----------------------------
21078 -- Record_Type_Definition --
21079 ----------------------------
21080
21081 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
21082 Component : Entity_Id;
21083 Ctrl_Components : Boolean := False;
21084 Final_Storage_Only : Boolean;
21085 T : Entity_Id;
21086
21087 begin
21088 if Ekind (Prev_T) = E_Incomplete_Type then
21089 T := Full_View (Prev_T);
21090 else
21091 T := Prev_T;
21092 end if;
21093
21094 -- In SPARK, tagged types and type extensions may only be declared in
21095 -- the specification of library unit packages.
21096
21097 if Present (Def) and then Is_Tagged_Type (T) then
21098 declare
21099 Typ : Node_Id;
21100 Ctxt : Node_Id;
21101
21102 begin
21103 if Nkind (Parent (Def)) = N_Full_Type_Declaration then
21104 Typ := Parent (Def);
21105 else
21106 pragma Assert
21107 (Nkind (Parent (Def)) = N_Derived_Type_Definition);
21108 Typ := Parent (Parent (Def));
21109 end if;
21110
21111 Ctxt := Parent (Typ);
21112
21113 if Nkind (Ctxt) = N_Package_Body
21114 and then Nkind (Parent (Ctxt)) = N_Compilation_Unit
21115 then
21116 Check_SPARK_05_Restriction
21117 ("type should be defined in package specification", Typ);
21118
21119 elsif Nkind (Ctxt) /= N_Package_Specification
21120 or else Nkind (Parent (Parent (Ctxt))) /= N_Compilation_Unit
21121 then
21122 Check_SPARK_05_Restriction
21123 ("type should be defined in library unit package", Typ);
21124 end if;
21125 end;
21126 end if;
21127
21128 Final_Storage_Only := not Is_Controlled (T);
21129
21130 -- Ada 2005: Check whether an explicit Limited is present in a derived
21131 -- type declaration.
21132
21133 if Nkind (Parent (Def)) = N_Derived_Type_Definition
21134 and then Limited_Present (Parent (Def))
21135 then
21136 Set_Is_Limited_Record (T);
21137 end if;
21138
21139 -- If the component list of a record type is defined by the reserved
21140 -- word null and there is no discriminant part, then the record type has
21141 -- no components and all records of the type are null records (RM 3.7)
21142 -- This procedure is also called to process the extension part of a
21143 -- record extension, in which case the current scope may have inherited
21144 -- components.
21145
21146 if No (Def)
21147 or else No (Component_List (Def))
21148 or else Null_Present (Component_List (Def))
21149 then
21150 if not Is_Tagged_Type (T) then
21151 Check_SPARK_05_Restriction ("untagged record cannot be null", Def);
21152 end if;
21153
21154 else
21155 Analyze_Declarations (Component_Items (Component_List (Def)));
21156
21157 if Present (Variant_Part (Component_List (Def))) then
21158 Check_SPARK_05_Restriction ("variant part is not allowed", Def);
21159 Analyze (Variant_Part (Component_List (Def)));
21160 end if;
21161 end if;
21162
21163 -- After completing the semantic analysis of the record definition,
21164 -- record components, both new and inherited, are accessible. Set their
21165 -- kind accordingly. Exclude malformed itypes from illegal declarations,
21166 -- whose Ekind may be void.
21167
21168 Component := First_Entity (Current_Scope);
21169 while Present (Component) loop
21170 if Ekind (Component) = E_Void
21171 and then not Is_Itype (Component)
21172 then
21173 Set_Ekind (Component, E_Component);
21174 Init_Component_Location (Component);
21175 end if;
21176
21177 if Has_Task (Etype (Component)) then
21178 Set_Has_Task (T);
21179 end if;
21180
21181 if Has_Protected (Etype (Component)) then
21182 Set_Has_Protected (T);
21183 end if;
21184
21185 if Ekind (Component) /= E_Component then
21186 null;
21187
21188 -- Do not set Has_Controlled_Component on a class-wide equivalent
21189 -- type. See Make_CW_Equivalent_Type.
21190
21191 elsif not Is_Class_Wide_Equivalent_Type (T)
21192 and then (Has_Controlled_Component (Etype (Component))
21193 or else (Chars (Component) /= Name_uParent
21194 and then Is_Controlled (Etype (Component))))
21195 then
21196 Set_Has_Controlled_Component (T, True);
21197 Final_Storage_Only :=
21198 Final_Storage_Only
21199 and then Finalize_Storage_Only (Etype (Component));
21200 Ctrl_Components := True;
21201 end if;
21202
21203 Next_Entity (Component);
21204 end loop;
21205
21206 -- A Type is Finalize_Storage_Only only if all its controlled components
21207 -- are also.
21208
21209 if Ctrl_Components then
21210 Set_Finalize_Storage_Only (T, Final_Storage_Only);
21211 end if;
21212
21213 -- Place reference to end record on the proper entity, which may
21214 -- be a partial view.
21215
21216 if Present (Def) then
21217 Process_End_Label (Def, 'e', Prev_T);
21218 end if;
21219 end Record_Type_Definition;
21220
21221 ------------------------
21222 -- Replace_Components --
21223 ------------------------
21224
21225 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id) is
21226 function Process (N : Node_Id) return Traverse_Result;
21227
21228 -------------
21229 -- Process --
21230 -------------
21231
21232 function Process (N : Node_Id) return Traverse_Result is
21233 Comp : Entity_Id;
21234
21235 begin
21236 if Nkind (N) = N_Discriminant_Specification then
21237 Comp := First_Discriminant (Typ);
21238 while Present (Comp) loop
21239 if Chars (Comp) = Chars (Defining_Identifier (N)) then
21240 Set_Defining_Identifier (N, Comp);
21241 exit;
21242 end if;
21243
21244 Next_Discriminant (Comp);
21245 end loop;
21246
21247 elsif Nkind (N) = N_Component_Declaration then
21248 Comp := First_Component (Typ);
21249 while Present (Comp) loop
21250 if Chars (Comp) = Chars (Defining_Identifier (N)) then
21251 Set_Defining_Identifier (N, Comp);
21252 exit;
21253 end if;
21254
21255 Next_Component (Comp);
21256 end loop;
21257 end if;
21258
21259 return OK;
21260 end Process;
21261
21262 procedure Replace is new Traverse_Proc (Process);
21263
21264 -- Start of processing for Replace_Components
21265
21266 begin
21267 Replace (Decl);
21268 end Replace_Components;
21269
21270 -------------------------------
21271 -- Set_Completion_Referenced --
21272 -------------------------------
21273
21274 procedure Set_Completion_Referenced (E : Entity_Id) is
21275 begin
21276 -- If in main unit, mark entity that is a completion as referenced,
21277 -- warnings go on the partial view when needed.
21278
21279 if In_Extended_Main_Source_Unit (E) then
21280 Set_Referenced (E);
21281 end if;
21282 end Set_Completion_Referenced;
21283
21284 ---------------------
21285 -- Set_Default_SSO --
21286 ---------------------
21287
21288 procedure Set_Default_SSO (T : Entity_Id) is
21289 begin
21290 case Opt.Default_SSO is
21291 when ' ' =>
21292 null;
21293 when 'L' =>
21294 Set_SSO_Set_Low_By_Default (T, True);
21295 when 'H' =>
21296 Set_SSO_Set_High_By_Default (T, True);
21297 when others =>
21298 raise Program_Error;
21299 end case;
21300 end Set_Default_SSO;
21301
21302 ---------------------
21303 -- Set_Fixed_Range --
21304 ---------------------
21305
21306 -- The range for fixed-point types is complicated by the fact that we
21307 -- do not know the exact end points at the time of the declaration. This
21308 -- is true for three reasons:
21309
21310 -- A size clause may affect the fudging of the end-points.
21311 -- A small clause may affect the values of the end-points.
21312 -- We try to include the end-points if it does not affect the size.
21313
21314 -- This means that the actual end-points must be established at the
21315 -- point when the type is frozen. Meanwhile, we first narrow the range
21316 -- as permitted (so that it will fit if necessary in a small specified
21317 -- size), and then build a range subtree with these narrowed bounds.
21318 -- Set_Fixed_Range constructs the range from real literal values, and
21319 -- sets the range as the Scalar_Range of the given fixed-point type entity.
21320
21321 -- The parent of this range is set to point to the entity so that it is
21322 -- properly hooked into the tree (unlike normal Scalar_Range entries for
21323 -- other scalar types, which are just pointers to the range in the
21324 -- original tree, this would otherwise be an orphan).
21325
21326 -- The tree is left unanalyzed. When the type is frozen, the processing
21327 -- in Freeze.Freeze_Fixed_Point_Type notices that the range is not
21328 -- analyzed, and uses this as an indication that it should complete
21329 -- work on the range (it will know the final small and size values).
21330
21331 procedure Set_Fixed_Range
21332 (E : Entity_Id;
21333 Loc : Source_Ptr;
21334 Lo : Ureal;
21335 Hi : Ureal)
21336 is
21337 S : constant Node_Id :=
21338 Make_Range (Loc,
21339 Low_Bound => Make_Real_Literal (Loc, Lo),
21340 High_Bound => Make_Real_Literal (Loc, Hi));
21341 begin
21342 Set_Scalar_Range (E, S);
21343 Set_Parent (S, E);
21344
21345 -- Before the freeze point, the bounds of a fixed point are universal
21346 -- and carry the corresponding type.
21347
21348 Set_Etype (Low_Bound (S), Universal_Real);
21349 Set_Etype (High_Bound (S), Universal_Real);
21350 end Set_Fixed_Range;
21351
21352 ----------------------------------
21353 -- Set_Scalar_Range_For_Subtype --
21354 ----------------------------------
21355
21356 procedure Set_Scalar_Range_For_Subtype
21357 (Def_Id : Entity_Id;
21358 R : Node_Id;
21359 Subt : Entity_Id)
21360 is
21361 Kind : constant Entity_Kind := Ekind (Def_Id);
21362
21363 begin
21364 -- Defend against previous error
21365
21366 if Nkind (R) = N_Error then
21367 return;
21368 end if;
21369
21370 Set_Scalar_Range (Def_Id, R);
21371
21372 -- We need to link the range into the tree before resolving it so
21373 -- that types that are referenced, including importantly the subtype
21374 -- itself, are properly frozen (Freeze_Expression requires that the
21375 -- expression be properly linked into the tree). Of course if it is
21376 -- already linked in, then we do not disturb the current link.
21377
21378 if No (Parent (R)) then
21379 Set_Parent (R, Def_Id);
21380 end if;
21381
21382 -- Reset the kind of the subtype during analysis of the range, to
21383 -- catch possible premature use in the bounds themselves.
21384
21385 Set_Ekind (Def_Id, E_Void);
21386 Process_Range_Expr_In_Decl (R, Subt, Subtyp => Def_Id);
21387 Set_Ekind (Def_Id, Kind);
21388 end Set_Scalar_Range_For_Subtype;
21389
21390 --------------------------------------------------------
21391 -- Set_Stored_Constraint_From_Discriminant_Constraint --
21392 --------------------------------------------------------
21393
21394 procedure Set_Stored_Constraint_From_Discriminant_Constraint
21395 (E : Entity_Id)
21396 is
21397 begin
21398 -- Make sure set if encountered during Expand_To_Stored_Constraint
21399
21400 Set_Stored_Constraint (E, No_Elist);
21401
21402 -- Give it the right value
21403
21404 if Is_Constrained (E) and then Has_Discriminants (E) then
21405 Set_Stored_Constraint (E,
21406 Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
21407 end if;
21408 end Set_Stored_Constraint_From_Discriminant_Constraint;
21409
21410 -------------------------------------
21411 -- Signed_Integer_Type_Declaration --
21412 -------------------------------------
21413
21414 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
21415 Implicit_Base : Entity_Id;
21416 Base_Typ : Entity_Id;
21417 Lo_Val : Uint;
21418 Hi_Val : Uint;
21419 Errs : Boolean := False;
21420 Lo : Node_Id;
21421 Hi : Node_Id;
21422
21423 function Can_Derive_From (E : Entity_Id) return Boolean;
21424 -- Determine whether given bounds allow derivation from specified type
21425
21426 procedure Check_Bound (Expr : Node_Id);
21427 -- Check bound to make sure it is integral and static. If not, post
21428 -- appropriate error message and set Errs flag
21429
21430 ---------------------
21431 -- Can_Derive_From --
21432 ---------------------
21433
21434 -- Note we check both bounds against both end values, to deal with
21435 -- strange types like ones with a range of 0 .. -12341234.
21436
21437 function Can_Derive_From (E : Entity_Id) return Boolean is
21438 Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
21439 Hi : constant Uint := Expr_Value (Type_High_Bound (E));
21440 begin
21441 return Lo <= Lo_Val and then Lo_Val <= Hi
21442 and then
21443 Lo <= Hi_Val and then Hi_Val <= Hi;
21444 end Can_Derive_From;
21445
21446 -----------------
21447 -- Check_Bound --
21448 -----------------
21449
21450 procedure Check_Bound (Expr : Node_Id) is
21451 begin
21452 -- If a range constraint is used as an integer type definition, each
21453 -- bound of the range must be defined by a static expression of some
21454 -- integer type, but the two bounds need not have the same integer
21455 -- type (Negative bounds are allowed.) (RM 3.5.4)
21456
21457 if not Is_Integer_Type (Etype (Expr)) then
21458 Error_Msg_N
21459 ("integer type definition bounds must be of integer type", Expr);
21460 Errs := True;
21461
21462 elsif not Is_OK_Static_Expression (Expr) then
21463 Flag_Non_Static_Expr
21464 ("non-static expression used for integer type bound!", Expr);
21465 Errs := True;
21466
21467 -- The bounds are folded into literals, and we set their type to be
21468 -- universal, to avoid typing difficulties: we cannot set the type
21469 -- of the literal to the new type, because this would be a forward
21470 -- reference for the back end, and if the original type is user-
21471 -- defined this can lead to spurious semantic errors (e.g. 2928-003).
21472
21473 else
21474 if Is_Entity_Name (Expr) then
21475 Fold_Uint (Expr, Expr_Value (Expr), True);
21476 end if;
21477
21478 Set_Etype (Expr, Universal_Integer);
21479 end if;
21480 end Check_Bound;
21481
21482 -- Start of processing for Signed_Integer_Type_Declaration
21483
21484 begin
21485 -- Create an anonymous base type
21486
21487 Implicit_Base :=
21488 Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
21489
21490 -- Analyze and check the bounds, they can be of any integer type
21491
21492 Lo := Low_Bound (Def);
21493 Hi := High_Bound (Def);
21494
21495 -- Arbitrarily use Integer as the type if either bound had an error
21496
21497 if Hi = Error or else Lo = Error then
21498 Base_Typ := Any_Integer;
21499 Set_Error_Posted (T, True);
21500
21501 -- Here both bounds are OK expressions
21502
21503 else
21504 Analyze_And_Resolve (Lo, Any_Integer);
21505 Analyze_And_Resolve (Hi, Any_Integer);
21506
21507 Check_Bound (Lo);
21508 Check_Bound (Hi);
21509
21510 if Errs then
21511 Hi := Type_High_Bound (Standard_Long_Long_Integer);
21512 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
21513 end if;
21514
21515 -- Find type to derive from
21516
21517 Lo_Val := Expr_Value (Lo);
21518 Hi_Val := Expr_Value (Hi);
21519
21520 if Can_Derive_From (Standard_Short_Short_Integer) then
21521 Base_Typ := Base_Type (Standard_Short_Short_Integer);
21522
21523 elsif Can_Derive_From (Standard_Short_Integer) then
21524 Base_Typ := Base_Type (Standard_Short_Integer);
21525
21526 elsif Can_Derive_From (Standard_Integer) then
21527 Base_Typ := Base_Type (Standard_Integer);
21528
21529 elsif Can_Derive_From (Standard_Long_Integer) then
21530 Base_Typ := Base_Type (Standard_Long_Integer);
21531
21532 elsif Can_Derive_From (Standard_Long_Long_Integer) then
21533 Check_Restriction (No_Long_Long_Integers, Def);
21534 Base_Typ := Base_Type (Standard_Long_Long_Integer);
21535
21536 else
21537 Base_Typ := Base_Type (Standard_Long_Long_Integer);
21538 Error_Msg_N ("integer type definition bounds out of range", Def);
21539 Hi := Type_High_Bound (Standard_Long_Long_Integer);
21540 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
21541 end if;
21542 end if;
21543
21544 -- Complete both implicit base and declared first subtype entities. The
21545 -- inheritance of the rep item chain ensures that SPARK-related pragmas
21546 -- are not clobbered when the signed integer type acts as a full view of
21547 -- a private type.
21548
21549 Set_Etype (Implicit_Base, Base_Typ);
21550 Set_Size_Info (Implicit_Base, Base_Typ);
21551 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
21552 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
21553 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
21554
21555 Set_Ekind (T, E_Signed_Integer_Subtype);
21556 Set_Etype (T, Implicit_Base);
21557 Set_Size_Info (T, Implicit_Base);
21558 Inherit_Rep_Item_Chain (T, Implicit_Base);
21559 Set_Scalar_Range (T, Def);
21560 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
21561 Set_Is_Constrained (T);
21562 end Signed_Integer_Type_Declaration;
21563
21564 end Sem_Ch3;