dd48cff4d175678824ef929afee245680f151776
[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-2011, 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 Atree; use Atree;
27 with Checks; use Checks;
28 with Debug; use Debug;
29 with Elists; use Elists;
30 with Einfo; use Einfo;
31 with Errout; use Errout;
32 with Eval_Fat; use Eval_Fat;
33 with Exp_Ch3; use Exp_Ch3;
34 with Exp_Ch9; use Exp_Ch9;
35 with Exp_Disp; use Exp_Disp;
36 with Exp_Dist; use Exp_Dist;
37 with Exp_Tss; use Exp_Tss;
38 with Exp_Util; use Exp_Util;
39 with Fname; use Fname;
40 with Freeze; use Freeze;
41 with Itypes; use Itypes;
42 with Layout; use Layout;
43 with Lib; use Lib;
44 with Lib.Xref; use Lib.Xref;
45 with Namet; use Namet;
46 with Nmake; use Nmake;
47 with Opt; use Opt;
48 with Restrict; use Restrict;
49 with Rident; use Rident;
50 with Rtsfind; use Rtsfind;
51 with Sem; use Sem;
52 with Sem_Aux; use Sem_Aux;
53 with Sem_Case; use Sem_Case;
54 with Sem_Cat; use Sem_Cat;
55 with Sem_Ch6; use Sem_Ch6;
56 with Sem_Ch7; use Sem_Ch7;
57 with Sem_Ch8; use Sem_Ch8;
58 with Sem_Ch13; use Sem_Ch13;
59 with Sem_Disp; use Sem_Disp;
60 with Sem_Dist; use Sem_Dist;
61 with Sem_Elim; use Sem_Elim;
62 with Sem_Eval; use Sem_Eval;
63 with Sem_Mech; use Sem_Mech;
64 with Sem_Prag; use Sem_Prag;
65 with Sem_Res; use Sem_Res;
66 with Sem_Smem; use Sem_Smem;
67 with Sem_Type; use Sem_Type;
68 with Sem_Util; use Sem_Util;
69 with Sem_Warn; use Sem_Warn;
70 with Stand; use Stand;
71 with Sinfo; use Sinfo;
72 with Sinput; use Sinput;
73 with Snames; use Snames;
74 with Targparm; use Targparm;
75 with Tbuild; use Tbuild;
76 with Ttypes; use Ttypes;
77 with Uintp; use Uintp;
78 with Urealp; use Urealp;
79
80 package body Sem_Ch3 is
81
82 -----------------------
83 -- Local Subprograms --
84 -----------------------
85
86 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id);
87 -- Ada 2005 (AI-251): Add the tag components corresponding to all the
88 -- abstract interface types implemented by a record type or a derived
89 -- record type.
90
91 procedure Build_Derived_Type
92 (N : Node_Id;
93 Parent_Type : Entity_Id;
94 Derived_Type : Entity_Id;
95 Is_Completion : Boolean;
96 Derive_Subps : Boolean := True);
97 -- Create and decorate a Derived_Type given the Parent_Type entity. N is
98 -- the N_Full_Type_Declaration node containing the derived type definition.
99 -- Parent_Type is the entity for the parent type in the derived type
100 -- definition and Derived_Type the actual derived type. Is_Completion must
101 -- be set to False if Derived_Type is the N_Defining_Identifier node in N
102 -- (i.e. Derived_Type = Defining_Identifier (N)). In this case N is not the
103 -- completion of a private type declaration. If Is_Completion is set to
104 -- True, N is the completion of a private type declaration and Derived_Type
105 -- is different from the defining identifier inside N (i.e. Derived_Type /=
106 -- Defining_Identifier (N)). Derive_Subps indicates whether the parent
107 -- subprograms should be derived. The only case where this parameter is
108 -- False is when Build_Derived_Type is recursively called to process an
109 -- implicit derived full type for a type derived from a private type (in
110 -- that case the subprograms must only be derived for the private view of
111 -- the type).
112 --
113 -- ??? These flags need a bit of re-examination and re-documentation:
114 -- ??? are they both necessary (both seem related to the recursion)?
115
116 procedure Build_Derived_Access_Type
117 (N : Node_Id;
118 Parent_Type : Entity_Id;
119 Derived_Type : Entity_Id);
120 -- Subsidiary procedure to Build_Derived_Type. For a derived access type,
121 -- create an implicit base if the parent type is constrained or if the
122 -- subtype indication has a constraint.
123
124 procedure Build_Derived_Array_Type
125 (N : Node_Id;
126 Parent_Type : Entity_Id;
127 Derived_Type : Entity_Id);
128 -- Subsidiary procedure to Build_Derived_Type. For a derived array type,
129 -- create an implicit base if the parent type is constrained or if the
130 -- subtype indication has a constraint.
131
132 procedure Build_Derived_Concurrent_Type
133 (N : Node_Id;
134 Parent_Type : Entity_Id;
135 Derived_Type : Entity_Id);
136 -- Subsidiary procedure to Build_Derived_Type. For a derived task or
137 -- protected type, inherit entries and protected subprograms, check
138 -- legality of discriminant constraints if any.
139
140 procedure Build_Derived_Enumeration_Type
141 (N : Node_Id;
142 Parent_Type : Entity_Id;
143 Derived_Type : Entity_Id);
144 -- Subsidiary procedure to Build_Derived_Type. For a derived enumeration
145 -- type, we must create a new list of literals. Types derived from
146 -- Character and [Wide_]Wide_Character are special-cased.
147
148 procedure Build_Derived_Numeric_Type
149 (N : Node_Id;
150 Parent_Type : Entity_Id;
151 Derived_Type : Entity_Id);
152 -- Subsidiary procedure to Build_Derived_Type. For numeric types, create
153 -- an anonymous base type, and propagate constraint to subtype if needed.
154
155 procedure Build_Derived_Private_Type
156 (N : Node_Id;
157 Parent_Type : Entity_Id;
158 Derived_Type : Entity_Id;
159 Is_Completion : Boolean;
160 Derive_Subps : Boolean := True);
161 -- Subsidiary procedure to Build_Derived_Type. This procedure is complex
162 -- because the parent may or may not have a completion, and the derivation
163 -- may itself be a completion.
164
165 procedure Build_Derived_Record_Type
166 (N : Node_Id;
167 Parent_Type : Entity_Id;
168 Derived_Type : Entity_Id;
169 Derive_Subps : Boolean := True);
170 -- Subsidiary procedure for Build_Derived_Type and
171 -- Analyze_Private_Extension_Declaration used for tagged and untagged
172 -- record types. All parameters are as in Build_Derived_Type except that
173 -- N, in addition to being an N_Full_Type_Declaration node, can also be an
174 -- N_Private_Extension_Declaration node. See the definition of this routine
175 -- for much more info. Derive_Subps indicates whether subprograms should
176 -- be derived from the parent type. The only case where Derive_Subps is
177 -- False is for an implicit derived full type for a type derived from a
178 -- private type (see Build_Derived_Type).
179
180 procedure Build_Discriminal (Discrim : Entity_Id);
181 -- Create the discriminal corresponding to discriminant Discrim, that is
182 -- the parameter corresponding to Discrim to be used in initialization
183 -- procedures for the type where Discrim is a discriminant. Discriminals
184 -- are not used during semantic analysis, and are not fully defined
185 -- entities until expansion. Thus they are not given a scope until
186 -- initialization procedures are built.
187
188 function Build_Discriminant_Constraints
189 (T : Entity_Id;
190 Def : Node_Id;
191 Derived_Def : Boolean := False) return Elist_Id;
192 -- Validate discriminant constraints and return the list of the constraints
193 -- in order of discriminant declarations, where T is the discriminated
194 -- unconstrained type. Def is the N_Subtype_Indication node where the
195 -- discriminants constraints for T are specified. Derived_Def is True
196 -- when building the discriminant constraints in a derived type definition
197 -- of the form "type D (...) is new T (xxx)". In this case T is the parent
198 -- type and Def is the constraint "(xxx)" on T and this routine sets the
199 -- Corresponding_Discriminant field of the discriminants in the derived
200 -- type D to point to the corresponding discriminants in the parent type T.
201
202 procedure Build_Discriminated_Subtype
203 (T : Entity_Id;
204 Def_Id : Entity_Id;
205 Elist : Elist_Id;
206 Related_Nod : Node_Id;
207 For_Access : Boolean := False);
208 -- Subsidiary procedure to Constrain_Discriminated_Type and to
209 -- Process_Incomplete_Dependents. Given
210 --
211 -- T (a possibly discriminated base type)
212 -- Def_Id (a very partially built subtype for T),
213 --
214 -- the call completes Def_Id to be the appropriate E_*_Subtype.
215 --
216 -- The Elist is the list of discriminant constraints if any (it is set
217 -- to No_Elist if T is not a discriminated type, and to an empty list if
218 -- T has discriminants but there are no discriminant constraints). The
219 -- Related_Nod is the same as Decl_Node in Create_Constrained_Components.
220 -- The For_Access says whether or not this subtype is really constraining
221 -- an access type. That is its sole purpose is the designated type of an
222 -- access type -- in which case a Private_Subtype Is_For_Access_Subtype
223 -- is built to avoid freezing T when the access subtype is frozen.
224
225 function Build_Scalar_Bound
226 (Bound : Node_Id;
227 Par_T : Entity_Id;
228 Der_T : Entity_Id) return Node_Id;
229 -- The bounds of a derived scalar type are conversions of the bounds of
230 -- the parent type. Optimize the representation if the bounds are literals.
231 -- Needs a more complete spec--what are the parameters exactly, and what
232 -- exactly is the returned value, and how is Bound affected???
233
234 procedure Build_Underlying_Full_View
235 (N : Node_Id;
236 Typ : Entity_Id;
237 Par : Entity_Id);
238 -- If the completion of a private type is itself derived from a private
239 -- type, or if the full view of a private subtype is itself private, the
240 -- back-end has no way to compute the actual size of this type. We build
241 -- an internal subtype declaration of the proper parent type to convey
242 -- this information. This extra mechanism is needed because a full
243 -- view cannot itself have a full view (it would get clobbered during
244 -- view exchanges).
245
246 procedure Check_Access_Discriminant_Requires_Limited
247 (D : Node_Id;
248 Loc : Node_Id);
249 -- Check the restriction that the type to which an access discriminant
250 -- belongs must be a concurrent type or a descendant of a type with
251 -- the reserved word 'limited' in its declaration.
252
253 procedure Check_Anonymous_Access_Components
254 (Typ_Decl : Node_Id;
255 Typ : Entity_Id;
256 Prev : Entity_Id;
257 Comp_List : Node_Id);
258 -- Ada 2005 AI-382: an access component in a record definition can refer to
259 -- the enclosing record, in which case it denotes the type itself, and not
260 -- the current instance of the type. We create an anonymous access type for
261 -- the component, and flag it as an access to a component, so accessibility
262 -- checks are properly performed on it. The declaration of the access type
263 -- is placed ahead of that of the record to prevent order-of-elaboration
264 -- circularity issues in Gigi. We create an incomplete type for the record
265 -- declaration, which is the designated type of the anonymous access.
266
267 procedure Check_Delta_Expression (E : Node_Id);
268 -- Check that the expression represented by E is suitable for use as a
269 -- delta expression, i.e. it is of real type and is static.
270
271 procedure Check_Digits_Expression (E : Node_Id);
272 -- Check that the expression represented by E is suitable for use as a
273 -- digits expression, i.e. it is of integer type, positive and static.
274
275 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id);
276 -- Validate the initialization of an object declaration. T is the required
277 -- type, and Exp is the initialization expression.
278
279 procedure Check_Interfaces (N : Node_Id; Def : Node_Id);
280 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
281
282 procedure Check_Or_Process_Discriminants
283 (N : Node_Id;
284 T : Entity_Id;
285 Prev : Entity_Id := Empty);
286 -- If N is the full declaration of the completion T of an incomplete or
287 -- private type, check its discriminants (which are already known to be
288 -- conformant with those of the partial view, see Find_Type_Name),
289 -- otherwise process them. Prev is the entity of the partial declaration,
290 -- if any.
291
292 procedure Check_Real_Bound (Bound : Node_Id);
293 -- Check given bound for being of real type and static. If not, post an
294 -- appropriate message, and rewrite the bound with the real literal zero.
295
296 procedure Constant_Redeclaration
297 (Id : Entity_Id;
298 N : Node_Id;
299 T : out Entity_Id);
300 -- Various checks on legality of full declaration of deferred constant.
301 -- Id is the entity for the redeclaration, N is the N_Object_Declaration,
302 -- node. The caller has not yet set any attributes of this entity.
303
304 function Contain_Interface
305 (Iface : Entity_Id;
306 Ifaces : Elist_Id) return Boolean;
307 -- Ada 2005: Determine whether Iface is present in the list Ifaces
308
309 procedure Convert_Scalar_Bounds
310 (N : Node_Id;
311 Parent_Type : Entity_Id;
312 Derived_Type : Entity_Id;
313 Loc : Source_Ptr);
314 -- For derived scalar types, convert the bounds in the type definition to
315 -- the derived type, and complete their analysis. Given a constraint of the
316 -- form ".. new T range Lo .. Hi", Lo and Hi are analyzed and resolved with
317 -- T'Base, the parent_type. The bounds of the derived type (the anonymous
318 -- base) are copies of Lo and Hi. Finally, the bounds of the derived
319 -- subtype are conversions of those bounds to the derived_type, so that
320 -- their typing is consistent.
321
322 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id);
323 -- Copies attributes from array base type T2 to array base type T1. Copies
324 -- only attributes that apply to base types, but not subtypes.
325
326 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id);
327 -- Copies attributes from array subtype T2 to array subtype T1. Copies
328 -- attributes that apply to both subtypes and base types.
329
330 procedure Create_Constrained_Components
331 (Subt : Entity_Id;
332 Decl_Node : Node_Id;
333 Typ : Entity_Id;
334 Constraints : Elist_Id);
335 -- Build the list of entities for a constrained discriminated record
336 -- subtype. If a component depends on a discriminant, replace its subtype
337 -- using the discriminant values in the discriminant constraint. Subt
338 -- is the defining identifier for the subtype whose list of constrained
339 -- entities we will create. Decl_Node is the type declaration node where
340 -- we will attach all the itypes created. Typ is the base discriminated
341 -- type for the subtype Subt. Constraints is the list of discriminant
342 -- constraints for Typ.
343
344 function Constrain_Component_Type
345 (Comp : Entity_Id;
346 Constrained_Typ : Entity_Id;
347 Related_Node : Node_Id;
348 Typ : Entity_Id;
349 Constraints : Elist_Id) return Entity_Id;
350 -- Given a discriminated base type Typ, a list of discriminant constraint
351 -- Constraints for Typ and a component of Typ, with type Compon_Type,
352 -- create and return the type corresponding to Compon_type where all
353 -- discriminant references are replaced with the corresponding constraint.
354 -- If no discriminant references occur in Compon_Typ then return it as is.
355 -- Constrained_Typ is the final constrained subtype to which the
356 -- constrained Compon_Type belongs. Related_Node is the node where we will
357 -- attach all the itypes created.
358 --
359 -- Above description is confused, what is Compon_Type???
360
361 procedure Constrain_Access
362 (Def_Id : in out Entity_Id;
363 S : Node_Id;
364 Related_Nod : Node_Id);
365 -- Apply a list of constraints to an access type. If Def_Id is empty, it is
366 -- an anonymous type created for a subtype indication. In that case it is
367 -- created in the procedure and attached to Related_Nod.
368
369 procedure Constrain_Array
370 (Def_Id : in out Entity_Id;
371 SI : Node_Id;
372 Related_Nod : Node_Id;
373 Related_Id : Entity_Id;
374 Suffix : Character);
375 -- Apply a list of index constraints to an unconstrained array type. The
376 -- first parameter is the entity for the resulting subtype. A value of
377 -- Empty for Def_Id indicates that an implicit type must be created, but
378 -- creation is delayed (and must be done by this procedure) because other
379 -- subsidiary implicit types must be created first (which is why Def_Id
380 -- is an in/out parameter). The second parameter is a subtype indication
381 -- node for the constrained array to be created (e.g. something of the
382 -- form string (1 .. 10)). Related_Nod gives the place where this type
383 -- has to be inserted in the tree. The Related_Id and Suffix parameters
384 -- are used to build the associated Implicit type name.
385
386 procedure Constrain_Concurrent
387 (Def_Id : in out Entity_Id;
388 SI : Node_Id;
389 Related_Nod : Node_Id;
390 Related_Id : Entity_Id;
391 Suffix : Character);
392 -- Apply list of discriminant constraints to an unconstrained concurrent
393 -- type.
394 --
395 -- SI is the N_Subtype_Indication node containing the constraint and
396 -- the unconstrained type to constrain.
397 --
398 -- Def_Id is the entity for the resulting constrained subtype. A value
399 -- of Empty for Def_Id indicates that an implicit type must be created,
400 -- but creation is delayed (and must be done by this procedure) because
401 -- other subsidiary implicit types must be created first (which is why
402 -- Def_Id is an in/out parameter).
403 --
404 -- Related_Nod gives the place where this type has to be inserted
405 -- in the tree
406 --
407 -- The last two arguments are used to create its external name if needed.
408
409 function Constrain_Corresponding_Record
410 (Prot_Subt : Entity_Id;
411 Corr_Rec : Entity_Id;
412 Related_Nod : Node_Id;
413 Related_Id : Entity_Id) return Entity_Id;
414 -- When constraining a protected type or task type with discriminants,
415 -- constrain the corresponding record with the same discriminant values.
416
417 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id);
418 -- Constrain a decimal fixed point type with a digits constraint and/or a
419 -- range constraint, and build E_Decimal_Fixed_Point_Subtype entity.
420
421 procedure Constrain_Discriminated_Type
422 (Def_Id : Entity_Id;
423 S : Node_Id;
424 Related_Nod : Node_Id;
425 For_Access : Boolean := False);
426 -- Process discriminant constraints of composite type. Verify that values
427 -- have been provided for all discriminants, that the original type is
428 -- unconstrained, and that the types of the supplied expressions match
429 -- the discriminant types. The first three parameters are like in routine
430 -- Constrain_Concurrent. See Build_Discriminated_Subtype for an explanation
431 -- of For_Access.
432
433 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id);
434 -- Constrain an enumeration type with a range constraint. This is identical
435 -- to Constrain_Integer, but for the Ekind of the resulting subtype.
436
437 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id);
438 -- Constrain a floating point type with either a digits constraint
439 -- and/or a range constraint, building a E_Floating_Point_Subtype.
440
441 procedure Constrain_Index
442 (Index : Node_Id;
443 S : Node_Id;
444 Related_Nod : Node_Id;
445 Related_Id : Entity_Id;
446 Suffix : Character;
447 Suffix_Index : Nat);
448 -- Process an index constraint S in a constrained array declaration. The
449 -- constraint can be a subtype name, or a range with or without an explicit
450 -- subtype mark. The index is the corresponding index of the unconstrained
451 -- array. The Related_Id and Suffix parameters are used to build the
452 -- associated Implicit type name.
453
454 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id);
455 -- Build subtype of a signed or modular integer type
456
457 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id);
458 -- Constrain an ordinary fixed point type with a range constraint, and
459 -- build an E_Ordinary_Fixed_Point_Subtype entity.
460
461 procedure Copy_And_Swap (Priv, Full : Entity_Id);
462 -- Copy the Priv entity into the entity of its full declaration then swap
463 -- the two entities in such a manner that the former private type is now
464 -- seen as a full type.
465
466 procedure Decimal_Fixed_Point_Type_Declaration
467 (T : Entity_Id;
468 Def : Node_Id);
469 -- Create a new decimal fixed point type, and apply the constraint to
470 -- obtain a subtype of this new type.
471
472 procedure Complete_Private_Subtype
473 (Priv : Entity_Id;
474 Full : Entity_Id;
475 Full_Base : Entity_Id;
476 Related_Nod : Node_Id);
477 -- Complete the implicit full view of a private subtype by setting the
478 -- appropriate semantic fields. If the full view of the parent is a record
479 -- type, build constrained components of subtype.
480
481 procedure Derive_Progenitor_Subprograms
482 (Parent_Type : Entity_Id;
483 Tagged_Type : Entity_Id);
484 -- Ada 2005 (AI-251): To complete type derivation, collect the primitive
485 -- operations of progenitors of Tagged_Type, and replace the subsidiary
486 -- subtypes with Tagged_Type, to build the specs of the inherited interface
487 -- primitives. The derived primitives are aliased to those of the
488 -- interface. This routine takes care also of transferring to the full view
489 -- subprograms associated with the partial view of Tagged_Type that cover
490 -- interface primitives.
491
492 procedure Derived_Standard_Character
493 (N : Node_Id;
494 Parent_Type : Entity_Id;
495 Derived_Type : Entity_Id);
496 -- Subsidiary procedure to Build_Derived_Enumeration_Type which handles
497 -- derivations from types Standard.Character and Standard.Wide_Character.
498
499 procedure Derived_Type_Declaration
500 (T : Entity_Id;
501 N : Node_Id;
502 Is_Completion : Boolean);
503 -- Process a derived type declaration. Build_Derived_Type is invoked
504 -- to process the actual derived type definition. Parameters N and
505 -- Is_Completion have the same meaning as in Build_Derived_Type.
506 -- T is the N_Defining_Identifier for the entity defined in the
507 -- N_Full_Type_Declaration node N, that is T is the derived type.
508
509 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id);
510 -- Insert each literal in symbol table, as an overloadable identifier. Each
511 -- enumeration type is mapped into a sequence of integers, and each literal
512 -- is defined as a constant with integer value. If any of the literals are
513 -- character literals, the type is a character type, which means that
514 -- strings are legal aggregates for arrays of components of the type.
515
516 function Expand_To_Stored_Constraint
517 (Typ : Entity_Id;
518 Constraint : Elist_Id) return Elist_Id;
519 -- Given a constraint (i.e. a list of expressions) on the discriminants of
520 -- Typ, expand it into a constraint on the stored discriminants and return
521 -- the new list of expressions constraining the stored discriminants.
522
523 function Find_Type_Of_Object
524 (Obj_Def : Node_Id;
525 Related_Nod : Node_Id) return Entity_Id;
526 -- Get type entity for object referenced by Obj_Def, attaching the
527 -- implicit types generated to Related_Nod
528
529 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id);
530 -- Create a new float and apply the constraint to obtain subtype of it
531
532 function Has_Range_Constraint (N : Node_Id) return Boolean;
533 -- Given an N_Subtype_Indication node N, return True if a range constraint
534 -- is present, either directly, or as part of a digits or delta constraint.
535 -- In addition, a digits constraint in the decimal case returns True, since
536 -- it establishes a default range if no explicit range is present.
537
538 function Inherit_Components
539 (N : Node_Id;
540 Parent_Base : Entity_Id;
541 Derived_Base : Entity_Id;
542 Is_Tagged : Boolean;
543 Inherit_Discr : Boolean;
544 Discs : Elist_Id) return Elist_Id;
545 -- Called from Build_Derived_Record_Type to inherit the components of
546 -- Parent_Base (a base type) into the Derived_Base (the derived base type).
547 -- For more information on derived types and component inheritance please
548 -- consult the comment above the body of Build_Derived_Record_Type.
549 --
550 -- N is the original derived type declaration
551 --
552 -- Is_Tagged is set if we are dealing with tagged types
553 --
554 -- If Inherit_Discr is set, Derived_Base inherits its discriminants from
555 -- Parent_Base, otherwise no discriminants are inherited.
556 --
557 -- Discs gives the list of constraints that apply to Parent_Base in the
558 -- derived type declaration. If Discs is set to No_Elist, then we have
559 -- the following situation:
560 --
561 -- type Parent (D1..Dn : ..) is [tagged] record ...;
562 -- type Derived is new Parent [with ...];
563 --
564 -- which gets treated as
565 --
566 -- type Derived (D1..Dn : ..) is new Parent (D1,..,Dn) [with ...];
567 --
568 -- For untagged types the returned value is an association list. The list
569 -- starts from the association (Parent_Base => Derived_Base), and then it
570 -- contains a sequence of the associations of the form
571 --
572 -- (Old_Component => New_Component),
573 --
574 -- where Old_Component is the Entity_Id of a component in Parent_Base and
575 -- New_Component is the Entity_Id of the corresponding component in
576 -- Derived_Base. For untagged records, this association list is needed when
577 -- copying the record declaration for the derived base. In the tagged case
578 -- the value returned is irrelevant.
579
580 function Is_Valid_Constraint_Kind
581 (T_Kind : Type_Kind;
582 Constraint_Kind : Node_Kind) return Boolean;
583 -- Returns True if it is legal to apply the given kind of constraint to the
584 -- given kind of type (index constraint to an array type, for example).
585
586 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id);
587 -- Create new modular type. Verify that modulus is in bounds
588
589 procedure New_Concatenation_Op (Typ : Entity_Id);
590 -- Create an abbreviated declaration for an operator in order to
591 -- materialize concatenation on array types.
592
593 procedure Ordinary_Fixed_Point_Type_Declaration
594 (T : Entity_Id;
595 Def : Node_Id);
596 -- Create a new ordinary fixed point type, and apply the constraint to
597 -- obtain subtype of it.
598
599 procedure Prepare_Private_Subtype_Completion
600 (Id : Entity_Id;
601 Related_Nod : Node_Id);
602 -- Id is a subtype of some private type. Creates the full declaration
603 -- associated with Id whenever possible, i.e. when the full declaration
604 -- of the base type is already known. Records each subtype into
605 -- Private_Dependents of the base type.
606
607 procedure Process_Incomplete_Dependents
608 (N : Node_Id;
609 Full_T : Entity_Id;
610 Inc_T : Entity_Id);
611 -- Process all entities that depend on an incomplete type. There include
612 -- subtypes, subprogram types that mention the incomplete type in their
613 -- profiles, and subprogram with access parameters that designate the
614 -- incomplete type.
615
616 -- Inc_T is the defining identifier of an incomplete type declaration, its
617 -- Ekind is E_Incomplete_Type.
618 --
619 -- N is the corresponding N_Full_Type_Declaration for Inc_T.
620 --
621 -- Full_T is N's defining identifier.
622 --
623 -- Subtypes of incomplete types with discriminants are completed when the
624 -- parent type is. This is simpler than private subtypes, because they can
625 -- only appear in the same scope, and there is no need to exchange views.
626 -- Similarly, access_to_subprogram types may have a parameter or a return
627 -- type that is an incomplete type, and that must be replaced with the
628 -- full type.
629 --
630 -- If the full type is tagged, subprogram with access parameters that
631 -- designated the incomplete may be primitive operations of the full type,
632 -- and have to be processed accordingly.
633
634 procedure Process_Real_Range_Specification (Def : Node_Id);
635 -- Given the type definition for a real type, this procedure processes and
636 -- checks the real range specification of this type definition if one is
637 -- present. If errors are found, error messages are posted, and the
638 -- Real_Range_Specification of Def is reset to Empty.
639
640 procedure Record_Type_Declaration
641 (T : Entity_Id;
642 N : Node_Id;
643 Prev : Entity_Id);
644 -- Process a record type declaration (for both untagged and tagged
645 -- records). Parameters T and N are exactly like in procedure
646 -- Derived_Type_Declaration, except that no flag Is_Completion is needed
647 -- for this routine. If this is the completion of an incomplete type
648 -- declaration, Prev is the entity of the incomplete declaration, used for
649 -- cross-referencing. Otherwise Prev = T.
650
651 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id);
652 -- This routine is used to process the actual record type definition (both
653 -- for untagged and tagged records). Def is a record type definition node.
654 -- This procedure analyzes the components in this record type definition.
655 -- Prev_T is the entity for the enclosing record type. It is provided so
656 -- that its Has_Task flag can be set if any of the component have Has_Task
657 -- set. If the declaration is the completion of an incomplete type
658 -- declaration, Prev_T is the original incomplete type, whose full view is
659 -- the record type.
660
661 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id);
662 -- Subsidiary to Build_Derived_Record_Type. For untagged records, we
663 -- build a copy of the declaration tree of the parent, and we create
664 -- independently the list of components for the derived type. Semantic
665 -- information uses the component entities, but record representation
666 -- clauses are validated on the declaration tree. This procedure replaces
667 -- discriminants and components in the declaration with those that have
668 -- been created by Inherit_Components.
669
670 procedure Set_Fixed_Range
671 (E : Entity_Id;
672 Loc : Source_Ptr;
673 Lo : Ureal;
674 Hi : Ureal);
675 -- Build a range node with the given bounds and set it as the Scalar_Range
676 -- of the given fixed-point type entity. Loc is the source location used
677 -- for the constructed range. See body for further details.
678
679 procedure Set_Scalar_Range_For_Subtype
680 (Def_Id : Entity_Id;
681 R : Node_Id;
682 Subt : Entity_Id);
683 -- This routine is used to set the scalar range field for a subtype given
684 -- Def_Id, the entity for the subtype, and R, the range expression for the
685 -- scalar range. Subt provides the parent subtype to be used to analyze,
686 -- resolve, and check the given range.
687
688 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id);
689 -- Create a new signed integer entity, and apply the constraint to obtain
690 -- the required first named subtype of this type.
691
692 procedure Set_Stored_Constraint_From_Discriminant_Constraint
693 (E : Entity_Id);
694 -- E is some record type. This routine computes E's Stored_Constraint
695 -- from its Discriminant_Constraint.
696
697 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id);
698 -- Check that an entity in a list of progenitors is an interface,
699 -- emit error otherwise.
700
701 -----------------------
702 -- Access_Definition --
703 -----------------------
704
705 function Access_Definition
706 (Related_Nod : Node_Id;
707 N : Node_Id) return Entity_Id
708 is
709 Loc : constant Source_Ptr := Sloc (Related_Nod);
710 Anon_Type : Entity_Id;
711 Anon_Scope : Entity_Id;
712 Desig_Type : Entity_Id;
713 Decl : Entity_Id;
714 Enclosing_Prot_Type : Entity_Id := Empty;
715
716 begin
717 Check_SPARK_Restriction ("access type is not allowed", N);
718
719 if Is_Entry (Current_Scope)
720 and then Is_Task_Type (Etype (Scope (Current_Scope)))
721 then
722 Error_Msg_N ("task entries cannot have access parameters", N);
723 return Empty;
724 end if;
725
726 -- Ada 2005: for an object declaration the corresponding anonymous
727 -- type is declared in the current scope.
728
729 -- If the access definition is the return type of another access to
730 -- function, scope is the current one, because it is the one of the
731 -- current type declaration.
732
733 if Nkind_In (Related_Nod, N_Object_Declaration,
734 N_Access_Function_Definition)
735 then
736 Anon_Scope := Current_Scope;
737
738 -- For the anonymous function result case, retrieve the scope of the
739 -- function specification's associated entity rather than using the
740 -- current scope. The current scope will be the function itself if the
741 -- formal part is currently being analyzed, but will be the parent scope
742 -- in the case of a parameterless function, and we always want to use
743 -- the function's parent scope. Finally, if the function is a child
744 -- unit, we must traverse the tree to retrieve the proper entity.
745
746 elsif Nkind (Related_Nod) = N_Function_Specification
747 and then Nkind (Parent (N)) /= N_Parameter_Specification
748 then
749 -- If the current scope is a protected type, the anonymous access
750 -- is associated with one of the protected operations, and must
751 -- be available in the scope that encloses the protected declaration.
752 -- Otherwise the type is in the scope enclosing the subprogram.
753
754 -- If the function has formals, The return type of a subprogram
755 -- declaration is analyzed in the scope of the subprogram (see
756 -- Process_Formals) and thus the protected type, if present, is
757 -- the scope of the current function scope.
758
759 if Ekind (Current_Scope) = E_Protected_Type then
760 Enclosing_Prot_Type := Current_Scope;
761
762 elsif Ekind (Current_Scope) = E_Function
763 and then Ekind (Scope (Current_Scope)) = E_Protected_Type
764 then
765 Enclosing_Prot_Type := Scope (Current_Scope);
766 end if;
767
768 if Present (Enclosing_Prot_Type) then
769 Anon_Scope := Scope (Enclosing_Prot_Type);
770
771 else
772 Anon_Scope := Scope (Defining_Entity (Related_Nod));
773 end if;
774
775 -- For an access type definition, if the current scope is a child
776 -- unit it is the scope of the type.
777
778 elsif Is_Compilation_Unit (Current_Scope) then
779 Anon_Scope := Current_Scope;
780
781 -- For access formals, access components, and access discriminants, the
782 -- scope is that of the enclosing declaration,
783
784 else
785 Anon_Scope := Scope (Current_Scope);
786 end if;
787
788 Anon_Type :=
789 Create_Itype
790 (E_Anonymous_Access_Type, Related_Nod, Scope_Id => Anon_Scope);
791
792 if All_Present (N)
793 and then Ada_Version >= Ada_2005
794 then
795 Error_Msg_N ("ALL is not permitted for anonymous access types", N);
796 end if;
797
798 -- Ada 2005 (AI-254): In case of anonymous access to subprograms call
799 -- the corresponding semantic routine
800
801 if Present (Access_To_Subprogram_Definition (N)) then
802
803 -- Compiler runtime units are compiled in Ada 2005 mode when building
804 -- the runtime library but must also be compilable in Ada 95 mode
805 -- (when bootstrapping the compiler).
806
807 Check_Compiler_Unit (N);
808
809 Access_Subprogram_Declaration
810 (T_Name => Anon_Type,
811 T_Def => Access_To_Subprogram_Definition (N));
812
813 if Ekind (Anon_Type) = E_Access_Protected_Subprogram_Type then
814 Set_Ekind
815 (Anon_Type, E_Anonymous_Access_Protected_Subprogram_Type);
816 else
817 Set_Ekind
818 (Anon_Type, E_Anonymous_Access_Subprogram_Type);
819 end if;
820
821 Set_Can_Use_Internal_Rep
822 (Anon_Type, not Always_Compatible_Rep_On_Target);
823
824 -- If the anonymous access is associated with a protected operation,
825 -- create a reference to it after the enclosing protected definition
826 -- because the itype will be used in the subsequent bodies.
827
828 if Ekind (Current_Scope) = E_Protected_Type then
829 Build_Itype_Reference (Anon_Type, Parent (Current_Scope));
830 end if;
831
832 return Anon_Type;
833 end if;
834
835 Find_Type (Subtype_Mark (N));
836 Desig_Type := Entity (Subtype_Mark (N));
837
838 Set_Directly_Designated_Type (Anon_Type, Desig_Type);
839 Set_Etype (Anon_Type, Anon_Type);
840
841 -- Make sure the anonymous access type has size and alignment fields
842 -- set, as required by gigi. This is necessary in the case of the
843 -- Task_Body_Procedure.
844
845 if not Has_Private_Component (Desig_Type) then
846 Layout_Type (Anon_Type);
847 end if;
848
849 -- Ada 2005 (AI-231): Ada 2005 semantics for anonymous access differs
850 -- from Ada 95 semantics. In Ada 2005, anonymous access must specify if
851 -- the null value is allowed. In Ada 95 the null value is never allowed.
852
853 if Ada_Version >= Ada_2005 then
854 Set_Can_Never_Be_Null (Anon_Type, Null_Exclusion_Present (N));
855 else
856 Set_Can_Never_Be_Null (Anon_Type, True);
857 end if;
858
859 -- The anonymous access type is as public as the discriminated type or
860 -- subprogram that defines it. It is imported (for back-end purposes)
861 -- if the designated type is.
862
863 Set_Is_Public (Anon_Type, Is_Public (Scope (Anon_Type)));
864
865 -- Ada 2005 (AI-231): Propagate the access-constant attribute
866
867 Set_Is_Access_Constant (Anon_Type, Constant_Present (N));
868
869 -- The context is either a subprogram declaration, object declaration,
870 -- or an access discriminant, in a private or a full type declaration.
871 -- In the case of a subprogram, if the designated type is incomplete,
872 -- the operation will be a primitive operation of the full type, to be
873 -- updated subsequently. If the type is imported through a limited_with
874 -- clause, the subprogram is not a primitive operation of the type
875 -- (which is declared elsewhere in some other scope).
876
877 if Ekind (Desig_Type) = E_Incomplete_Type
878 and then not From_With_Type (Desig_Type)
879 and then Is_Overloadable (Current_Scope)
880 then
881 Append_Elmt (Current_Scope, Private_Dependents (Desig_Type));
882 Set_Has_Delayed_Freeze (Current_Scope);
883 end if;
884
885 -- Ada 2005: if the designated type is an interface that may contain
886 -- tasks, create a Master entity for the declaration. This must be done
887 -- before expansion of the full declaration, because the declaration may
888 -- include an expression that is an allocator, whose expansion needs the
889 -- proper Master for the created tasks.
890
891 if Nkind (Related_Nod) = N_Object_Declaration
892 and then Expander_Active
893 then
894 if Is_Interface (Desig_Type)
895 and then Is_Limited_Record (Desig_Type)
896 then
897 Build_Class_Wide_Master (Anon_Type);
898
899 -- Similarly, if the type is an anonymous access that designates
900 -- tasks, create a master entity for it in the current context.
901
902 elsif Has_Task (Desig_Type)
903 and then Comes_From_Source (Related_Nod)
904 and then not Restriction_Active (No_Task_Hierarchy)
905 then
906 if not Has_Master_Entity (Current_Scope) then
907 Decl :=
908 Make_Object_Declaration (Loc,
909 Defining_Identifier =>
910 Make_Defining_Identifier (Loc, Name_uMaster),
911 Constant_Present => True,
912 Object_Definition =>
913 New_Reference_To (RTE (RE_Master_Id), Loc),
914 Expression =>
915 Make_Explicit_Dereference (Loc,
916 New_Reference_To (RTE (RE_Current_Master), Loc)));
917
918 Insert_Before (Related_Nod, Decl);
919 Analyze (Decl);
920
921 Set_Master_Id (Anon_Type, Defining_Identifier (Decl));
922 Set_Has_Master_Entity (Current_Scope);
923 else
924 Build_Master_Renaming (Related_Nod, Anon_Type);
925 end if;
926 end if;
927 end if;
928
929 -- For a private component of a protected type, it is imperative that
930 -- the back-end elaborate the type immediately after the protected
931 -- declaration, because this type will be used in the declarations
932 -- created for the component within each protected body, so we must
933 -- create an itype reference for it now.
934
935 if Nkind (Parent (Related_Nod)) = N_Protected_Definition then
936 Build_Itype_Reference (Anon_Type, Parent (Parent (Related_Nod)));
937
938 -- Similarly, if the access definition is the return result of a
939 -- function, create an itype reference for it because it will be used
940 -- within the function body. For a regular function that is not a
941 -- compilation unit, insert reference after the declaration. For a
942 -- protected operation, insert it after the enclosing protected type
943 -- declaration. In either case, do not create a reference for a type
944 -- obtained through a limited_with clause, because this would introduce
945 -- semantic dependencies.
946
947 -- Similarly, do not create a reference if the designated type is a
948 -- generic formal, because no use of it will reach the backend.
949
950 elsif Nkind (Related_Nod) = N_Function_Specification
951 and then not From_With_Type (Desig_Type)
952 and then not Is_Generic_Type (Desig_Type)
953 then
954 if Present (Enclosing_Prot_Type) then
955 Build_Itype_Reference (Anon_Type, Parent (Enclosing_Prot_Type));
956
957 elsif Is_List_Member (Parent (Related_Nod))
958 and then Nkind (Parent (N)) /= N_Parameter_Specification
959 then
960 Build_Itype_Reference (Anon_Type, Parent (Related_Nod));
961 end if;
962
963 -- Finally, create an itype reference for an object declaration of an
964 -- anonymous access type. This is strictly necessary only for deferred
965 -- constants, but in any case will avoid out-of-scope problems in the
966 -- back-end.
967
968 elsif Nkind (Related_Nod) = N_Object_Declaration then
969 Build_Itype_Reference (Anon_Type, Related_Nod);
970 end if;
971
972 return Anon_Type;
973 end Access_Definition;
974
975 -----------------------------------
976 -- Access_Subprogram_Declaration --
977 -----------------------------------
978
979 procedure Access_Subprogram_Declaration
980 (T_Name : Entity_Id;
981 T_Def : Node_Id)
982 is
983
984 procedure Check_For_Premature_Usage (Def : Node_Id);
985 -- Check that type T_Name is not used, directly or recursively, as a
986 -- parameter or a return type in Def. Def is either a subtype, an
987 -- access_definition, or an access_to_subprogram_definition.
988
989 -------------------------------
990 -- Check_For_Premature_Usage --
991 -------------------------------
992
993 procedure Check_For_Premature_Usage (Def : Node_Id) is
994 Param : Node_Id;
995
996 begin
997 -- Check for a subtype mark
998
999 if Nkind (Def) in N_Has_Etype then
1000 if Etype (Def) = T_Name then
1001 Error_Msg_N
1002 ("type& cannot be used before end of its declaration", Def);
1003 end if;
1004
1005 -- If this is not a subtype, then this is an access_definition
1006
1007 elsif Nkind (Def) = N_Access_Definition then
1008 if Present (Access_To_Subprogram_Definition (Def)) then
1009 Check_For_Premature_Usage
1010 (Access_To_Subprogram_Definition (Def));
1011 else
1012 Check_For_Premature_Usage (Subtype_Mark (Def));
1013 end if;
1014
1015 -- The only cases left are N_Access_Function_Definition and
1016 -- N_Access_Procedure_Definition.
1017
1018 else
1019 if Present (Parameter_Specifications (Def)) then
1020 Param := First (Parameter_Specifications (Def));
1021 while Present (Param) loop
1022 Check_For_Premature_Usage (Parameter_Type (Param));
1023 Param := Next (Param);
1024 end loop;
1025 end if;
1026
1027 if Nkind (Def) = N_Access_Function_Definition then
1028 Check_For_Premature_Usage (Result_Definition (Def));
1029 end if;
1030 end if;
1031 end Check_For_Premature_Usage;
1032
1033 -- Local variables
1034
1035 Formals : constant List_Id := Parameter_Specifications (T_Def);
1036 Formal : Entity_Id;
1037 D_Ityp : Node_Id;
1038 Desig_Type : constant Entity_Id :=
1039 Create_Itype (E_Subprogram_Type, Parent (T_Def));
1040
1041 -- Start of processing for Access_Subprogram_Declaration
1042
1043 begin
1044 Check_SPARK_Restriction ("access type is not allowed", T_Def);
1045
1046 -- Associate the Itype node with the inner full-type declaration or
1047 -- subprogram spec or entry body. This is required to handle nested
1048 -- anonymous declarations. For example:
1049
1050 -- procedure P
1051 -- (X : access procedure
1052 -- (Y : access procedure
1053 -- (Z : access T)))
1054
1055 D_Ityp := Associated_Node_For_Itype (Desig_Type);
1056 while not (Nkind_In (D_Ityp, N_Full_Type_Declaration,
1057 N_Private_Type_Declaration,
1058 N_Private_Extension_Declaration,
1059 N_Procedure_Specification,
1060 N_Function_Specification,
1061 N_Entry_Body)
1062
1063 or else
1064 Nkind_In (D_Ityp, N_Object_Declaration,
1065 N_Object_Renaming_Declaration,
1066 N_Formal_Object_Declaration,
1067 N_Formal_Type_Declaration,
1068 N_Task_Type_Declaration,
1069 N_Protected_Type_Declaration))
1070 loop
1071 D_Ityp := Parent (D_Ityp);
1072 pragma Assert (D_Ityp /= Empty);
1073 end loop;
1074
1075 Set_Associated_Node_For_Itype (Desig_Type, D_Ityp);
1076
1077 if Nkind_In (D_Ityp, N_Procedure_Specification,
1078 N_Function_Specification)
1079 then
1080 Set_Scope (Desig_Type, Scope (Defining_Entity (D_Ityp)));
1081
1082 elsif Nkind_In (D_Ityp, N_Full_Type_Declaration,
1083 N_Object_Declaration,
1084 N_Object_Renaming_Declaration,
1085 N_Formal_Type_Declaration)
1086 then
1087 Set_Scope (Desig_Type, Scope (Defining_Identifier (D_Ityp)));
1088 end if;
1089
1090 if Nkind (T_Def) = N_Access_Function_Definition then
1091 if Nkind (Result_Definition (T_Def)) = N_Access_Definition then
1092 declare
1093 Acc : constant Node_Id := Result_Definition (T_Def);
1094
1095 begin
1096 if Present (Access_To_Subprogram_Definition (Acc))
1097 and then
1098 Protected_Present (Access_To_Subprogram_Definition (Acc))
1099 then
1100 Set_Etype
1101 (Desig_Type,
1102 Replace_Anonymous_Access_To_Protected_Subprogram
1103 (T_Def));
1104
1105 else
1106 Set_Etype
1107 (Desig_Type,
1108 Access_Definition (T_Def, Result_Definition (T_Def)));
1109 end if;
1110 end;
1111
1112 else
1113 Analyze (Result_Definition (T_Def));
1114
1115 declare
1116 Typ : constant Entity_Id := Entity (Result_Definition (T_Def));
1117
1118 begin
1119 -- If a null exclusion is imposed on the result type, then
1120 -- create a null-excluding itype (an access subtype) and use
1121 -- it as the function's Etype.
1122
1123 if Is_Access_Type (Typ)
1124 and then Null_Exclusion_In_Return_Present (T_Def)
1125 then
1126 Set_Etype (Desig_Type,
1127 Create_Null_Excluding_Itype
1128 (T => Typ,
1129 Related_Nod => T_Def,
1130 Scope_Id => Current_Scope));
1131
1132 else
1133 if From_With_Type (Typ) then
1134
1135 -- AI05-151: Incomplete types are allowed in all basic
1136 -- declarations, including access to subprograms.
1137
1138 if Ada_Version >= Ada_2012 then
1139 null;
1140
1141 else
1142 Error_Msg_NE
1143 ("illegal use of incomplete type&",
1144 Result_Definition (T_Def), Typ);
1145 end if;
1146
1147 elsif Ekind (Current_Scope) = E_Package
1148 and then In_Private_Part (Current_Scope)
1149 then
1150 if Ekind (Typ) = E_Incomplete_Type then
1151 Append_Elmt (Desig_Type, Private_Dependents (Typ));
1152
1153 elsif Is_Class_Wide_Type (Typ)
1154 and then Ekind (Etype (Typ)) = E_Incomplete_Type
1155 then
1156 Append_Elmt
1157 (Desig_Type, Private_Dependents (Etype (Typ)));
1158 end if;
1159 end if;
1160
1161 Set_Etype (Desig_Type, Typ);
1162 end if;
1163 end;
1164 end if;
1165
1166 if not (Is_Type (Etype (Desig_Type))) then
1167 Error_Msg_N
1168 ("expect type in function specification",
1169 Result_Definition (T_Def));
1170 end if;
1171
1172 else
1173 Set_Etype (Desig_Type, Standard_Void_Type);
1174 end if;
1175
1176 if Present (Formals) then
1177 Push_Scope (Desig_Type);
1178
1179 -- A bit of a kludge here. These kludges will be removed when Itypes
1180 -- have proper parent pointers to their declarations???
1181
1182 -- Kludge 1) Link defining_identifier of formals. Required by
1183 -- First_Formal to provide its functionality.
1184
1185 declare
1186 F : Node_Id;
1187
1188 begin
1189 F := First (Formals);
1190
1191 -- In ASIS mode, the access_to_subprogram may be analyzed twice,
1192 -- when it is part of an unconstrained type and subtype expansion
1193 -- is disabled. To avoid back-end problems with shared profiles,
1194 -- use previous subprogram type as the designated type.
1195
1196 if ASIS_Mode
1197 and then Present (Scope (Defining_Identifier (F)))
1198 then
1199 Set_Etype (T_Name, T_Name);
1200 Init_Size_Align (T_Name);
1201 Set_Directly_Designated_Type (T_Name,
1202 Scope (Defining_Identifier (F)));
1203 return;
1204 end if;
1205
1206 while Present (F) loop
1207 if No (Parent (Defining_Identifier (F))) then
1208 Set_Parent (Defining_Identifier (F), F);
1209 end if;
1210
1211 Next (F);
1212 end loop;
1213 end;
1214
1215 Process_Formals (Formals, Parent (T_Def));
1216
1217 -- Kludge 2) End_Scope requires that the parent pointer be set to
1218 -- something reasonable, but Itypes don't have parent pointers. So
1219 -- we set it and then unset it ???
1220
1221 Set_Parent (Desig_Type, T_Name);
1222 End_Scope;
1223 Set_Parent (Desig_Type, Empty);
1224 end if;
1225
1226 -- Check for premature usage of the type being defined
1227
1228 Check_For_Premature_Usage (T_Def);
1229
1230 -- The return type and/or any parameter type may be incomplete. Mark
1231 -- the subprogram_type as depending on the incomplete type, so that
1232 -- it can be updated when the full type declaration is seen. This
1233 -- only applies to incomplete types declared in some enclosing scope,
1234 -- not to limited views from other packages.
1235
1236 if Present (Formals) then
1237 Formal := First_Formal (Desig_Type);
1238 while Present (Formal) loop
1239 if Ekind (Formal) /= E_In_Parameter
1240 and then Nkind (T_Def) = N_Access_Function_Definition
1241 then
1242 Error_Msg_N ("functions can only have IN parameters", Formal);
1243 end if;
1244
1245 if Ekind (Etype (Formal)) = E_Incomplete_Type
1246 and then In_Open_Scopes (Scope (Etype (Formal)))
1247 then
1248 Append_Elmt (Desig_Type, Private_Dependents (Etype (Formal)));
1249 Set_Has_Delayed_Freeze (Desig_Type);
1250 end if;
1251
1252 Next_Formal (Formal);
1253 end loop;
1254 end if;
1255
1256 -- If the return type is incomplete, this is legal as long as the
1257 -- type is declared in the current scope and will be completed in
1258 -- it (rather than being part of limited view).
1259
1260 if Ekind (Etype (Desig_Type)) = E_Incomplete_Type
1261 and then not Has_Delayed_Freeze (Desig_Type)
1262 and then In_Open_Scopes (Scope (Etype (Desig_Type)))
1263 then
1264 Append_Elmt (Desig_Type, Private_Dependents (Etype (Desig_Type)));
1265 Set_Has_Delayed_Freeze (Desig_Type);
1266 end if;
1267
1268 Check_Delayed_Subprogram (Desig_Type);
1269
1270 if Protected_Present (T_Def) then
1271 Set_Ekind (T_Name, E_Access_Protected_Subprogram_Type);
1272 Set_Convention (Desig_Type, Convention_Protected);
1273 else
1274 Set_Ekind (T_Name, E_Access_Subprogram_Type);
1275 end if;
1276
1277 Set_Can_Use_Internal_Rep (T_Name, not Always_Compatible_Rep_On_Target);
1278
1279 Set_Etype (T_Name, T_Name);
1280 Init_Size_Align (T_Name);
1281 Set_Directly_Designated_Type (T_Name, Desig_Type);
1282
1283 -- Ada 2005 (AI-231): Propagate the null-excluding attribute
1284
1285 Set_Can_Never_Be_Null (T_Name, Null_Exclusion_Present (T_Def));
1286
1287 Check_Restriction (No_Access_Subprograms, T_Def);
1288 end Access_Subprogram_Declaration;
1289
1290 ----------------------------
1291 -- Access_Type_Declaration --
1292 ----------------------------
1293
1294 procedure Access_Type_Declaration (T : Entity_Id; Def : Node_Id) is
1295 P : constant Node_Id := Parent (Def);
1296 S : constant Node_Id := Subtype_Indication (Def);
1297
1298 Full_Desig : Entity_Id;
1299
1300 begin
1301 Check_SPARK_Restriction ("access type is not allowed", Def);
1302
1303 -- Check for permissible use of incomplete type
1304
1305 if Nkind (S) /= N_Subtype_Indication then
1306 Analyze (S);
1307
1308 if Ekind (Root_Type (Entity (S))) = E_Incomplete_Type then
1309 Set_Directly_Designated_Type (T, Entity (S));
1310 else
1311 Set_Directly_Designated_Type (T,
1312 Process_Subtype (S, P, T, 'P'));
1313 end if;
1314
1315 else
1316 Set_Directly_Designated_Type (T,
1317 Process_Subtype (S, P, T, 'P'));
1318 end if;
1319
1320 if All_Present (Def) or Constant_Present (Def) then
1321 Set_Ekind (T, E_General_Access_Type);
1322 else
1323 Set_Ekind (T, E_Access_Type);
1324 end if;
1325
1326 Full_Desig := Designated_Type (T);
1327
1328 if Base_Type (Full_Desig) = T then
1329 Error_Msg_N ("access type cannot designate itself", S);
1330
1331 -- In Ada 2005, the type may have a limited view through some unit
1332 -- in its own context, allowing the following circularity that cannot
1333 -- be detected earlier
1334
1335 elsif Is_Class_Wide_Type (Full_Desig)
1336 and then Etype (Full_Desig) = T
1337 then
1338 Error_Msg_N
1339 ("access type cannot designate its own classwide type", S);
1340
1341 -- Clean up indication of tagged status to prevent cascaded errors
1342
1343 Set_Is_Tagged_Type (T, False);
1344 end if;
1345
1346 Set_Etype (T, T);
1347
1348 -- If the type has appeared already in a with_type clause, it is
1349 -- frozen and the pointer size is already set. Else, initialize.
1350
1351 if not From_With_Type (T) then
1352 Init_Size_Align (T);
1353 end if;
1354
1355 -- Note that Has_Task is always false, since the access type itself
1356 -- is not a task type. See Einfo for more description on this point.
1357 -- Exactly the same consideration applies to Has_Controlled_Component.
1358
1359 Set_Has_Task (T, False);
1360 Set_Has_Controlled_Component (T, False);
1361
1362 -- Initialize field Finalization_Master explicitly to Empty, to avoid
1363 -- problems where an incomplete view of this entity has been previously
1364 -- established by a limited with and an overlaid version of this field
1365 -- (Stored_Constraint) was initialized for the incomplete view.
1366
1367 -- This reset is performed in most cases except where the access type
1368 -- has been created for the purposes of allocating or deallocating a
1369 -- build-in-place object. Such access types have explicitly set pools
1370 -- and finalization masters.
1371
1372 if No (Associated_Storage_Pool (T)) then
1373 Set_Finalization_Master (T, Empty);
1374 end if;
1375
1376 -- Ada 2005 (AI-231): Propagate the null-excluding and access-constant
1377 -- attributes
1378
1379 Set_Can_Never_Be_Null (T, Null_Exclusion_Present (Def));
1380 Set_Is_Access_Constant (T, Constant_Present (Def));
1381 end Access_Type_Declaration;
1382
1383 ----------------------------------
1384 -- Add_Interface_Tag_Components --
1385 ----------------------------------
1386
1387 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id) is
1388 Loc : constant Source_Ptr := Sloc (N);
1389 L : List_Id;
1390 Last_Tag : Node_Id;
1391
1392 procedure Add_Tag (Iface : Entity_Id);
1393 -- Add tag for one of the progenitor interfaces
1394
1395 -------------
1396 -- Add_Tag --
1397 -------------
1398
1399 procedure Add_Tag (Iface : Entity_Id) is
1400 Decl : Node_Id;
1401 Def : Node_Id;
1402 Tag : Entity_Id;
1403 Offset : Entity_Id;
1404
1405 begin
1406 pragma Assert (Is_Tagged_Type (Iface)
1407 and then Is_Interface (Iface));
1408
1409 -- This is a reasonable place to propagate predicates
1410
1411 if Has_Predicates (Iface) then
1412 Set_Has_Predicates (Typ);
1413 end if;
1414
1415 Def :=
1416 Make_Component_Definition (Loc,
1417 Aliased_Present => True,
1418 Subtype_Indication =>
1419 New_Occurrence_Of (RTE (RE_Interface_Tag), Loc));
1420
1421 Tag := Make_Temporary (Loc, 'V');
1422
1423 Decl :=
1424 Make_Component_Declaration (Loc,
1425 Defining_Identifier => Tag,
1426 Component_Definition => Def);
1427
1428 Analyze_Component_Declaration (Decl);
1429
1430 Set_Analyzed (Decl);
1431 Set_Ekind (Tag, E_Component);
1432 Set_Is_Tag (Tag);
1433 Set_Is_Aliased (Tag);
1434 Set_Related_Type (Tag, Iface);
1435 Init_Component_Location (Tag);
1436
1437 pragma Assert (Is_Frozen (Iface));
1438
1439 Set_DT_Entry_Count (Tag,
1440 DT_Entry_Count (First_Entity (Iface)));
1441
1442 if No (Last_Tag) then
1443 Prepend (Decl, L);
1444 else
1445 Insert_After (Last_Tag, Decl);
1446 end if;
1447
1448 Last_Tag := Decl;
1449
1450 -- If the ancestor has discriminants we need to give special support
1451 -- to store the offset_to_top value of the secondary dispatch tables.
1452 -- For this purpose we add a supplementary component just after the
1453 -- field that contains the tag associated with each secondary DT.
1454
1455 if Typ /= Etype (Typ)
1456 and then Has_Discriminants (Etype (Typ))
1457 then
1458 Def :=
1459 Make_Component_Definition (Loc,
1460 Subtype_Indication =>
1461 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
1462
1463 Offset := Make_Temporary (Loc, 'V');
1464
1465 Decl :=
1466 Make_Component_Declaration (Loc,
1467 Defining_Identifier => Offset,
1468 Component_Definition => Def);
1469
1470 Analyze_Component_Declaration (Decl);
1471
1472 Set_Analyzed (Decl);
1473 Set_Ekind (Offset, E_Component);
1474 Set_Is_Aliased (Offset);
1475 Set_Related_Type (Offset, Iface);
1476 Init_Component_Location (Offset);
1477 Insert_After (Last_Tag, Decl);
1478 Last_Tag := Decl;
1479 end if;
1480 end Add_Tag;
1481
1482 -- Local variables
1483
1484 Elmt : Elmt_Id;
1485 Ext : Node_Id;
1486 Comp : Node_Id;
1487
1488 -- Start of processing for Add_Interface_Tag_Components
1489
1490 begin
1491 if not RTE_Available (RE_Interface_Tag) then
1492 Error_Msg
1493 ("(Ada 2005) interface types not supported by this run-time!",
1494 Sloc (N));
1495 return;
1496 end if;
1497
1498 if Ekind (Typ) /= E_Record_Type
1499 or else (Is_Concurrent_Record_Type (Typ)
1500 and then Is_Empty_List (Abstract_Interface_List (Typ)))
1501 or else (not Is_Concurrent_Record_Type (Typ)
1502 and then No (Interfaces (Typ))
1503 and then Is_Empty_Elmt_List (Interfaces (Typ)))
1504 then
1505 return;
1506 end if;
1507
1508 -- Find the current last tag
1509
1510 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1511 Ext := Record_Extension_Part (Type_Definition (N));
1512 else
1513 pragma Assert (Nkind (Type_Definition (N)) = N_Record_Definition);
1514 Ext := Type_Definition (N);
1515 end if;
1516
1517 Last_Tag := Empty;
1518
1519 if not (Present (Component_List (Ext))) then
1520 Set_Null_Present (Ext, False);
1521 L := New_List;
1522 Set_Component_List (Ext,
1523 Make_Component_List (Loc,
1524 Component_Items => L,
1525 Null_Present => False));
1526 else
1527 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1528 L := Component_Items
1529 (Component_List
1530 (Record_Extension_Part
1531 (Type_Definition (N))));
1532 else
1533 L := Component_Items
1534 (Component_List
1535 (Type_Definition (N)));
1536 end if;
1537
1538 -- Find the last tag component
1539
1540 Comp := First (L);
1541 while Present (Comp) loop
1542 if Nkind (Comp) = N_Component_Declaration
1543 and then Is_Tag (Defining_Identifier (Comp))
1544 then
1545 Last_Tag := Comp;
1546 end if;
1547
1548 Next (Comp);
1549 end loop;
1550 end if;
1551
1552 -- At this point L references the list of components and Last_Tag
1553 -- references the current last tag (if any). Now we add the tag
1554 -- corresponding with all the interfaces that are not implemented
1555 -- by the parent.
1556
1557 if Present (Interfaces (Typ)) then
1558 Elmt := First_Elmt (Interfaces (Typ));
1559 while Present (Elmt) loop
1560 Add_Tag (Node (Elmt));
1561 Next_Elmt (Elmt);
1562 end loop;
1563 end if;
1564 end Add_Interface_Tag_Components;
1565
1566 -------------------------------------
1567 -- Add_Internal_Interface_Entities --
1568 -------------------------------------
1569
1570 procedure Add_Internal_Interface_Entities (Tagged_Type : Entity_Id) is
1571 Elmt : Elmt_Id;
1572 Iface : Entity_Id;
1573 Iface_Elmt : Elmt_Id;
1574 Iface_Prim : Entity_Id;
1575 Ifaces_List : Elist_Id;
1576 New_Subp : Entity_Id := Empty;
1577 Prim : Entity_Id;
1578 Restore_Scope : Boolean := False;
1579
1580 begin
1581 pragma Assert (Ada_Version >= Ada_2005
1582 and then Is_Record_Type (Tagged_Type)
1583 and then Is_Tagged_Type (Tagged_Type)
1584 and then Has_Interfaces (Tagged_Type)
1585 and then not Is_Interface (Tagged_Type));
1586
1587 -- Ensure that the internal entities are added to the scope of the type
1588
1589 if Scope (Tagged_Type) /= Current_Scope then
1590 Push_Scope (Scope (Tagged_Type));
1591 Restore_Scope := True;
1592 end if;
1593
1594 Collect_Interfaces (Tagged_Type, Ifaces_List);
1595
1596 Iface_Elmt := First_Elmt (Ifaces_List);
1597 while Present (Iface_Elmt) loop
1598 Iface := Node (Iface_Elmt);
1599
1600 -- Originally we excluded here from this processing interfaces that
1601 -- are parents of Tagged_Type because their primitives are located
1602 -- in the primary dispatch table (and hence no auxiliary internal
1603 -- entities are required to handle secondary dispatch tables in such
1604 -- case). However, these auxiliary entities are also required to
1605 -- handle derivations of interfaces in formals of generics (see
1606 -- Derive_Subprograms).
1607
1608 Elmt := First_Elmt (Primitive_Operations (Iface));
1609 while Present (Elmt) loop
1610 Iface_Prim := Node (Elmt);
1611
1612 if not Is_Predefined_Dispatching_Operation (Iface_Prim) then
1613 Prim :=
1614 Find_Primitive_Covering_Interface
1615 (Tagged_Type => Tagged_Type,
1616 Iface_Prim => Iface_Prim);
1617
1618 if No (Prim) and then Serious_Errors_Detected > 0 then
1619 goto Continue;
1620 end if;
1621
1622 pragma Assert (Present (Prim));
1623
1624 -- Ada 2012 (AI05-0197): If the name of the covering primitive
1625 -- differs from the name of the interface primitive then it is
1626 -- a private primitive inherited from a parent type. In such
1627 -- case, given that Tagged_Type covers the interface, the
1628 -- inherited private primitive becomes visible. For such
1629 -- purpose we add a new entity that renames the inherited
1630 -- private primitive.
1631
1632 if Chars (Prim) /= Chars (Iface_Prim) then
1633 pragma Assert (Has_Suffix (Prim, 'P'));
1634 Derive_Subprogram
1635 (New_Subp => New_Subp,
1636 Parent_Subp => Iface_Prim,
1637 Derived_Type => Tagged_Type,
1638 Parent_Type => Iface);
1639 Set_Alias (New_Subp, Prim);
1640 Set_Is_Abstract_Subprogram
1641 (New_Subp, Is_Abstract_Subprogram (Prim));
1642 end if;
1643
1644 Derive_Subprogram
1645 (New_Subp => New_Subp,
1646 Parent_Subp => Iface_Prim,
1647 Derived_Type => Tagged_Type,
1648 Parent_Type => Iface);
1649
1650 -- Ada 2005 (AI-251): Decorate internal entity Iface_Subp
1651 -- associated with interface types. These entities are
1652 -- only registered in the list of primitives of its
1653 -- corresponding tagged type because they are only used
1654 -- to fill the contents of the secondary dispatch tables.
1655 -- Therefore they are removed from the homonym chains.
1656
1657 Set_Is_Hidden (New_Subp);
1658 Set_Is_Internal (New_Subp);
1659 Set_Alias (New_Subp, Prim);
1660 Set_Is_Abstract_Subprogram
1661 (New_Subp, Is_Abstract_Subprogram (Prim));
1662 Set_Interface_Alias (New_Subp, Iface_Prim);
1663
1664 -- Internal entities associated with interface types are
1665 -- only registered in the list of primitives of the tagged
1666 -- type. They are only used to fill the contents of the
1667 -- secondary dispatch tables. Therefore they are not needed
1668 -- in the homonym chains.
1669
1670 Remove_Homonym (New_Subp);
1671
1672 -- Hidden entities associated with interfaces must have set
1673 -- the Has_Delay_Freeze attribute to ensure that, in case of
1674 -- locally defined tagged types (or compiling with static
1675 -- dispatch tables generation disabled) the corresponding
1676 -- entry of the secondary dispatch table is filled when
1677 -- such an entity is frozen.
1678
1679 Set_Has_Delayed_Freeze (New_Subp);
1680 end if;
1681
1682 <<Continue>>
1683 Next_Elmt (Elmt);
1684 end loop;
1685
1686 Next_Elmt (Iface_Elmt);
1687 end loop;
1688
1689 if Restore_Scope then
1690 Pop_Scope;
1691 end if;
1692 end Add_Internal_Interface_Entities;
1693
1694 -----------------------------------
1695 -- Analyze_Component_Declaration --
1696 -----------------------------------
1697
1698 procedure Analyze_Component_Declaration (N : Node_Id) is
1699 Id : constant Entity_Id := Defining_Identifier (N);
1700 E : constant Node_Id := Expression (N);
1701 Typ : constant Node_Id :=
1702 Subtype_Indication (Component_Definition (N));
1703 T : Entity_Id;
1704 P : Entity_Id;
1705
1706 function Contains_POC (Constr : Node_Id) return Boolean;
1707 -- Determines whether a constraint uses the discriminant of a record
1708 -- type thus becoming a per-object constraint (POC).
1709
1710 function Is_Known_Limited (Typ : Entity_Id) return Boolean;
1711 -- Typ is the type of the current component, check whether this type is
1712 -- a limited type. Used to validate declaration against that of
1713 -- enclosing record.
1714
1715 ------------------
1716 -- Contains_POC --
1717 ------------------
1718
1719 function Contains_POC (Constr : Node_Id) return Boolean is
1720 begin
1721 -- Prevent cascaded errors
1722
1723 if Error_Posted (Constr) then
1724 return False;
1725 end if;
1726
1727 case Nkind (Constr) is
1728 when N_Attribute_Reference =>
1729 return
1730 Attribute_Name (Constr) = Name_Access
1731 and then Prefix (Constr) = Scope (Entity (Prefix (Constr)));
1732
1733 when N_Discriminant_Association =>
1734 return Denotes_Discriminant (Expression (Constr));
1735
1736 when N_Identifier =>
1737 return Denotes_Discriminant (Constr);
1738
1739 when N_Index_Or_Discriminant_Constraint =>
1740 declare
1741 IDC : Node_Id;
1742
1743 begin
1744 IDC := First (Constraints (Constr));
1745 while Present (IDC) loop
1746
1747 -- One per-object constraint is sufficient
1748
1749 if Contains_POC (IDC) then
1750 return True;
1751 end if;
1752
1753 Next (IDC);
1754 end loop;
1755
1756 return False;
1757 end;
1758
1759 when N_Range =>
1760 return Denotes_Discriminant (Low_Bound (Constr))
1761 or else
1762 Denotes_Discriminant (High_Bound (Constr));
1763
1764 when N_Range_Constraint =>
1765 return Denotes_Discriminant (Range_Expression (Constr));
1766
1767 when others =>
1768 return False;
1769
1770 end case;
1771 end Contains_POC;
1772
1773 ----------------------
1774 -- Is_Known_Limited --
1775 ----------------------
1776
1777 function Is_Known_Limited (Typ : Entity_Id) return Boolean is
1778 P : constant Entity_Id := Etype (Typ);
1779 R : constant Entity_Id := Root_Type (Typ);
1780
1781 begin
1782 if Is_Limited_Record (Typ) then
1783 return True;
1784
1785 -- If the root type is limited (and not a limited interface)
1786 -- so is the current type
1787
1788 elsif Is_Limited_Record (R)
1789 and then
1790 (not Is_Interface (R)
1791 or else not Is_Limited_Interface (R))
1792 then
1793 return True;
1794
1795 -- Else the type may have a limited interface progenitor, but a
1796 -- limited record parent.
1797
1798 elsif R /= P
1799 and then Is_Limited_Record (P)
1800 then
1801 return True;
1802
1803 else
1804 return False;
1805 end if;
1806 end Is_Known_Limited;
1807
1808 -- Start of processing for Analyze_Component_Declaration
1809
1810 begin
1811 Generate_Definition (Id);
1812 Enter_Name (Id);
1813
1814 if Present (Typ) then
1815 T := Find_Type_Of_Object
1816 (Subtype_Indication (Component_Definition (N)), N);
1817
1818 if not Nkind_In (Typ, N_Identifier, N_Expanded_Name) then
1819 Check_SPARK_Restriction ("subtype mark required", Typ);
1820 end if;
1821
1822 -- Ada 2005 (AI-230): Access Definition case
1823
1824 else
1825 pragma Assert (Present
1826 (Access_Definition (Component_Definition (N))));
1827
1828 T := Access_Definition
1829 (Related_Nod => N,
1830 N => Access_Definition (Component_Definition (N)));
1831 Set_Is_Local_Anonymous_Access (T);
1832
1833 -- Ada 2005 (AI-254)
1834
1835 if Present (Access_To_Subprogram_Definition
1836 (Access_Definition (Component_Definition (N))))
1837 and then Protected_Present (Access_To_Subprogram_Definition
1838 (Access_Definition
1839 (Component_Definition (N))))
1840 then
1841 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1842 end if;
1843 end if;
1844
1845 -- If the subtype is a constrained subtype of the enclosing record,
1846 -- (which must have a partial view) the back-end does not properly
1847 -- handle the recursion. Rewrite the component declaration with an
1848 -- explicit subtype indication, which is acceptable to Gigi. We can copy
1849 -- the tree directly because side effects have already been removed from
1850 -- discriminant constraints.
1851
1852 if Ekind (T) = E_Access_Subtype
1853 and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
1854 and then Comes_From_Source (T)
1855 and then Nkind (Parent (T)) = N_Subtype_Declaration
1856 and then Etype (Directly_Designated_Type (T)) = Current_Scope
1857 then
1858 Rewrite
1859 (Subtype_Indication (Component_Definition (N)),
1860 New_Copy_Tree (Subtype_Indication (Parent (T))));
1861 T := Find_Type_Of_Object
1862 (Subtype_Indication (Component_Definition (N)), N);
1863 end if;
1864
1865 -- If the component declaration includes a default expression, then we
1866 -- check that the component is not of a limited type (RM 3.7(5)),
1867 -- and do the special preanalysis of the expression (see section on
1868 -- "Handling of Default and Per-Object Expressions" in the spec of
1869 -- package Sem).
1870
1871 if Present (E) then
1872 Check_SPARK_Restriction ("default expression is not allowed", E);
1873 Preanalyze_Spec_Expression (E, T);
1874 Check_Initialization (T, E);
1875
1876 if Ada_Version >= Ada_2005
1877 and then Ekind (T) = E_Anonymous_Access_Type
1878 and then Etype (E) /= Any_Type
1879 then
1880 -- Check RM 3.9.2(9): "if the expected type for an expression is
1881 -- an anonymous access-to-specific tagged type, then the object
1882 -- designated by the expression shall not be dynamically tagged
1883 -- unless it is a controlling operand in a call on a dispatching
1884 -- operation"
1885
1886 if Is_Tagged_Type (Directly_Designated_Type (T))
1887 and then
1888 Ekind (Directly_Designated_Type (T)) /= E_Class_Wide_Type
1889 and then
1890 Ekind (Directly_Designated_Type (Etype (E))) =
1891 E_Class_Wide_Type
1892 then
1893 Error_Msg_N
1894 ("access to specific tagged type required (RM 3.9.2(9))", E);
1895 end if;
1896
1897 -- (Ada 2005: AI-230): Accessibility check for anonymous
1898 -- components
1899
1900 if Type_Access_Level (Etype (E)) > Type_Access_Level (T) then
1901 Error_Msg_N
1902 ("expression has deeper access level than component " &
1903 "(RM 3.10.2 (12.2))", E);
1904 end if;
1905
1906 -- The initialization expression is a reference to an access
1907 -- discriminant. The type of the discriminant is always deeper
1908 -- than any access type.
1909
1910 if Ekind (Etype (E)) = E_Anonymous_Access_Type
1911 and then Is_Entity_Name (E)
1912 and then Ekind (Entity (E)) = E_In_Parameter
1913 and then Present (Discriminal_Link (Entity (E)))
1914 then
1915 Error_Msg_N
1916 ("discriminant has deeper accessibility level than target",
1917 E);
1918 end if;
1919 end if;
1920 end if;
1921
1922 -- The parent type may be a private view with unknown discriminants,
1923 -- and thus unconstrained. Regular components must be constrained.
1924
1925 if Is_Indefinite_Subtype (T) and then Chars (Id) /= Name_uParent then
1926 if Is_Class_Wide_Type (T) then
1927 Error_Msg_N
1928 ("class-wide subtype with unknown discriminants" &
1929 " in component declaration",
1930 Subtype_Indication (Component_Definition (N)));
1931 else
1932 Error_Msg_N
1933 ("unconstrained subtype in component declaration",
1934 Subtype_Indication (Component_Definition (N)));
1935 end if;
1936
1937 -- Components cannot be abstract, except for the special case of
1938 -- the _Parent field (case of extending an abstract tagged type)
1939
1940 elsif Is_Abstract_Type (T) and then Chars (Id) /= Name_uParent then
1941 Error_Msg_N ("type of a component cannot be abstract", N);
1942 end if;
1943
1944 Set_Etype (Id, T);
1945 Set_Is_Aliased (Id, Aliased_Present (Component_Definition (N)));
1946
1947 -- The component declaration may have a per-object constraint, set
1948 -- the appropriate flag in the defining identifier of the subtype.
1949
1950 if Present (Subtype_Indication (Component_Definition (N))) then
1951 declare
1952 Sindic : constant Node_Id :=
1953 Subtype_Indication (Component_Definition (N));
1954 begin
1955 if Nkind (Sindic) = N_Subtype_Indication
1956 and then Present (Constraint (Sindic))
1957 and then Contains_POC (Constraint (Sindic))
1958 then
1959 Set_Has_Per_Object_Constraint (Id);
1960 end if;
1961 end;
1962 end if;
1963
1964 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
1965 -- out some static checks.
1966
1967 if Ada_Version >= Ada_2005
1968 and then Can_Never_Be_Null (T)
1969 then
1970 Null_Exclusion_Static_Checks (N);
1971 end if;
1972
1973 -- If this component is private (or depends on a private type), flag the
1974 -- record type to indicate that some operations are not available.
1975
1976 P := Private_Component (T);
1977
1978 if Present (P) then
1979
1980 -- Check for circular definitions
1981
1982 if P = Any_Type then
1983 Set_Etype (Id, Any_Type);
1984
1985 -- There is a gap in the visibility of operations only if the
1986 -- component type is not defined in the scope of the record type.
1987
1988 elsif Scope (P) = Scope (Current_Scope) then
1989 null;
1990
1991 elsif Is_Limited_Type (P) then
1992 Set_Is_Limited_Composite (Current_Scope);
1993
1994 else
1995 Set_Is_Private_Composite (Current_Scope);
1996 end if;
1997 end if;
1998
1999 if P /= Any_Type
2000 and then Is_Limited_Type (T)
2001 and then Chars (Id) /= Name_uParent
2002 and then Is_Tagged_Type (Current_Scope)
2003 then
2004 if Is_Derived_Type (Current_Scope)
2005 and then not Is_Known_Limited (Current_Scope)
2006 then
2007 Error_Msg_N
2008 ("extension of nonlimited type cannot have limited components",
2009 N);
2010
2011 if Is_Interface (Root_Type (Current_Scope)) then
2012 Error_Msg_N
2013 ("\limitedness is not inherited from limited interface", N);
2014 Error_Msg_N ("\add LIMITED to type indication", N);
2015 end if;
2016
2017 Explain_Limited_Type (T, N);
2018 Set_Etype (Id, Any_Type);
2019 Set_Is_Limited_Composite (Current_Scope, False);
2020
2021 elsif not Is_Derived_Type (Current_Scope)
2022 and then not Is_Limited_Record (Current_Scope)
2023 and then not Is_Concurrent_Type (Current_Scope)
2024 then
2025 Error_Msg_N
2026 ("nonlimited tagged type cannot have limited components", N);
2027 Explain_Limited_Type (T, N);
2028 Set_Etype (Id, Any_Type);
2029 Set_Is_Limited_Composite (Current_Scope, False);
2030 end if;
2031 end if;
2032
2033 Set_Original_Record_Component (Id, Id);
2034
2035 if Has_Aspects (N) then
2036 Analyze_Aspect_Specifications (N, Id);
2037 end if;
2038 end Analyze_Component_Declaration;
2039
2040 --------------------------
2041 -- Analyze_Declarations --
2042 --------------------------
2043
2044 procedure Analyze_Declarations (L : List_Id) is
2045 D : Node_Id;
2046 Freeze_From : Entity_Id := Empty;
2047 Next_Node : Node_Id;
2048
2049 procedure Adjust_D;
2050 -- Adjust D not to include implicit label declarations, since these
2051 -- have strange Sloc values that result in elaboration check problems.
2052 -- (They have the sloc of the label as found in the source, and that
2053 -- is ahead of the current declarative part).
2054
2055 --------------
2056 -- Adjust_D --
2057 --------------
2058
2059 procedure Adjust_D is
2060 begin
2061 while Present (Prev (D))
2062 and then Nkind (D) = N_Implicit_Label_Declaration
2063 loop
2064 Prev (D);
2065 end loop;
2066 end Adjust_D;
2067
2068 -- Start of processing for Analyze_Declarations
2069
2070 begin
2071 if Restriction_Check_Required (SPARK) then
2072 Check_Later_Vs_Basic_Declarations (L, During_Parsing => False);
2073 end if;
2074
2075 D := First (L);
2076 while Present (D) loop
2077
2078 -- Package spec cannot contain a package declaration in SPARK
2079
2080 if Nkind (D) = N_Package_Declaration
2081 and then Nkind (Parent (L)) = N_Package_Specification
2082 then
2083 Check_SPARK_Restriction
2084 ("package specification cannot contain a package declaration",
2085 D);
2086 end if;
2087
2088 -- Complete analysis of declaration
2089
2090 Analyze (D);
2091 Next_Node := Next (D);
2092
2093 if No (Freeze_From) then
2094 Freeze_From := First_Entity (Current_Scope);
2095 end if;
2096
2097 -- At the end of a declarative part, freeze remaining entities
2098 -- declared in it. The end of the visible declarations of package
2099 -- specification is not the end of a declarative part if private
2100 -- declarations are present. The end of a package declaration is a
2101 -- freezing point only if it a library package. A task definition or
2102 -- protected type definition is not a freeze point either. Finally,
2103 -- we do not freeze entities in generic scopes, because there is no
2104 -- code generated for them and freeze nodes will be generated for
2105 -- the instance.
2106
2107 -- The end of a package instantiation is not a freeze point, but
2108 -- for now we make it one, because the generic body is inserted
2109 -- (currently) immediately after. Generic instantiations will not
2110 -- be a freeze point once delayed freezing of bodies is implemented.
2111 -- (This is needed in any case for early instantiations ???).
2112
2113 if No (Next_Node) then
2114 if Nkind_In (Parent (L), N_Component_List,
2115 N_Task_Definition,
2116 N_Protected_Definition)
2117 then
2118 null;
2119
2120 elsif Nkind (Parent (L)) /= N_Package_Specification then
2121 if Nkind (Parent (L)) = N_Package_Body then
2122 Freeze_From := First_Entity (Current_Scope);
2123 end if;
2124
2125 Adjust_D;
2126 Freeze_All (Freeze_From, D);
2127 Freeze_From := Last_Entity (Current_Scope);
2128
2129 elsif Scope (Current_Scope) /= Standard_Standard
2130 and then not Is_Child_Unit (Current_Scope)
2131 and then No (Generic_Parent (Parent (L)))
2132 then
2133 null;
2134
2135 elsif L /= Visible_Declarations (Parent (L))
2136 or else No (Private_Declarations (Parent (L)))
2137 or else Is_Empty_List (Private_Declarations (Parent (L)))
2138 then
2139 Adjust_D;
2140 Freeze_All (Freeze_From, D);
2141 Freeze_From := Last_Entity (Current_Scope);
2142 end if;
2143
2144 -- If next node is a body then freeze all types before the body.
2145 -- An exception occurs for some expander-generated bodies. If these
2146 -- are generated at places where in general language rules would not
2147 -- allow a freeze point, then we assume that the expander has
2148 -- explicitly checked that all required types are properly frozen,
2149 -- and we do not cause general freezing here. This special circuit
2150 -- is used when the encountered body is marked as having already
2151 -- been analyzed.
2152
2153 -- In all other cases (bodies that come from source, and expander
2154 -- generated bodies that have not been analyzed yet), freeze all
2155 -- types now. Note that in the latter case, the expander must take
2156 -- care to attach the bodies at a proper place in the tree so as to
2157 -- not cause unwanted freezing at that point.
2158
2159 elsif not Analyzed (Next_Node)
2160 and then (Nkind_In (Next_Node, N_Subprogram_Body,
2161 N_Entry_Body,
2162 N_Package_Body,
2163 N_Protected_Body,
2164 N_Task_Body)
2165 or else
2166 Nkind (Next_Node) in N_Body_Stub)
2167 then
2168 Adjust_D;
2169 Freeze_All (Freeze_From, D);
2170 Freeze_From := Last_Entity (Current_Scope);
2171 end if;
2172
2173 D := Next_Node;
2174 end loop;
2175
2176 -- One more thing to do, we need to scan the declarations to check
2177 -- for any precondition/postcondition pragmas (Pre/Post aspects have
2178 -- by this stage been converted into corresponding pragmas). It is
2179 -- at this point that we analyze the expressions in such pragmas,
2180 -- to implement the delayed visibility requirement.
2181
2182 declare
2183 Decl : Node_Id;
2184 Spec : Node_Id;
2185 Sent : Entity_Id;
2186 Prag : Node_Id;
2187
2188 begin
2189 Decl := First (L);
2190 while Present (Decl) loop
2191 if Nkind (Original_Node (Decl)) = N_Subprogram_Declaration then
2192 Spec := Specification (Original_Node (Decl));
2193 Sent := Defining_Unit_Name (Spec);
2194
2195 Prag := Spec_PPC_List (Contract (Sent));
2196 while Present (Prag) loop
2197 Analyze_PPC_In_Decl_Part (Prag, Sent);
2198 Prag := Next_Pragma (Prag);
2199 end loop;
2200
2201 Check_Subprogram_Contract (Sent);
2202
2203 Prag := Spec_TC_List (Contract (Sent));
2204 while Present (Prag) loop
2205 Analyze_TC_In_Decl_Part (Prag, Sent);
2206 Prag := Next_Pragma (Prag);
2207 end loop;
2208 end if;
2209
2210 Next (Decl);
2211 end loop;
2212 end;
2213 end Analyze_Declarations;
2214
2215 -----------------------------------
2216 -- Analyze_Full_Type_Declaration --
2217 -----------------------------------
2218
2219 procedure Analyze_Full_Type_Declaration (N : Node_Id) is
2220 Def : constant Node_Id := Type_Definition (N);
2221 Def_Id : constant Entity_Id := Defining_Identifier (N);
2222 T : Entity_Id;
2223 Prev : Entity_Id;
2224
2225 Is_Remote : constant Boolean :=
2226 (Is_Remote_Types (Current_Scope)
2227 or else Is_Remote_Call_Interface (Current_Scope))
2228 and then not (In_Private_Part (Current_Scope)
2229 or else In_Package_Body (Current_Scope));
2230
2231 procedure Check_Ops_From_Incomplete_Type;
2232 -- If there is a tagged incomplete partial view of the type, traverse
2233 -- the primitives of the incomplete view and change the type of any
2234 -- controlling formals and result to indicate the full view. The
2235 -- primitives will be added to the full type's primitive operations
2236 -- list later in Sem_Disp.Check_Operation_From_Incomplete_Type (which
2237 -- is called from Process_Incomplete_Dependents).
2238
2239 ------------------------------------
2240 -- Check_Ops_From_Incomplete_Type --
2241 ------------------------------------
2242
2243 procedure Check_Ops_From_Incomplete_Type is
2244 Elmt : Elmt_Id;
2245 Formal : Entity_Id;
2246 Op : Entity_Id;
2247
2248 begin
2249 if Prev /= T
2250 and then Ekind (Prev) = E_Incomplete_Type
2251 and then Is_Tagged_Type (Prev)
2252 and then Is_Tagged_Type (T)
2253 then
2254 Elmt := First_Elmt (Primitive_Operations (Prev));
2255 while Present (Elmt) loop
2256 Op := Node (Elmt);
2257
2258 Formal := First_Formal (Op);
2259 while Present (Formal) loop
2260 if Etype (Formal) = Prev then
2261 Set_Etype (Formal, T);
2262 end if;
2263
2264 Next_Formal (Formal);
2265 end loop;
2266
2267 if Etype (Op) = Prev then
2268 Set_Etype (Op, T);
2269 end if;
2270
2271 Next_Elmt (Elmt);
2272 end loop;
2273 end if;
2274 end Check_Ops_From_Incomplete_Type;
2275
2276 -- Start of processing for Analyze_Full_Type_Declaration
2277
2278 begin
2279 Prev := Find_Type_Name (N);
2280
2281 -- The full view, if present, now points to the current type
2282
2283 -- Ada 2005 (AI-50217): If the type was previously decorated when
2284 -- imported through a LIMITED WITH clause, it appears as incomplete
2285 -- but has no full view.
2286
2287 if Ekind (Prev) = E_Incomplete_Type
2288 and then Present (Full_View (Prev))
2289 then
2290 T := Full_View (Prev);
2291 else
2292 T := Prev;
2293 end if;
2294
2295 Set_Is_Pure (T, Is_Pure (Current_Scope));
2296
2297 -- We set the flag Is_First_Subtype here. It is needed to set the
2298 -- corresponding flag for the Implicit class-wide-type created
2299 -- during tagged types processing.
2300
2301 Set_Is_First_Subtype (T, True);
2302
2303 -- Only composite types other than array types are allowed to have
2304 -- discriminants.
2305
2306 case Nkind (Def) is
2307
2308 -- For derived types, the rule will be checked once we've figured
2309 -- out the parent type.
2310
2311 when N_Derived_Type_Definition =>
2312 null;
2313
2314 -- For record types, discriminants are allowed, unless we are in
2315 -- SPARK.
2316
2317 when N_Record_Definition =>
2318 if Present (Discriminant_Specifications (N)) then
2319 Check_SPARK_Restriction
2320 ("discriminant type is not allowed",
2321 Defining_Identifier
2322 (First (Discriminant_Specifications (N))));
2323 end if;
2324
2325 when others =>
2326 if Present (Discriminant_Specifications (N)) then
2327 Error_Msg_N
2328 ("elementary or array type cannot have discriminants",
2329 Defining_Identifier
2330 (First (Discriminant_Specifications (N))));
2331 end if;
2332 end case;
2333
2334 -- Elaborate the type definition according to kind, and generate
2335 -- subsidiary (implicit) subtypes where needed. We skip this if it was
2336 -- already done (this happens during the reanalysis that follows a call
2337 -- to the high level optimizer).
2338
2339 if not Analyzed (T) then
2340 Set_Analyzed (T);
2341
2342 case Nkind (Def) is
2343
2344 when N_Access_To_Subprogram_Definition =>
2345 Access_Subprogram_Declaration (T, Def);
2346
2347 -- If this is a remote access to subprogram, we must create the
2348 -- equivalent fat pointer type, and related subprograms.
2349
2350 if Is_Remote then
2351 Process_Remote_AST_Declaration (N);
2352 end if;
2353
2354 -- Validate categorization rule against access type declaration
2355 -- usually a violation in Pure unit, Shared_Passive unit.
2356
2357 Validate_Access_Type_Declaration (T, N);
2358
2359 when N_Access_To_Object_Definition =>
2360 Access_Type_Declaration (T, Def);
2361
2362 -- Validate categorization rule against access type declaration
2363 -- usually a violation in Pure unit, Shared_Passive unit.
2364
2365 Validate_Access_Type_Declaration (T, N);
2366
2367 -- If we are in a Remote_Call_Interface package and define a
2368 -- RACW, then calling stubs and specific stream attributes
2369 -- must be added.
2370
2371 if Is_Remote
2372 and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
2373 then
2374 Add_RACW_Features (Def_Id);
2375 end if;
2376
2377 -- Set no strict aliasing flag if config pragma seen
2378
2379 if Opt.No_Strict_Aliasing then
2380 Set_No_Strict_Aliasing (Base_Type (Def_Id));
2381 end if;
2382
2383 when N_Array_Type_Definition =>
2384 Array_Type_Declaration (T, Def);
2385
2386 when N_Derived_Type_Definition =>
2387 Derived_Type_Declaration (T, N, T /= Def_Id);
2388
2389 when N_Enumeration_Type_Definition =>
2390 Enumeration_Type_Declaration (T, Def);
2391
2392 when N_Floating_Point_Definition =>
2393 Floating_Point_Type_Declaration (T, Def);
2394
2395 when N_Decimal_Fixed_Point_Definition =>
2396 Decimal_Fixed_Point_Type_Declaration (T, Def);
2397
2398 when N_Ordinary_Fixed_Point_Definition =>
2399 Ordinary_Fixed_Point_Type_Declaration (T, Def);
2400
2401 when N_Signed_Integer_Type_Definition =>
2402 Signed_Integer_Type_Declaration (T, Def);
2403
2404 when N_Modular_Type_Definition =>
2405 Modular_Type_Declaration (T, Def);
2406
2407 when N_Record_Definition =>
2408 Record_Type_Declaration (T, N, Prev);
2409
2410 -- If declaration has a parse error, nothing to elaborate.
2411
2412 when N_Error =>
2413 null;
2414
2415 when others =>
2416 raise Program_Error;
2417
2418 end case;
2419 end if;
2420
2421 if Etype (T) = Any_Type then
2422 return;
2423 end if;
2424
2425 -- Controlled type is not allowed in SPARK
2426
2427 if Is_Visibly_Controlled (T) then
2428 Check_SPARK_Restriction ("controlled type is not allowed", N);
2429 end if;
2430
2431 -- Some common processing for all types
2432
2433 Set_Depends_On_Private (T, Has_Private_Component (T));
2434 Check_Ops_From_Incomplete_Type;
2435
2436 -- Both the declared entity, and its anonymous base type if one
2437 -- was created, need freeze nodes allocated.
2438
2439 declare
2440 B : constant Entity_Id := Base_Type (T);
2441
2442 begin
2443 -- In the case where the base type differs from the first subtype, we
2444 -- pre-allocate a freeze node, and set the proper link to the first
2445 -- subtype. Freeze_Entity will use this preallocated freeze node when
2446 -- it freezes the entity.
2447
2448 -- This does not apply if the base type is a generic type, whose
2449 -- declaration is independent of the current derived definition.
2450
2451 if B /= T and then not Is_Generic_Type (B) then
2452 Ensure_Freeze_Node (B);
2453 Set_First_Subtype_Link (Freeze_Node (B), T);
2454 end if;
2455
2456 -- A type that is imported through a limited_with clause cannot
2457 -- generate any code, and thus need not be frozen. However, an access
2458 -- type with an imported designated type needs a finalization list,
2459 -- which may be referenced in some other package that has non-limited
2460 -- visibility on the designated type. Thus we must create the
2461 -- finalization list at the point the access type is frozen, to
2462 -- prevent unsatisfied references at link time.
2463
2464 if not From_With_Type (T) or else Is_Access_Type (T) then
2465 Set_Has_Delayed_Freeze (T);
2466 end if;
2467 end;
2468
2469 -- Case where T is the full declaration of some private type which has
2470 -- been swapped in Defining_Identifier (N).
2471
2472 if T /= Def_Id and then Is_Private_Type (Def_Id) then
2473 Process_Full_View (N, T, Def_Id);
2474
2475 -- Record the reference. The form of this is a little strange, since
2476 -- the full declaration has been swapped in. So the first parameter
2477 -- here represents the entity to which a reference is made which is
2478 -- the "real" entity, i.e. the one swapped in, and the second
2479 -- parameter provides the reference location.
2480
2481 -- Also, we want to kill Has_Pragma_Unreferenced temporarily here
2482 -- since we don't want a complaint about the full type being an
2483 -- unwanted reference to the private type
2484
2485 declare
2486 B : constant Boolean := Has_Pragma_Unreferenced (T);
2487 begin
2488 Set_Has_Pragma_Unreferenced (T, False);
2489 Generate_Reference (T, T, 'c');
2490 Set_Has_Pragma_Unreferenced (T, B);
2491 end;
2492
2493 Set_Completion_Referenced (Def_Id);
2494
2495 -- For completion of incomplete type, process incomplete dependents
2496 -- and always mark the full type as referenced (it is the incomplete
2497 -- type that we get for any real reference).
2498
2499 elsif Ekind (Prev) = E_Incomplete_Type then
2500 Process_Incomplete_Dependents (N, T, Prev);
2501 Generate_Reference (Prev, Def_Id, 'c');
2502 Set_Completion_Referenced (Def_Id);
2503
2504 -- If not private type or incomplete type completion, this is a real
2505 -- definition of a new entity, so record it.
2506
2507 else
2508 Generate_Definition (Def_Id);
2509 end if;
2510
2511 if Chars (Scope (Def_Id)) = Name_System
2512 and then Chars (Def_Id) = Name_Address
2513 and then Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (N)))
2514 then
2515 Set_Is_Descendent_Of_Address (Def_Id);
2516 Set_Is_Descendent_Of_Address (Base_Type (Def_Id));
2517 Set_Is_Descendent_Of_Address (Prev);
2518 end if;
2519
2520 Set_Optimize_Alignment_Flags (Def_Id);
2521 Check_Eliminated (Def_Id);
2522
2523 -- If the declaration is a completion and aspects are present, apply
2524 -- them to the entity for the type which is currently the partial
2525 -- view, but which is the one that will be frozen.
2526
2527 if Has_Aspects (N) then
2528 if Prev /= Def_Id then
2529 Analyze_Aspect_Specifications (N, Prev);
2530 else
2531 Analyze_Aspect_Specifications (N, Def_Id);
2532 end if;
2533 end if;
2534 end Analyze_Full_Type_Declaration;
2535
2536 ----------------------------------
2537 -- Analyze_Incomplete_Type_Decl --
2538 ----------------------------------
2539
2540 procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
2541 F : constant Boolean := Is_Pure (Current_Scope);
2542 T : Entity_Id;
2543
2544 begin
2545 Check_SPARK_Restriction ("incomplete type is not allowed", N);
2546
2547 Generate_Definition (Defining_Identifier (N));
2548
2549 -- Process an incomplete declaration. The identifier must not have been
2550 -- declared already in the scope. However, an incomplete declaration may
2551 -- appear in the private part of a package, for a private type that has
2552 -- already been declared.
2553
2554 -- In this case, the discriminants (if any) must match
2555
2556 T := Find_Type_Name (N);
2557
2558 Set_Ekind (T, E_Incomplete_Type);
2559 Init_Size_Align (T);
2560 Set_Is_First_Subtype (T, True);
2561 Set_Etype (T, T);
2562
2563 -- Ada 2005 (AI-326): Minimum decoration to give support to tagged
2564 -- incomplete types.
2565
2566 if Tagged_Present (N) then
2567 Set_Is_Tagged_Type (T);
2568 Make_Class_Wide_Type (T);
2569 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2570 end if;
2571
2572 Push_Scope (T);
2573
2574 Set_Stored_Constraint (T, No_Elist);
2575
2576 if Present (Discriminant_Specifications (N)) then
2577 Process_Discriminants (N);
2578 end if;
2579
2580 End_Scope;
2581
2582 -- If the type has discriminants, non-trivial subtypes may be
2583 -- declared before the full view of the type. The full views of those
2584 -- subtypes will be built after the full view of the type.
2585
2586 Set_Private_Dependents (T, New_Elmt_List);
2587 Set_Is_Pure (T, F);
2588 end Analyze_Incomplete_Type_Decl;
2589
2590 -----------------------------------
2591 -- Analyze_Interface_Declaration --
2592 -----------------------------------
2593
2594 procedure Analyze_Interface_Declaration (T : Entity_Id; Def : Node_Id) is
2595 CW : constant Entity_Id := Class_Wide_Type (T);
2596
2597 begin
2598 Set_Is_Tagged_Type (T);
2599
2600 Set_Is_Limited_Record (T, Limited_Present (Def)
2601 or else Task_Present (Def)
2602 or else Protected_Present (Def)
2603 or else Synchronized_Present (Def));
2604
2605 -- Type is abstract if full declaration carries keyword, or if previous
2606 -- partial view did.
2607
2608 Set_Is_Abstract_Type (T);
2609 Set_Is_Interface (T);
2610
2611 -- Type is a limited interface if it includes the keyword limited, task,
2612 -- protected, or synchronized.
2613
2614 Set_Is_Limited_Interface
2615 (T, Limited_Present (Def)
2616 or else Protected_Present (Def)
2617 or else Synchronized_Present (Def)
2618 or else Task_Present (Def));
2619
2620 Set_Interfaces (T, New_Elmt_List);
2621 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2622
2623 -- Complete the decoration of the class-wide entity if it was already
2624 -- built (i.e. during the creation of the limited view)
2625
2626 if Present (CW) then
2627 Set_Is_Interface (CW);
2628 Set_Is_Limited_Interface (CW, Is_Limited_Interface (T));
2629 end if;
2630
2631 -- Check runtime support for synchronized interfaces
2632
2633 if VM_Target = No_VM
2634 and then (Is_Task_Interface (T)
2635 or else Is_Protected_Interface (T)
2636 or else Is_Synchronized_Interface (T))
2637 and then not RTE_Available (RE_Select_Specific_Data)
2638 then
2639 Error_Msg_CRT ("synchronized interfaces", T);
2640 end if;
2641 end Analyze_Interface_Declaration;
2642
2643 -----------------------------
2644 -- Analyze_Itype_Reference --
2645 -----------------------------
2646
2647 -- Nothing to do. This node is placed in the tree only for the benefit of
2648 -- back end processing, and has no effect on the semantic processing.
2649
2650 procedure Analyze_Itype_Reference (N : Node_Id) is
2651 begin
2652 pragma Assert (Is_Itype (Itype (N)));
2653 null;
2654 end Analyze_Itype_Reference;
2655
2656 --------------------------------
2657 -- Analyze_Number_Declaration --
2658 --------------------------------
2659
2660 procedure Analyze_Number_Declaration (N : Node_Id) is
2661 Id : constant Entity_Id := Defining_Identifier (N);
2662 E : constant Node_Id := Expression (N);
2663 T : Entity_Id;
2664 Index : Interp_Index;
2665 It : Interp;
2666
2667 begin
2668 Generate_Definition (Id);
2669 Enter_Name (Id);
2670
2671 -- This is an optimization of a common case of an integer literal
2672
2673 if Nkind (E) = N_Integer_Literal then
2674 Set_Is_Static_Expression (E, True);
2675 Set_Etype (E, Universal_Integer);
2676
2677 Set_Etype (Id, Universal_Integer);
2678 Set_Ekind (Id, E_Named_Integer);
2679 Set_Is_Frozen (Id, True);
2680 return;
2681 end if;
2682
2683 Set_Is_Pure (Id, Is_Pure (Current_Scope));
2684
2685 -- Process expression, replacing error by integer zero, to avoid
2686 -- cascaded errors or aborts further along in the processing
2687
2688 -- Replace Error by integer zero, which seems least likely to
2689 -- cause cascaded errors.
2690
2691 if E = Error then
2692 Rewrite (E, Make_Integer_Literal (Sloc (E), Uint_0));
2693 Set_Error_Posted (E);
2694 end if;
2695
2696 Analyze (E);
2697
2698 -- Verify that the expression is static and numeric. If
2699 -- the expression is overloaded, we apply the preference
2700 -- rule that favors root numeric types.
2701
2702 if not Is_Overloaded (E) then
2703 T := Etype (E);
2704
2705 else
2706 T := Any_Type;
2707
2708 Get_First_Interp (E, Index, It);
2709 while Present (It.Typ) loop
2710 if (Is_Integer_Type (It.Typ)
2711 or else Is_Real_Type (It.Typ))
2712 and then (Scope (Base_Type (It.Typ))) = Standard_Standard
2713 then
2714 if T = Any_Type then
2715 T := It.Typ;
2716
2717 elsif It.Typ = Universal_Real
2718 or else It.Typ = Universal_Integer
2719 then
2720 -- Choose universal interpretation over any other
2721
2722 T := It.Typ;
2723 exit;
2724 end if;
2725 end if;
2726
2727 Get_Next_Interp (Index, It);
2728 end loop;
2729 end if;
2730
2731 if Is_Integer_Type (T) then
2732 Resolve (E, T);
2733 Set_Etype (Id, Universal_Integer);
2734 Set_Ekind (Id, E_Named_Integer);
2735
2736 elsif Is_Real_Type (T) then
2737
2738 -- Because the real value is converted to universal_real, this is a
2739 -- legal context for a universal fixed expression.
2740
2741 if T = Universal_Fixed then
2742 declare
2743 Loc : constant Source_Ptr := Sloc (N);
2744 Conv : constant Node_Id := Make_Type_Conversion (Loc,
2745 Subtype_Mark =>
2746 New_Occurrence_Of (Universal_Real, Loc),
2747 Expression => Relocate_Node (E));
2748
2749 begin
2750 Rewrite (E, Conv);
2751 Analyze (E);
2752 end;
2753
2754 elsif T = Any_Fixed then
2755 Error_Msg_N ("illegal context for mixed mode operation", E);
2756
2757 -- Expression is of the form : universal_fixed * integer. Try to
2758 -- resolve as universal_real.
2759
2760 T := Universal_Real;
2761 Set_Etype (E, T);
2762 end if;
2763
2764 Resolve (E, T);
2765 Set_Etype (Id, Universal_Real);
2766 Set_Ekind (Id, E_Named_Real);
2767
2768 else
2769 Wrong_Type (E, Any_Numeric);
2770 Resolve (E, T);
2771
2772 Set_Etype (Id, T);
2773 Set_Ekind (Id, E_Constant);
2774 Set_Never_Set_In_Source (Id, True);
2775 Set_Is_True_Constant (Id, True);
2776 return;
2777 end if;
2778
2779 if Nkind_In (E, N_Integer_Literal, N_Real_Literal) then
2780 Set_Etype (E, Etype (Id));
2781 end if;
2782
2783 if not Is_OK_Static_Expression (E) then
2784 Flag_Non_Static_Expr
2785 ("non-static expression used in number declaration!", E);
2786 Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
2787 Set_Etype (E, Any_Type);
2788 end if;
2789 end Analyze_Number_Declaration;
2790
2791 --------------------------------
2792 -- Analyze_Object_Declaration --
2793 --------------------------------
2794
2795 procedure Analyze_Object_Declaration (N : Node_Id) is
2796 Loc : constant Source_Ptr := Sloc (N);
2797 Id : constant Entity_Id := Defining_Identifier (N);
2798 T : Entity_Id;
2799 Act_T : Entity_Id;
2800
2801 E : Node_Id := Expression (N);
2802 -- E is set to Expression (N) throughout this routine. When
2803 -- Expression (N) is modified, E is changed accordingly.
2804
2805 Prev_Entity : Entity_Id := Empty;
2806
2807 function Count_Tasks (T : Entity_Id) return Uint;
2808 -- This function is called when a non-generic library level object of a
2809 -- task type is declared. Its function is to count the static number of
2810 -- tasks declared within the type (it is only called if Has_Tasks is set
2811 -- for T). As a side effect, if an array of tasks with non-static bounds
2812 -- or a variant record type is encountered, Check_Restrictions is called
2813 -- indicating the count is unknown.
2814
2815 -----------------
2816 -- Count_Tasks --
2817 -----------------
2818
2819 function Count_Tasks (T : Entity_Id) return Uint is
2820 C : Entity_Id;
2821 X : Node_Id;
2822 V : Uint;
2823
2824 begin
2825 if Is_Task_Type (T) then
2826 return Uint_1;
2827
2828 elsif Is_Record_Type (T) then
2829 if Has_Discriminants (T) then
2830 Check_Restriction (Max_Tasks, N);
2831 return Uint_0;
2832
2833 else
2834 V := Uint_0;
2835 C := First_Component (T);
2836 while Present (C) loop
2837 V := V + Count_Tasks (Etype (C));
2838 Next_Component (C);
2839 end loop;
2840
2841 return V;
2842 end if;
2843
2844 elsif Is_Array_Type (T) then
2845 X := First_Index (T);
2846 V := Count_Tasks (Component_Type (T));
2847 while Present (X) loop
2848 C := Etype (X);
2849
2850 if not Is_Static_Subtype (C) then
2851 Check_Restriction (Max_Tasks, N);
2852 return Uint_0;
2853 else
2854 V := V * (UI_Max (Uint_0,
2855 Expr_Value (Type_High_Bound (C)) -
2856 Expr_Value (Type_Low_Bound (C)) + Uint_1));
2857 end if;
2858
2859 Next_Index (X);
2860 end loop;
2861
2862 return V;
2863
2864 else
2865 return Uint_0;
2866 end if;
2867 end Count_Tasks;
2868
2869 -- Start of processing for Analyze_Object_Declaration
2870
2871 begin
2872 -- There are three kinds of implicit types generated by an
2873 -- object declaration:
2874
2875 -- 1. Those generated by the original Object Definition
2876
2877 -- 2. Those generated by the Expression
2878
2879 -- 3. Those used to constrain the Object Definition with the
2880 -- expression constraints when the definition is unconstrained.
2881
2882 -- They must be generated in this order to avoid order of elaboration
2883 -- issues. Thus the first step (after entering the name) is to analyze
2884 -- the object definition.
2885
2886 if Constant_Present (N) then
2887 Prev_Entity := Current_Entity_In_Scope (Id);
2888
2889 if Present (Prev_Entity)
2890 and then
2891
2892 -- If the homograph is an implicit subprogram, it is overridden
2893 -- by the current declaration.
2894
2895 ((Is_Overloadable (Prev_Entity)
2896 and then Is_Inherited_Operation (Prev_Entity))
2897
2898 -- The current object is a discriminal generated for an entry
2899 -- family index. Even though the index is a constant, in this
2900 -- particular context there is no true constant redeclaration.
2901 -- Enter_Name will handle the visibility.
2902
2903 or else
2904 (Is_Discriminal (Id)
2905 and then Ekind (Discriminal_Link (Id)) =
2906 E_Entry_Index_Parameter)
2907
2908 -- The current object is the renaming for a generic declared
2909 -- within the instance.
2910
2911 or else
2912 (Ekind (Prev_Entity) = E_Package
2913 and then Nkind (Parent (Prev_Entity)) =
2914 N_Package_Renaming_Declaration
2915 and then not Comes_From_Source (Prev_Entity)
2916 and then Is_Generic_Instance (Renamed_Entity (Prev_Entity))))
2917 then
2918 Prev_Entity := Empty;
2919 end if;
2920 end if;
2921
2922 if Present (Prev_Entity) then
2923 Constant_Redeclaration (Id, N, T);
2924
2925 Generate_Reference (Prev_Entity, Id, 'c');
2926 Set_Completion_Referenced (Id);
2927
2928 if Error_Posted (N) then
2929
2930 -- Type mismatch or illegal redeclaration, Do not analyze
2931 -- expression to avoid cascaded errors.
2932
2933 T := Find_Type_Of_Object (Object_Definition (N), N);
2934 Set_Etype (Id, T);
2935 Set_Ekind (Id, E_Variable);
2936 goto Leave;
2937 end if;
2938
2939 -- In the normal case, enter identifier at the start to catch premature
2940 -- usage in the initialization expression.
2941
2942 else
2943 Generate_Definition (Id);
2944 Enter_Name (Id);
2945
2946 Mark_Coextensions (N, Object_Definition (N));
2947
2948 T := Find_Type_Of_Object (Object_Definition (N), N);
2949
2950 if Nkind (Object_Definition (N)) = N_Access_Definition
2951 and then Present
2952 (Access_To_Subprogram_Definition (Object_Definition (N)))
2953 and then Protected_Present
2954 (Access_To_Subprogram_Definition (Object_Definition (N)))
2955 then
2956 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
2957 end if;
2958
2959 if Error_Posted (Id) then
2960 Set_Etype (Id, T);
2961 Set_Ekind (Id, E_Variable);
2962 goto Leave;
2963 end if;
2964 end if;
2965
2966 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
2967 -- out some static checks
2968
2969 if Ada_Version >= Ada_2005
2970 and then Can_Never_Be_Null (T)
2971 then
2972 -- In case of aggregates we must also take care of the correct
2973 -- initialization of nested aggregates bug this is done at the
2974 -- point of the analysis of the aggregate (see sem_aggr.adb)
2975
2976 if Present (Expression (N))
2977 and then Nkind (Expression (N)) = N_Aggregate
2978 then
2979 null;
2980
2981 else
2982 declare
2983 Save_Typ : constant Entity_Id := Etype (Id);
2984 begin
2985 Set_Etype (Id, T); -- Temp. decoration for static checks
2986 Null_Exclusion_Static_Checks (N);
2987 Set_Etype (Id, Save_Typ);
2988 end;
2989 end if;
2990 end if;
2991
2992 Set_Is_Pure (Id, Is_Pure (Current_Scope));
2993
2994 -- If deferred constant, make sure context is appropriate. We detect
2995 -- a deferred constant as a constant declaration with no expression.
2996 -- A deferred constant can appear in a package body if its completion
2997 -- is by means of an interface pragma.
2998
2999 if Constant_Present (N)
3000 and then No (E)
3001 then
3002 -- A deferred constant may appear in the declarative part of the
3003 -- following constructs:
3004
3005 -- blocks
3006 -- entry bodies
3007 -- extended return statements
3008 -- package specs
3009 -- package bodies
3010 -- subprogram bodies
3011 -- task bodies
3012
3013 -- When declared inside a package spec, a deferred constant must be
3014 -- completed by a full constant declaration or pragma Import. In all
3015 -- other cases, the only proper completion is pragma Import. Extended
3016 -- return statements are flagged as invalid contexts because they do
3017 -- not have a declarative part and so cannot accommodate the pragma.
3018
3019 if Ekind (Current_Scope) = E_Return_Statement then
3020 Error_Msg_N
3021 ("invalid context for deferred constant declaration (RM 7.4)",
3022 N);
3023 Error_Msg_N
3024 ("\declaration requires an initialization expression",
3025 N);
3026 Set_Constant_Present (N, False);
3027
3028 -- In Ada 83, deferred constant must be of private type
3029
3030 elsif not Is_Private_Type (T) then
3031 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
3032 Error_Msg_N
3033 ("(Ada 83) deferred constant must be private type", N);
3034 end if;
3035 end if;
3036
3037 -- If not a deferred constant, then object declaration freezes its type
3038
3039 else
3040 Check_Fully_Declared (T, N);
3041 Freeze_Before (N, T);
3042 end if;
3043
3044 -- If the object was created by a constrained array definition, then
3045 -- set the link in both the anonymous base type and anonymous subtype
3046 -- that are built to represent the array type to point to the object.
3047
3048 if Nkind (Object_Definition (Declaration_Node (Id))) =
3049 N_Constrained_Array_Definition
3050 then
3051 Set_Related_Array_Object (T, Id);
3052 Set_Related_Array_Object (Base_Type (T), Id);
3053 end if;
3054
3055 -- Special checks for protected objects not at library level
3056
3057 if Is_Protected_Type (T)
3058 and then not Is_Library_Level_Entity (Id)
3059 then
3060 Check_Restriction (No_Local_Protected_Objects, Id);
3061
3062 -- Protected objects with interrupt handlers must be at library level
3063
3064 -- Ada 2005: this test is not needed (and the corresponding clause
3065 -- in the RM is removed) because accessibility checks are sufficient
3066 -- to make handlers not at the library level illegal.
3067
3068 if Has_Interrupt_Handler (T)
3069 and then Ada_Version < Ada_2005
3070 then
3071 Error_Msg_N
3072 ("interrupt object can only be declared at library level", Id);
3073 end if;
3074 end if;
3075
3076 -- The actual subtype of the object is the nominal subtype, unless
3077 -- the nominal one is unconstrained and obtained from the expression.
3078
3079 Act_T := T;
3080
3081 -- These checks should be performed before the initialization expression
3082 -- is considered, so that the Object_Definition node is still the same
3083 -- as in source code.
3084
3085 -- In SPARK, the nominal subtype shall be given by a subtype mark and
3086 -- shall not be unconstrained. (The only exception to this is the
3087 -- admission of declarations of constants of type String.)
3088
3089 if not
3090 Nkind_In (Object_Definition (N), N_Identifier, N_Expanded_Name)
3091 then
3092 Check_SPARK_Restriction
3093 ("subtype mark required", Object_Definition (N));
3094
3095 elsif Is_Array_Type (T)
3096 and then not Is_Constrained (T)
3097 and then T /= Standard_String
3098 then
3099 Check_SPARK_Restriction
3100 ("subtype mark of constrained type expected",
3101 Object_Definition (N));
3102 end if;
3103
3104 -- There are no aliased objects in SPARK
3105
3106 if Aliased_Present (N) then
3107 Check_SPARK_Restriction ("aliased object is not allowed", N);
3108 end if;
3109
3110 -- Process initialization expression if present and not in error
3111
3112 if Present (E) and then E /= Error then
3113
3114 -- Generate an error in case of CPP class-wide object initialization.
3115 -- Required because otherwise the expansion of the class-wide
3116 -- assignment would try to use 'size to initialize the object
3117 -- (primitive that is not available in CPP tagged types).
3118
3119 if Is_Class_Wide_Type (Act_T)
3120 and then
3121 (Is_CPP_Class (Root_Type (Etype (Act_T)))
3122 or else
3123 (Present (Full_View (Root_Type (Etype (Act_T))))
3124 and then
3125 Is_CPP_Class (Full_View (Root_Type (Etype (Act_T))))))
3126 then
3127 Error_Msg_N
3128 ("predefined assignment not available for 'C'P'P tagged types",
3129 E);
3130 end if;
3131
3132 Mark_Coextensions (N, E);
3133 Analyze (E);
3134
3135 -- In case of errors detected in the analysis of the expression,
3136 -- decorate it with the expected type to avoid cascaded errors
3137
3138 if No (Etype (E)) then
3139 Set_Etype (E, T);
3140 end if;
3141
3142 -- If an initialization expression is present, then we set the
3143 -- Is_True_Constant flag. It will be reset if this is a variable
3144 -- and it is indeed modified.
3145
3146 Set_Is_True_Constant (Id, True);
3147
3148 -- If we are analyzing a constant declaration, set its completion
3149 -- flag after analyzing and resolving the expression.
3150
3151 if Constant_Present (N) then
3152 Set_Has_Completion (Id);
3153 end if;
3154
3155 -- Set type and resolve (type may be overridden later on)
3156
3157 Set_Etype (Id, T);
3158 Resolve (E, T);
3159
3160 -- If E is null and has been replaced by an N_Raise_Constraint_Error
3161 -- node (which was marked already-analyzed), we need to set the type
3162 -- to something other than Any_Access in order to keep gigi happy.
3163
3164 if Etype (E) = Any_Access then
3165 Set_Etype (E, T);
3166 end if;
3167
3168 -- If the object is an access to variable, the initialization
3169 -- expression cannot be an access to constant.
3170
3171 if Is_Access_Type (T)
3172 and then not Is_Access_Constant (T)
3173 and then Is_Access_Type (Etype (E))
3174 and then Is_Access_Constant (Etype (E))
3175 then
3176 Error_Msg_N
3177 ("access to variable cannot be initialized "
3178 & "with an access-to-constant expression", E);
3179 end if;
3180
3181 if not Assignment_OK (N) then
3182 Check_Initialization (T, E);
3183 end if;
3184
3185 Check_Unset_Reference (E);
3186
3187 -- If this is a variable, then set current value. If this is a
3188 -- declared constant of a scalar type with a static expression,
3189 -- indicate that it is always valid.
3190
3191 if not Constant_Present (N) then
3192 if Compile_Time_Known_Value (E) then
3193 Set_Current_Value (Id, E);
3194 end if;
3195
3196 elsif Is_Scalar_Type (T)
3197 and then Is_OK_Static_Expression (E)
3198 then
3199 Set_Is_Known_Valid (Id);
3200 end if;
3201
3202 -- Deal with setting of null flags
3203
3204 if Is_Access_Type (T) then
3205 if Known_Non_Null (E) then
3206 Set_Is_Known_Non_Null (Id, True);
3207 elsif Known_Null (E)
3208 and then not Can_Never_Be_Null (Id)
3209 then
3210 Set_Is_Known_Null (Id, True);
3211 end if;
3212 end if;
3213
3214 -- Check incorrect use of dynamically tagged expressions.
3215
3216 if Is_Tagged_Type (T) then
3217 Check_Dynamically_Tagged_Expression
3218 (Expr => E,
3219 Typ => T,
3220 Related_Nod => N);
3221 end if;
3222
3223 Apply_Scalar_Range_Check (E, T);
3224 Apply_Static_Length_Check (E, T);
3225
3226 if Nkind (Original_Node (N)) = N_Object_Declaration
3227 and then Comes_From_Source (Original_Node (N))
3228
3229 -- Only call test if needed
3230
3231 and then Restriction_Check_Required (SPARK)
3232 and then not Is_SPARK_Initialization_Expr (E)
3233 then
3234 Check_SPARK_Restriction
3235 ("initialization expression is not appropriate", E);
3236 end if;
3237 end if;
3238
3239 -- If the No_Streams restriction is set, check that the type of the
3240 -- object is not, and does not contain, any subtype derived from
3241 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
3242 -- Has_Stream just for efficiency reasons. There is no point in
3243 -- spending time on a Has_Stream check if the restriction is not set.
3244
3245 if Restriction_Check_Required (No_Streams) then
3246 if Has_Stream (T) then
3247 Check_Restriction (No_Streams, N);
3248 end if;
3249 end if;
3250
3251 -- Deal with predicate check before we start to do major rewriting.
3252 -- it is OK to initialize and then check the initialized value, since
3253 -- the object goes out of scope if we get a predicate failure. Note
3254 -- that we do this in the analyzer and not the expander because the
3255 -- analyzer does some substantial rewriting in some cases.
3256
3257 -- We need a predicate check if the type has predicates, and if either
3258 -- there is an initializing expression, or for default initialization
3259 -- when we have at least one case of an explicit default initial value.
3260
3261 if not Suppress_Assignment_Checks (N)
3262 and then Present (Predicate_Function (T))
3263 and then
3264 (Present (E)
3265 or else
3266 Is_Partially_Initialized_Type (T, Include_Implicit => False))
3267 then
3268 Insert_After (N,
3269 Make_Predicate_Check (T, New_Occurrence_Of (Id, Loc)));
3270 end if;
3271
3272 -- Case of unconstrained type
3273
3274 if Is_Indefinite_Subtype (T) then
3275
3276 -- In SPARK, a declaration of unconstrained type is allowed
3277 -- only for constants of type string.
3278
3279 if Is_String_Type (T) and then not Constant_Present (N) then
3280 Check_SPARK_Restriction
3281 ("declaration of object of unconstrained type not allowed",
3282 N);
3283 end if;
3284
3285 -- Nothing to do in deferred constant case
3286
3287 if Constant_Present (N) and then No (E) then
3288 null;
3289
3290 -- Case of no initialization present
3291
3292 elsif No (E) then
3293 if No_Initialization (N) then
3294 null;
3295
3296 elsif Is_Class_Wide_Type (T) then
3297 Error_Msg_N
3298 ("initialization required in class-wide declaration ", N);
3299
3300 else
3301 Error_Msg_N
3302 ("unconstrained subtype not allowed (need initialization)",
3303 Object_Definition (N));
3304
3305 if Is_Record_Type (T) and then Has_Discriminants (T) then
3306 Error_Msg_N
3307 ("\provide initial value or explicit discriminant values",
3308 Object_Definition (N));
3309
3310 Error_Msg_NE
3311 ("\or give default discriminant values for type&",
3312 Object_Definition (N), T);
3313
3314 elsif Is_Array_Type (T) then
3315 Error_Msg_N
3316 ("\provide initial value or explicit array bounds",
3317 Object_Definition (N));
3318 end if;
3319 end if;
3320
3321 -- Case of initialization present but in error. Set initial
3322 -- expression as absent (but do not make above complaints)
3323
3324 elsif E = Error then
3325 Set_Expression (N, Empty);
3326 E := Empty;
3327
3328 -- Case of initialization present
3329
3330 else
3331 -- Check restrictions in Ada 83
3332
3333 if not Constant_Present (N) then
3334
3335 -- Unconstrained variables not allowed in Ada 83 mode
3336
3337 if Ada_Version = Ada_83
3338 and then Comes_From_Source (Object_Definition (N))
3339 then
3340 Error_Msg_N
3341 ("(Ada 83) unconstrained variable not allowed",
3342 Object_Definition (N));
3343 end if;
3344 end if;
3345
3346 -- Now we constrain the variable from the initializing expression
3347
3348 -- If the expression is an aggregate, it has been expanded into
3349 -- individual assignments. Retrieve the actual type from the
3350 -- expanded construct.
3351
3352 if Is_Array_Type (T)
3353 and then No_Initialization (N)
3354 and then Nkind (Original_Node (E)) = N_Aggregate
3355 then
3356 Act_T := Etype (E);
3357
3358 -- In case of class-wide interface object declarations we delay
3359 -- the generation of the equivalent record type declarations until
3360 -- its expansion because there are cases in they are not required.
3361
3362 elsif Is_Interface (T) then
3363 null;
3364
3365 else
3366 Expand_Subtype_From_Expr (N, T, Object_Definition (N), E);
3367 Act_T := Find_Type_Of_Object (Object_Definition (N), N);
3368 end if;
3369
3370 Set_Is_Constr_Subt_For_U_Nominal (Act_T);
3371
3372 if Aliased_Present (N) then
3373 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
3374 end if;
3375
3376 Freeze_Before (N, Act_T);
3377 Freeze_Before (N, T);
3378 end if;
3379
3380 elsif Is_Array_Type (T)
3381 and then No_Initialization (N)
3382 and then Nkind (Original_Node (E)) = N_Aggregate
3383 then
3384 if not Is_Entity_Name (Object_Definition (N)) then
3385 Act_T := Etype (E);
3386 Check_Compile_Time_Size (Act_T);
3387
3388 if Aliased_Present (N) then
3389 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
3390 end if;
3391 end if;
3392
3393 -- When the given object definition and the aggregate are specified
3394 -- independently, and their lengths might differ do a length check.
3395 -- This cannot happen if the aggregate is of the form (others =>...)
3396
3397 if not Is_Constrained (T) then
3398 null;
3399
3400 elsif Nkind (E) = N_Raise_Constraint_Error then
3401
3402 -- Aggregate is statically illegal. Place back in declaration
3403
3404 Set_Expression (N, E);
3405 Set_No_Initialization (N, False);
3406
3407 elsif T = Etype (E) then
3408 null;
3409
3410 elsif Nkind (E) = N_Aggregate
3411 and then Present (Component_Associations (E))
3412 and then Present (Choices (First (Component_Associations (E))))
3413 and then Nkind (First
3414 (Choices (First (Component_Associations (E))))) = N_Others_Choice
3415 then
3416 null;
3417
3418 else
3419 Apply_Length_Check (E, T);
3420 end if;
3421
3422 -- If the type is limited unconstrained with defaulted discriminants and
3423 -- there is no expression, then the object is constrained by the
3424 -- defaults, so it is worthwhile building the corresponding subtype.
3425
3426 elsif (Is_Limited_Record (T) or else Is_Concurrent_Type (T))
3427 and then not Is_Constrained (T)
3428 and then Has_Discriminants (T)
3429 then
3430 if No (E) then
3431 Act_T := Build_Default_Subtype (T, N);
3432 else
3433 -- Ada 2005: a limited object may be initialized by means of an
3434 -- aggregate. If the type has default discriminants it has an
3435 -- unconstrained nominal type, Its actual subtype will be obtained
3436 -- from the aggregate, and not from the default discriminants.
3437
3438 Act_T := Etype (E);
3439 end if;
3440
3441 Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
3442
3443 elsif Present (Underlying_Type (T))
3444 and then not Is_Constrained (Underlying_Type (T))
3445 and then Has_Discriminants (Underlying_Type (T))
3446 and then Nkind (E) = N_Function_Call
3447 and then Constant_Present (N)
3448 then
3449 -- The back-end has problems with constants of a discriminated type
3450 -- with defaults, if the initial value is a function call. We
3451 -- generate an intermediate temporary for the result of the call.
3452 -- It is unclear why this should make it acceptable to gcc. ???
3453
3454 Remove_Side_Effects (E);
3455
3456 -- If this is a constant declaration of an unconstrained type and
3457 -- the initialization is an aggregate, we can use the subtype of the
3458 -- aggregate for the declared entity because it is immutable.
3459
3460 elsif not Is_Constrained (T)
3461 and then Has_Discriminants (T)
3462 and then Constant_Present (N)
3463 and then not Has_Unchecked_Union (T)
3464 and then Nkind (E) = N_Aggregate
3465 then
3466 Act_T := Etype (E);
3467 end if;
3468
3469 -- Check No_Wide_Characters restriction
3470
3471 Check_Wide_Character_Restriction (T, Object_Definition (N));
3472
3473 -- Indicate this is not set in source. Certainly true for constants, and
3474 -- true for variables so far (will be reset for a variable if and when
3475 -- we encounter a modification in the source).
3476
3477 Set_Never_Set_In_Source (Id, True);
3478
3479 -- Now establish the proper kind and type of the object
3480
3481 if Constant_Present (N) then
3482 Set_Ekind (Id, E_Constant);
3483 Set_Is_True_Constant (Id, True);
3484
3485 else
3486 Set_Ekind (Id, E_Variable);
3487
3488 -- A variable is set as shared passive if it appears in a shared
3489 -- passive package, and is at the outer level. This is not done for
3490 -- entities generated during expansion, because those are always
3491 -- manipulated locally.
3492
3493 if Is_Shared_Passive (Current_Scope)
3494 and then Is_Library_Level_Entity (Id)
3495 and then Comes_From_Source (Id)
3496 then
3497 Set_Is_Shared_Passive (Id);
3498 Check_Shared_Var (Id, T, N);
3499 end if;
3500
3501 -- Set Has_Initial_Value if initializing expression present. Note
3502 -- that if there is no initializing expression, we leave the state
3503 -- of this flag unchanged (usually it will be False, but notably in
3504 -- the case of exception choice variables, it will already be true).
3505
3506 if Present (E) then
3507 Set_Has_Initial_Value (Id, True);
3508 end if;
3509 end if;
3510
3511 -- Initialize alignment and size and capture alignment setting
3512
3513 Init_Alignment (Id);
3514 Init_Esize (Id);
3515 Set_Optimize_Alignment_Flags (Id);
3516
3517 -- Deal with aliased case
3518
3519 if Aliased_Present (N) then
3520 Set_Is_Aliased (Id);
3521
3522 -- If the object is aliased and the type is unconstrained with
3523 -- defaulted discriminants and there is no expression, then the
3524 -- object is constrained by the defaults, so it is worthwhile
3525 -- building the corresponding subtype.
3526
3527 -- Ada 2005 (AI-363): If the aliased object is discriminated and
3528 -- unconstrained, then only establish an actual subtype if the
3529 -- nominal subtype is indefinite. In definite cases the object is
3530 -- unconstrained in Ada 2005.
3531
3532 if No (E)
3533 and then Is_Record_Type (T)
3534 and then not Is_Constrained (T)
3535 and then Has_Discriminants (T)
3536 and then (Ada_Version < Ada_2005 or else Is_Indefinite_Subtype (T))
3537 then
3538 Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
3539 end if;
3540 end if;
3541
3542 -- Now we can set the type of the object
3543
3544 Set_Etype (Id, Act_T);
3545
3546 -- Deal with controlled types
3547
3548 if Has_Controlled_Component (Etype (Id))
3549 or else Is_Controlled (Etype (Id))
3550 then
3551 if not Is_Library_Level_Entity (Id) then
3552 Check_Restriction (No_Nested_Finalization, N);
3553 else
3554 Validate_Controlled_Object (Id);
3555 end if;
3556
3557 -- Generate a warning when an initialization causes an obvious ABE
3558 -- violation. If the init expression is a simple aggregate there
3559 -- shouldn't be any initialize/adjust call generated. This will be
3560 -- true as soon as aggregates are built in place when possible.
3561
3562 -- ??? at the moment we do not generate warnings for temporaries
3563 -- created for those aggregates although Program_Error might be
3564 -- generated if compiled with -gnato.
3565
3566 if Is_Controlled (Etype (Id))
3567 and then Comes_From_Source (Id)
3568 then
3569 declare
3570 BT : constant Entity_Id := Base_Type (Etype (Id));
3571
3572 Implicit_Call : Entity_Id;
3573 pragma Warnings (Off, Implicit_Call);
3574 -- ??? what is this for (never referenced!)
3575
3576 function Is_Aggr (N : Node_Id) return Boolean;
3577 -- Check that N is an aggregate
3578
3579 -------------
3580 -- Is_Aggr --
3581 -------------
3582
3583 function Is_Aggr (N : Node_Id) return Boolean is
3584 begin
3585 case Nkind (Original_Node (N)) is
3586 when N_Aggregate | N_Extension_Aggregate =>
3587 return True;
3588
3589 when N_Qualified_Expression |
3590 N_Type_Conversion |
3591 N_Unchecked_Type_Conversion =>
3592 return Is_Aggr (Expression (Original_Node (N)));
3593
3594 when others =>
3595 return False;
3596 end case;
3597 end Is_Aggr;
3598
3599 begin
3600 -- If no underlying type, we already are in an error situation.
3601 -- Do not try to add a warning since we do not have access to
3602 -- prim-op list.
3603
3604 if No (Underlying_Type (BT)) then
3605 Implicit_Call := Empty;
3606
3607 -- A generic type does not have usable primitive operators.
3608 -- Initialization calls are built for instances.
3609
3610 elsif Is_Generic_Type (BT) then
3611 Implicit_Call := Empty;
3612
3613 -- If the init expression is not an aggregate, an adjust call
3614 -- will be generated
3615
3616 elsif Present (E) and then not Is_Aggr (E) then
3617 Implicit_Call := Find_Prim_Op (BT, Name_Adjust);
3618
3619 -- If no init expression and we are not in the deferred
3620 -- constant case, an Initialize call will be generated
3621
3622 elsif No (E) and then not Constant_Present (N) then
3623 Implicit_Call := Find_Prim_Op (BT, Name_Initialize);
3624
3625 else
3626 Implicit_Call := Empty;
3627 end if;
3628 end;
3629 end if;
3630 end if;
3631
3632 if Has_Task (Etype (Id)) then
3633 Check_Restriction (No_Tasking, N);
3634
3635 -- Deal with counting max tasks
3636
3637 -- Nothing to do if inside a generic
3638
3639 if Inside_A_Generic then
3640 null;
3641
3642 -- If library level entity, then count tasks
3643
3644 elsif Is_Library_Level_Entity (Id) then
3645 Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
3646
3647 -- If not library level entity, then indicate we don't know max
3648 -- tasks and also check task hierarchy restriction and blocking
3649 -- operation (since starting a task is definitely blocking!)
3650
3651 else
3652 Check_Restriction (Max_Tasks, N);
3653 Check_Restriction (No_Task_Hierarchy, N);
3654 Check_Potentially_Blocking_Operation (N);
3655 end if;
3656
3657 -- A rather specialized test. If we see two tasks being declared
3658 -- of the same type in the same object declaration, and the task
3659 -- has an entry with an address clause, we know that program error
3660 -- will be raised at run time since we can't have two tasks with
3661 -- entries at the same address.
3662
3663 if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
3664 declare
3665 E : Entity_Id;
3666
3667 begin
3668 E := First_Entity (Etype (Id));
3669 while Present (E) loop
3670 if Ekind (E) = E_Entry
3671 and then Present (Get_Attribute_Definition_Clause
3672 (E, Attribute_Address))
3673 then
3674 Error_Msg_N
3675 ("?more than one task with same entry address", N);
3676 Error_Msg_N
3677 ("\?Program_Error will be raised at run time", N);
3678 Insert_Action (N,
3679 Make_Raise_Program_Error (Loc,
3680 Reason => PE_Duplicated_Entry_Address));
3681 exit;
3682 end if;
3683
3684 Next_Entity (E);
3685 end loop;
3686 end;
3687 end if;
3688 end if;
3689
3690 -- Some simple constant-propagation: if the expression is a constant
3691 -- string initialized with a literal, share the literal. This avoids
3692 -- a run-time copy.
3693
3694 if Present (E)
3695 and then Is_Entity_Name (E)
3696 and then Ekind (Entity (E)) = E_Constant
3697 and then Base_Type (Etype (E)) = Standard_String
3698 then
3699 declare
3700 Val : constant Node_Id := Constant_Value (Entity (E));
3701 begin
3702 if Present (Val)
3703 and then Nkind (Val) = N_String_Literal
3704 then
3705 Rewrite (E, New_Copy (Val));
3706 end if;
3707 end;
3708 end if;
3709
3710 -- Another optimization: if the nominal subtype is unconstrained and
3711 -- the expression is a function call that returns an unconstrained
3712 -- type, rewrite the declaration as a renaming of the result of the
3713 -- call. The exceptions below are cases where the copy is expected,
3714 -- either by the back end (Aliased case) or by the semantics, as for
3715 -- initializing controlled types or copying tags for classwide types.
3716
3717 if Present (E)
3718 and then Nkind (E) = N_Explicit_Dereference
3719 and then Nkind (Original_Node (E)) = N_Function_Call
3720 and then not Is_Library_Level_Entity (Id)
3721 and then not Is_Constrained (Underlying_Type (T))
3722 and then not Is_Aliased (Id)
3723 and then not Is_Class_Wide_Type (T)
3724 and then not Is_Controlled (T)
3725 and then not Has_Controlled_Component (Base_Type (T))
3726 and then Expander_Active
3727 then
3728 Rewrite (N,
3729 Make_Object_Renaming_Declaration (Loc,
3730 Defining_Identifier => Id,
3731 Access_Definition => Empty,
3732 Subtype_Mark => New_Occurrence_Of
3733 (Base_Type (Etype (Id)), Loc),
3734 Name => E));
3735
3736 Set_Renamed_Object (Id, E);
3737
3738 -- Force generation of debugging information for the constant and for
3739 -- the renamed function call.
3740
3741 Set_Debug_Info_Needed (Id);
3742 Set_Debug_Info_Needed (Entity (Prefix (E)));
3743 end if;
3744
3745 if Present (Prev_Entity)
3746 and then Is_Frozen (Prev_Entity)
3747 and then not Error_Posted (Id)
3748 then
3749 Error_Msg_N ("full constant declaration appears too late", N);
3750 end if;
3751
3752 Check_Eliminated (Id);
3753
3754 -- Deal with setting In_Private_Part flag if in private part
3755
3756 if Ekind (Scope (Id)) = E_Package
3757 and then In_Private_Part (Scope (Id))
3758 then
3759 Set_In_Private_Part (Id);
3760 end if;
3761
3762 -- Check for violation of No_Local_Timing_Events
3763
3764 if Restriction_Check_Required (No_Local_Timing_Events)
3765 and then not Is_Library_Level_Entity (Id)
3766 and then Is_RTE (Etype (Id), RE_Timing_Event)
3767 then
3768 Check_Restriction (No_Local_Timing_Events, N);
3769 end if;
3770
3771 <<Leave>>
3772 if Has_Aspects (N) then
3773 Analyze_Aspect_Specifications (N, Id);
3774 end if;
3775 end Analyze_Object_Declaration;
3776
3777 ---------------------------
3778 -- Analyze_Others_Choice --
3779 ---------------------------
3780
3781 -- Nothing to do for the others choice node itself, the semantic analysis
3782 -- of the others choice will occur as part of the processing of the parent
3783
3784 procedure Analyze_Others_Choice (N : Node_Id) is
3785 pragma Warnings (Off, N);
3786 begin
3787 null;
3788 end Analyze_Others_Choice;
3789
3790 -------------------------------------------
3791 -- Analyze_Private_Extension_Declaration --
3792 -------------------------------------------
3793
3794 procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
3795 T : constant Entity_Id := Defining_Identifier (N);
3796 Indic : constant Node_Id := Subtype_Indication (N);
3797 Parent_Type : Entity_Id;
3798 Parent_Base : Entity_Id;
3799
3800 begin
3801 -- Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
3802
3803 if Is_Non_Empty_List (Interface_List (N)) then
3804 declare
3805 Intf : Node_Id;
3806 T : Entity_Id;
3807
3808 begin
3809 Intf := First (Interface_List (N));
3810 while Present (Intf) loop
3811 T := Find_Type_Of_Subtype_Indic (Intf);
3812
3813 Diagnose_Interface (Intf, T);
3814 Next (Intf);
3815 end loop;
3816 end;
3817 end if;
3818
3819 Generate_Definition (T);
3820
3821 -- For other than Ada 2012, just enter the name in the current scope
3822
3823 if Ada_Version < Ada_2012 then
3824 Enter_Name (T);
3825
3826 -- Ada 2012 (AI05-0162): Enter the name in the current scope handling
3827 -- case of private type that completes an incomplete type.
3828
3829 else
3830 declare
3831 Prev : Entity_Id;
3832
3833 begin
3834 Prev := Find_Type_Name (N);
3835
3836 pragma Assert (Prev = T
3837 or else (Ekind (Prev) = E_Incomplete_Type
3838 and then Present (Full_View (Prev))
3839 and then Full_View (Prev) = T));
3840 end;
3841 end if;
3842
3843 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
3844 Parent_Base := Base_Type (Parent_Type);
3845
3846 if Parent_Type = Any_Type
3847 or else Etype (Parent_Type) = Any_Type
3848 then
3849 Set_Ekind (T, Ekind (Parent_Type));
3850 Set_Etype (T, Any_Type);
3851 goto Leave;
3852
3853 elsif not Is_Tagged_Type (Parent_Type) then
3854 Error_Msg_N
3855 ("parent of type extension must be a tagged type ", Indic);
3856 goto Leave;
3857
3858 elsif Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
3859 Error_Msg_N ("premature derivation of incomplete type", Indic);
3860 goto Leave;
3861
3862 elsif Is_Concurrent_Type (Parent_Type) then
3863 Error_Msg_N
3864 ("parent type of a private extension cannot be "
3865 & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
3866
3867 Set_Etype (T, Any_Type);
3868 Set_Ekind (T, E_Limited_Private_Type);
3869 Set_Private_Dependents (T, New_Elmt_List);
3870 Set_Error_Posted (T);
3871 goto Leave;
3872 end if;
3873
3874 -- Perhaps the parent type should be changed to the class-wide type's
3875 -- specific type in this case to prevent cascading errors ???
3876
3877 if Is_Class_Wide_Type (Parent_Type) then
3878 Error_Msg_N
3879 ("parent of type extension must not be a class-wide type", Indic);
3880 goto Leave;
3881 end if;
3882
3883 if (not Is_Package_Or_Generic_Package (Current_Scope)
3884 and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
3885 or else In_Private_Part (Current_Scope)
3886
3887 then
3888 Error_Msg_N ("invalid context for private extension", N);
3889 end if;
3890
3891 -- Set common attributes
3892
3893 Set_Is_Pure (T, Is_Pure (Current_Scope));
3894 Set_Scope (T, Current_Scope);
3895 Set_Ekind (T, E_Record_Type_With_Private);
3896 Init_Size_Align (T);
3897
3898 Set_Etype (T, Parent_Base);
3899 Set_Has_Task (T, Has_Task (Parent_Base));
3900
3901 Set_Convention (T, Convention (Parent_Type));
3902 Set_First_Rep_Item (T, First_Rep_Item (Parent_Type));
3903 Set_Is_First_Subtype (T);
3904 Make_Class_Wide_Type (T);
3905
3906 if Unknown_Discriminants_Present (N) then
3907 Set_Discriminant_Constraint (T, No_Elist);
3908 end if;
3909
3910 Build_Derived_Record_Type (N, Parent_Type, T);
3911
3912 -- Propagate inherited invariant information. The new type has
3913 -- invariants, if the parent type has inheritable invariants,
3914 -- and these invariants can in turn be inherited.
3915
3916 if Has_Inheritable_Invariants (Parent_Type) then
3917 Set_Has_Inheritable_Invariants (T);
3918 Set_Has_Invariants (T);
3919 end if;
3920
3921 -- Ada 2005 (AI-443): Synchronized private extension or a rewritten
3922 -- synchronized formal derived type.
3923
3924 if Ada_Version >= Ada_2005
3925 and then Synchronized_Present (N)
3926 then
3927 Set_Is_Limited_Record (T);
3928
3929 -- Formal derived type case
3930
3931 if Is_Generic_Type (T) then
3932
3933 -- The parent must be a tagged limited type or a synchronized
3934 -- interface.
3935
3936 if (not Is_Tagged_Type (Parent_Type)
3937 or else not Is_Limited_Type (Parent_Type))
3938 and then
3939 (not Is_Interface (Parent_Type)
3940 or else not Is_Synchronized_Interface (Parent_Type))
3941 then
3942 Error_Msg_NE ("parent type of & must be tagged limited " &
3943 "or synchronized", N, T);
3944 end if;
3945
3946 -- The progenitors (if any) must be limited or synchronized
3947 -- interfaces.
3948
3949 if Present (Interfaces (T)) then
3950 declare
3951 Iface : Entity_Id;
3952 Iface_Elmt : Elmt_Id;
3953
3954 begin
3955 Iface_Elmt := First_Elmt (Interfaces (T));
3956 while Present (Iface_Elmt) loop
3957 Iface := Node (Iface_Elmt);
3958
3959 if not Is_Limited_Interface (Iface)
3960 and then not Is_Synchronized_Interface (Iface)
3961 then
3962 Error_Msg_NE ("progenitor & must be limited " &
3963 "or synchronized", N, Iface);
3964 end if;
3965
3966 Next_Elmt (Iface_Elmt);
3967 end loop;
3968 end;
3969 end if;
3970
3971 -- Regular derived extension, the parent must be a limited or
3972 -- synchronized interface.
3973
3974 else
3975 if not Is_Interface (Parent_Type)
3976 or else (not Is_Limited_Interface (Parent_Type)
3977 and then
3978 not Is_Synchronized_Interface (Parent_Type))
3979 then
3980 Error_Msg_NE
3981 ("parent type of & must be limited interface", N, T);
3982 end if;
3983 end if;
3984
3985 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
3986 -- extension with a synchronized parent must be explicitly declared
3987 -- synchronized, because the full view will be a synchronized type.
3988 -- This must be checked before the check for limited types below,
3989 -- to ensure that types declared limited are not allowed to extend
3990 -- synchronized interfaces.
3991
3992 elsif Is_Interface (Parent_Type)
3993 and then Is_Synchronized_Interface (Parent_Type)
3994 and then not Synchronized_Present (N)
3995 then
3996 Error_Msg_NE
3997 ("private extension of& must be explicitly synchronized",
3998 N, Parent_Type);
3999
4000 elsif Limited_Present (N) then
4001 Set_Is_Limited_Record (T);
4002
4003 if not Is_Limited_Type (Parent_Type)
4004 and then
4005 (not Is_Interface (Parent_Type)
4006 or else not Is_Limited_Interface (Parent_Type))
4007 then
4008 Error_Msg_NE ("parent type& of limited extension must be limited",
4009 N, Parent_Type);
4010 end if;
4011 end if;
4012
4013 <<Leave>>
4014 if Has_Aspects (N) then
4015 Analyze_Aspect_Specifications (N, T);
4016 end if;
4017 end Analyze_Private_Extension_Declaration;
4018
4019 ---------------------------------
4020 -- Analyze_Subtype_Declaration --
4021 ---------------------------------
4022
4023 procedure Analyze_Subtype_Declaration
4024 (N : Node_Id;
4025 Skip : Boolean := False)
4026 is
4027 Id : constant Entity_Id := Defining_Identifier (N);
4028 T : Entity_Id;
4029 R_Checks : Check_Result;
4030
4031 begin
4032 Generate_Definition (Id);
4033 Set_Is_Pure (Id, Is_Pure (Current_Scope));
4034 Init_Size_Align (Id);
4035
4036 -- The following guard condition on Enter_Name is to handle cases where
4037 -- the defining identifier has already been entered into the scope but
4038 -- the declaration as a whole needs to be analyzed.
4039
4040 -- This case in particular happens for derived enumeration types. The
4041 -- derived enumeration type is processed as an inserted enumeration type
4042 -- declaration followed by a rewritten subtype declaration. The defining
4043 -- identifier, however, is entered into the name scope very early in the
4044 -- processing of the original type declaration and therefore needs to be
4045 -- avoided here, when the created subtype declaration is analyzed. (See
4046 -- Build_Derived_Types)
4047
4048 -- This also happens when the full view of a private type is derived
4049 -- type with constraints. In this case the entity has been introduced
4050 -- in the private declaration.
4051
4052 if Skip
4053 or else (Present (Etype (Id))
4054 and then (Is_Private_Type (Etype (Id))
4055 or else Is_Task_Type (Etype (Id))
4056 or else Is_Rewrite_Substitution (N)))
4057 then
4058 null;
4059
4060 else
4061 Enter_Name (Id);
4062 end if;
4063
4064 T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
4065
4066 -- Inherit common attributes
4067
4068 Set_Is_Generic_Type (Id, Is_Generic_Type (Base_Type (T)));
4069 Set_Is_Volatile (Id, Is_Volatile (T));
4070 Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
4071 Set_Is_Atomic (Id, Is_Atomic (T));
4072 Set_Is_Ada_2005_Only (Id, Is_Ada_2005_Only (T));
4073 Set_Is_Ada_2012_Only (Id, Is_Ada_2012_Only (T));
4074 Set_Convention (Id, Convention (T));
4075
4076 -- If ancestor has predicates then so does the subtype, and in addition
4077 -- we must delay the freeze to properly arrange predicate inheritance.
4078
4079 -- The Ancestor_Type test is a big kludge, there seem to be cases in
4080 -- which T = ID, so the above tests and assignments do nothing???
4081
4082 if Has_Predicates (T)
4083 or else (Present (Ancestor_Subtype (T))
4084 and then Has_Predicates (Ancestor_Subtype (T)))
4085 then
4086 Set_Has_Predicates (Id);
4087 Set_Has_Delayed_Freeze (Id);
4088 end if;
4089
4090 -- Subtype of Boolean cannot have a constraint in SPARK
4091
4092 if Is_Boolean_Type (T)
4093 and then Nkind (Subtype_Indication (N)) = N_Subtype_Indication
4094 then
4095 Check_SPARK_Restriction
4096 ("subtype of Boolean cannot have constraint", N);
4097 end if;
4098
4099 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
4100 declare
4101 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
4102 One_Cstr : Node_Id;
4103 Low : Node_Id;
4104 High : Node_Id;
4105
4106 begin
4107 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint then
4108 One_Cstr := First (Constraints (Cstr));
4109 while Present (One_Cstr) loop
4110
4111 -- Index or discriminant constraint in SPARK must be a
4112 -- subtype mark.
4113
4114 if not
4115 Nkind_In (One_Cstr, N_Identifier, N_Expanded_Name)
4116 then
4117 Check_SPARK_Restriction
4118 ("subtype mark required", One_Cstr);
4119
4120 -- String subtype must have a lower bound of 1 in SPARK.
4121 -- Note that we do not need to test for the non-static case
4122 -- here, since that was already taken care of in
4123 -- Process_Range_Expr_In_Decl.
4124
4125 elsif Base_Type (T) = Standard_String then
4126 Get_Index_Bounds (One_Cstr, Low, High);
4127
4128 if Is_OK_Static_Expression (Low)
4129 and then Expr_Value (Low) /= 1
4130 then
4131 Check_SPARK_Restriction
4132 ("String subtype must have lower bound of 1", N);
4133 end if;
4134 end if;
4135
4136 Next (One_Cstr);
4137 end loop;
4138 end if;
4139 end;
4140 end if;
4141
4142 -- In the case where there is no constraint given in the subtype
4143 -- indication, Process_Subtype just returns the Subtype_Mark, so its
4144 -- semantic attributes must be established here.
4145
4146 if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
4147 Set_Etype (Id, Base_Type (T));
4148
4149 -- Subtype of unconstrained array without constraint is not allowed
4150 -- in SPARK.
4151
4152 if Is_Array_Type (T)
4153 and then not Is_Constrained (T)
4154 then
4155 Check_SPARK_Restriction
4156 ("subtype of unconstrained array must have constraint", N);
4157 end if;
4158
4159 case Ekind (T) is
4160 when Array_Kind =>
4161 Set_Ekind (Id, E_Array_Subtype);
4162 Copy_Array_Subtype_Attributes (Id, T);
4163
4164 when Decimal_Fixed_Point_Kind =>
4165 Set_Ekind (Id, E_Decimal_Fixed_Point_Subtype);
4166 Set_Digits_Value (Id, Digits_Value (T));
4167 Set_Delta_Value (Id, Delta_Value (T));
4168 Set_Scale_Value (Id, Scale_Value (T));
4169 Set_Small_Value (Id, Small_Value (T));
4170 Set_Scalar_Range (Id, Scalar_Range (T));
4171 Set_Machine_Radix_10 (Id, Machine_Radix_10 (T));
4172 Set_Is_Constrained (Id, Is_Constrained (T));
4173 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4174 Set_RM_Size (Id, RM_Size (T));
4175
4176 when Enumeration_Kind =>
4177 Set_Ekind (Id, E_Enumeration_Subtype);
4178 Set_First_Literal (Id, First_Literal (Base_Type (T)));
4179 Set_Scalar_Range (Id, Scalar_Range (T));
4180 Set_Is_Character_Type (Id, Is_Character_Type (T));
4181 Set_Is_Constrained (Id, Is_Constrained (T));
4182 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4183 Set_RM_Size (Id, RM_Size (T));
4184
4185 when Ordinary_Fixed_Point_Kind =>
4186 Set_Ekind (Id, E_Ordinary_Fixed_Point_Subtype);
4187 Set_Scalar_Range (Id, Scalar_Range (T));
4188 Set_Small_Value (Id, Small_Value (T));
4189 Set_Delta_Value (Id, Delta_Value (T));
4190 Set_Is_Constrained (Id, Is_Constrained (T));
4191 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4192 Set_RM_Size (Id, RM_Size (T));
4193
4194 when Float_Kind =>
4195 Set_Ekind (Id, E_Floating_Point_Subtype);
4196 Set_Scalar_Range (Id, Scalar_Range (T));
4197 Set_Digits_Value (Id, Digits_Value (T));
4198 Set_Is_Constrained (Id, Is_Constrained (T));
4199
4200 when Signed_Integer_Kind =>
4201 Set_Ekind (Id, E_Signed_Integer_Subtype);
4202 Set_Scalar_Range (Id, Scalar_Range (T));
4203 Set_Is_Constrained (Id, Is_Constrained (T));
4204 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4205 Set_RM_Size (Id, RM_Size (T));
4206
4207 when Modular_Integer_Kind =>
4208 Set_Ekind (Id, E_Modular_Integer_Subtype);
4209 Set_Scalar_Range (Id, Scalar_Range (T));
4210 Set_Is_Constrained (Id, Is_Constrained (T));
4211 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4212 Set_RM_Size (Id, RM_Size (T));
4213
4214 when Class_Wide_Kind =>
4215 Set_Ekind (Id, E_Class_Wide_Subtype);
4216 Set_First_Entity (Id, First_Entity (T));
4217 Set_Last_Entity (Id, Last_Entity (T));
4218 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
4219 Set_Cloned_Subtype (Id, T);
4220 Set_Is_Tagged_Type (Id, True);
4221 Set_Has_Unknown_Discriminants
4222 (Id, True);
4223
4224 if Ekind (T) = E_Class_Wide_Subtype then
4225 Set_Equivalent_Type (Id, Equivalent_Type (T));
4226 end if;
4227
4228 when E_Record_Type | E_Record_Subtype =>
4229 Set_Ekind (Id, E_Record_Subtype);
4230
4231 if Ekind (T) = E_Record_Subtype
4232 and then Present (Cloned_Subtype (T))
4233 then
4234 Set_Cloned_Subtype (Id, Cloned_Subtype (T));
4235 else
4236 Set_Cloned_Subtype (Id, T);
4237 end if;
4238
4239 Set_First_Entity (Id, First_Entity (T));
4240 Set_Last_Entity (Id, Last_Entity (T));
4241 Set_Has_Discriminants (Id, Has_Discriminants (T));
4242 Set_Is_Constrained (Id, Is_Constrained (T));
4243 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
4244 Set_Has_Implicit_Dereference
4245 (Id, Has_Implicit_Dereference (T));
4246 Set_Has_Unknown_Discriminants
4247 (Id, Has_Unknown_Discriminants (T));
4248
4249 if Has_Discriminants (T) then
4250 Set_Discriminant_Constraint
4251 (Id, Discriminant_Constraint (T));
4252 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
4253
4254 elsif Has_Unknown_Discriminants (Id) then
4255 Set_Discriminant_Constraint (Id, No_Elist);
4256 end if;
4257
4258 if Is_Tagged_Type (T) then
4259 Set_Is_Tagged_Type (Id);
4260 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
4261 Set_Direct_Primitive_Operations
4262 (Id, Direct_Primitive_Operations (T));
4263 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
4264
4265 if Is_Interface (T) then
4266 Set_Is_Interface (Id);
4267 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
4268 end if;
4269 end if;
4270
4271 when Private_Kind =>
4272 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
4273 Set_Has_Discriminants (Id, Has_Discriminants (T));
4274 Set_Is_Constrained (Id, Is_Constrained (T));
4275 Set_First_Entity (Id, First_Entity (T));
4276 Set_Last_Entity (Id, Last_Entity (T));
4277 Set_Private_Dependents (Id, New_Elmt_List);
4278 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
4279 Set_Has_Implicit_Dereference
4280 (Id, Has_Implicit_Dereference (T));
4281 Set_Has_Unknown_Discriminants
4282 (Id, Has_Unknown_Discriminants (T));
4283 Set_Known_To_Have_Preelab_Init
4284 (Id, Known_To_Have_Preelab_Init (T));
4285
4286 if Is_Tagged_Type (T) then
4287 Set_Is_Tagged_Type (Id);
4288 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
4289 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
4290 Set_Direct_Primitive_Operations (Id,
4291 Direct_Primitive_Operations (T));
4292 end if;
4293
4294 -- In general the attributes of the subtype of a private type
4295 -- are the attributes of the partial view of parent. However,
4296 -- the full view may be a discriminated type, and the subtype
4297 -- must share the discriminant constraint to generate correct
4298 -- calls to initialization procedures.
4299
4300 if Has_Discriminants (T) then
4301 Set_Discriminant_Constraint
4302 (Id, Discriminant_Constraint (T));
4303 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
4304
4305 elsif Present (Full_View (T))
4306 and then Has_Discriminants (Full_View (T))
4307 then
4308 Set_Discriminant_Constraint
4309 (Id, Discriminant_Constraint (Full_View (T)));
4310 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
4311
4312 -- This would seem semantically correct, but apparently
4313 -- confuses the back-end. To be explained and checked with
4314 -- current version ???
4315
4316 -- Set_Has_Discriminants (Id);
4317 end if;
4318
4319 Prepare_Private_Subtype_Completion (Id, N);
4320
4321 when Access_Kind =>
4322 Set_Ekind (Id, E_Access_Subtype);
4323 Set_Is_Constrained (Id, Is_Constrained (T));
4324 Set_Is_Access_Constant
4325 (Id, Is_Access_Constant (T));
4326 Set_Directly_Designated_Type
4327 (Id, Designated_Type (T));
4328 Set_Can_Never_Be_Null (Id, Can_Never_Be_Null (T));
4329
4330 -- A Pure library_item must not contain the declaration of a
4331 -- named access type, except within a subprogram, generic
4332 -- subprogram, task unit, or protected unit, or if it has
4333 -- a specified Storage_Size of zero (RM05-10.2.1(15.4-15.5)).
4334
4335 if Comes_From_Source (Id)
4336 and then In_Pure_Unit
4337 and then not In_Subprogram_Task_Protected_Unit
4338 and then not No_Pool_Assigned (Id)
4339 then
4340 Error_Msg_N
4341 ("named access types not allowed in pure unit", N);
4342 end if;
4343
4344 when Concurrent_Kind =>
4345 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
4346 Set_Corresponding_Record_Type (Id,
4347 Corresponding_Record_Type (T));
4348 Set_First_Entity (Id, First_Entity (T));
4349 Set_First_Private_Entity (Id, First_Private_Entity (T));
4350 Set_Has_Discriminants (Id, Has_Discriminants (T));
4351 Set_Is_Constrained (Id, Is_Constrained (T));
4352 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
4353 Set_Last_Entity (Id, Last_Entity (T));
4354
4355 if Has_Discriminants (T) then
4356 Set_Discriminant_Constraint (Id,
4357 Discriminant_Constraint (T));
4358 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
4359 end if;
4360
4361 when E_Incomplete_Type =>
4362 if Ada_Version >= Ada_2005 then
4363 Set_Ekind (Id, E_Incomplete_Subtype);
4364
4365 -- Ada 2005 (AI-412): Decorate an incomplete subtype
4366 -- of an incomplete type visible through a limited
4367 -- with clause.
4368
4369 if From_With_Type (T)
4370 and then Present (Non_Limited_View (T))
4371 then
4372 Set_From_With_Type (Id);
4373 Set_Non_Limited_View (Id, Non_Limited_View (T));
4374
4375 -- Ada 2005 (AI-412): Add the regular incomplete subtype
4376 -- to the private dependents of the original incomplete
4377 -- type for future transformation.
4378
4379 else
4380 Append_Elmt (Id, Private_Dependents (T));
4381 end if;
4382
4383 -- If the subtype name denotes an incomplete type an error
4384 -- was already reported by Process_Subtype.
4385
4386 else
4387 Set_Etype (Id, Any_Type);
4388 end if;
4389
4390 when others =>
4391 raise Program_Error;
4392 end case;
4393 end if;
4394
4395 if Etype (Id) = Any_Type then
4396 goto Leave;
4397 end if;
4398
4399 -- Some common processing on all types
4400
4401 Set_Size_Info (Id, T);
4402 Set_First_Rep_Item (Id, First_Rep_Item (T));
4403
4404 T := Etype (Id);
4405
4406 Set_Is_Immediately_Visible (Id, True);
4407 Set_Depends_On_Private (Id, Has_Private_Component (T));
4408 Set_Is_Descendent_Of_Address (Id, Is_Descendent_Of_Address (T));
4409
4410 if Is_Interface (T) then
4411 Set_Is_Interface (Id);
4412 end if;
4413
4414 if Present (Generic_Parent_Type (N))
4415 and then
4416 (Nkind
4417 (Parent (Generic_Parent_Type (N))) /= N_Formal_Type_Declaration
4418 or else Nkind
4419 (Formal_Type_Definition (Parent (Generic_Parent_Type (N))))
4420 /= N_Formal_Private_Type_Definition)
4421 then
4422 if Is_Tagged_Type (Id) then
4423
4424 -- If this is a generic actual subtype for a synchronized type,
4425 -- the primitive operations are those of the corresponding record
4426 -- for which there is a separate subtype declaration.
4427
4428 if Is_Concurrent_Type (Id) then
4429 null;
4430 elsif Is_Class_Wide_Type (Id) then
4431 Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
4432 else
4433 Derive_Subprograms (Generic_Parent_Type (N), Id, T);
4434 end if;
4435
4436 elsif Scope (Etype (Id)) /= Standard_Standard then
4437 Derive_Subprograms (Generic_Parent_Type (N), Id);
4438 end if;
4439 end if;
4440
4441 if Is_Private_Type (T)
4442 and then Present (Full_View (T))
4443 then
4444 Conditional_Delay (Id, Full_View (T));
4445
4446 -- The subtypes of components or subcomponents of protected types
4447 -- do not need freeze nodes, which would otherwise appear in the
4448 -- wrong scope (before the freeze node for the protected type). The
4449 -- proper subtypes are those of the subcomponents of the corresponding
4450 -- record.
4451
4452 elsif Ekind (Scope (Id)) /= E_Protected_Type
4453 and then Present (Scope (Scope (Id))) -- error defense!
4454 and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
4455 then
4456 Conditional_Delay (Id, T);
4457 end if;
4458
4459 -- Check that Constraint_Error is raised for a scalar subtype indication
4460 -- when the lower or upper bound of a non-null range lies outside the
4461 -- range of the type mark.
4462
4463 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
4464 if Is_Scalar_Type (Etype (Id))
4465 and then Scalar_Range (Id) /=
4466 Scalar_Range (Etype (Subtype_Mark
4467 (Subtype_Indication (N))))
4468 then
4469 Apply_Range_Check
4470 (Scalar_Range (Id),
4471 Etype (Subtype_Mark (Subtype_Indication (N))));
4472
4473 -- In the array case, check compatibility for each index
4474
4475 elsif Is_Array_Type (Etype (Id))
4476 and then Present (First_Index (Id))
4477 then
4478 -- This really should be a subprogram that finds the indications
4479 -- to check???
4480
4481 declare
4482 Subt_Index : Node_Id := First_Index (Id);
4483 Target_Index : Node_Id :=
4484 First_Index (Etype
4485 (Subtype_Mark (Subtype_Indication (N))));
4486 Has_Dyn_Chk : Boolean := Has_Dynamic_Range_Check (N);
4487
4488 begin
4489 while Present (Subt_Index) loop
4490 if ((Nkind (Subt_Index) = N_Identifier
4491 and then Ekind (Entity (Subt_Index)) in Scalar_Kind)
4492 or else Nkind (Subt_Index) = N_Subtype_Indication)
4493 and then
4494 Nkind (Scalar_Range (Etype (Subt_Index))) = N_Range
4495 then
4496 declare
4497 Target_Typ : constant Entity_Id :=
4498 Etype (Target_Index);
4499 begin
4500 R_Checks :=
4501 Get_Range_Checks
4502 (Scalar_Range (Etype (Subt_Index)),
4503 Target_Typ,
4504 Etype (Subt_Index),
4505 Defining_Identifier (N));
4506
4507 -- Reset Has_Dynamic_Range_Check on the subtype to
4508 -- prevent elision of the index check due to a dynamic
4509 -- check generated for a preceding index (needed since
4510 -- Insert_Range_Checks tries to avoid generating
4511 -- redundant checks on a given declaration).
4512
4513 Set_Has_Dynamic_Range_Check (N, False);
4514
4515 Insert_Range_Checks
4516 (R_Checks,
4517 N,
4518 Target_Typ,
4519 Sloc (Defining_Identifier (N)));
4520
4521 -- Record whether this index involved a dynamic check
4522
4523 Has_Dyn_Chk :=
4524 Has_Dyn_Chk or else Has_Dynamic_Range_Check (N);
4525 end;
4526 end if;
4527
4528 Next_Index (Subt_Index);
4529 Next_Index (Target_Index);
4530 end loop;
4531
4532 -- Finally, mark whether the subtype involves dynamic checks
4533
4534 Set_Has_Dynamic_Range_Check (N, Has_Dyn_Chk);
4535 end;
4536 end if;
4537 end if;
4538
4539 -- Make sure that generic actual types are properly frozen. The subtype
4540 -- is marked as a generic actual type when the enclosing instance is
4541 -- analyzed, so here we identify the subtype from the tree structure.
4542
4543 if Expander_Active
4544 and then Is_Generic_Actual_Type (Id)
4545 and then In_Instance
4546 and then not Comes_From_Source (N)
4547 and then Nkind (Subtype_Indication (N)) /= N_Subtype_Indication
4548 and then Is_Frozen (T)
4549 then
4550 Freeze_Before (N, Id);
4551 end if;
4552
4553 Set_Optimize_Alignment_Flags (Id);
4554 Check_Eliminated (Id);
4555
4556 <<Leave>>
4557 if Has_Aspects (N) then
4558 Analyze_Aspect_Specifications (N, Id);
4559 end if;
4560 end Analyze_Subtype_Declaration;
4561
4562 --------------------------------
4563 -- Analyze_Subtype_Indication --
4564 --------------------------------
4565
4566 procedure Analyze_Subtype_Indication (N : Node_Id) is
4567 T : constant Entity_Id := Subtype_Mark (N);
4568 R : constant Node_Id := Range_Expression (Constraint (N));
4569
4570 begin
4571 Analyze (T);
4572
4573 if R /= Error then
4574 Analyze (R);
4575 Set_Etype (N, Etype (R));
4576 Resolve (R, Entity (T));
4577 else
4578 Set_Error_Posted (R);
4579 Set_Error_Posted (T);
4580 end if;
4581 end Analyze_Subtype_Indication;
4582
4583 --------------------------
4584 -- Analyze_Variant_Part --
4585 --------------------------
4586
4587 procedure Analyze_Variant_Part (N : Node_Id) is
4588
4589 procedure Non_Static_Choice_Error (Choice : Node_Id);
4590 -- Error routine invoked by the generic instantiation below when the
4591 -- variant part has a non static choice.
4592
4593 procedure Process_Declarations (Variant : Node_Id);
4594 -- Analyzes all the declarations associated with a Variant. Needed by
4595 -- the generic instantiation below.
4596
4597 package Variant_Choices_Processing is new
4598 Generic_Choices_Processing
4599 (Get_Alternatives => Variants,
4600 Get_Choices => Discrete_Choices,
4601 Process_Empty_Choice => No_OP,
4602 Process_Non_Static_Choice => Non_Static_Choice_Error,
4603 Process_Associated_Node => Process_Declarations);
4604 use Variant_Choices_Processing;
4605 -- Instantiation of the generic choice processing package
4606
4607 -----------------------------
4608 -- Non_Static_Choice_Error --
4609 -----------------------------
4610
4611 procedure Non_Static_Choice_Error (Choice : Node_Id) is
4612 begin
4613 Flag_Non_Static_Expr
4614 ("choice given in variant part is not static!", Choice);
4615 end Non_Static_Choice_Error;
4616
4617 --------------------------
4618 -- Process_Declarations --
4619 --------------------------
4620
4621 procedure Process_Declarations (Variant : Node_Id) is
4622 begin
4623 if not Null_Present (Component_List (Variant)) then
4624 Analyze_Declarations (Component_Items (Component_List (Variant)));
4625
4626 if Present (Variant_Part (Component_List (Variant))) then
4627 Analyze (Variant_Part (Component_List (Variant)));
4628 end if;
4629 end if;
4630 end Process_Declarations;
4631
4632 -- Local Variables
4633
4634 Discr_Name : Node_Id;
4635 Discr_Type : Entity_Id;
4636
4637 Dont_Care : Boolean;
4638 Others_Present : Boolean := False;
4639
4640 pragma Warnings (Off, Dont_Care);
4641 pragma Warnings (Off, Others_Present);
4642 -- We don't care about the assigned values of any of these
4643
4644 -- Start of processing for Analyze_Variant_Part
4645
4646 begin
4647 Discr_Name := Name (N);
4648 Analyze (Discr_Name);
4649
4650 -- If Discr_Name bad, get out (prevent cascaded errors)
4651
4652 if Etype (Discr_Name) = Any_Type then
4653 return;
4654 end if;
4655
4656 -- Check invalid discriminant in variant part
4657
4658 if Ekind (Entity (Discr_Name)) /= E_Discriminant then
4659 Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
4660 end if;
4661
4662 Discr_Type := Etype (Entity (Discr_Name));
4663
4664 if not Is_Discrete_Type (Discr_Type) then
4665 Error_Msg_N
4666 ("discriminant in a variant part must be of a discrete type",
4667 Name (N));
4668 return;
4669 end if;
4670
4671 -- Call the instantiated Analyze_Choices which does the rest of the work
4672
4673 Analyze_Choices (N, Discr_Type, Dont_Care, Others_Present);
4674 end Analyze_Variant_Part;
4675
4676 ----------------------------
4677 -- Array_Type_Declaration --
4678 ----------------------------
4679
4680 procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
4681 Component_Def : constant Node_Id := Component_Definition (Def);
4682 Component_Typ : constant Node_Id := Subtype_Indication (Component_Def);
4683 Element_Type : Entity_Id;
4684 Implicit_Base : Entity_Id;
4685 Index : Node_Id;
4686 Related_Id : Entity_Id := Empty;
4687 Nb_Index : Nat;
4688 P : constant Node_Id := Parent (Def);
4689 Priv : Entity_Id;
4690
4691 begin
4692 if Nkind (Def) = N_Constrained_Array_Definition then
4693 Index := First (Discrete_Subtype_Definitions (Def));
4694 else
4695 Index := First (Subtype_Marks (Def));
4696 end if;
4697
4698 -- Find proper names for the implicit types which may be public. In case
4699 -- of anonymous arrays we use the name of the first object of that type
4700 -- as prefix.
4701
4702 if No (T) then
4703 Related_Id := Defining_Identifier (P);
4704 else
4705 Related_Id := T;
4706 end if;
4707
4708 Nb_Index := 1;
4709 while Present (Index) loop
4710 Analyze (Index);
4711
4712 if not Nkind_In (Index, N_Identifier, N_Expanded_Name) then
4713 Check_SPARK_Restriction ("subtype mark required", Index);
4714 end if;
4715
4716 -- Add a subtype declaration for each index of private array type
4717 -- declaration whose etype is also private. For example:
4718
4719 -- package Pkg is
4720 -- type Index is private;
4721 -- private
4722 -- type Table is array (Index) of ...
4723 -- end;
4724
4725 -- This is currently required by the expander for the internally
4726 -- generated equality subprogram of records with variant parts in
4727 -- which the etype of some component is such private type.
4728
4729 if Ekind (Current_Scope) = E_Package
4730 and then In_Private_Part (Current_Scope)
4731 and then Has_Private_Declaration (Etype (Index))
4732 then
4733 declare
4734 Loc : constant Source_Ptr := Sloc (Def);
4735 New_E : Entity_Id;
4736 Decl : Entity_Id;
4737
4738 begin
4739 New_E := Make_Temporary (Loc, 'T');
4740 Set_Is_Internal (New_E);
4741
4742 Decl :=
4743 Make_Subtype_Declaration (Loc,
4744 Defining_Identifier => New_E,
4745 Subtype_Indication =>
4746 New_Occurrence_Of (Etype (Index), Loc));
4747
4748 Insert_Before (Parent (Def), Decl);
4749 Analyze (Decl);
4750 Set_Etype (Index, New_E);
4751
4752 -- If the index is a range the Entity attribute is not
4753 -- available. Example:
4754
4755 -- package Pkg is
4756 -- type T is private;
4757 -- private
4758 -- type T is new Natural;
4759 -- Table : array (T(1) .. T(10)) of Boolean;
4760 -- end Pkg;
4761
4762 if Nkind (Index) /= N_Range then
4763 Set_Entity (Index, New_E);
4764 end if;
4765 end;
4766 end if;
4767
4768 Make_Index (Index, P, Related_Id, Nb_Index);
4769
4770 -- Check error of subtype with predicate for index type
4771
4772 Bad_Predicated_Subtype_Use
4773 ("subtype& has predicate, not allowed as index subtype",
4774 Index, Etype (Index));
4775
4776 -- Move to next index
4777
4778 Next_Index (Index);
4779 Nb_Index := Nb_Index + 1;
4780 end loop;
4781
4782 -- Process subtype indication if one is present
4783
4784 if Present (Component_Typ) then
4785 Element_Type := Process_Subtype (Component_Typ, P, Related_Id, 'C');
4786
4787 Set_Etype (Component_Typ, Element_Type);
4788
4789 if not Nkind_In (Component_Typ, N_Identifier, N_Expanded_Name) then
4790 Check_SPARK_Restriction ("subtype mark required", Component_Typ);
4791 end if;
4792
4793 -- Ada 2005 (AI-230): Access Definition case
4794
4795 else pragma Assert (Present (Access_Definition (Component_Def)));
4796
4797 -- Indicate that the anonymous access type is created by the
4798 -- array type declaration.
4799
4800 Element_Type := Access_Definition
4801 (Related_Nod => P,
4802 N => Access_Definition (Component_Def));
4803 Set_Is_Local_Anonymous_Access (Element_Type);
4804
4805 -- Propagate the parent. This field is needed if we have to generate
4806 -- the master_id associated with an anonymous access to task type
4807 -- component (see Expand_N_Full_Type_Declaration.Build_Master)
4808
4809 Set_Parent (Element_Type, Parent (T));
4810
4811 -- Ada 2005 (AI-230): In case of components that are anonymous access
4812 -- types the level of accessibility depends on the enclosing type
4813 -- declaration
4814
4815 Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
4816
4817 -- Ada 2005 (AI-254)
4818
4819 declare
4820 CD : constant Node_Id :=
4821 Access_To_Subprogram_Definition
4822 (Access_Definition (Component_Def));
4823 begin
4824 if Present (CD) and then Protected_Present (CD) then
4825 Element_Type :=
4826 Replace_Anonymous_Access_To_Protected_Subprogram (Def);
4827 end if;
4828 end;
4829 end if;
4830
4831 -- Constrained array case
4832
4833 if No (T) then
4834 T := Create_Itype (E_Void, P, Related_Id, 'T');
4835 end if;
4836
4837 if Nkind (Def) = N_Constrained_Array_Definition then
4838
4839 -- Establish Implicit_Base as unconstrained base type
4840
4841 Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
4842
4843 Set_Etype (Implicit_Base, Implicit_Base);
4844 Set_Scope (Implicit_Base, Current_Scope);
4845 Set_Has_Delayed_Freeze (Implicit_Base);
4846
4847 -- The constrained array type is a subtype of the unconstrained one
4848
4849 Set_Ekind (T, E_Array_Subtype);
4850 Init_Size_Align (T);
4851 Set_Etype (T, Implicit_Base);
4852 Set_Scope (T, Current_Scope);
4853 Set_Is_Constrained (T, True);
4854 Set_First_Index (T, First (Discrete_Subtype_Definitions (Def)));
4855 Set_Has_Delayed_Freeze (T);
4856
4857 -- Complete setup of implicit base type
4858
4859 Set_First_Index (Implicit_Base, First_Index (T));
4860 Set_Component_Type (Implicit_Base, Element_Type);
4861 Set_Has_Task (Implicit_Base, Has_Task (Element_Type));
4862 Set_Component_Size (Implicit_Base, Uint_0);
4863 Set_Packed_Array_Type (Implicit_Base, Empty);
4864 Set_Has_Controlled_Component
4865 (Implicit_Base, Has_Controlled_Component
4866 (Element_Type)
4867 or else Is_Controlled
4868 (Element_Type));
4869 Set_Finalize_Storage_Only
4870 (Implicit_Base, Finalize_Storage_Only
4871 (Element_Type));
4872
4873 -- Unconstrained array case
4874
4875 else
4876 Set_Ekind (T, E_Array_Type);
4877 Init_Size_Align (T);
4878 Set_Etype (T, T);
4879 Set_Scope (T, Current_Scope);
4880 Set_Component_Size (T, Uint_0);
4881 Set_Is_Constrained (T, False);
4882 Set_First_Index (T, First (Subtype_Marks (Def)));
4883 Set_Has_Delayed_Freeze (T, True);
4884 Set_Has_Task (T, Has_Task (Element_Type));
4885 Set_Has_Controlled_Component (T, Has_Controlled_Component
4886 (Element_Type)
4887 or else
4888 Is_Controlled (Element_Type));
4889 Set_Finalize_Storage_Only (T, Finalize_Storage_Only
4890 (Element_Type));
4891 end if;
4892
4893 -- Common attributes for both cases
4894
4895 Set_Component_Type (Base_Type (T), Element_Type);
4896 Set_Packed_Array_Type (T, Empty);
4897
4898 if Aliased_Present (Component_Definition (Def)) then
4899 Check_SPARK_Restriction
4900 ("aliased is not allowed", Component_Definition (Def));
4901 Set_Has_Aliased_Components (Etype (T));
4902 end if;
4903
4904 -- Ada 2005 (AI-231): Propagate the null-excluding attribute to the
4905 -- array type to ensure that objects of this type are initialized.
4906
4907 if Ada_Version >= Ada_2005
4908 and then Can_Never_Be_Null (Element_Type)
4909 then
4910 Set_Can_Never_Be_Null (T);
4911
4912 if Null_Exclusion_Present (Component_Definition (Def))
4913
4914 -- No need to check itypes because in their case this check was
4915 -- done at their point of creation
4916
4917 and then not Is_Itype (Element_Type)
4918 then
4919 Error_Msg_N
4920 ("`NOT NULL` not allowed (null already excluded)",
4921 Subtype_Indication (Component_Definition (Def)));
4922 end if;
4923 end if;
4924
4925 Priv := Private_Component (Element_Type);
4926
4927 if Present (Priv) then
4928
4929 -- Check for circular definitions
4930
4931 if Priv = Any_Type then
4932 Set_Component_Type (Etype (T), Any_Type);
4933
4934 -- There is a gap in the visibility of operations on the composite
4935 -- type only if the component type is defined in a different scope.
4936
4937 elsif Scope (Priv) = Current_Scope then
4938 null;
4939
4940 elsif Is_Limited_Type (Priv) then
4941 Set_Is_Limited_Composite (Etype (T));
4942 Set_Is_Limited_Composite (T);
4943 else
4944 Set_Is_Private_Composite (Etype (T));
4945 Set_Is_Private_Composite (T);
4946 end if;
4947 end if;
4948
4949 -- A syntax error in the declaration itself may lead to an empty index
4950 -- list, in which case do a minimal patch.
4951
4952 if No (First_Index (T)) then
4953 Error_Msg_N ("missing index definition in array type declaration", T);
4954
4955 declare
4956 Indexes : constant List_Id :=
4957 New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
4958 begin
4959 Set_Discrete_Subtype_Definitions (Def, Indexes);
4960 Set_First_Index (T, First (Indexes));
4961 return;
4962 end;
4963 end if;
4964
4965 -- Create a concatenation operator for the new type. Internal array
4966 -- types created for packed entities do not need such, they are
4967 -- compatible with the user-defined type.
4968
4969 if Number_Dimensions (T) = 1
4970 and then not Is_Packed_Array_Type (T)
4971 then
4972 New_Concatenation_Op (T);
4973 end if;
4974
4975 -- In the case of an unconstrained array the parser has already verified
4976 -- that all the indexes are unconstrained but we still need to make sure
4977 -- that the element type is constrained.
4978
4979 if Is_Indefinite_Subtype (Element_Type) then
4980 Error_Msg_N
4981 ("unconstrained element type in array declaration",
4982 Subtype_Indication (Component_Def));
4983
4984 elsif Is_Abstract_Type (Element_Type) then
4985 Error_Msg_N
4986 ("the type of a component cannot be abstract",
4987 Subtype_Indication (Component_Def));
4988 end if;
4989 end Array_Type_Declaration;
4990
4991 ------------------------------------------------------
4992 -- Replace_Anonymous_Access_To_Protected_Subprogram --
4993 ------------------------------------------------------
4994
4995 function Replace_Anonymous_Access_To_Protected_Subprogram
4996 (N : Node_Id) return Entity_Id
4997 is
4998 Loc : constant Source_Ptr := Sloc (N);
4999
5000 Curr_Scope : constant Scope_Stack_Entry :=
5001 Scope_Stack.Table (Scope_Stack.Last);
5002
5003 Anon : constant Entity_Id := Make_Temporary (Loc, 'S');
5004 Acc : Node_Id;
5005 Comp : Node_Id;
5006 Decl : Node_Id;
5007 P : Node_Id;
5008
5009 begin
5010 Set_Is_Internal (Anon);
5011
5012 case Nkind (N) is
5013 when N_Component_Declaration |
5014 N_Unconstrained_Array_Definition |
5015 N_Constrained_Array_Definition =>
5016 Comp := Component_Definition (N);
5017 Acc := Access_Definition (Comp);
5018
5019 when N_Discriminant_Specification =>
5020 Comp := Discriminant_Type (N);
5021 Acc := Comp;
5022
5023 when N_Parameter_Specification =>
5024 Comp := Parameter_Type (N);
5025 Acc := Comp;
5026
5027 when N_Access_Function_Definition =>
5028 Comp := Result_Definition (N);
5029 Acc := Comp;
5030
5031 when N_Object_Declaration =>
5032 Comp := Object_Definition (N);
5033 Acc := Comp;
5034
5035 when N_Function_Specification =>
5036 Comp := Result_Definition (N);
5037 Acc := Comp;
5038
5039 when others =>
5040 raise Program_Error;
5041 end case;
5042
5043 Decl := Make_Full_Type_Declaration (Loc,
5044 Defining_Identifier => Anon,
5045 Type_Definition =>
5046 Copy_Separate_Tree (Access_To_Subprogram_Definition (Acc)));
5047
5048 Mark_Rewrite_Insertion (Decl);
5049
5050 -- Insert the new declaration in the nearest enclosing scope. If the
5051 -- node is a body and N is its return type, the declaration belongs in
5052 -- the enclosing scope.
5053
5054 P := Parent (N);
5055
5056 if Nkind (P) = N_Subprogram_Body
5057 and then Nkind (N) = N_Function_Specification
5058 then
5059 P := Parent (P);
5060 end if;
5061
5062 while Present (P) and then not Has_Declarations (P) loop
5063 P := Parent (P);
5064 end loop;
5065
5066 pragma Assert (Present (P));
5067
5068 if Nkind (P) = N_Package_Specification then
5069 Prepend (Decl, Visible_Declarations (P));
5070 else
5071 Prepend (Decl, Declarations (P));
5072 end if;
5073
5074 -- Replace the anonymous type with an occurrence of the new declaration.
5075 -- In all cases the rewritten node does not have the null-exclusion
5076 -- attribute because (if present) it was already inherited by the
5077 -- anonymous entity (Anon). Thus, in case of components we do not
5078 -- inherit this attribute.
5079
5080 if Nkind (N) = N_Parameter_Specification then
5081 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5082 Set_Etype (Defining_Identifier (N), Anon);
5083 Set_Null_Exclusion_Present (N, False);
5084
5085 elsif Nkind (N) = N_Object_Declaration then
5086 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5087 Set_Etype (Defining_Identifier (N), Anon);
5088
5089 elsif Nkind (N) = N_Access_Function_Definition then
5090 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5091
5092 elsif Nkind (N) = N_Function_Specification then
5093 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5094 Set_Etype (Defining_Unit_Name (N), Anon);
5095
5096 else
5097 Rewrite (Comp,
5098 Make_Component_Definition (Loc,
5099 Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
5100 end if;
5101
5102 Mark_Rewrite_Insertion (Comp);
5103
5104 if Nkind_In (N, N_Object_Declaration, N_Access_Function_Definition) then
5105 Analyze (Decl);
5106
5107 else
5108 -- Temporarily remove the current scope (record or subprogram) from
5109 -- the stack to add the new declarations to the enclosing scope.
5110
5111 Scope_Stack.Decrement_Last;
5112 Analyze (Decl);
5113 Set_Is_Itype (Anon);
5114 Scope_Stack.Append (Curr_Scope);
5115 end if;
5116
5117 Set_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
5118 Set_Can_Use_Internal_Rep (Anon, not Always_Compatible_Rep_On_Target);
5119 return Anon;
5120 end Replace_Anonymous_Access_To_Protected_Subprogram;
5121
5122 -------------------------------
5123 -- Build_Derived_Access_Type --
5124 -------------------------------
5125
5126 procedure Build_Derived_Access_Type
5127 (N : Node_Id;
5128 Parent_Type : Entity_Id;
5129 Derived_Type : Entity_Id)
5130 is
5131 S : constant Node_Id := Subtype_Indication (Type_Definition (N));
5132
5133 Desig_Type : Entity_Id;
5134 Discr : Entity_Id;
5135 Discr_Con_Elist : Elist_Id;
5136 Discr_Con_El : Elmt_Id;
5137 Subt : Entity_Id;
5138
5139 begin
5140 -- Set the designated type so it is available in case this is an access
5141 -- to a self-referential type, e.g. a standard list type with a next
5142 -- pointer. Will be reset after subtype is built.
5143
5144 Set_Directly_Designated_Type
5145 (Derived_Type, Designated_Type (Parent_Type));
5146
5147 Subt := Process_Subtype (S, N);
5148
5149 if Nkind (S) /= N_Subtype_Indication
5150 and then Subt /= Base_Type (Subt)
5151 then
5152 Set_Ekind (Derived_Type, E_Access_Subtype);
5153 end if;
5154
5155 if Ekind (Derived_Type) = E_Access_Subtype then
5156 declare
5157 Pbase : constant Entity_Id := Base_Type (Parent_Type);
5158 Ibase : constant Entity_Id :=
5159 Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
5160 Svg_Chars : constant Name_Id := Chars (Ibase);
5161 Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
5162
5163 begin
5164 Copy_Node (Pbase, Ibase);
5165
5166 Set_Chars (Ibase, Svg_Chars);
5167 Set_Next_Entity (Ibase, Svg_Next_E);
5168 Set_Sloc (Ibase, Sloc (Derived_Type));
5169 Set_Scope (Ibase, Scope (Derived_Type));
5170 Set_Freeze_Node (Ibase, Empty);
5171 Set_Is_Frozen (Ibase, False);
5172 Set_Comes_From_Source (Ibase, False);
5173 Set_Is_First_Subtype (Ibase, False);
5174
5175 Set_Etype (Ibase, Pbase);
5176 Set_Etype (Derived_Type, Ibase);
5177 end;
5178 end if;
5179
5180 Set_Directly_Designated_Type
5181 (Derived_Type, Designated_Type (Subt));
5182
5183 Set_Is_Constrained (Derived_Type, Is_Constrained (Subt));
5184 Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
5185 Set_Size_Info (Derived_Type, Parent_Type);
5186 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
5187 Set_Depends_On_Private (Derived_Type,
5188 Has_Private_Component (Derived_Type));
5189 Conditional_Delay (Derived_Type, Subt);
5190
5191 -- Ada 2005 (AI-231): Set the null-exclusion attribute, and verify
5192 -- that it is not redundant.
5193
5194 if Null_Exclusion_Present (Type_Definition (N)) then
5195 Set_Can_Never_Be_Null (Derived_Type);
5196
5197 if Can_Never_Be_Null (Parent_Type)
5198 and then False
5199 then
5200 Error_Msg_NE
5201 ("`NOT NULL` not allowed (& already excludes null)",
5202 N, Parent_Type);
5203 end if;
5204
5205 elsif Can_Never_Be_Null (Parent_Type) then
5206 Set_Can_Never_Be_Null (Derived_Type);
5207 end if;
5208
5209 -- Note: we do not copy the Storage_Size_Variable, since we always go to
5210 -- the root type for this information.
5211
5212 -- Apply range checks to discriminants for derived record case
5213 -- ??? THIS CODE SHOULD NOT BE HERE REALLY.
5214
5215 Desig_Type := Designated_Type (Derived_Type);
5216 if Is_Composite_Type (Desig_Type)
5217 and then (not Is_Array_Type (Desig_Type))
5218 and then Has_Discriminants (Desig_Type)
5219 and then Base_Type (Desig_Type) /= Desig_Type
5220 then
5221 Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
5222 Discr_Con_El := First_Elmt (Discr_Con_Elist);
5223
5224 Discr := First_Discriminant (Base_Type (Desig_Type));
5225 while Present (Discr_Con_El) loop
5226 Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
5227 Next_Elmt (Discr_Con_El);
5228 Next_Discriminant (Discr);
5229 end loop;
5230 end if;
5231 end Build_Derived_Access_Type;
5232
5233 ------------------------------
5234 -- Build_Derived_Array_Type --
5235 ------------------------------
5236
5237 procedure Build_Derived_Array_Type
5238 (N : Node_Id;
5239 Parent_Type : Entity_Id;
5240 Derived_Type : Entity_Id)
5241 is
5242 Loc : constant Source_Ptr := Sloc (N);
5243 Tdef : constant Node_Id := Type_Definition (N);
5244 Indic : constant Node_Id := Subtype_Indication (Tdef);
5245 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
5246 Implicit_Base : Entity_Id;
5247 New_Indic : Node_Id;
5248
5249 procedure Make_Implicit_Base;
5250 -- If the parent subtype is constrained, the derived type is a subtype
5251 -- of an implicit base type derived from the parent base.
5252
5253 ------------------------
5254 -- Make_Implicit_Base --
5255 ------------------------
5256
5257 procedure Make_Implicit_Base is
5258 begin
5259 Implicit_Base :=
5260 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
5261
5262 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
5263 Set_Etype (Implicit_Base, Parent_Base);
5264
5265 Copy_Array_Subtype_Attributes (Implicit_Base, Parent_Base);
5266 Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
5267
5268 Set_Has_Delayed_Freeze (Implicit_Base, True);
5269 end Make_Implicit_Base;
5270
5271 -- Start of processing for Build_Derived_Array_Type
5272
5273 begin
5274 if not Is_Constrained (Parent_Type) then
5275 if Nkind (Indic) /= N_Subtype_Indication then
5276 Set_Ekind (Derived_Type, E_Array_Type);
5277
5278 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
5279 Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
5280
5281 Set_Has_Delayed_Freeze (Derived_Type, True);
5282
5283 else
5284 Make_Implicit_Base;
5285 Set_Etype (Derived_Type, Implicit_Base);
5286
5287 New_Indic :=
5288 Make_Subtype_Declaration (Loc,
5289 Defining_Identifier => Derived_Type,
5290 Subtype_Indication =>
5291 Make_Subtype_Indication (Loc,
5292 Subtype_Mark => New_Reference_To (Implicit_Base, Loc),
5293 Constraint => Constraint (Indic)));
5294
5295 Rewrite (N, New_Indic);
5296 Analyze (N);
5297 end if;
5298
5299 else
5300 if Nkind (Indic) /= N_Subtype_Indication then
5301 Make_Implicit_Base;
5302
5303 Set_Ekind (Derived_Type, Ekind (Parent_Type));
5304 Set_Etype (Derived_Type, Implicit_Base);
5305 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
5306
5307 else
5308 Error_Msg_N ("illegal constraint on constrained type", Indic);
5309 end if;
5310 end if;
5311
5312 -- If parent type is not a derived type itself, and is declared in
5313 -- closed scope (e.g. a subprogram), then we must explicitly introduce
5314 -- the new type's concatenation operator since Derive_Subprograms
5315 -- will not inherit the parent's operator. If the parent type is
5316 -- unconstrained, the operator is of the unconstrained base type.
5317
5318 if Number_Dimensions (Parent_Type) = 1
5319 and then not Is_Limited_Type (Parent_Type)
5320 and then not Is_Derived_Type (Parent_Type)
5321 and then not Is_Package_Or_Generic_Package
5322 (Scope (Base_Type (Parent_Type)))
5323 then
5324 if not Is_Constrained (Parent_Type)
5325 and then Is_Constrained (Derived_Type)
5326 then
5327 New_Concatenation_Op (Implicit_Base);
5328 else
5329 New_Concatenation_Op (Derived_Type);
5330 end if;
5331 end if;
5332 end Build_Derived_Array_Type;
5333
5334 -----------------------------------
5335 -- Build_Derived_Concurrent_Type --
5336 -----------------------------------
5337
5338 procedure Build_Derived_Concurrent_Type
5339 (N : Node_Id;
5340 Parent_Type : Entity_Id;
5341 Derived_Type : Entity_Id)
5342 is
5343 Loc : constant Source_Ptr := Sloc (N);
5344
5345 Corr_Record : constant Entity_Id := Make_Temporary (Loc, 'C');
5346 Corr_Decl : Node_Id;
5347 Corr_Decl_Needed : Boolean;
5348 -- If the derived type has fewer discriminants than its parent, the
5349 -- corresponding record is also a derived type, in order to account for
5350 -- the bound discriminants. We create a full type declaration for it in
5351 -- this case.
5352
5353 Constraint_Present : constant Boolean :=
5354 Nkind (Subtype_Indication (Type_Definition (N))) =
5355 N_Subtype_Indication;
5356
5357 D_Constraint : Node_Id;
5358 New_Constraint : Elist_Id;
5359 Old_Disc : Entity_Id;
5360 New_Disc : Entity_Id;
5361 New_N : Node_Id;
5362
5363 begin
5364 Set_Stored_Constraint (Derived_Type, No_Elist);
5365 Corr_Decl_Needed := False;
5366 Old_Disc := Empty;
5367
5368 if Present (Discriminant_Specifications (N))
5369 and then Constraint_Present
5370 then
5371 Old_Disc := First_Discriminant (Parent_Type);
5372 New_Disc := First (Discriminant_Specifications (N));
5373 while Present (New_Disc) and then Present (Old_Disc) loop
5374 Next_Discriminant (Old_Disc);
5375 Next (New_Disc);
5376 end loop;
5377 end if;
5378
5379 if Present (Old_Disc) and then Expander_Active then
5380
5381 -- The new type has fewer discriminants, so we need to create a new
5382 -- corresponding record, which is derived from the corresponding
5383 -- record of the parent, and has a stored constraint that captures
5384 -- the values of the discriminant constraints. The corresponding
5385 -- record is needed only if expander is active and code generation is
5386 -- enabled.
5387
5388 -- The type declaration for the derived corresponding record has the
5389 -- same discriminant part and constraints as the current declaration.
5390 -- Copy the unanalyzed tree to build declaration.
5391
5392 Corr_Decl_Needed := True;
5393 New_N := Copy_Separate_Tree (N);
5394
5395 Corr_Decl :=
5396 Make_Full_Type_Declaration (Loc,
5397 Defining_Identifier => Corr_Record,
5398 Discriminant_Specifications =>
5399 Discriminant_Specifications (New_N),
5400 Type_Definition =>
5401 Make_Derived_Type_Definition (Loc,
5402 Subtype_Indication =>
5403 Make_Subtype_Indication (Loc,
5404 Subtype_Mark =>
5405 New_Occurrence_Of
5406 (Corresponding_Record_Type (Parent_Type), Loc),
5407 Constraint =>
5408 Constraint
5409 (Subtype_Indication (Type_Definition (New_N))))));
5410 end if;
5411
5412 -- Copy Storage_Size and Relative_Deadline variables if task case
5413
5414 if Is_Task_Type (Parent_Type) then
5415 Set_Storage_Size_Variable (Derived_Type,
5416 Storage_Size_Variable (Parent_Type));
5417 Set_Relative_Deadline_Variable (Derived_Type,
5418 Relative_Deadline_Variable (Parent_Type));
5419 end if;
5420
5421 if Present (Discriminant_Specifications (N)) then
5422 Push_Scope (Derived_Type);
5423 Check_Or_Process_Discriminants (N, Derived_Type);
5424
5425 if Constraint_Present then
5426 New_Constraint :=
5427 Expand_To_Stored_Constraint
5428 (Parent_Type,
5429 Build_Discriminant_Constraints
5430 (Parent_Type,
5431 Subtype_Indication (Type_Definition (N)), True));
5432 end if;
5433
5434 End_Scope;
5435
5436 elsif Constraint_Present then
5437
5438 -- Build constrained subtype and derive from it
5439
5440 declare
5441 Loc : constant Source_Ptr := Sloc (N);
5442 Anon : constant Entity_Id :=
5443 Make_Defining_Identifier (Loc,
5444 Chars => New_External_Name (Chars (Derived_Type), 'T'));
5445 Decl : Node_Id;
5446
5447 begin
5448 Decl :=
5449 Make_Subtype_Declaration (Loc,
5450 Defining_Identifier => Anon,
5451 Subtype_Indication =>
5452 Subtype_Indication (Type_Definition (N)));
5453 Insert_Before (N, Decl);
5454 Analyze (Decl);
5455
5456 Rewrite (Subtype_Indication (Type_Definition (N)),
5457 New_Occurrence_Of (Anon, Loc));
5458 Set_Analyzed (Derived_Type, False);
5459 Analyze (N);
5460 return;
5461 end;
5462 end if;
5463
5464 -- By default, operations and private data are inherited from parent.
5465 -- However, in the presence of bound discriminants, a new corresponding
5466 -- record will be created, see below.
5467
5468 Set_Has_Discriminants
5469 (Derived_Type, Has_Discriminants (Parent_Type));
5470 Set_Corresponding_Record_Type
5471 (Derived_Type, Corresponding_Record_Type (Parent_Type));
5472
5473 -- Is_Constrained is set according the parent subtype, but is set to
5474 -- False if the derived type is declared with new discriminants.
5475
5476 Set_Is_Constrained
5477 (Derived_Type,
5478 (Is_Constrained (Parent_Type) or else Constraint_Present)
5479 and then not Present (Discriminant_Specifications (N)));
5480
5481 if Constraint_Present then
5482 if not Has_Discriminants (Parent_Type) then
5483 Error_Msg_N ("untagged parent must have discriminants", N);
5484
5485 elsif Present (Discriminant_Specifications (N)) then
5486
5487 -- Verify that new discriminants are used to constrain old ones
5488
5489 D_Constraint :=
5490 First
5491 (Constraints
5492 (Constraint (Subtype_Indication (Type_Definition (N)))));
5493
5494 Old_Disc := First_Discriminant (Parent_Type);
5495
5496 while Present (D_Constraint) loop
5497 if Nkind (D_Constraint) /= N_Discriminant_Association then
5498
5499 -- Positional constraint. If it is a reference to a new
5500 -- discriminant, it constrains the corresponding old one.
5501
5502 if Nkind (D_Constraint) = N_Identifier then
5503 New_Disc := First_Discriminant (Derived_Type);
5504 while Present (New_Disc) loop
5505 exit when Chars (New_Disc) = Chars (D_Constraint);
5506 Next_Discriminant (New_Disc);
5507 end loop;
5508
5509 if Present (New_Disc) then
5510 Set_Corresponding_Discriminant (New_Disc, Old_Disc);
5511 end if;
5512 end if;
5513
5514 Next_Discriminant (Old_Disc);
5515
5516 -- if this is a named constraint, search by name for the old
5517 -- discriminants constrained by the new one.
5518
5519 elsif Nkind (Expression (D_Constraint)) = N_Identifier then
5520
5521 -- Find new discriminant with that name
5522
5523 New_Disc := First_Discriminant (Derived_Type);
5524 while Present (New_Disc) loop
5525 exit when
5526 Chars (New_Disc) = Chars (Expression (D_Constraint));
5527 Next_Discriminant (New_Disc);
5528 end loop;
5529
5530 if Present (New_Disc) then
5531
5532 -- Verify that new discriminant renames some discriminant
5533 -- of the parent type, and associate the new discriminant
5534 -- with one or more old ones that it renames.
5535
5536 declare
5537 Selector : Node_Id;
5538
5539 begin
5540 Selector := First (Selector_Names (D_Constraint));
5541 while Present (Selector) loop
5542 Old_Disc := First_Discriminant (Parent_Type);
5543 while Present (Old_Disc) loop
5544 exit when Chars (Old_Disc) = Chars (Selector);
5545 Next_Discriminant (Old_Disc);
5546 end loop;
5547
5548 if Present (Old_Disc) then
5549 Set_Corresponding_Discriminant
5550 (New_Disc, Old_Disc);
5551 end if;
5552
5553 Next (Selector);
5554 end loop;
5555 end;
5556 end if;
5557 end if;
5558
5559 Next (D_Constraint);
5560 end loop;
5561
5562 New_Disc := First_Discriminant (Derived_Type);
5563 while Present (New_Disc) loop
5564 if No (Corresponding_Discriminant (New_Disc)) then
5565 Error_Msg_NE
5566 ("new discriminant& must constrain old one", N, New_Disc);
5567
5568 elsif not
5569 Subtypes_Statically_Compatible
5570 (Etype (New_Disc),
5571 Etype (Corresponding_Discriminant (New_Disc)))
5572 then
5573 Error_Msg_NE
5574 ("& not statically compatible with parent discriminant",
5575 N, New_Disc);
5576 end if;
5577
5578 Next_Discriminant (New_Disc);
5579 end loop;
5580 end if;
5581
5582 elsif Present (Discriminant_Specifications (N)) then
5583 Error_Msg_N
5584 ("missing discriminant constraint in untagged derivation", N);
5585 end if;
5586
5587 -- The entity chain of the derived type includes the new discriminants
5588 -- but shares operations with the parent.
5589
5590 if Present (Discriminant_Specifications (N)) then
5591 Old_Disc := First_Discriminant (Parent_Type);
5592 while Present (Old_Disc) loop
5593 if No (Next_Entity (Old_Disc))
5594 or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
5595 then
5596 Set_Next_Entity
5597 (Last_Entity (Derived_Type), Next_Entity (Old_Disc));
5598 exit;
5599 end if;
5600
5601 Next_Discriminant (Old_Disc);
5602 end loop;
5603
5604 else
5605 Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
5606 if Has_Discriminants (Parent_Type) then
5607 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
5608 Set_Discriminant_Constraint (
5609 Derived_Type, Discriminant_Constraint (Parent_Type));
5610 end if;
5611 end if;
5612
5613 Set_Last_Entity (Derived_Type, Last_Entity (Parent_Type));
5614
5615 Set_Has_Completion (Derived_Type);
5616
5617 if Corr_Decl_Needed then
5618 Set_Stored_Constraint (Derived_Type, New_Constraint);
5619 Insert_After (N, Corr_Decl);
5620 Analyze (Corr_Decl);
5621 Set_Corresponding_Record_Type (Derived_Type, Corr_Record);
5622 end if;
5623 end Build_Derived_Concurrent_Type;
5624
5625 ------------------------------------
5626 -- Build_Derived_Enumeration_Type --
5627 ------------------------------------
5628
5629 procedure Build_Derived_Enumeration_Type
5630 (N : Node_Id;
5631 Parent_Type : Entity_Id;
5632 Derived_Type : Entity_Id)
5633 is
5634 Loc : constant Source_Ptr := Sloc (N);
5635 Def : constant Node_Id := Type_Definition (N);
5636 Indic : constant Node_Id := Subtype_Indication (Def);
5637 Implicit_Base : Entity_Id;
5638 Literal : Entity_Id;
5639 New_Lit : Entity_Id;
5640 Literals_List : List_Id;
5641 Type_Decl : Node_Id;
5642 Hi, Lo : Node_Id;
5643 Rang_Expr : Node_Id;
5644
5645 begin
5646 -- Since types Standard.Character and Standard.[Wide_]Wide_Character do
5647 -- not have explicit literals lists we need to process types derived
5648 -- from them specially. This is handled by Derived_Standard_Character.
5649 -- If the parent type is a generic type, there are no literals either,
5650 -- and we construct the same skeletal representation as for the generic
5651 -- parent type.
5652
5653 if Is_Standard_Character_Type (Parent_Type) then
5654 Derived_Standard_Character (N, Parent_Type, Derived_Type);
5655
5656 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
5657 declare
5658 Lo : Node_Id;
5659 Hi : Node_Id;
5660
5661 begin
5662 if Nkind (Indic) /= N_Subtype_Indication then
5663 Lo :=
5664 Make_Attribute_Reference (Loc,
5665 Attribute_Name => Name_First,
5666 Prefix => New_Reference_To (Derived_Type, Loc));
5667 Set_Etype (Lo, Derived_Type);
5668
5669 Hi :=
5670 Make_Attribute_Reference (Loc,
5671 Attribute_Name => Name_Last,
5672 Prefix => New_Reference_To (Derived_Type, Loc));
5673 Set_Etype (Hi, Derived_Type);
5674
5675 Set_Scalar_Range (Derived_Type,
5676 Make_Range (Loc,
5677 Low_Bound => Lo,
5678 High_Bound => Hi));
5679 else
5680
5681 -- Analyze subtype indication and verify compatibility
5682 -- with parent type.
5683
5684 if Base_Type (Process_Subtype (Indic, N)) /=
5685 Base_Type (Parent_Type)
5686 then
5687 Error_Msg_N
5688 ("illegal constraint for formal discrete type", N);
5689 end if;
5690 end if;
5691 end;
5692
5693 else
5694 -- If a constraint is present, analyze the bounds to catch
5695 -- premature usage of the derived literals.
5696
5697 if Nkind (Indic) = N_Subtype_Indication
5698 and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
5699 then
5700 Analyze (Low_Bound (Range_Expression (Constraint (Indic))));
5701 Analyze (High_Bound (Range_Expression (Constraint (Indic))));
5702 end if;
5703
5704 -- Introduce an implicit base type for the derived type even if there
5705 -- is no constraint attached to it, since this seems closer to the
5706 -- Ada semantics. Build a full type declaration tree for the derived
5707 -- type using the implicit base type as the defining identifier. The
5708 -- build a subtype declaration tree which applies the constraint (if
5709 -- any) have it replace the derived type declaration.
5710
5711 Literal := First_Literal (Parent_Type);
5712 Literals_List := New_List;
5713 while Present (Literal)
5714 and then Ekind (Literal) = E_Enumeration_Literal
5715 loop
5716 -- Literals of the derived type have the same representation as
5717 -- those of the parent type, but this representation can be
5718 -- overridden by an explicit representation clause. Indicate
5719 -- that there is no explicit representation given yet. These
5720 -- derived literals are implicit operations of the new type,
5721 -- and can be overridden by explicit ones.
5722
5723 if Nkind (Literal) = N_Defining_Character_Literal then
5724 New_Lit :=
5725 Make_Defining_Character_Literal (Loc, Chars (Literal));
5726 else
5727 New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
5728 end if;
5729
5730 Set_Ekind (New_Lit, E_Enumeration_Literal);
5731 Set_Enumeration_Pos (New_Lit, Enumeration_Pos (Literal));
5732 Set_Enumeration_Rep (New_Lit, Enumeration_Rep (Literal));
5733 Set_Enumeration_Rep_Expr (New_Lit, Empty);
5734 Set_Alias (New_Lit, Literal);
5735 Set_Is_Known_Valid (New_Lit, True);
5736
5737 Append (New_Lit, Literals_List);
5738 Next_Literal (Literal);
5739 end loop;
5740
5741 Implicit_Base :=
5742 Make_Defining_Identifier (Sloc (Derived_Type),
5743 Chars => New_External_Name (Chars (Derived_Type), 'B'));
5744
5745 -- Indicate the proper nature of the derived type. This must be done
5746 -- before analysis of the literals, to recognize cases when a literal
5747 -- may be hidden by a previous explicit function definition (cf.
5748 -- c83031a).
5749
5750 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
5751 Set_Etype (Derived_Type, Implicit_Base);
5752
5753 Type_Decl :=
5754 Make_Full_Type_Declaration (Loc,
5755 Defining_Identifier => Implicit_Base,
5756 Discriminant_Specifications => No_List,
5757 Type_Definition =>
5758 Make_Enumeration_Type_Definition (Loc, Literals_List));
5759
5760 Mark_Rewrite_Insertion (Type_Decl);
5761 Insert_Before (N, Type_Decl);
5762 Analyze (Type_Decl);
5763
5764 -- After the implicit base is analyzed its Etype needs to be changed
5765 -- to reflect the fact that it is derived from the parent type which
5766 -- was ignored during analysis. We also set the size at this point.
5767
5768 Set_Etype (Implicit_Base, Parent_Type);
5769
5770 Set_Size_Info (Implicit_Base, Parent_Type);
5771 Set_RM_Size (Implicit_Base, RM_Size (Parent_Type));
5772 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
5773
5774 -- Copy other flags from parent type
5775
5776 Set_Has_Non_Standard_Rep
5777 (Implicit_Base, Has_Non_Standard_Rep
5778 (Parent_Type));
5779 Set_Has_Pragma_Ordered
5780 (Implicit_Base, Has_Pragma_Ordered
5781 (Parent_Type));
5782 Set_Has_Delayed_Freeze (Implicit_Base);
5783
5784 -- Process the subtype indication including a validation check on the
5785 -- constraint, if any. If a constraint is given, its bounds must be
5786 -- implicitly converted to the new type.
5787
5788 if Nkind (Indic) = N_Subtype_Indication then
5789 declare
5790 R : constant Node_Id :=
5791 Range_Expression (Constraint (Indic));
5792
5793 begin
5794 if Nkind (R) = N_Range then
5795 Hi := Build_Scalar_Bound
5796 (High_Bound (R), Parent_Type, Implicit_Base);
5797 Lo := Build_Scalar_Bound
5798 (Low_Bound (R), Parent_Type, Implicit_Base);
5799
5800 else
5801 -- Constraint is a Range attribute. Replace with explicit
5802 -- mention of the bounds of the prefix, which must be a
5803 -- subtype.
5804
5805 Analyze (Prefix (R));
5806 Hi :=
5807 Convert_To (Implicit_Base,
5808 Make_Attribute_Reference (Loc,
5809 Attribute_Name => Name_Last,
5810 Prefix =>
5811 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
5812
5813 Lo :=
5814 Convert_To (Implicit_Base,
5815 Make_Attribute_Reference (Loc,
5816 Attribute_Name => Name_First,
5817 Prefix =>
5818 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
5819 end if;
5820 end;
5821
5822 else
5823 Hi :=
5824 Build_Scalar_Bound
5825 (Type_High_Bound (Parent_Type),
5826 Parent_Type, Implicit_Base);
5827 Lo :=
5828 Build_Scalar_Bound
5829 (Type_Low_Bound (Parent_Type),
5830 Parent_Type, Implicit_Base);
5831 end if;
5832
5833 Rang_Expr :=
5834 Make_Range (Loc,
5835 Low_Bound => Lo,
5836 High_Bound => Hi);
5837
5838 -- If we constructed a default range for the case where no range
5839 -- was given, then the expressions in the range must not freeze
5840 -- since they do not correspond to expressions in the source.
5841
5842 if Nkind (Indic) /= N_Subtype_Indication then
5843 Set_Must_Not_Freeze (Lo);
5844 Set_Must_Not_Freeze (Hi);
5845 Set_Must_Not_Freeze (Rang_Expr);
5846 end if;
5847
5848 Rewrite (N,
5849 Make_Subtype_Declaration (Loc,
5850 Defining_Identifier => Derived_Type,
5851 Subtype_Indication =>
5852 Make_Subtype_Indication (Loc,
5853 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
5854 Constraint =>
5855 Make_Range_Constraint (Loc,
5856 Range_Expression => Rang_Expr))));
5857
5858 Analyze (N);
5859
5860 -- If pragma Discard_Names applies on the first subtype of the parent
5861 -- type, then it must be applied on this subtype as well.
5862
5863 if Einfo.Discard_Names (First_Subtype (Parent_Type)) then
5864 Set_Discard_Names (Derived_Type);
5865 end if;
5866
5867 -- Apply a range check. Since this range expression doesn't have an
5868 -- Etype, we have to specifically pass the Source_Typ parameter. Is
5869 -- this right???
5870
5871 if Nkind (Indic) = N_Subtype_Indication then
5872 Apply_Range_Check (Range_Expression (Constraint (Indic)),
5873 Parent_Type,
5874 Source_Typ => Entity (Subtype_Mark (Indic)));
5875 end if;
5876 end if;
5877 end Build_Derived_Enumeration_Type;
5878
5879 --------------------------------
5880 -- Build_Derived_Numeric_Type --
5881 --------------------------------
5882
5883 procedure Build_Derived_Numeric_Type
5884 (N : Node_Id;
5885 Parent_Type : Entity_Id;
5886 Derived_Type : Entity_Id)
5887 is
5888 Loc : constant Source_Ptr := Sloc (N);
5889 Tdef : constant Node_Id := Type_Definition (N);
5890 Indic : constant Node_Id := Subtype_Indication (Tdef);
5891 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
5892 No_Constraint : constant Boolean := Nkind (Indic) /=
5893 N_Subtype_Indication;
5894 Implicit_Base : Entity_Id;
5895
5896 Lo : Node_Id;
5897 Hi : Node_Id;
5898
5899 begin
5900 -- Process the subtype indication including a validation check on
5901 -- the constraint if any.
5902
5903 Discard_Node (Process_Subtype (Indic, N));
5904
5905 -- Introduce an implicit base type for the derived type even if there
5906 -- is no constraint attached to it, since this seems closer to the Ada
5907 -- semantics.
5908
5909 Implicit_Base :=
5910 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
5911
5912 Set_Etype (Implicit_Base, Parent_Base);
5913 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
5914 Set_Size_Info (Implicit_Base, Parent_Base);
5915 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
5916 Set_Parent (Implicit_Base, Parent (Derived_Type));
5917 Set_Is_Known_Valid (Implicit_Base, Is_Known_Valid (Parent_Base));
5918
5919 -- Set RM Size for discrete type or decimal fixed-point type
5920 -- Ordinary fixed-point is excluded, why???
5921
5922 if Is_Discrete_Type (Parent_Base)
5923 or else Is_Decimal_Fixed_Point_Type (Parent_Base)
5924 then
5925 Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
5926 end if;
5927
5928 Set_Has_Delayed_Freeze (Implicit_Base);
5929
5930 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
5931 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
5932
5933 Set_Scalar_Range (Implicit_Base,
5934 Make_Range (Loc,
5935 Low_Bound => Lo,
5936 High_Bound => Hi));
5937
5938 if Has_Infinities (Parent_Base) then
5939 Set_Includes_Infinities (Scalar_Range (Implicit_Base));
5940 end if;
5941
5942 -- The Derived_Type, which is the entity of the declaration, is a
5943 -- subtype of the implicit base. Its Ekind is a subtype, even in the
5944 -- absence of an explicit constraint.
5945
5946 Set_Etype (Derived_Type, Implicit_Base);
5947
5948 -- If we did not have a constraint, then the Ekind is set from the
5949 -- parent type (otherwise Process_Subtype has set the bounds)
5950
5951 if No_Constraint then
5952 Set_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
5953 end if;
5954
5955 -- If we did not have a range constraint, then set the range from the
5956 -- parent type. Otherwise, the Process_Subtype call has set the bounds.
5957
5958 if No_Constraint
5959 or else not Has_Range_Constraint (Indic)
5960 then
5961 Set_Scalar_Range (Derived_Type,
5962 Make_Range (Loc,
5963 Low_Bound => New_Copy_Tree (Type_Low_Bound (Parent_Type)),
5964 High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
5965 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
5966
5967 if Has_Infinities (Parent_Type) then
5968 Set_Includes_Infinities (Scalar_Range (Derived_Type));
5969 end if;
5970
5971 Set_Is_Known_Valid (Derived_Type, Is_Known_Valid (Parent_Type));
5972 end if;
5973
5974 Set_Is_Descendent_Of_Address (Derived_Type,
5975 Is_Descendent_Of_Address (Parent_Type));
5976 Set_Is_Descendent_Of_Address (Implicit_Base,
5977 Is_Descendent_Of_Address (Parent_Type));
5978
5979 -- Set remaining type-specific fields, depending on numeric type
5980
5981 if Is_Modular_Integer_Type (Parent_Type) then
5982 Set_Modulus (Implicit_Base, Modulus (Parent_Base));
5983
5984 Set_Non_Binary_Modulus
5985 (Implicit_Base, Non_Binary_Modulus (Parent_Base));
5986
5987 Set_Is_Known_Valid
5988 (Implicit_Base, Is_Known_Valid (Parent_Base));
5989
5990 elsif Is_Floating_Point_Type (Parent_Type) then
5991
5992 -- Digits of base type is always copied from the digits value of
5993 -- the parent base type, but the digits of the derived type will
5994 -- already have been set if there was a constraint present.
5995
5996 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
5997 Set_Float_Rep (Implicit_Base, Float_Rep (Parent_Base));
5998
5999 if No_Constraint then
6000 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
6001 end if;
6002
6003 elsif Is_Fixed_Point_Type (Parent_Type) then
6004
6005 -- Small of base type and derived type are always copied from the
6006 -- parent base type, since smalls never change. The delta of the
6007 -- base type is also copied from the parent base type. However the
6008 -- delta of the derived type will have been set already if a
6009 -- constraint was present.
6010
6011 Set_Small_Value (Derived_Type, Small_Value (Parent_Base));
6012 Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
6013 Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
6014
6015 if No_Constraint then
6016 Set_Delta_Value (Derived_Type, Delta_Value (Parent_Type));
6017 end if;
6018
6019 -- The scale and machine radix in the decimal case are always
6020 -- copied from the parent base type.
6021
6022 if Is_Decimal_Fixed_Point_Type (Parent_Type) then
6023 Set_Scale_Value (Derived_Type, Scale_Value (Parent_Base));
6024 Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
6025
6026 Set_Machine_Radix_10
6027 (Derived_Type, Machine_Radix_10 (Parent_Base));
6028 Set_Machine_Radix_10
6029 (Implicit_Base, Machine_Radix_10 (Parent_Base));
6030
6031 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
6032
6033 if No_Constraint then
6034 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
6035
6036 else
6037 -- the analysis of the subtype_indication sets the
6038 -- digits value of the derived type.
6039
6040 null;
6041 end if;
6042 end if;
6043 end if;
6044
6045 -- The type of the bounds is that of the parent type, and they
6046 -- must be converted to the derived type.
6047
6048 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
6049
6050 -- The implicit_base should be frozen when the derived type is frozen,
6051 -- but note that it is used in the conversions of the bounds. For fixed
6052 -- types we delay the determination of the bounds until the proper
6053 -- freezing point. For other numeric types this is rejected by GCC, for
6054 -- reasons that are currently unclear (???), so we choose to freeze the
6055 -- implicit base now. In the case of integers and floating point types
6056 -- this is harmless because subsequent representation clauses cannot
6057 -- affect anything, but it is still baffling that we cannot use the
6058 -- same mechanism for all derived numeric types.
6059
6060 -- There is a further complication: actually *some* representation
6061 -- clauses can affect the implicit base type. Namely, attribute
6062 -- definition clauses for stream-oriented attributes need to set the
6063 -- corresponding TSS entries on the base type, and this normally cannot
6064 -- be done after the base type is frozen, so the circuitry in
6065 -- Sem_Ch13.New_Stream_Subprogram must account for this possibility and
6066 -- not use Set_TSS in this case.
6067
6068 if Is_Fixed_Point_Type (Parent_Type) then
6069 Conditional_Delay (Implicit_Base, Parent_Type);
6070 else
6071 Freeze_Before (N, Implicit_Base);
6072 end if;
6073 end Build_Derived_Numeric_Type;
6074
6075 --------------------------------
6076 -- Build_Derived_Private_Type --
6077 --------------------------------
6078
6079 procedure Build_Derived_Private_Type
6080 (N : Node_Id;
6081 Parent_Type : Entity_Id;
6082 Derived_Type : Entity_Id;
6083 Is_Completion : Boolean;
6084 Derive_Subps : Boolean := True)
6085 is
6086 Loc : constant Source_Ptr := Sloc (N);
6087 Der_Base : Entity_Id;
6088 Discr : Entity_Id;
6089 Full_Decl : Node_Id := Empty;
6090 Full_Der : Entity_Id;
6091 Full_P : Entity_Id;
6092 Last_Discr : Entity_Id;
6093 Par_Scope : constant Entity_Id := Scope (Base_Type (Parent_Type));
6094 Swapped : Boolean := False;
6095
6096 procedure Copy_And_Build;
6097 -- Copy derived type declaration, replace parent with its full view,
6098 -- and analyze new declaration.
6099
6100 --------------------
6101 -- Copy_And_Build --
6102 --------------------
6103
6104 procedure Copy_And_Build is
6105 Full_N : Node_Id;
6106
6107 begin
6108 if Ekind (Parent_Type) in Record_Kind
6109 or else
6110 (Ekind (Parent_Type) in Enumeration_Kind
6111 and then not Is_Standard_Character_Type (Parent_Type)
6112 and then not Is_Generic_Type (Root_Type (Parent_Type)))
6113 then
6114 Full_N := New_Copy_Tree (N);
6115 Insert_After (N, Full_N);
6116 Build_Derived_Type (
6117 Full_N, Parent_Type, Full_Der, True, Derive_Subps => False);
6118
6119 else
6120 Build_Derived_Type (
6121 N, Parent_Type, Full_Der, True, Derive_Subps => False);
6122 end if;
6123 end Copy_And_Build;
6124
6125 -- Start of processing for Build_Derived_Private_Type
6126
6127 begin
6128 if Is_Tagged_Type (Parent_Type) then
6129 Full_P := Full_View (Parent_Type);
6130
6131 -- A type extension of a type with unknown discriminants is an
6132 -- indefinite type that the back-end cannot handle directly.
6133 -- We treat it as a private type, and build a completion that is
6134 -- derived from the full view of the parent, and hopefully has
6135 -- known discriminants.
6136
6137 -- If the full view of the parent type has an underlying record view,
6138 -- use it to generate the underlying record view of this derived type
6139 -- (required for chains of derivations with unknown discriminants).
6140
6141 -- Minor optimization: we avoid the generation of useless underlying
6142 -- record view entities if the private type declaration has unknown
6143 -- discriminants but its corresponding full view has no
6144 -- discriminants.
6145
6146 if Has_Unknown_Discriminants (Parent_Type)
6147 and then Present (Full_P)
6148 and then (Has_Discriminants (Full_P)
6149 or else Present (Underlying_Record_View (Full_P)))
6150 and then not In_Open_Scopes (Par_Scope)
6151 and then Expander_Active
6152 then
6153 declare
6154 Full_Der : constant Entity_Id := Make_Temporary (Loc, 'T');
6155 New_Ext : constant Node_Id :=
6156 Copy_Separate_Tree
6157 (Record_Extension_Part (Type_Definition (N)));
6158 Decl : Node_Id;
6159
6160 begin
6161 Build_Derived_Record_Type
6162 (N, Parent_Type, Derived_Type, Derive_Subps);
6163
6164 -- Build anonymous completion, as a derivation from the full
6165 -- view of the parent. This is not a completion in the usual
6166 -- sense, because the current type is not private.
6167
6168 Decl :=
6169 Make_Full_Type_Declaration (Loc,
6170 Defining_Identifier => Full_Der,
6171 Type_Definition =>
6172 Make_Derived_Type_Definition (Loc,
6173 Subtype_Indication =>
6174 New_Copy_Tree
6175 (Subtype_Indication (Type_Definition (N))),
6176 Record_Extension_Part => New_Ext));
6177
6178 -- If the parent type has an underlying record view, use it
6179 -- here to build the new underlying record view.
6180
6181 if Present (Underlying_Record_View (Full_P)) then
6182 pragma Assert
6183 (Nkind (Subtype_Indication (Type_Definition (Decl)))
6184 = N_Identifier);
6185 Set_Entity (Subtype_Indication (Type_Definition (Decl)),
6186 Underlying_Record_View (Full_P));
6187 end if;
6188
6189 Install_Private_Declarations (Par_Scope);
6190 Install_Visible_Declarations (Par_Scope);
6191 Insert_Before (N, Decl);
6192
6193 -- Mark entity as an underlying record view before analysis,
6194 -- to avoid generating the list of its primitive operations
6195 -- (which is not really required for this entity) and thus
6196 -- prevent spurious errors associated with missing overriding
6197 -- of abstract primitives (overridden only for Derived_Type).
6198
6199 Set_Ekind (Full_Der, E_Record_Type);
6200 Set_Is_Underlying_Record_View (Full_Der);
6201
6202 Analyze (Decl);
6203
6204 pragma Assert (Has_Discriminants (Full_Der)
6205 and then not Has_Unknown_Discriminants (Full_Der));
6206
6207 Uninstall_Declarations (Par_Scope);
6208
6209 -- Freeze the underlying record view, to prevent generation of
6210 -- useless dispatching information, which is simply shared with
6211 -- the real derived type.
6212
6213 Set_Is_Frozen (Full_Der);
6214
6215 -- Set up links between real entity and underlying record view
6216
6217 Set_Underlying_Record_View (Derived_Type, Base_Type (Full_Der));
6218 Set_Underlying_Record_View (Base_Type (Full_Der), Derived_Type);
6219 end;
6220
6221 -- If discriminants are known, build derived record
6222
6223 else
6224 Build_Derived_Record_Type
6225 (N, Parent_Type, Derived_Type, Derive_Subps);
6226 end if;
6227
6228 return;
6229
6230 elsif Has_Discriminants (Parent_Type) then
6231 if Present (Full_View (Parent_Type)) then
6232 if not Is_Completion then
6233
6234 -- Copy declaration for subsequent analysis, to provide a
6235 -- completion for what is a private declaration. Indicate that
6236 -- the full type is internally generated.
6237
6238 Full_Decl := New_Copy_Tree (N);
6239 Full_Der := New_Copy (Derived_Type);
6240 Set_Comes_From_Source (Full_Decl, False);
6241 Set_Comes_From_Source (Full_Der, False);
6242 Set_Parent (Full_Der, Full_Decl);
6243
6244 Insert_After (N, Full_Decl);
6245
6246 else
6247 -- If this is a completion, the full view being built is itself
6248 -- private. We build a subtype of the parent with the same
6249 -- constraints as this full view, to convey to the back end the
6250 -- constrained components and the size of this subtype. If the
6251 -- parent is constrained, its full view can serve as the
6252 -- underlying full view of the derived type.
6253
6254 if No (Discriminant_Specifications (N)) then
6255 if Nkind (Subtype_Indication (Type_Definition (N))) =
6256 N_Subtype_Indication
6257 then
6258 Build_Underlying_Full_View (N, Derived_Type, Parent_Type);
6259
6260 elsif Is_Constrained (Full_View (Parent_Type)) then
6261 Set_Underlying_Full_View
6262 (Derived_Type, Full_View (Parent_Type));
6263 end if;
6264
6265 else
6266 -- If there are new discriminants, the parent subtype is
6267 -- constrained by them, but it is not clear how to build
6268 -- the Underlying_Full_View in this case???
6269
6270 null;
6271 end if;
6272 end if;
6273 end if;
6274
6275 -- Build partial view of derived type from partial view of parent
6276
6277 Build_Derived_Record_Type
6278 (N, Parent_Type, Derived_Type, Derive_Subps);
6279
6280 if Present (Full_View (Parent_Type)) and then not Is_Completion then
6281 if not In_Open_Scopes (Par_Scope)
6282 or else not In_Same_Source_Unit (N, Parent_Type)
6283 then
6284 -- Swap partial and full views temporarily
6285
6286 Install_Private_Declarations (Par_Scope);
6287 Install_Visible_Declarations (Par_Scope);
6288 Swapped := True;
6289 end if;
6290
6291 -- Build full view of derived type from full view of parent which
6292 -- is now installed. Subprograms have been derived on the partial
6293 -- view, the completion does not derive them anew.
6294
6295 if not Is_Tagged_Type (Parent_Type) then
6296
6297 -- If the parent is itself derived from another private type,
6298 -- installing the private declarations has not affected its
6299 -- privacy status, so use its own full view explicitly.
6300
6301 if Is_Private_Type (Parent_Type) then
6302 Build_Derived_Record_Type
6303 (Full_Decl, Full_View (Parent_Type), Full_Der, False);
6304 else
6305 Build_Derived_Record_Type
6306 (Full_Decl, Parent_Type, Full_Der, False);
6307 end if;
6308
6309 else
6310 -- If full view of parent is tagged, the completion inherits
6311 -- the proper primitive operations.
6312
6313 Set_Defining_Identifier (Full_Decl, Full_Der);
6314 Build_Derived_Record_Type
6315 (Full_Decl, Parent_Type, Full_Der, Derive_Subps);
6316 end if;
6317
6318 -- The full declaration has been introduced into the tree and
6319 -- processed in the step above. It should not be analyzed again
6320 -- (when encountered later in the current list of declarations)
6321 -- to prevent spurious name conflicts. The full entity remains
6322 -- invisible.
6323
6324 Set_Analyzed (Full_Decl);
6325
6326 if Swapped then
6327 Uninstall_Declarations (Par_Scope);
6328
6329 if In_Open_Scopes (Par_Scope) then
6330 Install_Visible_Declarations (Par_Scope);
6331 end if;
6332 end if;
6333
6334 Der_Base := Base_Type (Derived_Type);
6335 Set_Full_View (Derived_Type, Full_Der);
6336 Set_Full_View (Der_Base, Base_Type (Full_Der));
6337
6338 -- Copy the discriminant list from full view to the partial views
6339 -- (base type and its subtype). Gigi requires that the partial and
6340 -- full views have the same discriminants.
6341
6342 -- Note that since the partial view is pointing to discriminants
6343 -- in the full view, their scope will be that of the full view.
6344 -- This might cause some front end problems and need adjustment???
6345
6346 Discr := First_Discriminant (Base_Type (Full_Der));
6347 Set_First_Entity (Der_Base, Discr);
6348
6349 loop
6350 Last_Discr := Discr;
6351 Next_Discriminant (Discr);
6352 exit when No (Discr);
6353 end loop;
6354
6355 Set_Last_Entity (Der_Base, Last_Discr);
6356
6357 Set_First_Entity (Derived_Type, First_Entity (Der_Base));
6358 Set_Last_Entity (Derived_Type, Last_Entity (Der_Base));
6359 Set_Stored_Constraint (Full_Der, Stored_Constraint (Derived_Type));
6360
6361 else
6362 -- If this is a completion, the derived type stays private and
6363 -- there is no need to create a further full view, except in the
6364 -- unusual case when the derivation is nested within a child unit,
6365 -- see below.
6366
6367 null;
6368 end if;
6369
6370 elsif Present (Full_View (Parent_Type))
6371 and then Has_Discriminants (Full_View (Parent_Type))
6372 then
6373 if Has_Unknown_Discriminants (Parent_Type)
6374 and then Nkind (Subtype_Indication (Type_Definition (N))) =
6375 N_Subtype_Indication
6376 then
6377 Error_Msg_N
6378 ("cannot constrain type with unknown discriminants",
6379 Subtype_Indication (Type_Definition (N)));
6380 return;
6381 end if;
6382
6383 -- If full view of parent is a record type, build full view as a
6384 -- derivation from the parent's full view. Partial view remains
6385 -- private. For code generation and linking, the full view must have
6386 -- the same public status as the partial one. This full view is only
6387 -- needed if the parent type is in an enclosing scope, so that the
6388 -- full view may actually become visible, e.g. in a child unit. This
6389 -- is both more efficient, and avoids order of freezing problems with
6390 -- the added entities.
6391
6392 if not Is_Private_Type (Full_View (Parent_Type))
6393 and then (In_Open_Scopes (Scope (Parent_Type)))
6394 then
6395 Full_Der :=
6396 Make_Defining_Identifier
6397 (Sloc (Derived_Type), Chars (Derived_Type));
6398 Set_Is_Itype (Full_Der);
6399 Set_Has_Private_Declaration (Full_Der);
6400 Set_Has_Private_Declaration (Derived_Type);
6401 Set_Associated_Node_For_Itype (Full_Der, N);
6402 Set_Parent (Full_Der, Parent (Derived_Type));
6403 Set_Full_View (Derived_Type, Full_Der);
6404 Set_Is_Public (Full_Der, Is_Public (Derived_Type));
6405 Full_P := Full_View (Parent_Type);
6406 Exchange_Declarations (Parent_Type);
6407 Copy_And_Build;
6408 Exchange_Declarations (Full_P);
6409
6410 else
6411 Build_Derived_Record_Type
6412 (N, Full_View (Parent_Type), Derived_Type,
6413 Derive_Subps => False);
6414 end if;
6415
6416 -- In any case, the primitive operations are inherited from the
6417 -- parent type, not from the internal full view.
6418
6419 Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
6420
6421 if Derive_Subps then
6422 Derive_Subprograms (Parent_Type, Derived_Type);
6423 end if;
6424
6425 else
6426 -- Untagged type, No discriminants on either view
6427
6428 if Nkind (Subtype_Indication (Type_Definition (N))) =
6429 N_Subtype_Indication
6430 then
6431 Error_Msg_N
6432 ("illegal constraint on type without discriminants", N);
6433 end if;
6434
6435 if Present (Discriminant_Specifications (N))
6436 and then Present (Full_View (Parent_Type))
6437 and then not Is_Tagged_Type (Full_View (Parent_Type))
6438 then
6439 Error_Msg_N ("cannot add discriminants to untagged type", N);
6440 end if;
6441
6442 Set_Stored_Constraint (Derived_Type, No_Elist);
6443 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
6444 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
6445 Set_Has_Controlled_Component
6446 (Derived_Type, Has_Controlled_Component
6447 (Parent_Type));
6448
6449 -- Direct controlled types do not inherit Finalize_Storage_Only flag
6450
6451 if not Is_Controlled (Parent_Type) then
6452 Set_Finalize_Storage_Only
6453 (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
6454 end if;
6455
6456 -- Construct the implicit full view by deriving from full view of the
6457 -- parent type. In order to get proper visibility, we install the
6458 -- parent scope and its declarations.
6459
6460 -- ??? If the parent is untagged private and its completion is
6461 -- tagged, this mechanism will not work because we cannot derive from
6462 -- the tagged full view unless we have an extension.
6463
6464 if Present (Full_View (Parent_Type))
6465 and then not Is_Tagged_Type (Full_View (Parent_Type))
6466 and then not Is_Completion
6467 then
6468 Full_Der :=
6469 Make_Defining_Identifier
6470 (Sloc (Derived_Type), Chars (Derived_Type));
6471 Set_Is_Itype (Full_Der);
6472 Set_Has_Private_Declaration (Full_Der);
6473 Set_Has_Private_Declaration (Derived_Type);
6474 Set_Associated_Node_For_Itype (Full_Der, N);
6475 Set_Parent (Full_Der, Parent (Derived_Type));
6476 Set_Full_View (Derived_Type, Full_Der);
6477
6478 if not In_Open_Scopes (Par_Scope) then
6479 Install_Private_Declarations (Par_Scope);
6480 Install_Visible_Declarations (Par_Scope);
6481 Copy_And_Build;
6482 Uninstall_Declarations (Par_Scope);
6483
6484 -- If parent scope is open and in another unit, and parent has a
6485 -- completion, then the derivation is taking place in the visible
6486 -- part of a child unit. In that case retrieve the full view of
6487 -- the parent momentarily.
6488
6489 elsif not In_Same_Source_Unit (N, Parent_Type) then
6490 Full_P := Full_View (Parent_Type);
6491 Exchange_Declarations (Parent_Type);
6492 Copy_And_Build;
6493 Exchange_Declarations (Full_P);
6494
6495 -- Otherwise it is a local derivation
6496
6497 else
6498 Copy_And_Build;
6499 end if;
6500
6501 Set_Scope (Full_Der, Current_Scope);
6502 Set_Is_First_Subtype (Full_Der,
6503 Is_First_Subtype (Derived_Type));
6504 Set_Has_Size_Clause (Full_Der, False);
6505 Set_Has_Alignment_Clause (Full_Der, False);
6506 Set_Next_Entity (Full_Der, Empty);
6507 Set_Has_Delayed_Freeze (Full_Der);
6508 Set_Is_Frozen (Full_Der, False);
6509 Set_Freeze_Node (Full_Der, Empty);
6510 Set_Depends_On_Private (Full_Der,
6511 Has_Private_Component (Full_Der));
6512 Set_Public_Status (Full_Der);
6513 end if;
6514 end if;
6515
6516 Set_Has_Unknown_Discriminants (Derived_Type,
6517 Has_Unknown_Discriminants (Parent_Type));
6518
6519 if Is_Private_Type (Derived_Type) then
6520 Set_Private_Dependents (Derived_Type, New_Elmt_List);
6521 end if;
6522
6523 if Is_Private_Type (Parent_Type)
6524 and then Base_Type (Parent_Type) = Parent_Type
6525 and then In_Open_Scopes (Scope (Parent_Type))
6526 then
6527 Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
6528
6529 if Is_Child_Unit (Scope (Current_Scope))
6530 and then Is_Completion
6531 and then In_Private_Part (Current_Scope)
6532 and then Scope (Parent_Type) /= Current_Scope
6533 then
6534 -- This is the unusual case where a type completed by a private
6535 -- derivation occurs within a package nested in a child unit, and
6536 -- the parent is declared in an ancestor. In this case, the full
6537 -- view of the parent type will become visible in the body of
6538 -- the enclosing child, and only then will the current type be
6539 -- possibly non-private. We build a underlying full view that
6540 -- will be installed when the enclosing child body is compiled.
6541
6542 Full_Der :=
6543 Make_Defining_Identifier
6544 (Sloc (Derived_Type), Chars (Derived_Type));
6545 Set_Is_Itype (Full_Der);
6546 Build_Itype_Reference (Full_Der, N);
6547
6548 -- The full view will be used to swap entities on entry/exit to
6549 -- the body, and must appear in the entity list for the package.
6550
6551 Append_Entity (Full_Der, Scope (Derived_Type));
6552 Set_Has_Private_Declaration (Full_Der);
6553 Set_Has_Private_Declaration (Derived_Type);
6554 Set_Associated_Node_For_Itype (Full_Der, N);
6555 Set_Parent (Full_Der, Parent (Derived_Type));
6556 Full_P := Full_View (Parent_Type);
6557 Exchange_Declarations (Parent_Type);
6558 Copy_And_Build;
6559 Exchange_Declarations (Full_P);
6560 Set_Underlying_Full_View (Derived_Type, Full_Der);
6561 end if;
6562 end if;
6563 end Build_Derived_Private_Type;
6564
6565 -------------------------------
6566 -- Build_Derived_Record_Type --
6567 -------------------------------
6568
6569 -- 1. INTRODUCTION
6570
6571 -- Ideally we would like to use the same model of type derivation for
6572 -- tagged and untagged record types. Unfortunately this is not quite
6573 -- possible because the semantics of representation clauses is different
6574 -- for tagged and untagged records under inheritance. Consider the
6575 -- following:
6576
6577 -- type R (...) is [tagged] record ... end record;
6578 -- type T (...) is new R (...) [with ...];
6579
6580 -- The representation clauses for T can specify a completely different
6581 -- record layout from R's. Hence the same component can be placed in two
6582 -- very different positions in objects of type T and R. If R and T are
6583 -- tagged types, representation clauses for T can only specify the layout
6584 -- of non inherited components, thus components that are common in R and T
6585 -- have the same position in objects of type R and T.
6586
6587 -- This has two implications. The first is that the entire tree for R's
6588 -- declaration needs to be copied for T in the untagged case, so that T
6589 -- can be viewed as a record type of its own with its own representation
6590 -- clauses. The second implication is the way we handle discriminants.
6591 -- Specifically, in the untagged case we need a way to communicate to Gigi
6592 -- what are the real discriminants in the record, while for the semantics
6593 -- we need to consider those introduced by the user to rename the
6594 -- discriminants in the parent type. This is handled by introducing the
6595 -- notion of stored discriminants. See below for more.
6596
6597 -- Fortunately the way regular components are inherited can be handled in
6598 -- the same way in tagged and untagged types.
6599
6600 -- To complicate things a bit more the private view of a private extension
6601 -- cannot be handled in the same way as the full view (for one thing the
6602 -- semantic rules are somewhat different). We will explain what differs
6603 -- below.
6604
6605 -- 2. DISCRIMINANTS UNDER INHERITANCE
6606
6607 -- The semantic rules governing the discriminants of derived types are
6608 -- quite subtle.
6609
6610 -- type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
6611 -- [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
6612
6613 -- If parent type has discriminants, then the discriminants that are
6614 -- declared in the derived type are [3.4 (11)]:
6615
6616 -- o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
6617 -- there is one;
6618
6619 -- o Otherwise, each discriminant of the parent type (implicitly declared
6620 -- in the same order with the same specifications). In this case, the
6621 -- discriminants are said to be "inherited", or if unknown in the parent
6622 -- are also unknown in the derived type.
6623
6624 -- Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
6625
6626 -- o The parent subtype shall be constrained;
6627
6628 -- o If the parent type is not a tagged type, then each discriminant of
6629 -- the derived type shall be used in the constraint defining a parent
6630 -- subtype. [Implementation note: This ensures that the new discriminant
6631 -- can share storage with an existing discriminant.]
6632
6633 -- For the derived type each discriminant of the parent type is either
6634 -- inherited, constrained to equal some new discriminant of the derived
6635 -- type, or constrained to the value of an expression.
6636
6637 -- When inherited or constrained to equal some new discriminant, the
6638 -- parent discriminant and the discriminant of the derived type are said
6639 -- to "correspond".
6640
6641 -- If a discriminant of the parent type is constrained to a specific value
6642 -- in the derived type definition, then the discriminant is said to be
6643 -- "specified" by that derived type definition.
6644
6645 -- 3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
6646
6647 -- We have spoken about stored discriminants in point 1 (introduction)
6648 -- above. There are two sort of stored discriminants: implicit and
6649 -- explicit. As long as the derived type inherits the same discriminants as
6650 -- the root record type, stored discriminants are the same as regular
6651 -- discriminants, and are said to be implicit. However, if any discriminant
6652 -- in the root type was renamed in the derived type, then the derived
6653 -- type will contain explicit stored discriminants. Explicit stored
6654 -- discriminants are discriminants in addition to the semantically visible
6655 -- discriminants defined for the derived type. Stored discriminants are
6656 -- used by Gigi to figure out what are the physical discriminants in
6657 -- objects of the derived type (see precise definition in einfo.ads).
6658 -- As an example, consider the following:
6659
6660 -- type R (D1, D2, D3 : Int) is record ... end record;
6661 -- type T1 is new R;
6662 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
6663 -- type T3 is new T2;
6664 -- type T4 (Y : Int) is new T3 (Y, 99);
6665
6666 -- The following table summarizes the discriminants and stored
6667 -- discriminants in R and T1 through T4.
6668
6669 -- Type Discrim Stored Discrim Comment
6670 -- R (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in R
6671 -- T1 (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in T1
6672 -- T2 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T2
6673 -- T3 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T3
6674 -- T4 (Y) (D1, D2, D3) Girder discrims EXPLICIT in T4
6675
6676 -- Field Corresponding_Discriminant (abbreviated CD below) allows us to
6677 -- find the corresponding discriminant in the parent type, while
6678 -- Original_Record_Component (abbreviated ORC below), the actual physical
6679 -- component that is renamed. Finally the field Is_Completely_Hidden
6680 -- (abbreviated ICH below) is set for all explicit stored discriminants
6681 -- (see einfo.ads for more info). For the above example this gives:
6682
6683 -- Discrim CD ORC ICH
6684 -- ^^^^^^^ ^^ ^^^ ^^^
6685 -- D1 in R empty itself no
6686 -- D2 in R empty itself no
6687 -- D3 in R empty itself no
6688
6689 -- D1 in T1 D1 in R itself no
6690 -- D2 in T1 D2 in R itself no
6691 -- D3 in T1 D3 in R itself no
6692
6693 -- X1 in T2 D3 in T1 D3 in T2 no
6694 -- X2 in T2 D1 in T1 D1 in T2 no
6695 -- D1 in T2 empty itself yes
6696 -- D2 in T2 empty itself yes
6697 -- D3 in T2 empty itself yes
6698
6699 -- X1 in T3 X1 in T2 D3 in T3 no
6700 -- X2 in T3 X2 in T2 D1 in T3 no
6701 -- D1 in T3 empty itself yes
6702 -- D2 in T3 empty itself yes
6703 -- D3 in T3 empty itself yes
6704
6705 -- Y in T4 X1 in T3 D3 in T3 no
6706 -- D1 in T3 empty itself yes
6707 -- D2 in T3 empty itself yes
6708 -- D3 in T3 empty itself yes
6709
6710 -- 4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
6711
6712 -- Type derivation for tagged types is fairly straightforward. If no
6713 -- discriminants are specified by the derived type, these are inherited
6714 -- from the parent. No explicit stored discriminants are ever necessary.
6715 -- The only manipulation that is done to the tree is that of adding a
6716 -- _parent field with parent type and constrained to the same constraint
6717 -- specified for the parent in the derived type definition. For instance:
6718
6719 -- type R (D1, D2, D3 : Int) is tagged record ... end record;
6720 -- type T1 is new R with null record;
6721 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
6722
6723 -- are changed into:
6724
6725 -- type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
6726 -- _parent : R (D1, D2, D3);
6727 -- end record;
6728
6729 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
6730 -- _parent : T1 (X2, 88, X1);
6731 -- end record;
6732
6733 -- The discriminants actually present in R, T1 and T2 as well as their CD,
6734 -- ORC and ICH fields are:
6735
6736 -- Discrim CD ORC ICH
6737 -- ^^^^^^^ ^^ ^^^ ^^^
6738 -- D1 in R empty itself no
6739 -- D2 in R empty itself no
6740 -- D3 in R empty itself no
6741
6742 -- D1 in T1 D1 in R D1 in R no
6743 -- D2 in T1 D2 in R D2 in R no
6744 -- D3 in T1 D3 in R D3 in R no
6745
6746 -- X1 in T2 D3 in T1 D3 in R no
6747 -- X2 in T2 D1 in T1 D1 in R no
6748
6749 -- 5. FIRST TRANSFORMATION FOR DERIVED RECORDS
6750 --
6751 -- Regardless of whether we dealing with a tagged or untagged type
6752 -- we will transform all derived type declarations of the form
6753 --
6754 -- type T is new R (...) [with ...];
6755 -- or
6756 -- subtype S is R (...);
6757 -- type T is new S [with ...];
6758 -- into
6759 -- type BT is new R [with ...];
6760 -- subtype T is BT (...);
6761 --
6762 -- That is, the base derived type is constrained only if it has no
6763 -- discriminants. The reason for doing this is that GNAT's semantic model
6764 -- assumes that a base type with discriminants is unconstrained.
6765 --
6766 -- Note that, strictly speaking, the above transformation is not always
6767 -- correct. Consider for instance the following excerpt from ACVC b34011a:
6768 --
6769 -- procedure B34011A is
6770 -- type REC (D : integer := 0) is record
6771 -- I : Integer;
6772 -- end record;
6773
6774 -- package P is
6775 -- type T6 is new Rec;
6776 -- function F return T6;
6777 -- end P;
6778
6779 -- use P;
6780 -- package Q6 is
6781 -- type U is new T6 (Q6.F.I); -- ERROR: Q6.F.
6782 -- end Q6;
6783 --
6784 -- The definition of Q6.U is illegal. However transforming Q6.U into
6785
6786 -- type BaseU is new T6;
6787 -- subtype U is BaseU (Q6.F.I)
6788
6789 -- turns U into a legal subtype, which is incorrect. To avoid this problem
6790 -- we always analyze the constraint (in this case (Q6.F.I)) before applying
6791 -- the transformation described above.
6792
6793 -- There is another instance where the above transformation is incorrect.
6794 -- Consider:
6795
6796 -- package Pack is
6797 -- type Base (D : Integer) is tagged null record;
6798 -- procedure P (X : Base);
6799
6800 -- type Der is new Base (2) with null record;
6801 -- procedure P (X : Der);
6802 -- end Pack;
6803
6804 -- Then the above transformation turns this into
6805
6806 -- type Der_Base is new Base with null record;
6807 -- -- procedure P (X : Base) is implicitly inherited here
6808 -- -- as procedure P (X : Der_Base).
6809
6810 -- subtype Der is Der_Base (2);
6811 -- procedure P (X : Der);
6812 -- -- The overriding of P (X : Der_Base) is illegal since we
6813 -- -- have a parameter conformance problem.
6814
6815 -- To get around this problem, after having semantically processed Der_Base
6816 -- and the rewritten subtype declaration for Der, we copy Der_Base field
6817 -- Discriminant_Constraint from Der so that when parameter conformance is
6818 -- checked when P is overridden, no semantic errors are flagged.
6819
6820 -- 6. SECOND TRANSFORMATION FOR DERIVED RECORDS
6821
6822 -- Regardless of whether we are dealing with a tagged or untagged type
6823 -- we will transform all derived type declarations of the form
6824
6825 -- type R (D1, .., Dn : ...) is [tagged] record ...;
6826 -- type T is new R [with ...];
6827 -- into
6828 -- type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
6829
6830 -- The reason for such transformation is that it allows us to implement a
6831 -- very clean form of component inheritance as explained below.
6832
6833 -- Note that this transformation is not achieved by direct tree rewriting
6834 -- and manipulation, but rather by redoing the semantic actions that the
6835 -- above transformation will entail. This is done directly in routine
6836 -- Inherit_Components.
6837
6838 -- 7. TYPE DERIVATION AND COMPONENT INHERITANCE
6839
6840 -- In both tagged and untagged derived types, regular non discriminant
6841 -- components are inherited in the derived type from the parent type. In
6842 -- the absence of discriminants component, inheritance is straightforward
6843 -- as components can simply be copied from the parent.
6844
6845 -- If the parent has discriminants, inheriting components constrained with
6846 -- these discriminants requires caution. Consider the following example:
6847
6848 -- type R (D1, D2 : Positive) is [tagged] record
6849 -- S : String (D1 .. D2);
6850 -- end record;
6851
6852 -- type T1 is new R [with null record];
6853 -- type T2 (X : positive) is new R (1, X) [with null record];
6854
6855 -- As explained in 6. above, T1 is rewritten as
6856 -- type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
6857 -- which makes the treatment for T1 and T2 identical.
6858
6859 -- What we want when inheriting S, is that references to D1 and D2 in R are
6860 -- replaced with references to their correct constraints, i.e. D1 and D2 in
6861 -- T1 and 1 and X in T2. So all R's discriminant references are replaced
6862 -- with either discriminant references in the derived type or expressions.
6863 -- This replacement is achieved as follows: before inheriting R's
6864 -- components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
6865 -- created in the scope of T1 (resp. scope of T2) so that discriminants D1
6866 -- and D2 of T1 are visible (resp. discriminant X of T2 is visible).
6867 -- For T2, for instance, this has the effect of replacing String (D1 .. D2)
6868 -- by String (1 .. X).
6869
6870 -- 8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
6871
6872 -- We explain here the rules governing private type extensions relevant to
6873 -- type derivation. These rules are explained on the following example:
6874
6875 -- type D [(...)] is new A [(...)] with private; <-- partial view
6876 -- type D [(...)] is new P [(...)] with null record; <-- full view
6877
6878 -- Type A is called the ancestor subtype of the private extension.
6879 -- Type P is the parent type of the full view of the private extension. It
6880 -- must be A or a type derived from A.
6881
6882 -- The rules concerning the discriminants of private type extensions are
6883 -- [7.3(10-13)]:
6884
6885 -- o If a private extension inherits known discriminants from the ancestor
6886 -- subtype, then the full view shall also inherit its discriminants from
6887 -- the ancestor subtype and the parent subtype of the full view shall be
6888 -- constrained if and only if the ancestor subtype is constrained.
6889
6890 -- o If a partial view has unknown discriminants, then the full view may
6891 -- define a definite or an indefinite subtype, with or without
6892 -- discriminants.
6893
6894 -- o If a partial view has neither known nor unknown discriminants, then
6895 -- the full view shall define a definite subtype.
6896
6897 -- o If the ancestor subtype of a private extension has constrained
6898 -- discriminants, then the parent subtype of the full view shall impose a
6899 -- statically matching constraint on those discriminants.
6900
6901 -- This means that only the following forms of private extensions are
6902 -- allowed:
6903
6904 -- type D is new A with private; <-- partial view
6905 -- type D is new P with null record; <-- full view
6906
6907 -- If A has no discriminants than P has no discriminants, otherwise P must
6908 -- inherit A's discriminants.
6909
6910 -- type D is new A (...) with private; <-- partial view
6911 -- type D is new P (:::) with null record; <-- full view
6912
6913 -- P must inherit A's discriminants and (...) and (:::) must statically
6914 -- match.
6915
6916 -- subtype A is R (...);
6917 -- type D is new A with private; <-- partial view
6918 -- type D is new P with null record; <-- full view
6919
6920 -- P must have inherited R's discriminants and must be derived from A or
6921 -- any of its subtypes.
6922
6923 -- type D (..) is new A with private; <-- partial view
6924 -- type D (..) is new P [(:::)] with null record; <-- full view
6925
6926 -- No specific constraints on P's discriminants or constraint (:::).
6927 -- Note that A can be unconstrained, but the parent subtype P must either
6928 -- be constrained or (:::) must be present.
6929
6930 -- type D (..) is new A [(...)] with private; <-- partial view
6931 -- type D (..) is new P [(:::)] with null record; <-- full view
6932
6933 -- P's constraints on A's discriminants must statically match those
6934 -- imposed by (...).
6935
6936 -- 9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
6937
6938 -- The full view of a private extension is handled exactly as described
6939 -- above. The model chose for the private view of a private extension is
6940 -- the same for what concerns discriminants (i.e. they receive the same
6941 -- treatment as in the tagged case). However, the private view of the
6942 -- private extension always inherits the components of the parent base,
6943 -- without replacing any discriminant reference. Strictly speaking this is
6944 -- incorrect. However, Gigi never uses this view to generate code so this
6945 -- is a purely semantic issue. In theory, a set of transformations similar
6946 -- to those given in 5. and 6. above could be applied to private views of
6947 -- private extensions to have the same model of component inheritance as
6948 -- for non private extensions. However, this is not done because it would
6949 -- further complicate private type processing. Semantically speaking, this
6950 -- leaves us in an uncomfortable situation. As an example consider:
6951
6952 -- package Pack is
6953 -- type R (D : integer) is tagged record
6954 -- S : String (1 .. D);
6955 -- end record;
6956 -- procedure P (X : R);
6957 -- type T is new R (1) with private;
6958 -- private
6959 -- type T is new R (1) with null record;
6960 -- end;
6961
6962 -- This is transformed into:
6963
6964 -- package Pack is
6965 -- type R (D : integer) is tagged record
6966 -- S : String (1 .. D);
6967 -- end record;
6968 -- procedure P (X : R);
6969 -- type T is new R (1) with private;
6970 -- private
6971 -- type BaseT is new R with null record;
6972 -- subtype T is BaseT (1);
6973 -- end;
6974
6975 -- (strictly speaking the above is incorrect Ada)
6976
6977 -- From the semantic standpoint the private view of private extension T
6978 -- should be flagged as constrained since one can clearly have
6979 --
6980 -- Obj : T;
6981 --
6982 -- in a unit withing Pack. However, when deriving subprograms for the
6983 -- private view of private extension T, T must be seen as unconstrained
6984 -- since T has discriminants (this is a constraint of the current
6985 -- subprogram derivation model). Thus, when processing the private view of
6986 -- a private extension such as T, we first mark T as unconstrained, we
6987 -- process it, we perform program derivation and just before returning from
6988 -- Build_Derived_Record_Type we mark T as constrained.
6989
6990 -- ??? Are there are other uncomfortable cases that we will have to
6991 -- deal with.
6992
6993 -- 10. RECORD_TYPE_WITH_PRIVATE complications
6994
6995 -- Types that are derived from a visible record type and have a private
6996 -- extension present other peculiarities. They behave mostly like private
6997 -- types, but if they have primitive operations defined, these will not
6998 -- have the proper signatures for further inheritance, because other
6999 -- primitive operations will use the implicit base that we define for
7000 -- private derivations below. This affect subprogram inheritance (see
7001 -- Derive_Subprograms for details). We also derive the implicit base from
7002 -- the base type of the full view, so that the implicit base is a record
7003 -- type and not another private type, This avoids infinite loops.
7004
7005 procedure Build_Derived_Record_Type
7006 (N : Node_Id;
7007 Parent_Type : Entity_Id;
7008 Derived_Type : Entity_Id;
7009 Derive_Subps : Boolean := True)
7010 is
7011 Discriminant_Specs : constant Boolean :=
7012 Present (Discriminant_Specifications (N));
7013 Is_Tagged : constant Boolean := Is_Tagged_Type (Parent_Type);
7014 Loc : constant Source_Ptr := Sloc (N);
7015 Private_Extension : constant Boolean :=
7016 Nkind (N) = N_Private_Extension_Declaration;
7017 Assoc_List : Elist_Id;
7018 Constraint_Present : Boolean;
7019 Constrs : Elist_Id;
7020 Discrim : Entity_Id;
7021 Indic : Node_Id;
7022 Inherit_Discrims : Boolean := False;
7023 Last_Discrim : Entity_Id;
7024 New_Base : Entity_Id;
7025 New_Decl : Node_Id;
7026 New_Discrs : Elist_Id;
7027 New_Indic : Node_Id;
7028 Parent_Base : Entity_Id;
7029 Save_Etype : Entity_Id;
7030 Save_Discr_Constr : Elist_Id;
7031 Save_Next_Entity : Entity_Id;
7032 Type_Def : Node_Id;
7033
7034 Discs : Elist_Id := New_Elmt_List;
7035 -- An empty Discs list means that there were no constraints in the
7036 -- subtype indication or that there was an error processing it.
7037
7038 begin
7039 if Ekind (Parent_Type) = E_Record_Type_With_Private
7040 and then Present (Full_View (Parent_Type))
7041 and then Has_Discriminants (Parent_Type)
7042 then
7043 Parent_Base := Base_Type (Full_View (Parent_Type));
7044 else
7045 Parent_Base := Base_Type (Parent_Type);
7046 end if;
7047
7048 -- AI05-0115 : if this is a derivation from a private type in some
7049 -- other scope that may lead to invisible components for the derived
7050 -- type, mark it accordingly.
7051
7052 if Is_Private_Type (Parent_Type) then
7053 if Scope (Parent_Type) = Scope (Derived_Type) then
7054 null;
7055
7056 elsif In_Open_Scopes (Scope (Parent_Type))
7057 and then In_Private_Part (Scope (Parent_Type))
7058 then
7059 null;
7060
7061 else
7062 Set_Has_Private_Ancestor (Derived_Type);
7063 end if;
7064
7065 else
7066 Set_Has_Private_Ancestor
7067 (Derived_Type, Has_Private_Ancestor (Parent_Type));
7068 end if;
7069
7070 -- Before we start the previously documented transformations, here is
7071 -- little fix for size and alignment of tagged types. Normally when we
7072 -- derive type D from type P, we copy the size and alignment of P as the
7073 -- default for D, and in the absence of explicit representation clauses
7074 -- for D, the size and alignment are indeed the same as the parent.
7075
7076 -- But this is wrong for tagged types, since fields may be added, and
7077 -- the default size may need to be larger, and the default alignment may
7078 -- need to be larger.
7079
7080 -- We therefore reset the size and alignment fields in the tagged case.
7081 -- Note that the size and alignment will in any case be at least as
7082 -- large as the parent type (since the derived type has a copy of the
7083 -- parent type in the _parent field)
7084
7085 -- The type is also marked as being tagged here, which is needed when
7086 -- processing components with a self-referential anonymous access type
7087 -- in the call to Check_Anonymous_Access_Components below. Note that
7088 -- this flag is also set later on for completeness.
7089
7090 if Is_Tagged then
7091 Set_Is_Tagged_Type (Derived_Type);
7092 Init_Size_Align (Derived_Type);
7093 end if;
7094
7095 -- STEP 0a: figure out what kind of derived type declaration we have
7096
7097 if Private_Extension then
7098 Type_Def := N;
7099 Set_Ekind (Derived_Type, E_Record_Type_With_Private);
7100
7101 else
7102 Type_Def := Type_Definition (N);
7103
7104 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
7105 -- Parent_Base can be a private type or private extension. However,
7106 -- for tagged types with an extension the newly added fields are
7107 -- visible and hence the Derived_Type is always an E_Record_Type.
7108 -- (except that the parent may have its own private fields).
7109 -- For untagged types we preserve the Ekind of the Parent_Base.
7110
7111 if Present (Record_Extension_Part (Type_Def)) then
7112 Set_Ekind (Derived_Type, E_Record_Type);
7113
7114 -- Create internal access types for components with anonymous
7115 -- access types.
7116
7117 if Ada_Version >= Ada_2005 then
7118 Check_Anonymous_Access_Components
7119 (N, Derived_Type, Derived_Type,
7120 Component_List (Record_Extension_Part (Type_Def)));
7121 end if;
7122
7123 else
7124 Set_Ekind (Derived_Type, Ekind (Parent_Base));
7125 end if;
7126 end if;
7127
7128 -- Indic can either be an N_Identifier if the subtype indication
7129 -- contains no constraint or an N_Subtype_Indication if the subtype
7130 -- indication has a constraint.
7131
7132 Indic := Subtype_Indication (Type_Def);
7133 Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
7134
7135 -- Check that the type has visible discriminants. The type may be
7136 -- a private type with unknown discriminants whose full view has
7137 -- discriminants which are invisible.
7138
7139 if Constraint_Present then
7140 if not Has_Discriminants (Parent_Base)
7141 or else
7142 (Has_Unknown_Discriminants (Parent_Base)
7143 and then Is_Private_Type (Parent_Base))
7144 then
7145 Error_Msg_N
7146 ("invalid constraint: type has no discriminant",
7147 Constraint (Indic));
7148
7149 Constraint_Present := False;
7150 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
7151
7152 elsif Is_Constrained (Parent_Type) then
7153 Error_Msg_N
7154 ("invalid constraint: parent type is already constrained",
7155 Constraint (Indic));
7156
7157 Constraint_Present := False;
7158 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
7159 end if;
7160 end if;
7161
7162 -- STEP 0b: If needed, apply transformation given in point 5. above
7163
7164 if not Private_Extension
7165 and then Has_Discriminants (Parent_Type)
7166 and then not Discriminant_Specs
7167 and then (Is_Constrained (Parent_Type) or else Constraint_Present)
7168 then
7169 -- First, we must analyze the constraint (see comment in point 5.)
7170
7171 if Constraint_Present then
7172 New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
7173
7174 if Has_Discriminants (Derived_Type)
7175 and then Has_Private_Declaration (Derived_Type)
7176 and then Present (Discriminant_Constraint (Derived_Type))
7177 then
7178 -- Verify that constraints of the full view statically match
7179 -- those given in the partial view.
7180
7181 declare
7182 C1, C2 : Elmt_Id;
7183
7184 begin
7185 C1 := First_Elmt (New_Discrs);
7186 C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
7187 while Present (C1) and then Present (C2) loop
7188 if Fully_Conformant_Expressions (Node (C1), Node (C2))
7189 or else
7190 (Is_OK_Static_Expression (Node (C1))
7191 and then
7192 Is_OK_Static_Expression (Node (C2))
7193 and then
7194 Expr_Value (Node (C1)) = Expr_Value (Node (C2)))
7195 then
7196 null;
7197
7198 else
7199 Error_Msg_N (
7200 "constraint not conformant to previous declaration",
7201 Node (C1));
7202 end if;
7203
7204 Next_Elmt (C1);
7205 Next_Elmt (C2);
7206 end loop;
7207 end;
7208 end if;
7209 end if;
7210
7211 -- Insert and analyze the declaration for the unconstrained base type
7212
7213 New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
7214
7215 New_Decl :=
7216 Make_Full_Type_Declaration (Loc,
7217 Defining_Identifier => New_Base,
7218 Type_Definition =>
7219 Make_Derived_Type_Definition (Loc,
7220 Abstract_Present => Abstract_Present (Type_Def),
7221 Limited_Present => Limited_Present (Type_Def),
7222 Subtype_Indication =>
7223 New_Occurrence_Of (Parent_Base, Loc),
7224 Record_Extension_Part =>
7225 Relocate_Node (Record_Extension_Part (Type_Def)),
7226 Interface_List => Interface_List (Type_Def)));
7227
7228 Set_Parent (New_Decl, Parent (N));
7229 Mark_Rewrite_Insertion (New_Decl);
7230 Insert_Before (N, New_Decl);
7231
7232 -- In the extension case, make sure ancestor is frozen appropriately
7233 -- (see also non-discriminated case below).
7234
7235 if Present (Record_Extension_Part (Type_Def))
7236 or else Is_Interface (Parent_Base)
7237 then
7238 Freeze_Before (New_Decl, Parent_Type);
7239 end if;
7240
7241 -- Note that this call passes False for the Derive_Subps parameter
7242 -- because subprogram derivation is deferred until after creating
7243 -- the subtype (see below).
7244
7245 Build_Derived_Type
7246 (New_Decl, Parent_Base, New_Base,
7247 Is_Completion => True, Derive_Subps => False);
7248
7249 -- ??? This needs re-examination to determine whether the
7250 -- above call can simply be replaced by a call to Analyze.
7251
7252 Set_Analyzed (New_Decl);
7253
7254 -- Insert and analyze the declaration for the constrained subtype
7255
7256 if Constraint_Present then
7257 New_Indic :=
7258 Make_Subtype_Indication (Loc,
7259 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
7260 Constraint => Relocate_Node (Constraint (Indic)));
7261
7262 else
7263 declare
7264 Constr_List : constant List_Id := New_List;
7265 C : Elmt_Id;
7266 Expr : Node_Id;
7267
7268 begin
7269 C := First_Elmt (Discriminant_Constraint (Parent_Type));
7270 while Present (C) loop
7271 Expr := Node (C);
7272
7273 -- It is safe here to call New_Copy_Tree since
7274 -- Force_Evaluation was called on each constraint in
7275 -- Build_Discriminant_Constraints.
7276
7277 Append (New_Copy_Tree (Expr), To => Constr_List);
7278
7279 Next_Elmt (C);
7280 end loop;
7281
7282 New_Indic :=
7283 Make_Subtype_Indication (Loc,
7284 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
7285 Constraint =>
7286 Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
7287 end;
7288 end if;
7289
7290 Rewrite (N,
7291 Make_Subtype_Declaration (Loc,
7292 Defining_Identifier => Derived_Type,
7293 Subtype_Indication => New_Indic));
7294
7295 Analyze (N);
7296
7297 -- Derivation of subprograms must be delayed until the full subtype
7298 -- has been established, to ensure proper overriding of subprograms
7299 -- inherited by full types. If the derivations occurred as part of
7300 -- the call to Build_Derived_Type above, then the check for type
7301 -- conformance would fail because earlier primitive subprograms
7302 -- could still refer to the full type prior the change to the new
7303 -- subtype and hence would not match the new base type created here.
7304 -- Subprograms are not derived, however, when Derive_Subps is False
7305 -- (since otherwise there could be redundant derivations).
7306
7307 if Derive_Subps then
7308 Derive_Subprograms (Parent_Type, Derived_Type);
7309 end if;
7310
7311 -- For tagged types the Discriminant_Constraint of the new base itype
7312 -- is inherited from the first subtype so that no subtype conformance
7313 -- problem arise when the first subtype overrides primitive
7314 -- operations inherited by the implicit base type.
7315
7316 if Is_Tagged then
7317 Set_Discriminant_Constraint
7318 (New_Base, Discriminant_Constraint (Derived_Type));
7319 end if;
7320
7321 return;
7322 end if;
7323
7324 -- If we get here Derived_Type will have no discriminants or it will be
7325 -- a discriminated unconstrained base type.
7326
7327 -- STEP 1a: perform preliminary actions/checks for derived tagged types
7328
7329 if Is_Tagged then
7330
7331 -- The parent type is frozen for non-private extensions (RM 13.14(7))
7332 -- The declaration of a specific descendant of an interface type
7333 -- freezes the interface type (RM 13.14).
7334
7335 if not Private_Extension or else Is_Interface (Parent_Base) then
7336 Freeze_Before (N, Parent_Type);
7337 end if;
7338
7339 -- In Ada 2005 (AI-344), the restriction that a derived tagged type
7340 -- cannot be declared at a deeper level than its parent type is
7341 -- removed. The check on derivation within a generic body is also
7342 -- relaxed, but there's a restriction that a derived tagged type
7343 -- cannot be declared in a generic body if it's derived directly
7344 -- or indirectly from a formal type of that generic.
7345
7346 if Ada_Version >= Ada_2005 then
7347 if Present (Enclosing_Generic_Body (Derived_Type)) then
7348 declare
7349 Ancestor_Type : Entity_Id;
7350
7351 begin
7352 -- Check to see if any ancestor of the derived type is a
7353 -- formal type.
7354
7355 Ancestor_Type := Parent_Type;
7356 while not Is_Generic_Type (Ancestor_Type)
7357 and then Etype (Ancestor_Type) /= Ancestor_Type
7358 loop
7359 Ancestor_Type := Etype (Ancestor_Type);
7360 end loop;
7361
7362 -- If the derived type does have a formal type as an
7363 -- ancestor, then it's an error if the derived type is
7364 -- declared within the body of the generic unit that
7365 -- declares the formal type in its generic formal part. It's
7366 -- sufficient to check whether the ancestor type is declared
7367 -- inside the same generic body as the derived type (such as
7368 -- within a nested generic spec), in which case the
7369 -- derivation is legal. If the formal type is declared
7370 -- outside of that generic body, then it's guaranteed that
7371 -- the derived type is declared within the generic body of
7372 -- the generic unit declaring the formal type.
7373
7374 if Is_Generic_Type (Ancestor_Type)
7375 and then Enclosing_Generic_Body (Ancestor_Type) /=
7376 Enclosing_Generic_Body (Derived_Type)
7377 then
7378 Error_Msg_NE
7379 ("parent type of& must not be descendant of formal type"
7380 & " of an enclosing generic body",
7381 Indic, Derived_Type);
7382 end if;
7383 end;
7384 end if;
7385
7386 elsif Type_Access_Level (Derived_Type) /=
7387 Type_Access_Level (Parent_Type)
7388 and then not Is_Generic_Type (Derived_Type)
7389 then
7390 if Is_Controlled (Parent_Type) then
7391 Error_Msg_N
7392 ("controlled type must be declared at the library level",
7393 Indic);
7394 else
7395 Error_Msg_N
7396 ("type extension at deeper accessibility level than parent",
7397 Indic);
7398 end if;
7399
7400 else
7401 declare
7402 GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
7403
7404 begin
7405 if Present (GB)
7406 and then GB /= Enclosing_Generic_Body (Parent_Base)
7407 then
7408 Error_Msg_NE
7409 ("parent type of& must not be outside generic body"
7410 & " (RM 3.9.1(4))",
7411 Indic, Derived_Type);
7412 end if;
7413 end;
7414 end if;
7415 end if;
7416
7417 -- Ada 2005 (AI-251)
7418
7419 if Ada_Version >= Ada_2005 and then Is_Tagged then
7420
7421 -- "The declaration of a specific descendant of an interface type
7422 -- freezes the interface type" (RM 13.14).
7423
7424 declare
7425 Iface : Node_Id;
7426 begin
7427 if Is_Non_Empty_List (Interface_List (Type_Def)) then
7428 Iface := First (Interface_List (Type_Def));
7429 while Present (Iface) loop
7430 Freeze_Before (N, Etype (Iface));
7431 Next (Iface);
7432 end loop;
7433 end if;
7434 end;
7435 end if;
7436
7437 -- STEP 1b : preliminary cleanup of the full view of private types
7438
7439 -- If the type is already marked as having discriminants, then it's the
7440 -- completion of a private type or private extension and we need to
7441 -- retain the discriminants from the partial view if the current
7442 -- declaration has Discriminant_Specifications so that we can verify
7443 -- conformance. However, we must remove any existing components that
7444 -- were inherited from the parent (and attached in Copy_And_Swap)
7445 -- because the full type inherits all appropriate components anyway, and
7446 -- we do not want the partial view's components interfering.
7447
7448 if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
7449 Discrim := First_Discriminant (Derived_Type);
7450 loop
7451 Last_Discrim := Discrim;
7452 Next_Discriminant (Discrim);
7453 exit when No (Discrim);
7454 end loop;
7455
7456 Set_Last_Entity (Derived_Type, Last_Discrim);
7457
7458 -- In all other cases wipe out the list of inherited components (even
7459 -- inherited discriminants), it will be properly rebuilt here.
7460
7461 else
7462 Set_First_Entity (Derived_Type, Empty);
7463 Set_Last_Entity (Derived_Type, Empty);
7464 end if;
7465
7466 -- STEP 1c: Initialize some flags for the Derived_Type
7467
7468 -- The following flags must be initialized here so that
7469 -- Process_Discriminants can check that discriminants of tagged types do
7470 -- not have a default initial value and that access discriminants are
7471 -- only specified for limited records. For completeness, these flags are
7472 -- also initialized along with all the other flags below.
7473
7474 -- AI-419: Limitedness is not inherited from an interface parent, so to
7475 -- be limited in that case the type must be explicitly declared as
7476 -- limited. However, task and protected interfaces are always limited.
7477
7478 if Limited_Present (Type_Def) then
7479 Set_Is_Limited_Record (Derived_Type);
7480
7481 elsif Is_Limited_Record (Parent_Type)
7482 or else (Present (Full_View (Parent_Type))
7483 and then Is_Limited_Record (Full_View (Parent_Type)))
7484 then
7485 if not Is_Interface (Parent_Type)
7486 or else Is_Synchronized_Interface (Parent_Type)
7487 or else Is_Protected_Interface (Parent_Type)
7488 or else Is_Task_Interface (Parent_Type)
7489 then
7490 Set_Is_Limited_Record (Derived_Type);
7491 end if;
7492 end if;
7493
7494 -- STEP 2a: process discriminants of derived type if any
7495
7496 Push_Scope (Derived_Type);
7497
7498 if Discriminant_Specs then
7499 Set_Has_Unknown_Discriminants (Derived_Type, False);
7500
7501 -- The following call initializes fields Has_Discriminants and
7502 -- Discriminant_Constraint, unless we are processing the completion
7503 -- of a private type declaration.
7504
7505 Check_Or_Process_Discriminants (N, Derived_Type);
7506
7507 -- For untagged types, the constraint on the Parent_Type must be
7508 -- present and is used to rename the discriminants.
7509
7510 if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
7511 Error_Msg_N ("untagged parent must have discriminants", Indic);
7512
7513 elsif not Is_Tagged and then not Constraint_Present then
7514 Error_Msg_N
7515 ("discriminant constraint needed for derived untagged records",
7516 Indic);
7517
7518 -- Otherwise the parent subtype must be constrained unless we have a
7519 -- private extension.
7520
7521 elsif not Constraint_Present
7522 and then not Private_Extension
7523 and then not Is_Constrained (Parent_Type)
7524 then
7525 Error_Msg_N
7526 ("unconstrained type not allowed in this context", Indic);
7527
7528 elsif Constraint_Present then
7529 -- The following call sets the field Corresponding_Discriminant
7530 -- for the discriminants in the Derived_Type.
7531
7532 Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
7533
7534 -- For untagged types all new discriminants must rename
7535 -- discriminants in the parent. For private extensions new
7536 -- discriminants cannot rename old ones (implied by [7.3(13)]).
7537
7538 Discrim := First_Discriminant (Derived_Type);
7539 while Present (Discrim) loop
7540 if not Is_Tagged
7541 and then No (Corresponding_Discriminant (Discrim))
7542 then
7543 Error_Msg_N
7544 ("new discriminants must constrain old ones", Discrim);
7545
7546 elsif Private_Extension
7547 and then Present (Corresponding_Discriminant (Discrim))
7548 then
7549 Error_Msg_N
7550 ("only static constraints allowed for parent"
7551 & " discriminants in the partial view", Indic);
7552 exit;
7553 end if;
7554
7555 -- If a new discriminant is used in the constraint, then its
7556 -- subtype must be statically compatible with the parent
7557 -- discriminant's subtype (3.7(15)).
7558
7559 if Present (Corresponding_Discriminant (Discrim))
7560 and then
7561 not Subtypes_Statically_Compatible
7562 (Etype (Discrim),
7563 Etype (Corresponding_Discriminant (Discrim)))
7564 then
7565 Error_Msg_N
7566 ("subtype must be compatible with parent discriminant",
7567 Discrim);
7568 end if;
7569
7570 Next_Discriminant (Discrim);
7571 end loop;
7572
7573 -- Check whether the constraints of the full view statically
7574 -- match those imposed by the parent subtype [7.3(13)].
7575
7576 if Present (Stored_Constraint (Derived_Type)) then
7577 declare
7578 C1, C2 : Elmt_Id;
7579
7580 begin
7581 C1 := First_Elmt (Discs);
7582 C2 := First_Elmt (Stored_Constraint (Derived_Type));
7583 while Present (C1) and then Present (C2) loop
7584 if not
7585 Fully_Conformant_Expressions (Node (C1), Node (C2))
7586 then
7587 Error_Msg_N
7588 ("not conformant with previous declaration",
7589 Node (C1));
7590 end if;
7591
7592 Next_Elmt (C1);
7593 Next_Elmt (C2);
7594 end loop;
7595 end;
7596 end if;
7597 end if;
7598
7599 -- STEP 2b: No new discriminants, inherit discriminants if any
7600
7601 else
7602 if Private_Extension then
7603 Set_Has_Unknown_Discriminants
7604 (Derived_Type,
7605 Has_Unknown_Discriminants (Parent_Type)
7606 or else Unknown_Discriminants_Present (N));
7607
7608 -- The partial view of the parent may have unknown discriminants,
7609 -- but if the full view has discriminants and the parent type is
7610 -- in scope they must be inherited.
7611
7612 elsif Has_Unknown_Discriminants (Parent_Type)
7613 and then
7614 (not Has_Discriminants (Parent_Type)
7615 or else not In_Open_Scopes (Scope (Parent_Type)))
7616 then
7617 Set_Has_Unknown_Discriminants (Derived_Type);
7618 end if;
7619
7620 if not Has_Unknown_Discriminants (Derived_Type)
7621 and then not Has_Unknown_Discriminants (Parent_Base)
7622 and then Has_Discriminants (Parent_Type)
7623 then
7624 Inherit_Discrims := True;
7625 Set_Has_Discriminants
7626 (Derived_Type, True);
7627 Set_Discriminant_Constraint
7628 (Derived_Type, Discriminant_Constraint (Parent_Base));
7629 end if;
7630
7631 -- The following test is true for private types (remember
7632 -- transformation 5. is not applied to those) and in an error
7633 -- situation.
7634
7635 if Constraint_Present then
7636 Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
7637 end if;
7638
7639 -- For now mark a new derived type as constrained only if it has no
7640 -- discriminants. At the end of Build_Derived_Record_Type we properly
7641 -- set this flag in the case of private extensions. See comments in
7642 -- point 9. just before body of Build_Derived_Record_Type.
7643
7644 Set_Is_Constrained
7645 (Derived_Type,
7646 not (Inherit_Discrims
7647 or else Has_Unknown_Discriminants (Derived_Type)));
7648 end if;
7649
7650 -- STEP 3: initialize fields of derived type
7651
7652 Set_Is_Tagged_Type (Derived_Type, Is_Tagged);
7653 Set_Stored_Constraint (Derived_Type, No_Elist);
7654
7655 -- Ada 2005 (AI-251): Private type-declarations can implement interfaces
7656 -- but cannot be interfaces
7657
7658 if not Private_Extension
7659 and then Ekind (Derived_Type) /= E_Private_Type
7660 and then Ekind (Derived_Type) /= E_Limited_Private_Type
7661 then
7662 if Interface_Present (Type_Def) then
7663 Analyze_Interface_Declaration (Derived_Type, Type_Def);
7664 end if;
7665
7666 Set_Interfaces (Derived_Type, No_Elist);
7667 end if;
7668
7669 -- Fields inherited from the Parent_Type
7670
7671 Set_Discard_Names
7672 (Derived_Type, Einfo.Discard_Names (Parent_Type));
7673 Set_Has_Specified_Layout
7674 (Derived_Type, Has_Specified_Layout (Parent_Type));
7675 Set_Is_Limited_Composite
7676 (Derived_Type, Is_Limited_Composite (Parent_Type));
7677 Set_Is_Private_Composite
7678 (Derived_Type, Is_Private_Composite (Parent_Type));
7679
7680 -- Fields inherited from the Parent_Base
7681
7682 Set_Has_Controlled_Component
7683 (Derived_Type, Has_Controlled_Component (Parent_Base));
7684 Set_Has_Non_Standard_Rep
7685 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
7686 Set_Has_Primitive_Operations
7687 (Derived_Type, Has_Primitive_Operations (Parent_Base));
7688
7689 -- Fields inherited from the Parent_Base in the non-private case
7690
7691 if Ekind (Derived_Type) = E_Record_Type then
7692 Set_Has_Complex_Representation
7693 (Derived_Type, Has_Complex_Representation (Parent_Base));
7694 end if;
7695
7696 -- Fields inherited from the Parent_Base for record types
7697
7698 if Is_Record_Type (Derived_Type) then
7699
7700 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
7701 -- Parent_Base can be a private type or private extension.
7702
7703 if Present (Full_View (Parent_Base)) then
7704 Set_OK_To_Reorder_Components
7705 (Derived_Type,
7706 OK_To_Reorder_Components (Full_View (Parent_Base)));
7707 Set_Reverse_Bit_Order
7708 (Derived_Type, Reverse_Bit_Order (Full_View (Parent_Base)));
7709 else
7710 Set_OK_To_Reorder_Components
7711 (Derived_Type, OK_To_Reorder_Components (Parent_Base));
7712 Set_Reverse_Bit_Order
7713 (Derived_Type, Reverse_Bit_Order (Parent_Base));
7714 end if;
7715 end if;
7716
7717 -- Direct controlled types do not inherit Finalize_Storage_Only flag
7718
7719 if not Is_Controlled (Parent_Type) then
7720 Set_Finalize_Storage_Only
7721 (Derived_Type, Finalize_Storage_Only (Parent_Type));
7722 end if;
7723
7724 -- Set fields for private derived types
7725
7726 if Is_Private_Type (Derived_Type) then
7727 Set_Depends_On_Private (Derived_Type, True);
7728 Set_Private_Dependents (Derived_Type, New_Elmt_List);
7729
7730 -- Inherit fields from non private record types. If this is the
7731 -- completion of a derivation from a private type, the parent itself
7732 -- is private, and the attributes come from its full view, which must
7733 -- be present.
7734
7735 else
7736 if Is_Private_Type (Parent_Base)
7737 and then not Is_Record_Type (Parent_Base)
7738 then
7739 Set_Component_Alignment
7740 (Derived_Type, Component_Alignment (Full_View (Parent_Base)));
7741 Set_C_Pass_By_Copy
7742 (Derived_Type, C_Pass_By_Copy (Full_View (Parent_Base)));
7743 else
7744 Set_Component_Alignment
7745 (Derived_Type, Component_Alignment (Parent_Base));
7746 Set_C_Pass_By_Copy
7747 (Derived_Type, C_Pass_By_Copy (Parent_Base));
7748 end if;
7749 end if;
7750
7751 -- Set fields for tagged types
7752
7753 if Is_Tagged then
7754 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
7755
7756 -- All tagged types defined in Ada.Finalization are controlled
7757
7758 if Chars (Scope (Derived_Type)) = Name_Finalization
7759 and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
7760 and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
7761 then
7762 Set_Is_Controlled (Derived_Type);
7763 else
7764 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Base));
7765 end if;
7766
7767 -- Minor optimization: there is no need to generate the class-wide
7768 -- entity associated with an underlying record view.
7769
7770 if not Is_Underlying_Record_View (Derived_Type) then
7771 Make_Class_Wide_Type (Derived_Type);
7772 end if;
7773
7774 Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
7775
7776 if Has_Discriminants (Derived_Type)
7777 and then Constraint_Present
7778 then
7779 Set_Stored_Constraint
7780 (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
7781 end if;
7782
7783 if Ada_Version >= Ada_2005 then
7784 declare
7785 Ifaces_List : Elist_Id;
7786
7787 begin
7788 -- Checks rules 3.9.4 (13/2 and 14/2)
7789
7790 if Comes_From_Source (Derived_Type)
7791 and then not Is_Private_Type (Derived_Type)
7792 and then Is_Interface (Parent_Type)
7793 and then not Is_Interface (Derived_Type)
7794 then
7795 if Is_Task_Interface (Parent_Type) then
7796 Error_Msg_N
7797 ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
7798 Derived_Type);
7799
7800 elsif Is_Protected_Interface (Parent_Type) then
7801 Error_Msg_N
7802 ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
7803 Derived_Type);
7804 end if;
7805 end if;
7806
7807 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
7808
7809 Check_Interfaces (N, Type_Def);
7810
7811 -- Ada 2005 (AI-251): Collect the list of progenitors that are
7812 -- not already in the parents.
7813
7814 Collect_Interfaces
7815 (T => Derived_Type,
7816 Ifaces_List => Ifaces_List,
7817 Exclude_Parents => True);
7818
7819 Set_Interfaces (Derived_Type, Ifaces_List);
7820
7821 -- If the derived type is the anonymous type created for
7822 -- a declaration whose parent has a constraint, propagate
7823 -- the interface list to the source type. This must be done
7824 -- prior to the completion of the analysis of the source type
7825 -- because the components in the extension may contain current
7826 -- instances whose legality depends on some ancestor.
7827
7828 if Is_Itype (Derived_Type) then
7829 declare
7830 Def : constant Node_Id :=
7831 Associated_Node_For_Itype (Derived_Type);
7832 begin
7833 if Present (Def)
7834 and then Nkind (Def) = N_Full_Type_Declaration
7835 then
7836 Set_Interfaces
7837 (Defining_Identifier (Def), Ifaces_List);
7838 end if;
7839 end;
7840 end if;
7841 end;
7842 end if;
7843
7844 else
7845 Set_Is_Packed (Derived_Type, Is_Packed (Parent_Base));
7846 Set_Has_Non_Standard_Rep
7847 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
7848 end if;
7849
7850 -- STEP 4: Inherit components from the parent base and constrain them.
7851 -- Apply the second transformation described in point 6. above.
7852
7853 if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
7854 or else not Has_Discriminants (Parent_Type)
7855 or else not Is_Constrained (Parent_Type)
7856 then
7857 Constrs := Discs;
7858 else
7859 Constrs := Discriminant_Constraint (Parent_Type);
7860 end if;
7861
7862 Assoc_List :=
7863 Inherit_Components
7864 (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
7865
7866 -- STEP 5a: Copy the parent record declaration for untagged types
7867
7868 if not Is_Tagged then
7869
7870 -- Discriminant_Constraint (Derived_Type) has been properly
7871 -- constructed. Save it and temporarily set it to Empty because we
7872 -- do not want the call to New_Copy_Tree below to mess this list.
7873
7874 if Has_Discriminants (Derived_Type) then
7875 Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
7876 Set_Discriminant_Constraint (Derived_Type, No_Elist);
7877 else
7878 Save_Discr_Constr := No_Elist;
7879 end if;
7880
7881 -- Save the Etype field of Derived_Type. It is correctly set now,
7882 -- but the call to New_Copy tree may remap it to point to itself,
7883 -- which is not what we want. Ditto for the Next_Entity field.
7884
7885 Save_Etype := Etype (Derived_Type);
7886 Save_Next_Entity := Next_Entity (Derived_Type);
7887
7888 -- Assoc_List maps all stored discriminants in the Parent_Base to
7889 -- stored discriminants in the Derived_Type. It is fundamental that
7890 -- no types or itypes with discriminants other than the stored
7891 -- discriminants appear in the entities declared inside
7892 -- Derived_Type, since the back end cannot deal with it.
7893
7894 New_Decl :=
7895 New_Copy_Tree
7896 (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
7897
7898 -- Restore the fields saved prior to the New_Copy_Tree call
7899 -- and compute the stored constraint.
7900
7901 Set_Etype (Derived_Type, Save_Etype);
7902 Set_Next_Entity (Derived_Type, Save_Next_Entity);
7903
7904 if Has_Discriminants (Derived_Type) then
7905 Set_Discriminant_Constraint
7906 (Derived_Type, Save_Discr_Constr);
7907 Set_Stored_Constraint
7908 (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
7909 Replace_Components (Derived_Type, New_Decl);
7910 Set_Has_Implicit_Dereference
7911 (Derived_Type, Has_Implicit_Dereference (Parent_Type));
7912 end if;
7913
7914 -- Insert the new derived type declaration
7915
7916 Rewrite (N, New_Decl);
7917
7918 -- STEP 5b: Complete the processing for record extensions in generics
7919
7920 -- There is no completion for record extensions declared in the
7921 -- parameter part of a generic, so we need to complete processing for
7922 -- these generic record extensions here. The Record_Type_Definition call
7923 -- will change the Ekind of the components from E_Void to E_Component.
7924
7925 elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
7926 Record_Type_Definition (Empty, Derived_Type);
7927
7928 -- STEP 5c: Process the record extension for non private tagged types
7929
7930 elsif not Private_Extension then
7931
7932 -- Add the _parent field in the derived type
7933
7934 Expand_Record_Extension (Derived_Type, Type_Def);
7935
7936 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
7937 -- implemented interfaces if we are in expansion mode
7938
7939 if Expander_Active
7940 and then Has_Interfaces (Derived_Type)
7941 then
7942 Add_Interface_Tag_Components (N, Derived_Type);
7943 end if;
7944
7945 -- Analyze the record extension
7946
7947 Record_Type_Definition
7948 (Record_Extension_Part (Type_Def), Derived_Type);
7949 end if;
7950
7951 End_Scope;
7952
7953 -- Nothing else to do if there is an error in the derivation.
7954 -- An unusual case: the full view may be derived from a type in an
7955 -- instance, when the partial view was used illegally as an actual
7956 -- in that instance, leading to a circular definition.
7957
7958 if Etype (Derived_Type) = Any_Type
7959 or else Etype (Parent_Type) = Derived_Type
7960 then
7961 return;
7962 end if;
7963
7964 -- Set delayed freeze and then derive subprograms, we need to do
7965 -- this in this order so that derived subprograms inherit the
7966 -- derived freeze if necessary.
7967
7968 Set_Has_Delayed_Freeze (Derived_Type);
7969
7970 if Derive_Subps then
7971 Derive_Subprograms (Parent_Type, Derived_Type);
7972 end if;
7973
7974 -- If we have a private extension which defines a constrained derived
7975 -- type mark as constrained here after we have derived subprograms. See
7976 -- comment on point 9. just above the body of Build_Derived_Record_Type.
7977
7978 if Private_Extension and then Inherit_Discrims then
7979 if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
7980 Set_Is_Constrained (Derived_Type, True);
7981 Set_Discriminant_Constraint (Derived_Type, Discs);
7982
7983 elsif Is_Constrained (Parent_Type) then
7984 Set_Is_Constrained
7985 (Derived_Type, True);
7986 Set_Discriminant_Constraint
7987 (Derived_Type, Discriminant_Constraint (Parent_Type));
7988 end if;
7989 end if;
7990
7991 -- Update the class-wide type, which shares the now-completed entity
7992 -- list with its specific type. In case of underlying record views,
7993 -- we do not generate the corresponding class wide entity.
7994
7995 if Is_Tagged
7996 and then not Is_Underlying_Record_View (Derived_Type)
7997 then
7998 Set_First_Entity
7999 (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
8000 Set_Last_Entity
8001 (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
8002 end if;
8003 end Build_Derived_Record_Type;
8004
8005 ------------------------
8006 -- Build_Derived_Type --
8007 ------------------------
8008
8009 procedure Build_Derived_Type
8010 (N : Node_Id;
8011 Parent_Type : Entity_Id;
8012 Derived_Type : Entity_Id;
8013 Is_Completion : Boolean;
8014 Derive_Subps : Boolean := True)
8015 is
8016 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
8017
8018 begin
8019 -- Set common attributes
8020
8021 Set_Scope (Derived_Type, Current_Scope);
8022
8023 Set_Ekind (Derived_Type, Ekind (Parent_Base));
8024 Set_Etype (Derived_Type, Parent_Base);
8025 Set_Has_Task (Derived_Type, Has_Task (Parent_Base));
8026
8027 Set_Size_Info (Derived_Type, Parent_Type);
8028 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
8029 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
8030 Set_Is_Tagged_Type (Derived_Type, Is_Tagged_Type (Parent_Type));
8031
8032 -- If the parent type is a private subtype, the convention on the base
8033 -- type may be set in the private part, and not propagated to the
8034 -- subtype until later, so we obtain the convention from the base type.
8035
8036 Set_Convention (Derived_Type, Convention (Parent_Base));
8037
8038 -- Propagate invariant information. The new type has invariants if
8039 -- they are inherited from the parent type, and these invariants can
8040 -- be further inherited, so both flags are set.
8041
8042 if Has_Inheritable_Invariants (Parent_Type) then
8043 Set_Has_Inheritable_Invariants (Derived_Type);
8044 Set_Has_Invariants (Derived_Type);
8045 end if;
8046
8047 -- We similarly inherit predicates
8048
8049 if Has_Predicates (Parent_Type) then
8050 Set_Has_Predicates (Derived_Type);
8051 end if;
8052
8053 -- The derived type inherits the representation clauses of the parent.
8054 -- However, for a private type that is completed by a derivation, there
8055 -- may be operation attributes that have been specified already (stream
8056 -- attributes and External_Tag) and those must be provided. Finally,
8057 -- if the partial view is a private extension, the representation items
8058 -- of the parent have been inherited already, and should not be chained
8059 -- twice to the derived type.
8060
8061 if Is_Tagged_Type (Parent_Type)
8062 and then Present (First_Rep_Item (Derived_Type))
8063 then
8064 -- The existing items are either operational items or items inherited
8065 -- from a private extension declaration.
8066
8067 declare
8068 Rep : Node_Id;
8069 -- Used to iterate over representation items of the derived type
8070
8071 Last_Rep : Node_Id;
8072 -- Last representation item of the (non-empty) representation
8073 -- item list of the derived type.
8074
8075 Found : Boolean := False;
8076
8077 begin
8078 Rep := First_Rep_Item (Derived_Type);
8079 Last_Rep := Rep;
8080 while Present (Rep) loop
8081 if Rep = First_Rep_Item (Parent_Type) then
8082 Found := True;
8083 exit;
8084
8085 else
8086 Rep := Next_Rep_Item (Rep);
8087
8088 if Present (Rep) then
8089 Last_Rep := Rep;
8090 end if;
8091 end if;
8092 end loop;
8093
8094 -- Here if we either encountered the parent type's first rep
8095 -- item on the derived type's rep item list (in which case
8096 -- Found is True, and we have nothing else to do), or if we
8097 -- reached the last rep item of the derived type, which is
8098 -- Last_Rep, in which case we further chain the parent type's
8099 -- rep items to those of the derived type.
8100
8101 if not Found then
8102 Set_Next_Rep_Item (Last_Rep, First_Rep_Item (Parent_Type));
8103 end if;
8104 end;
8105
8106 else
8107 Set_First_Rep_Item (Derived_Type, First_Rep_Item (Parent_Type));
8108 end if;
8109
8110 case Ekind (Parent_Type) is
8111 when Numeric_Kind =>
8112 Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
8113
8114 when Array_Kind =>
8115 Build_Derived_Array_Type (N, Parent_Type, Derived_Type);
8116
8117 when E_Record_Type
8118 | E_Record_Subtype
8119 | Class_Wide_Kind =>
8120 Build_Derived_Record_Type
8121 (N, Parent_Type, Derived_Type, Derive_Subps);
8122 return;
8123
8124 when Enumeration_Kind =>
8125 Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
8126
8127 when Access_Kind =>
8128 Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
8129
8130 when Incomplete_Or_Private_Kind =>
8131 Build_Derived_Private_Type
8132 (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
8133
8134 -- For discriminated types, the derivation includes deriving
8135 -- primitive operations. For others it is done below.
8136
8137 if Is_Tagged_Type (Parent_Type)
8138 or else Has_Discriminants (Parent_Type)
8139 or else (Present (Full_View (Parent_Type))
8140 and then Has_Discriminants (Full_View (Parent_Type)))
8141 then
8142 return;
8143 end if;
8144
8145 when Concurrent_Kind =>
8146 Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
8147
8148 when others =>
8149 raise Program_Error;
8150 end case;
8151
8152 if Etype (Derived_Type) = Any_Type then
8153 return;
8154 end if;
8155
8156 -- Set delayed freeze and then derive subprograms, we need to do this
8157 -- in this order so that derived subprograms inherit the derived freeze
8158 -- if necessary.
8159
8160 Set_Has_Delayed_Freeze (Derived_Type);
8161 if Derive_Subps then
8162 Derive_Subprograms (Parent_Type, Derived_Type);
8163 end if;
8164
8165 Set_Has_Primitive_Operations
8166 (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
8167 end Build_Derived_Type;
8168
8169 -----------------------
8170 -- Build_Discriminal --
8171 -----------------------
8172
8173 procedure Build_Discriminal (Discrim : Entity_Id) is
8174 D_Minal : Entity_Id;
8175 CR_Disc : Entity_Id;
8176
8177 begin
8178 -- A discriminal has the same name as the discriminant
8179
8180 D_Minal := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
8181
8182 Set_Ekind (D_Minal, E_In_Parameter);
8183 Set_Mechanism (D_Minal, Default_Mechanism);
8184 Set_Etype (D_Minal, Etype (Discrim));
8185 Set_Scope (D_Minal, Current_Scope);
8186
8187 Set_Discriminal (Discrim, D_Minal);
8188 Set_Discriminal_Link (D_Minal, Discrim);
8189
8190 -- For task types, build at once the discriminants of the corresponding
8191 -- record, which are needed if discriminants are used in entry defaults
8192 -- and in family bounds.
8193
8194 if Is_Concurrent_Type (Current_Scope)
8195 or else Is_Limited_Type (Current_Scope)
8196 then
8197 CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
8198
8199 Set_Ekind (CR_Disc, E_In_Parameter);
8200 Set_Mechanism (CR_Disc, Default_Mechanism);
8201 Set_Etype (CR_Disc, Etype (Discrim));
8202 Set_Scope (CR_Disc, Current_Scope);
8203 Set_Discriminal_Link (CR_Disc, Discrim);
8204 Set_CR_Discriminant (Discrim, CR_Disc);
8205 end if;
8206 end Build_Discriminal;
8207
8208 ------------------------------------
8209 -- Build_Discriminant_Constraints --
8210 ------------------------------------
8211
8212 function Build_Discriminant_Constraints
8213 (T : Entity_Id;
8214 Def : Node_Id;
8215 Derived_Def : Boolean := False) return Elist_Id
8216 is
8217 C : constant Node_Id := Constraint (Def);
8218 Nb_Discr : constant Nat := Number_Discriminants (T);
8219
8220 Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
8221 -- Saves the expression corresponding to a given discriminant in T
8222
8223 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
8224 -- Return the Position number within array Discr_Expr of a discriminant
8225 -- D within the discriminant list of the discriminated type T.
8226
8227 ------------------
8228 -- Pos_Of_Discr --
8229 ------------------
8230
8231 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
8232 Disc : Entity_Id;
8233
8234 begin
8235 Disc := First_Discriminant (T);
8236 for J in Discr_Expr'Range loop
8237 if Disc = D then
8238 return J;
8239 end if;
8240
8241 Next_Discriminant (Disc);
8242 end loop;
8243
8244 -- Note: Since this function is called on discriminants that are
8245 -- known to belong to the discriminated type, falling through the
8246 -- loop with no match signals an internal compiler error.
8247
8248 raise Program_Error;
8249 end Pos_Of_Discr;
8250
8251 -- Declarations local to Build_Discriminant_Constraints
8252
8253 Discr : Entity_Id;
8254 E : Entity_Id;
8255 Elist : constant Elist_Id := New_Elmt_List;
8256
8257 Constr : Node_Id;
8258 Expr : Node_Id;
8259 Id : Node_Id;
8260 Position : Nat;
8261 Found : Boolean;
8262
8263 Discrim_Present : Boolean := False;
8264
8265 -- Start of processing for Build_Discriminant_Constraints
8266
8267 begin
8268 -- The following loop will process positional associations only.
8269 -- For a positional association, the (single) discriminant is
8270 -- implicitly specified by position, in textual order (RM 3.7.2).
8271
8272 Discr := First_Discriminant (T);
8273 Constr := First (Constraints (C));
8274 for D in Discr_Expr'Range loop
8275 exit when Nkind (Constr) = N_Discriminant_Association;
8276
8277 if No (Constr) then
8278 Error_Msg_N ("too few discriminants given in constraint", C);
8279 return New_Elmt_List;
8280
8281 elsif Nkind (Constr) = N_Range
8282 or else (Nkind (Constr) = N_Attribute_Reference
8283 and then
8284 Attribute_Name (Constr) = Name_Range)
8285 then
8286 Error_Msg_N
8287 ("a range is not a valid discriminant constraint", Constr);
8288 Discr_Expr (D) := Error;
8289
8290 else
8291 Analyze_And_Resolve (Constr, Base_Type (Etype (Discr)));
8292 Discr_Expr (D) := Constr;
8293 end if;
8294
8295 Next_Discriminant (Discr);
8296 Next (Constr);
8297 end loop;
8298
8299 if No (Discr) and then Present (Constr) then
8300 Error_Msg_N ("too many discriminants given in constraint", Constr);
8301 return New_Elmt_List;
8302 end if;
8303
8304 -- Named associations can be given in any order, but if both positional
8305 -- and named associations are used in the same discriminant constraint,
8306 -- then positional associations must occur first, at their normal
8307 -- position. Hence once a named association is used, the rest of the
8308 -- discriminant constraint must use only named associations.
8309
8310 while Present (Constr) loop
8311
8312 -- Positional association forbidden after a named association
8313
8314 if Nkind (Constr) /= N_Discriminant_Association then
8315 Error_Msg_N ("positional association follows named one", Constr);
8316 return New_Elmt_List;
8317
8318 -- Otherwise it is a named association
8319
8320 else
8321 -- E records the type of the discriminants in the named
8322 -- association. All the discriminants specified in the same name
8323 -- association must have the same type.
8324
8325 E := Empty;
8326
8327 -- Search the list of discriminants in T to see if the simple name
8328 -- given in the constraint matches any of them.
8329
8330 Id := First (Selector_Names (Constr));
8331 while Present (Id) loop
8332 Found := False;
8333
8334 -- If Original_Discriminant is present, we are processing a
8335 -- generic instantiation and this is an instance node. We need
8336 -- to find the name of the corresponding discriminant in the
8337 -- actual record type T and not the name of the discriminant in
8338 -- the generic formal. Example:
8339
8340 -- generic
8341 -- type G (D : int) is private;
8342 -- package P is
8343 -- subtype W is G (D => 1);
8344 -- end package;
8345 -- type Rec (X : int) is record ... end record;
8346 -- package Q is new P (G => Rec);
8347
8348 -- At the point of the instantiation, formal type G is Rec
8349 -- and therefore when reanalyzing "subtype W is G (D => 1);"
8350 -- which really looks like "subtype W is Rec (D => 1);" at
8351 -- the point of instantiation, we want to find the discriminant
8352 -- that corresponds to D in Rec, i.e. X.
8353
8354 if Present (Original_Discriminant (Id))
8355 and then In_Instance
8356 then
8357 Discr := Find_Corresponding_Discriminant (Id, T);
8358 Found := True;
8359
8360 else
8361 Discr := First_Discriminant (T);
8362 while Present (Discr) loop
8363 if Chars (Discr) = Chars (Id) then
8364 Found := True;
8365 exit;
8366 end if;
8367
8368 Next_Discriminant (Discr);
8369 end loop;
8370
8371 if not Found then
8372 Error_Msg_N ("& does not match any discriminant", Id);
8373 return New_Elmt_List;
8374
8375 -- If the parent type is a generic formal, preserve the
8376 -- name of the discriminant for subsequent instances.
8377 -- see comment at the beginning of this if statement.
8378
8379 elsif Is_Generic_Type (Root_Type (T)) then
8380 Set_Original_Discriminant (Id, Discr);
8381 end if;
8382 end if;
8383
8384 Position := Pos_Of_Discr (T, Discr);
8385
8386 if Present (Discr_Expr (Position)) then
8387 Error_Msg_N ("duplicate constraint for discriminant&", Id);
8388
8389 else
8390 -- Each discriminant specified in the same named association
8391 -- must be associated with a separate copy of the
8392 -- corresponding expression.
8393
8394 if Present (Next (Id)) then
8395 Expr := New_Copy_Tree (Expression (Constr));
8396 Set_Parent (Expr, Parent (Expression (Constr)));
8397 else
8398 Expr := Expression (Constr);
8399 end if;
8400
8401 Discr_Expr (Position) := Expr;
8402 Analyze_And_Resolve (Expr, Base_Type (Etype (Discr)));
8403 end if;
8404
8405 -- A discriminant association with more than one discriminant
8406 -- name is only allowed if the named discriminants are all of
8407 -- the same type (RM 3.7.1(8)).
8408
8409 if E = Empty then
8410 E := Base_Type (Etype (Discr));
8411
8412 elsif Base_Type (Etype (Discr)) /= E then
8413 Error_Msg_N
8414 ("all discriminants in an association " &
8415 "must have the same type", Id);
8416 end if;
8417
8418 Next (Id);
8419 end loop;
8420 end if;
8421
8422 Next (Constr);
8423 end loop;
8424
8425 -- A discriminant constraint must provide exactly one value for each
8426 -- discriminant of the type (RM 3.7.1(8)).
8427
8428 for J in Discr_Expr'Range loop
8429 if No (Discr_Expr (J)) then
8430 Error_Msg_N ("too few discriminants given in constraint", C);
8431 return New_Elmt_List;
8432 end if;
8433 end loop;
8434
8435 -- Determine if there are discriminant expressions in the constraint
8436
8437 for J in Discr_Expr'Range loop
8438 if Denotes_Discriminant
8439 (Discr_Expr (J), Check_Concurrent => True)
8440 then
8441 Discrim_Present := True;
8442 end if;
8443 end loop;
8444
8445 -- Build an element list consisting of the expressions given in the
8446 -- discriminant constraint and apply the appropriate checks. The list
8447 -- is constructed after resolving any named discriminant associations
8448 -- and therefore the expressions appear in the textual order of the
8449 -- discriminants.
8450
8451 Discr := First_Discriminant (T);
8452 for J in Discr_Expr'Range loop
8453 if Discr_Expr (J) /= Error then
8454 Append_Elmt (Discr_Expr (J), Elist);
8455
8456 -- If any of the discriminant constraints is given by a
8457 -- discriminant and we are in a derived type declaration we
8458 -- have a discriminant renaming. Establish link between new
8459 -- and old discriminant.
8460
8461 if Denotes_Discriminant (Discr_Expr (J)) then
8462 if Derived_Def then
8463 Set_Corresponding_Discriminant
8464 (Entity (Discr_Expr (J)), Discr);
8465 end if;
8466
8467 -- Force the evaluation of non-discriminant expressions.
8468 -- If we have found a discriminant in the constraint 3.4(26)
8469 -- and 3.8(18) demand that no range checks are performed are
8470 -- after evaluation. If the constraint is for a component
8471 -- definition that has a per-object constraint, expressions are
8472 -- evaluated but not checked either. In all other cases perform
8473 -- a range check.
8474
8475 else
8476 if Discrim_Present then
8477 null;
8478
8479 elsif Nkind (Parent (Parent (Def))) = N_Component_Declaration
8480 and then
8481 Has_Per_Object_Constraint
8482 (Defining_Identifier (Parent (Parent (Def))))
8483 then
8484 null;
8485
8486 elsif Is_Access_Type (Etype (Discr)) then
8487 Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
8488
8489 else
8490 Apply_Range_Check (Discr_Expr (J), Etype (Discr));
8491 end if;
8492
8493 Force_Evaluation (Discr_Expr (J));
8494 end if;
8495
8496 -- Check that the designated type of an access discriminant's
8497 -- expression is not a class-wide type unless the discriminant's
8498 -- designated type is also class-wide.
8499
8500 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
8501 and then not Is_Class_Wide_Type
8502 (Designated_Type (Etype (Discr)))
8503 and then Etype (Discr_Expr (J)) /= Any_Type
8504 and then Is_Class_Wide_Type
8505 (Designated_Type (Etype (Discr_Expr (J))))
8506 then
8507 Wrong_Type (Discr_Expr (J), Etype (Discr));
8508
8509 elsif Is_Access_Type (Etype (Discr))
8510 and then not Is_Access_Constant (Etype (Discr))
8511 and then Is_Access_Type (Etype (Discr_Expr (J)))
8512 and then Is_Access_Constant (Etype (Discr_Expr (J)))
8513 then
8514 Error_Msg_NE
8515 ("constraint for discriminant& must be access to variable",
8516 Def, Discr);
8517 end if;
8518 end if;
8519
8520 Next_Discriminant (Discr);
8521 end loop;
8522
8523 return Elist;
8524 end Build_Discriminant_Constraints;
8525
8526 ---------------------------------
8527 -- Build_Discriminated_Subtype --
8528 ---------------------------------
8529
8530 procedure Build_Discriminated_Subtype
8531 (T : Entity_Id;
8532 Def_Id : Entity_Id;
8533 Elist : Elist_Id;
8534 Related_Nod : Node_Id;
8535 For_Access : Boolean := False)
8536 is
8537 Has_Discrs : constant Boolean := Has_Discriminants (T);
8538 Constrained : constant Boolean :=
8539 (Has_Discrs
8540 and then not Is_Empty_Elmt_List (Elist)
8541 and then not Is_Class_Wide_Type (T))
8542 or else Is_Constrained (T);
8543
8544 begin
8545 if Ekind (T) = E_Record_Type then
8546 if For_Access then
8547 Set_Ekind (Def_Id, E_Private_Subtype);
8548 Set_Is_For_Access_Subtype (Def_Id, True);
8549 else
8550 Set_Ekind (Def_Id, E_Record_Subtype);
8551 end if;
8552
8553 -- Inherit preelaboration flag from base, for types for which it
8554 -- may have been set: records, private types, protected types.
8555
8556 Set_Known_To_Have_Preelab_Init
8557 (Def_Id, Known_To_Have_Preelab_Init (T));
8558
8559 elsif Ekind (T) = E_Task_Type then
8560 Set_Ekind (Def_Id, E_Task_Subtype);
8561
8562 elsif Ekind (T) = E_Protected_Type then
8563 Set_Ekind (Def_Id, E_Protected_Subtype);
8564 Set_Known_To_Have_Preelab_Init
8565 (Def_Id, Known_To_Have_Preelab_Init (T));
8566
8567 elsif Is_Private_Type (T) then
8568 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
8569 Set_Known_To_Have_Preelab_Init
8570 (Def_Id, Known_To_Have_Preelab_Init (T));
8571
8572 elsif Is_Class_Wide_Type (T) then
8573 Set_Ekind (Def_Id, E_Class_Wide_Subtype);
8574
8575 else
8576 -- Incomplete type. Attach subtype to list of dependents, to be
8577 -- completed with full view of parent type, unless is it the
8578 -- designated subtype of a record component within an init_proc.
8579 -- This last case arises for a component of an access type whose
8580 -- designated type is incomplete (e.g. a Taft Amendment type).
8581 -- The designated subtype is within an inner scope, and needs no
8582 -- elaboration, because only the access type is needed in the
8583 -- initialization procedure.
8584
8585 Set_Ekind (Def_Id, Ekind (T));
8586
8587 if For_Access and then Within_Init_Proc then
8588 null;
8589 else
8590 Append_Elmt (Def_Id, Private_Dependents (T));
8591 end if;
8592 end if;
8593
8594 Set_Etype (Def_Id, T);
8595 Init_Size_Align (Def_Id);
8596 Set_Has_Discriminants (Def_Id, Has_Discrs);
8597 Set_Is_Constrained (Def_Id, Constrained);
8598
8599 Set_First_Entity (Def_Id, First_Entity (T));
8600 Set_Last_Entity (Def_Id, Last_Entity (T));
8601 Set_Has_Implicit_Dereference
8602 (Def_Id, Has_Implicit_Dereference (T));
8603
8604 -- If the subtype is the completion of a private declaration, there may
8605 -- have been representation clauses for the partial view, and they must
8606 -- be preserved. Build_Derived_Type chains the inherited clauses with
8607 -- the ones appearing on the extension. If this comes from a subtype
8608 -- declaration, all clauses are inherited.
8609
8610 if No (First_Rep_Item (Def_Id)) then
8611 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
8612 end if;
8613
8614 if Is_Tagged_Type (T) then
8615 Set_Is_Tagged_Type (Def_Id);
8616 Make_Class_Wide_Type (Def_Id);
8617 end if;
8618
8619 Set_Stored_Constraint (Def_Id, No_Elist);
8620
8621 if Has_Discrs then
8622 Set_Discriminant_Constraint (Def_Id, Elist);
8623 Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
8624 end if;
8625
8626 if Is_Tagged_Type (T) then
8627
8628 -- Ada 2005 (AI-251): In case of concurrent types we inherit the
8629 -- concurrent record type (which has the list of primitive
8630 -- operations).
8631
8632 if Ada_Version >= Ada_2005
8633 and then Is_Concurrent_Type (T)
8634 then
8635 Set_Corresponding_Record_Type (Def_Id,
8636 Corresponding_Record_Type (T));
8637 else
8638 Set_Direct_Primitive_Operations (Def_Id,
8639 Direct_Primitive_Operations (T));
8640 end if;
8641
8642 Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
8643 end if;
8644
8645 -- Subtypes introduced by component declarations do not need to be
8646 -- marked as delayed, and do not get freeze nodes, because the semantics
8647 -- verifies that the parents of the subtypes are frozen before the
8648 -- enclosing record is frozen.
8649
8650 if not Is_Type (Scope (Def_Id)) then
8651 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
8652
8653 if Is_Private_Type (T)
8654 and then Present (Full_View (T))
8655 then
8656 Conditional_Delay (Def_Id, Full_View (T));
8657 else
8658 Conditional_Delay (Def_Id, T);
8659 end if;
8660 end if;
8661
8662 if Is_Record_Type (T) then
8663 Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
8664
8665 if Has_Discrs
8666 and then not Is_Empty_Elmt_List (Elist)
8667 and then not For_Access
8668 then
8669 Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
8670 elsif not For_Access then
8671 Set_Cloned_Subtype (Def_Id, T);
8672 end if;
8673 end if;
8674 end Build_Discriminated_Subtype;
8675
8676 ---------------------------
8677 -- Build_Itype_Reference --
8678 ---------------------------
8679
8680 procedure Build_Itype_Reference
8681 (Ityp : Entity_Id;
8682 Nod : Node_Id)
8683 is
8684 IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
8685 begin
8686
8687 -- Itype references are only created for use by the back-end
8688
8689 if Inside_A_Generic then
8690 return;
8691 else
8692 Set_Itype (IR, Ityp);
8693 Insert_After (Nod, IR);
8694 end if;
8695 end Build_Itype_Reference;
8696
8697 ------------------------
8698 -- Build_Scalar_Bound --
8699 ------------------------
8700
8701 function Build_Scalar_Bound
8702 (Bound : Node_Id;
8703 Par_T : Entity_Id;
8704 Der_T : Entity_Id) return Node_Id
8705 is
8706 New_Bound : Entity_Id;
8707
8708 begin
8709 -- Note: not clear why this is needed, how can the original bound
8710 -- be unanalyzed at this point? and if it is, what business do we
8711 -- have messing around with it? and why is the base type of the
8712 -- parent type the right type for the resolution. It probably is
8713 -- not! It is OK for the new bound we are creating, but not for
8714 -- the old one??? Still if it never happens, no problem!
8715
8716 Analyze_And_Resolve (Bound, Base_Type (Par_T));
8717
8718 if Nkind_In (Bound, N_Integer_Literal, N_Real_Literal) then
8719 New_Bound := New_Copy (Bound);
8720 Set_Etype (New_Bound, Der_T);
8721 Set_Analyzed (New_Bound);
8722
8723 elsif Is_Entity_Name (Bound) then
8724 New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
8725
8726 -- The following is almost certainly wrong. What business do we have
8727 -- relocating a node (Bound) that is presumably still attached to
8728 -- the tree elsewhere???
8729
8730 else
8731 New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
8732 end if;
8733
8734 Set_Etype (New_Bound, Der_T);
8735 return New_Bound;
8736 end Build_Scalar_Bound;
8737
8738 --------------------------------
8739 -- Build_Underlying_Full_View --
8740 --------------------------------
8741
8742 procedure Build_Underlying_Full_View
8743 (N : Node_Id;
8744 Typ : Entity_Id;
8745 Par : Entity_Id)
8746 is
8747 Loc : constant Source_Ptr := Sloc (N);
8748 Subt : constant Entity_Id :=
8749 Make_Defining_Identifier
8750 (Loc, New_External_Name (Chars (Typ), 'S'));
8751
8752 Constr : Node_Id;
8753 Indic : Node_Id;
8754 C : Node_Id;
8755 Id : Node_Id;
8756
8757 procedure Set_Discriminant_Name (Id : Node_Id);
8758 -- If the derived type has discriminants, they may rename discriminants
8759 -- of the parent. When building the full view of the parent, we need to
8760 -- recover the names of the original discriminants if the constraint is
8761 -- given by named associations.
8762
8763 ---------------------------
8764 -- Set_Discriminant_Name --
8765 ---------------------------
8766
8767 procedure Set_Discriminant_Name (Id : Node_Id) is
8768 Disc : Entity_Id;
8769
8770 begin
8771 Set_Original_Discriminant (Id, Empty);
8772
8773 if Has_Discriminants (Typ) then
8774 Disc := First_Discriminant (Typ);
8775 while Present (Disc) loop
8776 if Chars (Disc) = Chars (Id)
8777 and then Present (Corresponding_Discriminant (Disc))
8778 then
8779 Set_Chars (Id, Chars (Corresponding_Discriminant (Disc)));
8780 end if;
8781 Next_Discriminant (Disc);
8782 end loop;
8783 end if;
8784 end Set_Discriminant_Name;
8785
8786 -- Start of processing for Build_Underlying_Full_View
8787
8788 begin
8789 if Nkind (N) = N_Full_Type_Declaration then
8790 Constr := Constraint (Subtype_Indication (Type_Definition (N)));
8791
8792 elsif Nkind (N) = N_Subtype_Declaration then
8793 Constr := New_Copy_Tree (Constraint (Subtype_Indication (N)));
8794
8795 elsif Nkind (N) = N_Component_Declaration then
8796 Constr :=
8797 New_Copy_Tree
8798 (Constraint (Subtype_Indication (Component_Definition (N))));
8799
8800 else
8801 raise Program_Error;
8802 end if;
8803
8804 C := First (Constraints (Constr));
8805 while Present (C) loop
8806 if Nkind (C) = N_Discriminant_Association then
8807 Id := First (Selector_Names (C));
8808 while Present (Id) loop
8809 Set_Discriminant_Name (Id);
8810 Next (Id);
8811 end loop;
8812 end if;
8813
8814 Next (C);
8815 end loop;
8816
8817 Indic :=
8818 Make_Subtype_Declaration (Loc,
8819 Defining_Identifier => Subt,
8820 Subtype_Indication =>
8821 Make_Subtype_Indication (Loc,
8822 Subtype_Mark => New_Reference_To (Par, Loc),
8823 Constraint => New_Copy_Tree (Constr)));
8824
8825 -- If this is a component subtype for an outer itype, it is not
8826 -- a list member, so simply set the parent link for analysis: if
8827 -- the enclosing type does not need to be in a declarative list,
8828 -- neither do the components.
8829
8830 if Is_List_Member (N)
8831 and then Nkind (N) /= N_Component_Declaration
8832 then
8833 Insert_Before (N, Indic);
8834 else
8835 Set_Parent (Indic, Parent (N));
8836 end if;
8837
8838 Analyze (Indic);
8839 Set_Underlying_Full_View (Typ, Full_View (Subt));
8840 end Build_Underlying_Full_View;
8841
8842 -------------------------------
8843 -- Check_Abstract_Overriding --
8844 -------------------------------
8845
8846 procedure Check_Abstract_Overriding (T : Entity_Id) is
8847 Alias_Subp : Entity_Id;
8848 Elmt : Elmt_Id;
8849 Op_List : Elist_Id;
8850 Subp : Entity_Id;
8851 Type_Def : Node_Id;
8852
8853 procedure Check_Pragma_Implemented (Subp : Entity_Id);
8854 -- Ada 2012 (AI05-0030): Subprogram Subp overrides an interface routine
8855 -- which has pragma Implemented already set. Check whether Subp's entity
8856 -- kind conforms to the implementation kind of the overridden routine.
8857
8858 procedure Check_Pragma_Implemented
8859 (Subp : Entity_Id;
8860 Iface_Subp : Entity_Id);
8861 -- Ada 2012 (AI05-0030): Subprogram Subp overrides interface routine
8862 -- Iface_Subp and both entities have pragma Implemented already set on
8863 -- them. Check whether the two implementation kinds are conforming.
8864
8865 procedure Inherit_Pragma_Implemented
8866 (Subp : Entity_Id;
8867 Iface_Subp : Entity_Id);
8868 -- Ada 2012 (AI05-0030): Interface primitive Subp overrides interface
8869 -- subprogram Iface_Subp which has been marked by pragma Implemented.
8870 -- Propagate the implementation kind of Iface_Subp to Subp.
8871
8872 ------------------------------
8873 -- Check_Pragma_Implemented --
8874 ------------------------------
8875
8876 procedure Check_Pragma_Implemented (Subp : Entity_Id) is
8877 Iface_Alias : constant Entity_Id := Interface_Alias (Subp);
8878 Impl_Kind : constant Name_Id := Implementation_Kind (Iface_Alias);
8879 Contr_Typ : Entity_Id;
8880
8881 begin
8882 -- Subp must have an alias since it is a hidden entity used to link
8883 -- an interface subprogram to its overriding counterpart.
8884
8885 pragma Assert (Present (Alias (Subp)));
8886
8887 -- Extract the type of the controlling formal
8888
8889 Contr_Typ := Etype (First_Formal (Alias (Subp)));
8890
8891 if Is_Concurrent_Record_Type (Contr_Typ) then
8892 Contr_Typ := Corresponding_Concurrent_Type (Contr_Typ);
8893 end if;
8894
8895 -- An interface subprogram whose implementation kind is By_Entry must
8896 -- be implemented by an entry.
8897
8898 if Impl_Kind = Name_By_Entry
8899 and then Ekind (Wrapped_Entity (Alias (Subp))) /= E_Entry
8900 then
8901 Error_Msg_Node_2 := Iface_Alias;
8902 Error_Msg_NE
8903 ("type & must implement abstract subprogram & with an entry",
8904 Alias (Subp), Contr_Typ);
8905
8906 elsif Impl_Kind = Name_By_Protected_Procedure then
8907
8908 -- An interface subprogram whose implementation kind is By_
8909 -- Protected_Procedure cannot be implemented by a primitive
8910 -- procedure of a task type.
8911
8912 if Ekind (Contr_Typ) /= E_Protected_Type then
8913 Error_Msg_Node_2 := Contr_Typ;
8914 Error_Msg_NE
8915 ("interface subprogram & cannot be implemented by a " &
8916 "primitive procedure of task type &", Alias (Subp),
8917 Iface_Alias);
8918
8919 -- An interface subprogram whose implementation kind is By_
8920 -- Protected_Procedure must be implemented by a procedure.
8921
8922 elsif Is_Primitive_Wrapper (Alias (Subp))
8923 and then Ekind (Wrapped_Entity (Alias (Subp))) /= E_Procedure
8924 then
8925 Error_Msg_Node_2 := Iface_Alias;
8926 Error_Msg_NE
8927 ("type & must implement abstract subprogram & with a " &
8928 "procedure", Alias (Subp), Contr_Typ);
8929 end if;
8930 end if;
8931 end Check_Pragma_Implemented;
8932
8933 ------------------------------
8934 -- Check_Pragma_Implemented --
8935 ------------------------------
8936
8937 procedure Check_Pragma_Implemented
8938 (Subp : Entity_Id;
8939 Iface_Subp : Entity_Id)
8940 is
8941 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
8942 Subp_Kind : constant Name_Id := Implementation_Kind (Subp);
8943
8944 begin
8945 -- Ada 2012 (AI05-0030): The implementation kinds of an overridden
8946 -- and overriding subprogram are different. In general this is an
8947 -- error except when the implementation kind of the overridden
8948 -- subprograms is By_Any.
8949
8950 if Iface_Kind /= Subp_Kind
8951 and then Iface_Kind /= Name_By_Any
8952 then
8953 if Iface_Kind = Name_By_Entry then
8954 Error_Msg_N
8955 ("incompatible implementation kind, overridden subprogram " &
8956 "is marked By_Entry", Subp);
8957 else
8958 Error_Msg_N
8959 ("incompatible implementation kind, overridden subprogram " &
8960 "is marked By_Protected_Procedure", Subp);
8961 end if;
8962 end if;
8963 end Check_Pragma_Implemented;
8964
8965 --------------------------------
8966 -- Inherit_Pragma_Implemented --
8967 --------------------------------
8968
8969 procedure Inherit_Pragma_Implemented
8970 (Subp : Entity_Id;
8971 Iface_Subp : Entity_Id)
8972 is
8973 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
8974 Loc : constant Source_Ptr := Sloc (Subp);
8975 Impl_Prag : Node_Id;
8976
8977 begin
8978 -- Since the implementation kind is stored as a representation item
8979 -- rather than a flag, create a pragma node.
8980
8981 Impl_Prag :=
8982 Make_Pragma (Loc,
8983 Chars => Name_Implemented,
8984 Pragma_Argument_Associations => New_List (
8985 Make_Pragma_Argument_Association (Loc,
8986 Expression =>
8987 New_Reference_To (Subp, Loc)),
8988
8989 Make_Pragma_Argument_Association (Loc,
8990 Expression => Make_Identifier (Loc, Iface_Kind))));
8991
8992 -- The pragma doesn't need to be analyzed because it is internally
8993 -- build. It is safe to directly register it as a rep item since we
8994 -- are only interested in the characters of the implementation kind.
8995
8996 Record_Rep_Item (Subp, Impl_Prag);
8997 end Inherit_Pragma_Implemented;
8998
8999 -- Start of processing for Check_Abstract_Overriding
9000
9001 begin
9002 Op_List := Primitive_Operations (T);
9003
9004 -- Loop to check primitive operations
9005
9006 Elmt := First_Elmt (Op_List);
9007 while Present (Elmt) loop
9008 Subp := Node (Elmt);
9009 Alias_Subp := Alias (Subp);
9010
9011 -- Inherited subprograms are identified by the fact that they do not
9012 -- come from source, and the associated source location is the
9013 -- location of the first subtype of the derived type.
9014
9015 -- Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
9016 -- subprograms that "require overriding".
9017
9018 -- Special exception, do not complain about failure to override the
9019 -- stream routines _Input and _Output, as well as the primitive
9020 -- operations used in dispatching selects since we always provide
9021 -- automatic overridings for these subprograms.
9022
9023 -- Also ignore this rule for convention CIL since .NET libraries
9024 -- do bizarre things with interfaces???
9025
9026 -- The partial view of T may have been a private extension, for
9027 -- which inherited functions dispatching on result are abstract.
9028 -- If the full view is a null extension, there is no need for
9029 -- overriding in Ada2005, but wrappers need to be built for them
9030 -- (see exp_ch3, Build_Controlling_Function_Wrappers).
9031
9032 if Is_Null_Extension (T)
9033 and then Has_Controlling_Result (Subp)
9034 and then Ada_Version >= Ada_2005
9035 and then Present (Alias_Subp)
9036 and then not Comes_From_Source (Subp)
9037 and then not Is_Abstract_Subprogram (Alias_Subp)
9038 and then not Is_Access_Type (Etype (Subp))
9039 then
9040 null;
9041
9042 -- Ada 2005 (AI-251): Internal entities of interfaces need no
9043 -- processing because this check is done with the aliased
9044 -- entity
9045
9046 elsif Present (Interface_Alias (Subp)) then
9047 null;
9048
9049 elsif (Is_Abstract_Subprogram (Subp)
9050 or else Requires_Overriding (Subp)
9051 or else
9052 (Has_Controlling_Result (Subp)
9053 and then Present (Alias_Subp)
9054 and then not Comes_From_Source (Subp)
9055 and then Sloc (Subp) = Sloc (First_Subtype (T))))
9056 and then not Is_TSS (Subp, TSS_Stream_Input)
9057 and then not Is_TSS (Subp, TSS_Stream_Output)
9058 and then not Is_Abstract_Type (T)
9059 and then Convention (T) /= Convention_CIL
9060 and then not Is_Predefined_Interface_Primitive (Subp)
9061
9062 -- Ada 2005 (AI-251): Do not consider hidden entities associated
9063 -- with abstract interface types because the check will be done
9064 -- with the aliased entity (otherwise we generate a duplicated
9065 -- error message).
9066
9067 and then not Present (Interface_Alias (Subp))
9068 then
9069 if Present (Alias_Subp) then
9070
9071 -- Only perform the check for a derived subprogram when the
9072 -- type has an explicit record extension. This avoids incorrect
9073 -- flagging of abstract subprograms for the case of a type
9074 -- without an extension that is derived from a formal type
9075 -- with a tagged actual (can occur within a private part).
9076
9077 -- Ada 2005 (AI-391): In the case of an inherited function with
9078 -- a controlling result of the type, the rule does not apply if
9079 -- the type is a null extension (unless the parent function
9080 -- itself is abstract, in which case the function must still be
9081 -- be overridden). The expander will generate an overriding
9082 -- wrapper function calling the parent subprogram (see
9083 -- Exp_Ch3.Make_Controlling_Wrapper_Functions).
9084
9085 Type_Def := Type_Definition (Parent (T));
9086
9087 if Nkind (Type_Def) = N_Derived_Type_Definition
9088 and then Present (Record_Extension_Part (Type_Def))
9089 and then
9090 (Ada_Version < Ada_2005
9091 or else not Is_Null_Extension (T)
9092 or else Ekind (Subp) = E_Procedure
9093 or else not Has_Controlling_Result (Subp)
9094 or else Is_Abstract_Subprogram (Alias_Subp)
9095 or else Requires_Overriding (Subp)
9096 or else Is_Access_Type (Etype (Subp)))
9097 then
9098 -- Avoid reporting error in case of abstract predefined
9099 -- primitive inherited from interface type because the
9100 -- body of internally generated predefined primitives
9101 -- of tagged types are generated later by Freeze_Type
9102
9103 if Is_Interface (Root_Type (T))
9104 and then Is_Abstract_Subprogram (Subp)
9105 and then Is_Predefined_Dispatching_Operation (Subp)
9106 and then not Comes_From_Source (Ultimate_Alias (Subp))
9107 then
9108 null;
9109
9110 else
9111 Error_Msg_NE
9112 ("type must be declared abstract or & overridden",
9113 T, Subp);
9114
9115 -- Traverse the whole chain of aliased subprograms to
9116 -- complete the error notification. This is especially
9117 -- useful for traceability of the chain of entities when
9118 -- the subprogram corresponds with an interface
9119 -- subprogram (which may be defined in another package).
9120
9121 if Present (Alias_Subp) then
9122 declare
9123 E : Entity_Id;
9124
9125 begin
9126 E := Subp;
9127 while Present (Alias (E)) loop
9128
9129 -- Avoid reporting redundant errors on entities
9130 -- inherited from interfaces
9131
9132 if Sloc (E) /= Sloc (T) then
9133 Error_Msg_Sloc := Sloc (E);
9134 Error_Msg_NE
9135 ("\& has been inherited #", T, Subp);
9136 end if;
9137
9138 E := Alias (E);
9139 end loop;
9140
9141 Error_Msg_Sloc := Sloc (E);
9142
9143 -- AI05-0068: report if there is an overriding
9144 -- non-abstract subprogram that is invisible.
9145
9146 if Is_Hidden (E)
9147 and then not Is_Abstract_Subprogram (E)
9148 then
9149 Error_Msg_NE
9150 ("\& subprogram# is not visible",
9151 T, Subp);
9152
9153 else
9154 Error_Msg_NE
9155 ("\& has been inherited from subprogram #",
9156 T, Subp);
9157 end if;
9158 end;
9159 end if;
9160 end if;
9161
9162 -- Ada 2005 (AI-345): Protected or task type implementing
9163 -- abstract interfaces.
9164
9165 elsif Is_Concurrent_Record_Type (T)
9166 and then Present (Interfaces (T))
9167 then
9168 -- The controlling formal of Subp must be of mode "out",
9169 -- "in out" or an access-to-variable to be overridden.
9170
9171 if Ekind (First_Formal (Subp)) = E_In_Parameter
9172 and then Ekind (Subp) /= E_Function
9173 then
9174 if not Is_Predefined_Dispatching_Operation (Subp)
9175 and then Is_Protected_Type
9176 (Corresponding_Concurrent_Type (T))
9177 then
9178 Error_Msg_PT (T, Subp);
9179 end if;
9180
9181 -- Some other kind of overriding failure
9182
9183 else
9184 Error_Msg_NE
9185 ("interface subprogram & must be overridden",
9186 T, Subp);
9187
9188 -- Examine primitive operations of synchronized type,
9189 -- to find homonyms that have the wrong profile.
9190
9191 declare
9192 Prim : Entity_Id;
9193
9194 begin
9195 Prim :=
9196 First_Entity (Corresponding_Concurrent_Type (T));
9197 while Present (Prim) loop
9198 if Chars (Prim) = Chars (Subp) then
9199 Error_Msg_NE
9200 ("profile is not type conformant with "
9201 & "prefixed view profile of "
9202 & "inherited operation&", Prim, Subp);
9203 end if;
9204
9205 Next_Entity (Prim);
9206 end loop;
9207 end;
9208 end if;
9209 end if;
9210
9211 else
9212 Error_Msg_Node_2 := T;
9213 Error_Msg_N
9214 ("abstract subprogram& not allowed for type&", Subp);
9215
9216 -- Also post unconditional warning on the type (unconditional
9217 -- so that if there are more than one of these cases, we get
9218 -- them all, and not just the first one).
9219
9220 Error_Msg_Node_2 := Subp;
9221 Error_Msg_N ("nonabstract type& has abstract subprogram&!", T);
9222 end if;
9223 end if;
9224
9225 -- Ada 2012 (AI05-0030): Perform some checks related to pragma
9226 -- Implemented
9227
9228 -- Subp is an expander-generated procedure which maps an interface
9229 -- alias to a protected wrapper. The interface alias is flagged by
9230 -- pragma Implemented. Ensure that Subp is a procedure when the
9231 -- implementation kind is By_Protected_Procedure or an entry when
9232 -- By_Entry.
9233
9234 if Ada_Version >= Ada_2012
9235 and then Is_Hidden (Subp)
9236 and then Present (Interface_Alias (Subp))
9237 and then Has_Rep_Pragma (Interface_Alias (Subp), Name_Implemented)
9238 then
9239 Check_Pragma_Implemented (Subp);
9240 end if;
9241
9242 -- Subp is an interface primitive which overrides another interface
9243 -- primitive marked with pragma Implemented.
9244
9245 if Ada_Version >= Ada_2012
9246 and then Present (Overridden_Operation (Subp))
9247 and then Has_Rep_Pragma
9248 (Overridden_Operation (Subp), Name_Implemented)
9249 then
9250 -- If the overriding routine is also marked by Implemented, check
9251 -- that the two implementation kinds are conforming.
9252
9253 if Has_Rep_Pragma (Subp, Name_Implemented) then
9254 Check_Pragma_Implemented
9255 (Subp => Subp,
9256 Iface_Subp => Overridden_Operation (Subp));
9257
9258 -- Otherwise the overriding routine inherits the implementation
9259 -- kind from the overridden subprogram.
9260
9261 else
9262 Inherit_Pragma_Implemented
9263 (Subp => Subp,
9264 Iface_Subp => Overridden_Operation (Subp));
9265 end if;
9266 end if;
9267
9268 Next_Elmt (Elmt);
9269 end loop;
9270 end Check_Abstract_Overriding;
9271
9272 ------------------------------------------------
9273 -- Check_Access_Discriminant_Requires_Limited --
9274 ------------------------------------------------
9275
9276 procedure Check_Access_Discriminant_Requires_Limited
9277 (D : Node_Id;
9278 Loc : Node_Id)
9279 is
9280 begin
9281 -- A discriminant_specification for an access discriminant shall appear
9282 -- only in the declaration for a task or protected type, or for a type
9283 -- with the reserved word 'limited' in its definition or in one of its
9284 -- ancestors (RM 3.7(10)).
9285
9286 -- AI-0063: The proper condition is that type must be immutably limited,
9287 -- or else be a partial view.
9288
9289 if Nkind (Discriminant_Type (D)) = N_Access_Definition then
9290 if Is_Immutably_Limited_Type (Current_Scope)
9291 or else
9292 (Nkind (Parent (Current_Scope)) = N_Private_Type_Declaration
9293 and then Limited_Present (Parent (Current_Scope)))
9294 then
9295 null;
9296
9297 else
9298 Error_Msg_N
9299 ("access discriminants allowed only for limited types", Loc);
9300 end if;
9301 end if;
9302 end Check_Access_Discriminant_Requires_Limited;
9303
9304 -----------------------------------
9305 -- Check_Aliased_Component_Types --
9306 -----------------------------------
9307
9308 procedure Check_Aliased_Component_Types (T : Entity_Id) is
9309 C : Entity_Id;
9310
9311 begin
9312 -- ??? Also need to check components of record extensions, but not
9313 -- components of protected types (which are always limited).
9314
9315 -- Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
9316 -- types to be unconstrained. This is safe because it is illegal to
9317 -- create access subtypes to such types with explicit discriminant
9318 -- constraints.
9319
9320 if not Is_Limited_Type (T) then
9321 if Ekind (T) = E_Record_Type then
9322 C := First_Component (T);
9323 while Present (C) loop
9324 if Is_Aliased (C)
9325 and then Has_Discriminants (Etype (C))
9326 and then not Is_Constrained (Etype (C))
9327 and then not In_Instance_Body
9328 and then Ada_Version < Ada_2005
9329 then
9330 Error_Msg_N
9331 ("aliased component must be constrained (RM 3.6(11))",
9332 C);
9333 end if;
9334
9335 Next_Component (C);
9336 end loop;
9337
9338 elsif Ekind (T) = E_Array_Type then
9339 if Has_Aliased_Components (T)
9340 and then Has_Discriminants (Component_Type (T))
9341 and then not Is_Constrained (Component_Type (T))
9342 and then not In_Instance_Body
9343 and then Ada_Version < Ada_2005
9344 then
9345 Error_Msg_N
9346 ("aliased component type must be constrained (RM 3.6(11))",
9347 T);
9348 end if;
9349 end if;
9350 end if;
9351 end Check_Aliased_Component_Types;
9352
9353 ----------------------
9354 -- Check_Completion --
9355 ----------------------
9356
9357 procedure Check_Completion (Body_Id : Node_Id := Empty) is
9358 E : Entity_Id;
9359
9360 procedure Post_Error;
9361 -- Post error message for lack of completion for entity E
9362
9363 ----------------
9364 -- Post_Error --
9365 ----------------
9366
9367 procedure Post_Error is
9368
9369 procedure Missing_Body;
9370 -- Output missing body message
9371
9372 ------------------
9373 -- Missing_Body --
9374 ------------------
9375
9376 procedure Missing_Body is
9377 begin
9378 -- Spec is in same unit, so we can post on spec
9379
9380 if In_Same_Source_Unit (Body_Id, E) then
9381 Error_Msg_N ("missing body for &", E);
9382
9383 -- Spec is in a separate unit, so we have to post on the body
9384
9385 else
9386 Error_Msg_NE ("missing body for & declared#!", Body_Id, E);
9387 end if;
9388 end Missing_Body;
9389
9390 -- Start of processing for Post_Error
9391
9392 begin
9393 if not Comes_From_Source (E) then
9394
9395 if Ekind_In (E, E_Task_Type, E_Protected_Type) then
9396 -- It may be an anonymous protected type created for a
9397 -- single variable. Post error on variable, if present.
9398
9399 declare
9400 Var : Entity_Id;
9401
9402 begin
9403 Var := First_Entity (Current_Scope);
9404 while Present (Var) loop
9405 exit when Etype (Var) = E
9406 and then Comes_From_Source (Var);
9407
9408 Next_Entity (Var);
9409 end loop;
9410
9411 if Present (Var) then
9412 E := Var;
9413 end if;
9414 end;
9415 end if;
9416 end if;
9417
9418 -- If a generated entity has no completion, then either previous
9419 -- semantic errors have disabled the expansion phase, or else we had
9420 -- missing subunits, or else we are compiling without expansion,
9421 -- or else something is very wrong.
9422
9423 if not Comes_From_Source (E) then
9424 pragma Assert
9425 (Serious_Errors_Detected > 0
9426 or else Configurable_Run_Time_Violations > 0
9427 or else Subunits_Missing
9428 or else not Expander_Active);
9429 return;
9430
9431 -- Here for source entity
9432
9433 else
9434 -- Here if no body to post the error message, so we post the error
9435 -- on the declaration that has no completion. This is not really
9436 -- the right place to post it, think about this later ???
9437
9438 if No (Body_Id) then
9439 if Is_Type (E) then
9440 Error_Msg_NE
9441 ("missing full declaration for }", Parent (E), E);
9442 else
9443 Error_Msg_NE ("missing body for &", Parent (E), E);
9444 end if;
9445
9446 -- Package body has no completion for a declaration that appears
9447 -- in the corresponding spec. Post error on the body, with a
9448 -- reference to the non-completed declaration.
9449
9450 else
9451 Error_Msg_Sloc := Sloc (E);
9452
9453 if Is_Type (E) then
9454 Error_Msg_NE ("missing full declaration for }!", Body_Id, E);
9455
9456 elsif Is_Overloadable (E)
9457 and then Current_Entity_In_Scope (E) /= E
9458 then
9459 -- It may be that the completion is mistyped and appears as
9460 -- a distinct overloading of the entity.
9461
9462 declare
9463 Candidate : constant Entity_Id :=
9464 Current_Entity_In_Scope (E);
9465 Decl : constant Node_Id :=
9466 Unit_Declaration_Node (Candidate);
9467
9468 begin
9469 if Is_Overloadable (Candidate)
9470 and then Ekind (Candidate) = Ekind (E)
9471 and then Nkind (Decl) = N_Subprogram_Body
9472 and then Acts_As_Spec (Decl)
9473 then
9474 Check_Type_Conformant (Candidate, E);
9475
9476 else
9477 Missing_Body;
9478 end if;
9479 end;
9480
9481 else
9482 Missing_Body;
9483 end if;
9484 end if;
9485 end if;
9486 end Post_Error;
9487
9488 -- Start of processing for Check_Completion
9489
9490 begin
9491 E := First_Entity (Current_Scope);
9492 while Present (E) loop
9493 if Is_Intrinsic_Subprogram (E) then
9494 null;
9495
9496 -- The following situation requires special handling: a child unit
9497 -- that appears in the context clause of the body of its parent:
9498
9499 -- procedure Parent.Child (...);
9500
9501 -- with Parent.Child;
9502 -- package body Parent is
9503
9504 -- Here Parent.Child appears as a local entity, but should not be
9505 -- flagged as requiring completion, because it is a compilation
9506 -- unit.
9507
9508 -- Ignore missing completion for a subprogram that does not come from
9509 -- source (including the _Call primitive operation of RAS types,
9510 -- which has to have the flag Comes_From_Source for other purposes):
9511 -- we assume that the expander will provide the missing completion.
9512 -- In case of previous errors, other expansion actions that provide
9513 -- bodies for null procedures with not be invoked, so inhibit message
9514 -- in those cases.
9515 -- Note that E_Operator is not in the list that follows, because
9516 -- this kind is reserved for predefined operators, that are
9517 -- intrinsic and do not need completion.
9518
9519 elsif Ekind (E) = E_Function
9520 or else Ekind (E) = E_Procedure
9521 or else Ekind (E) = E_Generic_Function
9522 or else Ekind (E) = E_Generic_Procedure
9523 then
9524 if Has_Completion (E) then
9525 null;
9526
9527 elsif Is_Subprogram (E) and then Is_Abstract_Subprogram (E) then
9528 null;
9529
9530 elsif Is_Subprogram (E)
9531 and then (not Comes_From_Source (E)
9532 or else Chars (E) = Name_uCall)
9533 then
9534 null;
9535
9536 elsif
9537 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
9538 then
9539 null;
9540
9541 elsif Nkind (Parent (E)) = N_Procedure_Specification
9542 and then Null_Present (Parent (E))
9543 and then Serious_Errors_Detected > 0
9544 then
9545 null;
9546
9547 else
9548 Post_Error;
9549 end if;
9550
9551 elsif Is_Entry (E) then
9552 if not Has_Completion (E) and then
9553 (Ekind (Scope (E)) = E_Protected_Object
9554 or else Ekind (Scope (E)) = E_Protected_Type)
9555 then
9556 Post_Error;
9557 end if;
9558
9559 elsif Is_Package_Or_Generic_Package (E) then
9560 if Unit_Requires_Body (E) then
9561 if not Has_Completion (E)
9562 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
9563 N_Compilation_Unit
9564 then
9565 Post_Error;
9566 end if;
9567
9568 elsif not Is_Child_Unit (E) then
9569 May_Need_Implicit_Body (E);
9570 end if;
9571
9572 elsif Ekind (E) = E_Incomplete_Type
9573 and then No (Underlying_Type (E))
9574 then
9575 Post_Error;
9576
9577 elsif (Ekind (E) = E_Task_Type or else
9578 Ekind (E) = E_Protected_Type)
9579 and then not Has_Completion (E)
9580 then
9581 Post_Error;
9582
9583 -- A single task declared in the current scope is a constant, verify
9584 -- that the body of its anonymous type is in the same scope. If the
9585 -- task is defined elsewhere, this may be a renaming declaration for
9586 -- which no completion is needed.
9587
9588 elsif Ekind (E) = E_Constant
9589 and then Ekind (Etype (E)) = E_Task_Type
9590 and then not Has_Completion (Etype (E))
9591 and then Scope (Etype (E)) = Current_Scope
9592 then
9593 Post_Error;
9594
9595 elsif Ekind (E) = E_Protected_Object
9596 and then not Has_Completion (Etype (E))
9597 then
9598 Post_Error;
9599
9600 elsif Ekind (E) = E_Record_Type then
9601 if Is_Tagged_Type (E) then
9602 Check_Abstract_Overriding (E);
9603 Check_Conventions (E);
9604 end if;
9605
9606 Check_Aliased_Component_Types (E);
9607
9608 elsif Ekind (E) = E_Array_Type then
9609 Check_Aliased_Component_Types (E);
9610
9611 end if;
9612
9613 Next_Entity (E);
9614 end loop;
9615 end Check_Completion;
9616
9617 ----------------------------
9618 -- Check_Delta_Expression --
9619 ----------------------------
9620
9621 procedure Check_Delta_Expression (E : Node_Id) is
9622 begin
9623 if not (Is_Real_Type (Etype (E))) then
9624 Wrong_Type (E, Any_Real);
9625
9626 elsif not Is_OK_Static_Expression (E) then
9627 Flag_Non_Static_Expr
9628 ("non-static expression used for delta value!", E);
9629
9630 elsif not UR_Is_Positive (Expr_Value_R (E)) then
9631 Error_Msg_N ("delta expression must be positive", E);
9632
9633 else
9634 return;
9635 end if;
9636
9637 -- If any of above errors occurred, then replace the incorrect
9638 -- expression by the real 0.1, which should prevent further errors.
9639
9640 Rewrite (E,
9641 Make_Real_Literal (Sloc (E), Ureal_Tenth));
9642 Analyze_And_Resolve (E, Standard_Float);
9643 end Check_Delta_Expression;
9644
9645 -----------------------------
9646 -- Check_Digits_Expression --
9647 -----------------------------
9648
9649 procedure Check_Digits_Expression (E : Node_Id) is
9650 begin
9651 if not (Is_Integer_Type (Etype (E))) then
9652 Wrong_Type (E, Any_Integer);
9653
9654 elsif not Is_OK_Static_Expression (E) then
9655 Flag_Non_Static_Expr
9656 ("non-static expression used for digits value!", E);
9657
9658 elsif Expr_Value (E) <= 0 then
9659 Error_Msg_N ("digits value must be greater than zero", E);
9660
9661 else
9662 return;
9663 end if;
9664
9665 -- If any of above errors occurred, then replace the incorrect
9666 -- expression by the integer 1, which should prevent further errors.
9667
9668 Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
9669 Analyze_And_Resolve (E, Standard_Integer);
9670
9671 end Check_Digits_Expression;
9672
9673 --------------------------
9674 -- Check_Initialization --
9675 --------------------------
9676
9677 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
9678 begin
9679 if Is_Limited_Type (T)
9680 and then not In_Instance
9681 and then not In_Inlined_Body
9682 then
9683 if not OK_For_Limited_Init (T, Exp) then
9684
9685 -- In GNAT mode, this is just a warning, to allow it to be evilly
9686 -- turned off. Otherwise it is a real error.
9687
9688 if GNAT_Mode then
9689 Error_Msg_N
9690 ("?cannot initialize entities of limited type!", Exp);
9691
9692 elsif Ada_Version < Ada_2005 then
9693 Error_Msg_N
9694 ("cannot initialize entities of limited type", Exp);
9695 Explain_Limited_Type (T, Exp);
9696
9697 else
9698 -- Specialize error message according to kind of illegal
9699 -- initial expression.
9700
9701 if Nkind (Exp) = N_Type_Conversion
9702 and then Nkind (Expression (Exp)) = N_Function_Call
9703 then
9704 Error_Msg_N
9705 ("illegal context for call"
9706 & " to function with limited result", Exp);
9707
9708 else
9709 Error_Msg_N
9710 ("initialization of limited object requires aggregate "
9711 & "or function call", Exp);
9712 end if;
9713 end if;
9714 end if;
9715 end if;
9716 end Check_Initialization;
9717
9718 ----------------------
9719 -- Check_Interfaces --
9720 ----------------------
9721
9722 procedure Check_Interfaces (N : Node_Id; Def : Node_Id) is
9723 Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
9724
9725 Iface : Node_Id;
9726 Iface_Def : Node_Id;
9727 Iface_Typ : Entity_Id;
9728 Parent_Node : Node_Id;
9729
9730 Is_Task : Boolean := False;
9731 -- Set True if parent type or any progenitor is a task interface
9732
9733 Is_Protected : Boolean := False;
9734 -- Set True if parent type or any progenitor is a protected interface
9735
9736 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
9737 -- Check that a progenitor is compatible with declaration.
9738 -- Error is posted on Error_Node.
9739
9740 ------------------
9741 -- Check_Ifaces --
9742 ------------------
9743
9744 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
9745 Iface_Id : constant Entity_Id :=
9746 Defining_Identifier (Parent (Iface_Def));
9747 Type_Def : Node_Id;
9748
9749 begin
9750 if Nkind (N) = N_Private_Extension_Declaration then
9751 Type_Def := N;
9752 else
9753 Type_Def := Type_Definition (N);
9754 end if;
9755
9756 if Is_Task_Interface (Iface_Id) then
9757 Is_Task := True;
9758
9759 elsif Is_Protected_Interface (Iface_Id) then
9760 Is_Protected := True;
9761 end if;
9762
9763 if Is_Synchronized_Interface (Iface_Id) then
9764
9765 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
9766 -- extension derived from a synchronized interface must explicitly
9767 -- be declared synchronized, because the full view will be a
9768 -- synchronized type.
9769
9770 if Nkind (N) = N_Private_Extension_Declaration then
9771 if not Synchronized_Present (N) then
9772 Error_Msg_NE
9773 ("private extension of& must be explicitly synchronized",
9774 N, Iface_Id);
9775 end if;
9776
9777 -- However, by 3.9.4(16/2), a full type that is a record extension
9778 -- is never allowed to derive from a synchronized interface (note
9779 -- that interfaces must be excluded from this check, because those
9780 -- are represented by derived type definitions in some cases).
9781
9782 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition
9783 and then not Interface_Present (Type_Definition (N))
9784 then
9785 Error_Msg_N ("record extension cannot derive from synchronized"
9786 & " interface", Error_Node);
9787 end if;
9788 end if;
9789
9790 -- Check that the characteristics of the progenitor are compatible
9791 -- with the explicit qualifier in the declaration.
9792 -- The check only applies to qualifiers that come from source.
9793 -- Limited_Present also appears in the declaration of corresponding
9794 -- records, and the check does not apply to them.
9795
9796 if Limited_Present (Type_Def)
9797 and then not
9798 Is_Concurrent_Record_Type (Defining_Identifier (N))
9799 then
9800 if Is_Limited_Interface (Parent_Type)
9801 and then not Is_Limited_Interface (Iface_Id)
9802 then
9803 Error_Msg_NE
9804 ("progenitor& must be limited interface",
9805 Error_Node, Iface_Id);
9806
9807 elsif
9808 (Task_Present (Iface_Def)
9809 or else Protected_Present (Iface_Def)
9810 or else Synchronized_Present (Iface_Def))
9811 and then Nkind (N) /= N_Private_Extension_Declaration
9812 and then not Error_Posted (N)
9813 then
9814 Error_Msg_NE
9815 ("progenitor& must be limited interface",
9816 Error_Node, Iface_Id);
9817 end if;
9818
9819 -- Protected interfaces can only inherit from limited, synchronized
9820 -- or protected interfaces.
9821
9822 elsif Nkind (N) = N_Full_Type_Declaration
9823 and then Protected_Present (Type_Def)
9824 then
9825 if Limited_Present (Iface_Def)
9826 or else Synchronized_Present (Iface_Def)
9827 or else Protected_Present (Iface_Def)
9828 then
9829 null;
9830
9831 elsif Task_Present (Iface_Def) then
9832 Error_Msg_N ("(Ada 2005) protected interface cannot inherit"
9833 & " from task interface", Error_Node);
9834
9835 else
9836 Error_Msg_N ("(Ada 2005) protected interface cannot inherit"
9837 & " from non-limited interface", Error_Node);
9838 end if;
9839
9840 -- Ada 2005 (AI-345): Synchronized interfaces can only inherit from
9841 -- limited and synchronized.
9842
9843 elsif Synchronized_Present (Type_Def) then
9844 if Limited_Present (Iface_Def)
9845 or else Synchronized_Present (Iface_Def)
9846 then
9847 null;
9848
9849 elsif Protected_Present (Iface_Def)
9850 and then Nkind (N) /= N_Private_Extension_Declaration
9851 then
9852 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
9853 & " from protected interface", Error_Node);
9854
9855 elsif Task_Present (Iface_Def)
9856 and then Nkind (N) /= N_Private_Extension_Declaration
9857 then
9858 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
9859 & " from task interface", Error_Node);
9860
9861 elsif not Is_Limited_Interface (Iface_Id) then
9862 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
9863 & " from non-limited interface", Error_Node);
9864 end if;
9865
9866 -- Ada 2005 (AI-345): Task interfaces can only inherit from limited,
9867 -- synchronized or task interfaces.
9868
9869 elsif Nkind (N) = N_Full_Type_Declaration
9870 and then Task_Present (Type_Def)
9871 then
9872 if Limited_Present (Iface_Def)
9873 or else Synchronized_Present (Iface_Def)
9874 or else Task_Present (Iface_Def)
9875 then
9876 null;
9877
9878 elsif Protected_Present (Iface_Def) then
9879 Error_Msg_N ("(Ada 2005) task interface cannot inherit from"
9880 & " protected interface", Error_Node);
9881
9882 else
9883 Error_Msg_N ("(Ada 2005) task interface cannot inherit from"
9884 & " non-limited interface", Error_Node);
9885 end if;
9886 end if;
9887 end Check_Ifaces;
9888
9889 -- Start of processing for Check_Interfaces
9890
9891 begin
9892 if Is_Interface (Parent_Type) then
9893 if Is_Task_Interface (Parent_Type) then
9894 Is_Task := True;
9895
9896 elsif Is_Protected_Interface (Parent_Type) then
9897 Is_Protected := True;
9898 end if;
9899 end if;
9900
9901 if Nkind (N) = N_Private_Extension_Declaration then
9902
9903 -- Check that progenitors are compatible with declaration
9904
9905 Iface := First (Interface_List (Def));
9906 while Present (Iface) loop
9907 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
9908
9909 Parent_Node := Parent (Base_Type (Iface_Typ));
9910 Iface_Def := Type_Definition (Parent_Node);
9911
9912 if not Is_Interface (Iface_Typ) then
9913 Diagnose_Interface (Iface, Iface_Typ);
9914
9915 else
9916 Check_Ifaces (Iface_Def, Iface);
9917 end if;
9918
9919 Next (Iface);
9920 end loop;
9921
9922 if Is_Task and Is_Protected then
9923 Error_Msg_N
9924 ("type cannot derive from task and protected interface", N);
9925 end if;
9926
9927 return;
9928 end if;
9929
9930 -- Full type declaration of derived type.
9931 -- Check compatibility with parent if it is interface type
9932
9933 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
9934 and then Is_Interface (Parent_Type)
9935 then
9936 Parent_Node := Parent (Parent_Type);
9937
9938 -- More detailed checks for interface varieties
9939
9940 Check_Ifaces
9941 (Iface_Def => Type_Definition (Parent_Node),
9942 Error_Node => Subtype_Indication (Type_Definition (N)));
9943 end if;
9944
9945 Iface := First (Interface_List (Def));
9946 while Present (Iface) loop
9947 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
9948
9949 Parent_Node := Parent (Base_Type (Iface_Typ));
9950 Iface_Def := Type_Definition (Parent_Node);
9951
9952 if not Is_Interface (Iface_Typ) then
9953 Diagnose_Interface (Iface, Iface_Typ);
9954
9955 else
9956 -- "The declaration of a specific descendant of an interface
9957 -- type freezes the interface type" RM 13.14
9958
9959 Freeze_Before (N, Iface_Typ);
9960 Check_Ifaces (Iface_Def, Error_Node => Iface);
9961 end if;
9962
9963 Next (Iface);
9964 end loop;
9965
9966 if Is_Task and Is_Protected then
9967 Error_Msg_N
9968 ("type cannot derive from task and protected interface", N);
9969 end if;
9970 end Check_Interfaces;
9971
9972 ------------------------------------
9973 -- Check_Or_Process_Discriminants --
9974 ------------------------------------
9975
9976 -- If an incomplete or private type declaration was already given for the
9977 -- type, the discriminants may have already been processed if they were
9978 -- present on the incomplete declaration. In this case a full conformance
9979 -- check has been performed in Find_Type_Name, and we then recheck here
9980 -- some properties that can't be checked on the partial view alone.
9981 -- Otherwise we call Process_Discriminants.
9982
9983 procedure Check_Or_Process_Discriminants
9984 (N : Node_Id;
9985 T : Entity_Id;
9986 Prev : Entity_Id := Empty)
9987 is
9988 begin
9989 if Has_Discriminants (T) then
9990
9991 -- Discriminants are already set on T if they were already present
9992 -- on the partial view. Make them visible to component declarations.
9993
9994 declare
9995 D : Entity_Id;
9996 -- Discriminant on T (full view) referencing expr on partial view
9997
9998 Prev_D : Entity_Id;
9999 -- Entity of corresponding discriminant on partial view
10000
10001 New_D : Node_Id;
10002 -- Discriminant specification for full view, expression is the
10003 -- syntactic copy on full view (which has been checked for
10004 -- conformance with partial view), only used here to post error
10005 -- message.
10006
10007 begin
10008 D := First_Discriminant (T);
10009 New_D := First (Discriminant_Specifications (N));
10010 while Present (D) loop
10011 Prev_D := Current_Entity (D);
10012 Set_Current_Entity (D);
10013 Set_Is_Immediately_Visible (D);
10014 Set_Homonym (D, Prev_D);
10015
10016 -- Handle the case where there is an untagged partial view and
10017 -- the full view is tagged: must disallow discriminants with
10018 -- defaults, unless compiling for Ada 2012, which allows a
10019 -- limited tagged type to have defaulted discriminants (see
10020 -- AI05-0214). However, suppress the error here if it was
10021 -- already reported on the default expression of the partial
10022 -- view.
10023
10024 if Is_Tagged_Type (T)
10025 and then Present (Expression (Parent (D)))
10026 and then (not Is_Limited_Type (Current_Scope)
10027 or else Ada_Version < Ada_2012)
10028 and then not Error_Posted (Expression (Parent (D)))
10029 then
10030 if Ada_Version >= Ada_2012 then
10031 Error_Msg_N
10032 ("discriminants of nonlimited tagged type cannot have"
10033 & " defaults",
10034 Expression (New_D));
10035 else
10036 Error_Msg_N
10037 ("discriminants of tagged type cannot have defaults",
10038 Expression (New_D));
10039 end if;
10040 end if;
10041
10042 -- Ada 2005 (AI-230): Access discriminant allowed in
10043 -- non-limited record types.
10044
10045 if Ada_Version < Ada_2005 then
10046
10047 -- This restriction gets applied to the full type here. It
10048 -- has already been applied earlier to the partial view.
10049
10050 Check_Access_Discriminant_Requires_Limited (Parent (D), N);
10051 end if;
10052
10053 Next_Discriminant (D);
10054 Next (New_D);
10055 end loop;
10056 end;
10057
10058 elsif Present (Discriminant_Specifications (N)) then
10059 Process_Discriminants (N, Prev);
10060 end if;
10061 end Check_Or_Process_Discriminants;
10062
10063 ----------------------
10064 -- Check_Real_Bound --
10065 ----------------------
10066
10067 procedure Check_Real_Bound (Bound : Node_Id) is
10068 begin
10069 if not Is_Real_Type (Etype (Bound)) then
10070 Error_Msg_N
10071 ("bound in real type definition must be of real type", Bound);
10072
10073 elsif not Is_OK_Static_Expression (Bound) then
10074 Flag_Non_Static_Expr
10075 ("non-static expression used for real type bound!", Bound);
10076
10077 else
10078 return;
10079 end if;
10080
10081 Rewrite
10082 (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
10083 Analyze (Bound);
10084 Resolve (Bound, Standard_Float);
10085 end Check_Real_Bound;
10086
10087 ------------------------------
10088 -- Complete_Private_Subtype --
10089 ------------------------------
10090
10091 procedure Complete_Private_Subtype
10092 (Priv : Entity_Id;
10093 Full : Entity_Id;
10094 Full_Base : Entity_Id;
10095 Related_Nod : Node_Id)
10096 is
10097 Save_Next_Entity : Entity_Id;
10098 Save_Homonym : Entity_Id;
10099
10100 begin
10101 -- Set semantic attributes for (implicit) private subtype completion.
10102 -- If the full type has no discriminants, then it is a copy of the full
10103 -- view of the base. Otherwise, it is a subtype of the base with a
10104 -- possible discriminant constraint. Save and restore the original
10105 -- Next_Entity field of full to ensure that the calls to Copy_Node
10106 -- do not corrupt the entity chain.
10107
10108 -- Note that the type of the full view is the same entity as the type of
10109 -- the partial view. In this fashion, the subtype has access to the
10110 -- correct view of the parent.
10111
10112 Save_Next_Entity := Next_Entity (Full);
10113 Save_Homonym := Homonym (Priv);
10114
10115 case Ekind (Full_Base) is
10116 when E_Record_Type |
10117 E_Record_Subtype |
10118 Class_Wide_Kind |
10119 Private_Kind |
10120 Task_Kind |
10121 Protected_Kind =>
10122 Copy_Node (Priv, Full);
10123
10124 Set_Has_Discriminants (Full, Has_Discriminants (Full_Base));
10125 Set_First_Entity (Full, First_Entity (Full_Base));
10126 Set_Last_Entity (Full, Last_Entity (Full_Base));
10127
10128 when others =>
10129 Copy_Node (Full_Base, Full);
10130 Set_Chars (Full, Chars (Priv));
10131 Conditional_Delay (Full, Priv);
10132 Set_Sloc (Full, Sloc (Priv));
10133 end case;
10134
10135 Set_Next_Entity (Full, Save_Next_Entity);
10136 Set_Homonym (Full, Save_Homonym);
10137 Set_Associated_Node_For_Itype (Full, Related_Nod);
10138
10139 -- Set common attributes for all subtypes: kind, convention, etc.
10140
10141 Set_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
10142 Set_Convention (Full, Convention (Full_Base));
10143
10144 -- The Etype of the full view is inconsistent. Gigi needs to see the
10145 -- structural full view, which is what the current scheme gives:
10146 -- the Etype of the full view is the etype of the full base. However,
10147 -- if the full base is a derived type, the full view then looks like
10148 -- a subtype of the parent, not a subtype of the full base. If instead
10149 -- we write:
10150
10151 -- Set_Etype (Full, Full_Base);
10152
10153 -- then we get inconsistencies in the front-end (confusion between
10154 -- views). Several outstanding bugs are related to this ???
10155
10156 Set_Is_First_Subtype (Full, False);
10157 Set_Scope (Full, Scope (Priv));
10158 Set_Size_Info (Full, Full_Base);
10159 Set_RM_Size (Full, RM_Size (Full_Base));
10160 Set_Is_Itype (Full);
10161
10162 -- A subtype of a private-type-without-discriminants, whose full-view
10163 -- has discriminants with default expressions, is not constrained!
10164
10165 if not Has_Discriminants (Priv) then
10166 Set_Is_Constrained (Full, Is_Constrained (Full_Base));
10167
10168 if Has_Discriminants (Full_Base) then
10169 Set_Discriminant_Constraint
10170 (Full, Discriminant_Constraint (Full_Base));
10171
10172 -- The partial view may have been indefinite, the full view
10173 -- might not be.
10174
10175 Set_Has_Unknown_Discriminants
10176 (Full, Has_Unknown_Discriminants (Full_Base));
10177 end if;
10178 end if;
10179
10180 Set_First_Rep_Item (Full, First_Rep_Item (Full_Base));
10181 Set_Depends_On_Private (Full, Has_Private_Component (Full));
10182
10183 -- Freeze the private subtype entity if its parent is delayed, and not
10184 -- already frozen. We skip this processing if the type is an anonymous
10185 -- subtype of a record component, or is the corresponding record of a
10186 -- protected type, since ???
10187
10188 if not Is_Type (Scope (Full)) then
10189 Set_Has_Delayed_Freeze (Full,
10190 Has_Delayed_Freeze (Full_Base)
10191 and then (not Is_Frozen (Full_Base)));
10192 end if;
10193
10194 Set_Freeze_Node (Full, Empty);
10195 Set_Is_Frozen (Full, False);
10196 Set_Full_View (Priv, Full);
10197
10198 if Has_Discriminants (Full) then
10199 Set_Stored_Constraint_From_Discriminant_Constraint (Full);
10200 Set_Stored_Constraint (Priv, Stored_Constraint (Full));
10201
10202 if Has_Unknown_Discriminants (Full) then
10203 Set_Discriminant_Constraint (Full, No_Elist);
10204 end if;
10205 end if;
10206
10207 if Ekind (Full_Base) = E_Record_Type
10208 and then Has_Discriminants (Full_Base)
10209 and then Has_Discriminants (Priv) -- might not, if errors
10210 and then not Has_Unknown_Discriminants (Priv)
10211 and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
10212 then
10213 Create_Constrained_Components
10214 (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
10215
10216 -- If the full base is itself derived from private, build a congruent
10217 -- subtype of its underlying type, for use by the back end. For a
10218 -- constrained record component, the declaration cannot be placed on
10219 -- the component list, but it must nevertheless be built an analyzed, to
10220 -- supply enough information for Gigi to compute the size of component.
10221
10222 elsif Ekind (Full_Base) in Private_Kind
10223 and then Is_Derived_Type (Full_Base)
10224 and then Has_Discriminants (Full_Base)
10225 and then (Ekind (Current_Scope) /= E_Record_Subtype)
10226 then
10227 if not Is_Itype (Priv)
10228 and then
10229 Nkind (Subtype_Indication (Parent (Priv))) = N_Subtype_Indication
10230 then
10231 Build_Underlying_Full_View
10232 (Parent (Priv), Full, Etype (Full_Base));
10233
10234 elsif Nkind (Related_Nod) = N_Component_Declaration then
10235 Build_Underlying_Full_View (Related_Nod, Full, Etype (Full_Base));
10236 end if;
10237
10238 elsif Is_Record_Type (Full_Base) then
10239
10240 -- Show Full is simply a renaming of Full_Base
10241
10242 Set_Cloned_Subtype (Full, Full_Base);
10243 end if;
10244
10245 -- It is unsafe to share to bounds of a scalar type, because the Itype
10246 -- is elaborated on demand, and if a bound is non-static then different
10247 -- orders of elaboration in different units will lead to different
10248 -- external symbols.
10249
10250 if Is_Scalar_Type (Full_Base) then
10251 Set_Scalar_Range (Full,
10252 Make_Range (Sloc (Related_Nod),
10253 Low_Bound =>
10254 Duplicate_Subexpr_No_Checks (Type_Low_Bound (Full_Base)),
10255 High_Bound =>
10256 Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
10257
10258 -- This completion inherits the bounds of the full parent, but if
10259 -- the parent is an unconstrained floating point type, so is the
10260 -- completion.
10261
10262 if Is_Floating_Point_Type (Full_Base) then
10263 Set_Includes_Infinities
10264 (Scalar_Range (Full), Has_Infinities (Full_Base));
10265 end if;
10266 end if;
10267
10268 -- ??? It seems that a lot of fields are missing that should be copied
10269 -- from Full_Base to Full. Here are some that are introduced in a
10270 -- non-disruptive way but a cleanup is necessary.
10271
10272 if Is_Tagged_Type (Full_Base) then
10273 Set_Is_Tagged_Type (Full);
10274 Set_Direct_Primitive_Operations (Full,
10275 Direct_Primitive_Operations (Full_Base));
10276
10277 -- Inherit class_wide type of full_base in case the partial view was
10278 -- not tagged. Otherwise it has already been created when the private
10279 -- subtype was analyzed.
10280
10281 if No (Class_Wide_Type (Full)) then
10282 Set_Class_Wide_Type (Full, Class_Wide_Type (Full_Base));
10283 end if;
10284
10285 -- If this is a subtype of a protected or task type, constrain its
10286 -- corresponding record, unless this is a subtype without constraints,
10287 -- i.e. a simple renaming as with an actual subtype in an instance.
10288
10289 elsif Is_Concurrent_Type (Full_Base) then
10290 if Has_Discriminants (Full)
10291 and then Present (Corresponding_Record_Type (Full_Base))
10292 and then
10293 not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
10294 then
10295 Set_Corresponding_Record_Type (Full,
10296 Constrain_Corresponding_Record
10297 (Full, Corresponding_Record_Type (Full_Base),
10298 Related_Nod, Full_Base));
10299
10300 else
10301 Set_Corresponding_Record_Type (Full,
10302 Corresponding_Record_Type (Full_Base));
10303 end if;
10304 end if;
10305
10306 -- Link rep item chain, and also setting of Has_Predicates from private
10307 -- subtype to full subtype, since we will need these on the full subtype
10308 -- to create the predicate function. Note that the full subtype may
10309 -- already have rep items, inherited from the full view of the base
10310 -- type, so we must be sure not to overwrite these entries.
10311
10312 declare
10313 Append : Boolean;
10314 Item : Node_Id;
10315 Next_Item : Node_Id;
10316
10317 begin
10318 Item := First_Rep_Item (Full);
10319
10320 -- If no existing rep items on full type, we can just link directly
10321 -- to the list of items on the private type.
10322
10323 if No (Item) then
10324 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
10325
10326 -- Otherwise, search to the end of items currently linked to the full
10327 -- subtype and append the private items to the end. However, if Priv
10328 -- and Full already have the same list of rep items, then the append
10329 -- is not done, as that would create a circularity.
10330
10331 elsif Item /= First_Rep_Item (Priv) then
10332 Append := True;
10333
10334 loop
10335 Next_Item := Next_Rep_Item (Item);
10336 exit when No (Next_Item);
10337 Item := Next_Item;
10338
10339 -- If the private view has aspect specifications, the full view
10340 -- inherits them. Since these aspects may already have been
10341 -- attached to the full view during derivation, do not append
10342 -- them if already present.
10343
10344 if Item = First_Rep_Item (Priv) then
10345 Append := False;
10346 exit;
10347 end if;
10348 end loop;
10349
10350 -- And link the private type items at the end of the chain
10351
10352 if Append then
10353 Set_Next_Rep_Item (Item, First_Rep_Item (Priv));
10354 end if;
10355 end if;
10356 end;
10357
10358 -- Make sure Has_Predicates is set on full type if it is set on the
10359 -- private type. Note that it may already be set on the full type and
10360 -- if so, we don't want to unset it.
10361
10362 if Has_Predicates (Priv) then
10363 Set_Has_Predicates (Full);
10364 end if;
10365 end Complete_Private_Subtype;
10366
10367 ----------------------------
10368 -- Constant_Redeclaration --
10369 ----------------------------
10370
10371 procedure Constant_Redeclaration
10372 (Id : Entity_Id;
10373 N : Node_Id;
10374 T : out Entity_Id)
10375 is
10376 Prev : constant Entity_Id := Current_Entity_In_Scope (Id);
10377 Obj_Def : constant Node_Id := Object_Definition (N);
10378 New_T : Entity_Id;
10379
10380 procedure Check_Possible_Deferred_Completion
10381 (Prev_Id : Entity_Id;
10382 Prev_Obj_Def : Node_Id;
10383 Curr_Obj_Def : Node_Id);
10384 -- Determine whether the two object definitions describe the partial
10385 -- and the full view of a constrained deferred constant. Generate
10386 -- a subtype for the full view and verify that it statically matches
10387 -- the subtype of the partial view.
10388
10389 procedure Check_Recursive_Declaration (Typ : Entity_Id);
10390 -- If deferred constant is an access type initialized with an allocator,
10391 -- check whether there is an illegal recursion in the definition,
10392 -- through a default value of some record subcomponent. This is normally
10393 -- detected when generating init procs, but requires this additional
10394 -- mechanism when expansion is disabled.
10395
10396 ----------------------------------------
10397 -- Check_Possible_Deferred_Completion --
10398 ----------------------------------------
10399
10400 procedure Check_Possible_Deferred_Completion
10401 (Prev_Id : Entity_Id;
10402 Prev_Obj_Def : Node_Id;
10403 Curr_Obj_Def : Node_Id)
10404 is
10405 begin
10406 if Nkind (Prev_Obj_Def) = N_Subtype_Indication
10407 and then Present (Constraint (Prev_Obj_Def))
10408 and then Nkind (Curr_Obj_Def) = N_Subtype_Indication
10409 and then Present (Constraint (Curr_Obj_Def))
10410 then
10411 declare
10412 Loc : constant Source_Ptr := Sloc (N);
10413 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
10414 Decl : constant Node_Id :=
10415 Make_Subtype_Declaration (Loc,
10416 Defining_Identifier => Def_Id,
10417 Subtype_Indication =>
10418 Relocate_Node (Curr_Obj_Def));
10419
10420 begin
10421 Insert_Before_And_Analyze (N, Decl);
10422 Set_Etype (Id, Def_Id);
10423
10424 if not Subtypes_Statically_Match (Etype (Prev_Id), Def_Id) then
10425 Error_Msg_Sloc := Sloc (Prev_Id);
10426 Error_Msg_N ("subtype does not statically match deferred " &
10427 "declaration#", N);
10428 end if;
10429 end;
10430 end if;
10431 end Check_Possible_Deferred_Completion;
10432
10433 ---------------------------------
10434 -- Check_Recursive_Declaration --
10435 ---------------------------------
10436
10437 procedure Check_Recursive_Declaration (Typ : Entity_Id) is
10438 Comp : Entity_Id;
10439
10440 begin
10441 if Is_Record_Type (Typ) then
10442 Comp := First_Component (Typ);
10443 while Present (Comp) loop
10444 if Comes_From_Source (Comp) then
10445 if Present (Expression (Parent (Comp)))
10446 and then Is_Entity_Name (Expression (Parent (Comp)))
10447 and then Entity (Expression (Parent (Comp))) = Prev
10448 then
10449 Error_Msg_Sloc := Sloc (Parent (Comp));
10450 Error_Msg_NE
10451 ("illegal circularity with declaration for&#",
10452 N, Comp);
10453 return;
10454
10455 elsif Is_Record_Type (Etype (Comp)) then
10456 Check_Recursive_Declaration (Etype (Comp));
10457 end if;
10458 end if;
10459
10460 Next_Component (Comp);
10461 end loop;
10462 end if;
10463 end Check_Recursive_Declaration;
10464
10465 -- Start of processing for Constant_Redeclaration
10466
10467 begin
10468 if Nkind (Parent (Prev)) = N_Object_Declaration then
10469 if Nkind (Object_Definition
10470 (Parent (Prev))) = N_Subtype_Indication
10471 then
10472 -- Find type of new declaration. The constraints of the two
10473 -- views must match statically, but there is no point in
10474 -- creating an itype for the full view.
10475
10476 if Nkind (Obj_Def) = N_Subtype_Indication then
10477 Find_Type (Subtype_Mark (Obj_Def));
10478 New_T := Entity (Subtype_Mark (Obj_Def));
10479
10480 else
10481 Find_Type (Obj_Def);
10482 New_T := Entity (Obj_Def);
10483 end if;
10484
10485 T := Etype (Prev);
10486
10487 else
10488 -- The full view may impose a constraint, even if the partial
10489 -- view does not, so construct the subtype.
10490
10491 New_T := Find_Type_Of_Object (Obj_Def, N);
10492 T := New_T;
10493 end if;
10494
10495 else
10496 -- Current declaration is illegal, diagnosed below in Enter_Name
10497
10498 T := Empty;
10499 New_T := Any_Type;
10500 end if;
10501
10502 -- If previous full declaration or a renaming declaration exists, or if
10503 -- a homograph is present, let Enter_Name handle it, either with an
10504 -- error or with the removal of an overridden implicit subprogram.
10505
10506 if Ekind (Prev) /= E_Constant
10507 or else Nkind (Parent (Prev)) = N_Object_Renaming_Declaration
10508 or else Present (Expression (Parent (Prev)))
10509 or else Present (Full_View (Prev))
10510 then
10511 Enter_Name (Id);
10512
10513 -- Verify that types of both declarations match, or else that both types
10514 -- are anonymous access types whose designated subtypes statically match
10515 -- (as allowed in Ada 2005 by AI-385).
10516
10517 elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
10518 and then
10519 (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
10520 or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
10521 or else Is_Access_Constant (Etype (New_T)) /=
10522 Is_Access_Constant (Etype (Prev))
10523 or else Can_Never_Be_Null (Etype (New_T)) /=
10524 Can_Never_Be_Null (Etype (Prev))
10525 or else Null_Exclusion_Present (Parent (Prev)) /=
10526 Null_Exclusion_Present (Parent (Id))
10527 or else not Subtypes_Statically_Match
10528 (Designated_Type (Etype (Prev)),
10529 Designated_Type (Etype (New_T))))
10530 then
10531 Error_Msg_Sloc := Sloc (Prev);
10532 Error_Msg_N ("type does not match declaration#", N);
10533 Set_Full_View (Prev, Id);
10534 Set_Etype (Id, Any_Type);
10535
10536 elsif
10537 Null_Exclusion_Present (Parent (Prev))
10538 and then not Null_Exclusion_Present (N)
10539 then
10540 Error_Msg_Sloc := Sloc (Prev);
10541 Error_Msg_N ("null-exclusion does not match declaration#", N);
10542 Set_Full_View (Prev, Id);
10543 Set_Etype (Id, Any_Type);
10544
10545 -- If so, process the full constant declaration
10546
10547 else
10548 -- RM 7.4 (6): If the subtype defined by the subtype_indication in
10549 -- the deferred declaration is constrained, then the subtype defined
10550 -- by the subtype_indication in the full declaration shall match it
10551 -- statically.
10552
10553 Check_Possible_Deferred_Completion
10554 (Prev_Id => Prev,
10555 Prev_Obj_Def => Object_Definition (Parent (Prev)),
10556 Curr_Obj_Def => Obj_Def);
10557
10558 Set_Full_View (Prev, Id);
10559 Set_Is_Public (Id, Is_Public (Prev));
10560 Set_Is_Internal (Id);
10561 Append_Entity (Id, Current_Scope);
10562
10563 -- Check ALIASED present if present before (RM 7.4(7))
10564
10565 if Is_Aliased (Prev)
10566 and then not Aliased_Present (N)
10567 then
10568 Error_Msg_Sloc := Sloc (Prev);
10569 Error_Msg_N ("ALIASED required (see declaration#)", N);
10570 end if;
10571
10572 -- Check that placement is in private part and that the incomplete
10573 -- declaration appeared in the visible part.
10574
10575 if Ekind (Current_Scope) = E_Package
10576 and then not In_Private_Part (Current_Scope)
10577 then
10578 Error_Msg_Sloc := Sloc (Prev);
10579 Error_Msg_N
10580 ("full constant for declaration#"
10581 & " must be in private part", N);
10582
10583 elsif Ekind (Current_Scope) = E_Package
10584 and then
10585 List_Containing (Parent (Prev)) /=
10586 Visible_Declarations
10587 (Specification (Unit_Declaration_Node (Current_Scope)))
10588 then
10589 Error_Msg_N
10590 ("deferred constant must be declared in visible part",
10591 Parent (Prev));
10592 end if;
10593
10594 if Is_Access_Type (T)
10595 and then Nkind (Expression (N)) = N_Allocator
10596 then
10597 Check_Recursive_Declaration (Designated_Type (T));
10598 end if;
10599 end if;
10600 end Constant_Redeclaration;
10601
10602 ----------------------
10603 -- Constrain_Access --
10604 ----------------------
10605
10606 procedure Constrain_Access
10607 (Def_Id : in out Entity_Id;
10608 S : Node_Id;
10609 Related_Nod : Node_Id)
10610 is
10611 T : constant Entity_Id := Entity (Subtype_Mark (S));
10612 Desig_Type : constant Entity_Id := Designated_Type (T);
10613 Desig_Subtype : Entity_Id := Create_Itype (E_Void, Related_Nod);
10614 Constraint_OK : Boolean := True;
10615
10616 function Has_Defaulted_Discriminants (Typ : Entity_Id) return Boolean;
10617 -- Simple predicate to test for defaulted discriminants
10618 -- Shouldn't this be in sem_util???
10619
10620 ---------------------------------
10621 -- Has_Defaulted_Discriminants --
10622 ---------------------------------
10623
10624 function Has_Defaulted_Discriminants (Typ : Entity_Id) return Boolean is
10625 begin
10626 return Has_Discriminants (Typ)
10627 and then Present (First_Discriminant (Typ))
10628 and then Present
10629 (Discriminant_Default_Value (First_Discriminant (Typ)));
10630 end Has_Defaulted_Discriminants;
10631
10632 -- Start of processing for Constrain_Access
10633
10634 begin
10635 if Is_Array_Type (Desig_Type) then
10636 Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
10637
10638 elsif (Is_Record_Type (Desig_Type)
10639 or else Is_Incomplete_Or_Private_Type (Desig_Type))
10640 and then not Is_Constrained (Desig_Type)
10641 then
10642 -- ??? The following code is a temporary kludge to ignore a
10643 -- discriminant constraint on access type if it is constraining
10644 -- the current record. Avoid creating the implicit subtype of the
10645 -- record we are currently compiling since right now, we cannot
10646 -- handle these. For now, just return the access type itself.
10647
10648 if Desig_Type = Current_Scope
10649 and then No (Def_Id)
10650 then
10651 Set_Ekind (Desig_Subtype, E_Record_Subtype);
10652 Def_Id := Entity (Subtype_Mark (S));
10653
10654 -- This call added to ensure that the constraint is analyzed
10655 -- (needed for a B test). Note that we still return early from
10656 -- this procedure to avoid recursive processing. ???
10657
10658 Constrain_Discriminated_Type
10659 (Desig_Subtype, S, Related_Nod, For_Access => True);
10660 return;
10661 end if;
10662
10663 if (Ekind (T) = E_General_Access_Type
10664 or else Ada_Version >= Ada_2005)
10665 and then Has_Private_Declaration (Desig_Type)
10666 and then In_Open_Scopes (Scope (Desig_Type))
10667 and then Has_Discriminants (Desig_Type)
10668 then
10669 -- Enforce rule that the constraint is illegal if there is
10670 -- an unconstrained view of the designated type. This means
10671 -- that the partial view (either a private type declaration or
10672 -- a derivation from a private type) has no discriminants.
10673 -- (Defect Report 8652/0008, Technical Corrigendum 1, checked
10674 -- by ACATS B371001).
10675
10676 -- Rule updated for Ada 2005: the private type is said to have
10677 -- a constrained partial view, given that objects of the type
10678 -- can be declared. Furthermore, the rule applies to all access
10679 -- types, unlike the rule concerning default discriminants.
10680
10681 declare
10682 Pack : constant Node_Id :=
10683 Unit_Declaration_Node (Scope (Desig_Type));
10684 Decls : List_Id;
10685 Decl : Node_Id;
10686
10687 begin
10688 if Nkind (Pack) = N_Package_Declaration then
10689 Decls := Visible_Declarations (Specification (Pack));
10690 Decl := First (Decls);
10691 while Present (Decl) loop
10692 if (Nkind (Decl) = N_Private_Type_Declaration
10693 and then
10694 Chars (Defining_Identifier (Decl)) =
10695 Chars (Desig_Type))
10696
10697 or else
10698 (Nkind (Decl) = N_Full_Type_Declaration
10699 and then
10700 Chars (Defining_Identifier (Decl)) =
10701 Chars (Desig_Type)
10702 and then Is_Derived_Type (Desig_Type)
10703 and then
10704 Has_Private_Declaration (Etype (Desig_Type)))
10705 then
10706 if No (Discriminant_Specifications (Decl)) then
10707 Error_Msg_N
10708 ("cannot constrain general access type if " &
10709 "designated type has constrained partial view",
10710 S);
10711 end if;
10712
10713 exit;
10714 end if;
10715
10716 Next (Decl);
10717 end loop;
10718 end if;
10719 end;
10720 end if;
10721
10722 Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
10723 For_Access => True);
10724
10725 elsif (Is_Task_Type (Desig_Type)
10726 or else Is_Protected_Type (Desig_Type))
10727 and then not Is_Constrained (Desig_Type)
10728 then
10729 Constrain_Concurrent
10730 (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
10731
10732 else
10733 Error_Msg_N ("invalid constraint on access type", S);
10734 Desig_Subtype := Desig_Type; -- Ignore invalid constraint.
10735 Constraint_OK := False;
10736 end if;
10737
10738 if No (Def_Id) then
10739 Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
10740 else
10741 Set_Ekind (Def_Id, E_Access_Subtype);
10742 end if;
10743
10744 if Constraint_OK then
10745 Set_Etype (Def_Id, Base_Type (T));
10746
10747 if Is_Private_Type (Desig_Type) then
10748 Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
10749 end if;
10750 else
10751 Set_Etype (Def_Id, Any_Type);
10752 end if;
10753
10754 Set_Size_Info (Def_Id, T);
10755 Set_Is_Constrained (Def_Id, Constraint_OK);
10756 Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
10757 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
10758 Set_Is_Access_Constant (Def_Id, Is_Access_Constant (T));
10759
10760 Conditional_Delay (Def_Id, T);
10761
10762 -- AI-363 : Subtypes of general access types whose designated types have
10763 -- default discriminants are disallowed. In instances, the rule has to
10764 -- be checked against the actual, of which T is the subtype. In a
10765 -- generic body, the rule is checked assuming that the actual type has
10766 -- defaulted discriminants.
10767
10768 if Ada_Version >= Ada_2005 or else Warn_On_Ada_2005_Compatibility then
10769 if Ekind (Base_Type (T)) = E_General_Access_Type
10770 and then Has_Defaulted_Discriminants (Desig_Type)
10771 then
10772 if Ada_Version < Ada_2005 then
10773 Error_Msg_N
10774 ("access subtype of general access type would not " &
10775 "be allowed in Ada 2005?", S);
10776 else
10777 Error_Msg_N
10778 ("access subtype of general access type not allowed", S);
10779 end if;
10780
10781 Error_Msg_N ("\discriminants have defaults", S);
10782
10783 elsif Is_Access_Type (T)
10784 and then Is_Generic_Type (Desig_Type)
10785 and then Has_Discriminants (Desig_Type)
10786 and then In_Package_Body (Current_Scope)
10787 then
10788 if Ada_Version < Ada_2005 then
10789 Error_Msg_N
10790 ("access subtype would not be allowed in generic body " &
10791 "in Ada 2005?", S);
10792 else
10793 Error_Msg_N
10794 ("access subtype not allowed in generic body", S);
10795 end if;
10796
10797 Error_Msg_N
10798 ("\designated type is a discriminated formal", S);
10799 end if;
10800 end if;
10801 end Constrain_Access;
10802
10803 ---------------------
10804 -- Constrain_Array --
10805 ---------------------
10806
10807 procedure Constrain_Array
10808 (Def_Id : in out Entity_Id;
10809 SI : Node_Id;
10810 Related_Nod : Node_Id;
10811 Related_Id : Entity_Id;
10812 Suffix : Character)
10813 is
10814 C : constant Node_Id := Constraint (SI);
10815 Number_Of_Constraints : Nat := 0;
10816 Index : Node_Id;
10817 S, T : Entity_Id;
10818 Constraint_OK : Boolean := True;
10819
10820 begin
10821 T := Entity (Subtype_Mark (SI));
10822
10823 if Ekind (T) in Access_Kind then
10824 T := Designated_Type (T);
10825 end if;
10826
10827 -- If an index constraint follows a subtype mark in a subtype indication
10828 -- then the type or subtype denoted by the subtype mark must not already
10829 -- impose an index constraint. The subtype mark must denote either an
10830 -- unconstrained array type or an access type whose designated type
10831 -- is such an array type... (RM 3.6.1)
10832
10833 if Is_Constrained (T) then
10834 Error_Msg_N ("array type is already constrained", Subtype_Mark (SI));
10835 Constraint_OK := False;
10836
10837 else
10838 S := First (Constraints (C));
10839 while Present (S) loop
10840 Number_Of_Constraints := Number_Of_Constraints + 1;
10841 Next (S);
10842 end loop;
10843
10844 -- In either case, the index constraint must provide a discrete
10845 -- range for each index of the array type and the type of each
10846 -- discrete range must be the same as that of the corresponding
10847 -- index. (RM 3.6.1)
10848
10849 if Number_Of_Constraints /= Number_Dimensions (T) then
10850 Error_Msg_NE ("incorrect number of index constraints for }", C, T);
10851 Constraint_OK := False;
10852
10853 else
10854 S := First (Constraints (C));
10855 Index := First_Index (T);
10856 Analyze (Index);
10857
10858 -- Apply constraints to each index type
10859
10860 for J in 1 .. Number_Of_Constraints loop
10861 Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
10862 Next (Index);
10863 Next (S);
10864 end loop;
10865
10866 end if;
10867 end if;
10868
10869 if No (Def_Id) then
10870 Def_Id :=
10871 Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
10872 Set_Parent (Def_Id, Related_Nod);
10873
10874 else
10875 Set_Ekind (Def_Id, E_Array_Subtype);
10876 end if;
10877
10878 Set_Size_Info (Def_Id, (T));
10879 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10880 Set_Etype (Def_Id, Base_Type (T));
10881
10882 if Constraint_OK then
10883 Set_First_Index (Def_Id, First (Constraints (C)));
10884 else
10885 Set_First_Index (Def_Id, First_Index (T));
10886 end if;
10887
10888 Set_Is_Constrained (Def_Id, True);
10889 Set_Is_Aliased (Def_Id, Is_Aliased (T));
10890 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
10891
10892 Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
10893 Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
10894
10895 -- A subtype does not inherit the packed_array_type of is parent. We
10896 -- need to initialize the attribute because if Def_Id is previously
10897 -- analyzed through a limited_with clause, it will have the attributes
10898 -- of an incomplete type, one of which is an Elist that overlaps the
10899 -- Packed_Array_Type field.
10900
10901 Set_Packed_Array_Type (Def_Id, Empty);
10902
10903 -- Build a freeze node if parent still needs one. Also make sure that
10904 -- the Depends_On_Private status is set because the subtype will need
10905 -- reprocessing at the time the base type does, and also we must set a
10906 -- conditional delay.
10907
10908 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
10909 Conditional_Delay (Def_Id, T);
10910 end Constrain_Array;
10911
10912 ------------------------------
10913 -- Constrain_Component_Type --
10914 ------------------------------
10915
10916 function Constrain_Component_Type
10917 (Comp : Entity_Id;
10918 Constrained_Typ : Entity_Id;
10919 Related_Node : Node_Id;
10920 Typ : Entity_Id;
10921 Constraints : Elist_Id) return Entity_Id
10922 is
10923 Loc : constant Source_Ptr := Sloc (Constrained_Typ);
10924 Compon_Type : constant Entity_Id := Etype (Comp);
10925
10926 function Build_Constrained_Array_Type
10927 (Old_Type : Entity_Id) return Entity_Id;
10928 -- If Old_Type is an array type, one of whose indexes is constrained
10929 -- by a discriminant, build an Itype whose constraint replaces the
10930 -- discriminant with its value in the constraint.
10931
10932 function Build_Constrained_Discriminated_Type
10933 (Old_Type : Entity_Id) return Entity_Id;
10934 -- Ditto for record components
10935
10936 function Build_Constrained_Access_Type
10937 (Old_Type : Entity_Id) return Entity_Id;
10938 -- Ditto for access types. Makes use of previous two functions, to
10939 -- constrain designated type.
10940
10941 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id;
10942 -- T is an array or discriminated type, C is a list of constraints
10943 -- that apply to T. This routine builds the constrained subtype.
10944
10945 function Is_Discriminant (Expr : Node_Id) return Boolean;
10946 -- Returns True if Expr is a discriminant
10947
10948 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id;
10949 -- Find the value of discriminant Discrim in Constraint
10950
10951 -----------------------------------
10952 -- Build_Constrained_Access_Type --
10953 -----------------------------------
10954
10955 function Build_Constrained_Access_Type
10956 (Old_Type : Entity_Id) return Entity_Id
10957 is
10958 Desig_Type : constant Entity_Id := Designated_Type (Old_Type);
10959 Itype : Entity_Id;
10960 Desig_Subtype : Entity_Id;
10961 Scop : Entity_Id;
10962
10963 begin
10964 -- if the original access type was not embedded in the enclosing
10965 -- type definition, there is no need to produce a new access
10966 -- subtype. In fact every access type with an explicit constraint
10967 -- generates an itype whose scope is the enclosing record.
10968
10969 if not Is_Type (Scope (Old_Type)) then
10970 return Old_Type;
10971
10972 elsif Is_Array_Type (Desig_Type) then
10973 Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
10974
10975 elsif Has_Discriminants (Desig_Type) then
10976
10977 -- This may be an access type to an enclosing record type for
10978 -- which we are constructing the constrained components. Return
10979 -- the enclosing record subtype. This is not always correct,
10980 -- but avoids infinite recursion. ???
10981
10982 Desig_Subtype := Any_Type;
10983
10984 for J in reverse 0 .. Scope_Stack.Last loop
10985 Scop := Scope_Stack.Table (J).Entity;
10986
10987 if Is_Type (Scop)
10988 and then Base_Type (Scop) = Base_Type (Desig_Type)
10989 then
10990 Desig_Subtype := Scop;
10991 end if;
10992
10993 exit when not Is_Type (Scop);
10994 end loop;
10995
10996 if Desig_Subtype = Any_Type then
10997 Desig_Subtype :=
10998 Build_Constrained_Discriminated_Type (Desig_Type);
10999 end if;
11000
11001 else
11002 return Old_Type;
11003 end if;
11004
11005 if Desig_Subtype /= Desig_Type then
11006
11007 -- The Related_Node better be here or else we won't be able
11008 -- to attach new itypes to a node in the tree.
11009
11010 pragma Assert (Present (Related_Node));
11011
11012 Itype := Create_Itype (E_Access_Subtype, Related_Node);
11013
11014 Set_Etype (Itype, Base_Type (Old_Type));
11015 Set_Size_Info (Itype, (Old_Type));
11016 Set_Directly_Designated_Type (Itype, Desig_Subtype);
11017 Set_Depends_On_Private (Itype, Has_Private_Component
11018 (Old_Type));
11019 Set_Is_Access_Constant (Itype, Is_Access_Constant
11020 (Old_Type));
11021
11022 -- The new itype needs freezing when it depends on a not frozen
11023 -- type and the enclosing subtype needs freezing.
11024
11025 if Has_Delayed_Freeze (Constrained_Typ)
11026 and then not Is_Frozen (Constrained_Typ)
11027 then
11028 Conditional_Delay (Itype, Base_Type (Old_Type));
11029 end if;
11030
11031 return Itype;
11032
11033 else
11034 return Old_Type;
11035 end if;
11036 end Build_Constrained_Access_Type;
11037
11038 ----------------------------------
11039 -- Build_Constrained_Array_Type --
11040 ----------------------------------
11041
11042 function Build_Constrained_Array_Type
11043 (Old_Type : Entity_Id) return Entity_Id
11044 is
11045 Lo_Expr : Node_Id;
11046 Hi_Expr : Node_Id;
11047 Old_Index : Node_Id;
11048 Range_Node : Node_Id;
11049 Constr_List : List_Id;
11050
11051 Need_To_Create_Itype : Boolean := False;
11052
11053 begin
11054 Old_Index := First_Index (Old_Type);
11055 while Present (Old_Index) loop
11056 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
11057
11058 if Is_Discriminant (Lo_Expr)
11059 or else Is_Discriminant (Hi_Expr)
11060 then
11061 Need_To_Create_Itype := True;
11062 end if;
11063
11064 Next_Index (Old_Index);
11065 end loop;
11066
11067 if Need_To_Create_Itype then
11068 Constr_List := New_List;
11069
11070 Old_Index := First_Index (Old_Type);
11071 while Present (Old_Index) loop
11072 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
11073
11074 if Is_Discriminant (Lo_Expr) then
11075 Lo_Expr := Get_Discr_Value (Lo_Expr);
11076 end if;
11077
11078 if Is_Discriminant (Hi_Expr) then
11079 Hi_Expr := Get_Discr_Value (Hi_Expr);
11080 end if;
11081
11082 Range_Node :=
11083 Make_Range
11084 (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
11085
11086 Append (Range_Node, To => Constr_List);
11087
11088 Next_Index (Old_Index);
11089 end loop;
11090
11091 return Build_Subtype (Old_Type, Constr_List);
11092
11093 else
11094 return Old_Type;
11095 end if;
11096 end Build_Constrained_Array_Type;
11097
11098 ------------------------------------------
11099 -- Build_Constrained_Discriminated_Type --
11100 ------------------------------------------
11101
11102 function Build_Constrained_Discriminated_Type
11103 (Old_Type : Entity_Id) return Entity_Id
11104 is
11105 Expr : Node_Id;
11106 Constr_List : List_Id;
11107 Old_Constraint : Elmt_Id;
11108
11109 Need_To_Create_Itype : Boolean := False;
11110
11111 begin
11112 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
11113 while Present (Old_Constraint) loop
11114 Expr := Node (Old_Constraint);
11115
11116 if Is_Discriminant (Expr) then
11117 Need_To_Create_Itype := True;
11118 end if;
11119
11120 Next_Elmt (Old_Constraint);
11121 end loop;
11122
11123 if Need_To_Create_Itype then
11124 Constr_List := New_List;
11125
11126 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
11127 while Present (Old_Constraint) loop
11128 Expr := Node (Old_Constraint);
11129
11130 if Is_Discriminant (Expr) then
11131 Expr := Get_Discr_Value (Expr);
11132 end if;
11133
11134 Append (New_Copy_Tree (Expr), To => Constr_List);
11135
11136 Next_Elmt (Old_Constraint);
11137 end loop;
11138
11139 return Build_Subtype (Old_Type, Constr_List);
11140
11141 else
11142 return Old_Type;
11143 end if;
11144 end Build_Constrained_Discriminated_Type;
11145
11146 -------------------
11147 -- Build_Subtype --
11148 -------------------
11149
11150 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id is
11151 Indic : Node_Id;
11152 Subtyp_Decl : Node_Id;
11153 Def_Id : Entity_Id;
11154 Btyp : Entity_Id := Base_Type (T);
11155
11156 begin
11157 -- The Related_Node better be here or else we won't be able to
11158 -- attach new itypes to a node in the tree.
11159
11160 pragma Assert (Present (Related_Node));
11161
11162 -- If the view of the component's type is incomplete or private
11163 -- with unknown discriminants, then the constraint must be applied
11164 -- to the full type.
11165
11166 if Has_Unknown_Discriminants (Btyp)
11167 and then Present (Underlying_Type (Btyp))
11168 then
11169 Btyp := Underlying_Type (Btyp);
11170 end if;
11171
11172 Indic :=
11173 Make_Subtype_Indication (Loc,
11174 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
11175 Constraint => Make_Index_Or_Discriminant_Constraint (Loc, C));
11176
11177 Def_Id := Create_Itype (Ekind (T), Related_Node);
11178
11179 Subtyp_Decl :=
11180 Make_Subtype_Declaration (Loc,
11181 Defining_Identifier => Def_Id,
11182 Subtype_Indication => Indic);
11183
11184 Set_Parent (Subtyp_Decl, Parent (Related_Node));
11185
11186 -- Itypes must be analyzed with checks off (see package Itypes)
11187
11188 Analyze (Subtyp_Decl, Suppress => All_Checks);
11189
11190 return Def_Id;
11191 end Build_Subtype;
11192
11193 ---------------------
11194 -- Get_Discr_Value --
11195 ---------------------
11196
11197 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id is
11198 D : Entity_Id;
11199 E : Elmt_Id;
11200
11201 begin
11202 -- The discriminant may be declared for the type, in which case we
11203 -- find it by iterating over the list of discriminants. If the
11204 -- discriminant is inherited from a parent type, it appears as the
11205 -- corresponding discriminant of the current type. This will be the
11206 -- case when constraining an inherited component whose constraint is
11207 -- given by a discriminant of the parent.
11208
11209 D := First_Discriminant (Typ);
11210 E := First_Elmt (Constraints);
11211
11212 while Present (D) loop
11213 if D = Entity (Discrim)
11214 or else D = CR_Discriminant (Entity (Discrim))
11215 or else Corresponding_Discriminant (D) = Entity (Discrim)
11216 then
11217 return Node (E);
11218 end if;
11219
11220 Next_Discriminant (D);
11221 Next_Elmt (E);
11222 end loop;
11223
11224 -- The Corresponding_Discriminant mechanism is incomplete, because
11225 -- the correspondence between new and old discriminants is not one
11226 -- to one: one new discriminant can constrain several old ones. In
11227 -- that case, scan sequentially the stored_constraint, the list of
11228 -- discriminants of the parents, and the constraints.
11229 -- Previous code checked for the present of the Stored_Constraint
11230 -- list for the derived type, but did not use it at all. Should it
11231 -- be present when the component is a discriminated task type?
11232
11233 if Is_Derived_Type (Typ)
11234 and then Scope (Entity (Discrim)) = Etype (Typ)
11235 then
11236 D := First_Discriminant (Etype (Typ));
11237 E := First_Elmt (Constraints);
11238 while Present (D) loop
11239 if D = Entity (Discrim) then
11240 return Node (E);
11241 end if;
11242
11243 Next_Discriminant (D);
11244 Next_Elmt (E);
11245 end loop;
11246 end if;
11247
11248 -- Something is wrong if we did not find the value
11249
11250 raise Program_Error;
11251 end Get_Discr_Value;
11252
11253 ---------------------
11254 -- Is_Discriminant --
11255 ---------------------
11256
11257 function Is_Discriminant (Expr : Node_Id) return Boolean is
11258 Discrim_Scope : Entity_Id;
11259
11260 begin
11261 if Denotes_Discriminant (Expr) then
11262 Discrim_Scope := Scope (Entity (Expr));
11263
11264 -- Either we have a reference to one of Typ's discriminants,
11265
11266 pragma Assert (Discrim_Scope = Typ
11267
11268 -- or to the discriminants of the parent type, in the case
11269 -- of a derivation of a tagged type with variants.
11270
11271 or else Discrim_Scope = Etype (Typ)
11272 or else Full_View (Discrim_Scope) = Etype (Typ)
11273
11274 -- or same as above for the case where the discriminants
11275 -- were declared in Typ's private view.
11276
11277 or else (Is_Private_Type (Discrim_Scope)
11278 and then Chars (Discrim_Scope) = Chars (Typ))
11279
11280 -- or else we are deriving from the full view and the
11281 -- discriminant is declared in the private entity.
11282
11283 or else (Is_Private_Type (Typ)
11284 and then Chars (Discrim_Scope) = Chars (Typ))
11285
11286 -- Or we are constrained the corresponding record of a
11287 -- synchronized type that completes a private declaration.
11288
11289 or else (Is_Concurrent_Record_Type (Typ)
11290 and then
11291 Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
11292
11293 -- or we have a class-wide type, in which case make sure the
11294 -- discriminant found belongs to the root type.
11295
11296 or else (Is_Class_Wide_Type (Typ)
11297 and then Etype (Typ) = Discrim_Scope));
11298
11299 return True;
11300 end if;
11301
11302 -- In all other cases we have something wrong
11303
11304 return False;
11305 end Is_Discriminant;
11306
11307 -- Start of processing for Constrain_Component_Type
11308
11309 begin
11310 if Nkind (Parent (Comp)) = N_Component_Declaration
11311 and then Comes_From_Source (Parent (Comp))
11312 and then Comes_From_Source
11313 (Subtype_Indication (Component_Definition (Parent (Comp))))
11314 and then
11315 Is_Entity_Name
11316 (Subtype_Indication (Component_Definition (Parent (Comp))))
11317 then
11318 return Compon_Type;
11319
11320 elsif Is_Array_Type (Compon_Type) then
11321 return Build_Constrained_Array_Type (Compon_Type);
11322
11323 elsif Has_Discriminants (Compon_Type) then
11324 return Build_Constrained_Discriminated_Type (Compon_Type);
11325
11326 elsif Is_Access_Type (Compon_Type) then
11327 return Build_Constrained_Access_Type (Compon_Type);
11328
11329 else
11330 return Compon_Type;
11331 end if;
11332 end Constrain_Component_Type;
11333
11334 --------------------------
11335 -- Constrain_Concurrent --
11336 --------------------------
11337
11338 -- For concurrent types, the associated record value type carries the same
11339 -- discriminants, so when we constrain a concurrent type, we must constrain
11340 -- the corresponding record type as well.
11341
11342 procedure Constrain_Concurrent
11343 (Def_Id : in out Entity_Id;
11344 SI : Node_Id;
11345 Related_Nod : Node_Id;
11346 Related_Id : Entity_Id;
11347 Suffix : Character)
11348 is
11349 -- Retrieve Base_Type to ensure getting to the concurrent type in the
11350 -- case of a private subtype (needed when only doing semantic analysis).
11351
11352 T_Ent : Entity_Id := Base_Type (Entity (Subtype_Mark (SI)));
11353 T_Val : Entity_Id;
11354
11355 begin
11356 if Ekind (T_Ent) in Access_Kind then
11357 T_Ent := Designated_Type (T_Ent);
11358 end if;
11359
11360 T_Val := Corresponding_Record_Type (T_Ent);
11361
11362 if Present (T_Val) then
11363
11364 if No (Def_Id) then
11365 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
11366 end if;
11367
11368 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
11369
11370 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
11371 Set_Corresponding_Record_Type (Def_Id,
11372 Constrain_Corresponding_Record
11373 (Def_Id, T_Val, Related_Nod, Related_Id));
11374
11375 else
11376 -- If there is no associated record, expansion is disabled and this
11377 -- is a generic context. Create a subtype in any case, so that
11378 -- semantic analysis can proceed.
11379
11380 if No (Def_Id) then
11381 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
11382 end if;
11383
11384 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
11385 end if;
11386 end Constrain_Concurrent;
11387
11388 ------------------------------------
11389 -- Constrain_Corresponding_Record --
11390 ------------------------------------
11391
11392 function Constrain_Corresponding_Record
11393 (Prot_Subt : Entity_Id;
11394 Corr_Rec : Entity_Id;
11395 Related_Nod : Node_Id;
11396 Related_Id : Entity_Id) return Entity_Id
11397 is
11398 T_Sub : constant Entity_Id :=
11399 Create_Itype (E_Record_Subtype, Related_Nod, Related_Id, 'V');
11400
11401 begin
11402 Set_Etype (T_Sub, Corr_Rec);
11403 Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
11404 Set_Is_Constrained (T_Sub, True);
11405 Set_First_Entity (T_Sub, First_Entity (Corr_Rec));
11406 Set_Last_Entity (T_Sub, Last_Entity (Corr_Rec));
11407
11408 -- As elsewhere, we do not want to create a freeze node for this itype
11409 -- if it is created for a constrained component of an enclosing record
11410 -- because references to outer discriminants will appear out of scope.
11411
11412 if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
11413 Conditional_Delay (T_Sub, Corr_Rec);
11414 else
11415 Set_Is_Frozen (T_Sub);
11416 end if;
11417
11418 if Has_Discriminants (Prot_Subt) then -- False only if errors.
11419 Set_Discriminant_Constraint
11420 (T_Sub, Discriminant_Constraint (Prot_Subt));
11421 Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
11422 Create_Constrained_Components
11423 (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
11424 end if;
11425
11426 Set_Depends_On_Private (T_Sub, Has_Private_Component (T_Sub));
11427
11428 return T_Sub;
11429 end Constrain_Corresponding_Record;
11430
11431 -----------------------
11432 -- Constrain_Decimal --
11433 -----------------------
11434
11435 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id) is
11436 T : constant Entity_Id := Entity (Subtype_Mark (S));
11437 C : constant Node_Id := Constraint (S);
11438 Loc : constant Source_Ptr := Sloc (C);
11439 Range_Expr : Node_Id;
11440 Digits_Expr : Node_Id;
11441 Digits_Val : Uint;
11442 Bound_Val : Ureal;
11443
11444 begin
11445 Set_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
11446
11447 if Nkind (C) = N_Range_Constraint then
11448 Range_Expr := Range_Expression (C);
11449 Digits_Val := Digits_Value (T);
11450
11451 else
11452 pragma Assert (Nkind (C) = N_Digits_Constraint);
11453
11454 Check_SPARK_Restriction ("digits constraint is not allowed", S);
11455
11456 Digits_Expr := Digits_Expression (C);
11457 Analyze_And_Resolve (Digits_Expr, Any_Integer);
11458
11459 Check_Digits_Expression (Digits_Expr);
11460 Digits_Val := Expr_Value (Digits_Expr);
11461
11462 if Digits_Val > Digits_Value (T) then
11463 Error_Msg_N
11464 ("digits expression is incompatible with subtype", C);
11465 Digits_Val := Digits_Value (T);
11466 end if;
11467
11468 if Present (Range_Constraint (C)) then
11469 Range_Expr := Range_Expression (Range_Constraint (C));
11470 else
11471 Range_Expr := Empty;
11472 end if;
11473 end if;
11474
11475 Set_Etype (Def_Id, Base_Type (T));
11476 Set_Size_Info (Def_Id, (T));
11477 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
11478 Set_Delta_Value (Def_Id, Delta_Value (T));
11479 Set_Scale_Value (Def_Id, Scale_Value (T));
11480 Set_Small_Value (Def_Id, Small_Value (T));
11481 Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
11482 Set_Digits_Value (Def_Id, Digits_Val);
11483
11484 -- Manufacture range from given digits value if no range present
11485
11486 if No (Range_Expr) then
11487 Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
11488 Range_Expr :=
11489 Make_Range (Loc,
11490 Low_Bound =>
11491 Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
11492 High_Bound =>
11493 Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
11494 end if;
11495
11496 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
11497 Set_Discrete_RM_Size (Def_Id);
11498
11499 -- Unconditionally delay the freeze, since we cannot set size
11500 -- information in all cases correctly until the freeze point.
11501
11502 Set_Has_Delayed_Freeze (Def_Id);
11503 end Constrain_Decimal;
11504
11505 ----------------------------------
11506 -- Constrain_Discriminated_Type --
11507 ----------------------------------
11508
11509 procedure Constrain_Discriminated_Type
11510 (Def_Id : Entity_Id;
11511 S : Node_Id;
11512 Related_Nod : Node_Id;
11513 For_Access : Boolean := False)
11514 is
11515 E : constant Entity_Id := Entity (Subtype_Mark (S));
11516 T : Entity_Id;
11517 C : Node_Id;
11518 Elist : Elist_Id := New_Elmt_List;
11519
11520 procedure Fixup_Bad_Constraint;
11521 -- This is called after finding a bad constraint, and after having
11522 -- posted an appropriate error message. The mission is to leave the
11523 -- entity T in as reasonable state as possible!
11524
11525 --------------------------
11526 -- Fixup_Bad_Constraint --
11527 --------------------------
11528
11529 procedure Fixup_Bad_Constraint is
11530 begin
11531 -- Set a reasonable Ekind for the entity. For an incomplete type,
11532 -- we can't do much, but for other types, we can set the proper
11533 -- corresponding subtype kind.
11534
11535 if Ekind (T) = E_Incomplete_Type then
11536 Set_Ekind (Def_Id, Ekind (T));
11537 else
11538 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
11539 end if;
11540
11541 -- Set Etype to the known type, to reduce chances of cascaded errors
11542
11543 Set_Etype (Def_Id, E);
11544 Set_Error_Posted (Def_Id);
11545 end Fixup_Bad_Constraint;
11546
11547 -- Start of processing for Constrain_Discriminated_Type
11548
11549 begin
11550 C := Constraint (S);
11551
11552 -- A discriminant constraint is only allowed in a subtype indication,
11553 -- after a subtype mark. This subtype mark must denote either a type
11554 -- with discriminants, or an access type whose designated type is a
11555 -- type with discriminants. A discriminant constraint specifies the
11556 -- values of these discriminants (RM 3.7.2(5)).
11557
11558 T := Base_Type (Entity (Subtype_Mark (S)));
11559
11560 if Ekind (T) in Access_Kind then
11561 T := Designated_Type (T);
11562 end if;
11563
11564 -- Ada 2005 (AI-412): Constrained incomplete subtypes are illegal.
11565 -- Avoid generating an error for access-to-incomplete subtypes.
11566
11567 if Ada_Version >= Ada_2005
11568 and then Ekind (T) = E_Incomplete_Type
11569 and then Nkind (Parent (S)) = N_Subtype_Declaration
11570 and then not Is_Itype (Def_Id)
11571 then
11572 -- A little sanity check, emit an error message if the type
11573 -- has discriminants to begin with. Type T may be a regular
11574 -- incomplete type or imported via a limited with clause.
11575
11576 if Has_Discriminants (T)
11577 or else
11578 (From_With_Type (T)
11579 and then Present (Non_Limited_View (T))
11580 and then Nkind (Parent (Non_Limited_View (T))) =
11581 N_Full_Type_Declaration
11582 and then Present (Discriminant_Specifications
11583 (Parent (Non_Limited_View (T)))))
11584 then
11585 Error_Msg_N
11586 ("(Ada 2005) incomplete subtype may not be constrained", C);
11587 else
11588 Error_Msg_N ("invalid constraint: type has no discriminant", C);
11589 end if;
11590
11591 Fixup_Bad_Constraint;
11592 return;
11593
11594 -- Check that the type has visible discriminants. The type may be
11595 -- a private type with unknown discriminants whose full view has
11596 -- discriminants which are invisible.
11597
11598 elsif not Has_Discriminants (T)
11599 or else
11600 (Has_Unknown_Discriminants (T)
11601 and then Is_Private_Type (T))
11602 then
11603 Error_Msg_N ("invalid constraint: type has no discriminant", C);
11604 Fixup_Bad_Constraint;
11605 return;
11606
11607 elsif Is_Constrained (E)
11608 or else (Ekind (E) = E_Class_Wide_Subtype
11609 and then Present (Discriminant_Constraint (E)))
11610 then
11611 Error_Msg_N ("type is already constrained", Subtype_Mark (S));
11612 Fixup_Bad_Constraint;
11613 return;
11614 end if;
11615
11616 -- T may be an unconstrained subtype (e.g. a generic actual).
11617 -- Constraint applies to the base type.
11618
11619 T := Base_Type (T);
11620
11621 Elist := Build_Discriminant_Constraints (T, S);
11622
11623 -- If the list returned was empty we had an error in building the
11624 -- discriminant constraint. We have also already signalled an error
11625 -- in the incomplete type case
11626
11627 if Is_Empty_Elmt_List (Elist) then
11628 Fixup_Bad_Constraint;
11629 return;
11630 end if;
11631
11632 Build_Discriminated_Subtype (T, Def_Id, Elist, Related_Nod, For_Access);
11633 end Constrain_Discriminated_Type;
11634
11635 ---------------------------
11636 -- Constrain_Enumeration --
11637 ---------------------------
11638
11639 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id) is
11640 T : constant Entity_Id := Entity (Subtype_Mark (S));
11641 C : constant Node_Id := Constraint (S);
11642
11643 begin
11644 Set_Ekind (Def_Id, E_Enumeration_Subtype);
11645
11646 Set_First_Literal (Def_Id, First_Literal (Base_Type (T)));
11647
11648 Set_Etype (Def_Id, Base_Type (T));
11649 Set_Size_Info (Def_Id, (T));
11650 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
11651 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
11652
11653 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
11654
11655 Set_Discrete_RM_Size (Def_Id);
11656 end Constrain_Enumeration;
11657
11658 ----------------------
11659 -- Constrain_Float --
11660 ----------------------
11661
11662 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id) is
11663 T : constant Entity_Id := Entity (Subtype_Mark (S));
11664 C : Node_Id;
11665 D : Node_Id;
11666 Rais : Node_Id;
11667
11668 begin
11669 Set_Ekind (Def_Id, E_Floating_Point_Subtype);
11670
11671 Set_Etype (Def_Id, Base_Type (T));
11672 Set_Size_Info (Def_Id, (T));
11673 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
11674
11675 -- Process the constraint
11676
11677 C := Constraint (S);
11678
11679 -- Digits constraint present
11680
11681 if Nkind (C) = N_Digits_Constraint then
11682
11683 Check_SPARK_Restriction ("digits constraint is not allowed", S);
11684 Check_Restriction (No_Obsolescent_Features, C);
11685
11686 if Warn_On_Obsolescent_Feature then
11687 Error_Msg_N
11688 ("subtype digits constraint is an " &
11689 "obsolescent feature (RM J.3(8))?", C);
11690 end if;
11691
11692 D := Digits_Expression (C);
11693 Analyze_And_Resolve (D, Any_Integer);
11694 Check_Digits_Expression (D);
11695 Set_Digits_Value (Def_Id, Expr_Value (D));
11696
11697 -- Check that digits value is in range. Obviously we can do this
11698 -- at compile time, but it is strictly a runtime check, and of
11699 -- course there is an ACVC test that checks this!
11700
11701 if Digits_Value (Def_Id) > Digits_Value (T) then
11702 Error_Msg_Uint_1 := Digits_Value (T);
11703 Error_Msg_N ("?digits value is too large, maximum is ^", D);
11704 Rais :=
11705 Make_Raise_Constraint_Error (Sloc (D),
11706 Reason => CE_Range_Check_Failed);
11707 Insert_Action (Declaration_Node (Def_Id), Rais);
11708 end if;
11709
11710 C := Range_Constraint (C);
11711
11712 -- No digits constraint present
11713
11714 else
11715 Set_Digits_Value (Def_Id, Digits_Value (T));
11716 end if;
11717
11718 -- Range constraint present
11719
11720 if Nkind (C) = N_Range_Constraint then
11721 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
11722
11723 -- No range constraint present
11724
11725 else
11726 pragma Assert (No (C));
11727 Set_Scalar_Range (Def_Id, Scalar_Range (T));
11728 end if;
11729
11730 Set_Is_Constrained (Def_Id);
11731 end Constrain_Float;
11732
11733 ---------------------
11734 -- Constrain_Index --
11735 ---------------------
11736
11737 procedure Constrain_Index
11738 (Index : Node_Id;
11739 S : Node_Id;
11740 Related_Nod : Node_Id;
11741 Related_Id : Entity_Id;
11742 Suffix : Character;
11743 Suffix_Index : Nat)
11744 is
11745 Def_Id : Entity_Id;
11746 R : Node_Id := Empty;
11747 T : constant Entity_Id := Etype (Index);
11748
11749 begin
11750 if Nkind (S) = N_Range
11751 or else
11752 (Nkind (S) = N_Attribute_Reference
11753 and then Attribute_Name (S) = Name_Range)
11754 then
11755 -- A Range attribute will be transformed into N_Range by Resolve
11756
11757 Analyze (S);
11758 Set_Etype (S, T);
11759 R := S;
11760
11761 Process_Range_Expr_In_Decl (R, T, Empty_List);
11762
11763 if not Error_Posted (S)
11764 and then
11765 (Nkind (S) /= N_Range
11766 or else not Covers (T, (Etype (Low_Bound (S))))
11767 or else not Covers (T, (Etype (High_Bound (S)))))
11768 then
11769 if Base_Type (T) /= Any_Type
11770 and then Etype (Low_Bound (S)) /= Any_Type
11771 and then Etype (High_Bound (S)) /= Any_Type
11772 then
11773 Error_Msg_N ("range expected", S);
11774 end if;
11775 end if;
11776
11777 elsif Nkind (S) = N_Subtype_Indication then
11778
11779 -- The parser has verified that this is a discrete indication
11780
11781 Resolve_Discrete_Subtype_Indication (S, T);
11782 R := Range_Expression (Constraint (S));
11783
11784 -- Capture values of bounds and generate temporaries for them if
11785 -- needed, since checks may cause duplication of the expressions
11786 -- which must not be reevaluated.
11787
11788 if Expander_Active then
11789 Force_Evaluation (Low_Bound (R));
11790 Force_Evaluation (High_Bound (R));
11791 end if;
11792
11793 elsif Nkind (S) = N_Discriminant_Association then
11794
11795 -- Syntactically valid in subtype indication
11796
11797 Error_Msg_N ("invalid index constraint", S);
11798 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
11799 return;
11800
11801 -- Subtype_Mark case, no anonymous subtypes to construct
11802
11803 else
11804 Analyze (S);
11805
11806 if Is_Entity_Name (S) then
11807 if not Is_Type (Entity (S)) then
11808 Error_Msg_N ("expect subtype mark for index constraint", S);
11809
11810 elsif Base_Type (Entity (S)) /= Base_Type (T) then
11811 Wrong_Type (S, Base_Type (T));
11812
11813 -- Check error of subtype with predicate in index constraint
11814
11815 else
11816 Bad_Predicated_Subtype_Use
11817 ("subtype& has predicate, not allowed in index constraint",
11818 S, Entity (S));
11819 end if;
11820
11821 return;
11822
11823 else
11824 Error_Msg_N ("invalid index constraint", S);
11825 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
11826 return;
11827 end if;
11828 end if;
11829
11830 Def_Id :=
11831 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
11832
11833 Set_Etype (Def_Id, Base_Type (T));
11834
11835 if Is_Modular_Integer_Type (T) then
11836 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
11837
11838 elsif Is_Integer_Type (T) then
11839 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
11840
11841 else
11842 Set_Ekind (Def_Id, E_Enumeration_Subtype);
11843 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
11844 Set_First_Literal (Def_Id, First_Literal (T));
11845 end if;
11846
11847 Set_Size_Info (Def_Id, (T));
11848 Set_RM_Size (Def_Id, RM_Size (T));
11849 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
11850
11851 Set_Scalar_Range (Def_Id, R);
11852
11853 Set_Etype (S, Def_Id);
11854 Set_Discrete_RM_Size (Def_Id);
11855 end Constrain_Index;
11856
11857 -----------------------
11858 -- Constrain_Integer --
11859 -----------------------
11860
11861 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id) is
11862 T : constant Entity_Id := Entity (Subtype_Mark (S));
11863 C : constant Node_Id := Constraint (S);
11864
11865 begin
11866 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
11867
11868 if Is_Modular_Integer_Type (T) then
11869 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
11870 else
11871 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
11872 end if;
11873
11874 Set_Etype (Def_Id, Base_Type (T));
11875 Set_Size_Info (Def_Id, (T));
11876 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
11877 Set_Discrete_RM_Size (Def_Id);
11878 end Constrain_Integer;
11879
11880 ------------------------------
11881 -- Constrain_Ordinary_Fixed --
11882 ------------------------------
11883
11884 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id) is
11885 T : constant Entity_Id := Entity (Subtype_Mark (S));
11886 C : Node_Id;
11887 D : Node_Id;
11888 Rais : Node_Id;
11889
11890 begin
11891 Set_Ekind (Def_Id, E_Ordinary_Fixed_Point_Subtype);
11892 Set_Etype (Def_Id, Base_Type (T));
11893 Set_Size_Info (Def_Id, (T));
11894 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
11895 Set_Small_Value (Def_Id, Small_Value (T));
11896
11897 -- Process the constraint
11898
11899 C := Constraint (S);
11900
11901 -- Delta constraint present
11902
11903 if Nkind (C) = N_Delta_Constraint then
11904
11905 Check_SPARK_Restriction ("delta constraint is not allowed", S);
11906 Check_Restriction (No_Obsolescent_Features, C);
11907
11908 if Warn_On_Obsolescent_Feature then
11909 Error_Msg_S
11910 ("subtype delta constraint is an " &
11911 "obsolescent feature (RM J.3(7))?");
11912 end if;
11913
11914 D := Delta_Expression (C);
11915 Analyze_And_Resolve (D, Any_Real);
11916 Check_Delta_Expression (D);
11917 Set_Delta_Value (Def_Id, Expr_Value_R (D));
11918
11919 -- Check that delta value is in range. Obviously we can do this
11920 -- at compile time, but it is strictly a runtime check, and of
11921 -- course there is an ACVC test that checks this!
11922
11923 if Delta_Value (Def_Id) < Delta_Value (T) then
11924 Error_Msg_N ("?delta value is too small", D);
11925 Rais :=
11926 Make_Raise_Constraint_Error (Sloc (D),
11927 Reason => CE_Range_Check_Failed);
11928 Insert_Action (Declaration_Node (Def_Id), Rais);
11929 end if;
11930
11931 C := Range_Constraint (C);
11932
11933 -- No delta constraint present
11934
11935 else
11936 Set_Delta_Value (Def_Id, Delta_Value (T));
11937 end if;
11938
11939 -- Range constraint present
11940
11941 if Nkind (C) = N_Range_Constraint then
11942 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
11943
11944 -- No range constraint present
11945
11946 else
11947 pragma Assert (No (C));
11948 Set_Scalar_Range (Def_Id, Scalar_Range (T));
11949
11950 end if;
11951
11952 Set_Discrete_RM_Size (Def_Id);
11953
11954 -- Unconditionally delay the freeze, since we cannot set size
11955 -- information in all cases correctly until the freeze point.
11956
11957 Set_Has_Delayed_Freeze (Def_Id);
11958 end Constrain_Ordinary_Fixed;
11959
11960 -----------------------
11961 -- Contain_Interface --
11962 -----------------------
11963
11964 function Contain_Interface
11965 (Iface : Entity_Id;
11966 Ifaces : Elist_Id) return Boolean
11967 is
11968 Iface_Elmt : Elmt_Id;
11969
11970 begin
11971 if Present (Ifaces) then
11972 Iface_Elmt := First_Elmt (Ifaces);
11973 while Present (Iface_Elmt) loop
11974 if Node (Iface_Elmt) = Iface then
11975 return True;
11976 end if;
11977
11978 Next_Elmt (Iface_Elmt);
11979 end loop;
11980 end if;
11981
11982 return False;
11983 end Contain_Interface;
11984
11985 ---------------------------
11986 -- Convert_Scalar_Bounds --
11987 ---------------------------
11988
11989 procedure Convert_Scalar_Bounds
11990 (N : Node_Id;
11991 Parent_Type : Entity_Id;
11992 Derived_Type : Entity_Id;
11993 Loc : Source_Ptr)
11994 is
11995 Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
11996
11997 Lo : Node_Id;
11998 Hi : Node_Id;
11999 Rng : Node_Id;
12000
12001 begin
12002 -- Defend against previous errors
12003
12004 if No (Scalar_Range (Derived_Type)) then
12005 return;
12006 end if;
12007
12008 Lo := Build_Scalar_Bound
12009 (Type_Low_Bound (Derived_Type),
12010 Parent_Type, Implicit_Base);
12011
12012 Hi := Build_Scalar_Bound
12013 (Type_High_Bound (Derived_Type),
12014 Parent_Type, Implicit_Base);
12015
12016 Rng :=
12017 Make_Range (Loc,
12018 Low_Bound => Lo,
12019 High_Bound => Hi);
12020
12021 Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
12022
12023 Set_Parent (Rng, N);
12024 Set_Scalar_Range (Derived_Type, Rng);
12025
12026 -- Analyze the bounds
12027
12028 Analyze_And_Resolve (Lo, Implicit_Base);
12029 Analyze_And_Resolve (Hi, Implicit_Base);
12030
12031 -- Analyze the range itself, except that we do not analyze it if
12032 -- the bounds are real literals, and we have a fixed-point type.
12033 -- The reason for this is that we delay setting the bounds in this
12034 -- case till we know the final Small and Size values (see circuit
12035 -- in Freeze.Freeze_Fixed_Point_Type for further details).
12036
12037 if Is_Fixed_Point_Type (Parent_Type)
12038 and then Nkind (Lo) = N_Real_Literal
12039 and then Nkind (Hi) = N_Real_Literal
12040 then
12041 return;
12042
12043 -- Here we do the analysis of the range
12044
12045 -- Note: we do this manually, since if we do a normal Analyze and
12046 -- Resolve call, there are problems with the conversions used for
12047 -- the derived type range.
12048
12049 else
12050 Set_Etype (Rng, Implicit_Base);
12051 Set_Analyzed (Rng, True);
12052 end if;
12053 end Convert_Scalar_Bounds;
12054
12055 -------------------
12056 -- Copy_And_Swap --
12057 -------------------
12058
12059 procedure Copy_And_Swap (Priv, Full : Entity_Id) is
12060 begin
12061 -- Initialize new full declaration entity by copying the pertinent
12062 -- fields of the corresponding private declaration entity.
12063
12064 -- We temporarily set Ekind to a value appropriate for a type to
12065 -- avoid assert failures in Einfo from checking for setting type
12066 -- attributes on something that is not a type. Ekind (Priv) is an
12067 -- appropriate choice, since it allowed the attributes to be set
12068 -- in the first place. This Ekind value will be modified later.
12069
12070 Set_Ekind (Full, Ekind (Priv));
12071
12072 -- Also set Etype temporarily to Any_Type, again, in the absence
12073 -- of errors, it will be properly reset, and if there are errors,
12074 -- then we want a value of Any_Type to remain.
12075
12076 Set_Etype (Full, Any_Type);
12077
12078 -- Now start copying attributes
12079
12080 Set_Has_Discriminants (Full, Has_Discriminants (Priv));
12081
12082 if Has_Discriminants (Full) then
12083 Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
12084 Set_Stored_Constraint (Full, Stored_Constraint (Priv));
12085 end if;
12086
12087 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
12088 Set_Homonym (Full, Homonym (Priv));
12089 Set_Is_Immediately_Visible (Full, Is_Immediately_Visible (Priv));
12090 Set_Is_Public (Full, Is_Public (Priv));
12091 Set_Is_Pure (Full, Is_Pure (Priv));
12092 Set_Is_Tagged_Type (Full, Is_Tagged_Type (Priv));
12093 Set_Has_Pragma_Unmodified (Full, Has_Pragma_Unmodified (Priv));
12094 Set_Has_Pragma_Unreferenced (Full, Has_Pragma_Unreferenced (Priv));
12095 Set_Has_Pragma_Unreferenced_Objects
12096 (Full, Has_Pragma_Unreferenced_Objects
12097 (Priv));
12098
12099 Conditional_Delay (Full, Priv);
12100
12101 if Is_Tagged_Type (Full) then
12102 Set_Direct_Primitive_Operations (Full,
12103 Direct_Primitive_Operations (Priv));
12104
12105 if Is_Base_Type (Priv) then
12106 Set_Class_Wide_Type (Full, Class_Wide_Type (Priv));
12107 end if;
12108 end if;
12109
12110 Set_Is_Volatile (Full, Is_Volatile (Priv));
12111 Set_Treat_As_Volatile (Full, Treat_As_Volatile (Priv));
12112 Set_Scope (Full, Scope (Priv));
12113 Set_Next_Entity (Full, Next_Entity (Priv));
12114 Set_First_Entity (Full, First_Entity (Priv));
12115 Set_Last_Entity (Full, Last_Entity (Priv));
12116
12117 -- If access types have been recorded for later handling, keep them in
12118 -- the full view so that they get handled when the full view freeze
12119 -- node is expanded.
12120
12121 if Present (Freeze_Node (Priv))
12122 and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
12123 then
12124 Ensure_Freeze_Node (Full);
12125 Set_Access_Types_To_Process
12126 (Freeze_Node (Full),
12127 Access_Types_To_Process (Freeze_Node (Priv)));
12128 end if;
12129
12130 -- Swap the two entities. Now Private is the full type entity and Full
12131 -- is the private one. They will be swapped back at the end of the
12132 -- private part. This swapping ensures that the entity that is visible
12133 -- in the private part is the full declaration.
12134
12135 Exchange_Entities (Priv, Full);
12136 Append_Entity (Full, Scope (Full));
12137 end Copy_And_Swap;
12138
12139 -------------------------------------
12140 -- Copy_Array_Base_Type_Attributes --
12141 -------------------------------------
12142
12143 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
12144 begin
12145 Set_Component_Alignment (T1, Component_Alignment (T2));
12146 Set_Component_Type (T1, Component_Type (T2));
12147 Set_Component_Size (T1, Component_Size (T2));
12148 Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
12149 Set_Finalize_Storage_Only (T1, Finalize_Storage_Only (T2));
12150 Set_Has_Non_Standard_Rep (T1, Has_Non_Standard_Rep (T2));
12151 Set_Has_Task (T1, Has_Task (T2));
12152 Set_Is_Packed (T1, Is_Packed (T2));
12153 Set_Has_Aliased_Components (T1, Has_Aliased_Components (T2));
12154 Set_Has_Atomic_Components (T1, Has_Atomic_Components (T2));
12155 Set_Has_Volatile_Components (T1, Has_Volatile_Components (T2));
12156 end Copy_Array_Base_Type_Attributes;
12157
12158 -----------------------------------
12159 -- Copy_Array_Subtype_Attributes --
12160 -----------------------------------
12161
12162 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
12163 begin
12164 Set_Size_Info (T1, T2);
12165
12166 Set_First_Index (T1, First_Index (T2));
12167 Set_Is_Aliased (T1, Is_Aliased (T2));
12168 Set_Is_Atomic (T1, Is_Atomic (T2));
12169 Set_Is_Volatile (T1, Is_Volatile (T2));
12170 Set_Treat_As_Volatile (T1, Treat_As_Volatile (T2));
12171 Set_Is_Constrained (T1, Is_Constrained (T2));
12172 Set_Depends_On_Private (T1, Has_Private_Component (T2));
12173 Set_First_Rep_Item (T1, First_Rep_Item (T2));
12174 Set_Convention (T1, Convention (T2));
12175 Set_Is_Limited_Composite (T1, Is_Limited_Composite (T2));
12176 Set_Is_Private_Composite (T1, Is_Private_Composite (T2));
12177 Set_Packed_Array_Type (T1, Packed_Array_Type (T2));
12178 end Copy_Array_Subtype_Attributes;
12179
12180 -----------------------------------
12181 -- Create_Constrained_Components --
12182 -----------------------------------
12183
12184 procedure Create_Constrained_Components
12185 (Subt : Entity_Id;
12186 Decl_Node : Node_Id;
12187 Typ : Entity_Id;
12188 Constraints : Elist_Id)
12189 is
12190 Loc : constant Source_Ptr := Sloc (Subt);
12191 Comp_List : constant Elist_Id := New_Elmt_List;
12192 Parent_Type : constant Entity_Id := Etype (Typ);
12193 Assoc_List : constant List_Id := New_List;
12194 Discr_Val : Elmt_Id;
12195 Errors : Boolean;
12196 New_C : Entity_Id;
12197 Old_C : Entity_Id;
12198 Is_Static : Boolean := True;
12199
12200 procedure Collect_Fixed_Components (Typ : Entity_Id);
12201 -- Collect parent type components that do not appear in a variant part
12202
12203 procedure Create_All_Components;
12204 -- Iterate over Comp_List to create the components of the subtype
12205
12206 function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
12207 -- Creates a new component from Old_Compon, copying all the fields from
12208 -- it, including its Etype, inserts the new component in the Subt entity
12209 -- chain and returns the new component.
12210
12211 function Is_Variant_Record (T : Entity_Id) return Boolean;
12212 -- If true, and discriminants are static, collect only components from
12213 -- variants selected by discriminant values.
12214
12215 ------------------------------
12216 -- Collect_Fixed_Components --
12217 ------------------------------
12218
12219 procedure Collect_Fixed_Components (Typ : Entity_Id) is
12220 begin
12221 -- Build association list for discriminants, and find components of the
12222 -- variant part selected by the values of the discriminants.
12223
12224 Old_C := First_Discriminant (Typ);
12225 Discr_Val := First_Elmt (Constraints);
12226 while Present (Old_C) loop
12227 Append_To (Assoc_List,
12228 Make_Component_Association (Loc,
12229 Choices => New_List (New_Occurrence_Of (Old_C, Loc)),
12230 Expression => New_Copy (Node (Discr_Val))));
12231
12232 Next_Elmt (Discr_Val);
12233 Next_Discriminant (Old_C);
12234 end loop;
12235
12236 -- The tag and the possible parent component are unconditionally in
12237 -- the subtype.
12238
12239 if Is_Tagged_Type (Typ)
12240 or else Has_Controlled_Component (Typ)
12241 then
12242 Old_C := First_Component (Typ);
12243 while Present (Old_C) loop
12244 if Chars ((Old_C)) = Name_uTag
12245 or else Chars ((Old_C)) = Name_uParent
12246 then
12247 Append_Elmt (Old_C, Comp_List);
12248 end if;
12249
12250 Next_Component (Old_C);
12251 end loop;
12252 end if;
12253 end Collect_Fixed_Components;
12254
12255 ---------------------------
12256 -- Create_All_Components --
12257 ---------------------------
12258
12259 procedure Create_All_Components is
12260 Comp : Elmt_Id;
12261
12262 begin
12263 Comp := First_Elmt (Comp_List);
12264 while Present (Comp) loop
12265 Old_C := Node (Comp);
12266 New_C := Create_Component (Old_C);
12267
12268 Set_Etype
12269 (New_C,
12270 Constrain_Component_Type
12271 (Old_C, Subt, Decl_Node, Typ, Constraints));
12272 Set_Is_Public (New_C, Is_Public (Subt));
12273
12274 Next_Elmt (Comp);
12275 end loop;
12276 end Create_All_Components;
12277
12278 ----------------------
12279 -- Create_Component --
12280 ----------------------
12281
12282 function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
12283 New_Compon : constant Entity_Id := New_Copy (Old_Compon);
12284
12285 begin
12286 if Ekind (Old_Compon) = E_Discriminant
12287 and then Is_Completely_Hidden (Old_Compon)
12288 then
12289 -- This is a shadow discriminant created for a discriminant of
12290 -- the parent type, which needs to be present in the subtype.
12291 -- Give the shadow discriminant an internal name that cannot
12292 -- conflict with that of visible components.
12293
12294 Set_Chars (New_Compon, New_Internal_Name ('C'));
12295 end if;
12296
12297 -- Set the parent so we have a proper link for freezing etc. This is
12298 -- not a real parent pointer, since of course our parent does not own
12299 -- up to us and reference us, we are an illegitimate child of the
12300 -- original parent!
12301
12302 Set_Parent (New_Compon, Parent (Old_Compon));
12303
12304 -- If the old component's Esize was already determined and is a
12305 -- static value, then the new component simply inherits it. Otherwise
12306 -- the old component's size may require run-time determination, but
12307 -- the new component's size still might be statically determinable
12308 -- (if, for example it has a static constraint). In that case we want
12309 -- Layout_Type to recompute the component's size, so we reset its
12310 -- size and positional fields.
12311
12312 if Frontend_Layout_On_Target
12313 and then not Known_Static_Esize (Old_Compon)
12314 then
12315 Set_Esize (New_Compon, Uint_0);
12316 Init_Normalized_First_Bit (New_Compon);
12317 Init_Normalized_Position (New_Compon);
12318 Init_Normalized_Position_Max (New_Compon);
12319 end if;
12320
12321 -- We do not want this node marked as Comes_From_Source, since
12322 -- otherwise it would get first class status and a separate cross-
12323 -- reference line would be generated. Illegitimate children do not
12324 -- rate such recognition.
12325
12326 Set_Comes_From_Source (New_Compon, False);
12327
12328 -- But it is a real entity, and a birth certificate must be properly
12329 -- registered by entering it into the entity list.
12330
12331 Enter_Name (New_Compon);
12332
12333 return New_Compon;
12334 end Create_Component;
12335
12336 -----------------------
12337 -- Is_Variant_Record --
12338 -----------------------
12339
12340 function Is_Variant_Record (T : Entity_Id) return Boolean is
12341 begin
12342 return Nkind (Parent (T)) = N_Full_Type_Declaration
12343 and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
12344 and then Present (Component_List (Type_Definition (Parent (T))))
12345 and then
12346 Present
12347 (Variant_Part (Component_List (Type_Definition (Parent (T)))));
12348 end Is_Variant_Record;
12349
12350 -- Start of processing for Create_Constrained_Components
12351
12352 begin
12353 pragma Assert (Subt /= Base_Type (Subt));
12354 pragma Assert (Typ = Base_Type (Typ));
12355
12356 Set_First_Entity (Subt, Empty);
12357 Set_Last_Entity (Subt, Empty);
12358
12359 -- Check whether constraint is fully static, in which case we can
12360 -- optimize the list of components.
12361
12362 Discr_Val := First_Elmt (Constraints);
12363 while Present (Discr_Val) loop
12364 if not Is_OK_Static_Expression (Node (Discr_Val)) then
12365 Is_Static := False;
12366 exit;
12367 end if;
12368
12369 Next_Elmt (Discr_Val);
12370 end loop;
12371
12372 Set_Has_Static_Discriminants (Subt, Is_Static);
12373
12374 Push_Scope (Subt);
12375
12376 -- Inherit the discriminants of the parent type
12377
12378 Add_Discriminants : declare
12379 Num_Disc : Int;
12380 Num_Gird : Int;
12381
12382 begin
12383 Num_Disc := 0;
12384 Old_C := First_Discriminant (Typ);
12385
12386 while Present (Old_C) loop
12387 Num_Disc := Num_Disc + 1;
12388 New_C := Create_Component (Old_C);
12389 Set_Is_Public (New_C, Is_Public (Subt));
12390 Next_Discriminant (Old_C);
12391 end loop;
12392
12393 -- For an untagged derived subtype, the number of discriminants may
12394 -- be smaller than the number of inherited discriminants, because
12395 -- several of them may be renamed by a single new discriminant or
12396 -- constrained. In this case, add the hidden discriminants back into
12397 -- the subtype, because they need to be present if the optimizer of
12398 -- the GCC 4.x back-end decides to break apart assignments between
12399 -- objects using the parent view into member-wise assignments.
12400
12401 Num_Gird := 0;
12402
12403 if Is_Derived_Type (Typ)
12404 and then not Is_Tagged_Type (Typ)
12405 then
12406 Old_C := First_Stored_Discriminant (Typ);
12407
12408 while Present (Old_C) loop
12409 Num_Gird := Num_Gird + 1;
12410 Next_Stored_Discriminant (Old_C);
12411 end loop;
12412 end if;
12413
12414 if Num_Gird > Num_Disc then
12415
12416 -- Find out multiple uses of new discriminants, and add hidden
12417 -- components for the extra renamed discriminants. We recognize
12418 -- multiple uses through the Corresponding_Discriminant of a
12419 -- new discriminant: if it constrains several old discriminants,
12420 -- this field points to the last one in the parent type. The
12421 -- stored discriminants of the derived type have the same name
12422 -- as those of the parent.
12423
12424 declare
12425 Constr : Elmt_Id;
12426 New_Discr : Entity_Id;
12427 Old_Discr : Entity_Id;
12428
12429 begin
12430 Constr := First_Elmt (Stored_Constraint (Typ));
12431 Old_Discr := First_Stored_Discriminant (Typ);
12432 while Present (Constr) loop
12433 if Is_Entity_Name (Node (Constr))
12434 and then Ekind (Entity (Node (Constr))) = E_Discriminant
12435 then
12436 New_Discr := Entity (Node (Constr));
12437
12438 if Chars (Corresponding_Discriminant (New_Discr)) /=
12439 Chars (Old_Discr)
12440 then
12441 -- The new discriminant has been used to rename a
12442 -- subsequent old discriminant. Introduce a shadow
12443 -- component for the current old discriminant.
12444
12445 New_C := Create_Component (Old_Discr);
12446 Set_Original_Record_Component (New_C, Old_Discr);
12447 end if;
12448
12449 else
12450 -- The constraint has eliminated the old discriminant.
12451 -- Introduce a shadow component.
12452
12453 New_C := Create_Component (Old_Discr);
12454 Set_Original_Record_Component (New_C, Old_Discr);
12455 end if;
12456
12457 Next_Elmt (Constr);
12458 Next_Stored_Discriminant (Old_Discr);
12459 end loop;
12460 end;
12461 end if;
12462 end Add_Discriminants;
12463
12464 if Is_Static
12465 and then Is_Variant_Record (Typ)
12466 then
12467 Collect_Fixed_Components (Typ);
12468
12469 Gather_Components (
12470 Typ,
12471 Component_List (Type_Definition (Parent (Typ))),
12472 Governed_By => Assoc_List,
12473 Into => Comp_List,
12474 Report_Errors => Errors);
12475 pragma Assert (not Errors);
12476
12477 Create_All_Components;
12478
12479 -- If the subtype declaration is created for a tagged type derivation
12480 -- with constraints, we retrieve the record definition of the parent
12481 -- type to select the components of the proper variant.
12482
12483 elsif Is_Static
12484 and then Is_Tagged_Type (Typ)
12485 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
12486 and then
12487 Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
12488 and then Is_Variant_Record (Parent_Type)
12489 then
12490 Collect_Fixed_Components (Typ);
12491
12492 Gather_Components (
12493 Typ,
12494 Component_List (Type_Definition (Parent (Parent_Type))),
12495 Governed_By => Assoc_List,
12496 Into => Comp_List,
12497 Report_Errors => Errors);
12498 pragma Assert (not Errors);
12499
12500 -- If the tagged derivation has a type extension, collect all the
12501 -- new components therein.
12502
12503 if Present
12504 (Record_Extension_Part (Type_Definition (Parent (Typ))))
12505 then
12506 Old_C := First_Component (Typ);
12507 while Present (Old_C) loop
12508 if Original_Record_Component (Old_C) = Old_C
12509 and then Chars (Old_C) /= Name_uTag
12510 and then Chars (Old_C) /= Name_uParent
12511 then
12512 Append_Elmt (Old_C, Comp_List);
12513 end if;
12514
12515 Next_Component (Old_C);
12516 end loop;
12517 end if;
12518
12519 Create_All_Components;
12520
12521 else
12522 -- If discriminants are not static, or if this is a multi-level type
12523 -- extension, we have to include all components of the parent type.
12524
12525 Old_C := First_Component (Typ);
12526 while Present (Old_C) loop
12527 New_C := Create_Component (Old_C);
12528
12529 Set_Etype
12530 (New_C,
12531 Constrain_Component_Type
12532 (Old_C, Subt, Decl_Node, Typ, Constraints));
12533 Set_Is_Public (New_C, Is_Public (Subt));
12534
12535 Next_Component (Old_C);
12536 end loop;
12537 end if;
12538
12539 End_Scope;
12540 end Create_Constrained_Components;
12541
12542 ------------------------------------------
12543 -- Decimal_Fixed_Point_Type_Declaration --
12544 ------------------------------------------
12545
12546 procedure Decimal_Fixed_Point_Type_Declaration
12547 (T : Entity_Id;
12548 Def : Node_Id)
12549 is
12550 Loc : constant Source_Ptr := Sloc (Def);
12551 Digs_Expr : constant Node_Id := Digits_Expression (Def);
12552 Delta_Expr : constant Node_Id := Delta_Expression (Def);
12553 Implicit_Base : Entity_Id;
12554 Digs_Val : Uint;
12555 Delta_Val : Ureal;
12556 Scale_Val : Uint;
12557 Bound_Val : Ureal;
12558
12559 begin
12560 Check_SPARK_Restriction
12561 ("decimal fixed point type is not allowed", Def);
12562 Check_Restriction (No_Fixed_Point, Def);
12563
12564 -- Create implicit base type
12565
12566 Implicit_Base :=
12567 Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
12568 Set_Etype (Implicit_Base, Implicit_Base);
12569
12570 -- Analyze and process delta expression
12571
12572 Analyze_And_Resolve (Delta_Expr, Universal_Real);
12573
12574 Check_Delta_Expression (Delta_Expr);
12575 Delta_Val := Expr_Value_R (Delta_Expr);
12576
12577 -- Check delta is power of 10, and determine scale value from it
12578
12579 declare
12580 Val : Ureal;
12581
12582 begin
12583 Scale_Val := Uint_0;
12584 Val := Delta_Val;
12585
12586 if Val < Ureal_1 then
12587 while Val < Ureal_1 loop
12588 Val := Val * Ureal_10;
12589 Scale_Val := Scale_Val + 1;
12590 end loop;
12591
12592 if Scale_Val > 18 then
12593 Error_Msg_N ("scale exceeds maximum value of 18", Def);
12594 Scale_Val := UI_From_Int (+18);
12595 end if;
12596
12597 else
12598 while Val > Ureal_1 loop
12599 Val := Val / Ureal_10;
12600 Scale_Val := Scale_Val - 1;
12601 end loop;
12602
12603 if Scale_Val < -18 then
12604 Error_Msg_N ("scale is less than minimum value of -18", Def);
12605 Scale_Val := UI_From_Int (-18);
12606 end if;
12607 end if;
12608
12609 if Val /= Ureal_1 then
12610 Error_Msg_N ("delta expression must be a power of 10", Def);
12611 Delta_Val := Ureal_10 ** (-Scale_Val);
12612 end if;
12613 end;
12614
12615 -- Set delta, scale and small (small = delta for decimal type)
12616
12617 Set_Delta_Value (Implicit_Base, Delta_Val);
12618 Set_Scale_Value (Implicit_Base, Scale_Val);
12619 Set_Small_Value (Implicit_Base, Delta_Val);
12620
12621 -- Analyze and process digits expression
12622
12623 Analyze_And_Resolve (Digs_Expr, Any_Integer);
12624 Check_Digits_Expression (Digs_Expr);
12625 Digs_Val := Expr_Value (Digs_Expr);
12626
12627 if Digs_Val > 18 then
12628 Digs_Val := UI_From_Int (+18);
12629 Error_Msg_N ("digits value out of range, maximum is 18", Digs_Expr);
12630 end if;
12631
12632 Set_Digits_Value (Implicit_Base, Digs_Val);
12633 Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
12634
12635 -- Set range of base type from digits value for now. This will be
12636 -- expanded to represent the true underlying base range by Freeze.
12637
12638 Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
12639
12640 -- Note: We leave size as zero for now, size will be set at freeze
12641 -- time. We have to do this for ordinary fixed-point, because the size
12642 -- depends on the specified small, and we might as well do the same for
12643 -- decimal fixed-point.
12644
12645 pragma Assert (Esize (Implicit_Base) = Uint_0);
12646
12647 -- If there are bounds given in the declaration use them as the
12648 -- bounds of the first named subtype.
12649
12650 if Present (Real_Range_Specification (Def)) then
12651 declare
12652 RRS : constant Node_Id := Real_Range_Specification (Def);
12653 Low : constant Node_Id := Low_Bound (RRS);
12654 High : constant Node_Id := High_Bound (RRS);
12655 Low_Val : Ureal;
12656 High_Val : Ureal;
12657
12658 begin
12659 Analyze_And_Resolve (Low, Any_Real);
12660 Analyze_And_Resolve (High, Any_Real);
12661 Check_Real_Bound (Low);
12662 Check_Real_Bound (High);
12663 Low_Val := Expr_Value_R (Low);
12664 High_Val := Expr_Value_R (High);
12665
12666 if Low_Val < (-Bound_Val) then
12667 Error_Msg_N
12668 ("range low bound too small for digits value", Low);
12669 Low_Val := -Bound_Val;
12670 end if;
12671
12672 if High_Val > Bound_Val then
12673 Error_Msg_N
12674 ("range high bound too large for digits value", High);
12675 High_Val := Bound_Val;
12676 end if;
12677
12678 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
12679 end;
12680
12681 -- If no explicit range, use range that corresponds to given
12682 -- digits value. This will end up as the final range for the
12683 -- first subtype.
12684
12685 else
12686 Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
12687 end if;
12688
12689 -- Complete entity for first subtype
12690
12691 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
12692 Set_Etype (T, Implicit_Base);
12693 Set_Size_Info (T, Implicit_Base);
12694 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
12695 Set_Digits_Value (T, Digs_Val);
12696 Set_Delta_Value (T, Delta_Val);
12697 Set_Small_Value (T, Delta_Val);
12698 Set_Scale_Value (T, Scale_Val);
12699 Set_Is_Constrained (T);
12700 end Decimal_Fixed_Point_Type_Declaration;
12701
12702 -----------------------------------
12703 -- Derive_Progenitor_Subprograms --
12704 -----------------------------------
12705
12706 procedure Derive_Progenitor_Subprograms
12707 (Parent_Type : Entity_Id;
12708 Tagged_Type : Entity_Id)
12709 is
12710 E : Entity_Id;
12711 Elmt : Elmt_Id;
12712 Iface : Entity_Id;
12713 Iface_Elmt : Elmt_Id;
12714 Iface_Subp : Entity_Id;
12715 New_Subp : Entity_Id := Empty;
12716 Prim_Elmt : Elmt_Id;
12717 Subp : Entity_Id;
12718 Typ : Entity_Id;
12719
12720 begin
12721 pragma Assert (Ada_Version >= Ada_2005
12722 and then Is_Record_Type (Tagged_Type)
12723 and then Is_Tagged_Type (Tagged_Type)
12724 and then Has_Interfaces (Tagged_Type));
12725
12726 -- Step 1: Transfer to the full-view primitives associated with the
12727 -- partial-view that cover interface primitives. Conceptually this
12728 -- work should be done later by Process_Full_View; done here to
12729 -- simplify its implementation at later stages. It can be safely
12730 -- done here because interfaces must be visible in the partial and
12731 -- private view (RM 7.3(7.3/2)).
12732
12733 -- Small optimization: This work is only required if the parent is
12734 -- abstract. If the tagged type is not abstract, it cannot have
12735 -- abstract primitives (the only entities in the list of primitives of
12736 -- non-abstract tagged types that can reference abstract primitives
12737 -- through its Alias attribute are the internal entities that have
12738 -- attribute Interface_Alias, and these entities are generated later
12739 -- by Add_Internal_Interface_Entities).
12740
12741 if In_Private_Part (Current_Scope)
12742 and then Is_Abstract_Type (Parent_Type)
12743 then
12744 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
12745 while Present (Elmt) loop
12746 Subp := Node (Elmt);
12747
12748 -- At this stage it is not possible to have entities in the list
12749 -- of primitives that have attribute Interface_Alias
12750
12751 pragma Assert (No (Interface_Alias (Subp)));
12752
12753 Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
12754
12755 if Is_Interface (Typ) then
12756 E := Find_Primitive_Covering_Interface
12757 (Tagged_Type => Tagged_Type,
12758 Iface_Prim => Subp);
12759
12760 if Present (E)
12761 and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
12762 then
12763 Replace_Elmt (Elmt, E);
12764 Remove_Homonym (Subp);
12765 end if;
12766 end if;
12767
12768 Next_Elmt (Elmt);
12769 end loop;
12770 end if;
12771
12772 -- Step 2: Add primitives of progenitors that are not implemented by
12773 -- parents of Tagged_Type
12774
12775 if Present (Interfaces (Base_Type (Tagged_Type))) then
12776 Iface_Elmt := First_Elmt (Interfaces (Base_Type (Tagged_Type)));
12777 while Present (Iface_Elmt) loop
12778 Iface := Node (Iface_Elmt);
12779
12780 Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
12781 while Present (Prim_Elmt) loop
12782 Iface_Subp := Node (Prim_Elmt);
12783
12784 -- Exclude derivation of predefined primitives except those
12785 -- that come from source. Required to catch declarations of
12786 -- equality operators of interfaces. For example:
12787
12788 -- type Iface is interface;
12789 -- function "=" (Left, Right : Iface) return Boolean;
12790
12791 if not Is_Predefined_Dispatching_Operation (Iface_Subp)
12792 or else Comes_From_Source (Iface_Subp)
12793 then
12794 E := Find_Primitive_Covering_Interface
12795 (Tagged_Type => Tagged_Type,
12796 Iface_Prim => Iface_Subp);
12797
12798 -- If not found we derive a new primitive leaving its alias
12799 -- attribute referencing the interface primitive
12800
12801 if No (E) then
12802 Derive_Subprogram
12803 (New_Subp, Iface_Subp, Tagged_Type, Iface);
12804
12805 -- Ada 2012 (AI05-0197): If the covering primitive's name
12806 -- differs from the name of the interface primitive then it
12807 -- is a private primitive inherited from a parent type. In
12808 -- such case, given that Tagged_Type covers the interface,
12809 -- the inherited private primitive becomes visible. For such
12810 -- purpose we add a new entity that renames the inherited
12811 -- private primitive.
12812
12813 elsif Chars (E) /= Chars (Iface_Subp) then
12814 pragma Assert (Has_Suffix (E, 'P'));
12815 Derive_Subprogram
12816 (New_Subp, Iface_Subp, Tagged_Type, Iface);
12817 Set_Alias (New_Subp, E);
12818 Set_Is_Abstract_Subprogram (New_Subp,
12819 Is_Abstract_Subprogram (E));
12820
12821 -- Propagate to the full view interface entities associated
12822 -- with the partial view
12823
12824 elsif In_Private_Part (Current_Scope)
12825 and then Present (Alias (E))
12826 and then Alias (E) = Iface_Subp
12827 and then
12828 List_Containing (Parent (E)) /=
12829 Private_Declarations
12830 (Specification
12831 (Unit_Declaration_Node (Current_Scope)))
12832 then
12833 Append_Elmt (E, Primitive_Operations (Tagged_Type));
12834 end if;
12835 end if;
12836
12837 Next_Elmt (Prim_Elmt);
12838 end loop;
12839
12840 Next_Elmt (Iface_Elmt);
12841 end loop;
12842 end if;
12843 end Derive_Progenitor_Subprograms;
12844
12845 -----------------------
12846 -- Derive_Subprogram --
12847 -----------------------
12848
12849 procedure Derive_Subprogram
12850 (New_Subp : in out Entity_Id;
12851 Parent_Subp : Entity_Id;
12852 Derived_Type : Entity_Id;
12853 Parent_Type : Entity_Id;
12854 Actual_Subp : Entity_Id := Empty)
12855 is
12856 Formal : Entity_Id;
12857 -- Formal parameter of parent primitive operation
12858
12859 Formal_Of_Actual : Entity_Id;
12860 -- Formal parameter of actual operation, when the derivation is to
12861 -- create a renaming for a primitive operation of an actual in an
12862 -- instantiation.
12863
12864 New_Formal : Entity_Id;
12865 -- Formal of inherited operation
12866
12867 Visible_Subp : Entity_Id := Parent_Subp;
12868
12869 function Is_Private_Overriding return Boolean;
12870 -- If Subp is a private overriding of a visible operation, the inherited
12871 -- operation derives from the overridden op (even though its body is the
12872 -- overriding one) and the inherited operation is visible now. See
12873 -- sem_disp to see the full details of the handling of the overridden
12874 -- subprogram, which is removed from the list of primitive operations of
12875 -- the type. The overridden subprogram is saved locally in Visible_Subp,
12876 -- and used to diagnose abstract operations that need overriding in the
12877 -- derived type.
12878
12879 procedure Replace_Type (Id, New_Id : Entity_Id);
12880 -- When the type is an anonymous access type, create a new access type
12881 -- designating the derived type.
12882
12883 procedure Set_Derived_Name;
12884 -- This procedure sets the appropriate Chars name for New_Subp. This
12885 -- is normally just a copy of the parent name. An exception arises for
12886 -- type support subprograms, where the name is changed to reflect the
12887 -- name of the derived type, e.g. if type foo is derived from type bar,
12888 -- then a procedure barDA is derived with a name fooDA.
12889
12890 ---------------------------
12891 -- Is_Private_Overriding --
12892 ---------------------------
12893
12894 function Is_Private_Overriding return Boolean is
12895 Prev : Entity_Id;
12896
12897 begin
12898 -- If the parent is not a dispatching operation there is no
12899 -- need to investigate overridings
12900
12901 if not Is_Dispatching_Operation (Parent_Subp) then
12902 return False;
12903 end if;
12904
12905 -- The visible operation that is overridden is a homonym of the
12906 -- parent subprogram. We scan the homonym chain to find the one
12907 -- whose alias is the subprogram we are deriving.
12908
12909 Prev := Current_Entity (Parent_Subp);
12910 while Present (Prev) loop
12911 if Ekind (Prev) = Ekind (Parent_Subp)
12912 and then Alias (Prev) = Parent_Subp
12913 and then Scope (Parent_Subp) = Scope (Prev)
12914 and then not Is_Hidden (Prev)
12915 then
12916 Visible_Subp := Prev;
12917 return True;
12918 end if;
12919
12920 Prev := Homonym (Prev);
12921 end loop;
12922
12923 return False;
12924 end Is_Private_Overriding;
12925
12926 ------------------
12927 -- Replace_Type --
12928 ------------------
12929
12930 procedure Replace_Type (Id, New_Id : Entity_Id) is
12931 Acc_Type : Entity_Id;
12932 Par : constant Node_Id := Parent (Derived_Type);
12933
12934 begin
12935 -- When the type is an anonymous access type, create a new access
12936 -- type designating the derived type. This itype must be elaborated
12937 -- at the point of the derivation, not on subsequent calls that may
12938 -- be out of the proper scope for Gigi, so we insert a reference to
12939 -- it after the derivation.
12940
12941 if Ekind (Etype (Id)) = E_Anonymous_Access_Type then
12942 declare
12943 Desig_Typ : Entity_Id := Designated_Type (Etype (Id));
12944
12945 begin
12946 if Ekind (Desig_Typ) = E_Record_Type_With_Private
12947 and then Present (Full_View (Desig_Typ))
12948 and then not Is_Private_Type (Parent_Type)
12949 then
12950 Desig_Typ := Full_View (Desig_Typ);
12951 end if;
12952
12953 if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
12954
12955 -- Ada 2005 (AI-251): Handle also derivations of abstract
12956 -- interface primitives.
12957
12958 or else (Is_Interface (Desig_Typ)
12959 and then not Is_Class_Wide_Type (Desig_Typ))
12960 then
12961 Acc_Type := New_Copy (Etype (Id));
12962 Set_Etype (Acc_Type, Acc_Type);
12963 Set_Scope (Acc_Type, New_Subp);
12964
12965 -- Compute size of anonymous access type
12966
12967 if Is_Array_Type (Desig_Typ)
12968 and then not Is_Constrained (Desig_Typ)
12969 then
12970 Init_Size (Acc_Type, 2 * System_Address_Size);
12971 else
12972 Init_Size (Acc_Type, System_Address_Size);
12973 end if;
12974
12975 Init_Alignment (Acc_Type);
12976 Set_Directly_Designated_Type (Acc_Type, Derived_Type);
12977
12978 Set_Etype (New_Id, Acc_Type);
12979 Set_Scope (New_Id, New_Subp);
12980
12981 -- Create a reference to it
12982 Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
12983
12984 else
12985 Set_Etype (New_Id, Etype (Id));
12986 end if;
12987 end;
12988
12989 elsif Base_Type (Etype (Id)) = Base_Type (Parent_Type)
12990 or else
12991 (Ekind (Etype (Id)) = E_Record_Type_With_Private
12992 and then Present (Full_View (Etype (Id)))
12993 and then
12994 Base_Type (Full_View (Etype (Id))) = Base_Type (Parent_Type))
12995 then
12996 -- Constraint checks on formals are generated during expansion,
12997 -- based on the signature of the original subprogram. The bounds
12998 -- of the derived type are not relevant, and thus we can use
12999 -- the base type for the formals. However, the return type may be
13000 -- used in a context that requires that the proper static bounds
13001 -- be used (a case statement, for example) and for those cases
13002 -- we must use the derived type (first subtype), not its base.
13003
13004 -- If the derived_type_definition has no constraints, we know that
13005 -- the derived type has the same constraints as the first subtype
13006 -- of the parent, and we can also use it rather than its base,
13007 -- which can lead to more efficient code.
13008
13009 if Etype (Id) = Parent_Type then
13010 if Is_Scalar_Type (Parent_Type)
13011 and then
13012 Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
13013 then
13014 Set_Etype (New_Id, Derived_Type);
13015
13016 elsif Nkind (Par) = N_Full_Type_Declaration
13017 and then
13018 Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
13019 and then
13020 Is_Entity_Name
13021 (Subtype_Indication (Type_Definition (Par)))
13022 then
13023 Set_Etype (New_Id, Derived_Type);
13024
13025 else
13026 Set_Etype (New_Id, Base_Type (Derived_Type));
13027 end if;
13028
13029 else
13030 Set_Etype (New_Id, Base_Type (Derived_Type));
13031 end if;
13032
13033 else
13034 Set_Etype (New_Id, Etype (Id));
13035 end if;
13036 end Replace_Type;
13037
13038 ----------------------
13039 -- Set_Derived_Name --
13040 ----------------------
13041
13042 procedure Set_Derived_Name is
13043 Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
13044 begin
13045 if Nm = TSS_Null then
13046 Set_Chars (New_Subp, Chars (Parent_Subp));
13047 else
13048 Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
13049 end if;
13050 end Set_Derived_Name;
13051
13052 -- Start of processing for Derive_Subprogram
13053
13054 begin
13055 New_Subp :=
13056 New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
13057 Set_Ekind (New_Subp, Ekind (Parent_Subp));
13058 Set_Contract (New_Subp, Make_Contract (Sloc (New_Subp)));
13059
13060 -- Check whether the inherited subprogram is a private operation that
13061 -- should be inherited but not yet made visible. Such subprograms can
13062 -- become visible at a later point (e.g., the private part of a public
13063 -- child unit) via Declare_Inherited_Private_Subprograms. If the
13064 -- following predicate is true, then this is not such a private
13065 -- operation and the subprogram simply inherits the name of the parent
13066 -- subprogram. Note the special check for the names of controlled
13067 -- operations, which are currently exempted from being inherited with
13068 -- a hidden name because they must be findable for generation of
13069 -- implicit run-time calls.
13070
13071 if not Is_Hidden (Parent_Subp)
13072 or else Is_Internal (Parent_Subp)
13073 or else Is_Private_Overriding
13074 or else Is_Internal_Name (Chars (Parent_Subp))
13075 or else Chars (Parent_Subp) = Name_Initialize
13076 or else Chars (Parent_Subp) = Name_Adjust
13077 or else Chars (Parent_Subp) = Name_Finalize
13078 then
13079 Set_Derived_Name;
13080
13081 -- An inherited dispatching equality will be overridden by an internally
13082 -- generated one, or by an explicit one, so preserve its name and thus
13083 -- its entry in the dispatch table. Otherwise, if Parent_Subp is a
13084 -- private operation it may become invisible if the full view has
13085 -- progenitors, and the dispatch table will be malformed.
13086 -- We check that the type is limited to handle the anomalous declaration
13087 -- of Limited_Controlled, which is derived from a non-limited type, and
13088 -- which is handled specially elsewhere as well.
13089
13090 elsif Chars (Parent_Subp) = Name_Op_Eq
13091 and then Is_Dispatching_Operation (Parent_Subp)
13092 and then Etype (Parent_Subp) = Standard_Boolean
13093 and then not Is_Limited_Type (Etype (First_Formal (Parent_Subp)))
13094 and then
13095 Etype (First_Formal (Parent_Subp)) =
13096 Etype (Next_Formal (First_Formal (Parent_Subp)))
13097 then
13098 Set_Derived_Name;
13099
13100 -- If parent is hidden, this can be a regular derivation if the
13101 -- parent is immediately visible in a non-instantiating context,
13102 -- or if we are in the private part of an instance. This test
13103 -- should still be refined ???
13104
13105 -- The test for In_Instance_Not_Visible avoids inheriting the derived
13106 -- operation as a non-visible operation in cases where the parent
13107 -- subprogram might not be visible now, but was visible within the
13108 -- original generic, so it would be wrong to make the inherited
13109 -- subprogram non-visible now. (Not clear if this test is fully
13110 -- correct; are there any cases where we should declare the inherited
13111 -- operation as not visible to avoid it being overridden, e.g., when
13112 -- the parent type is a generic actual with private primitives ???)
13113
13114 -- (they should be treated the same as other private inherited
13115 -- subprograms, but it's not clear how to do this cleanly). ???
13116
13117 elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
13118 and then Is_Immediately_Visible (Parent_Subp)
13119 and then not In_Instance)
13120 or else In_Instance_Not_Visible
13121 then
13122 Set_Derived_Name;
13123
13124 -- Ada 2005 (AI-251): Regular derivation if the parent subprogram
13125 -- overrides an interface primitive because interface primitives
13126 -- must be visible in the partial view of the parent (RM 7.3 (7.3/2))
13127
13128 elsif Ada_Version >= Ada_2005
13129 and then Is_Dispatching_Operation (Parent_Subp)
13130 and then Covers_Some_Interface (Parent_Subp)
13131 then
13132 Set_Derived_Name;
13133
13134 -- Otherwise, the type is inheriting a private operation, so enter
13135 -- it with a special name so it can't be overridden.
13136
13137 else
13138 Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
13139 end if;
13140
13141 Set_Parent (New_Subp, Parent (Derived_Type));
13142
13143 if Present (Actual_Subp) then
13144 Replace_Type (Actual_Subp, New_Subp);
13145 else
13146 Replace_Type (Parent_Subp, New_Subp);
13147 end if;
13148
13149 Conditional_Delay (New_Subp, Parent_Subp);
13150
13151 -- If we are creating a renaming for a primitive operation of an
13152 -- actual of a generic derived type, we must examine the signature
13153 -- of the actual primitive, not that of the generic formal, which for
13154 -- example may be an interface. However the name and initial value
13155 -- of the inherited operation are those of the formal primitive.
13156
13157 Formal := First_Formal (Parent_Subp);
13158
13159 if Present (Actual_Subp) then
13160 Formal_Of_Actual := First_Formal (Actual_Subp);
13161 else
13162 Formal_Of_Actual := Empty;
13163 end if;
13164
13165 while Present (Formal) loop
13166 New_Formal := New_Copy (Formal);
13167
13168 -- Normally we do not go copying parents, but in the case of
13169 -- formals, we need to link up to the declaration (which is the
13170 -- parameter specification), and it is fine to link up to the
13171 -- original formal's parameter specification in this case.
13172
13173 Set_Parent (New_Formal, Parent (Formal));
13174 Append_Entity (New_Formal, New_Subp);
13175
13176 if Present (Formal_Of_Actual) then
13177 Replace_Type (Formal_Of_Actual, New_Formal);
13178 Next_Formal (Formal_Of_Actual);
13179 else
13180 Replace_Type (Formal, New_Formal);
13181 end if;
13182
13183 Next_Formal (Formal);
13184 end loop;
13185
13186 -- If this derivation corresponds to a tagged generic actual, then
13187 -- primitive operations rename those of the actual. Otherwise the
13188 -- primitive operations rename those of the parent type, If the parent
13189 -- renames an intrinsic operator, so does the new subprogram. We except
13190 -- concatenation, which is always properly typed, and does not get
13191 -- expanded as other intrinsic operations.
13192
13193 if No (Actual_Subp) then
13194 if Is_Intrinsic_Subprogram (Parent_Subp) then
13195 Set_Is_Intrinsic_Subprogram (New_Subp);
13196
13197 if Present (Alias (Parent_Subp))
13198 and then Chars (Parent_Subp) /= Name_Op_Concat
13199 then
13200 Set_Alias (New_Subp, Alias (Parent_Subp));
13201 else
13202 Set_Alias (New_Subp, Parent_Subp);
13203 end if;
13204
13205 else
13206 Set_Alias (New_Subp, Parent_Subp);
13207 end if;
13208
13209 else
13210 Set_Alias (New_Subp, Actual_Subp);
13211 end if;
13212
13213 -- Derived subprograms of a tagged type must inherit the convention
13214 -- of the parent subprogram (a requirement of AI-117). Derived
13215 -- subprograms of untagged types simply get convention Ada by default.
13216
13217 if Is_Tagged_Type (Derived_Type) then
13218 Set_Convention (New_Subp, Convention (Parent_Subp));
13219 end if;
13220
13221 -- Predefined controlled operations retain their name even if the parent
13222 -- is hidden (see above), but they are not primitive operations if the
13223 -- ancestor is not visible, for example if the parent is a private
13224 -- extension completed with a controlled extension. Note that a full
13225 -- type that is controlled can break privacy: the flag Is_Controlled is
13226 -- set on both views of the type.
13227
13228 if Is_Controlled (Parent_Type)
13229 and then
13230 (Chars (Parent_Subp) = Name_Initialize
13231 or else Chars (Parent_Subp) = Name_Adjust
13232 or else Chars (Parent_Subp) = Name_Finalize)
13233 and then Is_Hidden (Parent_Subp)
13234 and then not Is_Visibly_Controlled (Parent_Type)
13235 then
13236 Set_Is_Hidden (New_Subp);
13237 end if;
13238
13239 Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
13240 Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
13241
13242 if Ekind (Parent_Subp) = E_Procedure then
13243 Set_Is_Valued_Procedure
13244 (New_Subp, Is_Valued_Procedure (Parent_Subp));
13245 else
13246 Set_Has_Controlling_Result
13247 (New_Subp, Has_Controlling_Result (Parent_Subp));
13248 end if;
13249
13250 -- No_Return must be inherited properly. If this is overridden in the
13251 -- case of a dispatching operation, then a check is made in Sem_Disp
13252 -- that the overriding operation is also No_Return (no such check is
13253 -- required for the case of non-dispatching operation.
13254
13255 Set_No_Return (New_Subp, No_Return (Parent_Subp));
13256
13257 -- A derived function with a controlling result is abstract. If the
13258 -- Derived_Type is a nonabstract formal generic derived type, then
13259 -- inherited operations are not abstract: the required check is done at
13260 -- instantiation time. If the derivation is for a generic actual, the
13261 -- function is not abstract unless the actual is.
13262
13263 if Is_Generic_Type (Derived_Type)
13264 and then not Is_Abstract_Type (Derived_Type)
13265 then
13266 null;
13267
13268 -- Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
13269 -- properties of the subprogram, as defined in RM-3.9.3(4/2-6/2).
13270
13271 elsif Ada_Version >= Ada_2005
13272 and then (Is_Abstract_Subprogram (Alias (New_Subp))
13273 or else (Is_Tagged_Type (Derived_Type)
13274 and then Etype (New_Subp) = Derived_Type
13275 and then not Is_Null_Extension (Derived_Type))
13276 or else (Is_Tagged_Type (Derived_Type)
13277 and then Ekind (Etype (New_Subp)) =
13278 E_Anonymous_Access_Type
13279 and then Designated_Type (Etype (New_Subp)) =
13280 Derived_Type
13281 and then not Is_Null_Extension (Derived_Type)))
13282 and then No (Actual_Subp)
13283 then
13284 if not Is_Tagged_Type (Derived_Type)
13285 or else Is_Abstract_Type (Derived_Type)
13286 or else Is_Abstract_Subprogram (Alias (New_Subp))
13287 then
13288 Set_Is_Abstract_Subprogram (New_Subp);
13289 else
13290 Set_Requires_Overriding (New_Subp);
13291 end if;
13292
13293 elsif Ada_Version < Ada_2005
13294 and then (Is_Abstract_Subprogram (Alias (New_Subp))
13295 or else (Is_Tagged_Type (Derived_Type)
13296 and then Etype (New_Subp) = Derived_Type
13297 and then No (Actual_Subp)))
13298 then
13299 Set_Is_Abstract_Subprogram (New_Subp);
13300
13301 -- AI05-0097 : an inherited operation that dispatches on result is
13302 -- abstract if the derived type is abstract, even if the parent type
13303 -- is concrete and the derived type is a null extension.
13304
13305 elsif Has_Controlling_Result (Alias (New_Subp))
13306 and then Is_Abstract_Type (Etype (New_Subp))
13307 then
13308 Set_Is_Abstract_Subprogram (New_Subp);
13309
13310 -- Finally, if the parent type is abstract we must verify that all
13311 -- inherited operations are either non-abstract or overridden, or that
13312 -- the derived type itself is abstract (this check is performed at the
13313 -- end of a package declaration, in Check_Abstract_Overriding). A
13314 -- private overriding in the parent type will not be visible in the
13315 -- derivation if we are not in an inner package or in a child unit of
13316 -- the parent type, in which case the abstractness of the inherited
13317 -- operation is carried to the new subprogram.
13318
13319 elsif Is_Abstract_Type (Parent_Type)
13320 and then not In_Open_Scopes (Scope (Parent_Type))
13321 and then Is_Private_Overriding
13322 and then Is_Abstract_Subprogram (Visible_Subp)
13323 then
13324 if No (Actual_Subp) then
13325 Set_Alias (New_Subp, Visible_Subp);
13326 Set_Is_Abstract_Subprogram (New_Subp, True);
13327
13328 else
13329 -- If this is a derivation for an instance of a formal derived
13330 -- type, abstractness comes from the primitive operation of the
13331 -- actual, not from the operation inherited from the ancestor.
13332
13333 Set_Is_Abstract_Subprogram
13334 (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
13335 end if;
13336 end if;
13337
13338 New_Overloaded_Entity (New_Subp, Derived_Type);
13339
13340 -- Check for case of a derived subprogram for the instantiation of a
13341 -- formal derived tagged type, if so mark the subprogram as dispatching
13342 -- and inherit the dispatching attributes of the parent subprogram. The
13343 -- derived subprogram is effectively renaming of the actual subprogram,
13344 -- so it needs to have the same attributes as the actual.
13345
13346 if Present (Actual_Subp)
13347 and then Is_Dispatching_Operation (Parent_Subp)
13348 then
13349 Set_Is_Dispatching_Operation (New_Subp);
13350
13351 if Present (DTC_Entity (Parent_Subp)) then
13352 Set_DTC_Entity (New_Subp, DTC_Entity (Parent_Subp));
13353 Set_DT_Position (New_Subp, DT_Position (Parent_Subp));
13354 end if;
13355 end if;
13356
13357 -- Indicate that a derived subprogram does not require a body and that
13358 -- it does not require processing of default expressions.
13359
13360 Set_Has_Completion (New_Subp);
13361 Set_Default_Expressions_Processed (New_Subp);
13362
13363 if Ekind (New_Subp) = E_Function then
13364 Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
13365 end if;
13366 end Derive_Subprogram;
13367
13368 ------------------------
13369 -- Derive_Subprograms --
13370 ------------------------
13371
13372 procedure Derive_Subprograms
13373 (Parent_Type : Entity_Id;
13374 Derived_Type : Entity_Id;
13375 Generic_Actual : Entity_Id := Empty)
13376 is
13377 Op_List : constant Elist_Id :=
13378 Collect_Primitive_Operations (Parent_Type);
13379
13380 function Check_Derived_Type return Boolean;
13381 -- Check that all the entities derived from Parent_Type are found in
13382 -- the list of primitives of Derived_Type exactly in the same order.
13383
13384 procedure Derive_Interface_Subprogram
13385 (New_Subp : in out Entity_Id;
13386 Subp : Entity_Id;
13387 Actual_Subp : Entity_Id);
13388 -- Derive New_Subp from the ultimate alias of the parent subprogram Subp
13389 -- (which is an interface primitive). If Generic_Actual is present then
13390 -- Actual_Subp is the actual subprogram corresponding with the generic
13391 -- subprogram Subp.
13392
13393 function Check_Derived_Type return Boolean is
13394 E : Entity_Id;
13395 Elmt : Elmt_Id;
13396 List : Elist_Id;
13397 New_Subp : Entity_Id;
13398 Op_Elmt : Elmt_Id;
13399 Subp : Entity_Id;
13400
13401 begin
13402 -- Traverse list of entities in the current scope searching for
13403 -- an incomplete type whose full-view is derived type
13404
13405 E := First_Entity (Scope (Derived_Type));
13406 while Present (E)
13407 and then E /= Derived_Type
13408 loop
13409 if Ekind (E) = E_Incomplete_Type
13410 and then Present (Full_View (E))
13411 and then Full_View (E) = Derived_Type
13412 then
13413 -- Disable this test if Derived_Type completes an incomplete
13414 -- type because in such case more primitives can be added
13415 -- later to the list of primitives of Derived_Type by routine
13416 -- Process_Incomplete_Dependents
13417
13418 return True;
13419 end if;
13420
13421 E := Next_Entity (E);
13422 end loop;
13423
13424 List := Collect_Primitive_Operations (Derived_Type);
13425 Elmt := First_Elmt (List);
13426
13427 Op_Elmt := First_Elmt (Op_List);
13428 while Present (Op_Elmt) loop
13429 Subp := Node (Op_Elmt);
13430 New_Subp := Node (Elmt);
13431
13432 -- At this early stage Derived_Type has no entities with attribute
13433 -- Interface_Alias. In addition, such primitives are always
13434 -- located at the end of the list of primitives of Parent_Type.
13435 -- Therefore, if found we can safely stop processing pending
13436 -- entities.
13437
13438 exit when Present (Interface_Alias (Subp));
13439
13440 -- Handle hidden entities
13441
13442 if not Is_Predefined_Dispatching_Operation (Subp)
13443 and then Is_Hidden (Subp)
13444 then
13445 if Present (New_Subp)
13446 and then Primitive_Names_Match (Subp, New_Subp)
13447 then
13448 Next_Elmt (Elmt);
13449 end if;
13450
13451 else
13452 if not Present (New_Subp)
13453 or else Ekind (Subp) /= Ekind (New_Subp)
13454 or else not Primitive_Names_Match (Subp, New_Subp)
13455 then
13456 return False;
13457 end if;
13458
13459 Next_Elmt (Elmt);
13460 end if;
13461
13462 Next_Elmt (Op_Elmt);
13463 end loop;
13464
13465 return True;
13466 end Check_Derived_Type;
13467
13468 ---------------------------------
13469 -- Derive_Interface_Subprogram --
13470 ---------------------------------
13471
13472 procedure Derive_Interface_Subprogram
13473 (New_Subp : in out Entity_Id;
13474 Subp : Entity_Id;
13475 Actual_Subp : Entity_Id)
13476 is
13477 Iface_Subp : constant Entity_Id := Ultimate_Alias (Subp);
13478 Iface_Type : constant Entity_Id := Find_Dispatching_Type (Iface_Subp);
13479
13480 begin
13481 pragma Assert (Is_Interface (Iface_Type));
13482
13483 Derive_Subprogram
13484 (New_Subp => New_Subp,
13485 Parent_Subp => Iface_Subp,
13486 Derived_Type => Derived_Type,
13487 Parent_Type => Iface_Type,
13488 Actual_Subp => Actual_Subp);
13489
13490 -- Given that this new interface entity corresponds with a primitive
13491 -- of the parent that was not overridden we must leave it associated
13492 -- with its parent primitive to ensure that it will share the same
13493 -- dispatch table slot when overridden.
13494
13495 if No (Actual_Subp) then
13496 Set_Alias (New_Subp, Subp);
13497
13498 -- For instantiations this is not needed since the previous call to
13499 -- Derive_Subprogram leaves the entity well decorated.
13500
13501 else
13502 pragma Assert (Alias (New_Subp) = Actual_Subp);
13503 null;
13504 end if;
13505 end Derive_Interface_Subprogram;
13506
13507 -- Local variables
13508
13509 Alias_Subp : Entity_Id;
13510 Act_List : Elist_Id;
13511 Act_Elmt : Elmt_Id := No_Elmt;
13512 Act_Subp : Entity_Id := Empty;
13513 Elmt : Elmt_Id;
13514 Need_Search : Boolean := False;
13515 New_Subp : Entity_Id := Empty;
13516 Parent_Base : Entity_Id;
13517 Subp : Entity_Id;
13518
13519 -- Start of processing for Derive_Subprograms
13520
13521 begin
13522 if Ekind (Parent_Type) = E_Record_Type_With_Private
13523 and then Has_Discriminants (Parent_Type)
13524 and then Present (Full_View (Parent_Type))
13525 then
13526 Parent_Base := Full_View (Parent_Type);
13527 else
13528 Parent_Base := Parent_Type;
13529 end if;
13530
13531 if Present (Generic_Actual) then
13532 Act_List := Collect_Primitive_Operations (Generic_Actual);
13533 Act_Elmt := First_Elmt (Act_List);
13534 end if;
13535
13536 -- Derive primitives inherited from the parent. Note that if the generic
13537 -- actual is present, this is not really a type derivation, it is a
13538 -- completion within an instance.
13539
13540 -- Case 1: Derived_Type does not implement interfaces
13541
13542 if not Is_Tagged_Type (Derived_Type)
13543 or else (not Has_Interfaces (Derived_Type)
13544 and then not (Present (Generic_Actual)
13545 and then
13546 Has_Interfaces (Generic_Actual)))
13547 then
13548 Elmt := First_Elmt (Op_List);
13549 while Present (Elmt) loop
13550 Subp := Node (Elmt);
13551
13552 -- Literals are derived earlier in the process of building the
13553 -- derived type, and are skipped here.
13554
13555 if Ekind (Subp) = E_Enumeration_Literal then
13556 null;
13557
13558 -- The actual is a direct descendant and the common primitive
13559 -- operations appear in the same order.
13560
13561 -- If the generic parent type is present, the derived type is an
13562 -- instance of a formal derived type, and within the instance its
13563 -- operations are those of the actual. We derive from the formal
13564 -- type but make the inherited operations aliases of the
13565 -- corresponding operations of the actual.
13566
13567 else
13568 pragma Assert (No (Node (Act_Elmt))
13569 or else (Primitive_Names_Match (Subp, Node (Act_Elmt))
13570 and then
13571 Type_Conformant (Subp, Node (Act_Elmt),
13572 Skip_Controlling_Formals => True)));
13573
13574 Derive_Subprogram
13575 (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
13576
13577 if Present (Act_Elmt) then
13578 Next_Elmt (Act_Elmt);
13579 end if;
13580 end if;
13581
13582 Next_Elmt (Elmt);
13583 end loop;
13584
13585 -- Case 2: Derived_Type implements interfaces
13586
13587 else
13588 -- If the parent type has no predefined primitives we remove
13589 -- predefined primitives from the list of primitives of generic
13590 -- actual to simplify the complexity of this algorithm.
13591
13592 if Present (Generic_Actual) then
13593 declare
13594 Has_Predefined_Primitives : Boolean := False;
13595
13596 begin
13597 -- Check if the parent type has predefined primitives
13598
13599 Elmt := First_Elmt (Op_List);
13600 while Present (Elmt) loop
13601 Subp := Node (Elmt);
13602
13603 if Is_Predefined_Dispatching_Operation (Subp)
13604 and then not Comes_From_Source (Ultimate_Alias (Subp))
13605 then
13606 Has_Predefined_Primitives := True;
13607 exit;
13608 end if;
13609
13610 Next_Elmt (Elmt);
13611 end loop;
13612
13613 -- Remove predefined primitives of Generic_Actual. We must use
13614 -- an auxiliary list because in case of tagged types the value
13615 -- returned by Collect_Primitive_Operations is the value stored
13616 -- in its Primitive_Operations attribute (and we don't want to
13617 -- modify its current contents).
13618
13619 if not Has_Predefined_Primitives then
13620 declare
13621 Aux_List : constant Elist_Id := New_Elmt_List;
13622
13623 begin
13624 Elmt := First_Elmt (Act_List);
13625 while Present (Elmt) loop
13626 Subp := Node (Elmt);
13627
13628 if not Is_Predefined_Dispatching_Operation (Subp)
13629 or else Comes_From_Source (Subp)
13630 then
13631 Append_Elmt (Subp, Aux_List);
13632 end if;
13633
13634 Next_Elmt (Elmt);
13635 end loop;
13636
13637 Act_List := Aux_List;
13638 end;
13639 end if;
13640
13641 Act_Elmt := First_Elmt (Act_List);
13642 Act_Subp := Node (Act_Elmt);
13643 end;
13644 end if;
13645
13646 -- Stage 1: If the generic actual is not present we derive the
13647 -- primitives inherited from the parent type. If the generic parent
13648 -- type is present, the derived type is an instance of a formal
13649 -- derived type, and within the instance its operations are those of
13650 -- the actual. We derive from the formal type but make the inherited
13651 -- operations aliases of the corresponding operations of the actual.
13652
13653 Elmt := First_Elmt (Op_List);
13654 while Present (Elmt) loop
13655 Subp := Node (Elmt);
13656 Alias_Subp := Ultimate_Alias (Subp);
13657
13658 -- Do not derive internal entities of the parent that link
13659 -- interface primitives with their covering primitive. These
13660 -- entities will be added to this type when frozen.
13661
13662 if Present (Interface_Alias (Subp)) then
13663 goto Continue;
13664 end if;
13665
13666 -- If the generic actual is present find the corresponding
13667 -- operation in the generic actual. If the parent type is a
13668 -- direct ancestor of the derived type then, even if it is an
13669 -- interface, the operations are inherited from the primary
13670 -- dispatch table and are in the proper order. If we detect here
13671 -- that primitives are not in the same order we traverse the list
13672 -- of primitive operations of the actual to find the one that
13673 -- implements the interface primitive.
13674
13675 if Need_Search
13676 or else
13677 (Present (Generic_Actual)
13678 and then Present (Act_Subp)
13679 and then not
13680 (Primitive_Names_Match (Subp, Act_Subp)
13681 and then
13682 Type_Conformant (Subp, Act_Subp,
13683 Skip_Controlling_Formals => True)))
13684 then
13685 pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual,
13686 Use_Full_View => True));
13687
13688 -- Remember that we need searching for all pending primitives
13689
13690 Need_Search := True;
13691
13692 -- Handle entities associated with interface primitives
13693
13694 if Present (Alias_Subp)
13695 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
13696 and then not Is_Predefined_Dispatching_Operation (Subp)
13697 then
13698 -- Search for the primitive in the homonym chain
13699
13700 Act_Subp :=
13701 Find_Primitive_Covering_Interface
13702 (Tagged_Type => Generic_Actual,
13703 Iface_Prim => Alias_Subp);
13704
13705 -- Previous search may not locate primitives covering
13706 -- interfaces defined in generics units or instantiations.
13707 -- (it fails if the covering primitive has formals whose
13708 -- type is also defined in generics or instantiations).
13709 -- In such case we search in the list of primitives of the
13710 -- generic actual for the internal entity that links the
13711 -- interface primitive and the covering primitive.
13712
13713 if No (Act_Subp)
13714 and then Is_Generic_Type (Parent_Type)
13715 then
13716 -- This code has been designed to handle only generic
13717 -- formals that implement interfaces that are defined
13718 -- in a generic unit or instantiation. If this code is
13719 -- needed for other cases we must review it because
13720 -- (given that it relies on Original_Location to locate
13721 -- the primitive of Generic_Actual that covers the
13722 -- interface) it could leave linked through attribute
13723 -- Alias entities of unrelated instantiations).
13724
13725 pragma Assert
13726 (Is_Generic_Unit
13727 (Scope (Find_Dispatching_Type (Alias_Subp)))
13728 or else
13729 Instantiation_Depth
13730 (Sloc (Find_Dispatching_Type (Alias_Subp))) > 0);
13731
13732 declare
13733 Iface_Prim_Loc : constant Source_Ptr :=
13734 Original_Location (Sloc (Alias_Subp));
13735 Elmt : Elmt_Id;
13736 Prim : Entity_Id;
13737 begin
13738 Elmt :=
13739 First_Elmt (Primitive_Operations (Generic_Actual));
13740
13741 Search : while Present (Elmt) loop
13742 Prim := Node (Elmt);
13743
13744 if Present (Interface_Alias (Prim))
13745 and then Original_Location
13746 (Sloc (Interface_Alias (Prim)))
13747 = Iface_Prim_Loc
13748 then
13749 Act_Subp := Alias (Prim);
13750 exit Search;
13751 end if;
13752
13753 Next_Elmt (Elmt);
13754 end loop Search;
13755 end;
13756 end if;
13757
13758 pragma Assert (Present (Act_Subp)
13759 or else Is_Abstract_Type (Generic_Actual)
13760 or else Serious_Errors_Detected > 0);
13761
13762 -- Handle predefined primitives plus the rest of user-defined
13763 -- primitives
13764
13765 else
13766 Act_Elmt := First_Elmt (Act_List);
13767 while Present (Act_Elmt) loop
13768 Act_Subp := Node (Act_Elmt);
13769
13770 exit when Primitive_Names_Match (Subp, Act_Subp)
13771 and then Type_Conformant
13772 (Subp, Act_Subp,
13773 Skip_Controlling_Formals => True)
13774 and then No (Interface_Alias (Act_Subp));
13775
13776 Next_Elmt (Act_Elmt);
13777 end loop;
13778
13779 if No (Act_Elmt) then
13780 Act_Subp := Empty;
13781 end if;
13782 end if;
13783 end if;
13784
13785 -- Case 1: If the parent is a limited interface then it has the
13786 -- predefined primitives of synchronized interfaces. However, the
13787 -- actual type may be a non-limited type and hence it does not
13788 -- have such primitives.
13789
13790 if Present (Generic_Actual)
13791 and then not Present (Act_Subp)
13792 and then Is_Limited_Interface (Parent_Base)
13793 and then Is_Predefined_Interface_Primitive (Subp)
13794 then
13795 null;
13796
13797 -- Case 2: Inherit entities associated with interfaces that were
13798 -- not covered by the parent type. We exclude here null interface
13799 -- primitives because they do not need special management.
13800
13801 -- We also exclude interface operations that are renamings. If the
13802 -- subprogram is an explicit renaming of an interface primitive,
13803 -- it is a regular primitive operation, and the presence of its
13804 -- alias is not relevant: it has to be derived like any other
13805 -- primitive.
13806
13807 elsif Present (Alias (Subp))
13808 and then Nkind (Unit_Declaration_Node (Subp)) /=
13809 N_Subprogram_Renaming_Declaration
13810 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
13811 and then not
13812 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
13813 and then Null_Present (Parent (Alias_Subp)))
13814 then
13815 -- If this is an abstract private type then we transfer the
13816 -- derivation of the interface primitive from the partial view
13817 -- to the full view. This is safe because all the interfaces
13818 -- must be visible in the partial view. Done to avoid adding
13819 -- a new interface derivation to the private part of the
13820 -- enclosing package; otherwise this new derivation would be
13821 -- decorated as hidden when the analysis of the enclosing
13822 -- package completes.
13823
13824 if Is_Abstract_Type (Derived_Type)
13825 and then In_Private_Part (Current_Scope)
13826 and then Has_Private_Declaration (Derived_Type)
13827 then
13828 declare
13829 Partial_View : Entity_Id;
13830 Elmt : Elmt_Id;
13831 Ent : Entity_Id;
13832
13833 begin
13834 Partial_View := First_Entity (Current_Scope);
13835 loop
13836 exit when No (Partial_View)
13837 or else (Has_Private_Declaration (Partial_View)
13838 and then
13839 Full_View (Partial_View) = Derived_Type);
13840
13841 Next_Entity (Partial_View);
13842 end loop;
13843
13844 -- If the partial view was not found then the source code
13845 -- has errors and the derivation is not needed.
13846
13847 if Present (Partial_View) then
13848 Elmt :=
13849 First_Elmt (Primitive_Operations (Partial_View));
13850 while Present (Elmt) loop
13851 Ent := Node (Elmt);
13852
13853 if Present (Alias (Ent))
13854 and then Ultimate_Alias (Ent) = Alias (Subp)
13855 then
13856 Append_Elmt
13857 (Ent, Primitive_Operations (Derived_Type));
13858 exit;
13859 end if;
13860
13861 Next_Elmt (Elmt);
13862 end loop;
13863
13864 -- If the interface primitive was not found in the
13865 -- partial view then this interface primitive was
13866 -- overridden. We add a derivation to activate in
13867 -- Derive_Progenitor_Subprograms the machinery to
13868 -- search for it.
13869
13870 if No (Elmt) then
13871 Derive_Interface_Subprogram
13872 (New_Subp => New_Subp,
13873 Subp => Subp,
13874 Actual_Subp => Act_Subp);
13875 end if;
13876 end if;
13877 end;
13878 else
13879 Derive_Interface_Subprogram
13880 (New_Subp => New_Subp,
13881 Subp => Subp,
13882 Actual_Subp => Act_Subp);
13883 end if;
13884
13885 -- Case 3: Common derivation
13886
13887 else
13888 Derive_Subprogram
13889 (New_Subp => New_Subp,
13890 Parent_Subp => Subp,
13891 Derived_Type => Derived_Type,
13892 Parent_Type => Parent_Base,
13893 Actual_Subp => Act_Subp);
13894 end if;
13895
13896 -- No need to update Act_Elm if we must search for the
13897 -- corresponding operation in the generic actual
13898
13899 if not Need_Search
13900 and then Present (Act_Elmt)
13901 then
13902 Next_Elmt (Act_Elmt);
13903 Act_Subp := Node (Act_Elmt);
13904 end if;
13905
13906 <<Continue>>
13907 Next_Elmt (Elmt);
13908 end loop;
13909
13910 -- Inherit additional operations from progenitors. If the derived
13911 -- type is a generic actual, there are not new primitive operations
13912 -- for the type because it has those of the actual, and therefore
13913 -- nothing needs to be done. The renamings generated above are not
13914 -- primitive operations, and their purpose is simply to make the
13915 -- proper operations visible within an instantiation.
13916
13917 if No (Generic_Actual) then
13918 Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
13919 end if;
13920 end if;
13921
13922 -- Final check: Direct descendants must have their primitives in the
13923 -- same order. We exclude from this test untagged types and instances
13924 -- of formal derived types. We skip this test if we have already
13925 -- reported serious errors in the sources.
13926
13927 pragma Assert (not Is_Tagged_Type (Derived_Type)
13928 or else Present (Generic_Actual)
13929 or else Serious_Errors_Detected > 0
13930 or else Check_Derived_Type);
13931 end Derive_Subprograms;
13932
13933 --------------------------------
13934 -- Derived_Standard_Character --
13935 --------------------------------
13936
13937 procedure Derived_Standard_Character
13938 (N : Node_Id;
13939 Parent_Type : Entity_Id;
13940 Derived_Type : Entity_Id)
13941 is
13942 Loc : constant Source_Ptr := Sloc (N);
13943 Def : constant Node_Id := Type_Definition (N);
13944 Indic : constant Node_Id := Subtype_Indication (Def);
13945 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
13946 Implicit_Base : constant Entity_Id :=
13947 Create_Itype
13948 (E_Enumeration_Type, N, Derived_Type, 'B');
13949
13950 Lo : Node_Id;
13951 Hi : Node_Id;
13952
13953 begin
13954 Discard_Node (Process_Subtype (Indic, N));
13955
13956 Set_Etype (Implicit_Base, Parent_Base);
13957 Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
13958 Set_RM_Size (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
13959
13960 Set_Is_Character_Type (Implicit_Base, True);
13961 Set_Has_Delayed_Freeze (Implicit_Base);
13962
13963 -- The bounds of the implicit base are the bounds of the parent base.
13964 -- Note that their type is the parent base.
13965
13966 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
13967 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
13968
13969 Set_Scalar_Range (Implicit_Base,
13970 Make_Range (Loc,
13971 Low_Bound => Lo,
13972 High_Bound => Hi));
13973
13974 Conditional_Delay (Derived_Type, Parent_Type);
13975
13976 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
13977 Set_Etype (Derived_Type, Implicit_Base);
13978 Set_Size_Info (Derived_Type, Parent_Type);
13979
13980 if Unknown_RM_Size (Derived_Type) then
13981 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
13982 end if;
13983
13984 Set_Is_Character_Type (Derived_Type, True);
13985
13986 if Nkind (Indic) /= N_Subtype_Indication then
13987
13988 -- If no explicit constraint, the bounds are those
13989 -- of the parent type.
13990
13991 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Type));
13992 Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
13993 Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
13994 end if;
13995
13996 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
13997
13998 -- Because the implicit base is used in the conversion of the bounds, we
13999 -- have to freeze it now. This is similar to what is done for numeric
14000 -- types, and it equally suspicious, but otherwise a non-static bound
14001 -- will have a reference to an unfrozen type, which is rejected by Gigi
14002 -- (???). This requires specific care for definition of stream
14003 -- attributes. For details, see comments at the end of
14004 -- Build_Derived_Numeric_Type.
14005
14006 Freeze_Before (N, Implicit_Base);
14007 end Derived_Standard_Character;
14008
14009 ------------------------------
14010 -- Derived_Type_Declaration --
14011 ------------------------------
14012
14013 procedure Derived_Type_Declaration
14014 (T : Entity_Id;
14015 N : Node_Id;
14016 Is_Completion : Boolean)
14017 is
14018 Parent_Type : Entity_Id;
14019
14020 function Comes_From_Generic (Typ : Entity_Id) return Boolean;
14021 -- Check whether the parent type is a generic formal, or derives
14022 -- directly or indirectly from one.
14023
14024 ------------------------
14025 -- Comes_From_Generic --
14026 ------------------------
14027
14028 function Comes_From_Generic (Typ : Entity_Id) return Boolean is
14029 begin
14030 if Is_Generic_Type (Typ) then
14031 return True;
14032
14033 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
14034 return True;
14035
14036 elsif Is_Private_Type (Typ)
14037 and then Present (Full_View (Typ))
14038 and then Is_Generic_Type (Root_Type (Full_View (Typ)))
14039 then
14040 return True;
14041
14042 elsif Is_Generic_Actual_Type (Typ) then
14043 return True;
14044
14045 else
14046 return False;
14047 end if;
14048 end Comes_From_Generic;
14049
14050 -- Local variables
14051
14052 Def : constant Node_Id := Type_Definition (N);
14053 Iface_Def : Node_Id;
14054 Indic : constant Node_Id := Subtype_Indication (Def);
14055 Extension : constant Node_Id := Record_Extension_Part (Def);
14056 Parent_Node : Node_Id;
14057 Taggd : Boolean;
14058
14059 -- Start of processing for Derived_Type_Declaration
14060
14061 begin
14062 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
14063
14064 -- Ada 2005 (AI-251): In case of interface derivation check that the
14065 -- parent is also an interface.
14066
14067 if Interface_Present (Def) then
14068 Check_SPARK_Restriction ("interface is not allowed", Def);
14069
14070 if not Is_Interface (Parent_Type) then
14071 Diagnose_Interface (Indic, Parent_Type);
14072
14073 else
14074 Parent_Node := Parent (Base_Type (Parent_Type));
14075 Iface_Def := Type_Definition (Parent_Node);
14076
14077 -- Ada 2005 (AI-251): Limited interfaces can only inherit from
14078 -- other limited interfaces.
14079
14080 if Limited_Present (Def) then
14081 if Limited_Present (Iface_Def) then
14082 null;
14083
14084 elsif Protected_Present (Iface_Def) then
14085 Error_Msg_NE
14086 ("descendant of& must be declared"
14087 & " as a protected interface",
14088 N, Parent_Type);
14089
14090 elsif Synchronized_Present (Iface_Def) then
14091 Error_Msg_NE
14092 ("descendant of& must be declared"
14093 & " as a synchronized interface",
14094 N, Parent_Type);
14095
14096 elsif Task_Present (Iface_Def) then
14097 Error_Msg_NE
14098 ("descendant of& must be declared as a task interface",
14099 N, Parent_Type);
14100
14101 else
14102 Error_Msg_N
14103 ("(Ada 2005) limited interface cannot "
14104 & "inherit from non-limited interface", Indic);
14105 end if;
14106
14107 -- Ada 2005 (AI-345): Non-limited interfaces can only inherit
14108 -- from non-limited or limited interfaces.
14109
14110 elsif not Protected_Present (Def)
14111 and then not Synchronized_Present (Def)
14112 and then not Task_Present (Def)
14113 then
14114 if Limited_Present (Iface_Def) then
14115 null;
14116
14117 elsif Protected_Present (Iface_Def) then
14118 Error_Msg_NE
14119 ("descendant of& must be declared"
14120 & " as a protected interface",
14121 N, Parent_Type);
14122
14123 elsif Synchronized_Present (Iface_Def) then
14124 Error_Msg_NE
14125 ("descendant of& must be declared"
14126 & " as a synchronized interface",
14127 N, Parent_Type);
14128
14129 elsif Task_Present (Iface_Def) then
14130 Error_Msg_NE
14131 ("descendant of& must be declared as a task interface",
14132 N, Parent_Type);
14133 else
14134 null;
14135 end if;
14136 end if;
14137 end if;
14138 end if;
14139
14140 if Is_Tagged_Type (Parent_Type)
14141 and then Is_Concurrent_Type (Parent_Type)
14142 and then not Is_Interface (Parent_Type)
14143 then
14144 Error_Msg_N
14145 ("parent type of a record extension cannot be "
14146 & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
14147 Set_Etype (T, Any_Type);
14148 return;
14149 end if;
14150
14151 -- Ada 2005 (AI-251): Decorate all the names in the list of ancestor
14152 -- interfaces
14153
14154 if Is_Tagged_Type (Parent_Type)
14155 and then Is_Non_Empty_List (Interface_List (Def))
14156 then
14157 declare
14158 Intf : Node_Id;
14159 T : Entity_Id;
14160
14161 begin
14162 Intf := First (Interface_List (Def));
14163 while Present (Intf) loop
14164 T := Find_Type_Of_Subtype_Indic (Intf);
14165
14166 if not Is_Interface (T) then
14167 Diagnose_Interface (Intf, T);
14168
14169 -- Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
14170 -- a limited type from having a nonlimited progenitor.
14171
14172 elsif (Limited_Present (Def)
14173 or else (not Is_Interface (Parent_Type)
14174 and then Is_Limited_Type (Parent_Type)))
14175 and then not Is_Limited_Interface (T)
14176 then
14177 Error_Msg_NE
14178 ("progenitor interface& of limited type must be limited",
14179 N, T);
14180 end if;
14181
14182 Next (Intf);
14183 end loop;
14184 end;
14185 end if;
14186
14187 if Parent_Type = Any_Type
14188 or else Etype (Parent_Type) = Any_Type
14189 or else (Is_Class_Wide_Type (Parent_Type)
14190 and then Etype (Parent_Type) = T)
14191 then
14192 -- If Parent_Type is undefined or illegal, make new type into a
14193 -- subtype of Any_Type, and set a few attributes to prevent cascaded
14194 -- errors. If this is a self-definition, emit error now.
14195
14196 if T = Parent_Type
14197 or else T = Etype (Parent_Type)
14198 then
14199 Error_Msg_N ("type cannot be used in its own definition", Indic);
14200 end if;
14201
14202 Set_Ekind (T, Ekind (Parent_Type));
14203 Set_Etype (T, Any_Type);
14204 Set_Scalar_Range (T, Scalar_Range (Any_Type));
14205
14206 if Is_Tagged_Type (T)
14207 and then Is_Record_Type (T)
14208 then
14209 Set_Direct_Primitive_Operations (T, New_Elmt_List);
14210 end if;
14211
14212 return;
14213 end if;
14214
14215 -- Ada 2005 (AI-251): The case in which the parent of the full-view is
14216 -- an interface is special because the list of interfaces in the full
14217 -- view can be given in any order. For example:
14218
14219 -- type A is interface;
14220 -- type B is interface and A;
14221 -- type D is new B with private;
14222 -- private
14223 -- type D is new A and B with null record; -- 1 --
14224
14225 -- In this case we perform the following transformation of -1-:
14226
14227 -- type D is new B and A with null record;
14228
14229 -- If the parent of the full-view covers the parent of the partial-view
14230 -- we have two possible cases:
14231
14232 -- 1) They have the same parent
14233 -- 2) The parent of the full-view implements some further interfaces
14234
14235 -- In both cases we do not need to perform the transformation. In the
14236 -- first case the source program is correct and the transformation is
14237 -- not needed; in the second case the source program does not fulfill
14238 -- the no-hidden interfaces rule (AI-396) and the error will be reported
14239 -- later.
14240
14241 -- This transformation not only simplifies the rest of the analysis of
14242 -- this type declaration but also simplifies the correct generation of
14243 -- the object layout to the expander.
14244
14245 if In_Private_Part (Current_Scope)
14246 and then Is_Interface (Parent_Type)
14247 then
14248 declare
14249 Iface : Node_Id;
14250 Partial_View : Entity_Id;
14251 Partial_View_Parent : Entity_Id;
14252 New_Iface : Node_Id;
14253
14254 begin
14255 -- Look for the associated private type declaration
14256
14257 Partial_View := First_Entity (Current_Scope);
14258 loop
14259 exit when No (Partial_View)
14260 or else (Has_Private_Declaration (Partial_View)
14261 and then Full_View (Partial_View) = T);
14262
14263 Next_Entity (Partial_View);
14264 end loop;
14265
14266 -- If the partial view was not found then the source code has
14267 -- errors and the transformation is not needed.
14268
14269 if Present (Partial_View) then
14270 Partial_View_Parent := Etype (Partial_View);
14271
14272 -- If the parent of the full-view covers the parent of the
14273 -- partial-view we have nothing else to do.
14274
14275 if Interface_Present_In_Ancestor
14276 (Parent_Type, Partial_View_Parent)
14277 then
14278 null;
14279
14280 -- Traverse the list of interfaces of the full-view to look
14281 -- for the parent of the partial-view and perform the tree
14282 -- transformation.
14283
14284 else
14285 Iface := First (Interface_List (Def));
14286 while Present (Iface) loop
14287 if Etype (Iface) = Etype (Partial_View) then
14288 Rewrite (Subtype_Indication (Def),
14289 New_Copy (Subtype_Indication
14290 (Parent (Partial_View))));
14291
14292 New_Iface :=
14293 Make_Identifier (Sloc (N), Chars (Parent_Type));
14294 Append (New_Iface, Interface_List (Def));
14295
14296 -- Analyze the transformed code
14297
14298 Derived_Type_Declaration (T, N, Is_Completion);
14299 return;
14300 end if;
14301
14302 Next (Iface);
14303 end loop;
14304 end if;
14305 end if;
14306 end;
14307 end if;
14308
14309 -- Only composite types other than array types are allowed to have
14310 -- discriminants. In SPARK, no types are allowed to have discriminants.
14311
14312 if Present (Discriminant_Specifications (N)) then
14313 if (Is_Elementary_Type (Parent_Type)
14314 or else Is_Array_Type (Parent_Type))
14315 and then not Error_Posted (N)
14316 then
14317 Error_Msg_N
14318 ("elementary or array type cannot have discriminants",
14319 Defining_Identifier (First (Discriminant_Specifications (N))));
14320 Set_Has_Discriminants (T, False);
14321 else
14322 Check_SPARK_Restriction ("discriminant type is not allowed", N);
14323 end if;
14324 end if;
14325
14326 -- In Ada 83, a derived type defined in a package specification cannot
14327 -- be used for further derivation until the end of its visible part.
14328 -- Note that derivation in the private part of the package is allowed.
14329
14330 if Ada_Version = Ada_83
14331 and then Is_Derived_Type (Parent_Type)
14332 and then In_Visible_Part (Scope (Parent_Type))
14333 then
14334 if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
14335 Error_Msg_N
14336 ("(Ada 83): premature use of type for derivation", Indic);
14337 end if;
14338 end if;
14339
14340 -- Check for early use of incomplete or private type
14341
14342 if Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
14343 Error_Msg_N ("premature derivation of incomplete type", Indic);
14344 return;
14345
14346 elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
14347 and then not Comes_From_Generic (Parent_Type))
14348 or else Has_Private_Component (Parent_Type)
14349 then
14350 -- The ancestor type of a formal type can be incomplete, in which
14351 -- case only the operations of the partial view are available in the
14352 -- generic. Subsequent checks may be required when the full view is
14353 -- analyzed to verify that a derivation from a tagged type has an
14354 -- extension.
14355
14356 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
14357 null;
14358
14359 elsif No (Underlying_Type (Parent_Type))
14360 or else Has_Private_Component (Parent_Type)
14361 then
14362 Error_Msg_N
14363 ("premature derivation of derived or private type", Indic);
14364
14365 -- Flag the type itself as being in error, this prevents some
14366 -- nasty problems with subsequent uses of the malformed type.
14367
14368 Set_Error_Posted (T);
14369
14370 -- Check that within the immediate scope of an untagged partial
14371 -- view it's illegal to derive from the partial view if the
14372 -- full view is tagged. (7.3(7))
14373
14374 -- We verify that the Parent_Type is a partial view by checking
14375 -- that it is not a Full_Type_Declaration (i.e. a private type or
14376 -- private extension declaration), to distinguish a partial view
14377 -- from a derivation from a private type which also appears as
14378 -- E_Private_Type. If the parent base type is not declared in an
14379 -- enclosing scope there is no need to check.
14380
14381 elsif Present (Full_View (Parent_Type))
14382 and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
14383 and then not Is_Tagged_Type (Parent_Type)
14384 and then Is_Tagged_Type (Full_View (Parent_Type))
14385 and then In_Open_Scopes (Scope (Base_Type (Parent_Type)))
14386 then
14387 Error_Msg_N
14388 ("premature derivation from type with tagged full view",
14389 Indic);
14390 end if;
14391 end if;
14392
14393 -- Check that form of derivation is appropriate
14394
14395 Taggd := Is_Tagged_Type (Parent_Type);
14396
14397 -- Perhaps the parent type should be changed to the class-wide type's
14398 -- specific type in this case to prevent cascading errors ???
14399
14400 if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
14401 Error_Msg_N ("parent type must not be a class-wide type", Indic);
14402 return;
14403 end if;
14404
14405 if Present (Extension) and then not Taggd then
14406 Error_Msg_N
14407 ("type derived from untagged type cannot have extension", Indic);
14408
14409 elsif No (Extension) and then Taggd then
14410
14411 -- If this declaration is within a private part (or body) of a
14412 -- generic instantiation then the derivation is allowed (the parent
14413 -- type can only appear tagged in this case if it's a generic actual
14414 -- type, since it would otherwise have been rejected in the analysis
14415 -- of the generic template).
14416
14417 if not Is_Generic_Actual_Type (Parent_Type)
14418 or else In_Visible_Part (Scope (Parent_Type))
14419 then
14420 if Is_Class_Wide_Type (Parent_Type) then
14421 Error_Msg_N
14422 ("parent type must not be a class-wide type", Indic);
14423
14424 -- Use specific type to prevent cascaded errors.
14425
14426 Parent_Type := Etype (Parent_Type);
14427
14428 else
14429 Error_Msg_N
14430 ("type derived from tagged type must have extension", Indic);
14431 end if;
14432 end if;
14433 end if;
14434
14435 -- AI-443: Synchronized formal derived types require a private
14436 -- extension. There is no point in checking the ancestor type or
14437 -- the progenitors since the construct is wrong to begin with.
14438
14439 if Ada_Version >= Ada_2005
14440 and then Is_Generic_Type (T)
14441 and then Present (Original_Node (N))
14442 then
14443 declare
14444 Decl : constant Node_Id := Original_Node (N);
14445
14446 begin
14447 if Nkind (Decl) = N_Formal_Type_Declaration
14448 and then Nkind (Formal_Type_Definition (Decl)) =
14449 N_Formal_Derived_Type_Definition
14450 and then Synchronized_Present (Formal_Type_Definition (Decl))
14451 and then No (Extension)
14452
14453 -- Avoid emitting a duplicate error message
14454
14455 and then not Error_Posted (Indic)
14456 then
14457 Error_Msg_N
14458 ("synchronized derived type must have extension", N);
14459 end if;
14460 end;
14461 end if;
14462
14463 if Null_Exclusion_Present (Def)
14464 and then not Is_Access_Type (Parent_Type)
14465 then
14466 Error_Msg_N ("null exclusion can only apply to an access type", N);
14467 end if;
14468
14469 -- Avoid deriving parent primitives of underlying record views
14470
14471 Build_Derived_Type (N, Parent_Type, T, Is_Completion,
14472 Derive_Subps => not Is_Underlying_Record_View (T));
14473
14474 -- AI-419: The parent type of an explicitly limited derived type must
14475 -- be a limited type or a limited interface.
14476
14477 if Limited_Present (Def) then
14478 Set_Is_Limited_Record (T);
14479
14480 if Is_Interface (T) then
14481 Set_Is_Limited_Interface (T);
14482 end if;
14483
14484 if not Is_Limited_Type (Parent_Type)
14485 and then
14486 (not Is_Interface (Parent_Type)
14487 or else not Is_Limited_Interface (Parent_Type))
14488 then
14489 -- AI05-0096: a derivation in the private part of an instance is
14490 -- legal if the generic formal is untagged limited, and the actual
14491 -- is non-limited.
14492
14493 if Is_Generic_Actual_Type (Parent_Type)
14494 and then In_Private_Part (Current_Scope)
14495 and then
14496 not Is_Tagged_Type
14497 (Generic_Parent_Type (Parent (Parent_Type)))
14498 then
14499 null;
14500
14501 else
14502 Error_Msg_NE
14503 ("parent type& of limited type must be limited",
14504 N, Parent_Type);
14505 end if;
14506 end if;
14507 end if;
14508
14509 -- In SPARK, there are no derived type definitions other than type
14510 -- extensions of tagged record types.
14511
14512 if No (Extension) then
14513 Check_SPARK_Restriction ("derived type is not allowed", N);
14514 end if;
14515 end Derived_Type_Declaration;
14516
14517 ------------------------
14518 -- Diagnose_Interface --
14519 ------------------------
14520
14521 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id) is
14522 begin
14523 if not Is_Interface (E)
14524 and then E /= Any_Type
14525 then
14526 Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
14527 end if;
14528 end Diagnose_Interface;
14529
14530 ----------------------------------
14531 -- Enumeration_Type_Declaration --
14532 ----------------------------------
14533
14534 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
14535 Ev : Uint;
14536 L : Node_Id;
14537 R_Node : Node_Id;
14538 B_Node : Node_Id;
14539
14540 begin
14541 -- Create identifier node representing lower bound
14542
14543 B_Node := New_Node (N_Identifier, Sloc (Def));
14544 L := First (Literals (Def));
14545 Set_Chars (B_Node, Chars (L));
14546 Set_Entity (B_Node, L);
14547 Set_Etype (B_Node, T);
14548 Set_Is_Static_Expression (B_Node, True);
14549
14550 R_Node := New_Node (N_Range, Sloc (Def));
14551 Set_Low_Bound (R_Node, B_Node);
14552
14553 Set_Ekind (T, E_Enumeration_Type);
14554 Set_First_Literal (T, L);
14555 Set_Etype (T, T);
14556 Set_Is_Constrained (T);
14557
14558 Ev := Uint_0;
14559
14560 -- Loop through literals of enumeration type setting pos and rep values
14561 -- except that if the Ekind is already set, then it means the literal
14562 -- was already constructed (case of a derived type declaration and we
14563 -- should not disturb the Pos and Rep values.
14564
14565 while Present (L) loop
14566 if Ekind (L) /= E_Enumeration_Literal then
14567 Set_Ekind (L, E_Enumeration_Literal);
14568 Set_Enumeration_Pos (L, Ev);
14569 Set_Enumeration_Rep (L, Ev);
14570 Set_Is_Known_Valid (L, True);
14571 end if;
14572
14573 Set_Etype (L, T);
14574 New_Overloaded_Entity (L);
14575 Generate_Definition (L);
14576 Set_Convention (L, Convention_Intrinsic);
14577
14578 -- Case of character literal
14579
14580 if Nkind (L) = N_Defining_Character_Literal then
14581 Set_Is_Character_Type (T, True);
14582
14583 -- Check violation of No_Wide_Characters
14584
14585 if Restriction_Check_Required (No_Wide_Characters) then
14586 Get_Name_String (Chars (L));
14587
14588 if Name_Len >= 3 and then Name_Buffer (1 .. 2) = "QW" then
14589 Check_Restriction (No_Wide_Characters, L);
14590 end if;
14591 end if;
14592 end if;
14593
14594 Ev := Ev + 1;
14595 Next (L);
14596 end loop;
14597
14598 -- Now create a node representing upper bound
14599
14600 B_Node := New_Node (N_Identifier, Sloc (Def));
14601 Set_Chars (B_Node, Chars (Last (Literals (Def))));
14602 Set_Entity (B_Node, Last (Literals (Def)));
14603 Set_Etype (B_Node, T);
14604 Set_Is_Static_Expression (B_Node, True);
14605
14606 Set_High_Bound (R_Node, B_Node);
14607
14608 -- Initialize various fields of the type. Some of this information
14609 -- may be overwritten later through rep.clauses.
14610
14611 Set_Scalar_Range (T, R_Node);
14612 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
14613 Set_Enum_Esize (T);
14614 Set_Enum_Pos_To_Rep (T, Empty);
14615
14616 -- Set Discard_Names if configuration pragma set, or if there is
14617 -- a parameterless pragma in the current declarative region
14618
14619 if Global_Discard_Names
14620 or else Discard_Names (Scope (T))
14621 then
14622 Set_Discard_Names (T);
14623 end if;
14624
14625 -- Process end label if there is one
14626
14627 if Present (Def) then
14628 Process_End_Label (Def, 'e', T);
14629 end if;
14630 end Enumeration_Type_Declaration;
14631
14632 ---------------------------------
14633 -- Expand_To_Stored_Constraint --
14634 ---------------------------------
14635
14636 function Expand_To_Stored_Constraint
14637 (Typ : Entity_Id;
14638 Constraint : Elist_Id) return Elist_Id
14639 is
14640 Explicitly_Discriminated_Type : Entity_Id;
14641 Expansion : Elist_Id;
14642 Discriminant : Entity_Id;
14643
14644 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
14645 -- Find the nearest type that actually specifies discriminants
14646
14647 ---------------------------------
14648 -- Type_With_Explicit_Discrims --
14649 ---------------------------------
14650
14651 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
14652 Typ : constant E := Base_Type (Id);
14653
14654 begin
14655 if Ekind (Typ) in Incomplete_Or_Private_Kind then
14656 if Present (Full_View (Typ)) then
14657 return Type_With_Explicit_Discrims (Full_View (Typ));
14658 end if;
14659
14660 else
14661 if Has_Discriminants (Typ) then
14662 return Typ;
14663 end if;
14664 end if;
14665
14666 if Etype (Typ) = Typ then
14667 return Empty;
14668 elsif Has_Discriminants (Typ) then
14669 return Typ;
14670 else
14671 return Type_With_Explicit_Discrims (Etype (Typ));
14672 end if;
14673
14674 end Type_With_Explicit_Discrims;
14675
14676 -- Start of processing for Expand_To_Stored_Constraint
14677
14678 begin
14679 if No (Constraint)
14680 or else Is_Empty_Elmt_List (Constraint)
14681 then
14682 return No_Elist;
14683 end if;
14684
14685 Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
14686
14687 if No (Explicitly_Discriminated_Type) then
14688 return No_Elist;
14689 end if;
14690
14691 Expansion := New_Elmt_List;
14692
14693 Discriminant :=
14694 First_Stored_Discriminant (Explicitly_Discriminated_Type);
14695 while Present (Discriminant) loop
14696 Append_Elmt (
14697 Get_Discriminant_Value (
14698 Discriminant, Explicitly_Discriminated_Type, Constraint),
14699 Expansion);
14700 Next_Stored_Discriminant (Discriminant);
14701 end loop;
14702
14703 return Expansion;
14704 end Expand_To_Stored_Constraint;
14705
14706 ---------------------------
14707 -- Find_Hidden_Interface --
14708 ---------------------------
14709
14710 function Find_Hidden_Interface
14711 (Src : Elist_Id;
14712 Dest : Elist_Id) return Entity_Id
14713 is
14714 Iface : Entity_Id;
14715 Iface_Elmt : Elmt_Id;
14716
14717 begin
14718 if Present (Src) and then Present (Dest) then
14719 Iface_Elmt := First_Elmt (Src);
14720 while Present (Iface_Elmt) loop
14721 Iface := Node (Iface_Elmt);
14722
14723 if Is_Interface (Iface)
14724 and then not Contain_Interface (Iface, Dest)
14725 then
14726 return Iface;
14727 end if;
14728
14729 Next_Elmt (Iface_Elmt);
14730 end loop;
14731 end if;
14732
14733 return Empty;
14734 end Find_Hidden_Interface;
14735
14736 --------------------
14737 -- Find_Type_Name --
14738 --------------------
14739
14740 function Find_Type_Name (N : Node_Id) return Entity_Id is
14741 Id : constant Entity_Id := Defining_Identifier (N);
14742 Prev : Entity_Id;
14743 New_Id : Entity_Id;
14744 Prev_Par : Node_Id;
14745
14746 procedure Tag_Mismatch;
14747 -- Diagnose a tagged partial view whose full view is untagged.
14748 -- We post the message on the full view, with a reference to
14749 -- the previous partial view. The partial view can be private
14750 -- or incomplete, and these are handled in a different manner,
14751 -- so we determine the position of the error message from the
14752 -- respective slocs of both.
14753
14754 ------------------
14755 -- Tag_Mismatch --
14756 ------------------
14757
14758 procedure Tag_Mismatch is
14759 begin
14760 if Sloc (Prev) < Sloc (Id) then
14761 if Ada_Version >= Ada_2012
14762 and then Nkind (N) = N_Private_Type_Declaration
14763 then
14764 Error_Msg_NE
14765 ("declaration of private } must be a tagged type ", Id, Prev);
14766 else
14767 Error_Msg_NE
14768 ("full declaration of } must be a tagged type ", Id, Prev);
14769 end if;
14770 else
14771 if Ada_Version >= Ada_2012
14772 and then Nkind (N) = N_Private_Type_Declaration
14773 then
14774 Error_Msg_NE
14775 ("declaration of private } must be a tagged type ", Prev, Id);
14776 else
14777 Error_Msg_NE
14778 ("full declaration of } must be a tagged type ", Prev, Id);
14779 end if;
14780 end if;
14781 end Tag_Mismatch;
14782
14783 -- Start of processing for Find_Type_Name
14784
14785 begin
14786 -- Find incomplete declaration, if one was given
14787
14788 Prev := Current_Entity_In_Scope (Id);
14789
14790 -- New type declaration
14791
14792 if No (Prev) then
14793 Enter_Name (Id);
14794 return Id;
14795
14796 -- Previous declaration exists
14797
14798 else
14799 Prev_Par := Parent (Prev);
14800
14801 -- Error if not incomplete/private case except if previous
14802 -- declaration is implicit, etc. Enter_Name will emit error if
14803 -- appropriate.
14804
14805 if not Is_Incomplete_Or_Private_Type (Prev) then
14806 Enter_Name (Id);
14807 New_Id := Id;
14808
14809 -- Check invalid completion of private or incomplete type
14810
14811 elsif not Nkind_In (N, N_Full_Type_Declaration,
14812 N_Task_Type_Declaration,
14813 N_Protected_Type_Declaration)
14814 and then
14815 (Ada_Version < Ada_2012
14816 or else not Is_Incomplete_Type (Prev)
14817 or else not Nkind_In (N, N_Private_Type_Declaration,
14818 N_Private_Extension_Declaration))
14819 then
14820 -- Completion must be a full type declarations (RM 7.3(4))
14821
14822 Error_Msg_Sloc := Sloc (Prev);
14823 Error_Msg_NE ("invalid completion of }", Id, Prev);
14824
14825 -- Set scope of Id to avoid cascaded errors. Entity is never
14826 -- examined again, except when saving globals in generics.
14827
14828 Set_Scope (Id, Current_Scope);
14829 New_Id := Id;
14830
14831 -- If this is a repeated incomplete declaration, no further
14832 -- checks are possible.
14833
14834 if Nkind (N) = N_Incomplete_Type_Declaration then
14835 return Prev;
14836 end if;
14837
14838 -- Case of full declaration of incomplete type
14839
14840 elsif Ekind (Prev) = E_Incomplete_Type
14841 and then (Ada_Version < Ada_2012
14842 or else No (Full_View (Prev))
14843 or else not Is_Private_Type (Full_View (Prev)))
14844 then
14845
14846 -- Indicate that the incomplete declaration has a matching full
14847 -- declaration. The defining occurrence of the incomplete
14848 -- declaration remains the visible one, and the procedure
14849 -- Get_Full_View dereferences it whenever the type is used.
14850
14851 if Present (Full_View (Prev)) then
14852 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
14853 end if;
14854
14855 Set_Full_View (Prev, Id);
14856 Append_Entity (Id, Current_Scope);
14857 Set_Is_Public (Id, Is_Public (Prev));
14858 Set_Is_Internal (Id);
14859 New_Id := Prev;
14860
14861 -- If the incomplete view is tagged, a class_wide type has been
14862 -- created already. Use it for the private type as well, in order
14863 -- to prevent multiple incompatible class-wide types that may be
14864 -- created for self-referential anonymous access components.
14865
14866 if Is_Tagged_Type (Prev)
14867 and then Present (Class_Wide_Type (Prev))
14868 then
14869 Set_Ekind (Id, Ekind (Prev)); -- will be reset later
14870 Set_Class_Wide_Type (Id, Class_Wide_Type (Prev));
14871 Set_Etype (Class_Wide_Type (Id), Id);
14872 end if;
14873
14874 -- Case of full declaration of private type
14875
14876 else
14877 -- If the private type was a completion of an incomplete type then
14878 -- update Prev to reference the private type
14879
14880 if Ada_Version >= Ada_2012
14881 and then Ekind (Prev) = E_Incomplete_Type
14882 and then Present (Full_View (Prev))
14883 and then Is_Private_Type (Full_View (Prev))
14884 then
14885 Prev := Full_View (Prev);
14886 Prev_Par := Parent (Prev);
14887 end if;
14888
14889 if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
14890 if Etype (Prev) /= Prev then
14891
14892 -- Prev is a private subtype or a derived type, and needs
14893 -- no completion.
14894
14895 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
14896 New_Id := Id;
14897
14898 elsif Ekind (Prev) = E_Private_Type
14899 and then Nkind_In (N, N_Task_Type_Declaration,
14900 N_Protected_Type_Declaration)
14901 then
14902 Error_Msg_N
14903 ("completion of nonlimited type cannot be limited", N);
14904
14905 elsif Ekind (Prev) = E_Record_Type_With_Private
14906 and then Nkind_In (N, N_Task_Type_Declaration,
14907 N_Protected_Type_Declaration)
14908 then
14909 if not Is_Limited_Record (Prev) then
14910 Error_Msg_N
14911 ("completion of nonlimited type cannot be limited", N);
14912
14913 elsif No (Interface_List (N)) then
14914 Error_Msg_N
14915 ("completion of tagged private type must be tagged",
14916 N);
14917 end if;
14918
14919 elsif Nkind (N) = N_Full_Type_Declaration
14920 and then
14921 Nkind (Type_Definition (N)) = N_Record_Definition
14922 and then Interface_Present (Type_Definition (N))
14923 then
14924 Error_Msg_N
14925 ("completion of private type cannot be an interface", N);
14926 end if;
14927
14928 -- Ada 2005 (AI-251): Private extension declaration of a task
14929 -- type or a protected type. This case arises when covering
14930 -- interface types.
14931
14932 elsif Nkind_In (N, N_Task_Type_Declaration,
14933 N_Protected_Type_Declaration)
14934 then
14935 null;
14936
14937 elsif Nkind (N) /= N_Full_Type_Declaration
14938 or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
14939 then
14940 Error_Msg_N
14941 ("full view of private extension must be an extension", N);
14942
14943 elsif not (Abstract_Present (Parent (Prev)))
14944 and then Abstract_Present (Type_Definition (N))
14945 then
14946 Error_Msg_N
14947 ("full view of non-abstract extension cannot be abstract", N);
14948 end if;
14949
14950 if not In_Private_Part (Current_Scope) then
14951 Error_Msg_N
14952 ("declaration of full view must appear in private part", N);
14953 end if;
14954
14955 Copy_And_Swap (Prev, Id);
14956 Set_Has_Private_Declaration (Prev);
14957 Set_Has_Private_Declaration (Id);
14958
14959 -- Preserve aspect and iterator flags that may have been set on
14960 -- the partial view.
14961
14962 Set_Has_Delayed_Aspects (Prev, Has_Delayed_Aspects (Id));
14963 Set_Has_Implicit_Dereference (Prev, Has_Implicit_Dereference (Id));
14964
14965 -- If no error, propagate freeze_node from private to full view.
14966 -- It may have been generated for an early operational item.
14967
14968 if Present (Freeze_Node (Id))
14969 and then Serious_Errors_Detected = 0
14970 and then No (Full_View (Id))
14971 then
14972 Set_Freeze_Node (Prev, Freeze_Node (Id));
14973 Set_Freeze_Node (Id, Empty);
14974 Set_First_Rep_Item (Prev, First_Rep_Item (Id));
14975 end if;
14976
14977 Set_Full_View (Id, Prev);
14978 New_Id := Prev;
14979 end if;
14980
14981 -- Verify that full declaration conforms to partial one
14982
14983 if Is_Incomplete_Or_Private_Type (Prev)
14984 and then Present (Discriminant_Specifications (Prev_Par))
14985 then
14986 if Present (Discriminant_Specifications (N)) then
14987 if Ekind (Prev) = E_Incomplete_Type then
14988 Check_Discriminant_Conformance (N, Prev, Prev);
14989 else
14990 Check_Discriminant_Conformance (N, Prev, Id);
14991 end if;
14992
14993 else
14994 Error_Msg_N
14995 ("missing discriminants in full type declaration", N);
14996
14997 -- To avoid cascaded errors on subsequent use, share the
14998 -- discriminants of the partial view.
14999
15000 Set_Discriminant_Specifications (N,
15001 Discriminant_Specifications (Prev_Par));
15002 end if;
15003 end if;
15004
15005 -- A prior untagged partial view can have an associated class-wide
15006 -- type due to use of the class attribute, and in this case the full
15007 -- type must also be tagged. This Ada 95 usage is deprecated in favor
15008 -- of incomplete tagged declarations, but we check for it.
15009
15010 if Is_Type (Prev)
15011 and then (Is_Tagged_Type (Prev)
15012 or else Present (Class_Wide_Type (Prev)))
15013 then
15014 -- Ada 2012 (AI05-0162): A private type may be the completion of
15015 -- an incomplete type
15016
15017 if Ada_Version >= Ada_2012
15018 and then Is_Incomplete_Type (Prev)
15019 and then Nkind_In (N, N_Private_Type_Declaration,
15020 N_Private_Extension_Declaration)
15021 then
15022 -- No need to check private extensions since they are tagged
15023
15024 if Nkind (N) = N_Private_Type_Declaration
15025 and then not Tagged_Present (N)
15026 then
15027 Tag_Mismatch;
15028 end if;
15029
15030 -- The full declaration is either a tagged type (including
15031 -- a synchronized type that implements interfaces) or a
15032 -- type extension, otherwise this is an error.
15033
15034 elsif Nkind_In (N, N_Task_Type_Declaration,
15035 N_Protected_Type_Declaration)
15036 then
15037 if No (Interface_List (N))
15038 and then not Error_Posted (N)
15039 then
15040 Tag_Mismatch;
15041 end if;
15042
15043 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
15044
15045 -- Indicate that the previous declaration (tagged incomplete
15046 -- or private declaration) requires the same on the full one.
15047
15048 if not Tagged_Present (Type_Definition (N)) then
15049 Tag_Mismatch;
15050 Set_Is_Tagged_Type (Id);
15051 end if;
15052
15053 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
15054 if No (Record_Extension_Part (Type_Definition (N))) then
15055 Error_Msg_NE
15056 ("full declaration of } must be a record extension",
15057 Prev, Id);
15058
15059 -- Set some attributes to produce a usable full view
15060
15061 Set_Is_Tagged_Type (Id);
15062 end if;
15063
15064 else
15065 Tag_Mismatch;
15066 end if;
15067 end if;
15068
15069 if Present (Prev)
15070 and then Nkind (Parent (Prev)) = N_Incomplete_Type_Declaration
15071 and then Present (Premature_Use (Parent (Prev)))
15072 then
15073 Error_Msg_Sloc := Sloc (N);
15074 Error_Msg_N
15075 ("\full declaration #", Premature_Use (Parent (Prev)));
15076 end if;
15077
15078 return New_Id;
15079 end if;
15080 end Find_Type_Name;
15081
15082 -------------------------
15083 -- Find_Type_Of_Object --
15084 -------------------------
15085
15086 function Find_Type_Of_Object
15087 (Obj_Def : Node_Id;
15088 Related_Nod : Node_Id) return Entity_Id
15089 is
15090 Def_Kind : constant Node_Kind := Nkind (Obj_Def);
15091 P : Node_Id := Parent (Obj_Def);
15092 T : Entity_Id;
15093 Nam : Name_Id;
15094
15095 begin
15096 -- If the parent is a component_definition node we climb to the
15097 -- component_declaration node
15098
15099 if Nkind (P) = N_Component_Definition then
15100 P := Parent (P);
15101 end if;
15102
15103 -- Case of an anonymous array subtype
15104
15105 if Nkind_In (Def_Kind, N_Constrained_Array_Definition,
15106 N_Unconstrained_Array_Definition)
15107 then
15108 T := Empty;
15109 Array_Type_Declaration (T, Obj_Def);
15110
15111 -- Create an explicit subtype whenever possible
15112
15113 elsif Nkind (P) /= N_Component_Declaration
15114 and then Def_Kind = N_Subtype_Indication
15115 then
15116 -- Base name of subtype on object name, which will be unique in
15117 -- the current scope.
15118
15119 -- If this is a duplicate declaration, return base type, to avoid
15120 -- generating duplicate anonymous types.
15121
15122 if Error_Posted (P) then
15123 Analyze (Subtype_Mark (Obj_Def));
15124 return Entity (Subtype_Mark (Obj_Def));
15125 end if;
15126
15127 Nam :=
15128 New_External_Name
15129 (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
15130
15131 T := Make_Defining_Identifier (Sloc (P), Nam);
15132
15133 Insert_Action (Obj_Def,
15134 Make_Subtype_Declaration (Sloc (P),
15135 Defining_Identifier => T,
15136 Subtype_Indication => Relocate_Node (Obj_Def)));
15137
15138 -- This subtype may need freezing, and this will not be done
15139 -- automatically if the object declaration is not in declarative
15140 -- part. Since this is an object declaration, the type cannot always
15141 -- be frozen here. Deferred constants do not freeze their type
15142 -- (which often enough will be private).
15143
15144 if Nkind (P) = N_Object_Declaration
15145 and then Constant_Present (P)
15146 and then No (Expression (P))
15147 then
15148 null;
15149 else
15150 Insert_Actions (Obj_Def, Freeze_Entity (T, P));
15151 end if;
15152
15153 -- Ada 2005 AI-406: the object definition in an object declaration
15154 -- can be an access definition.
15155
15156 elsif Def_Kind = N_Access_Definition then
15157 T := Access_Definition (Related_Nod, Obj_Def);
15158
15159 Set_Is_Local_Anonymous_Access
15160 (T,
15161 V => (Ada_Version < Ada_2012)
15162 or else (Nkind (P) /= N_Object_Declaration)
15163 or else Is_Library_Level_Entity (Defining_Identifier (P)));
15164
15165 -- Otherwise, the object definition is just a subtype_mark
15166
15167 else
15168 T := Process_Subtype (Obj_Def, Related_Nod);
15169
15170 -- If expansion is disabled an object definition that is an aggregate
15171 -- will not get expanded and may lead to scoping problems in the back
15172 -- end, if the object is referenced in an inner scope. In that case
15173 -- create an itype reference for the object definition now. This
15174 -- may be redundant in some cases, but harmless.
15175
15176 if Is_Itype (T)
15177 and then Nkind (Related_Nod) = N_Object_Declaration
15178 and then ASIS_Mode
15179 then
15180 Build_Itype_Reference (T, Related_Nod);
15181 end if;
15182 end if;
15183
15184 return T;
15185 end Find_Type_Of_Object;
15186
15187 --------------------------------
15188 -- Find_Type_Of_Subtype_Indic --
15189 --------------------------------
15190
15191 function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
15192 Typ : Entity_Id;
15193
15194 begin
15195 -- Case of subtype mark with a constraint
15196
15197 if Nkind (S) = N_Subtype_Indication then
15198 Find_Type (Subtype_Mark (S));
15199 Typ := Entity (Subtype_Mark (S));
15200
15201 if not
15202 Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
15203 then
15204 Error_Msg_N
15205 ("incorrect constraint for this kind of type", Constraint (S));
15206 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
15207 end if;
15208
15209 -- Otherwise we have a subtype mark without a constraint
15210
15211 elsif Error_Posted (S) then
15212 Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
15213 return Any_Type;
15214
15215 else
15216 Find_Type (S);
15217 Typ := Entity (S);
15218 end if;
15219
15220 -- Check No_Wide_Characters restriction
15221
15222 Check_Wide_Character_Restriction (Typ, S);
15223
15224 return Typ;
15225 end Find_Type_Of_Subtype_Indic;
15226
15227 -------------------------------------
15228 -- Floating_Point_Type_Declaration --
15229 -------------------------------------
15230
15231 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
15232 Digs : constant Node_Id := Digits_Expression (Def);
15233 Max_Digs_Val : constant Uint := Digits_Value (Standard_Long_Long_Float);
15234 Digs_Val : Uint;
15235 Base_Typ : Entity_Id;
15236 Implicit_Base : Entity_Id;
15237 Bound : Node_Id;
15238
15239 function Can_Derive_From (E : Entity_Id) return Boolean;
15240 -- Find if given digits value, and possibly a specified range, allows
15241 -- derivation from specified type
15242
15243 function Find_Base_Type return Entity_Id;
15244 -- Find a predefined base type that Def can derive from, or generate
15245 -- an error and substitute Long_Long_Float if none exists.
15246
15247 ---------------------
15248 -- Can_Derive_From --
15249 ---------------------
15250
15251 function Can_Derive_From (E : Entity_Id) return Boolean is
15252 Spec : constant Entity_Id := Real_Range_Specification (Def);
15253
15254 begin
15255 if Digs_Val > Digits_Value (E) then
15256 return False;
15257 end if;
15258
15259 if Present (Spec) then
15260 if Expr_Value_R (Type_Low_Bound (E)) >
15261 Expr_Value_R (Low_Bound (Spec))
15262 then
15263 return False;
15264 end if;
15265
15266 if Expr_Value_R (Type_High_Bound (E)) <
15267 Expr_Value_R (High_Bound (Spec))
15268 then
15269 return False;
15270 end if;
15271 end if;
15272
15273 return True;
15274 end Can_Derive_From;
15275
15276 --------------------
15277 -- Find_Base_Type --
15278 --------------------
15279
15280 function Find_Base_Type return Entity_Id is
15281 Choice : Elmt_Id := First_Elmt (Predefined_Float_Types);
15282
15283 begin
15284 -- Iterate over the predefined types in order, returning the first
15285 -- one that Def can derive from.
15286
15287 while Present (Choice) loop
15288 if Can_Derive_From (Node (Choice)) then
15289 return Node (Choice);
15290 end if;
15291
15292 Next_Elmt (Choice);
15293 end loop;
15294
15295 -- If we can't derive from any existing type, use Long_Long_Float
15296 -- and give appropriate message explaining the problem.
15297
15298 if Digs_Val > Max_Digs_Val then
15299 -- It might be the case that there is a type with the requested
15300 -- range, just not the combination of digits and range.
15301
15302 Error_Msg_N
15303 ("no predefined type has requested range and precision",
15304 Real_Range_Specification (Def));
15305
15306 else
15307 Error_Msg_N
15308 ("range too large for any predefined type",
15309 Real_Range_Specification (Def));
15310 end if;
15311
15312 return Standard_Long_Long_Float;
15313 end Find_Base_Type;
15314
15315 -- Start of processing for Floating_Point_Type_Declaration
15316
15317 begin
15318 Check_Restriction (No_Floating_Point, Def);
15319
15320 -- Create an implicit base type
15321
15322 Implicit_Base :=
15323 Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
15324
15325 -- Analyze and verify digits value
15326
15327 Analyze_And_Resolve (Digs, Any_Integer);
15328 Check_Digits_Expression (Digs);
15329 Digs_Val := Expr_Value (Digs);
15330
15331 -- Process possible range spec and find correct type to derive from
15332
15333 Process_Real_Range_Specification (Def);
15334
15335 -- Check that requested number of digits is not too high.
15336
15337 if Digs_Val > Max_Digs_Val then
15338 -- The check for Max_Base_Digits may be somewhat expensive, as it
15339 -- requires reading System, so only do it when necessary.
15340
15341 declare
15342 Max_Base_Digits : constant Uint :=
15343 Expr_Value
15344 (Expression
15345 (Parent (RTE (RE_Max_Base_Digits))));
15346
15347 begin
15348 if Digs_Val > Max_Base_Digits then
15349 Error_Msg_Uint_1 := Max_Base_Digits;
15350 Error_Msg_N ("digits value out of range, maximum is ^", Digs);
15351
15352 elsif No (Real_Range_Specification (Def)) then
15353 Error_Msg_Uint_1 := Max_Digs_Val;
15354 Error_Msg_N ("types with more than ^ digits need range spec "
15355 & "(RM 3.5.7(6))", Digs);
15356 end if;
15357 end;
15358 end if;
15359
15360 -- Find a suitable type to derive from or complain and use a substitute
15361
15362 Base_Typ := Find_Base_Type;
15363
15364 -- If there are bounds given in the declaration use them as the bounds
15365 -- of the type, otherwise use the bounds of the predefined base type
15366 -- that was chosen based on the Digits value.
15367
15368 if Present (Real_Range_Specification (Def)) then
15369 Set_Scalar_Range (T, Real_Range_Specification (Def));
15370 Set_Is_Constrained (T);
15371
15372 -- The bounds of this range must be converted to machine numbers
15373 -- in accordance with RM 4.9(38).
15374
15375 Bound := Type_Low_Bound (T);
15376
15377 if Nkind (Bound) = N_Real_Literal then
15378 Set_Realval
15379 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
15380 Set_Is_Machine_Number (Bound);
15381 end if;
15382
15383 Bound := Type_High_Bound (T);
15384
15385 if Nkind (Bound) = N_Real_Literal then
15386 Set_Realval
15387 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
15388 Set_Is_Machine_Number (Bound);
15389 end if;
15390
15391 else
15392 Set_Scalar_Range (T, Scalar_Range (Base_Typ));
15393 end if;
15394
15395 -- Complete definition of implicit base and declared first subtype
15396
15397 Set_Etype (Implicit_Base, Base_Typ);
15398
15399 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
15400 Set_Size_Info (Implicit_Base, (Base_Typ));
15401 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
15402 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
15403 Set_Digits_Value (Implicit_Base, Digits_Value (Base_Typ));
15404 Set_Float_Rep (Implicit_Base, Float_Rep (Base_Typ));
15405
15406 Set_Ekind (T, E_Floating_Point_Subtype);
15407 Set_Etype (T, Implicit_Base);
15408
15409 Set_Size_Info (T, (Implicit_Base));
15410 Set_RM_Size (T, RM_Size (Implicit_Base));
15411 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
15412 Set_Digits_Value (T, Digs_Val);
15413 end Floating_Point_Type_Declaration;
15414
15415 ----------------------------
15416 -- Get_Discriminant_Value --
15417 ----------------------------
15418
15419 -- This is the situation:
15420
15421 -- There is a non-derived type
15422
15423 -- type T0 (Dx, Dy, Dz...)
15424
15425 -- There are zero or more levels of derivation, with each derivation
15426 -- either purely inheriting the discriminants, or defining its own.
15427
15428 -- type Ti is new Ti-1
15429 -- or
15430 -- type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
15431 -- or
15432 -- subtype Ti is ...
15433
15434 -- The subtype issue is avoided by the use of Original_Record_Component,
15435 -- and the fact that derived subtypes also derive the constraints.
15436
15437 -- This chain leads back from
15438
15439 -- Typ_For_Constraint
15440
15441 -- Typ_For_Constraint has discriminants, and the value for each
15442 -- discriminant is given by its corresponding Elmt of Constraints.
15443
15444 -- Discriminant is some discriminant in this hierarchy
15445
15446 -- We need to return its value
15447
15448 -- We do this by recursively searching each level, and looking for
15449 -- Discriminant. Once we get to the bottom, we start backing up
15450 -- returning the value for it which may in turn be a discriminant
15451 -- further up, so on the backup we continue the substitution.
15452
15453 function Get_Discriminant_Value
15454 (Discriminant : Entity_Id;
15455 Typ_For_Constraint : Entity_Id;
15456 Constraint : Elist_Id) return Node_Id
15457 is
15458 function Search_Derivation_Levels
15459 (Ti : Entity_Id;
15460 Discrim_Values : Elist_Id;
15461 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
15462 -- This is the routine that performs the recursive search of levels
15463 -- as described above.
15464
15465 ------------------------------
15466 -- Search_Derivation_Levels --
15467 ------------------------------
15468
15469 function Search_Derivation_Levels
15470 (Ti : Entity_Id;
15471 Discrim_Values : Elist_Id;
15472 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
15473 is
15474 Assoc : Elmt_Id;
15475 Disc : Entity_Id;
15476 Result : Node_Or_Entity_Id;
15477 Result_Entity : Node_Id;
15478
15479 begin
15480 -- If inappropriate type, return Error, this happens only in
15481 -- cascaded error situations, and we want to avoid a blow up.
15482
15483 if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
15484 return Error;
15485 end if;
15486
15487 -- Look deeper if possible. Use Stored_Constraints only for
15488 -- untagged types. For tagged types use the given constraint.
15489 -- This asymmetry needs explanation???
15490
15491 if not Stored_Discrim_Values
15492 and then Present (Stored_Constraint (Ti))
15493 and then not Is_Tagged_Type (Ti)
15494 then
15495 Result :=
15496 Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
15497 else
15498 declare
15499 Td : constant Entity_Id := Etype (Ti);
15500
15501 begin
15502 if Td = Ti then
15503 Result := Discriminant;
15504
15505 else
15506 if Present (Stored_Constraint (Ti)) then
15507 Result :=
15508 Search_Derivation_Levels
15509 (Td, Stored_Constraint (Ti), True);
15510 else
15511 Result :=
15512 Search_Derivation_Levels
15513 (Td, Discrim_Values, Stored_Discrim_Values);
15514 end if;
15515 end if;
15516 end;
15517 end if;
15518
15519 -- Extra underlying places to search, if not found above. For
15520 -- concurrent types, the relevant discriminant appears in the
15521 -- corresponding record. For a type derived from a private type
15522 -- without discriminant, the full view inherits the discriminants
15523 -- of the full view of the parent.
15524
15525 if Result = Discriminant then
15526 if Is_Concurrent_Type (Ti)
15527 and then Present (Corresponding_Record_Type (Ti))
15528 then
15529 Result :=
15530 Search_Derivation_Levels (
15531 Corresponding_Record_Type (Ti),
15532 Discrim_Values,
15533 Stored_Discrim_Values);
15534
15535 elsif Is_Private_Type (Ti)
15536 and then not Has_Discriminants (Ti)
15537 and then Present (Full_View (Ti))
15538 and then Etype (Full_View (Ti)) /= Ti
15539 then
15540 Result :=
15541 Search_Derivation_Levels (
15542 Full_View (Ti),
15543 Discrim_Values,
15544 Stored_Discrim_Values);
15545 end if;
15546 end if;
15547
15548 -- If Result is not a (reference to a) discriminant, return it,
15549 -- otherwise set Result_Entity to the discriminant.
15550
15551 if Nkind (Result) = N_Defining_Identifier then
15552 pragma Assert (Result = Discriminant);
15553 Result_Entity := Result;
15554
15555 else
15556 if not Denotes_Discriminant (Result) then
15557 return Result;
15558 end if;
15559
15560 Result_Entity := Entity (Result);
15561 end if;
15562
15563 -- See if this level of derivation actually has discriminants
15564 -- because tagged derivations can add them, hence the lower
15565 -- levels need not have any.
15566
15567 if not Has_Discriminants (Ti) then
15568 return Result;
15569 end if;
15570
15571 -- Scan Ti's discriminants for Result_Entity,
15572 -- and return its corresponding value, if any.
15573
15574 Result_Entity := Original_Record_Component (Result_Entity);
15575
15576 Assoc := First_Elmt (Discrim_Values);
15577
15578 if Stored_Discrim_Values then
15579 Disc := First_Stored_Discriminant (Ti);
15580 else
15581 Disc := First_Discriminant (Ti);
15582 end if;
15583
15584 while Present (Disc) loop
15585 pragma Assert (Present (Assoc));
15586
15587 if Original_Record_Component (Disc) = Result_Entity then
15588 return Node (Assoc);
15589 end if;
15590
15591 Next_Elmt (Assoc);
15592
15593 if Stored_Discrim_Values then
15594 Next_Stored_Discriminant (Disc);
15595 else
15596 Next_Discriminant (Disc);
15597 end if;
15598 end loop;
15599
15600 -- Could not find it
15601 --
15602 return Result;
15603 end Search_Derivation_Levels;
15604
15605 -- Local Variables
15606
15607 Result : Node_Or_Entity_Id;
15608
15609 -- Start of processing for Get_Discriminant_Value
15610
15611 begin
15612 -- ??? This routine is a gigantic mess and will be deleted. For the
15613 -- time being just test for the trivial case before calling recurse.
15614
15615 if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
15616 declare
15617 D : Entity_Id;
15618 E : Elmt_Id;
15619
15620 begin
15621 D := First_Discriminant (Typ_For_Constraint);
15622 E := First_Elmt (Constraint);
15623 while Present (D) loop
15624 if Chars (D) = Chars (Discriminant) then
15625 return Node (E);
15626 end if;
15627
15628 Next_Discriminant (D);
15629 Next_Elmt (E);
15630 end loop;
15631 end;
15632 end if;
15633
15634 Result := Search_Derivation_Levels
15635 (Typ_For_Constraint, Constraint, False);
15636
15637 -- ??? hack to disappear when this routine is gone
15638
15639 if Nkind (Result) = N_Defining_Identifier then
15640 declare
15641 D : Entity_Id;
15642 E : Elmt_Id;
15643
15644 begin
15645 D := First_Discriminant (Typ_For_Constraint);
15646 E := First_Elmt (Constraint);
15647 while Present (D) loop
15648 if Corresponding_Discriminant (D) = Discriminant then
15649 return Node (E);
15650 end if;
15651
15652 Next_Discriminant (D);
15653 Next_Elmt (E);
15654 end loop;
15655 end;
15656 end if;
15657
15658 pragma Assert (Nkind (Result) /= N_Defining_Identifier);
15659 return Result;
15660 end Get_Discriminant_Value;
15661
15662 --------------------------
15663 -- Has_Range_Constraint --
15664 --------------------------
15665
15666 function Has_Range_Constraint (N : Node_Id) return Boolean is
15667 C : constant Node_Id := Constraint (N);
15668
15669 begin
15670 if Nkind (C) = N_Range_Constraint then
15671 return True;
15672
15673 elsif Nkind (C) = N_Digits_Constraint then
15674 return
15675 Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
15676 or else
15677 Present (Range_Constraint (C));
15678
15679 elsif Nkind (C) = N_Delta_Constraint then
15680 return Present (Range_Constraint (C));
15681
15682 else
15683 return False;
15684 end if;
15685 end Has_Range_Constraint;
15686
15687 ------------------------
15688 -- Inherit_Components --
15689 ------------------------
15690
15691 function Inherit_Components
15692 (N : Node_Id;
15693 Parent_Base : Entity_Id;
15694 Derived_Base : Entity_Id;
15695 Is_Tagged : Boolean;
15696 Inherit_Discr : Boolean;
15697 Discs : Elist_Id) return Elist_Id
15698 is
15699 Assoc_List : constant Elist_Id := New_Elmt_List;
15700
15701 procedure Inherit_Component
15702 (Old_C : Entity_Id;
15703 Plain_Discrim : Boolean := False;
15704 Stored_Discrim : Boolean := False);
15705 -- Inherits component Old_C from Parent_Base to the Derived_Base. If
15706 -- Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
15707 -- True, Old_C is a stored discriminant. If they are both false then
15708 -- Old_C is a regular component.
15709
15710 -----------------------
15711 -- Inherit_Component --
15712 -----------------------
15713
15714 procedure Inherit_Component
15715 (Old_C : Entity_Id;
15716 Plain_Discrim : Boolean := False;
15717 Stored_Discrim : Boolean := False)
15718 is
15719 procedure Set_Anonymous_Type (Id : Entity_Id);
15720 -- Id denotes the entity of an access discriminant or anonymous
15721 -- access component. Set the type of Id to either the same type of
15722 -- Old_C or create a new one depending on whether the parent and
15723 -- the child types are in the same scope.
15724
15725 ------------------------
15726 -- Set_Anonymous_Type --
15727 ------------------------
15728
15729 procedure Set_Anonymous_Type (Id : Entity_Id) is
15730 Old_Typ : constant Entity_Id := Etype (Old_C);
15731
15732 begin
15733 if Scope (Parent_Base) = Scope (Derived_Base) then
15734 Set_Etype (Id, Old_Typ);
15735
15736 -- The parent and the derived type are in two different scopes.
15737 -- Reuse the type of the original discriminant / component by
15738 -- copying it in order to preserve all attributes.
15739
15740 else
15741 declare
15742 Typ : constant Entity_Id := New_Copy (Old_Typ);
15743
15744 begin
15745 Set_Etype (Id, Typ);
15746
15747 -- Since we do not generate component declarations for
15748 -- inherited components, associate the itype with the
15749 -- derived type.
15750
15751 Set_Associated_Node_For_Itype (Typ, Parent (Derived_Base));
15752 Set_Scope (Typ, Derived_Base);
15753 end;
15754 end if;
15755 end Set_Anonymous_Type;
15756
15757 -- Local variables and constants
15758
15759 New_C : constant Entity_Id := New_Copy (Old_C);
15760
15761 Corr_Discrim : Entity_Id;
15762 Discrim : Entity_Id;
15763
15764 -- Start of processing for Inherit_Component
15765
15766 begin
15767 pragma Assert (not Is_Tagged or else not Stored_Discrim);
15768
15769 Set_Parent (New_C, Parent (Old_C));
15770
15771 -- Regular discriminants and components must be inserted in the scope
15772 -- of the Derived_Base. Do it here.
15773
15774 if not Stored_Discrim then
15775 Enter_Name (New_C);
15776 end if;
15777
15778 -- For tagged types the Original_Record_Component must point to
15779 -- whatever this field was pointing to in the parent type. This has
15780 -- already been achieved by the call to New_Copy above.
15781
15782 if not Is_Tagged then
15783 Set_Original_Record_Component (New_C, New_C);
15784 end if;
15785
15786 -- Set the proper type of an access discriminant
15787
15788 if Ekind (New_C) = E_Discriminant
15789 and then Ekind (Etype (New_C)) = E_Anonymous_Access_Type
15790 then
15791 Set_Anonymous_Type (New_C);
15792 end if;
15793
15794 -- If we have inherited a component then see if its Etype contains
15795 -- references to Parent_Base discriminants. In this case, replace
15796 -- these references with the constraints given in Discs. We do not
15797 -- do this for the partial view of private types because this is
15798 -- not needed (only the components of the full view will be used
15799 -- for code generation) and cause problem. We also avoid this
15800 -- transformation in some error situations.
15801
15802 if Ekind (New_C) = E_Component then
15803
15804 -- Set the proper type of an anonymous access component
15805
15806 if Ekind (Etype (New_C)) = E_Anonymous_Access_Type then
15807 Set_Anonymous_Type (New_C);
15808
15809 elsif (Is_Private_Type (Derived_Base)
15810 and then not Is_Generic_Type (Derived_Base))
15811 or else (Is_Empty_Elmt_List (Discs)
15812 and then not Expander_Active)
15813 then
15814 Set_Etype (New_C, Etype (Old_C));
15815
15816 else
15817 -- The current component introduces a circularity of the
15818 -- following kind:
15819
15820 -- limited with Pack_2;
15821 -- package Pack_1 is
15822 -- type T_1 is tagged record
15823 -- Comp : access Pack_2.T_2;
15824 -- ...
15825 -- end record;
15826 -- end Pack_1;
15827
15828 -- with Pack_1;
15829 -- package Pack_2 is
15830 -- type T_2 is new Pack_1.T_1 with ...;
15831 -- end Pack_2;
15832
15833 Set_Etype
15834 (New_C,
15835 Constrain_Component_Type
15836 (Old_C, Derived_Base, N, Parent_Base, Discs));
15837 end if;
15838 end if;
15839
15840 -- In derived tagged types it is illegal to reference a non
15841 -- discriminant component in the parent type. To catch this, mark
15842 -- these components with an Ekind of E_Void. This will be reset in
15843 -- Record_Type_Definition after processing the record extension of
15844 -- the derived type.
15845
15846 -- If the declaration is a private extension, there is no further
15847 -- record extension to process, and the components retain their
15848 -- current kind, because they are visible at this point.
15849
15850 if Is_Tagged and then Ekind (New_C) = E_Component
15851 and then Nkind (N) /= N_Private_Extension_Declaration
15852 then
15853 Set_Ekind (New_C, E_Void);
15854 end if;
15855
15856 if Plain_Discrim then
15857 Set_Corresponding_Discriminant (New_C, Old_C);
15858 Build_Discriminal (New_C);
15859
15860 -- If we are explicitly inheriting a stored discriminant it will be
15861 -- completely hidden.
15862
15863 elsif Stored_Discrim then
15864 Set_Corresponding_Discriminant (New_C, Empty);
15865 Set_Discriminal (New_C, Empty);
15866 Set_Is_Completely_Hidden (New_C);
15867
15868 -- Set the Original_Record_Component of each discriminant in the
15869 -- derived base to point to the corresponding stored that we just
15870 -- created.
15871
15872 Discrim := First_Discriminant (Derived_Base);
15873 while Present (Discrim) loop
15874 Corr_Discrim := Corresponding_Discriminant (Discrim);
15875
15876 -- Corr_Discrim could be missing in an error situation
15877
15878 if Present (Corr_Discrim)
15879 and then Original_Record_Component (Corr_Discrim) = Old_C
15880 then
15881 Set_Original_Record_Component (Discrim, New_C);
15882 end if;
15883
15884 Next_Discriminant (Discrim);
15885 end loop;
15886
15887 Append_Entity (New_C, Derived_Base);
15888 end if;
15889
15890 if not Is_Tagged then
15891 Append_Elmt (Old_C, Assoc_List);
15892 Append_Elmt (New_C, Assoc_List);
15893 end if;
15894 end Inherit_Component;
15895
15896 -- Variables local to Inherit_Component
15897
15898 Loc : constant Source_Ptr := Sloc (N);
15899
15900 Parent_Discrim : Entity_Id;
15901 Stored_Discrim : Entity_Id;
15902 D : Entity_Id;
15903 Component : Entity_Id;
15904
15905 -- Start of processing for Inherit_Components
15906
15907 begin
15908 if not Is_Tagged then
15909 Append_Elmt (Parent_Base, Assoc_List);
15910 Append_Elmt (Derived_Base, Assoc_List);
15911 end if;
15912
15913 -- Inherit parent discriminants if needed
15914
15915 if Inherit_Discr then
15916 Parent_Discrim := First_Discriminant (Parent_Base);
15917 while Present (Parent_Discrim) loop
15918 Inherit_Component (Parent_Discrim, Plain_Discrim => True);
15919 Next_Discriminant (Parent_Discrim);
15920 end loop;
15921 end if;
15922
15923 -- Create explicit stored discrims for untagged types when necessary
15924
15925 if not Has_Unknown_Discriminants (Derived_Base)
15926 and then Has_Discriminants (Parent_Base)
15927 and then not Is_Tagged
15928 and then
15929 (not Inherit_Discr
15930 or else First_Discriminant (Parent_Base) /=
15931 First_Stored_Discriminant (Parent_Base))
15932 then
15933 Stored_Discrim := First_Stored_Discriminant (Parent_Base);
15934 while Present (Stored_Discrim) loop
15935 Inherit_Component (Stored_Discrim, Stored_Discrim => True);
15936 Next_Stored_Discriminant (Stored_Discrim);
15937 end loop;
15938 end if;
15939
15940 -- See if we can apply the second transformation for derived types, as
15941 -- explained in point 6. in the comments above Build_Derived_Record_Type
15942 -- This is achieved by appending Derived_Base discriminants into Discs,
15943 -- which has the side effect of returning a non empty Discs list to the
15944 -- caller of Inherit_Components, which is what we want. This must be
15945 -- done for private derived types if there are explicit stored
15946 -- discriminants, to ensure that we can retrieve the values of the
15947 -- constraints provided in the ancestors.
15948
15949 if Inherit_Discr
15950 and then Is_Empty_Elmt_List (Discs)
15951 and then Present (First_Discriminant (Derived_Base))
15952 and then
15953 (not Is_Private_Type (Derived_Base)
15954 or else Is_Completely_Hidden
15955 (First_Stored_Discriminant (Derived_Base))
15956 or else Is_Generic_Type (Derived_Base))
15957 then
15958 D := First_Discriminant (Derived_Base);
15959 while Present (D) loop
15960 Append_Elmt (New_Reference_To (D, Loc), Discs);
15961 Next_Discriminant (D);
15962 end loop;
15963 end if;
15964
15965 -- Finally, inherit non-discriminant components unless they are not
15966 -- visible because defined or inherited from the full view of the
15967 -- parent. Don't inherit the _parent field of the parent type.
15968
15969 Component := First_Entity (Parent_Base);
15970 while Present (Component) loop
15971
15972 -- Ada 2005 (AI-251): Do not inherit components associated with
15973 -- secondary tags of the parent.
15974
15975 if Ekind (Component) = E_Component
15976 and then Present (Related_Type (Component))
15977 then
15978 null;
15979
15980 elsif Ekind (Component) /= E_Component
15981 or else Chars (Component) = Name_uParent
15982 then
15983 null;
15984
15985 -- If the derived type is within the parent type's declarative
15986 -- region, then the components can still be inherited even though
15987 -- they aren't visible at this point. This can occur for cases
15988 -- such as within public child units where the components must
15989 -- become visible upon entering the child unit's private part.
15990
15991 elsif not Is_Visible_Component (Component)
15992 and then not In_Open_Scopes (Scope (Parent_Base))
15993 then
15994 null;
15995
15996 elsif Ekind_In (Derived_Base, E_Private_Type,
15997 E_Limited_Private_Type)
15998 then
15999 null;
16000
16001 else
16002 Inherit_Component (Component);
16003 end if;
16004
16005 Next_Entity (Component);
16006 end loop;
16007
16008 -- For tagged derived types, inherited discriminants cannot be used in
16009 -- component declarations of the record extension part. To achieve this
16010 -- we mark the inherited discriminants as not visible.
16011
16012 if Is_Tagged and then Inherit_Discr then
16013 D := First_Discriminant (Derived_Base);
16014 while Present (D) loop
16015 Set_Is_Immediately_Visible (D, False);
16016 Next_Discriminant (D);
16017 end loop;
16018 end if;
16019
16020 return Assoc_List;
16021 end Inherit_Components;
16022
16023 -----------------------
16024 -- Is_Constant_Bound --
16025 -----------------------
16026
16027 function Is_Constant_Bound (Exp : Node_Id) return Boolean is
16028 begin
16029 if Compile_Time_Known_Value (Exp) then
16030 return True;
16031
16032 elsif Is_Entity_Name (Exp)
16033 and then Present (Entity (Exp))
16034 then
16035 return Is_Constant_Object (Entity (Exp))
16036 or else Ekind (Entity (Exp)) = E_Enumeration_Literal;
16037
16038 elsif Nkind (Exp) in N_Binary_Op then
16039 return Is_Constant_Bound (Left_Opnd (Exp))
16040 and then Is_Constant_Bound (Right_Opnd (Exp))
16041 and then Scope (Entity (Exp)) = Standard_Standard;
16042
16043 else
16044 return False;
16045 end if;
16046 end Is_Constant_Bound;
16047
16048 -----------------------
16049 -- Is_Null_Extension --
16050 -----------------------
16051
16052 function Is_Null_Extension (T : Entity_Id) return Boolean is
16053 Type_Decl : constant Node_Id := Parent (Base_Type (T));
16054 Comp_List : Node_Id;
16055 Comp : Node_Id;
16056
16057 begin
16058 if Nkind (Type_Decl) /= N_Full_Type_Declaration
16059 or else not Is_Tagged_Type (T)
16060 or else Nkind (Type_Definition (Type_Decl)) /=
16061 N_Derived_Type_Definition
16062 or else No (Record_Extension_Part (Type_Definition (Type_Decl)))
16063 then
16064 return False;
16065 end if;
16066
16067 Comp_List :=
16068 Component_List (Record_Extension_Part (Type_Definition (Type_Decl)));
16069
16070 if Present (Discriminant_Specifications (Type_Decl)) then
16071 return False;
16072
16073 elsif Present (Comp_List)
16074 and then Is_Non_Empty_List (Component_Items (Comp_List))
16075 then
16076 Comp := First (Component_Items (Comp_List));
16077
16078 -- Only user-defined components are relevant. The component list
16079 -- may also contain a parent component and internal components
16080 -- corresponding to secondary tags, but these do not determine
16081 -- whether this is a null extension.
16082
16083 while Present (Comp) loop
16084 if Comes_From_Source (Comp) then
16085 return False;
16086 end if;
16087
16088 Next (Comp);
16089 end loop;
16090
16091 return True;
16092 else
16093 return True;
16094 end if;
16095 end Is_Null_Extension;
16096
16097 ------------------------------
16098 -- Is_Valid_Constraint_Kind --
16099 ------------------------------
16100
16101 function Is_Valid_Constraint_Kind
16102 (T_Kind : Type_Kind;
16103 Constraint_Kind : Node_Kind) return Boolean
16104 is
16105 begin
16106 case T_Kind is
16107 when Enumeration_Kind |
16108 Integer_Kind =>
16109 return Constraint_Kind = N_Range_Constraint;
16110
16111 when Decimal_Fixed_Point_Kind =>
16112 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
16113 N_Range_Constraint);
16114
16115 when Ordinary_Fixed_Point_Kind =>
16116 return Nkind_In (Constraint_Kind, N_Delta_Constraint,
16117 N_Range_Constraint);
16118
16119 when Float_Kind =>
16120 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
16121 N_Range_Constraint);
16122
16123 when Access_Kind |
16124 Array_Kind |
16125 E_Record_Type |
16126 E_Record_Subtype |
16127 Class_Wide_Kind |
16128 E_Incomplete_Type |
16129 Private_Kind |
16130 Concurrent_Kind =>
16131 return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
16132
16133 when others =>
16134 return True; -- Error will be detected later
16135 end case;
16136 end Is_Valid_Constraint_Kind;
16137
16138 --------------------------
16139 -- Is_Visible_Component --
16140 --------------------------
16141
16142 function Is_Visible_Component (C : Entity_Id) return Boolean is
16143 Original_Comp : Entity_Id := Empty;
16144 Original_Scope : Entity_Id;
16145 Type_Scope : Entity_Id;
16146
16147 function Is_Local_Type (Typ : Entity_Id) return Boolean;
16148 -- Check whether parent type of inherited component is declared locally,
16149 -- possibly within a nested package or instance. The current scope is
16150 -- the derived record itself.
16151
16152 -------------------
16153 -- Is_Local_Type --
16154 -------------------
16155
16156 function Is_Local_Type (Typ : Entity_Id) return Boolean is
16157 Scop : Entity_Id;
16158
16159 begin
16160 Scop := Scope (Typ);
16161 while Present (Scop)
16162 and then Scop /= Standard_Standard
16163 loop
16164 if Scop = Scope (Current_Scope) then
16165 return True;
16166 end if;
16167
16168 Scop := Scope (Scop);
16169 end loop;
16170
16171 return False;
16172 end Is_Local_Type;
16173
16174 -- Start of processing for Is_Visible_Component
16175
16176 begin
16177 if Ekind_In (C, E_Component, E_Discriminant) then
16178 Original_Comp := Original_Record_Component (C);
16179 end if;
16180
16181 if No (Original_Comp) then
16182
16183 -- Premature usage, or previous error
16184
16185 return False;
16186
16187 else
16188 Original_Scope := Scope (Original_Comp);
16189 Type_Scope := Scope (Base_Type (Scope (C)));
16190 end if;
16191
16192 -- This test only concerns tagged types
16193
16194 if not Is_Tagged_Type (Original_Scope) then
16195 return True;
16196
16197 -- If it is _Parent or _Tag, there is no visibility issue
16198
16199 elsif not Comes_From_Source (Original_Comp) then
16200 return True;
16201
16202 -- If we are in the body of an instantiation, the component is visible
16203 -- even when the parent type (possibly defined in an enclosing unit or
16204 -- in a parent unit) might not.
16205
16206 elsif In_Instance_Body then
16207 return True;
16208
16209 -- Discriminants are always visible
16210
16211 elsif Ekind (Original_Comp) = E_Discriminant
16212 and then not Has_Unknown_Discriminants (Original_Scope)
16213 then
16214 return True;
16215
16216 -- If the component has been declared in an ancestor which is currently
16217 -- a private type, then it is not visible. The same applies if the
16218 -- component's containing type is not in an open scope and the original
16219 -- component's enclosing type is a visible full view of a private type
16220 -- (which can occur in cases where an attempt is being made to reference
16221 -- a component in a sibling package that is inherited from a visible
16222 -- component of a type in an ancestor package; the component in the
16223 -- sibling package should not be visible even though the component it
16224 -- inherited from is visible). This does not apply however in the case
16225 -- where the scope of the type is a private child unit, or when the
16226 -- parent comes from a local package in which the ancestor is currently
16227 -- visible. The latter suppression of visibility is needed for cases
16228 -- that are tested in B730006.
16229
16230 elsif Is_Private_Type (Original_Scope)
16231 or else
16232 (not Is_Private_Descendant (Type_Scope)
16233 and then not In_Open_Scopes (Type_Scope)
16234 and then Has_Private_Declaration (Original_Scope))
16235 then
16236 -- If the type derives from an entity in a formal package, there
16237 -- are no additional visible components.
16238
16239 if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
16240 N_Formal_Package_Declaration
16241 then
16242 return False;
16243
16244 -- if we are not in the private part of the current package, there
16245 -- are no additional visible components.
16246
16247 elsif Ekind (Scope (Current_Scope)) = E_Package
16248 and then not In_Private_Part (Scope (Current_Scope))
16249 then
16250 return False;
16251 else
16252 return
16253 Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
16254 and then In_Open_Scopes (Scope (Original_Scope))
16255 and then Is_Local_Type (Type_Scope);
16256 end if;
16257
16258 -- There is another weird way in which a component may be invisible
16259 -- when the private and the full view are not derived from the same
16260 -- ancestor. Here is an example :
16261
16262 -- type A1 is tagged record F1 : integer; end record;
16263 -- type A2 is new A1 with record F2 : integer; end record;
16264 -- type T is new A1 with private;
16265 -- private
16266 -- type T is new A2 with null record;
16267
16268 -- In this case, the full view of T inherits F1 and F2 but the private
16269 -- view inherits only F1
16270
16271 else
16272 declare
16273 Ancestor : Entity_Id := Scope (C);
16274
16275 begin
16276 loop
16277 if Ancestor = Original_Scope then
16278 return True;
16279 elsif Ancestor = Etype (Ancestor) then
16280 return False;
16281 end if;
16282
16283 Ancestor := Etype (Ancestor);
16284 end loop;
16285 end;
16286 end if;
16287 end Is_Visible_Component;
16288
16289 --------------------------
16290 -- Make_Class_Wide_Type --
16291 --------------------------
16292
16293 procedure Make_Class_Wide_Type (T : Entity_Id) is
16294 CW_Type : Entity_Id;
16295 CW_Name : Name_Id;
16296 Next_E : Entity_Id;
16297
16298 begin
16299 if Present (Class_Wide_Type (T)) then
16300
16301 -- The class-wide type is a partially decorated entity created for a
16302 -- unanalyzed tagged type referenced through a limited with clause.
16303 -- When the tagged type is analyzed, its class-wide type needs to be
16304 -- redecorated. Note that we reuse the entity created by Decorate_
16305 -- Tagged_Type in order to preserve all links.
16306
16307 if Materialize_Entity (Class_Wide_Type (T)) then
16308 CW_Type := Class_Wide_Type (T);
16309 Set_Materialize_Entity (CW_Type, False);
16310
16311 -- The class wide type can have been defined by the partial view, in
16312 -- which case everything is already done.
16313
16314 else
16315 return;
16316 end if;
16317
16318 -- Default case, we need to create a new class-wide type
16319
16320 else
16321 CW_Type :=
16322 New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
16323 end if;
16324
16325 -- Inherit root type characteristics
16326
16327 CW_Name := Chars (CW_Type);
16328 Next_E := Next_Entity (CW_Type);
16329 Copy_Node (T, CW_Type);
16330 Set_Comes_From_Source (CW_Type, False);
16331 Set_Chars (CW_Type, CW_Name);
16332 Set_Parent (CW_Type, Parent (T));
16333 Set_Next_Entity (CW_Type, Next_E);
16334
16335 -- Ensure we have a new freeze node for the class-wide type. The partial
16336 -- view may have freeze action of its own, requiring a proper freeze
16337 -- node, and the same freeze node cannot be shared between the two
16338 -- types.
16339
16340 Set_Has_Delayed_Freeze (CW_Type);
16341 Set_Freeze_Node (CW_Type, Empty);
16342
16343 -- Customize the class-wide type: It has no prim. op., it cannot be
16344 -- abstract and its Etype points back to the specific root type.
16345
16346 Set_Ekind (CW_Type, E_Class_Wide_Type);
16347 Set_Is_Tagged_Type (CW_Type, True);
16348 Set_Direct_Primitive_Operations (CW_Type, New_Elmt_List);
16349 Set_Is_Abstract_Type (CW_Type, False);
16350 Set_Is_Constrained (CW_Type, False);
16351 Set_Is_First_Subtype (CW_Type, Is_First_Subtype (T));
16352
16353 if Ekind (T) = E_Class_Wide_Subtype then
16354 Set_Etype (CW_Type, Etype (Base_Type (T)));
16355 else
16356 Set_Etype (CW_Type, T);
16357 end if;
16358
16359 -- If this is the class_wide type of a constrained subtype, it does
16360 -- not have discriminants.
16361
16362 Set_Has_Discriminants (CW_Type,
16363 Has_Discriminants (T) and then not Is_Constrained (T));
16364
16365 Set_Has_Unknown_Discriminants (CW_Type, True);
16366 Set_Class_Wide_Type (T, CW_Type);
16367 Set_Equivalent_Type (CW_Type, Empty);
16368
16369 -- The class-wide type of a class-wide type is itself (RM 3.9(14))
16370
16371 Set_Class_Wide_Type (CW_Type, CW_Type);
16372 end Make_Class_Wide_Type;
16373
16374 ----------------
16375 -- Make_Index --
16376 ----------------
16377
16378 procedure Make_Index
16379 (I : Node_Id;
16380 Related_Nod : Node_Id;
16381 Related_Id : Entity_Id := Empty;
16382 Suffix_Index : Nat := 1;
16383 In_Iter_Schm : Boolean := False)
16384 is
16385 R : Node_Id;
16386 T : Entity_Id;
16387 Def_Id : Entity_Id := Empty;
16388 Found : Boolean := False;
16389
16390 begin
16391 -- For a discrete range used in a constrained array definition and
16392 -- defined by a range, an implicit conversion to the predefined type
16393 -- INTEGER is assumed if each bound is either a numeric literal, a named
16394 -- number, or an attribute, and the type of both bounds (prior to the
16395 -- implicit conversion) is the type universal_integer. Otherwise, both
16396 -- bounds must be of the same discrete type, other than universal
16397 -- integer; this type must be determinable independently of the
16398 -- context, but using the fact that the type must be discrete and that
16399 -- both bounds must have the same type.
16400
16401 -- Character literals also have a universal type in the absence of
16402 -- of additional context, and are resolved to Standard_Character.
16403
16404 if Nkind (I) = N_Range then
16405
16406 -- The index is given by a range constraint. The bounds are known
16407 -- to be of a consistent type.
16408
16409 if not Is_Overloaded (I) then
16410 T := Etype (I);
16411
16412 -- For universal bounds, choose the specific predefined type
16413
16414 if T = Universal_Integer then
16415 T := Standard_Integer;
16416
16417 elsif T = Any_Character then
16418 Ambiguous_Character (Low_Bound (I));
16419
16420 T := Standard_Character;
16421 end if;
16422
16423 -- The node may be overloaded because some user-defined operators
16424 -- are available, but if a universal interpretation exists it is
16425 -- also the selected one.
16426
16427 elsif Universal_Interpretation (I) = Universal_Integer then
16428 T := Standard_Integer;
16429
16430 else
16431 T := Any_Type;
16432
16433 declare
16434 Ind : Interp_Index;
16435 It : Interp;
16436
16437 begin
16438 Get_First_Interp (I, Ind, It);
16439 while Present (It.Typ) loop
16440 if Is_Discrete_Type (It.Typ) then
16441
16442 if Found
16443 and then not Covers (It.Typ, T)
16444 and then not Covers (T, It.Typ)
16445 then
16446 Error_Msg_N ("ambiguous bounds in discrete range", I);
16447 exit;
16448 else
16449 T := It.Typ;
16450 Found := True;
16451 end if;
16452 end if;
16453
16454 Get_Next_Interp (Ind, It);
16455 end loop;
16456
16457 if T = Any_Type then
16458 Error_Msg_N ("discrete type required for range", I);
16459 Set_Etype (I, Any_Type);
16460 return;
16461
16462 elsif T = Universal_Integer then
16463 T := Standard_Integer;
16464 end if;
16465 end;
16466 end if;
16467
16468 if not Is_Discrete_Type (T) then
16469 Error_Msg_N ("discrete type required for range", I);
16470 Set_Etype (I, Any_Type);
16471 return;
16472 end if;
16473
16474 if Nkind (Low_Bound (I)) = N_Attribute_Reference
16475 and then Attribute_Name (Low_Bound (I)) = Name_First
16476 and then Is_Entity_Name (Prefix (Low_Bound (I)))
16477 and then Is_Type (Entity (Prefix (Low_Bound (I))))
16478 and then Is_Discrete_Type (Entity (Prefix (Low_Bound (I))))
16479 then
16480 -- The type of the index will be the type of the prefix, as long
16481 -- as the upper bound is 'Last of the same type.
16482
16483 Def_Id := Entity (Prefix (Low_Bound (I)));
16484
16485 if Nkind (High_Bound (I)) /= N_Attribute_Reference
16486 or else Attribute_Name (High_Bound (I)) /= Name_Last
16487 or else not Is_Entity_Name (Prefix (High_Bound (I)))
16488 or else Entity (Prefix (High_Bound (I))) /= Def_Id
16489 then
16490 Def_Id := Empty;
16491 end if;
16492 end if;
16493
16494 R := I;
16495 Process_Range_Expr_In_Decl (R, T, In_Iter_Schm => In_Iter_Schm);
16496
16497 elsif Nkind (I) = N_Subtype_Indication then
16498
16499 -- The index is given by a subtype with a range constraint
16500
16501 T := Base_Type (Entity (Subtype_Mark (I)));
16502
16503 if not Is_Discrete_Type (T) then
16504 Error_Msg_N ("discrete type required for range", I);
16505 Set_Etype (I, Any_Type);
16506 return;
16507 end if;
16508
16509 R := Range_Expression (Constraint (I));
16510
16511 Resolve (R, T);
16512 Process_Range_Expr_In_Decl
16513 (R, Entity (Subtype_Mark (I)), In_Iter_Schm => In_Iter_Schm);
16514
16515 elsif Nkind (I) = N_Attribute_Reference then
16516
16517 -- The parser guarantees that the attribute is a RANGE attribute
16518
16519 -- If the node denotes the range of a type mark, that is also the
16520 -- resulting type, and we do no need to create an Itype for it.
16521
16522 if Is_Entity_Name (Prefix (I))
16523 and then Comes_From_Source (I)
16524 and then Is_Type (Entity (Prefix (I)))
16525 and then Is_Discrete_Type (Entity (Prefix (I)))
16526 then
16527 Def_Id := Entity (Prefix (I));
16528 end if;
16529
16530 Analyze_And_Resolve (I);
16531 T := Etype (I);
16532 R := I;
16533
16534 -- If none of the above, must be a subtype. We convert this to a
16535 -- range attribute reference because in the case of declared first
16536 -- named subtypes, the types in the range reference can be different
16537 -- from the type of the entity. A range attribute normalizes the
16538 -- reference and obtains the correct types for the bounds.
16539
16540 -- This transformation is in the nature of an expansion, is only
16541 -- done if expansion is active. In particular, it is not done on
16542 -- formal generic types, because we need to retain the name of the
16543 -- original index for instantiation purposes.
16544
16545 else
16546 if not Is_Entity_Name (I) or else not Is_Type (Entity (I)) then
16547 Error_Msg_N ("invalid subtype mark in discrete range ", I);
16548 Set_Etype (I, Any_Integer);
16549 return;
16550
16551 else
16552 -- The type mark may be that of an incomplete type. It is only
16553 -- now that we can get the full view, previous analysis does
16554 -- not look specifically for a type mark.
16555
16556 Set_Entity (I, Get_Full_View (Entity (I)));
16557 Set_Etype (I, Entity (I));
16558 Def_Id := Entity (I);
16559
16560 if not Is_Discrete_Type (Def_Id) then
16561 Error_Msg_N ("discrete type required for index", I);
16562 Set_Etype (I, Any_Type);
16563 return;
16564 end if;
16565 end if;
16566
16567 if Expander_Active then
16568 Rewrite (I,
16569 Make_Attribute_Reference (Sloc (I),
16570 Attribute_Name => Name_Range,
16571 Prefix => Relocate_Node (I)));
16572
16573 -- The original was a subtype mark that does not freeze. This
16574 -- means that the rewritten version must not freeze either.
16575
16576 Set_Must_Not_Freeze (I);
16577 Set_Must_Not_Freeze (Prefix (I));
16578
16579 -- Is order critical??? if so, document why, if not
16580 -- use Analyze_And_Resolve
16581
16582 Analyze_And_Resolve (I);
16583 T := Etype (I);
16584 R := I;
16585
16586 -- If expander is inactive, type is legal, nothing else to construct
16587
16588 else
16589 return;
16590 end if;
16591 end if;
16592
16593 if not Is_Discrete_Type (T) then
16594 Error_Msg_N ("discrete type required for range", I);
16595 Set_Etype (I, Any_Type);
16596 return;
16597
16598 elsif T = Any_Type then
16599 Set_Etype (I, Any_Type);
16600 return;
16601 end if;
16602
16603 -- We will now create the appropriate Itype to describe the range, but
16604 -- first a check. If we originally had a subtype, then we just label
16605 -- the range with this subtype. Not only is there no need to construct
16606 -- a new subtype, but it is wrong to do so for two reasons:
16607
16608 -- 1. A legality concern, if we have a subtype, it must not freeze,
16609 -- and the Itype would cause freezing incorrectly
16610
16611 -- 2. An efficiency concern, if we created an Itype, it would not be
16612 -- recognized as the same type for the purposes of eliminating
16613 -- checks in some circumstances.
16614
16615 -- We signal this case by setting the subtype entity in Def_Id
16616
16617 if No (Def_Id) then
16618 Def_Id :=
16619 Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
16620 Set_Etype (Def_Id, Base_Type (T));
16621
16622 if Is_Signed_Integer_Type (T) then
16623 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
16624
16625 elsif Is_Modular_Integer_Type (T) then
16626 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
16627
16628 else
16629 Set_Ekind (Def_Id, E_Enumeration_Subtype);
16630 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
16631 Set_First_Literal (Def_Id, First_Literal (T));
16632 end if;
16633
16634 Set_Size_Info (Def_Id, (T));
16635 Set_RM_Size (Def_Id, RM_Size (T));
16636 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
16637
16638 Set_Scalar_Range (Def_Id, R);
16639 Conditional_Delay (Def_Id, T);
16640
16641 -- In the subtype indication case, if the immediate parent of the
16642 -- new subtype is non-static, then the subtype we create is non-
16643 -- static, even if its bounds are static.
16644
16645 if Nkind (I) = N_Subtype_Indication
16646 and then not Is_Static_Subtype (Entity (Subtype_Mark (I)))
16647 then
16648 Set_Is_Non_Static_Subtype (Def_Id);
16649 end if;
16650 end if;
16651
16652 -- Final step is to label the index with this constructed type
16653
16654 Set_Etype (I, Def_Id);
16655 end Make_Index;
16656
16657 ------------------------------
16658 -- Modular_Type_Declaration --
16659 ------------------------------
16660
16661 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
16662 Mod_Expr : constant Node_Id := Expression (Def);
16663 M_Val : Uint;
16664
16665 procedure Set_Modular_Size (Bits : Int);
16666 -- Sets RM_Size to Bits, and Esize to normal word size above this
16667
16668 ----------------------
16669 -- Set_Modular_Size --
16670 ----------------------
16671
16672 procedure Set_Modular_Size (Bits : Int) is
16673 begin
16674 Set_RM_Size (T, UI_From_Int (Bits));
16675
16676 if Bits <= 8 then
16677 Init_Esize (T, 8);
16678
16679 elsif Bits <= 16 then
16680 Init_Esize (T, 16);
16681
16682 elsif Bits <= 32 then
16683 Init_Esize (T, 32);
16684
16685 else
16686 Init_Esize (T, System_Max_Binary_Modulus_Power);
16687 end if;
16688
16689 if not Non_Binary_Modulus (T)
16690 and then Esize (T) = RM_Size (T)
16691 then
16692 Set_Is_Known_Valid (T);
16693 end if;
16694 end Set_Modular_Size;
16695
16696 -- Start of processing for Modular_Type_Declaration
16697
16698 begin
16699 Analyze_And_Resolve (Mod_Expr, Any_Integer);
16700 Set_Etype (T, T);
16701 Set_Ekind (T, E_Modular_Integer_Type);
16702 Init_Alignment (T);
16703 Set_Is_Constrained (T);
16704
16705 if not Is_OK_Static_Expression (Mod_Expr) then
16706 Flag_Non_Static_Expr
16707 ("non-static expression used for modular type bound!", Mod_Expr);
16708 M_Val := 2 ** System_Max_Binary_Modulus_Power;
16709 else
16710 M_Val := Expr_Value (Mod_Expr);
16711 end if;
16712
16713 if M_Val < 1 then
16714 Error_Msg_N ("modulus value must be positive", Mod_Expr);
16715 M_Val := 2 ** System_Max_Binary_Modulus_Power;
16716 end if;
16717
16718 Set_Modulus (T, M_Val);
16719
16720 -- Create bounds for the modular type based on the modulus given in
16721 -- the type declaration and then analyze and resolve those bounds.
16722
16723 Set_Scalar_Range (T,
16724 Make_Range (Sloc (Mod_Expr),
16725 Low_Bound => Make_Integer_Literal (Sloc (Mod_Expr), 0),
16726 High_Bound => Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
16727
16728 -- Properly analyze the literals for the range. We do this manually
16729 -- because we can't go calling Resolve, since we are resolving these
16730 -- bounds with the type, and this type is certainly not complete yet!
16731
16732 Set_Etype (Low_Bound (Scalar_Range (T)), T);
16733 Set_Etype (High_Bound (Scalar_Range (T)), T);
16734 Set_Is_Static_Expression (Low_Bound (Scalar_Range (T)));
16735 Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
16736
16737 -- Loop through powers of two to find number of bits required
16738
16739 for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
16740
16741 -- Binary case
16742
16743 if M_Val = 2 ** Bits then
16744 Set_Modular_Size (Bits);
16745 return;
16746
16747 -- Non-binary case
16748
16749 elsif M_Val < 2 ** Bits then
16750 Check_SPARK_Restriction ("modulus should be a power of 2", T);
16751 Set_Non_Binary_Modulus (T);
16752
16753 if Bits > System_Max_Nonbinary_Modulus_Power then
16754 Error_Msg_Uint_1 :=
16755 UI_From_Int (System_Max_Nonbinary_Modulus_Power);
16756 Error_Msg_F
16757 ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
16758 Set_Modular_Size (System_Max_Binary_Modulus_Power);
16759 return;
16760
16761 else
16762 -- In the non-binary case, set size as per RM 13.3(55)
16763
16764 Set_Modular_Size (Bits);
16765 return;
16766 end if;
16767 end if;
16768
16769 end loop;
16770
16771 -- If we fall through, then the size exceed System.Max_Binary_Modulus
16772 -- so we just signal an error and set the maximum size.
16773
16774 Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
16775 Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
16776
16777 Set_Modular_Size (System_Max_Binary_Modulus_Power);
16778 Init_Alignment (T);
16779
16780 end Modular_Type_Declaration;
16781
16782 --------------------------
16783 -- New_Concatenation_Op --
16784 --------------------------
16785
16786 procedure New_Concatenation_Op (Typ : Entity_Id) is
16787 Loc : constant Source_Ptr := Sloc (Typ);
16788 Op : Entity_Id;
16789
16790 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
16791 -- Create abbreviated declaration for the formal of a predefined
16792 -- Operator 'Op' of type 'Typ'
16793
16794 --------------------
16795 -- Make_Op_Formal --
16796 --------------------
16797
16798 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
16799 Formal : Entity_Id;
16800 begin
16801 Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
16802 Set_Etype (Formal, Typ);
16803 Set_Mechanism (Formal, Default_Mechanism);
16804 return Formal;
16805 end Make_Op_Formal;
16806
16807 -- Start of processing for New_Concatenation_Op
16808
16809 begin
16810 Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
16811
16812 Set_Ekind (Op, E_Operator);
16813 Set_Scope (Op, Current_Scope);
16814 Set_Etype (Op, Typ);
16815 Set_Homonym (Op, Get_Name_Entity_Id (Name_Op_Concat));
16816 Set_Is_Immediately_Visible (Op);
16817 Set_Is_Intrinsic_Subprogram (Op);
16818 Set_Has_Completion (Op);
16819 Append_Entity (Op, Current_Scope);
16820
16821 Set_Name_Entity_Id (Name_Op_Concat, Op);
16822
16823 Append_Entity (Make_Op_Formal (Typ, Op), Op);
16824 Append_Entity (Make_Op_Formal (Typ, Op), Op);
16825 end New_Concatenation_Op;
16826
16827 -------------------------
16828 -- OK_For_Limited_Init --
16829 -------------------------
16830
16831 -- ???Check all calls of this, and compare the conditions under which it's
16832 -- called.
16833
16834 function OK_For_Limited_Init
16835 (Typ : Entity_Id;
16836 Exp : Node_Id) return Boolean
16837 is
16838 begin
16839 return Is_CPP_Constructor_Call (Exp)
16840 or else (Ada_Version >= Ada_2005
16841 and then not Debug_Flag_Dot_L
16842 and then OK_For_Limited_Init_In_05 (Typ, Exp));
16843 end OK_For_Limited_Init;
16844
16845 -------------------------------
16846 -- OK_For_Limited_Init_In_05 --
16847 -------------------------------
16848
16849 function OK_For_Limited_Init_In_05
16850 (Typ : Entity_Id;
16851 Exp : Node_Id) return Boolean
16852 is
16853 begin
16854 -- An object of a limited interface type can be initialized with any
16855 -- expression of a nonlimited descendant type.
16856
16857 if Is_Class_Wide_Type (Typ)
16858 and then Is_Limited_Interface (Typ)
16859 and then not Is_Limited_Type (Etype (Exp))
16860 then
16861 return True;
16862 end if;
16863
16864 -- Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
16865 -- case of limited aggregates (including extension aggregates), and
16866 -- function calls. The function call may have been given in prefixed
16867 -- notation, in which case the original node is an indexed component.
16868 -- If the function is parameterless, the original node was an explicit
16869 -- dereference. The function may also be parameterless, in which case
16870 -- the source node is just an identifier.
16871
16872 case Nkind (Original_Node (Exp)) is
16873 when N_Aggregate | N_Extension_Aggregate | N_Function_Call | N_Op =>
16874 return True;
16875
16876 when N_Identifier =>
16877 return Present (Entity (Original_Node (Exp)))
16878 and then Ekind (Entity (Original_Node (Exp))) = E_Function;
16879
16880 when N_Qualified_Expression =>
16881 return
16882 OK_For_Limited_Init_In_05
16883 (Typ, Expression (Original_Node (Exp)));
16884
16885 -- Ada 2005 (AI-251): If a class-wide interface object is initialized
16886 -- with a function call, the expander has rewritten the call into an
16887 -- N_Type_Conversion node to force displacement of the pointer to
16888 -- reference the component containing the secondary dispatch table.
16889 -- Otherwise a type conversion is not a legal context.
16890 -- A return statement for a build-in-place function returning a
16891 -- synchronized type also introduces an unchecked conversion.
16892
16893 when N_Type_Conversion |
16894 N_Unchecked_Type_Conversion =>
16895 return not Comes_From_Source (Exp)
16896 and then
16897 OK_For_Limited_Init_In_05
16898 (Typ, Expression (Original_Node (Exp)));
16899
16900 when N_Indexed_Component |
16901 N_Selected_Component |
16902 N_Explicit_Dereference =>
16903 return Nkind (Exp) = N_Function_Call;
16904
16905 -- A use of 'Input is a function call, hence allowed. Normally the
16906 -- attribute will be changed to a call, but the attribute by itself
16907 -- can occur with -gnatc.
16908
16909 when N_Attribute_Reference =>
16910 return Attribute_Name (Original_Node (Exp)) = Name_Input;
16911
16912 when others =>
16913 return False;
16914 end case;
16915 end OK_For_Limited_Init_In_05;
16916
16917 -------------------------------------------
16918 -- Ordinary_Fixed_Point_Type_Declaration --
16919 -------------------------------------------
16920
16921 procedure Ordinary_Fixed_Point_Type_Declaration
16922 (T : Entity_Id;
16923 Def : Node_Id)
16924 is
16925 Loc : constant Source_Ptr := Sloc (Def);
16926 Delta_Expr : constant Node_Id := Delta_Expression (Def);
16927 RRS : constant Node_Id := Real_Range_Specification (Def);
16928 Implicit_Base : Entity_Id;
16929 Delta_Val : Ureal;
16930 Small_Val : Ureal;
16931 Low_Val : Ureal;
16932 High_Val : Ureal;
16933
16934 begin
16935 Check_Restriction (No_Fixed_Point, Def);
16936
16937 -- Create implicit base type
16938
16939 Implicit_Base :=
16940 Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
16941 Set_Etype (Implicit_Base, Implicit_Base);
16942
16943 -- Analyze and process delta expression
16944
16945 Analyze_And_Resolve (Delta_Expr, Any_Real);
16946
16947 Check_Delta_Expression (Delta_Expr);
16948 Delta_Val := Expr_Value_R (Delta_Expr);
16949
16950 Set_Delta_Value (Implicit_Base, Delta_Val);
16951
16952 -- Compute default small from given delta, which is the largest power
16953 -- of two that does not exceed the given delta value.
16954
16955 declare
16956 Tmp : Ureal;
16957 Scale : Int;
16958
16959 begin
16960 Tmp := Ureal_1;
16961 Scale := 0;
16962
16963 if Delta_Val < Ureal_1 then
16964 while Delta_Val < Tmp loop
16965 Tmp := Tmp / Ureal_2;
16966 Scale := Scale + 1;
16967 end loop;
16968
16969 else
16970 loop
16971 Tmp := Tmp * Ureal_2;
16972 exit when Tmp > Delta_Val;
16973 Scale := Scale - 1;
16974 end loop;
16975 end if;
16976
16977 Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
16978 end;
16979
16980 Set_Small_Value (Implicit_Base, Small_Val);
16981
16982 -- If no range was given, set a dummy range
16983
16984 if RRS <= Empty_Or_Error then
16985 Low_Val := -Small_Val;
16986 High_Val := Small_Val;
16987
16988 -- Otherwise analyze and process given range
16989
16990 else
16991 declare
16992 Low : constant Node_Id := Low_Bound (RRS);
16993 High : constant Node_Id := High_Bound (RRS);
16994
16995 begin
16996 Analyze_And_Resolve (Low, Any_Real);
16997 Analyze_And_Resolve (High, Any_Real);
16998 Check_Real_Bound (Low);
16999 Check_Real_Bound (High);
17000
17001 -- Obtain and set the range
17002
17003 Low_Val := Expr_Value_R (Low);
17004 High_Val := Expr_Value_R (High);
17005
17006 if Low_Val > High_Val then
17007 Error_Msg_NE ("?fixed point type& has null range", Def, T);
17008 end if;
17009 end;
17010 end if;
17011
17012 -- The range for both the implicit base and the declared first subtype
17013 -- cannot be set yet, so we use the special routine Set_Fixed_Range to
17014 -- set a temporary range in place. Note that the bounds of the base
17015 -- type will be widened to be symmetrical and to fill the available
17016 -- bits when the type is frozen.
17017
17018 -- We could do this with all discrete types, and probably should, but
17019 -- we absolutely have to do it for fixed-point, since the end-points
17020 -- of the range and the size are determined by the small value, which
17021 -- could be reset before the freeze point.
17022
17023 Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
17024 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
17025
17026 -- Complete definition of first subtype
17027
17028 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
17029 Set_Etype (T, Implicit_Base);
17030 Init_Size_Align (T);
17031 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
17032 Set_Small_Value (T, Small_Val);
17033 Set_Delta_Value (T, Delta_Val);
17034 Set_Is_Constrained (T);
17035
17036 end Ordinary_Fixed_Point_Type_Declaration;
17037
17038 ----------------------------------------
17039 -- Prepare_Private_Subtype_Completion --
17040 ----------------------------------------
17041
17042 procedure Prepare_Private_Subtype_Completion
17043 (Id : Entity_Id;
17044 Related_Nod : Node_Id)
17045 is
17046 Id_B : constant Entity_Id := Base_Type (Id);
17047 Full_B : constant Entity_Id := Full_View (Id_B);
17048 Full : Entity_Id;
17049
17050 begin
17051 if Present (Full_B) then
17052
17053 -- The Base_Type is already completed, we can complete the subtype
17054 -- now. We have to create a new entity with the same name, Thus we
17055 -- can't use Create_Itype.
17056
17057 -- This is messy, should be fixed ???
17058
17059 Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
17060 Set_Is_Itype (Full);
17061 Set_Associated_Node_For_Itype (Full, Related_Nod);
17062 Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
17063 end if;
17064
17065 -- The parent subtype may be private, but the base might not, in some
17066 -- nested instances. In that case, the subtype does not need to be
17067 -- exchanged. It would still be nice to make private subtypes and their
17068 -- bases consistent at all times ???
17069
17070 if Is_Private_Type (Id_B) then
17071 Append_Elmt (Id, Private_Dependents (Id_B));
17072 end if;
17073
17074 end Prepare_Private_Subtype_Completion;
17075
17076 ---------------------------
17077 -- Process_Discriminants --
17078 ---------------------------
17079
17080 procedure Process_Discriminants
17081 (N : Node_Id;
17082 Prev : Entity_Id := Empty)
17083 is
17084 Elist : constant Elist_Id := New_Elmt_List;
17085 Id : Node_Id;
17086 Discr : Node_Id;
17087 Discr_Number : Uint;
17088 Discr_Type : Entity_Id;
17089 Default_Present : Boolean := False;
17090 Default_Not_Present : Boolean := False;
17091
17092 begin
17093 -- A composite type other than an array type can have discriminants.
17094 -- On entry, the current scope is the composite type.
17095
17096 -- The discriminants are initially entered into the scope of the type
17097 -- via Enter_Name with the default Ekind of E_Void to prevent premature
17098 -- use, as explained at the end of this procedure.
17099
17100 Discr := First (Discriminant_Specifications (N));
17101 while Present (Discr) loop
17102 Enter_Name (Defining_Identifier (Discr));
17103
17104 -- For navigation purposes we add a reference to the discriminant
17105 -- in the entity for the type. If the current declaration is a
17106 -- completion, place references on the partial view. Otherwise the
17107 -- type is the current scope.
17108
17109 if Present (Prev) then
17110
17111 -- The references go on the partial view, if present. If the
17112 -- partial view has discriminants, the references have been
17113 -- generated already.
17114
17115 if not Has_Discriminants (Prev) then
17116 Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
17117 end if;
17118 else
17119 Generate_Reference
17120 (Current_Scope, Defining_Identifier (Discr), 'd');
17121 end if;
17122
17123 if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
17124 Discr_Type := Access_Definition (Discr, Discriminant_Type (Discr));
17125
17126 -- Ada 2005 (AI-254)
17127
17128 if Present (Access_To_Subprogram_Definition
17129 (Discriminant_Type (Discr)))
17130 and then Protected_Present (Access_To_Subprogram_Definition
17131 (Discriminant_Type (Discr)))
17132 then
17133 Discr_Type :=
17134 Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
17135 end if;
17136
17137 else
17138 Find_Type (Discriminant_Type (Discr));
17139 Discr_Type := Etype (Discriminant_Type (Discr));
17140
17141 if Error_Posted (Discriminant_Type (Discr)) then
17142 Discr_Type := Any_Type;
17143 end if;
17144 end if;
17145
17146 if Is_Access_Type (Discr_Type) then
17147
17148 -- Ada 2005 (AI-230): Access discriminant allowed in non-limited
17149 -- record types
17150
17151 if Ada_Version < Ada_2005 then
17152 Check_Access_Discriminant_Requires_Limited
17153 (Discr, Discriminant_Type (Discr));
17154 end if;
17155
17156 if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
17157 Error_Msg_N
17158 ("(Ada 83) access discriminant not allowed", Discr);
17159 end if;
17160
17161 elsif not Is_Discrete_Type (Discr_Type) then
17162 Error_Msg_N ("discriminants must have a discrete or access type",
17163 Discriminant_Type (Discr));
17164 end if;
17165
17166 Set_Etype (Defining_Identifier (Discr), Discr_Type);
17167
17168 -- If a discriminant specification includes the assignment compound
17169 -- delimiter followed by an expression, the expression is the default
17170 -- expression of the discriminant; the default expression must be of
17171 -- the type of the discriminant. (RM 3.7.1) Since this expression is
17172 -- a default expression, we do the special preanalysis, since this
17173 -- expression does not freeze (see "Handling of Default and Per-
17174 -- Object Expressions" in spec of package Sem).
17175
17176 if Present (Expression (Discr)) then
17177 Preanalyze_Spec_Expression (Expression (Discr), Discr_Type);
17178
17179 if Nkind (N) = N_Formal_Type_Declaration then
17180 Error_Msg_N
17181 ("discriminant defaults not allowed for formal type",
17182 Expression (Discr));
17183
17184 -- Flag an error for a tagged type with defaulted discriminants,
17185 -- excluding limited tagged types when compiling for Ada 2012
17186 -- (see AI05-0214).
17187
17188 elsif Is_Tagged_Type (Current_Scope)
17189 and then (not Is_Limited_Type (Current_Scope)
17190 or else Ada_Version < Ada_2012)
17191 and then Comes_From_Source (N)
17192 then
17193 -- Note: see similar test in Check_Or_Process_Discriminants, to
17194 -- handle the (illegal) case of the completion of an untagged
17195 -- view with discriminants with defaults by a tagged full view.
17196 -- We skip the check if Discr does not come from source, to
17197 -- account for the case of an untagged derived type providing
17198 -- defaults for a renamed discriminant from a private untagged
17199 -- ancestor with a tagged full view (ACATS B460006).
17200
17201 if Ada_Version >= Ada_2012 then
17202 Error_Msg_N
17203 ("discriminants of nonlimited tagged type cannot have"
17204 & " defaults",
17205 Expression (Discr));
17206 else
17207 Error_Msg_N
17208 ("discriminants of tagged type cannot have defaults",
17209 Expression (Discr));
17210 end if;
17211
17212 else
17213 Default_Present := True;
17214 Append_Elmt (Expression (Discr), Elist);
17215
17216 -- Tag the defining identifiers for the discriminants with
17217 -- their corresponding default expressions from the tree.
17218
17219 Set_Discriminant_Default_Value
17220 (Defining_Identifier (Discr), Expression (Discr));
17221 end if;
17222
17223 else
17224 Default_Not_Present := True;
17225 end if;
17226
17227 -- Ada 2005 (AI-231): Create an Itype that is a duplicate of
17228 -- Discr_Type but with the null-exclusion attribute
17229
17230 if Ada_Version >= Ada_2005 then
17231
17232 -- Ada 2005 (AI-231): Static checks
17233
17234 if Can_Never_Be_Null (Discr_Type) then
17235 Null_Exclusion_Static_Checks (Discr);
17236
17237 elsif Is_Access_Type (Discr_Type)
17238 and then Null_Exclusion_Present (Discr)
17239
17240 -- No need to check itypes because in their case this check
17241 -- was done at their point of creation
17242
17243 and then not Is_Itype (Discr_Type)
17244 then
17245 if Can_Never_Be_Null (Discr_Type) then
17246 Error_Msg_NE
17247 ("`NOT NULL` not allowed (& already excludes null)",
17248 Discr,
17249 Discr_Type);
17250 end if;
17251
17252 Set_Etype (Defining_Identifier (Discr),
17253 Create_Null_Excluding_Itype
17254 (T => Discr_Type,
17255 Related_Nod => Discr));
17256
17257 -- Check for improper null exclusion if the type is otherwise
17258 -- legal for a discriminant.
17259
17260 elsif Null_Exclusion_Present (Discr)
17261 and then Is_Discrete_Type (Discr_Type)
17262 then
17263 Error_Msg_N
17264 ("null exclusion can only apply to an access type", Discr);
17265 end if;
17266
17267 -- Ada 2005 (AI-402): access discriminants of nonlimited types
17268 -- can't have defaults. Synchronized types, or types that are
17269 -- explicitly limited are fine, but special tests apply to derived
17270 -- types in generics: in a generic body we have to assume the
17271 -- worst, and therefore defaults are not allowed if the parent is
17272 -- a generic formal private type (see ACATS B370001).
17273
17274 if Is_Access_Type (Discr_Type) and then Default_Present then
17275 if Ekind (Discr_Type) /= E_Anonymous_Access_Type
17276 or else Is_Limited_Record (Current_Scope)
17277 or else Is_Concurrent_Type (Current_Scope)
17278 or else Is_Concurrent_Record_Type (Current_Scope)
17279 or else Ekind (Current_Scope) = E_Limited_Private_Type
17280 then
17281 if not Is_Derived_Type (Current_Scope)
17282 or else not Is_Generic_Type (Etype (Current_Scope))
17283 or else not In_Package_Body (Scope (Etype (Current_Scope)))
17284 or else Limited_Present
17285 (Type_Definition (Parent (Current_Scope)))
17286 then
17287 null;
17288
17289 else
17290 Error_Msg_N ("access discriminants of nonlimited types",
17291 Expression (Discr));
17292 Error_Msg_N ("\cannot have defaults", Expression (Discr));
17293 end if;
17294
17295 elsif Present (Expression (Discr)) then
17296 Error_Msg_N
17297 ("(Ada 2005) access discriminants of nonlimited types",
17298 Expression (Discr));
17299 Error_Msg_N ("\cannot have defaults", Expression (Discr));
17300 end if;
17301 end if;
17302 end if;
17303
17304 Next (Discr);
17305 end loop;
17306
17307 -- An element list consisting of the default expressions of the
17308 -- discriminants is constructed in the above loop and used to set
17309 -- the Discriminant_Constraint attribute for the type. If an object
17310 -- is declared of this (record or task) type without any explicit
17311 -- discriminant constraint given, this element list will form the
17312 -- actual parameters for the corresponding initialization procedure
17313 -- for the type.
17314
17315 Set_Discriminant_Constraint (Current_Scope, Elist);
17316 Set_Stored_Constraint (Current_Scope, No_Elist);
17317
17318 -- Default expressions must be provided either for all or for none
17319 -- of the discriminants of a discriminant part. (RM 3.7.1)
17320
17321 if Default_Present and then Default_Not_Present then
17322 Error_Msg_N
17323 ("incomplete specification of defaults for discriminants", N);
17324 end if;
17325
17326 -- The use of the name of a discriminant is not allowed in default
17327 -- expressions of a discriminant part if the specification of the
17328 -- discriminant is itself given in the discriminant part. (RM 3.7.1)
17329
17330 -- To detect this, the discriminant names are entered initially with an
17331 -- Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
17332 -- attempt to use a void entity (for example in an expression that is
17333 -- type-checked) produces the error message: premature usage. Now after
17334 -- completing the semantic analysis of the discriminant part, we can set
17335 -- the Ekind of all the discriminants appropriately.
17336
17337 Discr := First (Discriminant_Specifications (N));
17338 Discr_Number := Uint_1;
17339 while Present (Discr) loop
17340 Id := Defining_Identifier (Discr);
17341 Set_Ekind (Id, E_Discriminant);
17342 Init_Component_Location (Id);
17343 Init_Esize (Id);
17344 Set_Discriminant_Number (Id, Discr_Number);
17345
17346 -- Make sure this is always set, even in illegal programs
17347
17348 Set_Corresponding_Discriminant (Id, Empty);
17349
17350 -- Initialize the Original_Record_Component to the entity itself.
17351 -- Inherit_Components will propagate the right value to
17352 -- discriminants in derived record types.
17353
17354 Set_Original_Record_Component (Id, Id);
17355
17356 -- Create the discriminal for the discriminant
17357
17358 Build_Discriminal (Id);
17359
17360 Next (Discr);
17361 Discr_Number := Discr_Number + 1;
17362 end loop;
17363
17364 Set_Has_Discriminants (Current_Scope);
17365 end Process_Discriminants;
17366
17367 -----------------------
17368 -- Process_Full_View --
17369 -----------------------
17370
17371 procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
17372 Priv_Parent : Entity_Id;
17373 Full_Parent : Entity_Id;
17374 Full_Indic : Node_Id;
17375
17376 procedure Collect_Implemented_Interfaces
17377 (Typ : Entity_Id;
17378 Ifaces : Elist_Id);
17379 -- Ada 2005: Gather all the interfaces that Typ directly or
17380 -- inherently implements. Duplicate entries are not added to
17381 -- the list Ifaces.
17382
17383 ------------------------------------
17384 -- Collect_Implemented_Interfaces --
17385 ------------------------------------
17386
17387 procedure Collect_Implemented_Interfaces
17388 (Typ : Entity_Id;
17389 Ifaces : Elist_Id)
17390 is
17391 Iface : Entity_Id;
17392 Iface_Elmt : Elmt_Id;
17393
17394 begin
17395 -- Abstract interfaces are only associated with tagged record types
17396
17397 if not Is_Tagged_Type (Typ)
17398 or else not Is_Record_Type (Typ)
17399 then
17400 return;
17401 end if;
17402
17403 -- Recursively climb to the ancestors
17404
17405 if Etype (Typ) /= Typ
17406
17407 -- Protect the frontend against wrong cyclic declarations like:
17408
17409 -- type B is new A with private;
17410 -- type C is new A with private;
17411 -- private
17412 -- type B is new C with null record;
17413 -- type C is new B with null record;
17414
17415 and then Etype (Typ) /= Priv_T
17416 and then Etype (Typ) /= Full_T
17417 then
17418 -- Keep separate the management of private type declarations
17419
17420 if Ekind (Typ) = E_Record_Type_With_Private then
17421
17422 -- Handle the following erroneous case:
17423 -- type Private_Type is tagged private;
17424 -- private
17425 -- type Private_Type is new Type_Implementing_Iface;
17426
17427 if Present (Full_View (Typ))
17428 and then Etype (Typ) /= Full_View (Typ)
17429 then
17430 if Is_Interface (Etype (Typ)) then
17431 Append_Unique_Elmt (Etype (Typ), Ifaces);
17432 end if;
17433
17434 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
17435 end if;
17436
17437 -- Non-private types
17438
17439 else
17440 if Is_Interface (Etype (Typ)) then
17441 Append_Unique_Elmt (Etype (Typ), Ifaces);
17442 end if;
17443
17444 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
17445 end if;
17446 end if;
17447
17448 -- Handle entities in the list of abstract interfaces
17449
17450 if Present (Interfaces (Typ)) then
17451 Iface_Elmt := First_Elmt (Interfaces (Typ));
17452 while Present (Iface_Elmt) loop
17453 Iface := Node (Iface_Elmt);
17454
17455 pragma Assert (Is_Interface (Iface));
17456
17457 if not Contain_Interface (Iface, Ifaces) then
17458 Append_Elmt (Iface, Ifaces);
17459 Collect_Implemented_Interfaces (Iface, Ifaces);
17460 end if;
17461
17462 Next_Elmt (Iface_Elmt);
17463 end loop;
17464 end if;
17465 end Collect_Implemented_Interfaces;
17466
17467 -- Start of processing for Process_Full_View
17468
17469 begin
17470 -- First some sanity checks that must be done after semantic
17471 -- decoration of the full view and thus cannot be placed with other
17472 -- similar checks in Find_Type_Name
17473
17474 if not Is_Limited_Type (Priv_T)
17475 and then (Is_Limited_Type (Full_T)
17476 or else Is_Limited_Composite (Full_T))
17477 then
17478 if In_Instance then
17479 null;
17480 else
17481 Error_Msg_N
17482 ("completion of nonlimited type cannot be limited", Full_T);
17483 Explain_Limited_Type (Full_T, Full_T);
17484 end if;
17485
17486 elsif Is_Abstract_Type (Full_T)
17487 and then not Is_Abstract_Type (Priv_T)
17488 then
17489 Error_Msg_N
17490 ("completion of nonabstract type cannot be abstract", Full_T);
17491
17492 elsif Is_Tagged_Type (Priv_T)
17493 and then Is_Limited_Type (Priv_T)
17494 and then not Is_Limited_Type (Full_T)
17495 then
17496 -- If pragma CPP_Class was applied to the private declaration
17497 -- propagate the limitedness to the full-view
17498
17499 if Is_CPP_Class (Priv_T) then
17500 Set_Is_Limited_Record (Full_T);
17501
17502 -- GNAT allow its own definition of Limited_Controlled to disobey
17503 -- this rule in order in ease the implementation. This test is safe
17504 -- because Root_Controlled is defined in a child of System that
17505 -- normal programs are not supposed to use.
17506
17507 elsif Is_RTE (Etype (Full_T), RE_Root_Controlled) then
17508 Set_Is_Limited_Composite (Full_T);
17509 else
17510 Error_Msg_N
17511 ("completion of limited tagged type must be limited", Full_T);
17512 end if;
17513
17514 elsif Is_Generic_Type (Priv_T) then
17515 Error_Msg_N ("generic type cannot have a completion", Full_T);
17516 end if;
17517
17518 -- Check that ancestor interfaces of private and full views are
17519 -- consistent. We omit this check for synchronized types because
17520 -- they are performed on the corresponding record type when frozen.
17521
17522 if Ada_Version >= Ada_2005
17523 and then Is_Tagged_Type (Priv_T)
17524 and then Is_Tagged_Type (Full_T)
17525 and then not Is_Concurrent_Type (Full_T)
17526 then
17527 declare
17528 Iface : Entity_Id;
17529 Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
17530 Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
17531
17532 begin
17533 Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
17534 Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
17535
17536 -- Ada 2005 (AI-251): The partial view shall be a descendant of
17537 -- an interface type if and only if the full type is descendant
17538 -- of the interface type (AARM 7.3 (7.3/2)).
17539
17540 Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
17541
17542 if Present (Iface) then
17543 Error_Msg_NE
17544 ("interface & not implemented by full type " &
17545 "(RM-2005 7.3 (7.3/2))", Priv_T, Iface);
17546 end if;
17547
17548 Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
17549
17550 if Present (Iface) then
17551 Error_Msg_NE
17552 ("interface & not implemented by partial view " &
17553 "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
17554 end if;
17555 end;
17556 end if;
17557
17558 if Is_Tagged_Type (Priv_T)
17559 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
17560 and then Is_Derived_Type (Full_T)
17561 then
17562 Priv_Parent := Etype (Priv_T);
17563
17564 -- The full view of a private extension may have been transformed
17565 -- into an unconstrained derived type declaration and a subtype
17566 -- declaration (see build_derived_record_type for details).
17567
17568 if Nkind (N) = N_Subtype_Declaration then
17569 Full_Indic := Subtype_Indication (N);
17570 Full_Parent := Etype (Base_Type (Full_T));
17571 else
17572 Full_Indic := Subtype_Indication (Type_Definition (N));
17573 Full_Parent := Etype (Full_T);
17574 end if;
17575
17576 -- Check that the parent type of the full type is a descendant of
17577 -- the ancestor subtype given in the private extension. If either
17578 -- entity has an Etype equal to Any_Type then we had some previous
17579 -- error situation [7.3(8)].
17580
17581 if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
17582 return;
17583
17584 -- Ada 2005 (AI-251): Interfaces in the full-typ can be given in
17585 -- any order. Therefore we don't have to check that its parent must
17586 -- be a descendant of the parent of the private type declaration.
17587
17588 elsif Is_Interface (Priv_Parent)
17589 and then Is_Interface (Full_Parent)
17590 then
17591 null;
17592
17593 -- Ada 2005 (AI-251): If the parent of the private type declaration
17594 -- is an interface there is no need to check that it is an ancestor
17595 -- of the associated full type declaration. The required tests for
17596 -- this case are performed by Build_Derived_Record_Type.
17597
17598 elsif not Is_Interface (Base_Type (Priv_Parent))
17599 and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
17600 then
17601 Error_Msg_N
17602 ("parent of full type must descend from parent"
17603 & " of private extension", Full_Indic);
17604
17605 -- First check a formal restriction, and then proceed with checking
17606 -- Ada rules. Since the formal restriction is not a serious error, we
17607 -- don't prevent further error detection for this check, hence the
17608 -- ELSE.
17609
17610 else
17611
17612 -- In formal mode, when completing a private extension the type
17613 -- named in the private part must be exactly the same as that
17614 -- named in the visible part.
17615
17616 if Priv_Parent /= Full_Parent then
17617 Error_Msg_Name_1 := Chars (Priv_Parent);
17618 Check_SPARK_Restriction ("% expected", Full_Indic);
17619 end if;
17620
17621 -- Check the rules of 7.3(10): if the private extension inherits
17622 -- known discriminants, then the full type must also inherit those
17623 -- discriminants from the same (ancestor) type, and the parent
17624 -- subtype of the full type must be constrained if and only if
17625 -- the ancestor subtype of the private extension is constrained.
17626
17627 if No (Discriminant_Specifications (Parent (Priv_T)))
17628 and then not Has_Unknown_Discriminants (Priv_T)
17629 and then Has_Discriminants (Base_Type (Priv_Parent))
17630 then
17631 declare
17632 Priv_Indic : constant Node_Id :=
17633 Subtype_Indication (Parent (Priv_T));
17634
17635 Priv_Constr : constant Boolean :=
17636 Is_Constrained (Priv_Parent)
17637 or else
17638 Nkind (Priv_Indic) = N_Subtype_Indication
17639 or else
17640 Is_Constrained (Entity (Priv_Indic));
17641
17642 Full_Constr : constant Boolean :=
17643 Is_Constrained (Full_Parent)
17644 or else
17645 Nkind (Full_Indic) = N_Subtype_Indication
17646 or else
17647 Is_Constrained (Entity (Full_Indic));
17648
17649 Priv_Discr : Entity_Id;
17650 Full_Discr : Entity_Id;
17651
17652 begin
17653 Priv_Discr := First_Discriminant (Priv_Parent);
17654 Full_Discr := First_Discriminant (Full_Parent);
17655 while Present (Priv_Discr) and then Present (Full_Discr) loop
17656 if Original_Record_Component (Priv_Discr) =
17657 Original_Record_Component (Full_Discr)
17658 or else
17659 Corresponding_Discriminant (Priv_Discr) =
17660 Corresponding_Discriminant (Full_Discr)
17661 then
17662 null;
17663 else
17664 exit;
17665 end if;
17666
17667 Next_Discriminant (Priv_Discr);
17668 Next_Discriminant (Full_Discr);
17669 end loop;
17670
17671 if Present (Priv_Discr) or else Present (Full_Discr) then
17672 Error_Msg_N
17673 ("full view must inherit discriminants of the parent"
17674 & " type used in the private extension", Full_Indic);
17675
17676 elsif Priv_Constr and then not Full_Constr then
17677 Error_Msg_N
17678 ("parent subtype of full type must be constrained",
17679 Full_Indic);
17680
17681 elsif Full_Constr and then not Priv_Constr then
17682 Error_Msg_N
17683 ("parent subtype of full type must be unconstrained",
17684 Full_Indic);
17685 end if;
17686 end;
17687
17688 -- Check the rules of 7.3(12): if a partial view has neither
17689 -- known or unknown discriminants, then the full type
17690 -- declaration shall define a definite subtype.
17691
17692 elsif not Has_Unknown_Discriminants (Priv_T)
17693 and then not Has_Discriminants (Priv_T)
17694 and then not Is_Constrained (Full_T)
17695 then
17696 Error_Msg_N
17697 ("full view must define a constrained type if partial view"
17698 & " has no discriminants", Full_T);
17699 end if;
17700
17701 -- ??????? Do we implement the following properly ?????
17702 -- If the ancestor subtype of a private extension has constrained
17703 -- discriminants, then the parent subtype of the full view shall
17704 -- impose a statically matching constraint on those discriminants
17705 -- [7.3(13)].
17706 end if;
17707
17708 else
17709 -- For untagged types, verify that a type without discriminants
17710 -- is not completed with an unconstrained type.
17711
17712 if not Is_Indefinite_Subtype (Priv_T)
17713 and then Is_Indefinite_Subtype (Full_T)
17714 then
17715 Error_Msg_N ("full view of type must be definite subtype", Full_T);
17716 end if;
17717 end if;
17718
17719 -- AI-419: verify that the use of "limited" is consistent
17720
17721 declare
17722 Orig_Decl : constant Node_Id := Original_Node (N);
17723
17724 begin
17725 if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
17726 and then not Limited_Present (Parent (Priv_T))
17727 and then not Synchronized_Present (Parent (Priv_T))
17728 and then Nkind (Orig_Decl) = N_Full_Type_Declaration
17729 and then Nkind
17730 (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
17731 and then Limited_Present (Type_Definition (Orig_Decl))
17732 then
17733 Error_Msg_N
17734 ("full view of non-limited extension cannot be limited", N);
17735 end if;
17736 end;
17737
17738 -- Ada 2005 (AI-443): A synchronized private extension must be
17739 -- completed by a task or protected type.
17740
17741 if Ada_Version >= Ada_2005
17742 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
17743 and then Synchronized_Present (Parent (Priv_T))
17744 and then not Is_Concurrent_Type (Full_T)
17745 then
17746 Error_Msg_N ("full view of synchronized extension must " &
17747 "be synchronized type", N);
17748 end if;
17749
17750 -- Ada 2005 AI-363: if the full view has discriminants with
17751 -- defaults, it is illegal to declare constrained access subtypes
17752 -- whose designated type is the current type. This allows objects
17753 -- of the type that are declared in the heap to be unconstrained.
17754
17755 if not Has_Unknown_Discriminants (Priv_T)
17756 and then not Has_Discriminants (Priv_T)
17757 and then Has_Discriminants (Full_T)
17758 and then
17759 Present (Discriminant_Default_Value (First_Discriminant (Full_T)))
17760 then
17761 Set_Has_Constrained_Partial_View (Full_T);
17762 Set_Has_Constrained_Partial_View (Priv_T);
17763 end if;
17764
17765 -- Create a full declaration for all its subtypes recorded in
17766 -- Private_Dependents and swap them similarly to the base type. These
17767 -- are subtypes that have been define before the full declaration of
17768 -- the private type. We also swap the entry in Private_Dependents list
17769 -- so we can properly restore the private view on exit from the scope.
17770
17771 declare
17772 Priv_Elmt : Elmt_Id;
17773 Priv : Entity_Id;
17774 Full : Entity_Id;
17775
17776 begin
17777 Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
17778 while Present (Priv_Elmt) loop
17779 Priv := Node (Priv_Elmt);
17780
17781 if Ekind_In (Priv, E_Private_Subtype,
17782 E_Limited_Private_Subtype,
17783 E_Record_Subtype_With_Private)
17784 then
17785 Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
17786 Set_Is_Itype (Full);
17787 Set_Parent (Full, Parent (Priv));
17788 Set_Associated_Node_For_Itype (Full, N);
17789
17790 -- Now we need to complete the private subtype, but since the
17791 -- base type has already been swapped, we must also swap the
17792 -- subtypes (and thus, reverse the arguments in the call to
17793 -- Complete_Private_Subtype).
17794
17795 Copy_And_Swap (Priv, Full);
17796 Complete_Private_Subtype (Full, Priv, Full_T, N);
17797 Replace_Elmt (Priv_Elmt, Full);
17798 end if;
17799
17800 Next_Elmt (Priv_Elmt);
17801 end loop;
17802 end;
17803
17804 -- If the private view was tagged, copy the new primitive operations
17805 -- from the private view to the full view.
17806
17807 if Is_Tagged_Type (Full_T) then
17808 declare
17809 Disp_Typ : Entity_Id;
17810 Full_List : Elist_Id;
17811 Prim : Entity_Id;
17812 Prim_Elmt : Elmt_Id;
17813 Priv_List : Elist_Id;
17814
17815 function Contains
17816 (E : Entity_Id;
17817 L : Elist_Id) return Boolean;
17818 -- Determine whether list L contains element E
17819
17820 --------------
17821 -- Contains --
17822 --------------
17823
17824 function Contains
17825 (E : Entity_Id;
17826 L : Elist_Id) return Boolean
17827 is
17828 List_Elmt : Elmt_Id;
17829
17830 begin
17831 List_Elmt := First_Elmt (L);
17832 while Present (List_Elmt) loop
17833 if Node (List_Elmt) = E then
17834 return True;
17835 end if;
17836
17837 Next_Elmt (List_Elmt);
17838 end loop;
17839
17840 return False;
17841 end Contains;
17842
17843 -- Start of processing
17844
17845 begin
17846 if Is_Tagged_Type (Priv_T) then
17847 Priv_List := Primitive_Operations (Priv_T);
17848 Prim_Elmt := First_Elmt (Priv_List);
17849
17850 -- In the case of a concurrent type completing a private tagged
17851 -- type, primitives may have been declared in between the two
17852 -- views. These subprograms need to be wrapped the same way
17853 -- entries and protected procedures are handled because they
17854 -- cannot be directly shared by the two views.
17855
17856 if Is_Concurrent_Type (Full_T) then
17857 declare
17858 Conc_Typ : constant Entity_Id :=
17859 Corresponding_Record_Type (Full_T);
17860 Curr_Nod : Node_Id := Parent (Conc_Typ);
17861 Wrap_Spec : Node_Id;
17862
17863 begin
17864 while Present (Prim_Elmt) loop
17865 Prim := Node (Prim_Elmt);
17866
17867 if Comes_From_Source (Prim)
17868 and then not Is_Abstract_Subprogram (Prim)
17869 then
17870 Wrap_Spec :=
17871 Make_Subprogram_Declaration (Sloc (Prim),
17872 Specification =>
17873 Build_Wrapper_Spec
17874 (Subp_Id => Prim,
17875 Obj_Typ => Conc_Typ,
17876 Formals =>
17877 Parameter_Specifications (
17878 Parent (Prim))));
17879
17880 Insert_After (Curr_Nod, Wrap_Spec);
17881 Curr_Nod := Wrap_Spec;
17882
17883 Analyze (Wrap_Spec);
17884 end if;
17885
17886 Next_Elmt (Prim_Elmt);
17887 end loop;
17888
17889 return;
17890 end;
17891
17892 -- For non-concurrent types, transfer explicit primitives, but
17893 -- omit those inherited from the parent of the private view
17894 -- since they will be re-inherited later on.
17895
17896 else
17897 Full_List := Primitive_Operations (Full_T);
17898
17899 while Present (Prim_Elmt) loop
17900 Prim := Node (Prim_Elmt);
17901
17902 if Comes_From_Source (Prim)
17903 and then not Contains (Prim, Full_List)
17904 then
17905 Append_Elmt (Prim, Full_List);
17906 end if;
17907
17908 Next_Elmt (Prim_Elmt);
17909 end loop;
17910 end if;
17911
17912 -- Untagged private view
17913
17914 else
17915 Full_List := Primitive_Operations (Full_T);
17916
17917 -- In this case the partial view is untagged, so here we locate
17918 -- all of the earlier primitives that need to be treated as
17919 -- dispatching (those that appear between the two views). Note
17920 -- that these additional operations must all be new operations
17921 -- (any earlier operations that override inherited operations
17922 -- of the full view will already have been inserted in the
17923 -- primitives list, marked by Check_Operation_From_Private_View
17924 -- as dispatching. Note that implicit "/=" operators are
17925 -- excluded from being added to the primitives list since they
17926 -- shouldn't be treated as dispatching (tagged "/=" is handled
17927 -- specially).
17928
17929 Prim := Next_Entity (Full_T);
17930 while Present (Prim) and then Prim /= Priv_T loop
17931 if Ekind_In (Prim, E_Procedure, E_Function) then
17932 Disp_Typ := Find_Dispatching_Type (Prim);
17933
17934 if Disp_Typ = Full_T
17935 and then (Chars (Prim) /= Name_Op_Ne
17936 or else Comes_From_Source (Prim))
17937 then
17938 Check_Controlling_Formals (Full_T, Prim);
17939
17940 if not Is_Dispatching_Operation (Prim) then
17941 Append_Elmt (Prim, Full_List);
17942 Set_Is_Dispatching_Operation (Prim, True);
17943 Set_DT_Position (Prim, No_Uint);
17944 end if;
17945
17946 elsif Is_Dispatching_Operation (Prim)
17947 and then Disp_Typ /= Full_T
17948 then
17949
17950 -- Verify that it is not otherwise controlled by a
17951 -- formal or a return value of type T.
17952
17953 Check_Controlling_Formals (Disp_Typ, Prim);
17954 end if;
17955 end if;
17956
17957 Next_Entity (Prim);
17958 end loop;
17959 end if;
17960
17961 -- For the tagged case, the two views can share the same primitive
17962 -- operations list and the same class-wide type. Update attributes
17963 -- of the class-wide type which depend on the full declaration.
17964
17965 if Is_Tagged_Type (Priv_T) then
17966 Set_Direct_Primitive_Operations (Priv_T, Full_List);
17967 Set_Class_Wide_Type
17968 (Base_Type (Full_T), Class_Wide_Type (Priv_T));
17969
17970 Set_Has_Task (Class_Wide_Type (Priv_T), Has_Task (Full_T));
17971 end if;
17972 end;
17973 end if;
17974
17975 -- Ada 2005 AI 161: Check preelaboratable initialization consistency
17976
17977 if Known_To_Have_Preelab_Init (Priv_T) then
17978
17979 -- Case where there is a pragma Preelaborable_Initialization. We
17980 -- always allow this in predefined units, which is a bit of a kludge,
17981 -- but it means we don't have to struggle to meet the requirements in
17982 -- the RM for having Preelaborable Initialization. Otherwise we
17983 -- require that the type meets the RM rules. But we can't check that
17984 -- yet, because of the rule about overriding Initialize, so we simply
17985 -- set a flag that will be checked at freeze time.
17986
17987 if not In_Predefined_Unit (Full_T) then
17988 Set_Must_Have_Preelab_Init (Full_T);
17989 end if;
17990 end if;
17991
17992 -- If pragma CPP_Class was applied to the private type declaration,
17993 -- propagate it now to the full type declaration.
17994
17995 if Is_CPP_Class (Priv_T) then
17996 Set_Is_CPP_Class (Full_T);
17997 Set_Convention (Full_T, Convention_CPP);
17998 end if;
17999
18000 -- If the private view has user specified stream attributes, then so has
18001 -- the full view.
18002
18003 -- Why the test, how could these flags be already set in Full_T ???
18004
18005 if Has_Specified_Stream_Read (Priv_T) then
18006 Set_Has_Specified_Stream_Read (Full_T);
18007 end if;
18008
18009 if Has_Specified_Stream_Write (Priv_T) then
18010 Set_Has_Specified_Stream_Write (Full_T);
18011 end if;
18012
18013 if Has_Specified_Stream_Input (Priv_T) then
18014 Set_Has_Specified_Stream_Input (Full_T);
18015 end if;
18016
18017 if Has_Specified_Stream_Output (Priv_T) then
18018 Set_Has_Specified_Stream_Output (Full_T);
18019 end if;
18020
18021 -- Propagate invariants to full type
18022
18023 if Has_Invariants (Priv_T) then
18024 Set_Has_Invariants (Full_T);
18025 Set_Invariant_Procedure (Full_T, Invariant_Procedure (Priv_T));
18026 end if;
18027
18028 if Has_Inheritable_Invariants (Priv_T) then
18029 Set_Has_Inheritable_Invariants (Full_T);
18030 end if;
18031
18032 -- Propagate predicates to full type
18033
18034 if Has_Predicates (Priv_T) then
18035 Set_Predicate_Function (Priv_T, Predicate_Function (Full_T));
18036 Set_Has_Predicates (Priv_T);
18037 end if;
18038 end Process_Full_View;
18039
18040 -----------------------------------
18041 -- Process_Incomplete_Dependents --
18042 -----------------------------------
18043
18044 procedure Process_Incomplete_Dependents
18045 (N : Node_Id;
18046 Full_T : Entity_Id;
18047 Inc_T : Entity_Id)
18048 is
18049 Inc_Elmt : Elmt_Id;
18050 Priv_Dep : Entity_Id;
18051 New_Subt : Entity_Id;
18052
18053 Disc_Constraint : Elist_Id;
18054
18055 begin
18056 if No (Private_Dependents (Inc_T)) then
18057 return;
18058 end if;
18059
18060 -- Itypes that may be generated by the completion of an incomplete
18061 -- subtype are not used by the back-end and not attached to the tree.
18062 -- They are created only for constraint-checking purposes.
18063
18064 Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
18065 while Present (Inc_Elmt) loop
18066 Priv_Dep := Node (Inc_Elmt);
18067
18068 if Ekind (Priv_Dep) = E_Subprogram_Type then
18069
18070 -- An Access_To_Subprogram type may have a return type or a
18071 -- parameter type that is incomplete. Replace with the full view.
18072
18073 if Etype (Priv_Dep) = Inc_T then
18074 Set_Etype (Priv_Dep, Full_T);
18075 end if;
18076
18077 declare
18078 Formal : Entity_Id;
18079
18080 begin
18081 Formal := First_Formal (Priv_Dep);
18082 while Present (Formal) loop
18083 if Etype (Formal) = Inc_T then
18084 Set_Etype (Formal, Full_T);
18085 end if;
18086
18087 Next_Formal (Formal);
18088 end loop;
18089 end;
18090
18091 elsif Is_Overloadable (Priv_Dep) then
18092
18093 -- If a subprogram in the incomplete dependents list is primitive
18094 -- for a tagged full type then mark it as a dispatching operation,
18095 -- check whether it overrides an inherited subprogram, and check
18096 -- restrictions on its controlling formals. Note that a protected
18097 -- operation is never dispatching: only its wrapper operation
18098 -- (which has convention Ada) is.
18099
18100 if Is_Tagged_Type (Full_T)
18101 and then Is_Primitive (Priv_Dep)
18102 and then Convention (Priv_Dep) /= Convention_Protected
18103 then
18104 Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
18105 Set_Is_Dispatching_Operation (Priv_Dep);
18106 Check_Controlling_Formals (Full_T, Priv_Dep);
18107 end if;
18108
18109 elsif Ekind (Priv_Dep) = E_Subprogram_Body then
18110
18111 -- Can happen during processing of a body before the completion
18112 -- of a TA type. Ignore, because spec is also on dependent list.
18113
18114 return;
18115
18116 -- Ada 2005 (AI-412): Transform a regular incomplete subtype into a
18117 -- corresponding subtype of the full view.
18118
18119 elsif Ekind (Priv_Dep) = E_Incomplete_Subtype then
18120 Set_Subtype_Indication
18121 (Parent (Priv_Dep), New_Reference_To (Full_T, Sloc (Priv_Dep)));
18122 Set_Etype (Priv_Dep, Full_T);
18123 Set_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
18124 Set_Analyzed (Parent (Priv_Dep), False);
18125
18126 -- Reanalyze the declaration, suppressing the call to
18127 -- Enter_Name to avoid duplicate names.
18128
18129 Analyze_Subtype_Declaration
18130 (N => Parent (Priv_Dep),
18131 Skip => True);
18132
18133 -- Dependent is a subtype
18134
18135 else
18136 -- We build a new subtype indication using the full view of the
18137 -- incomplete parent. The discriminant constraints have been
18138 -- elaborated already at the point of the subtype declaration.
18139
18140 New_Subt := Create_Itype (E_Void, N);
18141
18142 if Has_Discriminants (Full_T) then
18143 Disc_Constraint := Discriminant_Constraint (Priv_Dep);
18144 else
18145 Disc_Constraint := No_Elist;
18146 end if;
18147
18148 Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
18149 Set_Full_View (Priv_Dep, New_Subt);
18150 end if;
18151
18152 Next_Elmt (Inc_Elmt);
18153 end loop;
18154 end Process_Incomplete_Dependents;
18155
18156 --------------------------------
18157 -- Process_Range_Expr_In_Decl --
18158 --------------------------------
18159
18160 procedure Process_Range_Expr_In_Decl
18161 (R : Node_Id;
18162 T : Entity_Id;
18163 Check_List : List_Id := Empty_List;
18164 R_Check_Off : Boolean := False;
18165 In_Iter_Schm : Boolean := False)
18166 is
18167 Lo, Hi : Node_Id;
18168 R_Checks : Check_Result;
18169 Insert_Node : Node_Id;
18170 Def_Id : Entity_Id;
18171
18172 begin
18173 Analyze_And_Resolve (R, Base_Type (T));
18174
18175 if Nkind (R) = N_Range then
18176
18177 -- In SPARK, all ranges should be static, with the exception of the
18178 -- discrete type definition of a loop parameter specification.
18179
18180 if not In_Iter_Schm
18181 and then not Is_Static_Range (R)
18182 then
18183 Check_SPARK_Restriction ("range should be static", R);
18184 end if;
18185
18186 Lo := Low_Bound (R);
18187 Hi := High_Bound (R);
18188
18189 -- We need to ensure validity of the bounds here, because if we
18190 -- go ahead and do the expansion, then the expanded code will get
18191 -- analyzed with range checks suppressed and we miss the check.
18192
18193 Validity_Check_Range (R);
18194
18195 -- If there were errors in the declaration, try and patch up some
18196 -- common mistakes in the bounds. The cases handled are literals
18197 -- which are Integer where the expected type is Real and vice versa.
18198 -- These corrections allow the compilation process to proceed further
18199 -- along since some basic assumptions of the format of the bounds
18200 -- are guaranteed.
18201
18202 if Etype (R) = Any_Type then
18203
18204 if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
18205 Rewrite (Lo,
18206 Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
18207
18208 elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
18209 Rewrite (Hi,
18210 Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
18211
18212 elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
18213 Rewrite (Lo,
18214 Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
18215
18216 elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
18217 Rewrite (Hi,
18218 Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
18219 end if;
18220
18221 Set_Etype (Lo, T);
18222 Set_Etype (Hi, T);
18223 end if;
18224
18225 -- If the bounds of the range have been mistakenly given as string
18226 -- literals (perhaps in place of character literals), then an error
18227 -- has already been reported, but we rewrite the string literal as a
18228 -- bound of the range's type to avoid blowups in later processing
18229 -- that looks at static values.
18230
18231 if Nkind (Lo) = N_String_Literal then
18232 Rewrite (Lo,
18233 Make_Attribute_Reference (Sloc (Lo),
18234 Attribute_Name => Name_First,
18235 Prefix => New_Reference_To (T, Sloc (Lo))));
18236 Analyze_And_Resolve (Lo);
18237 end if;
18238
18239 if Nkind (Hi) = N_String_Literal then
18240 Rewrite (Hi,
18241 Make_Attribute_Reference (Sloc (Hi),
18242 Attribute_Name => Name_First,
18243 Prefix => New_Reference_To (T, Sloc (Hi))));
18244 Analyze_And_Resolve (Hi);
18245 end if;
18246
18247 -- If bounds aren't scalar at this point then exit, avoiding
18248 -- problems with further processing of the range in this procedure.
18249
18250 if not Is_Scalar_Type (Etype (Lo)) then
18251 return;
18252 end if;
18253
18254 -- Resolve (actually Sem_Eval) has checked that the bounds are in
18255 -- then range of the base type. Here we check whether the bounds
18256 -- are in the range of the subtype itself. Note that if the bounds
18257 -- represent the null range the Constraint_Error exception should
18258 -- not be raised.
18259
18260 -- ??? The following code should be cleaned up as follows
18261
18262 -- 1. The Is_Null_Range (Lo, Hi) test should disappear since it
18263 -- is done in the call to Range_Check (R, T); below
18264
18265 -- 2. The use of R_Check_Off should be investigated and possibly
18266 -- removed, this would clean up things a bit.
18267
18268 if Is_Null_Range (Lo, Hi) then
18269 null;
18270
18271 else
18272 -- Capture values of bounds and generate temporaries for them
18273 -- if needed, before applying checks, since checks may cause
18274 -- duplication of the expression without forcing evaluation.
18275
18276 if Expander_Active then
18277 Force_Evaluation (Lo);
18278 Force_Evaluation (Hi);
18279 end if;
18280
18281 -- We use a flag here instead of suppressing checks on the
18282 -- type because the type we check against isn't necessarily
18283 -- the place where we put the check.
18284
18285 if not R_Check_Off then
18286 R_Checks := Get_Range_Checks (R, T);
18287
18288 -- Look up tree to find an appropriate insertion point. We
18289 -- can't just use insert_actions because later processing
18290 -- depends on the insertion node. Prior to Ada2012 the
18291 -- insertion point could only be a declaration or a loop, but
18292 -- quantified expressions can appear within any context in an
18293 -- expression, and the insertion point can be any statement,
18294 -- pragma, or declaration.
18295
18296 Insert_Node := Parent (R);
18297 while Present (Insert_Node) loop
18298 exit when
18299 Nkind (Insert_Node) in N_Declaration
18300 and then
18301 not Nkind_In
18302 (Insert_Node, N_Component_Declaration,
18303 N_Loop_Parameter_Specification,
18304 N_Function_Specification,
18305 N_Procedure_Specification);
18306
18307 exit when Nkind (Insert_Node) in N_Later_Decl_Item
18308 or else Nkind (Insert_Node) in
18309 N_Statement_Other_Than_Procedure_Call
18310 or else Nkind_In (Insert_Node, N_Procedure_Call_Statement,
18311 N_Pragma);
18312
18313 Insert_Node := Parent (Insert_Node);
18314 end loop;
18315
18316 -- Why would Type_Decl not be present??? Without this test,
18317 -- short regression tests fail.
18318
18319 if Present (Insert_Node) then
18320
18321 -- Case of loop statement. Verify that the range is part
18322 -- of the subtype indication of the iteration scheme.
18323
18324 if Nkind (Insert_Node) = N_Loop_Statement then
18325 declare
18326 Indic : Node_Id;
18327
18328 begin
18329 Indic := Parent (R);
18330 while Present (Indic)
18331 and then Nkind (Indic) /= N_Subtype_Indication
18332 loop
18333 Indic := Parent (Indic);
18334 end loop;
18335
18336 if Present (Indic) then
18337 Def_Id := Etype (Subtype_Mark (Indic));
18338
18339 Insert_Range_Checks
18340 (R_Checks,
18341 Insert_Node,
18342 Def_Id,
18343 Sloc (Insert_Node),
18344 R,
18345 Do_Before => True);
18346 end if;
18347 end;
18348
18349 -- Insertion before a declaration. If the declaration
18350 -- includes discriminants, the list of applicable checks
18351 -- is given by the caller.
18352
18353 elsif Nkind (Insert_Node) in N_Declaration then
18354 Def_Id := Defining_Identifier (Insert_Node);
18355
18356 if (Ekind (Def_Id) = E_Record_Type
18357 and then Depends_On_Discriminant (R))
18358 or else
18359 (Ekind (Def_Id) = E_Protected_Type
18360 and then Has_Discriminants (Def_Id))
18361 then
18362 Append_Range_Checks
18363 (R_Checks,
18364 Check_List, Def_Id, Sloc (Insert_Node), R);
18365
18366 else
18367 Insert_Range_Checks
18368 (R_Checks,
18369 Insert_Node, Def_Id, Sloc (Insert_Node), R);
18370
18371 end if;
18372
18373 -- Insertion before a statement. Range appears in the
18374 -- context of a quantified expression. Insertion will
18375 -- take place when expression is expanded.
18376
18377 else
18378 null;
18379 end if;
18380 end if;
18381 end if;
18382 end if;
18383
18384 -- Case of other than an explicit N_Range node
18385
18386 elsif Expander_Active then
18387 Get_Index_Bounds (R, Lo, Hi);
18388 Force_Evaluation (Lo);
18389 Force_Evaluation (Hi);
18390 end if;
18391 end Process_Range_Expr_In_Decl;
18392
18393 --------------------------------------
18394 -- Process_Real_Range_Specification --
18395 --------------------------------------
18396
18397 procedure Process_Real_Range_Specification (Def : Node_Id) is
18398 Spec : constant Node_Id := Real_Range_Specification (Def);
18399 Lo : Node_Id;
18400 Hi : Node_Id;
18401 Err : Boolean := False;
18402
18403 procedure Analyze_Bound (N : Node_Id);
18404 -- Analyze and check one bound
18405
18406 -------------------
18407 -- Analyze_Bound --
18408 -------------------
18409
18410 procedure Analyze_Bound (N : Node_Id) is
18411 begin
18412 Analyze_And_Resolve (N, Any_Real);
18413
18414 if not Is_OK_Static_Expression (N) then
18415 Flag_Non_Static_Expr
18416 ("bound in real type definition is not static!", N);
18417 Err := True;
18418 end if;
18419 end Analyze_Bound;
18420
18421 -- Start of processing for Process_Real_Range_Specification
18422
18423 begin
18424 if Present (Spec) then
18425 Lo := Low_Bound (Spec);
18426 Hi := High_Bound (Spec);
18427 Analyze_Bound (Lo);
18428 Analyze_Bound (Hi);
18429
18430 -- If error, clear away junk range specification
18431
18432 if Err then
18433 Set_Real_Range_Specification (Def, Empty);
18434 end if;
18435 end if;
18436 end Process_Real_Range_Specification;
18437
18438 ---------------------
18439 -- Process_Subtype --
18440 ---------------------
18441
18442 function Process_Subtype
18443 (S : Node_Id;
18444 Related_Nod : Node_Id;
18445 Related_Id : Entity_Id := Empty;
18446 Suffix : Character := ' ') return Entity_Id
18447 is
18448 P : Node_Id;
18449 Def_Id : Entity_Id;
18450 Error_Node : Node_Id;
18451 Full_View_Id : Entity_Id;
18452 Subtype_Mark_Id : Entity_Id;
18453
18454 May_Have_Null_Exclusion : Boolean;
18455
18456 procedure Check_Incomplete (T : Entity_Id);
18457 -- Called to verify that an incomplete type is not used prematurely
18458
18459 ----------------------
18460 -- Check_Incomplete --
18461 ----------------------
18462
18463 procedure Check_Incomplete (T : Entity_Id) is
18464 begin
18465 -- Ada 2005 (AI-412): Incomplete subtypes are legal
18466
18467 if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
18468 and then
18469 not (Ada_Version >= Ada_2005
18470 and then
18471 (Nkind (Parent (T)) = N_Subtype_Declaration
18472 or else
18473 (Nkind (Parent (T)) = N_Subtype_Indication
18474 and then Nkind (Parent (Parent (T))) =
18475 N_Subtype_Declaration)))
18476 then
18477 Error_Msg_N ("invalid use of type before its full declaration", T);
18478 end if;
18479 end Check_Incomplete;
18480
18481 -- Start of processing for Process_Subtype
18482
18483 begin
18484 -- Case of no constraints present
18485
18486 if Nkind (S) /= N_Subtype_Indication then
18487 Find_Type (S);
18488 Check_Incomplete (S);
18489 P := Parent (S);
18490
18491 -- Ada 2005 (AI-231): Static check
18492
18493 if Ada_Version >= Ada_2005
18494 and then Present (P)
18495 and then Null_Exclusion_Present (P)
18496 and then Nkind (P) /= N_Access_To_Object_Definition
18497 and then not Is_Access_Type (Entity (S))
18498 then
18499 Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
18500 end if;
18501
18502 -- The following is ugly, can't we have a range or even a flag???
18503
18504 May_Have_Null_Exclusion :=
18505 Nkind_In (P, N_Access_Definition,
18506 N_Access_Function_Definition,
18507 N_Access_Procedure_Definition,
18508 N_Access_To_Object_Definition,
18509 N_Allocator,
18510 N_Component_Definition)
18511 or else
18512 Nkind_In (P, N_Derived_Type_Definition,
18513 N_Discriminant_Specification,
18514 N_Formal_Object_Declaration,
18515 N_Object_Declaration,
18516 N_Object_Renaming_Declaration,
18517 N_Parameter_Specification,
18518 N_Subtype_Declaration);
18519
18520 -- Create an Itype that is a duplicate of Entity (S) but with the
18521 -- null-exclusion attribute.
18522
18523 if May_Have_Null_Exclusion
18524 and then Is_Access_Type (Entity (S))
18525 and then Null_Exclusion_Present (P)
18526
18527 -- No need to check the case of an access to object definition.
18528 -- It is correct to define double not-null pointers.
18529
18530 -- Example:
18531 -- type Not_Null_Int_Ptr is not null access Integer;
18532 -- type Acc is not null access Not_Null_Int_Ptr;
18533
18534 and then Nkind (P) /= N_Access_To_Object_Definition
18535 then
18536 if Can_Never_Be_Null (Entity (S)) then
18537 case Nkind (Related_Nod) is
18538 when N_Full_Type_Declaration =>
18539 if Nkind (Type_Definition (Related_Nod))
18540 in N_Array_Type_Definition
18541 then
18542 Error_Node :=
18543 Subtype_Indication
18544 (Component_Definition
18545 (Type_Definition (Related_Nod)));
18546 else
18547 Error_Node :=
18548 Subtype_Indication (Type_Definition (Related_Nod));
18549 end if;
18550
18551 when N_Subtype_Declaration =>
18552 Error_Node := Subtype_Indication (Related_Nod);
18553
18554 when N_Object_Declaration =>
18555 Error_Node := Object_Definition (Related_Nod);
18556
18557 when N_Component_Declaration =>
18558 Error_Node :=
18559 Subtype_Indication (Component_Definition (Related_Nod));
18560
18561 when N_Allocator =>
18562 Error_Node := Expression (Related_Nod);
18563
18564 when others =>
18565 pragma Assert (False);
18566 Error_Node := Related_Nod;
18567 end case;
18568
18569 Error_Msg_NE
18570 ("`NOT NULL` not allowed (& already excludes null)",
18571 Error_Node,
18572 Entity (S));
18573 end if;
18574
18575 Set_Etype (S,
18576 Create_Null_Excluding_Itype
18577 (T => Entity (S),
18578 Related_Nod => P));
18579 Set_Entity (S, Etype (S));
18580 end if;
18581
18582 return Entity (S);
18583
18584 -- Case of constraint present, so that we have an N_Subtype_Indication
18585 -- node (this node is created only if constraints are present).
18586
18587 else
18588 Find_Type (Subtype_Mark (S));
18589
18590 if Nkind (Parent (S)) /= N_Access_To_Object_Definition
18591 and then not
18592 (Nkind (Parent (S)) = N_Subtype_Declaration
18593 and then Is_Itype (Defining_Identifier (Parent (S))))
18594 then
18595 Check_Incomplete (Subtype_Mark (S));
18596 end if;
18597
18598 P := Parent (S);
18599 Subtype_Mark_Id := Entity (Subtype_Mark (S));
18600
18601 -- Explicit subtype declaration case
18602
18603 if Nkind (P) = N_Subtype_Declaration then
18604 Def_Id := Defining_Identifier (P);
18605
18606 -- Explicit derived type definition case
18607
18608 elsif Nkind (P) = N_Derived_Type_Definition then
18609 Def_Id := Defining_Identifier (Parent (P));
18610
18611 -- Implicit case, the Def_Id must be created as an implicit type.
18612 -- The one exception arises in the case of concurrent types, array
18613 -- and access types, where other subsidiary implicit types may be
18614 -- created and must appear before the main implicit type. In these
18615 -- cases we leave Def_Id set to Empty as a signal that Create_Itype
18616 -- has not yet been called to create Def_Id.
18617
18618 else
18619 if Is_Array_Type (Subtype_Mark_Id)
18620 or else Is_Concurrent_Type (Subtype_Mark_Id)
18621 or else Is_Access_Type (Subtype_Mark_Id)
18622 then
18623 Def_Id := Empty;
18624
18625 -- For the other cases, we create a new unattached Itype,
18626 -- and set the indication to ensure it gets attached later.
18627
18628 else
18629 Def_Id :=
18630 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
18631 end if;
18632 end if;
18633
18634 -- If the kind of constraint is invalid for this kind of type,
18635 -- then give an error, and then pretend no constraint was given.
18636
18637 if not Is_Valid_Constraint_Kind
18638 (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
18639 then
18640 Error_Msg_N
18641 ("incorrect constraint for this kind of type", Constraint (S));
18642
18643 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
18644
18645 -- Set Ekind of orphan itype, to prevent cascaded errors
18646
18647 if Present (Def_Id) then
18648 Set_Ekind (Def_Id, Ekind (Any_Type));
18649 end if;
18650
18651 -- Make recursive call, having got rid of the bogus constraint
18652
18653 return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
18654 end if;
18655
18656 -- Remaining processing depends on type. Select on Base_Type kind to
18657 -- ensure getting to the concrete type kind in the case of a private
18658 -- subtype (needed when only doing semantic analysis).
18659
18660 case Ekind (Base_Type (Subtype_Mark_Id)) is
18661 when Access_Kind =>
18662 Constrain_Access (Def_Id, S, Related_Nod);
18663
18664 if Expander_Active
18665 and then Is_Itype (Designated_Type (Def_Id))
18666 and then Nkind (Related_Nod) = N_Subtype_Declaration
18667 and then not Is_Incomplete_Type (Designated_Type (Def_Id))
18668 then
18669 Build_Itype_Reference
18670 (Designated_Type (Def_Id), Related_Nod);
18671 end if;
18672
18673 when Array_Kind =>
18674 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
18675
18676 when Decimal_Fixed_Point_Kind =>
18677 Constrain_Decimal (Def_Id, S);
18678
18679 when Enumeration_Kind =>
18680 Constrain_Enumeration (Def_Id, S);
18681
18682 when Ordinary_Fixed_Point_Kind =>
18683 Constrain_Ordinary_Fixed (Def_Id, S);
18684
18685 when Float_Kind =>
18686 Constrain_Float (Def_Id, S);
18687
18688 when Integer_Kind =>
18689 Constrain_Integer (Def_Id, S);
18690
18691 when E_Record_Type |
18692 E_Record_Subtype |
18693 Class_Wide_Kind |
18694 E_Incomplete_Type =>
18695 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
18696
18697 if Ekind (Def_Id) = E_Incomplete_Type then
18698 Set_Private_Dependents (Def_Id, New_Elmt_List);
18699 end if;
18700
18701 when Private_Kind =>
18702 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
18703 Set_Private_Dependents (Def_Id, New_Elmt_List);
18704
18705 -- In case of an invalid constraint prevent further processing
18706 -- since the type constructed is missing expected fields.
18707
18708 if Etype (Def_Id) = Any_Type then
18709 return Def_Id;
18710 end if;
18711
18712 -- If the full view is that of a task with discriminants,
18713 -- we must constrain both the concurrent type and its
18714 -- corresponding record type. Otherwise we will just propagate
18715 -- the constraint to the full view, if available.
18716
18717 if Present (Full_View (Subtype_Mark_Id))
18718 and then Has_Discriminants (Subtype_Mark_Id)
18719 and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
18720 then
18721 Full_View_Id :=
18722 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
18723
18724 Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
18725 Constrain_Concurrent (Full_View_Id, S,
18726 Related_Nod, Related_Id, Suffix);
18727 Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
18728 Set_Full_View (Def_Id, Full_View_Id);
18729
18730 -- Introduce an explicit reference to the private subtype,
18731 -- to prevent scope anomalies in gigi if first use appears
18732 -- in a nested context, e.g. a later function body.
18733 -- Should this be generated in other contexts than a full
18734 -- type declaration?
18735
18736 if Is_Itype (Def_Id)
18737 and then
18738 Nkind (Parent (P)) = N_Full_Type_Declaration
18739 then
18740 Build_Itype_Reference (Def_Id, Parent (P));
18741 end if;
18742
18743 else
18744 Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
18745 end if;
18746
18747 when Concurrent_Kind =>
18748 Constrain_Concurrent (Def_Id, S,
18749 Related_Nod, Related_Id, Suffix);
18750
18751 when others =>
18752 Error_Msg_N ("invalid subtype mark in subtype indication", S);
18753 end case;
18754
18755 -- Size and Convention are always inherited from the base type
18756
18757 Set_Size_Info (Def_Id, (Subtype_Mark_Id));
18758 Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
18759
18760 return Def_Id;
18761 end if;
18762 end Process_Subtype;
18763
18764 ---------------------------------------
18765 -- Check_Anonymous_Access_Components --
18766 ---------------------------------------
18767
18768 procedure Check_Anonymous_Access_Components
18769 (Typ_Decl : Node_Id;
18770 Typ : Entity_Id;
18771 Prev : Entity_Id;
18772 Comp_List : Node_Id)
18773 is
18774 Loc : constant Source_Ptr := Sloc (Typ_Decl);
18775 Anon_Access : Entity_Id;
18776 Acc_Def : Node_Id;
18777 Comp : Node_Id;
18778 Comp_Def : Node_Id;
18779 Decl : Node_Id;
18780 Type_Def : Node_Id;
18781
18782 procedure Build_Incomplete_Type_Declaration;
18783 -- If the record type contains components that include an access to the
18784 -- current record, then create an incomplete type declaration for the
18785 -- record, to be used as the designated type of the anonymous access.
18786 -- This is done only once, and only if there is no previous partial
18787 -- view of the type.
18788
18789 function Designates_T (Subt : Node_Id) return Boolean;
18790 -- Check whether a node designates the enclosing record type, or 'Class
18791 -- of that type
18792
18793 function Mentions_T (Acc_Def : Node_Id) return Boolean;
18794 -- Check whether an access definition includes a reference to
18795 -- the enclosing record type. The reference can be a subtype mark
18796 -- in the access definition itself, a 'Class attribute reference, or
18797 -- recursively a reference appearing in a parameter specification
18798 -- or result definition of an access_to_subprogram definition.
18799
18800 --------------------------------------
18801 -- Build_Incomplete_Type_Declaration --
18802 --------------------------------------
18803
18804 procedure Build_Incomplete_Type_Declaration is
18805 Decl : Node_Id;
18806 Inc_T : Entity_Id;
18807 H : Entity_Id;
18808
18809 -- Is_Tagged indicates whether the type is tagged. It is tagged if
18810 -- it's "is new ... with record" or else "is tagged record ...".
18811
18812 Is_Tagged : constant Boolean :=
18813 (Nkind (Type_Definition (Typ_Decl)) = N_Derived_Type_Definition
18814 and then
18815 Present
18816 (Record_Extension_Part (Type_Definition (Typ_Decl))))
18817 or else
18818 (Nkind (Type_Definition (Typ_Decl)) = N_Record_Definition
18819 and then Tagged_Present (Type_Definition (Typ_Decl)));
18820
18821 begin
18822 -- If there is a previous partial view, no need to create a new one
18823 -- If the partial view, given by Prev, is incomplete, If Prev is
18824 -- a private declaration, full declaration is flagged accordingly.
18825
18826 if Prev /= Typ then
18827 if Is_Tagged then
18828 Make_Class_Wide_Type (Prev);
18829 Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
18830 Set_Etype (Class_Wide_Type (Typ), Typ);
18831 end if;
18832
18833 return;
18834
18835 elsif Has_Private_Declaration (Typ) then
18836
18837 -- If we refer to T'Class inside T, and T is the completion of a
18838 -- private type, then we need to make sure the class-wide type
18839 -- exists.
18840
18841 if Is_Tagged then
18842 Make_Class_Wide_Type (Typ);
18843 end if;
18844
18845 return;
18846
18847 -- If there was a previous anonymous access type, the incomplete
18848 -- type declaration will have been created already.
18849
18850 elsif Present (Current_Entity (Typ))
18851 and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
18852 and then Full_View (Current_Entity (Typ)) = Typ
18853 then
18854 if Is_Tagged
18855 and then Comes_From_Source (Current_Entity (Typ))
18856 and then not Is_Tagged_Type (Current_Entity (Typ))
18857 then
18858 Make_Class_Wide_Type (Typ);
18859 Error_Msg_N
18860 ("incomplete view of tagged type should be declared tagged?",
18861 Parent (Current_Entity (Typ)));
18862 end if;
18863 return;
18864
18865 else
18866 Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
18867 Decl := Make_Incomplete_Type_Declaration (Loc, Inc_T);
18868
18869 -- Type has already been inserted into the current scope. Remove
18870 -- it, and add incomplete declaration for type, so that subsequent
18871 -- anonymous access types can use it. The entity is unchained from
18872 -- the homonym list and from immediate visibility. After analysis,
18873 -- the entity in the incomplete declaration becomes immediately
18874 -- visible in the record declaration that follows.
18875
18876 H := Current_Entity (Typ);
18877
18878 if H = Typ then
18879 Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
18880 else
18881 while Present (H)
18882 and then Homonym (H) /= Typ
18883 loop
18884 H := Homonym (Typ);
18885 end loop;
18886
18887 Set_Homonym (H, Homonym (Typ));
18888 end if;
18889
18890 Insert_Before (Typ_Decl, Decl);
18891 Analyze (Decl);
18892 Set_Full_View (Inc_T, Typ);
18893
18894 if Is_Tagged then
18895
18896 -- Create a common class-wide type for both views, and set the
18897 -- Etype of the class-wide type to the full view.
18898
18899 Make_Class_Wide_Type (Inc_T);
18900 Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
18901 Set_Etype (Class_Wide_Type (Typ), Typ);
18902 end if;
18903 end if;
18904 end Build_Incomplete_Type_Declaration;
18905
18906 ------------------
18907 -- Designates_T --
18908 ------------------
18909
18910 function Designates_T (Subt : Node_Id) return Boolean is
18911 Type_Id : constant Name_Id := Chars (Typ);
18912
18913 function Names_T (Nam : Node_Id) return Boolean;
18914 -- The record type has not been introduced in the current scope
18915 -- yet, so we must examine the name of the type itself, either
18916 -- an identifier T, or an expanded name of the form P.T, where
18917 -- P denotes the current scope.
18918
18919 -------------
18920 -- Names_T --
18921 -------------
18922
18923 function Names_T (Nam : Node_Id) return Boolean is
18924 begin
18925 if Nkind (Nam) = N_Identifier then
18926 return Chars (Nam) = Type_Id;
18927
18928 elsif Nkind (Nam) = N_Selected_Component then
18929 if Chars (Selector_Name (Nam)) = Type_Id then
18930 if Nkind (Prefix (Nam)) = N_Identifier then
18931 return Chars (Prefix (Nam)) = Chars (Current_Scope);
18932
18933 elsif Nkind (Prefix (Nam)) = N_Selected_Component then
18934 return Chars (Selector_Name (Prefix (Nam))) =
18935 Chars (Current_Scope);
18936 else
18937 return False;
18938 end if;
18939
18940 else
18941 return False;
18942 end if;
18943
18944 else
18945 return False;
18946 end if;
18947 end Names_T;
18948
18949 -- Start of processing for Designates_T
18950
18951 begin
18952 if Nkind (Subt) = N_Identifier then
18953 return Chars (Subt) = Type_Id;
18954
18955 -- Reference can be through an expanded name which has not been
18956 -- analyzed yet, and which designates enclosing scopes.
18957
18958 elsif Nkind (Subt) = N_Selected_Component then
18959 if Names_T (Subt) then
18960 return True;
18961
18962 -- Otherwise it must denote an entity that is already visible.
18963 -- The access definition may name a subtype of the enclosing
18964 -- type, if there is a previous incomplete declaration for it.
18965
18966 else
18967 Find_Selected_Component (Subt);
18968 return
18969 Is_Entity_Name (Subt)
18970 and then Scope (Entity (Subt)) = Current_Scope
18971 and then
18972 (Chars (Base_Type (Entity (Subt))) = Type_Id
18973 or else
18974 (Is_Class_Wide_Type (Entity (Subt))
18975 and then
18976 Chars (Etype (Base_Type (Entity (Subt)))) =
18977 Type_Id));
18978 end if;
18979
18980 -- A reference to the current type may appear as the prefix of
18981 -- a 'Class attribute.
18982
18983 elsif Nkind (Subt) = N_Attribute_Reference
18984 and then Attribute_Name (Subt) = Name_Class
18985 then
18986 return Names_T (Prefix (Subt));
18987
18988 else
18989 return False;
18990 end if;
18991 end Designates_T;
18992
18993 ----------------
18994 -- Mentions_T --
18995 ----------------
18996
18997 function Mentions_T (Acc_Def : Node_Id) return Boolean is
18998 Param_Spec : Node_Id;
18999
19000 Acc_Subprg : constant Node_Id :=
19001 Access_To_Subprogram_Definition (Acc_Def);
19002
19003 begin
19004 if No (Acc_Subprg) then
19005 return Designates_T (Subtype_Mark (Acc_Def));
19006 end if;
19007
19008 -- Component is an access_to_subprogram: examine its formals,
19009 -- and result definition in the case of an access_to_function.
19010
19011 Param_Spec := First (Parameter_Specifications (Acc_Subprg));
19012 while Present (Param_Spec) loop
19013 if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
19014 and then Mentions_T (Parameter_Type (Param_Spec))
19015 then
19016 return True;
19017
19018 elsif Designates_T (Parameter_Type (Param_Spec)) then
19019 return True;
19020 end if;
19021
19022 Next (Param_Spec);
19023 end loop;
19024
19025 if Nkind (Acc_Subprg) = N_Access_Function_Definition then
19026 if Nkind (Result_Definition (Acc_Subprg)) =
19027 N_Access_Definition
19028 then
19029 return Mentions_T (Result_Definition (Acc_Subprg));
19030 else
19031 return Designates_T (Result_Definition (Acc_Subprg));
19032 end if;
19033 end if;
19034
19035 return False;
19036 end Mentions_T;
19037
19038 -- Start of processing for Check_Anonymous_Access_Components
19039
19040 begin
19041 if No (Comp_List) then
19042 return;
19043 end if;
19044
19045 Comp := First (Component_Items (Comp_List));
19046 while Present (Comp) loop
19047 if Nkind (Comp) = N_Component_Declaration
19048 and then Present
19049 (Access_Definition (Component_Definition (Comp)))
19050 and then
19051 Mentions_T (Access_Definition (Component_Definition (Comp)))
19052 then
19053 Comp_Def := Component_Definition (Comp);
19054 Acc_Def :=
19055 Access_To_Subprogram_Definition
19056 (Access_Definition (Comp_Def));
19057
19058 Build_Incomplete_Type_Declaration;
19059 Anon_Access := Make_Temporary (Loc, 'S');
19060
19061 -- Create a declaration for the anonymous access type: either
19062 -- an access_to_object or an access_to_subprogram.
19063
19064 if Present (Acc_Def) then
19065 if Nkind (Acc_Def) = N_Access_Function_Definition then
19066 Type_Def :=
19067 Make_Access_Function_Definition (Loc,
19068 Parameter_Specifications =>
19069 Parameter_Specifications (Acc_Def),
19070 Result_Definition => Result_Definition (Acc_Def));
19071 else
19072 Type_Def :=
19073 Make_Access_Procedure_Definition (Loc,
19074 Parameter_Specifications =>
19075 Parameter_Specifications (Acc_Def));
19076 end if;
19077
19078 else
19079 Type_Def :=
19080 Make_Access_To_Object_Definition (Loc,
19081 Subtype_Indication =>
19082 Relocate_Node
19083 (Subtype_Mark
19084 (Access_Definition (Comp_Def))));
19085
19086 Set_Constant_Present
19087 (Type_Def, Constant_Present (Access_Definition (Comp_Def)));
19088 Set_All_Present
19089 (Type_Def, All_Present (Access_Definition (Comp_Def)));
19090 end if;
19091
19092 Set_Null_Exclusion_Present
19093 (Type_Def,
19094 Null_Exclusion_Present (Access_Definition (Comp_Def)));
19095
19096 Decl :=
19097 Make_Full_Type_Declaration (Loc,
19098 Defining_Identifier => Anon_Access,
19099 Type_Definition => Type_Def);
19100
19101 Insert_Before (Typ_Decl, Decl);
19102 Analyze (Decl);
19103
19104 -- If an access to subprogram, create the extra formals
19105
19106 if Present (Acc_Def) then
19107 Create_Extra_Formals (Designated_Type (Anon_Access));
19108
19109 -- If an access to object, preserve entity of designated type,
19110 -- for ASIS use, before rewriting the component definition.
19111
19112 else
19113 declare
19114 Desig : Entity_Id;
19115
19116 begin
19117 Desig := Entity (Subtype_Indication (Type_Def));
19118
19119 -- If the access definition is to the current record,
19120 -- the visible entity at this point is an incomplete
19121 -- type. Retrieve the full view to simplify ASIS queries
19122
19123 if Ekind (Desig) = E_Incomplete_Type then
19124 Desig := Full_View (Desig);
19125 end if;
19126
19127 Set_Entity
19128 (Subtype_Mark (Access_Definition (Comp_Def)), Desig);
19129 end;
19130 end if;
19131
19132 Rewrite (Comp_Def,
19133 Make_Component_Definition (Loc,
19134 Subtype_Indication =>
19135 New_Occurrence_Of (Anon_Access, Loc)));
19136
19137 if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
19138 Set_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
19139 else
19140 Set_Ekind (Anon_Access, E_Anonymous_Access_Type);
19141 end if;
19142
19143 Set_Is_Local_Anonymous_Access (Anon_Access);
19144 end if;
19145
19146 Next (Comp);
19147 end loop;
19148
19149 if Present (Variant_Part (Comp_List)) then
19150 declare
19151 V : Node_Id;
19152 begin
19153 V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
19154 while Present (V) loop
19155 Check_Anonymous_Access_Components
19156 (Typ_Decl, Typ, Prev, Component_List (V));
19157 Next_Non_Pragma (V);
19158 end loop;
19159 end;
19160 end if;
19161 end Check_Anonymous_Access_Components;
19162
19163 --------------------------------
19164 -- Preanalyze_Spec_Expression --
19165 --------------------------------
19166
19167 procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
19168 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
19169 begin
19170 In_Spec_Expression := True;
19171 Preanalyze_And_Resolve (N, T);
19172 In_Spec_Expression := Save_In_Spec_Expression;
19173 end Preanalyze_Spec_Expression;
19174
19175 -----------------------------
19176 -- Record_Type_Declaration --
19177 -----------------------------
19178
19179 procedure Record_Type_Declaration
19180 (T : Entity_Id;
19181 N : Node_Id;
19182 Prev : Entity_Id)
19183 is
19184 Def : constant Node_Id := Type_Definition (N);
19185 Is_Tagged : Boolean;
19186 Tag_Comp : Entity_Id;
19187
19188 begin
19189 -- These flags must be initialized before calling Process_Discriminants
19190 -- because this routine makes use of them.
19191
19192 Set_Ekind (T, E_Record_Type);
19193 Set_Etype (T, T);
19194 Init_Size_Align (T);
19195 Set_Interfaces (T, No_Elist);
19196 Set_Stored_Constraint (T, No_Elist);
19197
19198 -- Normal case
19199
19200 if Ada_Version < Ada_2005
19201 or else not Interface_Present (Def)
19202 then
19203 if Limited_Present (Def) then
19204 Check_SPARK_Restriction ("limited is not allowed", N);
19205 end if;
19206
19207 if Abstract_Present (Def) then
19208 Check_SPARK_Restriction ("abstract is not allowed", N);
19209 end if;
19210
19211 -- The flag Is_Tagged_Type might have already been set by
19212 -- Find_Type_Name if it detected an error for declaration T. This
19213 -- arises in the case of private tagged types where the full view
19214 -- omits the word tagged.
19215
19216 Is_Tagged :=
19217 Tagged_Present (Def)
19218 or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
19219
19220 Set_Is_Tagged_Type (T, Is_Tagged);
19221 Set_Is_Limited_Record (T, Limited_Present (Def));
19222
19223 -- Type is abstract if full declaration carries keyword, or if
19224 -- previous partial view did.
19225
19226 Set_Is_Abstract_Type (T, Is_Abstract_Type (T)
19227 or else Abstract_Present (Def));
19228
19229 else
19230 Check_SPARK_Restriction ("interface is not allowed", N);
19231
19232 Is_Tagged := True;
19233 Analyze_Interface_Declaration (T, Def);
19234
19235 if Present (Discriminant_Specifications (N)) then
19236 Error_Msg_N
19237 ("interface types cannot have discriminants",
19238 Defining_Identifier
19239 (First (Discriminant_Specifications (N))));
19240 end if;
19241 end if;
19242
19243 -- First pass: if there are self-referential access components,
19244 -- create the required anonymous access type declarations, and if
19245 -- need be an incomplete type declaration for T itself.
19246
19247 Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
19248
19249 if Ada_Version >= Ada_2005
19250 and then Present (Interface_List (Def))
19251 then
19252 Check_Interfaces (N, Def);
19253
19254 declare
19255 Ifaces_List : Elist_Id;
19256
19257 begin
19258 -- Ada 2005 (AI-251): Collect the list of progenitors that are not
19259 -- already in the parents.
19260
19261 Collect_Interfaces
19262 (T => T,
19263 Ifaces_List => Ifaces_List,
19264 Exclude_Parents => True);
19265
19266 Set_Interfaces (T, Ifaces_List);
19267 end;
19268 end if;
19269
19270 -- Records constitute a scope for the component declarations within.
19271 -- The scope is created prior to the processing of these declarations.
19272 -- Discriminants are processed first, so that they are visible when
19273 -- processing the other components. The Ekind of the record type itself
19274 -- is set to E_Record_Type (subtypes appear as E_Record_Subtype).
19275
19276 -- Enter record scope
19277
19278 Push_Scope (T);
19279
19280 -- If an incomplete or private type declaration was already given for
19281 -- the type, then this scope already exists, and the discriminants have
19282 -- been declared within. We must verify that the full declaration
19283 -- matches the incomplete one.
19284
19285 Check_Or_Process_Discriminants (N, T, Prev);
19286
19287 Set_Is_Constrained (T, not Has_Discriminants (T));
19288 Set_Has_Delayed_Freeze (T, True);
19289
19290 -- For tagged types add a manually analyzed component corresponding
19291 -- to the component _tag, the corresponding piece of tree will be
19292 -- expanded as part of the freezing actions if it is not a CPP_Class.
19293
19294 if Is_Tagged then
19295
19296 -- Do not add the tag unless we are in expansion mode
19297
19298 if Expander_Active then
19299 Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
19300 Enter_Name (Tag_Comp);
19301
19302 Set_Ekind (Tag_Comp, E_Component);
19303 Set_Is_Tag (Tag_Comp);
19304 Set_Is_Aliased (Tag_Comp);
19305 Set_Etype (Tag_Comp, RTE (RE_Tag));
19306 Set_DT_Entry_Count (Tag_Comp, No_Uint);
19307 Set_Original_Record_Component (Tag_Comp, Tag_Comp);
19308 Init_Component_Location (Tag_Comp);
19309
19310 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
19311 -- implemented interfaces.
19312
19313 if Has_Interfaces (T) then
19314 Add_Interface_Tag_Components (N, T);
19315 end if;
19316 end if;
19317
19318 Make_Class_Wide_Type (T);
19319 Set_Direct_Primitive_Operations (T, New_Elmt_List);
19320 end if;
19321
19322 -- We must suppress range checks when processing record components in
19323 -- the presence of discriminants, since we don't want spurious checks to
19324 -- be generated during their analysis, but Suppress_Range_Checks flags
19325 -- must be reset the after processing the record definition.
19326
19327 -- Note: this is the only use of Kill_Range_Checks, and is a bit odd,
19328 -- couldn't we just use the normal range check suppression method here.
19329 -- That would seem cleaner ???
19330
19331 if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
19332 Set_Kill_Range_Checks (T, True);
19333 Record_Type_Definition (Def, Prev);
19334 Set_Kill_Range_Checks (T, False);
19335 else
19336 Record_Type_Definition (Def, Prev);
19337 end if;
19338
19339 -- Exit from record scope
19340
19341 End_Scope;
19342
19343 -- Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
19344 -- the implemented interfaces and associate them an aliased entity.
19345
19346 if Is_Tagged
19347 and then not Is_Empty_List (Interface_List (Def))
19348 then
19349 Derive_Progenitor_Subprograms (T, T);
19350 end if;
19351 end Record_Type_Declaration;
19352
19353 ----------------------------
19354 -- Record_Type_Definition --
19355 ----------------------------
19356
19357 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
19358 Component : Entity_Id;
19359 Ctrl_Components : Boolean := False;
19360 Final_Storage_Only : Boolean;
19361 T : Entity_Id;
19362
19363 begin
19364 if Ekind (Prev_T) = E_Incomplete_Type then
19365 T := Full_View (Prev_T);
19366 else
19367 T := Prev_T;
19368 end if;
19369
19370 -- In SPARK, tagged types and type extensions may only be declared in
19371 -- the specification of library unit packages.
19372
19373 if Present (Def) and then Is_Tagged_Type (T) then
19374 declare
19375 Typ : Node_Id;
19376 Ctxt : Node_Id;
19377
19378 begin
19379 if Nkind (Parent (Def)) = N_Full_Type_Declaration then
19380 Typ := Parent (Def);
19381 else
19382 pragma Assert
19383 (Nkind (Parent (Def)) = N_Derived_Type_Definition);
19384 Typ := Parent (Parent (Def));
19385 end if;
19386
19387 Ctxt := Parent (Typ);
19388
19389 if Nkind (Ctxt) = N_Package_Body
19390 and then Nkind (Parent (Ctxt)) = N_Compilation_Unit
19391 then
19392 Check_SPARK_Restriction
19393 ("type should be defined in package specification", Typ);
19394
19395 elsif Nkind (Ctxt) /= N_Package_Specification
19396 or else Nkind (Parent (Parent (Ctxt))) /= N_Compilation_Unit
19397 then
19398 Check_SPARK_Restriction
19399 ("type should be defined in library unit package", Typ);
19400 end if;
19401 end;
19402 end if;
19403
19404 Final_Storage_Only := not Is_Controlled (T);
19405
19406 -- Ada 2005: check whether an explicit Limited is present in a derived
19407 -- type declaration.
19408
19409 if Nkind (Parent (Def)) = N_Derived_Type_Definition
19410 and then Limited_Present (Parent (Def))
19411 then
19412 Set_Is_Limited_Record (T);
19413 end if;
19414
19415 -- If the component list of a record type is defined by the reserved
19416 -- word null and there is no discriminant part, then the record type has
19417 -- no components and all records of the type are null records (RM 3.7)
19418 -- This procedure is also called to process the extension part of a
19419 -- record extension, in which case the current scope may have inherited
19420 -- components.
19421
19422 if No (Def)
19423 or else No (Component_List (Def))
19424 or else Null_Present (Component_List (Def))
19425 then
19426 if not Is_Tagged_Type (T) then
19427 Check_SPARK_Restriction ("non-tagged record cannot be null", Def);
19428 end if;
19429
19430 else
19431 Analyze_Declarations (Component_Items (Component_List (Def)));
19432
19433 if Present (Variant_Part (Component_List (Def))) then
19434 Check_SPARK_Restriction ("variant part is not allowed", Def);
19435 Analyze (Variant_Part (Component_List (Def)));
19436 end if;
19437 end if;
19438
19439 -- After completing the semantic analysis of the record definition,
19440 -- record components, both new and inherited, are accessible. Set their
19441 -- kind accordingly. Exclude malformed itypes from illegal declarations,
19442 -- whose Ekind may be void.
19443
19444 Component := First_Entity (Current_Scope);
19445 while Present (Component) loop
19446 if Ekind (Component) = E_Void
19447 and then not Is_Itype (Component)
19448 then
19449 Set_Ekind (Component, E_Component);
19450 Init_Component_Location (Component);
19451 end if;
19452
19453 if Has_Task (Etype (Component)) then
19454 Set_Has_Task (T);
19455 end if;
19456
19457 if Ekind (Component) /= E_Component then
19458 null;
19459
19460 -- Do not set Has_Controlled_Component on a class-wide equivalent
19461 -- type. See Make_CW_Equivalent_Type.
19462
19463 elsif not Is_Class_Wide_Equivalent_Type (T)
19464 and then (Has_Controlled_Component (Etype (Component))
19465 or else (Chars (Component) /= Name_uParent
19466 and then Is_Controlled (Etype (Component))))
19467 then
19468 Set_Has_Controlled_Component (T, True);
19469 Final_Storage_Only :=
19470 Final_Storage_Only
19471 and then Finalize_Storage_Only (Etype (Component));
19472 Ctrl_Components := True;
19473 end if;
19474
19475 Next_Entity (Component);
19476 end loop;
19477
19478 -- A Type is Finalize_Storage_Only only if all its controlled components
19479 -- are also.
19480
19481 if Ctrl_Components then
19482 Set_Finalize_Storage_Only (T, Final_Storage_Only);
19483 end if;
19484
19485 -- Place reference to end record on the proper entity, which may
19486 -- be a partial view.
19487
19488 if Present (Def) then
19489 Process_End_Label (Def, 'e', Prev_T);
19490 end if;
19491 end Record_Type_Definition;
19492
19493 ------------------------
19494 -- Replace_Components --
19495 ------------------------
19496
19497 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id) is
19498 function Process (N : Node_Id) return Traverse_Result;
19499
19500 -------------
19501 -- Process --
19502 -------------
19503
19504 function Process (N : Node_Id) return Traverse_Result is
19505 Comp : Entity_Id;
19506
19507 begin
19508 if Nkind (N) = N_Discriminant_Specification then
19509 Comp := First_Discriminant (Typ);
19510 while Present (Comp) loop
19511 if Chars (Comp) = Chars (Defining_Identifier (N)) then
19512 Set_Defining_Identifier (N, Comp);
19513 exit;
19514 end if;
19515
19516 Next_Discriminant (Comp);
19517 end loop;
19518
19519 elsif Nkind (N) = N_Component_Declaration then
19520 Comp := First_Component (Typ);
19521 while Present (Comp) loop
19522 if Chars (Comp) = Chars (Defining_Identifier (N)) then
19523 Set_Defining_Identifier (N, Comp);
19524 exit;
19525 end if;
19526
19527 Next_Component (Comp);
19528 end loop;
19529 end if;
19530
19531 return OK;
19532 end Process;
19533
19534 procedure Replace is new Traverse_Proc (Process);
19535
19536 -- Start of processing for Replace_Components
19537
19538 begin
19539 Replace (Decl);
19540 end Replace_Components;
19541
19542 -------------------------------
19543 -- Set_Completion_Referenced --
19544 -------------------------------
19545
19546 procedure Set_Completion_Referenced (E : Entity_Id) is
19547 begin
19548 -- If in main unit, mark entity that is a completion as referenced,
19549 -- warnings go on the partial view when needed.
19550
19551 if In_Extended_Main_Source_Unit (E) then
19552 Set_Referenced (E);
19553 end if;
19554 end Set_Completion_Referenced;
19555
19556 ---------------------
19557 -- Set_Fixed_Range --
19558 ---------------------
19559
19560 -- The range for fixed-point types is complicated by the fact that we
19561 -- do not know the exact end points at the time of the declaration. This
19562 -- is true for three reasons:
19563
19564 -- A size clause may affect the fudging of the end-points
19565 -- A small clause may affect the values of the end-points
19566 -- We try to include the end-points if it does not affect the size
19567
19568 -- This means that the actual end-points must be established at the point
19569 -- when the type is frozen. Meanwhile, we first narrow the range as
19570 -- permitted (so that it will fit if necessary in a small specified size),
19571 -- and then build a range subtree with these narrowed bounds.
19572
19573 -- Set_Fixed_Range constructs the range from real literal values, and sets
19574 -- the range as the Scalar_Range of the given fixed-point type entity.
19575
19576 -- The parent of this range is set to point to the entity so that it is
19577 -- properly hooked into the tree (unlike normal Scalar_Range entries for
19578 -- other scalar types, which are just pointers to the range in the
19579 -- original tree, this would otherwise be an orphan).
19580
19581 -- The tree is left unanalyzed. When the type is frozen, the processing
19582 -- in Freeze.Freeze_Fixed_Point_Type notices that the range is not
19583 -- analyzed, and uses this as an indication that it should complete
19584 -- work on the range (it will know the final small and size values).
19585
19586 procedure Set_Fixed_Range
19587 (E : Entity_Id;
19588 Loc : Source_Ptr;
19589 Lo : Ureal;
19590 Hi : Ureal)
19591 is
19592 S : constant Node_Id :=
19593 Make_Range (Loc,
19594 Low_Bound => Make_Real_Literal (Loc, Lo),
19595 High_Bound => Make_Real_Literal (Loc, Hi));
19596 begin
19597 Set_Scalar_Range (E, S);
19598 Set_Parent (S, E);
19599 end Set_Fixed_Range;
19600
19601 ----------------------------------
19602 -- Set_Scalar_Range_For_Subtype --
19603 ----------------------------------
19604
19605 procedure Set_Scalar_Range_For_Subtype
19606 (Def_Id : Entity_Id;
19607 R : Node_Id;
19608 Subt : Entity_Id)
19609 is
19610 Kind : constant Entity_Kind := Ekind (Def_Id);
19611
19612 begin
19613 -- Defend against previous error
19614
19615 if Nkind (R) = N_Error then
19616 return;
19617 end if;
19618
19619 Set_Scalar_Range (Def_Id, R);
19620
19621 -- We need to link the range into the tree before resolving it so
19622 -- that types that are referenced, including importantly the subtype
19623 -- itself, are properly frozen (Freeze_Expression requires that the
19624 -- expression be properly linked into the tree). Of course if it is
19625 -- already linked in, then we do not disturb the current link.
19626
19627 if No (Parent (R)) then
19628 Set_Parent (R, Def_Id);
19629 end if;
19630
19631 -- Reset the kind of the subtype during analysis of the range, to
19632 -- catch possible premature use in the bounds themselves.
19633
19634 Set_Ekind (Def_Id, E_Void);
19635 Process_Range_Expr_In_Decl (R, Subt);
19636 Set_Ekind (Def_Id, Kind);
19637 end Set_Scalar_Range_For_Subtype;
19638
19639 --------------------------------------------------------
19640 -- Set_Stored_Constraint_From_Discriminant_Constraint --
19641 --------------------------------------------------------
19642
19643 procedure Set_Stored_Constraint_From_Discriminant_Constraint
19644 (E : Entity_Id)
19645 is
19646 begin
19647 -- Make sure set if encountered during Expand_To_Stored_Constraint
19648
19649 Set_Stored_Constraint (E, No_Elist);
19650
19651 -- Give it the right value
19652
19653 if Is_Constrained (E) and then Has_Discriminants (E) then
19654 Set_Stored_Constraint (E,
19655 Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
19656 end if;
19657 end Set_Stored_Constraint_From_Discriminant_Constraint;
19658
19659 -------------------------------------
19660 -- Signed_Integer_Type_Declaration --
19661 -------------------------------------
19662
19663 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
19664 Implicit_Base : Entity_Id;
19665 Base_Typ : Entity_Id;
19666 Lo_Val : Uint;
19667 Hi_Val : Uint;
19668 Errs : Boolean := False;
19669 Lo : Node_Id;
19670 Hi : Node_Id;
19671
19672 function Can_Derive_From (E : Entity_Id) return Boolean;
19673 -- Determine whether given bounds allow derivation from specified type
19674
19675 procedure Check_Bound (Expr : Node_Id);
19676 -- Check bound to make sure it is integral and static. If not, post
19677 -- appropriate error message and set Errs flag
19678
19679 ---------------------
19680 -- Can_Derive_From --
19681 ---------------------
19682
19683 -- Note we check both bounds against both end values, to deal with
19684 -- strange types like ones with a range of 0 .. -12341234.
19685
19686 function Can_Derive_From (E : Entity_Id) return Boolean is
19687 Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
19688 Hi : constant Uint := Expr_Value (Type_High_Bound (E));
19689 begin
19690 return Lo <= Lo_Val and then Lo_Val <= Hi
19691 and then
19692 Lo <= Hi_Val and then Hi_Val <= Hi;
19693 end Can_Derive_From;
19694
19695 -----------------
19696 -- Check_Bound --
19697 -----------------
19698
19699 procedure Check_Bound (Expr : Node_Id) is
19700 begin
19701 -- If a range constraint is used as an integer type definition, each
19702 -- bound of the range must be defined by a static expression of some
19703 -- integer type, but the two bounds need not have the same integer
19704 -- type (Negative bounds are allowed.) (RM 3.5.4)
19705
19706 if not Is_Integer_Type (Etype (Expr)) then
19707 Error_Msg_N
19708 ("integer type definition bounds must be of integer type", Expr);
19709 Errs := True;
19710
19711 elsif not Is_OK_Static_Expression (Expr) then
19712 Flag_Non_Static_Expr
19713 ("non-static expression used for integer type bound!", Expr);
19714 Errs := True;
19715
19716 -- The bounds are folded into literals, and we set their type to be
19717 -- universal, to avoid typing difficulties: we cannot set the type
19718 -- of the literal to the new type, because this would be a forward
19719 -- reference for the back end, and if the original type is user-
19720 -- defined this can lead to spurious semantic errors (e.g. 2928-003).
19721
19722 else
19723 if Is_Entity_Name (Expr) then
19724 Fold_Uint (Expr, Expr_Value (Expr), True);
19725 end if;
19726
19727 Set_Etype (Expr, Universal_Integer);
19728 end if;
19729 end Check_Bound;
19730
19731 -- Start of processing for Signed_Integer_Type_Declaration
19732
19733 begin
19734 -- Create an anonymous base type
19735
19736 Implicit_Base :=
19737 Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
19738
19739 -- Analyze and check the bounds, they can be of any integer type
19740
19741 Lo := Low_Bound (Def);
19742 Hi := High_Bound (Def);
19743
19744 -- Arbitrarily use Integer as the type if either bound had an error
19745
19746 if Hi = Error or else Lo = Error then
19747 Base_Typ := Any_Integer;
19748 Set_Error_Posted (T, True);
19749
19750 -- Here both bounds are OK expressions
19751
19752 else
19753 Analyze_And_Resolve (Lo, Any_Integer);
19754 Analyze_And_Resolve (Hi, Any_Integer);
19755
19756 Check_Bound (Lo);
19757 Check_Bound (Hi);
19758
19759 if Errs then
19760 Hi := Type_High_Bound (Standard_Long_Long_Integer);
19761 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
19762 end if;
19763
19764 -- Find type to derive from
19765
19766 Lo_Val := Expr_Value (Lo);
19767 Hi_Val := Expr_Value (Hi);
19768
19769 if Can_Derive_From (Standard_Short_Short_Integer) then
19770 Base_Typ := Base_Type (Standard_Short_Short_Integer);
19771
19772 elsif Can_Derive_From (Standard_Short_Integer) then
19773 Base_Typ := Base_Type (Standard_Short_Integer);
19774
19775 elsif Can_Derive_From (Standard_Integer) then
19776 Base_Typ := Base_Type (Standard_Integer);
19777
19778 elsif Can_Derive_From (Standard_Long_Integer) then
19779 Base_Typ := Base_Type (Standard_Long_Integer);
19780
19781 elsif Can_Derive_From (Standard_Long_Long_Integer) then
19782 Base_Typ := Base_Type (Standard_Long_Long_Integer);
19783
19784 else
19785 Base_Typ := Base_Type (Standard_Long_Long_Integer);
19786 Error_Msg_N ("integer type definition bounds out of range", Def);
19787 Hi := Type_High_Bound (Standard_Long_Long_Integer);
19788 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
19789 end if;
19790 end if;
19791
19792 -- Complete both implicit base and declared first subtype entities
19793
19794 Set_Etype (Implicit_Base, Base_Typ);
19795 Set_Size_Info (Implicit_Base, (Base_Typ));
19796 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
19797 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
19798
19799 Set_Ekind (T, E_Signed_Integer_Subtype);
19800 Set_Etype (T, Implicit_Base);
19801
19802 -- In formal verification mode, restrict the base type's range to the
19803 -- minimum allowed by RM 3.5.4, namely the smallest symmetric range
19804 -- around zero with a possible extra negative value that contains the
19805 -- subtype range. Keep Size, RM_Size and First_Rep_Item info, which
19806 -- should not be relied upon in formal verification.
19807
19808 if Strict_Alfa_Mode then
19809 declare
19810 Sym_Hi_Val : Uint;
19811 Sym_Lo_Val : Uint;
19812 Dloc : constant Source_Ptr := Sloc (Def);
19813 Lbound : Node_Id;
19814 Ubound : Node_Id;
19815 Bounds : Node_Id;
19816
19817 begin
19818 -- If the subtype range is empty, the smallest base type range
19819 -- is the symmetric range around zero containing Lo_Val and
19820 -- Hi_Val.
19821
19822 if UI_Gt (Lo_Val, Hi_Val) then
19823 Sym_Hi_Val := UI_Max (UI_Abs (Lo_Val), UI_Abs (Hi_Val));
19824 Sym_Lo_Val := UI_Negate (Sym_Hi_Val);
19825
19826 -- Otherwise, if the subtype range is not empty and Hi_Val has
19827 -- the largest absolute value, Hi_Val is non negative and the
19828 -- smallest base type range is the symmetric range around zero
19829 -- containing Hi_Val.
19830
19831 elsif UI_Le (UI_Abs (Lo_Val), UI_Abs (Hi_Val)) then
19832 Sym_Hi_Val := Hi_Val;
19833 Sym_Lo_Val := UI_Negate (Hi_Val);
19834
19835 -- Otherwise, the subtype range is not empty, Lo_Val has the
19836 -- strictly largest absolute value, Lo_Val is negative and the
19837 -- smallest base type range is the symmetric range around zero
19838 -- with an extra negative value Lo_Val.
19839
19840 else
19841 Sym_Lo_Val := Lo_Val;
19842 Sym_Hi_Val := UI_Sub (UI_Negate (Lo_Val), Uint_1);
19843 end if;
19844
19845 Lbound := Make_Integer_Literal (Dloc, Sym_Lo_Val);
19846 Ubound := Make_Integer_Literal (Dloc, Sym_Hi_Val);
19847 Set_Is_Static_Expression (Lbound);
19848 Set_Is_Static_Expression (Ubound);
19849 Analyze_And_Resolve (Lbound, Any_Integer);
19850 Analyze_And_Resolve (Ubound, Any_Integer);
19851
19852 Bounds := Make_Range (Dloc, Lbound, Ubound);
19853 Set_Etype (Bounds, Base_Typ);
19854
19855 Set_Scalar_Range (Implicit_Base, Bounds);
19856 end;
19857
19858 else
19859 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
19860 end if;
19861
19862 Set_Size_Info (T, (Implicit_Base));
19863 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
19864 Set_Scalar_Range (T, Def);
19865 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
19866 Set_Is_Constrained (T);
19867 end Signed_Integer_Type_Declaration;
19868
19869 end Sem_Ch3;