[multiple changes]
[gcc.git] / gcc / ada / sem_ch3.adb
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ C H 3 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2015, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
25
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Debug; use Debug;
30 with Elists; use Elists;
31 with Einfo; use Einfo;
32 with Errout; use Errout;
33 with Eval_Fat; use Eval_Fat;
34 with Exp_Ch3; use Exp_Ch3;
35 with Exp_Ch9; use Exp_Ch9;
36 with Exp_Disp; use Exp_Disp;
37 with Exp_Dist; use Exp_Dist;
38 with Exp_Tss; use Exp_Tss;
39 with Exp_Util; use Exp_Util;
40 with Fname; use Fname;
41 with Freeze; use Freeze;
42 with Ghost; use Ghost;
43 with Itypes; use Itypes;
44 with Layout; use Layout;
45 with Lib; use Lib;
46 with Lib.Xref; use Lib.Xref;
47 with Namet; use Namet;
48 with Nmake; use Nmake;
49 with Opt; use Opt;
50 with Restrict; use Restrict;
51 with Rident; use Rident;
52 with Rtsfind; use Rtsfind;
53 with Sem; use Sem;
54 with Sem_Aux; use Sem_Aux;
55 with Sem_Case; use Sem_Case;
56 with Sem_Cat; use Sem_Cat;
57 with Sem_Ch6; use Sem_Ch6;
58 with Sem_Ch7; use Sem_Ch7;
59 with Sem_Ch8; use Sem_Ch8;
60 with Sem_Ch10; use Sem_Ch10;
61 with Sem_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. This is an expansion activity
1776 -- that must be suppressed for ASIS because it leads to
1777 -- gigi elaboration issues in annotate mode.
1778
1779 if not ASIS_Mode then
1780 Set_Has_Delayed_Freeze (New_Subp);
1781 end if;
1782 end if;
1783
1784 <<Continue>>
1785 Next_Elmt (Elmt);
1786 end loop;
1787
1788 Next_Elmt (Iface_Elmt);
1789 end loop;
1790
1791 if Restore_Scope then
1792 Pop_Scope;
1793 end if;
1794 end Add_Internal_Interface_Entities;
1795
1796 -----------------------------------
1797 -- Analyze_Component_Declaration --
1798 -----------------------------------
1799
1800 procedure Analyze_Component_Declaration (N : Node_Id) is
1801 Loc : constant Source_Ptr := Sloc (Component_Definition (N));
1802 Id : constant Entity_Id := Defining_Identifier (N);
1803 E : constant Node_Id := Expression (N);
1804 Typ : constant Node_Id :=
1805 Subtype_Indication (Component_Definition (N));
1806 T : Entity_Id;
1807 P : Entity_Id;
1808
1809 function Contains_POC (Constr : Node_Id) return Boolean;
1810 -- Determines whether a constraint uses the discriminant of a record
1811 -- type thus becoming a per-object constraint (POC).
1812
1813 function Is_Known_Limited (Typ : Entity_Id) return Boolean;
1814 -- Typ is the type of the current component, check whether this type is
1815 -- a limited type. Used to validate declaration against that of
1816 -- enclosing record.
1817
1818 ------------------
1819 -- Contains_POC --
1820 ------------------
1821
1822 function Contains_POC (Constr : Node_Id) return Boolean is
1823 begin
1824 -- Prevent cascaded errors
1825
1826 if Error_Posted (Constr) then
1827 return False;
1828 end if;
1829
1830 case Nkind (Constr) is
1831 when N_Attribute_Reference =>
1832 return Attribute_Name (Constr) = Name_Access
1833 and then Prefix (Constr) = Scope (Entity (Prefix (Constr)));
1834
1835 when N_Discriminant_Association =>
1836 return Denotes_Discriminant (Expression (Constr));
1837
1838 when N_Identifier =>
1839 return Denotes_Discriminant (Constr);
1840
1841 when N_Index_Or_Discriminant_Constraint =>
1842 declare
1843 IDC : Node_Id;
1844
1845 begin
1846 IDC := First (Constraints (Constr));
1847 while Present (IDC) loop
1848
1849 -- One per-object constraint is sufficient
1850
1851 if Contains_POC (IDC) then
1852 return True;
1853 end if;
1854
1855 Next (IDC);
1856 end loop;
1857
1858 return False;
1859 end;
1860
1861 when N_Range =>
1862 return Denotes_Discriminant (Low_Bound (Constr))
1863 or else
1864 Denotes_Discriminant (High_Bound (Constr));
1865
1866 when N_Range_Constraint =>
1867 return Denotes_Discriminant (Range_Expression (Constr));
1868
1869 when others =>
1870 return False;
1871
1872 end case;
1873 end Contains_POC;
1874
1875 ----------------------
1876 -- Is_Known_Limited --
1877 ----------------------
1878
1879 function Is_Known_Limited (Typ : Entity_Id) return Boolean is
1880 P : constant Entity_Id := Etype (Typ);
1881 R : constant Entity_Id := Root_Type (Typ);
1882
1883 begin
1884 if Is_Limited_Record (Typ) then
1885 return True;
1886
1887 -- If the root type is limited (and not a limited interface)
1888 -- so is the current type
1889
1890 elsif Is_Limited_Record (R)
1891 and then (not Is_Interface (R) or else not Is_Limited_Interface (R))
1892 then
1893 return True;
1894
1895 -- Else the type may have a limited interface progenitor, but a
1896 -- limited record parent.
1897
1898 elsif R /= P and then Is_Limited_Record (P) then
1899 return True;
1900
1901 else
1902 return False;
1903 end if;
1904 end Is_Known_Limited;
1905
1906 -- Start of processing for Analyze_Component_Declaration
1907
1908 begin
1909 Generate_Definition (Id);
1910 Enter_Name (Id);
1911
1912 if Present (Typ) then
1913 T := Find_Type_Of_Object
1914 (Subtype_Indication (Component_Definition (N)), N);
1915
1916 if not Nkind_In (Typ, N_Identifier, N_Expanded_Name) then
1917 Check_SPARK_05_Restriction ("subtype mark required", Typ);
1918 end if;
1919
1920 -- Ada 2005 (AI-230): Access Definition case
1921
1922 else
1923 pragma Assert (Present
1924 (Access_Definition (Component_Definition (N))));
1925
1926 T := Access_Definition
1927 (Related_Nod => N,
1928 N => Access_Definition (Component_Definition (N)));
1929 Set_Is_Local_Anonymous_Access (T);
1930
1931 -- Ada 2005 (AI-254)
1932
1933 if Present (Access_To_Subprogram_Definition
1934 (Access_Definition (Component_Definition (N))))
1935 and then Protected_Present (Access_To_Subprogram_Definition
1936 (Access_Definition
1937 (Component_Definition (N))))
1938 then
1939 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1940 end if;
1941 end if;
1942
1943 -- If the subtype is a constrained subtype of the enclosing record,
1944 -- (which must have a partial view) the back-end does not properly
1945 -- handle the recursion. Rewrite the component declaration with an
1946 -- explicit subtype indication, which is acceptable to Gigi. We can copy
1947 -- the tree directly because side effects have already been removed from
1948 -- discriminant constraints.
1949
1950 if Ekind (T) = E_Access_Subtype
1951 and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
1952 and then Comes_From_Source (T)
1953 and then Nkind (Parent (T)) = N_Subtype_Declaration
1954 and then Etype (Directly_Designated_Type (T)) = Current_Scope
1955 then
1956 Rewrite
1957 (Subtype_Indication (Component_Definition (N)),
1958 New_Copy_Tree (Subtype_Indication (Parent (T))));
1959 T := Find_Type_Of_Object
1960 (Subtype_Indication (Component_Definition (N)), N);
1961 end if;
1962
1963 -- If the component declaration includes a default expression, then we
1964 -- check that the component is not of a limited type (RM 3.7(5)),
1965 -- and do the special preanalysis of the expression (see section on
1966 -- "Handling of Default and Per-Object Expressions" in the spec of
1967 -- package Sem).
1968
1969 if Present (E) then
1970 Check_SPARK_05_Restriction ("default expression is not allowed", E);
1971 Preanalyze_Default_Expression (E, T);
1972 Check_Initialization (T, E);
1973
1974 if Ada_Version >= Ada_2005
1975 and then Ekind (T) = E_Anonymous_Access_Type
1976 and then Etype (E) /= Any_Type
1977 then
1978 -- Check RM 3.9.2(9): "if the expected type for an expression is
1979 -- an anonymous access-to-specific tagged type, then the object
1980 -- designated by the expression shall not be dynamically tagged
1981 -- unless it is a controlling operand in a call on a dispatching
1982 -- operation"
1983
1984 if Is_Tagged_Type (Directly_Designated_Type (T))
1985 and then
1986 Ekind (Directly_Designated_Type (T)) /= E_Class_Wide_Type
1987 and then
1988 Ekind (Directly_Designated_Type (Etype (E))) =
1989 E_Class_Wide_Type
1990 then
1991 Error_Msg_N
1992 ("access to specific tagged type required (RM 3.9.2(9))", E);
1993 end if;
1994
1995 -- (Ada 2005: AI-230): Accessibility check for anonymous
1996 -- components
1997
1998 if Type_Access_Level (Etype (E)) >
1999 Deepest_Type_Access_Level (T)
2000 then
2001 Error_Msg_N
2002 ("expression has deeper access level than component " &
2003 "(RM 3.10.2 (12.2))", E);
2004 end if;
2005
2006 -- The initialization expression is a reference to an access
2007 -- discriminant. The type of the discriminant is always deeper
2008 -- than any access type.
2009
2010 if Ekind (Etype (E)) = E_Anonymous_Access_Type
2011 and then Is_Entity_Name (E)
2012 and then Ekind (Entity (E)) = E_In_Parameter
2013 and then Present (Discriminal_Link (Entity (E)))
2014 then
2015 Error_Msg_N
2016 ("discriminant has deeper accessibility level than target",
2017 E);
2018 end if;
2019 end if;
2020 end if;
2021
2022 -- The parent type may be a private view with unknown discriminants,
2023 -- and thus unconstrained. Regular components must be constrained.
2024
2025 if Is_Indefinite_Subtype (T) and then Chars (Id) /= Name_uParent then
2026 if Is_Class_Wide_Type (T) then
2027 Error_Msg_N
2028 ("class-wide subtype with unknown discriminants" &
2029 " in component declaration",
2030 Subtype_Indication (Component_Definition (N)));
2031 else
2032 Error_Msg_N
2033 ("unconstrained subtype in component declaration",
2034 Subtype_Indication (Component_Definition (N)));
2035 end if;
2036
2037 -- Components cannot be abstract, except for the special case of
2038 -- the _Parent field (case of extending an abstract tagged type)
2039
2040 elsif Is_Abstract_Type (T) and then Chars (Id) /= Name_uParent then
2041 Error_Msg_N ("type of a component cannot be abstract", N);
2042 end if;
2043
2044 Set_Etype (Id, T);
2045 Set_Is_Aliased (Id, Aliased_Present (Component_Definition (N)));
2046
2047 -- The component declaration may have a per-object constraint, set
2048 -- the appropriate flag in the defining identifier of the subtype.
2049
2050 if Present (Subtype_Indication (Component_Definition (N))) then
2051 declare
2052 Sindic : constant Node_Id :=
2053 Subtype_Indication (Component_Definition (N));
2054 begin
2055 if Nkind (Sindic) = N_Subtype_Indication
2056 and then Present (Constraint (Sindic))
2057 and then Contains_POC (Constraint (Sindic))
2058 then
2059 Set_Has_Per_Object_Constraint (Id);
2060 end if;
2061 end;
2062 end if;
2063
2064 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
2065 -- out some static checks.
2066
2067 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (T) then
2068 Null_Exclusion_Static_Checks (N);
2069 end if;
2070
2071 -- If this component is private (or depends on a private type), flag the
2072 -- record type to indicate that some operations are not available.
2073
2074 P := Private_Component (T);
2075
2076 if Present (P) then
2077
2078 -- Check for circular definitions
2079
2080 if P = Any_Type then
2081 Set_Etype (Id, Any_Type);
2082
2083 -- There is a gap in the visibility of operations only if the
2084 -- component type is not defined in the scope of the record type.
2085
2086 elsif Scope (P) = Scope (Current_Scope) then
2087 null;
2088
2089 elsif Is_Limited_Type (P) then
2090 Set_Is_Limited_Composite (Current_Scope);
2091
2092 else
2093 Set_Is_Private_Composite (Current_Scope);
2094 end if;
2095 end if;
2096
2097 if P /= Any_Type
2098 and then Is_Limited_Type (T)
2099 and then Chars (Id) /= Name_uParent
2100 and then Is_Tagged_Type (Current_Scope)
2101 then
2102 if Is_Derived_Type (Current_Scope)
2103 and then not Is_Known_Limited (Current_Scope)
2104 then
2105 Error_Msg_N
2106 ("extension of nonlimited type cannot have limited components",
2107 N);
2108
2109 if Is_Interface (Root_Type (Current_Scope)) then
2110 Error_Msg_N
2111 ("\limitedness is not inherited from limited interface", N);
2112 Error_Msg_N ("\add LIMITED to type indication", N);
2113 end if;
2114
2115 Explain_Limited_Type (T, N);
2116 Set_Etype (Id, Any_Type);
2117 Set_Is_Limited_Composite (Current_Scope, False);
2118
2119 elsif not Is_Derived_Type (Current_Scope)
2120 and then not Is_Limited_Record (Current_Scope)
2121 and then not Is_Concurrent_Type (Current_Scope)
2122 then
2123 Error_Msg_N
2124 ("nonlimited tagged type cannot have limited components", N);
2125 Explain_Limited_Type (T, N);
2126 Set_Etype (Id, Any_Type);
2127 Set_Is_Limited_Composite (Current_Scope, False);
2128 end if;
2129 end if;
2130
2131 -- If the component is an unconstrained task or protected type with
2132 -- discriminants, the component and the enclosing record are limited
2133 -- and the component is constrained by its default values. Compute
2134 -- its actual subtype, else it may be allocated the maximum size by
2135 -- the backend, and possibly overflow.
2136
2137 if Is_Concurrent_Type (T)
2138 and then not Is_Constrained (T)
2139 and then Has_Discriminants (T)
2140 and then not Has_Discriminants (Current_Scope)
2141 then
2142 declare
2143 Act_T : constant Entity_Id := Build_Default_Subtype (T, N);
2144
2145 begin
2146 Set_Etype (Id, Act_T);
2147
2148 -- Rewrite the component definition to use the constrained
2149 -- subtype.
2150
2151 Rewrite (Component_Definition (N),
2152 Make_Component_Definition (Loc,
2153 Subtype_Indication => New_Occurrence_Of (Act_T, Loc)));
2154 end;
2155 end if;
2156
2157 Set_Original_Record_Component (Id, Id);
2158
2159 if Has_Aspects (N) then
2160 Analyze_Aspect_Specifications (N, Id);
2161 end if;
2162
2163 Analyze_Dimension (N);
2164 end Analyze_Component_Declaration;
2165
2166 --------------------------
2167 -- Analyze_Declarations --
2168 --------------------------
2169
2170 procedure Analyze_Declarations (L : List_Id) is
2171 Decl : Node_Id;
2172
2173 procedure Adjust_Decl;
2174 -- Adjust Decl not to include implicit label declarations, since these
2175 -- have strange Sloc values that result in elaboration check problems.
2176 -- (They have the sloc of the label as found in the source, and that
2177 -- is ahead of the current declarative part).
2178
2179 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id);
2180 -- Determine whether Body_Decl denotes the body of a late controlled
2181 -- primitive (either Initialize, Adjust or Finalize). If this is the
2182 -- case, add a proper spec if the body lacks one. The spec is inserted
2183 -- before Body_Decl and immedately analyzed.
2184
2185 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id);
2186 -- Spec_Id is the entity of a package that may define abstract states.
2187 -- If the states have visible refinement, remove the visibility of each
2188 -- constituent at the end of the package body declarations.
2189
2190 -----------------
2191 -- Adjust_Decl --
2192 -----------------
2193
2194 procedure Adjust_Decl is
2195 begin
2196 while Present (Prev (Decl))
2197 and then Nkind (Decl) = N_Implicit_Label_Declaration
2198 loop
2199 Prev (Decl);
2200 end loop;
2201 end Adjust_Decl;
2202
2203 --------------------------------------
2204 -- Handle_Late_Controlled_Primitive --
2205 --------------------------------------
2206
2207 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id) is
2208 Body_Spec : constant Node_Id := Specification (Body_Decl);
2209 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2210 Loc : constant Source_Ptr := Sloc (Body_Id);
2211 Params : constant List_Id :=
2212 Parameter_Specifications (Body_Spec);
2213 Spec : Node_Id;
2214 Spec_Id : Entity_Id;
2215 Typ : Node_Id;
2216
2217 begin
2218 -- Consider only procedure bodies whose name matches one of the three
2219 -- controlled primitives.
2220
2221 if Nkind (Body_Spec) /= N_Procedure_Specification
2222 or else not Nam_In (Chars (Body_Id), Name_Adjust,
2223 Name_Finalize,
2224 Name_Initialize)
2225 then
2226 return;
2227
2228 -- A controlled primitive must have exactly one formal which is not
2229 -- an anonymous access type.
2230
2231 elsif List_Length (Params) /= 1 then
2232 return;
2233 end if;
2234
2235 Typ := Parameter_Type (First (Params));
2236
2237 if Nkind (Typ) = N_Access_Definition then
2238 return;
2239 end if;
2240
2241 Find_Type (Typ);
2242
2243 -- The type of the formal must be derived from [Limited_]Controlled
2244
2245 if not Is_Controlled (Entity (Typ)) then
2246 return;
2247 end if;
2248
2249 -- Check whether a specification exists for this body. We do not
2250 -- analyze the spec of the body in full, because it will be analyzed
2251 -- again when the body is properly analyzed, and we cannot create
2252 -- duplicate entries in the formals chain. We look for an explicit
2253 -- specification because the body may be an overriding operation and
2254 -- an inherited spec may be present.
2255
2256 Spec_Id := Current_Entity (Body_Id);
2257
2258 while Present (Spec_Id) loop
2259 if Ekind_In (Spec_Id, E_Procedure, E_Generic_Procedure)
2260 and then Scope (Spec_Id) = Current_Scope
2261 and then Present (First_Formal (Spec_Id))
2262 and then No (Next_Formal (First_Formal (Spec_Id)))
2263 and then Etype (First_Formal (Spec_Id)) = Entity (Typ)
2264 and then Comes_From_Source (Spec_Id)
2265 then
2266 return;
2267 end if;
2268
2269 Spec_Id := Homonym (Spec_Id);
2270 end loop;
2271
2272 -- At this point the body is known to be a late controlled primitive.
2273 -- Generate a matching spec and insert it before the body. Note the
2274 -- use of Copy_Separate_Tree - we want an entirely separate semantic
2275 -- tree in this case.
2276
2277 Spec := Copy_Separate_Tree (Body_Spec);
2278
2279 -- Ensure that the subprogram declaration does not inherit the null
2280 -- indicator from the body as we now have a proper spec/body pair.
2281
2282 Set_Null_Present (Spec, False);
2283
2284 Insert_Before_And_Analyze (Body_Decl,
2285 Make_Subprogram_Declaration (Loc, Specification => Spec));
2286 end Handle_Late_Controlled_Primitive;
2287
2288 --------------------------------
2289 -- Remove_Visible_Refinements --
2290 --------------------------------
2291
2292 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id) is
2293 State_Elmt : Elmt_Id;
2294 begin
2295 if Present (Abstract_States (Spec_Id)) then
2296 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2297 while Present (State_Elmt) loop
2298 Set_Has_Visible_Refinement (Node (State_Elmt), False);
2299 Next_Elmt (State_Elmt);
2300 end loop;
2301 end if;
2302 end Remove_Visible_Refinements;
2303
2304 -- Local variables
2305
2306 Context : Node_Id;
2307 Freeze_From : Entity_Id := Empty;
2308 Next_Decl : Node_Id;
2309 Spec_Id : Entity_Id;
2310
2311 Body_Seen : Boolean := False;
2312 -- Flag set when the first body [stub] is encountered
2313
2314 In_Package_Body : Boolean := False;
2315 -- Flag set when the current declaration list belongs to a package body
2316
2317 -- Start of processing for Analyze_Declarations
2318
2319 begin
2320 if Restriction_Check_Required (SPARK_05) then
2321 Check_Later_Vs_Basic_Declarations (L, During_Parsing => False);
2322 end if;
2323
2324 Decl := First (L);
2325 while Present (Decl) loop
2326
2327 -- Package spec cannot contain a package declaration in SPARK
2328
2329 if Nkind (Decl) = N_Package_Declaration
2330 and then Nkind (Parent (L)) = N_Package_Specification
2331 then
2332 Check_SPARK_05_Restriction
2333 ("package specification cannot contain a package declaration",
2334 Decl);
2335 end if;
2336
2337 -- Complete analysis of declaration
2338
2339 Analyze (Decl);
2340 Next_Decl := Next (Decl);
2341
2342 if No (Freeze_From) then
2343 Freeze_From := First_Entity (Current_Scope);
2344 end if;
2345
2346 -- At the end of a declarative part, freeze remaining entities
2347 -- declared in it. The end of the visible declarations of package
2348 -- specification is not the end of a declarative part if private
2349 -- declarations are present. The end of a package declaration is a
2350 -- freezing point only if it a library package. A task definition or
2351 -- protected type definition is not a freeze point either. Finally,
2352 -- we do not freeze entities in generic scopes, because there is no
2353 -- code generated for them and freeze nodes will be generated for
2354 -- the instance.
2355
2356 -- The end of a package instantiation is not a freeze point, but
2357 -- for now we make it one, because the generic body is inserted
2358 -- (currently) immediately after. Generic instantiations will not
2359 -- be a freeze point once delayed freezing of bodies is implemented.
2360 -- (This is needed in any case for early instantiations ???).
2361
2362 if No (Next_Decl) then
2363 if Nkind_In (Parent (L), N_Component_List,
2364 N_Task_Definition,
2365 N_Protected_Definition)
2366 then
2367 null;
2368
2369 elsif Nkind (Parent (L)) /= N_Package_Specification then
2370 if Nkind (Parent (L)) = N_Package_Body then
2371 Freeze_From := First_Entity (Current_Scope);
2372 end if;
2373
2374 -- There may have been several freezing points previously,
2375 -- for example object declarations or subprogram bodies, but
2376 -- at the end of a declarative part we check freezing from
2377 -- the beginning, even though entities may already be frozen,
2378 -- in order to perform visibility checks on delayed aspects.
2379
2380 Adjust_Decl;
2381 Freeze_All (First_Entity (Current_Scope), Decl);
2382 Freeze_From := Last_Entity (Current_Scope);
2383
2384 elsif Scope (Current_Scope) /= Standard_Standard
2385 and then not Is_Child_Unit (Current_Scope)
2386 and then No (Generic_Parent (Parent (L)))
2387 then
2388 null;
2389
2390 elsif L /= Visible_Declarations (Parent (L))
2391 or else No (Private_Declarations (Parent (L)))
2392 or else Is_Empty_List (Private_Declarations (Parent (L)))
2393 then
2394 Adjust_Decl;
2395 Freeze_All (First_Entity (Current_Scope), Decl);
2396 Freeze_From := Last_Entity (Current_Scope);
2397 end if;
2398
2399 -- If next node is a body then freeze all types before the body.
2400 -- An exception occurs for some expander-generated bodies. If these
2401 -- are generated at places where in general language rules would not
2402 -- allow a freeze point, then we assume that the expander has
2403 -- explicitly checked that all required types are properly frozen,
2404 -- and we do not cause general freezing here. This special circuit
2405 -- is used when the encountered body is marked as having already
2406 -- been analyzed.
2407
2408 -- In all other cases (bodies that come from source, and expander
2409 -- generated bodies that have not been analyzed yet), freeze all
2410 -- types now. Note that in the latter case, the expander must take
2411 -- care to attach the bodies at a proper place in the tree so as to
2412 -- not cause unwanted freezing at that point.
2413
2414 elsif not Analyzed (Next_Decl) and then Is_Body (Next_Decl) then
2415
2416 -- When a controlled type is frozen, the expander generates stream
2417 -- and controlled type support routines. If the freeze is caused
2418 -- by the stand alone body of Initialize, Adjust and Finalize, the
2419 -- expander will end up using the wrong version of these routines
2420 -- as the body has not been processed yet. To remedy this, detect
2421 -- a late controlled primitive and create a proper spec for it.
2422 -- This ensures that the primitive will override its inherited
2423 -- counterpart before the freeze takes place.
2424
2425 -- If the declaration we just processed is a body, do not attempt
2426 -- to examine Next_Decl as the late primitive idiom can only apply
2427 -- to the first encountered body.
2428
2429 -- The spec of the late primitive is not generated in ASIS mode to
2430 -- ensure a consistent list of primitives that indicates the true
2431 -- semantic structure of the program (which is not relevant when
2432 -- generating executable code.
2433
2434 -- ??? a cleaner approach may be possible and/or this solution
2435 -- could be extended to general-purpose late primitives, TBD.
2436
2437 if not ASIS_Mode and then not Body_Seen and then not Is_Body (Decl)
2438 then
2439 Body_Seen := True;
2440
2441 if Nkind (Next_Decl) = N_Subprogram_Body then
2442 Handle_Late_Controlled_Primitive (Next_Decl);
2443 end if;
2444 end if;
2445
2446 Adjust_Decl;
2447 Freeze_All (Freeze_From, Decl);
2448 Freeze_From := Last_Entity (Current_Scope);
2449 end if;
2450
2451 Decl := Next_Decl;
2452 end loop;
2453
2454 -- Analyze the contracts of packages and their bodies
2455
2456 if Present (L) then
2457 Context := Parent (L);
2458
2459 if Nkind (Context) = N_Package_Specification then
2460
2461 -- When a package has private declarations, its contract must be
2462 -- analyzed at the end of the said declarations. This way both the
2463 -- analysis and freeze actions are properly synchronized in case
2464 -- of private type use within the contract.
2465
2466 if L = Private_Declarations (Context) then
2467 Analyze_Package_Contract (Defining_Entity (Context));
2468
2469 -- Build the bodies of the default initial condition procedures
2470 -- for all types subject to pragma Default_Initial_Condition.
2471 -- From a purely Ada stand point, this is a freezing activity,
2472 -- however freezing is not available under GNATprove_Mode. To
2473 -- accomodate both scenarios, the bodies are build at the end
2474 -- of private declaration analysis.
2475
2476 Build_Default_Init_Cond_Procedure_Bodies (L);
2477
2478 -- Otherwise the contract is analyzed at the end of the visible
2479 -- declarations.
2480
2481 elsif L = Visible_Declarations (Context)
2482 and then No (Private_Declarations (Context))
2483 then
2484 Analyze_Package_Contract (Defining_Entity (Context));
2485 end if;
2486
2487 elsif Nkind (Context) = N_Package_Body then
2488 In_Package_Body := True;
2489 Spec_Id := Corresponding_Spec (Context);
2490
2491 Analyze_Package_Body_Contract (Defining_Entity (Context));
2492 end if;
2493 end if;
2494
2495 -- Analyze the contracts of subprogram declarations, subprogram bodies
2496 -- and variables now due to the delayed visibility requirements of their
2497 -- aspects.
2498
2499 Decl := First (L);
2500 while Present (Decl) loop
2501 if Nkind (Decl) = N_Object_Declaration then
2502 Analyze_Object_Contract (Defining_Entity (Decl));
2503
2504 elsif Nkind_In (Decl, N_Abstract_Subprogram_Declaration,
2505 N_Generic_Subprogram_Declaration,
2506 N_Subprogram_Declaration)
2507 then
2508 Analyze_Subprogram_Contract (Defining_Entity (Decl));
2509
2510 elsif Nkind (Decl) = N_Subprogram_Body then
2511 Analyze_Subprogram_Body_Contract (Defining_Entity (Decl));
2512
2513 elsif Nkind (Decl) = N_Subprogram_Body_Stub then
2514 Analyze_Subprogram_Body_Stub_Contract (Defining_Entity (Decl));
2515 end if;
2516
2517 Next (Decl);
2518 end loop;
2519
2520 -- State refinements are visible upto the end the of the package body
2521 -- declarations. Hide the refinements from visibility to restore the
2522 -- original state conditions.
2523
2524 if In_Package_Body then
2525 Remove_Visible_Refinements (Spec_Id);
2526 end if;
2527 end Analyze_Declarations;
2528
2529 -----------------------------------
2530 -- Analyze_Full_Type_Declaration --
2531 -----------------------------------
2532
2533 procedure Analyze_Full_Type_Declaration (N : Node_Id) is
2534 Def : constant Node_Id := Type_Definition (N);
2535 Def_Id : constant Entity_Id := Defining_Identifier (N);
2536 T : Entity_Id;
2537 Prev : Entity_Id;
2538
2539 Is_Remote : constant Boolean :=
2540 (Is_Remote_Types (Current_Scope)
2541 or else Is_Remote_Call_Interface (Current_Scope))
2542 and then not (In_Private_Part (Current_Scope)
2543 or else In_Package_Body (Current_Scope));
2544
2545 procedure Check_Ops_From_Incomplete_Type;
2546 -- If there is a tagged incomplete partial view of the type, traverse
2547 -- the primitives of the incomplete view and change the type of any
2548 -- controlling formals and result to indicate the full view. The
2549 -- primitives will be added to the full type's primitive operations
2550 -- list later in Sem_Disp.Check_Operation_From_Incomplete_Type (which
2551 -- is called from Process_Incomplete_Dependents).
2552
2553 ------------------------------------
2554 -- Check_Ops_From_Incomplete_Type --
2555 ------------------------------------
2556
2557 procedure Check_Ops_From_Incomplete_Type is
2558 Elmt : Elmt_Id;
2559 Formal : Entity_Id;
2560 Op : Entity_Id;
2561
2562 begin
2563 if Prev /= T
2564 and then Ekind (Prev) = E_Incomplete_Type
2565 and then Is_Tagged_Type (Prev)
2566 and then Is_Tagged_Type (T)
2567 then
2568 Elmt := First_Elmt (Primitive_Operations (Prev));
2569 while Present (Elmt) loop
2570 Op := Node (Elmt);
2571
2572 Formal := First_Formal (Op);
2573 while Present (Formal) loop
2574 if Etype (Formal) = Prev then
2575 Set_Etype (Formal, T);
2576 end if;
2577
2578 Next_Formal (Formal);
2579 end loop;
2580
2581 if Etype (Op) = Prev then
2582 Set_Etype (Op, T);
2583 end if;
2584
2585 Next_Elmt (Elmt);
2586 end loop;
2587 end if;
2588 end Check_Ops_From_Incomplete_Type;
2589
2590 -- Start of processing for Analyze_Full_Type_Declaration
2591
2592 begin
2593 Prev := Find_Type_Name (N);
2594
2595 -- The type declaration may be subject to pragma Ghost with policy
2596 -- Ignore. Set the mode now to ensure that any nodes generated during
2597 -- analysis and expansion are properly flagged as ignored Ghost.
2598
2599 Set_Ghost_Mode (N, Prev);
2600
2601 -- The full view, if present, now points to the current type. If there
2602 -- is an incomplete partial view, set a link to it, to simplify the
2603 -- retrieval of primitive operations of the type.
2604
2605 -- Ada 2005 (AI-50217): If the type was previously decorated when
2606 -- imported through a LIMITED WITH clause, it appears as incomplete
2607 -- but has no full view.
2608
2609 if Ekind (Prev) = E_Incomplete_Type
2610 and then Present (Full_View (Prev))
2611 then
2612 T := Full_View (Prev);
2613 Set_Incomplete_View (N, Parent (Prev));
2614 else
2615 T := Prev;
2616 end if;
2617
2618 Set_Is_Pure (T, Is_Pure (Current_Scope));
2619
2620 -- We set the flag Is_First_Subtype here. It is needed to set the
2621 -- corresponding flag for the Implicit class-wide-type created
2622 -- during tagged types processing.
2623
2624 Set_Is_First_Subtype (T, True);
2625
2626 -- Only composite types other than array types are allowed to have
2627 -- discriminants.
2628
2629 case Nkind (Def) is
2630
2631 -- For derived types, the rule will be checked once we've figured
2632 -- out the parent type.
2633
2634 when N_Derived_Type_Definition =>
2635 null;
2636
2637 -- For record types, discriminants are allowed, unless we are in
2638 -- SPARK.
2639
2640 when N_Record_Definition =>
2641 if Present (Discriminant_Specifications (N)) then
2642 Check_SPARK_05_Restriction
2643 ("discriminant type is not allowed",
2644 Defining_Identifier
2645 (First (Discriminant_Specifications (N))));
2646 end if;
2647
2648 when others =>
2649 if Present (Discriminant_Specifications (N)) then
2650 Error_Msg_N
2651 ("elementary or array type cannot have discriminants",
2652 Defining_Identifier
2653 (First (Discriminant_Specifications (N))));
2654 end if;
2655 end case;
2656
2657 -- Elaborate the type definition according to kind, and generate
2658 -- subsidiary (implicit) subtypes where needed. We skip this if it was
2659 -- already done (this happens during the reanalysis that follows a call
2660 -- to the high level optimizer).
2661
2662 if not Analyzed (T) then
2663 Set_Analyzed (T);
2664
2665 case Nkind (Def) is
2666 when N_Access_To_Subprogram_Definition =>
2667 Access_Subprogram_Declaration (T, Def);
2668
2669 -- If this is a remote access to subprogram, we must create the
2670 -- equivalent fat pointer type, and related subprograms.
2671
2672 if Is_Remote then
2673 Process_Remote_AST_Declaration (N);
2674 end if;
2675
2676 -- Validate categorization rule against access type declaration
2677 -- usually a violation in Pure unit, Shared_Passive unit.
2678
2679 Validate_Access_Type_Declaration (T, N);
2680
2681 when N_Access_To_Object_Definition =>
2682 Access_Type_Declaration (T, Def);
2683
2684 -- Validate categorization rule against access type declaration
2685 -- usually a violation in Pure unit, Shared_Passive unit.
2686
2687 Validate_Access_Type_Declaration (T, N);
2688
2689 -- If we are in a Remote_Call_Interface package and define a
2690 -- RACW, then calling stubs and specific stream attributes
2691 -- must be added.
2692
2693 if Is_Remote
2694 and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
2695 then
2696 Add_RACW_Features (Def_Id);
2697 end if;
2698
2699 when N_Array_Type_Definition =>
2700 Array_Type_Declaration (T, Def);
2701
2702 when N_Derived_Type_Definition =>
2703 Derived_Type_Declaration (T, N, T /= Def_Id);
2704
2705 when N_Enumeration_Type_Definition =>
2706 Enumeration_Type_Declaration (T, Def);
2707
2708 when N_Floating_Point_Definition =>
2709 Floating_Point_Type_Declaration (T, Def);
2710
2711 when N_Decimal_Fixed_Point_Definition =>
2712 Decimal_Fixed_Point_Type_Declaration (T, Def);
2713
2714 when N_Ordinary_Fixed_Point_Definition =>
2715 Ordinary_Fixed_Point_Type_Declaration (T, Def);
2716
2717 when N_Signed_Integer_Type_Definition =>
2718 Signed_Integer_Type_Declaration (T, Def);
2719
2720 when N_Modular_Type_Definition =>
2721 Modular_Type_Declaration (T, Def);
2722
2723 when N_Record_Definition =>
2724 Record_Type_Declaration (T, N, Prev);
2725
2726 -- If declaration has a parse error, nothing to elaborate.
2727
2728 when N_Error =>
2729 null;
2730
2731 when others =>
2732 raise Program_Error;
2733
2734 end case;
2735 end if;
2736
2737 if Etype (T) = Any_Type then
2738 return;
2739 end if;
2740
2741 -- Controlled type is not allowed in SPARK
2742
2743 if Is_Visibly_Controlled (T) then
2744 Check_SPARK_05_Restriction ("controlled type is not allowed", N);
2745 end if;
2746
2747 -- A type declared within a Ghost region is automatically Ghost
2748 -- (SPARK RM 6.9(2)).
2749
2750 if Comes_From_Source (T) and then Ghost_Mode > None then
2751 Set_Is_Ghost_Entity (T);
2752 end if;
2753
2754 -- Some common processing for all types
2755
2756 Set_Depends_On_Private (T, Has_Private_Component (T));
2757 Check_Ops_From_Incomplete_Type;
2758
2759 -- Both the declared entity, and its anonymous base type if one was
2760 -- created, need freeze nodes allocated.
2761
2762 declare
2763 B : constant Entity_Id := Base_Type (T);
2764
2765 begin
2766 -- In the case where the base type differs from the first subtype, we
2767 -- pre-allocate a freeze node, and set the proper link to the first
2768 -- subtype. Freeze_Entity will use this preallocated freeze node when
2769 -- it freezes the entity.
2770
2771 -- This does not apply if the base type is a generic type, whose
2772 -- declaration is independent of the current derived definition.
2773
2774 if B /= T and then not Is_Generic_Type (B) then
2775 Ensure_Freeze_Node (B);
2776 Set_First_Subtype_Link (Freeze_Node (B), T);
2777 end if;
2778
2779 -- A type that is imported through a limited_with clause cannot
2780 -- generate any code, and thus need not be frozen. However, an access
2781 -- type with an imported designated type needs a finalization list,
2782 -- which may be referenced in some other package that has non-limited
2783 -- visibility on the designated type. Thus we must create the
2784 -- finalization list at the point the access type is frozen, to
2785 -- prevent unsatisfied references at link time.
2786
2787 if not From_Limited_With (T) or else Is_Access_Type (T) then
2788 Set_Has_Delayed_Freeze (T);
2789 end if;
2790 end;
2791
2792 -- Case where T is the full declaration of some private type which has
2793 -- been swapped in Defining_Identifier (N).
2794
2795 if T /= Def_Id and then Is_Private_Type (Def_Id) then
2796 Process_Full_View (N, T, Def_Id);
2797
2798 -- Record the reference. The form of this is a little strange, since
2799 -- the full declaration has been swapped in. So the first parameter
2800 -- here represents the entity to which a reference is made which is
2801 -- the "real" entity, i.e. the one swapped in, and the second
2802 -- parameter provides the reference location.
2803
2804 -- Also, we want to kill Has_Pragma_Unreferenced temporarily here
2805 -- since we don't want a complaint about the full type being an
2806 -- unwanted reference to the private type
2807
2808 declare
2809 B : constant Boolean := Has_Pragma_Unreferenced (T);
2810 begin
2811 Set_Has_Pragma_Unreferenced (T, False);
2812 Generate_Reference (T, T, 'c');
2813 Set_Has_Pragma_Unreferenced (T, B);
2814 end;
2815
2816 Set_Completion_Referenced (Def_Id);
2817
2818 -- For completion of incomplete type, process incomplete dependents
2819 -- and always mark the full type as referenced (it is the incomplete
2820 -- type that we get for any real reference).
2821
2822 elsif Ekind (Prev) = E_Incomplete_Type then
2823 Process_Incomplete_Dependents (N, T, Prev);
2824 Generate_Reference (Prev, Def_Id, 'c');
2825 Set_Completion_Referenced (Def_Id);
2826
2827 -- If not private type or incomplete type completion, this is a real
2828 -- definition of a new entity, so record it.
2829
2830 else
2831 Generate_Definition (Def_Id);
2832 end if;
2833
2834 -- Propagate any pending access types whose finalization masters need to
2835 -- be fully initialized from the partial to the full view. Guard against
2836 -- an illegal full view that remains unanalyzed.
2837
2838 if Is_Type (Def_Id) and then Is_Incomplete_Or_Private_Type (Prev) then
2839 Set_Pending_Access_Types (Def_Id, Pending_Access_Types (Prev));
2840 end if;
2841
2842 if Chars (Scope (Def_Id)) = Name_System
2843 and then Chars (Def_Id) = Name_Address
2844 and then Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (N)))
2845 then
2846 Set_Is_Descendent_Of_Address (Def_Id);
2847 Set_Is_Descendent_Of_Address (Base_Type (Def_Id));
2848 Set_Is_Descendent_Of_Address (Prev);
2849 end if;
2850
2851 Set_Optimize_Alignment_Flags (Def_Id);
2852 Check_Eliminated (Def_Id);
2853
2854 -- If the declaration is a completion and aspects are present, apply
2855 -- them to the entity for the type which is currently the partial
2856 -- view, but which is the one that will be frozen.
2857
2858 if Has_Aspects (N) then
2859
2860 -- In most cases the partial view is a private type, and both views
2861 -- appear in different declarative parts. In the unusual case where
2862 -- the partial view is incomplete, perform the analysis on the
2863 -- full view, to prevent freezing anomalies with the corresponding
2864 -- class-wide type, which otherwise might be frozen before the
2865 -- dispatch table is built.
2866
2867 if Prev /= Def_Id
2868 and then Ekind (Prev) /= E_Incomplete_Type
2869 then
2870 Analyze_Aspect_Specifications (N, Prev);
2871
2872 -- Normal case
2873
2874 else
2875 Analyze_Aspect_Specifications (N, Def_Id);
2876 end if;
2877 end if;
2878 end Analyze_Full_Type_Declaration;
2879
2880 ----------------------------------
2881 -- Analyze_Incomplete_Type_Decl --
2882 ----------------------------------
2883
2884 procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
2885 F : constant Boolean := Is_Pure (Current_Scope);
2886 T : Entity_Id;
2887
2888 begin
2889 Check_SPARK_05_Restriction ("incomplete type is not allowed", N);
2890
2891 Generate_Definition (Defining_Identifier (N));
2892
2893 -- Process an incomplete declaration. The identifier must not have been
2894 -- declared already in the scope. However, an incomplete declaration may
2895 -- appear in the private part of a package, for a private type that has
2896 -- already been declared.
2897
2898 -- In this case, the discriminants (if any) must match
2899
2900 T := Find_Type_Name (N);
2901
2902 Set_Ekind (T, E_Incomplete_Type);
2903 Init_Size_Align (T);
2904 Set_Is_First_Subtype (T, True);
2905 Set_Etype (T, T);
2906
2907 -- An incomplete type declared within a Ghost region is automatically
2908 -- Ghost (SPARK RM 6.9(2)).
2909
2910 if Ghost_Mode > None then
2911 Set_Is_Ghost_Entity (T);
2912 end if;
2913
2914 -- Ada 2005 (AI-326): Minimum decoration to give support to tagged
2915 -- incomplete types.
2916
2917 if Tagged_Present (N) then
2918 Set_Is_Tagged_Type (T, True);
2919 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
2920 Make_Class_Wide_Type (T);
2921 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2922 end if;
2923
2924 Push_Scope (T);
2925
2926 Set_Stored_Constraint (T, No_Elist);
2927
2928 if Present (Discriminant_Specifications (N)) then
2929 Process_Discriminants (N);
2930 end if;
2931
2932 End_Scope;
2933
2934 -- If the type has discriminants, non-trivial subtypes may be
2935 -- declared before the full view of the type. The full views of those
2936 -- subtypes will be built after the full view of the type.
2937
2938 Set_Private_Dependents (T, New_Elmt_List);
2939 Set_Is_Pure (T, F);
2940 end Analyze_Incomplete_Type_Decl;
2941
2942 -----------------------------------
2943 -- Analyze_Interface_Declaration --
2944 -----------------------------------
2945
2946 procedure Analyze_Interface_Declaration (T : Entity_Id; Def : Node_Id) is
2947 CW : constant Entity_Id := Class_Wide_Type (T);
2948
2949 begin
2950 Set_Is_Tagged_Type (T);
2951 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
2952
2953 Set_Is_Limited_Record (T, Limited_Present (Def)
2954 or else Task_Present (Def)
2955 or else Protected_Present (Def)
2956 or else Synchronized_Present (Def));
2957
2958 -- Type is abstract if full declaration carries keyword, or if previous
2959 -- partial view did.
2960
2961 Set_Is_Abstract_Type (T);
2962 Set_Is_Interface (T);
2963
2964 -- Type is a limited interface if it includes the keyword limited, task,
2965 -- protected, or synchronized.
2966
2967 Set_Is_Limited_Interface
2968 (T, Limited_Present (Def)
2969 or else Protected_Present (Def)
2970 or else Synchronized_Present (Def)
2971 or else Task_Present (Def));
2972
2973 Set_Interfaces (T, New_Elmt_List);
2974 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2975
2976 -- Complete the decoration of the class-wide entity if it was already
2977 -- built (i.e. during the creation of the limited view)
2978
2979 if Present (CW) then
2980 Set_Is_Interface (CW);
2981 Set_Is_Limited_Interface (CW, Is_Limited_Interface (T));
2982 end if;
2983
2984 -- Check runtime support for synchronized interfaces
2985
2986 if VM_Target = No_VM
2987 and then (Is_Task_Interface (T)
2988 or else Is_Protected_Interface (T)
2989 or else Is_Synchronized_Interface (T))
2990 and then not RTE_Available (RE_Select_Specific_Data)
2991 then
2992 Error_Msg_CRT ("synchronized interfaces", T);
2993 end if;
2994 end Analyze_Interface_Declaration;
2995
2996 -----------------------------
2997 -- Analyze_Itype_Reference --
2998 -----------------------------
2999
3000 -- Nothing to do. This node is placed in the tree only for the benefit of
3001 -- back end processing, and has no effect on the semantic processing.
3002
3003 procedure Analyze_Itype_Reference (N : Node_Id) is
3004 begin
3005 pragma Assert (Is_Itype (Itype (N)));
3006 null;
3007 end Analyze_Itype_Reference;
3008
3009 --------------------------------
3010 -- Analyze_Number_Declaration --
3011 --------------------------------
3012
3013 procedure Analyze_Number_Declaration (N : Node_Id) is
3014 Id : constant Entity_Id := Defining_Identifier (N);
3015 E : constant Node_Id := Expression (N);
3016 T : Entity_Id;
3017 Index : Interp_Index;
3018 It : Interp;
3019
3020 begin
3021 -- The number declaration may be subject to pragma Ghost with policy
3022 -- Ignore. Set the mode now to ensure that any nodes generated during
3023 -- analysis and expansion are properly flagged as ignored Ghost.
3024
3025 Set_Ghost_Mode (N);
3026
3027 Generate_Definition (Id);
3028 Enter_Name (Id);
3029
3030 -- A number declared within a Ghost region is automatically Ghost
3031 -- (SPARK RM 6.9(2)).
3032
3033 if Ghost_Mode > None then
3034 Set_Is_Ghost_Entity (Id);
3035 end if;
3036
3037 -- This is an optimization of a common case of an integer literal
3038
3039 if Nkind (E) = N_Integer_Literal then
3040 Set_Is_Static_Expression (E, True);
3041 Set_Etype (E, Universal_Integer);
3042
3043 Set_Etype (Id, Universal_Integer);
3044 Set_Ekind (Id, E_Named_Integer);
3045 Set_Is_Frozen (Id, True);
3046 return;
3047 end if;
3048
3049 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3050
3051 -- Process expression, replacing error by integer zero, to avoid
3052 -- cascaded errors or aborts further along in the processing
3053
3054 -- Replace Error by integer zero, which seems least likely to cause
3055 -- cascaded errors.
3056
3057 if E = Error then
3058 Rewrite (E, Make_Integer_Literal (Sloc (E), Uint_0));
3059 Set_Error_Posted (E);
3060 end if;
3061
3062 Analyze (E);
3063
3064 -- Verify that the expression is static and numeric. If
3065 -- the expression is overloaded, we apply the preference
3066 -- rule that favors root numeric types.
3067
3068 if not Is_Overloaded (E) then
3069 T := Etype (E);
3070 if Has_Dynamic_Predicate_Aspect (T) then
3071 Error_Msg_N
3072 ("subtype has dynamic predicate, "
3073 & "not allowed in number declaration", N);
3074 end if;
3075
3076 else
3077 T := Any_Type;
3078
3079 Get_First_Interp (E, Index, It);
3080 while Present (It.Typ) loop
3081 if (Is_Integer_Type (It.Typ) or else Is_Real_Type (It.Typ))
3082 and then (Scope (Base_Type (It.Typ))) = Standard_Standard
3083 then
3084 if T = Any_Type then
3085 T := It.Typ;
3086
3087 elsif It.Typ = Universal_Real
3088 or else
3089 It.Typ = Universal_Integer
3090 then
3091 -- Choose universal interpretation over any other
3092
3093 T := It.Typ;
3094 exit;
3095 end if;
3096 end if;
3097
3098 Get_Next_Interp (Index, It);
3099 end loop;
3100 end if;
3101
3102 if Is_Integer_Type (T) then
3103 Resolve (E, T);
3104 Set_Etype (Id, Universal_Integer);
3105 Set_Ekind (Id, E_Named_Integer);
3106
3107 elsif Is_Real_Type (T) then
3108
3109 -- Because the real value is converted to universal_real, this is a
3110 -- legal context for a universal fixed expression.
3111
3112 if T = Universal_Fixed then
3113 declare
3114 Loc : constant Source_Ptr := Sloc (N);
3115 Conv : constant Node_Id := Make_Type_Conversion (Loc,
3116 Subtype_Mark =>
3117 New_Occurrence_Of (Universal_Real, Loc),
3118 Expression => Relocate_Node (E));
3119
3120 begin
3121 Rewrite (E, Conv);
3122 Analyze (E);
3123 end;
3124
3125 elsif T = Any_Fixed then
3126 Error_Msg_N ("illegal context for mixed mode operation", E);
3127
3128 -- Expression is of the form : universal_fixed * integer. Try to
3129 -- resolve as universal_real.
3130
3131 T := Universal_Real;
3132 Set_Etype (E, T);
3133 end if;
3134
3135 Resolve (E, T);
3136 Set_Etype (Id, Universal_Real);
3137 Set_Ekind (Id, E_Named_Real);
3138
3139 else
3140 Wrong_Type (E, Any_Numeric);
3141 Resolve (E, T);
3142
3143 Set_Etype (Id, T);
3144 Set_Ekind (Id, E_Constant);
3145 Set_Never_Set_In_Source (Id, True);
3146 Set_Is_True_Constant (Id, True);
3147 return;
3148 end if;
3149
3150 if Nkind_In (E, N_Integer_Literal, N_Real_Literal) then
3151 Set_Etype (E, Etype (Id));
3152 end if;
3153
3154 if not Is_OK_Static_Expression (E) then
3155 Flag_Non_Static_Expr
3156 ("non-static expression used in number declaration!", E);
3157 Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
3158 Set_Etype (E, Any_Type);
3159 end if;
3160 end Analyze_Number_Declaration;
3161
3162 -----------------------------
3163 -- Analyze_Object_Contract --
3164 -----------------------------
3165
3166 procedure Analyze_Object_Contract (Obj_Id : Entity_Id) is
3167 Obj_Typ : constant Entity_Id := Etype (Obj_Id);
3168 AR_Val : Boolean := False;
3169 AW_Val : Boolean := False;
3170 ER_Val : Boolean := False;
3171 EW_Val : Boolean := False;
3172 Prag : Node_Id;
3173 Seen : Boolean := False;
3174
3175 begin
3176 -- The loop parameter in an element iterator over a formal container
3177 -- is declared with an object declaration but no contracts apply.
3178
3179 if Ekind (Obj_Id) = E_Loop_Parameter then
3180 return;
3181 end if;
3182
3183 if Ekind (Obj_Id) = E_Constant then
3184
3185 -- A constant cannot be effectively volatile. This check is only
3186 -- relevant with SPARK_Mode on as it is not a standard Ada legality
3187 -- rule. Do not flag internally-generated constants that map generic
3188 -- formals to actuals in instantiations (SPARK RM 7.1.3(6)).
3189
3190 if SPARK_Mode = On
3191 and then Is_Effectively_Volatile (Obj_Id)
3192 and then No (Corresponding_Generic_Association (Parent (Obj_Id)))
3193
3194 -- Don't give this for internally generated entities (such as the
3195 -- FIRST and LAST temporaries generated for bounds).
3196
3197 and then Comes_From_Source (Obj_Id)
3198 then
3199 Error_Msg_N ("constant cannot be volatile", Obj_Id);
3200 end if;
3201
3202 else pragma Assert (Ekind (Obj_Id) = E_Variable);
3203
3204 -- The following checks are only relevant when SPARK_Mode is on as
3205 -- they are not standard Ada legality rules. Internally generated
3206 -- temporaries are ignored.
3207
3208 if SPARK_Mode = On and then Comes_From_Source (Obj_Id) then
3209 if Is_Effectively_Volatile (Obj_Id) then
3210
3211 -- The declaration of an effectively volatile object must
3212 -- appear at the library level (SPARK RM 7.1.3(7), C.6(6)).
3213
3214 if not Is_Library_Level_Entity (Obj_Id) then
3215 Error_Msg_N
3216 ("volatile variable & must be declared at library level",
3217 Obj_Id);
3218
3219 -- An object of a discriminated type cannot be effectively
3220 -- volatile (SPARK RM C.6(4)).
3221
3222 elsif Has_Discriminants (Obj_Typ) then
3223 Error_Msg_N
3224 ("discriminated object & cannot be volatile", Obj_Id);
3225
3226 -- An object of a tagged type cannot be effectively volatile
3227 -- (SPARK RM C.6(5)).
3228
3229 elsif Is_Tagged_Type (Obj_Typ) then
3230 Error_Msg_N ("tagged object & cannot be volatile", Obj_Id);
3231 end if;
3232
3233 -- The object is not effectively volatile
3234
3235 else
3236 -- A non-effectively volatile object cannot have effectively
3237 -- volatile components (SPARK RM 7.1.3(7)).
3238
3239 if not Is_Effectively_Volatile (Obj_Id)
3240 and then Has_Volatile_Component (Obj_Typ)
3241 then
3242 Error_Msg_N
3243 ("non-volatile object & cannot have volatile components",
3244 Obj_Id);
3245 end if;
3246 end if;
3247 end if;
3248
3249 if Is_Ghost_Entity (Obj_Id) then
3250
3251 -- A Ghost object cannot be effectively volatile (SPARK RM 6.9(8))
3252
3253 if Is_Effectively_Volatile (Obj_Id) then
3254 Error_Msg_N ("ghost variable & cannot be volatile", Obj_Id);
3255
3256 -- A Ghost object cannot be imported or exported (SPARK RM 6.9(8))
3257
3258 elsif Is_Imported (Obj_Id) then
3259 Error_Msg_N ("ghost object & cannot be imported", Obj_Id);
3260
3261 elsif Is_Exported (Obj_Id) then
3262 Error_Msg_N ("ghost object & cannot be exported", Obj_Id);
3263 end if;
3264 end if;
3265
3266 -- Analyze all external properties
3267
3268 Prag := Get_Pragma (Obj_Id, Pragma_Async_Readers);
3269
3270 if Present (Prag) then
3271 Analyze_External_Property_In_Decl_Part (Prag, AR_Val);
3272 Seen := True;
3273 end if;
3274
3275 Prag := Get_Pragma (Obj_Id, Pragma_Async_Writers);
3276
3277 if Present (Prag) then
3278 Analyze_External_Property_In_Decl_Part (Prag, AW_Val);
3279 Seen := True;
3280 end if;
3281
3282 Prag := Get_Pragma (Obj_Id, Pragma_Effective_Reads);
3283
3284 if Present (Prag) then
3285 Analyze_External_Property_In_Decl_Part (Prag, ER_Val);
3286 Seen := True;
3287 end if;
3288
3289 Prag := Get_Pragma (Obj_Id, Pragma_Effective_Writes);
3290
3291 if Present (Prag) then
3292 Analyze_External_Property_In_Decl_Part (Prag, EW_Val);
3293 Seen := True;
3294 end if;
3295
3296 -- Verify the mutual interaction of the various external properties
3297
3298 if Seen then
3299 Check_External_Properties (Obj_Id, AR_Val, AW_Val, ER_Val, EW_Val);
3300 end if;
3301
3302 -- Check whether the lack of indicator Part_Of agrees with the
3303 -- placement of the variable with respect to the state space.
3304
3305 Prag := Get_Pragma (Obj_Id, Pragma_Part_Of);
3306
3307 if No (Prag) then
3308 Check_Missing_Part_Of (Obj_Id);
3309 end if;
3310 end if;
3311
3312 -- A ghost object cannot be imported or exported (SPARK RM 6.9(8))
3313
3314 if Is_Ghost_Entity (Obj_Id) then
3315 if Is_Exported (Obj_Id) then
3316 Error_Msg_N ("ghost object & cannot be exported", Obj_Id);
3317
3318 elsif Is_Imported (Obj_Id) then
3319 Error_Msg_N ("ghost object & cannot be imported", Obj_Id);
3320 end if;
3321 end if;
3322 end Analyze_Object_Contract;
3323
3324 --------------------------------
3325 -- Analyze_Object_Declaration --
3326 --------------------------------
3327
3328 procedure Analyze_Object_Declaration (N : Node_Id) is
3329 Loc : constant Source_Ptr := Sloc (N);
3330 Id : constant Entity_Id := Defining_Identifier (N);
3331 T : Entity_Id;
3332 Act_T : Entity_Id;
3333
3334 E : Node_Id := Expression (N);
3335 -- E is set to Expression (N) throughout this routine. When
3336 -- Expression (N) is modified, E is changed accordingly.
3337
3338 Prev_Entity : Entity_Id := Empty;
3339
3340 function Count_Tasks (T : Entity_Id) return Uint;
3341 -- This function is called when a non-generic library level object of a
3342 -- task type is declared. Its function is to count the static number of
3343 -- tasks declared within the type (it is only called if Has_Tasks is set
3344 -- for T). As a side effect, if an array of tasks with non-static bounds
3345 -- or a variant record type is encountered, Check_Restrictions is called
3346 -- indicating the count is unknown.
3347
3348 function Delayed_Aspect_Present return Boolean;
3349 -- If the declaration has an expression that is an aggregate, and it
3350 -- has aspects that require delayed analysis, the resolution of the
3351 -- aggregate must be deferred to the freeze point of the objet. This
3352 -- special processing was created for address clauses, but it must
3353 -- also apply to Alignment. This must be done before the aspect
3354 -- specifications are analyzed because we must handle the aggregate
3355 -- before the analysis of the object declaration is complete.
3356
3357 -- Any other relevant delayed aspects on object declarations ???
3358
3359 -----------------
3360 -- Count_Tasks --
3361 -----------------
3362
3363 function Count_Tasks (T : Entity_Id) return Uint is
3364 C : Entity_Id;
3365 X : Node_Id;
3366 V : Uint;
3367
3368 begin
3369 if Is_Task_Type (T) then
3370 return Uint_1;
3371
3372 elsif Is_Record_Type (T) then
3373 if Has_Discriminants (T) then
3374 Check_Restriction (Max_Tasks, N);
3375 return Uint_0;
3376
3377 else
3378 V := Uint_0;
3379 C := First_Component (T);
3380 while Present (C) loop
3381 V := V + Count_Tasks (Etype (C));
3382 Next_Component (C);
3383 end loop;
3384
3385 return V;
3386 end if;
3387
3388 elsif Is_Array_Type (T) then
3389 X := First_Index (T);
3390 V := Count_Tasks (Component_Type (T));
3391 while Present (X) loop
3392 C := Etype (X);
3393
3394 if not Is_OK_Static_Subtype (C) then
3395 Check_Restriction (Max_Tasks, N);
3396 return Uint_0;
3397 else
3398 V := V * (UI_Max (Uint_0,
3399 Expr_Value (Type_High_Bound (C)) -
3400 Expr_Value (Type_Low_Bound (C)) + Uint_1));
3401 end if;
3402
3403 Next_Index (X);
3404 end loop;
3405
3406 return V;
3407
3408 else
3409 return Uint_0;
3410 end if;
3411 end Count_Tasks;
3412
3413 ----------------------------
3414 -- Delayed_Aspect_Present --
3415 ----------------------------
3416
3417 function Delayed_Aspect_Present return Boolean is
3418 A : Node_Id;
3419 A_Id : Aspect_Id;
3420
3421 begin
3422 if Present (Aspect_Specifications (N)) then
3423 A := First (Aspect_Specifications (N));
3424 A_Id := Get_Aspect_Id (Chars (Identifier (A)));
3425 while Present (A) loop
3426 if A_Id = Aspect_Alignment or else A_Id = Aspect_Address then
3427 return True;
3428 end if;
3429
3430 Next (A);
3431 end loop;
3432 end if;
3433
3434 return False;
3435 end Delayed_Aspect_Present;
3436
3437 -- Start of processing for Analyze_Object_Declaration
3438
3439 begin
3440 -- There are three kinds of implicit types generated by an
3441 -- object declaration:
3442
3443 -- 1. Those generated by the original Object Definition
3444
3445 -- 2. Those generated by the Expression
3446
3447 -- 3. Those used to constrain the Object Definition with the
3448 -- expression constraints when the definition is unconstrained.
3449
3450 -- They must be generated in this order to avoid order of elaboration
3451 -- issues. Thus the first step (after entering the name) is to analyze
3452 -- the object definition.
3453
3454 if Constant_Present (N) then
3455 Prev_Entity := Current_Entity_In_Scope (Id);
3456
3457 if Present (Prev_Entity)
3458 and then
3459 -- If the homograph is an implicit subprogram, it is overridden
3460 -- by the current declaration.
3461
3462 ((Is_Overloadable (Prev_Entity)
3463 and then Is_Inherited_Operation (Prev_Entity))
3464
3465 -- The current object is a discriminal generated for an entry
3466 -- family index. Even though the index is a constant, in this
3467 -- particular context there is no true constant redeclaration.
3468 -- Enter_Name will handle the visibility.
3469
3470 or else
3471 (Is_Discriminal (Id)
3472 and then Ekind (Discriminal_Link (Id)) =
3473 E_Entry_Index_Parameter)
3474
3475 -- The current object is the renaming for a generic declared
3476 -- within the instance.
3477
3478 or else
3479 (Ekind (Prev_Entity) = E_Package
3480 and then Nkind (Parent (Prev_Entity)) =
3481 N_Package_Renaming_Declaration
3482 and then not Comes_From_Source (Prev_Entity)
3483 and then
3484 Is_Generic_Instance (Renamed_Entity (Prev_Entity))))
3485 then
3486 Prev_Entity := Empty;
3487 end if;
3488 end if;
3489
3490 -- The object declaration may be subject to pragma Ghost with policy
3491 -- Ignore. Set the mode now to ensure that any nodes generated during
3492 -- analysis and expansion are properly flagged as ignored Ghost.
3493
3494 Set_Ghost_Mode (N, Prev_Entity);
3495
3496 if Present (Prev_Entity) then
3497 Constant_Redeclaration (Id, N, T);
3498
3499 Generate_Reference (Prev_Entity, Id, 'c');
3500 Set_Completion_Referenced (Id);
3501
3502 if Error_Posted (N) then
3503
3504 -- Type mismatch or illegal redeclaration, Do not analyze
3505 -- expression to avoid cascaded errors.
3506
3507 T := Find_Type_Of_Object (Object_Definition (N), N);
3508 Set_Etype (Id, T);
3509 Set_Ekind (Id, E_Variable);
3510 goto Leave;
3511 end if;
3512
3513 -- In the normal case, enter identifier at the start to catch premature
3514 -- usage in the initialization expression.
3515
3516 else
3517 Generate_Definition (Id);
3518 Enter_Name (Id);
3519
3520 Mark_Coextensions (N, Object_Definition (N));
3521
3522 T := Find_Type_Of_Object (Object_Definition (N), N);
3523
3524 if Nkind (Object_Definition (N)) = N_Access_Definition
3525 and then Present
3526 (Access_To_Subprogram_Definition (Object_Definition (N)))
3527 and then Protected_Present
3528 (Access_To_Subprogram_Definition (Object_Definition (N)))
3529 then
3530 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
3531 end if;
3532
3533 if Error_Posted (Id) then
3534 Set_Etype (Id, T);
3535 Set_Ekind (Id, E_Variable);
3536 goto Leave;
3537 end if;
3538 end if;
3539
3540 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
3541 -- out some static checks
3542
3543 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (T) then
3544
3545 -- In case of aggregates we must also take care of the correct
3546 -- initialization of nested aggregates bug this is done at the
3547 -- point of the analysis of the aggregate (see sem_aggr.adb).
3548
3549 if Present (Expression (N))
3550 and then Nkind (Expression (N)) = N_Aggregate
3551 then
3552 null;
3553
3554 else
3555 declare
3556 Save_Typ : constant Entity_Id := Etype (Id);
3557 begin
3558 Set_Etype (Id, T); -- Temp. decoration for static checks
3559 Null_Exclusion_Static_Checks (N);
3560 Set_Etype (Id, Save_Typ);
3561 end;
3562 end if;
3563 end if;
3564
3565 -- Object is marked pure if it is in a pure scope
3566
3567 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3568
3569 -- If deferred constant, make sure context is appropriate. We detect
3570 -- a deferred constant as a constant declaration with no expression.
3571 -- A deferred constant can appear in a package body if its completion
3572 -- is by means of an interface pragma.
3573
3574 if Constant_Present (N) and then No (E) then
3575
3576 -- A deferred constant may appear in the declarative part of the
3577 -- following constructs:
3578
3579 -- blocks
3580 -- entry bodies
3581 -- extended return statements
3582 -- package specs
3583 -- package bodies
3584 -- subprogram bodies
3585 -- task bodies
3586
3587 -- When declared inside a package spec, a deferred constant must be
3588 -- completed by a full constant declaration or pragma Import. In all
3589 -- other cases, the only proper completion is pragma Import. Extended
3590 -- return statements are flagged as invalid contexts because they do
3591 -- not have a declarative part and so cannot accommodate the pragma.
3592
3593 if Ekind (Current_Scope) = E_Return_Statement then
3594 Error_Msg_N
3595 ("invalid context for deferred constant declaration (RM 7.4)",
3596 N);
3597 Error_Msg_N
3598 ("\declaration requires an initialization expression",
3599 N);
3600 Set_Constant_Present (N, False);
3601
3602 -- In Ada 83, deferred constant must be of private type
3603
3604 elsif not Is_Private_Type (T) then
3605 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
3606 Error_Msg_N
3607 ("(Ada 83) deferred constant must be private type", N);
3608 end if;
3609 end if;
3610
3611 -- If not a deferred constant, then the object declaration freezes
3612 -- its type, unless the object is of an anonymous type and has delayed
3613 -- aspects. In that case the type is frozen when the object itself is.
3614
3615 else
3616 Check_Fully_Declared (T, N);
3617
3618 if Has_Delayed_Aspects (Id)
3619 and then Is_Array_Type (T)
3620 and then Is_Itype (T)
3621 then
3622 Set_Has_Delayed_Freeze (T);
3623 else
3624 Freeze_Before (N, T);
3625 end if;
3626 end if;
3627
3628 -- If the object was created by a constrained array definition, then
3629 -- set the link in both the anonymous base type and anonymous subtype
3630 -- that are built to represent the array type to point to the object.
3631
3632 if Nkind (Object_Definition (Declaration_Node (Id))) =
3633 N_Constrained_Array_Definition
3634 then
3635 Set_Related_Array_Object (T, Id);
3636 Set_Related_Array_Object (Base_Type (T), Id);
3637 end if;
3638
3639 -- Special checks for protected objects not at library level
3640
3641 if Is_Protected_Type (T)
3642 and then not Is_Library_Level_Entity (Id)
3643 then
3644 Check_Restriction (No_Local_Protected_Objects, Id);
3645
3646 -- Protected objects with interrupt handlers must be at library level
3647
3648 -- Ada 2005: This test is not needed (and the corresponding clause
3649 -- in the RM is removed) because accessibility checks are sufficient
3650 -- to make handlers not at the library level illegal.
3651
3652 -- AI05-0303: The AI is in fact a binding interpretation, and thus
3653 -- applies to the '95 version of the language as well.
3654
3655 if Has_Interrupt_Handler (T) and then Ada_Version < Ada_95 then
3656 Error_Msg_N
3657 ("interrupt object can only be declared at library level", Id);
3658 end if;
3659 end if;
3660
3661 -- The actual subtype of the object is the nominal subtype, unless
3662 -- the nominal one is unconstrained and obtained from the expression.
3663
3664 Act_T := T;
3665
3666 -- These checks should be performed before the initialization expression
3667 -- is considered, so that the Object_Definition node is still the same
3668 -- as in source code.
3669
3670 -- In SPARK, the nominal subtype is always given by a subtype mark
3671 -- and must not be unconstrained. (The only exception to this is the
3672 -- acceptance of declarations of constants of type String.)
3673
3674 if not Nkind_In (Object_Definition (N), N_Expanded_Name, N_Identifier)
3675 then
3676 Check_SPARK_05_Restriction
3677 ("subtype mark required", Object_Definition (N));
3678
3679 elsif Is_Array_Type (T)
3680 and then not Is_Constrained (T)
3681 and then T /= Standard_String
3682 then
3683 Check_SPARK_05_Restriction
3684 ("subtype mark of constrained type expected",
3685 Object_Definition (N));
3686 end if;
3687
3688 -- There are no aliased objects in SPARK
3689
3690 if Aliased_Present (N) then
3691 Check_SPARK_05_Restriction ("aliased object is not allowed", N);
3692 end if;
3693
3694 -- Process initialization expression if present and not in error
3695
3696 if Present (E) and then E /= Error then
3697
3698 -- Generate an error in case of CPP class-wide object initialization.
3699 -- Required because otherwise the expansion of the class-wide
3700 -- assignment would try to use 'size to initialize the object
3701 -- (primitive that is not available in CPP tagged types).
3702
3703 if Is_Class_Wide_Type (Act_T)
3704 and then
3705 (Is_CPP_Class (Root_Type (Etype (Act_T)))
3706 or else
3707 (Present (Full_View (Root_Type (Etype (Act_T))))
3708 and then
3709 Is_CPP_Class (Full_View (Root_Type (Etype (Act_T))))))
3710 then
3711 Error_Msg_N
3712 ("predefined assignment not available for 'C'P'P tagged types",
3713 E);
3714 end if;
3715
3716 Mark_Coextensions (N, E);
3717 Analyze (E);
3718
3719 -- In case of errors detected in the analysis of the expression,
3720 -- decorate it with the expected type to avoid cascaded errors
3721
3722 if No (Etype (E)) then
3723 Set_Etype (E, T);
3724 end if;
3725
3726 -- If an initialization expression is present, then we set the
3727 -- Is_True_Constant flag. It will be reset if this is a variable
3728 -- and it is indeed modified.
3729
3730 Set_Is_True_Constant (Id, True);
3731
3732 -- If we are analyzing a constant declaration, set its completion
3733 -- flag after analyzing and resolving the expression.
3734
3735 if Constant_Present (N) then
3736 Set_Has_Completion (Id);
3737 end if;
3738
3739 -- Set type and resolve (type may be overridden later on). Note:
3740 -- Ekind (Id) must still be E_Void at this point so that incorrect
3741 -- early usage within E is properly diagnosed.
3742
3743 Set_Etype (Id, T);
3744
3745 -- If the expression is an aggregate we must look ahead to detect
3746 -- the possible presence of an address clause, and defer resolution
3747 -- and expansion of the aggregate to the freeze point of the entity.
3748
3749 if Comes_From_Source (N)
3750 and then Expander_Active
3751 and then Nkind (E) = N_Aggregate
3752 and then (Present (Following_Address_Clause (N))
3753 or else Delayed_Aspect_Present)
3754 then
3755 Set_Etype (E, T);
3756
3757 else
3758 Resolve (E, T);
3759 end if;
3760
3761 -- No further action needed if E is a call to an inlined function
3762 -- which returns an unconstrained type and it has been expanded into
3763 -- a procedure call. In that case N has been replaced by an object
3764 -- declaration without initializing expression and it has been
3765 -- analyzed (see Expand_Inlined_Call).
3766
3767 if Back_End_Inlining
3768 and then Expander_Active
3769 and then Nkind (E) = N_Function_Call
3770 and then Nkind (Name (E)) in N_Has_Entity
3771 and then Is_Inlined (Entity (Name (E)))
3772 and then not Is_Constrained (Etype (E))
3773 and then Analyzed (N)
3774 and then No (Expression (N))
3775 then
3776 return;
3777 end if;
3778
3779 -- If E is null and has been replaced by an N_Raise_Constraint_Error
3780 -- node (which was marked already-analyzed), we need to set the type
3781 -- to something other than Any_Access in order to keep gigi happy.
3782
3783 if Etype (E) = Any_Access then
3784 Set_Etype (E, T);
3785 end if;
3786
3787 -- If the object is an access to variable, the initialization
3788 -- expression cannot be an access to constant.
3789
3790 if Is_Access_Type (T)
3791 and then not Is_Access_Constant (T)
3792 and then Is_Access_Type (Etype (E))
3793 and then Is_Access_Constant (Etype (E))
3794 then
3795 Error_Msg_N
3796 ("access to variable cannot be initialized with an "
3797 & "access-to-constant expression", E);
3798 end if;
3799
3800 if not Assignment_OK (N) then
3801 Check_Initialization (T, E);
3802 end if;
3803
3804 Check_Unset_Reference (E);
3805
3806 -- If this is a variable, then set current value. If this is a
3807 -- declared constant of a scalar type with a static expression,
3808 -- indicate that it is always valid.
3809
3810 if not Constant_Present (N) then
3811 if Compile_Time_Known_Value (E) then
3812 Set_Current_Value (Id, E);
3813 end if;
3814
3815 elsif Is_Scalar_Type (T) and then Is_OK_Static_Expression (E) then
3816 Set_Is_Known_Valid (Id);
3817 end if;
3818
3819 -- Deal with setting of null flags
3820
3821 if Is_Access_Type (T) then
3822 if Known_Non_Null (E) then
3823 Set_Is_Known_Non_Null (Id, True);
3824 elsif Known_Null (E) and then not Can_Never_Be_Null (Id) then
3825 Set_Is_Known_Null (Id, True);
3826 end if;
3827 end if;
3828
3829 -- Check incorrect use of dynamically tagged expressions
3830
3831 if Is_Tagged_Type (T) then
3832 Check_Dynamically_Tagged_Expression
3833 (Expr => E,
3834 Typ => T,
3835 Related_Nod => N);
3836 end if;
3837
3838 Apply_Scalar_Range_Check (E, T);
3839 Apply_Static_Length_Check (E, T);
3840
3841 if Nkind (Original_Node (N)) = N_Object_Declaration
3842 and then Comes_From_Source (Original_Node (N))
3843
3844 -- Only call test if needed
3845
3846 and then Restriction_Check_Required (SPARK_05)
3847 and then not Is_SPARK_05_Initialization_Expr (Original_Node (E))
3848 then
3849 Check_SPARK_05_Restriction
3850 ("initialization expression is not appropriate", E);
3851 end if;
3852
3853 -- A formal parameter of a specific tagged type whose related
3854 -- subprogram is subject to pragma Extensions_Visible with value
3855 -- "False" cannot be implicitly converted to a class-wide type by
3856 -- means of an initialization expression (SPARK RM 6.1.7(3)).
3857
3858 if Is_Class_Wide_Type (T) and then Is_EVF_Expression (E) then
3859 Error_Msg_N
3860 ("formal parameter with Extensions_Visible False cannot be "
3861 & "implicitly converted to class-wide type", E);
3862 end if;
3863 end if;
3864
3865 -- If the No_Streams restriction is set, check that the type of the
3866 -- object is not, and does not contain, any subtype derived from
3867 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
3868 -- Has_Stream just for efficiency reasons. There is no point in
3869 -- spending time on a Has_Stream check if the restriction is not set.
3870
3871 if Restriction_Check_Required (No_Streams) then
3872 if Has_Stream (T) then
3873 Check_Restriction (No_Streams, N);
3874 end if;
3875 end if;
3876
3877 -- Deal with predicate check before we start to do major rewriting. It
3878 -- is OK to initialize and then check the initialized value, since the
3879 -- object goes out of scope if we get a predicate failure. Note that we
3880 -- do this in the analyzer and not the expander because the analyzer
3881 -- does some substantial rewriting in some cases.
3882
3883 -- We need a predicate check if the type has predicates, and if either
3884 -- there is an initializing expression, or for default initialization
3885 -- when we have at least one case of an explicit default initial value
3886 -- and then this is not an internal declaration whose initialization
3887 -- comes later (as for an aggregate expansion).
3888
3889 if not Suppress_Assignment_Checks (N)
3890 and then Present (Predicate_Function (T))
3891 and then not No_Initialization (N)
3892 and then
3893 (Present (E)
3894 or else
3895 Is_Partially_Initialized_Type (T, Include_Implicit => False))
3896 then
3897 -- If the type has a static predicate and the expression is known at
3898 -- compile time, see if the expression satisfies the predicate.
3899
3900 if Present (E) then
3901 Check_Expression_Against_Static_Predicate (E, T);
3902 end if;
3903
3904 Insert_After (N,
3905 Make_Predicate_Check (T, New_Occurrence_Of (Id, Loc)));
3906 end if;
3907
3908 -- Case of unconstrained type
3909
3910 if Is_Indefinite_Subtype (T) then
3911
3912 -- In SPARK, a declaration of unconstrained type is allowed
3913 -- only for constants of type string.
3914
3915 if Is_String_Type (T) and then not Constant_Present (N) then
3916 Check_SPARK_05_Restriction
3917 ("declaration of object of unconstrained type not allowed", N);
3918 end if;
3919
3920 -- Nothing to do in deferred constant case
3921
3922 if Constant_Present (N) and then No (E) then
3923 null;
3924
3925 -- Case of no initialization present
3926
3927 elsif No (E) then
3928 if No_Initialization (N) then
3929 null;
3930
3931 elsif Is_Class_Wide_Type (T) then
3932 Error_Msg_N
3933 ("initialization required in class-wide declaration ", N);
3934
3935 else
3936 Error_Msg_N
3937 ("unconstrained subtype not allowed (need initialization)",
3938 Object_Definition (N));
3939
3940 if Is_Record_Type (T) and then Has_Discriminants (T) then
3941 Error_Msg_N
3942 ("\provide initial value or explicit discriminant values",
3943 Object_Definition (N));
3944
3945 Error_Msg_NE
3946 ("\or give default discriminant values for type&",
3947 Object_Definition (N), T);
3948
3949 elsif Is_Array_Type (T) then
3950 Error_Msg_N
3951 ("\provide initial value or explicit array bounds",
3952 Object_Definition (N));
3953 end if;
3954 end if;
3955
3956 -- Case of initialization present but in error. Set initial
3957 -- expression as absent (but do not make above complaints)
3958
3959 elsif E = Error then
3960 Set_Expression (N, Empty);
3961 E := Empty;
3962
3963 -- Case of initialization present
3964
3965 else
3966 -- Check restrictions in Ada 83
3967
3968 if not Constant_Present (N) then
3969
3970 -- Unconstrained variables not allowed in Ada 83 mode
3971
3972 if Ada_Version = Ada_83
3973 and then Comes_From_Source (Object_Definition (N))
3974 then
3975 Error_Msg_N
3976 ("(Ada 83) unconstrained variable not allowed",
3977 Object_Definition (N));
3978 end if;
3979 end if;
3980
3981 -- Now we constrain the variable from the initializing expression
3982
3983 -- If the expression is an aggregate, it has been expanded into
3984 -- individual assignments. Retrieve the actual type from the
3985 -- expanded construct.
3986
3987 if Is_Array_Type (T)
3988 and then No_Initialization (N)
3989 and then Nkind (Original_Node (E)) = N_Aggregate
3990 then
3991 Act_T := Etype (E);
3992
3993 -- In case of class-wide interface object declarations we delay
3994 -- the generation of the equivalent record type declarations until
3995 -- its expansion because there are cases in they are not required.
3996
3997 elsif Is_Interface (T) then
3998 null;
3999
4000 -- In GNATprove mode, Expand_Subtype_From_Expr does nothing. Thus,
4001 -- we should prevent the generation of another Itype with the
4002 -- same name as the one already generated, or we end up with
4003 -- two identical types in GNATprove.
4004
4005 elsif GNATprove_Mode then
4006 null;
4007
4008 -- If the type is an unchecked union, no subtype can be built from
4009 -- the expression. Rewrite declaration as a renaming, which the
4010 -- back-end can handle properly. This is a rather unusual case,
4011 -- because most unchecked_union declarations have default values
4012 -- for discriminants and are thus not indefinite.
4013
4014 elsif Is_Unchecked_Union (T) then
4015 if Constant_Present (N) or else Nkind (E) = N_Function_Call then
4016 Set_Ekind (Id, E_Constant);
4017 else
4018 Set_Ekind (Id, E_Variable);
4019 end if;
4020
4021 -- An object declared within a Ghost region is automatically
4022 -- Ghost (SPARK RM 6.9(2)).
4023
4024 if Comes_From_Source (Id) and then Ghost_Mode > None then
4025 Set_Is_Ghost_Entity (Id);
4026
4027 -- The Ghost policy in effect at the point of declaration
4028 -- and at the point of completion must match
4029 -- (SPARK RM 6.9(15)).
4030
4031 if Present (Prev_Entity)
4032 and then Is_Ghost_Entity (Prev_Entity)
4033 then
4034 Check_Ghost_Completion (Prev_Entity, Id);
4035 end if;
4036 end if;
4037
4038 Rewrite (N,
4039 Make_Object_Renaming_Declaration (Loc,
4040 Defining_Identifier => Id,
4041 Subtype_Mark => New_Occurrence_Of (T, Loc),
4042 Name => E));
4043
4044 Set_Renamed_Object (Id, E);
4045 Freeze_Before (N, T);
4046 Set_Is_Frozen (Id);
4047 return;
4048
4049 else
4050 Expand_Subtype_From_Expr (N, T, Object_Definition (N), E);
4051 Act_T := Find_Type_Of_Object (Object_Definition (N), N);
4052 end if;
4053
4054 Set_Is_Constr_Subt_For_U_Nominal (Act_T);
4055
4056 if Aliased_Present (N) then
4057 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4058 end if;
4059
4060 Freeze_Before (N, Act_T);
4061 Freeze_Before (N, T);
4062 end if;
4063
4064 elsif Is_Array_Type (T)
4065 and then No_Initialization (N)
4066 and then Nkind (Original_Node (E)) = N_Aggregate
4067 then
4068 if not Is_Entity_Name (Object_Definition (N)) then
4069 Act_T := Etype (E);
4070 Check_Compile_Time_Size (Act_T);
4071
4072 if Aliased_Present (N) then
4073 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4074 end if;
4075 end if;
4076
4077 -- When the given object definition and the aggregate are specified
4078 -- independently, and their lengths might differ do a length check.
4079 -- This cannot happen if the aggregate is of the form (others =>...)
4080
4081 if not Is_Constrained (T) then
4082 null;
4083
4084 elsif Nkind (E) = N_Raise_Constraint_Error then
4085
4086 -- Aggregate is statically illegal. Place back in declaration
4087
4088 Set_Expression (N, E);
4089 Set_No_Initialization (N, False);
4090
4091 elsif T = Etype (E) then
4092 null;
4093
4094 elsif Nkind (E) = N_Aggregate
4095 and then Present (Component_Associations (E))
4096 and then Present (Choices (First (Component_Associations (E))))
4097 and then Nkind (First
4098 (Choices (First (Component_Associations (E))))) = N_Others_Choice
4099 then
4100 null;
4101
4102 else
4103 Apply_Length_Check (E, T);
4104 end if;
4105
4106 -- If the type is limited unconstrained with defaulted discriminants and
4107 -- there is no expression, then the object is constrained by the
4108 -- defaults, so it is worthwhile building the corresponding subtype.
4109
4110 elsif (Is_Limited_Record (T) or else Is_Concurrent_Type (T))
4111 and then not Is_Constrained (T)
4112 and then Has_Discriminants (T)
4113 then
4114 if No (E) then
4115 Act_T := Build_Default_Subtype (T, N);
4116 else
4117 -- Ada 2005: A limited object may be initialized by means of an
4118 -- aggregate. If the type has default discriminants it has an
4119 -- unconstrained nominal type, Its actual subtype will be obtained
4120 -- from the aggregate, and not from the default discriminants.
4121
4122 Act_T := Etype (E);
4123 end if;
4124
4125 Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
4126
4127 elsif Nkind (E) = N_Function_Call
4128 and then Constant_Present (N)
4129 and then Has_Unconstrained_Elements (Etype (E))
4130 then
4131 -- The back-end has problems with constants of a discriminated type
4132 -- with defaults, if the initial value is a function call. We
4133 -- generate an intermediate temporary that will receive a reference
4134 -- to the result of the call. The initialization expression then
4135 -- becomes a dereference of that temporary.
4136
4137 Remove_Side_Effects (E);
4138
4139 -- If this is a constant declaration of an unconstrained type and
4140 -- the initialization is an aggregate, we can use the subtype of the
4141 -- aggregate for the declared entity because it is immutable.
4142
4143 elsif not Is_Constrained (T)
4144 and then Has_Discriminants (T)
4145 and then Constant_Present (N)
4146 and then not Has_Unchecked_Union (T)
4147 and then Nkind (E) = N_Aggregate
4148 then
4149 Act_T := Etype (E);
4150 end if;
4151
4152 -- Check No_Wide_Characters restriction
4153
4154 Check_Wide_Character_Restriction (T, Object_Definition (N));
4155
4156 -- Indicate this is not set in source. Certainly true for constants, and
4157 -- true for variables so far (will be reset for a variable if and when
4158 -- we encounter a modification in the source).
4159
4160 Set_Never_Set_In_Source (Id);
4161
4162 -- Now establish the proper kind and type of the object
4163
4164 if Constant_Present (N) then
4165 Set_Ekind (Id, E_Constant);
4166 Set_Is_True_Constant (Id);
4167
4168 else
4169 Set_Ekind (Id, E_Variable);
4170
4171 -- A variable is set as shared passive if it appears in a shared
4172 -- passive package, and is at the outer level. This is not done for
4173 -- entities generated during expansion, because those are always
4174 -- manipulated locally.
4175
4176 if Is_Shared_Passive (Current_Scope)
4177 and then Is_Library_Level_Entity (Id)
4178 and then Comes_From_Source (Id)
4179 then
4180 Set_Is_Shared_Passive (Id);
4181 Check_Shared_Var (Id, T, N);
4182 end if;
4183
4184 -- Set Has_Initial_Value if initializing expression present. Note
4185 -- that if there is no initializing expression, we leave the state
4186 -- of this flag unchanged (usually it will be False, but notably in
4187 -- the case of exception choice variables, it will already be true).
4188
4189 if Present (E) then
4190 Set_Has_Initial_Value (Id);
4191 end if;
4192 end if;
4193
4194 -- Initialize alignment and size and capture alignment setting
4195
4196 Init_Alignment (Id);
4197 Init_Esize (Id);
4198 Set_Optimize_Alignment_Flags (Id);
4199
4200 -- An object declared within a Ghost region is automatically Ghost
4201 -- (SPARK RM 6.9(2)).
4202
4203 if Comes_From_Source (Id)
4204 and then (Ghost_Mode > None
4205 or else (Present (Prev_Entity)
4206 and then Is_Ghost_Entity (Prev_Entity)))
4207 then
4208 Set_Is_Ghost_Entity (Id);
4209
4210 -- The Ghost policy in effect at the point of declaration and at the
4211 -- point of completion must match (SPARK RM 6.9(16)).
4212
4213 if Present (Prev_Entity) and then Is_Ghost_Entity (Prev_Entity) then
4214 Check_Ghost_Completion (Prev_Entity, Id);
4215 end if;
4216 end if;
4217
4218 -- Deal with aliased case
4219
4220 if Aliased_Present (N) then
4221 Set_Is_Aliased (Id);
4222
4223 -- If the object is aliased and the type is unconstrained with
4224 -- defaulted discriminants and there is no expression, then the
4225 -- object is constrained by the defaults, so it is worthwhile
4226 -- building the corresponding subtype.
4227
4228 -- Ada 2005 (AI-363): If the aliased object is discriminated and
4229 -- unconstrained, then only establish an actual subtype if the
4230 -- nominal subtype is indefinite. In definite cases the object is
4231 -- unconstrained in Ada 2005.
4232
4233 if No (E)
4234 and then Is_Record_Type (T)
4235 and then not Is_Constrained (T)
4236 and then Has_Discriminants (T)
4237 and then (Ada_Version < Ada_2005 or else Is_Indefinite_Subtype (T))
4238 then
4239 Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
4240 end if;
4241 end if;
4242
4243 -- Now we can set the type of the object
4244
4245 Set_Etype (Id, Act_T);
4246
4247 -- Non-constant object is marked to be treated as volatile if type is
4248 -- volatile and we clear the Current_Value setting that may have been
4249 -- set above. Doing so for constants isn't required and might interfere
4250 -- with possible uses of the object as a static expression in contexts
4251 -- incompatible with volatility (e.g. as a case-statement alternative).
4252
4253 if Ekind (Id) /= E_Constant and then Treat_As_Volatile (Etype (Id)) then
4254 Set_Treat_As_Volatile (Id);
4255 Set_Current_Value (Id, Empty);
4256 end if;
4257
4258 -- Deal with controlled types
4259
4260 if Has_Controlled_Component (Etype (Id))
4261 or else Is_Controlled (Etype (Id))
4262 then
4263 if not Is_Library_Level_Entity (Id) then
4264 Check_Restriction (No_Nested_Finalization, N);
4265 else
4266 Validate_Controlled_Object (Id);
4267 end if;
4268 end if;
4269
4270 if Has_Task (Etype (Id)) then
4271 Check_Restriction (No_Tasking, N);
4272
4273 -- Deal with counting max tasks
4274
4275 -- Nothing to do if inside a generic
4276
4277 if Inside_A_Generic then
4278 null;
4279
4280 -- If library level entity, then count tasks
4281
4282 elsif Is_Library_Level_Entity (Id) then
4283 Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
4284
4285 -- If not library level entity, then indicate we don't know max
4286 -- tasks and also check task hierarchy restriction and blocking
4287 -- operation (since starting a task is definitely blocking).
4288
4289 else
4290 Check_Restriction (Max_Tasks, N);
4291 Check_Restriction (No_Task_Hierarchy, N);
4292 Check_Potentially_Blocking_Operation (N);
4293 end if;
4294
4295 -- A rather specialized test. If we see two tasks being declared
4296 -- of the same type in the same object declaration, and the task
4297 -- has an entry with an address clause, we know that program error
4298 -- will be raised at run time since we can't have two tasks with
4299 -- entries at the same address.
4300
4301 if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
4302 declare
4303 E : Entity_Id;
4304
4305 begin
4306 E := First_Entity (Etype (Id));
4307 while Present (E) loop
4308 if Ekind (E) = E_Entry
4309 and then Present (Get_Attribute_Definition_Clause
4310 (E, Attribute_Address))
4311 then
4312 Error_Msg_Warn := SPARK_Mode /= On;
4313 Error_Msg_N
4314 ("more than one task with same entry address<<", N);
4315 Error_Msg_N ("\Program_Error [<<", N);
4316 Insert_Action (N,
4317 Make_Raise_Program_Error (Loc,
4318 Reason => PE_Duplicated_Entry_Address));
4319 exit;
4320 end if;
4321
4322 Next_Entity (E);
4323 end loop;
4324 end;
4325 end if;
4326 end if;
4327
4328 -- Some simple constant-propagation: if the expression is a constant
4329 -- string initialized with a literal, share the literal. This avoids
4330 -- a run-time copy.
4331
4332 if Present (E)
4333 and then Is_Entity_Name (E)
4334 and then Ekind (Entity (E)) = E_Constant
4335 and then Base_Type (Etype (E)) = Standard_String
4336 then
4337 declare
4338 Val : constant Node_Id := Constant_Value (Entity (E));
4339 begin
4340 if Present (Val) and then Nkind (Val) = N_String_Literal then
4341 Rewrite (E, New_Copy (Val));
4342 end if;
4343 end;
4344 end if;
4345
4346 -- Another optimization: if the nominal subtype is unconstrained and
4347 -- the expression is a function call that returns an unconstrained
4348 -- type, rewrite the declaration as a renaming of the result of the
4349 -- call. The exceptions below are cases where the copy is expected,
4350 -- either by the back end (Aliased case) or by the semantics, as for
4351 -- initializing controlled types or copying tags for classwide types.
4352
4353 if Present (E)
4354 and then Nkind (E) = N_Explicit_Dereference
4355 and then Nkind (Original_Node (E)) = N_Function_Call
4356 and then not Is_Library_Level_Entity (Id)
4357 and then not Is_Constrained (Underlying_Type (T))
4358 and then not Is_Aliased (Id)
4359 and then not Is_Class_Wide_Type (T)
4360 and then not Is_Controlled (T)
4361 and then not Has_Controlled_Component (Base_Type (T))
4362 and then Expander_Active
4363 then
4364 Rewrite (N,
4365 Make_Object_Renaming_Declaration (Loc,
4366 Defining_Identifier => Id,
4367 Access_Definition => Empty,
4368 Subtype_Mark => New_Occurrence_Of
4369 (Base_Type (Etype (Id)), Loc),
4370 Name => E));
4371
4372 Set_Renamed_Object (Id, E);
4373
4374 -- Force generation of debugging information for the constant and for
4375 -- the renamed function call.
4376
4377 Set_Debug_Info_Needed (Id);
4378 Set_Debug_Info_Needed (Entity (Prefix (E)));
4379 end if;
4380
4381 if Present (Prev_Entity)
4382 and then Is_Frozen (Prev_Entity)
4383 and then not Error_Posted (Id)
4384 then
4385 Error_Msg_N ("full constant declaration appears too late", N);
4386 end if;
4387
4388 Check_Eliminated (Id);
4389
4390 -- Deal with setting In_Private_Part flag if in private part
4391
4392 if Ekind (Scope (Id)) = E_Package and then In_Private_Part (Scope (Id))
4393 then
4394 Set_In_Private_Part (Id);
4395 end if;
4396
4397 -- Check for violation of No_Local_Timing_Events
4398
4399 if Restriction_Check_Required (No_Local_Timing_Events)
4400 and then not Is_Library_Level_Entity (Id)
4401 and then Is_RTE (Etype (Id), RE_Timing_Event)
4402 then
4403 Check_Restriction (No_Local_Timing_Events, N);
4404 end if;
4405
4406 <<Leave>>
4407 -- Initialize the refined state of a variable here because this is a
4408 -- common destination for legal and illegal object declarations.
4409
4410 if Ekind (Id) = E_Variable then
4411 Set_Encapsulating_State (Id, Empty);
4412 end if;
4413
4414 if Has_Aspects (N) then
4415 Analyze_Aspect_Specifications (N, Id);
4416 end if;
4417
4418 Analyze_Dimension (N);
4419
4420 -- Verify whether the object declaration introduces an illegal hidden
4421 -- state within a package subject to a null abstract state.
4422
4423 if Ekind (Id) = E_Variable then
4424 Check_No_Hidden_State (Id);
4425 end if;
4426 end Analyze_Object_Declaration;
4427
4428 ---------------------------
4429 -- Analyze_Others_Choice --
4430 ---------------------------
4431
4432 -- Nothing to do for the others choice node itself, the semantic analysis
4433 -- of the others choice will occur as part of the processing of the parent
4434
4435 procedure Analyze_Others_Choice (N : Node_Id) is
4436 pragma Warnings (Off, N);
4437 begin
4438 null;
4439 end Analyze_Others_Choice;
4440
4441 -------------------------------------------
4442 -- Analyze_Private_Extension_Declaration --
4443 -------------------------------------------
4444
4445 procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
4446 T : constant Entity_Id := Defining_Identifier (N);
4447 Indic : constant Node_Id := Subtype_Indication (N);
4448 Parent_Type : Entity_Id;
4449 Parent_Base : Entity_Id;
4450
4451 begin
4452 -- The private extension declaration may be subject to pragma Ghost with
4453 -- policy Ignore. Set the mode now to ensure that any nodes generated
4454 -- during analysis and expansion are properly flagged as ignored Ghost.
4455
4456 Set_Ghost_Mode (N);
4457
4458 -- Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
4459
4460 if Is_Non_Empty_List (Interface_List (N)) then
4461 declare
4462 Intf : Node_Id;
4463 T : Entity_Id;
4464
4465 begin
4466 Intf := First (Interface_List (N));
4467 while Present (Intf) loop
4468 T := Find_Type_Of_Subtype_Indic (Intf);
4469
4470 Diagnose_Interface (Intf, T);
4471 Next (Intf);
4472 end loop;
4473 end;
4474 end if;
4475
4476 Generate_Definition (T);
4477
4478 -- For other than Ada 2012, just enter the name in the current scope
4479
4480 if Ada_Version < Ada_2012 then
4481 Enter_Name (T);
4482
4483 -- Ada 2012 (AI05-0162): Enter the name in the current scope handling
4484 -- case of private type that completes an incomplete type.
4485
4486 else
4487 declare
4488 Prev : Entity_Id;
4489
4490 begin
4491 Prev := Find_Type_Name (N);
4492
4493 pragma Assert (Prev = T
4494 or else (Ekind (Prev) = E_Incomplete_Type
4495 and then Present (Full_View (Prev))
4496 and then Full_View (Prev) = T));
4497 end;
4498 end if;
4499
4500 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
4501 Parent_Base := Base_Type (Parent_Type);
4502
4503 if Parent_Type = Any_Type or else Etype (Parent_Type) = Any_Type then
4504 Set_Ekind (T, Ekind (Parent_Type));
4505 Set_Etype (T, Any_Type);
4506 goto Leave;
4507
4508 elsif not Is_Tagged_Type (Parent_Type) then
4509 Error_Msg_N
4510 ("parent of type extension must be a tagged type ", Indic);
4511 goto Leave;
4512
4513 elsif Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
4514 Error_Msg_N ("premature derivation of incomplete type", Indic);
4515 goto Leave;
4516
4517 elsif Is_Concurrent_Type (Parent_Type) then
4518 Error_Msg_N
4519 ("parent type of a private extension cannot be "
4520 & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
4521
4522 Set_Etype (T, Any_Type);
4523 Set_Ekind (T, E_Limited_Private_Type);
4524 Set_Private_Dependents (T, New_Elmt_List);
4525 Set_Error_Posted (T);
4526 goto Leave;
4527 end if;
4528
4529 -- Perhaps the parent type should be changed to the class-wide type's
4530 -- specific type in this case to prevent cascading errors ???
4531
4532 if Is_Class_Wide_Type (Parent_Type) then
4533 Error_Msg_N
4534 ("parent of type extension must not be a class-wide type", Indic);
4535 goto Leave;
4536 end if;
4537
4538 if (not Is_Package_Or_Generic_Package (Current_Scope)
4539 and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
4540 or else In_Private_Part (Current_Scope)
4541
4542 then
4543 Error_Msg_N ("invalid context for private extension", N);
4544 end if;
4545
4546 -- Set common attributes
4547
4548 Set_Is_Pure (T, Is_Pure (Current_Scope));
4549 Set_Scope (T, Current_Scope);
4550 Set_Ekind (T, E_Record_Type_With_Private);
4551 Init_Size_Align (T);
4552 Set_Default_SSO (T);
4553
4554 Set_Etype (T, Parent_Base);
4555 Set_Has_Task (T, Has_Task (Parent_Base));
4556 Set_Has_Protected (T, Has_Task (Parent_Base));
4557
4558 Set_Convention (T, Convention (Parent_Type));
4559 Set_First_Rep_Item (T, First_Rep_Item (Parent_Type));
4560 Set_Is_First_Subtype (T);
4561 Make_Class_Wide_Type (T);
4562
4563 if Unknown_Discriminants_Present (N) then
4564 Set_Discriminant_Constraint (T, No_Elist);
4565 end if;
4566
4567 Build_Derived_Record_Type (N, Parent_Type, T);
4568
4569 -- Propagate inherited invariant information. The new type has
4570 -- invariants, if the parent type has inheritable invariants,
4571 -- and these invariants can in turn be inherited.
4572
4573 if Has_Inheritable_Invariants (Parent_Type) then
4574 Set_Has_Inheritable_Invariants (T);
4575 Set_Has_Invariants (T);
4576 end if;
4577
4578 -- Ada 2005 (AI-443): Synchronized private extension or a rewritten
4579 -- synchronized formal derived type.
4580
4581 if Ada_Version >= Ada_2005 and then Synchronized_Present (N) then
4582 Set_Is_Limited_Record (T);
4583
4584 -- Formal derived type case
4585
4586 if Is_Generic_Type (T) then
4587
4588 -- The parent must be a tagged limited type or a synchronized
4589 -- interface.
4590
4591 if (not Is_Tagged_Type (Parent_Type)
4592 or else not Is_Limited_Type (Parent_Type))
4593 and then
4594 (not Is_Interface (Parent_Type)
4595 or else not Is_Synchronized_Interface (Parent_Type))
4596 then
4597 Error_Msg_NE ("parent type of & must be tagged limited " &
4598 "or synchronized", N, T);
4599 end if;
4600
4601 -- The progenitors (if any) must be limited or synchronized
4602 -- interfaces.
4603
4604 if Present (Interfaces (T)) then
4605 declare
4606 Iface : Entity_Id;
4607 Iface_Elmt : Elmt_Id;
4608
4609 begin
4610 Iface_Elmt := First_Elmt (Interfaces (T));
4611 while Present (Iface_Elmt) loop
4612 Iface := Node (Iface_Elmt);
4613
4614 if not Is_Limited_Interface (Iface)
4615 and then not Is_Synchronized_Interface (Iface)
4616 then
4617 Error_Msg_NE ("progenitor & must be limited " &
4618 "or synchronized", N, Iface);
4619 end if;
4620
4621 Next_Elmt (Iface_Elmt);
4622 end loop;
4623 end;
4624 end if;
4625
4626 -- Regular derived extension, the parent must be a limited or
4627 -- synchronized interface.
4628
4629 else
4630 if not Is_Interface (Parent_Type)
4631 or else (not Is_Limited_Interface (Parent_Type)
4632 and then not Is_Synchronized_Interface (Parent_Type))
4633 then
4634 Error_Msg_NE
4635 ("parent type of & must be limited interface", N, T);
4636 end if;
4637 end if;
4638
4639 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
4640 -- extension with a synchronized parent must be explicitly declared
4641 -- synchronized, because the full view will be a synchronized type.
4642 -- This must be checked before the check for limited types below,
4643 -- to ensure that types declared limited are not allowed to extend
4644 -- synchronized interfaces.
4645
4646 elsif Is_Interface (Parent_Type)
4647 and then Is_Synchronized_Interface (Parent_Type)
4648 and then not Synchronized_Present (N)
4649 then
4650 Error_Msg_NE
4651 ("private extension of& must be explicitly synchronized",
4652 N, Parent_Type);
4653
4654 elsif Limited_Present (N) then
4655 Set_Is_Limited_Record (T);
4656
4657 if not Is_Limited_Type (Parent_Type)
4658 and then
4659 (not Is_Interface (Parent_Type)
4660 or else not Is_Limited_Interface (Parent_Type))
4661 then
4662 Error_Msg_NE ("parent type& of limited extension must be limited",
4663 N, Parent_Type);
4664 end if;
4665 end if;
4666
4667 <<Leave>>
4668 if Has_Aspects (N) then
4669 Analyze_Aspect_Specifications (N, T);
4670 end if;
4671 end Analyze_Private_Extension_Declaration;
4672
4673 ---------------------------------
4674 -- Analyze_Subtype_Declaration --
4675 ---------------------------------
4676
4677 procedure Analyze_Subtype_Declaration
4678 (N : Node_Id;
4679 Skip : Boolean := False)
4680 is
4681 Id : constant Entity_Id := Defining_Identifier (N);
4682 T : Entity_Id;
4683 R_Checks : Check_Result;
4684
4685 begin
4686 -- The subtype declaration may be subject to pragma Ghost with policy
4687 -- Ignore. Set the mode now to ensure that any nodes generated during
4688 -- analysis and expansion are properly flagged as ignored Ghost.
4689
4690 Set_Ghost_Mode (N);
4691
4692 Generate_Definition (Id);
4693 Set_Is_Pure (Id, Is_Pure (Current_Scope));
4694 Init_Size_Align (Id);
4695
4696 -- The following guard condition on Enter_Name is to handle cases where
4697 -- the defining identifier has already been entered into the scope but
4698 -- the declaration as a whole needs to be analyzed.
4699
4700 -- This case in particular happens for derived enumeration types. The
4701 -- derived enumeration type is processed as an inserted enumeration type
4702 -- declaration followed by a rewritten subtype declaration. The defining
4703 -- identifier, however, is entered into the name scope very early in the
4704 -- processing of the original type declaration and therefore needs to be
4705 -- avoided here, when the created subtype declaration is analyzed. (See
4706 -- Build_Derived_Types)
4707
4708 -- This also happens when the full view of a private type is derived
4709 -- type with constraints. In this case the entity has been introduced
4710 -- in the private declaration.
4711
4712 -- Finally this happens in some complex cases when validity checks are
4713 -- enabled, where the same subtype declaration may be analyzed twice.
4714 -- This can happen if the subtype is created by the pre-analysis of
4715 -- an attribute tht gives the range of a loop statement, and the loop
4716 -- itself appears within an if_statement that will be rewritten during
4717 -- expansion.
4718
4719 if Skip
4720 or else (Present (Etype (Id))
4721 and then (Is_Private_Type (Etype (Id))
4722 or else Is_Task_Type (Etype (Id))
4723 or else Is_Rewrite_Substitution (N)))
4724 then
4725 null;
4726
4727 elsif Current_Entity (Id) = Id then
4728 null;
4729
4730 else
4731 Enter_Name (Id);
4732 end if;
4733
4734 T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
4735
4736 -- Class-wide equivalent types of records with unknown discriminants
4737 -- involve the generation of an itype which serves as the private view
4738 -- of a constrained record subtype. In such cases the base type of the
4739 -- current subtype we are processing is the private itype. Use the full
4740 -- of the private itype when decorating various attributes.
4741
4742 if Is_Itype (T)
4743 and then Is_Private_Type (T)
4744 and then Present (Full_View (T))
4745 then
4746 T := Full_View (T);
4747 end if;
4748
4749 -- Inherit common attributes
4750
4751 Set_Is_Volatile (Id, Is_Volatile (T));
4752 Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
4753 Set_Is_Generic_Type (Id, Is_Generic_Type (Base_Type (T)));
4754 Set_Convention (Id, Convention (T));
4755
4756 -- If ancestor has predicates then so does the subtype, and in addition
4757 -- we must delay the freeze to properly arrange predicate inheritance.
4758
4759 -- The Ancestor_Type test is really unpleasant, there seem to be cases
4760 -- in which T = ID, so the above tests and assignments do nothing???
4761
4762 if Has_Predicates (T)
4763 or else (Present (Ancestor_Subtype (T))
4764 and then Has_Predicates (Ancestor_Subtype (T)))
4765 then
4766 Set_Has_Predicates (Id);
4767 Set_Has_Delayed_Freeze (Id);
4768 end if;
4769
4770 -- Subtype of Boolean cannot have a constraint in SPARK
4771
4772 if Is_Boolean_Type (T)
4773 and then Nkind (Subtype_Indication (N)) = N_Subtype_Indication
4774 then
4775 Check_SPARK_05_Restriction
4776 ("subtype of Boolean cannot have constraint", N);
4777 end if;
4778
4779 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
4780 declare
4781 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
4782 One_Cstr : Node_Id;
4783 Low : Node_Id;
4784 High : Node_Id;
4785
4786 begin
4787 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint then
4788 One_Cstr := First (Constraints (Cstr));
4789 while Present (One_Cstr) loop
4790
4791 -- Index or discriminant constraint in SPARK must be a
4792 -- subtype mark.
4793
4794 if not
4795 Nkind_In (One_Cstr, N_Identifier, N_Expanded_Name)
4796 then
4797 Check_SPARK_05_Restriction
4798 ("subtype mark required", One_Cstr);
4799
4800 -- String subtype must have a lower bound of 1 in SPARK.
4801 -- Note that we do not need to test for the non-static case
4802 -- here, since that was already taken care of in
4803 -- Process_Range_Expr_In_Decl.
4804
4805 elsif Base_Type (T) = Standard_String then
4806 Get_Index_Bounds (One_Cstr, Low, High);
4807
4808 if Is_OK_Static_Expression (Low)
4809 and then Expr_Value (Low) /= 1
4810 then
4811 Check_SPARK_05_Restriction
4812 ("String subtype must have lower bound of 1", N);
4813 end if;
4814 end if;
4815
4816 Next (One_Cstr);
4817 end loop;
4818 end if;
4819 end;
4820 end if;
4821
4822 -- In the case where there is no constraint given in the subtype
4823 -- indication, Process_Subtype just returns the Subtype_Mark, so its
4824 -- semantic attributes must be established here.
4825
4826 if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
4827 Set_Etype (Id, Base_Type (T));
4828
4829 -- Subtype of unconstrained array without constraint is not allowed
4830 -- in SPARK.
4831
4832 if Is_Array_Type (T) and then not Is_Constrained (T) then
4833 Check_SPARK_05_Restriction
4834 ("subtype of unconstrained array must have constraint", N);
4835 end if;
4836
4837 case Ekind (T) is
4838 when Array_Kind =>
4839 Set_Ekind (Id, E_Array_Subtype);
4840 Copy_Array_Subtype_Attributes (Id, T);
4841
4842 when Decimal_Fixed_Point_Kind =>
4843 Set_Ekind (Id, E_Decimal_Fixed_Point_Subtype);
4844 Set_Digits_Value (Id, Digits_Value (T));
4845 Set_Delta_Value (Id, Delta_Value (T));
4846 Set_Scale_Value (Id, Scale_Value (T));
4847 Set_Small_Value (Id, Small_Value (T));
4848 Set_Scalar_Range (Id, Scalar_Range (T));
4849 Set_Machine_Radix_10 (Id, Machine_Radix_10 (T));
4850 Set_Is_Constrained (Id, Is_Constrained (T));
4851 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4852 Set_RM_Size (Id, RM_Size (T));
4853
4854 when Enumeration_Kind =>
4855 Set_Ekind (Id, E_Enumeration_Subtype);
4856 Set_First_Literal (Id, First_Literal (Base_Type (T)));
4857 Set_Scalar_Range (Id, Scalar_Range (T));
4858 Set_Is_Character_Type (Id, Is_Character_Type (T));
4859 Set_Is_Constrained (Id, Is_Constrained (T));
4860 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4861 Set_RM_Size (Id, RM_Size (T));
4862 Inherit_Predicate_Flags (Id, T);
4863
4864 when Ordinary_Fixed_Point_Kind =>
4865 Set_Ekind (Id, E_Ordinary_Fixed_Point_Subtype);
4866 Set_Scalar_Range (Id, Scalar_Range (T));
4867 Set_Small_Value (Id, Small_Value (T));
4868 Set_Delta_Value (Id, Delta_Value (T));
4869 Set_Is_Constrained (Id, Is_Constrained (T));
4870 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4871 Set_RM_Size (Id, RM_Size (T));
4872
4873 when Float_Kind =>
4874 Set_Ekind (Id, E_Floating_Point_Subtype);
4875 Set_Scalar_Range (Id, Scalar_Range (T));
4876 Set_Digits_Value (Id, Digits_Value (T));
4877 Set_Is_Constrained (Id, Is_Constrained (T));
4878
4879 when Signed_Integer_Kind =>
4880 Set_Ekind (Id, E_Signed_Integer_Subtype);
4881 Set_Scalar_Range (Id, Scalar_Range (T));
4882 Set_Is_Constrained (Id, Is_Constrained (T));
4883 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4884 Set_RM_Size (Id, RM_Size (T));
4885 Inherit_Predicate_Flags (Id, T);
4886
4887 when Modular_Integer_Kind =>
4888 Set_Ekind (Id, E_Modular_Integer_Subtype);
4889 Set_Scalar_Range (Id, Scalar_Range (T));
4890 Set_Is_Constrained (Id, Is_Constrained (T));
4891 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4892 Set_RM_Size (Id, RM_Size (T));
4893 Inherit_Predicate_Flags (Id, T);
4894
4895 when Class_Wide_Kind =>
4896 Set_Ekind (Id, E_Class_Wide_Subtype);
4897 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
4898 Set_Cloned_Subtype (Id, T);
4899 Set_Is_Tagged_Type (Id, True);
4900 Set_Has_Unknown_Discriminants
4901 (Id, True);
4902 Set_No_Tagged_Streams_Pragma
4903 (Id, No_Tagged_Streams_Pragma (T));
4904
4905 if Ekind (T) = E_Class_Wide_Subtype then
4906 Set_Equivalent_Type (Id, Equivalent_Type (T));
4907 end if;
4908
4909 when E_Record_Type | E_Record_Subtype =>
4910 Set_Ekind (Id, E_Record_Subtype);
4911
4912 if Ekind (T) = E_Record_Subtype
4913 and then Present (Cloned_Subtype (T))
4914 then
4915 Set_Cloned_Subtype (Id, Cloned_Subtype (T));
4916 else
4917 Set_Cloned_Subtype (Id, T);
4918 end if;
4919
4920 Set_First_Entity (Id, First_Entity (T));
4921 Set_Last_Entity (Id, Last_Entity (T));
4922 Set_Has_Discriminants (Id, Has_Discriminants (T));
4923 Set_Is_Constrained (Id, Is_Constrained (T));
4924 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
4925 Set_Has_Implicit_Dereference
4926 (Id, Has_Implicit_Dereference (T));
4927 Set_Has_Unknown_Discriminants
4928 (Id, Has_Unknown_Discriminants (T));
4929
4930 if Has_Discriminants (T) then
4931 Set_Discriminant_Constraint
4932 (Id, Discriminant_Constraint (T));
4933 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
4934
4935 elsif Has_Unknown_Discriminants (Id) then
4936 Set_Discriminant_Constraint (Id, No_Elist);
4937 end if;
4938
4939 if Is_Tagged_Type (T) then
4940 Set_Is_Tagged_Type (Id, True);
4941 Set_No_Tagged_Streams_Pragma
4942 (Id, No_Tagged_Streams_Pragma (T));
4943 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
4944 Set_Direct_Primitive_Operations
4945 (Id, Direct_Primitive_Operations (T));
4946 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
4947
4948 if Is_Interface (T) then
4949 Set_Is_Interface (Id);
4950 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
4951 end if;
4952 end if;
4953
4954 when Private_Kind =>
4955 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
4956 Set_Has_Discriminants (Id, Has_Discriminants (T));
4957 Set_Is_Constrained (Id, Is_Constrained (T));
4958 Set_First_Entity (Id, First_Entity (T));
4959 Set_Last_Entity (Id, Last_Entity (T));
4960 Set_Private_Dependents (Id, New_Elmt_List);
4961 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
4962 Set_Has_Implicit_Dereference
4963 (Id, Has_Implicit_Dereference (T));
4964 Set_Has_Unknown_Discriminants
4965 (Id, Has_Unknown_Discriminants (T));
4966 Set_Known_To_Have_Preelab_Init
4967 (Id, Known_To_Have_Preelab_Init (T));
4968
4969 if Is_Tagged_Type (T) then
4970 Set_Is_Tagged_Type (Id);
4971 Set_No_Tagged_Streams_Pragma (Id,
4972 No_Tagged_Streams_Pragma (T));
4973 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
4974 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
4975 Set_Direct_Primitive_Operations (Id,
4976 Direct_Primitive_Operations (T));
4977 end if;
4978
4979 -- In general the attributes of the subtype of a private type
4980 -- are the attributes of the partial view of parent. However,
4981 -- the full view may be a discriminated type, and the subtype
4982 -- must share the discriminant constraint to generate correct
4983 -- calls to initialization procedures.
4984
4985 if Has_Discriminants (T) then
4986 Set_Discriminant_Constraint
4987 (Id, Discriminant_Constraint (T));
4988 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
4989
4990 elsif Present (Full_View (T))
4991 and then Has_Discriminants (Full_View (T))
4992 then
4993 Set_Discriminant_Constraint
4994 (Id, Discriminant_Constraint (Full_View (T)));
4995 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
4996
4997 -- This would seem semantically correct, but apparently
4998 -- generates spurious errors about missing components ???
4999
5000 -- Set_Has_Discriminants (Id);
5001 end if;
5002
5003 Prepare_Private_Subtype_Completion (Id, N);
5004
5005 -- If this is the subtype of a constrained private type with
5006 -- discriminants that has got a full view and we also have
5007 -- built a completion just above, show that the completion
5008 -- is a clone of the full view to the back-end.
5009
5010 if Has_Discriminants (T)
5011 and then not Has_Unknown_Discriminants (T)
5012 and then not Is_Empty_Elmt_List (Discriminant_Constraint (T))
5013 and then Present (Full_View (T))
5014 and then Present (Full_View (Id))
5015 then
5016 Set_Cloned_Subtype (Full_View (Id), Full_View (T));
5017 end if;
5018
5019 when Access_Kind =>
5020 Set_Ekind (Id, E_Access_Subtype);
5021 Set_Is_Constrained (Id, Is_Constrained (T));
5022 Set_Is_Access_Constant
5023 (Id, Is_Access_Constant (T));
5024 Set_Directly_Designated_Type
5025 (Id, Designated_Type (T));
5026 Set_Can_Never_Be_Null (Id, Can_Never_Be_Null (T));
5027
5028 -- A Pure library_item must not contain the declaration of a
5029 -- named access type, except within a subprogram, generic
5030 -- subprogram, task unit, or protected unit, or if it has
5031 -- a specified Storage_Size of zero (RM05-10.2.1(15.4-15.5)).
5032
5033 if Comes_From_Source (Id)
5034 and then In_Pure_Unit
5035 and then not In_Subprogram_Task_Protected_Unit
5036 and then not No_Pool_Assigned (Id)
5037 then
5038 Error_Msg_N
5039 ("named access types not allowed in pure unit", N);
5040 end if;
5041
5042 when Concurrent_Kind =>
5043 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
5044 Set_Corresponding_Record_Type (Id,
5045 Corresponding_Record_Type (T));
5046 Set_First_Entity (Id, First_Entity (T));
5047 Set_First_Private_Entity (Id, First_Private_Entity (T));
5048 Set_Has_Discriminants (Id, Has_Discriminants (T));
5049 Set_Is_Constrained (Id, Is_Constrained (T));
5050 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5051 Set_Last_Entity (Id, Last_Entity (T));
5052
5053 if Is_Tagged_Type (T) then
5054 Set_No_Tagged_Streams_Pragma
5055 (Id, No_Tagged_Streams_Pragma (T));
5056 end if;
5057
5058 if Has_Discriminants (T) then
5059 Set_Discriminant_Constraint
5060 (Id, Discriminant_Constraint (T));
5061 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5062 end if;
5063
5064 when Incomplete_Kind =>
5065 if Ada_Version >= Ada_2005 then
5066
5067 -- In Ada 2005 an incomplete type can be explicitly tagged:
5068 -- propagate indication. Note that we also have to include
5069 -- subtypes for Ada 2012 extended use of incomplete types.
5070
5071 Set_Ekind (Id, E_Incomplete_Subtype);
5072 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5073 Set_Private_Dependents (Id, New_Elmt_List);
5074
5075 if Is_Tagged_Type (Id) then
5076 Set_No_Tagged_Streams_Pragma
5077 (Id, No_Tagged_Streams_Pragma (T));
5078 Set_Direct_Primitive_Operations (Id, New_Elmt_List);
5079 end if;
5080
5081 -- Ada 2005 (AI-412): Decorate an incomplete subtype of an
5082 -- incomplete type visible through a limited with clause.
5083
5084 if From_Limited_With (T)
5085 and then Present (Non_Limited_View (T))
5086 then
5087 Set_From_Limited_With (Id);
5088 Set_Non_Limited_View (Id, Non_Limited_View (T));
5089
5090 -- Ada 2005 (AI-412): Add the regular incomplete subtype
5091 -- to the private dependents of the original incomplete
5092 -- type for future transformation.
5093
5094 else
5095 Append_Elmt (Id, Private_Dependents (T));
5096 end if;
5097
5098 -- If the subtype name denotes an incomplete type an error
5099 -- was already reported by Process_Subtype.
5100
5101 else
5102 Set_Etype (Id, Any_Type);
5103 end if;
5104
5105 when others =>
5106 raise Program_Error;
5107 end case;
5108 end if;
5109
5110 if Etype (Id) = Any_Type then
5111 goto Leave;
5112 end if;
5113
5114 -- Some common processing on all types
5115
5116 Set_Size_Info (Id, T);
5117 Set_First_Rep_Item (Id, First_Rep_Item (T));
5118
5119 -- If the parent type is a generic actual, so is the subtype. This may
5120 -- happen in a nested instance. Why Comes_From_Source test???
5121
5122 if not Comes_From_Source (N) then
5123 Set_Is_Generic_Actual_Type (Id, Is_Generic_Actual_Type (T));
5124 end if;
5125
5126 T := Etype (Id);
5127
5128 Set_Is_Immediately_Visible (Id, True);
5129 Set_Depends_On_Private (Id, Has_Private_Component (T));
5130 Set_Is_Descendent_Of_Address (Id, Is_Descendent_Of_Address (T));
5131
5132 if Is_Interface (T) then
5133 Set_Is_Interface (Id);
5134 end if;
5135
5136 if Present (Generic_Parent_Type (N))
5137 and then
5138 (Nkind (Parent (Generic_Parent_Type (N))) /=
5139 N_Formal_Type_Declaration
5140 or else Nkind (Formal_Type_Definition
5141 (Parent (Generic_Parent_Type (N)))) /=
5142 N_Formal_Private_Type_Definition)
5143 then
5144 if Is_Tagged_Type (Id) then
5145
5146 -- If this is a generic actual subtype for a synchronized type,
5147 -- the primitive operations are those of the corresponding record
5148 -- for which there is a separate subtype declaration.
5149
5150 if Is_Concurrent_Type (Id) then
5151 null;
5152 elsif Is_Class_Wide_Type (Id) then
5153 Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
5154 else
5155 Derive_Subprograms (Generic_Parent_Type (N), Id, T);
5156 end if;
5157
5158 elsif Scope (Etype (Id)) /= Standard_Standard then
5159 Derive_Subprograms (Generic_Parent_Type (N), Id);
5160 end if;
5161 end if;
5162
5163 if Is_Private_Type (T) and then Present (Full_View (T)) then
5164 Conditional_Delay (Id, Full_View (T));
5165
5166 -- The subtypes of components or subcomponents of protected types
5167 -- do not need freeze nodes, which would otherwise appear in the
5168 -- wrong scope (before the freeze node for the protected type). The
5169 -- proper subtypes are those of the subcomponents of the corresponding
5170 -- record.
5171
5172 elsif Ekind (Scope (Id)) /= E_Protected_Type
5173 and then Present (Scope (Scope (Id))) -- error defense
5174 and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
5175 then
5176 Conditional_Delay (Id, T);
5177 end if;
5178
5179 -- Check that Constraint_Error is raised for a scalar subtype indication
5180 -- when the lower or upper bound of a non-null range lies outside the
5181 -- range of the type mark.
5182
5183 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5184 if Is_Scalar_Type (Etype (Id))
5185 and then Scalar_Range (Id) /=
5186 Scalar_Range (Etype (Subtype_Mark
5187 (Subtype_Indication (N))))
5188 then
5189 Apply_Range_Check
5190 (Scalar_Range (Id),
5191 Etype (Subtype_Mark (Subtype_Indication (N))));
5192
5193 -- In the array case, check compatibility for each index
5194
5195 elsif Is_Array_Type (Etype (Id)) and then Present (First_Index (Id))
5196 then
5197 -- This really should be a subprogram that finds the indications
5198 -- to check???
5199
5200 declare
5201 Subt_Index : Node_Id := First_Index (Id);
5202 Target_Index : Node_Id :=
5203 First_Index (Etype
5204 (Subtype_Mark (Subtype_Indication (N))));
5205 Has_Dyn_Chk : Boolean := Has_Dynamic_Range_Check (N);
5206
5207 begin
5208 while Present (Subt_Index) loop
5209 if ((Nkind (Subt_Index) = N_Identifier
5210 and then Ekind (Entity (Subt_Index)) in Scalar_Kind)
5211 or else Nkind (Subt_Index) = N_Subtype_Indication)
5212 and then
5213 Nkind (Scalar_Range (Etype (Subt_Index))) = N_Range
5214 then
5215 declare
5216 Target_Typ : constant Entity_Id :=
5217 Etype (Target_Index);
5218 begin
5219 R_Checks :=
5220 Get_Range_Checks
5221 (Scalar_Range (Etype (Subt_Index)),
5222 Target_Typ,
5223 Etype (Subt_Index),
5224 Defining_Identifier (N));
5225
5226 -- Reset Has_Dynamic_Range_Check on the subtype to
5227 -- prevent elision of the index check due to a dynamic
5228 -- check generated for a preceding index (needed since
5229 -- Insert_Range_Checks tries to avoid generating
5230 -- redundant checks on a given declaration).
5231
5232 Set_Has_Dynamic_Range_Check (N, False);
5233
5234 Insert_Range_Checks
5235 (R_Checks,
5236 N,
5237 Target_Typ,
5238 Sloc (Defining_Identifier (N)));
5239
5240 -- Record whether this index involved a dynamic check
5241
5242 Has_Dyn_Chk :=
5243 Has_Dyn_Chk or else Has_Dynamic_Range_Check (N);
5244 end;
5245 end if;
5246
5247 Next_Index (Subt_Index);
5248 Next_Index (Target_Index);
5249 end loop;
5250
5251 -- Finally, mark whether the subtype involves dynamic checks
5252
5253 Set_Has_Dynamic_Range_Check (N, Has_Dyn_Chk);
5254 end;
5255 end if;
5256 end if;
5257
5258 -- A type invariant applies to any subtype in its scope, in particular
5259 -- to a generic actual.
5260
5261 if Has_Invariants (T) and then In_Open_Scopes (Scope (T)) then
5262 Set_Has_Invariants (Id);
5263 Set_Invariant_Procedure (Id, Invariant_Procedure (T));
5264 end if;
5265
5266 -- Make sure that generic actual types are properly frozen. The subtype
5267 -- is marked as a generic actual type when the enclosing instance is
5268 -- analyzed, so here we identify the subtype from the tree structure.
5269
5270 if Expander_Active
5271 and then Is_Generic_Actual_Type (Id)
5272 and then In_Instance
5273 and then not Comes_From_Source (N)
5274 and then Nkind (Subtype_Indication (N)) /= N_Subtype_Indication
5275 and then Is_Frozen (T)
5276 then
5277 Freeze_Before (N, Id);
5278 end if;
5279
5280 Set_Optimize_Alignment_Flags (Id);
5281 Check_Eliminated (Id);
5282
5283 <<Leave>>
5284 if Has_Aspects (N) then
5285 Analyze_Aspect_Specifications (N, Id);
5286 end if;
5287
5288 Analyze_Dimension (N);
5289 end Analyze_Subtype_Declaration;
5290
5291 --------------------------------
5292 -- Analyze_Subtype_Indication --
5293 --------------------------------
5294
5295 procedure Analyze_Subtype_Indication (N : Node_Id) is
5296 T : constant Entity_Id := Subtype_Mark (N);
5297 R : constant Node_Id := Range_Expression (Constraint (N));
5298
5299 begin
5300 Analyze (T);
5301
5302 if R /= Error then
5303 Analyze (R);
5304 Set_Etype (N, Etype (R));
5305 Resolve (R, Entity (T));
5306 else
5307 Set_Error_Posted (R);
5308 Set_Error_Posted (T);
5309 end if;
5310 end Analyze_Subtype_Indication;
5311
5312 --------------------------
5313 -- Analyze_Variant_Part --
5314 --------------------------
5315
5316 procedure Analyze_Variant_Part (N : Node_Id) is
5317 Discr_Name : Node_Id;
5318 Discr_Type : Entity_Id;
5319
5320 procedure Process_Variant (A : Node_Id);
5321 -- Analyze declarations for a single variant
5322
5323 package Analyze_Variant_Choices is
5324 new Generic_Analyze_Choices (Process_Variant);
5325 use Analyze_Variant_Choices;
5326
5327 ---------------------
5328 -- Process_Variant --
5329 ---------------------
5330
5331 procedure Process_Variant (A : Node_Id) is
5332 CL : constant Node_Id := Component_List (A);
5333 begin
5334 if not Null_Present (CL) then
5335 Analyze_Declarations (Component_Items (CL));
5336
5337 if Present (Variant_Part (CL)) then
5338 Analyze (Variant_Part (CL));
5339 end if;
5340 end if;
5341 end Process_Variant;
5342
5343 -- Start of processing for Analyze_Variant_Part
5344
5345 begin
5346 Discr_Name := Name (N);
5347 Analyze (Discr_Name);
5348
5349 -- If Discr_Name bad, get out (prevent cascaded errors)
5350
5351 if Etype (Discr_Name) = Any_Type then
5352 return;
5353 end if;
5354
5355 -- Check invalid discriminant in variant part
5356
5357 if Ekind (Entity (Discr_Name)) /= E_Discriminant then
5358 Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
5359 end if;
5360
5361 Discr_Type := Etype (Entity (Discr_Name));
5362
5363 if not Is_Discrete_Type (Discr_Type) then
5364 Error_Msg_N
5365 ("discriminant in a variant part must be of a discrete type",
5366 Name (N));
5367 return;
5368 end if;
5369
5370 -- Now analyze the choices, which also analyzes the declarations that
5371 -- are associated with each choice.
5372
5373 Analyze_Choices (Variants (N), Discr_Type);
5374
5375 -- Note: we used to instantiate and call Check_Choices here to check
5376 -- that the choices covered the discriminant, but it's too early to do
5377 -- that because of statically predicated subtypes, whose analysis may
5378 -- be deferred to their freeze point which may be as late as the freeze
5379 -- point of the containing record. So this call is now to be found in
5380 -- Freeze_Record_Declaration.
5381
5382 end Analyze_Variant_Part;
5383
5384 ----------------------------
5385 -- Array_Type_Declaration --
5386 ----------------------------
5387
5388 procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
5389 Component_Def : constant Node_Id := Component_Definition (Def);
5390 Component_Typ : constant Node_Id := Subtype_Indication (Component_Def);
5391 Element_Type : Entity_Id;
5392 Implicit_Base : Entity_Id;
5393 Index : Node_Id;
5394 Related_Id : Entity_Id := Empty;
5395 Nb_Index : Nat;
5396 P : constant Node_Id := Parent (Def);
5397 Priv : Entity_Id;
5398
5399 begin
5400 if Nkind (Def) = N_Constrained_Array_Definition then
5401 Index := First (Discrete_Subtype_Definitions (Def));
5402 else
5403 Index := First (Subtype_Marks (Def));
5404 end if;
5405
5406 -- Find proper names for the implicit types which may be public. In case
5407 -- of anonymous arrays we use the name of the first object of that type
5408 -- as prefix.
5409
5410 if No (T) then
5411 Related_Id := Defining_Identifier (P);
5412 else
5413 Related_Id := T;
5414 end if;
5415
5416 Nb_Index := 1;
5417 while Present (Index) loop
5418 Analyze (Index);
5419
5420 -- Test for odd case of trying to index a type by the type itself
5421
5422 if Is_Entity_Name (Index) and then Entity (Index) = T then
5423 Error_Msg_N ("type& cannot be indexed by itself", Index);
5424 Set_Entity (Index, Standard_Boolean);
5425 Set_Etype (Index, Standard_Boolean);
5426 end if;
5427
5428 -- Check SPARK restriction requiring a subtype mark
5429
5430 if not Nkind_In (Index, N_Identifier, N_Expanded_Name) then
5431 Check_SPARK_05_Restriction ("subtype mark required", Index);
5432 end if;
5433
5434 -- Add a subtype declaration for each index of private array type
5435 -- declaration whose etype is also private. For example:
5436
5437 -- package Pkg is
5438 -- type Index is private;
5439 -- private
5440 -- type Table is array (Index) of ...
5441 -- end;
5442
5443 -- This is currently required by the expander for the internally
5444 -- generated equality subprogram of records with variant parts in
5445 -- which the etype of some component is such private type.
5446
5447 if Ekind (Current_Scope) = E_Package
5448 and then In_Private_Part (Current_Scope)
5449 and then Has_Private_Declaration (Etype (Index))
5450 then
5451 declare
5452 Loc : constant Source_Ptr := Sloc (Def);
5453 New_E : Entity_Id;
5454 Decl : Entity_Id;
5455
5456 begin
5457 New_E := Make_Temporary (Loc, 'T');
5458 Set_Is_Internal (New_E);
5459
5460 Decl :=
5461 Make_Subtype_Declaration (Loc,
5462 Defining_Identifier => New_E,
5463 Subtype_Indication =>
5464 New_Occurrence_Of (Etype (Index), Loc));
5465
5466 Insert_Before (Parent (Def), Decl);
5467 Analyze (Decl);
5468 Set_Etype (Index, New_E);
5469
5470 -- If the index is a range the Entity attribute is not
5471 -- available. Example:
5472
5473 -- package Pkg is
5474 -- type T is private;
5475 -- private
5476 -- type T is new Natural;
5477 -- Table : array (T(1) .. T(10)) of Boolean;
5478 -- end Pkg;
5479
5480 if Nkind (Index) /= N_Range then
5481 Set_Entity (Index, New_E);
5482 end if;
5483 end;
5484 end if;
5485
5486 Make_Index (Index, P, Related_Id, Nb_Index);
5487
5488 -- Check error of subtype with predicate for index type
5489
5490 Bad_Predicated_Subtype_Use
5491 ("subtype& has predicate, not allowed as index subtype",
5492 Index, Etype (Index));
5493
5494 -- Move to next index
5495
5496 Next_Index (Index);
5497 Nb_Index := Nb_Index + 1;
5498 end loop;
5499
5500 -- Process subtype indication if one is present
5501
5502 if Present (Component_Typ) then
5503 Element_Type := Process_Subtype (Component_Typ, P, Related_Id, 'C');
5504
5505 Set_Etype (Component_Typ, Element_Type);
5506
5507 if not Nkind_In (Component_Typ, N_Identifier, N_Expanded_Name) then
5508 Check_SPARK_05_Restriction
5509 ("subtype mark required", Component_Typ);
5510 end if;
5511
5512 -- Ada 2005 (AI-230): Access Definition case
5513
5514 else pragma Assert (Present (Access_Definition (Component_Def)));
5515
5516 -- Indicate that the anonymous access type is created by the
5517 -- array type declaration.
5518
5519 Element_Type := Access_Definition
5520 (Related_Nod => P,
5521 N => Access_Definition (Component_Def));
5522 Set_Is_Local_Anonymous_Access (Element_Type);
5523
5524 -- Propagate the parent. This field is needed if we have to generate
5525 -- the master_id associated with an anonymous access to task type
5526 -- component (see Expand_N_Full_Type_Declaration.Build_Master)
5527
5528 Set_Parent (Element_Type, Parent (T));
5529
5530 -- Ada 2005 (AI-230): In case of components that are anonymous access
5531 -- types the level of accessibility depends on the enclosing type
5532 -- declaration
5533
5534 Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
5535
5536 -- Ada 2005 (AI-254)
5537
5538 declare
5539 CD : constant Node_Id :=
5540 Access_To_Subprogram_Definition
5541 (Access_Definition (Component_Def));
5542 begin
5543 if Present (CD) and then Protected_Present (CD) then
5544 Element_Type :=
5545 Replace_Anonymous_Access_To_Protected_Subprogram (Def);
5546 end if;
5547 end;
5548 end if;
5549
5550 -- Constrained array case
5551
5552 if No (T) then
5553 T := Create_Itype (E_Void, P, Related_Id, 'T');
5554 end if;
5555
5556 if Nkind (Def) = N_Constrained_Array_Definition then
5557
5558 -- Establish Implicit_Base as unconstrained base type
5559
5560 Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
5561
5562 Set_Etype (Implicit_Base, Implicit_Base);
5563 Set_Scope (Implicit_Base, Current_Scope);
5564 Set_Has_Delayed_Freeze (Implicit_Base);
5565 Set_Default_SSO (Implicit_Base);
5566
5567 -- The constrained array type is a subtype of the unconstrained one
5568
5569 Set_Ekind (T, E_Array_Subtype);
5570 Init_Size_Align (T);
5571 Set_Etype (T, Implicit_Base);
5572 Set_Scope (T, Current_Scope);
5573 Set_Is_Constrained (T);
5574 Set_First_Index (T,
5575 First (Discrete_Subtype_Definitions (Def)));
5576 Set_Has_Delayed_Freeze (T);
5577
5578 -- Complete setup of implicit base type
5579
5580 Set_First_Index (Implicit_Base, First_Index (T));
5581 Set_Component_Type (Implicit_Base, Element_Type);
5582 Set_Has_Task (Implicit_Base, Has_Task (Element_Type));
5583 Set_Has_Protected (Implicit_Base, Has_Protected (Element_Type));
5584 Set_Component_Size (Implicit_Base, Uint_0);
5585 Set_Packed_Array_Impl_Type (Implicit_Base, Empty);
5586 Set_Has_Controlled_Component (Implicit_Base,
5587 Has_Controlled_Component (Element_Type)
5588 or else Is_Controlled (Element_Type));
5589 Set_Finalize_Storage_Only (Implicit_Base,
5590 Finalize_Storage_Only (Element_Type));
5591
5592 -- Inherit the "ghostness" from the constrained array type
5593
5594 if Is_Ghost_Entity (T) or else Ghost_Mode > None then
5595 Set_Is_Ghost_Entity (Implicit_Base);
5596 end if;
5597
5598 -- Unconstrained array case
5599
5600 else
5601 Set_Ekind (T, E_Array_Type);
5602 Init_Size_Align (T);
5603 Set_Etype (T, T);
5604 Set_Scope (T, Current_Scope);
5605 Set_Component_Size (T, Uint_0);
5606 Set_Is_Constrained (T, False);
5607 Set_First_Index (T, First (Subtype_Marks (Def)));
5608 Set_Has_Delayed_Freeze (T, True);
5609 Set_Has_Task (T, Has_Task (Element_Type));
5610 Set_Has_Protected (T, Has_Protected (Element_Type));
5611 Set_Has_Controlled_Component (T, Has_Controlled_Component
5612 (Element_Type)
5613 or else
5614 Is_Controlled (Element_Type));
5615 Set_Finalize_Storage_Only (T, Finalize_Storage_Only
5616 (Element_Type));
5617 Set_Default_SSO (T);
5618 end if;
5619
5620 -- Common attributes for both cases
5621
5622 Set_Component_Type (Base_Type (T), Element_Type);
5623 Set_Packed_Array_Impl_Type (T, Empty);
5624
5625 if Aliased_Present (Component_Definition (Def)) then
5626 Check_SPARK_05_Restriction
5627 ("aliased is not allowed", Component_Definition (Def));
5628 Set_Has_Aliased_Components (Etype (T));
5629 end if;
5630
5631 -- Ada 2005 (AI-231): Propagate the null-excluding attribute to the
5632 -- array type to ensure that objects of this type are initialized.
5633
5634 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (Element_Type) then
5635 Set_Can_Never_Be_Null (T);
5636
5637 if Null_Exclusion_Present (Component_Definition (Def))
5638
5639 -- No need to check itypes because in their case this check was
5640 -- done at their point of creation
5641
5642 and then not Is_Itype (Element_Type)
5643 then
5644 Error_Msg_N
5645 ("`NOT NULL` not allowed (null already excluded)",
5646 Subtype_Indication (Component_Definition (Def)));
5647 end if;
5648 end if;
5649
5650 Priv := Private_Component (Element_Type);
5651
5652 if Present (Priv) then
5653
5654 -- Check for circular definitions
5655
5656 if Priv = Any_Type then
5657 Set_Component_Type (Etype (T), Any_Type);
5658
5659 -- There is a gap in the visibility of operations on the composite
5660 -- type only if the component type is defined in a different scope.
5661
5662 elsif Scope (Priv) = Current_Scope then
5663 null;
5664
5665 elsif Is_Limited_Type (Priv) then
5666 Set_Is_Limited_Composite (Etype (T));
5667 Set_Is_Limited_Composite (T);
5668 else
5669 Set_Is_Private_Composite (Etype (T));
5670 Set_Is_Private_Composite (T);
5671 end if;
5672 end if;
5673
5674 -- A syntax error in the declaration itself may lead to an empty index
5675 -- list, in which case do a minimal patch.
5676
5677 if No (First_Index (T)) then
5678 Error_Msg_N ("missing index definition in array type declaration", T);
5679
5680 declare
5681 Indexes : constant List_Id :=
5682 New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
5683 begin
5684 Set_Discrete_Subtype_Definitions (Def, Indexes);
5685 Set_First_Index (T, First (Indexes));
5686 return;
5687 end;
5688 end if;
5689
5690 -- Create a concatenation operator for the new type. Internal array
5691 -- types created for packed entities do not need such, they are
5692 -- compatible with the user-defined type.
5693
5694 if Number_Dimensions (T) = 1
5695 and then not Is_Packed_Array_Impl_Type (T)
5696 then
5697 New_Concatenation_Op (T);
5698 end if;
5699
5700 -- In the case of an unconstrained array the parser has already verified
5701 -- that all the indexes are unconstrained but we still need to make sure
5702 -- that the element type is constrained.
5703
5704 if Is_Indefinite_Subtype (Element_Type) then
5705 Error_Msg_N
5706 ("unconstrained element type in array declaration",
5707 Subtype_Indication (Component_Def));
5708
5709 elsif Is_Abstract_Type (Element_Type) then
5710 Error_Msg_N
5711 ("the type of a component cannot be abstract",
5712 Subtype_Indication (Component_Def));
5713 end if;
5714
5715 -- There may be an invariant declared for the component type, but
5716 -- the construction of the component invariant checking procedure
5717 -- takes place during expansion.
5718 end Array_Type_Declaration;
5719
5720 ------------------------------------------------------
5721 -- Replace_Anonymous_Access_To_Protected_Subprogram --
5722 ------------------------------------------------------
5723
5724 function Replace_Anonymous_Access_To_Protected_Subprogram
5725 (N : Node_Id) return Entity_Id
5726 is
5727 Loc : constant Source_Ptr := Sloc (N);
5728
5729 Curr_Scope : constant Scope_Stack_Entry :=
5730 Scope_Stack.Table (Scope_Stack.Last);
5731
5732 Anon : constant Entity_Id := Make_Temporary (Loc, 'S');
5733
5734 Acc : Node_Id;
5735 -- Access definition in declaration
5736
5737 Comp : Node_Id;
5738 -- Object definition or formal definition with an access definition
5739
5740 Decl : Node_Id;
5741 -- Declaration of anonymous access to subprogram type
5742
5743 Spec : Node_Id;
5744 -- Original specification in access to subprogram
5745
5746 P : Node_Id;
5747
5748 begin
5749 Set_Is_Internal (Anon);
5750
5751 case Nkind (N) is
5752 when N_Component_Declaration |
5753 N_Unconstrained_Array_Definition |
5754 N_Constrained_Array_Definition =>
5755 Comp := Component_Definition (N);
5756 Acc := Access_Definition (Comp);
5757
5758 when N_Discriminant_Specification =>
5759 Comp := Discriminant_Type (N);
5760 Acc := Comp;
5761
5762 when N_Parameter_Specification =>
5763 Comp := Parameter_Type (N);
5764 Acc := Comp;
5765
5766 when N_Access_Function_Definition =>
5767 Comp := Result_Definition (N);
5768 Acc := Comp;
5769
5770 when N_Object_Declaration =>
5771 Comp := Object_Definition (N);
5772 Acc := Comp;
5773
5774 when N_Function_Specification =>
5775 Comp := Result_Definition (N);
5776 Acc := Comp;
5777
5778 when others =>
5779 raise Program_Error;
5780 end case;
5781
5782 Spec := Access_To_Subprogram_Definition (Acc);
5783
5784 Decl :=
5785 Make_Full_Type_Declaration (Loc,
5786 Defining_Identifier => Anon,
5787 Type_Definition => Copy_Separate_Tree (Spec));
5788
5789 Mark_Rewrite_Insertion (Decl);
5790
5791 -- In ASIS mode, analyze the profile on the original node, because
5792 -- the separate copy does not provide enough links to recover the
5793 -- original tree. Analysis is limited to type annotations, within
5794 -- a temporary scope that serves as an anonymous subprogram to collect
5795 -- otherwise useless temporaries and itypes.
5796
5797 if ASIS_Mode then
5798 declare
5799 Typ : constant Entity_Id := Make_Temporary (Loc, 'S');
5800
5801 begin
5802 if Nkind (Spec) = N_Access_Function_Definition then
5803 Set_Ekind (Typ, E_Function);
5804 else
5805 Set_Ekind (Typ, E_Procedure);
5806 end if;
5807
5808 Set_Parent (Typ, N);
5809 Set_Scope (Typ, Current_Scope);
5810 Push_Scope (Typ);
5811
5812 Process_Formals (Parameter_Specifications (Spec), Spec);
5813
5814 if Nkind (Spec) = N_Access_Function_Definition then
5815 declare
5816 Def : constant Node_Id := Result_Definition (Spec);
5817
5818 begin
5819 -- The result might itself be an anonymous access type, so
5820 -- have to recurse.
5821
5822 if Nkind (Def) = N_Access_Definition then
5823 if Present (Access_To_Subprogram_Definition (Def)) then
5824 Set_Etype
5825 (Def,
5826 Replace_Anonymous_Access_To_Protected_Subprogram
5827 (Spec));
5828 else
5829 Find_Type (Subtype_Mark (Def));
5830 end if;
5831
5832 else
5833 Find_Type (Def);
5834 end if;
5835 end;
5836 end if;
5837
5838 End_Scope;
5839 end;
5840 end if;
5841
5842 -- Insert the new declaration in the nearest enclosing scope. If the
5843 -- node is a body and N is its return type, the declaration belongs in
5844 -- the enclosing scope.
5845
5846 P := Parent (N);
5847
5848 if Nkind (P) = N_Subprogram_Body
5849 and then Nkind (N) = N_Function_Specification
5850 then
5851 P := Parent (P);
5852 end if;
5853
5854 while Present (P) and then not Has_Declarations (P) loop
5855 P := Parent (P);
5856 end loop;
5857
5858 pragma Assert (Present (P));
5859
5860 if Nkind (P) = N_Package_Specification then
5861 Prepend (Decl, Visible_Declarations (P));
5862 else
5863 Prepend (Decl, Declarations (P));
5864 end if;
5865
5866 -- Replace the anonymous type with an occurrence of the new declaration.
5867 -- In all cases the rewritten node does not have the null-exclusion
5868 -- attribute because (if present) it was already inherited by the
5869 -- anonymous entity (Anon). Thus, in case of components we do not
5870 -- inherit this attribute.
5871
5872 if Nkind (N) = N_Parameter_Specification then
5873 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5874 Set_Etype (Defining_Identifier (N), Anon);
5875 Set_Null_Exclusion_Present (N, False);
5876
5877 elsif Nkind (N) = N_Object_Declaration then
5878 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5879 Set_Etype (Defining_Identifier (N), Anon);
5880
5881 elsif Nkind (N) = N_Access_Function_Definition then
5882 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5883
5884 elsif Nkind (N) = N_Function_Specification then
5885 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5886 Set_Etype (Defining_Unit_Name (N), Anon);
5887
5888 else
5889 Rewrite (Comp,
5890 Make_Component_Definition (Loc,
5891 Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
5892 end if;
5893
5894 Mark_Rewrite_Insertion (Comp);
5895
5896 if Nkind_In (N, N_Object_Declaration, N_Access_Function_Definition) then
5897 Analyze (Decl);
5898
5899 else
5900 -- Temporarily remove the current scope (record or subprogram) from
5901 -- the stack to add the new declarations to the enclosing scope.
5902
5903 Scope_Stack.Decrement_Last;
5904 Analyze (Decl);
5905 Set_Is_Itype (Anon);
5906 Scope_Stack.Append (Curr_Scope);
5907 end if;
5908
5909 Set_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
5910 Set_Can_Use_Internal_Rep (Anon, not Always_Compatible_Rep_On_Target);
5911 return Anon;
5912 end Replace_Anonymous_Access_To_Protected_Subprogram;
5913
5914 -------------------------------
5915 -- Build_Derived_Access_Type --
5916 -------------------------------
5917
5918 procedure Build_Derived_Access_Type
5919 (N : Node_Id;
5920 Parent_Type : Entity_Id;
5921 Derived_Type : Entity_Id)
5922 is
5923 S : constant Node_Id := Subtype_Indication (Type_Definition (N));
5924
5925 Desig_Type : Entity_Id;
5926 Discr : Entity_Id;
5927 Discr_Con_Elist : Elist_Id;
5928 Discr_Con_El : Elmt_Id;
5929 Subt : Entity_Id;
5930
5931 begin
5932 -- Set the designated type so it is available in case this is an access
5933 -- to a self-referential type, e.g. a standard list type with a next
5934 -- pointer. Will be reset after subtype is built.
5935
5936 Set_Directly_Designated_Type
5937 (Derived_Type, Designated_Type (Parent_Type));
5938
5939 Subt := Process_Subtype (S, N);
5940
5941 if Nkind (S) /= N_Subtype_Indication
5942 and then Subt /= Base_Type (Subt)
5943 then
5944 Set_Ekind (Derived_Type, E_Access_Subtype);
5945 end if;
5946
5947 if Ekind (Derived_Type) = E_Access_Subtype then
5948 declare
5949 Pbase : constant Entity_Id := Base_Type (Parent_Type);
5950 Ibase : constant Entity_Id :=
5951 Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
5952 Svg_Chars : constant Name_Id := Chars (Ibase);
5953 Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
5954
5955 begin
5956 Copy_Node (Pbase, Ibase);
5957
5958 Set_Chars (Ibase, Svg_Chars);
5959 Set_Next_Entity (Ibase, Svg_Next_E);
5960 Set_Sloc (Ibase, Sloc (Derived_Type));
5961 Set_Scope (Ibase, Scope (Derived_Type));
5962 Set_Freeze_Node (Ibase, Empty);
5963 Set_Is_Frozen (Ibase, False);
5964 Set_Comes_From_Source (Ibase, False);
5965 Set_Is_First_Subtype (Ibase, False);
5966
5967 Set_Etype (Ibase, Pbase);
5968 Set_Etype (Derived_Type, Ibase);
5969 end;
5970 end if;
5971
5972 Set_Directly_Designated_Type
5973 (Derived_Type, Designated_Type (Subt));
5974
5975 Set_Is_Constrained (Derived_Type, Is_Constrained (Subt));
5976 Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
5977 Set_Size_Info (Derived_Type, Parent_Type);
5978 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
5979 Set_Depends_On_Private (Derived_Type,
5980 Has_Private_Component (Derived_Type));
5981 Conditional_Delay (Derived_Type, Subt);
5982
5983 -- Ada 2005 (AI-231): Set the null-exclusion attribute, and verify
5984 -- that it is not redundant.
5985
5986 if Null_Exclusion_Present (Type_Definition (N)) then
5987 Set_Can_Never_Be_Null (Derived_Type);
5988
5989 -- What is with the "AND THEN FALSE" here ???
5990
5991 if Can_Never_Be_Null (Parent_Type)
5992 and then False
5993 then
5994 Error_Msg_NE
5995 ("`NOT NULL` not allowed (& already excludes null)",
5996 N, Parent_Type);
5997 end if;
5998
5999 elsif Can_Never_Be_Null (Parent_Type) then
6000 Set_Can_Never_Be_Null (Derived_Type);
6001 end if;
6002
6003 -- Note: we do not copy the Storage_Size_Variable, since we always go to
6004 -- the root type for this information.
6005
6006 -- Apply range checks to discriminants for derived record case
6007 -- ??? THIS CODE SHOULD NOT BE HERE REALLY.
6008
6009 Desig_Type := Designated_Type (Derived_Type);
6010 if Is_Composite_Type (Desig_Type)
6011 and then (not Is_Array_Type (Desig_Type))
6012 and then Has_Discriminants (Desig_Type)
6013 and then Base_Type (Desig_Type) /= Desig_Type
6014 then
6015 Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
6016 Discr_Con_El := First_Elmt (Discr_Con_Elist);
6017
6018 Discr := First_Discriminant (Base_Type (Desig_Type));
6019 while Present (Discr_Con_El) loop
6020 Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
6021 Next_Elmt (Discr_Con_El);
6022 Next_Discriminant (Discr);
6023 end loop;
6024 end if;
6025 end Build_Derived_Access_Type;
6026
6027 ------------------------------
6028 -- Build_Derived_Array_Type --
6029 ------------------------------
6030
6031 procedure Build_Derived_Array_Type
6032 (N : Node_Id;
6033 Parent_Type : Entity_Id;
6034 Derived_Type : Entity_Id)
6035 is
6036 Loc : constant Source_Ptr := Sloc (N);
6037 Tdef : constant Node_Id := Type_Definition (N);
6038 Indic : constant Node_Id := Subtype_Indication (Tdef);
6039 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
6040 Implicit_Base : Entity_Id;
6041 New_Indic : Node_Id;
6042
6043 procedure Make_Implicit_Base;
6044 -- If the parent subtype is constrained, the derived type is a subtype
6045 -- of an implicit base type derived from the parent base.
6046
6047 ------------------------
6048 -- Make_Implicit_Base --
6049 ------------------------
6050
6051 procedure Make_Implicit_Base is
6052 begin
6053 Implicit_Base :=
6054 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
6055
6056 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
6057 Set_Etype (Implicit_Base, Parent_Base);
6058
6059 Copy_Array_Subtype_Attributes (Implicit_Base, Parent_Base);
6060 Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
6061
6062 Set_Has_Delayed_Freeze (Implicit_Base, True);
6063
6064 -- Inherit the "ghostness" from the parent base type
6065
6066 if Is_Ghost_Entity (Parent_Base) or else Ghost_Mode > None then
6067 Set_Is_Ghost_Entity (Implicit_Base);
6068 end if;
6069 end Make_Implicit_Base;
6070
6071 -- Start of processing for Build_Derived_Array_Type
6072
6073 begin
6074 if not Is_Constrained (Parent_Type) then
6075 if Nkind (Indic) /= N_Subtype_Indication then
6076 Set_Ekind (Derived_Type, E_Array_Type);
6077
6078 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6079 Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
6080
6081 Set_Has_Delayed_Freeze (Derived_Type, True);
6082
6083 else
6084 Make_Implicit_Base;
6085 Set_Etype (Derived_Type, Implicit_Base);
6086
6087 New_Indic :=
6088 Make_Subtype_Declaration (Loc,
6089 Defining_Identifier => Derived_Type,
6090 Subtype_Indication =>
6091 Make_Subtype_Indication (Loc,
6092 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
6093 Constraint => Constraint (Indic)));
6094
6095 Rewrite (N, New_Indic);
6096 Analyze (N);
6097 end if;
6098
6099 else
6100 if Nkind (Indic) /= N_Subtype_Indication then
6101 Make_Implicit_Base;
6102
6103 Set_Ekind (Derived_Type, Ekind (Parent_Type));
6104 Set_Etype (Derived_Type, Implicit_Base);
6105 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6106
6107 else
6108 Error_Msg_N ("illegal constraint on constrained type", Indic);
6109 end if;
6110 end if;
6111
6112 -- If parent type is not a derived type itself, and is declared in
6113 -- closed scope (e.g. a subprogram), then we must explicitly introduce
6114 -- the new type's concatenation operator since Derive_Subprograms
6115 -- will not inherit the parent's operator. If the parent type is
6116 -- unconstrained, the operator is of the unconstrained base type.
6117
6118 if Number_Dimensions (Parent_Type) = 1
6119 and then not Is_Limited_Type (Parent_Type)
6120 and then not Is_Derived_Type (Parent_Type)
6121 and then not Is_Package_Or_Generic_Package
6122 (Scope (Base_Type (Parent_Type)))
6123 then
6124 if not Is_Constrained (Parent_Type)
6125 and then Is_Constrained (Derived_Type)
6126 then
6127 New_Concatenation_Op (Implicit_Base);
6128 else
6129 New_Concatenation_Op (Derived_Type);
6130 end if;
6131 end if;
6132 end Build_Derived_Array_Type;
6133
6134 -----------------------------------
6135 -- Build_Derived_Concurrent_Type --
6136 -----------------------------------
6137
6138 procedure Build_Derived_Concurrent_Type
6139 (N : Node_Id;
6140 Parent_Type : Entity_Id;
6141 Derived_Type : Entity_Id)
6142 is
6143 Loc : constant Source_Ptr := Sloc (N);
6144
6145 Corr_Record : constant Entity_Id := Make_Temporary (Loc, 'C');
6146 Corr_Decl : Node_Id;
6147 Corr_Decl_Needed : Boolean;
6148 -- If the derived type has fewer discriminants than its parent, the
6149 -- corresponding record is also a derived type, in order to account for
6150 -- the bound discriminants. We create a full type declaration for it in
6151 -- this case.
6152
6153 Constraint_Present : constant Boolean :=
6154 Nkind (Subtype_Indication (Type_Definition (N))) =
6155 N_Subtype_Indication;
6156
6157 D_Constraint : Node_Id;
6158 New_Constraint : Elist_Id;
6159 Old_Disc : Entity_Id;
6160 New_Disc : Entity_Id;
6161 New_N : Node_Id;
6162
6163 begin
6164 Set_Stored_Constraint (Derived_Type, No_Elist);
6165 Corr_Decl_Needed := False;
6166 Old_Disc := Empty;
6167
6168 if Present (Discriminant_Specifications (N))
6169 and then Constraint_Present
6170 then
6171 Old_Disc := First_Discriminant (Parent_Type);
6172 New_Disc := First (Discriminant_Specifications (N));
6173 while Present (New_Disc) and then Present (Old_Disc) loop
6174 Next_Discriminant (Old_Disc);
6175 Next (New_Disc);
6176 end loop;
6177 end if;
6178
6179 if Present (Old_Disc) and then Expander_Active then
6180
6181 -- The new type has fewer discriminants, so we need to create a new
6182 -- corresponding record, which is derived from the corresponding
6183 -- record of the parent, and has a stored constraint that captures
6184 -- the values of the discriminant constraints. The corresponding
6185 -- record is needed only if expander is active and code generation is
6186 -- enabled.
6187
6188 -- The type declaration for the derived corresponding record has the
6189 -- same discriminant part and constraints as the current declaration.
6190 -- Copy the unanalyzed tree to build declaration.
6191
6192 Corr_Decl_Needed := True;
6193 New_N := Copy_Separate_Tree (N);
6194
6195 Corr_Decl :=
6196 Make_Full_Type_Declaration (Loc,
6197 Defining_Identifier => Corr_Record,
6198 Discriminant_Specifications =>
6199 Discriminant_Specifications (New_N),
6200 Type_Definition =>
6201 Make_Derived_Type_Definition (Loc,
6202 Subtype_Indication =>
6203 Make_Subtype_Indication (Loc,
6204 Subtype_Mark =>
6205 New_Occurrence_Of
6206 (Corresponding_Record_Type (Parent_Type), Loc),
6207 Constraint =>
6208 Constraint
6209 (Subtype_Indication (Type_Definition (New_N))))));
6210 end if;
6211
6212 -- Copy Storage_Size and Relative_Deadline variables if task case
6213
6214 if Is_Task_Type (Parent_Type) then
6215 Set_Storage_Size_Variable (Derived_Type,
6216 Storage_Size_Variable (Parent_Type));
6217 Set_Relative_Deadline_Variable (Derived_Type,
6218 Relative_Deadline_Variable (Parent_Type));
6219 end if;
6220
6221 if Present (Discriminant_Specifications (N)) then
6222 Push_Scope (Derived_Type);
6223 Check_Or_Process_Discriminants (N, Derived_Type);
6224
6225 if Constraint_Present then
6226 New_Constraint :=
6227 Expand_To_Stored_Constraint
6228 (Parent_Type,
6229 Build_Discriminant_Constraints
6230 (Parent_Type,
6231 Subtype_Indication (Type_Definition (N)), True));
6232 end if;
6233
6234 End_Scope;
6235
6236 elsif Constraint_Present then
6237
6238 -- Build constrained subtype, copying the constraint, and derive
6239 -- from it to create a derived constrained type.
6240
6241 declare
6242 Loc : constant Source_Ptr := Sloc (N);
6243 Anon : constant Entity_Id :=
6244 Make_Defining_Identifier (Loc,
6245 Chars => New_External_Name (Chars (Derived_Type), 'T'));
6246 Decl : Node_Id;
6247
6248 begin
6249 Decl :=
6250 Make_Subtype_Declaration (Loc,
6251 Defining_Identifier => Anon,
6252 Subtype_Indication =>
6253 New_Copy_Tree (Subtype_Indication (Type_Definition (N))));
6254 Insert_Before (N, Decl);
6255 Analyze (Decl);
6256
6257 Rewrite (Subtype_Indication (Type_Definition (N)),
6258 New_Occurrence_Of (Anon, Loc));
6259 Set_Analyzed (Derived_Type, False);
6260 Analyze (N);
6261 return;
6262 end;
6263 end if;
6264
6265 -- By default, operations and private data are inherited from parent.
6266 -- However, in the presence of bound discriminants, a new corresponding
6267 -- record will be created, see below.
6268
6269 Set_Has_Discriminants
6270 (Derived_Type, Has_Discriminants (Parent_Type));
6271 Set_Corresponding_Record_Type
6272 (Derived_Type, Corresponding_Record_Type (Parent_Type));
6273
6274 -- Is_Constrained is set according the parent subtype, but is set to
6275 -- False if the derived type is declared with new discriminants.
6276
6277 Set_Is_Constrained
6278 (Derived_Type,
6279 (Is_Constrained (Parent_Type) or else Constraint_Present)
6280 and then not Present (Discriminant_Specifications (N)));
6281
6282 if Constraint_Present then
6283 if not Has_Discriminants (Parent_Type) then
6284 Error_Msg_N ("untagged parent must have discriminants", N);
6285
6286 elsif Present (Discriminant_Specifications (N)) then
6287
6288 -- Verify that new discriminants are used to constrain old ones
6289
6290 D_Constraint :=
6291 First
6292 (Constraints
6293 (Constraint (Subtype_Indication (Type_Definition (N)))));
6294
6295 Old_Disc := First_Discriminant (Parent_Type);
6296
6297 while Present (D_Constraint) loop
6298 if Nkind (D_Constraint) /= N_Discriminant_Association then
6299
6300 -- Positional constraint. If it is a reference to a new
6301 -- discriminant, it constrains the corresponding old one.
6302
6303 if Nkind (D_Constraint) = N_Identifier then
6304 New_Disc := First_Discriminant (Derived_Type);
6305 while Present (New_Disc) loop
6306 exit when Chars (New_Disc) = Chars (D_Constraint);
6307 Next_Discriminant (New_Disc);
6308 end loop;
6309
6310 if Present (New_Disc) then
6311 Set_Corresponding_Discriminant (New_Disc, Old_Disc);
6312 end if;
6313 end if;
6314
6315 Next_Discriminant (Old_Disc);
6316
6317 -- if this is a named constraint, search by name for the old
6318 -- discriminants constrained by the new one.
6319
6320 elsif Nkind (Expression (D_Constraint)) = N_Identifier then
6321
6322 -- Find new discriminant with that name
6323
6324 New_Disc := First_Discriminant (Derived_Type);
6325 while Present (New_Disc) loop
6326 exit when
6327 Chars (New_Disc) = Chars (Expression (D_Constraint));
6328 Next_Discriminant (New_Disc);
6329 end loop;
6330
6331 if Present (New_Disc) then
6332
6333 -- Verify that new discriminant renames some discriminant
6334 -- of the parent type, and associate the new discriminant
6335 -- with one or more old ones that it renames.
6336
6337 declare
6338 Selector : Node_Id;
6339
6340 begin
6341 Selector := First (Selector_Names (D_Constraint));
6342 while Present (Selector) loop
6343 Old_Disc := First_Discriminant (Parent_Type);
6344 while Present (Old_Disc) loop
6345 exit when Chars (Old_Disc) = Chars (Selector);
6346 Next_Discriminant (Old_Disc);
6347 end loop;
6348
6349 if Present (Old_Disc) then
6350 Set_Corresponding_Discriminant
6351 (New_Disc, Old_Disc);
6352 end if;
6353
6354 Next (Selector);
6355 end loop;
6356 end;
6357 end if;
6358 end if;
6359
6360 Next (D_Constraint);
6361 end loop;
6362
6363 New_Disc := First_Discriminant (Derived_Type);
6364 while Present (New_Disc) loop
6365 if No (Corresponding_Discriminant (New_Disc)) then
6366 Error_Msg_NE
6367 ("new discriminant& must constrain old one", N, New_Disc);
6368
6369 elsif not
6370 Subtypes_Statically_Compatible
6371 (Etype (New_Disc),
6372 Etype (Corresponding_Discriminant (New_Disc)))
6373 then
6374 Error_Msg_NE
6375 ("& not statically compatible with parent discriminant",
6376 N, New_Disc);
6377 end if;
6378
6379 Next_Discriminant (New_Disc);
6380 end loop;
6381 end if;
6382
6383 elsif Present (Discriminant_Specifications (N)) then
6384 Error_Msg_N
6385 ("missing discriminant constraint in untagged derivation", N);
6386 end if;
6387
6388 -- The entity chain of the derived type includes the new discriminants
6389 -- but shares operations with the parent.
6390
6391 if Present (Discriminant_Specifications (N)) then
6392 Old_Disc := First_Discriminant (Parent_Type);
6393 while Present (Old_Disc) loop
6394 if No (Next_Entity (Old_Disc))
6395 or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
6396 then
6397 Set_Next_Entity
6398 (Last_Entity (Derived_Type), Next_Entity (Old_Disc));
6399 exit;
6400 end if;
6401
6402 Next_Discriminant (Old_Disc);
6403 end loop;
6404
6405 else
6406 Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
6407 if Has_Discriminants (Parent_Type) then
6408 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
6409 Set_Discriminant_Constraint (
6410 Derived_Type, Discriminant_Constraint (Parent_Type));
6411 end if;
6412 end if;
6413
6414 Set_Last_Entity (Derived_Type, Last_Entity (Parent_Type));
6415
6416 Set_Has_Completion (Derived_Type);
6417
6418 if Corr_Decl_Needed then
6419 Set_Stored_Constraint (Derived_Type, New_Constraint);
6420 Insert_After (N, Corr_Decl);
6421 Analyze (Corr_Decl);
6422 Set_Corresponding_Record_Type (Derived_Type, Corr_Record);
6423 end if;
6424 end Build_Derived_Concurrent_Type;
6425
6426 ------------------------------------
6427 -- Build_Derived_Enumeration_Type --
6428 ------------------------------------
6429
6430 procedure Build_Derived_Enumeration_Type
6431 (N : Node_Id;
6432 Parent_Type : Entity_Id;
6433 Derived_Type : Entity_Id)
6434 is
6435 Loc : constant Source_Ptr := Sloc (N);
6436 Def : constant Node_Id := Type_Definition (N);
6437 Indic : constant Node_Id := Subtype_Indication (Def);
6438 Implicit_Base : Entity_Id;
6439 Literal : Entity_Id;
6440 New_Lit : Entity_Id;
6441 Literals_List : List_Id;
6442 Type_Decl : Node_Id;
6443 Hi, Lo : Node_Id;
6444 Rang_Expr : Node_Id;
6445
6446 begin
6447 -- Since types Standard.Character and Standard.[Wide_]Wide_Character do
6448 -- not have explicit literals lists we need to process types derived
6449 -- from them specially. This is handled by Derived_Standard_Character.
6450 -- If the parent type is a generic type, there are no literals either,
6451 -- and we construct the same skeletal representation as for the generic
6452 -- parent type.
6453
6454 if Is_Standard_Character_Type (Parent_Type) then
6455 Derived_Standard_Character (N, Parent_Type, Derived_Type);
6456
6457 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
6458 declare
6459 Lo : Node_Id;
6460 Hi : Node_Id;
6461
6462 begin
6463 if Nkind (Indic) /= N_Subtype_Indication then
6464 Lo :=
6465 Make_Attribute_Reference (Loc,
6466 Attribute_Name => Name_First,
6467 Prefix => New_Occurrence_Of (Derived_Type, Loc));
6468 Set_Etype (Lo, Derived_Type);
6469
6470 Hi :=
6471 Make_Attribute_Reference (Loc,
6472 Attribute_Name => Name_Last,
6473 Prefix => New_Occurrence_Of (Derived_Type, Loc));
6474 Set_Etype (Hi, Derived_Type);
6475
6476 Set_Scalar_Range (Derived_Type,
6477 Make_Range (Loc,
6478 Low_Bound => Lo,
6479 High_Bound => Hi));
6480 else
6481
6482 -- Analyze subtype indication and verify compatibility
6483 -- with parent type.
6484
6485 if Base_Type (Process_Subtype (Indic, N)) /=
6486 Base_Type (Parent_Type)
6487 then
6488 Error_Msg_N
6489 ("illegal constraint for formal discrete type", N);
6490 end if;
6491 end if;
6492 end;
6493
6494 else
6495 -- If a constraint is present, analyze the bounds to catch
6496 -- premature usage of the derived literals.
6497
6498 if Nkind (Indic) = N_Subtype_Indication
6499 and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
6500 then
6501 Analyze (Low_Bound (Range_Expression (Constraint (Indic))));
6502 Analyze (High_Bound (Range_Expression (Constraint (Indic))));
6503 end if;
6504
6505 -- Introduce an implicit base type for the derived type even if there
6506 -- is no constraint attached to it, since this seems closer to the
6507 -- Ada semantics. Build a full type declaration tree for the derived
6508 -- type using the implicit base type as the defining identifier. The
6509 -- build a subtype declaration tree which applies the constraint (if
6510 -- any) have it replace the derived type declaration.
6511
6512 Literal := First_Literal (Parent_Type);
6513 Literals_List := New_List;
6514 while Present (Literal)
6515 and then Ekind (Literal) = E_Enumeration_Literal
6516 loop
6517 -- Literals of the derived type have the same representation as
6518 -- those of the parent type, but this representation can be
6519 -- overridden by an explicit representation clause. Indicate
6520 -- that there is no explicit representation given yet. These
6521 -- derived literals are implicit operations of the new type,
6522 -- and can be overridden by explicit ones.
6523
6524 if Nkind (Literal) = N_Defining_Character_Literal then
6525 New_Lit :=
6526 Make_Defining_Character_Literal (Loc, Chars (Literal));
6527 else
6528 New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
6529 end if;
6530
6531 Set_Ekind (New_Lit, E_Enumeration_Literal);
6532 Set_Enumeration_Pos (New_Lit, Enumeration_Pos (Literal));
6533 Set_Enumeration_Rep (New_Lit, Enumeration_Rep (Literal));
6534 Set_Enumeration_Rep_Expr (New_Lit, Empty);
6535 Set_Alias (New_Lit, Literal);
6536 Set_Is_Known_Valid (New_Lit, True);
6537
6538 Append (New_Lit, Literals_List);
6539 Next_Literal (Literal);
6540 end loop;
6541
6542 Implicit_Base :=
6543 Make_Defining_Identifier (Sloc (Derived_Type),
6544 Chars => New_External_Name (Chars (Derived_Type), 'B'));
6545
6546 -- Indicate the proper nature of the derived type. This must be done
6547 -- before analysis of the literals, to recognize cases when a literal
6548 -- may be hidden by a previous explicit function definition (cf.
6549 -- c83031a).
6550
6551 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
6552 Set_Etype (Derived_Type, Implicit_Base);
6553
6554 Type_Decl :=
6555 Make_Full_Type_Declaration (Loc,
6556 Defining_Identifier => Implicit_Base,
6557 Discriminant_Specifications => No_List,
6558 Type_Definition =>
6559 Make_Enumeration_Type_Definition (Loc, Literals_List));
6560
6561 Mark_Rewrite_Insertion (Type_Decl);
6562 Insert_Before (N, Type_Decl);
6563 Analyze (Type_Decl);
6564
6565 -- The anonymous base now has a full declaration, but this base
6566 -- is not a first subtype.
6567
6568 Set_Is_First_Subtype (Implicit_Base, False);
6569
6570 -- After the implicit base is analyzed its Etype needs to be changed
6571 -- to reflect the fact that it is derived from the parent type which
6572 -- was ignored during analysis. We also set the size at this point.
6573
6574 Set_Etype (Implicit_Base, Parent_Type);
6575
6576 Set_Size_Info (Implicit_Base, Parent_Type);
6577 Set_RM_Size (Implicit_Base, RM_Size (Parent_Type));
6578 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
6579
6580 -- Copy other flags from parent type
6581
6582 Set_Has_Non_Standard_Rep
6583 (Implicit_Base, Has_Non_Standard_Rep
6584 (Parent_Type));
6585 Set_Has_Pragma_Ordered
6586 (Implicit_Base, Has_Pragma_Ordered
6587 (Parent_Type));
6588 Set_Has_Delayed_Freeze (Implicit_Base);
6589
6590 -- Process the subtype indication including a validation check on the
6591 -- constraint, if any. If a constraint is given, its bounds must be
6592 -- implicitly converted to the new type.
6593
6594 if Nkind (Indic) = N_Subtype_Indication then
6595 declare
6596 R : constant Node_Id :=
6597 Range_Expression (Constraint (Indic));
6598
6599 begin
6600 if Nkind (R) = N_Range then
6601 Hi := Build_Scalar_Bound
6602 (High_Bound (R), Parent_Type, Implicit_Base);
6603 Lo := Build_Scalar_Bound
6604 (Low_Bound (R), Parent_Type, Implicit_Base);
6605
6606 else
6607 -- Constraint is a Range attribute. Replace with explicit
6608 -- mention of the bounds of the prefix, which must be a
6609 -- subtype.
6610
6611 Analyze (Prefix (R));
6612 Hi :=
6613 Convert_To (Implicit_Base,
6614 Make_Attribute_Reference (Loc,
6615 Attribute_Name => Name_Last,
6616 Prefix =>
6617 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
6618
6619 Lo :=
6620 Convert_To (Implicit_Base,
6621 Make_Attribute_Reference (Loc,
6622 Attribute_Name => Name_First,
6623 Prefix =>
6624 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
6625 end if;
6626 end;
6627
6628 else
6629 Hi :=
6630 Build_Scalar_Bound
6631 (Type_High_Bound (Parent_Type),
6632 Parent_Type, Implicit_Base);
6633 Lo :=
6634 Build_Scalar_Bound
6635 (Type_Low_Bound (Parent_Type),
6636 Parent_Type, Implicit_Base);
6637 end if;
6638
6639 Rang_Expr :=
6640 Make_Range (Loc,
6641 Low_Bound => Lo,
6642 High_Bound => Hi);
6643
6644 -- If we constructed a default range for the case where no range
6645 -- was given, then the expressions in the range must not freeze
6646 -- since they do not correspond to expressions in the source.
6647
6648 if Nkind (Indic) /= N_Subtype_Indication then
6649 Set_Must_Not_Freeze (Lo);
6650 Set_Must_Not_Freeze (Hi);
6651 Set_Must_Not_Freeze (Rang_Expr);
6652 end if;
6653
6654 Rewrite (N,
6655 Make_Subtype_Declaration (Loc,
6656 Defining_Identifier => Derived_Type,
6657 Subtype_Indication =>
6658 Make_Subtype_Indication (Loc,
6659 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
6660 Constraint =>
6661 Make_Range_Constraint (Loc,
6662 Range_Expression => Rang_Expr))));
6663
6664 Analyze (N);
6665
6666 -- Propagate the aspects from the original type declaration to the
6667 -- declaration of the implicit base.
6668
6669 Move_Aspects (From => Original_Node (N), To => Type_Decl);
6670
6671 -- Apply a range check. Since this range expression doesn't have an
6672 -- Etype, we have to specifically pass the Source_Typ parameter. Is
6673 -- this right???
6674
6675 if Nkind (Indic) = N_Subtype_Indication then
6676 Apply_Range_Check
6677 (Range_Expression (Constraint (Indic)), Parent_Type,
6678 Source_Typ => Entity (Subtype_Mark (Indic)));
6679 end if;
6680 end if;
6681 end Build_Derived_Enumeration_Type;
6682
6683 --------------------------------
6684 -- Build_Derived_Numeric_Type --
6685 --------------------------------
6686
6687 procedure Build_Derived_Numeric_Type
6688 (N : Node_Id;
6689 Parent_Type : Entity_Id;
6690 Derived_Type : Entity_Id)
6691 is
6692 Loc : constant Source_Ptr := Sloc (N);
6693 Tdef : constant Node_Id := Type_Definition (N);
6694 Indic : constant Node_Id := Subtype_Indication (Tdef);
6695 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
6696 No_Constraint : constant Boolean := Nkind (Indic) /=
6697 N_Subtype_Indication;
6698 Implicit_Base : Entity_Id;
6699
6700 Lo : Node_Id;
6701 Hi : Node_Id;
6702
6703 begin
6704 -- Process the subtype indication including a validation check on
6705 -- the constraint if any.
6706
6707 Discard_Node (Process_Subtype (Indic, N));
6708
6709 -- Introduce an implicit base type for the derived type even if there
6710 -- is no constraint attached to it, since this seems closer to the Ada
6711 -- semantics.
6712
6713 Implicit_Base :=
6714 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
6715
6716 Set_Etype (Implicit_Base, Parent_Base);
6717 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
6718 Set_Size_Info (Implicit_Base, Parent_Base);
6719 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
6720 Set_Parent (Implicit_Base, Parent (Derived_Type));
6721 Set_Is_Known_Valid (Implicit_Base, Is_Known_Valid (Parent_Base));
6722
6723 -- Set RM Size for discrete type or decimal fixed-point type
6724 -- Ordinary fixed-point is excluded, why???
6725
6726 if Is_Discrete_Type (Parent_Base)
6727 or else Is_Decimal_Fixed_Point_Type (Parent_Base)
6728 then
6729 Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
6730 end if;
6731
6732 Set_Has_Delayed_Freeze (Implicit_Base);
6733
6734 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
6735 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
6736
6737 Set_Scalar_Range (Implicit_Base,
6738 Make_Range (Loc,
6739 Low_Bound => Lo,
6740 High_Bound => Hi));
6741
6742 if Has_Infinities (Parent_Base) then
6743 Set_Includes_Infinities (Scalar_Range (Implicit_Base));
6744 end if;
6745
6746 -- The Derived_Type, which is the entity of the declaration, is a
6747 -- subtype of the implicit base. Its Ekind is a subtype, even in the
6748 -- absence of an explicit constraint.
6749
6750 Set_Etype (Derived_Type, Implicit_Base);
6751
6752 -- If we did not have a constraint, then the Ekind is set from the
6753 -- parent type (otherwise Process_Subtype has set the bounds)
6754
6755 if No_Constraint then
6756 Set_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
6757 end if;
6758
6759 -- If we did not have a range constraint, then set the range from the
6760 -- parent type. Otherwise, the Process_Subtype call has set the bounds.
6761
6762 if No_Constraint or else not Has_Range_Constraint (Indic) then
6763 Set_Scalar_Range (Derived_Type,
6764 Make_Range (Loc,
6765 Low_Bound => New_Copy_Tree (Type_Low_Bound (Parent_Type)),
6766 High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
6767 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
6768
6769 if Has_Infinities (Parent_Type) then
6770 Set_Includes_Infinities (Scalar_Range (Derived_Type));
6771 end if;
6772
6773 Set_Is_Known_Valid (Derived_Type, Is_Known_Valid (Parent_Type));
6774 end if;
6775
6776 Set_Is_Descendent_Of_Address (Derived_Type,
6777 Is_Descendent_Of_Address (Parent_Type));
6778 Set_Is_Descendent_Of_Address (Implicit_Base,
6779 Is_Descendent_Of_Address (Parent_Type));
6780
6781 -- Set remaining type-specific fields, depending on numeric type
6782
6783 if Is_Modular_Integer_Type (Parent_Type) then
6784 Set_Modulus (Implicit_Base, Modulus (Parent_Base));
6785
6786 Set_Non_Binary_Modulus
6787 (Implicit_Base, Non_Binary_Modulus (Parent_Base));
6788
6789 Set_Is_Known_Valid
6790 (Implicit_Base, Is_Known_Valid (Parent_Base));
6791
6792 elsif Is_Floating_Point_Type (Parent_Type) then
6793
6794 -- Digits of base type is always copied from the digits value of
6795 -- the parent base type, but the digits of the derived type will
6796 -- already have been set if there was a constraint present.
6797
6798 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
6799 Set_Float_Rep (Implicit_Base, Float_Rep (Parent_Base));
6800
6801 if No_Constraint then
6802 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
6803 end if;
6804
6805 elsif Is_Fixed_Point_Type (Parent_Type) then
6806
6807 -- Small of base type and derived type are always copied from the
6808 -- parent base type, since smalls never change. The delta of the
6809 -- base type is also copied from the parent base type. However the
6810 -- delta of the derived type will have been set already if a
6811 -- constraint was present.
6812
6813 Set_Small_Value (Derived_Type, Small_Value (Parent_Base));
6814 Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
6815 Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
6816
6817 if No_Constraint then
6818 Set_Delta_Value (Derived_Type, Delta_Value (Parent_Type));
6819 end if;
6820
6821 -- The scale and machine radix in the decimal case are always
6822 -- copied from the parent base type.
6823
6824 if Is_Decimal_Fixed_Point_Type (Parent_Type) then
6825 Set_Scale_Value (Derived_Type, Scale_Value (Parent_Base));
6826 Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
6827
6828 Set_Machine_Radix_10
6829 (Derived_Type, Machine_Radix_10 (Parent_Base));
6830 Set_Machine_Radix_10
6831 (Implicit_Base, Machine_Radix_10 (Parent_Base));
6832
6833 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
6834
6835 if No_Constraint then
6836 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
6837
6838 else
6839 -- the analysis of the subtype_indication sets the
6840 -- digits value of the derived type.
6841
6842 null;
6843 end if;
6844 end if;
6845 end if;
6846
6847 if Is_Integer_Type (Parent_Type) then
6848 Set_Has_Shift_Operator
6849 (Implicit_Base, Has_Shift_Operator (Parent_Type));
6850 end if;
6851
6852 -- The type of the bounds is that of the parent type, and they
6853 -- must be converted to the derived type.
6854
6855 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
6856
6857 -- The implicit_base should be frozen when the derived type is frozen,
6858 -- but note that it is used in the conversions of the bounds. For fixed
6859 -- types we delay the determination of the bounds until the proper
6860 -- freezing point. For other numeric types this is rejected by GCC, for
6861 -- reasons that are currently unclear (???), so we choose to freeze the
6862 -- implicit base now. In the case of integers and floating point types
6863 -- this is harmless because subsequent representation clauses cannot
6864 -- affect anything, but it is still baffling that we cannot use the
6865 -- same mechanism for all derived numeric types.
6866
6867 -- There is a further complication: actually some representation
6868 -- clauses can affect the implicit base type. For example, attribute
6869 -- definition clauses for stream-oriented attributes need to set the
6870 -- corresponding TSS entries on the base type, and this normally
6871 -- cannot be done after the base type is frozen, so the circuitry in
6872 -- Sem_Ch13.New_Stream_Subprogram must account for this possibility
6873 -- and not use Set_TSS in this case.
6874
6875 -- There are also consequences for the case of delayed representation
6876 -- aspects for some cases. For example, a Size aspect is delayed and
6877 -- should not be evaluated to the freeze point. This early freezing
6878 -- means that the size attribute evaluation happens too early???
6879
6880 if Is_Fixed_Point_Type (Parent_Type) then
6881 Conditional_Delay (Implicit_Base, Parent_Type);
6882 else
6883 Freeze_Before (N, Implicit_Base);
6884 end if;
6885 end Build_Derived_Numeric_Type;
6886
6887 --------------------------------
6888 -- Build_Derived_Private_Type --
6889 --------------------------------
6890
6891 procedure Build_Derived_Private_Type
6892 (N : Node_Id;
6893 Parent_Type : Entity_Id;
6894 Derived_Type : Entity_Id;
6895 Is_Completion : Boolean;
6896 Derive_Subps : Boolean := True)
6897 is
6898 Loc : constant Source_Ptr := Sloc (N);
6899 Par_Base : constant Entity_Id := Base_Type (Parent_Type);
6900 Par_Scope : constant Entity_Id := Scope (Par_Base);
6901 Full_N : constant Node_Id := New_Copy_Tree (N);
6902 Full_Der : Entity_Id := New_Copy (Derived_Type);
6903 Full_P : Entity_Id;
6904
6905 procedure Build_Full_Derivation;
6906 -- Build full derivation, i.e. derive from the full view
6907
6908 procedure Copy_And_Build;
6909 -- Copy derived type declaration, replace parent with its full view,
6910 -- and build derivation
6911
6912 ---------------------------
6913 -- Build_Full_Derivation --
6914 ---------------------------
6915
6916 procedure Build_Full_Derivation is
6917 begin
6918 -- If parent scope is not open, install the declarations
6919
6920 if not In_Open_Scopes (Par_Scope) then
6921 Install_Private_Declarations (Par_Scope);
6922 Install_Visible_Declarations (Par_Scope);
6923 Copy_And_Build;
6924 Uninstall_Declarations (Par_Scope);
6925
6926 -- If parent scope is open and in another unit, and parent has a
6927 -- completion, then the derivation is taking place in the visible
6928 -- part of a child unit. In that case retrieve the full view of
6929 -- the parent momentarily.
6930
6931 elsif not In_Same_Source_Unit (N, Parent_Type) then
6932 Full_P := Full_View (Parent_Type);
6933 Exchange_Declarations (Parent_Type);
6934 Copy_And_Build;
6935 Exchange_Declarations (Full_P);
6936
6937 -- Otherwise it is a local derivation
6938
6939 else
6940 Copy_And_Build;
6941 end if;
6942 end Build_Full_Derivation;
6943
6944 --------------------
6945 -- Copy_And_Build --
6946 --------------------
6947
6948 procedure Copy_And_Build is
6949 Full_Parent : Entity_Id := Parent_Type;
6950
6951 begin
6952 -- If the parent is itself derived from another private type,
6953 -- installing the private declarations has not affected its
6954 -- privacy status, so use its own full view explicitly.
6955
6956 if Is_Private_Type (Full_Parent)
6957 and then Present (Full_View (Full_Parent))
6958 then
6959 Full_Parent := Full_View (Full_Parent);
6960 end if;
6961
6962 -- And its underlying full view if necessary
6963
6964 if Is_Private_Type (Full_Parent)
6965 and then Present (Underlying_Full_View (Full_Parent))
6966 then
6967 Full_Parent := Underlying_Full_View (Full_Parent);
6968 end if;
6969
6970 -- For record, access and most enumeration types, derivation from
6971 -- the full view requires a fully-fledged declaration. In the other
6972 -- cases, just use an itype.
6973
6974 if Ekind (Full_Parent) in Record_Kind
6975 or else Ekind (Full_Parent) in Access_Kind
6976 or else
6977 (Ekind (Full_Parent) in Enumeration_Kind
6978 and then not Is_Standard_Character_Type (Full_Parent)
6979 and then not Is_Generic_Type (Root_Type (Full_Parent)))
6980 then
6981 -- Copy and adjust declaration to provide a completion for what
6982 -- is originally a private declaration. Indicate that full view
6983 -- is internally generated.
6984
6985 Set_Comes_From_Source (Full_N, False);
6986 Set_Comes_From_Source (Full_Der, False);
6987 Set_Parent (Full_Der, Full_N);
6988 Set_Defining_Identifier (Full_N, Full_Der);
6989
6990 -- If there are no constraints, adjust the subtype mark
6991
6992 if Nkind (Subtype_Indication (Type_Definition (Full_N))) /=
6993 N_Subtype_Indication
6994 then
6995 Set_Subtype_Indication
6996 (Type_Definition (Full_N),
6997 New_Occurrence_Of (Full_Parent, Sloc (Full_N)));
6998 end if;
6999
7000 Insert_After (N, Full_N);
7001
7002 -- Build full view of derived type from full view of parent which
7003 -- is now installed. Subprograms have been derived on the partial
7004 -- view, the completion does not derive them anew.
7005
7006 if Ekind (Full_Parent) in Record_Kind then
7007
7008 -- If parent type is tagged, the completion inherits the proper
7009 -- primitive operations.
7010
7011 if Is_Tagged_Type (Parent_Type) then
7012 Build_Derived_Record_Type
7013 (Full_N, Full_Parent, Full_Der, Derive_Subps);
7014 else
7015 Build_Derived_Record_Type
7016 (Full_N, Full_Parent, Full_Der, Derive_Subps => False);
7017 end if;
7018
7019 else
7020 Build_Derived_Type
7021 (Full_N, Full_Parent, Full_Der,
7022 Is_Completion => False, Derive_Subps => False);
7023 end if;
7024
7025 -- The full declaration has been introduced into the tree and
7026 -- processed in the step above. It should not be analyzed again
7027 -- (when encountered later in the current list of declarations)
7028 -- to prevent spurious name conflicts. The full entity remains
7029 -- invisible.
7030
7031 Set_Analyzed (Full_N);
7032
7033 else
7034 Full_Der :=
7035 Make_Defining_Identifier (Sloc (Derived_Type),
7036 Chars => Chars (Derived_Type));
7037 Set_Is_Itype (Full_Der);
7038 Set_Associated_Node_For_Itype (Full_Der, N);
7039 Set_Parent (Full_Der, N);
7040 Build_Derived_Type
7041 (N, Full_Parent, Full_Der,
7042 Is_Completion => False, Derive_Subps => False);
7043 end if;
7044
7045 Set_Has_Private_Declaration (Full_Der);
7046 Set_Has_Private_Declaration (Derived_Type);
7047
7048 Set_Scope (Full_Der, Scope (Derived_Type));
7049 Set_Is_First_Subtype (Full_Der, Is_First_Subtype (Derived_Type));
7050 Set_Has_Size_Clause (Full_Der, False);
7051 Set_Has_Alignment_Clause (Full_Der, False);
7052 Set_Has_Delayed_Freeze (Full_Der);
7053 Set_Is_Frozen (Full_Der, False);
7054 Set_Freeze_Node (Full_Der, Empty);
7055 Set_Depends_On_Private (Full_Der, Has_Private_Component (Full_Der));
7056 Set_Is_Public (Full_Der, Is_Public (Derived_Type));
7057
7058 -- The convention on the base type may be set in the private part
7059 -- and not propagated to the subtype until later, so we obtain the
7060 -- convention from the base type of the parent.
7061
7062 Set_Convention (Full_Der, Convention (Base_Type (Full_Parent)));
7063 end Copy_And_Build;
7064
7065 -- Start of processing for Build_Derived_Private_Type
7066
7067 begin
7068 if Is_Tagged_Type (Parent_Type) then
7069 Full_P := Full_View (Parent_Type);
7070
7071 -- A type extension of a type with unknown discriminants is an
7072 -- indefinite type that the back-end cannot handle directly.
7073 -- We treat it as a private type, and build a completion that is
7074 -- derived from the full view of the parent, and hopefully has
7075 -- known discriminants.
7076
7077 -- If the full view of the parent type has an underlying record view,
7078 -- use it to generate the underlying record view of this derived type
7079 -- (required for chains of derivations with unknown discriminants).
7080
7081 -- Minor optimization: we avoid the generation of useless underlying
7082 -- record view entities if the private type declaration has unknown
7083 -- discriminants but its corresponding full view has no
7084 -- discriminants.
7085
7086 if Has_Unknown_Discriminants (Parent_Type)
7087 and then Present (Full_P)
7088 and then (Has_Discriminants (Full_P)
7089 or else Present (Underlying_Record_View (Full_P)))
7090 and then not In_Open_Scopes (Par_Scope)
7091 and then Expander_Active
7092 then
7093 declare
7094 Full_Der : constant Entity_Id := Make_Temporary (Loc, 'T');
7095 New_Ext : constant Node_Id :=
7096 Copy_Separate_Tree
7097 (Record_Extension_Part (Type_Definition (N)));
7098 Decl : Node_Id;
7099
7100 begin
7101 Build_Derived_Record_Type
7102 (N, Parent_Type, Derived_Type, Derive_Subps);
7103
7104 -- Build anonymous completion, as a derivation from the full
7105 -- view of the parent. This is not a completion in the usual
7106 -- sense, because the current type is not private.
7107
7108 Decl :=
7109 Make_Full_Type_Declaration (Loc,
7110 Defining_Identifier => Full_Der,
7111 Type_Definition =>
7112 Make_Derived_Type_Definition (Loc,
7113 Subtype_Indication =>
7114 New_Copy_Tree
7115 (Subtype_Indication (Type_Definition (N))),
7116 Record_Extension_Part => New_Ext));
7117
7118 -- If the parent type has an underlying record view, use it
7119 -- here to build the new underlying record view.
7120
7121 if Present (Underlying_Record_View (Full_P)) then
7122 pragma Assert
7123 (Nkind (Subtype_Indication (Type_Definition (Decl)))
7124 = N_Identifier);
7125 Set_Entity (Subtype_Indication (Type_Definition (Decl)),
7126 Underlying_Record_View (Full_P));
7127 end if;
7128
7129 Install_Private_Declarations (Par_Scope);
7130 Install_Visible_Declarations (Par_Scope);
7131 Insert_Before (N, Decl);
7132
7133 -- Mark entity as an underlying record view before analysis,
7134 -- to avoid generating the list of its primitive operations
7135 -- (which is not really required for this entity) and thus
7136 -- prevent spurious errors associated with missing overriding
7137 -- of abstract primitives (overridden only for Derived_Type).
7138
7139 Set_Ekind (Full_Der, E_Record_Type);
7140 Set_Is_Underlying_Record_View (Full_Der);
7141 Set_Default_SSO (Full_Der);
7142
7143 Analyze (Decl);
7144
7145 pragma Assert (Has_Discriminants (Full_Der)
7146 and then not Has_Unknown_Discriminants (Full_Der));
7147
7148 Uninstall_Declarations (Par_Scope);
7149
7150 -- Freeze the underlying record view, to prevent generation of
7151 -- useless dispatching information, which is simply shared with
7152 -- the real derived type.
7153
7154 Set_Is_Frozen (Full_Der);
7155
7156 -- If the derived type has access discriminants, create
7157 -- references to their anonymous types now, to prevent
7158 -- back-end problems when their first use is in generated
7159 -- bodies of primitives.
7160
7161 declare
7162 E : Entity_Id;
7163
7164 begin
7165 E := First_Entity (Full_Der);
7166
7167 while Present (E) loop
7168 if Ekind (E) = E_Discriminant
7169 and then Ekind (Etype (E)) = E_Anonymous_Access_Type
7170 then
7171 Build_Itype_Reference (Etype (E), Decl);
7172 end if;
7173
7174 Next_Entity (E);
7175 end loop;
7176 end;
7177
7178 -- Set up links between real entity and underlying record view
7179
7180 Set_Underlying_Record_View (Derived_Type, Base_Type (Full_Der));
7181 Set_Underlying_Record_View (Base_Type (Full_Der), Derived_Type);
7182 end;
7183
7184 -- If discriminants are known, build derived record
7185
7186 else
7187 Build_Derived_Record_Type
7188 (N, Parent_Type, Derived_Type, Derive_Subps);
7189 end if;
7190
7191 return;
7192
7193 elsif Has_Discriminants (Parent_Type) then
7194
7195 -- Build partial view of derived type from partial view of parent.
7196 -- This must be done before building the full derivation because the
7197 -- second derivation will modify the discriminants of the first and
7198 -- the discriminants are chained with the rest of the components in
7199 -- the full derivation.
7200
7201 Build_Derived_Record_Type
7202 (N, Parent_Type, Derived_Type, Derive_Subps);
7203
7204 -- Build the full derivation if this is not the anonymous derived
7205 -- base type created by Build_Derived_Record_Type in the constrained
7206 -- case (see point 5. of its head comment) since we build it for the
7207 -- derived subtype. And skip it for protected types altogether, as
7208 -- gigi does not use these types directly.
7209
7210 if Present (Full_View (Parent_Type))
7211 and then not Is_Itype (Derived_Type)
7212 and then not (Ekind (Full_View (Parent_Type)) in Protected_Kind)
7213 then
7214 declare
7215 Der_Base : constant Entity_Id := Base_Type (Derived_Type);
7216 Discr : Entity_Id;
7217 Last_Discr : Entity_Id;
7218
7219 begin
7220 -- If this is not a completion, construct the implicit full
7221 -- view by deriving from the full view of the parent type.
7222 -- But if this is a completion, the derived private type
7223 -- being built is a full view and the full derivation can
7224 -- only be its underlying full view.
7225
7226 Build_Full_Derivation;
7227
7228 if not Is_Completion then
7229 Set_Full_View (Derived_Type, Full_Der);
7230 else
7231 Set_Underlying_Full_View (Derived_Type, Full_Der);
7232 end if;
7233
7234 if not Is_Base_Type (Derived_Type) then
7235 Set_Full_View (Der_Base, Base_Type (Full_Der));
7236 end if;
7237
7238 -- Copy the discriminant list from full view to the partial
7239 -- view (base type and its subtype). Gigi requires that the
7240 -- partial and full views have the same discriminants.
7241
7242 -- Note that since the partial view points to discriminants
7243 -- in the full view, their scope will be that of the full
7244 -- view. This might cause some front end problems and need
7245 -- adjustment???
7246
7247 Discr := First_Discriminant (Base_Type (Full_Der));
7248 Set_First_Entity (Der_Base, Discr);
7249
7250 loop
7251 Last_Discr := Discr;
7252 Next_Discriminant (Discr);
7253 exit when No (Discr);
7254 end loop;
7255
7256 Set_Last_Entity (Der_Base, Last_Discr);
7257 Set_First_Entity (Derived_Type, First_Entity (Der_Base));
7258 Set_Last_Entity (Derived_Type, Last_Entity (Der_Base));
7259
7260 Set_Stored_Constraint
7261 (Full_Der, Stored_Constraint (Derived_Type));
7262 end;
7263 end if;
7264
7265 elsif Present (Full_View (Parent_Type))
7266 and then Has_Discriminants (Full_View (Parent_Type))
7267 then
7268 if Has_Unknown_Discriminants (Parent_Type)
7269 and then Nkind (Subtype_Indication (Type_Definition (N))) =
7270 N_Subtype_Indication
7271 then
7272 Error_Msg_N
7273 ("cannot constrain type with unknown discriminants",
7274 Subtype_Indication (Type_Definition (N)));
7275 return;
7276 end if;
7277
7278 -- If this is not a completion, construct the implicit full view by
7279 -- deriving from the full view of the parent type. But if this is a
7280 -- completion, the derived private type being built is a full view
7281 -- and the full derivation can only be its underlying full view.
7282
7283 Build_Full_Derivation;
7284
7285 if not Is_Completion then
7286 Set_Full_View (Derived_Type, Full_Der);
7287 else
7288 Set_Underlying_Full_View (Derived_Type, Full_Der);
7289 end if;
7290
7291 -- In any case, the primitive operations are inherited from the
7292 -- parent type, not from the internal full view.
7293
7294 Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
7295
7296 if Derive_Subps then
7297 Derive_Subprograms (Parent_Type, Derived_Type);
7298 end if;
7299
7300 Set_Stored_Constraint (Derived_Type, No_Elist);
7301 Set_Is_Constrained
7302 (Derived_Type, Is_Constrained (Full_View (Parent_Type)));
7303
7304 else
7305 -- Untagged type, No discriminants on either view
7306
7307 if Nkind (Subtype_Indication (Type_Definition (N))) =
7308 N_Subtype_Indication
7309 then
7310 Error_Msg_N
7311 ("illegal constraint on type without discriminants", N);
7312 end if;
7313
7314 if Present (Discriminant_Specifications (N))
7315 and then Present (Full_View (Parent_Type))
7316 and then not Is_Tagged_Type (Full_View (Parent_Type))
7317 then
7318 Error_Msg_N ("cannot add discriminants to untagged type", N);
7319 end if;
7320
7321 Set_Stored_Constraint (Derived_Type, No_Elist);
7322 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7323 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
7324 Set_Has_Controlled_Component
7325 (Derived_Type, Has_Controlled_Component
7326 (Parent_Type));
7327
7328 -- Direct controlled types do not inherit Finalize_Storage_Only flag
7329
7330 if not Is_Controlled (Parent_Type) then
7331 Set_Finalize_Storage_Only
7332 (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
7333 end if;
7334
7335 -- If this is not a completion, construct the implicit full view by
7336 -- deriving from the full view of the parent type.
7337
7338 -- ??? If the parent is untagged private and its completion is
7339 -- tagged, this mechanism will not work because we cannot derive from
7340 -- the tagged full view unless we have an extension.
7341
7342 if Present (Full_View (Parent_Type))
7343 and then not Is_Tagged_Type (Full_View (Parent_Type))
7344 and then not Is_Completion
7345 then
7346 Build_Full_Derivation;
7347 Set_Full_View (Derived_Type, Full_Der);
7348 end if;
7349 end if;
7350
7351 Set_Has_Unknown_Discriminants (Derived_Type,
7352 Has_Unknown_Discriminants (Parent_Type));
7353
7354 if Is_Private_Type (Derived_Type) then
7355 Set_Private_Dependents (Derived_Type, New_Elmt_List);
7356 end if;
7357
7358 -- If the parent base type is in scope, add the derived type to its
7359 -- list of private dependents, because its full view may become
7360 -- visible subsequently (in a nested private part, a body, or in a
7361 -- further child unit).
7362
7363 if Is_Private_Type (Par_Base) and then In_Open_Scopes (Par_Scope) then
7364 Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
7365
7366 -- Check for unusual case where a type completed by a private
7367 -- derivation occurs within a package nested in a child unit, and
7368 -- the parent is declared in an ancestor.
7369
7370 if Is_Child_Unit (Scope (Current_Scope))
7371 and then Is_Completion
7372 and then In_Private_Part (Current_Scope)
7373 and then Scope (Parent_Type) /= Current_Scope
7374
7375 -- Note that if the parent has a completion in the private part,
7376 -- (which is itself a derivation from some other private type)
7377 -- it is that completion that is visible, there is no full view
7378 -- available, and no special processing is needed.
7379
7380 and then Present (Full_View (Parent_Type))
7381 then
7382 -- In this case, the full view of the parent type will become
7383 -- visible in the body of the enclosing child, and only then will
7384 -- the current type be possibly non-private. Build an underlying
7385 -- full view that will be installed when the enclosing child body
7386 -- is compiled.
7387
7388 if Present (Underlying_Full_View (Derived_Type)) then
7389 Full_Der := Underlying_Full_View (Derived_Type);
7390 else
7391 Build_Full_Derivation;
7392 Set_Underlying_Full_View (Derived_Type, Full_Der);
7393 end if;
7394
7395 -- The full view will be used to swap entities on entry/exit to
7396 -- the body, and must appear in the entity list for the package.
7397
7398 Append_Entity (Full_Der, Scope (Derived_Type));
7399 end if;
7400 end if;
7401 end Build_Derived_Private_Type;
7402
7403 -------------------------------
7404 -- Build_Derived_Record_Type --
7405 -------------------------------
7406
7407 -- 1. INTRODUCTION
7408
7409 -- Ideally we would like to use the same model of type derivation for
7410 -- tagged and untagged record types. Unfortunately this is not quite
7411 -- possible because the semantics of representation clauses is different
7412 -- for tagged and untagged records under inheritance. Consider the
7413 -- following:
7414
7415 -- type R (...) is [tagged] record ... end record;
7416 -- type T (...) is new R (...) [with ...];
7417
7418 -- The representation clauses for T can specify a completely different
7419 -- record layout from R's. Hence the same component can be placed in two
7420 -- very different positions in objects of type T and R. If R and T are
7421 -- tagged types, representation clauses for T can only specify the layout
7422 -- of non inherited components, thus components that are common in R and T
7423 -- have the same position in objects of type R and T.
7424
7425 -- This has two implications. The first is that the entire tree for R's
7426 -- declaration needs to be copied for T in the untagged case, so that T
7427 -- can be viewed as a record type of its own with its own representation
7428 -- clauses. The second implication is the way we handle discriminants.
7429 -- Specifically, in the untagged case we need a way to communicate to Gigi
7430 -- what are the real discriminants in the record, while for the semantics
7431 -- we need to consider those introduced by the user to rename the
7432 -- discriminants in the parent type. This is handled by introducing the
7433 -- notion of stored discriminants. See below for more.
7434
7435 -- Fortunately the way regular components are inherited can be handled in
7436 -- the same way in tagged and untagged types.
7437
7438 -- To complicate things a bit more the private view of a private extension
7439 -- cannot be handled in the same way as the full view (for one thing the
7440 -- semantic rules are somewhat different). We will explain what differs
7441 -- below.
7442
7443 -- 2. DISCRIMINANTS UNDER INHERITANCE
7444
7445 -- The semantic rules governing the discriminants of derived types are
7446 -- quite subtle.
7447
7448 -- type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
7449 -- [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
7450
7451 -- If parent type has discriminants, then the discriminants that are
7452 -- declared in the derived type are [3.4 (11)]:
7453
7454 -- o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
7455 -- there is one;
7456
7457 -- o Otherwise, each discriminant of the parent type (implicitly declared
7458 -- in the same order with the same specifications). In this case, the
7459 -- discriminants are said to be "inherited", or if unknown in the parent
7460 -- are also unknown in the derived type.
7461
7462 -- Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
7463
7464 -- o The parent subtype must be constrained;
7465
7466 -- o If the parent type is not a tagged type, then each discriminant of
7467 -- the derived type must be used in the constraint defining a parent
7468 -- subtype. [Implementation note: This ensures that the new discriminant
7469 -- can share storage with an existing discriminant.]
7470
7471 -- For the derived type each discriminant of the parent type is either
7472 -- inherited, constrained to equal some new discriminant of the derived
7473 -- type, or constrained to the value of an expression.
7474
7475 -- When inherited or constrained to equal some new discriminant, the
7476 -- parent discriminant and the discriminant of the derived type are said
7477 -- to "correspond".
7478
7479 -- If a discriminant of the parent type is constrained to a specific value
7480 -- in the derived type definition, then the discriminant is said to be
7481 -- "specified" by that derived type definition.
7482
7483 -- 3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
7484
7485 -- We have spoken about stored discriminants in point 1 (introduction)
7486 -- above. There are two sort of stored discriminants: implicit and
7487 -- explicit. As long as the derived type inherits the same discriminants as
7488 -- the root record type, stored discriminants are the same as regular
7489 -- discriminants, and are said to be implicit. However, if any discriminant
7490 -- in the root type was renamed in the derived type, then the derived
7491 -- type will contain explicit stored discriminants. Explicit stored
7492 -- discriminants are discriminants in addition to the semantically visible
7493 -- discriminants defined for the derived type. Stored discriminants are
7494 -- used by Gigi to figure out what are the physical discriminants in
7495 -- objects of the derived type (see precise definition in einfo.ads).
7496 -- As an example, consider the following:
7497
7498 -- type R (D1, D2, D3 : Int) is record ... end record;
7499 -- type T1 is new R;
7500 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
7501 -- type T3 is new T2;
7502 -- type T4 (Y : Int) is new T3 (Y, 99);
7503
7504 -- The following table summarizes the discriminants and stored
7505 -- discriminants in R and T1 through T4.
7506
7507 -- Type Discrim Stored Discrim Comment
7508 -- R (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in R
7509 -- T1 (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in T1
7510 -- T2 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T2
7511 -- T3 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T3
7512 -- T4 (Y) (D1, D2, D3) Girder discrims EXPLICIT in T4
7513
7514 -- Field Corresponding_Discriminant (abbreviated CD below) allows us to
7515 -- find the corresponding discriminant in the parent type, while
7516 -- Original_Record_Component (abbreviated ORC below), the actual physical
7517 -- component that is renamed. Finally the field Is_Completely_Hidden
7518 -- (abbreviated ICH below) is set for all explicit stored discriminants
7519 -- (see einfo.ads for more info). For the above example this gives:
7520
7521 -- Discrim CD ORC ICH
7522 -- ^^^^^^^ ^^ ^^^ ^^^
7523 -- D1 in R empty itself no
7524 -- D2 in R empty itself no
7525 -- D3 in R empty itself no
7526
7527 -- D1 in T1 D1 in R itself no
7528 -- D2 in T1 D2 in R itself no
7529 -- D3 in T1 D3 in R itself no
7530
7531 -- X1 in T2 D3 in T1 D3 in T2 no
7532 -- X2 in T2 D1 in T1 D1 in T2 no
7533 -- D1 in T2 empty itself yes
7534 -- D2 in T2 empty itself yes
7535 -- D3 in T2 empty itself yes
7536
7537 -- X1 in T3 X1 in T2 D3 in T3 no
7538 -- X2 in T3 X2 in T2 D1 in T3 no
7539 -- D1 in T3 empty itself yes
7540 -- D2 in T3 empty itself yes
7541 -- D3 in T3 empty itself yes
7542
7543 -- Y in T4 X1 in T3 D3 in T3 no
7544 -- D1 in T3 empty itself yes
7545 -- D2 in T3 empty itself yes
7546 -- D3 in T3 empty itself yes
7547
7548 -- 4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
7549
7550 -- Type derivation for tagged types is fairly straightforward. If no
7551 -- discriminants are specified by the derived type, these are inherited
7552 -- from the parent. No explicit stored discriminants are ever necessary.
7553 -- The only manipulation that is done to the tree is that of adding a
7554 -- _parent field with parent type and constrained to the same constraint
7555 -- specified for the parent in the derived type definition. For instance:
7556
7557 -- type R (D1, D2, D3 : Int) is tagged record ... end record;
7558 -- type T1 is new R with null record;
7559 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
7560
7561 -- are changed into:
7562
7563 -- type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
7564 -- _parent : R (D1, D2, D3);
7565 -- end record;
7566
7567 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
7568 -- _parent : T1 (X2, 88, X1);
7569 -- end record;
7570
7571 -- The discriminants actually present in R, T1 and T2 as well as their CD,
7572 -- ORC and ICH fields are:
7573
7574 -- Discrim CD ORC ICH
7575 -- ^^^^^^^ ^^ ^^^ ^^^
7576 -- D1 in R empty itself no
7577 -- D2 in R empty itself no
7578 -- D3 in R empty itself no
7579
7580 -- D1 in T1 D1 in R D1 in R no
7581 -- D2 in T1 D2 in R D2 in R no
7582 -- D3 in T1 D3 in R D3 in R no
7583
7584 -- X1 in T2 D3 in T1 D3 in R no
7585 -- X2 in T2 D1 in T1 D1 in R no
7586
7587 -- 5. FIRST TRANSFORMATION FOR DERIVED RECORDS
7588 --
7589 -- Regardless of whether we dealing with a tagged or untagged type
7590 -- we will transform all derived type declarations of the form
7591 --
7592 -- type T is new R (...) [with ...];
7593 -- or
7594 -- subtype S is R (...);
7595 -- type T is new S [with ...];
7596 -- into
7597 -- type BT is new R [with ...];
7598 -- subtype T is BT (...);
7599 --
7600 -- That is, the base derived type is constrained only if it has no
7601 -- discriminants. The reason for doing this is that GNAT's semantic model
7602 -- assumes that a base type with discriminants is unconstrained.
7603 --
7604 -- Note that, strictly speaking, the above transformation is not always
7605 -- correct. Consider for instance the following excerpt from ACVC b34011a:
7606 --
7607 -- procedure B34011A is
7608 -- type REC (D : integer := 0) is record
7609 -- I : Integer;
7610 -- end record;
7611
7612 -- package P is
7613 -- type T6 is new Rec;
7614 -- function F return T6;
7615 -- end P;
7616
7617 -- use P;
7618 -- package Q6 is
7619 -- type U is new T6 (Q6.F.I); -- ERROR: Q6.F.
7620 -- end Q6;
7621 --
7622 -- The definition of Q6.U is illegal. However transforming Q6.U into
7623
7624 -- type BaseU is new T6;
7625 -- subtype U is BaseU (Q6.F.I)
7626
7627 -- turns U into a legal subtype, which is incorrect. To avoid this problem
7628 -- we always analyze the constraint (in this case (Q6.F.I)) before applying
7629 -- the transformation described above.
7630
7631 -- There is another instance where the above transformation is incorrect.
7632 -- Consider:
7633
7634 -- package Pack is
7635 -- type Base (D : Integer) is tagged null record;
7636 -- procedure P (X : Base);
7637
7638 -- type Der is new Base (2) with null record;
7639 -- procedure P (X : Der);
7640 -- end Pack;
7641
7642 -- Then the above transformation turns this into
7643
7644 -- type Der_Base is new Base with null record;
7645 -- -- procedure P (X : Base) is implicitly inherited here
7646 -- -- as procedure P (X : Der_Base).
7647
7648 -- subtype Der is Der_Base (2);
7649 -- procedure P (X : Der);
7650 -- -- The overriding of P (X : Der_Base) is illegal since we
7651 -- -- have a parameter conformance problem.
7652
7653 -- To get around this problem, after having semantically processed Der_Base
7654 -- and the rewritten subtype declaration for Der, we copy Der_Base field
7655 -- Discriminant_Constraint from Der so that when parameter conformance is
7656 -- checked when P is overridden, no semantic errors are flagged.
7657
7658 -- 6. SECOND TRANSFORMATION FOR DERIVED RECORDS
7659
7660 -- Regardless of whether we are dealing with a tagged or untagged type
7661 -- we will transform all derived type declarations of the form
7662
7663 -- type R (D1, .., Dn : ...) is [tagged] record ...;
7664 -- type T is new R [with ...];
7665 -- into
7666 -- type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
7667
7668 -- The reason for such transformation is that it allows us to implement a
7669 -- very clean form of component inheritance as explained below.
7670
7671 -- Note that this transformation is not achieved by direct tree rewriting
7672 -- and manipulation, but rather by redoing the semantic actions that the
7673 -- above transformation will entail. This is done directly in routine
7674 -- Inherit_Components.
7675
7676 -- 7. TYPE DERIVATION AND COMPONENT INHERITANCE
7677
7678 -- In both tagged and untagged derived types, regular non discriminant
7679 -- components are inherited in the derived type from the parent type. In
7680 -- the absence of discriminants component, inheritance is straightforward
7681 -- as components can simply be copied from the parent.
7682
7683 -- If the parent has discriminants, inheriting components constrained with
7684 -- these discriminants requires caution. Consider the following example:
7685
7686 -- type R (D1, D2 : Positive) is [tagged] record
7687 -- S : String (D1 .. D2);
7688 -- end record;
7689
7690 -- type T1 is new R [with null record];
7691 -- type T2 (X : positive) is new R (1, X) [with null record];
7692
7693 -- As explained in 6. above, T1 is rewritten as
7694 -- type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
7695 -- which makes the treatment for T1 and T2 identical.
7696
7697 -- What we want when inheriting S, is that references to D1 and D2 in R are
7698 -- replaced with references to their correct constraints, i.e. D1 and D2 in
7699 -- T1 and 1 and X in T2. So all R's discriminant references are replaced
7700 -- with either discriminant references in the derived type or expressions.
7701 -- This replacement is achieved as follows: before inheriting R's
7702 -- components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
7703 -- created in the scope of T1 (resp. scope of T2) so that discriminants D1
7704 -- and D2 of T1 are visible (resp. discriminant X of T2 is visible).
7705 -- For T2, for instance, this has the effect of replacing String (D1 .. D2)
7706 -- by String (1 .. X).
7707
7708 -- 8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
7709
7710 -- We explain here the rules governing private type extensions relevant to
7711 -- type derivation. These rules are explained on the following example:
7712
7713 -- type D [(...)] is new A [(...)] with private; <-- partial view
7714 -- type D [(...)] is new P [(...)] with null record; <-- full view
7715
7716 -- Type A is called the ancestor subtype of the private extension.
7717 -- Type P is the parent type of the full view of the private extension. It
7718 -- must be A or a type derived from A.
7719
7720 -- The rules concerning the discriminants of private type extensions are
7721 -- [7.3(10-13)]:
7722
7723 -- o If a private extension inherits known discriminants from the ancestor
7724 -- subtype, then the full view must also inherit its discriminants from
7725 -- the ancestor subtype and the parent subtype of the full view must be
7726 -- constrained if and only if the ancestor subtype is constrained.
7727
7728 -- o If a partial view has unknown discriminants, then the full view may
7729 -- define a definite or an indefinite subtype, with or without
7730 -- discriminants.
7731
7732 -- o If a partial view has neither known nor unknown discriminants, then
7733 -- the full view must define a definite subtype.
7734
7735 -- o If the ancestor subtype of a private extension has constrained
7736 -- discriminants, then the parent subtype of the full view must impose a
7737 -- statically matching constraint on those discriminants.
7738
7739 -- This means that only the following forms of private extensions are
7740 -- allowed:
7741
7742 -- type D is new A with private; <-- partial view
7743 -- type D is new P with null record; <-- full view
7744
7745 -- If A has no discriminants than P has no discriminants, otherwise P must
7746 -- inherit A's discriminants.
7747
7748 -- type D is new A (...) with private; <-- partial view
7749 -- type D is new P (:::) with null record; <-- full view
7750
7751 -- P must inherit A's discriminants and (...) and (:::) must statically
7752 -- match.
7753
7754 -- subtype A is R (...);
7755 -- type D is new A with private; <-- partial view
7756 -- type D is new P with null record; <-- full view
7757
7758 -- P must have inherited R's discriminants and must be derived from A or
7759 -- any of its subtypes.
7760
7761 -- type D (..) is new A with private; <-- partial view
7762 -- type D (..) is new P [(:::)] with null record; <-- full view
7763
7764 -- No specific constraints on P's discriminants or constraint (:::).
7765 -- Note that A can be unconstrained, but the parent subtype P must either
7766 -- be constrained or (:::) must be present.
7767
7768 -- type D (..) is new A [(...)] with private; <-- partial view
7769 -- type D (..) is new P [(:::)] with null record; <-- full view
7770
7771 -- P's constraints on A's discriminants must statically match those
7772 -- imposed by (...).
7773
7774 -- 9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
7775
7776 -- The full view of a private extension is handled exactly as described
7777 -- above. The model chose for the private view of a private extension is
7778 -- the same for what concerns discriminants (i.e. they receive the same
7779 -- treatment as in the tagged case). However, the private view of the
7780 -- private extension always inherits the components of the parent base,
7781 -- without replacing any discriminant reference. Strictly speaking this is
7782 -- incorrect. However, Gigi never uses this view to generate code so this
7783 -- is a purely semantic issue. In theory, a set of transformations similar
7784 -- to those given in 5. and 6. above could be applied to private views of
7785 -- private extensions to have the same model of component inheritance as
7786 -- for non private extensions. However, this is not done because it would
7787 -- further complicate private type processing. Semantically speaking, this
7788 -- leaves us in an uncomfortable situation. As an example consider:
7789
7790 -- package Pack is
7791 -- type R (D : integer) is tagged record
7792 -- S : String (1 .. D);
7793 -- end record;
7794 -- procedure P (X : R);
7795 -- type T is new R (1) with private;
7796 -- private
7797 -- type T is new R (1) with null record;
7798 -- end;
7799
7800 -- This is transformed into:
7801
7802 -- package Pack is
7803 -- type R (D : integer) is tagged record
7804 -- S : String (1 .. D);
7805 -- end record;
7806 -- procedure P (X : R);
7807 -- type T is new R (1) with private;
7808 -- private
7809 -- type BaseT is new R with null record;
7810 -- subtype T is BaseT (1);
7811 -- end;
7812
7813 -- (strictly speaking the above is incorrect Ada)
7814
7815 -- From the semantic standpoint the private view of private extension T
7816 -- should be flagged as constrained since one can clearly have
7817 --
7818 -- Obj : T;
7819 --
7820 -- in a unit withing Pack. However, when deriving subprograms for the
7821 -- private view of private extension T, T must be seen as unconstrained
7822 -- since T has discriminants (this is a constraint of the current
7823 -- subprogram derivation model). Thus, when processing the private view of
7824 -- a private extension such as T, we first mark T as unconstrained, we
7825 -- process it, we perform program derivation and just before returning from
7826 -- Build_Derived_Record_Type we mark T as constrained.
7827
7828 -- ??? Are there are other uncomfortable cases that we will have to
7829 -- deal with.
7830
7831 -- 10. RECORD_TYPE_WITH_PRIVATE complications
7832
7833 -- Types that are derived from a visible record type and have a private
7834 -- extension present other peculiarities. They behave mostly like private
7835 -- types, but if they have primitive operations defined, these will not
7836 -- have the proper signatures for further inheritance, because other
7837 -- primitive operations will use the implicit base that we define for
7838 -- private derivations below. This affect subprogram inheritance (see
7839 -- Derive_Subprograms for details). We also derive the implicit base from
7840 -- the base type of the full view, so that the implicit base is a record
7841 -- type and not another private type, This avoids infinite loops.
7842
7843 procedure Build_Derived_Record_Type
7844 (N : Node_Id;
7845 Parent_Type : Entity_Id;
7846 Derived_Type : Entity_Id;
7847 Derive_Subps : Boolean := True)
7848 is
7849 Discriminant_Specs : constant Boolean :=
7850 Present (Discriminant_Specifications (N));
7851 Is_Tagged : constant Boolean := Is_Tagged_Type (Parent_Type);
7852 Loc : constant Source_Ptr := Sloc (N);
7853 Private_Extension : constant Boolean :=
7854 Nkind (N) = N_Private_Extension_Declaration;
7855 Assoc_List : Elist_Id;
7856 Constraint_Present : Boolean;
7857 Constrs : Elist_Id;
7858 Discrim : Entity_Id;
7859 Indic : Node_Id;
7860 Inherit_Discrims : Boolean := False;
7861 Last_Discrim : Entity_Id;
7862 New_Base : Entity_Id;
7863 New_Decl : Node_Id;
7864 New_Discrs : Elist_Id;
7865 New_Indic : Node_Id;
7866 Parent_Base : Entity_Id;
7867 Save_Etype : Entity_Id;
7868 Save_Discr_Constr : Elist_Id;
7869 Save_Next_Entity : Entity_Id;
7870 Type_Def : Node_Id;
7871
7872 Discs : Elist_Id := New_Elmt_List;
7873 -- An empty Discs list means that there were no constraints in the
7874 -- subtype indication or that there was an error processing it.
7875
7876 begin
7877 if Ekind (Parent_Type) = E_Record_Type_With_Private
7878 and then Present (Full_View (Parent_Type))
7879 and then Has_Discriminants (Parent_Type)
7880 then
7881 Parent_Base := Base_Type (Full_View (Parent_Type));
7882 else
7883 Parent_Base := Base_Type (Parent_Type);
7884 end if;
7885
7886 -- AI05-0115 : if this is a derivation from a private type in some
7887 -- other scope that may lead to invisible components for the derived
7888 -- type, mark it accordingly.
7889
7890 if Is_Private_Type (Parent_Type) then
7891 if Scope (Parent_Type) = Scope (Derived_Type) then
7892 null;
7893
7894 elsif In_Open_Scopes (Scope (Parent_Type))
7895 and then In_Private_Part (Scope (Parent_Type))
7896 then
7897 null;
7898
7899 else
7900 Set_Has_Private_Ancestor (Derived_Type);
7901 end if;
7902
7903 else
7904 Set_Has_Private_Ancestor
7905 (Derived_Type, Has_Private_Ancestor (Parent_Type));
7906 end if;
7907
7908 -- Before we start the previously documented transformations, here is
7909 -- little fix for size and alignment of tagged types. Normally when we
7910 -- derive type D from type P, we copy the size and alignment of P as the
7911 -- default for D, and in the absence of explicit representation clauses
7912 -- for D, the size and alignment are indeed the same as the parent.
7913
7914 -- But this is wrong for tagged types, since fields may be added, and
7915 -- the default size may need to be larger, and the default alignment may
7916 -- need to be larger.
7917
7918 -- We therefore reset the size and alignment fields in the tagged case.
7919 -- Note that the size and alignment will in any case be at least as
7920 -- large as the parent type (since the derived type has a copy of the
7921 -- parent type in the _parent field)
7922
7923 -- The type is also marked as being tagged here, which is needed when
7924 -- processing components with a self-referential anonymous access type
7925 -- in the call to Check_Anonymous_Access_Components below. Note that
7926 -- this flag is also set later on for completeness.
7927
7928 if Is_Tagged then
7929 Set_Is_Tagged_Type (Derived_Type);
7930 Init_Size_Align (Derived_Type);
7931 end if;
7932
7933 -- STEP 0a: figure out what kind of derived type declaration we have
7934
7935 if Private_Extension then
7936 Type_Def := N;
7937 Set_Ekind (Derived_Type, E_Record_Type_With_Private);
7938 Set_Default_SSO (Derived_Type);
7939
7940 else
7941 Type_Def := Type_Definition (N);
7942
7943 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
7944 -- Parent_Base can be a private type or private extension. However,
7945 -- for tagged types with an extension the newly added fields are
7946 -- visible and hence the Derived_Type is always an E_Record_Type.
7947 -- (except that the parent may have its own private fields).
7948 -- For untagged types we preserve the Ekind of the Parent_Base.
7949
7950 if Present (Record_Extension_Part (Type_Def)) then
7951 Set_Ekind (Derived_Type, E_Record_Type);
7952 Set_Default_SSO (Derived_Type);
7953
7954 -- Create internal access types for components with anonymous
7955 -- access types.
7956
7957 if Ada_Version >= Ada_2005 then
7958 Check_Anonymous_Access_Components
7959 (N, Derived_Type, Derived_Type,
7960 Component_List (Record_Extension_Part (Type_Def)));
7961 end if;
7962
7963 else
7964 Set_Ekind (Derived_Type, Ekind (Parent_Base));
7965 end if;
7966 end if;
7967
7968 -- Indic can either be an N_Identifier if the subtype indication
7969 -- contains no constraint or an N_Subtype_Indication if the subtype
7970 -- indication has a constraint.
7971
7972 Indic := Subtype_Indication (Type_Def);
7973 Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
7974
7975 -- Check that the type has visible discriminants. The type may be
7976 -- a private type with unknown discriminants whose full view has
7977 -- discriminants which are invisible.
7978
7979 if Constraint_Present then
7980 if not Has_Discriminants (Parent_Base)
7981 or else
7982 (Has_Unknown_Discriminants (Parent_Base)
7983 and then Is_Private_Type (Parent_Base))
7984 then
7985 Error_Msg_N
7986 ("invalid constraint: type has no discriminant",
7987 Constraint (Indic));
7988
7989 Constraint_Present := False;
7990 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
7991
7992 elsif Is_Constrained (Parent_Type) then
7993 Error_Msg_N
7994 ("invalid constraint: parent type is already constrained",
7995 Constraint (Indic));
7996
7997 Constraint_Present := False;
7998 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
7999 end if;
8000 end if;
8001
8002 -- STEP 0b: If needed, apply transformation given in point 5. above
8003
8004 if not Private_Extension
8005 and then Has_Discriminants (Parent_Type)
8006 and then not Discriminant_Specs
8007 and then (Is_Constrained (Parent_Type) or else Constraint_Present)
8008 then
8009 -- First, we must analyze the constraint (see comment in point 5.)
8010 -- The constraint may come from the subtype indication of the full
8011 -- declaration.
8012
8013 if Constraint_Present then
8014 New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
8015
8016 -- If there is no explicit constraint, there might be one that is
8017 -- inherited from a constrained parent type. In that case verify that
8018 -- it conforms to the constraint in the partial view. In perverse
8019 -- cases the parent subtypes of the partial and full view can have
8020 -- different constraints.
8021
8022 elsif Present (Stored_Constraint (Parent_Type)) then
8023 New_Discrs := Stored_Constraint (Parent_Type);
8024
8025 else
8026 New_Discrs := No_Elist;
8027 end if;
8028
8029 if Has_Discriminants (Derived_Type)
8030 and then Has_Private_Declaration (Derived_Type)
8031 and then Present (Discriminant_Constraint (Derived_Type))
8032 and then Present (New_Discrs)
8033 then
8034 -- Verify that constraints of the full view statically match
8035 -- those given in the partial view.
8036
8037 declare
8038 C1, C2 : Elmt_Id;
8039
8040 begin
8041 C1 := First_Elmt (New_Discrs);
8042 C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
8043 while Present (C1) and then Present (C2) loop
8044 if Fully_Conformant_Expressions (Node (C1), Node (C2))
8045 or else
8046 (Is_OK_Static_Expression (Node (C1))
8047 and then Is_OK_Static_Expression (Node (C2))
8048 and then
8049 Expr_Value (Node (C1)) = Expr_Value (Node (C2)))
8050 then
8051 null;
8052
8053 else
8054 if Constraint_Present then
8055 Error_Msg_N
8056 ("constraint not conformant to previous declaration",
8057 Node (C1));
8058 else
8059 Error_Msg_N
8060 ("constraint of full view is incompatible "
8061 & "with partial view", N);
8062 end if;
8063 end if;
8064
8065 Next_Elmt (C1);
8066 Next_Elmt (C2);
8067 end loop;
8068 end;
8069 end if;
8070
8071 -- Insert and analyze the declaration for the unconstrained base type
8072
8073 New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
8074
8075 New_Decl :=
8076 Make_Full_Type_Declaration (Loc,
8077 Defining_Identifier => New_Base,
8078 Type_Definition =>
8079 Make_Derived_Type_Definition (Loc,
8080 Abstract_Present => Abstract_Present (Type_Def),
8081 Limited_Present => Limited_Present (Type_Def),
8082 Subtype_Indication =>
8083 New_Occurrence_Of (Parent_Base, Loc),
8084 Record_Extension_Part =>
8085 Relocate_Node (Record_Extension_Part (Type_Def)),
8086 Interface_List => Interface_List (Type_Def)));
8087
8088 Set_Parent (New_Decl, Parent (N));
8089 Mark_Rewrite_Insertion (New_Decl);
8090 Insert_Before (N, New_Decl);
8091
8092 -- In the extension case, make sure ancestor is frozen appropriately
8093 -- (see also non-discriminated case below).
8094
8095 if Present (Record_Extension_Part (Type_Def))
8096 or else Is_Interface (Parent_Base)
8097 then
8098 Freeze_Before (New_Decl, Parent_Type);
8099 end if;
8100
8101 -- Note that this call passes False for the Derive_Subps parameter
8102 -- because subprogram derivation is deferred until after creating
8103 -- the subtype (see below).
8104
8105 Build_Derived_Type
8106 (New_Decl, Parent_Base, New_Base,
8107 Is_Completion => False, Derive_Subps => False);
8108
8109 -- ??? This needs re-examination to determine whether the
8110 -- above call can simply be replaced by a call to Analyze.
8111
8112 Set_Analyzed (New_Decl);
8113
8114 -- Insert and analyze the declaration for the constrained subtype
8115
8116 if Constraint_Present then
8117 New_Indic :=
8118 Make_Subtype_Indication (Loc,
8119 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
8120 Constraint => Relocate_Node (Constraint (Indic)));
8121
8122 else
8123 declare
8124 Constr_List : constant List_Id := New_List;
8125 C : Elmt_Id;
8126 Expr : Node_Id;
8127
8128 begin
8129 C := First_Elmt (Discriminant_Constraint (Parent_Type));
8130 while Present (C) loop
8131 Expr := Node (C);
8132
8133 -- It is safe here to call New_Copy_Tree since we called
8134 -- Force_Evaluation on each constraint previously
8135 -- in Build_Discriminant_Constraints.
8136
8137 Append (New_Copy_Tree (Expr), To => Constr_List);
8138
8139 Next_Elmt (C);
8140 end loop;
8141
8142 New_Indic :=
8143 Make_Subtype_Indication (Loc,
8144 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
8145 Constraint =>
8146 Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
8147 end;
8148 end if;
8149
8150 Rewrite (N,
8151 Make_Subtype_Declaration (Loc,
8152 Defining_Identifier => Derived_Type,
8153 Subtype_Indication => New_Indic));
8154
8155 Analyze (N);
8156
8157 -- Derivation of subprograms must be delayed until the full subtype
8158 -- has been established, to ensure proper overriding of subprograms
8159 -- inherited by full types. If the derivations occurred as part of
8160 -- the call to Build_Derived_Type above, then the check for type
8161 -- conformance would fail because earlier primitive subprograms
8162 -- could still refer to the full type prior the change to the new
8163 -- subtype and hence would not match the new base type created here.
8164 -- Subprograms are not derived, however, when Derive_Subps is False
8165 -- (since otherwise there could be redundant derivations).
8166
8167 if Derive_Subps then
8168 Derive_Subprograms (Parent_Type, Derived_Type);
8169 end if;
8170
8171 -- For tagged types the Discriminant_Constraint of the new base itype
8172 -- is inherited from the first subtype so that no subtype conformance
8173 -- problem arise when the first subtype overrides primitive
8174 -- operations inherited by the implicit base type.
8175
8176 if Is_Tagged then
8177 Set_Discriminant_Constraint
8178 (New_Base, Discriminant_Constraint (Derived_Type));
8179 end if;
8180
8181 return;
8182 end if;
8183
8184 -- If we get here Derived_Type will have no discriminants or it will be
8185 -- a discriminated unconstrained base type.
8186
8187 -- STEP 1a: perform preliminary actions/checks for derived tagged types
8188
8189 if Is_Tagged then
8190
8191 -- The parent type is frozen for non-private extensions (RM 13.14(7))
8192 -- The declaration of a specific descendant of an interface type
8193 -- freezes the interface type (RM 13.14).
8194
8195 if not Private_Extension or else Is_Interface (Parent_Base) then
8196 Freeze_Before (N, Parent_Type);
8197 end if;
8198
8199 -- In Ada 2005 (AI-344), the restriction that a derived tagged type
8200 -- cannot be declared at a deeper level than its parent type is
8201 -- removed. The check on derivation within a generic body is also
8202 -- relaxed, but there's a restriction that a derived tagged type
8203 -- cannot be declared in a generic body if it's derived directly
8204 -- or indirectly from a formal type of that generic.
8205
8206 if Ada_Version >= Ada_2005 then
8207 if Present (Enclosing_Generic_Body (Derived_Type)) then
8208 declare
8209 Ancestor_Type : Entity_Id;
8210
8211 begin
8212 -- Check to see if any ancestor of the derived type is a
8213 -- formal type.
8214
8215 Ancestor_Type := Parent_Type;
8216 while not Is_Generic_Type (Ancestor_Type)
8217 and then Etype (Ancestor_Type) /= Ancestor_Type
8218 loop
8219 Ancestor_Type := Etype (Ancestor_Type);
8220 end loop;
8221
8222 -- If the derived type does have a formal type as an
8223 -- ancestor, then it's an error if the derived type is
8224 -- declared within the body of the generic unit that
8225 -- declares the formal type in its generic formal part. It's
8226 -- sufficient to check whether the ancestor type is declared
8227 -- inside the same generic body as the derived type (such as
8228 -- within a nested generic spec), in which case the
8229 -- derivation is legal. If the formal type is declared
8230 -- outside of that generic body, then it's guaranteed that
8231 -- the derived type is declared within the generic body of
8232 -- the generic unit declaring the formal type.
8233
8234 if Is_Generic_Type (Ancestor_Type)
8235 and then Enclosing_Generic_Body (Ancestor_Type) /=
8236 Enclosing_Generic_Body (Derived_Type)
8237 then
8238 Error_Msg_NE
8239 ("parent type of& must not be descendant of formal type"
8240 & " of an enclosing generic body",
8241 Indic, Derived_Type);
8242 end if;
8243 end;
8244 end if;
8245
8246 elsif Type_Access_Level (Derived_Type) /=
8247 Type_Access_Level (Parent_Type)
8248 and then not Is_Generic_Type (Derived_Type)
8249 then
8250 if Is_Controlled (Parent_Type) then
8251 Error_Msg_N
8252 ("controlled type must be declared at the library level",
8253 Indic);
8254 else
8255 Error_Msg_N
8256 ("type extension at deeper accessibility level than parent",
8257 Indic);
8258 end if;
8259
8260 else
8261 declare
8262 GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
8263 begin
8264 if Present (GB)
8265 and then GB /= Enclosing_Generic_Body (Parent_Base)
8266 then
8267 Error_Msg_NE
8268 ("parent type of& must not be outside generic body"
8269 & " (RM 3.9.1(4))",
8270 Indic, Derived_Type);
8271 end if;
8272 end;
8273 end if;
8274 end if;
8275
8276 -- Ada 2005 (AI-251)
8277
8278 if Ada_Version >= Ada_2005 and then Is_Tagged then
8279
8280 -- "The declaration of a specific descendant of an interface type
8281 -- freezes the interface type" (RM 13.14).
8282
8283 declare
8284 Iface : Node_Id;
8285 begin
8286 if Is_Non_Empty_List (Interface_List (Type_Def)) then
8287 Iface := First (Interface_List (Type_Def));
8288 while Present (Iface) loop
8289 Freeze_Before (N, Etype (Iface));
8290 Next (Iface);
8291 end loop;
8292 end if;
8293 end;
8294 end if;
8295
8296 -- STEP 1b : preliminary cleanup of the full view of private types
8297
8298 -- If the type is already marked as having discriminants, then it's the
8299 -- completion of a private type or private extension and we need to
8300 -- retain the discriminants from the partial view if the current
8301 -- declaration has Discriminant_Specifications so that we can verify
8302 -- conformance. However, we must remove any existing components that
8303 -- were inherited from the parent (and attached in Copy_And_Swap)
8304 -- because the full type inherits all appropriate components anyway, and
8305 -- we do not want the partial view's components interfering.
8306
8307 if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
8308 Discrim := First_Discriminant (Derived_Type);
8309 loop
8310 Last_Discrim := Discrim;
8311 Next_Discriminant (Discrim);
8312 exit when No (Discrim);
8313 end loop;
8314
8315 Set_Last_Entity (Derived_Type, Last_Discrim);
8316
8317 -- In all other cases wipe out the list of inherited components (even
8318 -- inherited discriminants), it will be properly rebuilt here.
8319
8320 else
8321 Set_First_Entity (Derived_Type, Empty);
8322 Set_Last_Entity (Derived_Type, Empty);
8323 end if;
8324
8325 -- STEP 1c: Initialize some flags for the Derived_Type
8326
8327 -- The following flags must be initialized here so that
8328 -- Process_Discriminants can check that discriminants of tagged types do
8329 -- not have a default initial value and that access discriminants are
8330 -- only specified for limited records. For completeness, these flags are
8331 -- also initialized along with all the other flags below.
8332
8333 -- AI-419: Limitedness is not inherited from an interface parent, so to
8334 -- be limited in that case the type must be explicitly declared as
8335 -- limited. However, task and protected interfaces are always limited.
8336
8337 if Limited_Present (Type_Def) then
8338 Set_Is_Limited_Record (Derived_Type);
8339
8340 elsif Is_Limited_Record (Parent_Type)
8341 or else (Present (Full_View (Parent_Type))
8342 and then Is_Limited_Record (Full_View (Parent_Type)))
8343 then
8344 if not Is_Interface (Parent_Type)
8345 or else Is_Synchronized_Interface (Parent_Type)
8346 or else Is_Protected_Interface (Parent_Type)
8347 or else Is_Task_Interface (Parent_Type)
8348 then
8349 Set_Is_Limited_Record (Derived_Type);
8350 end if;
8351 end if;
8352
8353 -- STEP 2a: process discriminants of derived type if any
8354
8355 Push_Scope (Derived_Type);
8356
8357 if Discriminant_Specs then
8358 Set_Has_Unknown_Discriminants (Derived_Type, False);
8359
8360 -- The following call initializes fields Has_Discriminants and
8361 -- Discriminant_Constraint, unless we are processing the completion
8362 -- of a private type declaration.
8363
8364 Check_Or_Process_Discriminants (N, Derived_Type);
8365
8366 -- For untagged types, the constraint on the Parent_Type must be
8367 -- present and is used to rename the discriminants.
8368
8369 if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
8370 Error_Msg_N ("untagged parent must have discriminants", Indic);
8371
8372 elsif not Is_Tagged and then not Constraint_Present then
8373 Error_Msg_N
8374 ("discriminant constraint needed for derived untagged records",
8375 Indic);
8376
8377 -- Otherwise the parent subtype must be constrained unless we have a
8378 -- private extension.
8379
8380 elsif not Constraint_Present
8381 and then not Private_Extension
8382 and then not Is_Constrained (Parent_Type)
8383 then
8384 Error_Msg_N
8385 ("unconstrained type not allowed in this context", Indic);
8386
8387 elsif Constraint_Present then
8388 -- The following call sets the field Corresponding_Discriminant
8389 -- for the discriminants in the Derived_Type.
8390
8391 Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
8392
8393 -- For untagged types all new discriminants must rename
8394 -- discriminants in the parent. For private extensions new
8395 -- discriminants cannot rename old ones (implied by [7.3(13)]).
8396
8397 Discrim := First_Discriminant (Derived_Type);
8398 while Present (Discrim) loop
8399 if not Is_Tagged
8400 and then No (Corresponding_Discriminant (Discrim))
8401 then
8402 Error_Msg_N
8403 ("new discriminants must constrain old ones", Discrim);
8404
8405 elsif Private_Extension
8406 and then Present (Corresponding_Discriminant (Discrim))
8407 then
8408 Error_Msg_N
8409 ("only static constraints allowed for parent"
8410 & " discriminants in the partial view", Indic);
8411 exit;
8412 end if;
8413
8414 -- If a new discriminant is used in the constraint, then its
8415 -- subtype must be statically compatible with the parent
8416 -- discriminant's subtype (3.7(15)).
8417
8418 -- However, if the record contains an array constrained by
8419 -- the discriminant but with some different bound, the compiler
8420 -- attemps to create a smaller range for the discriminant type.
8421 -- (See exp_ch3.Adjust_Discriminants). In this case, where
8422 -- the discriminant type is a scalar type, the check must use
8423 -- the original discriminant type in the parent declaration.
8424
8425 declare
8426 Corr_Disc : constant Entity_Id :=
8427 Corresponding_Discriminant (Discrim);
8428 Disc_Type : constant Entity_Id := Etype (Discrim);
8429 Corr_Type : Entity_Id;
8430
8431 begin
8432 if Present (Corr_Disc) then
8433 if Is_Scalar_Type (Disc_Type) then
8434 Corr_Type :=
8435 Entity (Discriminant_Type (Parent (Corr_Disc)));
8436 else
8437 Corr_Type := Etype (Corr_Disc);
8438 end if;
8439
8440 if not
8441 Subtypes_Statically_Compatible (Disc_Type, Corr_Type)
8442 then
8443 Error_Msg_N
8444 ("subtype must be compatible "
8445 & "with parent discriminant",
8446 Discrim);
8447 end if;
8448 end if;
8449 end;
8450
8451 Next_Discriminant (Discrim);
8452 end loop;
8453
8454 -- Check whether the constraints of the full view statically
8455 -- match those imposed by the parent subtype [7.3(13)].
8456
8457 if Present (Stored_Constraint (Derived_Type)) then
8458 declare
8459 C1, C2 : Elmt_Id;
8460
8461 begin
8462 C1 := First_Elmt (Discs);
8463 C2 := First_Elmt (Stored_Constraint (Derived_Type));
8464 while Present (C1) and then Present (C2) loop
8465 if not
8466 Fully_Conformant_Expressions (Node (C1), Node (C2))
8467 then
8468 Error_Msg_N
8469 ("not conformant with previous declaration",
8470 Node (C1));
8471 end if;
8472
8473 Next_Elmt (C1);
8474 Next_Elmt (C2);
8475 end loop;
8476 end;
8477 end if;
8478 end if;
8479
8480 -- STEP 2b: No new discriminants, inherit discriminants if any
8481
8482 else
8483 if Private_Extension then
8484 Set_Has_Unknown_Discriminants
8485 (Derived_Type,
8486 Has_Unknown_Discriminants (Parent_Type)
8487 or else Unknown_Discriminants_Present (N));
8488
8489 -- The partial view of the parent may have unknown discriminants,
8490 -- but if the full view has discriminants and the parent type is
8491 -- in scope they must be inherited.
8492
8493 elsif Has_Unknown_Discriminants (Parent_Type)
8494 and then
8495 (not Has_Discriminants (Parent_Type)
8496 or else not In_Open_Scopes (Scope (Parent_Type)))
8497 then
8498 Set_Has_Unknown_Discriminants (Derived_Type);
8499 end if;
8500
8501 if not Has_Unknown_Discriminants (Derived_Type)
8502 and then not Has_Unknown_Discriminants (Parent_Base)
8503 and then Has_Discriminants (Parent_Type)
8504 then
8505 Inherit_Discrims := True;
8506 Set_Has_Discriminants
8507 (Derived_Type, True);
8508 Set_Discriminant_Constraint
8509 (Derived_Type, Discriminant_Constraint (Parent_Base));
8510 end if;
8511
8512 -- The following test is true for private types (remember
8513 -- transformation 5. is not applied to those) and in an error
8514 -- situation.
8515
8516 if Constraint_Present then
8517 Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
8518 end if;
8519
8520 -- For now mark a new derived type as constrained only if it has no
8521 -- discriminants. At the end of Build_Derived_Record_Type we properly
8522 -- set this flag in the case of private extensions. See comments in
8523 -- point 9. just before body of Build_Derived_Record_Type.
8524
8525 Set_Is_Constrained
8526 (Derived_Type,
8527 not (Inherit_Discrims
8528 or else Has_Unknown_Discriminants (Derived_Type)));
8529 end if;
8530
8531 -- STEP 3: initialize fields of derived type
8532
8533 Set_Is_Tagged_Type (Derived_Type, Is_Tagged);
8534 Set_Stored_Constraint (Derived_Type, No_Elist);
8535
8536 -- Ada 2005 (AI-251): Private type-declarations can implement interfaces
8537 -- but cannot be interfaces
8538
8539 if not Private_Extension
8540 and then Ekind (Derived_Type) /= E_Private_Type
8541 and then Ekind (Derived_Type) /= E_Limited_Private_Type
8542 then
8543 if Interface_Present (Type_Def) then
8544 Analyze_Interface_Declaration (Derived_Type, Type_Def);
8545 end if;
8546
8547 Set_Interfaces (Derived_Type, No_Elist);
8548 end if;
8549
8550 -- Fields inherited from the Parent_Type
8551
8552 Set_Has_Specified_Layout
8553 (Derived_Type, Has_Specified_Layout (Parent_Type));
8554 Set_Is_Limited_Composite
8555 (Derived_Type, Is_Limited_Composite (Parent_Type));
8556 Set_Is_Private_Composite
8557 (Derived_Type, Is_Private_Composite (Parent_Type));
8558
8559 if Is_Tagged_Type (Parent_Type) then
8560 Set_No_Tagged_Streams_Pragma
8561 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
8562 end if;
8563
8564 -- Fields inherited from the Parent_Base
8565
8566 Set_Has_Controlled_Component
8567 (Derived_Type, Has_Controlled_Component (Parent_Base));
8568 Set_Has_Non_Standard_Rep
8569 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
8570 Set_Has_Primitive_Operations
8571 (Derived_Type, Has_Primitive_Operations (Parent_Base));
8572
8573 -- Fields inherited from the Parent_Base in the non-private case
8574
8575 if Ekind (Derived_Type) = E_Record_Type then
8576 Set_Has_Complex_Representation
8577 (Derived_Type, Has_Complex_Representation (Parent_Base));
8578 end if;
8579
8580 -- Fields inherited from the Parent_Base for record types
8581
8582 if Is_Record_Type (Derived_Type) then
8583 declare
8584 Parent_Full : Entity_Id;
8585
8586 begin
8587 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
8588 -- Parent_Base can be a private type or private extension. Go
8589 -- to the full view here to get the E_Record_Type specific flags.
8590
8591 if Present (Full_View (Parent_Base)) then
8592 Parent_Full := Full_View (Parent_Base);
8593 else
8594 Parent_Full := Parent_Base;
8595 end if;
8596
8597 Set_OK_To_Reorder_Components
8598 (Derived_Type, OK_To_Reorder_Components (Parent_Full));
8599 end;
8600 end if;
8601
8602 -- Set fields for private derived types
8603
8604 if Is_Private_Type (Derived_Type) then
8605 Set_Depends_On_Private (Derived_Type, True);
8606 Set_Private_Dependents (Derived_Type, New_Elmt_List);
8607
8608 -- Inherit fields from non private record types. If this is the
8609 -- completion of a derivation from a private type, the parent itself
8610 -- is private, and the attributes come from its full view, which must
8611 -- be present.
8612
8613 else
8614 if Is_Private_Type (Parent_Base)
8615 and then not Is_Record_Type (Parent_Base)
8616 then
8617 Set_Component_Alignment
8618 (Derived_Type, Component_Alignment (Full_View (Parent_Base)));
8619 Set_C_Pass_By_Copy
8620 (Derived_Type, C_Pass_By_Copy (Full_View (Parent_Base)));
8621 else
8622 Set_Component_Alignment
8623 (Derived_Type, Component_Alignment (Parent_Base));
8624 Set_C_Pass_By_Copy
8625 (Derived_Type, C_Pass_By_Copy (Parent_Base));
8626 end if;
8627 end if;
8628
8629 -- Set fields for tagged types
8630
8631 if Is_Tagged then
8632 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
8633
8634 -- All tagged types defined in Ada.Finalization are controlled
8635
8636 if Chars (Scope (Derived_Type)) = Name_Finalization
8637 and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
8638 and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
8639 then
8640 Set_Is_Controlled (Derived_Type);
8641 else
8642 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Base));
8643 end if;
8644
8645 -- Minor optimization: there is no need to generate the class-wide
8646 -- entity associated with an underlying record view.
8647
8648 if not Is_Underlying_Record_View (Derived_Type) then
8649 Make_Class_Wide_Type (Derived_Type);
8650 end if;
8651
8652 Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
8653
8654 if Has_Discriminants (Derived_Type)
8655 and then Constraint_Present
8656 then
8657 Set_Stored_Constraint
8658 (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
8659 end if;
8660
8661 if Ada_Version >= Ada_2005 then
8662 declare
8663 Ifaces_List : Elist_Id;
8664
8665 begin
8666 -- Checks rules 3.9.4 (13/2 and 14/2)
8667
8668 if Comes_From_Source (Derived_Type)
8669 and then not Is_Private_Type (Derived_Type)
8670 and then Is_Interface (Parent_Type)
8671 and then not Is_Interface (Derived_Type)
8672 then
8673 if Is_Task_Interface (Parent_Type) then
8674 Error_Msg_N
8675 ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
8676 Derived_Type);
8677
8678 elsif Is_Protected_Interface (Parent_Type) then
8679 Error_Msg_N
8680 ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
8681 Derived_Type);
8682 end if;
8683 end if;
8684
8685 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
8686
8687 Check_Interfaces (N, Type_Def);
8688
8689 -- Ada 2005 (AI-251): Collect the list of progenitors that are
8690 -- not already in the parents.
8691
8692 Collect_Interfaces
8693 (T => Derived_Type,
8694 Ifaces_List => Ifaces_List,
8695 Exclude_Parents => True);
8696
8697 Set_Interfaces (Derived_Type, Ifaces_List);
8698
8699 -- If the derived type is the anonymous type created for
8700 -- a declaration whose parent has a constraint, propagate
8701 -- the interface list to the source type. This must be done
8702 -- prior to the completion of the analysis of the source type
8703 -- because the components in the extension may contain current
8704 -- instances whose legality depends on some ancestor.
8705
8706 if Is_Itype (Derived_Type) then
8707 declare
8708 Def : constant Node_Id :=
8709 Associated_Node_For_Itype (Derived_Type);
8710 begin
8711 if Present (Def)
8712 and then Nkind (Def) = N_Full_Type_Declaration
8713 then
8714 Set_Interfaces
8715 (Defining_Identifier (Def), Ifaces_List);
8716 end if;
8717 end;
8718 end if;
8719
8720 -- Propagate inherited invariant information of parents
8721 -- and progenitors
8722
8723 if Ada_Version >= Ada_2012
8724 and then not Is_Interface (Derived_Type)
8725 then
8726 if Has_Inheritable_Invariants (Parent_Type) then
8727 Set_Has_Invariants (Derived_Type);
8728 Set_Has_Inheritable_Invariants (Derived_Type);
8729
8730 elsif not Is_Empty_Elmt_List (Ifaces_List) then
8731 declare
8732 AI : Elmt_Id;
8733
8734 begin
8735 AI := First_Elmt (Ifaces_List);
8736 while Present (AI) loop
8737 if Has_Inheritable_Invariants (Node (AI)) then
8738 Set_Has_Invariants (Derived_Type);
8739 Set_Has_Inheritable_Invariants (Derived_Type);
8740
8741 exit;
8742 end if;
8743
8744 Next_Elmt (AI);
8745 end loop;
8746 end;
8747 end if;
8748 end if;
8749
8750 -- A type extension is automatically Ghost when one of its
8751 -- progenitors is Ghost (SPARK RM 6.9(9)). This property is
8752 -- also inherited when the parent type is Ghost, but this is
8753 -- done in Build_Derived_Type as the mechanism also handles
8754 -- untagged derivations.
8755
8756 if Implements_Ghost_Interface (Derived_Type) then
8757 Set_Is_Ghost_Entity (Derived_Type);
8758 end if;
8759 end;
8760 end if;
8761
8762 else
8763 Set_Is_Packed (Derived_Type, Is_Packed (Parent_Base));
8764 Set_Has_Non_Standard_Rep
8765 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
8766 end if;
8767
8768 -- STEP 4: Inherit components from the parent base and constrain them.
8769 -- Apply the second transformation described in point 6. above.
8770
8771 if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
8772 or else not Has_Discriminants (Parent_Type)
8773 or else not Is_Constrained (Parent_Type)
8774 then
8775 Constrs := Discs;
8776 else
8777 Constrs := Discriminant_Constraint (Parent_Type);
8778 end if;
8779
8780 Assoc_List :=
8781 Inherit_Components
8782 (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
8783
8784 -- STEP 5a: Copy the parent record declaration for untagged types
8785
8786 if not Is_Tagged then
8787
8788 -- Discriminant_Constraint (Derived_Type) has been properly
8789 -- constructed. Save it and temporarily set it to Empty because we
8790 -- do not want the call to New_Copy_Tree below to mess this list.
8791
8792 if Has_Discriminants (Derived_Type) then
8793 Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
8794 Set_Discriminant_Constraint (Derived_Type, No_Elist);
8795 else
8796 Save_Discr_Constr := No_Elist;
8797 end if;
8798
8799 -- Save the Etype field of Derived_Type. It is correctly set now,
8800 -- but the call to New_Copy tree may remap it to point to itself,
8801 -- which is not what we want. Ditto for the Next_Entity field.
8802
8803 Save_Etype := Etype (Derived_Type);
8804 Save_Next_Entity := Next_Entity (Derived_Type);
8805
8806 -- Assoc_List maps all stored discriminants in the Parent_Base to
8807 -- stored discriminants in the Derived_Type. It is fundamental that
8808 -- no types or itypes with discriminants other than the stored
8809 -- discriminants appear in the entities declared inside
8810 -- Derived_Type, since the back end cannot deal with it.
8811
8812 New_Decl :=
8813 New_Copy_Tree
8814 (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
8815
8816 -- Restore the fields saved prior to the New_Copy_Tree call
8817 -- and compute the stored constraint.
8818
8819 Set_Etype (Derived_Type, Save_Etype);
8820 Set_Next_Entity (Derived_Type, Save_Next_Entity);
8821
8822 if Has_Discriminants (Derived_Type) then
8823 Set_Discriminant_Constraint
8824 (Derived_Type, Save_Discr_Constr);
8825 Set_Stored_Constraint
8826 (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
8827 Replace_Components (Derived_Type, New_Decl);
8828 Set_Has_Implicit_Dereference
8829 (Derived_Type, Has_Implicit_Dereference (Parent_Type));
8830 end if;
8831
8832 -- Insert the new derived type declaration
8833
8834 Rewrite (N, New_Decl);
8835
8836 -- STEP 5b: Complete the processing for record extensions in generics
8837
8838 -- There is no completion for record extensions declared in the
8839 -- parameter part of a generic, so we need to complete processing for
8840 -- these generic record extensions here. The Record_Type_Definition call
8841 -- will change the Ekind of the components from E_Void to E_Component.
8842
8843 elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
8844 Record_Type_Definition (Empty, Derived_Type);
8845
8846 -- STEP 5c: Process the record extension for non private tagged types
8847
8848 elsif not Private_Extension then
8849 Expand_Record_Extension (Derived_Type, Type_Def);
8850
8851 -- Note : previously in ASIS mode we set the Parent_Subtype of the
8852 -- derived type to propagate some semantic information. This led
8853 -- to other ASIS failures and has been removed.
8854
8855 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
8856 -- implemented interfaces if we are in expansion mode
8857
8858 if Expander_Active
8859 and then Has_Interfaces (Derived_Type)
8860 then
8861 Add_Interface_Tag_Components (N, Derived_Type);
8862 end if;
8863
8864 -- Analyze the record extension
8865
8866 Record_Type_Definition
8867 (Record_Extension_Part (Type_Def), Derived_Type);
8868 end if;
8869
8870 End_Scope;
8871
8872 -- Nothing else to do if there is an error in the derivation.
8873 -- An unusual case: the full view may be derived from a type in an
8874 -- instance, when the partial view was used illegally as an actual
8875 -- in that instance, leading to a circular definition.
8876
8877 if Etype (Derived_Type) = Any_Type
8878 or else Etype (Parent_Type) = Derived_Type
8879 then
8880 return;
8881 end if;
8882
8883 -- Set delayed freeze and then derive subprograms, we need to do
8884 -- this in this order so that derived subprograms inherit the
8885 -- derived freeze if necessary.
8886
8887 Set_Has_Delayed_Freeze (Derived_Type);
8888
8889 if Derive_Subps then
8890 Derive_Subprograms (Parent_Type, Derived_Type);
8891 end if;
8892
8893 -- If we have a private extension which defines a constrained derived
8894 -- type mark as constrained here after we have derived subprograms. See
8895 -- comment on point 9. just above the body of Build_Derived_Record_Type.
8896
8897 if Private_Extension and then Inherit_Discrims then
8898 if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
8899 Set_Is_Constrained (Derived_Type, True);
8900 Set_Discriminant_Constraint (Derived_Type, Discs);
8901
8902 elsif Is_Constrained (Parent_Type) then
8903 Set_Is_Constrained
8904 (Derived_Type, True);
8905 Set_Discriminant_Constraint
8906 (Derived_Type, Discriminant_Constraint (Parent_Type));
8907 end if;
8908 end if;
8909
8910 -- Update the class-wide type, which shares the now-completed entity
8911 -- list with its specific type. In case of underlying record views,
8912 -- we do not generate the corresponding class wide entity.
8913
8914 if Is_Tagged
8915 and then not Is_Underlying_Record_View (Derived_Type)
8916 then
8917 Set_First_Entity
8918 (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
8919 Set_Last_Entity
8920 (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
8921 end if;
8922
8923 Check_Function_Writable_Actuals (N);
8924 end Build_Derived_Record_Type;
8925
8926 ------------------------
8927 -- Build_Derived_Type --
8928 ------------------------
8929
8930 procedure Build_Derived_Type
8931 (N : Node_Id;
8932 Parent_Type : Entity_Id;
8933 Derived_Type : Entity_Id;
8934 Is_Completion : Boolean;
8935 Derive_Subps : Boolean := True)
8936 is
8937 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
8938
8939 begin
8940 -- Set common attributes
8941
8942 Set_Scope (Derived_Type, Current_Scope);
8943
8944 Set_Etype (Derived_Type, Parent_Base);
8945 Set_Ekind (Derived_Type, Ekind (Parent_Base));
8946 Set_Has_Task (Derived_Type, Has_Task (Parent_Base));
8947 Set_Has_Protected (Derived_Type, Has_Protected (Parent_Base));
8948
8949 Set_Size_Info (Derived_Type, Parent_Type);
8950 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
8951 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
8952 Set_Is_Tagged_Type (Derived_Type, Is_Tagged_Type (Parent_Type));
8953 Set_Is_Volatile (Derived_Type, Is_Volatile (Parent_Type));
8954
8955 if Is_Tagged_Type (Derived_Type) then
8956 Set_No_Tagged_Streams_Pragma
8957 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
8958 end if;
8959
8960 -- If the parent has primitive routines, set the derived type link
8961
8962 if Has_Primitive_Operations (Parent_Type) then
8963 Set_Derived_Type_Link (Parent_Base, Derived_Type);
8964 end if;
8965
8966 -- If the parent type is a private subtype, the convention on the base
8967 -- type may be set in the private part, and not propagated to the
8968 -- subtype until later, so we obtain the convention from the base type.
8969
8970 Set_Convention (Derived_Type, Convention (Parent_Base));
8971
8972 -- Set SSO default for record or array type
8973
8974 if (Is_Array_Type (Derived_Type) or else Is_Record_Type (Derived_Type))
8975 and then Is_Base_Type (Derived_Type)
8976 then
8977 Set_Default_SSO (Derived_Type);
8978 end if;
8979
8980 -- Propagate invariant information. The new type has invariants if
8981 -- they are inherited from the parent type, and these invariants can
8982 -- be further inherited, so both flags are set.
8983
8984 -- We similarly inherit predicates
8985
8986 if Has_Predicates (Parent_Type) then
8987 Set_Has_Predicates (Derived_Type);
8988 end if;
8989
8990 -- The derived type inherits the representation clauses of the parent
8991
8992 Inherit_Rep_Item_Chain (Derived_Type, Parent_Type);
8993
8994 -- Propagate the attributes related to pragma Default_Initial_Condition
8995 -- from the parent type to the private extension. A derived type always
8996 -- inherits the default initial condition flag from the parent type. If
8997 -- the derived type carries its own Default_Initial_Condition pragma,
8998 -- the flag is later reset in Analyze_Pragma. Note that both flags are
8999 -- mutually exclusive.
9000
9001 Propagate_Default_Init_Cond_Attributes
9002 (From_Typ => Parent_Type,
9003 To_Typ => Derived_Type,
9004 Parent_To_Derivation => True);
9005
9006 -- If the parent type has delayed rep aspects, then mark the derived
9007 -- type as possibly inheriting a delayed rep aspect.
9008
9009 if Has_Delayed_Rep_Aspects (Parent_Type) then
9010 Set_May_Inherit_Delayed_Rep_Aspects (Derived_Type);
9011 end if;
9012
9013 -- Propagate the attributes related to pragma Ghost from the parent type
9014 -- to the derived type or type extension (SPARK RM 6.9(9)).
9015
9016 if Is_Ghost_Entity (Parent_Type) then
9017 Set_Is_Ghost_Entity (Derived_Type);
9018 end if;
9019
9020 -- Type dependent processing
9021
9022 case Ekind (Parent_Type) is
9023 when Numeric_Kind =>
9024 Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
9025
9026 when Array_Kind =>
9027 Build_Derived_Array_Type (N, Parent_Type, Derived_Type);
9028
9029 when E_Record_Type
9030 | E_Record_Subtype
9031 | Class_Wide_Kind =>
9032 Build_Derived_Record_Type
9033 (N, Parent_Type, Derived_Type, Derive_Subps);
9034 return;
9035
9036 when Enumeration_Kind =>
9037 Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
9038
9039 when Access_Kind =>
9040 Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
9041
9042 when Incomplete_Or_Private_Kind =>
9043 Build_Derived_Private_Type
9044 (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
9045
9046 -- For discriminated types, the derivation includes deriving
9047 -- primitive operations. For others it is done below.
9048
9049 if Is_Tagged_Type (Parent_Type)
9050 or else Has_Discriminants (Parent_Type)
9051 or else (Present (Full_View (Parent_Type))
9052 and then Has_Discriminants (Full_View (Parent_Type)))
9053 then
9054 return;
9055 end if;
9056
9057 when Concurrent_Kind =>
9058 Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
9059
9060 when others =>
9061 raise Program_Error;
9062 end case;
9063
9064 -- Nothing more to do if some error occurred
9065
9066 if Etype (Derived_Type) = Any_Type then
9067 return;
9068 end if;
9069
9070 -- Set delayed freeze and then derive subprograms, we need to do this
9071 -- in this order so that derived subprograms inherit the derived freeze
9072 -- if necessary.
9073
9074 Set_Has_Delayed_Freeze (Derived_Type);
9075
9076 if Derive_Subps then
9077 Derive_Subprograms (Parent_Type, Derived_Type);
9078 end if;
9079
9080 Set_Has_Primitive_Operations
9081 (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
9082 end Build_Derived_Type;
9083
9084 -----------------------
9085 -- Build_Discriminal --
9086 -----------------------
9087
9088 procedure Build_Discriminal (Discrim : Entity_Id) is
9089 D_Minal : Entity_Id;
9090 CR_Disc : Entity_Id;
9091
9092 begin
9093 -- A discriminal has the same name as the discriminant
9094
9095 D_Minal := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9096
9097 Set_Ekind (D_Minal, E_In_Parameter);
9098 Set_Mechanism (D_Minal, Default_Mechanism);
9099 Set_Etype (D_Minal, Etype (Discrim));
9100 Set_Scope (D_Minal, Current_Scope);
9101
9102 Set_Discriminal (Discrim, D_Minal);
9103 Set_Discriminal_Link (D_Minal, Discrim);
9104
9105 -- For task types, build at once the discriminants of the corresponding
9106 -- record, which are needed if discriminants are used in entry defaults
9107 -- and in family bounds.
9108
9109 if Is_Concurrent_Type (Current_Scope)
9110 or else
9111 Is_Limited_Type (Current_Scope)
9112 then
9113 CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9114
9115 Set_Ekind (CR_Disc, E_In_Parameter);
9116 Set_Mechanism (CR_Disc, Default_Mechanism);
9117 Set_Etype (CR_Disc, Etype (Discrim));
9118 Set_Scope (CR_Disc, Current_Scope);
9119 Set_Discriminal_Link (CR_Disc, Discrim);
9120 Set_CR_Discriminant (Discrim, CR_Disc);
9121 end if;
9122 end Build_Discriminal;
9123
9124 ------------------------------------
9125 -- Build_Discriminant_Constraints --
9126 ------------------------------------
9127
9128 function Build_Discriminant_Constraints
9129 (T : Entity_Id;
9130 Def : Node_Id;
9131 Derived_Def : Boolean := False) return Elist_Id
9132 is
9133 C : constant Node_Id := Constraint (Def);
9134 Nb_Discr : constant Nat := Number_Discriminants (T);
9135
9136 Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
9137 -- Saves the expression corresponding to a given discriminant in T
9138
9139 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
9140 -- Return the Position number within array Discr_Expr of a discriminant
9141 -- D within the discriminant list of the discriminated type T.
9142
9143 procedure Process_Discriminant_Expression
9144 (Expr : Node_Id;
9145 D : Entity_Id);
9146 -- If this is a discriminant constraint on a partial view, do not
9147 -- generate an overflow check on the discriminant expression. The check
9148 -- will be generated when constraining the full view. Otherwise the
9149 -- backend creates duplicate symbols for the temporaries corresponding
9150 -- to the expressions to be checked, causing spurious assembler errors.
9151
9152 ------------------
9153 -- Pos_Of_Discr --
9154 ------------------
9155
9156 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
9157 Disc : Entity_Id;
9158
9159 begin
9160 Disc := First_Discriminant (T);
9161 for J in Discr_Expr'Range loop
9162 if Disc = D then
9163 return J;
9164 end if;
9165
9166 Next_Discriminant (Disc);
9167 end loop;
9168
9169 -- Note: Since this function is called on discriminants that are
9170 -- known to belong to the discriminated type, falling through the
9171 -- loop with no match signals an internal compiler error.
9172
9173 raise Program_Error;
9174 end Pos_Of_Discr;
9175
9176 -------------------------------------
9177 -- Process_Discriminant_Expression --
9178 -------------------------------------
9179
9180 procedure Process_Discriminant_Expression
9181 (Expr : Node_Id;
9182 D : Entity_Id)
9183 is
9184 BDT : constant Entity_Id := Base_Type (Etype (D));
9185
9186 begin
9187 -- If this is a discriminant constraint on a partial view, do
9188 -- not generate an overflow on the discriminant expression. The
9189 -- check will be generated when constraining the full view.
9190
9191 if Is_Private_Type (T)
9192 and then Present (Full_View (T))
9193 then
9194 Analyze_And_Resolve (Expr, BDT, Suppress => Overflow_Check);
9195 else
9196 Analyze_And_Resolve (Expr, BDT);
9197 end if;
9198 end Process_Discriminant_Expression;
9199
9200 -- Declarations local to Build_Discriminant_Constraints
9201
9202 Discr : Entity_Id;
9203 E : Entity_Id;
9204 Elist : constant Elist_Id := New_Elmt_List;
9205
9206 Constr : Node_Id;
9207 Expr : Node_Id;
9208 Id : Node_Id;
9209 Position : Nat;
9210 Found : Boolean;
9211
9212 Discrim_Present : Boolean := False;
9213
9214 -- Start of processing for Build_Discriminant_Constraints
9215
9216 begin
9217 -- The following loop will process positional associations only.
9218 -- For a positional association, the (single) discriminant is
9219 -- implicitly specified by position, in textual order (RM 3.7.2).
9220
9221 Discr := First_Discriminant (T);
9222 Constr := First (Constraints (C));
9223 for D in Discr_Expr'Range loop
9224 exit when Nkind (Constr) = N_Discriminant_Association;
9225
9226 if No (Constr) then
9227 Error_Msg_N ("too few discriminants given in constraint", C);
9228 return New_Elmt_List;
9229
9230 elsif Nkind (Constr) = N_Range
9231 or else (Nkind (Constr) = N_Attribute_Reference
9232 and then Attribute_Name (Constr) = Name_Range)
9233 then
9234 Error_Msg_N
9235 ("a range is not a valid discriminant constraint", Constr);
9236 Discr_Expr (D) := Error;
9237
9238 else
9239 Process_Discriminant_Expression (Constr, Discr);
9240 Discr_Expr (D) := Constr;
9241 end if;
9242
9243 Next_Discriminant (Discr);
9244 Next (Constr);
9245 end loop;
9246
9247 if No (Discr) and then Present (Constr) then
9248 Error_Msg_N ("too many discriminants given in constraint", Constr);
9249 return New_Elmt_List;
9250 end if;
9251
9252 -- Named associations can be given in any order, but if both positional
9253 -- and named associations are used in the same discriminant constraint,
9254 -- then positional associations must occur first, at their normal
9255 -- position. Hence once a named association is used, the rest of the
9256 -- discriminant constraint must use only named associations.
9257
9258 while Present (Constr) loop
9259
9260 -- Positional association forbidden after a named association
9261
9262 if Nkind (Constr) /= N_Discriminant_Association then
9263 Error_Msg_N ("positional association follows named one", Constr);
9264 return New_Elmt_List;
9265
9266 -- Otherwise it is a named association
9267
9268 else
9269 -- E records the type of the discriminants in the named
9270 -- association. All the discriminants specified in the same name
9271 -- association must have the same type.
9272
9273 E := Empty;
9274
9275 -- Search the list of discriminants in T to see if the simple name
9276 -- given in the constraint matches any of them.
9277
9278 Id := First (Selector_Names (Constr));
9279 while Present (Id) loop
9280 Found := False;
9281
9282 -- If Original_Discriminant is present, we are processing a
9283 -- generic instantiation and this is an instance node. We need
9284 -- to find the name of the corresponding discriminant in the
9285 -- actual record type T and not the name of the discriminant in
9286 -- the generic formal. Example:
9287
9288 -- generic
9289 -- type G (D : int) is private;
9290 -- package P is
9291 -- subtype W is G (D => 1);
9292 -- end package;
9293 -- type Rec (X : int) is record ... end record;
9294 -- package Q is new P (G => Rec);
9295
9296 -- At the point of the instantiation, formal type G is Rec
9297 -- and therefore when reanalyzing "subtype W is G (D => 1);"
9298 -- which really looks like "subtype W is Rec (D => 1);" at
9299 -- the point of instantiation, we want to find the discriminant
9300 -- that corresponds to D in Rec, i.e. X.
9301
9302 if Present (Original_Discriminant (Id))
9303 and then In_Instance
9304 then
9305 Discr := Find_Corresponding_Discriminant (Id, T);
9306 Found := True;
9307
9308 else
9309 Discr := First_Discriminant (T);
9310 while Present (Discr) loop
9311 if Chars (Discr) = Chars (Id) then
9312 Found := True;
9313 exit;
9314 end if;
9315
9316 Next_Discriminant (Discr);
9317 end loop;
9318
9319 if not Found then
9320 Error_Msg_N ("& does not match any discriminant", Id);
9321 return New_Elmt_List;
9322
9323 -- If the parent type is a generic formal, preserve the
9324 -- name of the discriminant for subsequent instances.
9325 -- see comment at the beginning of this if statement.
9326
9327 elsif Is_Generic_Type (Root_Type (T)) then
9328 Set_Original_Discriminant (Id, Discr);
9329 end if;
9330 end if;
9331
9332 Position := Pos_Of_Discr (T, Discr);
9333
9334 if Present (Discr_Expr (Position)) then
9335 Error_Msg_N ("duplicate constraint for discriminant&", Id);
9336
9337 else
9338 -- Each discriminant specified in the same named association
9339 -- must be associated with a separate copy of the
9340 -- corresponding expression.
9341
9342 if Present (Next (Id)) then
9343 Expr := New_Copy_Tree (Expression (Constr));
9344 Set_Parent (Expr, Parent (Expression (Constr)));
9345 else
9346 Expr := Expression (Constr);
9347 end if;
9348
9349 Discr_Expr (Position) := Expr;
9350 Process_Discriminant_Expression (Expr, Discr);
9351 end if;
9352
9353 -- A discriminant association with more than one discriminant
9354 -- name is only allowed if the named discriminants are all of
9355 -- the same type (RM 3.7.1(8)).
9356
9357 if E = Empty then
9358 E := Base_Type (Etype (Discr));
9359
9360 elsif Base_Type (Etype (Discr)) /= E then
9361 Error_Msg_N
9362 ("all discriminants in an association " &
9363 "must have the same type", Id);
9364 end if;
9365
9366 Next (Id);
9367 end loop;
9368 end if;
9369
9370 Next (Constr);
9371 end loop;
9372
9373 -- A discriminant constraint must provide exactly one value for each
9374 -- discriminant of the type (RM 3.7.1(8)).
9375
9376 for J in Discr_Expr'Range loop
9377 if No (Discr_Expr (J)) then
9378 Error_Msg_N ("too few discriminants given in constraint", C);
9379 return New_Elmt_List;
9380 end if;
9381 end loop;
9382
9383 -- Determine if there are discriminant expressions in the constraint
9384
9385 for J in Discr_Expr'Range loop
9386 if Denotes_Discriminant
9387 (Discr_Expr (J), Check_Concurrent => True)
9388 then
9389 Discrim_Present := True;
9390 end if;
9391 end loop;
9392
9393 -- Build an element list consisting of the expressions given in the
9394 -- discriminant constraint and apply the appropriate checks. The list
9395 -- is constructed after resolving any named discriminant associations
9396 -- and therefore the expressions appear in the textual order of the
9397 -- discriminants.
9398
9399 Discr := First_Discriminant (T);
9400 for J in Discr_Expr'Range loop
9401 if Discr_Expr (J) /= Error then
9402 Append_Elmt (Discr_Expr (J), Elist);
9403
9404 -- If any of the discriminant constraints is given by a
9405 -- discriminant and we are in a derived type declaration we
9406 -- have a discriminant renaming. Establish link between new
9407 -- and old discriminant.
9408
9409 if Denotes_Discriminant (Discr_Expr (J)) then
9410 if Derived_Def then
9411 Set_Corresponding_Discriminant
9412 (Entity (Discr_Expr (J)), Discr);
9413 end if;
9414
9415 -- Force the evaluation of non-discriminant expressions.
9416 -- If we have found a discriminant in the constraint 3.4(26)
9417 -- and 3.8(18) demand that no range checks are performed are
9418 -- after evaluation. If the constraint is for a component
9419 -- definition that has a per-object constraint, expressions are
9420 -- evaluated but not checked either. In all other cases perform
9421 -- a range check.
9422
9423 else
9424 if Discrim_Present then
9425 null;
9426
9427 elsif Nkind (Parent (Parent (Def))) = N_Component_Declaration
9428 and then
9429 Has_Per_Object_Constraint
9430 (Defining_Identifier (Parent (Parent (Def))))
9431 then
9432 null;
9433
9434 elsif Is_Access_Type (Etype (Discr)) then
9435 Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
9436
9437 else
9438 Apply_Range_Check (Discr_Expr (J), Etype (Discr));
9439 end if;
9440
9441 Force_Evaluation (Discr_Expr (J));
9442 end if;
9443
9444 -- Check that the designated type of an access discriminant's
9445 -- expression is not a class-wide type unless the discriminant's
9446 -- designated type is also class-wide.
9447
9448 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
9449 and then not Is_Class_Wide_Type
9450 (Designated_Type (Etype (Discr)))
9451 and then Etype (Discr_Expr (J)) /= Any_Type
9452 and then Is_Class_Wide_Type
9453 (Designated_Type (Etype (Discr_Expr (J))))
9454 then
9455 Wrong_Type (Discr_Expr (J), Etype (Discr));
9456
9457 elsif Is_Access_Type (Etype (Discr))
9458 and then not Is_Access_Constant (Etype (Discr))
9459 and then Is_Access_Type (Etype (Discr_Expr (J)))
9460 and then Is_Access_Constant (Etype (Discr_Expr (J)))
9461 then
9462 Error_Msg_NE
9463 ("constraint for discriminant& must be access to variable",
9464 Def, Discr);
9465 end if;
9466 end if;
9467
9468 Next_Discriminant (Discr);
9469 end loop;
9470
9471 return Elist;
9472 end Build_Discriminant_Constraints;
9473
9474 ---------------------------------
9475 -- Build_Discriminated_Subtype --
9476 ---------------------------------
9477
9478 procedure Build_Discriminated_Subtype
9479 (T : Entity_Id;
9480 Def_Id : Entity_Id;
9481 Elist : Elist_Id;
9482 Related_Nod : Node_Id;
9483 For_Access : Boolean := False)
9484 is
9485 Has_Discrs : constant Boolean := Has_Discriminants (T);
9486 Constrained : constant Boolean :=
9487 (Has_Discrs
9488 and then not Is_Empty_Elmt_List (Elist)
9489 and then not Is_Class_Wide_Type (T))
9490 or else Is_Constrained (T);
9491
9492 begin
9493 if Ekind (T) = E_Record_Type then
9494 if For_Access then
9495 Set_Ekind (Def_Id, E_Private_Subtype);
9496 Set_Is_For_Access_Subtype (Def_Id, True);
9497 else
9498 Set_Ekind (Def_Id, E_Record_Subtype);
9499 end if;
9500
9501 -- Inherit preelaboration flag from base, for types for which it
9502 -- may have been set: records, private types, protected types.
9503
9504 Set_Known_To_Have_Preelab_Init
9505 (Def_Id, Known_To_Have_Preelab_Init (T));
9506
9507 elsif Ekind (T) = E_Task_Type then
9508 Set_Ekind (Def_Id, E_Task_Subtype);
9509
9510 elsif Ekind (T) = E_Protected_Type then
9511 Set_Ekind (Def_Id, E_Protected_Subtype);
9512 Set_Known_To_Have_Preelab_Init
9513 (Def_Id, Known_To_Have_Preelab_Init (T));
9514
9515 elsif Is_Private_Type (T) then
9516 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
9517 Set_Known_To_Have_Preelab_Init
9518 (Def_Id, Known_To_Have_Preelab_Init (T));
9519
9520 -- Private subtypes may have private dependents
9521
9522 Set_Private_Dependents (Def_Id, New_Elmt_List);
9523
9524 elsif Is_Class_Wide_Type (T) then
9525 Set_Ekind (Def_Id, E_Class_Wide_Subtype);
9526
9527 else
9528 -- Incomplete type. Attach subtype to list of dependents, to be
9529 -- completed with full view of parent type, unless is it the
9530 -- designated subtype of a record component within an init_proc.
9531 -- This last case arises for a component of an access type whose
9532 -- designated type is incomplete (e.g. a Taft Amendment type).
9533 -- The designated subtype is within an inner scope, and needs no
9534 -- elaboration, because only the access type is needed in the
9535 -- initialization procedure.
9536
9537 Set_Ekind (Def_Id, Ekind (T));
9538
9539 if For_Access and then Within_Init_Proc then
9540 null;
9541 else
9542 Append_Elmt (Def_Id, Private_Dependents (T));
9543 end if;
9544 end if;
9545
9546 Set_Etype (Def_Id, T);
9547 Init_Size_Align (Def_Id);
9548 Set_Has_Discriminants (Def_Id, Has_Discrs);
9549 Set_Is_Constrained (Def_Id, Constrained);
9550
9551 Set_First_Entity (Def_Id, First_Entity (T));
9552 Set_Last_Entity (Def_Id, Last_Entity (T));
9553 Set_Has_Implicit_Dereference
9554 (Def_Id, Has_Implicit_Dereference (T));
9555
9556 -- If the subtype is the completion of a private declaration, there may
9557 -- have been representation clauses for the partial view, and they must
9558 -- be preserved. Build_Derived_Type chains the inherited clauses with
9559 -- the ones appearing on the extension. If this comes from a subtype
9560 -- declaration, all clauses are inherited.
9561
9562 if No (First_Rep_Item (Def_Id)) then
9563 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
9564 end if;
9565
9566 if Is_Tagged_Type (T) then
9567 Set_Is_Tagged_Type (Def_Id);
9568 Set_No_Tagged_Streams_Pragma (Def_Id, No_Tagged_Streams_Pragma (T));
9569 Make_Class_Wide_Type (Def_Id);
9570 end if;
9571
9572 Set_Stored_Constraint (Def_Id, No_Elist);
9573
9574 if Has_Discrs then
9575 Set_Discriminant_Constraint (Def_Id, Elist);
9576 Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
9577 end if;
9578
9579 if Is_Tagged_Type (T) then
9580
9581 -- Ada 2005 (AI-251): In case of concurrent types we inherit the
9582 -- concurrent record type (which has the list of primitive
9583 -- operations).
9584
9585 if Ada_Version >= Ada_2005
9586 and then Is_Concurrent_Type (T)
9587 then
9588 Set_Corresponding_Record_Type (Def_Id,
9589 Corresponding_Record_Type (T));
9590 else
9591 Set_Direct_Primitive_Operations (Def_Id,
9592 Direct_Primitive_Operations (T));
9593 end if;
9594
9595 Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
9596 end if;
9597
9598 -- Subtypes introduced by component declarations do not need to be
9599 -- marked as delayed, and do not get freeze nodes, because the semantics
9600 -- verifies that the parents of the subtypes are frozen before the
9601 -- enclosing record is frozen.
9602
9603 if not Is_Type (Scope (Def_Id)) then
9604 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
9605
9606 if Is_Private_Type (T)
9607 and then Present (Full_View (T))
9608 then
9609 Conditional_Delay (Def_Id, Full_View (T));
9610 else
9611 Conditional_Delay (Def_Id, T);
9612 end if;
9613 end if;
9614
9615 if Is_Record_Type (T) then
9616 Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
9617
9618 if Has_Discrs
9619 and then not Is_Empty_Elmt_List (Elist)
9620 and then not For_Access
9621 then
9622 Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
9623 elsif not For_Access then
9624 Set_Cloned_Subtype (Def_Id, T);
9625 end if;
9626 end if;
9627 end Build_Discriminated_Subtype;
9628
9629 ---------------------------
9630 -- Build_Itype_Reference --
9631 ---------------------------
9632
9633 procedure Build_Itype_Reference
9634 (Ityp : Entity_Id;
9635 Nod : Node_Id)
9636 is
9637 IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
9638 begin
9639
9640 -- Itype references are only created for use by the back-end
9641
9642 if Inside_A_Generic then
9643 return;
9644 else
9645 Set_Itype (IR, Ityp);
9646 Insert_After (Nod, IR);
9647 end if;
9648 end Build_Itype_Reference;
9649
9650 ------------------------
9651 -- Build_Scalar_Bound --
9652 ------------------------
9653
9654 function Build_Scalar_Bound
9655 (Bound : Node_Id;
9656 Par_T : Entity_Id;
9657 Der_T : Entity_Id) return Node_Id
9658 is
9659 New_Bound : Entity_Id;
9660
9661 begin
9662 -- Note: not clear why this is needed, how can the original bound
9663 -- be unanalyzed at this point? and if it is, what business do we
9664 -- have messing around with it? and why is the base type of the
9665 -- parent type the right type for the resolution. It probably is
9666 -- not. It is OK for the new bound we are creating, but not for
9667 -- the old one??? Still if it never happens, no problem.
9668
9669 Analyze_And_Resolve (Bound, Base_Type (Par_T));
9670
9671 if Nkind_In (Bound, N_Integer_Literal, N_Real_Literal) then
9672 New_Bound := New_Copy (Bound);
9673 Set_Etype (New_Bound, Der_T);
9674 Set_Analyzed (New_Bound);
9675
9676 elsif Is_Entity_Name (Bound) then
9677 New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
9678
9679 -- The following is almost certainly wrong. What business do we have
9680 -- relocating a node (Bound) that is presumably still attached to
9681 -- the tree elsewhere???
9682
9683 else
9684 New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
9685 end if;
9686
9687 Set_Etype (New_Bound, Der_T);
9688 return New_Bound;
9689 end Build_Scalar_Bound;
9690
9691 --------------------------------
9692 -- Build_Underlying_Full_View --
9693 --------------------------------
9694
9695 procedure Build_Underlying_Full_View
9696 (N : Node_Id;
9697 Typ : Entity_Id;
9698 Par : Entity_Id)
9699 is
9700 Loc : constant Source_Ptr := Sloc (N);
9701 Subt : constant Entity_Id :=
9702 Make_Defining_Identifier
9703 (Loc, New_External_Name (Chars (Typ), 'S'));
9704
9705 Constr : Node_Id;
9706 Indic : Node_Id;
9707 C : Node_Id;
9708 Id : Node_Id;
9709
9710 procedure Set_Discriminant_Name (Id : Node_Id);
9711 -- If the derived type has discriminants, they may rename discriminants
9712 -- of the parent. When building the full view of the parent, we need to
9713 -- recover the names of the original discriminants if the constraint is
9714 -- given by named associations.
9715
9716 ---------------------------
9717 -- Set_Discriminant_Name --
9718 ---------------------------
9719
9720 procedure Set_Discriminant_Name (Id : Node_Id) is
9721 Disc : Entity_Id;
9722
9723 begin
9724 Set_Original_Discriminant (Id, Empty);
9725
9726 if Has_Discriminants (Typ) then
9727 Disc := First_Discriminant (Typ);
9728 while Present (Disc) loop
9729 if Chars (Disc) = Chars (Id)
9730 and then Present (Corresponding_Discriminant (Disc))
9731 then
9732 Set_Chars (Id, Chars (Corresponding_Discriminant (Disc)));
9733 end if;
9734 Next_Discriminant (Disc);
9735 end loop;
9736 end if;
9737 end Set_Discriminant_Name;
9738
9739 -- Start of processing for Build_Underlying_Full_View
9740
9741 begin
9742 if Nkind (N) = N_Full_Type_Declaration then
9743 Constr := Constraint (Subtype_Indication (Type_Definition (N)));
9744
9745 elsif Nkind (N) = N_Subtype_Declaration then
9746 Constr := New_Copy_Tree (Constraint (Subtype_Indication (N)));
9747
9748 elsif Nkind (N) = N_Component_Declaration then
9749 Constr :=
9750 New_Copy_Tree
9751 (Constraint (Subtype_Indication (Component_Definition (N))));
9752
9753 else
9754 raise Program_Error;
9755 end if;
9756
9757 C := First (Constraints (Constr));
9758 while Present (C) loop
9759 if Nkind (C) = N_Discriminant_Association then
9760 Id := First (Selector_Names (C));
9761 while Present (Id) loop
9762 Set_Discriminant_Name (Id);
9763 Next (Id);
9764 end loop;
9765 end if;
9766
9767 Next (C);
9768 end loop;
9769
9770 Indic :=
9771 Make_Subtype_Declaration (Loc,
9772 Defining_Identifier => Subt,
9773 Subtype_Indication =>
9774 Make_Subtype_Indication (Loc,
9775 Subtype_Mark => New_Occurrence_Of (Par, Loc),
9776 Constraint => New_Copy_Tree (Constr)));
9777
9778 -- If this is a component subtype for an outer itype, it is not
9779 -- a list member, so simply set the parent link for analysis: if
9780 -- the enclosing type does not need to be in a declarative list,
9781 -- neither do the components.
9782
9783 if Is_List_Member (N)
9784 and then Nkind (N) /= N_Component_Declaration
9785 then
9786 Insert_Before (N, Indic);
9787 else
9788 Set_Parent (Indic, Parent (N));
9789 end if;
9790
9791 Analyze (Indic);
9792 Set_Underlying_Full_View (Typ, Full_View (Subt));
9793 end Build_Underlying_Full_View;
9794
9795 -------------------------------
9796 -- Check_Abstract_Overriding --
9797 -------------------------------
9798
9799 procedure Check_Abstract_Overriding (T : Entity_Id) is
9800 Alias_Subp : Entity_Id;
9801 Elmt : Elmt_Id;
9802 Op_List : Elist_Id;
9803 Subp : Entity_Id;
9804 Type_Def : Node_Id;
9805
9806 procedure Check_Pragma_Implemented (Subp : Entity_Id);
9807 -- Ada 2012 (AI05-0030): Subprogram Subp overrides an interface routine
9808 -- which has pragma Implemented already set. Check whether Subp's entity
9809 -- kind conforms to the implementation kind of the overridden routine.
9810
9811 procedure Check_Pragma_Implemented
9812 (Subp : Entity_Id;
9813 Iface_Subp : Entity_Id);
9814 -- Ada 2012 (AI05-0030): Subprogram Subp overrides interface routine
9815 -- Iface_Subp and both entities have pragma Implemented already set on
9816 -- them. Check whether the two implementation kinds are conforming.
9817
9818 procedure Inherit_Pragma_Implemented
9819 (Subp : Entity_Id;
9820 Iface_Subp : Entity_Id);
9821 -- Ada 2012 (AI05-0030): Interface primitive Subp overrides interface
9822 -- subprogram Iface_Subp which has been marked by pragma Implemented.
9823 -- Propagate the implementation kind of Iface_Subp to Subp.
9824
9825 ------------------------------
9826 -- Check_Pragma_Implemented --
9827 ------------------------------
9828
9829 procedure Check_Pragma_Implemented (Subp : Entity_Id) is
9830 Iface_Alias : constant Entity_Id := Interface_Alias (Subp);
9831 Impl_Kind : constant Name_Id := Implementation_Kind (Iface_Alias);
9832 Subp_Alias : constant Entity_Id := Alias (Subp);
9833 Contr_Typ : Entity_Id;
9834 Impl_Subp : Entity_Id;
9835
9836 begin
9837 -- Subp must have an alias since it is a hidden entity used to link
9838 -- an interface subprogram to its overriding counterpart.
9839
9840 pragma Assert (Present (Subp_Alias));
9841
9842 -- Handle aliases to synchronized wrappers
9843
9844 Impl_Subp := Subp_Alias;
9845
9846 if Is_Primitive_Wrapper (Impl_Subp) then
9847 Impl_Subp := Wrapped_Entity (Impl_Subp);
9848 end if;
9849
9850 -- Extract the type of the controlling formal
9851
9852 Contr_Typ := Etype (First_Formal (Subp_Alias));
9853
9854 if Is_Concurrent_Record_Type (Contr_Typ) then
9855 Contr_Typ := Corresponding_Concurrent_Type (Contr_Typ);
9856 end if;
9857
9858 -- An interface subprogram whose implementation kind is By_Entry must
9859 -- be implemented by an entry.
9860
9861 if Impl_Kind = Name_By_Entry
9862 and then Ekind (Impl_Subp) /= E_Entry
9863 then
9864 Error_Msg_Node_2 := Iface_Alias;
9865 Error_Msg_NE
9866 ("type & must implement abstract subprogram & with an entry",
9867 Subp_Alias, Contr_Typ);
9868
9869 elsif Impl_Kind = Name_By_Protected_Procedure then
9870
9871 -- An interface subprogram whose implementation kind is By_
9872 -- Protected_Procedure cannot be implemented by a primitive
9873 -- procedure of a task type.
9874
9875 if Ekind (Contr_Typ) /= E_Protected_Type then
9876 Error_Msg_Node_2 := Contr_Typ;
9877 Error_Msg_NE
9878 ("interface subprogram & cannot be implemented by a " &
9879 "primitive procedure of task type &", Subp_Alias,
9880 Iface_Alias);
9881
9882 -- An interface subprogram whose implementation kind is By_
9883 -- Protected_Procedure must be implemented by a procedure.
9884
9885 elsif Ekind (Impl_Subp) /= E_Procedure then
9886 Error_Msg_Node_2 := Iface_Alias;
9887 Error_Msg_NE
9888 ("type & must implement abstract subprogram & with a " &
9889 "procedure", Subp_Alias, Contr_Typ);
9890
9891 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
9892 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
9893 then
9894 Error_Msg_Name_1 := Impl_Kind;
9895 Error_Msg_N
9896 ("overriding operation& must have synchronization%",
9897 Subp_Alias);
9898 end if;
9899
9900 -- If primitive has Optional synchronization, overriding operation
9901 -- must match if it has an explicit synchronization..
9902
9903 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
9904 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
9905 then
9906 Error_Msg_Name_1 := Impl_Kind;
9907 Error_Msg_N
9908 ("overriding operation& must have syncrhonization%",
9909 Subp_Alias);
9910 end if;
9911 end Check_Pragma_Implemented;
9912
9913 ------------------------------
9914 -- Check_Pragma_Implemented --
9915 ------------------------------
9916
9917 procedure Check_Pragma_Implemented
9918 (Subp : Entity_Id;
9919 Iface_Subp : Entity_Id)
9920 is
9921 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
9922 Subp_Kind : constant Name_Id := Implementation_Kind (Subp);
9923
9924 begin
9925 -- Ada 2012 (AI05-0030): The implementation kinds of an overridden
9926 -- and overriding subprogram are different. In general this is an
9927 -- error except when the implementation kind of the overridden
9928 -- subprograms is By_Any or Optional.
9929
9930 if Iface_Kind /= Subp_Kind
9931 and then Iface_Kind /= Name_By_Any
9932 and then Iface_Kind /= Name_Optional
9933 then
9934 if Iface_Kind = Name_By_Entry then
9935 Error_Msg_N
9936 ("incompatible implementation kind, overridden subprogram " &
9937 "is marked By_Entry", Subp);
9938 else
9939 Error_Msg_N
9940 ("incompatible implementation kind, overridden subprogram " &
9941 "is marked By_Protected_Procedure", Subp);
9942 end if;
9943 end if;
9944 end Check_Pragma_Implemented;
9945
9946 --------------------------------
9947 -- Inherit_Pragma_Implemented --
9948 --------------------------------
9949
9950 procedure Inherit_Pragma_Implemented
9951 (Subp : Entity_Id;
9952 Iface_Subp : Entity_Id)
9953 is
9954 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
9955 Loc : constant Source_Ptr := Sloc (Subp);
9956 Impl_Prag : Node_Id;
9957
9958 begin
9959 -- Since the implementation kind is stored as a representation item
9960 -- rather than a flag, create a pragma node.
9961
9962 Impl_Prag :=
9963 Make_Pragma (Loc,
9964 Chars => Name_Implemented,
9965 Pragma_Argument_Associations => New_List (
9966 Make_Pragma_Argument_Association (Loc,
9967 Expression => New_Occurrence_Of (Subp, Loc)),
9968
9969 Make_Pragma_Argument_Association (Loc,
9970 Expression => Make_Identifier (Loc, Iface_Kind))));
9971
9972 -- The pragma doesn't need to be analyzed because it is internally
9973 -- built. It is safe to directly register it as a rep item since we
9974 -- are only interested in the characters of the implementation kind.
9975
9976 Record_Rep_Item (Subp, Impl_Prag);
9977 end Inherit_Pragma_Implemented;
9978
9979 -- Start of processing for Check_Abstract_Overriding
9980
9981 begin
9982 Op_List := Primitive_Operations (T);
9983
9984 -- Loop to check primitive operations
9985
9986 Elmt := First_Elmt (Op_List);
9987 while Present (Elmt) loop
9988 Subp := Node (Elmt);
9989 Alias_Subp := Alias (Subp);
9990
9991 -- Inherited subprograms are identified by the fact that they do not
9992 -- come from source, and the associated source location is the
9993 -- location of the first subtype of the derived type.
9994
9995 -- Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
9996 -- subprograms that "require overriding".
9997
9998 -- Special exception, do not complain about failure to override the
9999 -- stream routines _Input and _Output, as well as the primitive
10000 -- operations used in dispatching selects since we always provide
10001 -- automatic overridings for these subprograms.
10002
10003 -- Also ignore this rule for convention CIL since .NET libraries
10004 -- do bizarre things with interfaces???
10005
10006 -- The partial view of T may have been a private extension, for
10007 -- which inherited functions dispatching on result are abstract.
10008 -- If the full view is a null extension, there is no need for
10009 -- overriding in Ada 2005, but wrappers need to be built for them
10010 -- (see exp_ch3, Build_Controlling_Function_Wrappers).
10011
10012 if Is_Null_Extension (T)
10013 and then Has_Controlling_Result (Subp)
10014 and then Ada_Version >= Ada_2005
10015 and then Present (Alias_Subp)
10016 and then not Comes_From_Source (Subp)
10017 and then not Is_Abstract_Subprogram (Alias_Subp)
10018 and then not Is_Access_Type (Etype (Subp))
10019 then
10020 null;
10021
10022 -- Ada 2005 (AI-251): Internal entities of interfaces need no
10023 -- processing because this check is done with the aliased
10024 -- entity
10025
10026 elsif Present (Interface_Alias (Subp)) then
10027 null;
10028
10029 elsif (Is_Abstract_Subprogram (Subp)
10030 or else Requires_Overriding (Subp)
10031 or else
10032 (Has_Controlling_Result (Subp)
10033 and then Present (Alias_Subp)
10034 and then not Comes_From_Source (Subp)
10035 and then Sloc (Subp) = Sloc (First_Subtype (T))))
10036 and then not Is_TSS (Subp, TSS_Stream_Input)
10037 and then not Is_TSS (Subp, TSS_Stream_Output)
10038 and then not Is_Abstract_Type (T)
10039 and then Convention (T) /= Convention_CIL
10040 and then not Is_Predefined_Interface_Primitive (Subp)
10041
10042 -- Ada 2005 (AI-251): Do not consider hidden entities associated
10043 -- with abstract interface types because the check will be done
10044 -- with the aliased entity (otherwise we generate a duplicated
10045 -- error message).
10046
10047 and then not Present (Interface_Alias (Subp))
10048 then
10049 if Present (Alias_Subp) then
10050
10051 -- Only perform the check for a derived subprogram when the
10052 -- type has an explicit record extension. This avoids incorrect
10053 -- flagging of abstract subprograms for the case of a type
10054 -- without an extension that is derived from a formal type
10055 -- with a tagged actual (can occur within a private part).
10056
10057 -- Ada 2005 (AI-391): In the case of an inherited function with
10058 -- a controlling result of the type, the rule does not apply if
10059 -- the type is a null extension (unless the parent function
10060 -- itself is abstract, in which case the function must still be
10061 -- be overridden). The expander will generate an overriding
10062 -- wrapper function calling the parent subprogram (see
10063 -- Exp_Ch3.Make_Controlling_Wrapper_Functions).
10064
10065 Type_Def := Type_Definition (Parent (T));
10066
10067 if Nkind (Type_Def) = N_Derived_Type_Definition
10068 and then Present (Record_Extension_Part (Type_Def))
10069 and then
10070 (Ada_Version < Ada_2005
10071 or else not Is_Null_Extension (T)
10072 or else Ekind (Subp) = E_Procedure
10073 or else not Has_Controlling_Result (Subp)
10074 or else Is_Abstract_Subprogram (Alias_Subp)
10075 or else Requires_Overriding (Subp)
10076 or else Is_Access_Type (Etype (Subp)))
10077 then
10078 -- Avoid reporting error in case of abstract predefined
10079 -- primitive inherited from interface type because the
10080 -- body of internally generated predefined primitives
10081 -- of tagged types are generated later by Freeze_Type
10082
10083 if Is_Interface (Root_Type (T))
10084 and then Is_Abstract_Subprogram (Subp)
10085 and then Is_Predefined_Dispatching_Operation (Subp)
10086 and then not Comes_From_Source (Ultimate_Alias (Subp))
10087 then
10088 null;
10089
10090 -- A null extension is not obliged to override an inherited
10091 -- procedure subject to pragma Extensions_Visible with value
10092 -- False and at least one controlling OUT parameter
10093 -- (SPARK RM 6.1.7(6)).
10094
10095 elsif Is_Null_Extension (T)
10096 and then Is_EVF_Procedure (Subp)
10097 then
10098 null;
10099
10100 else
10101 Error_Msg_NE
10102 ("type must be declared abstract or & overridden",
10103 T, Subp);
10104
10105 -- Traverse the whole chain of aliased subprograms to
10106 -- complete the error notification. This is especially
10107 -- useful for traceability of the chain of entities when
10108 -- the subprogram corresponds with an interface
10109 -- subprogram (which may be defined in another package).
10110
10111 if Present (Alias_Subp) then
10112 declare
10113 E : Entity_Id;
10114
10115 begin
10116 E := Subp;
10117 while Present (Alias (E)) loop
10118
10119 -- Avoid reporting redundant errors on entities
10120 -- inherited from interfaces
10121
10122 if Sloc (E) /= Sloc (T) then
10123 Error_Msg_Sloc := Sloc (E);
10124 Error_Msg_NE
10125 ("\& has been inherited #", T, Subp);
10126 end if;
10127
10128 E := Alias (E);
10129 end loop;
10130
10131 Error_Msg_Sloc := Sloc (E);
10132
10133 -- AI05-0068: report if there is an overriding
10134 -- non-abstract subprogram that is invisible.
10135
10136 if Is_Hidden (E)
10137 and then not Is_Abstract_Subprogram (E)
10138 then
10139 Error_Msg_NE
10140 ("\& subprogram# is not visible",
10141 T, Subp);
10142
10143 -- Clarify the case where a non-null extension must
10144 -- override inherited procedure subject to pragma
10145 -- Extensions_Visible with value False and at least
10146 -- one controlling OUT param.
10147
10148 elsif Is_EVF_Procedure (E) then
10149 Error_Msg_NE
10150 ("\& # is subject to Extensions_Visible False",
10151 T, Subp);
10152
10153 else
10154 Error_Msg_NE
10155 ("\& has been inherited from subprogram #",
10156 T, Subp);
10157 end if;
10158 end;
10159 end if;
10160 end if;
10161
10162 -- Ada 2005 (AI-345): Protected or task type implementing
10163 -- abstract interfaces.
10164
10165 elsif Is_Concurrent_Record_Type (T)
10166 and then Present (Interfaces (T))
10167 then
10168 -- There is no need to check here RM 9.4(11.9/3) since we
10169 -- are processing the corresponding record type and the
10170 -- mode of the overriding subprograms was verified by
10171 -- Check_Conformance when the corresponding concurrent
10172 -- type declaration was analyzed.
10173
10174 Error_Msg_NE
10175 ("interface subprogram & must be overridden", T, Subp);
10176
10177 -- Examine primitive operations of synchronized type to find
10178 -- homonyms that have the wrong profile.
10179
10180 declare
10181 Prim : Entity_Id;
10182
10183 begin
10184 Prim := First_Entity (Corresponding_Concurrent_Type (T));
10185 while Present (Prim) loop
10186 if Chars (Prim) = Chars (Subp) then
10187 Error_Msg_NE
10188 ("profile is not type conformant with prefixed "
10189 & "view profile of inherited operation&",
10190 Prim, Subp);
10191 end if;
10192
10193 Next_Entity (Prim);
10194 end loop;
10195 end;
10196 end if;
10197
10198 else
10199 Error_Msg_Node_2 := T;
10200 Error_Msg_N
10201 ("abstract subprogram& not allowed for type&", Subp);
10202
10203 -- Also post unconditional warning on the type (unconditional
10204 -- so that if there are more than one of these cases, we get
10205 -- them all, and not just the first one).
10206
10207 Error_Msg_Node_2 := Subp;
10208 Error_Msg_N ("nonabstract type& has abstract subprogram&!", T);
10209 end if;
10210
10211 -- A subprogram subject to pragma Extensions_Visible with value
10212 -- "True" cannot override a subprogram subject to the same pragma
10213 -- with value "False" (SPARK RM 6.1.7(5)).
10214
10215 elsif Extensions_Visible_Status (Subp) = Extensions_Visible_True
10216 and then Present (Overridden_Operation (Subp))
10217 and then Extensions_Visible_Status (Overridden_Operation (Subp)) =
10218 Extensions_Visible_False
10219 then
10220 Error_Msg_Sloc := Sloc (Overridden_Operation (Subp));
10221 Error_Msg_N
10222 ("subprogram & with Extensions_Visible True cannot override "
10223 & "subprogram # with Extensions_Visible False", Subp);
10224 end if;
10225
10226 -- Ada 2012 (AI05-0030): Perform checks related to pragma Implemented
10227
10228 -- Subp is an expander-generated procedure which maps an interface
10229 -- alias to a protected wrapper. The interface alias is flagged by
10230 -- pragma Implemented. Ensure that Subp is a procedure when the
10231 -- implementation kind is By_Protected_Procedure or an entry when
10232 -- By_Entry.
10233
10234 if Ada_Version >= Ada_2012
10235 and then Is_Hidden (Subp)
10236 and then Present (Interface_Alias (Subp))
10237 and then Has_Rep_Pragma (Interface_Alias (Subp), Name_Implemented)
10238 then
10239 Check_Pragma_Implemented (Subp);
10240 end if;
10241
10242 -- Subp is an interface primitive which overrides another interface
10243 -- primitive marked with pragma Implemented.
10244
10245 if Ada_Version >= Ada_2012
10246 and then Present (Overridden_Operation (Subp))
10247 and then Has_Rep_Pragma
10248 (Overridden_Operation (Subp), Name_Implemented)
10249 then
10250 -- If the overriding routine is also marked by Implemented, check
10251 -- that the two implementation kinds are conforming.
10252
10253 if Has_Rep_Pragma (Subp, Name_Implemented) then
10254 Check_Pragma_Implemented
10255 (Subp => Subp,
10256 Iface_Subp => Overridden_Operation (Subp));
10257
10258 -- Otherwise the overriding routine inherits the implementation
10259 -- kind from the overridden subprogram.
10260
10261 else
10262 Inherit_Pragma_Implemented
10263 (Subp => Subp,
10264 Iface_Subp => Overridden_Operation (Subp));
10265 end if;
10266 end if;
10267
10268 -- If the operation is a wrapper for a synchronized primitive, it
10269 -- may be called indirectly through a dispatching select. We assume
10270 -- that it will be referenced elsewhere indirectly, and suppress
10271 -- warnings about an unused entity.
10272
10273 if Is_Primitive_Wrapper (Subp)
10274 and then Present (Wrapped_Entity (Subp))
10275 then
10276 Set_Referenced (Wrapped_Entity (Subp));
10277 end if;
10278
10279 Next_Elmt (Elmt);
10280 end loop;
10281 end Check_Abstract_Overriding;
10282
10283 ------------------------------------------------
10284 -- Check_Access_Discriminant_Requires_Limited --
10285 ------------------------------------------------
10286
10287 procedure Check_Access_Discriminant_Requires_Limited
10288 (D : Node_Id;
10289 Loc : Node_Id)
10290 is
10291 begin
10292 -- A discriminant_specification for an access discriminant shall appear
10293 -- only in the declaration for a task or protected type, or for a type
10294 -- with the reserved word 'limited' in its definition or in one of its
10295 -- ancestors (RM 3.7(10)).
10296
10297 -- AI-0063: The proper condition is that type must be immutably limited,
10298 -- or else be a partial view.
10299
10300 if Nkind (Discriminant_Type (D)) = N_Access_Definition then
10301 if Is_Limited_View (Current_Scope)
10302 or else
10303 (Nkind (Parent (Current_Scope)) = N_Private_Type_Declaration
10304 and then Limited_Present (Parent (Current_Scope)))
10305 then
10306 null;
10307
10308 else
10309 Error_Msg_N
10310 ("access discriminants allowed only for limited types", Loc);
10311 end if;
10312 end if;
10313 end Check_Access_Discriminant_Requires_Limited;
10314
10315 -----------------------------------
10316 -- Check_Aliased_Component_Types --
10317 -----------------------------------
10318
10319 procedure Check_Aliased_Component_Types (T : Entity_Id) is
10320 C : Entity_Id;
10321
10322 begin
10323 -- ??? Also need to check components of record extensions, but not
10324 -- components of protected types (which are always limited).
10325
10326 -- Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
10327 -- types to be unconstrained. This is safe because it is illegal to
10328 -- create access subtypes to such types with explicit discriminant
10329 -- constraints.
10330
10331 if not Is_Limited_Type (T) then
10332 if Ekind (T) = E_Record_Type then
10333 C := First_Component (T);
10334 while Present (C) loop
10335 if Is_Aliased (C)
10336 and then Has_Discriminants (Etype (C))
10337 and then not Is_Constrained (Etype (C))
10338 and then not In_Instance_Body
10339 and then Ada_Version < Ada_2005
10340 then
10341 Error_Msg_N
10342 ("aliased component must be constrained (RM 3.6(11))",
10343 C);
10344 end if;
10345
10346 Next_Component (C);
10347 end loop;
10348
10349 elsif Ekind (T) = E_Array_Type then
10350 if Has_Aliased_Components (T)
10351 and then Has_Discriminants (Component_Type (T))
10352 and then not Is_Constrained (Component_Type (T))
10353 and then not In_Instance_Body
10354 and then Ada_Version < Ada_2005
10355 then
10356 Error_Msg_N
10357 ("aliased component type must be constrained (RM 3.6(11))",
10358 T);
10359 end if;
10360 end if;
10361 end if;
10362 end Check_Aliased_Component_Types;
10363
10364 ---------------------------------------
10365 -- Check_Anonymous_Access_Components --
10366 ---------------------------------------
10367
10368 procedure Check_Anonymous_Access_Components
10369 (Typ_Decl : Node_Id;
10370 Typ : Entity_Id;
10371 Prev : Entity_Id;
10372 Comp_List : Node_Id)
10373 is
10374 Loc : constant Source_Ptr := Sloc (Typ_Decl);
10375 Anon_Access : Entity_Id;
10376 Acc_Def : Node_Id;
10377 Comp : Node_Id;
10378 Comp_Def : Node_Id;
10379 Decl : Node_Id;
10380 Type_Def : Node_Id;
10381
10382 procedure Build_Incomplete_Type_Declaration;
10383 -- If the record type contains components that include an access to the
10384 -- current record, then create an incomplete type declaration for the
10385 -- record, to be used as the designated type of the anonymous access.
10386 -- This is done only once, and only if there is no previous partial
10387 -- view of the type.
10388
10389 function Designates_T (Subt : Node_Id) return Boolean;
10390 -- Check whether a node designates the enclosing record type, or 'Class
10391 -- of that type
10392
10393 function Mentions_T (Acc_Def : Node_Id) return Boolean;
10394 -- Check whether an access definition includes a reference to
10395 -- the enclosing record type. The reference can be a subtype mark
10396 -- in the access definition itself, a 'Class attribute reference, or
10397 -- recursively a reference appearing in a parameter specification
10398 -- or result definition of an access_to_subprogram definition.
10399
10400 --------------------------------------
10401 -- Build_Incomplete_Type_Declaration --
10402 --------------------------------------
10403
10404 procedure Build_Incomplete_Type_Declaration is
10405 Decl : Node_Id;
10406 Inc_T : Entity_Id;
10407 H : Entity_Id;
10408
10409 -- Is_Tagged indicates whether the type is tagged. It is tagged if
10410 -- it's "is new ... with record" or else "is tagged record ...".
10411
10412 Is_Tagged : constant Boolean :=
10413 (Nkind (Type_Definition (Typ_Decl)) = N_Derived_Type_Definition
10414 and then
10415 Present (Record_Extension_Part (Type_Definition (Typ_Decl))))
10416 or else
10417 (Nkind (Type_Definition (Typ_Decl)) = N_Record_Definition
10418 and then Tagged_Present (Type_Definition (Typ_Decl)));
10419
10420 begin
10421 -- If there is a previous partial view, no need to create a new one
10422 -- If the partial view, given by Prev, is incomplete, If Prev is
10423 -- a private declaration, full declaration is flagged accordingly.
10424
10425 if Prev /= Typ then
10426 if Is_Tagged then
10427 Make_Class_Wide_Type (Prev);
10428 Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
10429 Set_Etype (Class_Wide_Type (Typ), Typ);
10430 end if;
10431
10432 return;
10433
10434 elsif Has_Private_Declaration (Typ) then
10435
10436 -- If we refer to T'Class inside T, and T is the completion of a
10437 -- private type, then make sure the class-wide type exists.
10438
10439 if Is_Tagged then
10440 Make_Class_Wide_Type (Typ);
10441 end if;
10442
10443 return;
10444
10445 -- If there was a previous anonymous access type, the incomplete
10446 -- type declaration will have been created already.
10447
10448 elsif Present (Current_Entity (Typ))
10449 and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
10450 and then Full_View (Current_Entity (Typ)) = Typ
10451 then
10452 if Is_Tagged
10453 and then Comes_From_Source (Current_Entity (Typ))
10454 and then not Is_Tagged_Type (Current_Entity (Typ))
10455 then
10456 Make_Class_Wide_Type (Typ);
10457 Error_Msg_N
10458 ("incomplete view of tagged type should be declared tagged??",
10459 Parent (Current_Entity (Typ)));
10460 end if;
10461 return;
10462
10463 else
10464 Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
10465 Decl := Make_Incomplete_Type_Declaration (Loc, Inc_T);
10466
10467 -- Type has already been inserted into the current scope. Remove
10468 -- it, and add incomplete declaration for type, so that subsequent
10469 -- anonymous access types can use it. The entity is unchained from
10470 -- the homonym list and from immediate visibility. After analysis,
10471 -- the entity in the incomplete declaration becomes immediately
10472 -- visible in the record declaration that follows.
10473
10474 H := Current_Entity (Typ);
10475
10476 if H = Typ then
10477 Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
10478 else
10479 while Present (H)
10480 and then Homonym (H) /= Typ
10481 loop
10482 H := Homonym (Typ);
10483 end loop;
10484
10485 Set_Homonym (H, Homonym (Typ));
10486 end if;
10487
10488 Insert_Before (Typ_Decl, Decl);
10489 Analyze (Decl);
10490 Set_Full_View (Inc_T, Typ);
10491
10492 if Is_Tagged then
10493
10494 -- Create a common class-wide type for both views, and set the
10495 -- Etype of the class-wide type to the full view.
10496
10497 Make_Class_Wide_Type (Inc_T);
10498 Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
10499 Set_Etype (Class_Wide_Type (Typ), Typ);
10500 end if;
10501 end if;
10502 end Build_Incomplete_Type_Declaration;
10503
10504 ------------------
10505 -- Designates_T --
10506 ------------------
10507
10508 function Designates_T (Subt : Node_Id) return Boolean is
10509 Type_Id : constant Name_Id := Chars (Typ);
10510
10511 function Names_T (Nam : Node_Id) return Boolean;
10512 -- The record type has not been introduced in the current scope
10513 -- yet, so we must examine the name of the type itself, either
10514 -- an identifier T, or an expanded name of the form P.T, where
10515 -- P denotes the current scope.
10516
10517 -------------
10518 -- Names_T --
10519 -------------
10520
10521 function Names_T (Nam : Node_Id) return Boolean is
10522 begin
10523 if Nkind (Nam) = N_Identifier then
10524 return Chars (Nam) = Type_Id;
10525
10526 elsif Nkind (Nam) = N_Selected_Component then
10527 if Chars (Selector_Name (Nam)) = Type_Id then
10528 if Nkind (Prefix (Nam)) = N_Identifier then
10529 return Chars (Prefix (Nam)) = Chars (Current_Scope);
10530
10531 elsif Nkind (Prefix (Nam)) = N_Selected_Component then
10532 return Chars (Selector_Name (Prefix (Nam))) =
10533 Chars (Current_Scope);
10534 else
10535 return False;
10536 end if;
10537
10538 else
10539 return False;
10540 end if;
10541
10542 else
10543 return False;
10544 end if;
10545 end Names_T;
10546
10547 -- Start of processing for Designates_T
10548
10549 begin
10550 if Nkind (Subt) = N_Identifier then
10551 return Chars (Subt) = Type_Id;
10552
10553 -- Reference can be through an expanded name which has not been
10554 -- analyzed yet, and which designates enclosing scopes.
10555
10556 elsif Nkind (Subt) = N_Selected_Component then
10557 if Names_T (Subt) then
10558 return True;
10559
10560 -- Otherwise it must denote an entity that is already visible.
10561 -- The access definition may name a subtype of the enclosing
10562 -- type, if there is a previous incomplete declaration for it.
10563
10564 else
10565 Find_Selected_Component (Subt);
10566 return
10567 Is_Entity_Name (Subt)
10568 and then Scope (Entity (Subt)) = Current_Scope
10569 and then
10570 (Chars (Base_Type (Entity (Subt))) = Type_Id
10571 or else
10572 (Is_Class_Wide_Type (Entity (Subt))
10573 and then
10574 Chars (Etype (Base_Type (Entity (Subt)))) =
10575 Type_Id));
10576 end if;
10577
10578 -- A reference to the current type may appear as the prefix of
10579 -- a 'Class attribute.
10580
10581 elsif Nkind (Subt) = N_Attribute_Reference
10582 and then Attribute_Name (Subt) = Name_Class
10583 then
10584 return Names_T (Prefix (Subt));
10585
10586 else
10587 return False;
10588 end if;
10589 end Designates_T;
10590
10591 ----------------
10592 -- Mentions_T --
10593 ----------------
10594
10595 function Mentions_T (Acc_Def : Node_Id) return Boolean is
10596 Param_Spec : Node_Id;
10597
10598 Acc_Subprg : constant Node_Id :=
10599 Access_To_Subprogram_Definition (Acc_Def);
10600
10601 begin
10602 if No (Acc_Subprg) then
10603 return Designates_T (Subtype_Mark (Acc_Def));
10604 end if;
10605
10606 -- Component is an access_to_subprogram: examine its formals,
10607 -- and result definition in the case of an access_to_function.
10608
10609 Param_Spec := First (Parameter_Specifications (Acc_Subprg));
10610 while Present (Param_Spec) loop
10611 if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
10612 and then Mentions_T (Parameter_Type (Param_Spec))
10613 then
10614 return True;
10615
10616 elsif Designates_T (Parameter_Type (Param_Spec)) then
10617 return True;
10618 end if;
10619
10620 Next (Param_Spec);
10621 end loop;
10622
10623 if Nkind (Acc_Subprg) = N_Access_Function_Definition then
10624 if Nkind (Result_Definition (Acc_Subprg)) =
10625 N_Access_Definition
10626 then
10627 return Mentions_T (Result_Definition (Acc_Subprg));
10628 else
10629 return Designates_T (Result_Definition (Acc_Subprg));
10630 end if;
10631 end if;
10632
10633 return False;
10634 end Mentions_T;
10635
10636 -- Start of processing for Check_Anonymous_Access_Components
10637
10638 begin
10639 if No (Comp_List) then
10640 return;
10641 end if;
10642
10643 Comp := First (Component_Items (Comp_List));
10644 while Present (Comp) loop
10645 if Nkind (Comp) = N_Component_Declaration
10646 and then Present
10647 (Access_Definition (Component_Definition (Comp)))
10648 and then
10649 Mentions_T (Access_Definition (Component_Definition (Comp)))
10650 then
10651 Comp_Def := Component_Definition (Comp);
10652 Acc_Def :=
10653 Access_To_Subprogram_Definition (Access_Definition (Comp_Def));
10654
10655 Build_Incomplete_Type_Declaration;
10656 Anon_Access := Make_Temporary (Loc, 'S');
10657
10658 -- Create a declaration for the anonymous access type: either
10659 -- an access_to_object or an access_to_subprogram.
10660
10661 if Present (Acc_Def) then
10662 if Nkind (Acc_Def) = N_Access_Function_Definition then
10663 Type_Def :=
10664 Make_Access_Function_Definition (Loc,
10665 Parameter_Specifications =>
10666 Parameter_Specifications (Acc_Def),
10667 Result_Definition => Result_Definition (Acc_Def));
10668 else
10669 Type_Def :=
10670 Make_Access_Procedure_Definition (Loc,
10671 Parameter_Specifications =>
10672 Parameter_Specifications (Acc_Def));
10673 end if;
10674
10675 else
10676 Type_Def :=
10677 Make_Access_To_Object_Definition (Loc,
10678 Subtype_Indication =>
10679 Relocate_Node
10680 (Subtype_Mark (Access_Definition (Comp_Def))));
10681
10682 Set_Constant_Present
10683 (Type_Def, Constant_Present (Access_Definition (Comp_Def)));
10684 Set_All_Present
10685 (Type_Def, All_Present (Access_Definition (Comp_Def)));
10686 end if;
10687
10688 Set_Null_Exclusion_Present
10689 (Type_Def,
10690 Null_Exclusion_Present (Access_Definition (Comp_Def)));
10691
10692 Decl :=
10693 Make_Full_Type_Declaration (Loc,
10694 Defining_Identifier => Anon_Access,
10695 Type_Definition => Type_Def);
10696
10697 Insert_Before (Typ_Decl, Decl);
10698 Analyze (Decl);
10699
10700 -- If an access to subprogram, create the extra formals
10701
10702 if Present (Acc_Def) then
10703 Create_Extra_Formals (Designated_Type (Anon_Access));
10704
10705 -- If an access to object, preserve entity of designated type,
10706 -- for ASIS use, before rewriting the component definition.
10707
10708 else
10709 declare
10710 Desig : Entity_Id;
10711
10712 begin
10713 Desig := Entity (Subtype_Indication (Type_Def));
10714
10715 -- If the access definition is to the current record,
10716 -- the visible entity at this point is an incomplete
10717 -- type. Retrieve the full view to simplify ASIS queries
10718
10719 if Ekind (Desig) = E_Incomplete_Type then
10720 Desig := Full_View (Desig);
10721 end if;
10722
10723 Set_Entity
10724 (Subtype_Mark (Access_Definition (Comp_Def)), Desig);
10725 end;
10726 end if;
10727
10728 Rewrite (Comp_Def,
10729 Make_Component_Definition (Loc,
10730 Subtype_Indication =>
10731 New_Occurrence_Of (Anon_Access, Loc)));
10732
10733 if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
10734 Set_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
10735 else
10736 Set_Ekind (Anon_Access, E_Anonymous_Access_Type);
10737 end if;
10738
10739 Set_Is_Local_Anonymous_Access (Anon_Access);
10740 end if;
10741
10742 Next (Comp);
10743 end loop;
10744
10745 if Present (Variant_Part (Comp_List)) then
10746 declare
10747 V : Node_Id;
10748 begin
10749 V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
10750 while Present (V) loop
10751 Check_Anonymous_Access_Components
10752 (Typ_Decl, Typ, Prev, Component_List (V));
10753 Next_Non_Pragma (V);
10754 end loop;
10755 end;
10756 end if;
10757 end Check_Anonymous_Access_Components;
10758
10759 ----------------------
10760 -- Check_Completion --
10761 ----------------------
10762
10763 procedure Check_Completion (Body_Id : Node_Id := Empty) is
10764 E : Entity_Id;
10765
10766 procedure Post_Error;
10767 -- Post error message for lack of completion for entity E
10768
10769 ----------------
10770 -- Post_Error --
10771 ----------------
10772
10773 procedure Post_Error is
10774
10775 procedure Missing_Body;
10776 -- Output missing body message
10777
10778 ------------------
10779 -- Missing_Body --
10780 ------------------
10781
10782 procedure Missing_Body is
10783 begin
10784 -- Spec is in same unit, so we can post on spec
10785
10786 if In_Same_Source_Unit (Body_Id, E) then
10787 Error_Msg_N ("missing body for &", E);
10788
10789 -- Spec is in a separate unit, so we have to post on the body
10790
10791 else
10792 Error_Msg_NE ("missing body for & declared#!", Body_Id, E);
10793 end if;
10794 end Missing_Body;
10795
10796 -- Start of processing for Post_Error
10797
10798 begin
10799 if not Comes_From_Source (E) then
10800
10801 if Ekind_In (E, E_Task_Type, E_Protected_Type) then
10802
10803 -- It may be an anonymous protected type created for a
10804 -- single variable. Post error on variable, if present.
10805
10806 declare
10807 Var : Entity_Id;
10808
10809 begin
10810 Var := First_Entity (Current_Scope);
10811 while Present (Var) loop
10812 exit when Etype (Var) = E
10813 and then Comes_From_Source (Var);
10814
10815 Next_Entity (Var);
10816 end loop;
10817
10818 if Present (Var) then
10819 E := Var;
10820 end if;
10821 end;
10822 end if;
10823 end if;
10824
10825 -- If a generated entity has no completion, then either previous
10826 -- semantic errors have disabled the expansion phase, or else we had
10827 -- missing subunits, or else we are compiling without expansion,
10828 -- or else something is very wrong.
10829
10830 if not Comes_From_Source (E) then
10831 pragma Assert
10832 (Serious_Errors_Detected > 0
10833 or else Configurable_Run_Time_Violations > 0
10834 or else Subunits_Missing
10835 or else not Expander_Active);
10836 return;
10837
10838 -- Here for source entity
10839
10840 else
10841 -- Here if no body to post the error message, so we post the error
10842 -- on the declaration that has no completion. This is not really
10843 -- the right place to post it, think about this later ???
10844
10845 if No (Body_Id) then
10846 if Is_Type (E) then
10847 Error_Msg_NE
10848 ("missing full declaration for }", Parent (E), E);
10849 else
10850 Error_Msg_NE ("missing body for &", Parent (E), E);
10851 end if;
10852
10853 -- Package body has no completion for a declaration that appears
10854 -- in the corresponding spec. Post error on the body, with a
10855 -- reference to the non-completed declaration.
10856
10857 else
10858 Error_Msg_Sloc := Sloc (E);
10859
10860 if Is_Type (E) then
10861 Error_Msg_NE ("missing full declaration for }!", Body_Id, E);
10862
10863 elsif Is_Overloadable (E)
10864 and then Current_Entity_In_Scope (E) /= E
10865 then
10866 -- It may be that the completion is mistyped and appears as
10867 -- a distinct overloading of the entity.
10868
10869 declare
10870 Candidate : constant Entity_Id :=
10871 Current_Entity_In_Scope (E);
10872 Decl : constant Node_Id :=
10873 Unit_Declaration_Node (Candidate);
10874
10875 begin
10876 if Is_Overloadable (Candidate)
10877 and then Ekind (Candidate) = Ekind (E)
10878 and then Nkind (Decl) = N_Subprogram_Body
10879 and then Acts_As_Spec (Decl)
10880 then
10881 Check_Type_Conformant (Candidate, E);
10882
10883 else
10884 Missing_Body;
10885 end if;
10886 end;
10887
10888 else
10889 Missing_Body;
10890 end if;
10891 end if;
10892 end if;
10893 end Post_Error;
10894
10895 -- Local variables
10896
10897 Pack_Id : constant Entity_Id := Current_Scope;
10898
10899 -- Start of processing for Check_Completion
10900
10901 begin
10902 E := First_Entity (Pack_Id);
10903 while Present (E) loop
10904 if Is_Intrinsic_Subprogram (E) then
10905 null;
10906
10907 -- A Ghost entity declared in a non-Ghost package does not force the
10908 -- need for a body (SPARK RM 6.9(11)).
10909
10910 elsif not Is_Ghost_Entity (Pack_Id) and then Is_Ghost_Entity (E) then
10911 null;
10912
10913 -- The following situation requires special handling: a child unit
10914 -- that appears in the context clause of the body of its parent:
10915
10916 -- procedure Parent.Child (...);
10917
10918 -- with Parent.Child;
10919 -- package body Parent is
10920
10921 -- Here Parent.Child appears as a local entity, but should not be
10922 -- flagged as requiring completion, because it is a compilation
10923 -- unit.
10924
10925 -- Ignore missing completion for a subprogram that does not come from
10926 -- source (including the _Call primitive operation of RAS types,
10927 -- which has to have the flag Comes_From_Source for other purposes):
10928 -- we assume that the expander will provide the missing completion.
10929 -- In case of previous errors, other expansion actions that provide
10930 -- bodies for null procedures with not be invoked, so inhibit message
10931 -- in those cases.
10932
10933 -- Note that E_Operator is not in the list that follows, because
10934 -- this kind is reserved for predefined operators, that are
10935 -- intrinsic and do not need completion.
10936
10937 elsif Ekind_In (E, E_Function,
10938 E_Procedure,
10939 E_Generic_Function,
10940 E_Generic_Procedure)
10941 then
10942 if Has_Completion (E) then
10943 null;
10944
10945 elsif Is_Subprogram (E) and then Is_Abstract_Subprogram (E) then
10946 null;
10947
10948 elsif Is_Subprogram (E)
10949 and then (not Comes_From_Source (E)
10950 or else Chars (E) = Name_uCall)
10951 then
10952 null;
10953
10954 elsif
10955 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
10956 then
10957 null;
10958
10959 elsif Nkind (Parent (E)) = N_Procedure_Specification
10960 and then Null_Present (Parent (E))
10961 and then Serious_Errors_Detected > 0
10962 then
10963 null;
10964
10965 else
10966 Post_Error;
10967 end if;
10968
10969 elsif Is_Entry (E) then
10970 if not Has_Completion (E) and then
10971 (Ekind (Scope (E)) = E_Protected_Object
10972 or else Ekind (Scope (E)) = E_Protected_Type)
10973 then
10974 Post_Error;
10975 end if;
10976
10977 elsif Is_Package_Or_Generic_Package (E) then
10978 if Unit_Requires_Body (E) then
10979 if not Has_Completion (E)
10980 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
10981 N_Compilation_Unit
10982 then
10983 Post_Error;
10984 end if;
10985
10986 elsif not Is_Child_Unit (E) then
10987 May_Need_Implicit_Body (E);
10988 end if;
10989
10990 -- A formal incomplete type (Ada 2012) does not require a completion;
10991 -- other incomplete type declarations do.
10992
10993 elsif Ekind (E) = E_Incomplete_Type
10994 and then No (Underlying_Type (E))
10995 and then not Is_Generic_Type (E)
10996 then
10997 Post_Error;
10998
10999 elsif Ekind_In (E, E_Task_Type, E_Protected_Type)
11000 and then not Has_Completion (E)
11001 then
11002 Post_Error;
11003
11004 -- A single task declared in the current scope is a constant, verify
11005 -- that the body of its anonymous type is in the same scope. If the
11006 -- task is defined elsewhere, this may be a renaming declaration for
11007 -- which no completion is needed.
11008
11009 elsif Ekind (E) = E_Constant
11010 and then Ekind (Etype (E)) = E_Task_Type
11011 and then not Has_Completion (Etype (E))
11012 and then Scope (Etype (E)) = Current_Scope
11013 then
11014 Post_Error;
11015
11016 elsif Ekind (E) = E_Protected_Object
11017 and then not Has_Completion (Etype (E))
11018 then
11019 Post_Error;
11020
11021 elsif Ekind (E) = E_Record_Type then
11022 if Is_Tagged_Type (E) then
11023 Check_Abstract_Overriding (E);
11024 Check_Conventions (E);
11025 end if;
11026
11027 Check_Aliased_Component_Types (E);
11028
11029 elsif Ekind (E) = E_Array_Type then
11030 Check_Aliased_Component_Types (E);
11031
11032 end if;
11033
11034 Next_Entity (E);
11035 end loop;
11036 end Check_Completion;
11037
11038 ------------------------------------
11039 -- Check_CPP_Type_Has_No_Defaults --
11040 ------------------------------------
11041
11042 procedure Check_CPP_Type_Has_No_Defaults (T : Entity_Id) is
11043 Tdef : constant Node_Id := Type_Definition (Declaration_Node (T));
11044 Clist : Node_Id;
11045 Comp : Node_Id;
11046
11047 begin
11048 -- Obtain the component list
11049
11050 if Nkind (Tdef) = N_Record_Definition then
11051 Clist := Component_List (Tdef);
11052 else pragma Assert (Nkind (Tdef) = N_Derived_Type_Definition);
11053 Clist := Component_List (Record_Extension_Part (Tdef));
11054 end if;
11055
11056 -- Check all components to ensure no default expressions
11057
11058 if Present (Clist) then
11059 Comp := First (Component_Items (Clist));
11060 while Present (Comp) loop
11061 if Present (Expression (Comp)) then
11062 Error_Msg_N
11063 ("component of imported 'C'P'P type cannot have "
11064 & "default expression", Expression (Comp));
11065 end if;
11066
11067 Next (Comp);
11068 end loop;
11069 end if;
11070 end Check_CPP_Type_Has_No_Defaults;
11071
11072 ----------------------------
11073 -- Check_Delta_Expression --
11074 ----------------------------
11075
11076 procedure Check_Delta_Expression (E : Node_Id) is
11077 begin
11078 if not (Is_Real_Type (Etype (E))) then
11079 Wrong_Type (E, Any_Real);
11080
11081 elsif not Is_OK_Static_Expression (E) then
11082 Flag_Non_Static_Expr
11083 ("non-static expression used for delta value!", E);
11084
11085 elsif not UR_Is_Positive (Expr_Value_R (E)) then
11086 Error_Msg_N ("delta expression must be positive", E);
11087
11088 else
11089 return;
11090 end if;
11091
11092 -- If any of above errors occurred, then replace the incorrect
11093 -- expression by the real 0.1, which should prevent further errors.
11094
11095 Rewrite (E,
11096 Make_Real_Literal (Sloc (E), Ureal_Tenth));
11097 Analyze_And_Resolve (E, Standard_Float);
11098 end Check_Delta_Expression;
11099
11100 -----------------------------
11101 -- Check_Digits_Expression --
11102 -----------------------------
11103
11104 procedure Check_Digits_Expression (E : Node_Id) is
11105 begin
11106 if not (Is_Integer_Type (Etype (E))) then
11107 Wrong_Type (E, Any_Integer);
11108
11109 elsif not Is_OK_Static_Expression (E) then
11110 Flag_Non_Static_Expr
11111 ("non-static expression used for digits value!", E);
11112
11113 elsif Expr_Value (E) <= 0 then
11114 Error_Msg_N ("digits value must be greater than zero", E);
11115
11116 else
11117 return;
11118 end if;
11119
11120 -- If any of above errors occurred, then replace the incorrect
11121 -- expression by the integer 1, which should prevent further errors.
11122
11123 Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
11124 Analyze_And_Resolve (E, Standard_Integer);
11125
11126 end Check_Digits_Expression;
11127
11128 --------------------------
11129 -- Check_Initialization --
11130 --------------------------
11131
11132 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
11133 begin
11134 -- Special processing for limited types
11135
11136 if Is_Limited_Type (T)
11137 and then not In_Instance
11138 and then not In_Inlined_Body
11139 then
11140 if not OK_For_Limited_Init (T, Exp) then
11141
11142 -- In GNAT mode, this is just a warning, to allow it to be evilly
11143 -- turned off. Otherwise it is a real error.
11144
11145 if GNAT_Mode then
11146 Error_Msg_N
11147 ("??cannot initialize entities of limited type!", Exp);
11148
11149 elsif Ada_Version < Ada_2005 then
11150
11151 -- The side effect removal machinery may generate illegal Ada
11152 -- code to avoid the usage of access types and 'reference in
11153 -- SPARK mode. Since this is legal code with respect to theorem
11154 -- proving, do not emit the error.
11155
11156 if GNATprove_Mode
11157 and then Nkind (Exp) = N_Function_Call
11158 and then Nkind (Parent (Exp)) = N_Object_Declaration
11159 and then not Comes_From_Source
11160 (Defining_Identifier (Parent (Exp)))
11161 then
11162 null;
11163
11164 else
11165 Error_Msg_N
11166 ("cannot initialize entities of limited type", Exp);
11167 Explain_Limited_Type (T, Exp);
11168 end if;
11169
11170 else
11171 -- Specialize error message according to kind of illegal
11172 -- initial expression.
11173
11174 if Nkind (Exp) = N_Type_Conversion
11175 and then Nkind (Expression (Exp)) = N_Function_Call
11176 then
11177 Error_Msg_N
11178 ("illegal context for call"
11179 & " to function with limited result", Exp);
11180
11181 else
11182 Error_Msg_N
11183 ("initialization of limited object requires aggregate "
11184 & "or function call", Exp);
11185 end if;
11186 end if;
11187 end if;
11188 end if;
11189
11190 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag gets
11191 -- set unless we can be sure that no range check is required.
11192
11193 if (GNATprove_Mode or not Expander_Active)
11194 and then Is_Scalar_Type (T)
11195 and then not Is_In_Range (Exp, T, Assume_Valid => True)
11196 then
11197 Set_Do_Range_Check (Exp);
11198 end if;
11199 end Check_Initialization;
11200
11201 ----------------------
11202 -- Check_Interfaces --
11203 ----------------------
11204
11205 procedure Check_Interfaces (N : Node_Id; Def : Node_Id) is
11206 Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
11207
11208 Iface : Node_Id;
11209 Iface_Def : Node_Id;
11210 Iface_Typ : Entity_Id;
11211 Parent_Node : Node_Id;
11212
11213 Is_Task : Boolean := False;
11214 -- Set True if parent type or any progenitor is a task interface
11215
11216 Is_Protected : Boolean := False;
11217 -- Set True if parent type or any progenitor is a protected interface
11218
11219 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
11220 -- Check that a progenitor is compatible with declaration. If an error
11221 -- message is output, it is posted on Error_Node.
11222
11223 ------------------
11224 -- Check_Ifaces --
11225 ------------------
11226
11227 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
11228 Iface_Id : constant Entity_Id :=
11229 Defining_Identifier (Parent (Iface_Def));
11230 Type_Def : Node_Id;
11231
11232 begin
11233 if Nkind (N) = N_Private_Extension_Declaration then
11234 Type_Def := N;
11235 else
11236 Type_Def := Type_Definition (N);
11237 end if;
11238
11239 if Is_Task_Interface (Iface_Id) then
11240 Is_Task := True;
11241
11242 elsif Is_Protected_Interface (Iface_Id) then
11243 Is_Protected := True;
11244 end if;
11245
11246 if Is_Synchronized_Interface (Iface_Id) then
11247
11248 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
11249 -- extension derived from a synchronized interface must explicitly
11250 -- be declared synchronized, because the full view will be a
11251 -- synchronized type.
11252
11253 if Nkind (N) = N_Private_Extension_Declaration then
11254 if not Synchronized_Present (N) then
11255 Error_Msg_NE
11256 ("private extension of& must be explicitly synchronized",
11257 N, Iface_Id);
11258 end if;
11259
11260 -- However, by 3.9.4(16/2), a full type that is a record extension
11261 -- is never allowed to derive from a synchronized interface (note
11262 -- that interfaces must be excluded from this check, because those
11263 -- are represented by derived type definitions in some cases).
11264
11265 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition
11266 and then not Interface_Present (Type_Definition (N))
11267 then
11268 Error_Msg_N ("record extension cannot derive from synchronized "
11269 & "interface", Error_Node);
11270 end if;
11271 end if;
11272
11273 -- Check that the characteristics of the progenitor are compatible
11274 -- with the explicit qualifier in the declaration.
11275 -- The check only applies to qualifiers that come from source.
11276 -- Limited_Present also appears in the declaration of corresponding
11277 -- records, and the check does not apply to them.
11278
11279 if Limited_Present (Type_Def)
11280 and then not
11281 Is_Concurrent_Record_Type (Defining_Identifier (N))
11282 then
11283 if Is_Limited_Interface (Parent_Type)
11284 and then not Is_Limited_Interface (Iface_Id)
11285 then
11286 Error_Msg_NE
11287 ("progenitor & must be limited interface",
11288 Error_Node, Iface_Id);
11289
11290 elsif
11291 (Task_Present (Iface_Def)
11292 or else Protected_Present (Iface_Def)
11293 or else Synchronized_Present (Iface_Def))
11294 and then Nkind (N) /= N_Private_Extension_Declaration
11295 and then not Error_Posted (N)
11296 then
11297 Error_Msg_NE
11298 ("progenitor & must be limited interface",
11299 Error_Node, Iface_Id);
11300 end if;
11301
11302 -- Protected interfaces can only inherit from limited, synchronized
11303 -- or protected interfaces.
11304
11305 elsif Nkind (N) = N_Full_Type_Declaration
11306 and then Protected_Present (Type_Def)
11307 then
11308 if Limited_Present (Iface_Def)
11309 or else Synchronized_Present (Iface_Def)
11310 or else Protected_Present (Iface_Def)
11311 then
11312 null;
11313
11314 elsif Task_Present (Iface_Def) then
11315 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
11316 & "from task interface", Error_Node);
11317
11318 else
11319 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
11320 & "from non-limited interface", Error_Node);
11321 end if;
11322
11323 -- Ada 2005 (AI-345): Synchronized interfaces can only inherit from
11324 -- limited and synchronized.
11325
11326 elsif Synchronized_Present (Type_Def) then
11327 if Limited_Present (Iface_Def)
11328 or else Synchronized_Present (Iface_Def)
11329 then
11330 null;
11331
11332 elsif Protected_Present (Iface_Def)
11333 and then Nkind (N) /= N_Private_Extension_Declaration
11334 then
11335 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11336 & "from protected interface", Error_Node);
11337
11338 elsif Task_Present (Iface_Def)
11339 and then Nkind (N) /= N_Private_Extension_Declaration
11340 then
11341 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11342 & "from task interface", Error_Node);
11343
11344 elsif not Is_Limited_Interface (Iface_Id) then
11345 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11346 & "from non-limited interface", Error_Node);
11347 end if;
11348
11349 -- Ada 2005 (AI-345): Task interfaces can only inherit from limited,
11350 -- synchronized or task interfaces.
11351
11352 elsif Nkind (N) = N_Full_Type_Declaration
11353 and then Task_Present (Type_Def)
11354 then
11355 if Limited_Present (Iface_Def)
11356 or else Synchronized_Present (Iface_Def)
11357 or else Task_Present (Iface_Def)
11358 then
11359 null;
11360
11361 elsif Protected_Present (Iface_Def) then
11362 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
11363 & "protected interface", Error_Node);
11364
11365 else
11366 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
11367 & "non-limited interface", Error_Node);
11368 end if;
11369 end if;
11370 end Check_Ifaces;
11371
11372 -- Start of processing for Check_Interfaces
11373
11374 begin
11375 if Is_Interface (Parent_Type) then
11376 if Is_Task_Interface (Parent_Type) then
11377 Is_Task := True;
11378
11379 elsif Is_Protected_Interface (Parent_Type) then
11380 Is_Protected := True;
11381 end if;
11382 end if;
11383
11384 if Nkind (N) = N_Private_Extension_Declaration then
11385
11386 -- Check that progenitors are compatible with declaration
11387
11388 Iface := First (Interface_List (Def));
11389 while Present (Iface) loop
11390 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
11391
11392 Parent_Node := Parent (Base_Type (Iface_Typ));
11393 Iface_Def := Type_Definition (Parent_Node);
11394
11395 if not Is_Interface (Iface_Typ) then
11396 Diagnose_Interface (Iface, Iface_Typ);
11397 else
11398 Check_Ifaces (Iface_Def, Iface);
11399 end if;
11400
11401 Next (Iface);
11402 end loop;
11403
11404 if Is_Task and Is_Protected then
11405 Error_Msg_N
11406 ("type cannot derive from task and protected interface", N);
11407 end if;
11408
11409 return;
11410 end if;
11411
11412 -- Full type declaration of derived type.
11413 -- Check compatibility with parent if it is interface type
11414
11415 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
11416 and then Is_Interface (Parent_Type)
11417 then
11418 Parent_Node := Parent (Parent_Type);
11419
11420 -- More detailed checks for interface varieties
11421
11422 Check_Ifaces
11423 (Iface_Def => Type_Definition (Parent_Node),
11424 Error_Node => Subtype_Indication (Type_Definition (N)));
11425 end if;
11426
11427 Iface := First (Interface_List (Def));
11428 while Present (Iface) loop
11429 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
11430
11431 Parent_Node := Parent (Base_Type (Iface_Typ));
11432 Iface_Def := Type_Definition (Parent_Node);
11433
11434 if not Is_Interface (Iface_Typ) then
11435 Diagnose_Interface (Iface, Iface_Typ);
11436
11437 else
11438 -- "The declaration of a specific descendant of an interface
11439 -- type freezes the interface type" RM 13.14
11440
11441 Freeze_Before (N, Iface_Typ);
11442 Check_Ifaces (Iface_Def, Error_Node => Iface);
11443 end if;
11444
11445 Next (Iface);
11446 end loop;
11447
11448 if Is_Task and Is_Protected then
11449 Error_Msg_N
11450 ("type cannot derive from task and protected interface", N);
11451 end if;
11452 end Check_Interfaces;
11453
11454 ------------------------------------
11455 -- Check_Or_Process_Discriminants --
11456 ------------------------------------
11457
11458 -- If an incomplete or private type declaration was already given for the
11459 -- type, the discriminants may have already been processed if they were
11460 -- present on the incomplete declaration. In this case a full conformance
11461 -- check has been performed in Find_Type_Name, and we then recheck here
11462 -- some properties that can't be checked on the partial view alone.
11463 -- Otherwise we call Process_Discriminants.
11464
11465 procedure Check_Or_Process_Discriminants
11466 (N : Node_Id;
11467 T : Entity_Id;
11468 Prev : Entity_Id := Empty)
11469 is
11470 begin
11471 if Has_Discriminants (T) then
11472
11473 -- Discriminants are already set on T if they were already present
11474 -- on the partial view. Make them visible to component declarations.
11475
11476 declare
11477 D : Entity_Id;
11478 -- Discriminant on T (full view) referencing expr on partial view
11479
11480 Prev_D : Entity_Id;
11481 -- Entity of corresponding discriminant on partial view
11482
11483 New_D : Node_Id;
11484 -- Discriminant specification for full view, expression is
11485 -- the syntactic copy on full view (which has been checked for
11486 -- conformance with partial view), only used here to post error
11487 -- message.
11488
11489 begin
11490 D := First_Discriminant (T);
11491 New_D := First (Discriminant_Specifications (N));
11492 while Present (D) loop
11493 Prev_D := Current_Entity (D);
11494 Set_Current_Entity (D);
11495 Set_Is_Immediately_Visible (D);
11496 Set_Homonym (D, Prev_D);
11497
11498 -- Handle the case where there is an untagged partial view and
11499 -- the full view is tagged: must disallow discriminants with
11500 -- defaults, unless compiling for Ada 2012, which allows a
11501 -- limited tagged type to have defaulted discriminants (see
11502 -- AI05-0214). However, suppress error here if it was already
11503 -- reported on the default expression of the partial view.
11504
11505 if Is_Tagged_Type (T)
11506 and then Present (Expression (Parent (D)))
11507 and then (not Is_Limited_Type (Current_Scope)
11508 or else Ada_Version < Ada_2012)
11509 and then not Error_Posted (Expression (Parent (D)))
11510 then
11511 if Ada_Version >= Ada_2012 then
11512 Error_Msg_N
11513 ("discriminants of nonlimited tagged type cannot have "
11514 & "defaults",
11515 Expression (New_D));
11516 else
11517 Error_Msg_N
11518 ("discriminants of tagged type cannot have defaults",
11519 Expression (New_D));
11520 end if;
11521 end if;
11522
11523 -- Ada 2005 (AI-230): Access discriminant allowed in
11524 -- non-limited record types.
11525
11526 if Ada_Version < Ada_2005 then
11527
11528 -- This restriction gets applied to the full type here. It
11529 -- has already been applied earlier to the partial view.
11530
11531 Check_Access_Discriminant_Requires_Limited (Parent (D), N);
11532 end if;
11533
11534 Next_Discriminant (D);
11535 Next (New_D);
11536 end loop;
11537 end;
11538
11539 elsif Present (Discriminant_Specifications (N)) then
11540 Process_Discriminants (N, Prev);
11541 end if;
11542 end Check_Or_Process_Discriminants;
11543
11544 ----------------------
11545 -- Check_Real_Bound --
11546 ----------------------
11547
11548 procedure Check_Real_Bound (Bound : Node_Id) is
11549 begin
11550 if not Is_Real_Type (Etype (Bound)) then
11551 Error_Msg_N
11552 ("bound in real type definition must be of real type", Bound);
11553
11554 elsif not Is_OK_Static_Expression (Bound) then
11555 Flag_Non_Static_Expr
11556 ("non-static expression used for real type bound!", Bound);
11557
11558 else
11559 return;
11560 end if;
11561
11562 Rewrite
11563 (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
11564 Analyze (Bound);
11565 Resolve (Bound, Standard_Float);
11566 end Check_Real_Bound;
11567
11568 ------------------------------
11569 -- Complete_Private_Subtype --
11570 ------------------------------
11571
11572 procedure Complete_Private_Subtype
11573 (Priv : Entity_Id;
11574 Full : Entity_Id;
11575 Full_Base : Entity_Id;
11576 Related_Nod : Node_Id)
11577 is
11578 Save_Next_Entity : Entity_Id;
11579 Save_Homonym : Entity_Id;
11580
11581 begin
11582 -- Set semantic attributes for (implicit) private subtype completion.
11583 -- If the full type has no discriminants, then it is a copy of the
11584 -- full view of the base. Otherwise, it is a subtype of the base with
11585 -- a possible discriminant constraint. Save and restore the original
11586 -- Next_Entity field of full to ensure that the calls to Copy_Node do
11587 -- not corrupt the entity chain.
11588
11589 -- Note that the type of the full view is the same entity as the type
11590 -- of the partial view. In this fashion, the subtype has access to the
11591 -- correct view of the parent.
11592
11593 Save_Next_Entity := Next_Entity (Full);
11594 Save_Homonym := Homonym (Priv);
11595
11596 case Ekind (Full_Base) is
11597 when E_Record_Type |
11598 E_Record_Subtype |
11599 Class_Wide_Kind |
11600 Private_Kind |
11601 Task_Kind |
11602 Protected_Kind =>
11603 Copy_Node (Priv, Full);
11604
11605 Set_Has_Discriminants
11606 (Full, Has_Discriminants (Full_Base));
11607 Set_Has_Unknown_Discriminants
11608 (Full, Has_Unknown_Discriminants (Full_Base));
11609 Set_First_Entity (Full, First_Entity (Full_Base));
11610 Set_Last_Entity (Full, Last_Entity (Full_Base));
11611
11612 -- If the underlying base type is constrained, we know that the
11613 -- full view of the subtype is constrained as well (the converse
11614 -- is not necessarily true).
11615
11616 if Is_Constrained (Full_Base) then
11617 Set_Is_Constrained (Full);
11618 end if;
11619
11620 when others =>
11621 Copy_Node (Full_Base, Full);
11622
11623 Set_Chars (Full, Chars (Priv));
11624 Conditional_Delay (Full, Priv);
11625 Set_Sloc (Full, Sloc (Priv));
11626 end case;
11627
11628 Set_Next_Entity (Full, Save_Next_Entity);
11629 Set_Homonym (Full, Save_Homonym);
11630 Set_Associated_Node_For_Itype (Full, Related_Nod);
11631
11632 -- Set common attributes for all subtypes: kind, convention, etc.
11633
11634 Set_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
11635 Set_Convention (Full, Convention (Full_Base));
11636
11637 -- The Etype of the full view is inconsistent. Gigi needs to see the
11638 -- structural full view, which is what the current scheme gives: the
11639 -- Etype of the full view is the etype of the full base. However, if the
11640 -- full base is a derived type, the full view then looks like a subtype
11641 -- of the parent, not a subtype of the full base. If instead we write:
11642
11643 -- Set_Etype (Full, Full_Base);
11644
11645 -- then we get inconsistencies in the front-end (confusion between
11646 -- views). Several outstanding bugs are related to this ???
11647
11648 Set_Is_First_Subtype (Full, False);
11649 Set_Scope (Full, Scope (Priv));
11650 Set_Size_Info (Full, Full_Base);
11651 Set_RM_Size (Full, RM_Size (Full_Base));
11652 Set_Is_Itype (Full);
11653
11654 -- A subtype of a private-type-without-discriminants, whose full-view
11655 -- has discriminants with default expressions, is not constrained.
11656
11657 if not Has_Discriminants (Priv) then
11658 Set_Is_Constrained (Full, Is_Constrained (Full_Base));
11659
11660 if Has_Discriminants (Full_Base) then
11661 Set_Discriminant_Constraint
11662 (Full, Discriminant_Constraint (Full_Base));
11663
11664 -- The partial view may have been indefinite, the full view
11665 -- might not be.
11666
11667 Set_Has_Unknown_Discriminants
11668 (Full, Has_Unknown_Discriminants (Full_Base));
11669 end if;
11670 end if;
11671
11672 Set_First_Rep_Item (Full, First_Rep_Item (Full_Base));
11673 Set_Depends_On_Private (Full, Has_Private_Component (Full));
11674
11675 -- Freeze the private subtype entity if its parent is delayed, and not
11676 -- already frozen. We skip this processing if the type is an anonymous
11677 -- subtype of a record component, or is the corresponding record of a
11678 -- protected type, since these are processed when the enclosing type
11679 -- is frozen.
11680
11681 if not Is_Type (Scope (Full)) then
11682 Set_Has_Delayed_Freeze (Full,
11683 Has_Delayed_Freeze (Full_Base)
11684 and then (not Is_Frozen (Full_Base)));
11685 end if;
11686
11687 Set_Freeze_Node (Full, Empty);
11688 Set_Is_Frozen (Full, False);
11689 Set_Full_View (Priv, Full);
11690
11691 if Has_Discriminants (Full) then
11692 Set_Stored_Constraint_From_Discriminant_Constraint (Full);
11693 Set_Stored_Constraint (Priv, Stored_Constraint (Full));
11694
11695 if Has_Unknown_Discriminants (Full) then
11696 Set_Discriminant_Constraint (Full, No_Elist);
11697 end if;
11698 end if;
11699
11700 if Ekind (Full_Base) = E_Record_Type
11701 and then Has_Discriminants (Full_Base)
11702 and then Has_Discriminants (Priv) -- might not, if errors
11703 and then not Has_Unknown_Discriminants (Priv)
11704 and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
11705 then
11706 Create_Constrained_Components
11707 (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
11708
11709 -- If the full base is itself derived from private, build a congruent
11710 -- subtype of its underlying type, for use by the back end. For a
11711 -- constrained record component, the declaration cannot be placed on
11712 -- the component list, but it must nevertheless be built an analyzed, to
11713 -- supply enough information for Gigi to compute the size of component.
11714
11715 elsif Ekind (Full_Base) in Private_Kind
11716 and then Is_Derived_Type (Full_Base)
11717 and then Has_Discriminants (Full_Base)
11718 and then (Ekind (Current_Scope) /= E_Record_Subtype)
11719 then
11720 if not Is_Itype (Priv)
11721 and then
11722 Nkind (Subtype_Indication (Parent (Priv))) = N_Subtype_Indication
11723 then
11724 Build_Underlying_Full_View
11725 (Parent (Priv), Full, Etype (Full_Base));
11726
11727 elsif Nkind (Related_Nod) = N_Component_Declaration then
11728 Build_Underlying_Full_View (Related_Nod, Full, Etype (Full_Base));
11729 end if;
11730
11731 elsif Is_Record_Type (Full_Base) then
11732
11733 -- Show Full is simply a renaming of Full_Base
11734
11735 Set_Cloned_Subtype (Full, Full_Base);
11736 end if;
11737
11738 -- It is unsafe to share the bounds of a scalar type, because the Itype
11739 -- is elaborated on demand, and if a bound is non-static then different
11740 -- orders of elaboration in different units will lead to different
11741 -- external symbols.
11742
11743 if Is_Scalar_Type (Full_Base) then
11744 Set_Scalar_Range (Full,
11745 Make_Range (Sloc (Related_Nod),
11746 Low_Bound =>
11747 Duplicate_Subexpr_No_Checks (Type_Low_Bound (Full_Base)),
11748 High_Bound =>
11749 Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
11750
11751 -- This completion inherits the bounds of the full parent, but if
11752 -- the parent is an unconstrained floating point type, so is the
11753 -- completion.
11754
11755 if Is_Floating_Point_Type (Full_Base) then
11756 Set_Includes_Infinities
11757 (Scalar_Range (Full), Has_Infinities (Full_Base));
11758 end if;
11759 end if;
11760
11761 -- ??? It seems that a lot of fields are missing that should be copied
11762 -- from Full_Base to Full. Here are some that are introduced in a
11763 -- non-disruptive way but a cleanup is necessary.
11764
11765 if Is_Tagged_Type (Full_Base) then
11766 Set_Is_Tagged_Type (Full);
11767 Set_Direct_Primitive_Operations
11768 (Full, Direct_Primitive_Operations (Full_Base));
11769 Set_No_Tagged_Streams_Pragma
11770 (Full, No_Tagged_Streams_Pragma (Full_Base));
11771
11772 -- Inherit class_wide type of full_base in case the partial view was
11773 -- not tagged. Otherwise it has already been created when the private
11774 -- subtype was analyzed.
11775
11776 if No (Class_Wide_Type (Full)) then
11777 Set_Class_Wide_Type (Full, Class_Wide_Type (Full_Base));
11778 end if;
11779
11780 -- If this is a subtype of a protected or task type, constrain its
11781 -- corresponding record, unless this is a subtype without constraints,
11782 -- i.e. a simple renaming as with an actual subtype in an instance.
11783
11784 elsif Is_Concurrent_Type (Full_Base) then
11785 if Has_Discriminants (Full)
11786 and then Present (Corresponding_Record_Type (Full_Base))
11787 and then
11788 not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
11789 then
11790 Set_Corresponding_Record_Type (Full,
11791 Constrain_Corresponding_Record
11792 (Full, Corresponding_Record_Type (Full_Base), Related_Nod));
11793
11794 else
11795 Set_Corresponding_Record_Type (Full,
11796 Corresponding_Record_Type (Full_Base));
11797 end if;
11798 end if;
11799
11800 -- Link rep item chain, and also setting of Has_Predicates from private
11801 -- subtype to full subtype, since we will need these on the full subtype
11802 -- to create the predicate function. Note that the full subtype may
11803 -- already have rep items, inherited from the full view of the base
11804 -- type, so we must be sure not to overwrite these entries.
11805
11806 declare
11807 Append : Boolean;
11808 Item : Node_Id;
11809 Next_Item : Node_Id;
11810
11811 begin
11812 Item := First_Rep_Item (Full);
11813
11814 -- If no existing rep items on full type, we can just link directly
11815 -- to the list of items on the private type, if any exist.. Same if
11816 -- the rep items are only those inherited from the base
11817
11818 if (No (Item)
11819 or else Nkind (Item) /= N_Aspect_Specification
11820 or else Entity (Item) = Full_Base)
11821 and then Present (First_Rep_Item (Priv))
11822 then
11823 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
11824
11825 -- Otherwise, search to the end of items currently linked to the full
11826 -- subtype and append the private items to the end. However, if Priv
11827 -- and Full already have the same list of rep items, then the append
11828 -- is not done, as that would create a circularity.
11829
11830 elsif Item /= First_Rep_Item (Priv) then
11831 Append := True;
11832 loop
11833 Next_Item := Next_Rep_Item (Item);
11834 exit when No (Next_Item);
11835 Item := Next_Item;
11836
11837 -- If the private view has aspect specifications, the full view
11838 -- inherits them. Since these aspects may already have been
11839 -- attached to the full view during derivation, do not append
11840 -- them if already present.
11841
11842 if Item = First_Rep_Item (Priv) then
11843 Append := False;
11844 exit;
11845 end if;
11846 end loop;
11847
11848 -- And link the private type items at the end of the chain
11849
11850 if Append then
11851 Set_Next_Rep_Item (Item, First_Rep_Item (Priv));
11852 end if;
11853 end if;
11854 end;
11855
11856 -- Make sure Has_Predicates is set on full type if it is set on the
11857 -- private type. Note that it may already be set on the full type and
11858 -- if so, we don't want to unset it. Similarly, propagate information
11859 -- about delayed aspects, because the corresponding pragmas must be
11860 -- analyzed when one of the views is frozen. This last step is needed
11861 -- in particular when the full type is a scalar type for which an
11862 -- anonymous base type is constructed.
11863
11864 if Has_Predicates (Priv) then
11865 Set_Has_Predicates (Full);
11866 end if;
11867
11868 if Has_Delayed_Aspects (Priv) then
11869 Set_Has_Delayed_Aspects (Full);
11870 end if;
11871 end Complete_Private_Subtype;
11872
11873 ----------------------------
11874 -- Constant_Redeclaration --
11875 ----------------------------
11876
11877 procedure Constant_Redeclaration
11878 (Id : Entity_Id;
11879 N : Node_Id;
11880 T : out Entity_Id)
11881 is
11882 Prev : constant Entity_Id := Current_Entity_In_Scope (Id);
11883 Obj_Def : constant Node_Id := Object_Definition (N);
11884 New_T : Entity_Id;
11885
11886 procedure Check_Possible_Deferred_Completion
11887 (Prev_Id : Entity_Id;
11888 Prev_Obj_Def : Node_Id;
11889 Curr_Obj_Def : Node_Id);
11890 -- Determine whether the two object definitions describe the partial
11891 -- and the full view of a constrained deferred constant. Generate
11892 -- a subtype for the full view and verify that it statically matches
11893 -- the subtype of the partial view.
11894
11895 procedure Check_Recursive_Declaration (Typ : Entity_Id);
11896 -- If deferred constant is an access type initialized with an allocator,
11897 -- check whether there is an illegal recursion in the definition,
11898 -- through a default value of some record subcomponent. This is normally
11899 -- detected when generating init procs, but requires this additional
11900 -- mechanism when expansion is disabled.
11901
11902 ----------------------------------------
11903 -- Check_Possible_Deferred_Completion --
11904 ----------------------------------------
11905
11906 procedure Check_Possible_Deferred_Completion
11907 (Prev_Id : Entity_Id;
11908 Prev_Obj_Def : Node_Id;
11909 Curr_Obj_Def : Node_Id)
11910 is
11911 begin
11912 if Nkind (Prev_Obj_Def) = N_Subtype_Indication
11913 and then Present (Constraint (Prev_Obj_Def))
11914 and then Nkind (Curr_Obj_Def) = N_Subtype_Indication
11915 and then Present (Constraint (Curr_Obj_Def))
11916 then
11917 declare
11918 Loc : constant Source_Ptr := Sloc (N);
11919 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
11920 Decl : constant Node_Id :=
11921 Make_Subtype_Declaration (Loc,
11922 Defining_Identifier => Def_Id,
11923 Subtype_Indication =>
11924 Relocate_Node (Curr_Obj_Def));
11925
11926 begin
11927 Insert_Before_And_Analyze (N, Decl);
11928 Set_Etype (Id, Def_Id);
11929
11930 if not Subtypes_Statically_Match (Etype (Prev_Id), Def_Id) then
11931 Error_Msg_Sloc := Sloc (Prev_Id);
11932 Error_Msg_N ("subtype does not statically match deferred "
11933 & "declaration #", N);
11934 end if;
11935 end;
11936 end if;
11937 end Check_Possible_Deferred_Completion;
11938
11939 ---------------------------------
11940 -- Check_Recursive_Declaration --
11941 ---------------------------------
11942
11943 procedure Check_Recursive_Declaration (Typ : Entity_Id) is
11944 Comp : Entity_Id;
11945
11946 begin
11947 if Is_Record_Type (Typ) then
11948 Comp := First_Component (Typ);
11949 while Present (Comp) loop
11950 if Comes_From_Source (Comp) then
11951 if Present (Expression (Parent (Comp)))
11952 and then Is_Entity_Name (Expression (Parent (Comp)))
11953 and then Entity (Expression (Parent (Comp))) = Prev
11954 then
11955 Error_Msg_Sloc := Sloc (Parent (Comp));
11956 Error_Msg_NE
11957 ("illegal circularity with declaration for & #",
11958 N, Comp);
11959 return;
11960
11961 elsif Is_Record_Type (Etype (Comp)) then
11962 Check_Recursive_Declaration (Etype (Comp));
11963 end if;
11964 end if;
11965
11966 Next_Component (Comp);
11967 end loop;
11968 end if;
11969 end Check_Recursive_Declaration;
11970
11971 -- Start of processing for Constant_Redeclaration
11972
11973 begin
11974 if Nkind (Parent (Prev)) = N_Object_Declaration then
11975 if Nkind (Object_Definition
11976 (Parent (Prev))) = N_Subtype_Indication
11977 then
11978 -- Find type of new declaration. The constraints of the two
11979 -- views must match statically, but there is no point in
11980 -- creating an itype for the full view.
11981
11982 if Nkind (Obj_Def) = N_Subtype_Indication then
11983 Find_Type (Subtype_Mark (Obj_Def));
11984 New_T := Entity (Subtype_Mark (Obj_Def));
11985
11986 else
11987 Find_Type (Obj_Def);
11988 New_T := Entity (Obj_Def);
11989 end if;
11990
11991 T := Etype (Prev);
11992
11993 else
11994 -- The full view may impose a constraint, even if the partial
11995 -- view does not, so construct the subtype.
11996
11997 New_T := Find_Type_Of_Object (Obj_Def, N);
11998 T := New_T;
11999 end if;
12000
12001 else
12002 -- Current declaration is illegal, diagnosed below in Enter_Name
12003
12004 T := Empty;
12005 New_T := Any_Type;
12006 end if;
12007
12008 -- If previous full declaration or a renaming declaration exists, or if
12009 -- a homograph is present, let Enter_Name handle it, either with an
12010 -- error or with the removal of an overridden implicit subprogram.
12011 -- The previous one is a full declaration if it has an expression
12012 -- (which in the case of an aggregate is indicated by the Init flag).
12013
12014 if Ekind (Prev) /= E_Constant
12015 or else Nkind (Parent (Prev)) = N_Object_Renaming_Declaration
12016 or else Present (Expression (Parent (Prev)))
12017 or else Has_Init_Expression (Parent (Prev))
12018 or else Present (Full_View (Prev))
12019 then
12020 Enter_Name (Id);
12021
12022 -- Verify that types of both declarations match, or else that both types
12023 -- are anonymous access types whose designated subtypes statically match
12024 -- (as allowed in Ada 2005 by AI-385).
12025
12026 elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
12027 and then
12028 (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
12029 or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
12030 or else Is_Access_Constant (Etype (New_T)) /=
12031 Is_Access_Constant (Etype (Prev))
12032 or else Can_Never_Be_Null (Etype (New_T)) /=
12033 Can_Never_Be_Null (Etype (Prev))
12034 or else Null_Exclusion_Present (Parent (Prev)) /=
12035 Null_Exclusion_Present (Parent (Id))
12036 or else not Subtypes_Statically_Match
12037 (Designated_Type (Etype (Prev)),
12038 Designated_Type (Etype (New_T))))
12039 then
12040 Error_Msg_Sloc := Sloc (Prev);
12041 Error_Msg_N ("type does not match declaration#", N);
12042 Set_Full_View (Prev, Id);
12043 Set_Etype (Id, Any_Type);
12044
12045 -- A deferred constant whose type is an anonymous array is always
12046 -- illegal (unless imported). A detailed error message might be
12047 -- helpful for Ada beginners.
12048
12049 if Nkind (Object_Definition (Parent (Prev)))
12050 = N_Constrained_Array_Definition
12051 and then Nkind (Object_Definition (N))
12052 = N_Constrained_Array_Definition
12053 then
12054 Error_Msg_N ("\each anonymous array is a distinct type", N);
12055 Error_Msg_N ("a deferred constant must have a named type",
12056 Object_Definition (Parent (Prev)));
12057 end if;
12058
12059 elsif
12060 Null_Exclusion_Present (Parent (Prev))
12061 and then not Null_Exclusion_Present (N)
12062 then
12063 Error_Msg_Sloc := Sloc (Prev);
12064 Error_Msg_N ("null-exclusion does not match declaration#", N);
12065 Set_Full_View (Prev, Id);
12066 Set_Etype (Id, Any_Type);
12067
12068 -- If so, process the full constant declaration
12069
12070 else
12071 -- RM 7.4 (6): If the subtype defined by the subtype_indication in
12072 -- the deferred declaration is constrained, then the subtype defined
12073 -- by the subtype_indication in the full declaration shall match it
12074 -- statically.
12075
12076 Check_Possible_Deferred_Completion
12077 (Prev_Id => Prev,
12078 Prev_Obj_Def => Object_Definition (Parent (Prev)),
12079 Curr_Obj_Def => Obj_Def);
12080
12081 Set_Full_View (Prev, Id);
12082 Set_Is_Public (Id, Is_Public (Prev));
12083 Set_Is_Internal (Id);
12084 Append_Entity (Id, Current_Scope);
12085
12086 -- Check ALIASED present if present before (RM 7.4(7))
12087
12088 if Is_Aliased (Prev)
12089 and then not Aliased_Present (N)
12090 then
12091 Error_Msg_Sloc := Sloc (Prev);
12092 Error_Msg_N ("ALIASED required (see declaration #)", N);
12093 end if;
12094
12095 -- Check that placement is in private part and that the incomplete
12096 -- declaration appeared in the visible part.
12097
12098 if Ekind (Current_Scope) = E_Package
12099 and then not In_Private_Part (Current_Scope)
12100 then
12101 Error_Msg_Sloc := Sloc (Prev);
12102 Error_Msg_N
12103 ("full constant for declaration # must be in private part", N);
12104
12105 elsif Ekind (Current_Scope) = E_Package
12106 and then
12107 List_Containing (Parent (Prev)) /=
12108 Visible_Declarations (Package_Specification (Current_Scope))
12109 then
12110 Error_Msg_N
12111 ("deferred constant must be declared in visible part",
12112 Parent (Prev));
12113 end if;
12114
12115 if Is_Access_Type (T)
12116 and then Nkind (Expression (N)) = N_Allocator
12117 then
12118 Check_Recursive_Declaration (Designated_Type (T));
12119 end if;
12120
12121 -- A deferred constant is a visible entity. If type has invariants,
12122 -- verify that the initial value satisfies them.
12123
12124 if Has_Invariants (T) and then Present (Invariant_Procedure (T)) then
12125 Insert_After (N,
12126 Make_Invariant_Call (New_Occurrence_Of (Prev, Sloc (N))));
12127 end if;
12128 end if;
12129 end Constant_Redeclaration;
12130
12131 ----------------------
12132 -- Constrain_Access --
12133 ----------------------
12134
12135 procedure Constrain_Access
12136 (Def_Id : in out Entity_Id;
12137 S : Node_Id;
12138 Related_Nod : Node_Id)
12139 is
12140 T : constant Entity_Id := Entity (Subtype_Mark (S));
12141 Desig_Type : constant Entity_Id := Designated_Type (T);
12142 Desig_Subtype : Entity_Id := Create_Itype (E_Void, Related_Nod);
12143 Constraint_OK : Boolean := True;
12144
12145 begin
12146 if Is_Array_Type (Desig_Type) then
12147 Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
12148
12149 elsif (Is_Record_Type (Desig_Type)
12150 or else Is_Incomplete_Or_Private_Type (Desig_Type))
12151 and then not Is_Constrained (Desig_Type)
12152 then
12153 -- ??? The following code is a temporary bypass to ignore a
12154 -- discriminant constraint on access type if it is constraining
12155 -- the current record. Avoid creating the implicit subtype of the
12156 -- record we are currently compiling since right now, we cannot
12157 -- handle these. For now, just return the access type itself.
12158
12159 if Desig_Type = Current_Scope
12160 and then No (Def_Id)
12161 then
12162 Set_Ekind (Desig_Subtype, E_Record_Subtype);
12163 Def_Id := Entity (Subtype_Mark (S));
12164
12165 -- This call added to ensure that the constraint is analyzed
12166 -- (needed for a B test). Note that we still return early from
12167 -- this procedure to avoid recursive processing. ???
12168
12169 Constrain_Discriminated_Type
12170 (Desig_Subtype, S, Related_Nod, For_Access => True);
12171 return;
12172 end if;
12173
12174 -- Enforce rule that the constraint is illegal if there is an
12175 -- unconstrained view of the designated type. This means that the
12176 -- partial view (either a private type declaration or a derivation
12177 -- from a private type) has no discriminants. (Defect Report
12178 -- 8652/0008, Technical Corrigendum 1, checked by ACATS B371001).
12179
12180 -- Rule updated for Ada 2005: The private type is said to have
12181 -- a constrained partial view, given that objects of the type
12182 -- can be declared. Furthermore, the rule applies to all access
12183 -- types, unlike the rule concerning default discriminants (see
12184 -- RM 3.7.1(7/3))
12185
12186 if (Ekind (T) = E_General_Access_Type or else Ada_Version >= Ada_2005)
12187 and then Has_Private_Declaration (Desig_Type)
12188 and then In_Open_Scopes (Scope (Desig_Type))
12189 and then Has_Discriminants (Desig_Type)
12190 then
12191 declare
12192 Pack : constant Node_Id :=
12193 Unit_Declaration_Node (Scope (Desig_Type));
12194 Decls : List_Id;
12195 Decl : Node_Id;
12196
12197 begin
12198 if Nkind (Pack) = N_Package_Declaration then
12199 Decls := Visible_Declarations (Specification (Pack));
12200 Decl := First (Decls);
12201 while Present (Decl) loop
12202 if (Nkind (Decl) = N_Private_Type_Declaration
12203 and then Chars (Defining_Identifier (Decl)) =
12204 Chars (Desig_Type))
12205
12206 or else
12207 (Nkind (Decl) = N_Full_Type_Declaration
12208 and then
12209 Chars (Defining_Identifier (Decl)) =
12210 Chars (Desig_Type)
12211 and then Is_Derived_Type (Desig_Type)
12212 and then
12213 Has_Private_Declaration (Etype (Desig_Type)))
12214 then
12215 if No (Discriminant_Specifications (Decl)) then
12216 Error_Msg_N
12217 ("cannot constrain access type if designated "
12218 & "type has constrained partial view", S);
12219 end if;
12220
12221 exit;
12222 end if;
12223
12224 Next (Decl);
12225 end loop;
12226 end if;
12227 end;
12228 end if;
12229
12230 Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
12231 For_Access => True);
12232
12233 elsif Is_Concurrent_Type (Desig_Type)
12234 and then not Is_Constrained (Desig_Type)
12235 then
12236 Constrain_Concurrent (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
12237
12238 else
12239 Error_Msg_N ("invalid constraint on access type", S);
12240
12241 -- We simply ignore an invalid constraint
12242
12243 Desig_Subtype := Desig_Type;
12244 Constraint_OK := False;
12245 end if;
12246
12247 if No (Def_Id) then
12248 Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
12249 else
12250 Set_Ekind (Def_Id, E_Access_Subtype);
12251 end if;
12252
12253 if Constraint_OK then
12254 Set_Etype (Def_Id, Base_Type (T));
12255
12256 if Is_Private_Type (Desig_Type) then
12257 Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
12258 end if;
12259 else
12260 Set_Etype (Def_Id, Any_Type);
12261 end if;
12262
12263 Set_Size_Info (Def_Id, T);
12264 Set_Is_Constrained (Def_Id, Constraint_OK);
12265 Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
12266 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
12267 Set_Is_Access_Constant (Def_Id, Is_Access_Constant (T));
12268
12269 Conditional_Delay (Def_Id, T);
12270
12271 -- AI-363 : Subtypes of general access types whose designated types have
12272 -- default discriminants are disallowed. In instances, the rule has to
12273 -- be checked against the actual, of which T is the subtype. In a
12274 -- generic body, the rule is checked assuming that the actual type has
12275 -- defaulted discriminants.
12276
12277 if Ada_Version >= Ada_2005 or else Warn_On_Ada_2005_Compatibility then
12278 if Ekind (Base_Type (T)) = E_General_Access_Type
12279 and then Has_Defaulted_Discriminants (Desig_Type)
12280 then
12281 if Ada_Version < Ada_2005 then
12282 Error_Msg_N
12283 ("access subtype of general access type would not " &
12284 "be allowed in Ada 2005?y?", S);
12285 else
12286 Error_Msg_N
12287 ("access subtype of general access type not allowed", S);
12288 end if;
12289
12290 Error_Msg_N ("\discriminants have defaults", S);
12291
12292 elsif Is_Access_Type (T)
12293 and then Is_Generic_Type (Desig_Type)
12294 and then Has_Discriminants (Desig_Type)
12295 and then In_Package_Body (Current_Scope)
12296 then
12297 if Ada_Version < Ada_2005 then
12298 Error_Msg_N
12299 ("access subtype would not be allowed in generic body "
12300 & "in Ada 2005?y?", S);
12301 else
12302 Error_Msg_N
12303 ("access subtype not allowed in generic body", S);
12304 end if;
12305
12306 Error_Msg_N
12307 ("\designated type is a discriminated formal", S);
12308 end if;
12309 end if;
12310 end Constrain_Access;
12311
12312 ---------------------
12313 -- Constrain_Array --
12314 ---------------------
12315
12316 procedure Constrain_Array
12317 (Def_Id : in out Entity_Id;
12318 SI : Node_Id;
12319 Related_Nod : Node_Id;
12320 Related_Id : Entity_Id;
12321 Suffix : Character)
12322 is
12323 C : constant Node_Id := Constraint (SI);
12324 Number_Of_Constraints : Nat := 0;
12325 Index : Node_Id;
12326 S, T : Entity_Id;
12327 Constraint_OK : Boolean := True;
12328
12329 begin
12330 T := Entity (Subtype_Mark (SI));
12331
12332 if Is_Access_Type (T) then
12333 T := Designated_Type (T);
12334 end if;
12335
12336 -- If an index constraint follows a subtype mark in a subtype indication
12337 -- then the type or subtype denoted by the subtype mark must not already
12338 -- impose an index constraint. The subtype mark must denote either an
12339 -- unconstrained array type or an access type whose designated type
12340 -- is such an array type... (RM 3.6.1)
12341
12342 if Is_Constrained (T) then
12343 Error_Msg_N ("array type is already constrained", Subtype_Mark (SI));
12344 Constraint_OK := False;
12345
12346 else
12347 S := First (Constraints (C));
12348 while Present (S) loop
12349 Number_Of_Constraints := Number_Of_Constraints + 1;
12350 Next (S);
12351 end loop;
12352
12353 -- In either case, the index constraint must provide a discrete
12354 -- range for each index of the array type and the type of each
12355 -- discrete range must be the same as that of the corresponding
12356 -- index. (RM 3.6.1)
12357
12358 if Number_Of_Constraints /= Number_Dimensions (T) then
12359 Error_Msg_NE ("incorrect number of index constraints for }", C, T);
12360 Constraint_OK := False;
12361
12362 else
12363 S := First (Constraints (C));
12364 Index := First_Index (T);
12365 Analyze (Index);
12366
12367 -- Apply constraints to each index type
12368
12369 for J in 1 .. Number_Of_Constraints loop
12370 Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
12371 Next (Index);
12372 Next (S);
12373 end loop;
12374
12375 end if;
12376 end if;
12377
12378 if No (Def_Id) then
12379 Def_Id :=
12380 Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
12381 Set_Parent (Def_Id, Related_Nod);
12382
12383 else
12384 Set_Ekind (Def_Id, E_Array_Subtype);
12385 end if;
12386
12387 Set_Size_Info (Def_Id, (T));
12388 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
12389 Set_Etype (Def_Id, Base_Type (T));
12390
12391 if Constraint_OK then
12392 Set_First_Index (Def_Id, First (Constraints (C)));
12393 else
12394 Set_First_Index (Def_Id, First_Index (T));
12395 end if;
12396
12397 Set_Is_Constrained (Def_Id, True);
12398 Set_Is_Aliased (Def_Id, Is_Aliased (T));
12399 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
12400
12401 Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
12402 Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
12403
12404 -- A subtype does not inherit the Packed_Array_Impl_Type of is parent.
12405 -- We need to initialize the attribute because if Def_Id is previously
12406 -- analyzed through a limited_with clause, it will have the attributes
12407 -- of an incomplete type, one of which is an Elist that overlaps the
12408 -- Packed_Array_Impl_Type field.
12409
12410 Set_Packed_Array_Impl_Type (Def_Id, Empty);
12411
12412 -- Build a freeze node if parent still needs one. Also make sure that
12413 -- the Depends_On_Private status is set because the subtype will need
12414 -- reprocessing at the time the base type does, and also we must set a
12415 -- conditional delay.
12416
12417 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
12418 Conditional_Delay (Def_Id, T);
12419 end Constrain_Array;
12420
12421 ------------------------------
12422 -- Constrain_Component_Type --
12423 ------------------------------
12424
12425 function Constrain_Component_Type
12426 (Comp : Entity_Id;
12427 Constrained_Typ : Entity_Id;
12428 Related_Node : Node_Id;
12429 Typ : Entity_Id;
12430 Constraints : Elist_Id) return Entity_Id
12431 is
12432 Loc : constant Source_Ptr := Sloc (Constrained_Typ);
12433 Compon_Type : constant Entity_Id := Etype (Comp);
12434
12435 function Build_Constrained_Array_Type
12436 (Old_Type : Entity_Id) return Entity_Id;
12437 -- If Old_Type is an array type, one of whose indexes is constrained
12438 -- by a discriminant, build an Itype whose constraint replaces the
12439 -- discriminant with its value in the constraint.
12440
12441 function Build_Constrained_Discriminated_Type
12442 (Old_Type : Entity_Id) return Entity_Id;
12443 -- Ditto for record components
12444
12445 function Build_Constrained_Access_Type
12446 (Old_Type : Entity_Id) return Entity_Id;
12447 -- Ditto for access types. Makes use of previous two functions, to
12448 -- constrain designated type.
12449
12450 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id;
12451 -- T is an array or discriminated type, C is a list of constraints
12452 -- that apply to T. This routine builds the constrained subtype.
12453
12454 function Is_Discriminant (Expr : Node_Id) return Boolean;
12455 -- Returns True if Expr is a discriminant
12456
12457 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id;
12458 -- Find the value of discriminant Discrim in Constraint
12459
12460 -----------------------------------
12461 -- Build_Constrained_Access_Type --
12462 -----------------------------------
12463
12464 function Build_Constrained_Access_Type
12465 (Old_Type : Entity_Id) return Entity_Id
12466 is
12467 Desig_Type : constant Entity_Id := Designated_Type (Old_Type);
12468 Itype : Entity_Id;
12469 Desig_Subtype : Entity_Id;
12470 Scop : Entity_Id;
12471
12472 begin
12473 -- if the original access type was not embedded in the enclosing
12474 -- type definition, there is no need to produce a new access
12475 -- subtype. In fact every access type with an explicit constraint
12476 -- generates an itype whose scope is the enclosing record.
12477
12478 if not Is_Type (Scope (Old_Type)) then
12479 return Old_Type;
12480
12481 elsif Is_Array_Type (Desig_Type) then
12482 Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
12483
12484 elsif Has_Discriminants (Desig_Type) then
12485
12486 -- This may be an access type to an enclosing record type for
12487 -- which we are constructing the constrained components. Return
12488 -- the enclosing record subtype. This is not always correct,
12489 -- but avoids infinite recursion. ???
12490
12491 Desig_Subtype := Any_Type;
12492
12493 for J in reverse 0 .. Scope_Stack.Last loop
12494 Scop := Scope_Stack.Table (J).Entity;
12495
12496 if Is_Type (Scop)
12497 and then Base_Type (Scop) = Base_Type (Desig_Type)
12498 then
12499 Desig_Subtype := Scop;
12500 end if;
12501
12502 exit when not Is_Type (Scop);
12503 end loop;
12504
12505 if Desig_Subtype = Any_Type then
12506 Desig_Subtype :=
12507 Build_Constrained_Discriminated_Type (Desig_Type);
12508 end if;
12509
12510 else
12511 return Old_Type;
12512 end if;
12513
12514 if Desig_Subtype /= Desig_Type then
12515
12516 -- The Related_Node better be here or else we won't be able
12517 -- to attach new itypes to a node in the tree.
12518
12519 pragma Assert (Present (Related_Node));
12520
12521 Itype := Create_Itype (E_Access_Subtype, Related_Node);
12522
12523 Set_Etype (Itype, Base_Type (Old_Type));
12524 Set_Size_Info (Itype, (Old_Type));
12525 Set_Directly_Designated_Type (Itype, Desig_Subtype);
12526 Set_Depends_On_Private (Itype, Has_Private_Component
12527 (Old_Type));
12528 Set_Is_Access_Constant (Itype, Is_Access_Constant
12529 (Old_Type));
12530
12531 -- The new itype needs freezing when it depends on a not frozen
12532 -- type and the enclosing subtype needs freezing.
12533
12534 if Has_Delayed_Freeze (Constrained_Typ)
12535 and then not Is_Frozen (Constrained_Typ)
12536 then
12537 Conditional_Delay (Itype, Base_Type (Old_Type));
12538 end if;
12539
12540 return Itype;
12541
12542 else
12543 return Old_Type;
12544 end if;
12545 end Build_Constrained_Access_Type;
12546
12547 ----------------------------------
12548 -- Build_Constrained_Array_Type --
12549 ----------------------------------
12550
12551 function Build_Constrained_Array_Type
12552 (Old_Type : Entity_Id) return Entity_Id
12553 is
12554 Lo_Expr : Node_Id;
12555 Hi_Expr : Node_Id;
12556 Old_Index : Node_Id;
12557 Range_Node : Node_Id;
12558 Constr_List : List_Id;
12559
12560 Need_To_Create_Itype : Boolean := False;
12561
12562 begin
12563 Old_Index := First_Index (Old_Type);
12564 while Present (Old_Index) loop
12565 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
12566
12567 if Is_Discriminant (Lo_Expr)
12568 or else
12569 Is_Discriminant (Hi_Expr)
12570 then
12571 Need_To_Create_Itype := True;
12572 end if;
12573
12574 Next_Index (Old_Index);
12575 end loop;
12576
12577 if Need_To_Create_Itype then
12578 Constr_List := New_List;
12579
12580 Old_Index := First_Index (Old_Type);
12581 while Present (Old_Index) loop
12582 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
12583
12584 if Is_Discriminant (Lo_Expr) then
12585 Lo_Expr := Get_Discr_Value (Lo_Expr);
12586 end if;
12587
12588 if Is_Discriminant (Hi_Expr) then
12589 Hi_Expr := Get_Discr_Value (Hi_Expr);
12590 end if;
12591
12592 Range_Node :=
12593 Make_Range
12594 (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
12595
12596 Append (Range_Node, To => Constr_List);
12597
12598 Next_Index (Old_Index);
12599 end loop;
12600
12601 return Build_Subtype (Old_Type, Constr_List);
12602
12603 else
12604 return Old_Type;
12605 end if;
12606 end Build_Constrained_Array_Type;
12607
12608 ------------------------------------------
12609 -- Build_Constrained_Discriminated_Type --
12610 ------------------------------------------
12611
12612 function Build_Constrained_Discriminated_Type
12613 (Old_Type : Entity_Id) return Entity_Id
12614 is
12615 Expr : Node_Id;
12616 Constr_List : List_Id;
12617 Old_Constraint : Elmt_Id;
12618
12619 Need_To_Create_Itype : Boolean := False;
12620
12621 begin
12622 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
12623 while Present (Old_Constraint) loop
12624 Expr := Node (Old_Constraint);
12625
12626 if Is_Discriminant (Expr) then
12627 Need_To_Create_Itype := True;
12628 end if;
12629
12630 Next_Elmt (Old_Constraint);
12631 end loop;
12632
12633 if Need_To_Create_Itype then
12634 Constr_List := New_List;
12635
12636 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
12637 while Present (Old_Constraint) loop
12638 Expr := Node (Old_Constraint);
12639
12640 if Is_Discriminant (Expr) then
12641 Expr := Get_Discr_Value (Expr);
12642 end if;
12643
12644 Append (New_Copy_Tree (Expr), To => Constr_List);
12645
12646 Next_Elmt (Old_Constraint);
12647 end loop;
12648
12649 return Build_Subtype (Old_Type, Constr_List);
12650
12651 else
12652 return Old_Type;
12653 end if;
12654 end Build_Constrained_Discriminated_Type;
12655
12656 -------------------
12657 -- Build_Subtype --
12658 -------------------
12659
12660 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id is
12661 Indic : Node_Id;
12662 Subtyp_Decl : Node_Id;
12663 Def_Id : Entity_Id;
12664 Btyp : Entity_Id := Base_Type (T);
12665
12666 begin
12667 -- The Related_Node better be here or else we won't be able to
12668 -- attach new itypes to a node in the tree.
12669
12670 pragma Assert (Present (Related_Node));
12671
12672 -- If the view of the component's type is incomplete or private
12673 -- with unknown discriminants, then the constraint must be applied
12674 -- to the full type.
12675
12676 if Has_Unknown_Discriminants (Btyp)
12677 and then Present (Underlying_Type (Btyp))
12678 then
12679 Btyp := Underlying_Type (Btyp);
12680 end if;
12681
12682 Indic :=
12683 Make_Subtype_Indication (Loc,
12684 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
12685 Constraint => Make_Index_Or_Discriminant_Constraint (Loc, C));
12686
12687 Def_Id := Create_Itype (Ekind (T), Related_Node);
12688
12689 Subtyp_Decl :=
12690 Make_Subtype_Declaration (Loc,
12691 Defining_Identifier => Def_Id,
12692 Subtype_Indication => Indic);
12693
12694 Set_Parent (Subtyp_Decl, Parent (Related_Node));
12695
12696 -- Itypes must be analyzed with checks off (see package Itypes)
12697
12698 Analyze (Subtyp_Decl, Suppress => All_Checks);
12699
12700 return Def_Id;
12701 end Build_Subtype;
12702
12703 ---------------------
12704 -- Get_Discr_Value --
12705 ---------------------
12706
12707 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id is
12708 D : Entity_Id;
12709 E : Elmt_Id;
12710
12711 begin
12712 -- The discriminant may be declared for the type, in which case we
12713 -- find it by iterating over the list of discriminants. If the
12714 -- discriminant is inherited from a parent type, it appears as the
12715 -- corresponding discriminant of the current type. This will be the
12716 -- case when constraining an inherited component whose constraint is
12717 -- given by a discriminant of the parent.
12718
12719 D := First_Discriminant (Typ);
12720 E := First_Elmt (Constraints);
12721
12722 while Present (D) loop
12723 if D = Entity (Discrim)
12724 or else D = CR_Discriminant (Entity (Discrim))
12725 or else Corresponding_Discriminant (D) = Entity (Discrim)
12726 then
12727 return Node (E);
12728 end if;
12729
12730 Next_Discriminant (D);
12731 Next_Elmt (E);
12732 end loop;
12733
12734 -- The Corresponding_Discriminant mechanism is incomplete, because
12735 -- the correspondence between new and old discriminants is not one
12736 -- to one: one new discriminant can constrain several old ones. In
12737 -- that case, scan sequentially the stored_constraint, the list of
12738 -- discriminants of the parents, and the constraints.
12739
12740 -- Previous code checked for the present of the Stored_Constraint
12741 -- list for the derived type, but did not use it at all. Should it
12742 -- be present when the component is a discriminated task type?
12743
12744 if Is_Derived_Type (Typ)
12745 and then Scope (Entity (Discrim)) = Etype (Typ)
12746 then
12747 D := First_Discriminant (Etype (Typ));
12748 E := First_Elmt (Constraints);
12749 while Present (D) loop
12750 if D = Entity (Discrim) then
12751 return Node (E);
12752 end if;
12753
12754 Next_Discriminant (D);
12755 Next_Elmt (E);
12756 end loop;
12757 end if;
12758
12759 -- Something is wrong if we did not find the value
12760
12761 raise Program_Error;
12762 end Get_Discr_Value;
12763
12764 ---------------------
12765 -- Is_Discriminant --
12766 ---------------------
12767
12768 function Is_Discriminant (Expr : Node_Id) return Boolean is
12769 Discrim_Scope : Entity_Id;
12770
12771 begin
12772 if Denotes_Discriminant (Expr) then
12773 Discrim_Scope := Scope (Entity (Expr));
12774
12775 -- Either we have a reference to one of Typ's discriminants,
12776
12777 pragma Assert (Discrim_Scope = Typ
12778
12779 -- or to the discriminants of the parent type, in the case
12780 -- of a derivation of a tagged type with variants.
12781
12782 or else Discrim_Scope = Etype (Typ)
12783 or else Full_View (Discrim_Scope) = Etype (Typ)
12784
12785 -- or same as above for the case where the discriminants
12786 -- were declared in Typ's private view.
12787
12788 or else (Is_Private_Type (Discrim_Scope)
12789 and then Chars (Discrim_Scope) = Chars (Typ))
12790
12791 -- or else we are deriving from the full view and the
12792 -- discriminant is declared in the private entity.
12793
12794 or else (Is_Private_Type (Typ)
12795 and then Chars (Discrim_Scope) = Chars (Typ))
12796
12797 -- Or we are constrained the corresponding record of a
12798 -- synchronized type that completes a private declaration.
12799
12800 or else (Is_Concurrent_Record_Type (Typ)
12801 and then
12802 Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
12803
12804 -- or we have a class-wide type, in which case make sure the
12805 -- discriminant found belongs to the root type.
12806
12807 or else (Is_Class_Wide_Type (Typ)
12808 and then Etype (Typ) = Discrim_Scope));
12809
12810 return True;
12811 end if;
12812
12813 -- In all other cases we have something wrong
12814
12815 return False;
12816 end Is_Discriminant;
12817
12818 -- Start of processing for Constrain_Component_Type
12819
12820 begin
12821 if Nkind (Parent (Comp)) = N_Component_Declaration
12822 and then Comes_From_Source (Parent (Comp))
12823 and then Comes_From_Source
12824 (Subtype_Indication (Component_Definition (Parent (Comp))))
12825 and then
12826 Is_Entity_Name
12827 (Subtype_Indication (Component_Definition (Parent (Comp))))
12828 then
12829 return Compon_Type;
12830
12831 elsif Is_Array_Type (Compon_Type) then
12832 return Build_Constrained_Array_Type (Compon_Type);
12833
12834 elsif Has_Discriminants (Compon_Type) then
12835 return Build_Constrained_Discriminated_Type (Compon_Type);
12836
12837 elsif Is_Access_Type (Compon_Type) then
12838 return Build_Constrained_Access_Type (Compon_Type);
12839
12840 else
12841 return Compon_Type;
12842 end if;
12843 end Constrain_Component_Type;
12844
12845 --------------------------
12846 -- Constrain_Concurrent --
12847 --------------------------
12848
12849 -- For concurrent types, the associated record value type carries the same
12850 -- discriminants, so when we constrain a concurrent type, we must constrain
12851 -- the corresponding record type as well.
12852
12853 procedure Constrain_Concurrent
12854 (Def_Id : in out Entity_Id;
12855 SI : Node_Id;
12856 Related_Nod : Node_Id;
12857 Related_Id : Entity_Id;
12858 Suffix : Character)
12859 is
12860 -- Retrieve Base_Type to ensure getting to the concurrent type in the
12861 -- case of a private subtype (needed when only doing semantic analysis).
12862
12863 T_Ent : Entity_Id := Base_Type (Entity (Subtype_Mark (SI)));
12864 T_Val : Entity_Id;
12865
12866 begin
12867 if Is_Access_Type (T_Ent) then
12868 T_Ent := Designated_Type (T_Ent);
12869 end if;
12870
12871 T_Val := Corresponding_Record_Type (T_Ent);
12872
12873 if Present (T_Val) then
12874
12875 if No (Def_Id) then
12876 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
12877 end if;
12878
12879 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
12880
12881 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
12882 Set_Corresponding_Record_Type (Def_Id,
12883 Constrain_Corresponding_Record (Def_Id, T_Val, Related_Nod));
12884
12885 else
12886 -- If there is no associated record, expansion is disabled and this
12887 -- is a generic context. Create a subtype in any case, so that
12888 -- semantic analysis can proceed.
12889
12890 if No (Def_Id) then
12891 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
12892 end if;
12893
12894 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
12895 end if;
12896 end Constrain_Concurrent;
12897
12898 ------------------------------------
12899 -- Constrain_Corresponding_Record --
12900 ------------------------------------
12901
12902 function Constrain_Corresponding_Record
12903 (Prot_Subt : Entity_Id;
12904 Corr_Rec : Entity_Id;
12905 Related_Nod : Node_Id) return Entity_Id
12906 is
12907 T_Sub : constant Entity_Id :=
12908 Create_Itype (E_Record_Subtype, Related_Nod, Corr_Rec, 'C');
12909
12910 begin
12911 Set_Etype (T_Sub, Corr_Rec);
12912 Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
12913 Set_Is_Constrained (T_Sub, True);
12914 Set_First_Entity (T_Sub, First_Entity (Corr_Rec));
12915 Set_Last_Entity (T_Sub, Last_Entity (Corr_Rec));
12916
12917 if Has_Discriminants (Prot_Subt) then -- False only if errors.
12918 Set_Discriminant_Constraint
12919 (T_Sub, Discriminant_Constraint (Prot_Subt));
12920 Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
12921 Create_Constrained_Components
12922 (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
12923 end if;
12924
12925 Set_Depends_On_Private (T_Sub, Has_Private_Component (T_Sub));
12926
12927 if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
12928 Conditional_Delay (T_Sub, Corr_Rec);
12929
12930 else
12931 -- This is a component subtype: it will be frozen in the context of
12932 -- the enclosing record's init_proc, so that discriminant references
12933 -- are resolved to discriminals. (Note: we used to skip freezing
12934 -- altogether in that case, which caused errors downstream for
12935 -- components of a bit packed array type).
12936
12937 Set_Has_Delayed_Freeze (T_Sub);
12938 end if;
12939
12940 return T_Sub;
12941 end Constrain_Corresponding_Record;
12942
12943 -----------------------
12944 -- Constrain_Decimal --
12945 -----------------------
12946
12947 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id) is
12948 T : constant Entity_Id := Entity (Subtype_Mark (S));
12949 C : constant Node_Id := Constraint (S);
12950 Loc : constant Source_Ptr := Sloc (C);
12951 Range_Expr : Node_Id;
12952 Digits_Expr : Node_Id;
12953 Digits_Val : Uint;
12954 Bound_Val : Ureal;
12955
12956 begin
12957 Set_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
12958
12959 if Nkind (C) = N_Range_Constraint then
12960 Range_Expr := Range_Expression (C);
12961 Digits_Val := Digits_Value (T);
12962
12963 else
12964 pragma Assert (Nkind (C) = N_Digits_Constraint);
12965
12966 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
12967
12968 Digits_Expr := Digits_Expression (C);
12969 Analyze_And_Resolve (Digits_Expr, Any_Integer);
12970
12971 Check_Digits_Expression (Digits_Expr);
12972 Digits_Val := Expr_Value (Digits_Expr);
12973
12974 if Digits_Val > Digits_Value (T) then
12975 Error_Msg_N
12976 ("digits expression is incompatible with subtype", C);
12977 Digits_Val := Digits_Value (T);
12978 end if;
12979
12980 if Present (Range_Constraint (C)) then
12981 Range_Expr := Range_Expression (Range_Constraint (C));
12982 else
12983 Range_Expr := Empty;
12984 end if;
12985 end if;
12986
12987 Set_Etype (Def_Id, Base_Type (T));
12988 Set_Size_Info (Def_Id, (T));
12989 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
12990 Set_Delta_Value (Def_Id, Delta_Value (T));
12991 Set_Scale_Value (Def_Id, Scale_Value (T));
12992 Set_Small_Value (Def_Id, Small_Value (T));
12993 Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
12994 Set_Digits_Value (Def_Id, Digits_Val);
12995
12996 -- Manufacture range from given digits value if no range present
12997
12998 if No (Range_Expr) then
12999 Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
13000 Range_Expr :=
13001 Make_Range (Loc,
13002 Low_Bound =>
13003 Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
13004 High_Bound =>
13005 Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
13006 end if;
13007
13008 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
13009 Set_Discrete_RM_Size (Def_Id);
13010
13011 -- Unconditionally delay the freeze, since we cannot set size
13012 -- information in all cases correctly until the freeze point.
13013
13014 Set_Has_Delayed_Freeze (Def_Id);
13015 end Constrain_Decimal;
13016
13017 ----------------------------------
13018 -- Constrain_Discriminated_Type --
13019 ----------------------------------
13020
13021 procedure Constrain_Discriminated_Type
13022 (Def_Id : Entity_Id;
13023 S : Node_Id;
13024 Related_Nod : Node_Id;
13025 For_Access : Boolean := False)
13026 is
13027 E : constant Entity_Id := Entity (Subtype_Mark (S));
13028 T : Entity_Id;
13029 C : Node_Id;
13030 Elist : Elist_Id := New_Elmt_List;
13031
13032 procedure Fixup_Bad_Constraint;
13033 -- This is called after finding a bad constraint, and after having
13034 -- posted an appropriate error message. The mission is to leave the
13035 -- entity T in as reasonable state as possible.
13036
13037 --------------------------
13038 -- Fixup_Bad_Constraint --
13039 --------------------------
13040
13041 procedure Fixup_Bad_Constraint is
13042 begin
13043 -- Set a reasonable Ekind for the entity. For an incomplete type,
13044 -- we can't do much, but for other types, we can set the proper
13045 -- corresponding subtype kind.
13046
13047 if Ekind (T) = E_Incomplete_Type then
13048 Set_Ekind (Def_Id, Ekind (T));
13049 else
13050 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
13051 end if;
13052
13053 -- Set Etype to the known type, to reduce chances of cascaded errors
13054
13055 Set_Etype (Def_Id, E);
13056 Set_Error_Posted (Def_Id);
13057 end Fixup_Bad_Constraint;
13058
13059 -- Start of processing for Constrain_Discriminated_Type
13060
13061 begin
13062 C := Constraint (S);
13063
13064 -- A discriminant constraint is only allowed in a subtype indication,
13065 -- after a subtype mark. This subtype mark must denote either a type
13066 -- with discriminants, or an access type whose designated type is a
13067 -- type with discriminants. A discriminant constraint specifies the
13068 -- values of these discriminants (RM 3.7.2(5)).
13069
13070 T := Base_Type (Entity (Subtype_Mark (S)));
13071
13072 if Is_Access_Type (T) then
13073 T := Designated_Type (T);
13074 end if;
13075
13076 -- Ada 2005 (AI-412): Constrained incomplete subtypes are illegal.
13077 -- Avoid generating an error for access-to-incomplete subtypes.
13078
13079 if Ada_Version >= Ada_2005
13080 and then Ekind (T) = E_Incomplete_Type
13081 and then Nkind (Parent (S)) = N_Subtype_Declaration
13082 and then not Is_Itype (Def_Id)
13083 then
13084 -- A little sanity check, emit an error message if the type
13085 -- has discriminants to begin with. Type T may be a regular
13086 -- incomplete type or imported via a limited with clause.
13087
13088 if Has_Discriminants (T)
13089 or else (From_Limited_With (T)
13090 and then Present (Non_Limited_View (T))
13091 and then Nkind (Parent (Non_Limited_View (T))) =
13092 N_Full_Type_Declaration
13093 and then Present (Discriminant_Specifications
13094 (Parent (Non_Limited_View (T)))))
13095 then
13096 Error_Msg_N
13097 ("(Ada 2005) incomplete subtype may not be constrained", C);
13098 else
13099 Error_Msg_N ("invalid constraint: type has no discriminant", C);
13100 end if;
13101
13102 Fixup_Bad_Constraint;
13103 return;
13104
13105 -- Check that the type has visible discriminants. The type may be
13106 -- a private type with unknown discriminants whose full view has
13107 -- discriminants which are invisible.
13108
13109 elsif not Has_Discriminants (T)
13110 or else
13111 (Has_Unknown_Discriminants (T)
13112 and then Is_Private_Type (T))
13113 then
13114 Error_Msg_N ("invalid constraint: type has no discriminant", C);
13115 Fixup_Bad_Constraint;
13116 return;
13117
13118 elsif Is_Constrained (E)
13119 or else (Ekind (E) = E_Class_Wide_Subtype
13120 and then Present (Discriminant_Constraint (E)))
13121 then
13122 Error_Msg_N ("type is already constrained", Subtype_Mark (S));
13123 Fixup_Bad_Constraint;
13124 return;
13125 end if;
13126
13127 -- T may be an unconstrained subtype (e.g. a generic actual).
13128 -- Constraint applies to the base type.
13129
13130 T := Base_Type (T);
13131
13132 Elist := Build_Discriminant_Constraints (T, S);
13133
13134 -- If the list returned was empty we had an error in building the
13135 -- discriminant constraint. We have also already signalled an error
13136 -- in the incomplete type case
13137
13138 if Is_Empty_Elmt_List (Elist) then
13139 Fixup_Bad_Constraint;
13140 return;
13141 end if;
13142
13143 Build_Discriminated_Subtype (T, Def_Id, Elist, Related_Nod, For_Access);
13144 end Constrain_Discriminated_Type;
13145
13146 ---------------------------
13147 -- Constrain_Enumeration --
13148 ---------------------------
13149
13150 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id) is
13151 T : constant Entity_Id := Entity (Subtype_Mark (S));
13152 C : constant Node_Id := Constraint (S);
13153
13154 begin
13155 Set_Ekind (Def_Id, E_Enumeration_Subtype);
13156
13157 Set_First_Literal (Def_Id, First_Literal (Base_Type (T)));
13158
13159 Set_Etype (Def_Id, Base_Type (T));
13160 Set_Size_Info (Def_Id, (T));
13161 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13162 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
13163
13164 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13165
13166 Set_Discrete_RM_Size (Def_Id);
13167 end Constrain_Enumeration;
13168
13169 ----------------------
13170 -- Constrain_Float --
13171 ----------------------
13172
13173 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id) is
13174 T : constant Entity_Id := Entity (Subtype_Mark (S));
13175 C : Node_Id;
13176 D : Node_Id;
13177 Rais : Node_Id;
13178
13179 begin
13180 Set_Ekind (Def_Id, E_Floating_Point_Subtype);
13181
13182 Set_Etype (Def_Id, Base_Type (T));
13183 Set_Size_Info (Def_Id, (T));
13184 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13185
13186 -- Process the constraint
13187
13188 C := Constraint (S);
13189
13190 -- Digits constraint present
13191
13192 if Nkind (C) = N_Digits_Constraint then
13193
13194 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
13195 Check_Restriction (No_Obsolescent_Features, C);
13196
13197 if Warn_On_Obsolescent_Feature then
13198 Error_Msg_N
13199 ("subtype digits constraint is an " &
13200 "obsolescent feature (RM J.3(8))?j?", C);
13201 end if;
13202
13203 D := Digits_Expression (C);
13204 Analyze_And_Resolve (D, Any_Integer);
13205 Check_Digits_Expression (D);
13206 Set_Digits_Value (Def_Id, Expr_Value (D));
13207
13208 -- Check that digits value is in range. Obviously we can do this
13209 -- at compile time, but it is strictly a runtime check, and of
13210 -- course there is an ACVC test that checks this.
13211
13212 if Digits_Value (Def_Id) > Digits_Value (T) then
13213 Error_Msg_Uint_1 := Digits_Value (T);
13214 Error_Msg_N ("??digits value is too large, maximum is ^", D);
13215 Rais :=
13216 Make_Raise_Constraint_Error (Sloc (D),
13217 Reason => CE_Range_Check_Failed);
13218 Insert_Action (Declaration_Node (Def_Id), Rais);
13219 end if;
13220
13221 C := Range_Constraint (C);
13222
13223 -- No digits constraint present
13224
13225 else
13226 Set_Digits_Value (Def_Id, Digits_Value (T));
13227 end if;
13228
13229 -- Range constraint present
13230
13231 if Nkind (C) = N_Range_Constraint then
13232 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13233
13234 -- No range constraint present
13235
13236 else
13237 pragma Assert (No (C));
13238 Set_Scalar_Range (Def_Id, Scalar_Range (T));
13239 end if;
13240
13241 Set_Is_Constrained (Def_Id);
13242 end Constrain_Float;
13243
13244 ---------------------
13245 -- Constrain_Index --
13246 ---------------------
13247
13248 procedure Constrain_Index
13249 (Index : Node_Id;
13250 S : Node_Id;
13251 Related_Nod : Node_Id;
13252 Related_Id : Entity_Id;
13253 Suffix : Character;
13254 Suffix_Index : Nat)
13255 is
13256 Def_Id : Entity_Id;
13257 R : Node_Id := Empty;
13258 T : constant Entity_Id := Etype (Index);
13259
13260 begin
13261 Def_Id :=
13262 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
13263 Set_Etype (Def_Id, Base_Type (T));
13264
13265 if Nkind (S) = N_Range
13266 or else
13267 (Nkind (S) = N_Attribute_Reference
13268 and then Attribute_Name (S) = Name_Range)
13269 then
13270 -- A Range attribute will be transformed into N_Range by Resolve
13271
13272 Analyze (S);
13273 Set_Etype (S, T);
13274 R := S;
13275
13276 Process_Range_Expr_In_Decl (R, T);
13277
13278 if not Error_Posted (S)
13279 and then
13280 (Nkind (S) /= N_Range
13281 or else not Covers (T, (Etype (Low_Bound (S))))
13282 or else not Covers (T, (Etype (High_Bound (S)))))
13283 then
13284 if Base_Type (T) /= Any_Type
13285 and then Etype (Low_Bound (S)) /= Any_Type
13286 and then Etype (High_Bound (S)) /= Any_Type
13287 then
13288 Error_Msg_N ("range expected", S);
13289 end if;
13290 end if;
13291
13292 elsif Nkind (S) = N_Subtype_Indication then
13293
13294 -- The parser has verified that this is a discrete indication
13295
13296 Resolve_Discrete_Subtype_Indication (S, T);
13297 Bad_Predicated_Subtype_Use
13298 ("subtype& has predicate, not allowed in index constraint",
13299 S, Entity (Subtype_Mark (S)));
13300
13301 R := Range_Expression (Constraint (S));
13302
13303 -- Capture values of bounds and generate temporaries for them if
13304 -- needed, since checks may cause duplication of the expressions
13305 -- which must not be reevaluated.
13306
13307 -- The forced evaluation removes side effects from expressions, which
13308 -- should occur also in GNATprove mode. Otherwise, we end up with
13309 -- unexpected insertions of actions at places where this is not
13310 -- supposed to occur, e.g. on default parameters of a call.
13311
13312 if Expander_Active or GNATprove_Mode then
13313 Force_Evaluation
13314 (Low_Bound (R), Related_Id => Def_Id, Is_Low_Bound => True);
13315 Force_Evaluation
13316 (High_Bound (R), Related_Id => Def_Id, Is_High_Bound => True);
13317 end if;
13318
13319 elsif Nkind (S) = N_Discriminant_Association then
13320
13321 -- Syntactically valid in subtype indication
13322
13323 Error_Msg_N ("invalid index constraint", S);
13324 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
13325 return;
13326
13327 -- Subtype_Mark case, no anonymous subtypes to construct
13328
13329 else
13330 Analyze (S);
13331
13332 if Is_Entity_Name (S) then
13333 if not Is_Type (Entity (S)) then
13334 Error_Msg_N ("expect subtype mark for index constraint", S);
13335
13336 elsif Base_Type (Entity (S)) /= Base_Type (T) then
13337 Wrong_Type (S, Base_Type (T));
13338
13339 -- Check error of subtype with predicate in index constraint
13340
13341 else
13342 Bad_Predicated_Subtype_Use
13343 ("subtype& has predicate, not allowed in index constraint",
13344 S, Entity (S));
13345 end if;
13346
13347 return;
13348
13349 else
13350 Error_Msg_N ("invalid index constraint", S);
13351 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
13352 return;
13353 end if;
13354 end if;
13355
13356 -- Complete construction of the Itype
13357
13358 if Is_Modular_Integer_Type (T) then
13359 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
13360
13361 elsif Is_Integer_Type (T) then
13362 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
13363
13364 else
13365 Set_Ekind (Def_Id, E_Enumeration_Subtype);
13366 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
13367 Set_First_Literal (Def_Id, First_Literal (T));
13368 end if;
13369
13370 Set_Size_Info (Def_Id, (T));
13371 Set_RM_Size (Def_Id, RM_Size (T));
13372 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13373
13374 Set_Scalar_Range (Def_Id, R);
13375
13376 Set_Etype (S, Def_Id);
13377 Set_Discrete_RM_Size (Def_Id);
13378 end Constrain_Index;
13379
13380 -----------------------
13381 -- Constrain_Integer --
13382 -----------------------
13383
13384 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id) is
13385 T : constant Entity_Id := Entity (Subtype_Mark (S));
13386 C : constant Node_Id := Constraint (S);
13387
13388 begin
13389 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13390
13391 if Is_Modular_Integer_Type (T) then
13392 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
13393 else
13394 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
13395 end if;
13396
13397 Set_Etype (Def_Id, Base_Type (T));
13398 Set_Size_Info (Def_Id, (T));
13399 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13400 Set_Discrete_RM_Size (Def_Id);
13401 end Constrain_Integer;
13402
13403 ------------------------------
13404 -- Constrain_Ordinary_Fixed --
13405 ------------------------------
13406
13407 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id) is
13408 T : constant Entity_Id := Entity (Subtype_Mark (S));
13409 C : Node_Id;
13410 D : Node_Id;
13411 Rais : Node_Id;
13412
13413 begin
13414 Set_Ekind (Def_Id, E_Ordinary_Fixed_Point_Subtype);
13415 Set_Etype (Def_Id, Base_Type (T));
13416 Set_Size_Info (Def_Id, (T));
13417 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13418 Set_Small_Value (Def_Id, Small_Value (T));
13419
13420 -- Process the constraint
13421
13422 C := Constraint (S);
13423
13424 -- Delta constraint present
13425
13426 if Nkind (C) = N_Delta_Constraint then
13427
13428 Check_SPARK_05_Restriction ("delta constraint is not allowed", S);
13429 Check_Restriction (No_Obsolescent_Features, C);
13430
13431 if Warn_On_Obsolescent_Feature then
13432 Error_Msg_S
13433 ("subtype delta constraint is an " &
13434 "obsolescent feature (RM J.3(7))?j?");
13435 end if;
13436
13437 D := Delta_Expression (C);
13438 Analyze_And_Resolve (D, Any_Real);
13439 Check_Delta_Expression (D);
13440 Set_Delta_Value (Def_Id, Expr_Value_R (D));
13441
13442 -- Check that delta value is in range. Obviously we can do this
13443 -- at compile time, but it is strictly a runtime check, and of
13444 -- course there is an ACVC test that checks this.
13445
13446 if Delta_Value (Def_Id) < Delta_Value (T) then
13447 Error_Msg_N ("??delta value is too small", D);
13448 Rais :=
13449 Make_Raise_Constraint_Error (Sloc (D),
13450 Reason => CE_Range_Check_Failed);
13451 Insert_Action (Declaration_Node (Def_Id), Rais);
13452 end if;
13453
13454 C := Range_Constraint (C);
13455
13456 -- No delta constraint present
13457
13458 else
13459 Set_Delta_Value (Def_Id, Delta_Value (T));
13460 end if;
13461
13462 -- Range constraint present
13463
13464 if Nkind (C) = N_Range_Constraint then
13465 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13466
13467 -- No range constraint present
13468
13469 else
13470 pragma Assert (No (C));
13471 Set_Scalar_Range (Def_Id, Scalar_Range (T));
13472 end if;
13473
13474 Set_Discrete_RM_Size (Def_Id);
13475
13476 -- Unconditionally delay the freeze, since we cannot set size
13477 -- information in all cases correctly until the freeze point.
13478
13479 Set_Has_Delayed_Freeze (Def_Id);
13480 end Constrain_Ordinary_Fixed;
13481
13482 -----------------------
13483 -- Contain_Interface --
13484 -----------------------
13485
13486 function Contain_Interface
13487 (Iface : Entity_Id;
13488 Ifaces : Elist_Id) return Boolean
13489 is
13490 Iface_Elmt : Elmt_Id;
13491
13492 begin
13493 if Present (Ifaces) then
13494 Iface_Elmt := First_Elmt (Ifaces);
13495 while Present (Iface_Elmt) loop
13496 if Node (Iface_Elmt) = Iface then
13497 return True;
13498 end if;
13499
13500 Next_Elmt (Iface_Elmt);
13501 end loop;
13502 end if;
13503
13504 return False;
13505 end Contain_Interface;
13506
13507 ---------------------------
13508 -- Convert_Scalar_Bounds --
13509 ---------------------------
13510
13511 procedure Convert_Scalar_Bounds
13512 (N : Node_Id;
13513 Parent_Type : Entity_Id;
13514 Derived_Type : Entity_Id;
13515 Loc : Source_Ptr)
13516 is
13517 Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
13518
13519 Lo : Node_Id;
13520 Hi : Node_Id;
13521 Rng : Node_Id;
13522
13523 begin
13524 -- Defend against previous errors
13525
13526 if No (Scalar_Range (Derived_Type)) then
13527 Check_Error_Detected;
13528 return;
13529 end if;
13530
13531 Lo := Build_Scalar_Bound
13532 (Type_Low_Bound (Derived_Type),
13533 Parent_Type, Implicit_Base);
13534
13535 Hi := Build_Scalar_Bound
13536 (Type_High_Bound (Derived_Type),
13537 Parent_Type, Implicit_Base);
13538
13539 Rng :=
13540 Make_Range (Loc,
13541 Low_Bound => Lo,
13542 High_Bound => Hi);
13543
13544 Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
13545
13546 Set_Parent (Rng, N);
13547 Set_Scalar_Range (Derived_Type, Rng);
13548
13549 -- Analyze the bounds
13550
13551 Analyze_And_Resolve (Lo, Implicit_Base);
13552 Analyze_And_Resolve (Hi, Implicit_Base);
13553
13554 -- Analyze the range itself, except that we do not analyze it if
13555 -- the bounds are real literals, and we have a fixed-point type.
13556 -- The reason for this is that we delay setting the bounds in this
13557 -- case till we know the final Small and Size values (see circuit
13558 -- in Freeze.Freeze_Fixed_Point_Type for further details).
13559
13560 if Is_Fixed_Point_Type (Parent_Type)
13561 and then Nkind (Lo) = N_Real_Literal
13562 and then Nkind (Hi) = N_Real_Literal
13563 then
13564 return;
13565
13566 -- Here we do the analysis of the range
13567
13568 -- Note: we do this manually, since if we do a normal Analyze and
13569 -- Resolve call, there are problems with the conversions used for
13570 -- the derived type range.
13571
13572 else
13573 Set_Etype (Rng, Implicit_Base);
13574 Set_Analyzed (Rng, True);
13575 end if;
13576 end Convert_Scalar_Bounds;
13577
13578 -------------------
13579 -- Copy_And_Swap --
13580 -------------------
13581
13582 procedure Copy_And_Swap (Priv, Full : Entity_Id) is
13583 begin
13584 -- Initialize new full declaration entity by copying the pertinent
13585 -- fields of the corresponding private declaration entity.
13586
13587 -- We temporarily set Ekind to a value appropriate for a type to
13588 -- avoid assert failures in Einfo from checking for setting type
13589 -- attributes on something that is not a type. Ekind (Priv) is an
13590 -- appropriate choice, since it allowed the attributes to be set
13591 -- in the first place. This Ekind value will be modified later.
13592
13593 Set_Ekind (Full, Ekind (Priv));
13594
13595 -- Also set Etype temporarily to Any_Type, again, in the absence
13596 -- of errors, it will be properly reset, and if there are errors,
13597 -- then we want a value of Any_Type to remain.
13598
13599 Set_Etype (Full, Any_Type);
13600
13601 -- Now start copying attributes
13602
13603 Set_Has_Discriminants (Full, Has_Discriminants (Priv));
13604
13605 if Has_Discriminants (Full) then
13606 Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
13607 Set_Stored_Constraint (Full, Stored_Constraint (Priv));
13608 end if;
13609
13610 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
13611 Set_Homonym (Full, Homonym (Priv));
13612 Set_Is_Immediately_Visible (Full, Is_Immediately_Visible (Priv));
13613 Set_Is_Public (Full, Is_Public (Priv));
13614 Set_Is_Pure (Full, Is_Pure (Priv));
13615 Set_Is_Tagged_Type (Full, Is_Tagged_Type (Priv));
13616 Set_Has_Pragma_Unmodified (Full, Has_Pragma_Unmodified (Priv));
13617 Set_Has_Pragma_Unreferenced (Full, Has_Pragma_Unreferenced (Priv));
13618 Set_Has_Pragma_Unreferenced_Objects
13619 (Full, Has_Pragma_Unreferenced_Objects
13620 (Priv));
13621
13622 Conditional_Delay (Full, Priv);
13623
13624 if Is_Tagged_Type (Full) then
13625 Set_Direct_Primitive_Operations
13626 (Full, Direct_Primitive_Operations (Priv));
13627 Set_No_Tagged_Streams_Pragma
13628 (Full, No_Tagged_Streams_Pragma (Priv));
13629
13630 if Is_Base_Type (Priv) then
13631 Set_Class_Wide_Type (Full, Class_Wide_Type (Priv));
13632 end if;
13633 end if;
13634
13635 Set_Is_Volatile (Full, Is_Volatile (Priv));
13636 Set_Treat_As_Volatile (Full, Treat_As_Volatile (Priv));
13637 Set_Scope (Full, Scope (Priv));
13638 Set_Next_Entity (Full, Next_Entity (Priv));
13639 Set_First_Entity (Full, First_Entity (Priv));
13640 Set_Last_Entity (Full, Last_Entity (Priv));
13641
13642 -- If access types have been recorded for later handling, keep them in
13643 -- the full view so that they get handled when the full view freeze
13644 -- node is expanded.
13645
13646 if Present (Freeze_Node (Priv))
13647 and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
13648 then
13649 Ensure_Freeze_Node (Full);
13650 Set_Access_Types_To_Process
13651 (Freeze_Node (Full),
13652 Access_Types_To_Process (Freeze_Node (Priv)));
13653 end if;
13654
13655 -- Swap the two entities. Now Private is the full type entity and Full
13656 -- is the private one. They will be swapped back at the end of the
13657 -- private part. This swapping ensures that the entity that is visible
13658 -- in the private part is the full declaration.
13659
13660 Exchange_Entities (Priv, Full);
13661 Append_Entity (Full, Scope (Full));
13662 end Copy_And_Swap;
13663
13664 -------------------------------------
13665 -- Copy_Array_Base_Type_Attributes --
13666 -------------------------------------
13667
13668 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
13669 begin
13670 Set_Component_Alignment (T1, Component_Alignment (T2));
13671 Set_Component_Type (T1, Component_Type (T2));
13672 Set_Component_Size (T1, Component_Size (T2));
13673 Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
13674 Set_Has_Non_Standard_Rep (T1, Has_Non_Standard_Rep (T2));
13675 Set_Has_Protected (T1, Has_Protected (T2));
13676 Set_Has_Task (T1, Has_Task (T2));
13677 Set_Is_Packed (T1, Is_Packed (T2));
13678 Set_Has_Aliased_Components (T1, Has_Aliased_Components (T2));
13679 Set_Has_Atomic_Components (T1, Has_Atomic_Components (T2));
13680 Set_Has_Volatile_Components (T1, Has_Volatile_Components (T2));
13681 end Copy_Array_Base_Type_Attributes;
13682
13683 -----------------------------------
13684 -- Copy_Array_Subtype_Attributes --
13685 -----------------------------------
13686
13687 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
13688 begin
13689 Set_Size_Info (T1, T2);
13690
13691 Set_First_Index (T1, First_Index (T2));
13692 Set_Is_Aliased (T1, Is_Aliased (T2));
13693 Set_Is_Volatile (T1, Is_Volatile (T2));
13694 Set_Treat_As_Volatile (T1, Treat_As_Volatile (T2));
13695 Set_Is_Constrained (T1, Is_Constrained (T2));
13696 Set_Depends_On_Private (T1, Has_Private_Component (T2));
13697 Inherit_Rep_Item_Chain (T1, T2);
13698 Set_Convention (T1, Convention (T2));
13699 Set_Is_Limited_Composite (T1, Is_Limited_Composite (T2));
13700 Set_Is_Private_Composite (T1, Is_Private_Composite (T2));
13701 Set_Packed_Array_Impl_Type (T1, Packed_Array_Impl_Type (T2));
13702 end Copy_Array_Subtype_Attributes;
13703
13704 -----------------------------------
13705 -- Create_Constrained_Components --
13706 -----------------------------------
13707
13708 procedure Create_Constrained_Components
13709 (Subt : Entity_Id;
13710 Decl_Node : Node_Id;
13711 Typ : Entity_Id;
13712 Constraints : Elist_Id)
13713 is
13714 Loc : constant Source_Ptr := Sloc (Subt);
13715 Comp_List : constant Elist_Id := New_Elmt_List;
13716 Parent_Type : constant Entity_Id := Etype (Typ);
13717 Assoc_List : constant List_Id := New_List;
13718 Discr_Val : Elmt_Id;
13719 Errors : Boolean;
13720 New_C : Entity_Id;
13721 Old_C : Entity_Id;
13722 Is_Static : Boolean := True;
13723
13724 procedure Collect_Fixed_Components (Typ : Entity_Id);
13725 -- Collect parent type components that do not appear in a variant part
13726
13727 procedure Create_All_Components;
13728 -- Iterate over Comp_List to create the components of the subtype
13729
13730 function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
13731 -- Creates a new component from Old_Compon, copying all the fields from
13732 -- it, including its Etype, inserts the new component in the Subt entity
13733 -- chain and returns the new component.
13734
13735 function Is_Variant_Record (T : Entity_Id) return Boolean;
13736 -- If true, and discriminants are static, collect only components from
13737 -- variants selected by discriminant values.
13738
13739 ------------------------------
13740 -- Collect_Fixed_Components --
13741 ------------------------------
13742
13743 procedure Collect_Fixed_Components (Typ : Entity_Id) is
13744 begin
13745 -- Build association list for discriminants, and find components of the
13746 -- variant part selected by the values of the discriminants.
13747
13748 Old_C := First_Discriminant (Typ);
13749 Discr_Val := First_Elmt (Constraints);
13750 while Present (Old_C) loop
13751 Append_To (Assoc_List,
13752 Make_Component_Association (Loc,
13753 Choices => New_List (New_Occurrence_Of (Old_C, Loc)),
13754 Expression => New_Copy (Node (Discr_Val))));
13755
13756 Next_Elmt (Discr_Val);
13757 Next_Discriminant (Old_C);
13758 end loop;
13759
13760 -- The tag and the possible parent component are unconditionally in
13761 -- the subtype.
13762
13763 if Is_Tagged_Type (Typ) or else Has_Controlled_Component (Typ) then
13764 Old_C := First_Component (Typ);
13765 while Present (Old_C) loop
13766 if Nam_In (Chars (Old_C), Name_uTag, Name_uParent) then
13767 Append_Elmt (Old_C, Comp_List);
13768 end if;
13769
13770 Next_Component (Old_C);
13771 end loop;
13772 end if;
13773 end Collect_Fixed_Components;
13774
13775 ---------------------------
13776 -- Create_All_Components --
13777 ---------------------------
13778
13779 procedure Create_All_Components is
13780 Comp : Elmt_Id;
13781
13782 begin
13783 Comp := First_Elmt (Comp_List);
13784 while Present (Comp) loop
13785 Old_C := Node (Comp);
13786 New_C := Create_Component (Old_C);
13787
13788 Set_Etype
13789 (New_C,
13790 Constrain_Component_Type
13791 (Old_C, Subt, Decl_Node, Typ, Constraints));
13792 Set_Is_Public (New_C, Is_Public (Subt));
13793
13794 Next_Elmt (Comp);
13795 end loop;
13796 end Create_All_Components;
13797
13798 ----------------------
13799 -- Create_Component --
13800 ----------------------
13801
13802 function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
13803 New_Compon : constant Entity_Id := New_Copy (Old_Compon);
13804
13805 begin
13806 if Ekind (Old_Compon) = E_Discriminant
13807 and then Is_Completely_Hidden (Old_Compon)
13808 then
13809 -- This is a shadow discriminant created for a discriminant of
13810 -- the parent type, which needs to be present in the subtype.
13811 -- Give the shadow discriminant an internal name that cannot
13812 -- conflict with that of visible components.
13813
13814 Set_Chars (New_Compon, New_Internal_Name ('C'));
13815 end if;
13816
13817 -- Set the parent so we have a proper link for freezing etc. This is
13818 -- not a real parent pointer, since of course our parent does not own
13819 -- up to us and reference us, we are an illegitimate child of the
13820 -- original parent.
13821
13822 Set_Parent (New_Compon, Parent (Old_Compon));
13823
13824 -- If the old component's Esize was already determined and is a
13825 -- static value, then the new component simply inherits it. Otherwise
13826 -- the old component's size may require run-time determination, but
13827 -- the new component's size still might be statically determinable
13828 -- (if, for example it has a static constraint). In that case we want
13829 -- Layout_Type to recompute the component's size, so we reset its
13830 -- size and positional fields.
13831
13832 if Frontend_Layout_On_Target
13833 and then not Known_Static_Esize (Old_Compon)
13834 then
13835 Set_Esize (New_Compon, Uint_0);
13836 Init_Normalized_First_Bit (New_Compon);
13837 Init_Normalized_Position (New_Compon);
13838 Init_Normalized_Position_Max (New_Compon);
13839 end if;
13840
13841 -- We do not want this node marked as Comes_From_Source, since
13842 -- otherwise it would get first class status and a separate cross-
13843 -- reference line would be generated. Illegitimate children do not
13844 -- rate such recognition.
13845
13846 Set_Comes_From_Source (New_Compon, False);
13847
13848 -- But it is a real entity, and a birth certificate must be properly
13849 -- registered by entering it into the entity list.
13850
13851 Enter_Name (New_Compon);
13852
13853 return New_Compon;
13854 end Create_Component;
13855
13856 -----------------------
13857 -- Is_Variant_Record --
13858 -----------------------
13859
13860 function Is_Variant_Record (T : Entity_Id) return Boolean is
13861 begin
13862 return Nkind (Parent (T)) = N_Full_Type_Declaration
13863 and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
13864 and then Present (Component_List (Type_Definition (Parent (T))))
13865 and then
13866 Present
13867 (Variant_Part (Component_List (Type_Definition (Parent (T)))));
13868 end Is_Variant_Record;
13869
13870 -- Start of processing for Create_Constrained_Components
13871
13872 begin
13873 pragma Assert (Subt /= Base_Type (Subt));
13874 pragma Assert (Typ = Base_Type (Typ));
13875
13876 Set_First_Entity (Subt, Empty);
13877 Set_Last_Entity (Subt, Empty);
13878
13879 -- Check whether constraint is fully static, in which case we can
13880 -- optimize the list of components.
13881
13882 Discr_Val := First_Elmt (Constraints);
13883 while Present (Discr_Val) loop
13884 if not Is_OK_Static_Expression (Node (Discr_Val)) then
13885 Is_Static := False;
13886 exit;
13887 end if;
13888
13889 Next_Elmt (Discr_Val);
13890 end loop;
13891
13892 Set_Has_Static_Discriminants (Subt, Is_Static);
13893
13894 Push_Scope (Subt);
13895
13896 -- Inherit the discriminants of the parent type
13897
13898 Add_Discriminants : declare
13899 Num_Disc : Int;
13900 Num_Gird : Int;
13901
13902 begin
13903 Num_Disc := 0;
13904 Old_C := First_Discriminant (Typ);
13905
13906 while Present (Old_C) loop
13907 Num_Disc := Num_Disc + 1;
13908 New_C := Create_Component (Old_C);
13909 Set_Is_Public (New_C, Is_Public (Subt));
13910 Next_Discriminant (Old_C);
13911 end loop;
13912
13913 -- For an untagged derived subtype, the number of discriminants may
13914 -- be smaller than the number of inherited discriminants, because
13915 -- several of them may be renamed by a single new discriminant or
13916 -- constrained. In this case, add the hidden discriminants back into
13917 -- the subtype, because they need to be present if the optimizer of
13918 -- the GCC 4.x back-end decides to break apart assignments between
13919 -- objects using the parent view into member-wise assignments.
13920
13921 Num_Gird := 0;
13922
13923 if Is_Derived_Type (Typ)
13924 and then not Is_Tagged_Type (Typ)
13925 then
13926 Old_C := First_Stored_Discriminant (Typ);
13927
13928 while Present (Old_C) loop
13929 Num_Gird := Num_Gird + 1;
13930 Next_Stored_Discriminant (Old_C);
13931 end loop;
13932 end if;
13933
13934 if Num_Gird > Num_Disc then
13935
13936 -- Find out multiple uses of new discriminants, and add hidden
13937 -- components for the extra renamed discriminants. We recognize
13938 -- multiple uses through the Corresponding_Discriminant of a
13939 -- new discriminant: if it constrains several old discriminants,
13940 -- this field points to the last one in the parent type. The
13941 -- stored discriminants of the derived type have the same name
13942 -- as those of the parent.
13943
13944 declare
13945 Constr : Elmt_Id;
13946 New_Discr : Entity_Id;
13947 Old_Discr : Entity_Id;
13948
13949 begin
13950 Constr := First_Elmt (Stored_Constraint (Typ));
13951 Old_Discr := First_Stored_Discriminant (Typ);
13952 while Present (Constr) loop
13953 if Is_Entity_Name (Node (Constr))
13954 and then Ekind (Entity (Node (Constr))) = E_Discriminant
13955 then
13956 New_Discr := Entity (Node (Constr));
13957
13958 if Chars (Corresponding_Discriminant (New_Discr)) /=
13959 Chars (Old_Discr)
13960 then
13961 -- The new discriminant has been used to rename a
13962 -- subsequent old discriminant. Introduce a shadow
13963 -- component for the current old discriminant.
13964
13965 New_C := Create_Component (Old_Discr);
13966 Set_Original_Record_Component (New_C, Old_Discr);
13967 end if;
13968
13969 else
13970 -- The constraint has eliminated the old discriminant.
13971 -- Introduce a shadow component.
13972
13973 New_C := Create_Component (Old_Discr);
13974 Set_Original_Record_Component (New_C, Old_Discr);
13975 end if;
13976
13977 Next_Elmt (Constr);
13978 Next_Stored_Discriminant (Old_Discr);
13979 end loop;
13980 end;
13981 end if;
13982 end Add_Discriminants;
13983
13984 if Is_Static
13985 and then Is_Variant_Record (Typ)
13986 then
13987 Collect_Fixed_Components (Typ);
13988
13989 Gather_Components (
13990 Typ,
13991 Component_List (Type_Definition (Parent (Typ))),
13992 Governed_By => Assoc_List,
13993 Into => Comp_List,
13994 Report_Errors => Errors);
13995 pragma Assert (not Errors);
13996
13997 Create_All_Components;
13998
13999 -- If the subtype declaration is created for a tagged type derivation
14000 -- with constraints, we retrieve the record definition of the parent
14001 -- type to select the components of the proper variant.
14002
14003 elsif Is_Static
14004 and then Is_Tagged_Type (Typ)
14005 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
14006 and then
14007 Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
14008 and then Is_Variant_Record (Parent_Type)
14009 then
14010 Collect_Fixed_Components (Typ);
14011
14012 Gather_Components
14013 (Typ,
14014 Component_List (Type_Definition (Parent (Parent_Type))),
14015 Governed_By => Assoc_List,
14016 Into => Comp_List,
14017 Report_Errors => Errors);
14018
14019 -- Note: previously there was a check at this point that no errors
14020 -- were detected. As a consequence of AI05-220 there may be an error
14021 -- if an inherited discriminant that controls a variant has a non-
14022 -- static constraint.
14023
14024 -- If the tagged derivation has a type extension, collect all the
14025 -- new components therein.
14026
14027 if Present (Record_Extension_Part (Type_Definition (Parent (Typ))))
14028 then
14029 Old_C := First_Component (Typ);
14030 while Present (Old_C) loop
14031 if Original_Record_Component (Old_C) = Old_C
14032 and then Chars (Old_C) /= Name_uTag
14033 and then Chars (Old_C) /= Name_uParent
14034 then
14035 Append_Elmt (Old_C, Comp_List);
14036 end if;
14037
14038 Next_Component (Old_C);
14039 end loop;
14040 end if;
14041
14042 Create_All_Components;
14043
14044 else
14045 -- If discriminants are not static, or if this is a multi-level type
14046 -- extension, we have to include all components of the parent type.
14047
14048 Old_C := First_Component (Typ);
14049 while Present (Old_C) loop
14050 New_C := Create_Component (Old_C);
14051
14052 Set_Etype
14053 (New_C,
14054 Constrain_Component_Type
14055 (Old_C, Subt, Decl_Node, Typ, Constraints));
14056 Set_Is_Public (New_C, Is_Public (Subt));
14057
14058 Next_Component (Old_C);
14059 end loop;
14060 end if;
14061
14062 End_Scope;
14063 end Create_Constrained_Components;
14064
14065 ------------------------------------------
14066 -- Decimal_Fixed_Point_Type_Declaration --
14067 ------------------------------------------
14068
14069 procedure Decimal_Fixed_Point_Type_Declaration
14070 (T : Entity_Id;
14071 Def : Node_Id)
14072 is
14073 Loc : constant Source_Ptr := Sloc (Def);
14074 Digs_Expr : constant Node_Id := Digits_Expression (Def);
14075 Delta_Expr : constant Node_Id := Delta_Expression (Def);
14076 Implicit_Base : Entity_Id;
14077 Digs_Val : Uint;
14078 Delta_Val : Ureal;
14079 Scale_Val : Uint;
14080 Bound_Val : Ureal;
14081
14082 begin
14083 Check_SPARK_05_Restriction
14084 ("decimal fixed point type is not allowed", Def);
14085 Check_Restriction (No_Fixed_Point, Def);
14086
14087 -- Create implicit base type
14088
14089 Implicit_Base :=
14090 Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
14091 Set_Etype (Implicit_Base, Implicit_Base);
14092
14093 -- Analyze and process delta expression
14094
14095 Analyze_And_Resolve (Delta_Expr, Universal_Real);
14096
14097 Check_Delta_Expression (Delta_Expr);
14098 Delta_Val := Expr_Value_R (Delta_Expr);
14099
14100 -- Check delta is power of 10, and determine scale value from it
14101
14102 declare
14103 Val : Ureal;
14104
14105 begin
14106 Scale_Val := Uint_0;
14107 Val := Delta_Val;
14108
14109 if Val < Ureal_1 then
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 exceeds maximum value of 18", Def);
14117 Scale_Val := UI_From_Int (+18);
14118 end if;
14119
14120 else
14121 while Val > Ureal_1 loop
14122 Val := Val / Ureal_10;
14123 Scale_Val := Scale_Val - 1;
14124 end loop;
14125
14126 if Scale_Val < -18 then
14127 Error_Msg_N ("scale is less than minimum value of -18", Def);
14128 Scale_Val := UI_From_Int (-18);
14129 end if;
14130 end if;
14131
14132 if Val /= Ureal_1 then
14133 Error_Msg_N ("delta expression must be a power of 10", Def);
14134 Delta_Val := Ureal_10 ** (-Scale_Val);
14135 end if;
14136 end;
14137
14138 -- Set delta, scale and small (small = delta for decimal type)
14139
14140 Set_Delta_Value (Implicit_Base, Delta_Val);
14141 Set_Scale_Value (Implicit_Base, Scale_Val);
14142 Set_Small_Value (Implicit_Base, Delta_Val);
14143
14144 -- Analyze and process digits expression
14145
14146 Analyze_And_Resolve (Digs_Expr, Any_Integer);
14147 Check_Digits_Expression (Digs_Expr);
14148 Digs_Val := Expr_Value (Digs_Expr);
14149
14150 if Digs_Val > 18 then
14151 Digs_Val := UI_From_Int (+18);
14152 Error_Msg_N ("digits value out of range, maximum is 18", Digs_Expr);
14153 end if;
14154
14155 Set_Digits_Value (Implicit_Base, Digs_Val);
14156 Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
14157
14158 -- Set range of base type from digits value for now. This will be
14159 -- expanded to represent the true underlying base range by Freeze.
14160
14161 Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
14162
14163 -- Note: We leave size as zero for now, size will be set at freeze
14164 -- time. We have to do this for ordinary fixed-point, because the size
14165 -- depends on the specified small, and we might as well do the same for
14166 -- decimal fixed-point.
14167
14168 pragma Assert (Esize (Implicit_Base) = Uint_0);
14169
14170 -- If there are bounds given in the declaration use them as the
14171 -- bounds of the first named subtype.
14172
14173 if Present (Real_Range_Specification (Def)) then
14174 declare
14175 RRS : constant Node_Id := Real_Range_Specification (Def);
14176 Low : constant Node_Id := Low_Bound (RRS);
14177 High : constant Node_Id := High_Bound (RRS);
14178 Low_Val : Ureal;
14179 High_Val : Ureal;
14180
14181 begin
14182 Analyze_And_Resolve (Low, Any_Real);
14183 Analyze_And_Resolve (High, Any_Real);
14184 Check_Real_Bound (Low);
14185 Check_Real_Bound (High);
14186 Low_Val := Expr_Value_R (Low);
14187 High_Val := Expr_Value_R (High);
14188
14189 if Low_Val < (-Bound_Val) then
14190 Error_Msg_N
14191 ("range low bound too small for digits value", Low);
14192 Low_Val := -Bound_Val;
14193 end if;
14194
14195 if High_Val > Bound_Val then
14196 Error_Msg_N
14197 ("range high bound too large for digits value", High);
14198 High_Val := Bound_Val;
14199 end if;
14200
14201 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
14202 end;
14203
14204 -- If no explicit range, use range that corresponds to given
14205 -- digits value. This will end up as the final range for the
14206 -- first subtype.
14207
14208 else
14209 Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
14210 end if;
14211
14212 -- Complete entity for first subtype. The inheritance of the rep item
14213 -- chain ensures that SPARK-related pragmas are not clobbered when the
14214 -- decimal fixed point type acts as a full view of a private type.
14215
14216 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
14217 Set_Etype (T, Implicit_Base);
14218 Set_Size_Info (T, Implicit_Base);
14219 Inherit_Rep_Item_Chain (T, Implicit_Base);
14220 Set_Digits_Value (T, Digs_Val);
14221 Set_Delta_Value (T, Delta_Val);
14222 Set_Small_Value (T, Delta_Val);
14223 Set_Scale_Value (T, Scale_Val);
14224 Set_Is_Constrained (T);
14225 end Decimal_Fixed_Point_Type_Declaration;
14226
14227 -----------------------------------
14228 -- Derive_Progenitor_Subprograms --
14229 -----------------------------------
14230
14231 procedure Derive_Progenitor_Subprograms
14232 (Parent_Type : Entity_Id;
14233 Tagged_Type : Entity_Id)
14234 is
14235 E : Entity_Id;
14236 Elmt : Elmt_Id;
14237 Iface : Entity_Id;
14238 Iface_Elmt : Elmt_Id;
14239 Iface_Subp : Entity_Id;
14240 New_Subp : Entity_Id := Empty;
14241 Prim_Elmt : Elmt_Id;
14242 Subp : Entity_Id;
14243 Typ : Entity_Id;
14244
14245 begin
14246 pragma Assert (Ada_Version >= Ada_2005
14247 and then Is_Record_Type (Tagged_Type)
14248 and then Is_Tagged_Type (Tagged_Type)
14249 and then Has_Interfaces (Tagged_Type));
14250
14251 -- Step 1: Transfer to the full-view primitives associated with the
14252 -- partial-view that cover interface primitives. Conceptually this
14253 -- work should be done later by Process_Full_View; done here to
14254 -- simplify its implementation at later stages. It can be safely
14255 -- done here because interfaces must be visible in the partial and
14256 -- private view (RM 7.3(7.3/2)).
14257
14258 -- Small optimization: This work is only required if the parent may
14259 -- have entities whose Alias attribute reference an interface primitive.
14260 -- Such a situation may occur if the parent is an abstract type and the
14261 -- primitive has not been yet overridden or if the parent is a generic
14262 -- formal type covering interfaces.
14263
14264 -- If the tagged type is not abstract, it cannot have abstract
14265 -- primitives (the only entities in the list of primitives of
14266 -- non-abstract tagged types that can reference abstract primitives
14267 -- through its Alias attribute are the internal entities that have
14268 -- attribute Interface_Alias, and these entities are generated later
14269 -- by Add_Internal_Interface_Entities).
14270
14271 if In_Private_Part (Current_Scope)
14272 and then (Is_Abstract_Type (Parent_Type)
14273 or else
14274 Is_Generic_Type (Parent_Type))
14275 then
14276 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
14277 while Present (Elmt) loop
14278 Subp := Node (Elmt);
14279
14280 -- At this stage it is not possible to have entities in the list
14281 -- of primitives that have attribute Interface_Alias.
14282
14283 pragma Assert (No (Interface_Alias (Subp)));
14284
14285 Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
14286
14287 if Is_Interface (Typ) then
14288 E := Find_Primitive_Covering_Interface
14289 (Tagged_Type => Tagged_Type,
14290 Iface_Prim => Subp);
14291
14292 if Present (E)
14293 and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
14294 then
14295 Replace_Elmt (Elmt, E);
14296 Remove_Homonym (Subp);
14297 end if;
14298 end if;
14299
14300 Next_Elmt (Elmt);
14301 end loop;
14302 end if;
14303
14304 -- Step 2: Add primitives of progenitors that are not implemented by
14305 -- parents of Tagged_Type.
14306
14307 if Present (Interfaces (Base_Type (Tagged_Type))) then
14308 Iface_Elmt := First_Elmt (Interfaces (Base_Type (Tagged_Type)));
14309 while Present (Iface_Elmt) loop
14310 Iface := Node (Iface_Elmt);
14311
14312 Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
14313 while Present (Prim_Elmt) loop
14314 Iface_Subp := Node (Prim_Elmt);
14315
14316 -- Exclude derivation of predefined primitives except those
14317 -- that come from source, or are inherited from one that comes
14318 -- from source. Required to catch declarations of equality
14319 -- operators of interfaces. For example:
14320
14321 -- type Iface is interface;
14322 -- function "=" (Left, Right : Iface) return Boolean;
14323
14324 if not Is_Predefined_Dispatching_Operation (Iface_Subp)
14325 or else Comes_From_Source (Ultimate_Alias (Iface_Subp))
14326 then
14327 E := Find_Primitive_Covering_Interface
14328 (Tagged_Type => Tagged_Type,
14329 Iface_Prim => Iface_Subp);
14330
14331 -- If not found we derive a new primitive leaving its alias
14332 -- attribute referencing the interface primitive.
14333
14334 if No (E) then
14335 Derive_Subprogram
14336 (New_Subp, Iface_Subp, Tagged_Type, Iface);
14337
14338 -- Ada 2012 (AI05-0197): If the covering primitive's name
14339 -- differs from the name of the interface primitive then it
14340 -- is a private primitive inherited from a parent type. In
14341 -- such case, given that Tagged_Type covers the interface,
14342 -- the inherited private primitive becomes visible. For such
14343 -- purpose we add a new entity that renames the inherited
14344 -- private primitive.
14345
14346 elsif Chars (E) /= Chars (Iface_Subp) then
14347 pragma Assert (Has_Suffix (E, 'P'));
14348 Derive_Subprogram
14349 (New_Subp, Iface_Subp, Tagged_Type, Iface);
14350 Set_Alias (New_Subp, E);
14351 Set_Is_Abstract_Subprogram (New_Subp,
14352 Is_Abstract_Subprogram (E));
14353
14354 -- Propagate to the full view interface entities associated
14355 -- with the partial view.
14356
14357 elsif In_Private_Part (Current_Scope)
14358 and then Present (Alias (E))
14359 and then Alias (E) = Iface_Subp
14360 and then
14361 List_Containing (Parent (E)) /=
14362 Private_Declarations
14363 (Specification
14364 (Unit_Declaration_Node (Current_Scope)))
14365 then
14366 Append_Elmt (E, Primitive_Operations (Tagged_Type));
14367 end if;
14368 end if;
14369
14370 Next_Elmt (Prim_Elmt);
14371 end loop;
14372
14373 Next_Elmt (Iface_Elmt);
14374 end loop;
14375 end if;
14376 end Derive_Progenitor_Subprograms;
14377
14378 -----------------------
14379 -- Derive_Subprogram --
14380 -----------------------
14381
14382 procedure Derive_Subprogram
14383 (New_Subp : in out Entity_Id;
14384 Parent_Subp : Entity_Id;
14385 Derived_Type : Entity_Id;
14386 Parent_Type : Entity_Id;
14387 Actual_Subp : Entity_Id := Empty)
14388 is
14389 Formal : Entity_Id;
14390 -- Formal parameter of parent primitive operation
14391
14392 Formal_Of_Actual : Entity_Id;
14393 -- Formal parameter of actual operation, when the derivation is to
14394 -- create a renaming for a primitive operation of an actual in an
14395 -- instantiation.
14396
14397 New_Formal : Entity_Id;
14398 -- Formal of inherited operation
14399
14400 Visible_Subp : Entity_Id := Parent_Subp;
14401
14402 function Is_Private_Overriding return Boolean;
14403 -- If Subp is a private overriding of a visible operation, the inherited
14404 -- operation derives from the overridden op (even though its body is the
14405 -- overriding one) and the inherited operation is visible now. See
14406 -- sem_disp to see the full details of the handling of the overridden
14407 -- subprogram, which is removed from the list of primitive operations of
14408 -- the type. The overridden subprogram is saved locally in Visible_Subp,
14409 -- and used to diagnose abstract operations that need overriding in the
14410 -- derived type.
14411
14412 procedure Replace_Type (Id, New_Id : Entity_Id);
14413 -- When the type is an anonymous access type, create a new access type
14414 -- designating the derived type.
14415
14416 procedure Set_Derived_Name;
14417 -- This procedure sets the appropriate Chars name for New_Subp. This
14418 -- is normally just a copy of the parent name. An exception arises for
14419 -- type support subprograms, where the name is changed to reflect the
14420 -- name of the derived type, e.g. if type foo is derived from type bar,
14421 -- then a procedure barDA is derived with a name fooDA.
14422
14423 ---------------------------
14424 -- Is_Private_Overriding --
14425 ---------------------------
14426
14427 function Is_Private_Overriding return Boolean is
14428 Prev : Entity_Id;
14429
14430 begin
14431 -- If the parent is not a dispatching operation there is no
14432 -- need to investigate overridings
14433
14434 if not Is_Dispatching_Operation (Parent_Subp) then
14435 return False;
14436 end if;
14437
14438 -- The visible operation that is overridden is a homonym of the
14439 -- parent subprogram. We scan the homonym chain to find the one
14440 -- whose alias is the subprogram we are deriving.
14441
14442 Prev := Current_Entity (Parent_Subp);
14443 while Present (Prev) loop
14444 if Ekind (Prev) = Ekind (Parent_Subp)
14445 and then Alias (Prev) = Parent_Subp
14446 and then Scope (Parent_Subp) = Scope (Prev)
14447 and then not Is_Hidden (Prev)
14448 then
14449 Visible_Subp := Prev;
14450 return True;
14451 end if;
14452
14453 Prev := Homonym (Prev);
14454 end loop;
14455
14456 return False;
14457 end Is_Private_Overriding;
14458
14459 ------------------
14460 -- Replace_Type --
14461 ------------------
14462
14463 procedure Replace_Type (Id, New_Id : Entity_Id) is
14464 Id_Type : constant Entity_Id := Etype (Id);
14465 Acc_Type : Entity_Id;
14466 Par : constant Node_Id := Parent (Derived_Type);
14467
14468 begin
14469 -- When the type is an anonymous access type, create a new access
14470 -- type designating the derived type. This itype must be elaborated
14471 -- at the point of the derivation, not on subsequent calls that may
14472 -- be out of the proper scope for Gigi, so we insert a reference to
14473 -- it after the derivation.
14474
14475 if Ekind (Id_Type) = E_Anonymous_Access_Type then
14476 declare
14477 Desig_Typ : Entity_Id := Designated_Type (Id_Type);
14478
14479 begin
14480 if Ekind (Desig_Typ) = E_Record_Type_With_Private
14481 and then Present (Full_View (Desig_Typ))
14482 and then not Is_Private_Type (Parent_Type)
14483 then
14484 Desig_Typ := Full_View (Desig_Typ);
14485 end if;
14486
14487 if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
14488
14489 -- Ada 2005 (AI-251): Handle also derivations of abstract
14490 -- interface primitives.
14491
14492 or else (Is_Interface (Desig_Typ)
14493 and then not Is_Class_Wide_Type (Desig_Typ))
14494 then
14495 Acc_Type := New_Copy (Id_Type);
14496 Set_Etype (Acc_Type, Acc_Type);
14497 Set_Scope (Acc_Type, New_Subp);
14498
14499 -- Set size of anonymous access type. If we have an access
14500 -- to an unconstrained array, this is a fat pointer, so it
14501 -- is sizes at twice addtress size.
14502
14503 if Is_Array_Type (Desig_Typ)
14504 and then not Is_Constrained (Desig_Typ)
14505 then
14506 Init_Size (Acc_Type, 2 * System_Address_Size);
14507
14508 -- Other cases use a thin pointer
14509
14510 else
14511 Init_Size (Acc_Type, System_Address_Size);
14512 end if;
14513
14514 -- Set remaining characterstics of anonymous access type
14515
14516 Init_Alignment (Acc_Type);
14517 Set_Directly_Designated_Type (Acc_Type, Derived_Type);
14518
14519 Set_Etype (New_Id, Acc_Type);
14520 Set_Scope (New_Id, New_Subp);
14521
14522 -- Create a reference to it
14523
14524 Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
14525
14526 else
14527 Set_Etype (New_Id, Id_Type);
14528 end if;
14529 end;
14530
14531 -- In Ada2012, a formal may have an incomplete type but the type
14532 -- derivation that inherits the primitive follows the full view.
14533
14534 elsif Base_Type (Id_Type) = Base_Type (Parent_Type)
14535 or else
14536 (Ekind (Id_Type) = E_Record_Type_With_Private
14537 and then Present (Full_View (Id_Type))
14538 and then
14539 Base_Type (Full_View (Id_Type)) = Base_Type (Parent_Type))
14540 or else
14541 (Ada_Version >= Ada_2012
14542 and then Ekind (Id_Type) = E_Incomplete_Type
14543 and then Full_View (Id_Type) = Parent_Type)
14544 then
14545 -- Constraint checks on formals are generated during expansion,
14546 -- based on the signature of the original subprogram. The bounds
14547 -- of the derived type are not relevant, and thus we can use
14548 -- the base type for the formals. However, the return type may be
14549 -- used in a context that requires that the proper static bounds
14550 -- be used (a case statement, for example) and for those cases
14551 -- we must use the derived type (first subtype), not its base.
14552
14553 -- If the derived_type_definition has no constraints, we know that
14554 -- the derived type has the same constraints as the first subtype
14555 -- of the parent, and we can also use it rather than its base,
14556 -- which can lead to more efficient code.
14557
14558 if Etype (Id) = Parent_Type then
14559 if Is_Scalar_Type (Parent_Type)
14560 and then
14561 Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
14562 then
14563 Set_Etype (New_Id, Derived_Type);
14564
14565 elsif Nkind (Par) = N_Full_Type_Declaration
14566 and then
14567 Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
14568 and then
14569 Is_Entity_Name
14570 (Subtype_Indication (Type_Definition (Par)))
14571 then
14572 Set_Etype (New_Id, Derived_Type);
14573
14574 else
14575 Set_Etype (New_Id, Base_Type (Derived_Type));
14576 end if;
14577
14578 else
14579 Set_Etype (New_Id, Base_Type (Derived_Type));
14580 end if;
14581
14582 else
14583 Set_Etype (New_Id, Etype (Id));
14584 end if;
14585 end Replace_Type;
14586
14587 ----------------------
14588 -- Set_Derived_Name --
14589 ----------------------
14590
14591 procedure Set_Derived_Name is
14592 Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
14593 begin
14594 if Nm = TSS_Null then
14595 Set_Chars (New_Subp, Chars (Parent_Subp));
14596 else
14597 Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
14598 end if;
14599 end Set_Derived_Name;
14600
14601 -- Start of processing for Derive_Subprogram
14602
14603 begin
14604 New_Subp := New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
14605 Set_Ekind (New_Subp, Ekind (Parent_Subp));
14606
14607 -- Check whether the inherited subprogram is a private operation that
14608 -- should be inherited but not yet made visible. Such subprograms can
14609 -- become visible at a later point (e.g., the private part of a public
14610 -- child unit) via Declare_Inherited_Private_Subprograms. If the
14611 -- following predicate is true, then this is not such a private
14612 -- operation and the subprogram simply inherits the name of the parent
14613 -- subprogram. Note the special check for the names of controlled
14614 -- operations, which are currently exempted from being inherited with
14615 -- a hidden name because they must be findable for generation of
14616 -- implicit run-time calls.
14617
14618 if not Is_Hidden (Parent_Subp)
14619 or else Is_Internal (Parent_Subp)
14620 or else Is_Private_Overriding
14621 or else Is_Internal_Name (Chars (Parent_Subp))
14622 or else Nam_In (Chars (Parent_Subp), Name_Initialize,
14623 Name_Adjust,
14624 Name_Finalize)
14625 then
14626 Set_Derived_Name;
14627
14628 -- An inherited dispatching equality will be overridden by an internally
14629 -- generated one, or by an explicit one, so preserve its name and thus
14630 -- its entry in the dispatch table. Otherwise, if Parent_Subp is a
14631 -- private operation it may become invisible if the full view has
14632 -- progenitors, and the dispatch table will be malformed.
14633 -- We check that the type is limited to handle the anomalous declaration
14634 -- of Limited_Controlled, which is derived from a non-limited type, and
14635 -- which is handled specially elsewhere as well.
14636
14637 elsif Chars (Parent_Subp) = Name_Op_Eq
14638 and then Is_Dispatching_Operation (Parent_Subp)
14639 and then Etype (Parent_Subp) = Standard_Boolean
14640 and then not Is_Limited_Type (Etype (First_Formal (Parent_Subp)))
14641 and then
14642 Etype (First_Formal (Parent_Subp)) =
14643 Etype (Next_Formal (First_Formal (Parent_Subp)))
14644 then
14645 Set_Derived_Name;
14646
14647 -- If parent is hidden, this can be a regular derivation if the
14648 -- parent is immediately visible in a non-instantiating context,
14649 -- or if we are in the private part of an instance. This test
14650 -- should still be refined ???
14651
14652 -- The test for In_Instance_Not_Visible avoids inheriting the derived
14653 -- operation as a non-visible operation in cases where the parent
14654 -- subprogram might not be visible now, but was visible within the
14655 -- original generic, so it would be wrong to make the inherited
14656 -- subprogram non-visible now. (Not clear if this test is fully
14657 -- correct; are there any cases where we should declare the inherited
14658 -- operation as not visible to avoid it being overridden, e.g., when
14659 -- the parent type is a generic actual with private primitives ???)
14660
14661 -- (they should be treated the same as other private inherited
14662 -- subprograms, but it's not clear how to do this cleanly). ???
14663
14664 elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
14665 and then Is_Immediately_Visible (Parent_Subp)
14666 and then not In_Instance)
14667 or else In_Instance_Not_Visible
14668 then
14669 Set_Derived_Name;
14670
14671 -- Ada 2005 (AI-251): Regular derivation if the parent subprogram
14672 -- overrides an interface primitive because interface primitives
14673 -- must be visible in the partial view of the parent (RM 7.3 (7.3/2))
14674
14675 elsif Ada_Version >= Ada_2005
14676 and then Is_Dispatching_Operation (Parent_Subp)
14677 and then Covers_Some_Interface (Parent_Subp)
14678 then
14679 Set_Derived_Name;
14680
14681 -- Otherwise, the type is inheriting a private operation, so enter
14682 -- it with a special name so it can't be overridden.
14683
14684 else
14685 Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
14686 end if;
14687
14688 Set_Parent (New_Subp, Parent (Derived_Type));
14689
14690 if Present (Actual_Subp) then
14691 Replace_Type (Actual_Subp, New_Subp);
14692 else
14693 Replace_Type (Parent_Subp, New_Subp);
14694 end if;
14695
14696 Conditional_Delay (New_Subp, Parent_Subp);
14697
14698 -- If we are creating a renaming for a primitive operation of an
14699 -- actual of a generic derived type, we must examine the signature
14700 -- of the actual primitive, not that of the generic formal, which for
14701 -- example may be an interface. However the name and initial value
14702 -- of the inherited operation are those of the formal primitive.
14703
14704 Formal := First_Formal (Parent_Subp);
14705
14706 if Present (Actual_Subp) then
14707 Formal_Of_Actual := First_Formal (Actual_Subp);
14708 else
14709 Formal_Of_Actual := Empty;
14710 end if;
14711
14712 while Present (Formal) loop
14713 New_Formal := New_Copy (Formal);
14714
14715 -- Normally we do not go copying parents, but in the case of
14716 -- formals, we need to link up to the declaration (which is the
14717 -- parameter specification), and it is fine to link up to the
14718 -- original formal's parameter specification in this case.
14719
14720 Set_Parent (New_Formal, Parent (Formal));
14721 Append_Entity (New_Formal, New_Subp);
14722
14723 if Present (Formal_Of_Actual) then
14724 Replace_Type (Formal_Of_Actual, New_Formal);
14725 Next_Formal (Formal_Of_Actual);
14726 else
14727 Replace_Type (Formal, New_Formal);
14728 end if;
14729
14730 Next_Formal (Formal);
14731 end loop;
14732
14733 -- If this derivation corresponds to a tagged generic actual, then
14734 -- primitive operations rename those of the actual. Otherwise the
14735 -- primitive operations rename those of the parent type, If the parent
14736 -- renames an intrinsic operator, so does the new subprogram. We except
14737 -- concatenation, which is always properly typed, and does not get
14738 -- expanded as other intrinsic operations.
14739
14740 if No (Actual_Subp) then
14741 if Is_Intrinsic_Subprogram (Parent_Subp) then
14742 Set_Is_Intrinsic_Subprogram (New_Subp);
14743
14744 if Present (Alias (Parent_Subp))
14745 and then Chars (Parent_Subp) /= Name_Op_Concat
14746 then
14747 Set_Alias (New_Subp, Alias (Parent_Subp));
14748 else
14749 Set_Alias (New_Subp, Parent_Subp);
14750 end if;
14751
14752 else
14753 Set_Alias (New_Subp, Parent_Subp);
14754 end if;
14755
14756 else
14757 Set_Alias (New_Subp, Actual_Subp);
14758 end if;
14759
14760 -- Inherit the "ghostness" from the parent subprogram
14761
14762 if Is_Ghost_Entity (Alias (New_Subp)) then
14763 Set_Is_Ghost_Entity (New_Subp);
14764 end if;
14765
14766 -- Derived subprograms of a tagged type must inherit the convention
14767 -- of the parent subprogram (a requirement of AI-117). Derived
14768 -- subprograms of untagged types simply get convention Ada by default.
14769
14770 -- If the derived type is a tagged generic formal type with unknown
14771 -- discriminants, its convention is intrinsic (RM 6.3.1 (8)).
14772
14773 -- However, if the type is derived from a generic formal, the further
14774 -- inherited subprogram has the convention of the non-generic ancestor.
14775 -- Otherwise there would be no way to override the operation.
14776 -- (This is subject to forthcoming ARG discussions).
14777
14778 if Is_Tagged_Type (Derived_Type) then
14779 if Is_Generic_Type (Derived_Type)
14780 and then Has_Unknown_Discriminants (Derived_Type)
14781 then
14782 Set_Convention (New_Subp, Convention_Intrinsic);
14783
14784 else
14785 if Is_Generic_Type (Parent_Type)
14786 and then Has_Unknown_Discriminants (Parent_Type)
14787 then
14788 Set_Convention (New_Subp, Convention (Alias (Parent_Subp)));
14789 else
14790 Set_Convention (New_Subp, Convention (Parent_Subp));
14791 end if;
14792 end if;
14793 end if;
14794
14795 -- Predefined controlled operations retain their name even if the parent
14796 -- is hidden (see above), but they are not primitive operations if the
14797 -- ancestor is not visible, for example if the parent is a private
14798 -- extension completed with a controlled extension. Note that a full
14799 -- type that is controlled can break privacy: the flag Is_Controlled is
14800 -- set on both views of the type.
14801
14802 if Is_Controlled (Parent_Type)
14803 and then Nam_In (Chars (Parent_Subp), Name_Initialize,
14804 Name_Adjust,
14805 Name_Finalize)
14806 and then Is_Hidden (Parent_Subp)
14807 and then not Is_Visibly_Controlled (Parent_Type)
14808 then
14809 Set_Is_Hidden (New_Subp);
14810 end if;
14811
14812 Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
14813 Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
14814
14815 if Ekind (Parent_Subp) = E_Procedure then
14816 Set_Is_Valued_Procedure
14817 (New_Subp, Is_Valued_Procedure (Parent_Subp));
14818 else
14819 Set_Has_Controlling_Result
14820 (New_Subp, Has_Controlling_Result (Parent_Subp));
14821 end if;
14822
14823 -- No_Return must be inherited properly. If this is overridden in the
14824 -- case of a dispatching operation, then a check is made in Sem_Disp
14825 -- that the overriding operation is also No_Return (no such check is
14826 -- required for the case of non-dispatching operation.
14827
14828 Set_No_Return (New_Subp, No_Return (Parent_Subp));
14829
14830 -- A derived function with a controlling result is abstract. If the
14831 -- Derived_Type is a nonabstract formal generic derived type, then
14832 -- inherited operations are not abstract: the required check is done at
14833 -- instantiation time. If the derivation is for a generic actual, the
14834 -- function is not abstract unless the actual is.
14835
14836 if Is_Generic_Type (Derived_Type)
14837 and then not Is_Abstract_Type (Derived_Type)
14838 then
14839 null;
14840
14841 -- Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
14842 -- properties of the subprogram, as defined in RM-3.9.3(4/2-6/2).
14843
14844 -- A subprogram subject to pragma Extensions_Visible with value False
14845 -- requires overriding if the subprogram has at least one controlling
14846 -- OUT parameter (SPARK RM 6.1.7(6)).
14847
14848 elsif Ada_Version >= Ada_2005
14849 and then (Is_Abstract_Subprogram (Alias (New_Subp))
14850 or else (Is_Tagged_Type (Derived_Type)
14851 and then Etype (New_Subp) = Derived_Type
14852 and then not Is_Null_Extension (Derived_Type))
14853 or else (Is_Tagged_Type (Derived_Type)
14854 and then Ekind (Etype (New_Subp)) =
14855 E_Anonymous_Access_Type
14856 and then Designated_Type (Etype (New_Subp)) =
14857 Derived_Type
14858 and then not Is_Null_Extension (Derived_Type))
14859 or else (Comes_From_Source (Alias (New_Subp))
14860 and then Is_EVF_Procedure (Alias (New_Subp))))
14861 and then No (Actual_Subp)
14862 then
14863 if not Is_Tagged_Type (Derived_Type)
14864 or else Is_Abstract_Type (Derived_Type)
14865 or else Is_Abstract_Subprogram (Alias (New_Subp))
14866 then
14867 Set_Is_Abstract_Subprogram (New_Subp);
14868 else
14869 Set_Requires_Overriding (New_Subp);
14870 end if;
14871
14872 elsif Ada_Version < Ada_2005
14873 and then (Is_Abstract_Subprogram (Alias (New_Subp))
14874 or else (Is_Tagged_Type (Derived_Type)
14875 and then Etype (New_Subp) = Derived_Type
14876 and then No (Actual_Subp)))
14877 then
14878 Set_Is_Abstract_Subprogram (New_Subp);
14879
14880 -- AI05-0097 : an inherited operation that dispatches on result is
14881 -- abstract if the derived type is abstract, even if the parent type
14882 -- is concrete and the derived type is a null extension.
14883
14884 elsif Has_Controlling_Result (Alias (New_Subp))
14885 and then Is_Abstract_Type (Etype (New_Subp))
14886 then
14887 Set_Is_Abstract_Subprogram (New_Subp);
14888
14889 -- Finally, if the parent type is abstract we must verify that all
14890 -- inherited operations are either non-abstract or overridden, or that
14891 -- the derived type itself is abstract (this check is performed at the
14892 -- end of a package declaration, in Check_Abstract_Overriding). A
14893 -- private overriding in the parent type will not be visible in the
14894 -- derivation if we are not in an inner package or in a child unit of
14895 -- the parent type, in which case the abstractness of the inherited
14896 -- operation is carried to the new subprogram.
14897
14898 elsif Is_Abstract_Type (Parent_Type)
14899 and then not In_Open_Scopes (Scope (Parent_Type))
14900 and then Is_Private_Overriding
14901 and then Is_Abstract_Subprogram (Visible_Subp)
14902 then
14903 if No (Actual_Subp) then
14904 Set_Alias (New_Subp, Visible_Subp);
14905 Set_Is_Abstract_Subprogram (New_Subp, True);
14906
14907 else
14908 -- If this is a derivation for an instance of a formal derived
14909 -- type, abstractness comes from the primitive operation of the
14910 -- actual, not from the operation inherited from the ancestor.
14911
14912 Set_Is_Abstract_Subprogram
14913 (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
14914 end if;
14915 end if;
14916
14917 New_Overloaded_Entity (New_Subp, Derived_Type);
14918
14919 -- Check for case of a derived subprogram for the instantiation of a
14920 -- formal derived tagged type, if so mark the subprogram as dispatching
14921 -- and inherit the dispatching attributes of the actual subprogram. The
14922 -- derived subprogram is effectively renaming of the actual subprogram,
14923 -- so it needs to have the same attributes as the actual.
14924
14925 if Present (Actual_Subp)
14926 and then Is_Dispatching_Operation (Actual_Subp)
14927 then
14928 Set_Is_Dispatching_Operation (New_Subp);
14929
14930 if Present (DTC_Entity (Actual_Subp)) then
14931 Set_DTC_Entity (New_Subp, DTC_Entity (Actual_Subp));
14932 Set_DT_Position_Value (New_Subp, DT_Position (Actual_Subp));
14933 end if;
14934 end if;
14935
14936 -- Indicate that a derived subprogram does not require a body and that
14937 -- it does not require processing of default expressions.
14938
14939 Set_Has_Completion (New_Subp);
14940 Set_Default_Expressions_Processed (New_Subp);
14941
14942 if Ekind (New_Subp) = E_Function then
14943 Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
14944 end if;
14945 end Derive_Subprogram;
14946
14947 ------------------------
14948 -- Derive_Subprograms --
14949 ------------------------
14950
14951 procedure Derive_Subprograms
14952 (Parent_Type : Entity_Id;
14953 Derived_Type : Entity_Id;
14954 Generic_Actual : Entity_Id := Empty)
14955 is
14956 Op_List : constant Elist_Id :=
14957 Collect_Primitive_Operations (Parent_Type);
14958
14959 function Check_Derived_Type return Boolean;
14960 -- Check that all the entities derived from Parent_Type are found in
14961 -- the list of primitives of Derived_Type exactly in the same order.
14962
14963 procedure Derive_Interface_Subprogram
14964 (New_Subp : in out Entity_Id;
14965 Subp : Entity_Id;
14966 Actual_Subp : Entity_Id);
14967 -- Derive New_Subp from the ultimate alias of the parent subprogram Subp
14968 -- (which is an interface primitive). If Generic_Actual is present then
14969 -- Actual_Subp is the actual subprogram corresponding with the generic
14970 -- subprogram Subp.
14971
14972 function Check_Derived_Type return Boolean is
14973 E : Entity_Id;
14974 Elmt : Elmt_Id;
14975 List : Elist_Id;
14976 New_Subp : Entity_Id;
14977 Op_Elmt : Elmt_Id;
14978 Subp : Entity_Id;
14979
14980 begin
14981 -- Traverse list of entities in the current scope searching for
14982 -- an incomplete type whose full-view is derived type
14983
14984 E := First_Entity (Scope (Derived_Type));
14985 while Present (E) and then E /= Derived_Type loop
14986 if Ekind (E) = E_Incomplete_Type
14987 and then Present (Full_View (E))
14988 and then Full_View (E) = Derived_Type
14989 then
14990 -- Disable this test if Derived_Type completes an incomplete
14991 -- type because in such case more primitives can be added
14992 -- later to the list of primitives of Derived_Type by routine
14993 -- Process_Incomplete_Dependents
14994
14995 return True;
14996 end if;
14997
14998 E := Next_Entity (E);
14999 end loop;
15000
15001 List := Collect_Primitive_Operations (Derived_Type);
15002 Elmt := First_Elmt (List);
15003
15004 Op_Elmt := First_Elmt (Op_List);
15005 while Present (Op_Elmt) loop
15006 Subp := Node (Op_Elmt);
15007 New_Subp := Node (Elmt);
15008
15009 -- At this early stage Derived_Type has no entities with attribute
15010 -- Interface_Alias. In addition, such primitives are always
15011 -- located at the end of the list of primitives of Parent_Type.
15012 -- Therefore, if found we can safely stop processing pending
15013 -- entities.
15014
15015 exit when Present (Interface_Alias (Subp));
15016
15017 -- Handle hidden entities
15018
15019 if not Is_Predefined_Dispatching_Operation (Subp)
15020 and then Is_Hidden (Subp)
15021 then
15022 if Present (New_Subp)
15023 and then Primitive_Names_Match (Subp, New_Subp)
15024 then
15025 Next_Elmt (Elmt);
15026 end if;
15027
15028 else
15029 if not Present (New_Subp)
15030 or else Ekind (Subp) /= Ekind (New_Subp)
15031 or else not Primitive_Names_Match (Subp, New_Subp)
15032 then
15033 return False;
15034 end if;
15035
15036 Next_Elmt (Elmt);
15037 end if;
15038
15039 Next_Elmt (Op_Elmt);
15040 end loop;
15041
15042 return True;
15043 end Check_Derived_Type;
15044
15045 ---------------------------------
15046 -- Derive_Interface_Subprogram --
15047 ---------------------------------
15048
15049 procedure Derive_Interface_Subprogram
15050 (New_Subp : in out Entity_Id;
15051 Subp : Entity_Id;
15052 Actual_Subp : Entity_Id)
15053 is
15054 Iface_Subp : constant Entity_Id := Ultimate_Alias (Subp);
15055 Iface_Type : constant Entity_Id := Find_Dispatching_Type (Iface_Subp);
15056
15057 begin
15058 pragma Assert (Is_Interface (Iface_Type));
15059
15060 Derive_Subprogram
15061 (New_Subp => New_Subp,
15062 Parent_Subp => Iface_Subp,
15063 Derived_Type => Derived_Type,
15064 Parent_Type => Iface_Type,
15065 Actual_Subp => Actual_Subp);
15066
15067 -- Given that this new interface entity corresponds with a primitive
15068 -- of the parent that was not overridden we must leave it associated
15069 -- with its parent primitive to ensure that it will share the same
15070 -- dispatch table slot when overridden.
15071
15072 if No (Actual_Subp) then
15073 Set_Alias (New_Subp, Subp);
15074
15075 -- For instantiations this is not needed since the previous call to
15076 -- Derive_Subprogram leaves the entity well decorated.
15077
15078 else
15079 pragma Assert (Alias (New_Subp) = Actual_Subp);
15080 null;
15081 end if;
15082 end Derive_Interface_Subprogram;
15083
15084 -- Local variables
15085
15086 Alias_Subp : Entity_Id;
15087 Act_List : Elist_Id;
15088 Act_Elmt : Elmt_Id;
15089 Act_Subp : Entity_Id := Empty;
15090 Elmt : Elmt_Id;
15091 Need_Search : Boolean := False;
15092 New_Subp : Entity_Id := Empty;
15093 Parent_Base : Entity_Id;
15094 Subp : Entity_Id;
15095
15096 -- Start of processing for Derive_Subprograms
15097
15098 begin
15099 if Ekind (Parent_Type) = E_Record_Type_With_Private
15100 and then Has_Discriminants (Parent_Type)
15101 and then Present (Full_View (Parent_Type))
15102 then
15103 Parent_Base := Full_View (Parent_Type);
15104 else
15105 Parent_Base := Parent_Type;
15106 end if;
15107
15108 if Present (Generic_Actual) then
15109 Act_List := Collect_Primitive_Operations (Generic_Actual);
15110 Act_Elmt := First_Elmt (Act_List);
15111 else
15112 Act_List := No_Elist;
15113 Act_Elmt := No_Elmt;
15114 end if;
15115
15116 -- Derive primitives inherited from the parent. Note that if the generic
15117 -- actual is present, this is not really a type derivation, it is a
15118 -- completion within an instance.
15119
15120 -- Case 1: Derived_Type does not implement interfaces
15121
15122 if not Is_Tagged_Type (Derived_Type)
15123 or else (not Has_Interfaces (Derived_Type)
15124 and then not (Present (Generic_Actual)
15125 and then Has_Interfaces (Generic_Actual)))
15126 then
15127 Elmt := First_Elmt (Op_List);
15128 while Present (Elmt) loop
15129 Subp := Node (Elmt);
15130
15131 -- Literals are derived earlier in the process of building the
15132 -- derived type, and are skipped here.
15133
15134 if Ekind (Subp) = E_Enumeration_Literal then
15135 null;
15136
15137 -- The actual is a direct descendant and the common primitive
15138 -- operations appear in the same order.
15139
15140 -- If the generic parent type is present, the derived type is an
15141 -- instance of a formal derived type, and within the instance its
15142 -- operations are those of the actual. We derive from the formal
15143 -- type but make the inherited operations aliases of the
15144 -- corresponding operations of the actual.
15145
15146 else
15147 pragma Assert (No (Node (Act_Elmt))
15148 or else (Primitive_Names_Match (Subp, Node (Act_Elmt))
15149 and then
15150 Type_Conformant
15151 (Subp, Node (Act_Elmt),
15152 Skip_Controlling_Formals => True)));
15153
15154 Derive_Subprogram
15155 (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
15156
15157 if Present (Act_Elmt) then
15158 Next_Elmt (Act_Elmt);
15159 end if;
15160 end if;
15161
15162 Next_Elmt (Elmt);
15163 end loop;
15164
15165 -- Case 2: Derived_Type implements interfaces
15166
15167 else
15168 -- If the parent type has no predefined primitives we remove
15169 -- predefined primitives from the list of primitives of generic
15170 -- actual to simplify the complexity of this algorithm.
15171
15172 if Present (Generic_Actual) then
15173 declare
15174 Has_Predefined_Primitives : Boolean := False;
15175
15176 begin
15177 -- Check if the parent type has predefined primitives
15178
15179 Elmt := First_Elmt (Op_List);
15180 while Present (Elmt) loop
15181 Subp := Node (Elmt);
15182
15183 if Is_Predefined_Dispatching_Operation (Subp)
15184 and then not Comes_From_Source (Ultimate_Alias (Subp))
15185 then
15186 Has_Predefined_Primitives := True;
15187 exit;
15188 end if;
15189
15190 Next_Elmt (Elmt);
15191 end loop;
15192
15193 -- Remove predefined primitives of Generic_Actual. We must use
15194 -- an auxiliary list because in case of tagged types the value
15195 -- returned by Collect_Primitive_Operations is the value stored
15196 -- in its Primitive_Operations attribute (and we don't want to
15197 -- modify its current contents).
15198
15199 if not Has_Predefined_Primitives then
15200 declare
15201 Aux_List : constant Elist_Id := New_Elmt_List;
15202
15203 begin
15204 Elmt := First_Elmt (Act_List);
15205 while Present (Elmt) loop
15206 Subp := Node (Elmt);
15207
15208 if not Is_Predefined_Dispatching_Operation (Subp)
15209 or else Comes_From_Source (Subp)
15210 then
15211 Append_Elmt (Subp, Aux_List);
15212 end if;
15213
15214 Next_Elmt (Elmt);
15215 end loop;
15216
15217 Act_List := Aux_List;
15218 end;
15219 end if;
15220
15221 Act_Elmt := First_Elmt (Act_List);
15222 Act_Subp := Node (Act_Elmt);
15223 end;
15224 end if;
15225
15226 -- Stage 1: If the generic actual is not present we derive the
15227 -- primitives inherited from the parent type. If the generic parent
15228 -- type is present, the derived type is an instance of a formal
15229 -- derived type, and within the instance its operations are those of
15230 -- the actual. We derive from the formal type but make the inherited
15231 -- operations aliases of the corresponding operations of the actual.
15232
15233 Elmt := First_Elmt (Op_List);
15234 while Present (Elmt) loop
15235 Subp := Node (Elmt);
15236 Alias_Subp := Ultimate_Alias (Subp);
15237
15238 -- Do not derive internal entities of the parent that link
15239 -- interface primitives with their covering primitive. These
15240 -- entities will be added to this type when frozen.
15241
15242 if Present (Interface_Alias (Subp)) then
15243 goto Continue;
15244 end if;
15245
15246 -- If the generic actual is present find the corresponding
15247 -- operation in the generic actual. If the parent type is a
15248 -- direct ancestor of the derived type then, even if it is an
15249 -- interface, the operations are inherited from the primary
15250 -- dispatch table and are in the proper order. If we detect here
15251 -- that primitives are not in the same order we traverse the list
15252 -- of primitive operations of the actual to find the one that
15253 -- implements the interface primitive.
15254
15255 if Need_Search
15256 or else
15257 (Present (Generic_Actual)
15258 and then Present (Act_Subp)
15259 and then not
15260 (Primitive_Names_Match (Subp, Act_Subp)
15261 and then
15262 Type_Conformant (Subp, Act_Subp,
15263 Skip_Controlling_Formals => True)))
15264 then
15265 pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual,
15266 Use_Full_View => True));
15267
15268 -- Remember that we need searching for all pending primitives
15269
15270 Need_Search := True;
15271
15272 -- Handle entities associated with interface primitives
15273
15274 if Present (Alias_Subp)
15275 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
15276 and then not Is_Predefined_Dispatching_Operation (Subp)
15277 then
15278 -- Search for the primitive in the homonym chain
15279
15280 Act_Subp :=
15281 Find_Primitive_Covering_Interface
15282 (Tagged_Type => Generic_Actual,
15283 Iface_Prim => Alias_Subp);
15284
15285 -- Previous search may not locate primitives covering
15286 -- interfaces defined in generics units or instantiations.
15287 -- (it fails if the covering primitive has formals whose
15288 -- type is also defined in generics or instantiations).
15289 -- In such case we search in the list of primitives of the
15290 -- generic actual for the internal entity that links the
15291 -- interface primitive and the covering primitive.
15292
15293 if No (Act_Subp)
15294 and then Is_Generic_Type (Parent_Type)
15295 then
15296 -- This code has been designed to handle only generic
15297 -- formals that implement interfaces that are defined
15298 -- in a generic unit or instantiation. If this code is
15299 -- needed for other cases we must review it because
15300 -- (given that it relies on Original_Location to locate
15301 -- the primitive of Generic_Actual that covers the
15302 -- interface) it could leave linked through attribute
15303 -- Alias entities of unrelated instantiations).
15304
15305 pragma Assert
15306 (Is_Generic_Unit
15307 (Scope (Find_Dispatching_Type (Alias_Subp)))
15308 or else
15309 Instantiation_Depth
15310 (Sloc (Find_Dispatching_Type (Alias_Subp))) > 0);
15311
15312 declare
15313 Iface_Prim_Loc : constant Source_Ptr :=
15314 Original_Location (Sloc (Alias_Subp));
15315
15316 Elmt : Elmt_Id;
15317 Prim : Entity_Id;
15318
15319 begin
15320 Elmt :=
15321 First_Elmt (Primitive_Operations (Generic_Actual));
15322
15323 Search : while Present (Elmt) loop
15324 Prim := Node (Elmt);
15325
15326 if Present (Interface_Alias (Prim))
15327 and then Original_Location
15328 (Sloc (Interface_Alias (Prim))) =
15329 Iface_Prim_Loc
15330 then
15331 Act_Subp := Alias (Prim);
15332 exit Search;
15333 end if;
15334
15335 Next_Elmt (Elmt);
15336 end loop Search;
15337 end;
15338 end if;
15339
15340 pragma Assert (Present (Act_Subp)
15341 or else Is_Abstract_Type (Generic_Actual)
15342 or else Serious_Errors_Detected > 0);
15343
15344 -- Handle predefined primitives plus the rest of user-defined
15345 -- primitives
15346
15347 else
15348 Act_Elmt := First_Elmt (Act_List);
15349 while Present (Act_Elmt) loop
15350 Act_Subp := Node (Act_Elmt);
15351
15352 exit when Primitive_Names_Match (Subp, Act_Subp)
15353 and then Type_Conformant
15354 (Subp, Act_Subp,
15355 Skip_Controlling_Formals => True)
15356 and then No (Interface_Alias (Act_Subp));
15357
15358 Next_Elmt (Act_Elmt);
15359 end loop;
15360
15361 if No (Act_Elmt) then
15362 Act_Subp := Empty;
15363 end if;
15364 end if;
15365 end if;
15366
15367 -- Case 1: If the parent is a limited interface then it has the
15368 -- predefined primitives of synchronized interfaces. However, the
15369 -- actual type may be a non-limited type and hence it does not
15370 -- have such primitives.
15371
15372 if Present (Generic_Actual)
15373 and then not Present (Act_Subp)
15374 and then Is_Limited_Interface (Parent_Base)
15375 and then Is_Predefined_Interface_Primitive (Subp)
15376 then
15377 null;
15378
15379 -- Case 2: Inherit entities associated with interfaces that were
15380 -- not covered by the parent type. We exclude here null interface
15381 -- primitives because they do not need special management.
15382
15383 -- We also exclude interface operations that are renamings. If the
15384 -- subprogram is an explicit renaming of an interface primitive,
15385 -- it is a regular primitive operation, and the presence of its
15386 -- alias is not relevant: it has to be derived like any other
15387 -- primitive.
15388
15389 elsif Present (Alias (Subp))
15390 and then Nkind (Unit_Declaration_Node (Subp)) /=
15391 N_Subprogram_Renaming_Declaration
15392 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
15393 and then not
15394 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
15395 and then Null_Present (Parent (Alias_Subp)))
15396 then
15397 -- If this is an abstract private type then we transfer the
15398 -- derivation of the interface primitive from the partial view
15399 -- to the full view. This is safe because all the interfaces
15400 -- must be visible in the partial view. Done to avoid adding
15401 -- a new interface derivation to the private part of the
15402 -- enclosing package; otherwise this new derivation would be
15403 -- decorated as hidden when the analysis of the enclosing
15404 -- package completes.
15405
15406 if Is_Abstract_Type (Derived_Type)
15407 and then In_Private_Part (Current_Scope)
15408 and then Has_Private_Declaration (Derived_Type)
15409 then
15410 declare
15411 Partial_View : Entity_Id;
15412 Elmt : Elmt_Id;
15413 Ent : Entity_Id;
15414
15415 begin
15416 Partial_View := First_Entity (Current_Scope);
15417 loop
15418 exit when No (Partial_View)
15419 or else (Has_Private_Declaration (Partial_View)
15420 and then
15421 Full_View (Partial_View) = Derived_Type);
15422
15423 Next_Entity (Partial_View);
15424 end loop;
15425
15426 -- If the partial view was not found then the source code
15427 -- has errors and the derivation is not needed.
15428
15429 if Present (Partial_View) then
15430 Elmt :=
15431 First_Elmt (Primitive_Operations (Partial_View));
15432 while Present (Elmt) loop
15433 Ent := Node (Elmt);
15434
15435 if Present (Alias (Ent))
15436 and then Ultimate_Alias (Ent) = Alias (Subp)
15437 then
15438 Append_Elmt
15439 (Ent, Primitive_Operations (Derived_Type));
15440 exit;
15441 end if;
15442
15443 Next_Elmt (Elmt);
15444 end loop;
15445
15446 -- If the interface primitive was not found in the
15447 -- partial view then this interface primitive was
15448 -- overridden. We add a derivation to activate in
15449 -- Derive_Progenitor_Subprograms the machinery to
15450 -- search for it.
15451
15452 if No (Elmt) then
15453 Derive_Interface_Subprogram
15454 (New_Subp => New_Subp,
15455 Subp => Subp,
15456 Actual_Subp => Act_Subp);
15457 end if;
15458 end if;
15459 end;
15460 else
15461 Derive_Interface_Subprogram
15462 (New_Subp => New_Subp,
15463 Subp => Subp,
15464 Actual_Subp => Act_Subp);
15465 end if;
15466
15467 -- Case 3: Common derivation
15468
15469 else
15470 Derive_Subprogram
15471 (New_Subp => New_Subp,
15472 Parent_Subp => Subp,
15473 Derived_Type => Derived_Type,
15474 Parent_Type => Parent_Base,
15475 Actual_Subp => Act_Subp);
15476 end if;
15477
15478 -- No need to update Act_Elm if we must search for the
15479 -- corresponding operation in the generic actual
15480
15481 if not Need_Search
15482 and then Present (Act_Elmt)
15483 then
15484 Next_Elmt (Act_Elmt);
15485 Act_Subp := Node (Act_Elmt);
15486 end if;
15487
15488 <<Continue>>
15489 Next_Elmt (Elmt);
15490 end loop;
15491
15492 -- Inherit additional operations from progenitors. If the derived
15493 -- type is a generic actual, there are not new primitive operations
15494 -- for the type because it has those of the actual, and therefore
15495 -- nothing needs to be done. The renamings generated above are not
15496 -- primitive operations, and their purpose is simply to make the
15497 -- proper operations visible within an instantiation.
15498
15499 if No (Generic_Actual) then
15500 Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
15501 end if;
15502 end if;
15503
15504 -- Final check: Direct descendants must have their primitives in the
15505 -- same order. We exclude from this test untagged types and instances
15506 -- of formal derived types. We skip this test if we have already
15507 -- reported serious errors in the sources.
15508
15509 pragma Assert (not Is_Tagged_Type (Derived_Type)
15510 or else Present (Generic_Actual)
15511 or else Serious_Errors_Detected > 0
15512 or else Check_Derived_Type);
15513 end Derive_Subprograms;
15514
15515 --------------------------------
15516 -- Derived_Standard_Character --
15517 --------------------------------
15518
15519 procedure Derived_Standard_Character
15520 (N : Node_Id;
15521 Parent_Type : Entity_Id;
15522 Derived_Type : Entity_Id)
15523 is
15524 Loc : constant Source_Ptr := Sloc (N);
15525 Def : constant Node_Id := Type_Definition (N);
15526 Indic : constant Node_Id := Subtype_Indication (Def);
15527 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
15528 Implicit_Base : constant Entity_Id :=
15529 Create_Itype
15530 (E_Enumeration_Type, N, Derived_Type, 'B');
15531
15532 Lo : Node_Id;
15533 Hi : Node_Id;
15534
15535 begin
15536 Discard_Node (Process_Subtype (Indic, N));
15537
15538 Set_Etype (Implicit_Base, Parent_Base);
15539 Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
15540 Set_RM_Size (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
15541
15542 Set_Is_Character_Type (Implicit_Base, True);
15543 Set_Has_Delayed_Freeze (Implicit_Base);
15544
15545 -- The bounds of the implicit base are the bounds of the parent base.
15546 -- Note that their type is the parent base.
15547
15548 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
15549 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
15550
15551 Set_Scalar_Range (Implicit_Base,
15552 Make_Range (Loc,
15553 Low_Bound => Lo,
15554 High_Bound => Hi));
15555
15556 Conditional_Delay (Derived_Type, Parent_Type);
15557
15558 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
15559 Set_Etype (Derived_Type, Implicit_Base);
15560 Set_Size_Info (Derived_Type, Parent_Type);
15561
15562 if Unknown_RM_Size (Derived_Type) then
15563 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
15564 end if;
15565
15566 Set_Is_Character_Type (Derived_Type, True);
15567
15568 if Nkind (Indic) /= N_Subtype_Indication then
15569
15570 -- If no explicit constraint, the bounds are those
15571 -- of the parent type.
15572
15573 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Type));
15574 Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
15575 Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
15576 end if;
15577
15578 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
15579
15580 -- Because the implicit base is used in the conversion of the bounds, we
15581 -- have to freeze it now. This is similar to what is done for numeric
15582 -- types, and it equally suspicious, but otherwise a non-static bound
15583 -- will have a reference to an unfrozen type, which is rejected by Gigi
15584 -- (???). This requires specific care for definition of stream
15585 -- attributes. For details, see comments at the end of
15586 -- Build_Derived_Numeric_Type.
15587
15588 Freeze_Before (N, Implicit_Base);
15589 end Derived_Standard_Character;
15590
15591 ------------------------------
15592 -- Derived_Type_Declaration --
15593 ------------------------------
15594
15595 procedure Derived_Type_Declaration
15596 (T : Entity_Id;
15597 N : Node_Id;
15598 Is_Completion : Boolean)
15599 is
15600 Parent_Type : Entity_Id;
15601
15602 function Comes_From_Generic (Typ : Entity_Id) return Boolean;
15603 -- Check whether the parent type is a generic formal, or derives
15604 -- directly or indirectly from one.
15605
15606 ------------------------
15607 -- Comes_From_Generic --
15608 ------------------------
15609
15610 function Comes_From_Generic (Typ : Entity_Id) return Boolean is
15611 begin
15612 if Is_Generic_Type (Typ) then
15613 return True;
15614
15615 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
15616 return True;
15617
15618 elsif Is_Private_Type (Typ)
15619 and then Present (Full_View (Typ))
15620 and then Is_Generic_Type (Root_Type (Full_View (Typ)))
15621 then
15622 return True;
15623
15624 elsif Is_Generic_Actual_Type (Typ) then
15625 return True;
15626
15627 else
15628 return False;
15629 end if;
15630 end Comes_From_Generic;
15631
15632 -- Local variables
15633
15634 Def : constant Node_Id := Type_Definition (N);
15635 Iface_Def : Node_Id;
15636 Indic : constant Node_Id := Subtype_Indication (Def);
15637 Extension : constant Node_Id := Record_Extension_Part (Def);
15638 Parent_Node : Node_Id;
15639 Taggd : Boolean;
15640
15641 -- Start of processing for Derived_Type_Declaration
15642
15643 begin
15644 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
15645
15646 -- Ada 2005 (AI-251): In case of interface derivation check that the
15647 -- parent is also an interface.
15648
15649 if Interface_Present (Def) then
15650 Check_SPARK_05_Restriction ("interface is not allowed", Def);
15651
15652 if not Is_Interface (Parent_Type) then
15653 Diagnose_Interface (Indic, Parent_Type);
15654
15655 else
15656 Parent_Node := Parent (Base_Type (Parent_Type));
15657 Iface_Def := Type_Definition (Parent_Node);
15658
15659 -- Ada 2005 (AI-251): Limited interfaces can only inherit from
15660 -- other limited interfaces.
15661
15662 if Limited_Present (Def) then
15663 if Limited_Present (Iface_Def) then
15664 null;
15665
15666 elsif Protected_Present (Iface_Def) then
15667 Error_Msg_NE
15668 ("descendant of& must be declared"
15669 & " as a protected interface",
15670 N, Parent_Type);
15671
15672 elsif Synchronized_Present (Iface_Def) then
15673 Error_Msg_NE
15674 ("descendant of& must be declared"
15675 & " as a synchronized interface",
15676 N, Parent_Type);
15677
15678 elsif Task_Present (Iface_Def) then
15679 Error_Msg_NE
15680 ("descendant of& must be declared as a task interface",
15681 N, Parent_Type);
15682
15683 else
15684 Error_Msg_N
15685 ("(Ada 2005) limited interface cannot "
15686 & "inherit from non-limited interface", Indic);
15687 end if;
15688
15689 -- Ada 2005 (AI-345): Non-limited interfaces can only inherit
15690 -- from non-limited or limited interfaces.
15691
15692 elsif not Protected_Present (Def)
15693 and then not Synchronized_Present (Def)
15694 and then not Task_Present (Def)
15695 then
15696 if Limited_Present (Iface_Def) then
15697 null;
15698
15699 elsif Protected_Present (Iface_Def) then
15700 Error_Msg_NE
15701 ("descendant of& must be declared"
15702 & " as a protected interface",
15703 N, Parent_Type);
15704
15705 elsif Synchronized_Present (Iface_Def) then
15706 Error_Msg_NE
15707 ("descendant of& must be declared"
15708 & " as a synchronized interface",
15709 N, Parent_Type);
15710
15711 elsif Task_Present (Iface_Def) then
15712 Error_Msg_NE
15713 ("descendant of& must be declared as a task interface",
15714 N, Parent_Type);
15715 else
15716 null;
15717 end if;
15718 end if;
15719 end if;
15720 end if;
15721
15722 if Is_Tagged_Type (Parent_Type)
15723 and then Is_Concurrent_Type (Parent_Type)
15724 and then not Is_Interface (Parent_Type)
15725 then
15726 Error_Msg_N
15727 ("parent type of a record extension cannot be "
15728 & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
15729 Set_Etype (T, Any_Type);
15730 return;
15731 end if;
15732
15733 -- Ada 2005 (AI-251): Decorate all the names in the list of ancestor
15734 -- interfaces
15735
15736 if Is_Tagged_Type (Parent_Type)
15737 and then Is_Non_Empty_List (Interface_List (Def))
15738 then
15739 declare
15740 Intf : Node_Id;
15741 T : Entity_Id;
15742
15743 begin
15744 Intf := First (Interface_List (Def));
15745 while Present (Intf) loop
15746 T := Find_Type_Of_Subtype_Indic (Intf);
15747
15748 if not Is_Interface (T) then
15749 Diagnose_Interface (Intf, T);
15750
15751 -- Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
15752 -- a limited type from having a nonlimited progenitor.
15753
15754 elsif (Limited_Present (Def)
15755 or else (not Is_Interface (Parent_Type)
15756 and then Is_Limited_Type (Parent_Type)))
15757 and then not Is_Limited_Interface (T)
15758 then
15759 Error_Msg_NE
15760 ("progenitor interface& of limited type must be limited",
15761 N, T);
15762 end if;
15763
15764 Next (Intf);
15765 end loop;
15766 end;
15767 end if;
15768
15769 if Parent_Type = Any_Type
15770 or else Etype (Parent_Type) = Any_Type
15771 or else (Is_Class_Wide_Type (Parent_Type)
15772 and then Etype (Parent_Type) = T)
15773 then
15774 -- If Parent_Type is undefined or illegal, make new type into a
15775 -- subtype of Any_Type, and set a few attributes to prevent cascaded
15776 -- errors. If this is a self-definition, emit error now.
15777
15778 if T = Parent_Type or else T = Etype (Parent_Type) then
15779 Error_Msg_N ("type cannot be used in its own definition", Indic);
15780 end if;
15781
15782 Set_Ekind (T, Ekind (Parent_Type));
15783 Set_Etype (T, Any_Type);
15784 Set_Scalar_Range (T, Scalar_Range (Any_Type));
15785
15786 if Is_Tagged_Type (T)
15787 and then Is_Record_Type (T)
15788 then
15789 Set_Direct_Primitive_Operations (T, New_Elmt_List);
15790 end if;
15791
15792 return;
15793 end if;
15794
15795 -- Ada 2005 (AI-251): The case in which the parent of the full-view is
15796 -- an interface is special because the list of interfaces in the full
15797 -- view can be given in any order. For example:
15798
15799 -- type A is interface;
15800 -- type B is interface and A;
15801 -- type D is new B with private;
15802 -- private
15803 -- type D is new A and B with null record; -- 1 --
15804
15805 -- In this case we perform the following transformation of -1-:
15806
15807 -- type D is new B and A with null record;
15808
15809 -- If the parent of the full-view covers the parent of the partial-view
15810 -- we have two possible cases:
15811
15812 -- 1) They have the same parent
15813 -- 2) The parent of the full-view implements some further interfaces
15814
15815 -- In both cases we do not need to perform the transformation. In the
15816 -- first case the source program is correct and the transformation is
15817 -- not needed; in the second case the source program does not fulfill
15818 -- the no-hidden interfaces rule (AI-396) and the error will be reported
15819 -- later.
15820
15821 -- This transformation not only simplifies the rest of the analysis of
15822 -- this type declaration but also simplifies the correct generation of
15823 -- the object layout to the expander.
15824
15825 if In_Private_Part (Current_Scope)
15826 and then Is_Interface (Parent_Type)
15827 then
15828 declare
15829 Iface : Node_Id;
15830 Partial_View : Entity_Id;
15831 Partial_View_Parent : Entity_Id;
15832 New_Iface : Node_Id;
15833
15834 begin
15835 -- Look for the associated private type declaration
15836
15837 Partial_View := First_Entity (Current_Scope);
15838 loop
15839 exit when No (Partial_View)
15840 or else (Has_Private_Declaration (Partial_View)
15841 and then Full_View (Partial_View) = T);
15842
15843 Next_Entity (Partial_View);
15844 end loop;
15845
15846 -- If the partial view was not found then the source code has
15847 -- errors and the transformation is not needed.
15848
15849 if Present (Partial_View) then
15850 Partial_View_Parent := Etype (Partial_View);
15851
15852 -- If the parent of the full-view covers the parent of the
15853 -- partial-view we have nothing else to do.
15854
15855 if Interface_Present_In_Ancestor
15856 (Parent_Type, Partial_View_Parent)
15857 then
15858 null;
15859
15860 -- Traverse the list of interfaces of the full-view to look
15861 -- for the parent of the partial-view and perform the tree
15862 -- transformation.
15863
15864 else
15865 Iface := First (Interface_List (Def));
15866 while Present (Iface) loop
15867 if Etype (Iface) = Etype (Partial_View) then
15868 Rewrite (Subtype_Indication (Def),
15869 New_Copy (Subtype_Indication
15870 (Parent (Partial_View))));
15871
15872 New_Iface :=
15873 Make_Identifier (Sloc (N), Chars (Parent_Type));
15874 Append (New_Iface, Interface_List (Def));
15875
15876 -- Analyze the transformed code
15877
15878 Derived_Type_Declaration (T, N, Is_Completion);
15879 return;
15880 end if;
15881
15882 Next (Iface);
15883 end loop;
15884 end if;
15885 end if;
15886 end;
15887 end if;
15888
15889 -- Only composite types other than array types are allowed to have
15890 -- discriminants.
15891
15892 if Present (Discriminant_Specifications (N)) then
15893 if (Is_Elementary_Type (Parent_Type)
15894 or else
15895 Is_Array_Type (Parent_Type))
15896 and then not Error_Posted (N)
15897 then
15898 Error_Msg_N
15899 ("elementary or array type cannot have discriminants",
15900 Defining_Identifier (First (Discriminant_Specifications (N))));
15901 Set_Has_Discriminants (T, False);
15902
15903 -- The type is allowed to have discriminants
15904
15905 else
15906 Check_SPARK_05_Restriction ("discriminant type is not allowed", N);
15907 end if;
15908 end if;
15909
15910 -- In Ada 83, a derived type defined in a package specification cannot
15911 -- be used for further derivation until the end of its visible part.
15912 -- Note that derivation in the private part of the package is allowed.
15913
15914 if Ada_Version = Ada_83
15915 and then Is_Derived_Type (Parent_Type)
15916 and then In_Visible_Part (Scope (Parent_Type))
15917 then
15918 if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
15919 Error_Msg_N
15920 ("(Ada 83): premature use of type for derivation", Indic);
15921 end if;
15922 end if;
15923
15924 -- Check for early use of incomplete or private type
15925
15926 if Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
15927 Error_Msg_N ("premature derivation of incomplete type", Indic);
15928 return;
15929
15930 elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
15931 and then not Comes_From_Generic (Parent_Type))
15932 or else Has_Private_Component (Parent_Type)
15933 then
15934 -- The ancestor type of a formal type can be incomplete, in which
15935 -- case only the operations of the partial view are available in the
15936 -- generic. Subsequent checks may be required when the full view is
15937 -- analyzed to verify that a derivation from a tagged type has an
15938 -- extension.
15939
15940 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
15941 null;
15942
15943 elsif No (Underlying_Type (Parent_Type))
15944 or else Has_Private_Component (Parent_Type)
15945 then
15946 Error_Msg_N
15947 ("premature derivation of derived or private type", Indic);
15948
15949 -- Flag the type itself as being in error, this prevents some
15950 -- nasty problems with subsequent uses of the malformed type.
15951
15952 Set_Error_Posted (T);
15953
15954 -- Check that within the immediate scope of an untagged partial
15955 -- view it's illegal to derive from the partial view if the
15956 -- full view is tagged. (7.3(7))
15957
15958 -- We verify that the Parent_Type is a partial view by checking
15959 -- that it is not a Full_Type_Declaration (i.e. a private type or
15960 -- private extension declaration), to distinguish a partial view
15961 -- from a derivation from a private type which also appears as
15962 -- E_Private_Type. If the parent base type is not declared in an
15963 -- enclosing scope there is no need to check.
15964
15965 elsif Present (Full_View (Parent_Type))
15966 and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
15967 and then not Is_Tagged_Type (Parent_Type)
15968 and then Is_Tagged_Type (Full_View (Parent_Type))
15969 and then In_Open_Scopes (Scope (Base_Type (Parent_Type)))
15970 then
15971 Error_Msg_N
15972 ("premature derivation from type with tagged full view",
15973 Indic);
15974 end if;
15975 end if;
15976
15977 -- Check that form of derivation is appropriate
15978
15979 Taggd := Is_Tagged_Type (Parent_Type);
15980
15981 -- Set the parent type to the class-wide type's specific type in this
15982 -- case to prevent cascading errors
15983
15984 if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
15985 Error_Msg_N ("parent type must not be a class-wide type", Indic);
15986 Set_Etype (T, Etype (Parent_Type));
15987 return;
15988 end if;
15989
15990 if Present (Extension) and then not Taggd then
15991 Error_Msg_N
15992 ("type derived from untagged type cannot have extension", Indic);
15993
15994 elsif No (Extension) and then Taggd then
15995
15996 -- If this declaration is within a private part (or body) of a
15997 -- generic instantiation then the derivation is allowed (the parent
15998 -- type can only appear tagged in this case if it's a generic actual
15999 -- type, since it would otherwise have been rejected in the analysis
16000 -- of the generic template).
16001
16002 if not Is_Generic_Actual_Type (Parent_Type)
16003 or else In_Visible_Part (Scope (Parent_Type))
16004 then
16005 if Is_Class_Wide_Type (Parent_Type) then
16006 Error_Msg_N
16007 ("parent type must not be a class-wide type", Indic);
16008
16009 -- Use specific type to prevent cascaded errors.
16010
16011 Parent_Type := Etype (Parent_Type);
16012
16013 else
16014 Error_Msg_N
16015 ("type derived from tagged type must have extension", Indic);
16016 end if;
16017 end if;
16018 end if;
16019
16020 -- AI-443: Synchronized formal derived types require a private
16021 -- extension. There is no point in checking the ancestor type or
16022 -- the progenitors since the construct is wrong to begin with.
16023
16024 if Ada_Version >= Ada_2005
16025 and then Is_Generic_Type (T)
16026 and then Present (Original_Node (N))
16027 then
16028 declare
16029 Decl : constant Node_Id := Original_Node (N);
16030
16031 begin
16032 if Nkind (Decl) = N_Formal_Type_Declaration
16033 and then Nkind (Formal_Type_Definition (Decl)) =
16034 N_Formal_Derived_Type_Definition
16035 and then Synchronized_Present (Formal_Type_Definition (Decl))
16036 and then No (Extension)
16037
16038 -- Avoid emitting a duplicate error message
16039
16040 and then not Error_Posted (Indic)
16041 then
16042 Error_Msg_N
16043 ("synchronized derived type must have extension", N);
16044 end if;
16045 end;
16046 end if;
16047
16048 if Null_Exclusion_Present (Def)
16049 and then not Is_Access_Type (Parent_Type)
16050 then
16051 Error_Msg_N ("null exclusion can only apply to an access type", N);
16052 end if;
16053
16054 -- Avoid deriving parent primitives of underlying record views
16055
16056 Build_Derived_Type (N, Parent_Type, T, Is_Completion,
16057 Derive_Subps => not Is_Underlying_Record_View (T));
16058
16059 -- AI-419: The parent type of an explicitly limited derived type must
16060 -- be a limited type or a limited interface.
16061
16062 if Limited_Present (Def) then
16063 Set_Is_Limited_Record (T);
16064
16065 if Is_Interface (T) then
16066 Set_Is_Limited_Interface (T);
16067 end if;
16068
16069 if not Is_Limited_Type (Parent_Type)
16070 and then
16071 (not Is_Interface (Parent_Type)
16072 or else not Is_Limited_Interface (Parent_Type))
16073 then
16074 -- AI05-0096: a derivation in the private part of an instance is
16075 -- legal if the generic formal is untagged limited, and the actual
16076 -- is non-limited.
16077
16078 if Is_Generic_Actual_Type (Parent_Type)
16079 and then In_Private_Part (Current_Scope)
16080 and then
16081 not Is_Tagged_Type
16082 (Generic_Parent_Type (Parent (Parent_Type)))
16083 then
16084 null;
16085
16086 else
16087 Error_Msg_NE
16088 ("parent type& of limited type must be limited",
16089 N, Parent_Type);
16090 end if;
16091 end if;
16092 end if;
16093
16094 -- In SPARK, there are no derived type definitions other than type
16095 -- extensions of tagged record types.
16096
16097 if No (Extension) then
16098 Check_SPARK_05_Restriction
16099 ("derived type is not allowed", Original_Node (N));
16100 end if;
16101 end Derived_Type_Declaration;
16102
16103 ------------------------
16104 -- Diagnose_Interface --
16105 ------------------------
16106
16107 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id) is
16108 begin
16109 if not Is_Interface (E) and then E /= Any_Type then
16110 Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
16111 end if;
16112 end Diagnose_Interface;
16113
16114 ----------------------------------
16115 -- Enumeration_Type_Declaration --
16116 ----------------------------------
16117
16118 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
16119 Ev : Uint;
16120 L : Node_Id;
16121 R_Node : Node_Id;
16122 B_Node : Node_Id;
16123
16124 begin
16125 -- Create identifier node representing lower bound
16126
16127 B_Node := New_Node (N_Identifier, Sloc (Def));
16128 L := First (Literals (Def));
16129 Set_Chars (B_Node, Chars (L));
16130 Set_Entity (B_Node, L);
16131 Set_Etype (B_Node, T);
16132 Set_Is_Static_Expression (B_Node, True);
16133
16134 R_Node := New_Node (N_Range, Sloc (Def));
16135 Set_Low_Bound (R_Node, B_Node);
16136
16137 Set_Ekind (T, E_Enumeration_Type);
16138 Set_First_Literal (T, L);
16139 Set_Etype (T, T);
16140 Set_Is_Constrained (T);
16141
16142 Ev := Uint_0;
16143
16144 -- Loop through literals of enumeration type setting pos and rep values
16145 -- except that if the Ekind is already set, then it means the literal
16146 -- was already constructed (case of a derived type declaration and we
16147 -- should not disturb the Pos and Rep values.
16148
16149 while Present (L) loop
16150 if Ekind (L) /= E_Enumeration_Literal then
16151 Set_Ekind (L, E_Enumeration_Literal);
16152 Set_Enumeration_Pos (L, Ev);
16153 Set_Enumeration_Rep (L, Ev);
16154 Set_Is_Known_Valid (L, True);
16155 end if;
16156
16157 Set_Etype (L, T);
16158 New_Overloaded_Entity (L);
16159 Generate_Definition (L);
16160 Set_Convention (L, Convention_Intrinsic);
16161
16162 -- Case of character literal
16163
16164 if Nkind (L) = N_Defining_Character_Literal then
16165 Set_Is_Character_Type (T, True);
16166
16167 -- Check violation of No_Wide_Characters
16168
16169 if Restriction_Check_Required (No_Wide_Characters) then
16170 Get_Name_String (Chars (L));
16171
16172 if Name_Len >= 3 and then Name_Buffer (1 .. 2) = "QW" then
16173 Check_Restriction (No_Wide_Characters, L);
16174 end if;
16175 end if;
16176 end if;
16177
16178 Ev := Ev + 1;
16179 Next (L);
16180 end loop;
16181
16182 -- Now create a node representing upper bound
16183
16184 B_Node := New_Node (N_Identifier, Sloc (Def));
16185 Set_Chars (B_Node, Chars (Last (Literals (Def))));
16186 Set_Entity (B_Node, Last (Literals (Def)));
16187 Set_Etype (B_Node, T);
16188 Set_Is_Static_Expression (B_Node, True);
16189
16190 Set_High_Bound (R_Node, B_Node);
16191
16192 -- Initialize various fields of the type. Some of this information
16193 -- may be overwritten later through rep.clauses.
16194
16195 Set_Scalar_Range (T, R_Node);
16196 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
16197 Set_Enum_Esize (T);
16198 Set_Enum_Pos_To_Rep (T, Empty);
16199
16200 -- Set Discard_Names if configuration pragma set, or if there is
16201 -- a parameterless pragma in the current declarative region
16202
16203 if Global_Discard_Names or else Discard_Names (Scope (T)) then
16204 Set_Discard_Names (T);
16205 end if;
16206
16207 -- Process end label if there is one
16208
16209 if Present (Def) then
16210 Process_End_Label (Def, 'e', T);
16211 end if;
16212 end Enumeration_Type_Declaration;
16213
16214 ---------------------------------
16215 -- Expand_To_Stored_Constraint --
16216 ---------------------------------
16217
16218 function Expand_To_Stored_Constraint
16219 (Typ : Entity_Id;
16220 Constraint : Elist_Id) return Elist_Id
16221 is
16222 Explicitly_Discriminated_Type : Entity_Id;
16223 Expansion : Elist_Id;
16224 Discriminant : Entity_Id;
16225
16226 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
16227 -- Find the nearest type that actually specifies discriminants
16228
16229 ---------------------------------
16230 -- Type_With_Explicit_Discrims --
16231 ---------------------------------
16232
16233 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
16234 Typ : constant E := Base_Type (Id);
16235
16236 begin
16237 if Ekind (Typ) in Incomplete_Or_Private_Kind then
16238 if Present (Full_View (Typ)) then
16239 return Type_With_Explicit_Discrims (Full_View (Typ));
16240 end if;
16241
16242 else
16243 if Has_Discriminants (Typ) then
16244 return Typ;
16245 end if;
16246 end if;
16247
16248 if Etype (Typ) = Typ then
16249 return Empty;
16250 elsif Has_Discriminants (Typ) then
16251 return Typ;
16252 else
16253 return Type_With_Explicit_Discrims (Etype (Typ));
16254 end if;
16255
16256 end Type_With_Explicit_Discrims;
16257
16258 -- Start of processing for Expand_To_Stored_Constraint
16259
16260 begin
16261 if No (Constraint) or else Is_Empty_Elmt_List (Constraint) then
16262 return No_Elist;
16263 end if;
16264
16265 Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
16266
16267 if No (Explicitly_Discriminated_Type) then
16268 return No_Elist;
16269 end if;
16270
16271 Expansion := New_Elmt_List;
16272
16273 Discriminant :=
16274 First_Stored_Discriminant (Explicitly_Discriminated_Type);
16275 while Present (Discriminant) loop
16276 Append_Elmt
16277 (Get_Discriminant_Value
16278 (Discriminant, Explicitly_Discriminated_Type, Constraint),
16279 To => Expansion);
16280 Next_Stored_Discriminant (Discriminant);
16281 end loop;
16282
16283 return Expansion;
16284 end Expand_To_Stored_Constraint;
16285
16286 ---------------------------
16287 -- Find_Hidden_Interface --
16288 ---------------------------
16289
16290 function Find_Hidden_Interface
16291 (Src : Elist_Id;
16292 Dest : Elist_Id) return Entity_Id
16293 is
16294 Iface : Entity_Id;
16295 Iface_Elmt : Elmt_Id;
16296
16297 begin
16298 if Present (Src) and then Present (Dest) then
16299 Iface_Elmt := First_Elmt (Src);
16300 while Present (Iface_Elmt) loop
16301 Iface := Node (Iface_Elmt);
16302
16303 if Is_Interface (Iface)
16304 and then not Contain_Interface (Iface, Dest)
16305 then
16306 return Iface;
16307 end if;
16308
16309 Next_Elmt (Iface_Elmt);
16310 end loop;
16311 end if;
16312
16313 return Empty;
16314 end Find_Hidden_Interface;
16315
16316 --------------------
16317 -- Find_Type_Name --
16318 --------------------
16319
16320 function Find_Type_Name (N : Node_Id) return Entity_Id is
16321 Id : constant Entity_Id := Defining_Identifier (N);
16322 Prev : Entity_Id;
16323 New_Id : Entity_Id;
16324 Prev_Par : Node_Id;
16325
16326 procedure Check_Duplicate_Aspects;
16327 -- Check that aspects specified in a completion have not been specified
16328 -- already in the partial view. Type_Invariant and others can be
16329 -- specified on either view but never on both.
16330
16331 procedure Tag_Mismatch;
16332 -- Diagnose a tagged partial view whose full view is untagged.
16333 -- We post the message on the full view, with a reference to
16334 -- the previous partial view. The partial view can be private
16335 -- or incomplete, and these are handled in a different manner,
16336 -- so we determine the position of the error message from the
16337 -- respective slocs of both.
16338
16339 -----------------------------
16340 -- Check_Duplicate_Aspects --
16341 -----------------------------
16342 procedure Check_Duplicate_Aspects is
16343 Prev_Aspects : constant List_Id := Aspect_Specifications (Prev_Par);
16344 Full_Aspects : constant List_Id := Aspect_Specifications (N);
16345 F_Spec, P_Spec : Node_Id;
16346
16347 begin
16348 if Present (Prev_Aspects) and then Present (Full_Aspects) then
16349 F_Spec := First (Full_Aspects);
16350 while Present (F_Spec) loop
16351 P_Spec := First (Prev_Aspects);
16352 while Present (P_Spec) loop
16353 if Chars (Identifier (P_Spec)) = Chars (Identifier (F_Spec))
16354 then
16355 Error_Msg_N
16356 ("aspect already specified in private declaration",
16357 F_Spec);
16358 Remove (F_Spec);
16359 return;
16360 end if;
16361
16362 Next (P_Spec);
16363 end loop;
16364
16365 Next (F_Spec);
16366 end loop;
16367 end if;
16368 end Check_Duplicate_Aspects;
16369
16370 ------------------
16371 -- Tag_Mismatch --
16372 ------------------
16373
16374 procedure Tag_Mismatch is
16375 begin
16376 if Sloc (Prev) < Sloc (Id) then
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 ", Id, Prev);
16382 else
16383 Error_Msg_NE
16384 ("full declaration of } must be a tagged type ", Id, Prev);
16385 end if;
16386
16387 else
16388 if Ada_Version >= Ada_2012
16389 and then Nkind (N) = N_Private_Type_Declaration
16390 then
16391 Error_Msg_NE
16392 ("declaration of private } must be a tagged type ", Prev, Id);
16393 else
16394 Error_Msg_NE
16395 ("full declaration of } must be a tagged type ", Prev, Id);
16396 end if;
16397 end if;
16398 end Tag_Mismatch;
16399
16400 -- Start of processing for Find_Type_Name
16401
16402 begin
16403 -- Find incomplete declaration, if one was given
16404
16405 Prev := Current_Entity_In_Scope (Id);
16406
16407 -- New type declaration
16408
16409 if No (Prev) then
16410 Enter_Name (Id);
16411 return Id;
16412
16413 -- Previous declaration exists
16414
16415 else
16416 Prev_Par := Parent (Prev);
16417
16418 -- Error if not incomplete/private case except if previous
16419 -- declaration is implicit, etc. Enter_Name will emit error if
16420 -- appropriate.
16421
16422 if not Is_Incomplete_Or_Private_Type (Prev) then
16423 Enter_Name (Id);
16424 New_Id := Id;
16425
16426 -- Check invalid completion of private or incomplete type
16427
16428 elsif not Nkind_In (N, N_Full_Type_Declaration,
16429 N_Task_Type_Declaration,
16430 N_Protected_Type_Declaration)
16431 and then
16432 (Ada_Version < Ada_2012
16433 or else not Is_Incomplete_Type (Prev)
16434 or else not Nkind_In (N, N_Private_Type_Declaration,
16435 N_Private_Extension_Declaration))
16436 then
16437 -- Completion must be a full type declarations (RM 7.3(4))
16438
16439 Error_Msg_Sloc := Sloc (Prev);
16440 Error_Msg_NE ("invalid completion of }", Id, Prev);
16441
16442 -- Set scope of Id to avoid cascaded errors. Entity is never
16443 -- examined again, except when saving globals in generics.
16444
16445 Set_Scope (Id, Current_Scope);
16446 New_Id := Id;
16447
16448 -- If this is a repeated incomplete declaration, no further
16449 -- checks are possible.
16450
16451 if Nkind (N) = N_Incomplete_Type_Declaration then
16452 return Prev;
16453 end if;
16454
16455 -- Case of full declaration of incomplete type
16456
16457 elsif Ekind (Prev) = E_Incomplete_Type
16458 and then (Ada_Version < Ada_2012
16459 or else No (Full_View (Prev))
16460 or else not Is_Private_Type (Full_View (Prev)))
16461 then
16462 -- Indicate that the incomplete declaration has a matching full
16463 -- declaration. The defining occurrence of the incomplete
16464 -- declaration remains the visible one, and the procedure
16465 -- Get_Full_View dereferences it whenever the type is used.
16466
16467 if Present (Full_View (Prev)) then
16468 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
16469 end if;
16470
16471 Set_Full_View (Prev, Id);
16472 Append_Entity (Id, Current_Scope);
16473 Set_Is_Public (Id, Is_Public (Prev));
16474 Set_Is_Internal (Id);
16475 New_Id := Prev;
16476
16477 -- If the incomplete view is tagged, a class_wide type has been
16478 -- created already. Use it for the private type as well, in order
16479 -- to prevent multiple incompatible class-wide types that may be
16480 -- created for self-referential anonymous access components.
16481
16482 if Is_Tagged_Type (Prev)
16483 and then Present (Class_Wide_Type (Prev))
16484 then
16485 Set_Ekind (Id, Ekind (Prev)); -- will be reset later
16486 Set_Class_Wide_Type (Id, Class_Wide_Type (Prev));
16487
16488 -- The type of the classwide type is the current Id. Previously
16489 -- this was not done for private declarations because of order-
16490 -- of elaboration issues in the back-end, but gigi now handles
16491 -- this properly.
16492
16493 Set_Etype (Class_Wide_Type (Id), Id);
16494 end if;
16495
16496 -- Case of full declaration of private type
16497
16498 else
16499 -- If the private type was a completion of an incomplete type then
16500 -- update Prev to reference the private type
16501
16502 if Ada_Version >= Ada_2012
16503 and then Ekind (Prev) = E_Incomplete_Type
16504 and then Present (Full_View (Prev))
16505 and then Is_Private_Type (Full_View (Prev))
16506 then
16507 Prev := Full_View (Prev);
16508 Prev_Par := Parent (Prev);
16509 end if;
16510
16511 if Nkind (N) = N_Full_Type_Declaration
16512 and then Nkind_In
16513 (Type_Definition (N), N_Record_Definition,
16514 N_Derived_Type_Definition)
16515 and then Interface_Present (Type_Definition (N))
16516 then
16517 Error_Msg_N
16518 ("completion of private type cannot be an interface", N);
16519 end if;
16520
16521 if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
16522 if Etype (Prev) /= Prev then
16523
16524 -- Prev is a private subtype or a derived type, and needs
16525 -- no completion.
16526
16527 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
16528 New_Id := Id;
16529
16530 elsif Ekind (Prev) = E_Private_Type
16531 and then Nkind_In (N, N_Task_Type_Declaration,
16532 N_Protected_Type_Declaration)
16533 then
16534 Error_Msg_N
16535 ("completion of nonlimited type cannot be limited", N);
16536
16537 elsif Ekind (Prev) = E_Record_Type_With_Private
16538 and then Nkind_In (N, N_Task_Type_Declaration,
16539 N_Protected_Type_Declaration)
16540 then
16541 if not Is_Limited_Record (Prev) then
16542 Error_Msg_N
16543 ("completion of nonlimited type cannot be limited", N);
16544
16545 elsif No (Interface_List (N)) then
16546 Error_Msg_N
16547 ("completion of tagged private type must be tagged",
16548 N);
16549 end if;
16550 end if;
16551
16552 -- Ada 2005 (AI-251): Private extension declaration of a task
16553 -- type or a protected type. This case arises when covering
16554 -- interface types.
16555
16556 elsif Nkind_In (N, N_Task_Type_Declaration,
16557 N_Protected_Type_Declaration)
16558 then
16559 null;
16560
16561 elsif Nkind (N) /= N_Full_Type_Declaration
16562 or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
16563 then
16564 Error_Msg_N
16565 ("full view of private extension must be an extension", N);
16566
16567 elsif not (Abstract_Present (Parent (Prev)))
16568 and then Abstract_Present (Type_Definition (N))
16569 then
16570 Error_Msg_N
16571 ("full view of non-abstract extension cannot be abstract", N);
16572 end if;
16573
16574 if not In_Private_Part (Current_Scope) then
16575 Error_Msg_N
16576 ("declaration of full view must appear in private part", N);
16577 end if;
16578
16579 if Ada_Version >= Ada_2012 then
16580 Check_Duplicate_Aspects;
16581 end if;
16582
16583 Copy_And_Swap (Prev, Id);
16584 Set_Has_Private_Declaration (Prev);
16585 Set_Has_Private_Declaration (Id);
16586
16587 -- AI12-0133: Indicate whether we have a partial view with
16588 -- unknown discriminants, in which case initialization of objects
16589 -- of the type do not receive an invariant check.
16590
16591 Set_Partial_View_Has_Unknown_Discr
16592 (Prev, Has_Unknown_Discriminants (Id));
16593
16594 -- Preserve aspect and iterator flags that may have been set on
16595 -- the partial view.
16596
16597 Set_Has_Delayed_Aspects (Prev, Has_Delayed_Aspects (Id));
16598 Set_Has_Implicit_Dereference (Prev, Has_Implicit_Dereference (Id));
16599
16600 -- If no error, propagate freeze_node from private to full view.
16601 -- It may have been generated for an early operational item.
16602
16603 if Present (Freeze_Node (Id))
16604 and then Serious_Errors_Detected = 0
16605 and then No (Full_View (Id))
16606 then
16607 Set_Freeze_Node (Prev, Freeze_Node (Id));
16608 Set_Freeze_Node (Id, Empty);
16609 Set_First_Rep_Item (Prev, First_Rep_Item (Id));
16610 end if;
16611
16612 Set_Full_View (Id, Prev);
16613 New_Id := Prev;
16614 end if;
16615
16616 -- Verify that full declaration conforms to partial one
16617
16618 if Is_Incomplete_Or_Private_Type (Prev)
16619 and then Present (Discriminant_Specifications (Prev_Par))
16620 then
16621 if Present (Discriminant_Specifications (N)) then
16622 if Ekind (Prev) = E_Incomplete_Type then
16623 Check_Discriminant_Conformance (N, Prev, Prev);
16624 else
16625 Check_Discriminant_Conformance (N, Prev, Id);
16626 end if;
16627
16628 else
16629 Error_Msg_N
16630 ("missing discriminants in full type declaration", N);
16631
16632 -- To avoid cascaded errors on subsequent use, share the
16633 -- discriminants of the partial view.
16634
16635 Set_Discriminant_Specifications (N,
16636 Discriminant_Specifications (Prev_Par));
16637 end if;
16638 end if;
16639
16640 -- A prior untagged partial view can have an associated class-wide
16641 -- type due to use of the class attribute, and in this case the full
16642 -- type must also be tagged. This Ada 95 usage is deprecated in favor
16643 -- of incomplete tagged declarations, but we check for it.
16644
16645 if Is_Type (Prev)
16646 and then (Is_Tagged_Type (Prev)
16647 or else Present (Class_Wide_Type (Prev)))
16648 then
16649 -- Ada 2012 (AI05-0162): A private type may be the completion of
16650 -- an incomplete type.
16651
16652 if Ada_Version >= Ada_2012
16653 and then Is_Incomplete_Type (Prev)
16654 and then Nkind_In (N, N_Private_Type_Declaration,
16655 N_Private_Extension_Declaration)
16656 then
16657 -- No need to check private extensions since they are tagged
16658
16659 if Nkind (N) = N_Private_Type_Declaration
16660 and then not Tagged_Present (N)
16661 then
16662 Tag_Mismatch;
16663 end if;
16664
16665 -- The full declaration is either a tagged type (including
16666 -- a synchronized type that implements interfaces) or a
16667 -- type extension, otherwise this is an error.
16668
16669 elsif Nkind_In (N, N_Task_Type_Declaration,
16670 N_Protected_Type_Declaration)
16671 then
16672 if No (Interface_List (N)) and then not Error_Posted (N) then
16673 Tag_Mismatch;
16674 end if;
16675
16676 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
16677
16678 -- Indicate that the previous declaration (tagged incomplete
16679 -- or private declaration) requires the same on the full one.
16680
16681 if not Tagged_Present (Type_Definition (N)) then
16682 Tag_Mismatch;
16683 Set_Is_Tagged_Type (Id);
16684 end if;
16685
16686 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
16687 if No (Record_Extension_Part (Type_Definition (N))) then
16688 Error_Msg_NE
16689 ("full declaration of } must be a record extension",
16690 Prev, Id);
16691
16692 -- Set some attributes to produce a usable full view
16693
16694 Set_Is_Tagged_Type (Id);
16695 end if;
16696
16697 else
16698 Tag_Mismatch;
16699 end if;
16700 end if;
16701
16702 if Present (Prev)
16703 and then Nkind (Parent (Prev)) = N_Incomplete_Type_Declaration
16704 and then Present (Premature_Use (Parent (Prev)))
16705 then
16706 Error_Msg_Sloc := Sloc (N);
16707 Error_Msg_N
16708 ("\full declaration #", Premature_Use (Parent (Prev)));
16709 end if;
16710
16711 return New_Id;
16712 end if;
16713 end Find_Type_Name;
16714
16715 -------------------------
16716 -- Find_Type_Of_Object --
16717 -------------------------
16718
16719 function Find_Type_Of_Object
16720 (Obj_Def : Node_Id;
16721 Related_Nod : Node_Id) return Entity_Id
16722 is
16723 Def_Kind : constant Node_Kind := Nkind (Obj_Def);
16724 P : Node_Id := Parent (Obj_Def);
16725 T : Entity_Id;
16726 Nam : Name_Id;
16727
16728 begin
16729 -- If the parent is a component_definition node we climb to the
16730 -- component_declaration node
16731
16732 if Nkind (P) = N_Component_Definition then
16733 P := Parent (P);
16734 end if;
16735
16736 -- Case of an anonymous array subtype
16737
16738 if Nkind_In (Def_Kind, N_Constrained_Array_Definition,
16739 N_Unconstrained_Array_Definition)
16740 then
16741 T := Empty;
16742 Array_Type_Declaration (T, Obj_Def);
16743
16744 -- Create an explicit subtype whenever possible
16745
16746 elsif Nkind (P) /= N_Component_Declaration
16747 and then Def_Kind = N_Subtype_Indication
16748 then
16749 -- Base name of subtype on object name, which will be unique in
16750 -- the current scope.
16751
16752 -- If this is a duplicate declaration, return base type, to avoid
16753 -- generating duplicate anonymous types.
16754
16755 if Error_Posted (P) then
16756 Analyze (Subtype_Mark (Obj_Def));
16757 return Entity (Subtype_Mark (Obj_Def));
16758 end if;
16759
16760 Nam :=
16761 New_External_Name
16762 (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
16763
16764 T := Make_Defining_Identifier (Sloc (P), Nam);
16765
16766 Insert_Action (Obj_Def,
16767 Make_Subtype_Declaration (Sloc (P),
16768 Defining_Identifier => T,
16769 Subtype_Indication => Relocate_Node (Obj_Def)));
16770
16771 -- This subtype may need freezing, and this will not be done
16772 -- automatically if the object declaration is not in declarative
16773 -- part. Since this is an object declaration, the type cannot always
16774 -- be frozen here. Deferred constants do not freeze their type
16775 -- (which often enough will be private).
16776
16777 if Nkind (P) = N_Object_Declaration
16778 and then Constant_Present (P)
16779 and then No (Expression (P))
16780 then
16781 null;
16782
16783 -- Here we freeze the base type of object type to catch premature use
16784 -- of discriminated private type without a full view.
16785
16786 else
16787 Insert_Actions (Obj_Def, Freeze_Entity (Base_Type (T), P));
16788 end if;
16789
16790 -- Ada 2005 AI-406: the object definition in an object declaration
16791 -- can be an access definition.
16792
16793 elsif Def_Kind = N_Access_Definition then
16794 T := Access_Definition (Related_Nod, Obj_Def);
16795
16796 Set_Is_Local_Anonymous_Access
16797 (T,
16798 V => (Ada_Version < Ada_2012)
16799 or else (Nkind (P) /= N_Object_Declaration)
16800 or else Is_Library_Level_Entity (Defining_Identifier (P)));
16801
16802 -- Otherwise, the object definition is just a subtype_mark
16803
16804 else
16805 T := Process_Subtype (Obj_Def, Related_Nod);
16806
16807 -- If expansion is disabled an object definition that is an aggregate
16808 -- will not get expanded and may lead to scoping problems in the back
16809 -- end, if the object is referenced in an inner scope. In that case
16810 -- create an itype reference for the object definition now. This
16811 -- may be redundant in some cases, but harmless.
16812
16813 if Is_Itype (T)
16814 and then Nkind (Related_Nod) = N_Object_Declaration
16815 and then ASIS_Mode
16816 then
16817 Build_Itype_Reference (T, Related_Nod);
16818 end if;
16819 end if;
16820
16821 return T;
16822 end Find_Type_Of_Object;
16823
16824 --------------------------------
16825 -- Find_Type_Of_Subtype_Indic --
16826 --------------------------------
16827
16828 function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
16829 Typ : Entity_Id;
16830
16831 begin
16832 -- Case of subtype mark with a constraint
16833
16834 if Nkind (S) = N_Subtype_Indication then
16835 Find_Type (Subtype_Mark (S));
16836 Typ := Entity (Subtype_Mark (S));
16837
16838 if not
16839 Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
16840 then
16841 Error_Msg_N
16842 ("incorrect constraint for this kind of type", Constraint (S));
16843 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
16844 end if;
16845
16846 -- Otherwise we have a subtype mark without a constraint
16847
16848 elsif Error_Posted (S) then
16849 Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
16850 return Any_Type;
16851
16852 else
16853 Find_Type (S);
16854 Typ := Entity (S);
16855 end if;
16856
16857 -- Check No_Wide_Characters restriction
16858
16859 Check_Wide_Character_Restriction (Typ, S);
16860
16861 return Typ;
16862 end Find_Type_Of_Subtype_Indic;
16863
16864 -------------------------------------
16865 -- Floating_Point_Type_Declaration --
16866 -------------------------------------
16867
16868 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
16869 Digs : constant Node_Id := Digits_Expression (Def);
16870 Max_Digs_Val : constant Uint := Digits_Value (Standard_Long_Long_Float);
16871 Digs_Val : Uint;
16872 Base_Typ : Entity_Id;
16873 Implicit_Base : Entity_Id;
16874 Bound : Node_Id;
16875
16876 function Can_Derive_From (E : Entity_Id) return Boolean;
16877 -- Find if given digits value, and possibly a specified range, allows
16878 -- derivation from specified type
16879
16880 function Find_Base_Type return Entity_Id;
16881 -- Find a predefined base type that Def can derive from, or generate
16882 -- an error and substitute Long_Long_Float if none exists.
16883
16884 ---------------------
16885 -- Can_Derive_From --
16886 ---------------------
16887
16888 function Can_Derive_From (E : Entity_Id) return Boolean is
16889 Spec : constant Entity_Id := Real_Range_Specification (Def);
16890
16891 begin
16892 -- Check specified "digits" constraint
16893
16894 if Digs_Val > Digits_Value (E) then
16895 return False;
16896 end if;
16897
16898 -- Check for matching range, if specified
16899
16900 if Present (Spec) then
16901 if Expr_Value_R (Type_Low_Bound (E)) >
16902 Expr_Value_R (Low_Bound (Spec))
16903 then
16904 return False;
16905 end if;
16906
16907 if Expr_Value_R (Type_High_Bound (E)) <
16908 Expr_Value_R (High_Bound (Spec))
16909 then
16910 return False;
16911 end if;
16912 end if;
16913
16914 return True;
16915 end Can_Derive_From;
16916
16917 --------------------
16918 -- Find_Base_Type --
16919 --------------------
16920
16921 function Find_Base_Type return Entity_Id is
16922 Choice : Elmt_Id := First_Elmt (Predefined_Float_Types);
16923
16924 begin
16925 -- Iterate over the predefined types in order, returning the first
16926 -- one that Def can derive from.
16927
16928 while Present (Choice) loop
16929 if Can_Derive_From (Node (Choice)) then
16930 return Node (Choice);
16931 end if;
16932
16933 Next_Elmt (Choice);
16934 end loop;
16935
16936 -- If we can't derive from any existing type, use Long_Long_Float
16937 -- and give appropriate message explaining the problem.
16938
16939 if Digs_Val > Max_Digs_Val then
16940 -- It might be the case that there is a type with the requested
16941 -- range, just not the combination of digits and range.
16942
16943 Error_Msg_N
16944 ("no predefined type has requested range and precision",
16945 Real_Range_Specification (Def));
16946
16947 else
16948 Error_Msg_N
16949 ("range too large for any predefined type",
16950 Real_Range_Specification (Def));
16951 end if;
16952
16953 return Standard_Long_Long_Float;
16954 end Find_Base_Type;
16955
16956 -- Start of processing for Floating_Point_Type_Declaration
16957
16958 begin
16959 Check_Restriction (No_Floating_Point, Def);
16960
16961 -- Create an implicit base type
16962
16963 Implicit_Base :=
16964 Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
16965
16966 -- Analyze and verify digits value
16967
16968 Analyze_And_Resolve (Digs, Any_Integer);
16969 Check_Digits_Expression (Digs);
16970 Digs_Val := Expr_Value (Digs);
16971
16972 -- Process possible range spec and find correct type to derive from
16973
16974 Process_Real_Range_Specification (Def);
16975
16976 -- Check that requested number of digits is not too high.
16977
16978 if Digs_Val > Max_Digs_Val then
16979
16980 -- The check for Max_Base_Digits may be somewhat expensive, as it
16981 -- requires reading System, so only do it when necessary.
16982
16983 declare
16984 Max_Base_Digits : constant Uint :=
16985 Expr_Value
16986 (Expression
16987 (Parent (RTE (RE_Max_Base_Digits))));
16988
16989 begin
16990 if Digs_Val > Max_Base_Digits then
16991 Error_Msg_Uint_1 := Max_Base_Digits;
16992 Error_Msg_N ("digits value out of range, maximum is ^", Digs);
16993
16994 elsif No (Real_Range_Specification (Def)) then
16995 Error_Msg_Uint_1 := Max_Digs_Val;
16996 Error_Msg_N ("types with more than ^ digits need range spec "
16997 & "(RM 3.5.7(6))", Digs);
16998 end if;
16999 end;
17000 end if;
17001
17002 -- Find a suitable type to derive from or complain and use a substitute
17003
17004 Base_Typ := Find_Base_Type;
17005
17006 -- If there are bounds given in the declaration use them as the bounds
17007 -- of the type, otherwise use the bounds of the predefined base type
17008 -- that was chosen based on the Digits value.
17009
17010 if Present (Real_Range_Specification (Def)) then
17011 Set_Scalar_Range (T, Real_Range_Specification (Def));
17012 Set_Is_Constrained (T);
17013
17014 -- The bounds of this range must be converted to machine numbers
17015 -- in accordance with RM 4.9(38).
17016
17017 Bound := Type_Low_Bound (T);
17018
17019 if Nkind (Bound) = N_Real_Literal then
17020 Set_Realval
17021 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
17022 Set_Is_Machine_Number (Bound);
17023 end if;
17024
17025 Bound := Type_High_Bound (T);
17026
17027 if Nkind (Bound) = N_Real_Literal then
17028 Set_Realval
17029 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
17030 Set_Is_Machine_Number (Bound);
17031 end if;
17032
17033 else
17034 Set_Scalar_Range (T, Scalar_Range (Base_Typ));
17035 end if;
17036
17037 -- Complete definition of implicit base and declared first subtype. The
17038 -- inheritance of the rep item chain ensures that SPARK-related pragmas
17039 -- are not clobbered when the floating point type acts as a full view of
17040 -- a private type.
17041
17042 Set_Etype (Implicit_Base, Base_Typ);
17043 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
17044 Set_Size_Info (Implicit_Base, Base_Typ);
17045 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
17046 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
17047 Set_Digits_Value (Implicit_Base, Digits_Value (Base_Typ));
17048 Set_Float_Rep (Implicit_Base, Float_Rep (Base_Typ));
17049
17050 Set_Ekind (T, E_Floating_Point_Subtype);
17051 Set_Etype (T, Implicit_Base);
17052 Set_Size_Info (T, Implicit_Base);
17053 Set_RM_Size (T, RM_Size (Implicit_Base));
17054 Inherit_Rep_Item_Chain (T, Implicit_Base);
17055 Set_Digits_Value (T, Digs_Val);
17056 end Floating_Point_Type_Declaration;
17057
17058 ----------------------------
17059 -- Get_Discriminant_Value --
17060 ----------------------------
17061
17062 -- This is the situation:
17063
17064 -- There is a non-derived type
17065
17066 -- type T0 (Dx, Dy, Dz...)
17067
17068 -- There are zero or more levels of derivation, with each derivation
17069 -- either purely inheriting the discriminants, or defining its own.
17070
17071 -- type Ti is new Ti-1
17072 -- or
17073 -- type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
17074 -- or
17075 -- subtype Ti is ...
17076
17077 -- The subtype issue is avoided by the use of Original_Record_Component,
17078 -- and the fact that derived subtypes also derive the constraints.
17079
17080 -- This chain leads back from
17081
17082 -- Typ_For_Constraint
17083
17084 -- Typ_For_Constraint has discriminants, and the value for each
17085 -- discriminant is given by its corresponding Elmt of Constraints.
17086
17087 -- Discriminant is some discriminant in this hierarchy
17088
17089 -- We need to return its value
17090
17091 -- We do this by recursively searching each level, and looking for
17092 -- Discriminant. Once we get to the bottom, we start backing up
17093 -- returning the value for it which may in turn be a discriminant
17094 -- further up, so on the backup we continue the substitution.
17095
17096 function Get_Discriminant_Value
17097 (Discriminant : Entity_Id;
17098 Typ_For_Constraint : Entity_Id;
17099 Constraint : Elist_Id) return Node_Id
17100 is
17101 function Root_Corresponding_Discriminant
17102 (Discr : Entity_Id) return Entity_Id;
17103 -- Given a discriminant, traverse the chain of inherited discriminants
17104 -- and return the topmost discriminant.
17105
17106 function Search_Derivation_Levels
17107 (Ti : Entity_Id;
17108 Discrim_Values : Elist_Id;
17109 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
17110 -- This is the routine that performs the recursive search of levels
17111 -- as described above.
17112
17113 -------------------------------------
17114 -- Root_Corresponding_Discriminant --
17115 -------------------------------------
17116
17117 function Root_Corresponding_Discriminant
17118 (Discr : Entity_Id) return Entity_Id
17119 is
17120 D : Entity_Id;
17121
17122 begin
17123 D := Discr;
17124 while Present (Corresponding_Discriminant (D)) loop
17125 D := Corresponding_Discriminant (D);
17126 end loop;
17127
17128 return D;
17129 end Root_Corresponding_Discriminant;
17130
17131 ------------------------------
17132 -- Search_Derivation_Levels --
17133 ------------------------------
17134
17135 function Search_Derivation_Levels
17136 (Ti : Entity_Id;
17137 Discrim_Values : Elist_Id;
17138 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
17139 is
17140 Assoc : Elmt_Id;
17141 Disc : Entity_Id;
17142 Result : Node_Or_Entity_Id;
17143 Result_Entity : Node_Id;
17144
17145 begin
17146 -- If inappropriate type, return Error, this happens only in
17147 -- cascaded error situations, and we want to avoid a blow up.
17148
17149 if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
17150 return Error;
17151 end if;
17152
17153 -- Look deeper if possible. Use Stored_Constraints only for
17154 -- untagged types. For tagged types use the given constraint.
17155 -- This asymmetry needs explanation???
17156
17157 if not Stored_Discrim_Values
17158 and then Present (Stored_Constraint (Ti))
17159 and then not Is_Tagged_Type (Ti)
17160 then
17161 Result :=
17162 Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
17163 else
17164 declare
17165 Td : constant Entity_Id := Etype (Ti);
17166
17167 begin
17168 if Td = Ti then
17169 Result := Discriminant;
17170
17171 else
17172 if Present (Stored_Constraint (Ti)) then
17173 Result :=
17174 Search_Derivation_Levels
17175 (Td, Stored_Constraint (Ti), True);
17176 else
17177 Result :=
17178 Search_Derivation_Levels
17179 (Td, Discrim_Values, Stored_Discrim_Values);
17180 end if;
17181 end if;
17182 end;
17183 end if;
17184
17185 -- Extra underlying places to search, if not found above. For
17186 -- concurrent types, the relevant discriminant appears in the
17187 -- corresponding record. For a type derived from a private type
17188 -- without discriminant, the full view inherits the discriminants
17189 -- of the full view of the parent.
17190
17191 if Result = Discriminant then
17192 if Is_Concurrent_Type (Ti)
17193 and then Present (Corresponding_Record_Type (Ti))
17194 then
17195 Result :=
17196 Search_Derivation_Levels (
17197 Corresponding_Record_Type (Ti),
17198 Discrim_Values,
17199 Stored_Discrim_Values);
17200
17201 elsif Is_Private_Type (Ti)
17202 and then not Has_Discriminants (Ti)
17203 and then Present (Full_View (Ti))
17204 and then Etype (Full_View (Ti)) /= Ti
17205 then
17206 Result :=
17207 Search_Derivation_Levels (
17208 Full_View (Ti),
17209 Discrim_Values,
17210 Stored_Discrim_Values);
17211 end if;
17212 end if;
17213
17214 -- If Result is not a (reference to a) discriminant, return it,
17215 -- otherwise set Result_Entity to the discriminant.
17216
17217 if Nkind (Result) = N_Defining_Identifier then
17218 pragma Assert (Result = Discriminant);
17219 Result_Entity := Result;
17220
17221 else
17222 if not Denotes_Discriminant (Result) then
17223 return Result;
17224 end if;
17225
17226 Result_Entity := Entity (Result);
17227 end if;
17228
17229 -- See if this level of derivation actually has discriminants because
17230 -- tagged derivations can add them, hence the lower levels need not
17231 -- have any.
17232
17233 if not Has_Discriminants (Ti) then
17234 return Result;
17235 end if;
17236
17237 -- Scan Ti's discriminants for Result_Entity, and return its
17238 -- corresponding value, if any.
17239
17240 Result_Entity := Original_Record_Component (Result_Entity);
17241
17242 Assoc := First_Elmt (Discrim_Values);
17243
17244 if Stored_Discrim_Values then
17245 Disc := First_Stored_Discriminant (Ti);
17246 else
17247 Disc := First_Discriminant (Ti);
17248 end if;
17249
17250 while Present (Disc) loop
17251 pragma Assert (Present (Assoc));
17252
17253 if Original_Record_Component (Disc) = Result_Entity then
17254 return Node (Assoc);
17255 end if;
17256
17257 Next_Elmt (Assoc);
17258
17259 if Stored_Discrim_Values then
17260 Next_Stored_Discriminant (Disc);
17261 else
17262 Next_Discriminant (Disc);
17263 end if;
17264 end loop;
17265
17266 -- Could not find it
17267
17268 return Result;
17269 end Search_Derivation_Levels;
17270
17271 -- Local Variables
17272
17273 Result : Node_Or_Entity_Id;
17274
17275 -- Start of processing for Get_Discriminant_Value
17276
17277 begin
17278 -- ??? This routine is a gigantic mess and will be deleted. For the
17279 -- time being just test for the trivial case before calling recurse.
17280
17281 if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
17282 declare
17283 D : Entity_Id;
17284 E : Elmt_Id;
17285
17286 begin
17287 D := First_Discriminant (Typ_For_Constraint);
17288 E := First_Elmt (Constraint);
17289 while Present (D) loop
17290 if Chars (D) = Chars (Discriminant) then
17291 return Node (E);
17292 end if;
17293
17294 Next_Discriminant (D);
17295 Next_Elmt (E);
17296 end loop;
17297 end;
17298 end if;
17299
17300 Result := Search_Derivation_Levels
17301 (Typ_For_Constraint, Constraint, False);
17302
17303 -- ??? hack to disappear when this routine is gone
17304
17305 if Nkind (Result) = N_Defining_Identifier then
17306 declare
17307 D : Entity_Id;
17308 E : Elmt_Id;
17309
17310 begin
17311 D := First_Discriminant (Typ_For_Constraint);
17312 E := First_Elmt (Constraint);
17313 while Present (D) loop
17314 if Root_Corresponding_Discriminant (D) = Discriminant then
17315 return Node (E);
17316 end if;
17317
17318 Next_Discriminant (D);
17319 Next_Elmt (E);
17320 end loop;
17321 end;
17322 end if;
17323
17324 pragma Assert (Nkind (Result) /= N_Defining_Identifier);
17325 return Result;
17326 end Get_Discriminant_Value;
17327
17328 --------------------------
17329 -- Has_Range_Constraint --
17330 --------------------------
17331
17332 function Has_Range_Constraint (N : Node_Id) return Boolean is
17333 C : constant Node_Id := Constraint (N);
17334
17335 begin
17336 if Nkind (C) = N_Range_Constraint then
17337 return True;
17338
17339 elsif Nkind (C) = N_Digits_Constraint then
17340 return
17341 Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
17342 or else Present (Range_Constraint (C));
17343
17344 elsif Nkind (C) = N_Delta_Constraint then
17345 return Present (Range_Constraint (C));
17346
17347 else
17348 return False;
17349 end if;
17350 end Has_Range_Constraint;
17351
17352 ------------------------
17353 -- Inherit_Components --
17354 ------------------------
17355
17356 function Inherit_Components
17357 (N : Node_Id;
17358 Parent_Base : Entity_Id;
17359 Derived_Base : Entity_Id;
17360 Is_Tagged : Boolean;
17361 Inherit_Discr : Boolean;
17362 Discs : Elist_Id) return Elist_Id
17363 is
17364 Assoc_List : constant Elist_Id := New_Elmt_List;
17365
17366 procedure Inherit_Component
17367 (Old_C : Entity_Id;
17368 Plain_Discrim : Boolean := False;
17369 Stored_Discrim : Boolean := False);
17370 -- Inherits component Old_C from Parent_Base to the Derived_Base. If
17371 -- Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
17372 -- True, Old_C is a stored discriminant. If they are both false then
17373 -- Old_C is a regular component.
17374
17375 -----------------------
17376 -- Inherit_Component --
17377 -----------------------
17378
17379 procedure Inherit_Component
17380 (Old_C : Entity_Id;
17381 Plain_Discrim : Boolean := False;
17382 Stored_Discrim : Boolean := False)
17383 is
17384 procedure Set_Anonymous_Type (Id : Entity_Id);
17385 -- Id denotes the entity of an access discriminant or anonymous
17386 -- access component. Set the type of Id to either the same type of
17387 -- Old_C or create a new one depending on whether the parent and
17388 -- the child types are in the same scope.
17389
17390 ------------------------
17391 -- Set_Anonymous_Type --
17392 ------------------------
17393
17394 procedure Set_Anonymous_Type (Id : Entity_Id) is
17395 Old_Typ : constant Entity_Id := Etype (Old_C);
17396
17397 begin
17398 if Scope (Parent_Base) = Scope (Derived_Base) then
17399 Set_Etype (Id, Old_Typ);
17400
17401 -- The parent and the derived type are in two different scopes.
17402 -- Reuse the type of the original discriminant / component by
17403 -- copying it in order to preserve all attributes.
17404
17405 else
17406 declare
17407 Typ : constant Entity_Id := New_Copy (Old_Typ);
17408
17409 begin
17410 Set_Etype (Id, Typ);
17411
17412 -- Since we do not generate component declarations for
17413 -- inherited components, associate the itype with the
17414 -- derived type.
17415
17416 Set_Associated_Node_For_Itype (Typ, Parent (Derived_Base));
17417 Set_Scope (Typ, Derived_Base);
17418 end;
17419 end if;
17420 end Set_Anonymous_Type;
17421
17422 -- Local variables and constants
17423
17424 New_C : constant Entity_Id := New_Copy (Old_C);
17425
17426 Corr_Discrim : Entity_Id;
17427 Discrim : Entity_Id;
17428
17429 -- Start of processing for Inherit_Component
17430
17431 begin
17432 pragma Assert (not Is_Tagged or not Stored_Discrim);
17433
17434 Set_Parent (New_C, Parent (Old_C));
17435
17436 -- Regular discriminants and components must be inserted in the scope
17437 -- of the Derived_Base. Do it here.
17438
17439 if not Stored_Discrim then
17440 Enter_Name (New_C);
17441 end if;
17442
17443 -- For tagged types the Original_Record_Component must point to
17444 -- whatever this field was pointing to in the parent type. This has
17445 -- already been achieved by the call to New_Copy above.
17446
17447 if not Is_Tagged then
17448 Set_Original_Record_Component (New_C, New_C);
17449 end if;
17450
17451 -- Set the proper type of an access discriminant
17452
17453 if Ekind (New_C) = E_Discriminant
17454 and then Ekind (Etype (New_C)) = E_Anonymous_Access_Type
17455 then
17456 Set_Anonymous_Type (New_C);
17457 end if;
17458
17459 -- If we have inherited a component then see if its Etype contains
17460 -- references to Parent_Base discriminants. In this case, replace
17461 -- these references with the constraints given in Discs. We do not
17462 -- do this for the partial view of private types because this is
17463 -- not needed (only the components of the full view will be used
17464 -- for code generation) and cause problem. We also avoid this
17465 -- transformation in some error situations.
17466
17467 if Ekind (New_C) = E_Component then
17468
17469 -- Set the proper type of an anonymous access component
17470
17471 if Ekind (Etype (New_C)) = E_Anonymous_Access_Type then
17472 Set_Anonymous_Type (New_C);
17473
17474 elsif (Is_Private_Type (Derived_Base)
17475 and then not Is_Generic_Type (Derived_Base))
17476 or else (Is_Empty_Elmt_List (Discs)
17477 and then not Expander_Active)
17478 then
17479 Set_Etype (New_C, Etype (Old_C));
17480
17481 else
17482 -- The current component introduces a circularity of the
17483 -- following kind:
17484
17485 -- limited with Pack_2;
17486 -- package Pack_1 is
17487 -- type T_1 is tagged record
17488 -- Comp : access Pack_2.T_2;
17489 -- ...
17490 -- end record;
17491 -- end Pack_1;
17492
17493 -- with Pack_1;
17494 -- package Pack_2 is
17495 -- type T_2 is new Pack_1.T_1 with ...;
17496 -- end Pack_2;
17497
17498 Set_Etype
17499 (New_C,
17500 Constrain_Component_Type
17501 (Old_C, Derived_Base, N, Parent_Base, Discs));
17502 end if;
17503 end if;
17504
17505 -- In derived tagged types it is illegal to reference a non
17506 -- discriminant component in the parent type. To catch this, mark
17507 -- these components with an Ekind of E_Void. This will be reset in
17508 -- Record_Type_Definition after processing the record extension of
17509 -- the derived type.
17510
17511 -- If the declaration is a private extension, there is no further
17512 -- record extension to process, and the components retain their
17513 -- current kind, because they are visible at this point.
17514
17515 if Is_Tagged and then Ekind (New_C) = E_Component
17516 and then Nkind (N) /= N_Private_Extension_Declaration
17517 then
17518 Set_Ekind (New_C, E_Void);
17519 end if;
17520
17521 if Plain_Discrim then
17522 Set_Corresponding_Discriminant (New_C, Old_C);
17523 Build_Discriminal (New_C);
17524
17525 -- If we are explicitly inheriting a stored discriminant it will be
17526 -- completely hidden.
17527
17528 elsif Stored_Discrim then
17529 Set_Corresponding_Discriminant (New_C, Empty);
17530 Set_Discriminal (New_C, Empty);
17531 Set_Is_Completely_Hidden (New_C);
17532
17533 -- Set the Original_Record_Component of each discriminant in the
17534 -- derived base to point to the corresponding stored that we just
17535 -- created.
17536
17537 Discrim := First_Discriminant (Derived_Base);
17538 while Present (Discrim) loop
17539 Corr_Discrim := Corresponding_Discriminant (Discrim);
17540
17541 -- Corr_Discrim could be missing in an error situation
17542
17543 if Present (Corr_Discrim)
17544 and then Original_Record_Component (Corr_Discrim) = Old_C
17545 then
17546 Set_Original_Record_Component (Discrim, New_C);
17547 end if;
17548
17549 Next_Discriminant (Discrim);
17550 end loop;
17551
17552 Append_Entity (New_C, Derived_Base);
17553 end if;
17554
17555 if not Is_Tagged then
17556 Append_Elmt (Old_C, Assoc_List);
17557 Append_Elmt (New_C, Assoc_List);
17558 end if;
17559 end Inherit_Component;
17560
17561 -- Variables local to Inherit_Component
17562
17563 Loc : constant Source_Ptr := Sloc (N);
17564
17565 Parent_Discrim : Entity_Id;
17566 Stored_Discrim : Entity_Id;
17567 D : Entity_Id;
17568 Component : Entity_Id;
17569
17570 -- Start of processing for Inherit_Components
17571
17572 begin
17573 if not Is_Tagged then
17574 Append_Elmt (Parent_Base, Assoc_List);
17575 Append_Elmt (Derived_Base, Assoc_List);
17576 end if;
17577
17578 -- Inherit parent discriminants if needed
17579
17580 if Inherit_Discr then
17581 Parent_Discrim := First_Discriminant (Parent_Base);
17582 while Present (Parent_Discrim) loop
17583 Inherit_Component (Parent_Discrim, Plain_Discrim => True);
17584 Next_Discriminant (Parent_Discrim);
17585 end loop;
17586 end if;
17587
17588 -- Create explicit stored discrims for untagged types when necessary
17589
17590 if not Has_Unknown_Discriminants (Derived_Base)
17591 and then Has_Discriminants (Parent_Base)
17592 and then not Is_Tagged
17593 and then
17594 (not Inherit_Discr
17595 or else First_Discriminant (Parent_Base) /=
17596 First_Stored_Discriminant (Parent_Base))
17597 then
17598 Stored_Discrim := First_Stored_Discriminant (Parent_Base);
17599 while Present (Stored_Discrim) loop
17600 Inherit_Component (Stored_Discrim, Stored_Discrim => True);
17601 Next_Stored_Discriminant (Stored_Discrim);
17602 end loop;
17603 end if;
17604
17605 -- See if we can apply the second transformation for derived types, as
17606 -- explained in point 6. in the comments above Build_Derived_Record_Type
17607 -- This is achieved by appending Derived_Base discriminants into Discs,
17608 -- which has the side effect of returning a non empty Discs list to the
17609 -- caller of Inherit_Components, which is what we want. This must be
17610 -- done for private derived types if there are explicit stored
17611 -- discriminants, to ensure that we can retrieve the values of the
17612 -- constraints provided in the ancestors.
17613
17614 if Inherit_Discr
17615 and then Is_Empty_Elmt_List (Discs)
17616 and then Present (First_Discriminant (Derived_Base))
17617 and then
17618 (not Is_Private_Type (Derived_Base)
17619 or else Is_Completely_Hidden
17620 (First_Stored_Discriminant (Derived_Base))
17621 or else Is_Generic_Type (Derived_Base))
17622 then
17623 D := First_Discriminant (Derived_Base);
17624 while Present (D) loop
17625 Append_Elmt (New_Occurrence_Of (D, Loc), Discs);
17626 Next_Discriminant (D);
17627 end loop;
17628 end if;
17629
17630 -- Finally, inherit non-discriminant components unless they are not
17631 -- visible because defined or inherited from the full view of the
17632 -- parent. Don't inherit the _parent field of the parent type.
17633
17634 Component := First_Entity (Parent_Base);
17635 while Present (Component) loop
17636
17637 -- Ada 2005 (AI-251): Do not inherit components associated with
17638 -- secondary tags of the parent.
17639
17640 if Ekind (Component) = E_Component
17641 and then Present (Related_Type (Component))
17642 then
17643 null;
17644
17645 elsif Ekind (Component) /= E_Component
17646 or else Chars (Component) = Name_uParent
17647 then
17648 null;
17649
17650 -- If the derived type is within the parent type's declarative
17651 -- region, then the components can still be inherited even though
17652 -- they aren't visible at this point. This can occur for cases
17653 -- such as within public child units where the components must
17654 -- become visible upon entering the child unit's private part.
17655
17656 elsif not Is_Visible_Component (Component)
17657 and then not In_Open_Scopes (Scope (Parent_Base))
17658 then
17659 null;
17660
17661 elsif Ekind_In (Derived_Base, E_Private_Type,
17662 E_Limited_Private_Type)
17663 then
17664 null;
17665
17666 else
17667 Inherit_Component (Component);
17668 end if;
17669
17670 Next_Entity (Component);
17671 end loop;
17672
17673 -- For tagged derived types, inherited discriminants cannot be used in
17674 -- component declarations of the record extension part. To achieve this
17675 -- we mark the inherited discriminants as not visible.
17676
17677 if Is_Tagged and then Inherit_Discr then
17678 D := First_Discriminant (Derived_Base);
17679 while Present (D) loop
17680 Set_Is_Immediately_Visible (D, False);
17681 Next_Discriminant (D);
17682 end loop;
17683 end if;
17684
17685 return Assoc_List;
17686 end Inherit_Components;
17687
17688 -----------------------------
17689 -- Inherit_Predicate_Flags --
17690 -----------------------------
17691
17692 procedure Inherit_Predicate_Flags (Subt, Par : Entity_Id) is
17693 begin
17694 Set_Has_Predicates (Subt, Has_Predicates (Par));
17695 Set_Has_Static_Predicate_Aspect
17696 (Subt, Has_Static_Predicate_Aspect (Par));
17697 Set_Has_Dynamic_Predicate_Aspect
17698 (Subt, Has_Dynamic_Predicate_Aspect (Par));
17699 end Inherit_Predicate_Flags;
17700
17701 ----------------------
17702 -- Is_EVF_Procedure --
17703 ----------------------
17704
17705 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean is
17706 Formal : Entity_Id;
17707
17708 begin
17709 -- Examine the formals of an Extensions_Visible False procedure looking
17710 -- for a controlling OUT parameter.
17711
17712 if Ekind (Subp) = E_Procedure
17713 and then Extensions_Visible_Status (Subp) = Extensions_Visible_False
17714 then
17715 Formal := First_Formal (Subp);
17716 while Present (Formal) loop
17717 if Ekind (Formal) = E_Out_Parameter
17718 and then Is_Controlling_Formal (Formal)
17719 then
17720 return True;
17721 end if;
17722
17723 Next_Formal (Formal);
17724 end loop;
17725 end if;
17726
17727 return False;
17728 end Is_EVF_Procedure;
17729
17730 -----------------------
17731 -- Is_Null_Extension --
17732 -----------------------
17733
17734 function Is_Null_Extension (T : Entity_Id) return Boolean is
17735 Type_Decl : constant Node_Id := Parent (Base_Type (T));
17736 Comp_List : Node_Id;
17737 Comp : Node_Id;
17738
17739 begin
17740 if Nkind (Type_Decl) /= N_Full_Type_Declaration
17741 or else not Is_Tagged_Type (T)
17742 or else Nkind (Type_Definition (Type_Decl)) /=
17743 N_Derived_Type_Definition
17744 or else No (Record_Extension_Part (Type_Definition (Type_Decl)))
17745 then
17746 return False;
17747 end if;
17748
17749 Comp_List :=
17750 Component_List (Record_Extension_Part (Type_Definition (Type_Decl)));
17751
17752 if Present (Discriminant_Specifications (Type_Decl)) then
17753 return False;
17754
17755 elsif Present (Comp_List)
17756 and then Is_Non_Empty_List (Component_Items (Comp_List))
17757 then
17758 Comp := First (Component_Items (Comp_List));
17759
17760 -- Only user-defined components are relevant. The component list
17761 -- may also contain a parent component and internal components
17762 -- corresponding to secondary tags, but these do not determine
17763 -- whether this is a null extension.
17764
17765 while Present (Comp) loop
17766 if Comes_From_Source (Comp) then
17767 return False;
17768 end if;
17769
17770 Next (Comp);
17771 end loop;
17772
17773 return True;
17774
17775 else
17776 return True;
17777 end if;
17778 end Is_Null_Extension;
17779
17780 ------------------------------
17781 -- Is_Valid_Constraint_Kind --
17782 ------------------------------
17783
17784 function Is_Valid_Constraint_Kind
17785 (T_Kind : Type_Kind;
17786 Constraint_Kind : Node_Kind) return Boolean
17787 is
17788 begin
17789 case T_Kind is
17790 when Enumeration_Kind |
17791 Integer_Kind =>
17792 return Constraint_Kind = N_Range_Constraint;
17793
17794 when Decimal_Fixed_Point_Kind =>
17795 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
17796 N_Range_Constraint);
17797
17798 when Ordinary_Fixed_Point_Kind =>
17799 return Nkind_In (Constraint_Kind, N_Delta_Constraint,
17800 N_Range_Constraint);
17801
17802 when Float_Kind =>
17803 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
17804 N_Range_Constraint);
17805
17806 when Access_Kind |
17807 Array_Kind |
17808 E_Record_Type |
17809 E_Record_Subtype |
17810 Class_Wide_Kind |
17811 E_Incomplete_Type |
17812 Private_Kind |
17813 Concurrent_Kind =>
17814 return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
17815
17816 when others =>
17817 return True; -- Error will be detected later
17818 end case;
17819 end Is_Valid_Constraint_Kind;
17820
17821 --------------------------
17822 -- Is_Visible_Component --
17823 --------------------------
17824
17825 function Is_Visible_Component
17826 (C : Entity_Id;
17827 N : Node_Id := Empty) return Boolean
17828 is
17829 Original_Comp : Entity_Id := Empty;
17830 Original_Scope : Entity_Id;
17831 Type_Scope : Entity_Id;
17832
17833 function Is_Local_Type (Typ : Entity_Id) return Boolean;
17834 -- Check whether parent type of inherited component is declared locally,
17835 -- possibly within a nested package or instance. The current scope is
17836 -- the derived record itself.
17837
17838 -------------------
17839 -- Is_Local_Type --
17840 -------------------
17841
17842 function Is_Local_Type (Typ : Entity_Id) return Boolean is
17843 Scop : Entity_Id;
17844
17845 begin
17846 Scop := Scope (Typ);
17847 while Present (Scop)
17848 and then Scop /= Standard_Standard
17849 loop
17850 if Scop = Scope (Current_Scope) then
17851 return True;
17852 end if;
17853
17854 Scop := Scope (Scop);
17855 end loop;
17856
17857 return False;
17858 end Is_Local_Type;
17859
17860 -- Start of processing for Is_Visible_Component
17861
17862 begin
17863 if Ekind_In (C, E_Component, E_Discriminant) then
17864 Original_Comp := Original_Record_Component (C);
17865 end if;
17866
17867 if No (Original_Comp) then
17868
17869 -- Premature usage, or previous error
17870
17871 return False;
17872
17873 else
17874 Original_Scope := Scope (Original_Comp);
17875 Type_Scope := Scope (Base_Type (Scope (C)));
17876 end if;
17877
17878 -- This test only concerns tagged types
17879
17880 if not Is_Tagged_Type (Original_Scope) then
17881 return True;
17882
17883 -- If it is _Parent or _Tag, there is no visibility issue
17884
17885 elsif not Comes_From_Source (Original_Comp) then
17886 return True;
17887
17888 -- Discriminants are visible unless the (private) type has unknown
17889 -- discriminants. If the discriminant reference is inserted for a
17890 -- discriminant check on a full view it is also visible.
17891
17892 elsif Ekind (Original_Comp) = E_Discriminant
17893 and then
17894 (not Has_Unknown_Discriminants (Original_Scope)
17895 or else (Present (N)
17896 and then Nkind (N) = N_Selected_Component
17897 and then Nkind (Prefix (N)) = N_Type_Conversion
17898 and then not Comes_From_Source (Prefix (N))))
17899 then
17900 return True;
17901
17902 -- In the body of an instantiation, no need to check for the visibility
17903 -- of a component.
17904
17905 elsif In_Instance_Body then
17906 return True;
17907
17908 -- If the component has been declared in an ancestor which is currently
17909 -- a private type, then it is not visible. The same applies if the
17910 -- component's containing type is not in an open scope and the original
17911 -- component's enclosing type is a visible full view of a private type
17912 -- (which can occur in cases where an attempt is being made to reference
17913 -- a component in a sibling package that is inherited from a visible
17914 -- component of a type in an ancestor package; the component in the
17915 -- sibling package should not be visible even though the component it
17916 -- inherited from is visible). This does not apply however in the case
17917 -- where the scope of the type is a private child unit, or when the
17918 -- parent comes from a local package in which the ancestor is currently
17919 -- visible. The latter suppression of visibility is needed for cases
17920 -- that are tested in B730006.
17921
17922 elsif Is_Private_Type (Original_Scope)
17923 or else
17924 (not Is_Private_Descendant (Type_Scope)
17925 and then not In_Open_Scopes (Type_Scope)
17926 and then Has_Private_Declaration (Original_Scope))
17927 then
17928 -- If the type derives from an entity in a formal package, there
17929 -- are no additional visible components.
17930
17931 if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
17932 N_Formal_Package_Declaration
17933 then
17934 return False;
17935
17936 -- if we are not in the private part of the current package, there
17937 -- are no additional visible components.
17938
17939 elsif Ekind (Scope (Current_Scope)) = E_Package
17940 and then not In_Private_Part (Scope (Current_Scope))
17941 then
17942 return False;
17943 else
17944 return
17945 Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
17946 and then In_Open_Scopes (Scope (Original_Scope))
17947 and then Is_Local_Type (Type_Scope);
17948 end if;
17949
17950 -- There is another weird way in which a component may be invisible when
17951 -- the private and the full view are not derived from the same ancestor.
17952 -- Here is an example :
17953
17954 -- type A1 is tagged record F1 : integer; end record;
17955 -- type A2 is new A1 with record F2 : integer; end record;
17956 -- type T is new A1 with private;
17957 -- private
17958 -- type T is new A2 with null record;
17959
17960 -- In this case, the full view of T inherits F1 and F2 but the private
17961 -- view inherits only F1
17962
17963 else
17964 declare
17965 Ancestor : Entity_Id := Scope (C);
17966
17967 begin
17968 loop
17969 if Ancestor = Original_Scope then
17970 return True;
17971 elsif Ancestor = Etype (Ancestor) then
17972 return False;
17973 end if;
17974
17975 Ancestor := Etype (Ancestor);
17976 end loop;
17977 end;
17978 end if;
17979 end Is_Visible_Component;
17980
17981 --------------------------
17982 -- Make_Class_Wide_Type --
17983 --------------------------
17984
17985 procedure Make_Class_Wide_Type (T : Entity_Id) is
17986 CW_Type : Entity_Id;
17987 CW_Name : Name_Id;
17988 Next_E : Entity_Id;
17989
17990 begin
17991 if Present (Class_Wide_Type (T)) then
17992
17993 -- The class-wide type is a partially decorated entity created for a
17994 -- unanalyzed tagged type referenced through a limited with clause.
17995 -- When the tagged type is analyzed, its class-wide type needs to be
17996 -- redecorated. Note that we reuse the entity created by Decorate_
17997 -- Tagged_Type in order to preserve all links.
17998
17999 if Materialize_Entity (Class_Wide_Type (T)) then
18000 CW_Type := Class_Wide_Type (T);
18001 Set_Materialize_Entity (CW_Type, False);
18002
18003 -- The class wide type can have been defined by the partial view, in
18004 -- which case everything is already done.
18005
18006 else
18007 return;
18008 end if;
18009
18010 -- Default case, we need to create a new class-wide type
18011
18012 else
18013 CW_Type :=
18014 New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
18015 end if;
18016
18017 -- Inherit root type characteristics
18018
18019 CW_Name := Chars (CW_Type);
18020 Next_E := Next_Entity (CW_Type);
18021 Copy_Node (T, CW_Type);
18022 Set_Comes_From_Source (CW_Type, False);
18023 Set_Chars (CW_Type, CW_Name);
18024 Set_Parent (CW_Type, Parent (T));
18025 Set_Next_Entity (CW_Type, Next_E);
18026
18027 -- Ensure we have a new freeze node for the class-wide type. The partial
18028 -- view may have freeze action of its own, requiring a proper freeze
18029 -- node, and the same freeze node cannot be shared between the two
18030 -- types.
18031
18032 Set_Has_Delayed_Freeze (CW_Type);
18033 Set_Freeze_Node (CW_Type, Empty);
18034
18035 -- Customize the class-wide type: It has no prim. op., it cannot be
18036 -- abstract and its Etype points back to the specific root type.
18037
18038 Set_Ekind (CW_Type, E_Class_Wide_Type);
18039 Set_Is_Tagged_Type (CW_Type, True);
18040 Set_Direct_Primitive_Operations (CW_Type, New_Elmt_List);
18041 Set_Is_Abstract_Type (CW_Type, False);
18042 Set_Is_Constrained (CW_Type, False);
18043 Set_Is_First_Subtype (CW_Type, Is_First_Subtype (T));
18044 Set_Default_SSO (CW_Type);
18045
18046 if Ekind (T) = E_Class_Wide_Subtype then
18047 Set_Etype (CW_Type, Etype (Base_Type (T)));
18048 else
18049 Set_Etype (CW_Type, T);
18050 end if;
18051
18052 Set_No_Tagged_Streams_Pragma (CW_Type, No_Tagged_Streams);
18053
18054 -- If this is the class_wide type of a constrained subtype, it does
18055 -- not have discriminants.
18056
18057 Set_Has_Discriminants (CW_Type,
18058 Has_Discriminants (T) and then not Is_Constrained (T));
18059
18060 Set_Has_Unknown_Discriminants (CW_Type, True);
18061 Set_Class_Wide_Type (T, CW_Type);
18062 Set_Equivalent_Type (CW_Type, Empty);
18063
18064 -- The class-wide type of a class-wide type is itself (RM 3.9(14))
18065
18066 Set_Class_Wide_Type (CW_Type, CW_Type);
18067 end Make_Class_Wide_Type;
18068
18069 ----------------
18070 -- Make_Index --
18071 ----------------
18072
18073 procedure Make_Index
18074 (N : Node_Id;
18075 Related_Nod : Node_Id;
18076 Related_Id : Entity_Id := Empty;
18077 Suffix_Index : Nat := 1;
18078 In_Iter_Schm : Boolean := False)
18079 is
18080 R : Node_Id;
18081 T : Entity_Id;
18082 Def_Id : Entity_Id := Empty;
18083 Found : Boolean := False;
18084
18085 begin
18086 -- For a discrete range used in a constrained array definition and
18087 -- defined by a range, an implicit conversion to the predefined type
18088 -- INTEGER is assumed if each bound is either a numeric literal, a named
18089 -- number, or an attribute, and the type of both bounds (prior to the
18090 -- implicit conversion) is the type universal_integer. Otherwise, both
18091 -- bounds must be of the same discrete type, other than universal
18092 -- integer; this type must be determinable independently of the
18093 -- context, but using the fact that the type must be discrete and that
18094 -- both bounds must have the same type.
18095
18096 -- Character literals also have a universal type in the absence of
18097 -- of additional context, and are resolved to Standard_Character.
18098
18099 if Nkind (N) = N_Range then
18100
18101 -- The index is given by a range constraint. The bounds are known
18102 -- to be of a consistent type.
18103
18104 if not Is_Overloaded (N) then
18105 T := Etype (N);
18106
18107 -- For universal bounds, choose the specific predefined type
18108
18109 if T = Universal_Integer then
18110 T := Standard_Integer;
18111
18112 elsif T = Any_Character then
18113 Ambiguous_Character (Low_Bound (N));
18114
18115 T := Standard_Character;
18116 end if;
18117
18118 -- The node may be overloaded because some user-defined operators
18119 -- are available, but if a universal interpretation exists it is
18120 -- also the selected one.
18121
18122 elsif Universal_Interpretation (N) = Universal_Integer then
18123 T := Standard_Integer;
18124
18125 else
18126 T := Any_Type;
18127
18128 declare
18129 Ind : Interp_Index;
18130 It : Interp;
18131
18132 begin
18133 Get_First_Interp (N, Ind, It);
18134 while Present (It.Typ) loop
18135 if Is_Discrete_Type (It.Typ) then
18136
18137 if Found
18138 and then not Covers (It.Typ, T)
18139 and then not Covers (T, It.Typ)
18140 then
18141 Error_Msg_N ("ambiguous bounds in discrete range", N);
18142 exit;
18143 else
18144 T := It.Typ;
18145 Found := True;
18146 end if;
18147 end if;
18148
18149 Get_Next_Interp (Ind, It);
18150 end loop;
18151
18152 if T = Any_Type then
18153 Error_Msg_N ("discrete type required for range", N);
18154 Set_Etype (N, Any_Type);
18155 return;
18156
18157 elsif T = Universal_Integer then
18158 T := Standard_Integer;
18159 end if;
18160 end;
18161 end if;
18162
18163 if not Is_Discrete_Type (T) then
18164 Error_Msg_N ("discrete type required for range", N);
18165 Set_Etype (N, Any_Type);
18166 return;
18167 end if;
18168
18169 if Nkind (Low_Bound (N)) = N_Attribute_Reference
18170 and then Attribute_Name (Low_Bound (N)) = Name_First
18171 and then Is_Entity_Name (Prefix (Low_Bound (N)))
18172 and then Is_Type (Entity (Prefix (Low_Bound (N))))
18173 and then Is_Discrete_Type (Entity (Prefix (Low_Bound (N))))
18174 then
18175 -- The type of the index will be the type of the prefix, as long
18176 -- as the upper bound is 'Last of the same type.
18177
18178 Def_Id := Entity (Prefix (Low_Bound (N)));
18179
18180 if Nkind (High_Bound (N)) /= N_Attribute_Reference
18181 or else Attribute_Name (High_Bound (N)) /= Name_Last
18182 or else not Is_Entity_Name (Prefix (High_Bound (N)))
18183 or else Entity (Prefix (High_Bound (N))) /= Def_Id
18184 then
18185 Def_Id := Empty;
18186 end if;
18187 end if;
18188
18189 R := N;
18190 Process_Range_Expr_In_Decl (R, T, In_Iter_Schm => In_Iter_Schm);
18191
18192 elsif Nkind (N) = N_Subtype_Indication then
18193
18194 -- The index is given by a subtype with a range constraint
18195
18196 T := Base_Type (Entity (Subtype_Mark (N)));
18197
18198 if not Is_Discrete_Type (T) then
18199 Error_Msg_N ("discrete type required for range", N);
18200 Set_Etype (N, Any_Type);
18201 return;
18202 end if;
18203
18204 R := Range_Expression (Constraint (N));
18205
18206 Resolve (R, T);
18207 Process_Range_Expr_In_Decl
18208 (R, Entity (Subtype_Mark (N)), In_Iter_Schm => In_Iter_Schm);
18209
18210 elsif Nkind (N) = N_Attribute_Reference then
18211
18212 -- Catch beginner's error (use of attribute other than 'Range)
18213
18214 if Attribute_Name (N) /= Name_Range then
18215 Error_Msg_N ("expect attribute ''Range", N);
18216 Set_Etype (N, Any_Type);
18217 return;
18218 end if;
18219
18220 -- If the node denotes the range of a type mark, that is also the
18221 -- resulting type, and we do not need to create an Itype for it.
18222
18223 if Is_Entity_Name (Prefix (N))
18224 and then Comes_From_Source (N)
18225 and then Is_Type (Entity (Prefix (N)))
18226 and then Is_Discrete_Type (Entity (Prefix (N)))
18227 then
18228 Def_Id := Entity (Prefix (N));
18229 end if;
18230
18231 Analyze_And_Resolve (N);
18232 T := Etype (N);
18233 R := N;
18234
18235 -- If none of the above, must be a subtype. We convert this to a
18236 -- range attribute reference because in the case of declared first
18237 -- named subtypes, the types in the range reference can be different
18238 -- from the type of the entity. A range attribute normalizes the
18239 -- reference and obtains the correct types for the bounds.
18240
18241 -- This transformation is in the nature of an expansion, is only
18242 -- done if expansion is active. In particular, it is not done on
18243 -- formal generic types, because we need to retain the name of the
18244 -- original index for instantiation purposes.
18245
18246 else
18247 if not Is_Entity_Name (N) or else not Is_Type (Entity (N)) then
18248 Error_Msg_N ("invalid subtype mark in discrete range ", N);
18249 Set_Etype (N, Any_Integer);
18250 return;
18251
18252 else
18253 -- The type mark may be that of an incomplete type. It is only
18254 -- now that we can get the full view, previous analysis does
18255 -- not look specifically for a type mark.
18256
18257 Set_Entity (N, Get_Full_View (Entity (N)));
18258 Set_Etype (N, Entity (N));
18259 Def_Id := Entity (N);
18260
18261 if not Is_Discrete_Type (Def_Id) then
18262 Error_Msg_N ("discrete type required for index", N);
18263 Set_Etype (N, Any_Type);
18264 return;
18265 end if;
18266 end if;
18267
18268 if Expander_Active then
18269 Rewrite (N,
18270 Make_Attribute_Reference (Sloc (N),
18271 Attribute_Name => Name_Range,
18272 Prefix => Relocate_Node (N)));
18273
18274 -- The original was a subtype mark that does not freeze. This
18275 -- means that the rewritten version must not freeze either.
18276
18277 Set_Must_Not_Freeze (N);
18278 Set_Must_Not_Freeze (Prefix (N));
18279 Analyze_And_Resolve (N);
18280 T := Etype (N);
18281 R := N;
18282
18283 -- If expander is inactive, type is legal, nothing else to construct
18284
18285 else
18286 return;
18287 end if;
18288 end if;
18289
18290 if not Is_Discrete_Type (T) then
18291 Error_Msg_N ("discrete type required for range", N);
18292 Set_Etype (N, Any_Type);
18293 return;
18294
18295 elsif T = Any_Type then
18296 Set_Etype (N, Any_Type);
18297 return;
18298 end if;
18299
18300 -- We will now create the appropriate Itype to describe the range, but
18301 -- first a check. If we originally had a subtype, then we just label
18302 -- the range with this subtype. Not only is there no need to construct
18303 -- a new subtype, but it is wrong to do so for two reasons:
18304
18305 -- 1. A legality concern, if we have a subtype, it must not freeze,
18306 -- and the Itype would cause freezing incorrectly
18307
18308 -- 2. An efficiency concern, if we created an Itype, it would not be
18309 -- recognized as the same type for the purposes of eliminating
18310 -- checks in some circumstances.
18311
18312 -- We signal this case by setting the subtype entity in Def_Id
18313
18314 if No (Def_Id) then
18315 Def_Id :=
18316 Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
18317 Set_Etype (Def_Id, Base_Type (T));
18318
18319 if Is_Signed_Integer_Type (T) then
18320 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
18321
18322 elsif Is_Modular_Integer_Type (T) then
18323 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
18324
18325 else
18326 Set_Ekind (Def_Id, E_Enumeration_Subtype);
18327 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
18328 Set_First_Literal (Def_Id, First_Literal (T));
18329 end if;
18330
18331 Set_Size_Info (Def_Id, (T));
18332 Set_RM_Size (Def_Id, RM_Size (T));
18333 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
18334
18335 Set_Scalar_Range (Def_Id, R);
18336 Conditional_Delay (Def_Id, T);
18337
18338 if Nkind (N) = N_Subtype_Indication then
18339 Inherit_Predicate_Flags (Def_Id, Entity (Subtype_Mark (N)));
18340 end if;
18341
18342 -- In the subtype indication case, if the immediate parent of the
18343 -- new subtype is non-static, then the subtype we create is non-
18344 -- static, even if its bounds are static.
18345
18346 if Nkind (N) = N_Subtype_Indication
18347 and then not Is_OK_Static_Subtype (Entity (Subtype_Mark (N)))
18348 then
18349 Set_Is_Non_Static_Subtype (Def_Id);
18350 end if;
18351 end if;
18352
18353 -- Final step is to label the index with this constructed type
18354
18355 Set_Etype (N, Def_Id);
18356 end Make_Index;
18357
18358 ------------------------------
18359 -- Modular_Type_Declaration --
18360 ------------------------------
18361
18362 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
18363 Mod_Expr : constant Node_Id := Expression (Def);
18364 M_Val : Uint;
18365
18366 procedure Set_Modular_Size (Bits : Int);
18367 -- Sets RM_Size to Bits, and Esize to normal word size above this
18368
18369 ----------------------
18370 -- Set_Modular_Size --
18371 ----------------------
18372
18373 procedure Set_Modular_Size (Bits : Int) is
18374 begin
18375 Set_RM_Size (T, UI_From_Int (Bits));
18376
18377 if Bits <= 8 then
18378 Init_Esize (T, 8);
18379
18380 elsif Bits <= 16 then
18381 Init_Esize (T, 16);
18382
18383 elsif Bits <= 32 then
18384 Init_Esize (T, 32);
18385
18386 else
18387 Init_Esize (T, System_Max_Binary_Modulus_Power);
18388 end if;
18389
18390 if not Non_Binary_Modulus (T) and then Esize (T) = RM_Size (T) then
18391 Set_Is_Known_Valid (T);
18392 end if;
18393 end Set_Modular_Size;
18394
18395 -- Start of processing for Modular_Type_Declaration
18396
18397 begin
18398 -- If the mod expression is (exactly) 2 * literal, where literal is
18399 -- 64 or less,then almost certainly the * was meant to be **. Warn.
18400
18401 if Warn_On_Suspicious_Modulus_Value
18402 and then Nkind (Mod_Expr) = N_Op_Multiply
18403 and then Nkind (Left_Opnd (Mod_Expr)) = N_Integer_Literal
18404 and then Intval (Left_Opnd (Mod_Expr)) = Uint_2
18405 and then Nkind (Right_Opnd (Mod_Expr)) = N_Integer_Literal
18406 and then Intval (Right_Opnd (Mod_Expr)) <= Uint_64
18407 then
18408 Error_Msg_N
18409 ("suspicious MOD value, was '*'* intended'??M?", Mod_Expr);
18410 end if;
18411
18412 -- Proceed with analysis of mod expression
18413
18414 Analyze_And_Resolve (Mod_Expr, Any_Integer);
18415 Set_Etype (T, T);
18416 Set_Ekind (T, E_Modular_Integer_Type);
18417 Init_Alignment (T);
18418 Set_Is_Constrained (T);
18419
18420 if not Is_OK_Static_Expression (Mod_Expr) then
18421 Flag_Non_Static_Expr
18422 ("non-static expression used for modular type bound!", Mod_Expr);
18423 M_Val := 2 ** System_Max_Binary_Modulus_Power;
18424 else
18425 M_Val := Expr_Value (Mod_Expr);
18426 end if;
18427
18428 if M_Val < 1 then
18429 Error_Msg_N ("modulus value must be positive", Mod_Expr);
18430 M_Val := 2 ** System_Max_Binary_Modulus_Power;
18431 end if;
18432
18433 if M_Val > 2 ** Standard_Long_Integer_Size then
18434 Check_Restriction (No_Long_Long_Integers, Mod_Expr);
18435 end if;
18436
18437 Set_Modulus (T, M_Val);
18438
18439 -- Create bounds for the modular type based on the modulus given in
18440 -- the type declaration and then analyze and resolve those bounds.
18441
18442 Set_Scalar_Range (T,
18443 Make_Range (Sloc (Mod_Expr),
18444 Low_Bound => Make_Integer_Literal (Sloc (Mod_Expr), 0),
18445 High_Bound => Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
18446
18447 -- Properly analyze the literals for the range. We do this manually
18448 -- because we can't go calling Resolve, since we are resolving these
18449 -- bounds with the type, and this type is certainly not complete yet.
18450
18451 Set_Etype (Low_Bound (Scalar_Range (T)), T);
18452 Set_Etype (High_Bound (Scalar_Range (T)), T);
18453 Set_Is_Static_Expression (Low_Bound (Scalar_Range (T)));
18454 Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
18455
18456 -- Loop through powers of two to find number of bits required
18457
18458 for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
18459
18460 -- Binary case
18461
18462 if M_Val = 2 ** Bits then
18463 Set_Modular_Size (Bits);
18464 return;
18465
18466 -- Non-binary case
18467
18468 elsif M_Val < 2 ** Bits then
18469 Check_SPARK_05_Restriction ("modulus should be a power of 2", T);
18470 Set_Non_Binary_Modulus (T);
18471
18472 if Bits > System_Max_Nonbinary_Modulus_Power then
18473 Error_Msg_Uint_1 :=
18474 UI_From_Int (System_Max_Nonbinary_Modulus_Power);
18475 Error_Msg_F
18476 ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
18477 Set_Modular_Size (System_Max_Binary_Modulus_Power);
18478 return;
18479
18480 else
18481 -- In the non-binary case, set size as per RM 13.3(55)
18482
18483 Set_Modular_Size (Bits);
18484 return;
18485 end if;
18486 end if;
18487
18488 end loop;
18489
18490 -- If we fall through, then the size exceed System.Max_Binary_Modulus
18491 -- so we just signal an error and set the maximum size.
18492
18493 Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
18494 Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
18495
18496 Set_Modular_Size (System_Max_Binary_Modulus_Power);
18497 Init_Alignment (T);
18498
18499 end Modular_Type_Declaration;
18500
18501 --------------------------
18502 -- New_Concatenation_Op --
18503 --------------------------
18504
18505 procedure New_Concatenation_Op (Typ : Entity_Id) is
18506 Loc : constant Source_Ptr := Sloc (Typ);
18507 Op : Entity_Id;
18508
18509 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
18510 -- Create abbreviated declaration for the formal of a predefined
18511 -- Operator 'Op' of type 'Typ'
18512
18513 --------------------
18514 -- Make_Op_Formal --
18515 --------------------
18516
18517 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
18518 Formal : Entity_Id;
18519 begin
18520 Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
18521 Set_Etype (Formal, Typ);
18522 Set_Mechanism (Formal, Default_Mechanism);
18523 return Formal;
18524 end Make_Op_Formal;
18525
18526 -- Start of processing for New_Concatenation_Op
18527
18528 begin
18529 Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
18530
18531 Set_Ekind (Op, E_Operator);
18532 Set_Scope (Op, Current_Scope);
18533 Set_Etype (Op, Typ);
18534 Set_Homonym (Op, Get_Name_Entity_Id (Name_Op_Concat));
18535 Set_Is_Immediately_Visible (Op);
18536 Set_Is_Intrinsic_Subprogram (Op);
18537 Set_Has_Completion (Op);
18538 Append_Entity (Op, Current_Scope);
18539
18540 Set_Name_Entity_Id (Name_Op_Concat, Op);
18541
18542 Append_Entity (Make_Op_Formal (Typ, Op), Op);
18543 Append_Entity (Make_Op_Formal (Typ, Op), Op);
18544 end New_Concatenation_Op;
18545
18546 -------------------------
18547 -- OK_For_Limited_Init --
18548 -------------------------
18549
18550 -- ???Check all calls of this, and compare the conditions under which it's
18551 -- called.
18552
18553 function OK_For_Limited_Init
18554 (Typ : Entity_Id;
18555 Exp : Node_Id) return Boolean
18556 is
18557 begin
18558 return Is_CPP_Constructor_Call (Exp)
18559 or else (Ada_Version >= Ada_2005
18560 and then not Debug_Flag_Dot_L
18561 and then OK_For_Limited_Init_In_05 (Typ, Exp));
18562 end OK_For_Limited_Init;
18563
18564 -------------------------------
18565 -- OK_For_Limited_Init_In_05 --
18566 -------------------------------
18567
18568 function OK_For_Limited_Init_In_05
18569 (Typ : Entity_Id;
18570 Exp : Node_Id) return Boolean
18571 is
18572 begin
18573 -- An object of a limited interface type can be initialized with any
18574 -- expression of a nonlimited descendant type.
18575
18576 if Is_Class_Wide_Type (Typ)
18577 and then Is_Limited_Interface (Typ)
18578 and then not Is_Limited_Type (Etype (Exp))
18579 then
18580 return True;
18581 end if;
18582
18583 -- Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
18584 -- case of limited aggregates (including extension aggregates), and
18585 -- function calls. The function call may have been given in prefixed
18586 -- notation, in which case the original node is an indexed component.
18587 -- If the function is parameterless, the original node was an explicit
18588 -- dereference. The function may also be parameterless, in which case
18589 -- the source node is just an identifier.
18590
18591 case Nkind (Original_Node (Exp)) is
18592 when N_Aggregate | N_Extension_Aggregate | N_Function_Call | N_Op =>
18593 return True;
18594
18595 when N_Identifier =>
18596 return Present (Entity (Original_Node (Exp)))
18597 and then Ekind (Entity (Original_Node (Exp))) = E_Function;
18598
18599 when N_Qualified_Expression =>
18600 return
18601 OK_For_Limited_Init_In_05
18602 (Typ, Expression (Original_Node (Exp)));
18603
18604 -- Ada 2005 (AI-251): If a class-wide interface object is initialized
18605 -- with a function call, the expander has rewritten the call into an
18606 -- N_Type_Conversion node to force displacement of the pointer to
18607 -- reference the component containing the secondary dispatch table.
18608 -- Otherwise a type conversion is not a legal context.
18609 -- A return statement for a build-in-place function returning a
18610 -- synchronized type also introduces an unchecked conversion.
18611
18612 when N_Type_Conversion |
18613 N_Unchecked_Type_Conversion =>
18614 return not Comes_From_Source (Exp)
18615 and then
18616 OK_For_Limited_Init_In_05
18617 (Typ, Expression (Original_Node (Exp)));
18618
18619 when N_Indexed_Component |
18620 N_Selected_Component |
18621 N_Explicit_Dereference =>
18622 return Nkind (Exp) = N_Function_Call;
18623
18624 -- A use of 'Input is a function call, hence allowed. Normally the
18625 -- attribute will be changed to a call, but the attribute by itself
18626 -- can occur with -gnatc.
18627
18628 when N_Attribute_Reference =>
18629 return Attribute_Name (Original_Node (Exp)) = Name_Input;
18630
18631 -- For a case expression, all dependent expressions must be legal
18632
18633 when N_Case_Expression =>
18634 declare
18635 Alt : Node_Id;
18636
18637 begin
18638 Alt := First (Alternatives (Original_Node (Exp)));
18639 while Present (Alt) loop
18640 if not OK_For_Limited_Init_In_05 (Typ, Expression (Alt)) then
18641 return False;
18642 end if;
18643
18644 Next (Alt);
18645 end loop;
18646
18647 return True;
18648 end;
18649
18650 -- For an if expression, all dependent expressions must be legal
18651
18652 when N_If_Expression =>
18653 declare
18654 Then_Expr : constant Node_Id :=
18655 Next (First (Expressions (Original_Node (Exp))));
18656 Else_Expr : constant Node_Id := Next (Then_Expr);
18657 begin
18658 return OK_For_Limited_Init_In_05 (Typ, Then_Expr)
18659 and then
18660 OK_For_Limited_Init_In_05 (Typ, Else_Expr);
18661 end;
18662
18663 when others =>
18664 return False;
18665 end case;
18666 end OK_For_Limited_Init_In_05;
18667
18668 -------------------------------------------
18669 -- Ordinary_Fixed_Point_Type_Declaration --
18670 -------------------------------------------
18671
18672 procedure Ordinary_Fixed_Point_Type_Declaration
18673 (T : Entity_Id;
18674 Def : Node_Id)
18675 is
18676 Loc : constant Source_Ptr := Sloc (Def);
18677 Delta_Expr : constant Node_Id := Delta_Expression (Def);
18678 RRS : constant Node_Id := Real_Range_Specification (Def);
18679 Implicit_Base : Entity_Id;
18680 Delta_Val : Ureal;
18681 Small_Val : Ureal;
18682 Low_Val : Ureal;
18683 High_Val : Ureal;
18684
18685 begin
18686 Check_Restriction (No_Fixed_Point, Def);
18687
18688 -- Create implicit base type
18689
18690 Implicit_Base :=
18691 Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
18692 Set_Etype (Implicit_Base, Implicit_Base);
18693
18694 -- Analyze and process delta expression
18695
18696 Analyze_And_Resolve (Delta_Expr, Any_Real);
18697
18698 Check_Delta_Expression (Delta_Expr);
18699 Delta_Val := Expr_Value_R (Delta_Expr);
18700
18701 Set_Delta_Value (Implicit_Base, Delta_Val);
18702
18703 -- Compute default small from given delta, which is the largest power
18704 -- of two that does not exceed the given delta value.
18705
18706 declare
18707 Tmp : Ureal;
18708 Scale : Int;
18709
18710 begin
18711 Tmp := Ureal_1;
18712 Scale := 0;
18713
18714 if Delta_Val < Ureal_1 then
18715 while Delta_Val < Tmp loop
18716 Tmp := Tmp / Ureal_2;
18717 Scale := Scale + 1;
18718 end loop;
18719
18720 else
18721 loop
18722 Tmp := Tmp * Ureal_2;
18723 exit when Tmp > Delta_Val;
18724 Scale := Scale - 1;
18725 end loop;
18726 end if;
18727
18728 Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
18729 end;
18730
18731 Set_Small_Value (Implicit_Base, Small_Val);
18732
18733 -- If no range was given, set a dummy range
18734
18735 if RRS <= Empty_Or_Error then
18736 Low_Val := -Small_Val;
18737 High_Val := Small_Val;
18738
18739 -- Otherwise analyze and process given range
18740
18741 else
18742 declare
18743 Low : constant Node_Id := Low_Bound (RRS);
18744 High : constant Node_Id := High_Bound (RRS);
18745
18746 begin
18747 Analyze_And_Resolve (Low, Any_Real);
18748 Analyze_And_Resolve (High, Any_Real);
18749 Check_Real_Bound (Low);
18750 Check_Real_Bound (High);
18751
18752 -- Obtain and set the range
18753
18754 Low_Val := Expr_Value_R (Low);
18755 High_Val := Expr_Value_R (High);
18756
18757 if Low_Val > High_Val then
18758 Error_Msg_NE ("??fixed point type& has null range", Def, T);
18759 end if;
18760 end;
18761 end if;
18762
18763 -- The range for both the implicit base and the declared first subtype
18764 -- cannot be set yet, so we use the special routine Set_Fixed_Range to
18765 -- set a temporary range in place. Note that the bounds of the base
18766 -- type will be widened to be symmetrical and to fill the available
18767 -- bits when the type is frozen.
18768
18769 -- We could do this with all discrete types, and probably should, but
18770 -- we absolutely have to do it for fixed-point, since the end-points
18771 -- of the range and the size are determined by the small value, which
18772 -- could be reset before the freeze point.
18773
18774 Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
18775 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
18776
18777 -- Complete definition of first subtype. The inheritance of the rep item
18778 -- chain ensures that SPARK-related pragmas are not clobbered when the
18779 -- ordinary fixed point type acts as a full view of a private type.
18780
18781 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
18782 Set_Etype (T, Implicit_Base);
18783 Init_Size_Align (T);
18784 Inherit_Rep_Item_Chain (T, Implicit_Base);
18785 Set_Small_Value (T, Small_Val);
18786 Set_Delta_Value (T, Delta_Val);
18787 Set_Is_Constrained (T);
18788 end Ordinary_Fixed_Point_Type_Declaration;
18789
18790 ----------------------------------
18791 -- Preanalyze_Assert_Expression --
18792 ----------------------------------
18793
18794 procedure Preanalyze_Assert_Expression (N : Node_Id; T : Entity_Id) is
18795 begin
18796 In_Assertion_Expr := In_Assertion_Expr + 1;
18797 Preanalyze_Spec_Expression (N, T);
18798 In_Assertion_Expr := In_Assertion_Expr - 1;
18799 end Preanalyze_Assert_Expression;
18800
18801 -----------------------------------
18802 -- Preanalyze_Default_Expression --
18803 -----------------------------------
18804
18805 procedure Preanalyze_Default_Expression (N : Node_Id; T : Entity_Id) is
18806 Save_In_Default_Expr : constant Boolean := In_Default_Expr;
18807 begin
18808 In_Default_Expr := True;
18809 Preanalyze_Spec_Expression (N, T);
18810 In_Default_Expr := Save_In_Default_Expr;
18811 end Preanalyze_Default_Expression;
18812
18813 --------------------------------
18814 -- Preanalyze_Spec_Expression --
18815 --------------------------------
18816
18817 procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
18818 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
18819 begin
18820 In_Spec_Expression := True;
18821 Preanalyze_And_Resolve (N, T);
18822 In_Spec_Expression := Save_In_Spec_Expression;
18823 end Preanalyze_Spec_Expression;
18824
18825 ----------------------------------------
18826 -- Prepare_Private_Subtype_Completion --
18827 ----------------------------------------
18828
18829 procedure Prepare_Private_Subtype_Completion
18830 (Id : Entity_Id;
18831 Related_Nod : Node_Id)
18832 is
18833 Id_B : constant Entity_Id := Base_Type (Id);
18834 Full_B : Entity_Id := Full_View (Id_B);
18835 Full : Entity_Id;
18836
18837 begin
18838 if Present (Full_B) then
18839
18840 -- Get to the underlying full view if necessary
18841
18842 if Is_Private_Type (Full_B)
18843 and then Present (Underlying_Full_View (Full_B))
18844 then
18845 Full_B := Underlying_Full_View (Full_B);
18846 end if;
18847
18848 -- The Base_Type is already completed, we can complete the subtype
18849 -- now. We have to create a new entity with the same name, Thus we
18850 -- can't use Create_Itype.
18851
18852 Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
18853 Set_Is_Itype (Full);
18854 Set_Associated_Node_For_Itype (Full, Related_Nod);
18855 Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
18856 end if;
18857
18858 -- The parent subtype may be private, but the base might not, in some
18859 -- nested instances. In that case, the subtype does not need to be
18860 -- exchanged. It would still be nice to make private subtypes and their
18861 -- bases consistent at all times ???
18862
18863 if Is_Private_Type (Id_B) then
18864 Append_Elmt (Id, Private_Dependents (Id_B));
18865 end if;
18866 end Prepare_Private_Subtype_Completion;
18867
18868 ---------------------------
18869 -- Process_Discriminants --
18870 ---------------------------
18871
18872 procedure Process_Discriminants
18873 (N : Node_Id;
18874 Prev : Entity_Id := Empty)
18875 is
18876 Elist : constant Elist_Id := New_Elmt_List;
18877 Id : Node_Id;
18878 Discr : Node_Id;
18879 Discr_Number : Uint;
18880 Discr_Type : Entity_Id;
18881 Default_Present : Boolean := False;
18882 Default_Not_Present : Boolean := False;
18883
18884 begin
18885 -- A composite type other than an array type can have discriminants.
18886 -- On entry, the current scope is the composite type.
18887
18888 -- The discriminants are initially entered into the scope of the type
18889 -- via Enter_Name with the default Ekind of E_Void to prevent premature
18890 -- use, as explained at the end of this procedure.
18891
18892 Discr := First (Discriminant_Specifications (N));
18893 while Present (Discr) loop
18894 Enter_Name (Defining_Identifier (Discr));
18895
18896 -- For navigation purposes we add a reference to the discriminant
18897 -- in the entity for the type. If the current declaration is a
18898 -- completion, place references on the partial view. Otherwise the
18899 -- type is the current scope.
18900
18901 if Present (Prev) then
18902
18903 -- The references go on the partial view, if present. If the
18904 -- partial view has discriminants, the references have been
18905 -- generated already.
18906
18907 if not Has_Discriminants (Prev) then
18908 Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
18909 end if;
18910 else
18911 Generate_Reference
18912 (Current_Scope, Defining_Identifier (Discr), 'd');
18913 end if;
18914
18915 if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
18916 Discr_Type := Access_Definition (Discr, Discriminant_Type (Discr));
18917
18918 -- Ada 2005 (AI-254)
18919
18920 if Present (Access_To_Subprogram_Definition
18921 (Discriminant_Type (Discr)))
18922 and then Protected_Present (Access_To_Subprogram_Definition
18923 (Discriminant_Type (Discr)))
18924 then
18925 Discr_Type :=
18926 Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
18927 end if;
18928
18929 else
18930 Find_Type (Discriminant_Type (Discr));
18931 Discr_Type := Etype (Discriminant_Type (Discr));
18932
18933 if Error_Posted (Discriminant_Type (Discr)) then
18934 Discr_Type := Any_Type;
18935 end if;
18936 end if;
18937
18938 -- Handling of discriminants that are access types
18939
18940 if Is_Access_Type (Discr_Type) then
18941
18942 -- Ada 2005 (AI-230): Access discriminant allowed in non-
18943 -- limited record types
18944
18945 if Ada_Version < Ada_2005 then
18946 Check_Access_Discriminant_Requires_Limited
18947 (Discr, Discriminant_Type (Discr));
18948 end if;
18949
18950 if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
18951 Error_Msg_N
18952 ("(Ada 83) access discriminant not allowed", Discr);
18953 end if;
18954
18955 -- If not access type, must be a discrete type
18956
18957 elsif not Is_Discrete_Type (Discr_Type) then
18958 Error_Msg_N
18959 ("discriminants must have a discrete or access type",
18960 Discriminant_Type (Discr));
18961 end if;
18962
18963 Set_Etype (Defining_Identifier (Discr), Discr_Type);
18964
18965 -- If a discriminant specification includes the assignment compound
18966 -- delimiter followed by an expression, the expression is the default
18967 -- expression of the discriminant; the default expression must be of
18968 -- the type of the discriminant. (RM 3.7.1) Since this expression is
18969 -- a default expression, we do the special preanalysis, since this
18970 -- expression does not freeze (see section "Handling of Default and
18971 -- Per-Object Expressions" in spec of package Sem).
18972
18973 if Present (Expression (Discr)) then
18974 Preanalyze_Spec_Expression (Expression (Discr), Discr_Type);
18975
18976 -- Legaity checks
18977
18978 if Nkind (N) = N_Formal_Type_Declaration then
18979 Error_Msg_N
18980 ("discriminant defaults not allowed for formal type",
18981 Expression (Discr));
18982
18983 -- Flag an error for a tagged type with defaulted discriminants,
18984 -- excluding limited tagged types when compiling for Ada 2012
18985 -- (see AI05-0214).
18986
18987 elsif Is_Tagged_Type (Current_Scope)
18988 and then (not Is_Limited_Type (Current_Scope)
18989 or else Ada_Version < Ada_2012)
18990 and then Comes_From_Source (N)
18991 then
18992 -- Note: see similar test in Check_Or_Process_Discriminants, to
18993 -- handle the (illegal) case of the completion of an untagged
18994 -- view with discriminants with defaults by a tagged full view.
18995 -- We skip the check if Discr does not come from source, to
18996 -- account for the case of an untagged derived type providing
18997 -- defaults for a renamed discriminant from a private untagged
18998 -- ancestor with a tagged full view (ACATS B460006).
18999
19000 if Ada_Version >= Ada_2012 then
19001 Error_Msg_N
19002 ("discriminants of nonlimited tagged type cannot have"
19003 & " defaults",
19004 Expression (Discr));
19005 else
19006 Error_Msg_N
19007 ("discriminants of tagged type cannot have defaults",
19008 Expression (Discr));
19009 end if;
19010
19011 else
19012 Default_Present := True;
19013 Append_Elmt (Expression (Discr), Elist);
19014
19015 -- Tag the defining identifiers for the discriminants with
19016 -- their corresponding default expressions from the tree.
19017
19018 Set_Discriminant_Default_Value
19019 (Defining_Identifier (Discr), Expression (Discr));
19020 end if;
19021
19022 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag
19023 -- gets set unless we can be sure that no range check is required.
19024
19025 if (GNATprove_Mode or not Expander_Active)
19026 and then not
19027 Is_In_Range
19028 (Expression (Discr), Discr_Type, Assume_Valid => True)
19029 then
19030 Set_Do_Range_Check (Expression (Discr));
19031 end if;
19032
19033 -- No default discriminant value given
19034
19035 else
19036 Default_Not_Present := True;
19037 end if;
19038
19039 -- Ada 2005 (AI-231): Create an Itype that is a duplicate of
19040 -- Discr_Type but with the null-exclusion attribute
19041
19042 if Ada_Version >= Ada_2005 then
19043
19044 -- Ada 2005 (AI-231): Static checks
19045
19046 if Can_Never_Be_Null (Discr_Type) then
19047 Null_Exclusion_Static_Checks (Discr);
19048
19049 elsif Is_Access_Type (Discr_Type)
19050 and then Null_Exclusion_Present (Discr)
19051
19052 -- No need to check itypes because in their case this check
19053 -- was done at their point of creation
19054
19055 and then not Is_Itype (Discr_Type)
19056 then
19057 if Can_Never_Be_Null (Discr_Type) then
19058 Error_Msg_NE
19059 ("`NOT NULL` not allowed (& already excludes null)",
19060 Discr,
19061 Discr_Type);
19062 end if;
19063
19064 Set_Etype (Defining_Identifier (Discr),
19065 Create_Null_Excluding_Itype
19066 (T => Discr_Type,
19067 Related_Nod => Discr));
19068
19069 -- Check for improper null exclusion if the type is otherwise
19070 -- legal for a discriminant.
19071
19072 elsif Null_Exclusion_Present (Discr)
19073 and then Is_Discrete_Type (Discr_Type)
19074 then
19075 Error_Msg_N
19076 ("null exclusion can only apply to an access type", Discr);
19077 end if;
19078
19079 -- Ada 2005 (AI-402): access discriminants of nonlimited types
19080 -- can't have defaults. Synchronized types, or types that are
19081 -- explicitly limited are fine, but special tests apply to derived
19082 -- types in generics: in a generic body we have to assume the
19083 -- worst, and therefore defaults are not allowed if the parent is
19084 -- a generic formal private type (see ACATS B370001).
19085
19086 if Is_Access_Type (Discr_Type) and then Default_Present then
19087 if Ekind (Discr_Type) /= E_Anonymous_Access_Type
19088 or else Is_Limited_Record (Current_Scope)
19089 or else Is_Concurrent_Type (Current_Scope)
19090 or else Is_Concurrent_Record_Type (Current_Scope)
19091 or else Ekind (Current_Scope) = E_Limited_Private_Type
19092 then
19093 if not Is_Derived_Type (Current_Scope)
19094 or else not Is_Generic_Type (Etype (Current_Scope))
19095 or else not In_Package_Body (Scope (Etype (Current_Scope)))
19096 or else Limited_Present
19097 (Type_Definition (Parent (Current_Scope)))
19098 then
19099 null;
19100
19101 else
19102 Error_Msg_N
19103 ("access discriminants of nonlimited types cannot "
19104 & "have defaults", Expression (Discr));
19105 end if;
19106
19107 elsif Present (Expression (Discr)) then
19108 Error_Msg_N
19109 ("(Ada 2005) access discriminants of nonlimited types "
19110 & "cannot have defaults", Expression (Discr));
19111 end if;
19112 end if;
19113 end if;
19114
19115 -- A discriminant cannot be effectively volatile. This check is only
19116 -- relevant when SPARK_Mode is on as it is not standard Ada legality
19117 -- rule (SPARK RM 7.1.3(6)).
19118
19119 if SPARK_Mode = On
19120 and then Is_Effectively_Volatile (Defining_Identifier (Discr))
19121 then
19122 Error_Msg_N ("discriminant cannot be volatile", Discr);
19123 end if;
19124
19125 Next (Discr);
19126 end loop;
19127
19128 -- An element list consisting of the default expressions of the
19129 -- discriminants is constructed in the above loop and used to set
19130 -- the Discriminant_Constraint attribute for the type. If an object
19131 -- is declared of this (record or task) type without any explicit
19132 -- discriminant constraint given, this element list will form the
19133 -- actual parameters for the corresponding initialization procedure
19134 -- for the type.
19135
19136 Set_Discriminant_Constraint (Current_Scope, Elist);
19137 Set_Stored_Constraint (Current_Scope, No_Elist);
19138
19139 -- Default expressions must be provided either for all or for none
19140 -- of the discriminants of a discriminant part. (RM 3.7.1)
19141
19142 if Default_Present and then Default_Not_Present then
19143 Error_Msg_N
19144 ("incomplete specification of defaults for discriminants", N);
19145 end if;
19146
19147 -- The use of the name of a discriminant is not allowed in default
19148 -- expressions of a discriminant part if the specification of the
19149 -- discriminant is itself given in the discriminant part. (RM 3.7.1)
19150
19151 -- To detect this, the discriminant names are entered initially with an
19152 -- Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
19153 -- attempt to use a void entity (for example in an expression that is
19154 -- type-checked) produces the error message: premature usage. Now after
19155 -- completing the semantic analysis of the discriminant part, we can set
19156 -- the Ekind of all the discriminants appropriately.
19157
19158 Discr := First (Discriminant_Specifications (N));
19159 Discr_Number := Uint_1;
19160 while Present (Discr) loop
19161 Id := Defining_Identifier (Discr);
19162 Set_Ekind (Id, E_Discriminant);
19163 Init_Component_Location (Id);
19164 Init_Esize (Id);
19165 Set_Discriminant_Number (Id, Discr_Number);
19166
19167 -- Make sure this is always set, even in illegal programs
19168
19169 Set_Corresponding_Discriminant (Id, Empty);
19170
19171 -- Initialize the Original_Record_Component to the entity itself.
19172 -- Inherit_Components will propagate the right value to
19173 -- discriminants in derived record types.
19174
19175 Set_Original_Record_Component (Id, Id);
19176
19177 -- Create the discriminal for the discriminant
19178
19179 Build_Discriminal (Id);
19180
19181 Next (Discr);
19182 Discr_Number := Discr_Number + 1;
19183 end loop;
19184
19185 Set_Has_Discriminants (Current_Scope);
19186 end Process_Discriminants;
19187
19188 -----------------------
19189 -- Process_Full_View --
19190 -----------------------
19191
19192 procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
19193 procedure Collect_Implemented_Interfaces
19194 (Typ : Entity_Id;
19195 Ifaces : Elist_Id);
19196 -- Ada 2005: Gather all the interfaces that Typ directly or
19197 -- inherently implements. Duplicate entries are not added to
19198 -- the list Ifaces.
19199
19200 ------------------------------------
19201 -- Collect_Implemented_Interfaces --
19202 ------------------------------------
19203
19204 procedure Collect_Implemented_Interfaces
19205 (Typ : Entity_Id;
19206 Ifaces : Elist_Id)
19207 is
19208 Iface : Entity_Id;
19209 Iface_Elmt : Elmt_Id;
19210
19211 begin
19212 -- Abstract interfaces are only associated with tagged record types
19213
19214 if not Is_Tagged_Type (Typ) or else not Is_Record_Type (Typ) then
19215 return;
19216 end if;
19217
19218 -- Recursively climb to the ancestors
19219
19220 if Etype (Typ) /= Typ
19221
19222 -- Protect the frontend against wrong cyclic declarations like:
19223
19224 -- type B is new A with private;
19225 -- type C is new A with private;
19226 -- private
19227 -- type B is new C with null record;
19228 -- type C is new B with null record;
19229
19230 and then Etype (Typ) /= Priv_T
19231 and then Etype (Typ) /= Full_T
19232 then
19233 -- Keep separate the management of private type declarations
19234
19235 if Ekind (Typ) = E_Record_Type_With_Private then
19236
19237 -- Handle the following illegal usage:
19238 -- type Private_Type is tagged private;
19239 -- private
19240 -- type Private_Type is new Type_Implementing_Iface;
19241
19242 if Present (Full_View (Typ))
19243 and then Etype (Typ) /= Full_View (Typ)
19244 then
19245 if Is_Interface (Etype (Typ)) then
19246 Append_Unique_Elmt (Etype (Typ), Ifaces);
19247 end if;
19248
19249 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
19250 end if;
19251
19252 -- Non-private types
19253
19254 else
19255 if Is_Interface (Etype (Typ)) then
19256 Append_Unique_Elmt (Etype (Typ), Ifaces);
19257 end if;
19258
19259 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
19260 end if;
19261 end if;
19262
19263 -- Handle entities in the list of abstract interfaces
19264
19265 if Present (Interfaces (Typ)) then
19266 Iface_Elmt := First_Elmt (Interfaces (Typ));
19267 while Present (Iface_Elmt) loop
19268 Iface := Node (Iface_Elmt);
19269
19270 pragma Assert (Is_Interface (Iface));
19271
19272 if not Contain_Interface (Iface, Ifaces) then
19273 Append_Elmt (Iface, Ifaces);
19274 Collect_Implemented_Interfaces (Iface, Ifaces);
19275 end if;
19276
19277 Next_Elmt (Iface_Elmt);
19278 end loop;
19279 end if;
19280 end Collect_Implemented_Interfaces;
19281
19282 -- Local variables
19283
19284 Full_Indic : Node_Id;
19285 Full_Parent : Entity_Id;
19286 Priv_Parent : Entity_Id;
19287
19288 -- Start of processing for Process_Full_View
19289
19290 begin
19291 -- First some sanity checks that must be done after semantic
19292 -- decoration of the full view and thus cannot be placed with other
19293 -- similar checks in Find_Type_Name
19294
19295 if not Is_Limited_Type (Priv_T)
19296 and then (Is_Limited_Type (Full_T)
19297 or else Is_Limited_Composite (Full_T))
19298 then
19299 if In_Instance then
19300 null;
19301 else
19302 Error_Msg_N
19303 ("completion of nonlimited type cannot be limited", Full_T);
19304 Explain_Limited_Type (Full_T, Full_T);
19305 end if;
19306
19307 elsif Is_Abstract_Type (Full_T)
19308 and then not Is_Abstract_Type (Priv_T)
19309 then
19310 Error_Msg_N
19311 ("completion of nonabstract type cannot be abstract", Full_T);
19312
19313 elsif Is_Tagged_Type (Priv_T)
19314 and then Is_Limited_Type (Priv_T)
19315 and then not Is_Limited_Type (Full_T)
19316 then
19317 -- If pragma CPP_Class was applied to the private declaration
19318 -- propagate the limitedness to the full-view
19319
19320 if Is_CPP_Class (Priv_T) then
19321 Set_Is_Limited_Record (Full_T);
19322
19323 -- GNAT allow its own definition of Limited_Controlled to disobey
19324 -- this rule in order in ease the implementation. This test is safe
19325 -- because Root_Controlled is defined in a child of System that
19326 -- normal programs are not supposed to use.
19327
19328 elsif Is_RTE (Etype (Full_T), RE_Root_Controlled) then
19329 Set_Is_Limited_Composite (Full_T);
19330 else
19331 Error_Msg_N
19332 ("completion of limited tagged type must be limited", Full_T);
19333 end if;
19334
19335 elsif Is_Generic_Type (Priv_T) then
19336 Error_Msg_N ("generic type cannot have a completion", Full_T);
19337 end if;
19338
19339 -- Check that ancestor interfaces of private and full views are
19340 -- consistent. We omit this check for synchronized types because
19341 -- they are performed on the corresponding record type when frozen.
19342
19343 if Ada_Version >= Ada_2005
19344 and then Is_Tagged_Type (Priv_T)
19345 and then Is_Tagged_Type (Full_T)
19346 and then not Is_Concurrent_Type (Full_T)
19347 then
19348 declare
19349 Iface : Entity_Id;
19350 Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
19351 Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
19352
19353 begin
19354 Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
19355 Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
19356
19357 -- Ada 2005 (AI-251): The partial view shall be a descendant of
19358 -- an interface type if and only if the full type is descendant
19359 -- of the interface type (AARM 7.3 (7.3/2)).
19360
19361 Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
19362
19363 if Present (Iface) then
19364 Error_Msg_NE
19365 ("interface in partial view& not implemented by full type "
19366 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
19367 end if;
19368
19369 Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
19370
19371 if Present (Iface) then
19372 Error_Msg_NE
19373 ("interface & not implemented by partial view "
19374 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
19375 end if;
19376 end;
19377 end if;
19378
19379 if Is_Tagged_Type (Priv_T)
19380 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
19381 and then Is_Derived_Type (Full_T)
19382 then
19383 Priv_Parent := Etype (Priv_T);
19384
19385 -- The full view of a private extension may have been transformed
19386 -- into an unconstrained derived type declaration and a subtype
19387 -- declaration (see build_derived_record_type for details).
19388
19389 if Nkind (N) = N_Subtype_Declaration then
19390 Full_Indic := Subtype_Indication (N);
19391 Full_Parent := Etype (Base_Type (Full_T));
19392 else
19393 Full_Indic := Subtype_Indication (Type_Definition (N));
19394 Full_Parent := Etype (Full_T);
19395 end if;
19396
19397 -- Check that the parent type of the full type is a descendant of
19398 -- the ancestor subtype given in the private extension. If either
19399 -- entity has an Etype equal to Any_Type then we had some previous
19400 -- error situation [7.3(8)].
19401
19402 if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
19403 return;
19404
19405 -- Ada 2005 (AI-251): Interfaces in the full type can be given in
19406 -- any order. Therefore we don't have to check that its parent must
19407 -- be a descendant of the parent of the private type declaration.
19408
19409 elsif Is_Interface (Priv_Parent)
19410 and then Is_Interface (Full_Parent)
19411 then
19412 null;
19413
19414 -- Ada 2005 (AI-251): If the parent of the private type declaration
19415 -- is an interface there is no need to check that it is an ancestor
19416 -- of the associated full type declaration. The required tests for
19417 -- this case are performed by Build_Derived_Record_Type.
19418
19419 elsif not Is_Interface (Base_Type (Priv_Parent))
19420 and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
19421 then
19422 Error_Msg_N
19423 ("parent of full type must descend from parent"
19424 & " of private extension", Full_Indic);
19425
19426 -- First check a formal restriction, and then proceed with checking
19427 -- Ada rules. Since the formal restriction is not a serious error, we
19428 -- don't prevent further error detection for this check, hence the
19429 -- ELSE.
19430
19431 else
19432 -- In formal mode, when completing a private extension the type
19433 -- named in the private part must be exactly the same as that
19434 -- named in the visible part.
19435
19436 if Priv_Parent /= Full_Parent then
19437 Error_Msg_Name_1 := Chars (Priv_Parent);
19438 Check_SPARK_05_Restriction ("% expected", Full_Indic);
19439 end if;
19440
19441 -- Check the rules of 7.3(10): if the private extension inherits
19442 -- known discriminants, then the full type must also inherit those
19443 -- discriminants from the same (ancestor) type, and the parent
19444 -- subtype of the full type must be constrained if and only if
19445 -- the ancestor subtype of the private extension is constrained.
19446
19447 if No (Discriminant_Specifications (Parent (Priv_T)))
19448 and then not Has_Unknown_Discriminants (Priv_T)
19449 and then Has_Discriminants (Base_Type (Priv_Parent))
19450 then
19451 declare
19452 Priv_Indic : constant Node_Id :=
19453 Subtype_Indication (Parent (Priv_T));
19454
19455 Priv_Constr : constant Boolean :=
19456 Is_Constrained (Priv_Parent)
19457 or else
19458 Nkind (Priv_Indic) = N_Subtype_Indication
19459 or else
19460 Is_Constrained (Entity (Priv_Indic));
19461
19462 Full_Constr : constant Boolean :=
19463 Is_Constrained (Full_Parent)
19464 or else
19465 Nkind (Full_Indic) = N_Subtype_Indication
19466 or else
19467 Is_Constrained (Entity (Full_Indic));
19468
19469 Priv_Discr : Entity_Id;
19470 Full_Discr : Entity_Id;
19471
19472 begin
19473 Priv_Discr := First_Discriminant (Priv_Parent);
19474 Full_Discr := First_Discriminant (Full_Parent);
19475 while Present (Priv_Discr) and then Present (Full_Discr) loop
19476 if Original_Record_Component (Priv_Discr) =
19477 Original_Record_Component (Full_Discr)
19478 or else
19479 Corresponding_Discriminant (Priv_Discr) =
19480 Corresponding_Discriminant (Full_Discr)
19481 then
19482 null;
19483 else
19484 exit;
19485 end if;
19486
19487 Next_Discriminant (Priv_Discr);
19488 Next_Discriminant (Full_Discr);
19489 end loop;
19490
19491 if Present (Priv_Discr) or else Present (Full_Discr) then
19492 Error_Msg_N
19493 ("full view must inherit discriminants of the parent"
19494 & " type used in the private extension", Full_Indic);
19495
19496 elsif Priv_Constr and then not Full_Constr then
19497 Error_Msg_N
19498 ("parent subtype of full type must be constrained",
19499 Full_Indic);
19500
19501 elsif Full_Constr and then not Priv_Constr then
19502 Error_Msg_N
19503 ("parent subtype of full type must be unconstrained",
19504 Full_Indic);
19505 end if;
19506 end;
19507
19508 -- Check the rules of 7.3(12): if a partial view has neither
19509 -- known or unknown discriminants, then the full type
19510 -- declaration shall define a definite subtype.
19511
19512 elsif not Has_Unknown_Discriminants (Priv_T)
19513 and then not Has_Discriminants (Priv_T)
19514 and then not Is_Constrained (Full_T)
19515 then
19516 Error_Msg_N
19517 ("full view must define a constrained type if partial view"
19518 & " has no discriminants", Full_T);
19519 end if;
19520
19521 -- ??????? Do we implement the following properly ?????
19522 -- If the ancestor subtype of a private extension has constrained
19523 -- discriminants, then the parent subtype of the full view shall
19524 -- impose a statically matching constraint on those discriminants
19525 -- [7.3(13)].
19526 end if;
19527
19528 else
19529 -- For untagged types, verify that a type without discriminants is
19530 -- not completed with an unconstrained type. A separate error message
19531 -- is produced if the full type has defaulted discriminants.
19532
19533 if not Is_Indefinite_Subtype (Priv_T)
19534 and then Is_Indefinite_Subtype (Full_T)
19535 then
19536 Error_Msg_Sloc := Sloc (Parent (Priv_T));
19537 Error_Msg_NE
19538 ("full view of& not compatible with declaration#",
19539 Full_T, Priv_T);
19540
19541 if not Is_Tagged_Type (Full_T) then
19542 Error_Msg_N
19543 ("\one is constrained, the other unconstrained", Full_T);
19544 end if;
19545 end if;
19546 end if;
19547
19548 -- AI-419: verify that the use of "limited" is consistent
19549
19550 declare
19551 Orig_Decl : constant Node_Id := Original_Node (N);
19552
19553 begin
19554 if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
19555 and then Nkind (Orig_Decl) = N_Full_Type_Declaration
19556 and then Nkind
19557 (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
19558 then
19559 if not Limited_Present (Parent (Priv_T))
19560 and then not Synchronized_Present (Parent (Priv_T))
19561 and then Limited_Present (Type_Definition (Orig_Decl))
19562 then
19563 Error_Msg_N
19564 ("full view of non-limited extension cannot be limited", N);
19565
19566 -- Conversely, if the partial view carries the limited keyword,
19567 -- the full view must as well, even if it may be redundant.
19568
19569 elsif Limited_Present (Parent (Priv_T))
19570 and then not Limited_Present (Type_Definition (Orig_Decl))
19571 then
19572 Error_Msg_N
19573 ("full view of limited extension must be explicitly limited",
19574 N);
19575 end if;
19576 end if;
19577 end;
19578
19579 -- Ada 2005 (AI-443): A synchronized private extension must be
19580 -- completed by a task or protected type.
19581
19582 if Ada_Version >= Ada_2005
19583 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
19584 and then Synchronized_Present (Parent (Priv_T))
19585 and then not Is_Concurrent_Type (Full_T)
19586 then
19587 Error_Msg_N ("full view of synchronized extension must " &
19588 "be synchronized type", N);
19589 end if;
19590
19591 -- Ada 2005 AI-363: if the full view has discriminants with
19592 -- defaults, it is illegal to declare constrained access subtypes
19593 -- whose designated type is the current type. This allows objects
19594 -- of the type that are declared in the heap to be unconstrained.
19595
19596 if not Has_Unknown_Discriminants (Priv_T)
19597 and then not Has_Discriminants (Priv_T)
19598 and then Has_Discriminants (Full_T)
19599 and then
19600 Present (Discriminant_Default_Value (First_Discriminant (Full_T)))
19601 then
19602 Set_Has_Constrained_Partial_View (Full_T);
19603 Set_Has_Constrained_Partial_View (Priv_T);
19604 end if;
19605
19606 -- Create a full declaration for all its subtypes recorded in
19607 -- Private_Dependents and swap them similarly to the base type. These
19608 -- are subtypes that have been define before the full declaration of
19609 -- the private type. We also swap the entry in Private_Dependents list
19610 -- so we can properly restore the private view on exit from the scope.
19611
19612 declare
19613 Priv_Elmt : Elmt_Id;
19614 Priv_Scop : Entity_Id;
19615 Priv : Entity_Id;
19616 Full : Entity_Id;
19617
19618 begin
19619 Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
19620 while Present (Priv_Elmt) loop
19621 Priv := Node (Priv_Elmt);
19622 Priv_Scop := Scope (Priv);
19623
19624 if Ekind_In (Priv, E_Private_Subtype,
19625 E_Limited_Private_Subtype,
19626 E_Record_Subtype_With_Private)
19627 then
19628 Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
19629 Set_Is_Itype (Full);
19630 Set_Parent (Full, Parent (Priv));
19631 Set_Associated_Node_For_Itype (Full, N);
19632
19633 -- Now we need to complete the private subtype, but since the
19634 -- base type has already been swapped, we must also swap the
19635 -- subtypes (and thus, reverse the arguments in the call to
19636 -- Complete_Private_Subtype). Also note that we may need to
19637 -- re-establish the scope of the private subtype.
19638
19639 Copy_And_Swap (Priv, Full);
19640
19641 if not In_Open_Scopes (Priv_Scop) then
19642 Push_Scope (Priv_Scop);
19643
19644 else
19645 -- Reset Priv_Scop to Empty to indicate no scope was pushed
19646
19647 Priv_Scop := Empty;
19648 end if;
19649
19650 Complete_Private_Subtype (Full, Priv, Full_T, N);
19651
19652 if Present (Priv_Scop) then
19653 Pop_Scope;
19654 end if;
19655
19656 Replace_Elmt (Priv_Elmt, Full);
19657 end if;
19658
19659 Next_Elmt (Priv_Elmt);
19660 end loop;
19661 end;
19662
19663 -- If the private view was tagged, copy the new primitive operations
19664 -- from the private view to the full view.
19665
19666 if Is_Tagged_Type (Full_T) then
19667 declare
19668 Disp_Typ : Entity_Id;
19669 Full_List : Elist_Id;
19670 Prim : Entity_Id;
19671 Prim_Elmt : Elmt_Id;
19672 Priv_List : Elist_Id;
19673
19674 function Contains
19675 (E : Entity_Id;
19676 L : Elist_Id) return Boolean;
19677 -- Determine whether list L contains element E
19678
19679 --------------
19680 -- Contains --
19681 --------------
19682
19683 function Contains
19684 (E : Entity_Id;
19685 L : Elist_Id) return Boolean
19686 is
19687 List_Elmt : Elmt_Id;
19688
19689 begin
19690 List_Elmt := First_Elmt (L);
19691 while Present (List_Elmt) loop
19692 if Node (List_Elmt) = E then
19693 return True;
19694 end if;
19695
19696 Next_Elmt (List_Elmt);
19697 end loop;
19698
19699 return False;
19700 end Contains;
19701
19702 -- Start of processing
19703
19704 begin
19705 if Is_Tagged_Type (Priv_T) then
19706 Priv_List := Primitive_Operations (Priv_T);
19707 Prim_Elmt := First_Elmt (Priv_List);
19708
19709 -- In the case of a concurrent type completing a private tagged
19710 -- type, primitives may have been declared in between the two
19711 -- views. These subprograms need to be wrapped the same way
19712 -- entries and protected procedures are handled because they
19713 -- cannot be directly shared by the two views.
19714
19715 if Is_Concurrent_Type (Full_T) then
19716 declare
19717 Conc_Typ : constant Entity_Id :=
19718 Corresponding_Record_Type (Full_T);
19719 Curr_Nod : Node_Id := Parent (Conc_Typ);
19720 Wrap_Spec : Node_Id;
19721
19722 begin
19723 while Present (Prim_Elmt) loop
19724 Prim := Node (Prim_Elmt);
19725
19726 if Comes_From_Source (Prim)
19727 and then not Is_Abstract_Subprogram (Prim)
19728 then
19729 Wrap_Spec :=
19730 Make_Subprogram_Declaration (Sloc (Prim),
19731 Specification =>
19732 Build_Wrapper_Spec
19733 (Subp_Id => Prim,
19734 Obj_Typ => Conc_Typ,
19735 Formals =>
19736 Parameter_Specifications (
19737 Parent (Prim))));
19738
19739 Insert_After (Curr_Nod, Wrap_Spec);
19740 Curr_Nod := Wrap_Spec;
19741
19742 Analyze (Wrap_Spec);
19743 end if;
19744
19745 Next_Elmt (Prim_Elmt);
19746 end loop;
19747
19748 return;
19749 end;
19750
19751 -- For non-concurrent types, transfer explicit primitives, but
19752 -- omit those inherited from the parent of the private view
19753 -- since they will be re-inherited later on.
19754
19755 else
19756 Full_List := Primitive_Operations (Full_T);
19757
19758 while Present (Prim_Elmt) loop
19759 Prim := Node (Prim_Elmt);
19760
19761 if Comes_From_Source (Prim)
19762 and then not Contains (Prim, Full_List)
19763 then
19764 Append_Elmt (Prim, Full_List);
19765 end if;
19766
19767 Next_Elmt (Prim_Elmt);
19768 end loop;
19769 end if;
19770
19771 -- Untagged private view
19772
19773 else
19774 Full_List := Primitive_Operations (Full_T);
19775
19776 -- In this case the partial view is untagged, so here we locate
19777 -- all of the earlier primitives that need to be treated as
19778 -- dispatching (those that appear between the two views). Note
19779 -- that these additional operations must all be new operations
19780 -- (any earlier operations that override inherited operations
19781 -- of the full view will already have been inserted in the
19782 -- primitives list, marked by Check_Operation_From_Private_View
19783 -- as dispatching. Note that implicit "/=" operators are
19784 -- excluded from being added to the primitives list since they
19785 -- shouldn't be treated as dispatching (tagged "/=" is handled
19786 -- specially).
19787
19788 Prim := Next_Entity (Full_T);
19789 while Present (Prim) and then Prim /= Priv_T loop
19790 if Ekind_In (Prim, E_Procedure, E_Function) then
19791 Disp_Typ := Find_Dispatching_Type (Prim);
19792
19793 if Disp_Typ = Full_T
19794 and then (Chars (Prim) /= Name_Op_Ne
19795 or else Comes_From_Source (Prim))
19796 then
19797 Check_Controlling_Formals (Full_T, Prim);
19798
19799 if not Is_Dispatching_Operation (Prim) then
19800 Append_Elmt (Prim, Full_List);
19801 Set_Is_Dispatching_Operation (Prim, True);
19802 Set_DT_Position_Value (Prim, No_Uint);
19803 end if;
19804
19805 elsif Is_Dispatching_Operation (Prim)
19806 and then Disp_Typ /= Full_T
19807 then
19808
19809 -- Verify that it is not otherwise controlled by a
19810 -- formal or a return value of type T.
19811
19812 Check_Controlling_Formals (Disp_Typ, Prim);
19813 end if;
19814 end if;
19815
19816 Next_Entity (Prim);
19817 end loop;
19818 end if;
19819
19820 -- For the tagged case, the two views can share the same primitive
19821 -- operations list and the same class-wide type. Update attributes
19822 -- of the class-wide type which depend on the full declaration.
19823
19824 if Is_Tagged_Type (Priv_T) then
19825 Set_Direct_Primitive_Operations (Priv_T, Full_List);
19826 Set_Class_Wide_Type
19827 (Base_Type (Full_T), Class_Wide_Type (Priv_T));
19828
19829 Set_Has_Task (Class_Wide_Type (Priv_T), Has_Task (Full_T));
19830 Set_Has_Protected
19831 (Class_Wide_Type (Priv_T), Has_Protected (Full_T));
19832 end if;
19833 end;
19834 end if;
19835
19836 -- Ada 2005 AI 161: Check preelaborable initialization consistency
19837
19838 if Known_To_Have_Preelab_Init (Priv_T) then
19839
19840 -- Case where there is a pragma Preelaborable_Initialization. We
19841 -- always allow this in predefined units, which is cheating a bit,
19842 -- but it means we don't have to struggle to meet the requirements in
19843 -- the RM for having Preelaborable Initialization. Otherwise we
19844 -- require that the type meets the RM rules. But we can't check that
19845 -- yet, because of the rule about overriding Initialize, so we simply
19846 -- set a flag that will be checked at freeze time.
19847
19848 if not In_Predefined_Unit (Full_T) then
19849 Set_Must_Have_Preelab_Init (Full_T);
19850 end if;
19851 end if;
19852
19853 -- If pragma CPP_Class was applied to the private type declaration,
19854 -- propagate it now to the full type declaration.
19855
19856 if Is_CPP_Class (Priv_T) then
19857 Set_Is_CPP_Class (Full_T);
19858 Set_Convention (Full_T, Convention_CPP);
19859
19860 -- Check that components of imported CPP types do not have default
19861 -- expressions.
19862
19863 Check_CPP_Type_Has_No_Defaults (Full_T);
19864 end if;
19865
19866 -- If the private view has user specified stream attributes, then so has
19867 -- the full view.
19868
19869 -- Why the test, how could these flags be already set in Full_T ???
19870
19871 if Has_Specified_Stream_Read (Priv_T) then
19872 Set_Has_Specified_Stream_Read (Full_T);
19873 end if;
19874
19875 if Has_Specified_Stream_Write (Priv_T) then
19876 Set_Has_Specified_Stream_Write (Full_T);
19877 end if;
19878
19879 if Has_Specified_Stream_Input (Priv_T) then
19880 Set_Has_Specified_Stream_Input (Full_T);
19881 end if;
19882
19883 if Has_Specified_Stream_Output (Priv_T) then
19884 Set_Has_Specified_Stream_Output (Full_T);
19885 end if;
19886
19887 -- Propagate the attributes related to pragma Default_Initial_Condition
19888 -- from the private to the full view. Note that both flags are mutually
19889 -- exclusive.
19890
19891 if Has_Default_Init_Cond (Priv_T)
19892 or else Has_Inherited_Default_Init_Cond (Priv_T)
19893 then
19894 Propagate_Default_Init_Cond_Attributes
19895 (From_Typ => Priv_T,
19896 To_Typ => Full_T,
19897 Private_To_Full_View => True);
19898
19899 -- In the case where the full view is derived from another private type,
19900 -- the attributes related to pragma Default_Initial_Condition must be
19901 -- propagated from the full to the private view to maintain consistency
19902 -- of views.
19903
19904 -- package Pack is
19905 -- type Parent_Typ is private
19906 -- with Default_Initial_Condition ...;
19907 -- private
19908 -- type Parent_Typ is ...;
19909 -- end Pack;
19910
19911 -- with Pack; use Pack;
19912 -- package Pack_2 is
19913 -- type Deriv_Typ is private; -- must inherit
19914 -- private
19915 -- type Deriv_Typ is new Parent_Typ; -- must inherit
19916 -- end Pack_2;
19917
19918 elsif Has_Default_Init_Cond (Full_T)
19919 or else Has_Inherited_Default_Init_Cond (Full_T)
19920 then
19921 Propagate_Default_Init_Cond_Attributes
19922 (From_Typ => Full_T,
19923 To_Typ => Priv_T,
19924 Private_To_Full_View => True);
19925 end if;
19926
19927 -- Propagate the attributes related to pragma Ghost from the private to
19928 -- the full view.
19929
19930 if Is_Ghost_Entity (Priv_T) then
19931 Set_Is_Ghost_Entity (Full_T);
19932
19933 -- The Ghost policy in effect at the point of declaration and at the
19934 -- point of completion must match (SPARK RM 6.9(15)).
19935
19936 Check_Ghost_Completion (Priv_T, Full_T);
19937
19938 -- In the case where the private view of a tagged type lacks a parent
19939 -- type and is subject to pragma Ghost, ensure that the parent type
19940 -- specified by the full view is also Ghost (SPARK RM 6.9(9)).
19941
19942 if Is_Derived_Type (Full_T) then
19943 Check_Ghost_Derivation (Full_T);
19944 end if;
19945 end if;
19946
19947 -- Propagate invariants to full type
19948
19949 if Has_Invariants (Priv_T) then
19950 Set_Has_Invariants (Full_T);
19951 Set_Invariant_Procedure (Full_T, Invariant_Procedure (Priv_T));
19952 end if;
19953
19954 if Has_Inheritable_Invariants (Priv_T) then
19955 Set_Has_Inheritable_Invariants (Full_T);
19956 end if;
19957
19958 -- Check hidden inheritance of class-wide type invariants
19959
19960 if Ada_Version >= Ada_2012
19961 and then not Has_Inheritable_Invariants (Full_T)
19962 and then In_Private_Part (Current_Scope)
19963 and then Has_Interfaces (Full_T)
19964 then
19965 declare
19966 Ifaces : Elist_Id;
19967 AI : Elmt_Id;
19968
19969 begin
19970 Collect_Interfaces (Full_T, Ifaces, Exclude_Parents => True);
19971
19972 AI := First_Elmt (Ifaces);
19973 while Present (AI) loop
19974 if Has_Inheritable_Invariants (Node (AI)) then
19975 Error_Msg_N
19976 ("hidden inheritance of class-wide type invariants " &
19977 "not allowed", N);
19978 exit;
19979 end if;
19980
19981 Next_Elmt (AI);
19982 end loop;
19983 end;
19984 end if;
19985
19986 -- Propagate predicates to full type, and predicate function if already
19987 -- defined. It is not clear that this can actually happen? the partial
19988 -- view cannot be frozen yet, and the predicate function has not been
19989 -- built. Still it is a cheap check and seems safer to make it.
19990
19991 if Has_Predicates (Priv_T) then
19992 if Present (Predicate_Function (Priv_T)) then
19993 Set_Predicate_Function (Full_T, Predicate_Function (Priv_T));
19994 end if;
19995
19996 Set_Has_Predicates (Full_T);
19997 end if;
19998 end Process_Full_View;
19999
20000 -----------------------------------
20001 -- Process_Incomplete_Dependents --
20002 -----------------------------------
20003
20004 procedure Process_Incomplete_Dependents
20005 (N : Node_Id;
20006 Full_T : Entity_Id;
20007 Inc_T : Entity_Id)
20008 is
20009 Inc_Elmt : Elmt_Id;
20010 Priv_Dep : Entity_Id;
20011 New_Subt : Entity_Id;
20012
20013 Disc_Constraint : Elist_Id;
20014
20015 begin
20016 if No (Private_Dependents (Inc_T)) then
20017 return;
20018 end if;
20019
20020 -- Itypes that may be generated by the completion of an incomplete
20021 -- subtype are not used by the back-end and not attached to the tree.
20022 -- They are created only for constraint-checking purposes.
20023
20024 Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
20025 while Present (Inc_Elmt) loop
20026 Priv_Dep := Node (Inc_Elmt);
20027
20028 if Ekind (Priv_Dep) = E_Subprogram_Type then
20029
20030 -- An Access_To_Subprogram type may have a return type or a
20031 -- parameter type that is incomplete. Replace with the full view.
20032
20033 if Etype (Priv_Dep) = Inc_T then
20034 Set_Etype (Priv_Dep, Full_T);
20035 end if;
20036
20037 declare
20038 Formal : Entity_Id;
20039
20040 begin
20041 Formal := First_Formal (Priv_Dep);
20042 while Present (Formal) loop
20043 if Etype (Formal) = Inc_T then
20044 Set_Etype (Formal, Full_T);
20045 end if;
20046
20047 Next_Formal (Formal);
20048 end loop;
20049 end;
20050
20051 elsif Is_Overloadable (Priv_Dep) then
20052
20053 -- If a subprogram in the incomplete dependents list is primitive
20054 -- for a tagged full type then mark it as a dispatching operation,
20055 -- check whether it overrides an inherited subprogram, and check
20056 -- restrictions on its controlling formals. Note that a protected
20057 -- operation is never dispatching: only its wrapper operation
20058 -- (which has convention Ada) is.
20059
20060 if Is_Tagged_Type (Full_T)
20061 and then Is_Primitive (Priv_Dep)
20062 and then Convention (Priv_Dep) /= Convention_Protected
20063 then
20064 Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
20065 Set_Is_Dispatching_Operation (Priv_Dep);
20066 Check_Controlling_Formals (Full_T, Priv_Dep);
20067 end if;
20068
20069 elsif Ekind (Priv_Dep) = E_Subprogram_Body then
20070
20071 -- Can happen during processing of a body before the completion
20072 -- of a TA type. Ignore, because spec is also on dependent list.
20073
20074 return;
20075
20076 -- Ada 2005 (AI-412): Transform a regular incomplete subtype into a
20077 -- corresponding subtype of the full view.
20078
20079 elsif Ekind (Priv_Dep) = E_Incomplete_Subtype then
20080 Set_Subtype_Indication
20081 (Parent (Priv_Dep), New_Occurrence_Of (Full_T, Sloc (Priv_Dep)));
20082 Set_Etype (Priv_Dep, Full_T);
20083 Set_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
20084 Set_Analyzed (Parent (Priv_Dep), False);
20085
20086 -- Reanalyze the declaration, suppressing the call to
20087 -- Enter_Name to avoid duplicate names.
20088
20089 Analyze_Subtype_Declaration
20090 (N => Parent (Priv_Dep),
20091 Skip => True);
20092
20093 -- Dependent is a subtype
20094
20095 else
20096 -- We build a new subtype indication using the full view of the
20097 -- incomplete parent. The discriminant constraints have been
20098 -- elaborated already at the point of the subtype declaration.
20099
20100 New_Subt := Create_Itype (E_Void, N);
20101
20102 if Has_Discriminants (Full_T) then
20103 Disc_Constraint := Discriminant_Constraint (Priv_Dep);
20104 else
20105 Disc_Constraint := No_Elist;
20106 end if;
20107
20108 Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
20109 Set_Full_View (Priv_Dep, New_Subt);
20110 end if;
20111
20112 Next_Elmt (Inc_Elmt);
20113 end loop;
20114 end Process_Incomplete_Dependents;
20115
20116 --------------------------------
20117 -- Process_Range_Expr_In_Decl --
20118 --------------------------------
20119
20120 procedure Process_Range_Expr_In_Decl
20121 (R : Node_Id;
20122 T : Entity_Id;
20123 Subtyp : Entity_Id := Empty;
20124 Check_List : List_Id := Empty_List;
20125 R_Check_Off : Boolean := False;
20126 In_Iter_Schm : Boolean := False)
20127 is
20128 Lo, Hi : Node_Id;
20129 R_Checks : Check_Result;
20130 Insert_Node : Node_Id;
20131 Def_Id : Entity_Id;
20132
20133 begin
20134 Analyze_And_Resolve (R, Base_Type (T));
20135
20136 if Nkind (R) = N_Range then
20137
20138 -- In SPARK, all ranges should be static, with the exception of the
20139 -- discrete type definition of a loop parameter specification.
20140
20141 if not In_Iter_Schm
20142 and then not Is_OK_Static_Range (R)
20143 then
20144 Check_SPARK_05_Restriction ("range should be static", R);
20145 end if;
20146
20147 Lo := Low_Bound (R);
20148 Hi := High_Bound (R);
20149
20150 -- Validity checks on the range of a quantified expression are
20151 -- delayed until the construct is transformed into a loop.
20152
20153 if Nkind (Parent (R)) = N_Loop_Parameter_Specification
20154 and then Nkind (Parent (Parent (R))) = N_Quantified_Expression
20155 then
20156 null;
20157
20158 -- We need to ensure validity of the bounds here, because if we
20159 -- go ahead and do the expansion, then the expanded code will get
20160 -- analyzed with range checks suppressed and we miss the check.
20161
20162 -- WARNING: The capture of the range bounds with xxx_FIRST/_LAST and
20163 -- the temporaries generated by routine Remove_Side_Effects by means
20164 -- of validity checks must use the same names. When a range appears
20165 -- in the parent of a generic, the range is processed with checks
20166 -- disabled as part of the generic context and with checks enabled
20167 -- for code generation purposes. This leads to link issues as the
20168 -- generic contains references to xxx_FIRST/_LAST, but the inlined
20169 -- template sees the temporaries generated by Remove_Side_Effects.
20170
20171 else
20172 Validity_Check_Range (R, Subtyp);
20173 end if;
20174
20175 -- If there were errors in the declaration, try and patch up some
20176 -- common mistakes in the bounds. The cases handled are literals
20177 -- which are Integer where the expected type is Real and vice versa.
20178 -- These corrections allow the compilation process to proceed further
20179 -- along since some basic assumptions of the format of the bounds
20180 -- are guaranteed.
20181
20182 if Etype (R) = Any_Type then
20183 if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
20184 Rewrite (Lo,
20185 Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
20186
20187 elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
20188 Rewrite (Hi,
20189 Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
20190
20191 elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
20192 Rewrite (Lo,
20193 Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
20194
20195 elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
20196 Rewrite (Hi,
20197 Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
20198 end if;
20199
20200 Set_Etype (Lo, T);
20201 Set_Etype (Hi, T);
20202 end if;
20203
20204 -- If the bounds of the range have been mistakenly given as string
20205 -- literals (perhaps in place of character literals), then an error
20206 -- has already been reported, but we rewrite the string literal as a
20207 -- bound of the range's type to avoid blowups in later processing
20208 -- that looks at static values.
20209
20210 if Nkind (Lo) = N_String_Literal then
20211 Rewrite (Lo,
20212 Make_Attribute_Reference (Sloc (Lo),
20213 Prefix => New_Occurrence_Of (T, Sloc (Lo)),
20214 Attribute_Name => Name_First));
20215 Analyze_And_Resolve (Lo);
20216 end if;
20217
20218 if Nkind (Hi) = N_String_Literal then
20219 Rewrite (Hi,
20220 Make_Attribute_Reference (Sloc (Hi),
20221 Prefix => New_Occurrence_Of (T, Sloc (Hi)),
20222 Attribute_Name => Name_First));
20223 Analyze_And_Resolve (Hi);
20224 end if;
20225
20226 -- If bounds aren't scalar at this point then exit, avoiding
20227 -- problems with further processing of the range in this procedure.
20228
20229 if not Is_Scalar_Type (Etype (Lo)) then
20230 return;
20231 end if;
20232
20233 -- Resolve (actually Sem_Eval) has checked that the bounds are in
20234 -- then range of the base type. Here we check whether the bounds
20235 -- are in the range of the subtype itself. Note that if the bounds
20236 -- represent the null range the Constraint_Error exception should
20237 -- not be raised.
20238
20239 -- ??? The following code should be cleaned up as follows
20240
20241 -- 1. The Is_Null_Range (Lo, Hi) test should disappear since it
20242 -- is done in the call to Range_Check (R, T); below
20243
20244 -- 2. The use of R_Check_Off should be investigated and possibly
20245 -- removed, this would clean up things a bit.
20246
20247 if Is_Null_Range (Lo, Hi) then
20248 null;
20249
20250 else
20251 -- Capture values of bounds and generate temporaries for them
20252 -- if needed, before applying checks, since checks may cause
20253 -- duplication of the expression without forcing evaluation.
20254
20255 -- The forced evaluation removes side effects from expressions,
20256 -- which should occur also in GNATprove mode. Otherwise, we end up
20257 -- with unexpected insertions of actions at places where this is
20258 -- not supposed to occur, e.g. on default parameters of a call.
20259
20260 if Expander_Active or GNATprove_Mode then
20261
20262 -- Call Force_Evaluation to create declarations as needed to
20263 -- deal with side effects, and also create typ_FIRST/LAST
20264 -- entities for bounds if we have a subtype name.
20265
20266 -- Note: we do this transformation even if expansion is not
20267 -- active if we are in GNATprove_Mode since the transformation
20268 -- is in general required to ensure that the resulting tree has
20269 -- proper Ada semantics.
20270
20271 Force_Evaluation
20272 (Lo, Related_Id => Subtyp, Is_Low_Bound => True);
20273 Force_Evaluation
20274 (Hi, Related_Id => Subtyp, Is_High_Bound => True);
20275 end if;
20276
20277 -- We use a flag here instead of suppressing checks on the type
20278 -- because the type we check against isn't necessarily the place
20279 -- where we put the check.
20280
20281 if not R_Check_Off then
20282 R_Checks := Get_Range_Checks (R, T);
20283
20284 -- Look up tree to find an appropriate insertion point. We
20285 -- can't just use insert_actions because later processing
20286 -- depends on the insertion node. Prior to Ada 2012 the
20287 -- insertion point could only be a declaration or a loop, but
20288 -- quantified expressions can appear within any context in an
20289 -- expression, and the insertion point can be any statement,
20290 -- pragma, or declaration.
20291
20292 Insert_Node := Parent (R);
20293 while Present (Insert_Node) loop
20294 exit when
20295 Nkind (Insert_Node) in N_Declaration
20296 and then
20297 not Nkind_In
20298 (Insert_Node, N_Component_Declaration,
20299 N_Loop_Parameter_Specification,
20300 N_Function_Specification,
20301 N_Procedure_Specification);
20302
20303 exit when Nkind (Insert_Node) in N_Later_Decl_Item
20304 or else Nkind (Insert_Node) in
20305 N_Statement_Other_Than_Procedure_Call
20306 or else Nkind_In (Insert_Node, N_Procedure_Call_Statement,
20307 N_Pragma);
20308
20309 Insert_Node := Parent (Insert_Node);
20310 end loop;
20311
20312 -- Why would Type_Decl not be present??? Without this test,
20313 -- short regression tests fail.
20314
20315 if Present (Insert_Node) then
20316
20317 -- Case of loop statement. Verify that the range is part
20318 -- of the subtype indication of the iteration scheme.
20319
20320 if Nkind (Insert_Node) = N_Loop_Statement then
20321 declare
20322 Indic : Node_Id;
20323
20324 begin
20325 Indic := Parent (R);
20326 while Present (Indic)
20327 and then Nkind (Indic) /= N_Subtype_Indication
20328 loop
20329 Indic := Parent (Indic);
20330 end loop;
20331
20332 if Present (Indic) then
20333 Def_Id := Etype (Subtype_Mark (Indic));
20334
20335 Insert_Range_Checks
20336 (R_Checks,
20337 Insert_Node,
20338 Def_Id,
20339 Sloc (Insert_Node),
20340 R,
20341 Do_Before => True);
20342 end if;
20343 end;
20344
20345 -- Insertion before a declaration. If the declaration
20346 -- includes discriminants, the list of applicable checks
20347 -- is given by the caller.
20348
20349 elsif Nkind (Insert_Node) in N_Declaration then
20350 Def_Id := Defining_Identifier (Insert_Node);
20351
20352 if (Ekind (Def_Id) = E_Record_Type
20353 and then Depends_On_Discriminant (R))
20354 or else
20355 (Ekind (Def_Id) = E_Protected_Type
20356 and then Has_Discriminants (Def_Id))
20357 then
20358 Append_Range_Checks
20359 (R_Checks,
20360 Check_List, Def_Id, Sloc (Insert_Node), R);
20361
20362 else
20363 Insert_Range_Checks
20364 (R_Checks,
20365 Insert_Node, Def_Id, Sloc (Insert_Node), R);
20366
20367 end if;
20368
20369 -- Insertion before a statement. Range appears in the
20370 -- context of a quantified expression. Insertion will
20371 -- take place when expression is expanded.
20372
20373 else
20374 null;
20375 end if;
20376 end if;
20377 end if;
20378 end if;
20379
20380 -- Case of other than an explicit N_Range node
20381
20382 -- The forced evaluation removes side effects from expressions, which
20383 -- should occur also in GNATprove mode. Otherwise, we end up with
20384 -- unexpected insertions of actions at places where this is not
20385 -- supposed to occur, e.g. on default parameters of a call.
20386
20387 elsif Expander_Active or GNATprove_Mode then
20388 Get_Index_Bounds (R, Lo, Hi);
20389 Force_Evaluation (Lo);
20390 Force_Evaluation (Hi);
20391 end if;
20392 end Process_Range_Expr_In_Decl;
20393
20394 --------------------------------------
20395 -- Process_Real_Range_Specification --
20396 --------------------------------------
20397
20398 procedure Process_Real_Range_Specification (Def : Node_Id) is
20399 Spec : constant Node_Id := Real_Range_Specification (Def);
20400 Lo : Node_Id;
20401 Hi : Node_Id;
20402 Err : Boolean := False;
20403
20404 procedure Analyze_Bound (N : Node_Id);
20405 -- Analyze and check one bound
20406
20407 -------------------
20408 -- Analyze_Bound --
20409 -------------------
20410
20411 procedure Analyze_Bound (N : Node_Id) is
20412 begin
20413 Analyze_And_Resolve (N, Any_Real);
20414
20415 if not Is_OK_Static_Expression (N) then
20416 Flag_Non_Static_Expr
20417 ("bound in real type definition is not static!", N);
20418 Err := True;
20419 end if;
20420 end Analyze_Bound;
20421
20422 -- Start of processing for Process_Real_Range_Specification
20423
20424 begin
20425 if Present (Spec) then
20426 Lo := Low_Bound (Spec);
20427 Hi := High_Bound (Spec);
20428 Analyze_Bound (Lo);
20429 Analyze_Bound (Hi);
20430
20431 -- If error, clear away junk range specification
20432
20433 if Err then
20434 Set_Real_Range_Specification (Def, Empty);
20435 end if;
20436 end if;
20437 end Process_Real_Range_Specification;
20438
20439 ---------------------
20440 -- Process_Subtype --
20441 ---------------------
20442
20443 function Process_Subtype
20444 (S : Node_Id;
20445 Related_Nod : Node_Id;
20446 Related_Id : Entity_Id := Empty;
20447 Suffix : Character := ' ') return Entity_Id
20448 is
20449 P : Node_Id;
20450 Def_Id : Entity_Id;
20451 Error_Node : Node_Id;
20452 Full_View_Id : Entity_Id;
20453 Subtype_Mark_Id : Entity_Id;
20454
20455 May_Have_Null_Exclusion : Boolean;
20456
20457 procedure Check_Incomplete (T : Entity_Id);
20458 -- Called to verify that an incomplete type is not used prematurely
20459
20460 ----------------------
20461 -- Check_Incomplete --
20462 ----------------------
20463
20464 procedure Check_Incomplete (T : Entity_Id) is
20465 begin
20466 -- Ada 2005 (AI-412): Incomplete subtypes are legal
20467
20468 if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
20469 and then
20470 not (Ada_Version >= Ada_2005
20471 and then
20472 (Nkind (Parent (T)) = N_Subtype_Declaration
20473 or else (Nkind (Parent (T)) = N_Subtype_Indication
20474 and then Nkind (Parent (Parent (T))) =
20475 N_Subtype_Declaration)))
20476 then
20477 Error_Msg_N ("invalid use of type before its full declaration", T);
20478 end if;
20479 end Check_Incomplete;
20480
20481 -- Start of processing for Process_Subtype
20482
20483 begin
20484 -- Case of no constraints present
20485
20486 if Nkind (S) /= N_Subtype_Indication then
20487 Find_Type (S);
20488 Check_Incomplete (S);
20489 P := Parent (S);
20490
20491 -- Ada 2005 (AI-231): Static check
20492
20493 if Ada_Version >= Ada_2005
20494 and then Present (P)
20495 and then Null_Exclusion_Present (P)
20496 and then Nkind (P) /= N_Access_To_Object_Definition
20497 and then not Is_Access_Type (Entity (S))
20498 then
20499 Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
20500 end if;
20501
20502 -- The following is ugly, can't we have a range or even a flag???
20503
20504 May_Have_Null_Exclusion :=
20505 Nkind_In (P, N_Access_Definition,
20506 N_Access_Function_Definition,
20507 N_Access_Procedure_Definition,
20508 N_Access_To_Object_Definition,
20509 N_Allocator,
20510 N_Component_Definition)
20511 or else
20512 Nkind_In (P, N_Derived_Type_Definition,
20513 N_Discriminant_Specification,
20514 N_Formal_Object_Declaration,
20515 N_Object_Declaration,
20516 N_Object_Renaming_Declaration,
20517 N_Parameter_Specification,
20518 N_Subtype_Declaration);
20519
20520 -- Create an Itype that is a duplicate of Entity (S) but with the
20521 -- null-exclusion attribute.
20522
20523 if May_Have_Null_Exclusion
20524 and then Is_Access_Type (Entity (S))
20525 and then Null_Exclusion_Present (P)
20526
20527 -- No need to check the case of an access to object definition.
20528 -- It is correct to define double not-null pointers.
20529
20530 -- Example:
20531 -- type Not_Null_Int_Ptr is not null access Integer;
20532 -- type Acc is not null access Not_Null_Int_Ptr;
20533
20534 and then Nkind (P) /= N_Access_To_Object_Definition
20535 then
20536 if Can_Never_Be_Null (Entity (S)) then
20537 case Nkind (Related_Nod) is
20538 when N_Full_Type_Declaration =>
20539 if Nkind (Type_Definition (Related_Nod))
20540 in N_Array_Type_Definition
20541 then
20542 Error_Node :=
20543 Subtype_Indication
20544 (Component_Definition
20545 (Type_Definition (Related_Nod)));
20546 else
20547 Error_Node :=
20548 Subtype_Indication (Type_Definition (Related_Nod));
20549 end if;
20550
20551 when N_Subtype_Declaration =>
20552 Error_Node := Subtype_Indication (Related_Nod);
20553
20554 when N_Object_Declaration =>
20555 Error_Node := Object_Definition (Related_Nod);
20556
20557 when N_Component_Declaration =>
20558 Error_Node :=
20559 Subtype_Indication (Component_Definition (Related_Nod));
20560
20561 when N_Allocator =>
20562 Error_Node := Expression (Related_Nod);
20563
20564 when others =>
20565 pragma Assert (False);
20566 Error_Node := Related_Nod;
20567 end case;
20568
20569 Error_Msg_NE
20570 ("`NOT NULL` not allowed (& already excludes null)",
20571 Error_Node,
20572 Entity (S));
20573 end if;
20574
20575 Set_Etype (S,
20576 Create_Null_Excluding_Itype
20577 (T => Entity (S),
20578 Related_Nod => P));
20579 Set_Entity (S, Etype (S));
20580 end if;
20581
20582 return Entity (S);
20583
20584 -- Case of constraint present, so that we have an N_Subtype_Indication
20585 -- node (this node is created only if constraints are present).
20586
20587 else
20588 Find_Type (Subtype_Mark (S));
20589
20590 if Nkind (Parent (S)) /= N_Access_To_Object_Definition
20591 and then not
20592 (Nkind (Parent (S)) = N_Subtype_Declaration
20593 and then Is_Itype (Defining_Identifier (Parent (S))))
20594 then
20595 Check_Incomplete (Subtype_Mark (S));
20596 end if;
20597
20598 P := Parent (S);
20599 Subtype_Mark_Id := Entity (Subtype_Mark (S));
20600
20601 -- Explicit subtype declaration case
20602
20603 if Nkind (P) = N_Subtype_Declaration then
20604 Def_Id := Defining_Identifier (P);
20605
20606 -- Explicit derived type definition case
20607
20608 elsif Nkind (P) = N_Derived_Type_Definition then
20609 Def_Id := Defining_Identifier (Parent (P));
20610
20611 -- Implicit case, the Def_Id must be created as an implicit type.
20612 -- The one exception arises in the case of concurrent types, array
20613 -- and access types, where other subsidiary implicit types may be
20614 -- created and must appear before the main implicit type. In these
20615 -- cases we leave Def_Id set to Empty as a signal that Create_Itype
20616 -- has not yet been called to create Def_Id.
20617
20618 else
20619 if Is_Array_Type (Subtype_Mark_Id)
20620 or else Is_Concurrent_Type (Subtype_Mark_Id)
20621 or else Is_Access_Type (Subtype_Mark_Id)
20622 then
20623 Def_Id := Empty;
20624
20625 -- For the other cases, we create a new unattached Itype,
20626 -- and set the indication to ensure it gets attached later.
20627
20628 else
20629 Def_Id :=
20630 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
20631 end if;
20632 end if;
20633
20634 -- If the kind of constraint is invalid for this kind of type,
20635 -- then give an error, and then pretend no constraint was given.
20636
20637 if not Is_Valid_Constraint_Kind
20638 (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
20639 then
20640 Error_Msg_N
20641 ("incorrect constraint for this kind of type", Constraint (S));
20642
20643 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
20644
20645 -- Set Ekind of orphan itype, to prevent cascaded errors
20646
20647 if Present (Def_Id) then
20648 Set_Ekind (Def_Id, Ekind (Any_Type));
20649 end if;
20650
20651 -- Make recursive call, having got rid of the bogus constraint
20652
20653 return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
20654 end if;
20655
20656 -- Remaining processing depends on type. Select on Base_Type kind to
20657 -- ensure getting to the concrete type kind in the case of a private
20658 -- subtype (needed when only doing semantic analysis).
20659
20660 case Ekind (Base_Type (Subtype_Mark_Id)) is
20661 when Access_Kind =>
20662
20663 -- If this is a constraint on a class-wide type, discard it.
20664 -- There is currently no way to express a partial discriminant
20665 -- constraint on a type with unknown discriminants. This is
20666 -- a pathology that the ACATS wisely decides not to test.
20667
20668 if Is_Class_Wide_Type (Designated_Type (Subtype_Mark_Id)) then
20669 if Comes_From_Source (S) then
20670 Error_Msg_N
20671 ("constraint on class-wide type ignored??",
20672 Constraint (S));
20673 end if;
20674
20675 if Nkind (P) = N_Subtype_Declaration then
20676 Set_Subtype_Indication (P,
20677 New_Occurrence_Of (Subtype_Mark_Id, Sloc (S)));
20678 end if;
20679
20680 return Subtype_Mark_Id;
20681 end if;
20682
20683 Constrain_Access (Def_Id, S, Related_Nod);
20684
20685 if Expander_Active
20686 and then Is_Itype (Designated_Type (Def_Id))
20687 and then Nkind (Related_Nod) = N_Subtype_Declaration
20688 and then not Is_Incomplete_Type (Designated_Type (Def_Id))
20689 then
20690 Build_Itype_Reference
20691 (Designated_Type (Def_Id), Related_Nod);
20692 end if;
20693
20694 when Array_Kind =>
20695 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
20696
20697 when Decimal_Fixed_Point_Kind =>
20698 Constrain_Decimal (Def_Id, S);
20699
20700 when Enumeration_Kind =>
20701 Constrain_Enumeration (Def_Id, S);
20702 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
20703
20704 when Ordinary_Fixed_Point_Kind =>
20705 Constrain_Ordinary_Fixed (Def_Id, S);
20706
20707 when Float_Kind =>
20708 Constrain_Float (Def_Id, S);
20709
20710 when Integer_Kind =>
20711 Constrain_Integer (Def_Id, S);
20712 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
20713
20714 when E_Record_Type |
20715 E_Record_Subtype |
20716 Class_Wide_Kind |
20717 E_Incomplete_Type =>
20718 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
20719
20720 if Ekind (Def_Id) = E_Incomplete_Type then
20721 Set_Private_Dependents (Def_Id, New_Elmt_List);
20722 end if;
20723
20724 when Private_Kind =>
20725 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
20726 Set_Private_Dependents (Def_Id, New_Elmt_List);
20727
20728 -- In case of an invalid constraint prevent further processing
20729 -- since the type constructed is missing expected fields.
20730
20731 if Etype (Def_Id) = Any_Type then
20732 return Def_Id;
20733 end if;
20734
20735 -- If the full view is that of a task with discriminants,
20736 -- we must constrain both the concurrent type and its
20737 -- corresponding record type. Otherwise we will just propagate
20738 -- the constraint to the full view, if available.
20739
20740 if Present (Full_View (Subtype_Mark_Id))
20741 and then Has_Discriminants (Subtype_Mark_Id)
20742 and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
20743 then
20744 Full_View_Id :=
20745 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
20746
20747 Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
20748 Constrain_Concurrent (Full_View_Id, S,
20749 Related_Nod, Related_Id, Suffix);
20750 Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
20751 Set_Full_View (Def_Id, Full_View_Id);
20752
20753 -- Introduce an explicit reference to the private subtype,
20754 -- to prevent scope anomalies in gigi if first use appears
20755 -- in a nested context, e.g. a later function body.
20756 -- Should this be generated in other contexts than a full
20757 -- type declaration?
20758
20759 if Is_Itype (Def_Id)
20760 and then
20761 Nkind (Parent (P)) = N_Full_Type_Declaration
20762 then
20763 Build_Itype_Reference (Def_Id, Parent (P));
20764 end if;
20765
20766 else
20767 Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
20768 end if;
20769
20770 when Concurrent_Kind =>
20771 Constrain_Concurrent (Def_Id, S,
20772 Related_Nod, Related_Id, Suffix);
20773
20774 when others =>
20775 Error_Msg_N ("invalid subtype mark in subtype indication", S);
20776 end case;
20777
20778 -- Size and Convention are always inherited from the base type
20779
20780 Set_Size_Info (Def_Id, (Subtype_Mark_Id));
20781 Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
20782
20783 return Def_Id;
20784 end if;
20785 end Process_Subtype;
20786
20787 --------------------------------------------
20788 -- Propagate_Default_Init_Cond_Attributes --
20789 --------------------------------------------
20790
20791 procedure Propagate_Default_Init_Cond_Attributes
20792 (From_Typ : Entity_Id;
20793 To_Typ : Entity_Id;
20794 Parent_To_Derivation : Boolean := False;
20795 Private_To_Full_View : Boolean := False)
20796 is
20797 procedure Remove_Default_Init_Cond_Procedure (Typ : Entity_Id);
20798 -- Remove the default initial procedure (if any) from the rep chain of
20799 -- type Typ.
20800
20801 ----------------------------------------
20802 -- Remove_Default_Init_Cond_Procedure --
20803 ----------------------------------------
20804
20805 procedure Remove_Default_Init_Cond_Procedure (Typ : Entity_Id) is
20806 Found : Boolean := False;
20807 Prev : Entity_Id;
20808 Subp : Entity_Id;
20809
20810 begin
20811 Prev := Typ;
20812 Subp := Subprograms_For_Type (Typ);
20813 while Present (Subp) loop
20814 if Is_Default_Init_Cond_Procedure (Subp) then
20815 Found := True;
20816 exit;
20817 end if;
20818
20819 Prev := Subp;
20820 Subp := Subprograms_For_Type (Subp);
20821 end loop;
20822
20823 if Found then
20824 Set_Subprograms_For_Type (Prev, Subprograms_For_Type (Subp));
20825 Set_Subprograms_For_Type (Subp, Empty);
20826 end if;
20827 end Remove_Default_Init_Cond_Procedure;
20828
20829 -- Local variables
20830
20831 Inherit_Procedure : Boolean := False;
20832
20833 -- Start of processing for Propagate_Default_Init_Cond_Attributes
20834
20835 begin
20836 if Has_Default_Init_Cond (From_Typ) then
20837
20838 -- A derived type inherits the attributes from its parent type
20839
20840 if Parent_To_Derivation then
20841 Set_Has_Inherited_Default_Init_Cond (To_Typ);
20842
20843 -- A full view shares the attributes with its private view
20844
20845 else
20846 Set_Has_Default_Init_Cond (To_Typ);
20847 end if;
20848
20849 Inherit_Procedure := True;
20850
20851 -- Due to the order of expansion, a derived private type is processed
20852 -- by two routines which both attempt to set the attributes related
20853 -- to pragma Default_Initial_Condition - Build_Derived_Type and then
20854 -- Process_Full_View.
20855
20856 -- package Pack is
20857 -- type Parent_Typ is private
20858 -- with Default_Initial_Condition ...;
20859 -- private
20860 -- type Parent_Typ is ...;
20861 -- end Pack;
20862
20863 -- with Pack; use Pack;
20864 -- package Pack_2 is
20865 -- type Deriv_Typ is private
20866 -- with Default_Initial_Condition ...;
20867 -- private
20868 -- type Deriv_Typ is new Parent_Typ;
20869 -- end Pack_2;
20870
20871 -- When Build_Derived_Type operates, it sets the attributes on the
20872 -- full view without taking into account that the private view may
20873 -- define its own default initial condition procedure. This becomes
20874 -- apparent in Process_Full_View which must undo some of the work by
20875 -- Build_Derived_Type and propagate the attributes from the private
20876 -- to the full view.
20877
20878 if Private_To_Full_View then
20879 Set_Has_Inherited_Default_Init_Cond (To_Typ, False);
20880 Remove_Default_Init_Cond_Procedure (To_Typ);
20881 end if;
20882
20883 -- A type must inherit the default initial condition procedure from a
20884 -- parent type when the parent itself is inheriting the procedure or
20885 -- when it is defining one. This circuitry is also used when dealing
20886 -- with the private / full view of a type.
20887
20888 elsif Has_Inherited_Default_Init_Cond (From_Typ)
20889 or (Parent_To_Derivation
20890 and Present (Get_Pragma
20891 (From_Typ, Pragma_Default_Initial_Condition)))
20892 then
20893 Set_Has_Inherited_Default_Init_Cond (To_Typ);
20894 Inherit_Procedure := True;
20895 end if;
20896
20897 if Inherit_Procedure
20898 and then No (Default_Init_Cond_Procedure (To_Typ))
20899 then
20900 Set_Default_Init_Cond_Procedure
20901 (To_Typ, Default_Init_Cond_Procedure (From_Typ));
20902 end if;
20903 end Propagate_Default_Init_Cond_Attributes;
20904
20905 -----------------------------
20906 -- Record_Type_Declaration --
20907 -----------------------------
20908
20909 procedure Record_Type_Declaration
20910 (T : Entity_Id;
20911 N : Node_Id;
20912 Prev : Entity_Id)
20913 is
20914 Def : constant Node_Id := Type_Definition (N);
20915 Is_Tagged : Boolean;
20916 Tag_Comp : Entity_Id;
20917
20918 begin
20919 -- These flags must be initialized before calling Process_Discriminants
20920 -- because this routine makes use of them.
20921
20922 Set_Ekind (T, E_Record_Type);
20923 Set_Etype (T, T);
20924 Init_Size_Align (T);
20925 Set_Interfaces (T, No_Elist);
20926 Set_Stored_Constraint (T, No_Elist);
20927 Set_Default_SSO (T);
20928
20929 -- Normal case
20930
20931 if Ada_Version < Ada_2005 or else not Interface_Present (Def) then
20932 if Limited_Present (Def) then
20933 Check_SPARK_05_Restriction ("limited is not allowed", N);
20934 end if;
20935
20936 if Abstract_Present (Def) then
20937 Check_SPARK_05_Restriction ("abstract is not allowed", N);
20938 end if;
20939
20940 -- The flag Is_Tagged_Type might have already been set by
20941 -- Find_Type_Name if it detected an error for declaration T. This
20942 -- arises in the case of private tagged types where the full view
20943 -- omits the word tagged.
20944
20945 Is_Tagged :=
20946 Tagged_Present (Def)
20947 or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
20948
20949 Set_Is_Limited_Record (T, Limited_Present (Def));
20950
20951 if Is_Tagged then
20952 Set_Is_Tagged_Type (T, True);
20953 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
20954 end if;
20955
20956 -- Type is abstract if full declaration carries keyword, or if
20957 -- previous partial view did.
20958
20959 Set_Is_Abstract_Type (T, Is_Abstract_Type (T)
20960 or else Abstract_Present (Def));
20961
20962 else
20963 Check_SPARK_05_Restriction ("interface is not allowed", N);
20964
20965 Is_Tagged := True;
20966 Analyze_Interface_Declaration (T, Def);
20967
20968 if Present (Discriminant_Specifications (N)) then
20969 Error_Msg_N
20970 ("interface types cannot have discriminants",
20971 Defining_Identifier
20972 (First (Discriminant_Specifications (N))));
20973 end if;
20974 end if;
20975
20976 -- First pass: if there are self-referential access components,
20977 -- create the required anonymous access type declarations, and if
20978 -- need be an incomplete type declaration for T itself.
20979
20980 Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
20981
20982 if Ada_Version >= Ada_2005
20983 and then Present (Interface_List (Def))
20984 then
20985 Check_Interfaces (N, Def);
20986
20987 declare
20988 Ifaces_List : Elist_Id;
20989
20990 begin
20991 -- Ada 2005 (AI-251): Collect the list of progenitors that are not
20992 -- already in the parents.
20993
20994 Collect_Interfaces
20995 (T => T,
20996 Ifaces_List => Ifaces_List,
20997 Exclude_Parents => True);
20998
20999 Set_Interfaces (T, Ifaces_List);
21000 end;
21001 end if;
21002
21003 -- Records constitute a scope for the component declarations within.
21004 -- The scope is created prior to the processing of these declarations.
21005 -- Discriminants are processed first, so that they are visible when
21006 -- processing the other components. The Ekind of the record type itself
21007 -- is set to E_Record_Type (subtypes appear as E_Record_Subtype).
21008
21009 -- Enter record scope
21010
21011 Push_Scope (T);
21012
21013 -- If an incomplete or private type declaration was already given for
21014 -- the type, then this scope already exists, and the discriminants have
21015 -- been declared within. We must verify that the full declaration
21016 -- matches the incomplete one.
21017
21018 Check_Or_Process_Discriminants (N, T, Prev);
21019
21020 Set_Is_Constrained (T, not Has_Discriminants (T));
21021 Set_Has_Delayed_Freeze (T, True);
21022
21023 -- For tagged types add a manually analyzed component corresponding
21024 -- to the component _tag, the corresponding piece of tree will be
21025 -- expanded as part of the freezing actions if it is not a CPP_Class.
21026
21027 if Is_Tagged then
21028
21029 -- Do not add the tag unless we are in expansion mode
21030
21031 if Expander_Active then
21032 Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
21033 Enter_Name (Tag_Comp);
21034
21035 Set_Ekind (Tag_Comp, E_Component);
21036 Set_Is_Tag (Tag_Comp);
21037 Set_Is_Aliased (Tag_Comp);
21038 Set_Etype (Tag_Comp, RTE (RE_Tag));
21039 Set_DT_Entry_Count (Tag_Comp, No_Uint);
21040 Set_Original_Record_Component (Tag_Comp, Tag_Comp);
21041 Init_Component_Location (Tag_Comp);
21042
21043 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
21044 -- implemented interfaces.
21045
21046 if Has_Interfaces (T) then
21047 Add_Interface_Tag_Components (N, T);
21048 end if;
21049 end if;
21050
21051 Make_Class_Wide_Type (T);
21052 Set_Direct_Primitive_Operations (T, New_Elmt_List);
21053 end if;
21054
21055 -- We must suppress range checks when processing record components in
21056 -- the presence of discriminants, since we don't want spurious checks to
21057 -- be generated during their analysis, but Suppress_Range_Checks flags
21058 -- must be reset the after processing the record definition.
21059
21060 -- Note: this is the only use of Kill_Range_Checks, and is a bit odd,
21061 -- couldn't we just use the normal range check suppression method here.
21062 -- That would seem cleaner ???
21063
21064 if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
21065 Set_Kill_Range_Checks (T, True);
21066 Record_Type_Definition (Def, Prev);
21067 Set_Kill_Range_Checks (T, False);
21068 else
21069 Record_Type_Definition (Def, Prev);
21070 end if;
21071
21072 -- Exit from record scope
21073
21074 End_Scope;
21075
21076 -- Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
21077 -- the implemented interfaces and associate them an aliased entity.
21078
21079 if Is_Tagged
21080 and then not Is_Empty_List (Interface_List (Def))
21081 then
21082 Derive_Progenitor_Subprograms (T, T);
21083 end if;
21084
21085 Check_Function_Writable_Actuals (N);
21086 end Record_Type_Declaration;
21087
21088 ----------------------------
21089 -- Record_Type_Definition --
21090 ----------------------------
21091
21092 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
21093 Component : Entity_Id;
21094 Ctrl_Components : Boolean := False;
21095 Final_Storage_Only : Boolean;
21096 T : Entity_Id;
21097
21098 begin
21099 if Ekind (Prev_T) = E_Incomplete_Type then
21100 T := Full_View (Prev_T);
21101 else
21102 T := Prev_T;
21103 end if;
21104
21105 -- In SPARK, tagged types and type extensions may only be declared in
21106 -- the specification of library unit packages.
21107
21108 if Present (Def) and then Is_Tagged_Type (T) then
21109 declare
21110 Typ : Node_Id;
21111 Ctxt : Node_Id;
21112
21113 begin
21114 if Nkind (Parent (Def)) = N_Full_Type_Declaration then
21115 Typ := Parent (Def);
21116 else
21117 pragma Assert
21118 (Nkind (Parent (Def)) = N_Derived_Type_Definition);
21119 Typ := Parent (Parent (Def));
21120 end if;
21121
21122 Ctxt := Parent (Typ);
21123
21124 if Nkind (Ctxt) = N_Package_Body
21125 and then Nkind (Parent (Ctxt)) = N_Compilation_Unit
21126 then
21127 Check_SPARK_05_Restriction
21128 ("type should be defined in package specification", Typ);
21129
21130 elsif Nkind (Ctxt) /= N_Package_Specification
21131 or else Nkind (Parent (Parent (Ctxt))) /= N_Compilation_Unit
21132 then
21133 Check_SPARK_05_Restriction
21134 ("type should be defined in library unit package", Typ);
21135 end if;
21136 end;
21137 end if;
21138
21139 Final_Storage_Only := not Is_Controlled (T);
21140
21141 -- Ada 2005: Check whether an explicit Limited is present in a derived
21142 -- type declaration.
21143
21144 if Nkind (Parent (Def)) = N_Derived_Type_Definition
21145 and then Limited_Present (Parent (Def))
21146 then
21147 Set_Is_Limited_Record (T);
21148 end if;
21149
21150 -- If the component list of a record type is defined by the reserved
21151 -- word null and there is no discriminant part, then the record type has
21152 -- no components and all records of the type are null records (RM 3.7)
21153 -- This procedure is also called to process the extension part of a
21154 -- record extension, in which case the current scope may have inherited
21155 -- components.
21156
21157 if No (Def)
21158 or else No (Component_List (Def))
21159 or else Null_Present (Component_List (Def))
21160 then
21161 if not Is_Tagged_Type (T) then
21162 Check_SPARK_05_Restriction ("untagged record cannot be null", Def);
21163 end if;
21164
21165 else
21166 Analyze_Declarations (Component_Items (Component_List (Def)));
21167
21168 if Present (Variant_Part (Component_List (Def))) then
21169 Check_SPARK_05_Restriction ("variant part is not allowed", Def);
21170 Analyze (Variant_Part (Component_List (Def)));
21171 end if;
21172 end if;
21173
21174 -- After completing the semantic analysis of the record definition,
21175 -- record components, both new and inherited, are accessible. Set their
21176 -- kind accordingly. Exclude malformed itypes from illegal declarations,
21177 -- whose Ekind may be void.
21178
21179 Component := First_Entity (Current_Scope);
21180 while Present (Component) loop
21181 if Ekind (Component) = E_Void
21182 and then not Is_Itype (Component)
21183 then
21184 Set_Ekind (Component, E_Component);
21185 Init_Component_Location (Component);
21186 end if;
21187
21188 if Has_Task (Etype (Component)) then
21189 Set_Has_Task (T);
21190 end if;
21191
21192 if Has_Protected (Etype (Component)) then
21193 Set_Has_Protected (T);
21194 end if;
21195
21196 if Ekind (Component) /= E_Component then
21197 null;
21198
21199 -- Do not set Has_Controlled_Component on a class-wide equivalent
21200 -- type. See Make_CW_Equivalent_Type.
21201
21202 elsif not Is_Class_Wide_Equivalent_Type (T)
21203 and then (Has_Controlled_Component (Etype (Component))
21204 or else (Chars (Component) /= Name_uParent
21205 and then Is_Controlled (Etype (Component))))
21206 then
21207 Set_Has_Controlled_Component (T, True);
21208 Final_Storage_Only :=
21209 Final_Storage_Only
21210 and then Finalize_Storage_Only (Etype (Component));
21211 Ctrl_Components := True;
21212 end if;
21213
21214 Next_Entity (Component);
21215 end loop;
21216
21217 -- A Type is Finalize_Storage_Only only if all its controlled components
21218 -- are also.
21219
21220 if Ctrl_Components then
21221 Set_Finalize_Storage_Only (T, Final_Storage_Only);
21222 end if;
21223
21224 -- Place reference to end record on the proper entity, which may
21225 -- be a partial view.
21226
21227 if Present (Def) then
21228 Process_End_Label (Def, 'e', Prev_T);
21229 end if;
21230 end Record_Type_Definition;
21231
21232 ------------------------
21233 -- Replace_Components --
21234 ------------------------
21235
21236 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id) is
21237 function Process (N : Node_Id) return Traverse_Result;
21238
21239 -------------
21240 -- Process --
21241 -------------
21242
21243 function Process (N : Node_Id) return Traverse_Result is
21244 Comp : Entity_Id;
21245
21246 begin
21247 if Nkind (N) = N_Discriminant_Specification then
21248 Comp := First_Discriminant (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_Discriminant (Comp);
21256 end loop;
21257
21258 elsif Nkind (N) = N_Component_Declaration then
21259 Comp := First_Component (Typ);
21260 while Present (Comp) loop
21261 if Chars (Comp) = Chars (Defining_Identifier (N)) then
21262 Set_Defining_Identifier (N, Comp);
21263 exit;
21264 end if;
21265
21266 Next_Component (Comp);
21267 end loop;
21268 end if;
21269
21270 return OK;
21271 end Process;
21272
21273 procedure Replace is new Traverse_Proc (Process);
21274
21275 -- Start of processing for Replace_Components
21276
21277 begin
21278 Replace (Decl);
21279 end Replace_Components;
21280
21281 -------------------------------
21282 -- Set_Completion_Referenced --
21283 -------------------------------
21284
21285 procedure Set_Completion_Referenced (E : Entity_Id) is
21286 begin
21287 -- If in main unit, mark entity that is a completion as referenced,
21288 -- warnings go on the partial view when needed.
21289
21290 if In_Extended_Main_Source_Unit (E) then
21291 Set_Referenced (E);
21292 end if;
21293 end Set_Completion_Referenced;
21294
21295 ---------------------
21296 -- Set_Default_SSO --
21297 ---------------------
21298
21299 procedure Set_Default_SSO (T : Entity_Id) is
21300 begin
21301 case Opt.Default_SSO is
21302 when ' ' =>
21303 null;
21304 when 'L' =>
21305 Set_SSO_Set_Low_By_Default (T, True);
21306 when 'H' =>
21307 Set_SSO_Set_High_By_Default (T, True);
21308 when others =>
21309 raise Program_Error;
21310 end case;
21311 end Set_Default_SSO;
21312
21313 ---------------------
21314 -- Set_Fixed_Range --
21315 ---------------------
21316
21317 -- The range for fixed-point types is complicated by the fact that we
21318 -- do not know the exact end points at the time of the declaration. This
21319 -- is true for three reasons:
21320
21321 -- A size clause may affect the fudging of the end-points.
21322 -- A small clause may affect the values of the end-points.
21323 -- We try to include the end-points if it does not affect the size.
21324
21325 -- This means that the actual end-points must be established at the
21326 -- point when the type is frozen. Meanwhile, we first narrow the range
21327 -- as permitted (so that it will fit if necessary in a small specified
21328 -- size), and then build a range subtree with these narrowed bounds.
21329 -- Set_Fixed_Range constructs the range from real literal values, and
21330 -- sets the range as the Scalar_Range of the given fixed-point type entity.
21331
21332 -- The parent of this range is set to point to the entity so that it is
21333 -- properly hooked into the tree (unlike normal Scalar_Range entries for
21334 -- other scalar types, which are just pointers to the range in the
21335 -- original tree, this would otherwise be an orphan).
21336
21337 -- The tree is left unanalyzed. When the type is frozen, the processing
21338 -- in Freeze.Freeze_Fixed_Point_Type notices that the range is not
21339 -- analyzed, and uses this as an indication that it should complete
21340 -- work on the range (it will know the final small and size values).
21341
21342 procedure Set_Fixed_Range
21343 (E : Entity_Id;
21344 Loc : Source_Ptr;
21345 Lo : Ureal;
21346 Hi : Ureal)
21347 is
21348 S : constant Node_Id :=
21349 Make_Range (Loc,
21350 Low_Bound => Make_Real_Literal (Loc, Lo),
21351 High_Bound => Make_Real_Literal (Loc, Hi));
21352 begin
21353 Set_Scalar_Range (E, S);
21354 Set_Parent (S, E);
21355
21356 -- Before the freeze point, the bounds of a fixed point are universal
21357 -- and carry the corresponding type.
21358
21359 Set_Etype (Low_Bound (S), Universal_Real);
21360 Set_Etype (High_Bound (S), Universal_Real);
21361 end Set_Fixed_Range;
21362
21363 ----------------------------------
21364 -- Set_Scalar_Range_For_Subtype --
21365 ----------------------------------
21366
21367 procedure Set_Scalar_Range_For_Subtype
21368 (Def_Id : Entity_Id;
21369 R : Node_Id;
21370 Subt : Entity_Id)
21371 is
21372 Kind : constant Entity_Kind := Ekind (Def_Id);
21373
21374 begin
21375 -- Defend against previous error
21376
21377 if Nkind (R) = N_Error then
21378 return;
21379 end if;
21380
21381 Set_Scalar_Range (Def_Id, R);
21382
21383 -- We need to link the range into the tree before resolving it so
21384 -- that types that are referenced, including importantly the subtype
21385 -- itself, are properly frozen (Freeze_Expression requires that the
21386 -- expression be properly linked into the tree). Of course if it is
21387 -- already linked in, then we do not disturb the current link.
21388
21389 if No (Parent (R)) then
21390 Set_Parent (R, Def_Id);
21391 end if;
21392
21393 -- Reset the kind of the subtype during analysis of the range, to
21394 -- catch possible premature use in the bounds themselves.
21395
21396 Set_Ekind (Def_Id, E_Void);
21397 Process_Range_Expr_In_Decl (R, Subt, Subtyp => Def_Id);
21398 Set_Ekind (Def_Id, Kind);
21399 end Set_Scalar_Range_For_Subtype;
21400
21401 --------------------------------------------------------
21402 -- Set_Stored_Constraint_From_Discriminant_Constraint --
21403 --------------------------------------------------------
21404
21405 procedure Set_Stored_Constraint_From_Discriminant_Constraint
21406 (E : Entity_Id)
21407 is
21408 begin
21409 -- Make sure set if encountered during Expand_To_Stored_Constraint
21410
21411 Set_Stored_Constraint (E, No_Elist);
21412
21413 -- Give it the right value
21414
21415 if Is_Constrained (E) and then Has_Discriminants (E) then
21416 Set_Stored_Constraint (E,
21417 Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
21418 end if;
21419 end Set_Stored_Constraint_From_Discriminant_Constraint;
21420
21421 -------------------------------------
21422 -- Signed_Integer_Type_Declaration --
21423 -------------------------------------
21424
21425 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
21426 Implicit_Base : Entity_Id;
21427 Base_Typ : Entity_Id;
21428 Lo_Val : Uint;
21429 Hi_Val : Uint;
21430 Errs : Boolean := False;
21431 Lo : Node_Id;
21432 Hi : Node_Id;
21433
21434 function Can_Derive_From (E : Entity_Id) return Boolean;
21435 -- Determine whether given bounds allow derivation from specified type
21436
21437 procedure Check_Bound (Expr : Node_Id);
21438 -- Check bound to make sure it is integral and static. If not, post
21439 -- appropriate error message and set Errs flag
21440
21441 ---------------------
21442 -- Can_Derive_From --
21443 ---------------------
21444
21445 -- Note we check both bounds against both end values, to deal with
21446 -- strange types like ones with a range of 0 .. -12341234.
21447
21448 function Can_Derive_From (E : Entity_Id) return Boolean is
21449 Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
21450 Hi : constant Uint := Expr_Value (Type_High_Bound (E));
21451 begin
21452 return Lo <= Lo_Val and then Lo_Val <= Hi
21453 and then
21454 Lo <= Hi_Val and then Hi_Val <= Hi;
21455 end Can_Derive_From;
21456
21457 -----------------
21458 -- Check_Bound --
21459 -----------------
21460
21461 procedure Check_Bound (Expr : Node_Id) is
21462 begin
21463 -- If a range constraint is used as an integer type definition, each
21464 -- bound of the range must be defined by a static expression of some
21465 -- integer type, but the two bounds need not have the same integer
21466 -- type (Negative bounds are allowed.) (RM 3.5.4)
21467
21468 if not Is_Integer_Type (Etype (Expr)) then
21469 Error_Msg_N
21470 ("integer type definition bounds must be of integer type", Expr);
21471 Errs := True;
21472
21473 elsif not Is_OK_Static_Expression (Expr) then
21474 Flag_Non_Static_Expr
21475 ("non-static expression used for integer type bound!", Expr);
21476 Errs := True;
21477
21478 -- The bounds are folded into literals, and we set their type to be
21479 -- universal, to avoid typing difficulties: we cannot set the type
21480 -- of the literal to the new type, because this would be a forward
21481 -- reference for the back end, and if the original type is user-
21482 -- defined this can lead to spurious semantic errors (e.g. 2928-003).
21483
21484 else
21485 if Is_Entity_Name (Expr) then
21486 Fold_Uint (Expr, Expr_Value (Expr), True);
21487 end if;
21488
21489 Set_Etype (Expr, Universal_Integer);
21490 end if;
21491 end Check_Bound;
21492
21493 -- Start of processing for Signed_Integer_Type_Declaration
21494
21495 begin
21496 -- Create an anonymous base type
21497
21498 Implicit_Base :=
21499 Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
21500
21501 -- Analyze and check the bounds, they can be of any integer type
21502
21503 Lo := Low_Bound (Def);
21504 Hi := High_Bound (Def);
21505
21506 -- Arbitrarily use Integer as the type if either bound had an error
21507
21508 if Hi = Error or else Lo = Error then
21509 Base_Typ := Any_Integer;
21510 Set_Error_Posted (T, True);
21511
21512 -- Here both bounds are OK expressions
21513
21514 else
21515 Analyze_And_Resolve (Lo, Any_Integer);
21516 Analyze_And_Resolve (Hi, Any_Integer);
21517
21518 Check_Bound (Lo);
21519 Check_Bound (Hi);
21520
21521 if Errs then
21522 Hi := Type_High_Bound (Standard_Long_Long_Integer);
21523 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
21524 end if;
21525
21526 -- Find type to derive from
21527
21528 Lo_Val := Expr_Value (Lo);
21529 Hi_Val := Expr_Value (Hi);
21530
21531 if Can_Derive_From (Standard_Short_Short_Integer) then
21532 Base_Typ := Base_Type (Standard_Short_Short_Integer);
21533
21534 elsif Can_Derive_From (Standard_Short_Integer) then
21535 Base_Typ := Base_Type (Standard_Short_Integer);
21536
21537 elsif Can_Derive_From (Standard_Integer) then
21538 Base_Typ := Base_Type (Standard_Integer);
21539
21540 elsif Can_Derive_From (Standard_Long_Integer) then
21541 Base_Typ := Base_Type (Standard_Long_Integer);
21542
21543 elsif Can_Derive_From (Standard_Long_Long_Integer) then
21544 Check_Restriction (No_Long_Long_Integers, Def);
21545 Base_Typ := Base_Type (Standard_Long_Long_Integer);
21546
21547 else
21548 Base_Typ := Base_Type (Standard_Long_Long_Integer);
21549 Error_Msg_N ("integer type definition bounds out of range", Def);
21550 Hi := Type_High_Bound (Standard_Long_Long_Integer);
21551 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
21552 end if;
21553 end if;
21554
21555 -- Complete both implicit base and declared first subtype entities. The
21556 -- inheritance of the rep item chain ensures that SPARK-related pragmas
21557 -- are not clobbered when the signed integer type acts as a full view of
21558 -- a private type.
21559
21560 Set_Etype (Implicit_Base, Base_Typ);
21561 Set_Size_Info (Implicit_Base, Base_Typ);
21562 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
21563 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
21564 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
21565
21566 Set_Ekind (T, E_Signed_Integer_Subtype);
21567 Set_Etype (T, Implicit_Base);
21568 Set_Size_Info (T, Implicit_Base);
21569 Inherit_Rep_Item_Chain (T, Implicit_Base);
21570 Set_Scalar_Range (T, Def);
21571 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
21572 Set_Is_Constrained (T);
21573 end Signed_Integer_Type_Declaration;
21574
21575 end Sem_Ch3;